FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
projection_filter.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
9#include <kernel/geometry/atlas/surface_mesh.hpp>
10#include <kernel/geometry/cgal.hpp>
11#include <kernel/geometry/index_set.hpp>
12#include <kernel/geometry/mesh_part.hpp>
13#include <kernel/geometry/vertex_set.hpp>
14#include <kernel/lafem/base.hpp>
15#include <kernel/lafem/dense_vector_blocked.hpp>
16#include <kernel/lafem/unit_filter_blocked.hpp>
17#include <kernel/shape.hpp>
18#include <kernel/space/dof_mapping_renderer.hpp>
19#include <kernel/util/dist.hpp>
20#include <kernel/util/memory_arbiter.hpp>
21#include <kernel/util/tiny_algebra.hpp>
22
23namespace FEAT::LAFEM
24{
25 namespace Intern
26 {
28 template<typename DataType_>
30 {
31 template<int dim>
33
34 template<int dim_>
35 static Vec<dim_> on_segment(const Vec<dim_>& a, const Vec<dim_>& b, const Vec<dim_>& p)
36 {
37 const Vec<dim_> ab = b - a;
38 const Vec<dim_> ap = p - a;
39
40 const DataType_ alpha = Math::clamp(Tiny::dot(ab, ap) / Tiny::dot(ab, ab), 0.0, 1.0);
41 return a + alpha * ab;
42 }
43
44 static Vec<3> on_triangle(const Vec<3>& a, const Vec<3>& b, const Vec<3>& c, const Vec<3>& p)
45 {
46 // https://stackoverflow.com/a/74395029
47 const Vec<3> ab = b - a;
48 const Vec<3> ac = c - a;
49 const Vec<3> ap = p - a;
50
51 const DataType_ d1 = dot(ab, ap);
52 const DataType_ d2 = dot(ac, ap);
53 if (d1 <= 0.0 && d2 <= 0.0)
54 {
55 return a; //#1
56 }
57
58 const Vec<3> bp = p - b;
59 const DataType_ d3 = dot(ab, bp);
60 const DataType_ d4 = dot(ac, bp);
61 if (d3 >= 0.0 && d4 <= d3)
62 {
63 return b; //#2
64 }
65
66 const Vec<3> cp = p - c;
67 const DataType_ d5 = dot(ab, cp);
68 const DataType_ d6 = dot(ac, cp);
69 if (d6 >= 0.0 && d5 <= d6)
70 {
71 return c; //#3
72 }
73
74 const DataType_ vc = (d1 * d4) - (d3 * d2);
75 if (vc <= 0.0 && d1 >= 0.0 && d3 <= 0.0)
76 {
77 const DataType_ v = d1 / (d1 - d3);
78 return a + v * ab; //#4
79 }
80
81 const DataType_ vb = (d5 * d2) - (d1 * d6);
82 if (vb <= 0.0 && d2 >= 0.0 && d6 <= 0.0)
83 {
84 const DataType_ v = d2 / (d2 - d6);
85 return a + v * ac; //#5
86 }
87
88 const DataType_ va = (d3 * d6) - (d5 * d4);
89 if (va <= 0.0 && (d4 - d3) >= 0.0 && (d5 - d6) >= 0.0)
90 {
91 const DataType_ v = (d4 - d3) / ((d4 - d3) + (d5 - d6));
92 return b + v * (c - b); //#6
93 }
94
95 const DataType_ denom = 1.0 / (va + vb + vc);
96 const DataType_ v = vb * denom;
97 const DataType_ w = vc * denom;
98 return a + v * ab + w * ac; //#0
99 }
100 };
101
103 template<typename FacetShape_, typename MeshType_>
105 {
106 using MeshType = MeshType_;
107
108 using VertexType = typename MeshType_::VertexType;
109
110 static VertexType on_facet(const MeshType& mesh, Index facet, const VertexType& p);
111 };
112
113 template<typename MeshType_>
114 struct ClosestPoint<Shape::Vertex, MeshType_>
115 {
116 using MeshType = MeshType_;
117
118 using VertexType = typename MeshType_::VertexType;
119
120 static VertexType on_facet(const MeshType& mesh, Index facet, const VertexType& /*p*/)
121 {
122 return mesh.get_vertex_set()[facet];
123 }
124 };
125
126 template<typename MeshType_>
127 struct ClosestPoint<Shape::Hypercube<1>, MeshType_>
128 {
129 using MeshType = MeshType_;
130
131 using VertexType = typename MeshType_::VertexType;
132
133 static VertexType on_facet(const MeshType& mesh, Index facet, const VertexType& p)
134 {
135 const auto& vertices = mesh.get_vertex_set();
136 const auto& indices = mesh.template get_index_set<1, 0>();
137 const VertexType a = vertices[indices(facet, 0)];
138 const VertexType b = vertices[indices(facet, 1)];
140 }
141 };
142
143 template<typename MeshType_>
144 struct ClosestPoint<Shape::Simplex<1>, MeshType_>
145 {
146 using MeshType = MeshType_;
147
148 using VertexType = typename MeshType_::VertexType;
149
150 static VertexType on_facet(const MeshType& mesh, Index facet, const VertexType& p)
151 {
152 const auto& vertices = mesh.get_vertex_set();
153 const auto& indices = mesh.template get_index_set<1, 0>();
154 const VertexType a = vertices[indices(facet, 0)];
155 const VertexType b = vertices[indices(facet, 1)];
157 }
158 };
159
160 template<typename MeshType_>
161 struct ClosestPoint<Shape::Simplex<2>, MeshType_>
162 {
163 using MeshType = MeshType_;
164
165 using VertexType = typename MeshType_::VertexType;
166
167 static VertexType on_facet(const MeshType& mesh, Index facet, const VertexType& p)
168 {
169 const auto& vertices = mesh.get_vertex_set();
170 const auto& indices = mesh.template get_index_set<2, 0>();
171 const VertexType a = vertices[indices(facet, 0)];
172 const VertexType b = vertices[indices(facet, 1)];
173 const VertexType c = vertices[indices(facet, 2)];
175 }
176 };
177
178 template<typename MeshType_>
179 struct ClosestPoint<Shape::Hypercube<2>, MeshType_>
180 {
181 using MeshType = MeshType_;
182
183 using VertexType = typename MeshType_::VertexType;
184
185 static VertexType on_facet(const MeshType& mesh, Index facet, const VertexType& p)
186 {
187 const auto& vertices = mesh.get_vertex_set();
188 const auto& indices = mesh.template get_index_set<2, 0>();
189 const VertexType a = vertices[indices(facet, 0)];
190 const VertexType b = vertices[indices(facet, 1)];
191 const VertexType c = vertices[indices(facet, 2)];
192 const VertexType d = vertices[indices(facet, 3)];
193
196
197 if((p1 - p).norm_euclid_sqr() < (p2 - p).norm_euclid_sqr())
198 {
199 return p1;
200 }
201 else
202 {
203 return p2;
204 }
205 }
206 };
207
209 template<typename MeshType_, int dim_ = MeshType_::world_dim - 1>
210 typename MeshType_::VertexType project_onto_mesh_part(
211 const MeshType_& mesh,
212 const Geometry::MeshPart<MeshType_>& mesh_part,
213 const typename MeshType_::VertexType& p)
214 {
215 using DataType = typename MeshType_::CoordType;
216 using VertexType = typename MeshType_::VertexType;
217
219
220 const auto& vertices = mesh.get_vertex_set();
221
222 VertexType closest_point = vertices[0];
223 if constexpr (dim_ > 0)
224 {
225 closest_point = project_onto_mesh_part<MeshType_, dim_ - 1>(mesh, mesh_part, p);
226 }
227
228 const auto& target_set = mesh_part.template get_target_set<dim_>();
229 DataType distance_to_closest = (p - closest_point).norm_euclid_sqr();
230 for(Index i(0); i < mesh_part.get_num_entities(dim_); i++)
231 {
232 VertexType candidate = ClosestPoint<FacetShape, MeshType_>::on_facet(mesh, target_set[i], p);
233 DataType distance = (p - candidate).norm_euclid_sqr();
234 if(distance < distance_to_closest)
235 {
236 closest_point = candidate;
237 distance_to_closest = distance;
238 }
239 }
240
241 return closest_point;
242 }
243 }
244
246 template<typename MeshType_, typename DT_, typename IT_>
248 {
249 public:
251 using DataType = DT_;
252
254 using IndexType = IT_;
255
257 using MeshType = MeshType_;
258
261
262 static constexpr int BlockSize = MeshType::world_dim;
263
264 static constexpr std::array<Index, 4> empty_mesh_part_size = {{0, 0, 0, 0}};
265
268
271
272 static_assert(BlockSize > 1, "BlockSize has to be >= 2 in ProjectionFilter!");
273
275 enum class EntryType
276 {
277 Boundary, //< DoF must be projected onto boundary
278 Feature, //< DoF must be projected onto feature
279 Fixed, //< DoF must be set to zero
280 };
281
284 {
291
294 {
295 }
296 };
297
298 private:
299 const MeshType* _mesh;
300
303
306
308 std::vector<FilterEntry> _entries;
309
310 public:
311
313 ProjectionFilter() = default;
314
316 ProjectionFilter(const MeshType* m, MeshPartType&& neumann_boundary, MeshPartType&& feature_network) :
317 _mesh(m),
318 _neumann_zero_boundary(std::move(neumann_boundary)),
319 _feature_network(std::move(feature_network))
320 {
321 }
322
325
328
330 ProjectionFilter(const ProjectionFilter& other) = delete;
331
334
335 virtual ~ProjectionFilter() = default;
336
341 {
342 ProjectionFilter other;
343 other.clone(*this, clone_mode);
344 return other;
345 }
346
348 void clone(const ProjectionFilter& other, CloneMode /*clone_mode*/ = CloneMode::Deep)
349 {
350 if(*this != other)
351 {
352 _mesh = other._mesh;
353 _neumann_zero_boundary = other._neumann_zero_boundary.clone();
354 _feature_network = other._feature_network.clone();
355 _entries = other._entries;
356 }
357 }
358
360 std::size_t bytes() const
361 {
362 return _feature_network.bytes() + _neumann_zero_boundary.bytes() + _entries.size() * sizeof(FilterEntry);
363 }
364
377 void add(Index dof, ValueType position, EntryType type)
378 {
379 _entries.emplace_back(dof, position, type);
380 }
381
383 void add_boundary_dof(Index dof, ValueType position)
384 {
385 _entries.emplace_back(dof, position, EntryType::Boundary);
386 }
387
389 void add_feature_dof(Index dof, ValueType position)
390 {
391 _entries.emplace_back(dof, position, EntryType::Feature);
392 }
393
395 void add_fixed_dof(Index dof, ValueType position)
396 {
397 _entries.emplace_back(dof, position, EntryType::Fixed);
398 }
399
401 void filter_rhs(VectorType& vector) const
402 {
403 _filter(vector);
404 }
405
407 void filter_sol(VectorType& vector) const
408 {
409 _filter(vector);
410 }
411
413 void filter_def(VectorType& vector) const
414 {
415 _filter(vector);
416 }
417
419 void filter_cor(VectorType& vector) const
420 {
421 _filter(vector);
422 }
423
424 private:
425 void _filter(VectorType& vector) const
426 {
427 Memory::TypedView<ValueType> view = vector.elements_view_rw();
428 for(auto& [dof, position, type] : _entries)
429 {
430 ValueType velocity = view[dof];
431
432 DataType speed = velocity.norm_euclid();
433
434 DataType scale = 0.01 / speed;
435
436 // Add velocity to location
437 ValueType displaced = position + scale * velocity;
438
439 // Project onto surface
440 ValueType projected;
441 switch(type)
442 {
443 case EntryType::Boundary:
444 projected = Intern::project_onto_mesh_part(*_mesh, _neumann_zero_boundary, displaced);
445 break;
446 case EntryType::Feature:
447 projected = Intern::project_onto_mesh_part(*_mesh, _feature_network, displaced);
448 break;
449 case EntryType::Fixed:
450 projected = position;
451 break;
452 }
453
454 // Determine new velocity
455 view[dof] = (projected - position) * (1.0 / scale);
456 }
457 }
458
459 };
460} // namespace FEAT::LAFEM
FEAT Kernel base header.
Class template for partial meshes.
Definition: mesh_part.hpp:90
Index get_num_entities(int dim) const
Returns the number of entities.
Definition: mesh_part.hpp:311
Blocked Dense data vector class template.
Enforces homogenous Neumann boundary conditions in normal direction by projecting onto the boundary.
ProjectionFilter(const ProjectionFilter &other)=delete
Copy constructor.
ProjectionFilter & operator=(const ProjectionFilter &other)=delete
Copy constructor.
void filter_sol(VectorType &vector) const
Applies the filter onto the solution vector.
void add(Index dof, ValueType position, EntryType type)
Add a DoF to the filter.
ProjectionFilter & operator=(ProjectionFilter &&other)=default
Move-assignment.
void filter_rhs(VectorType &vector) const
Applies the filter onto the right-hand-side vector.
ProjectionFilter clone(CloneMode clone_mode=CloneMode::Deep)
Create a clone of this filter.
std::vector< FilterEntry > _entries
Filter entries.
void add_fixed_dof(Index dof, ValueType position)
Add a fixed DoF. See ProjectionFilter::add for details.
void filter_def(VectorType &vector) const
Applies the filter onto a defect vector.
void filter_cor(VectorType &vector) const
Applies the filter onto a correction vector.
Geometry::MeshPart< MeshType_ > _feature_network
Features to project onto.
void add_boundary_dof(Index dof, ValueType position)
Add a boundary DoF. See ProjectionFilter::add for details.
void clone(const ProjectionFilter &other, CloneMode=CloneMode::Deep)
Clone a ProjectionFiler. This is always a deep clone.
MeshType_ MeshType
Mesh type this filter works on.
void add_feature_dof(Index dof, ValueType position)
Add a feature DoF. See ProjectionFilter::add for details.
ProjectionFilter(ProjectionFilter &&other)=default
Move constructor.
Geometry::MeshPart< MeshType_ > _neumann_zero_boundary
Surface mesh chart to project onto.
Tiny::Vector< DataType, BlockSize > ValueType
Value type.
DenseVectorBlocked< DataType, IndexType, BlockSize > VectorType
Supported vector type.
ProjectionFilter(const MeshType *m, MeshPartType &&neumann_boundary, MeshPartType &&feature_network)
Constructor.
std::size_t bytes() const
Size of this filter in bytes.
ProjectionFilter()=default
Default constructor.
Typed memory view class.
Tiny Vector class template.
LAFEM common type definitions.
LAFEM namespace.
Definition: axpy_block.hpp:14
T_ clamp(T_ x, T_ a, T_ b)
Clamps a value to a range.
Definition: math.hpp:216
CUDA_HOST_DEVICE T_ dot(const T_ &a, const T_ &b)
Computes the dot-product of two scalars.
std::uint64_t Index
Index data type.
Helper functions for determining closest points on a segment and triangle.
Filter entry. Stores information on how to project a single DoF.
Index dof
DoF to filter.
FilterEntry(Index d, ValueType p, EntryType t)
Constructor.
ValueType position
Position of this DoF. Used as origin in projections.
EntryType type
The type of this DoF, determines how DoF is projected.
Face traits tag struct template.
Definition: shape.hpp:106