dune-mmesh (unstable)

distance.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
9#ifndef DUNE_MMESH_REMESHING_DISTANCE_HH
10#define DUNE_MMESH_REMESHING_DISTANCE_HH
11
12#include <dune/common/exceptions.hh>
13#include <dune/grid/common/partitionset.hh>
14#include <memory>
15
16namespace Dune {
17
22template <class Grid>
23class Distance {
25 static constexpr int dim = Grid::dimensionworld;
26 using ctype = typename Grid::ctype;
27 using GlobalCoordinate = FieldVector<ctype, dim>;
28 using Vertex = typename Grid::Vertex;
29 using Element = typename Grid::template Codim<0>::Entity;
30 using Facet = typename Grid::template Codim<1>::Entity;
31 using InterfaceElement =
32 typename Grid::InterfaceGrid::template Codim<0>::Entity;
33
34 public:
37
39 Distance(const Grid& grid) : grid_(&grid), initialized_(false) {}
40
42 void update() {
43 // Resize distance_ and set to high default value
44 distances_.resize(indexSet().size(dim));
45 std::fill(distances_.begin(), distances_.end(), 1e100);
46
47 // Set all interface vertices to zero and initialize queue
48
49 for (const InterfaceElement& ielement : elements(
50 grid_->interfaceGrid().leafGridView(), Partitions::interior)) {
51 // Convert to bulk facet
52 const Facet facet = grid_->entity(ielement.impl().hostEntity());
53
54 // Compute vertex distances to this facet
55 handleFacet(facet);
56 }
57
58 initialized_ = true;
59 };
60
62 bool initialized() const { return initialized_; }
63
69 ctype operator()(const Vertex& vertex) const {
70 assert(initialized_);
71 assert(indexSet().index(vertex) < size());
72 return distances_[indexSet().index(vertex)];
73 }
74
81 void set(const Vertex& vertex, ctype value) {
82 assert(indexSet().index(vertex) < size());
83 distances_[indexSet().index(vertex)] = value;
84 }
85
92 ctype operator()(const Element& element) const {
93 assert(initialized_);
94 ctype dist = 0.0;
95 for (std::size_t i = 0; i < dim + 1; ++i) {
96 const auto& vertex = element.template subEntity<dim>(i);
97 dist += distances_[indexSet().index(vertex)];
98 }
99 dist /= dim + 1;
100 return dist;
101 }
102
104 ctype operator()(const InterfaceElement& element) const { return 0.0; }
105
111 template <class Index>
112 ctype operator[](const Index& index) const {
113 assert(initialized_);
114 return distances_[index];
115 }
116
118 ctype maximum() const {
119 assert(initialized_);
120 double maximum = 0.0;
121 for (const auto& d : distances_) maximum = std::max(d, maximum);
122 return maximum;
123 }
124
126 std::size_t size() const {
127 assert(initialized_);
128 return distances_.size();
129 }
130
131 private:
133 void handleFacet(const Facet& facet) {
134 for (const auto& v :
135 vertices(grid_->leafGridView(), Partitions::interior)) {
136 ctype dist = computeDistance(v, facet);
137 ctype& currentDist = distances_[indexSet().index(v)];
138 if (dist < currentDist) currentDist = dist;
139 }
140 }
141
143 ctype computeDistance(const Vertex& v, const Facet& facet) const {
144 GlobalCoordinate vp = v.geometry().center();
145 const auto& geo = facet.geometry();
146
147 if constexpr (dim == 2) {
148 for (std::size_t i = 0; i < dim; ++i) {
149 const GlobalCoordinate& vi = geo.corner(i);
150 const GlobalCoordinate& vj = geo.corner(1 - i);
151 ctype sgn = (vi - vj) * (vp - vi);
152 if (sgn >= 0) return (vp - vi).two_norm();
153 }
154
155 Dune::Plane<GlobalCoordinate> plane(geo.corner(0), geo.corner(1));
156 return std::abs(plane.signedDistance(vp));
157 } else // dim == 3
158 {
159 // We only use an estimate for 3d.
160 ctype dist = 1e100;
161 for (std::size_t i = 0; i < dim; ++i) {
162 const GlobalCoordinate& vi = geo.corner(i);
163 dist = std::min(dist, (vp - vi).two_norm2());
164
165 const GlobalCoordinate& vj = geo.corner((i + 1) % dim);
166 dist = std::min(dist, (vp - 0.5 * (vi + vj)).two_norm2());
167 }
168
169 const GlobalCoordinate& vc = geo.center();
170 dist = std::min(dist, (vp - vc).two_norm2());
171
172 return std::sqrt(dist);
173 }
174
175 DUNE_THROW(InvalidStateException, "Compute distance failed");
176 return -1.;
177 }
178
179 const typename Grid::LeafIndexSet& indexSet() const {
180 return grid_->leafIndexSet();
181 }
182
183 std::vector<ctype> distances_;
184 const Grid* grid_;
185 bool initialized_;
186};
187
188} // end namespace Dune
189
190#endif
Class for computing the distance to the interface.
Definition: distance.hh:23
ctype operator()(const InterfaceElement &element) const
Interface element.
Definition: distance.hh:104
ctype operator()(const Element &element) const
function call operator to return distance of element (average of the vertex distances)
Definition: distance.hh:92
bool initialized() const
Return if distance has been initialized.
Definition: distance.hh:62
ctype operator[](const Index &index) const
function call operator to return distance
Definition: distance.hh:112
void update()
Update the distances of all vertices.
Definition: distance.hh:42
ctype maximum() const
return maximum distance
Definition: distance.hh:118
std::size_t size() const
return size of distances vector
Definition: distance.hh:126
Distance(const Grid &grid)
Constructor with grid reference.
Definition: distance.hh:39
Distance()
Default constructor.
Definition: distance.hh:36
void set(const Vertex &vertex, ctype value)
Set distance of vertex.
Definition: distance.hh:81
ctype operator()(const Vertex &vertex) const
function call operator to return distance of vertex
Definition: distance.hh:69
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Mar 17, 23:30, 2025)