dune-mmesh (1.4)

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 <memory>
13#include <dune/common/exceptions.hh>
14#include <dune/grid/common/partitionset.hh>
15
16namespace Dune
17{
18
23template<class Grid>
25{
27 static constexpr int dim = Grid::dimensionworld;
28 using ctype = typename Grid::ctype;
29 using GlobalCoordinate = FieldVector<ctype, dim>;
30 using Vertex = typename Grid::Vertex;
31 using Element = typename Grid::template Codim<0>::Entity;
32 using Facet = typename Grid::template Codim<1>::Entity;
33 using InterfaceElement = typename Grid::InterfaceGrid::template Codim<0>::Entity;
34
35public:
38
40 Distance(const Grid& grid)
41 : grid_( &grid ), initialized_(false)
42 {}
43
45 void update()
46 {
47 // Resize distance_ and set to high default value
48 distances_.resize( indexSet().size(dim) );
49 std::fill(distances_.begin(), distances_.end(), 1e100);
50
51 // Set all interface vertices to zero and initialize queue
52
53 for ( const InterfaceElement& ielement : elements( grid_->interfaceGrid().leafGridView(), Partitions::interior ) )
54 {
55 // Convert to bulk facet
56 const Facet facet = grid_->entity( ielement.impl().hostEntity() );
57
58 // Compute vertex distances to this facet
59 handleFacet(facet);
60 }
61
62 initialized_ = true;
63 };
64
66 bool initialized() const
67 {
68 return initialized_;
69 }
70
76 ctype operator() (const Vertex& vertex) const
77 {
78 assert(initialized_);
79 assert( indexSet().index( vertex ) < size() );
80 return distances_[ indexSet().index( vertex ) ];
81 }
82
89 void set(const Vertex& vertex, ctype value)
90 {
91 assert( indexSet().index( vertex ) < size() );
92 distances_[ indexSet().index( vertex ) ] = value;
93 }
94
100 ctype operator() (const Element& element) const
101 {
102 assert(initialized_);
103 ctype dist = 0.0;
104 for ( std::size_t i = 0; i < dim+1; ++i )
105 {
106 const auto& vertex = element.template subEntity<dim>(i);
107 dist += distances_[ indexSet().index( vertex ) ];
108 }
109 dist /= dim+1;
110 return dist;
111 }
112
114 ctype operator() (const InterfaceElement& element) const
115 {
116 return 0.0;
117 }
118
124 template< class Index >
125 ctype operator[] (const Index& index) const
126 {
127 assert(initialized_);
128 return distances_[ index ];
129 }
130
132 ctype maximum() const
133 {
134 assert(initialized_);
135 double maximum = 0.0;
136 for ( const auto& d : distances_ )
137 maximum = std::max( d, maximum );
138 return maximum;
139 }
140
142 std::size_t size() const
143 {
144 assert(initialized_);
145 return distances_.size();
146 }
147
148 private:
150 void handleFacet(const Facet& facet)
151 {
152 for (const auto& v : vertices(grid_->leafGridView(), Partitions::interior))
153 {
154 ctype dist = computeDistance(v, facet);
155 ctype &currentDist = distances_[ indexSet().index( v ) ];
156 if (dist < currentDist)
157 currentDist = dist;
158 }
159 }
160
162 ctype computeDistance(const Vertex& v, const Facet &facet) const
163 {
164 GlobalCoordinate vp = v.geometry().center();
165 const auto& geo = facet.geometry();
166
167 if constexpr (dim == 2)
168 {
169 for (std::size_t i = 0; i < dim; ++i)
170 {
171 const GlobalCoordinate& vi = geo.corner(i);
172 const GlobalCoordinate& vj = geo.corner(1-i);
173 ctype sgn = (vi - vj) * (vp - vi);
174 if (sgn >= 0)
175 return (vp - vi).two_norm();
176 }
177
178 Dune::Plane<GlobalCoordinate> plane (geo.corner(0), geo.corner(1));
179 return std::abs(plane.signedDistance(vp));
180 }
181 else // dim == 3
182 {
183 // We only use an estimate for 3d.
184 ctype dist = 1e100;
185 for (std::size_t i = 0; i < dim; ++i)
186 {
187 const GlobalCoordinate& vi = geo.corner(i);
188 dist = std::min(dist, (vp - vi).two_norm2());
189
190 const GlobalCoordinate& vj = geo.corner((i+1)%dim);
191 dist = std::min(dist, (vp - 0.5*(vi+vj)).two_norm2());
192 }
193
194 const GlobalCoordinate& vc = geo.center();
195 dist = std::min(dist, (vp - vc).two_norm2());
196
197 return std::sqrt(dist);
198 }
199
200 DUNE_THROW(InvalidStateException, "Compute distance failed");
201 return -1.;
202 }
203
204 const typename Grid::LeafIndexSet& indexSet() const
205 {
206 return grid_->leafIndexSet();
207 }
208
209 std::vector<ctype> distances_;
210 const Grid* grid_;
211 bool initialized_;
212};
213
214} // end namespace Dune
215
216#endif
Class for computing the distance to the interface.
Definition: distance.hh:25
bool initialized() const
Return if distance has been initialized.
Definition: distance.hh:66
ctype operator[](const Index &index) const
function call operator to return distance
Definition: distance.hh:125
void update()
Update the distances of all vertices.
Definition: distance.hh:45
ctype maximum() const
return maximum distance
Definition: distance.hh:132
std::size_t size() const
return size of distances vector
Definition: distance.hh:142
Distance(const Grid &grid)
Constructor with grid reference.
Definition: distance.hh:40
Distance()
Default constructor.
Definition: distance.hh:37
void set(const Vertex &vertex, ctype value)
Set distance of vertex.
Definition: distance.hh:89
ctype operator()(const Vertex &vertex) const
function call operator to return distance of vertex
Definition: distance.hh:76
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Apr 7, 22:57, 2025)