DOLFIN
DOLFIN C++ interface
DirichletBC.h
1 // Copyright (C) 2007-2012 Anders Logg 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 Kristian Oelgaard, 2007
19 // Modified by Johan Hake, 2009
20 // Modified by Joachim B Haga, 2012
21 // Modified by Mikael Mortensen, 2014
22 //
23 // First added: 2007-04-10
24 // Last changed: 2014-01-23
25 //
26 // FIXME: This class needs some cleanup, in particular collecting
27 // all data from different representations into a common
28 // data structure (perhaps an std::vector<std::size_t> with
29 // facet indices).
30 
31 #ifndef __DIRICHLET_BC_H
32 #define __DIRICHLET_BC_H
33 
34 #include <map>
35 #include <set>
36 #include <string>
37 #include <vector>
38 #include <boost/multi_array.hpp>
39 #include <memory>
40 #include <unordered_map>
41 
42 #include <dolfin/common/types.h>
43 #include <dolfin/common/Hierarchical.h>
44 #include <dolfin/common/MPI.h>
45 #include <dolfin/common/Variable.h>
46 
47 namespace dolfin
48 {
49 
50  class GenericFunction;
51  class FunctionSpace;
52  class Facet;
53  class GenericMatrix;
54  class GenericVector;
55  class SubDomain;
56  template<typename T> class MeshFunction;
57 
59 
123 
124  class DirichletBC : public Hierarchical<DirichletBC>, public Variable
125  {
126 
127  public:
128 
130  typedef std::unordered_map<std::size_t, double> Map;
131 
144  DirichletBC(std::shared_ptr<const FunctionSpace> V,
145  std::shared_ptr<const GenericFunction> g,
146  std::shared_ptr<const SubDomain> sub_domain,
147  std::string method="topological",
148  bool check_midpoint=true);
149 
163  DirichletBC(std::shared_ptr<const FunctionSpace> V,
164  std::shared_ptr<const GenericFunction> g,
165  std::shared_ptr<const MeshFunction<std::size_t>> sub_domains,
166  std::size_t sub_domain,
167  std::string method="topological");
168 
169  // TODO: Remove/deprecate this function
181  DirichletBC(std::shared_ptr<const FunctionSpace> V,
182  std::shared_ptr<const GenericFunction> g,
183  std::size_t sub_domain,
184  std::string method="topological");
185 
198  DirichletBC(std::shared_ptr<const FunctionSpace> V,
199  std::shared_ptr<const GenericFunction> g,
200  const std::vector<std::size_t>& markers,
201  std::string method="topological");
202 
207  DirichletBC(const DirichletBC& bc);
208 
210  ~DirichletBC();
211 
216  const DirichletBC& operator= (const DirichletBC& bc);
217 
222  void apply(GenericMatrix& A) const;
223 
228  void apply(GenericVector& b) const;
229 
236  void apply(GenericMatrix& A, GenericVector& b) const;
237 
244  void apply(GenericVector& b, const GenericVector& x) const;
245 
254  void apply(GenericMatrix& A, GenericVector& b,
255  const GenericVector& x) const;
256 
265  void get_boundary_values(Map& boundary_values) const;
266 
273  void gather(Map& boundary_values) const;
274 
280  void zero(GenericMatrix& A) const;
281 
293  double diag_val=0) const;
294 
300  const std::vector<std::size_t>& markers() const;
301 
306  std::shared_ptr<const FunctionSpace> function_space() const
307  { return _function_space; }
308 
313  std::shared_ptr<const GenericFunction> value() const;
314 
319  std::shared_ptr<const SubDomain> user_sub_domain() const;
320 
325  void set_value(std::shared_ptr<const GenericFunction> g);
326 
328  void homogenize();
329 
335  std::string method() const;
336 
340  {
341  Parameters p("dirichlet_bc");
342  p.add("use_ident", true);
343  p.add("check_dofmap_range", true);
344  return p;
345  }
346 
347  private:
348 
349  class LocalData;
350 
351  // Apply boundary conditions, common method
352  void apply(GenericMatrix* A, GenericVector* b,
353  const GenericVector* x) const;
354 
355  // Check input data to constructor
356  void check() const;
357 
358  // Initialize facets (from sub domain, mesh, etc)
359  void init_facets(const MPI_Comm mpi_comm) const;
360 
361  // Initialize sub domain markers from sub domain
362  void
363  init_from_sub_domain(std::shared_ptr<const SubDomain> sub_domain) const;
364 
365  // Initialize sub domain markers from MeshFunction
366  void init_from_mesh_function(const MeshFunction<std::size_t>& sub_domains,
367  std::size_t sub_domain) const;
368 
369  // Initialize sub domain markers from mesh
370  void init_from_mesh(std::size_t sub_domain) const;
371 
372  // Compute dofs and values for application of boundary conditions
373  // using given method
374  void compute_bc(Map& boundary_values, LocalData& data,
375  std::string method) const;
376 
377  // Compute boundary values for facet (topological approach)
378  void compute_bc_topological(Map& boundary_values,
379  LocalData& data) const;
380 
381  // Compute boundary values for facet (geometrical approach)
382  void compute_bc_geometric(Map& boundary_values,
383  LocalData& data) const;
384 
385  // Compute boundary values for facet (pointwise approach)
386  void compute_bc_pointwise(Map& boundary_values,
387  LocalData& data) const;
388 
389  // Check if the point is in the same plane as the given facet
390  bool on_facet(const double* coordinates, const Facet& facet) const;
391 
392  // Check arguments for compatibility of tensors and dofmap,
393  // dim is means an axis to which bc applies
394  void check_arguments(GenericMatrix* A, GenericVector* b,
395  const GenericVector* x,
396  std::size_t dim) const;
397 
398  // The function space (possibly a sub function space)
399  std::shared_ptr<const FunctionSpace> _function_space;
400 
401  // The function
402  std::shared_ptr<const GenericFunction> _g;
403 
404  // Search method
405  std::string _method;
406 
407  // Possible search methods
408  static const std::set<std::string> methods;
409 
410  public:
411 
412  // User defined sub domain
413  std::shared_ptr<const SubDomain> _user_sub_domain;
414 
415  private:
416 
417  // Cached number of bc dofs, used for memory allocation on second use
418  mutable std::size_t _num_dofs;
419 
420  // Boundary facets, stored by facet index (local to process)
421  mutable std::vector<std::size_t> _facets;
422 
423  // Cells attached to boundary, stored by cell index with map to
424  // local dof number
425  mutable std::map<std::size_t, std::vector<std::size_t>>
426  _cells_to_localdofs;
427 
428  // User defined mesh function
429  std::shared_ptr<const MeshFunction<std::size_t>> _user_mesh_function;
430 
431  // User defined sub domain marker for mesh or mesh function
432  std::size_t _user_sub_domain_marker;
433 
434  // Flag for whether midpoints should be checked
435  bool _check_midpoint;
436 
437  // Local data for application of boundary conditions
438  class LocalData
439  {
440  public:
441 
442  // Constructor
443  LocalData(const FunctionSpace& V);
444 
445  // Coefficients
446  std::vector<double> w;
447 
448  // Facet dofs
449  std::vector<std::size_t> facet_dofs;
450 
451  // Coordinates for dofs
452  boost::multi_array<double, 2> coordinates;
453 
454  };
455 
456 
457  };
458 
459 }
460 
461 #endif
void get_boundary_values(Map &boundary_values) const
Definition: DirichletBC.cpp:272
Common base class for DOLFIN variables.
Definition: Variable.h:35
static Parameters default_parameters()
Definition: DirichletBC.h:339
Definition: Hierarchical.h:43
Definition: adapt.h:29
~DirichletBC()
Destructor.
Definition: DirichletBC.cpp:123
void add(std::string key)
Definition: Parameters.h:128
Definition: FunctionSpace.h:53
void gather(Map &boundary_values) const
Definition: DirichletBC.cpp:175
std::unordered_map< std::size_t, double > Map
map type used by DirichletBC
Definition: DirichletBC.h:130
std::string method() const
Definition: DirichletBC.cpp:438
void zero_columns(GenericMatrix &A, GenericVector &b, double diag_val=0) const
Definition: DirichletBC.cpp:310
const DirichletBC & operator=(const DirichletBC &bc)
Definition: DirichletBC.cpp:128
DirichletBC(std::shared_ptr< const FunctionSpace > V, std::shared_ptr< const GenericFunction > g, std::shared_ptr< const SubDomain > sub_domain, std::string method="topological", bool check_midpoint=true)
Definition: DirichletBC.cpp:68
const std::vector< std::size_t > & markers() const
Definition: DirichletBC.cpp:392
std::shared_ptr< const SubDomain > user_sub_domain() const
Definition: DirichletBC.cpp:402
void apply(GenericMatrix &A) const
Definition: DirichletBC.cpp:148
std::shared_ptr< const FunctionSpace > function_space() const
Definition: DirichletBC.h:306
Definition: Parameters.h:94
void set_value(std::shared_ptr< const GenericFunction > g)
Definition: DirichletBC.cpp:433
void zero(GenericMatrix &A) const
Definition: DirichletBC.cpp:282
This class defines a common interface for matrices.
Definition: GenericMatrix.h:46
Interface for setting (strong) Dirichlet boundary conditions.
Definition: DirichletBC.h:124
void homogenize()
Set value to 0.0.
Definition: DirichletBC.cpp:407
std::shared_ptr< const GenericFunction > value() const
Definition: DirichletBC.cpp:397
A Facet is a MeshEntity of topological codimension 1.
Definition: Facet.h:39
This class defines a common interface for vectors.
Definition: GenericVector.h:47