v0.16.3
Loading...
Searching...
No Matches
DGProjectionInterface.cpp
Go to the documentation of this file.
1/** \file DGProjectionInterface.cpp
2 * \brief Request-local field evaluation and element-local L2 projection.
3 */
4#include <MoFEM.hpp>
5#include <cholesky.hpp>
6#include <algorithm>
7#include <array>
8#include <cmath>
9#include <limits>
10#include <map>
11#include <set>
12
13namespace MoFEM {
14namespace {
15
16using Projection = DGProjectionInterface;
17using UDO = ForcesAndSourcesCore::UserDataOperator;
18using EntData = EntitiesFieldData::EntData;
19using FieldRequest = Projection::Request<Projection::FieldMap>;
20using FieldResult = Projection::Report<Projection::FieldReport>;
21using TagRequest = Projection::Request<Projection::TagMap>;
22using TagResult = Projection::Report<Projection::TagReport>;
23
25
26/// Values used where a field map has no source data at a target point.
27struct SamplePolicy {
28 std::vector<double> empty; ///< One value per component.
29 bool finiteSource = true; ///< Reject nonfinite source values.
30};
31
32/// Metadata and integration-point buffers for one field map.
33struct ProjectedField {
34 const Field *source = nullptr, *target = nullptr;
35 SamplePolicy policy;
36 boost::shared_ptr<FieldEntity> targetEntity;
37 boost::shared_ptr<MatrixDouble> sourceValues =
38 boost::make_shared<MatrixDouble>();
40 double residual = 0.;
41};
42
43/// Query coordinates and offsets shared by every map in the request.
44struct TargetPoints {
45 std::vector<std::array<double, 3>> coordinates;
46 std::map<EntityHandle, size_t> offsets;
47};
48
49/// One source/target tag pair and its element-local projection data.
50struct ProjectedTag {
51 std::string sourceName, targetName;
52 int order = 0; ///< L2 order shared by the request.
53 int components = 0; ///< Inferred from the source tag length.
54 Tag source = 0, target = 0;
55 bool created = false;
56 int targetLength = -1; ///< Values held by a fixed-length target tag.
57 SamplePolicy policy;
58 boost::shared_ptr<MatrixDouble> sourceValues =
59 boost::make_shared<MatrixDouble>();
61 std::map<EntityHandle, MatrixDouble> sourceCoefficients;
62 std::map<EntityHandle, MatrixDouble> targetCoefficients;
63 unsigned long long withoutData = 0;
64 double sourceResidual = 0.;
65 double targetResidual = 0.;
66 std::map<EntityHandle, std::vector<double>> staged;
67 std::map<EntityHandle, std::pair<bool, std::vector<double>>> previous;
68};
69
70/// Values of a source tag on one element; false where the element holds no
71/// data or only the default value of a sparse tag. A value equal to the
72/// default but of the expected length is data.
73bool sourceTagData(moab::Interface &mb, Tag tag, EntityHandle ent,
74 const double *def, int def_size, size_t expected,
75 const double *&data, int &size) {
76 data = nullptr;
77 size = 0;
78 if (mb.tag_get_by_ptr(tag, &ent, 1, (const void **)&data, &size) !=
79 MB_SUCCESS ||
80 !data || !size)
81 return false;
82 const bool is_default =
83 def && (data == def ||
84 (static_cast<size_t>(size) != expected && size == def_size &&
85 std::equal(data, data + size, def, [](double a, double b) {
86 return a == b || (std::isnan(a) && std::isnan(b));
87 })));
88 return !is_default;
89}
90
91bool allFinite(const MatrixDouble &values) {
92 return std::all_of(values.data().begin(), values.data().end(),
93 [](double value) { return std::isfinite(value); });
94}
95
96/// Algebraic residual of a factorised local mass solve, independent of
97/// quadrature accuracy.
98double massSolveResidual(const MatrixDouble &n, const MatrixDouble &gauss_pts,
99 const MatrixDouble &values, const MatrixDouble &factor,
100 const MatrixDouble &coefficients) {
101 MatrixDouble weighted(values);
102 const auto weights = gauss_pts.size1() - 1;
103 for (size_t q = 0; q != weighted.size1(); ++q)
104 for (size_t c = 0; c != weighted.size2(); ++c)
105 weighted(q, c) *= gauss_pts(weights, q);
106 MatrixDouble rhs = prod(trans(n), weighted);
107 MatrixDouble lower(factor);
108 for (size_t i = 0; i != lower.size1(); ++i)
109 for (size_t j = i + 1; j != lower.size2(); ++j)
110 lower(i, j) = 0.;
111 MatrixDouble tmp = prod(trans(lower), coefficients);
112 MatrixDouble residual = prod(lower, tmp) - rhs;
113 return static_cast<double>(norm_frobenius(residual)) /
114 std::max(static_cast<double>(norm_frobenius(rhs)),
115 std::numeric_limits<double>::min());
116}
117
118/// Build the same DG mass as OpDGProjectionMassMatrix with element-local order.
119struct OpElementMass : OpBaseDerivativesBase {
121 ProjectedField &field;
122 OpElementMass(Interface &m_field, ProjectedField &f,
123 boost::shared_ptr<MatrixDouble> mass,
124 boost::shared_ptr<EntitiesFieldData> basis)
125 : OpBaseDerivativesBase(mass, basis, f.target->getApproxBase(), L2),
126 mField(m_field), field(f) {}
127 MoFEMErrorCode doWork(int, EntityType, EntData &) override {
129 const auto &entities = mField.get_field_ents()->get<Unique_mi_tag>();
131 field.target->getBitNumber(), getFEEntityHandle());
132 const auto it = entities.find(uid);
133 if (it == entities.end())
134 SETERRQ(PETSC_COMM_SELF, MOFEM_NOT_FOUND,
135 "Target field entity is missing");
136 field.targetEntity = *it;
137 CHKERR calculateBase([&]() { return field.targetEntity->getMaxOrder(); });
138 CHKERR calculateMass();
140 }
141};
142
143/// Factorised mass of the tag reconstruction basis on the source rule.
144/// Rejects rules whose points cannot determine a polynomial of this order.
145struct OpReconstructionMass : OpBaseDerivativesBase {
146 int order;
147 OpReconstructionMass(int o, FieldApproximationBase base,
148 boost::shared_ptr<MatrixDouble> mass,
149 boost::shared_ptr<EntitiesFieldData> basis)
150 : OpBaseDerivativesBase(mass, basis, base, NOSPACE), order(o) {}
151 MoFEMErrorCode doWork(int, EntityType, EntData &) override {
153 CHKERR calculateBase([this]() { return order; });
154 auto &ent_data = dataL2->dataOnEntities[getFEType()][0];
155 const auto &n = ent_data.getN(base);
156 auto &mass = *baseMassPtr;
157 const auto nb = n.size2();
158 mass.resize(nb, nb, false);
159 mass.clear();
160 auto t_w = getFTensor0IntegrationWeight();
161 auto t_row_base = ent_data.getFTensor0N();
162 for (size_t gg = 0; gg != n.size1(); ++gg) {
163 const double alpha = t_w;
164 for (size_t rr = 0; rr != nb; ++rr) {
165 auto a_mat_ptr = &mass(rr, 0);
166 auto t_col_base = ent_data.getFTensor0N(gg, 0);
167 for (size_t cc = 0; cc <= rr; ++cc) {
168 *a_mat_ptr += alpha * (t_row_base * t_col_base);
169 ++t_col_base;
170 ++a_mat_ptr;
171 }
172 ++t_row_base;
173 }
174 ++t_w;
175 }
176 // An exact zero pivot fails the factorisation; roundoff leaves a tiny one.
177 bool singular = cholesky_decompose(mass) != 0;
178 double largest = 0.;
179 for (size_t i = 0; i != nb && !singular; ++i)
180 largest = std::max(largest, mass(i, i));
181 for (size_t i = 0; i != nb && !singular; ++i)
182 singular = !std::isfinite(mass(i, i)) || !(mass(i, i) > 1.e-6 * largest);
183 if (singular)
184 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
185 "Source integration rule cannot determine the order-%d tag "
186 "reconstruction",
187 order);
189 }
190};
191
192/// Prepare an L2 basis for field-free evaluation at arbitrary points.
193struct OpTagBasis : OpBaseDerivativesBase {
194 int order;
195 OpTagBasis(int o, boost::shared_ptr<EntitiesFieldData> basis)
196 : OpBaseDerivativesBase(boost::make_shared<MatrixDouble>(), basis,
197 tagBase, NOSPACE),
198 order(o) {}
199 MoFEMErrorCode doWork(int, EntityType, EntData &) override {
201 CHKERR calculateBase([this]() { return order; });
203 }
204};
205
206template <typename FE>
207MoFEMErrorCode loopSelectedCells(DM dm, const std::string &element,
208 const boost::shared_ptr<FE> &fe,
209 const Range &cells) {
210 fe->exeTestHook = [&cells](FEMethod *method) {
211 return cells.find(method->numeredEntFiniteElementPtr->getEnt()) !=
212 cells.end();
213 };
214 return DMoFEMLoopFiniteElements(dm, element, fe);
215}
216
217/// Own only bookkeeping; each project call releases its local working state.
218struct DGProjectionInterfaceImpl final : DGProjectionInterface {
221 unsigned long long sequence = 0;
222 explicit DGProjectionInterfaceImpl(const Core &core)
223 : mCore(const_cast<Core &>(core)), mField(mCore) {}
224 MoFEMErrorCode query_interface(boost::typeindex::type_index,
225 UnknownInterface **iface) const override {
226 *iface = const_cast<DGProjectionInterfaceImpl *>(this);
227 return 0;
228 }
229
230 /// Preserve the named local stage and propagate its error on this rank.
231 template <typename F> MoFEMErrorCode phase(const char *name, F work) const {
233 const auto error = work();
234 if (error)
235 SETERRQ(PETSC_COMM_SELF, error, "DG projection: %s failed", name);
237 }
238
239 MoFEMErrorCode project(const FieldRequest &, FieldResult &) override;
240 MoFEMErrorCode project(const TagRequest &, TagResult &) override;
241
242 template <typename F> MoFEMErrorCode preservingBuildState(F work) {
244 const int build_state = mCore.getBuildMoFEM();
245 const auto error = work();
246 mCore.getBuildMoFEM() = build_state;
247 CHKERR error;
249 }
250 MoFEMErrorCode reserveProblemName(std::string &name);
251 MoFEMErrorCode selectCells(const MeshLevel &, const MeshLevel &, Range &,
252 Range &, int &);
253 MoFEMErrorCode transferFields(const FieldRequest &,
254 const std::vector<SamplePolicy> &,
255 FieldResult &);
256 MoFEMErrorCode validate(const FieldRequest &,
257 const std::vector<SamplePolicy> &, Range &, Range &,
258 std::vector<ProjectedField> &, int &);
259 MoFEMErrorCode createSubDM(DM, const std::string &, const MeshLevel &,
260 const MeshLevel &, const std::string &,
261 SmartPetscObj<DM> &);
262 template <int D>
263 MoFEMErrorCode projectImpl(const FieldRequest &, DM,
264 const std::string &, const Range &,
265 std::vector<ProjectedField> &, bool, FieldResult &);
266 MoFEMErrorCode validateTags(const TagRequest &, std::vector<ProjectedTag> &);
267 template <int D>
268 MoFEMErrorCode localTagComponents(const TagRequest &, DM, const Range &,
269 const std::vector<ProjectedTag> &,
270 std::vector<int> &);
271 MoFEMErrorCode inferTagComponents(const TagRequest &, DM, const Range &,
272 const Range &, int,
273 std::vector<ProjectedTag> &);
274 MoFEMErrorCode transferTags(const TagRequest &, TagResult &);
275 template <int D>
276 MoFEMErrorCode reconstructTags(const TagRequest &, DM, const Range &,
277 std::vector<ProjectedTag> &);
278 template <int D>
279 MoFEMErrorCode projectTagsImpl(const TagRequest &, DM, const std::string &,
280 const Range &,
281 std::vector<ProjectedTag> &, bool,
282 TagResult &);
283 template <int D>
284 MoFEMErrorCode evaluateTags(const TagRequest &, DM, const Range &,
285 std::vector<ProjectedTag> &);
286 MoFEMErrorCode publishTags(std::vector<ProjectedTag> &, const Range &);
287};
288
289/// Choose a temporary problem name unused on this rank.
290MoFEMErrorCode DGProjectionInterfaceImpl::reserveProblemName(
291 std::string &name) {
293 do {
294 name = "__DG_PROJECTION_" + std::to_string(++sequence);
297 break;
298 } while (true);
300}
301
302/// Select local source and target cells; either range may be empty.
303MoFEMErrorCode DGProjectionInterfaceImpl::selectCells(
304 const MeshLevel &source_level, const MeshLevel &target_level, Range &source,
305 Range &target, int &dimension) {
307 auto &mb = mField.get_moab();
308 int target_dim = 0;
309 CHKERR phase("mesh selection", [&]() {
311 for (const auto *level : {&source_level, &target_level})
312 if (!level->bit.any() || (level->bit & level->mask) != level->bit)
313 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
314 "Invalid mesh selectors");
315 CHKERR mField.getInterface<BitRefManager>()->getEntitiesByRefLevel(
316 source_level.bit, source_level.mask, source);
317 CHKERR mField.getInterface<BitRefManager>()->getEntitiesByRefLevel(
318 target_level.bit, target_level.mask, target);
319 for (auto e : target)
320 if (mb.type_from_handle(e) != MBENTITYSET)
321 target_dim = std::max(target_dim, mb.dimension_from_handle(e));
323 });
324 dimension = target_dim;
325 // A rank may hold only lower-dimensional closure entities and no target cell.
326 if (dimension < 2) {
327 source.clear();
328 target.clear();
330 }
331 source = source.subset_by_dimension(dimension);
332 target = target.subset_by_dimension(dimension);
334}
335
336/// Select local cells and check field metadata needed by the projection.
337MoFEMErrorCode DGProjectionInterfaceImpl::validate(
338 const FieldRequest &request, const std::vector<SamplePolicy> &policies,
339 Range &source, Range &target, std::vector<ProjectedField> &fields,
340 int &dimension) {
342 CHKERR selectCells(request.source, request.target, source, target, dimension);
343 CHKERR phase("request validation", [&]() {
345 if (request.maps.empty() || policies.size() != request.maps.size())
346 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
347 "Invalid field maps");
348 if (request.options.quadratureOrder < 0)
349 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
350 "DG projection requires a caller-supplied quadrature order");
351 std::set<std::string> destinations;
352 for (size_t m = 0; m != request.maps.size(); ++m) {
353 const auto &[source_name, target_name] = request.maps[m];
354 if (source_name.empty() || target_name.empty() ||
355 !destinations.insert(target_name).second ||
356 !mField.check_field(source_name) || !mField.check_field(target_name))
357 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
358 "Missing field or duplicate projection destination");
359 ProjectedField field;
360 field.source = mField.get_field_structure(source_name);
361 field.target = mField.get_field_structure(target_name);
362 field.policy = policies[m];
363 const auto components = field.source->getNbOfCoeffs();
364 if ((field.source->getSpace() != H1 && field.source->getSpace() != L2) ||
365 field.target->getSpace() != L2)
366 SETERRQ(PETSC_COMM_SELF, MOFEM_NOT_IMPLEMENTED,
367 "DG projection supports H1/L2 sources and L2 targets");
368 if (components != field.target->getNbOfCoeffs())
369 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
370 "Source/target component counts differ");
371 if (components < 1 || components > 9)
372 SETERRQ(PETSC_COMM_SELF, MOFEM_NOT_IMPLEMENTED,
373 "DG projection supports 1 through 9 components");
374 if (field.policy.empty.size() != static_cast<size_t>(components))
375 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
376 "Empty value does not match the component count");
377 fields.push_back(std::move(field));
378 }
380 });
382}
383
384/// Build one local-loop problem covering both selected mesh levels.
385MoFEMErrorCode DGProjectionInterfaceImpl::createSubDM(
386 DM parent, const std::string &name, const MeshLevel &source,
387 const MeshLevel &target, const std::string &element,
388 SmartPetscObj<DM> &subdm) {
390 MPI_Comm comm;
391 CHKERR PetscObjectGetComm((PetscObject)parent, &comm);
392 subdm = createDM(comm, "DMMOFEM");
393 CHKERR DMMoFEMCreateSubDM(subdm, parent, name.c_str());
396 source.mask | target.mask);
397 CHKERR DMMoFEMSetSquareProblem(subdm, PETSC_TRUE);
398 CHKERR DMMoFEMAddElement(subdm, element);
399 CHKERR DMSetUp(subdm);
400 const Problem *problem;
401 CHKERR DMMoFEMGetProblemPtr(subdm, &problem);
402 auto &elements = *problem->numeredFiniteElementsPtr;
403 for (auto it = elements.begin(); it != elements.end();) {
404 if (!elements.modify(
405 it++, NumeredEntFiniteElement_change_part(mField.get_comm_rank())))
406 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
407 "Could not assign local projection FE ownership");
408 }
410}
411
412/// Install the fixed-component evaluator behind runtime field metadata.
413/// An empty vector argument makes it read the local element field data.
414template <int N = 1>
415UDO *valuesOperator(int components, const std::string &name,
416 boost::shared_ptr<MatrixDouble> values) {
417 if (components == N)
418 return new OpCalculateVectorFieldValues<N>(name, values,
419 SmartPetscObj<Vec>());
420 if constexpr (N < 9)
421 return valuesOperator<N + 1>(components, name, values);
422 THROW_MESSAGE("Unsupported projection component count");
423}
424
425/// Use explicit points when given; otherwise the order of a standard rule.
426template <typename FE>
427void installRule(FE &fe, const Projection::IntegrationRule &rule) {
428 if (rule.setRuleHook) {
429 fe.getRuleHook = [](int, int, int) { return -1; };
430 fe.setRuleHook = rule.setRuleHook;
431 } else {
432 fe.getRuleHook = rule.ruleHook;
433 }
434}
435
436/// Collect target quadrature once, then reuse it for sampling and all local solves.
437template <int D>
438MoFEMErrorCode DGProjectionInterfaceImpl::projectImpl(
439 const FieldRequest &request, DM projection_dm,
440 const std::string &problem_name, const Range &target_cells,
441 std::vector<ProjectedField> &fields, bool has_source, FieldResult &report) {
443 using Element = typename PipelineManager::ElementsAndOpsByDim<D>::DomainEle;
444 TargetPoints points;
445 auto target_fe = boost::make_shared<Element>(mField);
446 target_fe->getRuleHook =
447 [order = request.options.quadratureOrder](int, int, int) { return order; };
448 auto collect = new UDO(NOSPACE, UDO::OPSPACE);
449 collect->doWorkRhsHook = [&](DataOperator *base, int, EntityType, EntData &) {
451 auto op = static_cast<UDO *>(base);
452 const auto &coords = op->getCoordsAtGaussPts();
453 points.offsets.emplace(op->getFEEntityHandle(), points.coordinates.size());
454 for (size_t gg = 0; gg != coords.size1(); ++gg)
455 points.coordinates.push_back(
456 {coords(gg, 0), coords(gg, 1), coords(gg, 2)});
458 };
459 target_fe->getOpPtrVector().push_back(collect);
460 CHKERR phase("target quadrature", [&]() {
461 return loopSelectedCells(projection_dm, request.elementName, target_fe,
462 target_cells);
463 });
464 report.targetElements = points.offsets.size();
465 report.sampledPoints = points.coordinates.size();
466 auto evaluator = mField.getInterface<FieldEvaluatorInterface>();
467 auto search = evaluator->getData<Element>();
468 auto located_coordinates = boost::make_shared<MatrixDouble>();
469 double source_scale = 0.;
470 auto cache = boost::make_shared<CacheTuple>();
471 // A rank without source cells leaves all of its target points uncovered.
472 const bool search_source = has_source && !points.coordinates.empty();
473 CHKERR phase("source search", [&]() {
475 CHKERR mField.cache_problem_entities(problem_name, cache);
476 if (search_source) {
477 CHKERR evaluator->buildTree<D>(search, request.elementName, request.source.bit,
478 request.source.mask);
479 double lower[3], upper[3];
480 unsigned depth;
481 CHKERR search->treePtr->get_info(search->rooTreeSet, lower, upper, depth);
482 for (int d = 0; d != 3; ++d)
483 source_scale = std::hypot(source_scale, upper[d] - lower[d]);
484 }
485 search->feMethodPtr->getRuleHook = [](int, int, int) { return -1; };
486 auto locate_check = new UDO(NOSPACE, UDO::OPSPACE);
487 locate_check->doWorkRhsHook = [located_coordinates](DataOperator *base, int,
488 EntityType, EntData &) {
489 *located_coordinates = static_cast<UDO *>(base)->getCoordsAtGaussPts();
490 return MoFEMErrorCode(0);
491 };
492 search->feMethodPtr->getOpPtrVector().push_back(locate_check);
493 for (auto &field : fields) {
494 field.samples.resize(points.coordinates.size(),
495 field.source->getNbOfCoeffs(), false);
496 search->feMethodPtr->getOpPtrVector().push_back(
497 valuesOperator(field.source->getNbOfCoeffs(), field.source->getName(),
498 field.sourceValues));
499 }
501 });
502 unsigned long long missing = 0;
503 CHKERR phase("source sampling", [&]() {
505 std::vector<char> sampled(points.coordinates.size(), false);
506 for (size_t q = 0; q != points.coordinates.size(); ++q) {
507 for (auto &field : fields)
508 for (size_t c = 0; c != field.samples.size2(); ++c)
509 field.samples(q, c) = field.policy.empty[c];
510 }
511 if (search_source)
512 CHKERR evaluator->evalFEAtPoints<D>(
513 points.coordinates, 1.e-10, problem_name, request.elementName,
514 search, 0, mField.get_comm_size(), request.source.bit,
515 request.source.mask, cache,
516 [&](const std::vector<size_t> &indices) {
518 if (located_coordinates->size1() != indices.size() ||
519 located_coordinates->size2() != 3)
520 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
521 "Invalid located-point coordinates");
522 for (size_t row = 0; row != indices.size(); ++row) {
523 const auto q = indices[row];
524 const auto &x = points.coordinates[q];
525 const moab::CartVect back(&(*located_coordinates)(row, 0));
526 const moab::CartVect query(x.data());
527 double coordinate_scale = 0.;
528 for (double value : x)
529 coordinate_scale = std::max(coordinate_scale, std::abs(value));
530 const double physical_tolerance = std::max(
531 1.e-10 * source_scale,
532 64 * std::numeric_limits<double>::epsilon() *
533 coordinate_scale);
534 if (!std::isfinite((back - query).length()) ||
535 (back - query).length() > physical_tolerance)
536 continue;
537 for (auto &field : fields) {
538 const auto &values = *field.sourceValues;
539 if (values.size1() != indices.size() ||
540 values.size2() != field.source->getNbOfCoeffs())
541 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
542 "Inconsistent source sample layout");
543 for (size_t c = 0; c != values.size2(); ++c) {
544 if (field.policy.finiteSource &&
545 !std::isfinite(values(row, c)))
546 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
547 "Nonfinite source field value");
548 field.samples(q, c) = values(row, c);
549 }
550 }
551 sampled[q] = true;
552 }
554 },
555 MF_EXIST, QUIET);
556 for (auto valid : sampled)
557 if (!valid)
558 ++missing;
560 });
561 report.missingPoints = missing;
562 target_fe->getOpPtrVector().clear();
563 for (auto &field : fields) {
564 auto mass = boost::make_shared<MatrixDouble>();
565 auto basis = boost::make_shared<EntitiesFieldData>(MBENTITYSET);
566 auto values = boost::make_shared<MatrixDouble>();
567 auto coefficients = boost::make_shared<MatrixDouble>();
568 auto load = new UDO(NOSPACE, UDO::OPSPACE);
569 load->doWorkRhsHook = [&points, &field, values](DataOperator *base, int,
570 EntityType, EntData &) {
572 auto op = static_cast<UDO *>(base);
573 const auto offset = points.offsets.at(op->getFEEntityHandle());
574 const auto count = op->getGaussPts().size2();
575 values->resize(count, field.samples.size2(), false);
576 for (size_t q = 0; q != count; ++q)
577 for (size_t c = 0; c != values->size2(); ++c)
578 (*values)(q, c) = field.samples(offset + q, c);
580 };
581 target_fe->getOpPtrVector().push_back(load);
582 target_fe->getOpPtrVector().push_back(
583 new OpElementMass(mField, field, mass, basis));
584 target_fe->getOpPtrVector().push_back(new OpDGProjectionCoefficients(
585 values, coefficients, mass, basis, field.target->getApproxBase(), L2));
586 auto store = new UDO(NOSPACE, UDO::OPSPACE);
587 store->doWorkRhsHook = [&field, values, coefficients, mass, basis](
588 DataOperator *base, int, EntityType, EntData &) {
590 auto op = static_cast<UDO *>(base);
591 auto destination = field.targetEntity->getEntFieldData();
592 if (coefficients->data().size() != destination.size())
593 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
594 "Target coefficient layout mismatch");
595 if (allFinite(*values) && allFinite(*coefficients)) {
596 const auto &n = basis->dataOnEntities[op->getFEType()][0].getN(
597 field.target->getApproxBase());
598 field.residual =
599 std::max(field.residual, massSolveResidual(n, op->getGaussPts(),
600 *values, *mass,
601 *coefficients));
602 }
603 std::copy(coefficients->data().begin(), coefficients->data().end(),
604 destination.begin());
606 };
607 target_fe->getOpPtrVector().push_back(store);
608 }
609 CHKERR phase("local projection", [&]() {
610 return loopSelectedCells(projection_dm, request.elementName, target_fe,
611 target_cells);
612 });
613 for (const auto &field : fields)
614 report.items.push_back({field.target->getName(), field.residual});
616}
617
618/// Transfer fields through one subproblem of the caller's DM.
620DGProjectionInterfaceImpl::transferFields(const FieldRequest &request,
621 const std::vector<SamplePolicy> &policies,
622 FieldResult &report) {
624 report = {};
626 std::vector<ProjectedField> fields;
627 int dimension;
628 CHKERR validate(request, policies, source, target, fields, dimension);
629 if (!request.dm || request.elementName.empty() ||
630 !mField.check_finite_element(request.elementName))
631 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
632 "Projection requires a MoFEM DM and existing finite element");
633 std::string problem_name;
634 CHKERR reserveProblemName(problem_name);
635 SmartPetscObj<DM> projection_dm;
636 CHKERR createSubDM(request.dm, problem_name, request.source, request.target,
637 request.elementName, projection_dm);
638 if (!target.empty()) {
639 if (dimension == 2)
640 CHKERR projectImpl<2>(request, projection_dm, problem_name, target,
641 fields, !source.empty(), report);
642 else
643 CHKERR projectImpl<3>(request, projection_dm, problem_name, target,
644 fields, !source.empty(), report);
645 }
647}
648
649MoFEMErrorCode DGProjectionInterfaceImpl::project(const FieldRequest &request,
650 FieldResult &report) {
652 std::vector<SamplePolicy> policies;
653 for (const auto &[source_name, target_name] : request.maps) {
654 SamplePolicy policy;
655 if (mField.check_field(source_name))
656 policy.empty.assign(
658 request.options.emptyValue);
659 policies.push_back(policy);
660 }
661 CHKERR preservingBuildState(
662 [&]() { return transferFields(request, policies, report); });
664}
665
666/// Check local tag maps, rules and tag metadata.
668DGProjectionInterfaceImpl::validateTags(const TagRequest &request,
669 std::vector<ProjectedTag> &tags) {
671 auto &mb = mField.get_moab();
672 CHKERR phase("tag request validation", [&]() {
674 const auto has_rule = [](const IntegrationRule &rule) {
675 return static_cast<bool>(rule.ruleHook) ||
676 static_cast<bool>(rule.setRuleHook);
677 };
678 const auto &[pairs, order] = request.maps;
679 if (pairs.empty() || request.options.quadratureOrder < 0)
680 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
681 "Tag projection requires maps and a quadrature order");
682 if (order < 0)
683 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
684 "Negative tag projection order");
685 if (!has_rule(request.options.sourceRule) ||
686 !has_rule(request.options.targetRule))
687 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
688 "Tag projection requires source and target integration rules");
689 // Validate the source and any existing destination tag.
690 const auto describe = [&](Tag tag) {
692 DataType type;
693 CHKERR mb.tag_get_data_type(tag, type);
694 if (type != MB_TYPE_DOUBLE)
695 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
696 "Projected tags must hold doubles");
698 };
699 std::set<std::string> destinations;
700 for (const auto &[source_name, target_name] : pairs) {
701 if (source_name.empty() || target_name.empty() ||
702 !destinations.insert(target_name).second)
703 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
704 "Missing tag name or duplicate projection destination");
705 ProjectedTag tag;
706 tag.sourceName = source_name;
707 tag.targetName = target_name;
708 tag.order = order;
709 if (mb.tag_get_handle(source_name.c_str(), tag.source) != MB_SUCCESS)
710 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA, "Missing source tag %s",
711 source_name.c_str());
712 CHKERR describe(tag.source);
713 const bool target_exists =
714 mb.tag_get_handle(target_name.c_str(), tag.target) == MB_SUCCESS;
715 if (!target_exists)
716 tag.target = 0;
717 if (target_exists)
718 CHKERR describe(tag.target);
719 tags.push_back(std::move(tag));
720 }
722 });
724}
725
726/// Values per source rule point of each tag on local cells: 0 without data,
727/// -1 where the length does not divide or differs between elements. A
728/// fixed-length tag gives its length on every element.
729template <int D>
730MoFEMErrorCode DGProjectionInterfaceImpl::localTagComponents(
731 const TagRequest &request, DM projection_dm, const Range &source_cells,
732 const std::vector<ProjectedTag> &tags, std::vector<int> &local) {
734 using Element = typename PipelineManager::ElementsAndOpsByDim<D>::DomainEle;
735 auto &mb = mField.get_moab();
736 std::vector<const double *> defaults(tags.size(), nullptr);
737 std::vector<int> default_sizes(tags.size(), 0);
738 std::vector<int> fixed_lengths(tags.size(), -1);
739 for (size_t t = 0; t != tags.size(); ++t) {
740 int length = 0;
741 if (mb.tag_get_length(tags[t].source, length) != MB_VARIABLE_DATA_LENGTH)
742 fixed_lengths[t] = length;
743 const void *def = nullptr;
744 if (mb.tag_get_default_value(tags[t].source, def, default_sizes[t]) ==
745 MB_SUCCESS)
746 defaults[t] = static_cast<const double *>(def);
747 }
748 auto fe = boost::make_shared<Element>(mField);
749 installRule(*fe, request.options.sourceRule);
750 auto count = new UDO(NOSPACE, UDO::OPSPACE);
751 count->doWorkRhsHook = [&](DataOperator *base, int, EntityType, EntData &) {
752 auto op = static_cast<UDO *>(base);
753 const auto ent = op->getFEEntityHandle();
754 const int points = op->getGaussPts().size2();
755 for (size_t t = 0; t != tags.size(); ++t) {
756 const double *data = nullptr;
757 int size = fixed_lengths[t];
758 if (local[t] < 0 ||
759 (size < 0 && !sourceTagData(mb, tags[t].source, ent, defaults[t],
760 default_sizes[t], 0, data, size)))
761 continue;
762 const int components = points && size % points == 0 ? size / points : -1;
763 local[t] = local[t] == 0 || local[t] == components ? components : -1;
764 }
765 return MoFEMErrorCode(0);
766 };
767 fe->getOpPtrVector().push_back(count);
768 CHKERR loopSelectedCells(projection_dm, request.elementName, fe,
769 source_cells);
771}
772
773/// Agree on the components of each source tag over the DM communicator; all
774/// ranks fail together. Without source data on any rank, a tag has as many
775/// components as values in its default. Nothing is required without target
776/// cells on any rank.
777MoFEMErrorCode DGProjectionInterfaceImpl::inferTagComponents(
778 const TagRequest &request, DM projection_dm, const Range &source_cells,
779 const Range &target_cells, int dimension, std::vector<ProjectedTag> &tags) {
781 auto &mb = mField.get_moab();
782 const auto n = tags.size();
783 std::vector<int> local(n, 0);
784 const auto error = phase("tag component inference", [&]() {
785 if (dimension == 2)
786 return localTagComponents<2>(request, projection_dm, source_cells, tags,
787 local);
788 if (dimension == 3)
789 return localTagComponents<3>(request, projection_dm, source_cells, tags,
790 local);
791 return MoFEMErrorCode(0);
792 });
793 if (error)
794 local.assign(n, -2);
795 // Maximum of the counts, of their negated nonzero values and of the target
796 // presence.
797 constexpr int no_data = std::numeric_limits<int>::min();
798 std::vector<int> send(2 * n + 1), global(2 * n + 1);
799 for (size_t t = 0; t != n; ++t) {
800 send[t] = local[t];
801 send[n + t] = local[t] ? -local[t] : no_data;
802 }
803 send[2 * n] = !target_cells.empty();
804 MPI_Comm comm;
805 CHKERR PetscObjectGetComm((PetscObject)projection_dm, &comm);
806 CHKERR MPI_Allreduce(send.data(), global.data(), send.size(), MPI_INT,
807 MPI_MAX, comm);
808 CHKERR error;
809 if (!global[2 * n])
811 for (size_t t = 0; t != n; ++t) {
812 auto &tag = tags[t];
813 if (global[n + t] == no_data) {
814 const void *def = nullptr;
815 int def_size = 0;
816 if (mb.tag_get_default_value(tag.source, def, def_size) != MB_SUCCESS ||
817 !def)
818 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
819 "Tag %s has no source data and no default value; its "
820 "components cannot be inferred",
821 tag.sourceName.c_str());
822 tag.components = def_size;
823 } else {
824 const int lowest = -global[n + t];
825 if (lowest == -2)
826 SETERRQ(PETSC_COMM_SELF, MOFEM_OPERATION_UNSUCCESSFUL,
827 "Tag component inference failed on another rank");
828 if (lowest < 0 || lowest != global[t])
829 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
830 "Tag %s lengths are not one multiple of the source rule "
831 "points on every element",
832 tag.sourceName.c_str());
833 tag.components = lowest;
834 }
835 if (tag.components < 1 || tag.components > 9)
836 SETERRQ(PETSC_COMM_SELF, MOFEM_NOT_IMPLEMENTED,
837 "Tag %s has %d components; tag projection supports 1 through 9",
838 tag.sourceName.c_str(), tag.components);
839 }
841}
842
843/// Fit each source tag to local L2 coefficients without a MoFEM field.
844template <int D>
845MoFEMErrorCode DGProjectionInterfaceImpl::reconstructTags(
846 const TagRequest &request, DM projection_dm, const Range &source_cells,
847 std::vector<ProjectedTag> &tags) {
849 using Element = typename PipelineManager::ElementsAndOpsByDim<D>::DomainEle;
850 auto &mb = mField.get_moab();
851 auto fe = boost::make_shared<Element>(mField);
852 installRule(*fe, request.options.sourceRule);
853 for (auto &tag : tags) {
854 const int components = tag.components;
855 auto values = boost::make_shared<MatrixDouble>();
856 auto coefficients = boost::make_shared<MatrixDouble>();
857 auto mass = boost::make_shared<MatrixDouble>();
858 auto basis = boost::make_shared<EntitiesFieldData>(MBENTITYSET);
859 // A sparse tag returns its default value for elements without data.
860 const void *def = nullptr;
861 int def_size = 0;
862 if (mb.tag_get_default_value(tag.source, def, def_size) != MB_SUCCESS)
863 def = nullptr;
864 auto read = new UDO(NOSPACE, UDO::OPSPACE);
865 read->doWorkRhsHook = [&mb, &tag, values, components, def,
866 def_size](DataOperator *base, int, EntityType,
867 EntData &) {
869 auto op = static_cast<UDO *>(base);
870 const auto ent = op->getFEEntityHandle();
871 const auto points = op->getGaussPts().size2();
872 values->resize(points, components, false);
873 const double *data = nullptr;
874 int size = 0;
875 if (!sourceTagData(mb, tag.source, ent, static_cast<const double *>(def),
876 def_size, points * components, data, size)) {
877 ++tag.withoutData;
878 for (size_t q = 0; q != points; ++q)
879 for (int c = 0; c != components; ++c)
880 (*values)(q, c) = tag.policy.empty[c];
882 }
883 if (static_cast<size_t>(size) != points * components)
884 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
885 "Tag %s holds %d values; the source rule has %zu points of "
886 "%d components",
887 tag.sourceName.c_str(), size, points, components);
888 std::copy(data, data + size, values->data().begin());
889 if (tag.policy.finiteSource && !allFinite(*values))
890 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA, "Nonfinite tag %s value",
891 tag.sourceName.c_str());
893 };
894 fe->getOpPtrVector().push_back(read);
895 fe->getOpPtrVector().push_back(
896 new OpReconstructionMass(tag.order, tagBase, mass, basis));
897 fe->getOpPtrVector().push_back(new OpDGProjectionCoefficients(
898 values, coefficients, mass, basis, tagBase, NOSPACE));
899 auto store = new UDO(NOSPACE, UDO::OPSPACE);
900 store->doWorkRhsHook = [&tag, values, coefficients, mass,
901 basis](DataOperator *base, int, EntityType,
902 EntData &) {
904 auto op = static_cast<UDO *>(base);
905 tag.sourceCoefficients[op->getFEEntityHandle()] = *coefficients;
906 if (!allFinite(*values))
908 if (!allFinite(*coefficients))
909 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
910 "Nonfinite tag reconstruction coefficient");
911 const auto &n =
912 basis->dataOnEntities[op->getFEType()][0].getN(tagBase);
913 tag.sourceResidual =
914 std::max(tag.sourceResidual,
915 massSolveResidual(n, op->getGaussPts(), *values, *mass,
916 *coefficients));
918 };
919 fe->getOpPtrVector().push_back(store);
920 }
921 CHKERR phase("tag reconstruction", [&]() {
922 return loopSelectedCells(projection_dm, request.elementName, fe,
923 source_cells);
924 });
926}
927
928/// Sample reconstructed source polynomials and solve local target L2 systems.
929template <int D>
930MoFEMErrorCode DGProjectionInterfaceImpl::projectTagsImpl(
931 const TagRequest &request, DM projection_dm, const std::string &problem_name,
932 const Range &target_cells, std::vector<ProjectedTag> &tags,
933 bool has_source, TagResult &report) {
935 using Element = typename PipelineManager::ElementsAndOpsByDim<D>::DomainEle;
936 TargetPoints points;
937 auto target_fe = boost::make_shared<Element>(mField);
938 target_fe->getRuleHook =
939 [order = request.options.quadratureOrder](int, int, int) { return order; };
940 auto collect = new UDO(NOSPACE, UDO::OPSPACE);
941 collect->doWorkRhsHook = [&points](DataOperator *base, int, EntityType,
942 EntData &) {
944 auto op = static_cast<UDO *>(base);
945 const auto &coords = op->getCoordsAtGaussPts();
946 points.offsets.emplace(op->getFEEntityHandle(), points.coordinates.size());
947 for (size_t q = 0; q != coords.size1(); ++q)
948 points.coordinates.push_back(
949 {coords(q, 0), coords(q, 1), coords(q, 2)});
951 };
952 target_fe->getOpPtrVector().push_back(collect);
953 CHKERR phase("tag target quadrature", [&]() {
954 return loopSelectedCells(projection_dm, request.elementName, target_fe,
955 target_cells);
956 });
957 report.targetElements = points.offsets.size();
958 report.sampledPoints = points.coordinates.size();
959
960 auto evaluator = mField.getInterface<FieldEvaluatorInterface>();
961 auto search = evaluator->getData<Element>();
962 auto located_coordinates = boost::make_shared<MatrixDouble>();
963 auto cache = boost::make_shared<CacheTuple>();
964 double source_scale = 0.;
965 const bool search_source = has_source && !points.coordinates.empty();
966 CHKERR phase("tag source search", [&]() {
968 CHKERR mField.cache_problem_entities(problem_name, cache);
969 if (search_source) {
970 CHKERR evaluator->buildTree<D>(search, request.elementName,
971 request.source.bit, request.source.mask);
972 double lower[3], upper[3];
973 unsigned depth;
974 CHKERR search->treePtr->get_info(search->rooTreeSet, lower, upper, depth);
975 for (int d = 0; d != 3; ++d)
976 source_scale = std::hypot(source_scale, upper[d] - lower[d]);
977 }
978 search->feMethodPtr->getRuleHook = [](int, int, int) { return -1; };
979 auto locate = new UDO(NOSPACE, UDO::OPSPACE);
980 locate->doWorkRhsHook = [located_coordinates](DataOperator *base, int,
981 EntityType, EntData &) {
982 *located_coordinates = static_cast<UDO *>(base)->getCoordsAtGaussPts();
983 return MoFEMErrorCode(0);
984 };
985 search->feMethodPtr->getOpPtrVector().push_back(locate);
986 for (auto &tag : tags) {
987 tag.samples.resize(points.coordinates.size(), tag.components,
988 false);
989 auto coefficients = boost::make_shared<MatrixDouble>();
990 auto basis = boost::make_shared<EntitiesFieldData>(MBENTITYSET);
991 auto load = new UDO(NOSPACE, UDO::OPSPACE);
992 load->doWorkRhsHook = [&tag, coefficients](DataOperator *base, int,
993 EntityType, EntData &) {
995 const auto ent = static_cast<UDO *>(base)->getFEEntityHandle();
996 const auto it = tag.sourceCoefficients.find(ent);
997 if (it == tag.sourceCoefficients.end())
998 SETERRQ(PETSC_COMM_SELF, MOFEM_NOT_FOUND,
999 "Source tag coefficients are missing");
1000 *coefficients = it->second;
1002 };
1003 search->feMethodPtr->getOpPtrVector().push_back(load);
1004 search->feMethodPtr->getOpPtrVector().push_back(
1005 new OpTagBasis(tag.order, basis));
1006 search->feMethodPtr->getOpPtrVector().push_back(
1007 new OpDGProjectionEvaluation(tag.sourceValues, coefficients, basis,
1008 tagBase, NOSPACE));
1009 }
1011 });
1012
1013 unsigned long long missing = 0;
1014 CHKERR phase("tag source sampling", [&]() {
1016 std::vector<char> sampled(points.coordinates.size(), false);
1017 for (size_t q = 0; q != points.coordinates.size(); ++q) {
1018 for (auto &tag : tags)
1019 for (size_t c = 0; c != tag.samples.size2(); ++c)
1020 tag.samples(q, c) = tag.policy.empty[c];
1021 }
1022 if (search_source)
1023 CHKERR evaluator->evalFEAtPoints<D>(
1024 points.coordinates, 1.e-10, problem_name, request.elementName,
1025 search, 0, mField.get_comm_size(), request.source.bit,
1026 request.source.mask, cache,
1027 [&](const std::vector<size_t> &indices) {
1029 if (located_coordinates->size1() != indices.size() ||
1030 located_coordinates->size2() != 3)
1031 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
1032 "Invalid located tag point");
1033 for (size_t row = 0; row != indices.size(); ++row) {
1034 const auto q = indices[row];
1035 const auto &x = points.coordinates[q];
1036 const moab::CartVect back(&(*located_coordinates)(row, 0));
1037 const moab::CartVect query(x.data());
1038 double coordinate_scale = 0.;
1039 for (double value : x)
1040 coordinate_scale = std::max(coordinate_scale, std::abs(value));
1041 const double tolerance = std::max(
1042 1.e-10 * source_scale,
1043 64 * std::numeric_limits<double>::epsilon() *
1044 coordinate_scale);
1045 if (!std::isfinite((back - query).length()) ||
1046 (back - query).length() > tolerance)
1047 continue;
1048 for (auto &tag : tags) {
1049 const auto &values = *tag.sourceValues;
1050 if (values.size1() != indices.size() ||
1051 values.size2() !=
1052 static_cast<size_t>(tag.components))
1053 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
1054 "Inconsistent reconstructed tag values");
1055 for (size_t c = 0; c != values.size2(); ++c)
1056 tag.samples(q, c) = values(row, c);
1057 }
1058 sampled[q] = true;
1059 }
1061 },
1062 MF_EXIST, QUIET);
1063 for (auto valid : sampled)
1064 if (!valid)
1065 ++missing;
1067 });
1068 report.missingPoints = missing;
1069
1070 target_fe->getOpPtrVector().clear();
1071 for (auto &tag : tags) {
1072 auto values = boost::make_shared<MatrixDouble>();
1073 auto coefficients = boost::make_shared<MatrixDouble>();
1074 auto mass = boost::make_shared<MatrixDouble>();
1075 auto basis = boost::make_shared<EntitiesFieldData>(MBENTITYSET);
1076 auto load = new UDO(NOSPACE, UDO::OPSPACE);
1077 load->doWorkRhsHook = [&points, &tag, values](DataOperator *base, int,
1078 EntityType, EntData &) {
1080 auto op = static_cast<UDO *>(base);
1081 const auto offset = points.offsets.at(op->getFEEntityHandle());
1082 const auto count = op->getGaussPts().size2();
1083 values->resize(count, tag.components, false);
1084 for (size_t q = 0; q != count; ++q)
1085 for (size_t c = 0; c != values->size2(); ++c)
1086 (*values)(q, c) = tag.samples(offset + q, c);
1088 };
1089 target_fe->getOpPtrVector().push_back(load);
1090 target_fe->getOpPtrVector().push_back(
1091 new OpReconstructionMass(tag.order, tagBase, mass, basis));
1092 target_fe->getOpPtrVector().push_back(new OpDGProjectionCoefficients(
1093 values, coefficients, mass, basis, tagBase, NOSPACE));
1094 auto store = new UDO(NOSPACE, UDO::OPSPACE);
1095 store->doWorkRhsHook = [&tag, values, coefficients, mass,
1096 basis](DataOperator *base, int, EntityType,
1097 EntData &) {
1099 auto op = static_cast<UDO *>(base);
1100 tag.targetCoefficients[op->getFEEntityHandle()] = *coefficients;
1101 if (allFinite(*values) && allFinite(*coefficients)) {
1102 const auto &n = basis->dataOnEntities[op->getFEType()][0].getN(tagBase);
1103 tag.targetResidual =
1104 std::max(tag.targetResidual,
1105 massSolveResidual(n, op->getGaussPts(), *values, *mass,
1106 *coefficients));
1107 }
1109 };
1110 target_fe->getOpPtrVector().push_back(store);
1111 }
1112 CHKERR phase("tag local projection", [&]() {
1113 return loopSelectedCells(projection_dm, request.elementName, target_fe,
1114 target_cells);
1115 });
1117}
1118
1119/// Evaluate projected local coefficients at the target rule and stage tag
1120/// values on every local target copy.
1121template <int D>
1122MoFEMErrorCode DGProjectionInterfaceImpl::evaluateTags(
1123 const TagRequest &request, DM projection_dm, const Range &target_cells,
1124 std::vector<ProjectedTag> &tags) {
1126 using Element = typename PipelineManager::ElementsAndOpsByDim<D>::DomainEle;
1127 auto fe = boost::make_shared<Element>(mField);
1128 installRule(*fe, request.options.targetRule);
1129 for (auto &tag : tags) {
1130 auto values = boost::make_shared<MatrixDouble>();
1131 auto coefficients = boost::make_shared<MatrixDouble>();
1132 auto basis = boost::make_shared<EntitiesFieldData>(MBENTITYSET);
1133 auto load = new UDO(NOSPACE, UDO::OPSPACE);
1134 load->doWorkRhsHook = [&tag, coefficients](DataOperator *base, int,
1135 EntityType, EntData &) {
1137 const auto ent = static_cast<UDO *>(base)->getFEEntityHandle();
1138 const auto it = tag.targetCoefficients.find(ent);
1139 if (it == tag.targetCoefficients.end())
1140 SETERRQ(PETSC_COMM_SELF, MOFEM_NOT_FOUND,
1141 "Target tag coefficients are missing");
1142 *coefficients = it->second;
1144 };
1145 fe->getOpPtrVector().push_back(load);
1146 fe->getOpPtrVector().push_back(new OpTagBasis(tag.order, basis));
1147 fe->getOpPtrVector().push_back(new OpDGProjectionEvaluation(
1148 values, coefficients, basis, tagBase, NOSPACE));
1149 auto stage = new UDO(NOSPACE, UDO::OPSPACE);
1150 stage->doWorkRhsHook = [&tag, values](DataOperator *base, int, EntityType,
1151 EntData &) {
1153 auto op = static_cast<UDO *>(base);
1154 const auto points = op->getGaussPts().size2();
1155 if (values->size1() != points ||
1156 values->size2() != static_cast<size_t>(tag.components))
1157 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
1158 "Inconsistent target tag value layout");
1159 if (tag.targetLength >= 0 &&
1160 static_cast<size_t>(tag.targetLength) != values->data().size())
1161 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
1162 "Fixed-length tag %s holds %d values; the target rule needs "
1163 "%zu",
1164 tag.targetName.c_str(), tag.targetLength,
1165 values->data().size());
1166 tag.staged[op->getFEEntityHandle()].assign(values->data().begin(),
1167 values->data().end());
1169 };
1170 fe->getOpPtrVector().push_back(stage);
1171 }
1172 CHKERR phase("tag evaluation", [&]() {
1173 return loopSelectedCells(projection_dm, request.elementName, fe,
1174 target_cells);
1175 });
1177}
1178
1179/// Write the staged values of every local copy; restore the original values
1180/// if any rank fails.
1182DGProjectionInterfaceImpl::publishTags(std::vector<ProjectedTag> &tags,
1183 const Range &target_copies) {
1185 auto &mb = mField.get_moab();
1186 const auto set_values = [&](const ProjectedTag &tag, EntityHandle ent,
1187 const std::vector<double> &values) {
1189 if (tag.targetLength >= 0) {
1190 CHKERR mb.tag_set_data(tag.target, &ent, 1, values.data());
1191 } else {
1192 const void *data[] = {values.data()};
1193 const int size = values.size();
1194 CHKERR mb.tag_set_by_ptr(tag.target, &ent, 1, data, &size);
1195 }
1197 };
1198 const auto restore = [&]() {
1199 return phase("tag publication rollback", [&]() {
1201 for (auto &tag : tags)
1202 for (const auto &[ent, previous] : tag.previous) {
1203 if (previous.first)
1204 CHKERR set_values(tag, ent, previous.second);
1205 else
1206 CHKERR mb.tag_delete_data(tag.target, &ent, 1);
1207 }
1209 });
1210 };
1211 auto error = phase("tag publication", [&]() {
1213 for (auto &tag : tags)
1214 for (auto ent : target_copies) {
1215 const double *data = nullptr;
1216 int size = 0;
1217 auto &previous = tag.previous[ent];
1218 previous.first = mb.tag_get_by_ptr(tag.target, &ent, 1,
1219 (const void **)&data,
1220 &size) == MB_SUCCESS &&
1221 data;
1222 if (previous.first)
1223 previous.second.assign(data, data + size);
1224 }
1225 for (auto &tag : tags)
1226 for (auto ent : target_copies)
1227 CHKERR set_values(tag, ent, tag.staged.at(ent));
1229 });
1230 if (error) {
1231 CHKERR restore();
1232 CHKERR error;
1233 }
1235}
1236
1237/// Reconstruct, project and evaluate tags through field-free L2 operators.
1238MoFEMErrorCode DGProjectionInterfaceImpl::transferTags(const TagRequest &request,
1239 TagResult &report) {
1241 report = {};
1242 if (!request.dm || request.elementName.empty() ||
1243 !mField.check_finite_element(request.elementName))
1244 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
1245 "Tag projection requires a MoFEM DM and existing finite element");
1246 auto &mb = mField.get_moab();
1248 int dimension;
1249 CHKERR selectCells(request.source, request.target, source, target, dimension);
1250 std::vector<ProjectedTag> tags;
1251 CHKERR validateTags(request, tags);
1252 std::string problem_name;
1253 CHKERR reserveProblemName(problem_name);
1254 SmartPetscObj<DM> projection_dm;
1255 CHKERR createSubDM(request.dm, problem_name, request.source, request.target,
1256 request.elementName, projection_dm);
1257 CHKERR inferTagComponents(request, projection_dm, source, target, dimension,
1258 tags);
1259 if (target.empty())
1261 const auto execute = [&]() {
1263 // Missing target tags take the source tag's type, storage, length and
1264 // default value; the target default defines the empty value.
1265 CHKERR phase("target tag preparation", [&]() {
1267 for (auto &tag : tags) {
1268 const void *def = nullptr;
1269 int def_size = 0;
1270 if (!tag.target) {
1271 TagType storage;
1272 CHKERR mb.tag_get_type(tag.source, storage);
1273 int length = 0;
1274 const bool variable =
1275 mb.tag_get_length(tag.source, length) == MB_VARIABLE_DATA_LENGTH;
1276 const bool has_default =
1277 mb.tag_get_default_value(tag.source, def, def_size) ==
1278 MB_SUCCESS &&
1279 def;
1280 unsigned flags = MB_TAG_CREAT | MB_TAG_EXCL | storage;
1281 if (variable)
1282 flags |= MB_TAG_VARLEN;
1283 CHKERR mb.tag_get_handle(tag.targetName.c_str(),
1284 variable ? (has_default ? def_size : 0)
1285 : length,
1286 MB_TYPE_DOUBLE, tag.target, flags,
1287 has_default ? def : nullptr);
1288 tag.created = true;
1289 }
1290 int length = 0;
1291 if (mb.tag_get_length(tag.target, length) != MB_VARIABLE_DATA_LENGTH)
1292 tag.targetLength = length;
1293 const int components = tag.components;
1294 tag.policy.empty.assign(components, request.options.emptyValue);
1295 def = nullptr;
1296 def_size = 0;
1297 if (mb.tag_get_default_value(tag.target, def, def_size) == MB_SUCCESS &&
1298 def && def_size > 0) {
1299 const auto *values = static_cast<const double *>(def);
1300 for (int c = 0; c != components; ++c)
1301 tag.policy.empty[c] = def_size >= components ? values[c] : values[0];
1302 }
1303 tag.policy.finiteSource =
1304 std::all_of(tag.policy.empty.begin(), tag.policy.empty.end(),
1305 [](double value) { return std::isfinite(value); });
1306 }
1308 });
1309 if (dimension == 2)
1310 CHKERR reconstructTags<2>(request, projection_dm, source, tags);
1311 else
1312 CHKERR reconstructTags<3>(request, projection_dm, source, tags);
1313
1314 if (dimension == 2)
1315 CHKERR projectTagsImpl<2>(request, projection_dm, problem_name,
1316 target, tags, !source.empty(), report);
1317 else
1318 CHKERR projectTagsImpl<3>(request, projection_dm, problem_name,
1319 target, tags, !source.empty(), report);
1320
1321 if (dimension == 2)
1322 CHKERR evaluateTags<2>(request, projection_dm, target, tags);
1323 else
1324 CHKERR evaluateTags<3>(request, projection_dm, target, tags);
1325 CHKERR publishTags(tags, target);
1326
1327 for (size_t t = 0; t != tags.size(); ++t) {
1328 TagReport item;
1329 item.targetTag = tags[t].targetName;
1330 item.relativeMassResidual = tags[t].targetResidual;
1331 item.sourceRelativeMassResidual = tags[t].sourceResidual;
1332 item.sourceElementsWithoutData = tags[t].withoutData;
1333 report.items.push_back(item);
1334 }
1336 };
1337 const auto error = execute();
1338 if (error)
1339 for (auto &tag : tags)
1340 if (tag.created)
1341 CHKERR mb.tag_delete(tag.target);
1342 CHKERR error;
1344}
1345
1346MoFEMErrorCode DGProjectionInterfaceImpl::project(const TagRequest &request,
1347 TagResult &report) {
1349 CHKERR preservingBuildState([&]() { return transferTags(request, report); });
1351}
1352} // namespace
1353
1354template <>
1356createInterface<DGProjectionInterface>(const Core &core) {
1357 return new DGProjectionInterfaceImpl(core);
1358}
1359} // namespace MoFEM
MoFEM::Interface & mField
Report & report
std::vector< int > created
std::vector< int > source
const std::vector< Vec > * points
std::vector< int > size
double length
double tolerance
Radians.
WorkMesh base
Flags flags
Vec t
Removal: the ring edge.
std::vector< int > parent
bool back
Slid back beyond the node along its edge.
std::vector< Cell > cells
MPI_Comm comm
moab::Interface & mb
Tag tag
std::map< EntityHandle, size_t > offsets
boost::shared_ptr< FieldEntity > targetEntity
int components
Inferred from the source tag length.
unsigned long long withoutData
unsigned long long sequence
std::map< EntityHandle, MatrixDouble > targetCoefficients
std::string sourceName
double sourceResidual
const Field * source
SamplePolicy policy
const Field * target
double residual
Core & mCore
ProjectedField & field
std::map< EntityHandle, std::pair< bool, std::vector< double > > > previous
boost::shared_ptr< MatrixDouble > sourceValues
int targetLength
Values held by a fixed-length target tag.
std::vector< double > empty
One value per component.
MatrixDouble samples
bool finiteSource
Reject nonfinite source values.
std::vector< std::array< double, 3 > > coordinates
std::string targetName
double targetResidual
std::map< EntityHandle, std::vector< double > > staged
Interface & mField
std::map< EntityHandle, MatrixDouble > sourceCoefficients
std::string type
std::vector< FieldBitNumber > row
Definition Schur.cpp:2488
It it
Definition Schur.cpp:2568
int nb
number of DOFs
Definition Schur.cpp:2505
SmartPetscObj< Vec > work
SmartPetscObj< Vec > values
std::string name
constexpr double a
cholesky decomposition
size_t cholesky_decompose(const MATRIX &A, TRIA &L)
decompose the symmetric positive definit matrix A into product L L^T.
Definition cholesky.hpp:52
@ QUIET
@ MF_EXIST
FieldApproximationBase
approximation base
Definition definitions.h:58
@ AINSWORTH_LEGENDRE_BASE
Ainsworth Cole (Legendre) approx. base .
Definition definitions.h:60
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
@ L2
field with C-1 continuity
Definition definitions.h:88
@ H1
continuous field
Definition definitions.h:85
@ NOSPACE
Definition definitions.h:83
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
@ MOFEM_NOT_FOUND
Definition definitions.h:33
@ MOFEM_OPERATION_UNSUCCESSFUL
Definition definitions.h:34
@ MOFEM_DATA_INCONSISTENCY
Definition definitions.h:31
@ MOFEM_INVALID_DATA
Definition definitions.h:36
@ 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.
#define THROW_MESSAGE(msg)
Throw MoFEM exception.
constexpr int order
@ F
PetscErrorCode DMMoFEMCreateSubDM(DM subdm, DM dm, const char problem_name[])
Must be called by user to set Sub DM MoFEM data structures.
Definition DMMoFEM.cpp:244
PetscErrorCode DMMoFEMAddElement(DM dm, std::string fe_name)
add element to dm
Definition DMMoFEM.cpp:574
PetscErrorCode DMMoFEMSetSquareProblem(DM dm, PetscBool square_problem)
set squared problem
Definition DMMoFEM.cpp:536
PetscErrorCode DMMoFEMGetProblemPtr(DM dm, const MoFEM::Problem **problem_ptr)
Get pointer to problem data structure.
Definition DMMoFEM.cpp:451
PetscErrorCode DMoFEMLoopFiniteElements(DM dm, const char fe_name[], MoFEM::FEMethod *method, CacheTupleWeakPtr cache_ptr=CacheTupleSharedPtr())
Executes FEMethod for finite elements in DM.
Definition DMMoFEM.cpp:662
virtual const FieldEntity_multiIndex * get_field_ents() const =0
Get the field ents object.
virtual const Field * get_field_structure(const std::string &name, enum MoFEMTypes bh=MF_EXIST) const =0
get field structure
virtual bool check_field(const std::string &name) const =0
check if field is in database
virtual MoFEMErrorCode modify_problem_mask_ref_level_set_bit(const std::string &name_problem, const BitRefLevel &bit)=0
set dof mask ref level for problem
virtual MoFEMErrorCode modify_problem_ref_level_set_bit(const std::string &name_problem, const BitRefLevel &bit)=0
set ref level for problem
virtual bool check_problem(const std::string name)=0
check if problem exist
FTensor::Index< 'i', SPACE_DIM > i
const double c
speed of light (cm/ns)
double D
FTensor::Index< 'j', 3 > j
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
UBlasMatrix< double > MatrixDouble
Definition Types.hpp:77
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
auto type_from_handle(const EntityHandle h)
get type from entity handle
auto dimension_from_handle(const EntityHandle h)
get entity dimension form handle
CoreTmp< 0 > Core
Definition Core.hpp:1161
DeprecatedCoreInterface Interface
auto createDM(MPI_Comm comm, const std::string dm_type_name)
Creates smart DM object.
float offset
Definition sdf_python.py:3
double q
FTensor::Index< 'm', 3 > m
const int N
Definition speed_test.cpp:3
virtual int get_comm_size() const =0
virtual moab::Interface & get_moab()=0
virtual MoFEMErrorCode cache_problem_entities(const std::string prb_name, CacheTupleWeakPtr cache_ptr)=0
Cache variables.
virtual bool check_finite_element(const std::string &name) const =0
Check if finite element is in database.
virtual int get_comm_rank() const =0
Core (interface) class.
Definition Core.hpp:83
Data on single entity (This is passed as argument to DataOperator::doWork)
UId getLocalUniqueIdCalculate()
Get the Local Unique Id Calculate object.
FieldCoefficientsNumber getNbOfCoeffs() const
Get number of field coefficients per DOF.
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.