FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
rew_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/analytic/function.hpp>
10#include <kernel/assembly/asm_traits.hpp>
11#include <kernel/lafem/dense_vector.hpp>
12#include <kernel/util/math.hpp>
13
14namespace FEAT
15{
16 namespace Assembly
17 {
28 {
29 public:
33 enum class WeightType
34 {
36 arithmetic = 0,
38 volume
39 };
40
62 template<
63 typename DT_,
64 typename IT_,
65 typename Function_,
66 typename Space_,
67 typename CubatureFactory_>
68 static void project(
70 const Function_& function,
71 const Space_& space,
72 const CubatureFactory_& cubature_factory,
73 WeightType weight_type = WeightType::volume)
74 {
75 typedef LAFEM::DenseVector<DT_, IT_> VectorType;
76 typedef Function_ FunctionType;
77 typedef Space_ SpaceType;
78 typedef typename SpaceType::TrafoType TrafoType;
79
80 static_assert(Function_::can_value, "analytic function can't compute function values");
81
82 // define assembly traits
84 typedef typename AsmTraits::DataType DataType;
85
86 // define the cubature rule
87 typename AsmTraits::CubatureRuleType cubature_rule(Cubature::ctor_factory, cubature_factory);
88
89 // fetch the trafo and the mesh
90 const TrafoType& trafo(space.get_trafo());
91
92 // create a trafo evaluator
93 typename AsmTraits::TrafoEvaluator trafo_eval(trafo);
94
95 // create a space evaluator and evaluation data
96 typename AsmTraits::SpaceEvaluator space_eval(space);
97
98 // create a dof-mapping
99 typename AsmTraits::DofMapping dof_mapping(space);
100
101 // define our analytic evaluation traits
102 typedef Analytic::EvalTraits<DataType, Function_> AnalyticEvalTraits;
103
104 // create a function evaluator
105 typename FunctionType::template Evaluator<AnalyticEvalTraits> func_eval(function);
106
107 // create trafo evaluation data
108 typename AsmTraits::TrafoEvalData trafo_data;
109
110 // create space evaluation data
111 typename AsmTraits::SpaceEvalData space_data;
112
113 // allocate fine-mesh mass matrix
114 typename AsmTraits::template TLocalMatrix<DT_> mass;
115
116 // pivot array for factorization
117 int pivot[mass.n];
118
119 // create local vector data
120 typename AsmTraits::template TLocalVector<DT_> lvad, lxad;
121
122 // create local weight data
123 typename AsmTraits::template TLocalVector<DT_> lwad;
124
125 // fetch the dof count
126 const Index num_dofs(space.get_num_dofs());
127
128 // create a clear output vector
129 vector = VectorType(num_dofs, DataType(0));
130
131 // allocate weight vector
132 VectorType weight(num_dofs, DataType(0));
133
134 {
135 // create vector scatter-axpy
136 typename VectorType::ScatterAxpy scatter_axpy(vector);
137
138 // create weight scatter-axpy
139 typename VectorType::ScatterAxpy weight_scatter_axpy(weight);
140
141 // format local weight vector
142 if(weight_type == WeightType::arithmetic)
143 lwad.format(DataType(1));
144
145 // loop over all cells of the mesh
146 for(Index cell(trafo_eval.begin()); cell != trafo_eval.end(); ++cell)
147 {
148 // prepare trafo evaluator
149 trafo_eval.prepare(cell);
150
151 // prepare space evaluator
152 space_eval.prepare(trafo_eval);
153
154 // fetch number of local dofs
155 int num_loc_dofs(space_eval.get_num_local_dofs());
156
157 // format local system
158 lvad.format();
159 mass.format();
160
161 // cell volume
162 DataType volume(DataType(0));
163
164 // loop over all cubature points and integrate
165 for(int k(0); k < cubature_rule.get_num_points(); ++k)
166 {
167 // compute trafo data
168 trafo_eval(trafo_data, cubature_rule.get_point(k));
169
170 // compute basis function data
171 space_eval(space_data, trafo_data);
172
173 // compute function value
174 DataType fv = func_eval.value(trafo_data.img_point);
175
176 // pre-compute integration weight
177 DataType omega(trafo_data.jac_det * cubature_rule.get_weight(k));
178
179 // update volume
180 volume += omega;
181
182 // test function loop
183 for(int i(0); i < num_loc_dofs; ++i)
184 {
185 // assemble RHS entry
186 lvad(i) += omega * fv * space_data.phi[i].value;
187
188 // trial function loop
189 for(int j(0); j < num_loc_dofs; ++j)
190 {
191 mass(i,j) += omega * space_data.phi[i].value * space_data.phi[j].value;
192 }
193 }
194 }
195
196 // finish evaluators
197 space_eval.finish();
198 trafo_eval.finish();
199
200 // try to factorize the local mass matrix
201 Math::invert_matrix(num_loc_dofs, mass.sn, &mass.v[0][0], pivot);
202
203 // solve M*x=f
204 lxad.set_mat_vec_mult(mass, lvad);
205
206 // choose weight
207 if(weight_type == WeightType::volume)
208 {
209 lxad *= volume;
210 lwad.format(volume);
211 }
212
213 // prepare dof-mapping
214 dof_mapping.prepare(cell);
215
216 // incorporate local vector and weights
217 scatter_axpy(lxad, dof_mapping);
218 weight_scatter_axpy(lwad, dof_mapping);
219
220 // finish dof-mapping
221 dof_mapping.finish();
222 }
223
224 }
225
226 // finally, scale the vector by the weights
227 Memory::TypedView<DataType> vx(vector.elements_view_rw());
228 const Memory::TypedView<DataType> wx(weight.elements_view_r());
229 for(Index i(0); i < num_dofs; ++i)
230 {
231 if(wx[i] > DataType(0))
232 vx[i] /= wx[i];
233 }
234
235 // okay
236 }
237 }; // class RewProjector
238 } // namespace Assembly
239} // namespace FEAT
Common single-space assembly traits class template.
Definition: asm_traits.hpp:83
Restricted Element-Wise Projection operator.
WeightType
Weighting type enumeration.
@ volume
use volume-based averaging
@ arithmetic
use arithmetic averaging
static void project(LAFEM::DenseVector< DT_, IT_ > &vector, const Function_ &function, const Space_ &space, const CubatureFactory_ &cubature_factory, WeightType weight_type=WeightType::volume)
Projects an analytic function into a finite element space.
Dense data vector class template.
Typed memory view class.
DT_ invert_matrix(const IT_ n, const IT_ stride, DT_ a[], IT_ p[])
Inverts a matrix and returns its determinant.
Definition: math.hpp:1292
FEAT namespace.
Definition: adjactor.hpp:12
std::uint64_t Index
Index data type.