FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
projection_filter_assembler.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
8#include <kernel/adjacency/base.hpp>
9#include <kernel/adjacency/graph.hpp>
10#include <kernel/assembly/common_operators.hpp>
11#include <kernel/assembly/fe_interpolator.hpp>
12#include <kernel/assembly/interpolator.hpp>
13#include <kernel/assembly/slip_filter_assembler.hpp>
14#include <kernel/assembly/unit_filter_assembler.hpp>
16#include <kernel/geometry/export_vtk.hpp>
17#include <kernel/geometry/mesh_part.hpp>
19#include <kernel/global/gate.hpp>
20#include <kernel/lafem/dense_vector.hpp>
21#include <kernel/lafem/dense_vector_blocked.hpp>
22#include <kernel/lafem/projection_filter.hpp>
23#include <kernel/lafem/vector_mirror.hpp>
24#include <kernel/shape.hpp>
25#include <kernel/space/lagrange1/element.hpp>
26#include <kernel/trafo/standard/mapping.hpp>
28#include <kernel/util/dist.hpp>
29#include <kernel/util/memory_arbiter.hpp>
30#include <kernel/util/tiny_algebra.hpp>
31
32namespace FEAT::Assembly
33{
53 template<typename Mesh_>
55 {
56 public:
59
77 static MeshPart make(Mesh_& mesh, const MeshPart& boundary, typename Mesh_::CoordType critial_angle = 0.34)
78 {
79 // Dimension of mesh shapes
80 static constexpr int dim = Mesh_::ShapeType::dimension;
81
82 // We are producing 0-dimensional features on 2d meshes and 1-dimensional features on 3d meshes
83 static constexpr int feature_dim = dim - 2;
84
85 // Dimension of facets whose normals we compare to determine features
86 static constexpr int facet_dim = dim - 1;
87
88 // Trafo type for determining surface normals
89 using TrafoType = Trafo::Standard::Mapping<Mesh_>;
90
91 // Normal computer type
92 using NormalComputer = Assembly::OuterNormalComputer<TrafoType>;
93
94 // Datatype
95 using DataType = typename Mesh_::CoordType;
96
97 // Entities of the feature meshpart
98 std::array<std::set<Index>, dim + 1> entities;
99
100 // Precompute orientations and facet_mask
101 std::vector<DataType> orientations(mesh.get_num_entities(facet_dim), DataType(0));
102 std::vector<Index> facet_mask(mesh.get_num_entities(facet_dim), ~Index(0));
103 {
104 const auto& facet_tgs = boundary.template get_target_set<facet_dim>();
105 Index idx(0);
106 for(Index i(0); i < boundary.get_num_entities(facet_dim); i++)
107 {
108 facet_mask[facet_tgs[i]] = idx;
109 idx++;
110 }
111 }
112 NormalComputer::compute_orientations(orientations.data(), facet_mask.data(), mesh);
113
114 const TrafoType trafo(mesh);
115 const auto& feature_tgs = boundary.template get_target_set<feature_dim>();
116
117 const auto& feature_at_facet = mesh.template get_index_set<facet_dim, feature_dim>();
118 const Adjacency::Graph facet_at_feature(Adjacency::RenderType::transpose, feature_at_facet);
119
120 for(Index i(0); i < boundary.get_num_entities(feature_dim); i++)
121 {
122 const Index candidate = feature_tgs[i];
123
124 // NOTE: We don't want to assume that the boundary meshpart has a topology.
125 // We thus search through the mesh to find the 2 adjacent facets and re-use the facet mask
126 // to check if a facet is part of the boundary.
127 std::array<Index, 2> boundary_facets;
128 Index idx(0);
129 for(auto it = facet_at_feature.image_begin(candidate); it != facet_at_feature.image_end(candidate); ++it)
130 {
131 if(facet_mask[*it] != ~Index(0))
132 {
133 boundary_facets[idx++] = *it;
134 }
135
136 }
137
138 XASSERTM(idx == 2, "FeatureMeshPartFactory: Too many adjacent facets at feature entity " + stringify(candidate));
139
140 // Found adjacent facets, determine angle between normal vectors
141
142 Index facet_a = boundary_facets[0];
143 Index facet_b = boundary_facets[1];
144
145 auto normals_a = NormalComputer::compute_oriented_normals(trafo, facet_a);
146 auto normals_b = NormalComputer::compute_oriented_normals(trafo, facet_b);
147
148 Tiny::Vector<DataType, dim> normal_a = normals_a[0] * orientations[facet_a];
149 Tiny::Vector<DataType, dim> normal_b = normals_b[0] * orientations[facet_b];
150
151 const DataType angle = Math::acos(Tiny::dot(normal_a, normal_b));
152
153 if(angle > critial_angle) // 20 degrees
154 {
155 add_sub_entities<dim, feature_dim>(entities, mesh, candidate);
156 }
157 }
158
159 // Create result meshpart by writing collected entities into target sets
160 std::array<Index, 4> result_size{0, 0, 0, 0};
161 for(Index i(0); i < Index(dim); i++)
162 {
163 result_size[i] = Index(entities[i].size());
164 }
165 MeshPart result(result_size.data());
166
167 fill_target_sets<dim, dim>(entities, result);
168
169 return result;
170 }
171
172 private:
174 template<int mesh_dim, int dim>
175 static void fill_target_sets(std::array<std::set<Index>, mesh_dim + 1>& entities, MeshPart& mesh_part)
176 {
177 if constexpr(dim >= 0)
178 {
179 auto& target_set = mesh_part.template get_target_set<dim>();
180 Index i(0);
181 for(Index entity : entities[dim])
182 {
183 target_set[i++] = entity;
184 }
185
186 fill_target_sets<mesh_dim, dim - 1>(entities, mesh_part);
187 }
188 }
189
191 template<int mesh_dim, int entity_dim>
192 static void add_sub_entities(std::array<std::set<Index>, mesh_dim + 1>& entities, const Mesh_& mesh, Index entity)
193 {
194 entities[entity_dim].insert(entity);
195
196 if constexpr(entity_dim > 0)
197 {
198 const auto& idx_set = mesh.template get_index_set<entity_dim, entity_dim - 1>();
199
200 for(int i(0); i < idx_set.num_indices; i++)
201 {
202 add_sub_entities<mesh_dim, entity_dim - 1>(entities, mesh, idx_set(entity, i));
203 }
204 }
205 }
206 };
207
217 template<typename Mesh_>
219 {
220 public:
222 using MeshType = Mesh_;
223
224 private:
226 static constexpr int world_dim = MeshType::world_dim;
228 static constexpr int shape_dim = MeshType::shape_dim;
230 using IndexSet = std::set<Index>;
232 std::array<IndexSet, shape_dim + 1> _entities;
234 std::array<IndexSet, shape_dim + 1> _feature_entities;
235
236 static constexpr std::array<Index, 4> empty_mesh_part_size = {{0, 0, 0, 0}};
237
241 Geometry::MeshPart<Mesh_> _feature_network{empty_mesh_part_size.data()};
242
243 public:
244 ProjectionFilterAssembler() = default;
248 ProjectionFilterAssembler& operator=(const ProjectionFilterAssembler&) = delete;
249 virtual ~ProjectionFilterAssembler() = default;
250
257 {
258 Intern::UnitAsmWrapper<shape_dim>::merge(_entities.data(), mesh_part);
261 mesh_part,
262 [](auto a, auto /*b*/) { return a; });
263 }
264
271 {
272 Intern::UnitAsmWrapper<shape_dim>::merge(_feature_entities.data(), mesh_part);
275 mesh_part,
276 [](auto a, auto /*b*/) { return a; });
277 }
278
288 template<typename DataType_, typename IndexType_, typename Space_, typename Gate_>
289 void
290 assemble(LAFEM::ProjectionFilter<MeshType, DataType_, IndexType_>& filter, Space_& space, const Gate_* gate = nullptr) const
291 {
293
294 // We need two things for assembling a ProjectionFilter
295 // 1. The coordinates of the DoFs we want to filter
296 // 2. A classification of the boundary DoFs into boundary, feature, and fixed DoFs
297
298 // Determine DoF positions
300 _dof_positions<DataType_, IndexType_>(space);
301
302 // Determine valences of DoFs in feature network.
303 // WARNING: Only works for Space_ = Space::Lagrange1::Element for now
304 // To make this work for at least other Lagrange spaces, one needs to interpolate the valences similarly to the DoF positions
305 LAFEM::DenseVector<DataType_, Index> feature_valences = _feature_network_valences<DataType_>(space.get_mesh());
306
307 // Sync valences if possible/required
308 if(gate)
309 {
310 gate->sync_0(feature_valences);
311 }
312
313 // Create new filter
314 filter = FilterType(&space.get_mesh(), _neuman_zero_boundary.clone(), _feature_network.clone());
315
316 // Assemble filter
317 // WARNING: Only works for Space_ = Space::Lagrange1::Element for now
318 // To make this work for other spaces, one needs to properly maps DoFs in this routine
319 _assemble(filter, feature_valences, dof_positions);
320 }
321
322 protected:
324 template<typename DataType_, typename IndexType_>
327 const LAFEM::DenseVector<DataType_, IndexType_>& feature_valences,
329 {
330 const auto& target_set = _neuman_zero_boundary.template get_target_set<0>();
331
332 Memory::TypedView<Tiny::Vector<DataType_, MeshType::world_dim>> dof_view = dof_positions.elements_view_r();
333 Memory::TypedView<DataType_> valences = feature_valences.elements_view_r();
334
335 for(Index i(0); i < target_set.get_num_entities(); i++)
336 {
337 // TODO: Handle DoF-mapping
338 Index vertex = target_set[i];
339 if(Index(valences(vertex)) == 0)
340 {
341 // A vertex with valence 0 lies somewhere on the boundary, but not on a feature.
342 // Tell the filter it is a boundary DoF and can be freely projected
343 filter.add_boundary_dof(vertex, dof_view(vertex));
344 }
345 else if(Index(valences(vertex)) == 2)
346 {
347 // A vertex with valence 2 is part of a run of feature edges.
348 // Tell the filter is is a boundary DoF and needs to projected onto the feature network
349 filter.add_feature_dof(vertex, dof_view(vertex));
350 }
351 else
352 {
353 // A vertex with any other valence is some special vertex in the feature network.
354 // In 2D it is just a feature vertex.
355 // In 3D it is either an end of a run of feature edges or an intersection between runs of feature edges.
356 // Either way we want the filter to not allow any modification its position
357 filter.add_fixed_dof(vertex, dof_view(vertex));
358 }
359 }
360 }
361
362 // Determine valences of vertices in the feature network
363 template<typename DataType_>
364 LAFEM::DenseVector<DataType_, Index> _feature_network_valences(const MeshType& mesh) const
365 {
366 LAFEM::DenseVector<DataType_, Index> valences(mesh.get_num_vertices(), 0);
367 Memory::TypedView<DataType_> view = valences.elements_view_w();
368
369 if constexpr(Mesh_::world_dim == 3)
370 {
371 // Feature network consists of edges
372 const auto& target_set = _feature_network.template get_target_set<1>();
373 const auto& index_set = mesh.template get_index_set<1, 0>();
374 for(Index i(0); i < _feature_network.get_num_entities(1); i++)
375 {
376 view[index_set(target_set[i], 0)]++;
377 view[index_set(target_set[i], 1)]++;
378 }
379 }
380
381 // mesh.get_index_set<0, 0>() should be allowed and be an identity mapping
382 // Then these if constexpr would not be needed
383 if constexpr(Mesh_::world_dim == 2)
384 {
385 // Feature network consists of vertices
386 const auto& target_set = _feature_network.template get_target_set<0>();
387 for(Index i(0); i < _feature_network.get_num_entities(0); i++)
388 {
389 view[target_set[i]]++;
390 }
391 }
392
393 return valences;
394 }
395
397 template<typename DataType_, typename IndexType_, typename Space_>
399 {
400 using TrafoType = typename Space_::TrafoType;
401
402 MeshType& mesh = space.get_mesh();
403
404 // Copy vertices into a DenseVectorBlocked for interpolation
406 vertices.format();
407 // Copy mesh vertices into dense vector blocked
408 {
409 const auto& vtx = mesh.get_vertex_set();
410 Memory::TypedView<Tiny::Vector<DataType_, MeshType::world_dim>> vertex_view = vertices.elements_view_w();
411 for(Index i(0); i < mesh.get_num_vertices(); i++)
412 {
413 vertex_view[i] = vtx[i];
414 }
415 }
416
417 // Interpolate from P1 to given space
418 Space::Lagrange1::Element<TrafoType> vertex_space(space.get_trafo());
420 Assembly::FEInterpolator<decltype(vertex_space), Space_>::interpolate(
421 dof_positions,
422 vertices,
423 space,
424 vertex_space);
425
426 return dof_positions;
427 }
428 };
429} // namespace FEAT::Assembly
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
FEAT Kernel base header.
Adjacency Graph implementation.
Definition: graph.hpp:34
ImageIterator image_begin(Index domain_node) const
Returns an iterator for the first adjacent image node.
Definition: graph.hpp:964
ImageIterator image_end(Index domain_node) const
Returns an iterator for the first position past the last adjacent image node.
Definition: graph.hpp:972
Factory for MeshParts describing features of the mesh boundary.
static void add_sub_entities(std::array< std::set< Index >, mesh_dim+1 > &entities, const Mesh_ &mesh, Index entity)
Add entity and all contained lower-dimensional entities into entity arrays.
static MeshPart make(Mesh_ &mesh, const MeshPart &boundary, typename Mesh_::CoordType critial_angle=0.34)
Creates a MeshPart whose target sets contain all feature entities of the given boundary.
static void fill_target_sets(std::array< std::set< Index >, mesh_dim+1 > &entities, MeshPart &mesh_part)
Write collected entities into proper target sets.
Helper class that computes a weighted outer unit normal field.
ProjectionFilter assembler class template.
static constexpr int world_dim
World dimension.
static constexpr int shape_dim
Shape dimension.
void assemble(LAFEM::ProjectionFilter< MeshType, DataType_, IndexType_ > &filter, Space_ &space, const Gate_ *gate=nullptr) const
Assemble a ProjectionFilter.
void add_mesh_part(const Geometry::MeshPart< MeshType > &mesh_part)
Adds the given MeshPart to the Neumann boundary.
Geometry::MeshPart< Mesh_ > _neuman_zero_boundary
MeshPart describing Neumann boundary.
LAFEM::DenseVectorBlocked< DataType_, Index, MeshType::world_dim > _dof_positions(Space_ &space) const
Determine position of DoFs by interpolating vertex positions from P1 to the given space.
void add_feature_mesh_part(const Geometry::MeshPart< MeshType > &mesh_part)
Adds the given MeshPart to the boundary features.
std::array< IndexSet, shape_dim+1 > _entities
dof-index sets for entities that must be filtered
Geometry::MeshPart< Mesh_ > _feature_network
MeshPart describing boundary features.
Mesh_ MeshType
Type of mesh to assemble filter on.
void _assemble(LAFEM::ProjectionFilter< MeshType, DataType_, IndexType_ > &filter, const LAFEM::DenseVector< DataType_, IndexType_ > &feature_valences, const LAFEM::DenseVectorBlocked< DataType_, IndexType_, MeshType::world_dim > &dof_positions) const
Inner assembly helper. Classifies DoFs based on feature network valences and adds DoFs to ProjectionF...
std::array< IndexSet, shape_dim+1 > _feature_entities
dof-index sets for feature entities
Index get_num_entities(int dim) const
Returns the number of entities.
Definition: mesh_part.hpp:311
void clone(const MeshPart &other)
Clones another mesh part object into this object.
Definition: mesh_part.hpp:243
static MeshPartType meshpart_union(const MeshPartType &left, const MeshPartType &right, const AttributeFn_ &attribute_merge)
Compute the union of two meshparts.
void format()
Reset all elements of the container to a given value or zero if missing.
Definition: container.hpp:831
Blocked Dense data vector class template.
Dense data vector class template.
Enforces homogenous Neumann boundary conditions in normal direction by projecting onto the boundary.
Typed memory view class.
Standard Lagrange-1 Finite-Element space class template.
Definition: element.hpp:39
Tiny Vector class template.
Standard transformation mapping class template.
Definition: mapping.hpp:39
Contains boolean operations on meshparts (intersection, union, difference, symmetric difference)
@ transpose
Render-Transpose mode.
Assembly namespace.
T_ acos(T_ x)
Returns the arccosine of a value.
Definition: math.hpp:967
CUDA_HOST_DEVICE T_ dot(const T_ &a, const T_ &b)
Computes the dot-product of two scalars.
String stringify(const T_ &item)
Converts an item into a String.
Definition: string.hpp:993
std::uint64_t Index
Index data type.
Interpolation operator between two finite element spaces.