v0.16.3
Loading...
Searching...
No Matches
CouplerInterface.cpp
Go to the documentation of this file.
1/** \file CouplerInterface.cpp
2 * \brief Source-local MBCoupler search with collective query routing.
3 */
4#include <config.h>
5#ifdef WITH_MBCOUPLER
6 // Include before MoFEM namespace imports: MOAB declares two unrelated fail functions.
7 #include <mbcoupler/ElemUtil.hpp>
8 #include <mbcoupler/Coupler.hpp>
9#endif
10#include <MoFEM.hpp>
11#include <boost/uuid/detail/sha1.hpp>
12#include <algorithm>
13#include <array>
14#include <cmath>
15#include <iomanip>
16#include <initializer_list>
17#include <sstream>
18#include <numeric>
19#include <climits>
20#include <limits>
21#include <map>
22#include <memory>
23#include <set>
24#include <unordered_map>
25
26namespace MoFEM {
27
28/// Source selection, search backend and routing state shared by location batches.
30 const Core *core = nullptr;
34 // Changes when the mapped-point cache is replaced or the transfer is closed.
35 unsigned long long generation = 0;
36 bool closed = true;
37 double distance = 0; // Physical search tolerance resolved from source scale.
38#ifdef WITH_MBCOUPLER
39 // Private MPI_COMM_SELF part prevents MBCoupler from widening the selection.
40 moab::Interface *database = nullptr;
41 EntityHandle part = 0;
42 std::unique_ptr<ParallelComm> backendComm;
43 std::unique_ptr<moab::Coupler> backend;
44 std::unique_ptr<moab::gs_data::crystal_data> router;
45 // Per-rank source bounds: three lower coordinates followed by three upper.
46 std::vector<double> boxes;
47 int sourceRanks = 0;
48 /// Cached element identity and linear map used by localLocation.
49 struct SourceElement {
50 EntityHandle handle;
51 int id;
52 double coordinateScale = 0.;
53 std::unique_ptr<moab::Element::Map> map;
54 };
55 std::vector<SourceElement> elements;
56 std::unordered_map<EntityHandle, size_t> elementIndices;
57#endif
58 /// Release local backend resources; collective closure uses destroyTransfer.
60 if (!closed)
61 MOFEM_LOG("SELF", Sev::warning)
62 << "CouplerInterface: transfer left unclosed; call destroyTransfer "
63 "before destroying the Core";
64#ifdef WITH_MBCOUPLER
65 backend.reset();
66 backendComm.reset();
67 if (part)
68 database->delete_entities(&part, 1);
69#endif
70 }
71};
72
73/// Target-to-source matches tied to one generation of the backend point cache.
75 boost::weak_ptr<Transfer> transfer;
76 unsigned long long generation = 0;
77 // Keys are (owner rank, owner-local handle), never a foreign MOAB handle.
78 using Key = std::pair<int, unsigned long long>;
79 // Local target handles map to rows in the query and sampled-value arrays.
80 std::map<EntityHandle, size_t> localIndices;
81 std::vector<int> winners; // Source rank per query, or -1 for a miss.
82 // Source-local indices into MBCoupler's mapped-point cache.
83 std::vector<int> mappedIndices;
84 // Adjacent-cell centroid query rows used when a target vertex is missing.
85 std::vector<std::vector<size_t>> fallback;
86 std::vector<int> status; // 0 missing, 1 directly located, 2 fallback.
88};
89
90namespace {
91
92/// Core-backed implementation with collective checks around local MOAB work.
93struct CouplerInterfaceImpl final : CouplerInterface {
94 /// Borrow the Core for mesh access and the coupling communicator.
95 explicit CouplerInterfaceImpl(const Core &core)
96 : cOre(const_cast<Core &>(core)) {}
98 unsigned long long scratchSequence = 0;
99 /// Return the communicator used by every collective coupling operation.
100 MPI_Comm comm() const {
101 Interface &m_field = cOre;
102 return m_field.get_comm();
103 }
104 /// Access the Core mesh database containing source and target entities.
105 moab::Interface &mb() const {
106 Interface &m_field = cOre;
107 return m_field.get_moab();
108 }
109 /// Expose this implementation through the MoFEM interface lookup.
110 MoFEMErrorCode query_interface(boost::typeindex::type_index,
111 UnknownInterface **iface) const override {
112 *iface = const_cast<CouplerInterfaceImpl *>(this);
113 return 0;
114 }
115 MoFEMErrorCode createTransfer(const Selection &, const Options &,
116 TransferPtr &) override;
117 MoFEMErrorCode locateTargets(const TransferPtr &, const Selection &,
118 LocationsPtr &, Report &) override;
119 MoFEMErrorCode transferTags(const TransferPtr &, const LocationsPtr &,
120 const std::vector<TagMap> &, Report &) override;
121 MoFEMErrorCode destroyTransfer(TransferPtr &) override;
122
123 // Audit a local phase before any rank enters the next collective.
124 template <typename F> MoFEMErrorCode phase(const char *name, F work) const {
126 const int local_error = work();
127 int global_error = 0;
128 CHKERR MPI_Allreduce(&local_error, &global_error, 1, MPI_INT, MPI_MAX,
129 comm());
130 if (global_error)
131 SETERRQ(comm(), global_error, "CouplerInterface: %s failed on a rank",
132 name);
134 }
135 /// All-gather small metadata arrays and return their per-rank counts/offsets.
136 template <typename T>
137 MoFEMErrorCode gather(const std::vector<T> &local, MPI_Datatype datatype,
138 std::vector<T> &global, std::vector<int> &counts,
139 std::vector<int> &offsets) const {
141 CHKERR phase("message size", [&]() -> MoFEMErrorCode {
142 if (local.size() > INT_MAX)
143 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
144 "Coupler message exceeds MPI int count");
145 return 0;
146 });
147 int size;
148 CHKERR MPI_Comm_size(comm(), &size);
149 counts.resize(size);
150 offsets.resize(size);
151 int count = static_cast<int>(local.size());
152 CHKERR MPI_Allgather(&count, 1, MPI_INT, counts.data(), 1, MPI_INT, comm());
153 size_t total = 0;
154 for (int r = 0; r != size; ++r) {
155 offsets[r] = static_cast<int>(total);
156 total += counts[r];
157 if (total > INT_MAX)
158 SETERRQ(comm(), MOFEM_INVALID_DATA,
159 "Global coupling batch exceeds MPI int count");
160 }
161 global.resize(total);
162 CHKERR MPI_Allgatherv(local.data(), count, datatype, global.data(),
163 counts.data(), offsets.data(), datatype, comm());
165 }
166 /// Reject differing collective options, tag maps or replica fingerprints.
167 MoFEMErrorCode sameInput(const std::string &input,
168 const char *description = "options/tag maps") const {
170 std::vector<char> local(input.begin(), input.end()), all;
171 std::vector<int> counts, offsets;
172 CHKERR gather(local, MPI_CHAR, all, counts, offsets);
173 for (size_t r = 0; r != counts.size(); ++r)
174 if (counts[r] != static_cast<int>(local.size()) ||
175 !std::equal(local.begin(), local.end(), all.begin() + offsets[r]))
176 SETERRQ(comm(), MOFEM_INVALID_DATA, "Coupler %s differ across ranks",
177 description);
179 }
180 /// Reject null, closed or foreign-Core handles before using their state.
181 MoFEMErrorCode checkTransfer(const TransferPtr &t) const {
183 if (!t || t->closed || t->core != &cOre)
184 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
185 "Null, closed or cross-Core coupling transfer");
187 }
188 /// Resolve optional ownership metadata and check its communicator.
189 MoFEMErrorCode parallel(const Selection &s, ParallelComm *&pc) const {
191 pc = s.parallelCommId < 0
192 ? nullptr
193 : ParallelComm::get_pcomm(&mb(), s.parallelCommId);
194 if (s.parallelCommId >= 0 && !pc)
195 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
196 "Invalid coupling ParallelComm id");
197 if (pc) {
198 int comparison;
199 CHKERR MPI_Comm_compare(comm(), pc->comm(), &comparison);
200 if (comparison != MPI_IDENT && comparison != MPI_CONGRUENT)
201 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
202 "Coupling ParallelComm must match Core communicator");
203 }
205 }
206 /// Identify an entity by its owner rank and handle on that owner.
207 MoFEMErrorCode key(ParallelComm *pc, EntityHandle entity,
208 Locations::Key &k) const {
210 int rank;
211 CHKERR MPI_Comm_rank(comm(), &rank);
212 EntityHandle handle = entity;
213 if (pc)
214 CHKERR pc->get_owner_handle(entity, rank, handle);
215 k = {rank, static_cast<unsigned long long>(handle)};
217 }
218 /// Filter the selection to entities owned by this rank.
219 MoFEMErrorCode owned(const Selection &s, Range &entities) const {
221 ParallelComm *pc;
222 CHKERR parallel(s, pc);
223 int rank;
224 CHKERR MPI_Comm_rank(comm(), &rank);
225 entities.clear();
226 for (auto e : s.elements) {
227 Locations::Key k;
228 CHKERR key(pc, e, k);
229 if (k.first == rank)
230 entities.insert(e);
231 }
233 }
234 /// Read corner connectivity and enforce the supported linear tet/hex types.
235 MoFEMErrorCode corners(EntityHandle e,
236 std::vector<EntityHandle> &vertices) const {
238 const EntityHandle *conn;
239 int n;
240 CHKERR mb().get_connectivity(e, conn, n, false);
241 const auto type = mb().type_from_handle(e);
242 if (!((type == MBTET && n == 4) || (type == MBHEX && n == 8)))
243 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
244 "Coupler accepts only four-node tets and eight-node hexes");
245 vertices.assign(conn, conn + n);
247 }
248 /// Read a vertex position or the arithmetic mean of an element's corners.
249 MoFEMErrorCode coordinates(EntityHandle entity,
250 std::array<double, 3> &x) const {
252 if (mb().type_from_handle(entity) == MBVERTEX) {
253 CHKERR mb().get_coords(&entity, 1, x.data());
254 } else {
255 std::vector<EntityHandle> vertices;
256 CHKERR corners(entity, vertices);
257 std::vector<double> coords(3 * vertices.size());
258 CHKERR mb().get_coords(vertices.data(), vertices.size(), coords.data());
259 x.fill(0.);
260 for (size_t v = 0; v != vertices.size(); ++v)
261 for (int d = 0; d != 3; ++d)
262 x[d] += coords[3 * v + d] / vertices.size();
263 }
264 for (auto v : x)
265 if (!std::isfinite(v))
266 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
267 "Nonfinite coupling coordinates");
269 }
270#ifdef WITH_MBCOUPLER
271 /// Route query points and retain the match with the smallest source GLOBAL_ID.
272 MoFEMErrorCode locate(Transfer &t, const std::vector<double> &points,
273 Locations &locations) const;
274 /// Search the local backend tree and invert candidate element maps.
275 MoFEMErrorCode localLocation(Transfer &t, const double *xyz,
276 EntityHandle &element,
277 std::array<double, 3> &natural,
278 std::vector<EntityHandle> &leaves,
279 Range &candidates) const;
280 /// Exchange destination-addressed tuples through MOAB's crystal router.
281 MoFEMErrorCode route(Transfer &t, moab::TupleList &messages) const {
282 return phase("crystal routing", [&]() -> MoFEMErrorCode {
284 CHKERR t.router->gs_transfer(1, messages, 0);
286 });
287 }
288 /// Create a private sparse double tag with a name unused on every rank.
289 MoFEMErrorCode scratchTag(int components, Tag &tag) {
291 std::string name;
292 int occupied;
293 do {
294 name = "__MOFEM_COUPLER_" + std::to_string(++scratchSequence);
295 Tag existing;
296 int local_occupied =
297 mb().tag_get_handle(name.c_str(), existing) == MB_SUCCESS;
298 CHKERR MPI_Allreduce(&local_occupied, &occupied, 1, MPI_INT, MPI_MAX,
299 comm());
300 } while (occupied);
301 CHKERR phase("scratch tag", [&]() {
303 std::vector<double> zero(components, 0.);
304 CHKERR mb().tag_get_handle(name.c_str(), components, MB_TYPE_DOUBLE, tag,
305 MB_TAG_SPARSE | MB_TAG_CREAT | MB_TAG_EXCL,
306 zero.data());
308 });
310 }
311 /// Prepare source values, interpolate cached matches and apply fallback.
312 MoFEMErrorCode sample(Transfer &t, const Locations &locations,
313 const TagMap &mapping, int components,
314 std::vector<double> &values);
315#endif
316};
317
318#ifdef WITH_MBCOUPLER
319// Append one routing tuple, growing its storage as needed.
320// Field zero is the destination before routing and the sender afterwards.
321void append(moab::TupleList &list, std::initializer_list<int> integers,
322 std::initializer_list<EntityHandle> handles = {},
323 std::initializer_list<double> reals = {}) {
324 if (list.get_n() >= static_cast<unsigned>(INT_MAX))
326 "Coupling message exceeds int indices");
327 if (list.get_n() == list.get_max())
328 CHK_MOAB_THROW(list.resize(std::min(static_cast<unsigned>(INT_MAX),
329 std::max(16u, list.get_max() * 2))),
330 "Grow coupling message buffer");
331 list.enableWriteAccess();
332 const auto i = list.get_n();
333 if (integers.size())
334 std::copy(integers.begin(), integers.end(),
335 list.vi_wr + i * integers.size());
336 if (handles.size())
337 std::copy(handles.begin(), handles.end(), list.vul_wr + i * handles.size());
338 if (reals.size())
339 std::copy(reals.begin(), reals.end(), list.vr_wr + i * reals.size());
340 list.inc_n();
341}
342#endif
343
344// Scratch tag lifetime is local; caller-owned definitions are never reused.
345struct CouplerScratchTag {
346 moab::Interface &mb;
347 Tag tag = nullptr;
348 /// Delete only this private tag; no MPI calls are made during cleanup.
349 ~CouplerScratchTag() {
350 if (tag)
351 mb.tag_delete(tag);
352 }
353};
354
355/// Validate source geometry/identities and build source-local MBCoupler trees.
356MoFEMErrorCode CouplerInterfaceImpl::createTransfer(const Selection &source,
357 const Options &options,
358 TransferPtr &transfer) {
360#ifndef WITH_MBCOUPLER
361 SETERRQ(comm(), MOFEM_NOT_IMPLEMENTED,
362 "CouplerInterface requires MOAB +mpi+coupler and WITH_MBCOUPLER");
363#else
364 std::ostringstream signature;
365 signature << static_cast<int>(options.sourceLayout) << ' '
366 << static_cast<int>(options.target) << ' '
367 << static_cast<int>(options.missing) << ' ' << std::setprecision(17)
368 << options.relativeTolerance << ' ' << options.absoluteTolerance;
369 CHKERR sameInput(signature.str());
370 auto t = boost::make_shared<Transfer>();
371 t->core = &cOre;
372 t->source = source;
373 t->options = options;
374 ParallelComm *pc = nullptr;
375 std::array<double, 3> lower, upper;
376 lower.fill(std::numeric_limits<double>::infinity());
377 upper.fill(-std::numeric_limits<double>::infinity());
378 std::vector<int> ids;
379 std::vector<double> replica;
380 CHKERR phase("source validation", [&]() {
382 if (transfer)
383 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
384 "Close an existing transfer before replacing it");
385 if ((options.sourceLayout != SourceLayout::REPLICATED &&
386 options.sourceLayout != SourceLayout::DISTRIBUTED) ||
387 (options.target != Target::VERTICES &&
388 options.target != Target::ELEMENT_CENTROIDS) ||
389 (options.missing != Missing::ERROR &&
390 options.missing != Missing::KEEP &&
391 options.missing != Missing::ADJACENT_ELEMENT_AVERAGE) ||
392 !std::isfinite(options.relativeTolerance) ||
393 !std::isfinite(options.absoluteTolerance) ||
394 options.relativeTolerance < 0 || options.absoluteTolerance < 0)
395 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
396 "Invalid coupling options/tolerances");
397 if (options.target == Target::ELEMENT_CENTROIDS &&
398 options.missing == Missing::ADJACENT_ELEMENT_AVERAGE)
399 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
400 "Adjacent-element fallback requires vertex targets");
401 if (options.sourceLayout == SourceLayout::DISTRIBUTED) {
402 CHKERR parallel(source, pc);
403 CHKERR owned(source, t->ownedElements);
404 } else {
405 t->ownedElements = source.elements;
406 }
407 Tag gid;
408 if (!source.elements.empty())
409 CHKERR mb().tag_get_handle(GLOBAL_ID_TAG_NAME, 1, MB_TYPE_INTEGER, gid);
410 for (auto e : source.elements) {
411 std::vector<EntityHandle> vertices;
412 CHKERR corners(e, vertices);
413 t->vertices.insert(vertices.data(), vertices.data() + vertices.size());
414 }
415 for (auto e : t->ownedElements) {
416 int id;
417 CHKERR mb().tag_get_data(gid, &e, 1, &id);
418 if (id <= 0 || id == INT_MAX)
419 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
420 "Source GLOBAL_ID must be positive and below INT_MAX");
421 ids.push_back(id);
422 std::vector<EntityHandle> vertices;
423 CHKERR corners(e, vertices);
424 std::vector<moab::CartVect> coords(vertices.size());
425 CHKERR mb().get_coords(vertices.data(), vertices.size(),
426 coords.front().array());
427 for (const auto &x : coords)
428 for (int d = 0; d != 3; ++d) {
429 if (!std::isfinite(x[d]))
430 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
431 "Nonfinite source coordinates");
432 lower[d] = std::min(lower[d], x[d]);
433 upper[d] = std::max(upper[d], x[d]);
434 }
435 std::unique_ptr<moab::Element::Map> map;
436 if (vertices.size() == 4)
437 map = std::make_unique<moab::Element::LinearTet>(coords);
438 else
439 map = std::make_unique<moab::Element::LinearHex>(coords);
440 const double determinant = map->det_jacobian(moab::CartVect(0.));
441 if (!std::isfinite(determinant) || determinant == 0.)
442 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA, "Singular source element");
443 Transfer::SourceElement cached;
444 cached.handle = e;
445 cached.id = id;
446 for (const auto &x : coords)
447 for (int d = 0; d != 3; ++d)
448 cached.coordinateScale =
449 std::max(cached.coordinateScale, std::abs(x[d]));
450 cached.map = std::move(map);
451 t->elementIndices.emplace(e, t->elements.size());
452 t->elements.push_back(std::move(cached));
453 }
454 // Canonical corner indices detect different connectivity even when
455 // unshared vertices happen to have identical coordinates.
456 std::map<int, size_t> sorted;
457 std::map<EntityHandle, size_t> corner_indices;
458 for (size_t i = 0; i != t->elements.size(); ++i)
459 if (!sorted.emplace(t->elements[i].id, i).second)
460 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
461 "Duplicate source GLOBAL_ID");
462 if (options.sourceLayout == SourceLayout::REPLICATED)
463 for (const auto &entry : sorted) {
464 const auto &cached = t->elements[entry.second];
465 std::vector<EntityHandle> vertices;
466 CHKERR corners(cached.handle, vertices);
467 std::vector<moab::CartVect> coords(vertices.size());
468 CHKERR mb().get_coords(vertices.data(), vertices.size(),
469 coords.front().array());
470 replica.push_back(cached.id);
471 replica.push_back(coords.size());
472 for (size_t v = 0; v != vertices.size(); ++v) {
473 replica.push_back(
474 corner_indices.emplace(vertices[v], corner_indices.size())
475 .first->second);
476 for (int d = 0; d != 3; ++d)
477 replica.push_back(coords[v][d]);
478 }
479 }
481 });
482 t->router = std::make_unique<moab::gs_data::crystal_data>(comm());
483 int rank, size;
484 CHKERR MPI_Comm_rank(comm(), &rank);
485 CHKERR MPI_Comm_size(comm(), &size);
486 const int active = !t->ownedElements.empty();
487 CHKERR MPI_Allreduce(&active, &t->sourceRanks, 1, MPI_INT, MPI_SUM, comm());
488 if (!t->sourceRanks)
489 SETERRQ(comm(), MOFEM_INVALID_DATA, "Empty global coupling source");
490 if (options.sourceLayout == SourceLayout::DISTRIBUTED) {
491 // Bucket identities rather than replicating the global source ID list.
492 moab::TupleList identities(2, 0, 0, 0, ids.size());
493 for (int id : ids)
494 append(identities, {id % size, id});
495 CHKERR route(*t, identities);
496 CHKERR phase("source identities", [&]() {
498 std::set<int> unique;
499 for (unsigned i = 0; i != identities.get_n(); ++i)
500 if (!unique.insert(identities.vi_rd[2 * i + 1]).second)
501 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
502 "Duplicate owned source GLOBAL_ID across partitions");
504 });
505 int has_pc = pc != nullptr, min_pc, max_pc;
506 CHKERR MPI_Allreduce(&has_pc, &min_pc, 1, MPI_INT, MPI_MIN, comm());
507 CHKERR MPI_Allreduce(&has_pc, &max_pc, 1, MPI_INT, MPI_MAX, comm());
508 if (min_pc != max_pc)
509 SETERRQ(comm(), MOFEM_INVALID_DATA,
510 "Distributed source ParallelComm must exist on every rank or use "
511 "-1 everywhere");
512 } else {
513 // Hash ordered identities/connectivity geometry; communication is O(ranks).
514 boost::uuids::detail::sha1 hash;
515 for (double value : replica) {
516 if (value == 0.)
517 value = 0.; // Canonicalise signed zero.
518 hash.process_bytes(&value, sizeof(value));
519 }
520 boost::uuids::detail::sha1::digest_type digest;
521 hash.get_digest(digest);
522 std::ostringstream fingerprint;
523 fingerprint << ids.size() << ':' << replica.size();
524 for (auto word : digest)
525 fingerprint << ':' << static_cast<unsigned>(word);
526 CHKERR sameInput(fingerprint.str(), "source replicas");
527 }
528 std::array<double, 6> box;
529 std::copy(lower.begin(), lower.end(), box.begin());
530 std::copy(upper.begin(), upper.end(), box.begin() + 3);
531 t->boxes.resize(6 * size);
532 CHKERR MPI_Allgather(box.data(), 6, MPI_DOUBLE, t->boxes.data(), 6,
533 MPI_DOUBLE, comm());
534 std::array<double, 3> global_lower, global_upper;
535 CHKERR MPI_Allreduce(lower.data(), global_lower.data(), 3, MPI_DOUBLE,
536 MPI_MIN, comm());
537 CHKERR MPI_Allreduce(upper.data(), global_upper.data(), 3, MPI_DOUBLE,
538 MPI_MAX, comm());
539 double scale = 0;
540 for (int d = 0; d != 3; ++d)
541 scale = std::hypot(scale, global_upper[d] - global_lower[d]);
542 if (!std::isfinite(scale) || scale <= 0)
543 SETERRQ(comm(), MOFEM_INVALID_DATA, "Invalid source bounding-box scale");
544 t->distance =
545 std::max(options.absoluteTolerance, options.relativeTolerance * scale);
546 if (!std::isfinite(t->distance))
547 SETERRQ(comm(), MOFEM_INVALID_DATA,
548 "Nonfinite resolved coupling tolerance");
549 CHKERR phase("source tree", [&]() {
551 if (!t->ownedElements.empty()) {
552 t->database = &mb();
553 t->backendComm = std::make_unique<ParallelComm>(&mb(), PETSC_COMM_SELF);
554 CHKERR mb().create_meshset(MESHSET_SET, t -> part);
555 CHKERR mb().add_entities(t -> part, t->ownedElements);
556 t->backendComm->partition_sets().insert(t->part);
557 t->backend = std::make_unique<moab::Coupler>(&mb(), t->backendComm.get(),
558 t->ownedElements, 0, false);
559 CHKERR t->backend->initialize_tree();
560 // Allocate the backend cache once. Calling locate_points again leaks the
561 // previous cache in MOAB 5.5.1; subsequent batches reset it in place.
562 moab::TupleList empty;
563 double dummy[3] = {};
564 CHKERR t->backend->locate_points(dummy, 0, 0., t->distance, &empty,
565 false);
566 }
568 });
569 t->closed = false;
570 transfer = t;
571#endif
573}
574
575#ifdef WITH_MBCOUPLER
576MoFEMErrorCode CouplerInterfaceImpl::localLocation(
577 Transfer &t, const double *xyz, EntityHandle &element,
578 std::array<double, 3> &coordinates, std::vector<EntityHandle> &leaves,
579 Range &candidates) const {
581 element = 0;
582 int best_id = INT_MAX;
583 if (t.backend) {
584 leaves.clear();
585 candidates.clear();
586 auto root = t.backend->local_root();
587 CHKERR t.backend->my_tree()->distance_search(xyz, t.distance, leaves,
588 t.distance, t.distance,
589 nullptr, nullptr, &root);
590 for (auto leaf : leaves)
591 CHKERR mb().get_entities_by_dimension(leaf, 3, candidates);
592 for (auto e : candidates) {
593 const auto &cached = t.elements[t.elementIndices.at(e)];
594 const auto id = cached.id;
595 if (id >= best_id)
596 continue;
597 auto &map = *cached.map;
598 const moab::CartVect x(xyz);
599 double physical = t.distance;
600 if (!map.inside_box(x, physical))
601 continue;
602 moab::CartVect natural(0.);
603 // MOAB 5.5.1 Map::ievaluate ignores its tolerance argument and uses
604 // an absolute determinant cutoff. Solve here with a physical residual.
605 const double coordinate_scale = cached.coordinateScale;
606 const double solve_tol = std::max(
607 t.distance * 0.01,
608 64 * std::numeric_limits<double>::epsilon() * coordinate_scale);
609 bool converged = false;
610 for (int iteration = 0; iteration != 30; ++iteration) {
611 const auto residual = map.evaluate(natural) - x;
612 if (std::isfinite(residual.length()) &&
613 residual.length() <= solve_tol) {
614 converged = true;
615 break;
616 }
617 const auto jacobian = map.jacobian(natural);
618 const double determinant = jacobian.determinant();
619 if (!std::isfinite(determinant) || determinant == 0.)
620 break;
621 natural -= jacobian.inverse() * residual;
622 }
623 if (!converged)
624 continue;
625 const auto inverse = map.ijacobian(natural);
626 double inverse_norm = 0;
627 for (int i = 0; i != 3; ++i) {
628 double row = 0;
629 for (int j = 0; j != 3; ++j)
630 row += std::abs(inverse(i, j));
631 inverse_norm = std::max(inverse_norm, row);
632 }
633 double inside_tol = t.distance * inverse_norm +
634 64 * std::numeric_limits<double>::epsilon();
635 if (!map.inside_nat_space(natural, inside_tol))
636 continue;
637 const auto back = map.evaluate(natural);
638 if ((back - x).length() > std::max(t.distance, solve_tol) * 4)
639 continue;
640 element = e;
641 best_id = id;
642 for (int d = 0; d != 3; ++d)
643 coordinates[d] = natural[d];
644 }
645 }
647}
648
649MoFEMErrorCode CouplerInterfaceImpl::locate(Transfer &t,
650 const std::vector<double> &points,
651 Locations &locations) const {
653 int rank, size;
654 CHKERR MPI_Comm_rank(comm(), &rank);
655 CHKERR MPI_Comm_size(comm(), &size);
656 const auto n = points.size() / 3;
657 locations.winners.assign(n, -1);
658 locations.mappedIndices.assign(n, -1);
659 moab::TupleList requests(2, 0, 0, 3, n);
660 CHKERR phase("location requests", [&]() {
662 if (n > INT_MAX)
663 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
664 "Coupling batch exceeds int indices");
665 if (t.backend) {
666 auto mapped = t.backend->mapped_pts();
667 mapped->set_n(0);
668 }
669 for (size_t p = 0; p != n; ++p) {
670 const auto x = points.data() + 3 * p;
671 // Complete replicas search locally; distributed requests visit only
672 // ranks whose source bounding box contains this point.
673 const bool replicated =
674 t.options.sourceLayout == SourceLayout::REPLICATED;
675 for (int r = replicated ? rank : 0, end = replicated ? rank + 1 : size;
676 r != end; ++r) {
677 const auto box = t.boxes.data() + 6 * r;
678 bool inside = true;
679 for (int d = 0; d != 3; ++d)
680 inside &=
681 x[d] >= box[d] - t.distance && x[d] <= box[d + 3] + t.distance;
682 if (inside)
683 append(requests, {r, static_cast<int>(p)}, {}, {x[0], x[1], x[2]});
684 }
685 }
687 });
688 if (t.options.sourceLayout == SourceLayout::DISTRIBUTED)
689 CHKERR route(t, requests);
690 moab::TupleList matches(4, 0, 0, 0, requests.get_n());
691 CHKERR phase("source location", [&]() {
693 std::vector<EntityHandle> leaves;
694 Range candidates;
695 for (unsigned i = 0; i != requests.get_n(); ++i) {
696 EntityHandle element;
697 std::array<double, 3> natural;
698 CHKERR localLocation(t, requests.vr_rd + 3 * i, element, natural, leaves,
699 candidates);
700 if (!element)
701 continue;
702 auto mapped = t.backend->mapped_pts();
703 const auto index = mapped->get_n();
704 append(*mapped, {}, {element}, {natural[0], natural[1], natural[2]});
705 append(matches, {requests.vi_rd[2 * i], requests.vi_rd[2 * i + 1],
706 t.elements[t.elementIndices.at(element)].id,
707 static_cast<int>(index)});
708 }
710 });
711 if (t.options.sourceLayout == SourceLayout::DISTRIBUTED)
712 CHKERR route(t, matches);
713 std::vector<int> best(n, INT_MAX);
714 for (unsigned i = 0; i != matches.get_n(); ++i) {
715 const auto p = matches.vi_rd[4 * i + 1];
716 if (matches.vi_rd[4 * i + 2] < best[p]) {
717 best[p] = matches.vi_rd[4 * i + 2];
718 locations.winners[p] = matches.vi_rd[4 * i];
719 locations.mappedIndices[p] = matches.vi_rd[4 * i + 3];
720 }
721 }
723}
724
725#endif
726
727/// Resolve target owners, locate points/centroids and report unresolved targets.
728MoFEMErrorCode CouplerInterfaceImpl::locateTargets(const TransferPtr &t,
729 const Selection &target,
730 LocationsPtr &locations,
731 Report &report) {
733 auto l = boost::make_shared<Locations>();
734 ParallelComm *pc = nullptr;
735 Range target_entities;
736 int rank;
737 CHKERR MPI_Comm_rank(comm(), &rank);
738 CHKERR phase("target validation", [&]() {
740 CHKERR checkTransfer(t);
741 CHKERR parallel(target, pc);
742 for (auto e : target.elements) {
743 std::vector<EntityHandle> vertices;
744 CHKERR corners(e, vertices);
745 if (t->options.target == Target::VERTICES)
746 target_entities.insert(vertices.data(),
747 vertices.data() + vertices.size());
748 else
749 target_entities.insert(e);
750 }
752 });
753#ifdef WITH_MBCOUPLER
754 // Route selected entities and selected-cell incidences to target owners.
755 // Tuple: destination, kind, entity owner, cell owner; handles; coordinates.
756 moab::TupleList requests(4, 0, 2, 3, target_entities.size());
757 CHKERR phase("target requests", [&]() {
759 for (auto e : target_entities) {
760 Locations::Key k;
761 std::array<double, 3> x;
762 CHKERR key(pc, e, k);
763 CHKERR coordinates(e, x);
764 append(requests, {k.first, 0, k.first, -1}, {k.second, 0},
765 {x[0], x[1], x[2]});
766 }
767 if (t->options.missing == Missing::ADJACENT_ELEMENT_AVERAGE) {
768 Range cells;
769 CHKERR owned(target, cells);
770 for (auto cell : cells) {
771 std::array<double, 3> x;
772 CHKERR coordinates(cell, x);
773 std::vector<EntityHandle> vertices;
774 CHKERR corners(cell, vertices);
775 for (auto v : vertices) {
776 Locations::Key k;
777 CHKERR key(pc, v, k);
778 append(requests, {k.first, 1, k.first, rank}, {k.second, cell},
779 {x[0], x[1], x[2]});
780 }
781 }
782 }
784 });
785 CHKERR route(*t, requests);
786 /// Owner-side subscribers and unique adjacent cells for vertex fallback.
787 struct OwnedTarget {
788 std::set<int> subscribers;
789 std::map<Locations::Key, std::array<double, 3>> centroids;
790 };
791 std::map<EntityHandle, OwnedTarget> selected;
792 CHKERR phase("target owner selection", [&]() {
794 for (unsigned i = 0; i != requests.get_n(); ++i) {
795 const EntityHandle e = requests.vul_rd[2 * i];
796 auto &entry = selected[e];
797 if (!requests.vi_rd[4 * i + 1]) {
798 std::array<double, 3> x;
799 CHKERR coordinates(e, x);
800 // True shared copies have identical coordinates; this is not a
801 // geometric point matching test, so no approximate comparison applies.
802 for (int d = 0; d != 3; ++d)
803 if (x[d] != requests.vr_rd[3 * i + d])
804 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
805 "Shared target coordinates differ from their owner");
806 entry.subscribers.insert(requests.vi_rd[4 * i]);
807 } else {
808 const Locations::Key cell{requests.vi_rd[4 * i + 3],
809 requests.vul_rd[2 * i + 1]};
810 std::copy_n(requests.vr_rd + 3 * i, 3, entry.centroids[cell].begin());
811 }
812 entry.subscribers.insert(rank);
813 }
815 });
816 // Return the owner's complete selected adjacency to each selected copy.
817 // This includes owners with no selected cells and partition-seam fallback.
818 moab::TupleList replies(4, 0, 2, 3, requests.get_n());
819 CHKERR phase("target subscriptions", [&]() {
821 for (const auto &item : selected) {
822 std::array<double, 3> x;
823 CHKERR coordinates(item.first, x);
824 for (int subscriber : item.second.subscribers) {
825 append(replies, {subscriber, 0, rank, -1}, {item.first, 0},
826 {x[0], x[1], x[2]});
827 for (const auto &cell : item.second.centroids) {
828 const auto &c = cell.second;
829 append(replies, {subscriber, 1, rank, cell.first.first},
830 {item.first, cell.first.second}, {c[0], c[1], c[2]});
831 }
832 }
833 }
835 });
836 CHKERR route(*t, replies);
837 std::vector<double> points;
838 std::map<Locations::Key, size_t> indices, centroids;
839 for (unsigned i = 0; i != replies.get_n(); ++i) {
840 if (replies.vi_rd[4 * i + 1])
841 continue;
842 const Locations::Key k{replies.vi_rd[4 * i + 2], replies.vul_rd[2 * i]};
843 if (indices.emplace(k, points.size() / 3).second)
844 points.insert(points.end(), replies.vr_rd + 3 * i,
845 replies.vr_rd + 3 * i + 3);
846 if (k.first == rank)
847 target_entities.insert(k.second);
848 }
849 l->fallback.resize(indices.size());
850 for (unsigned i = 0; i != replies.get_n(); ++i) {
851 if (!replies.vi_rd[4 * i + 1])
852 continue;
853 const Locations::Key k{replies.vi_rd[4 * i + 2], replies.vul_rd[2 * i]};
854 const Locations::Key cell{replies.vi_rd[4 * i + 3],
855 replies.vul_rd[2 * i + 1]};
856 auto inserted = centroids.emplace(cell, points.size() / 3);
857 if (inserted.second)
858 points.insert(points.end(), replies.vr_rd + 3 * i,
859 replies.vr_rd + 3 * i + 3);
860 l->fallback[indices.at(k)].push_back(inserted.first->second);
861 }
862 CHKERR phase("target ownership", [&]() {
864 for (auto e : target_entities) {
865 Locations::Key k;
866 CHKERR key(pc, e, k);
867 auto it = indices.find(k);
868 if (it == indices.end())
869 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
870 "Missing target owner subscription");
871 l->localIndices[e] = it->second;
872 }
874 });
875 // From this point the mutable backend cache is being replaced, so invalidate
876 // previous locations even if a later phase fails.
877 l->generation = ++t->generation;
878 CHKERR locate(*t, points, *l);
879 l->status.resize(points.size() / 3, 0);
880 unsigned long long local_counts[3] = {};
881 for (const auto &item : indices) {
882 const auto p = item.second;
883 if (l->winners[p] >= 0)
884 l->status[p] = 1;
885 else if (!l->fallback[p].empty() &&
886 std::all_of(l->fallback[p].begin(), l->fallback[p].end(),
887 [&](size_t q) { return l->winners[q] >= 0; }))
888 l->status[p] = 2;
889 if (item.first.first != rank)
890 continue;
891 const EntityHandle e = item.first.second;
892 if (l->status[p] == 1)
893 l->report.locatedEntities.insert(e);
894 else if (l->status[p] == 2)
895 l->report.fallbackEntities.insert(e);
896 else
897 l->report.missingEntities.insert(e);
898 ++local_counts[l->status[p]];
899 }
900 unsigned long long global_counts[3];
901 CHKERR MPI_Allreduce(local_counts, global_counts, 3, MPI_UNSIGNED_LONG_LONG,
902 MPI_SUM, comm());
903 l->report.globalMissing = global_counts[0];
904 l->report.globalLocated = global_counts[1];
905 l->report.globalFallback = global_counts[2];
906#endif
907 l->transfer = t;
908 report = l->report;
909 locations = l;
911}
912
913#ifdef WITH_MBCOUPLER
914MoFEMErrorCode CouplerInterfaceImpl::sample(Transfer &t, const Locations &l,
915 const TagMap &mapping,
916 int source_components,
917 std::vector<double> &values) {
919 const int n = mapping.component < 0 ? source_components : 1;
920 Tag source_tag = nullptr;
921 ParallelComm *pc = nullptr;
922 const bool nodal = mapping.sampling == Sampling::VERTEX_LINEAR ||
923 mapping.sampling == Sampling::VERTEX_AVERAGED_CONSTANT;
924 const bool recovery = mapping.sampling == Sampling::ELEMENT_RECOVERED_LINEAR;
925 std::map<EntityHandle, std::vector<double>> data;
926 CouplerScratchTag scratch{mb()};
927 std::vector<double> zero(n + 1, 0.);
928 // Retain all components or the requested one, checking source finiteness.
929 const auto read_value = [&](EntityHandle entity, std::vector<double> &value) {
931 std::vector<double> source(source_components);
932 CHKERR mb().tag_get_data(source_tag, &entity, 1, source.data());
933 if (mapping.component < 0)
934 value = std::move(source);
935 else
936 value.assign(1, source[mapping.component]);
937 for (double x : value)
938 if (!std::isfinite(x))
939 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
940 "Nonfinite source tag value");
942 };
943 CHKERR phase("source communicator", [&]() {
945 if (t.options.sourceLayout == SourceLayout::DISTRIBUTED)
946 CHKERR parallel(t.source, pc);
947 if (recovery && !pc &&
948 t.options.sourceLayout == SourceLayout::DISTRIBUTED &&
949 t.sourceRanks > 1)
950 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
951 "Distributed recovery with multiple source ranks requires "
952 "ParallelComm sharing metadata");
954 });
955 CHKERR phase("source sampling data", [&]() {
957 if (!t.source.elements.empty())
958 CHKERR mb().tag_get_handle(mapping.sourceTag.c_str(), source_tag);
959 const Range &entities = nodal ? t.vertices : t.ownedElements;
960 for (auto e : entities) {
961 if (nodal && pc) {
962 Locations::Key k;
963 CHKERR key(pc, e, k);
964 int rank;
965 CHKERR MPI_Comm_rank(comm(), &rank);
966 if (k.first != rank)
967 continue; // Ghost data is deliberately ignored.
968 }
969 CHKERR read_value(e, data[e]);
970 }
972 });
973 CouplerScratchTag nodal_copy{mb()};
974 if (nodal && pc) {
975 CHKERR scratchTag(n, nodal_copy.tag);
976 Range exchange_vertices = t.vertices;
977 moab::TupleList requests(1, 0, 1, 0, t.vertices.size());
978 CHKERR phase("source vertex owners", [&]() {
980 for (auto v : t.vertices) {
981 Locations::Key k;
982 CHKERR key(pc, v, k);
983 append(requests, {k.first}, {k.second});
984 }
986 });
987 CHKERR route(t, requests);
988 CHKERR phase("owner source values", [&]() {
990 // An owner may have no selected cells. Initialise its requested vertex
991 // copy before ParallelComm exchanges the private tag, never the input.
992 if (requests.get_n())
993 CHKERR mb().tag_get_handle(mapping.sourceTag.c_str(), source_components,
994 MB_TYPE_DOUBLE, source_tag);
995 for (unsigned i = 0; i != requests.get_n(); ++i) {
996 const auto v = requests.vul_rd[i];
997 std::vector<double> value;
998 CHKERR read_value(v, value);
999 CHKERR mb().tag_set_data(nodal_copy.tag, &v, 1, value.data());
1000 exchange_vertices.insert(v);
1001 }
1003 });
1004 CHKERR phase("source vertex exchange", [&]() {
1006 CHKERR pc->exchange_tags(nodal_copy.tag, exchange_vertices);
1008 });
1009 CHKERR phase("synchronised source data", [&]() {
1011 for (auto v : t.vertices) {
1012 data[v].resize(n);
1013 CHKERR mb().tag_get_data(nodal_copy.tag, &v, 1, data[v].data());
1014 }
1016 });
1017 }
1018 if (recovery) {
1019 CHKERR scratchTag(n + 1, scratch.tag);
1020 CHKERR phase("source recovery sums", [&]() {
1022 std::map<EntityHandle, std::vector<double>> sums;
1023 for (auto e : t.ownedElements) {
1024 std::vector<EntityHandle> vertices;
1025 CHKERR corners(e, vertices);
1026 for (auto v : vertices) {
1027 auto &sum = sums[v];
1028 if (sum.empty())
1029 sum = zero;
1030 for (int c = 0; c != n; ++c)
1031 sum[c] += data.at(e)[c];
1032 sum[n] += 1.;
1033 }
1034 }
1035 for (auto &v : sums)
1036 CHKERR mb().tag_set_data(scratch.tag, &v.first, 1, v.second.data());
1038 });
1039 }
1040 if (recovery && t.options.sourceLayout == SourceLayout::DISTRIBUTED) {
1041 CHKERR phase("source communicator",
1042 [&]() { return parallel(t.source, pc); });
1043 if (pc)
1044 CHKERR phase("source recovery reduction", [&]() -> MoFEMErrorCode {
1046 CHKERR pc->reduce_tags(scratch.tag, MPI_SUM, t.vertices);
1048 });
1049 }
1050 CHKERR phase("recovered source data", [&]() {
1052 if (recovery) {
1053 data.clear();
1054 for (auto v : t.vertices) {
1055 std::vector<double> sum(n + 1);
1056 CHKERR mb().tag_get_data(scratch.tag, &v, 1, sum.data());
1057 if (sum[n] > 0.) {
1058 auto &value = data[v];
1059 value.resize(n);
1060 for (int c = 0; c != n; ++c)
1061 value[c] = sum[c] / sum[n];
1062 }
1063 }
1064 }
1065 if (mapping.sampling == Sampling::VERTEX_AVERAGED_CONSTANT) {
1066 for (auto e : t.ownedElements) {
1067 std::vector<EntityHandle> vertices;
1068 CHKERR corners(e, vertices);
1069 auto &value = data[e];
1070 value.assign(n, 0.);
1071 for (auto v : vertices)
1072 for (int c = 0; c != n; ++c)
1073 value[c] += data.at(v)[c] / vertices.size();
1074 }
1075 }
1076 for (const auto &entry : data)
1077 for (double x : entry.second)
1078 if (!std::isfinite(x))
1079 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
1080 "Nonfinite recovered source value");
1082 });
1083 moab::TupleList queries(3, 0, 0, n, l.winners.size());
1084 for (size_t p = 0; p != l.winners.size(); ++p)
1085 if (l.winners[p] >= 0) {
1086 append(queries, {l.winners[p], static_cast<int>(p), l.mappedIndices[p]});
1087 std::fill_n(queries.vr_wr + (queries.get_n() - 1) * n, n, 0.);
1088 }
1089 if (t.options.sourceLayout == SourceLayout::DISTRIBUTED)
1090 CHKERR route(t, queries);
1091 CouplerScratchTag scalar{mb()};
1092 CHKERR scratchTag(1, scalar.tag);
1093 CHKERR phase("MBCoupler interpolation", [&]() {
1095 if (queries.get_n()) {
1096 if (!t.backend || queries.get_n() > INT_MAX)
1097 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
1098 "Invalid source interpolation batch");
1099 moab::TupleList locations(3, 0, 0, 0, queries.get_n());
1100 for (unsigned i = 0; i != queries.get_n(); ++i)
1101 append(locations, {0, static_cast<int>(i), queries.vi_rd[3 * i + 2]});
1102 std::vector<double> component(queries.get_n());
1103 const auto method =
1104 (mapping.sampling == Sampling::ELEMENT_CONSTANT ||
1105 mapping.sampling == Sampling::VERTEX_AVERAGED_CONSTANT)
1106 ? moab::Coupler::CONSTANT
1107 : moab::Coupler::LINEAR_FE;
1108 const Range &entities =
1109 method == moab::Coupler::CONSTANT ? t.ownedElements : t.vertices;
1110 // MBCoupler's scalar API handles each component and its own local tuple
1111 // routing; the Core crystal router carries only the requested results.
1112 for (int c = 0; c != n; ++c) {
1113 for (auto e : entities) {
1114 const auto &value = data.at(e);
1115 CHKERR mb().tag_set_data(scalar.tag, &e, 1, &value[c]);
1116 }
1117 CHKERR t.backend->interpolate(method, scalar.tag, component.data(),
1118 &locations, false);
1119 queries.enableWriteAccess();
1120 for (unsigned i = 0; i != queries.get_n(); ++i)
1121 queries.vr_wr[i * n + c] = component[i];
1122 }
1123 }
1125 });
1126 if (t.options.sourceLayout == SourceLayout::DISTRIBUTED)
1127 CHKERR route(t, queries);
1128 values.assign(l.winners.size() * n, 0.);
1129 for (unsigned i = 0; i != queries.get_n(); ++i)
1130 std::copy_n(queries.vr_rd + i * n, n,
1131 values.begin() + queries.vi_rd[3 * i + 1] * n);
1132
1133 for (size_t p = 0; p != l.status.size(); ++p)
1134 if (l.status[p] == 2)
1135 for (int c = 0; c != n; ++c) {
1136 double sum = 0;
1137 for (auto q : l.fallback[p])
1138 sum += values[q * n + c] / l.fallback[p].size();
1139 values[p * n + c] = sum;
1140 }
1141 CHKERR phase("sample finiteness", [&]() {
1143 for (double value : values)
1144 if (!std::isfinite(value))
1145 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
1146 "Nonfinite sampled/fallback value");
1148 });
1150}
1151#endif
1152
1153/// Stage every tag map before publication; restore snapshots on write failure.
1155CouplerInterfaceImpl::transferTags(const TransferPtr &t, const LocationsPtr &l,
1156 const std::vector<TagMap> &tags,
1157 Report &report) {
1159 CHKERR phase("location lifetime", [&]() {
1161 CHKERR checkTransfer(t);
1162 if (!l || l->transfer.lock() != t || l->generation != t->generation)
1163 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
1164 "Stale or foreign coupling locations");
1166 });
1167 std::ostringstream signature;
1168 for (const auto &map : tags)
1169 signature << map.sourceTag.size() << ':' << map.sourceTag
1170 << map.targetTag.size() << ':' << map.targetTag << ':'
1171 << static_cast<int>(map.sampling) << ':' << map.component << ';';
1172 CHKERR sameInput(signature.str());
1173 report = l->report;
1174 if (report.globalMissing && t->options.missing != Missing::KEEP)
1175 SETERRQ(comm(), MOFEM_DATA_INCONSISTENCY, "Unresolved target points: %llu",
1176 report.globalMissing);
1177 /// Pending destination values and snapshots needed to undo publication.
1178 struct StagedTag {
1179 Tag tag = nullptr;
1180 bool existed = false, created = false;
1181 int components = 0;
1182 Range entities;
1183 std::vector<double> values;
1184 std::map<EntityHandle, std::vector<double>> oldValues;
1185 // Restore absent sparse entries, including those backed by tag defaults.
1186 Range oldAbsent;
1187 };
1188 std::vector<StagedTag> staged(tags.size());
1189 std::set<std::string> destinations;
1190 for (size_t i = 0; i != tags.size(); ++i) {
1191 const auto &mapping = tags[i];
1192 auto &s = staged[i];
1193 int local_components = 0;
1194 CHKERR phase("tag schema", [&]() {
1196 if (mapping.sourceTag.empty() || mapping.targetTag.empty() ||
1197 !destinations.insert(mapping.targetTag).second ||
1198 static_cast<int>(mapping.sampling) < 0 ||
1199 static_cast<int>(mapping.sampling) > 3 || mapping.component < -1)
1200 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
1201 "Empty tag name, duplicate destination or invalid sampling");
1202 Tag source_tag;
1203 if (!t->source.elements.empty()) {
1204 CHKERR mb().tag_get_handle(mapping.sourceTag.c_str(), source_tag);
1205 DataType type;
1206 CHKERR mb().tag_get_data_type(source_tag, type);
1207 CHKERR mb().tag_get_length(source_tag, local_components);
1208 if (type != MB_TYPE_DOUBLE || local_components <= 0)
1209 SETERRQ(
1210 PETSC_COMM_SELF, MOFEM_INVALID_DATA,
1211 "Coupling source requires a fixed positive double tag length");
1212 }
1214 });
1215 int source_components;
1216 CHKERR MPI_Allreduce(&local_components, &source_components, 1, MPI_INT,
1217 MPI_MAX, comm());
1218 if (mapping.component >= source_components)
1219 SETERRQ(comm(), MOFEM_INVALID_DATA,
1220 "Source component index is out of bounds");
1221 s.components = mapping.component < 0 ? source_components : 1;
1222 CHKERR phase("destination schema and snapshot", [&]() {
1224 if (local_components && local_components != source_components)
1225 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
1226 "Source component count differs across ranks");
1227 auto error = mb().tag_get_handle(mapping.targetTag.c_str(), s.tag);
1228 if (error != MB_SUCCESS && error != MB_TAG_NOT_FOUND)
1229 CHKERR error;
1230 s.existed = error == MB_SUCCESS;
1231 TagType storage = MB_TAG_SPARSE;
1232 std::vector<double> defaults(s.components);
1233 bool has_default = false;
1234 if (s.existed) {
1235 DataType type;
1236 int n;
1237 CHKERR mb().tag_get_data_type(s.tag, type);
1238 CHKERR mb().tag_get_length(s.tag, n);
1239 CHKERR mb().tag_get_type(s.tag, storage);
1240 if (type != MB_TYPE_DOUBLE || n != s.components ||
1241 (storage != MB_TAG_SPARSE && storage != MB_TAG_DENSE))
1242 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
1243 "Destination tag type/length/storage mismatch");
1244 error = mb().tag_get_default_value(s.tag, defaults.data());
1245 if (error != MB_SUCCESS && error != MB_ENTITY_NOT_FOUND)
1246 CHKERR error;
1247 has_default = error == MB_SUCCESS &&
1248 std::all_of(defaults.begin(), defaults.end(),
1249 [](double x) { return std::isfinite(x); });
1250 }
1251 for (const auto &entry : l->localIndices) {
1252 const auto e = entry.first;
1253 const size_t p = entry.second;
1254 if (!l->status[p]) {
1255 if (!s.existed)
1256 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
1257 "KEEP requires an existing destination");
1258 std::vector<Tag> explicit_tags;
1259 CHKERR mb().tag_get_tags_on_entity(e, explicit_tags);
1260 const bool explicit_sparse =
1261 storage == MB_TAG_SPARSE &&
1262 std::find(explicit_tags.begin(), explicit_tags.end(), s.tag) !=
1263 explicit_tags.end();
1264 if (!explicit_sparse && !has_default)
1265 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
1266 "KEEP requires explicit sparse data or a finite default");
1267 std::vector<double> old(s.components);
1268 CHKERR mb().tag_get_data(s.tag, &e, 1, old.data());
1269 if (!std::all_of(old.begin(), old.end(),
1270 [](double x) { return std::isfinite(x); }))
1271 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
1272 "KEEP destination is nonfinite");
1273 continue;
1274 }
1275 s.entities.insert(e);
1276 if (s.existed) {
1277 auto &old = s.oldValues[e];
1278 old.resize(s.components);
1279 error = mb().tag_get_data(s.tag, &e, 1, old.data());
1280 if (error == MB_TAG_NOT_FOUND) {
1281 s.oldValues.erase(e);
1282 s.oldAbsent.insert(e);
1283 } else {
1284 CHKERR error;
1285 // Default-backed sparse data must remain absent after rollback.
1286 if (storage == MB_TAG_SPARSE) {
1287 std::vector<Tag> explicit_tags;
1288 CHKERR mb().tag_get_tags_on_entity(e, explicit_tags);
1289 if (std::find(explicit_tags.begin(), explicit_tags.end(),
1290 s.tag) == explicit_tags.end()) {
1291 s.oldValues.erase(e);
1292 s.oldAbsent.insert(e);
1293 }
1294 }
1295 }
1296 }
1297 }
1299 });
1300#ifdef WITH_MBCOUPLER
1301 std::vector<double> values;
1302 CHKERR sample(*t, *l, mapping, source_components, values);
1303 for (auto e : s.entities) {
1304 const auto p = l->localIndices.at(e);
1305 s.values.insert(s.values.end(), values.begin() + p * s.components,
1306 values.begin() + (p + 1) * s.components);
1307 }
1308#endif
1309 }
1310 const auto publish_error = phase("tag publication", [&]() {
1312 for (size_t i = 0; i != tags.size(); ++i) {
1313 auto &s = staged[i];
1314 if (!s.existed) {
1315 CHKERR mb().tag_get_handle(tags[i].targetTag.c_str(), s.components,
1316 MB_TYPE_DOUBLE, s.tag,
1317 MB_TAG_SPARSE | MB_TAG_CREAT | MB_TAG_EXCL);
1318 s.created = true;
1319 }
1320 if (!s.entities.empty())
1321 CHKERR mb().tag_set_data(s.tag, s.entities, s.values.data());
1322 }
1324 });
1325 if (publish_error) {
1326 CHKERR phase("publication rollback", [&]() {
1328 for (auto &s : staged) {
1329 if (s.created)
1330 CHKERR mb().tag_delete(s.tag);
1331 else if (s.existed) {
1332 for (const auto &old : s.oldValues)
1333 CHKERR mb().tag_set_data(s.tag, &old.first, 1, old.second.data());
1334 if (!s.oldAbsent.empty())
1335 CHKERR mb().tag_delete_data(s.tag, s.oldAbsent);
1336 }
1337 }
1339 });
1340 CHKERR publish_error;
1341 }
1343}
1344
1345/// Collectively close a transfer and invalidate its aliases and cached locations.
1346MoFEMErrorCode CouplerInterfaceImpl::destroyTransfer(TransferPtr &t) {
1348 CHKERR phase("transfer close", [&]() {
1350 if (t && t->core != &cOre)
1351 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA, "Cross-Core coupling close");
1353 });
1354 if (t) {
1355#ifdef WITH_MBCOUPLER
1356 t->backend.reset();
1357 t->backendComm.reset();
1358 if (t->part) {
1359 CHKERR mb().delete_entities(&t -> part, 1);
1360 t->part = 0;
1361 }
1362 t->router.reset();
1363 t->elements.clear();
1364 t->elementIndices.clear();
1365#endif
1366 t->closed = true;
1367 ++t->generation;
1368 t.reset();
1369 }
1371}
1372
1373} // namespace
1374
1375/// Construct the coupling implementation for MoFEM's interface factory.
1376template <>
1377CouplerInterface *createInterface<CouplerInterface>(const Core &core) {
1378 return new CouplerInterfaceImpl(core);
1379}
1380
1381} // namespace MoFEM
std::map< EntityHandle, int > index
bool selected
Definition CGALTools.cpp:97
size_t best
Definition CGALTools.cpp:63
Report & report
std::vector< int > created
std::vector< int > source
const std::vector< Vec > * points
std::vector< int > size
double length
std::vector< int > vertices
Vec t
Removal: the ring edge.
bool back
Slid back beyond the node along its edge.
std::vector< Cell > cells
EntityHandle handle
ParentChildKey key
bool active
MPI_Comm comm
moab::Interface & mb
unsigned long long scratchSequence
Core & cOre
Tag tag
std::map< EntityHandle, size_t > offsets
int components
Inferred from the source tag length.
const Field * target
double residual
std::vector< double > empty
One value per component.
std::vector< std::array< double, 3 > > coordinates
std::map< EntityHandle, std::vector< double > > staged
std::string type
std::vector< FieldBitNumber > row
Definition Schur.cpp:2488
It it
Definition Schur.cpp:2568
It end
Definition Schur.cpp:2568
SmartPetscObj< Vec > work
SmartPetscObj< Mat > jacobian
SmartPetscObj< Vec > values
SmartPetscObj< Mat > inverse
std::string name
#define CHK_THROW_MESSAGE(err, msg)
Check and throw MoFEM exception.
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
#define CHK_MOAB_THROW(err, msg)
Check error code of MoAB function and throw MoFEM exception.
@ 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.
@ F
#define MOFEM_LOG(channel, severity)
Log.
FTensor::Index< 'i', SPACE_DIM > i
const double c
speed of light (cm/ns)
const double v
phase velocity of light in medium (cm/ns)
FTensor::Index< 'l', 3 > l
FTensor::Index< 'j', 3 > j
FTensor::Index< 'k', 3 > k
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
auto type_from_handle(const EntityHandle h)
get type from entity handle
CoreTmp< 0 > Core
Definition Core.hpp:1161
DeprecatedCoreInterface Interface
int r
Definition sdf.py:205
double scale
Definition plastic.cpp:124
double q
Core (interface) class.
Definition Core.hpp:83
Target-to-source matches tied to one generation of the backend point cache.
std::pair< int, unsigned long long > Key
std::map< EntityHandle, size_t > localIndices
std::vector< std::vector< size_t > > fallback
boost::weak_ptr< Transfer > transfer
Source selection, search backend and routing state shared by location batches.
~Transfer()
Release local backend resources; collective closure uses destroyTransfer.