DirichletBC.hpp 4.92 KB
Newer Older
1
2
3
#pragma once

#include <functional>
4
#include <list>
5
6
7
#include <type_traits>
#include <vector>

8
9
10
11
12
#include <amdis/Output.hpp>
#include <amdis/common/Concepts.hpp>
#include <amdis/common/ValueCategory.hpp>
#include <amdis/utility/RangeType.hpp>
#include <amdis/utility/TreeData.hpp>
13

14
15
namespace AMDiS
{
16
17
  /// Implements a boundary condition of Dirichlet-type.
  /**
18
   * By calling the methods \ref init() and \ref finish before and after
19
   * assembling the system-matrix, respectively, dirichlet boundary conditions
20
   * can be applied to the matrix and system vector. Therefore, a predicate
21
   * functions indicates the DOFs where values should be enforced and a second
22
   * functor provided in the constructor is responsible for determining the
23
   * values to be set at the DOFs.
24
25
26
27
   *
   * In the \ref finish method the matrix is called with \ref applyDirichletBC
   * to erase the corresponding rows and columns for the DOF indices. This
   * application of boundary conditions can be symmetric if the matrix does
28
29
30
   * support this symmetric modification. As a result, this method returns a list
   * of columns values, that should be subtracted from the rhs.
   **/
31
  template <class Domain, class Range = double>
32
33
34
35
  class DirichletBC
  {
  public:
    template <class Predicate, class Values,
36
37
      REQUIRES(Concepts::Functor<Predicate, bool(Domain)> &&
				       Concepts::Functor<Values,  Range(Domain)>) >
38
    DirichletBC(Predicate&& predicate, Values&& values)
39
40
      : predicate_(std::forward<Predicate>(predicate))
      , values_(std::forward<Values>(values))
41
    {}
42
43


44
45
46
    /// Prepare the matrix, rhs and solution block for assembling dirichlet
    /// boundary conditions, e.g. collect the corresponding indices of boundary
    /// DOFS for later elimination in \ref finish.
47
48
49
50
51
    template <class Matrix, class VectorX, class VectorB, class RowBasis, class ColBasis>
    void init(Matrix& /*matrix*/, VectorX& /*rhs*/, VectorB& /*solution*/,
              RowBasis const& rowBasis,
              ColBasis const& /*colBasis*/)
    {
52
53
54
55
56
      if (!initialized_) {
        using RowNode = typename RowBasis::LocalView::Tree;
        initImpl(rowBasis, typename RowNode::NodeTag{});
        initialized_ = true;
      }
57
58
    }

59
60


61
62
63
64
65
66
67
    /// \brief Apply dirichlet BC to matrix and vector, i.e., add a unit-row
    /// to the matrix and optionally delete the corresponding matrix-column.
    /** If DBC is set for matrix block {R,C}, then \p apply_row means that
      * the block-row R is assembled, and \p apply_col means that the block-col C
      * is assembled. The \p matrix, \p rhs, and \p solution correspond to the
      * currently visited blocks of system-matrix and system-vector.
      **/
68
69
    template <class Matrix, class VectorX, class VectorB, class RowBasis, class ColBasis>
    void finish(Matrix& matrix,
70
                VectorX& rhs,
71
72
                VectorB& solution,
                RowBasis const& rowBasis,
73
74
                ColBasis const& colBasis)
    {
75
76
77
      test_exit_dbg(initialized_, "Boundary condition not initialized!");
      auto columns = matrix.applyDirichletBC(dirichletNodes_);

78
      finishImpl(matrix, rhs, solution, rowBasis, colBasis, ValueCategory_t<Range>{});
79
80

      // subtract columns of dirichlet nodes from rhs
81
82
      // for (auto const& triplet : columns)
      //   rhs[triplet.row] -= triplet.value * solution[triplet.col];
83
      initialized_ = false;
84
    }
85
86
87
88
89
90
91
92

  protected:
    template <class RowBasis>
    void initImpl(RowBasis const& rowBasis, Dune::TypeTree::LeafNodeTag);

    template <class RowBasis>
    void initImpl(RowBasis const& rowBasis, Dune::TypeTree::PowerNodeTag);

93
94
95
    template <class RowBasis>
    void initImpl(RowBasis const& rowBasis, Dune::TypeTree::CompositeNodeTag);

96
97
    template <class RB, class RowNodeTag>
    void initImpl(RB const&, RowNodeTag) {}
98

99
100
101
102
103
104
105
106
107
108
    template <class Matrix, class VectorX, class VectorB, class RowBasis, class ColBasis, class ValueCat>
    void finishImpl(Matrix& matrix, VectorX& rhs, VectorB& solution,
                    RowBasis const& rowBasis, ColBasis const& colBasis,
                    ValueCat);

    template <class Matrix, class VectorX, class VectorB, class RowBasis, class ColBasis>
    void finishImpl(Matrix& matrix, VectorX& rhs, VectorB& solution,
                    RowBasis const& rowBasis, ColBasis const& colBasis,
                    tag::unknown) {}

109
  private:
110
111
    std::function<bool(Domain)> predicate_;
    std::function<Range(Domain)> values_;
112

113
114
    bool initialized_ = false;
    std::vector<char> dirichletNodes_;
115
116
  };

117
118
119
120
121
122

  template <class GlobalBasis>
  struct DirichletData
  {
    using WorldVector = typename GlobalBasis::GridView::template Codim<0>::Geometry::GlobalCoordinate;

123
    template <class RowNode, class ColNode>
124
    using type = std::list< std::shared_ptr<DirichletBC<WorldVector, RangeType_t<RowNode>>> >;
125
126
127
128
129
  };

  template <class GlobalBasis>
  using Constraints = MatrixData<GlobalBasis, DirichletData<GlobalBasis>::template type>;

130
131
132
} // end namespace AMDiS

#include "DirichletBC.inc.hpp"