FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
surface_integrator.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/cubature/dynamic_factory.hpp>
10#include <kernel/cubature/rule.hpp>
11#include <kernel/trafo/standard/mapping.hpp>
12#include <kernel/trafo/standard/inverse_mapping.hpp>
13#include <kernel/util/tiny_algebra.hpp>
14
15#include <vector>
16#include <numeric>
17
18namespace FEAT::Assembly
19{
20
21 template<typename DT_, int dim_>
22 using BoundingBoxType = Tiny::Matrix<DT_, 2, dim_>;
23
24 namespace Intern
25 {
26 template<typename DT_, int dim_, typename VertexSet_, typename IndexTuple_>
27 static inline BoundingBoxType<DT_, dim_> get_boundingbox(const VertexSet_& vtx_set, const IndexTuple_& vidx, DT_ tol)
28 {
29 typedef DT_ DataType;
30 BoundingBoxType<DT_, dim_> bbox;
31
32 // build cell bounding box
33 bbox[0] = bbox[1] = vtx_set[vidx[0]];
34 for(int i(1); i < vidx.num_indices; ++i)
35 {
36 // get vertex and update bounding box
37 const auto& vtx = vtx_set[vidx[i]];
38 for(int j(0); j < dim_; ++j)
39 {
40 Math::minimax(DataType(vtx[j]), bbox[0][j], bbox[1][j]);
41 }
42 }
43
44 // finally, incorporate tolerances
45 for(int j(0); j < dim_; ++j)
46 {
47 // compute bbox dimension
48 DataType bb_extra = tol * (bbox[1][j] - bbox[0][j]);
49 bbox[0][j] -= bb_extra;
50 bbox[1][j] += bb_extra;
51 }
52
53 return bbox;
54 }
55
56 template<typename DT_, int dim_>
57 static inline bool check_point(const Tiny::Vector<DT_, dim_>& pt, const BoundingBoxType<DT_, dim_>& bb)
58 {
59 bool outside = false;
60 for(int i = 0; i < dim_; ++i)
61 {
62 outside |= (pt[i] < bb[0][i]) || (pt[i] > bb[1][i]);
63 }
64 return !outside;
65 }
66 }
67
96 template<typename Trafo_, typename SurfaceShape_>
98 {
99 public:
100 typedef Trafo_ TrafoType;
101 typedef typename TrafoType::MeshType MeshType;
102 typedef SurfaceShape_ SurfaceShapeType;
103 static constexpr int domain_dim = Trafo_::ShapeType::dimension;
104 static constexpr int sub_dim = SurfaceShapeType::dimension;
105 typedef typename TrafoType::CoordType DataType;
106 typedef Index IndexType;
115
116 static constexpr int verts_per_face = Shape::FaceTraits<SurfaceShapeType, 0>::count;
117
118
119 private:
120 const TrafoType& _trafo;
121 std::vector<Tiny::Vector<DataType, domain_dim>> _shape_vertices;
123
124 std::vector<Geometry::IndexTuple<verts_per_face>> _shape_index_tmp;
125
126 std::vector<IndexType> _cell_helper;
127 std::vector<IndexType> _cell_helper_offsets;
128 std::vector<IndexType> _active_surfaces;
129 std::vector<int> _cell_mask_vec;
130
131 CubatureRuleType _cubature_rule;
132
135
138 {
139 bool no_intersect = false;
140 for(int d = 0; d < domain_dim; ++d)
141 {
142 no_intersect |= (bb_a[0][d] > bb_b[1][d]) || (bb_b[0][d] > bb_a[1][d]);
143 }
144
145 return !no_intersect;
146 }
147
148 public:
149
150 void set_normal_view(bool inside_view)
151 {
152 _inside_view = inside_view;
153 }
154
155 bool get_normal_view() const
156 {
157 return _inside_view;
158 }
159
160 void compile()
161 {
162 // move our helper vector
163 _shape_indices.set_indices(std::move(_shape_index_tmp));
164 // get mesh
165 const MeshType& mesh = _trafo.get_mesh();
166 const auto& vtx_set = mesh.get_vertex_set();
167 // first of all, get bounding box of this mesh
168 BoundingBoxType<DataType, domain_dim> mesh_bb;
169 for(int k = 0; k < domain_dim; ++k)
170 {
171 mesh_bb[0][k] = Math::Limits<DataType>::max();
172 mesh_bb[1][k] = Math::Limits<DataType>::lowest();
173 }
174 {
175 for(const auto* verts = vtx_set.begin(); verts != vtx_set.end(); ++verts)
176 {
177 for(int k = 0; k < domain_dim; ++k)
178 Math::minimax((*verts)[k], mesh_bb[0][k], mesh_bb[1][k]);
179 }
180 }
181
182 // preprocess which surface elements should be checked at all
183 _active_surfaces.clear();
184 for(IndexType k = 0; k < _shape_indices.get_num_entities(); ++k)
185 {
186 const auto& loc_ind_tuple = _shape_indices[k];
187 // domain_dim boundingbox of our shape
188 auto shape_bb = Intern::get_boundingbox<DataType, domain_dim>(_shape_vertices, loc_ind_tuple, DataType(1E-2));
189 // are we inside of our local domain boundingbox ?
190 if(boundingbox_intersect_test(shape_bb, mesh_bb))
191 _active_surfaces.push_back(k);
192 }
193
194 // now process which mesh cells are candidates for each surface cell
195 _cell_helper.clear();
196 _cell_helper_offsets.resize(_shape_indices.get_num_entities()+1);
197 for(auto& c : _cell_helper_offsets)
198 {
199 c = IndexType(0);
200 }
201
202
203 // get mapping element to vertices
204 const auto& ele_to_vert = mesh.template get_index_set<domain_dim, 0>();
205 const Index num_elements = mesh.get_num_elements();
206 constexpr Index batch_size = 1 << 19;
207 std::vector<std::vector<IndexType>> gather_helper(std::min(batch_size, Index(_active_surfaces.size())));
208 FEAT_PRAGMA_OMP(parallel for)
209 for(std::size_t i = 0; i < gather_helper.size(); ++i)
210 {
211 // reserve 128 elements per entry, this should be enough without triggering a reallocation besides extrem cases
212 gather_helper[i].reserve(128);
213 }
214 // now iterate over our surface elements in a batched manner
215 for(Index num_batch = 0; num_batch*batch_size < _active_surfaces.size(); ++num_batch)
216 {
217 FEAT_PRAGMA_OMP(parallel for)
218 for(Index t = 0; t < gather_helper.size(); ++t)
219 {
220 gather_helper.at(t).clear();
221 }
222
223 const Index offset = num_batch*batch_size;
224 // now iterate through our mesh and test each cell for its boundingbox... TODO: we could speed this up by using macro elements
225 FEAT_PRAGMA_OMP(parallel for)
226 for(Index t = 0; t < num_elements; ++t)
227 {
228 // check if our current element is outside of our fluid domain, an thus should be ignored
229 if(!_cell_mask_vec.empty() && _cell_mask_vec.at(t) == 3)
230 {
231 continue;
232 }
233
234 // create boundingbox
235 auto cell_bb = Intern::get_boundingbox<DataType, domain_dim>(vtx_set, ele_to_vert[t], DataType(1E-4));
236
237 // now test for each shape if the current cell interesects
238 for(Index k = 0; k+offset < std::min(_active_surfaces.size(), offset+batch_size); ++k)
239 {
240 const auto& loc_ind_tuple = _shape_indices[_active_surfaces.at(k+offset)];
241 // domain_dim boundingbox of our shape
242 auto shape_bb = Intern::get_boundingbox<DataType, domain_dim>(_shape_vertices, loc_ind_tuple, DataType(1E-4));
243 // are we inside of our local domain boundingbox ?
244 if(boundingbox_intersect_test(shape_bb, cell_bb))
245 {
246 FEAT_PRAGMA_OMP(critical)
247 gather_helper.at(k).push_back(t);
248 }
249 }
250 }
251 // now gather our information into our offset array
252 for(std::size_t k = 0; k < gather_helper.size(); ++k)
253 {
254 const auto& v_info = gather_helper[k];
255 _cell_helper_offsets.at(_active_surfaces.at(k+offset)+1) = v_info.size();
256 for(const auto& val : v_info)
257 _cell_helper.push_back(val);
258 }
259 // now gather the values
260 }
261 // now perform partial sum to get actual offsets
262 std::partial_sum(_cell_helper_offsets.begin(), _cell_helper_offsets.end(), _cell_helper_offsets.begin());
263
264 XASSERTM(_cell_helper_offsets.back() == _cell_helper.size(), "Offsets do not match size of cell helper array");
265
266 }
267
268
269 public:
276 explicit SurfaceIntegrator(const Trafo_& trafo, const Cubature::DynamicFactory& cubature_factory_) :
277 _trafo(trafo),
278 _cubature_rule(Cubature::ctor_factory, cubature_factory_),
279 _inside_view(false)
280 {
281 }
282
286 template<typename Vtxs_>
287 void set_vertices(const std::vector<Vtxs_>& vertices)
288 {
289 _shape_index_tmp.clear();
290 _shape_vertices.resize(vertices.size());
291 FEAT_PRAGMA_OMP(parallel for)
292 for(std::size_t k = 0; k < std::size_t(vertices.size()); ++k)
293 {
294 _shape_vertices[k] = vertices[k];
295 }
296 }
297
301 template<typename Vtxs_>
302 void add_face_vertices(const Vtxs_& face_vertices)
303 {
304 for(std::size_t i = 0; i < std::size_t(verts_per_face); ++i)
305 {
306 _shape_vertices.push_back(face_vertices[i]);
307 }
308
309 const IndexType num_indx = _shape_index_tmp.size();
310 _shape_index_tmp.emplace_back();
311 for(IndexType i = 0; i < IndexType(verts_per_face); ++i)
312 {
313 _shape_index_tmp.back()[int(i)] = num_indx*verts_per_face + IndexType(i);
314 }
315 }
316
320 template<typename VertIdx_>
321 void add_face(const VertIdx_& vert_idx)
322 {
323 _shape_index_tmp.emplace_back();
324 for(int i = 0; i < verts_per_face; ++i)
325 {
326 _shape_index_tmp.back()[i] = IndexType(vert_idx[std::size_t(i)]);
327 }
328 }
329
330
331 template<typename Job_>
332 void assemble(Job_& job)
333 {
334 if(this->_shape_indices.get_num_entities() == 0u)
335 {
336 return;
337 }
338
339 assemble_omp(job);
340 }
341
352 template<typename MaskVec_>
353 void set_mask_vector(const MaskVec_& cell_mask_vec)
354 {
355 _cell_mask_vec.resize(cell_mask_vec.size());
356 FEAT_PRAGMA_OMP(parallel for)
357 for(Index k = 0; k < cell_mask_vec.size(); ++k)
358 {
359 _cell_mask_vec[k] = int(cell_mask_vec[k]);
360 }
361 }
362
363 template<typename Job_>
364 void assemble_omp(Job_& job)
365 {
366 typedef typename Job_::Task TaskType;
367 // typedef for our evalhelper
369
370 FEAT_PRAGMA_OMP(parallel)
371 {
372 // create a task for this thread
373 std::unique_ptr<TaskType> task(new TaskType(job));
374
375 FEAT_PRAGMA_OMP(for)
376 for(IndexType k = 0; k < _active_surfaces.size(); ++k)
377 {
378 // construct actual surface
380 for(int i = 0; i < verts_per_face; ++i)
381 {
382 surface_verts[i] = _shape_vertices[_shape_indices[_active_surfaces[k]][i]];
383 }
384 // we now have to construct the transformation from the surface to the reference element, here we use our trafo capabilities
385 // we assume we have a id 0 orientation
387 coefficients.format();
388 EvalHelper::set_coefficients(coefficients, _shape_vertices, _shape_indices, _active_surfaces[k]);
389
390 // our jacobian of the transformation
391 JacobianType jac;
392 jac.format();
393 // since we have standard trafo, the jacobian is independent of our actual domain point
394 // our determinant
395 DataType jac_det = DataType(0);
396
397 DataType face_volume = EvalHelper::volume(coefficients);
398
399 // our cubature points
400 std::vector<CoordVecType> points(std::size_t(_cubature_rule.get_num_points()));
401 // our weights
402 std::vector<DataType> weights(std::size_t(_cubature_rule.get_num_points()));
403 // averaged normal (we assume we have planar normals, in theory this should also be a vector)
404 NormalVecType normal(DataType(0));
405
406 // now iterate through our cubature points
407 for(int pt = 0; pt < _cubature_rule.get_num_points(); ++pt)
408 {
409 RefCoordVecType domain_point = _cubature_rule.get_point(pt);
410 DataType weight = _cubature_rule.get_weight(pt);
411 CoordVecType img_point;
412 EvalHelper::map_point(img_point, domain_point, coefficients);
413 EvalHelper::calc_jac_mat(jac, domain_point, coefficients);
414 jac_det = jac.vol();
415 // calculate normal <- normal should point outside
416 // generally normal only makes sense for a dim-1 manifold
417 if constexpr(sub_dim+1==domain_dim)
418 {
419 normal = Tiny::orthogonal(jac).normalize();
420 if(_inside_view)
421 normal = normal.negate();
422 }
423 else
424 {
425 normal.format();
426 }
427 DataType point_weight = jac_det * weight;
428 points[std::size_t(pt)] = img_point;
429 weights[std::size_t(pt)] = point_weight;
430 }
431
432 // prepare our job, we simply provide all the candidate cells, its the jobs job to handle the information
433 task->prepare(_cell_helper, _cell_helper_offsets, points, weights, normal, face_volume, _active_surfaces[k]);
434
435 // now, assemble our task
436 task->assemble();
437
438 // scatter
439 FEAT_PRAGMA_OMP(critical)
440 task->scatter();
441
442 }
443 // and combine
444 FEAT_PRAGMA_OMP(critical)
445 task->combine();
446 }
447
448 }
449
450 }; // class SurfaceIntegrator
451}
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
FEAT Kernel base header.
Assemble class Discrete Integrale of an FE function over a d-1 manifold.
void add_face_vertices(const Vtxs_ &face_vertices)
Simply pushes new vertices to the end of the current vertex array.
SurfaceIntegrator(const Trafo_ &trafo, const Cubature::DynamicFactory &cubature_factory_)
Initializes the integrator with a cell wise list of vertices.
bool _inside_view
Normal defined from the inside or outside?
void set_mask_vector(const MaskVec_ &cell_mask_vec)
Sets the cell mask vector, which sorts out any cells completelly outside the fluid domain.
void add_face(const VertIdx_ &vert_idx)
bool boundingbox_intersect_test(const BoundingBoxType< DataType, domain_dim > &bb_a, const BoundingBoxType< DataType, domain_dim > &bb_b)
tests whether two boundingboxes intersect in any way
void set_vertices(const std::vector< Vtxs_ > &vertices)
Cubature Rule class template.
Definition: rule.hpp:38
Conformal Index-Set class template.
Definition: index_set.hpp:82
Index get_num_entities() const
Returns the number of entities.
Definition: index_set.hpp:185
Tiny Matrix class template.
CUDA_HOST_DEVICE void format(DataType alpha=DataType(0))
Formats the matrix.
Tiny Tensor3 class template.
Tiny Vector class template.
Assembly namespace.
void minimax(T_ x, T_ &a, T_ &b)
Updates the minimum and maximum.
Definition: math.hpp:195
Vector< T_, m_ > orthogonal(const Matrix< T_, m_, m_-1, sm_, sn_ > &tau)
Computes the positively oriented orthogonal vector to the columns of a m_ x (m_-1) Matrix.
std::uint64_t Index
Index data type.
@ img_point
specifies whether the trafo should supply image point coordinates
@ jac_det
specifies whether the trafo should supply jacobian determinants
Face traits tag struct template.
Definition: shape.hpp:106
Evalautator helper class for standard trafo.
Definition: details.hpp:48