PeriodicBC.inc.hpp 7.81 KB
Newer Older
1
2
3
4
5
6
#pragma once

#include <limits>

#include <dune/common/reservedvector.hh>
#include <amdis/LinearAlgebra.hpp>
7
#include <amdis/functions/FunctionFromCallable.hpp>
8
9
10
11

namespace AMDiS {

template <class D, class MI>
12
  template <class RB, class CB>
13
void PeriodicBC<D,MI>::
14
init(RB const& basis, CB const& /*colBasis*/)
15
16
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
{
  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)
{
44
45
  using std::sqrt;
  static const auto tol = sqrt(std::numeric_limits<typename D::field_type>::epsilon());
46
47
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
  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];
85
          if (distance(x,y) < tol) {
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
            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
    {
109
      using std::abs;
110
      for (int i = 0; i < D::dimension; i++) {
111
        if (abs(lhs[i] - rhs[i]) < tol_)
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
          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)
{
130
131
  using std::sqrt;
  static const auto tol = sqrt(std::numeric_limits<typename D::field_type>::epsilon());
132
133
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
  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];
184
        if (distance(x,y) < tol) {
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
          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());
200
  for_each_leaf_node(tree, [&](auto const& node, auto const& tp)
201
202
203
204
205
  {
    std::size_t size = node.finiteElement().size();
    auto geometry = node.element().geometry();

    auto const& localInterpol = node.finiteElement().localInterpolation();
206
    using FiniteElement = TYPEOF(node.finiteElement());
207
208
209
210
211
    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) {
212
213
      auto evalCoord = functionFromCallable<RangeType(DomainType)>([&](DomainType const& local) -> RangeType { return geometry.global(local)[d]; });
      localInterpol.interpolate(evalCoord, coeffs[d]);
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
    }

    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>
234
  template <class Mat, class Sol, class Rhs, class RN, class RTP, class CN, class CTP>
235
void PeriodicBC<D,MI>::
236
fillBoundaryCondition(Mat& matrix, Sol& solution, Rhs& rhs, RN const& rowNode, RTP rowTreePath, CN const& colNode, CTP colTreePath)
237
{
238
  periodicBC(matrix, solution, rhs, periodicNodes_, associations_);
239
240
241
}

} // end namespace AMDiS