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

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

8
9
10
#include <dune/common/hybridutilities.hh>
#include <dune/functions/functionspacebases/interpolate.hh>

11
12
13
14
15
#include <amdis/Output.hpp>
#include <amdis/common/Concepts.hpp>
#include <amdis/common/ValueCategory.hpp>
#include <amdis/utility/RangeType.hpp>
#include <amdis/utility/TreeData.hpp>
16

17
18
namespace AMDiS
{
19
20
  struct BoundaryType { int b; };

21
22
  /// Implements a boundary condition of Dirichlet-type.
  /**
23
   * By calling the methods \ref init() and \ref finish before and after
24
   * assembling the system-matrix, respectively, dirichlet boundary conditions
25
   * can be applied to the matrix and system vector. Therefore, a predicate
26
   * functions indicates the DOFs where values should be enforced and a second
27
   * functor provided in the constructor is responsible for determining the
28
   * values to be set at the DOFs.
29
30
31
32
   *
   * 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
33
34
35
   * support this symmetric modification. As a result, this method returns a list
   * of columns values, that should be subtracted from the rhs.
   **/
36
  template <class Domain, class Range = double>
37
38
39
40
  class DirichletBC
  {
  public:
    template <class Predicate, class Values,
41
42
      REQUIRES(Concepts::Functor<Predicate, bool(Domain)> &&
				       Concepts::Functor<Values,  Range(Domain)>) >
43
    DirichletBC(Predicate&& predicate, Values&& values)
44
45
      : predicate_(std::forward<Predicate>(predicate))
      , values_(std::forward<Values>(values))
46
    {}
47
48


49
50
51
    /// 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.
52
53
54
55
56
    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*/)
    {
57
58
59
60
61
      if (!initialized_) {
        using RowNode = typename RowBasis::LocalView::Tree;
        initImpl(rowBasis, typename RowNode::NodeTag{});
        initialized_ = true;
      }
62
63
    }

64
65


66
67
68
69
70
71
72
    /// \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.
      **/
73
74
    template <class Matrix, class VectorX, class VectorB, class RowBasis, class ColBasis>
    void finish(Matrix& matrix,
75
                VectorX& rhs,
76
77
                VectorB& solution,
                RowBasis const& rowBasis,
78
79
                ColBasis const& colBasis)
    {
80
81
82
      test_exit_dbg(initialized_, "Boundary condition not initialized!");
      auto columns = matrix.applyDirichletBC(dirichletNodes_);

83
84
85
      using Dune::Functions::interpolate;
      Dune::Hybrid::ifElse(std::is_same<RangeType_t<typename RowBasis::LocalView::Tree>, Range>{},
        [&](auto id) {
86
          interpolate(id(rowBasis), rhs, values_, dirichletNodes_);
87
88
89
90
        });

      Dune::Hybrid::ifElse(std::is_same<RangeType_t<typename ColBasis::LocalView::Tree>, Range>{},
        [&](auto id) {
91
          interpolate(id(colBasis), solution, values_, dirichletNodes_);
92
        });
93
94

      // subtract columns of dirichlet nodes from rhs
95
96
      // for (auto const& triplet : columns)
      //   rhs[triplet.row] -= triplet.value * solution[triplet.col];
97
      initialized_ = false;
98
    }
99
100
101
102
103
104
105
106

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

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

107
108
109
    template <class RowBasis>
    void initImpl(RowBasis const& rowBasis, Dune::TypeTree::CompositeNodeTag);

110
111
    template <class RB, class RowNodeTag>
    void initImpl(RB const&, RowNodeTag) {}
112

113
  private:
114
115
    std::function<bool(Domain)> predicate_;
    std::function<Range(Domain)> values_;
116

117
118
    bool initialized_ = false;
    std::vector<char> dirichletNodes_;
119
120
  };

121
122
123
124
125
126

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

127
    template <class RowNode, class ColNode>
128
    using type = std::list< std::shared_ptr<DirichletBC<WorldVector, RangeType_t<RowNode>>> >;
129
130
131
132
133
  };

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

134
135
136
} // end namespace AMDiS

#include "DirichletBC.inc.hpp"