PeriodicBC.inc.hpp 8.22 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
#pragma once

#include <limits>

#include <dune/common/reservedvector.hh>
#include <dune/functions/common/functionfromcallable.hh>
#include <dune/functions/functionspacebases/interpolate.hh>
#include <dune/functions/functionspacebases/subentitydofs.hh>
#include <amdis/LinearAlgebra.hpp>

namespace AMDiS {

template <class D, class MI>
14
  template <class RB, class CB>
15
void PeriodicBC<D,MI>::
16
init(RB const& basis, CB const& /*colBasis*/)
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{
  if (!bool(hasConnectedIntersections_)) {
    hasConnectedIntersections_ = true;
    const auto& gridView = basis.gridView().grid().levelGridView(0);
    for (auto const& element : elements(gridView)) {
      for (const auto& intersection : intersections(gridView, element)) {
        if (!this->onBoundary(intersection))
          continue;

        if (!intersection.neighbor()) {
          hasConnectedIntersections_ = false;
          break;
        }
      }
    }
  }

  if (*hasConnectedIntersections_)
    initAssociations(basis);
  else
    initAssociations2(basis);
}


template <class D, class MI>
  template <class Basis>
void PeriodicBC<D,MI>::
initAssociations(Basis const& basis)
{
46
47
  using std::sqrt;
  static const auto tol = sqrt(std::numeric_limits<typename D::field_type>::epsilon());
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  periodicNodes_.clear();
  periodicNodes_.resize(basis.dimension(), false);

  auto localView = basis.localView();
  auto seDOFs = subEntityDOFs(basis);
  const auto& gridView = basis.gridView();
  for (auto const& element : elements(gridView)) {
    if (!element.hasBoundaryIntersections())
      continue;

    localView.bind(element);
    for (const auto& intersection : intersections(gridView, element)) {
      if (!this->onBoundary(intersection))
        continue;

      test_exit(intersection.neighbor(),
        "Neighbors of periodic intersection not assigned. Use a periodic Grid instead!");

      // collect DOFs of inside element on intersection
      localView.bind(intersection.inside());
      seDOFs.bind(localView, intersection.indexInInside(), 1);
      std::vector<std::size_t> insideDOFs(seDOFs.begin(), seDOFs.end());
      std::vector<MI> insideGlobalDOFs(insideDOFs.size());
      std::transform(insideDOFs.begin(), insideDOFs.end(), insideGlobalDOFs.begin(),
        [&](std::size_t localIndex) { return localView.index(localIndex); });
      auto insideCoords = coords(localView.tree(), insideDOFs);

      // collect DOFs of ouside element on intersection
      localView.bind(intersection.outside());
      seDOFs.bind(localView, intersection.indexInOutside(), 1);
      std::vector<std::size_t> outsideDOFs(seDOFs.begin(), seDOFs.end());
      auto outsideCoords = coords(localView.tree(), outsideDOFs);

      // compare mapped coords of DOFs
      assert(insideDOFs.size() == outsideDOFs.size());
      for (std::size_t i = 0; i < insideCoords.size(); ++i) {
        auto x = faceTrafo_.evaluate(insideCoords[i]);
        for (std::size_t j = 0; j < outsideCoords.size(); ++j) {
          auto const& y = outsideCoords[j];
          if ((x-y).two_norm() < tol) {
            periodicNodes_[insideGlobalDOFs[i]] = true;
            associations_[insideGlobalDOFs[i]] = localView.index(outsideDOFs[j]);
          }
        }
      }
    }
  }
}

namespace Impl
{
  template <class D>
  class CompareTol
  {
    using ctype = typename D::field_type;

  public:
    CompareTol(ctype tol)
      : tol_(tol)
    {}

    bool operator()(D const& lhs, D const& rhs) const
    {
111
      using std::abs;
112
      for (int i = 0; i < D::dimension; i++) {
113
        if (abs(lhs[i] - rhs[i]) < tol_)
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
          continue;
        return lhs[i] < rhs[i];
      }
      return false;
    }

