FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
discrete_projector.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// includes, FEAT
9#include <kernel/assembly/asm_traits.hpp>
10#include <kernel/util/memory_arbiter.hpp>
11
12// includes, system
13#include <vector>
14
15namespace FEAT
16{
17 namespace Assembly
18 {
28 {
29 public:
42 template<
43 typename VectorOut_,
44 typename VectorIn_,
45 typename Space_>
46 static void project(
47 VectorOut_& vector,
48 const VectorIn_& coeff,
49 const Space_& space)
50 {
51 typedef Space_ SpaceType;
52 typedef typename SpaceType::TrafoType TrafoType;
53 typedef typename TrafoType::MeshType MeshType;
54 typedef typename MeshType::ShapeType ShapeType;
55
56 static constexpr int shape_dim = ShapeType::dimension;
57 static constexpr int nverts = Shape::FaceTraits<ShapeType, 0>::count;
58
59 // define assembly traits
61 typedef typename AsmTraits::DataType DataType;
62
63 // get our value type
64 typedef typename VectorOut_::ValueType ValueType;
65
66 // fetch the trafo and the mesh
67 const TrafoType& trafo(space.get_trafo());
68 const MeshType& mesh(space.get_mesh());
69
70 // fetch the index set
71 typedef typename MeshType::template IndexSet<shape_dim, 0>::Type IndexSetType;
72 const IndexSetType& vert_idx(mesh.template get_index_set<shape_dim, 0>());
73
74 // fetch the cell count
75 const Index num_verts(mesh.get_num_entities(0));
76
77 // create a clear output vector
78 vector = VectorOut_(num_verts, DataType(0));
79
80 // allocate an auxiliary count array
81 std::vector<int> aux(num_verts, 0);
82
83 // create a trafo evaluator
84 typename AsmTraits::TrafoEvaluator trafo_eval(trafo);
85
86 // create a space evaluator and evaluation data
87 typename AsmTraits::SpaceEvaluator space_eval(space);
88
89 // create a dof-mapping
90 typename AsmTraits::DofMapping dof_mapping(space);
91
92 // create trafo evaluation data
93 typename AsmTraits::TrafoEvalData trafo_data;
94
95 // create space evaluation data
96 typename AsmTraits::SpaceEvalData space_data;
97
98 // create local vector data
99 typename AsmTraits::template TLocalVector<ValueType> loc_vec;
100
101 // create a vector gather-axpy
102 typename VectorIn_::GatherAxpy gather_axpy(coeff);
103
104 // create a view for the vector's elements
105 Memory::TypedView<ValueType> vector_view = vector.elements_view_rw();
106
107 // loop over all cells of the mesh
108 for(Index cell(0); cell < trafo_eval.get_num_cells(); ++cell)
109 {
110 // format local matrix
111 loc_vec.format();
112
113 // initialize dof-mapping
114 dof_mapping.prepare(cell);
115
116 // fetch local vector
117 gather_axpy(loc_vec, dof_mapping);
118
119 // finish dof-mapping
120 dof_mapping.finish();
121
122 // prepare trafo evaluator
123 trafo_eval.prepare(cell);
124
125 // prepare space evaluator
126 space_eval.prepare(trafo_eval);
127
128 // fetch number of local dofs
129 int num_loc_dofs = space_eval.get_num_local_dofs();
130
131 // loop over all vertices of the cell
132 for(int k(0); k < nverts; ++k)
133 {
134 typename AsmTraits::DomainPointType dom_point;
135
136 // initialize domain point
137 for(int i(0); i < shape_dim; ++i)
138 {
139 dom_point[i] = Shape::ReferenceCell<ShapeType>::template vertex<DataType>(k, i);
140 }
141
142 // compute trafo data
143 trafo_eval(trafo_data, dom_point);
144
145 // compute basis function data
146 space_eval(space_data, trafo_data);
147
148 // compute function value
149 ValueType value(DataType(0));
150
151 // basis function loop
152 for(int i(0); i < num_loc_dofs; ++i)
153 {
154 // evaluate fe function
155 Tiny::axpy(value, loc_vec[i], space_data.phi[i].value);
156
157 // continue with next basis function
158 }
159
160 // fetch the vertex index
161 Index vi = vert_idx(cell, k);
162
163 // add vertex contribution
164 vector_view[vi] += value;
165
166 // update contribution counter
167 ++aux[vi];
168
169 // continue with next vertex
170 }
171
172 // finish evaluators
173 space_eval.finish();
174 trafo_eval.finish();
175
176 // continue with next cell
177 }
178
179 // finally, scale the output vector
180 for(Index i(0); i < num_verts; ++i)
181 {
182 if(aux[i] > 1)
183 {
184 vector_view[i] *= DataType(1) / DataType(aux[i]);
185 }
186 }
187 }
188
203 template<
204 typename VectorOut_,
205 typename VectorIn_,
206 typename Space_>
207 static void project_gradient(
208 VectorOut_& vector,
209 const VectorIn_& coeff,
210 const Space_& space)
211 {
212 typedef Space_ SpaceType;
213 typedef typename SpaceType::TrafoType TrafoType;
214 typedef typename TrafoType::MeshType MeshType;
215 typedef typename MeshType::ShapeType ShapeType;
216
217 static constexpr int shape_dim = ShapeType::dimension;
218 static constexpr int nverts = Shape::FaceTraits<ShapeType, 0>::count;
219
220 // define assembly traits
222 typedef typename AsmTraits::DataType DataType;
223
224 // get our value type
225 typedef typename VectorOut_::ValueType ValueOutType;
226 typedef typename VectorIn_::ValueType ValueInType;
227
228 // fetch the trafo and the mesh
229 const TrafoType& trafo(space.get_trafo());
230 const MeshType& mesh(space.get_mesh());
231
232 // fetch the index set
233 typedef typename MeshType::template IndexSet<shape_dim, 0>::Type IndexSetType;
234 const IndexSetType& vert_idx(mesh.template get_index_set<shape_dim, 0>());
235
236 // fetch the cell count
237 const Index num_verts(mesh.get_num_entities(0));
238
239 // create a clear output vector
240 vector = VectorOut_(num_verts, DataType(0));
241
242 // allocate an auxiliary count array
243 std::vector<int> aux(num_verts, 0);
244
245 // create a trafo evaluator
246 typename AsmTraits::TrafoEvaluator trafo_eval(trafo);
247
248 // create a space evaluator and evaluation data
249 typename AsmTraits::SpaceEvaluator space_eval(space);
250
251 // create a dof-mapping
252 typename AsmTraits::DofMapping dof_mapping(space);
253
254 // create trafo evaluation data
255 typename AsmTraits::TrafoEvalData trafo_data;
256
257 // create space evaluation data
258 typename AsmTraits::SpaceEvalData space_data;
259
260 // create local vector data
261 typename AsmTraits::template TLocalVector<ValueInType> loc_vec;
262
263 // create a vector gather-axpy
264 typename VectorIn_::GatherAxpy gather_axpy(coeff);
265
266 // create a view for the vector's elements
267 Memory::TypedView<ValueOutType> vector_view = vector.elements_view_rw();
268
269 // loop over all cells of the mesh
270 for(Index cell(0); cell < trafo_eval.get_num_cells(); ++cell)
271 {
272 // format local matrix
273 loc_vec.format();
274
275 // initialize dof-mapping
276 dof_mapping.prepare(cell);
277
278 // fetch local vector
279 gather_axpy(loc_vec, dof_mapping);
280
281 // finish dof-mapping
282 dof_mapping.finish();
283
284 // prepare trafo evaluator
285 trafo_eval.prepare(cell);
286
287 // prepare space evaluator
288 space_eval.prepare(trafo_eval);
289
290 // fetch number of local dofs
291 int num_loc_dofs = space_eval.get_num_local_dofs();
292
293 // loop over all vertices of the cell
294 for(int k(0); k < nverts; ++k)
295 {
296 typename AsmTraits::DomainPointType dom_point;
297
298 // initialize domain point
299 for(int i(0); i < shape_dim; ++i)
300 {
301 dom_point[i] = Shape::ReferenceCell<ShapeType>::template vertex<DataType>(k, i);
302 }
303
304 // compute trafo data
305 trafo_eval(trafo_data, dom_point);
306
307 // compute basis function data
308 space_eval(space_data, trafo_data);
309
310 // compute function value
311 ValueOutType value(DataType(0));
312
313 // basis function loop
314 for(int i(0); i < num_loc_dofs; ++i)
315 {
316 // evaluate fe function
317 Tiny::axpy(value, space_data.phi[i].grad, loc_vec[i]);
318
319 // continue with next basis function
320 }
321
322 // fetch the vertex index
323 Index vi = vert_idx(cell, k);
324
325 // add vertex contribution
326 vector_view[vi] += value;
327
328 // update contribution counter
329 ++aux[vi];
330
331 // continue with next vertex
332 }
333
334 // finish evaluators
335 space_eval.finish();
336 trafo_eval.finish();
337
338 // continue with next cell
339 }
340
341 // finally, scale the output vector
342 for(Index i(0); i < num_verts; ++i)
343 {
344 if(aux[i] > 1)
345 {
346 vector_view[i] *= (DataType(1) / DataType(aux[i]));
347 }
348 }
349 }
350 }; // class DiscreteVertexProjector<...>
351
361 {
362 public:
375 template<
376 typename VectorOut_,
377 typename VectorIn_,
378 typename Space_>
379 static void project(
380 VectorOut_& vector,
381 const VectorIn_& coeff,
382 const Space_& space)
383 {
384 Cubature::DynamicFactory cubature_factory("barycentre");
385 project(vector, coeff, space, cubature_factory);
386 }
387
403 template<
404 typename VectorOut_,
405 typename VectorIn_,
406 typename Space_>
407 static void project(
408 VectorOut_& vector,
409 const VectorIn_& coeff,
410 const Space_& space,
411 const String& cubature_name)
412 {
413 Cubature::DynamicFactory cubature_factory(cubature_name);
414 project(vector, coeff, space, cubature_factory);
415 }
416
432 template<typename VectorOut_, typename VectorIn_, typename Space_>
433 static void project(
434 VectorOut_& vector,
435 const VectorIn_& coeff,
436 const Space_& space,
437 const Cubature::DynamicFactory& cubature_factory)
438 {
439 typedef Space_ SpaceType;
440 typedef typename SpaceType::TrafoType TrafoType;
441 typedef typename TrafoType::MeshType MeshType;
442 typedef typename MeshType::ShapeType ShapeType;
443
444 // define assembly traits
446 typedef typename AsmTraits::DataType DataType;
447
448 // get our value type
449 typedef typename VectorOut_::ValueType ValueType;
450
451 // define the cubature rule
452 typename AsmTraits::CubatureRuleType cubature_rule(Cubature::ctor_factory, cubature_factory);
453
454 // fetch the trafo and the mesh
455 const TrafoType& trafo(space.get_trafo());
456 const MeshType& mesh(space.get_mesh());
457
458 // fetch the cell count
459 const Index num_cells(mesh.get_num_entities(ShapeType::dimension));
460
461 // create a clear output vector
462 vector = VectorOut_(num_cells);
463
464 // create a trafo evaluator
465 typename AsmTraits::TrafoEvaluator trafo_eval(trafo);
466
467 // create a space evaluator and evaluation data
468 typename AsmTraits::SpaceEvaluator space_eval(space);
469
470 // create a dof-mapping
471 typename AsmTraits::DofMapping dof_mapping(space);
472
473 // create trafo evaluation data
474 typename AsmTraits::TrafoEvalData trafo_data;
475
476 // create space evaluation data
477 typename AsmTraits::SpaceEvalData space_data;
478
479 // create local vector data
480 typename AsmTraits::template TLocalVector<ValueType> loc_vec;
481
482 // create a vector gather-axpy
483 typename VectorIn_::GatherAxpy gather_axpy(coeff);
484
485 // create a view for the vector's elements
486 Memory::TypedView<ValueType> vector_view = vector.elements_view_w();
487
488 // loop over all cells of the mesh
489 for(Index cell(0); cell < trafo_eval.get_num_cells(); ++cell)
490 {
491 // format local matrix
492 loc_vec.format();
493
494 // initialize dof-mapping
495 dof_mapping.prepare(cell);
496
497 // fetch local vector
498 gather_axpy(loc_vec, dof_mapping);
499
500 // finish dof-mapping
501 dof_mapping.finish();
502
503 // prepare trafo evaluator
504 trafo_eval.prepare(cell);
505
506 // prepare space evaluator
507 space_eval.prepare(trafo_eval);
508
509 // fetch number of local dofs
510 int num_loc_dofs = space_eval.get_num_local_dofs();
511
512 // compute function value
513 DataType value(DataType(0));
514 DataType area(DataType(0));
515
516 // loop over all quadrature points and integrate
517 for(int k(0); k < cubature_rule.get_num_points(); ++k)
518 {
519 // compute trafo data
520 trafo_eval(trafo_data, cubature_rule.get_point(k));
521
522 // compute basis function data
523 space_eval(space_data, trafo_data);
524
525 ValueType val(DataType(0));
526
527 // basis function loop
528 for(int i(0); i < num_loc_dofs; ++i)
529 {
530 // evaluate functor and integrate
531 Tiny::axpy(val, loc_vec[i], space_data.phi[i].value);
532 // continue with next basis function
533 }
534
535 // compute weight
536 DataType weight(trafo_data.jac_det * cubature_rule.get_weight(k));
537
538 // update cell area
539 area += weight;
540
541 // update cell value
542 value += weight * val;
543
544 // continue with next vertex
545 }
546
547 // set contribution
548 vector_view[cell] = (DataType(1) / area) * value;
549
550 // finish evaluators
551 space_eval.finish();
552 trafo_eval.finish();
553
554 // continue with next cell
555 }
556 }
557
574 template<
575 typename VectorOut_,
576 typename VectorIn_,
577 typename Space_>
578 static void project_gradient(
579 VectorOut_& vector,
580 const VectorIn_& coeff,
581 const Space_& space,
582 const String& cubature_name)
583 {
584 Cubature::DynamicFactory cubature_factory(cubature_name);
585 project_gradient(vector, coeff, space, cubature_factory);
586 }
587
603 template<typename VectorOut_, typename VectorIn_, typename Space_>
604 static void project_gradient(
605 VectorOut_& vector,
606 const VectorIn_& coeff,
607 const Space_& space,
608 const Cubature::DynamicFactory& cubature_factory)
609 {
610 typedef Space_ SpaceType;
611 typedef typename SpaceType::TrafoType TrafoType;
612 typedef typename TrafoType::MeshType MeshType;
613 typedef typename MeshType::ShapeType ShapeType;
614
615 // define assembly traits
617 typedef typename AsmTraits::DataType DataType;
618
619 // get our value type
620 typedef typename VectorIn_::ValueType ValueInType;
621 typedef typename VectorOut_::ValueType ValueOutType;
622
623 // define the cubature rule
624 typename AsmTraits::CubatureRuleType cubature_rule(Cubature::ctor_factory, cubature_factory);
625
626 // fetch the trafo and the mesh
627 const TrafoType& trafo(space.get_trafo());
628 const MeshType& mesh(space.get_mesh());
629
630 // fetch the cell count
631 const Index num_cells(mesh.get_num_entities(ShapeType::dimension));
632
633 // create a clear output vector
634 vector = VectorOut_(num_cells);
635
636 // create a trafo evaluator
637 typename AsmTraits::TrafoEvaluator trafo_eval(trafo);
638
639 // create a space evaluator and evaluation data
640 typename AsmTraits::SpaceEvaluator space_eval(space);
641
642 // create a dof-mapping
643 typename AsmTraits::DofMapping dof_mapping(space);
644
645 // create trafo evaluation data
646 typename AsmTraits::TrafoEvalData trafo_data;
647
648 // create space evaluation data
649 typename AsmTraits::SpaceEvalData space_data;
650
651 // create local vector data
652 typename AsmTraits::template TLocalVector<ValueInType> loc_vec;
653
654 // create a vector gather-axpy
655 typename VectorIn_::GatherAxpy gather_axpy(coeff);
656
657 // create a view for the vector's elements
658 Memory::TypedView<ValueOutType> vector_view = vector.element_view_w();
659
660 // loop over all cells of the mesh
661 for(Index cell(0); cell < trafo_eval.get_num_cells(); ++cell)
662 {
663 // format local matrix
664 loc_vec.format();
665
666 // initialize dof-mapping
667 dof_mapping.prepare(cell);
668
669 // fetch local vector
670 gather_axpy(loc_vec, dof_mapping);
671
672 // finish dof-mapping
673 dof_mapping.finish();
674
675 // prepare trafo evaluator
676 trafo_eval.prepare(cell);
677
678 // prepare space evaluator
679 space_eval.prepare(trafo_eval);
680
681 // fetch number of local dofs
682 int num_loc_dofs = space_eval.get_num_local_dofs();
683
684 // compute function value
685 ValueOutType value(DataType(0));
686 DataType area(DataType(0));
687
688 // loop over all quadrature points and integrate
689 for(int k(0); k < cubature_rule.get_num_points(); ++k)
690 {
691 // compute trafo data
692 trafo_eval(trafo_data, cubature_rule.get_point(k));
693
694 // compute basis function data
695 space_eval(space_data, trafo_data);
696
697 ValueOutType val(DataType(0));
698
699 // basis function loop
700 for(int i(0); i < num_loc_dofs; ++i)
701 {
702 // evaluate functor and integrate
703 Tiny::axpy(val, space_data.phi[i].grad, loc_vec[i]);
704 // continue with next basis function
705 }
706
707 // compute weight
708 DataType weight(trafo_data.jac_det * cubature_rule.get_weight(k));
709
710 // update cell area
711 area += weight;
712
713 // update cell value
714 Tiny::axpy(value, val, weight);
715
716 // continue with next vertex
717 }
718
719 // set contribution
720 vector_view[cell] = (DataType(1) / area) * value;
721
722 // finish evaluators
723 space_eval.finish();
724 trafo_eval.finish();
725
726 // continue with next cell
727 }
728 }
729
742 template<typename VectorOut_, typename VectorIn_, typename Space_>
743 static void project_refined(VectorOut_& vector, const VectorIn_& coeff, const Space_& space)
744 {
745 typedef Space_ SpaceType;
746 typedef typename SpaceType::TrafoType TrafoType;
747 typedef typename TrafoType::MeshType MeshType;
748 typedef typename MeshType::ShapeType ShapeType;
749
750 // define assembly traits
752 typedef typename AsmTraits::DataType DataType;
753
754 // get our value type
755 typedef typename VectorOut_::ValueType ValueType;
756
757 // define the cubature rule
758 Cubature::DynamicFactory cubature_factory("refine:midpoint");
759 typename AsmTraits::CubatureRuleType cubature_rule(Cubature::ctor_factory, cubature_factory);
760
761 // fetch the trafo and the mesh
762 const TrafoType& trafo(space.get_trafo());
763 const MeshType& mesh(space.get_mesh());
764
765 // fetch the cell count
766 const Index num_cells(mesh.get_num_entities(ShapeType::dimension));
767 const int num_points = cubature_rule.get_num_points();
768
769 // create a clear output vector
770 vector = VectorOut_(num_cells * Index(num_points), DataType(0));
771
772 // create a trafo evaluator
773 typename AsmTraits::TrafoEvaluator trafo_eval(trafo);
774
775 // create a space evaluator and evaluation data
776 typename AsmTraits::SpaceEvaluator space_eval(space);
777
778 // create a dof-mapping
779 typename AsmTraits::DofMapping dof_mapping(space);
780
781 // create trafo evaluation data
782 typename AsmTraits::TrafoEvalData trafo_data;
783
784 // create space evaluation data
785 typename AsmTraits::SpaceEvalData space_data;
786
787 // create local vector data
788 typename AsmTraits::template TLocalVector<ValueType> loc_vec;
789
790 // create a vector gather-axpy
791 typename VectorIn_::GatherAxpy gather_axpy(coeff);
792
793 // create a view for the vector's elements
794 Memory::TypedView<ValueType> vector_view = vector.elements_view_w();
795
796 // loop over all cells of the mesh
797 for(Index cell(0); cell < trafo_eval.get_num_cells(); ++cell)
798 {
799 // format local matrix
800 loc_vec.format();
801
802 // initialize dof-mapping
803 dof_mapping.prepare(cell);
804
805 // fetch local vector
806 gather_axpy(loc_vec, dof_mapping);
807
808 // finish dof-mapping
809 dof_mapping.finish();
810
811 // prepare trafo evaluator
812 trafo_eval.prepare(cell);
813
814 // prepare space evaluator
815 space_eval.prepare(trafo_eval);
816
817 // fetch number of local dofs
818 int num_loc_dofs = space_eval.get_num_local_dofs();
819
820 // loop over all quadrature points and integrate
821 for(int k(0); k < cubature_rule.get_num_points(); ++k)
822 {
823 // compute trafo data
824 trafo_eval(trafo_data, cubature_rule.get_point(k));
825
826 // compute basis function data
827 space_eval(space_data, trafo_data);
828
829 ValueType value(DataType(0));
830
831 // basis function loop
832 for(int i(0); i < num_loc_dofs; ++i)
833 {
834 // evaluate functor and integrate
835 Tiny::axpy(value, loc_vec[i], space_data.phi[i].value);
836 // continue with next basis function
837 }
838
839 // save value
840 vector_view[cell*Index(num_points) + Index(k)] = value;
841
842 // continue with next vertex
843 }
844
845 // finish evaluators
846 space_eval.finish();
847 trafo_eval.finish();
848
849 // continue with next cell
850 }
851 }
852 }; // class DiscreteCellProjector<...>
853 } // namespace Assembly
854} // namespace FEAT
Common single-space assembly traits class template.
Definition: asm_traits.hpp:83
Discrete cell projector class.
static void project(VectorOut_ &vector, const VectorIn_ &coeff, const Space_ &space, const String &cubature_name)
Projects a discrete function into the cells.
static void project(VectorOut_ &vector, const VectorIn_ &coeff, const Space_ &space)
Projects a discrete function into the cells using the barycentre cubature rule.
static void project(VectorOut_ &vector, const VectorIn_ &coeff, const Space_ &space, const Cubature::DynamicFactory &cubature_factory)
Projects a discrete function into the cells.
static void project_refined(VectorOut_ &vector, const VectorIn_ &coeff, const Space_ &space)
Projects a discrete function into the cells of a once refined mesh.
static void project_gradient(VectorOut_ &vector, const VectorIn_ &coeff, const Space_ &space, const Cubature::DynamicFactory &cubature_factory)
Projects the gradient of a scalar discrete function into the cells.
static void project_gradient(VectorOut_ &vector, const VectorIn_ &coeff, const Space_ &space, const String &cubature_name)
Projects the gradient of a scalar discrete function into the cells.
Discrete vertex projector class.
static void project_gradient(VectorOut_ &vector, const VectorIn_ &coeff, const Space_ &space)
Projects the gradient of a scalar discrete function into the vertices.
static void project(VectorOut_ &vector, const VectorIn_ &coeff, const Space_ &space)
Projects a discrete function into the vertices.
Typed memory view class.
void format(const T_ &value)
Formats the array elements to a given value, i.e. sets all elements to the same value.
String class implementation.
Definition: string.hpp:47
CUDA_HOST_DEVICE void axpy(T_ &y, const T_ &x, const T_ &alpha)
Performs an AXPY of two scalars.
FEAT namespace.
Definition: adjactor.hpp:12
@ value
specifies whether the space should supply basis function values
std::uint64_t Index
Index data type.
@ dom_point
specifies whether the trafo should supply domain point coordinates
Face traits tag struct template.
Definition: shape.hpp:106
Reference cell traits structure.
Definition: shape.hpp:230