v0.16.0
Loading...
Searching...
No Matches
DGProjection.cpp
Go to the documentation of this file.
1/**
2 * @file DGProjection.cpp
3 */
4
5namespace MoFEM {
6
7static auto dg_save_range(moab::Interface &moab, const std::string name,
8 const Range r, std::vector<Tag> tags = {}) {
10 auto out_meshset = get_temp_meshset_ptr(moab);
11 CHKERR moab.add_entities(*out_meshset, r);
12 if (r.size()) {
13 CHKERR moab.write_file(name.c_str(), "VTK", "", out_meshset->get_ptr(), 1,
14 tags.data(), tags.size());
15 } else {
16 MOFEM_LOG("SELF", Sev::warning) << "Empty range for " << name;
17 }
19};
20
22
24 return 2 * (p + 1);
25 };
26
27 DGSetIntegration(boost::shared_ptr<Range> edges_ptr,
29 : edgesPtr(edges_ptr), funRule(fun_rule) {};
30
32 int order_col, int order_data) {
34
35 constexpr bool debug = false;
36
37 constexpr int numNodes = 3;
38 constexpr int numEdges = 3;
39 constexpr int refinementLevels = 1;
40
41 auto &m_field = fe_raw_ptr->mField;
42 auto fe_ptr = static_cast<Fe *>(fe_raw_ptr);
43 auto fe_handle = fe_ptr->getFEEntityHandle();
44 auto type = type_from_handle(fe_handle);
45 if (type != MBTRI) {
46 SETERRQ(PETSC_COMM_SELF, MOFEM_NOT_IMPLEMENTED,
47 "only MBTRI is implemented");
48 }
49
50
51
52 auto set_base_quadrature = [&]() {
54 int rule = funRule(order_row, order_col, order_data);
55 if (rule < QUAD_2D_TABLE_SIZE) {
56 if (QUAD_2D_TABLE[rule]->dim != 2) {
57 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY, "wrong dimension");
58 }
59 if (QUAD_2D_TABLE[rule]->order < rule) {
60 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
61 "wrong order %d != %d", QUAD_2D_TABLE[rule]->order, rule);
62 }
63 const size_t nb_gauss_pts = QUAD_2D_TABLE[rule]->npoints;
64 fe_ptr->gaussPts.resize(3, nb_gauss_pts, false);
65 cblas_dcopy(nb_gauss_pts, &QUAD_2D_TABLE[rule]->points[1], 3,
66 &fe_ptr->gaussPts(0, 0), 1);
67 cblas_dcopy(nb_gauss_pts, &QUAD_2D_TABLE[rule]->points[2], 3,
68 &fe_ptr->gaussPts(1, 0), 1);
69 cblas_dcopy(nb_gauss_pts, QUAD_2D_TABLE[rule]->weights, 1,
70 &fe_ptr->gaussPts(2, 0), 1);
71 auto &data = fe_ptr->dataOnElement[H1];
72 data->dataOnEntities[MBVERTEX][0].getN(NOBASE).resize(nb_gauss_pts, 3,
73 false);
74 double *shape_ptr =
75 &*data->dataOnEntities[MBVERTEX][0].getN(NOBASE).data().begin();
76 cblas_dcopy(3 * nb_gauss_pts, QUAD_2D_TABLE[rule]->points, 1, shape_ptr,
77 1);
78 data->dataOnEntities[MBVERTEX][0].getDiffN(NOBASE).resize(3, 2, false);
79 std::copy(
81 data->dataOnEntities[MBVERTEX][0].getDiffN(NOBASE).data().begin());
82
83 } else {
84 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
85 "rule > quadrature order %d < %d", rule, QUAD_3D_TABLE_SIZE);
86 }
88 };
89
90 CHKERR set_base_quadrature();
91
92 if (edgesPtr) {
93
94 auto get_edges = [&]() {
95 std::bitset<numEdges> edges;
96 for (int ee = 0; ee != numEdges; ee++) {
97 EntityHandle edge;
98 CHKERR m_field.get_moab().side_element(fe_handle, 1, ee, edge);
99 if (edgesPtr->find(edge) != edgesPtr->end()) {
100 edges.set(ee);
101 } else {
102 edges.reset(ee);
103 }
104 }
105 return edges;
106 };
107
108 auto refine_quadrature = [&]() {
110
111 const int max_level = refinementLevels;
112
113 moab::Core moab_ref;
114 double base_coords[] = {0, 0, 0, 1, 0, 0, 0, 1, 0};
115 EntityHandle nodes[numNodes];
116 for (int nn = 0; nn != numNodes; nn++)
117 CHKERR moab_ref.create_vertex(&base_coords[3 * nn], nodes[nn]);
118 EntityHandle tri;
119 CHKERR moab_ref.create_element(MBTRI, nodes, numNodes, tri);
120 MoFEM::CoreTmp<-1> mofem_ref_core(moab_ref, PETSC_COMM_SELF, -2);
121 MoFEM::Interface &m_field_ref = mofem_ref_core;
122 {
123 Range tris(tri, tri);
124 Range edges;
125 CHKERR m_field_ref.get_moab().get_adjacencies(tris, 1, true, edges,
126 moab::Interface::UNION);
127 CHKERR m_field_ref.getInterface<BitRefManager>()->setBitRefLevel(
128 tris, BitRefLevel().set(0), false, VERBOSE);
129 }
130
131 auto edges = get_edges();
132
133 EntityHandle meshset;
134 CHKERR moab_ref.create_meshset(MESHSET_SET, meshset);
135 for (int ee = 0; ee != numEdges; ee++) {
136 if (edges[ee]) {
137 EntityHandle ent;
138 CHKERR moab_ref.side_element(tri, 1, ee, ent);
139 CHKERR moab_ref.add_entities(meshset, &ent, 1);
140 }
141 }
142
143 // refine mesh
144 auto *m_ref = m_field_ref.getInterface<MeshRefinement>();
145 for (int ll = 0; ll != max_level; ll++) {
146 Range ref_edges;
147 CHKERR moab_ref.get_entities_by_type(meshset, MBEDGE, ref_edges,
148 true);
149 CHKERR m_field_ref.getInterface<BitRefManager>()
150 ->filterEntitiesByRefLevel(BitRefLevel().set(ll),
151 BitRefLevel().set(), ref_edges);
152
153 Range tris;
154 CHKERR m_field_ref.getInterface<BitRefManager>()
155 ->getEntitiesByTypeAndRefLevel(BitRefLevel().set(ll),
156 BitRefLevel().set(), MBTRI, tris);
157 CHKERR m_ref->addVerticesInTheMiddleOfEdges(
158 ref_edges, BitRefLevel().set(ll + 1));
159 CHKERR m_ref->refineTris(tris, BitRefLevel().set(ll + 1));
160 CHKERR m_field_ref.getInterface<BitRefManager>()
161 ->updateMeshsetByEntitiesChildren(
162 meshset, BitRefLevel().set(ll + 1), meshset, MBEDGE, true);
163 }
164
165 // get ref coords
166 Range tris;
167 CHKERR m_field_ref.getInterface<BitRefManager>()
168 ->getEntitiesByTypeAndRefLevel(BitRefLevel().set(max_level),
169 BitRefLevel().set(), MBTRI, tris);
170
171 if (debug) {
172 CHKERR dg_save_range(moab_ref, "ref_tris.vtk", tris);
173 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY, "debug");
174 }
175
176 MatrixDouble ref_coords(tris.size(), 9, false);
177 int tt = 0;
178 for (Range::iterator tit = tris.begin(); tit != tris.end();
179 tit++, tt++) {
180 int num_nodes;
181 const EntityHandle *conn;
182 CHKERR moab_ref.get_connectivity(*tit, conn, num_nodes, false);
183 CHKERR moab_ref.get_coords(conn, num_nodes, &ref_coords(tt, 0));
184 }
185
186 auto &data = fe_ptr->dataOnElement[H1];
187 const size_t nb_gauss_pts = fe_ptr->gaussPts.size2();
188 MatrixDouble ref_gauss_pts(3, nb_gauss_pts * ref_coords.size1());
189 MatrixDouble &shape_n = data->dataOnEntities[MBVERTEX][0].getN(NOBASE);
190 int gg = 0;
191 for (size_t tt = 0; tt != ref_coords.size1(); tt++) {
192 double *tri_coords = &ref_coords(tt, 0);
194 CHKERR Tools::getTriNormal(tri_coords, &t_normal(0));
195 auto det = t_normal.l2();
196 for (size_t ggg = 0; ggg != nb_gauss_pts; ++ggg, ++gg) {
197 for (int dd = 0; dd != 2; dd++) {
198 ref_gauss_pts(dd, gg) = shape_n(ggg, 0) * tri_coords[3 * 0 + dd] +
199 shape_n(ggg, 1) * tri_coords[3 * 1 + dd] +
200 shape_n(ggg, 2) * tri_coords[3 * 2 + dd];
201 }
202 ref_gauss_pts(2, gg) = fe_ptr->gaussPts(2, ggg) * det;
203 }
204 }
205
207 };
208
209 CHKERR refine_quadrature();
210 }
211
213 }
214
215private:
216 struct Fe : public ForcesAndSourcesCore {
218
219 private:
221 };
222
223 boost::shared_ptr<Range> edgesPtr;
224
225};
226
227template <>
231 boost::shared_ptr<Range> edges_ptr) {
233 set_hook = DGSetIntegration(edges_ptr, rule);
235}
236
238 int order, boost::shared_ptr<MatrixDouble> mass_ptr,
239 boost::shared_ptr<EntitiesFieldData> data_l2,
240 const FieldApproximationBase b, const FieldSpace s, int verb, Sev sev)
241 : OpBaseDerivativesBase(mass_ptr, data_l2, b, s, verb, sev),
242 baseOrder(order) {}
243
248
249 if (sPace != L2) {
250 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
251 "Space should be set to L2");
252 }
253
255
256 [this]() { return this->baseOrder; }
257
258 );
260
262}
263
265 boost::shared_ptr<MatrixDouble> data_ptr,
266 boost::shared_ptr<MatrixDouble> coeffs_ptr,
267 boost::shared_ptr<MatrixDouble> mass_ptr,
268 boost::shared_ptr<EntitiesFieldData> data_l2,
269 const FieldApproximationBase b, const FieldSpace s,
270 const LogManager::SeverityLevel sev, DataLayout input_layout)
271 : OpBaseDerivativesBase(mass_ptr, data_l2, b, s, VERBOSE, sev),
272 dataPtr(data_ptr), coeffsPtr(coeffs_ptr), inputLayout(input_layout) {}
273
278
279 const auto fe_type = getFEType();
280 constexpr auto side_number = 0;
281 auto &ent_data = dataL2->dataOnEntities[fe_type][side_number];
282 auto nb_base_functions = ent_data.getN(base).size2();
283
284 auto &rhs = *coeffsPtr;
285 auto &data = *dataPtr;
286
287 auto nb_integration_pts = getGaussPts().size2();
288#ifndef NDEBUG
289 auto nb_data_gauss_pts =
290 inputLayout == DataLayout::CoeffsByGauss ? data.size2() : data.size1();
291 if (nb_data_gauss_pts != nb_integration_pts) {
292 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
293 "Inconsistent number of integration points in data size (%zu, "
294 "%zu), and number of integration points %zu",
295 data.size1(), data.size2(), nb_integration_pts);
296 }
297#endif
298 auto nb_data_coeffs =
299 inputLayout == DataLayout::CoeffsByGauss ? data.size1() : data.size2();
300 rhs.resize(nb_base_functions, nb_data_coeffs, false);
301 rhs.clear();
302
304
305 // assemble rhs
306 auto t_base = ent_data.getFTensor0N(base, 0, 0);
307 auto t_w = getFTensor0IntegrationWeight();
308 for (auto gg = 0; gg != nb_integration_pts; ++gg) {
309 Tensor0 t_rhs(&*rhs.data().begin());
310 for (auto bb = 0; bb != nb_base_functions; ++bb) {
311 double alpha = t_w * t_base;
312 for (auto cc = 0; cc != nb_data_coeffs; ++cc) {
313 const auto r = inputLayout == DataLayout::CoeffsByGauss ? cc : gg;
314 const auto c = inputLayout == DataLayout::CoeffsByGauss ? gg : cc;
315 t_rhs += alpha * data(r, c);
316 ++t_rhs;
317 }
318 ++t_base;
319 }
320 ++t_w;
321 }
322
323 // solve for coefficients
324 for (auto cc = 0; cc != nb_data_coeffs; ++cc) {
325 ublas::matrix_column<MatrixDouble> mc(rhs, cc);
326 cholesky_solve(*baseMassPtr, mc, ublas::lower());
327 }
328
330}
331
333 boost::shared_ptr<MatrixDouble> data_ptr,
334 boost::shared_ptr<MatrixDouble> coeffs_ptr,
335 boost::shared_ptr<EntitiesFieldData> data_l2,
336 const FieldApproximationBase b, const FieldSpace s,
337 const LogManager::SeverityLevel sev, DataLayout output_layout)
338 : OpDGProjectionCoefficients(data_ptr, coeffs_ptr,
339 boost::make_shared<MatrixDouble>(), data_l2, b,
340 s, sev),
341 outputLayout(output_layout) {}
342
346
348
349 if (sPace != L2) {
350 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
351 "Space should be set to L2");
352 }
353
354 auto fe_type = getFEType();
355 constexpr auto side_number = 0;
356 auto order = dataL2->dataOnEntities[fe_type][side_number].getOrder();
358
359 [order]() { return order; }
360
361 );
362
363 auto &ent_data = dataL2->dataOnEntities[fe_type][side_number];
364 const auto nb_base_functions = ent_data.getN(base).size2();
365 auto &data = *dataPtr;
366 auto &coeffs = *coeffsPtr;
367
368 auto nb_data_coeffs = coeffs.size2();
369 auto nb_integration_pts = getGaussPts().size2();
370 data.resize(nb_integration_pts, nb_data_coeffs);
371 data.clear();
372
374
375 auto t_base = ent_data.getFTensor0N(base, 0, 0);
376 for (auto gg = 0; gg != nb_integration_pts; ++gg) {
377 Tensor0 t_coeffs(&*coeffs.data().begin());
378 for (auto bb = 0; bb != nb_base_functions; ++bb) {
379 double alpha = t_base;
380 Tensor0 t_data(&data(gg, 0));
381 for (auto cc = 0; cc != nb_data_coeffs; ++cc) {
382 t_data += alpha * t_coeffs;
383 ++t_data;
384 ++t_coeffs;
385 }
386 ++t_base;
387 }
388 }
389
391 *dataPtr = trans(*dataPtr);
392 }
393
395}
396} // namespace MoFEM
std::string type
void cholesky_solve(const TRIA &L, VEC &x, ublas::lower)
solve system L L^T x = b inplace
Definition cholesky.hpp:221
@ VERBOSE
FieldApproximationBase
approximation base
Definition definitions.h:58
@ NOBASE
Definition definitions.h:59
FieldSpace
approximation spaces
Definition definitions.h:82
@ L2
field with C-1 continuity
Definition definitions.h:88
@ H1
continuous field
Definition definitions.h:85
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
@ MOFEM_DATA_INCONSISTENCY
Definition definitions.h:31
@ MOFEM_NOT_IMPLEMENTED
Definition definitions.h:32
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
constexpr int order
#define MOFEM_LOG(channel, severity)
Log.
SeverityLevel
Severity levels.
const double c
speed of light (cm/ns)
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
std::bitset< BITREFLEVEL_SIZE > BitRefLevel
Bit structure attached to each entity identifying to what mesh entity is attached.
Definition Types.hpp:40
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
auto type_from_handle(const EntityHandle h)
get type from entity handle
static const bool debug
MoFEMErrorCode setDGSetIntegrationPoints< 2 >(ForcesAndSourcesCore::GaussHookFun &set_hook, ForcesAndSourcesCore::RuleHookFun rule, boost::shared_ptr< Range > edges_ptr)
static auto dg_save_range(moab::Interface &moab, const std::string name, const Range r, std::vector< Tag > tags={})
auto get_temp_meshset_ptr(moab::Interface &moab)
Create smart pointer to temporary meshset.
#define QUAD_2D_TABLE_SIZE
Definition quad.h:174
#define QUAD_3D_TABLE_SIZE
Definition quad.h:186
static QUAD *const QUAD_2D_TABLE[]
Definition quad.h:175
Managing BitRefLevels.
virtual moab::Interface & get_moab()=0
DGSetIntegration(boost::shared_ptr< Range > edges_ptr, ForcesAndSourcesCore::RuleHookFun fun_rule)
boost::shared_ptr< Range > edgesPtr
MoFEMErrorCode operator()(ForcesAndSourcesCore *fe_raw_ptr, int order_row, int order_col, int order_data)
ForcesAndSourcesCore::RuleHookFun funRule
Deprecated interface functions.
Data on single entity (This is passed as argument to DataOperator::doWork)
EntityHandle getFEEntityHandle() const
Get the entity handle of the current finite element.
EntityType getFEType() const
Get dimension of finite element.
auto getFTensor0IntegrationWeight()
Get integration weights.
MatrixDouble & getGaussPts()
matrix of integration (Gauss) points for Volume Element
structure to get information from mofem into EntitiesFieldData
ForcesAndSourcesCore(Interface &m_field)
boost::function< MoFEMErrorCode(ForcesAndSourcesCore *fe_raw_ptr, int order_row, int order_col, int order_data)> GaussHookFun
const std::array< boost::shared_ptr< EntitiesFieldData >, LASTSPACE > dataOnElement
Entity data on element entity rows fields.
boost::function< int(int order_row, int order_col, int order_data)> RuleHookFun
Mesh refinement interface.
boost::shared_ptr< EntitiesFieldData > dataL2
MoFEMErrorCode calculateBase(GetOrderFun get_order)
boost::shared_ptr< MatrixDouble > baseMassPtr
Evaluate field for given DG projection coefficients.
boost::shared_ptr< MatrixDouble > coeffsPtr
boost::shared_ptr< MatrixDouble > dataPtr
OpDGProjectionCoefficients(boost::shared_ptr< MatrixDouble > data_ptr, boost::shared_ptr< MatrixDouble > coeffs_ptr, boost::shared_ptr< MatrixDouble > mass_ptr, boost::shared_ptr< EntitiesFieldData > data_l2, const FieldApproximationBase b, const FieldSpace s, const LogManager::SeverityLevel sev=Sev::noisy, DataLayout input_layout=DataLayout::GaussByCoeffs)
MoFEMErrorCode doWork(int side, EntityType type, EntitiesFieldData::EntData &data)
Operator for linear form, usually to calculate values on right hand side.
MoFEMErrorCode doWork(int side, EntityType type, EntitiesFieldData::EntData &data)
Operator for linear form, usually to calculate values on right hand side.
OpDGProjectionEvaluation(boost::shared_ptr< MatrixDouble > data_ptr, boost::shared_ptr< MatrixDouble > coeffs_ptr, boost::shared_ptr< EntitiesFieldData > data_l2, const FieldApproximationBase b, const FieldSpace s, const LogManager::SeverityLevel sev=Sev::noisy, DataLayout output_layout=DataLayout::GaussByCoeffs)
MoFEMErrorCode doWork(int side, EntityType type, EntitiesFieldData::EntData &data)
Operator for linear form, usually to calculate values on right hand side.
OpDGProjectionMassMatrix(int order, boost::shared_ptr< MatrixDouble > mass_ptr, boost::shared_ptr< EntitiesFieldData > data_l2, const FieldApproximationBase b, const FieldSpace s, int verb=QUIET, Sev sev=Sev::verbose)
static constexpr std::array< double, 6 > diffShapeFunMBTRI
Definition Tools.hpp:104
static MoFEMErrorCode getTriNormal(const double *coords, double *normal, double *d_normal=nullptr)
Get the Tri Normal objectGet triangle normal.
Definition Tools.cpp:353
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.
int npoints
Definition quad.h:29