  private:
    ctype tol_;
  };
}



template <class D, class MI>
  template <class Basis>
void PeriodicBC<D,MI>::
initAssociations2(Basis const& basis)
{
132
133
  using std::sqrt;
  static const auto tol = sqrt(std::numeric_limits<typename D::field_type>::epsilon());
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
  periodicNodes_.clear();
  periodicNodes_.resize(basis.dimension(), false);

  // Dune::ReservedVector<std::pair<EntitySeed,int>,2>
  using EntitySeed = typename Basis::GridView::Grid::template Codim<0>::EntitySeed;
  using CoordAssoc = std::map<D, std::vector<std::pair<EntitySeed,int>>, Impl::CompareTol<D>>;
  CoordAssoc coordAssoc(Impl::CompareTol<D>{tol});

  // generate periodic element pairs
  const auto& gridView = basis.gridView();
  for (auto const& element : elements(gridView)) {
    for (const auto& intersection : intersections(gridView, element)) {
      if (!this->onBoundary(intersection))
        continue;

      auto x = intersection.geometry().center();
      auto seed = intersection.inside().seed();
      coordAssoc[x].push_back({seed,intersection.indexInInside()});
      coordAssoc[faceTrafo_.evaluate(x)].push_back({seed,intersection.indexInInside()});
    }
  }

  auto localView = basis.localView();
  auto seDOFs = subEntityDOFs(basis);
  for (auto const& assoc : coordAssoc) {
    auto const& seeds = assoc.second;
    if (seeds.size() != 2)
      continue;

    // collect DOFs of inside element on intersection
    auto el1 = gridView.grid().entity(seeds[0].first);
    localView.bind(el1);
    seDOFs.bind(localView, seeds[0].second, 1);
    std::vector<std::size_t> insideDOFs(seDOFs.begin(), seDOFs.end());
    std::vector<MI> insideGlobalDOFs(insideDOFs.size());
    std::transform(insideDOFs.begin(), insideDOFs.end(), insideGlobalDOFs.begin(),
      [&](std::size_t localIndex) { return localView.index(localIndex); });
    auto insideCoords = coords(localView.tree(), insideDOFs);

    // collect DOFs of ouside element on intersection
    auto el2 = gridView.grid().entity(seeds[1].first);
    localView.bind(el2);
    seDOFs.bind(localView, seeds[1].second, 1);
    std::vector<std::size_t> outsideDOFs(seDOFs.begin(), seDOFs.end());
    auto outsideCoords = coords(localView.tree(), outsideDOFs);

    // compare mapped coords of DOFs
    assert(insideDOFs.size() == outsideDOFs.size());
    for (std::size_t i = 0; i < insideCoords.size(); ++i) {
      auto x = faceTrafo_.evaluate(insideCoords[i]);
      for (std::size_t j = 0; j < outsideCoords.size(); ++j) {
        auto const& y = outsideCoords[j];
        if ((x-y).two_norm() < tol) {
          periodicNodes_[insideGlobalDOFs[i]] = true;
          associations_[insideGlobalDOFs[i]] = localView.index(outsideDOFs[j]);
        }
      }
    }
  }
}


template <class D, class MI>
  template <class Node>
std::vector<D> PeriodicBC<D,MI>::
coords(Node const& tree, std::vector<std::size_t> const& localIndices) const
{
  std::vector<D> dofCoords(localIndices.size());
202
  for_each_leaf_node(tree, [&](auto const& node, auto const& tp)
203
204
205
206
207
  {
    std::size_t size = node.finiteElement().size();
    auto geometry = node.element().geometry();

    auto const& localInterpol = node.finiteElement().localInterpolation();
208
    using FiniteElement = TYPEOF(node.finiteElement());
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
    using DomainType = typename FiniteElement::Traits::LocalBasisType::Traits::DomainType;
    using RangeType = typename FiniteElement::Traits::LocalBasisType::Traits::RangeType;

    std::array<std::vector<typename RangeType::field_type>, D::dimension> coeffs;
    for (int d = 0; d < D::dimension; ++d) {
      auto evalCoord = [&](DomainType const& local) -> RangeType { return geometry.global(local)[d]; };
      using FFC = Dune::Functions::FunctionFromCallable<RangeType(DomainType), decltype(evalCoord)>;
      localInterpol.interpolate(FFC(evalCoord), coeffs[d]);
    }

    for (std::size_t j = 0; j < localIndices.size(); ++j) {
      D x;
      for (std::size_t i = 0; i < size; ++i) {
        if (node.localIndex(i) == localIndices[j]) {
          for (int d = 0; d < D::dimension; ++d)
            x[d] = coeffs[d][i];
          break;
        }
      }
      dofCoords[j] = std::move(x);
    }
  });

  return dofCoords;
}


template <class D, class MI>
237
  template <class Mat, class Sol, class Rhs, class RN, class RTP, class CN, class CTP>
238
void PeriodicBC<D,MI>::
239
fillBoundaryCondition(Mat& matrix, Sol& solution, Rhs& rhs, RN const& rowNode, RTP rowTreePath, CN const& colNode, CTP colTreePath)
240
{
241
  Constraints<Mat>::periodicBC(matrix, periodicNodes_, associations_);
242
243
244
245
246

  for (auto const& a : associations_) {
    rhs[a.second] += rhs[a.first];
    solution[a.second] = solution[a.first];
  }
247
  Dune::Functions::interpolate(*rhs.basis(), rhs, [](auto const&) { return 0.0; }, periodicNodes_);
248
249
250
}

} // end namespace AMDiS