| 1 |
// Copyright (C) 2008-2015 Kent-Andre Mardal and Garth N. Wells |
| 2 |
// |
| 3 |
// This file is part of DOLFIN. |
| 4 |
// |
| 5 |
// DOLFIN is free software: you can redistribute it and/or modify |
| 6 |
// it under the terms of the GNU Lesser General Public License as published by |
| 7 |
// the Free Software Foundation, either version 3 of the License, or |
| 8 |
// (at your option) any later version. |
| 9 |
// |
| 10 |
// DOLFIN is distributed in the hope that it will be useful, |
| 11 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
// GNU Lesser General Public License for more details. |
| 14 |
// |
| 15 |
// You should have received a copy of the GNU Lesser General Public License |
| 16 |
// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
// |
| 18 |
// Modified by Anders Logg 2008-2011 |
| 19 |
// Modified by Cecile Daversin-Catty 2018 |
| 20 |
|
| 21 |
#ifndef __SYSTEM_ASSEMBLER_H |
| 22 |
#define __SYSTEM_ASSEMBLER_H |
| 23 |
|
| 24 |
#include <array> |
| 25 |
#include <map> |
| 26 |
#include <memory> |
| 27 |
#include <vector> |
| 28 |
|
| 29 |
#include "DirichletBC.h" |
| 30 |
#include "AssemblerBase.h" |
| 31 |
|
| 32 |
namespace ufc |
| 33 |
{ |
| 34 |
class cell; |
| 35 |
class cell_integral; |
| 36 |
class exterior_facet_integral; |
| 37 |
class interior_facet_integral; |
| 38 |
} |
| 39 |
|
| 40 |
namespace dolfin |
| 41 |
{ |
| 42 |
|
| 43 |
// Forward declarations |
| 44 |
template<typename T> class ArrayView; |
| 45 |
class Cell; |
| 46 |
class Facet; |
| 47 |
class Form; |
| 48 |
class GenericDofMap; |
| 49 |
class GenericMatrix; |
| 50 |
class GenericVector; |
| 51 |
template<typename T> class MeshFunction; |
| 52 |
class UFC; |
| 53 |
|
| 54 |
/// This class provides an assembler for systems of the form Ax = |
| 55 |
/// b. It differs from the default DOLFIN assembler in that it |
| 56 |
/// applies boundary conditions at the time of assembly, which |
| 57 |
/// preserves any symmetries in A. |
| 58 |
|
| 59 |
class SystemAssembler : public AssemblerBase |
| 60 |
{ |
| 61 |
public: |
| 62 |
|
| 63 |
/// Constructor |
| 64 |
SystemAssembler(std::shared_ptr<const Form> a, |
| 65 |
std::shared_ptr<const Form> L, |
| 66 |
std::vector<std::shared_ptr<const DirichletBC>> bcs); |
| 67 |
|
| 68 |
/// Constructor with multiple LHS forms, matching a common RHS form |
| 69 |
/// with BCs defined for the column space of each LHS form |
| 70 |
SystemAssembler(std::vector<std::shared_ptr<const Form>> a, |
| 71 |
std::vector<std::shared_ptr<const Form>> L, |
| 72 |
std::vector<std::vector<std::shared_ptr<const DirichletBC>>> bcs); |
| 73 |
|
| 74 |
/// FIXME: Constructor with two sets of BCs (for 2x2 system) |
| 75 |
/// Need to figure out how to wrap vector<vector<shared_ptr<TYPE>>> in SWIG |
| 76 |
SystemAssembler(std::vector<std::shared_ptr<const Form>> a, |
| 77 |
std::vector<std::shared_ptr<const Form>> L, |
| 78 |
std::vector<std::shared_ptr<const DirichletBC>> bcs0, |
| 79 |
std::vector<std::shared_ptr<const DirichletBC>> bcs1); |
| 80 |
|
| 81 |
/// Assemble system (A, b) |
| 82 |
void assemble(GenericMatrix& A, GenericVector& b); |
| 83 |
|
| 84 |
/// Assemble matrix A |
| 85 |
void assemble(GenericMatrix& A); |
| 86 |
|
| 87 |
/// Assemble vector b |
| 88 |
void assemble(GenericVector& b); |
| 89 |
|
| 90 |
/// Assemble system (A, b) for (negative) increment dx, where x = |
| 91 |
/// x0 - dx is solution to system a == -L subject to bcs. |
| 92 |
/// Suitable for use inside a (quasi-)Newton solver. |
| 93 |
void assemble(GenericMatrix& A, GenericVector& b, const GenericVector& x0); |
| 94 |
|
| 95 |
/// Assemble rhs vector b for (negative) increment dx, where x = |
| 96 |
/// x0 - dx is solution to system a == -L subject to bcs. |
| 97 |
/// Suitable for use inside a (quasi-)Newton solver. |
| 98 |
void assemble(GenericVector& b, const GenericVector& x0); |
| 99 |
|
| 100 |
/// Assemble row of matrices and associated RHS vector. |
| 101 |
void assemble(std::vector<std::shared_ptr<GenericMatrix>> A, |
| 102 |
std::vector<std::shared_ptr<GenericVector>> b); |
| 103 |
|
| 104 |
private: |
| 105 |
|
| 106 |
// Class to hold temporary data |
| 107 |
class Scratch |
| 108 |
{ |
| 109 |
public: |
| 110 |
Scratch(const Form& a, const Form& L); |
| 111 |
~Scratch(); |
| 112 |
std::array<std::vector<double>, 2> Ae; |
| 113 |
}; |
| 114 |
|
| 115 |
// Check form arity etc |
| 116 |
static void check_forms(std::vector<std::shared_ptr<const Form>> a, |
| 117 |
std::vector<std::shared_ptr<const Form>> L); |
| 118 |
|
| 119 |
// Assemble system |
| 120 |
void assemble(GenericMatrix* A, GenericVector* b, |
| 121 |
const GenericVector* x0, |
| 122 |
std::shared_ptr<const Form> a, |
| 123 |
std::shared_ptr<const Form> L, |
| 124 |
std::vector<std::vector<std::shared_ptr<const DirichletBC>>> bcs, |
| 125 |
bool integrate_rhs); |
| 126 |
|
| 127 |
// Bilinear and linear forms |
| 128 |
std::vector<std::shared_ptr<const Form>> _a; |
| 129 |
std::vector<std::shared_ptr<const Form>> _l; |
| 130 |
|
| 131 |
// Boundary conditions for each space in the forms |
| 132 |
std::vector<std::vector<std::shared_ptr<const DirichletBC>>> _bcs; |
| 133 |
|
| 134 |
static void cell_wise_assembly( |
| 135 |
std::array<GenericTensor*, 2>& tensors, |
| 136 |
std::array<UFC*, 2>& ufc, |
| 137 |
Scratch& data, |
| 138 |
const std::vector<DirichletBC::Map>& boundary_values, |
| 139 |
std::shared_ptr<const MeshFunction<std::size_t>> cell_domains, |
| 140 |
std::shared_ptr<const MeshFunction<std::size_t>> exterior_facet_domains, |
| 141 |
bool integrate_rhs); |
| 142 |
|
| 143 |
static void facet_wise_assembly( |
| 144 |
std::array<GenericTensor*, 2>& tensors, |
| 145 |
std::array<UFC*, 2>& ufc, |
| 146 |
Scratch& data, |
| 147 |
const std::vector<DirichletBC::Map>& boundary_values, |
| 148 |
std::shared_ptr<const MeshFunction<std::size_t>> cell_domains, |
| 149 |
std::shared_ptr<const MeshFunction<std::size_t>> exterior_facet_domains, |
| 150 |
std::shared_ptr<const MeshFunction<std::size_t>> interior_facet_domains); |
| 151 |
|
| 152 |
// Compute exterior facet (and possibly connected cell) |
| 153 |
// contribution |
| 154 |
static void compute_exterior_facet_tensor( |
| 155 |
std::array<std::vector<double>, 2>& Ae, |
| 156 |
std::array<UFC*, 2>& ufc, |
| 157 |
ufc::cell& ufc_cell, |
| 158 |
std::vector<double>& coordinate_dofs, |
| 159 |
const std::array<bool, 2>& tensor_required_cell, |
| 160 |
const std::array<bool, 2>& tensor_required_facet, |
| 161 |
const Cell& cell, |
| 162 |
const Facet& facet, |
| 163 |
const std::array<const ufc::cell_integral*, 2>& cell_integrals, |
| 164 |
const std::array<const ufc::exterior_facet_integral*, 2>& exterior_facet_integrals, |
| 165 |
const bool compute_cell_tensor); |
| 166 |
|
| 167 |
// Compute interior facet (and possibly connected cell) |
| 168 |
// contribution |
| 169 |
static void compute_interior_facet_tensor( |
| 170 |
std::array<UFC*, 2>& ufc, |
| 171 |
std::array<ufc::cell, 2>& ufc_cell, |
| 172 |
std::array<std::vector<double>, 2>& coordinate_dofs, |
| 173 |
const std::array<bool, 2>& tensor_required_cell, |
| 174 |
const std::array<bool, 2>& tensor_required_facet, |
| 175 |
const std::array<Cell, 2>& cell, |
| 176 |
const std::array<std::size_t, 2>& local_facet, |
| 177 |
const bool facet_owner, |
| 178 |
const std::array<const ufc::cell_integral*, 2>& cell_integrals, |
| 179 |
const std::array<const ufc::interior_facet_integral*, 2>& interior_facet_integrals, |
| 180 |
const std::array<std::size_t, 2>& matrix_size, |
| 181 |
const std::size_t vector_size, |
| 182 |
const std::array<bool, 2> compute_cell_tensor); |
| 183 |
|
| 184 |
// Modified matrix insertion for case when rhs has facet integrals |
| 185 |
// and lhs has no facet integrals |
| 186 |
static void matrix_block_add( |
| 187 |
GenericTensor& tensor, |
| 188 |
std::vector<double>& Ae, |
| 189 |
std::vector<double>& macro_A, |
| 190 |
const std::array<bool, 2>& add_local_tensor, |
| 191 |
const std::array<std::vector<ArrayView<const la_index>>, 2>& cell_dofs); |
| 192 |
|
| 193 |
static void apply_bc(double* A, double* b, |
| 194 |
const std::vector<DirichletBC::Map>& boundary_values, |
| 195 |
const ArrayView<const dolfin::la_index>& global_dofs0, |
| 196 |
const ArrayView<const dolfin::la_index>& global_dofs1); |
| 197 |
|
| 198 |
bool check_functionspace_for_bc |
| 199 |
(std::shared_ptr<const FunctionSpace> fs, std::shared_ptr<const DirichletBC> bc) const; |
| 200 |
|
| 201 |
// Return true if cell has an Dirichlet/essential boundary |
| 202 |
// condition applied |
| 203 |
static bool has_bc(const DirichletBC::Map& boundary_values, |
| 204 |
const ArrayView<const dolfin::la_index>& dofs); |
| 205 |
|
| 206 |
// Return true if element matrix is required |
| 207 |
static bool |
| 208 |
cell_matrix_required(const GenericTensor* A, |
| 209 |
const void* integral, |
| 210 |
const std::vector<DirichletBC::Map>& boundary_values, |
| 211 |
const ArrayView<const dolfin::la_index>& dofs); |
| 212 |
|
| 213 |
}; |
| 214 |
|
| 215 |
} |
| 216 |
|
| 217 |
#endif |