7 #include <mbcoupler/ElemUtil.hpp>
8 #include <mbcoupler/Coupler.hpp>
11#include <boost/uuid/detail/sha1.hpp>
16#include <initializer_list>
24#include <unordered_map>
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;
46 std::vector<double> boxes;
49 struct SourceElement {
52 double coordinateScale = 0.;
53 std::unique_ptr<moab::Element::Map> map;
55 std::vector<SourceElement> elements;
56 std::unordered_map<EntityHandle, size_t> elementIndices;
62 <<
"CouplerInterface: transfer left unclosed; call destroyTransfer "
63 "before destroying the Core";
68 database->delete_entities(&part, 1);
78 using Key = std::pair<int, unsigned long long>;
95 explicit CouplerInterfaceImpl(
const Core &core)
100 MPI_Comm
comm()
const {
102 return m_field.get_comm();
105 moab::Interface &
mb()
const {
107 return m_field.get_moab();
111 UnknownInterface **iface)
const override {
112 *iface =
const_cast<CouplerInterfaceImpl *
>(
this);
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;
126 const int local_error =
work();
127 int global_error = 0;
128 CHKERR MPI_Allreduce(&local_error, &global_error, 1, MPI_INT, MPI_MAX,
131 SETERRQ(
comm(), global_error,
"CouplerInterface: %s failed on a rank",
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 {
142 if (local.size() > INT_MAX)
144 "Coupler message exceeds MPI int count");
151 int count =
static_cast<int>(local.size());
152 CHKERR MPI_Allgather(&count, 1, MPI_INT, counts.data(), 1, MPI_INT,
comm());
154 for (
int r = 0;
r !=
size; ++
r) {
155 offsets[
r] =
static_cast<int>(total);
159 "Global coupling batch exceeds MPI int count");
161 global.resize(total);
162 CHKERR MPI_Allgatherv(local.data(), count, datatype, global.data(),
168 const char *description =
"options/tag maps")
const {
170 std::vector<char> local(input.begin(), input.end()), all;
171 std::vector<int> 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]))
183 if (!
t ||
t->closed ||
t->core != &cOre)
185 "Null, closed or cross-Core coupling transfer");
191 pc =
s.parallelCommId < 0
193 : ParallelComm::get_pcomm(&
mb(),
s.parallelCommId);
194 if (
s.parallelCommId >= 0 && !pc)
196 "Invalid coupling ParallelComm id");
199 CHKERR MPI_Comm_compare(
comm(), pc->comm(), &comparison);
200 if (comparison != MPI_IDENT && comparison != MPI_CONGRUENT)
202 "Coupling ParallelComm must match Core communicator");
208 Locations::Key &
k)
const {
212 EntityHandle
handle = entity;
215 k = {rank,
static_cast<unsigned long long>(
handle)};
226 for (
auto e :
s.elements) {
236 std::vector<EntityHandle> &
vertices)
const {
238 const EntityHandle *conn;
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)))
244 "Coupler accepts only four-node tets and eight-node hexes");
250 std::array<double, 3> &x)
const {
253 CHKERR mb().get_coords(&entity, 1, x.data());
257 std::vector<double> coords(3 *
vertices.size());
261 for (
int d = 0;
d != 3; ++
d)
265 if (!
std::isfinite(
v))
267 "Nonfinite coupling coordinates");
273 Locations &locations)
const;
276 EntityHandle &element,
277 std::array<double, 3> &natural,
278 std::vector<EntityHandle> &leaves,
279 Range &candidates)
const;
284 CHKERR t.router->gs_transfer(1, messages, 0);
294 name =
"__MOFEM_COUPLER_" + std::to_string(++scratchSequence);
297 mb().tag_get_handle(
name.c_str(), existing) == MB_SUCCESS;
298 CHKERR MPI_Allreduce(&local_occupied, &occupied, 1, MPI_INT, MPI_MAX,
301 CHKERR phase(
"scratch tag", [&]() {
305 MB_TAG_SPARSE | MB_TAG_CREAT | MB_TAG_EXCL,
314 std::vector<double> &
values);
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();
334 std::copy(integers.begin(), integers.end(),
335 list.vi_wr +
i * integers.size());
337 std::copy(handles.begin(), handles.end(), list.vul_wr +
i * handles.size());
339 std::copy(reals.begin(), reals.end(), list.vr_wr +
i * reals.size());
345struct CouplerScratchTag {
349 ~CouplerScratchTag() {
357 const Options &options,
358 TransferPtr &transfer) {
360#ifndef WITH_MBCOUPLER
362 "CouplerInterface requires MOAB +mpi+coupler and WITH_MBCOUPLER");
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>();
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", [&]() {
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)
396 "Invalid coupling options/tolerances");
397 if (options.target == Target::ELEMENT_CENTROIDS &&
398 options.missing == Missing::ADJACENT_ELEMENT_AVERAGE)
400 "Adjacent-element fallback requires vertex targets");
401 if (options.sourceLayout == SourceLayout::DISTRIBUTED) {
402 CHKERR parallel(source, pc);
403 CHKERR owned(source, t->ownedElements);
405 t->ownedElements = source.elements;
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) {
415 for (
auto e :
t->ownedElements) {
417 CHKERR mb().tag_get_data(gid, &e, 1, &
id);
418 if (
id <= 0 ||
id == INT_MAX)
420 "Source GLOBAL_ID must be positive and below INT_MAX");
424 std::vector<moab::CartVect> coords(
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]))
431 "Nonfinite source coordinates");
432 lower[
d] = std::min(lower[
d], x[
d]);
433 upper[
d] = std::max(upper[
d], x[
d]);
435 std::unique_ptr<moab::Element::Map> map;
437 map = std::make_unique<moab::Element::LinearTet>(coords);
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.)
443 Transfer::SourceElement cached;
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));
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)
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];
467 std::vector<moab::CartVect> coords(
vertices.size());
469 coords.front().array());
470 replica.push_back(cached.id);
471 replica.push_back(coords.size());
474 corner_indices.emplace(
vertices[
v], corner_indices.size())
476 for (
int d = 0;
d != 3; ++
d)
477 replica.push_back(coords[
v][
d]);
482 t->router = std::make_unique<moab::gs_data::crystal_data>(
comm());
486 const int active = !
t->ownedElements.empty();
490 if (options.sourceLayout == SourceLayout::DISTRIBUTED) {
492 moab::TupleList identities(2, 0, 0, 0, ids.size());
494 append(identities, {
id %
size,
id});
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)
502 "Duplicate owned source GLOBAL_ID across partitions");
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)
510 "Distributed source ParallelComm must exist on every rank or use "
514 boost::uuids::detail::sha1 hash;
515 for (
double value : replica) {
518 hash.process_bytes(&value,
sizeof(value));
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");
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,
534 std::array<double, 3> global_lower, global_upper;
535 CHKERR MPI_Allreduce(lower.data(), global_lower.data(), 3, MPI_DOUBLE,
537 CHKERR MPI_Allreduce(upper.data(), global_upper.data(), 3, MPI_DOUBLE,
540 for (
int d = 0;
d != 3; ++
d)
541 scale = std::hypot(
scale, global_upper[
d] - global_lower[
d]);
545 std::max(options.absoluteTolerance, options.relativeTolerance *
scale);
546 if (!std::isfinite(
t->distance))
548 "Nonfinite resolved coupling tolerance");
549 CHKERR phase(
"source tree", [&]() {
551 if (!
t->ownedElements.empty()) {
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();
562 moab::TupleList empty;
563 double dummy[3] = {};
564 CHKERR t->backend->locate_points(dummy, 0, 0.,
t->distance, &
empty,
577 Transfer &
t,
const double *xyz, EntityHandle &element,
578 std::array<double, 3> &
coordinates, std::vector<EntityHandle> &leaves,
579 Range &candidates)
const {
582 int best_id = INT_MAX;
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;
597 auto &map = *cached.map;
598 const moab::CartVect x(xyz);
599 double physical =
t.distance;
600 if (!map.inside_box(x, physical))
602 moab::CartVect natural(0.);
605 const double coordinate_scale = cached.coordinateScale;
606 const double solve_tol = std::max(
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()) &&
617 const auto jacobian = map.jacobian(natural);
618 const double determinant =
jacobian.determinant();
619 if (!std::isfinite(determinant) || determinant == 0.)
625 const auto inverse = map.ijacobian(natural);
626 double inverse_norm = 0;
627 for (
int i = 0;
i != 3; ++
i) {
629 for (
int j = 0;
j != 3; ++
j)
631 inverse_norm = std::max(inverse_norm,
row);
633 double inside_tol =
t.distance * inverse_norm +
634 64 * std::numeric_limits<double>::epsilon();
635 if (!map.inside_nat_space(natural, inside_tol))
637 const auto back = map.evaluate(natural);
638 if ((
back - x).
length() > std::max(
t.distance, solve_tol) * 4)
642 for (
int d = 0;
d != 3; ++
d)
650 const std::vector<double> &
points,
651 Locations &locations)
const {
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", [&]() {
664 "Coupling batch exceeds int indices");
666 auto mapped = t.backend->mapped_pts();
669 for (
size_t p = 0; p !=
n; ++p) {
670 const auto x =
points.data() + 3 * p;
673 const bool replicated =
674 t.options.sourceLayout == SourceLayout::REPLICATED;
675 for (
int r = replicated ? rank : 0,
end = replicated ? rank + 1 :
size;
677 const auto box =
t.boxes.data() + 6 *
r;
679 for (
int d = 0;
d != 3; ++
d)
681 x[
d] >= box[
d] -
t.distance && x[
d] <= box[
d + 3] +
t.distance;
683 append(requests, {
r,
static_cast<int>(p)}, {}, {x[0], x[1], x[2]});
688 if (
t.options.sourceLayout == SourceLayout::DISTRIBUTED)
690 moab::TupleList matches(4, 0, 0, 0, requests.get_n());
691 CHKERR phase(
"source location", [&]() {
693 std::vector<EntityHandle> leaves;
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,
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)});
711 if (
t.options.sourceLayout == SourceLayout::DISTRIBUTED)
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];
728MoFEMErrorCode CouplerInterfaceImpl::locateTargets(
const TransferPtr &
t,
730 LocationsPtr &locations,
733 auto l = boost::make_shared<Locations>();
734 ParallelComm *pc =
nullptr;
735 Range target_entities;
738 CHKERR phase(
"target validation", [&]() {
742 for (
auto e :
target.elements) {
745 if (
t->options.target == Target::VERTICES)
746 target_entities.insert(
vertices.data(),
749 target_entities.insert(e);
756 moab::TupleList requests(4, 0, 2, 3, target_entities.size());
757 CHKERR phase(
"target requests", [&]() {
759 for (
auto e : target_entities) {
761 std::array<double, 3> x;
764 append(requests, {
k.first, 0,
k.first, -1}, {
k.second, 0},
767 if (
t->options.missing == Missing::ADJACENT_ELEMENT_AVERAGE) {
770 for (
auto cell :
cells) {
771 std::array<double, 3> x;
778 append(requests, {
k.first, 1,
k.first, rank}, {
k.second, cell},
788 std::set<int> subscribers;
789 std::map<Locations::Key, std::array<double, 3>> centroids;
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];
797 if (!requests.vi_rd[4 *
i + 1]) {
798 std::array<double, 3> x;
799 CHKERR coordinates(e, x);
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]);
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());
812 entry.subscribers.insert(rank);
818 moab::TupleList replies(4, 0, 2, 3, requests.get_n());
819 CHKERR phase(
"target subscriptions", [&]() {
822 std::array<double, 3> x;
824 for (
int subscriber : item.second.subscribers) {
825 append(replies, {subscriber, 0, rank, -1}, {item.first, 0},
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]});
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])
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)
845 replies.vr_rd + 3 *
i + 3);
847 target_entities.insert(
k.second);
849 l->fallback.resize(indices.size());
850 for (
unsigned i = 0;
i != replies.get_n(); ++
i) {
851 if (!replies.vi_rd[4 *
i + 1])
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);
859 replies.vr_rd + 3 *
i + 3);
860 l->fallback[indices.at(
k)].push_back(inserted.first->second);
862 CHKERR phase(
"target ownership", [&]() {
864 for (
auto e : target_entities) {
867 auto it = indices.find(
k);
868 if (
it == indices.end())
870 "Missing target owner subscription");
871 l->localIndices[e] =
it->second;
877 l->generation = ++
t->generation;
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)
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; }))
889 if (item.first.first != rank)
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);
897 l->report.missingEntities.insert(e);
898 ++local_counts[
l->status[p]];
900 unsigned long long global_counts[3];
901 CHKERR MPI_Allreduce(local_counts, global_counts, 3, MPI_UNSIGNED_LONG_LONG,
903 l->report.globalMissing = global_counts[0];
904 l->report.globalLocated = global_counts[1];
905 l->report.globalFallback = global_counts[2];
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.);
929 const auto read_value = [&](EntityHandle entity, std::vector<double> &value) {
931 std::vector<double>
source(source_components);
933 if (mapping.component < 0)
934 value = std::move(
source);
936 value.assign(1,
source[mapping.component]);
937 for (
double x : value)
938 if (!
std::isfinite(x))
940 "Nonfinite source tag value");
943 CHKERR phase(
"source communicator", [&]() {
945 if (
t.options.sourceLayout == SourceLayout::DISTRIBUTED)
947 if (recovery && !pc &&
948 t.options.sourceLayout == SourceLayout::DISTRIBUTED &&
951 "Distributed recovery with multiple source ranks requires "
952 "ParallelComm sharing metadata");
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) {
969 CHKERR read_value(e, data[e]);
973 CouplerScratchTag nodal_copy{
mb()};
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", [&]() {
983 append(requests, {
k.first}, {
k.second});
988 CHKERR phase(
"owner source values", [&]() {
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;
999 CHKERR mb().tag_set_data(nodal_copy.tag, &
v, 1, value.data());
1000 exchange_vertices.insert(
v);
1004 CHKERR phase(
"source vertex exchange", [&]() {
1006 CHKERR pc->exchange_tags(nodal_copy.tag, exchange_vertices);
1009 CHKERR phase(
"synchronised source data", [&]() {
1013 CHKERR mb().tag_get_data(nodal_copy.tag, &
v, 1, data[
v].data());
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;
1027 auto &sum = sums[
v];
1030 for (
int c = 0;
c !=
n; ++
c)
1031 sum[
c] += data.at(e)[
c];
1035 for (
auto &
v : sums)
1036 CHKERR mb().tag_set_data(scratch.
tag, &
v.first, 1,
v.second.data());
1040 if (recovery &&
t.options.sourceLayout == SourceLayout::DISTRIBUTED) {
1041 CHKERR phase(
"source communicator",
1042 [&]() {
return parallel(
t.source, pc); });
1044 CHKERR phase(
"source recovery reduction", [&]() -> MoFEMErrorCode {
1046 CHKERR pc->reduce_tags(scratch.tag, MPI_SUM,
t.vertices);
1050 CHKERR phase(
"recovered source data", [&]() {
1055 std::vector<double> sum(
n + 1);
1056 CHKERR mb().tag_get_data(scratch.tag, &
v, 1, sum.data());
1058 auto &value = data[
v];
1060 for (
int c = 0;
c !=
n; ++
c)
1061 value[
c] = sum[
c] / sum[
n];
1065 if (mapping.sampling == Sampling::VERTEX_AVERAGED_CONSTANT) {
1066 for (
auto e :
t.ownedElements) {
1067 std::vector<EntityHandle>
vertices;
1069 auto &value = data[e];
1070 value.assign(
n, 0.);
1076 for (
const auto &entry : data)
1077 for (
double x : entry.second)
1078 if (!
std::isfinite(x))
1080 "Nonfinite recovered source value");
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.);
1089 if (
t.options.sourceLayout == SourceLayout::DISTRIBUTED)
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());
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;
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]);
1117 CHKERR t.backend->interpolate(method, scalar.tag, component.data(),
1119 queries.enableWriteAccess();
1120 for (
unsigned i = 0;
i != queries.get_n(); ++
i)
1121 queries.vr_wr[
i *
n +
c] = component[
i];
1126 if (
t.options.sourceLayout == SourceLayout::DISTRIBUTED)
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);
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) {
1137 for (
auto q :
l.fallback[p])
1141 CHKERR phase(
"sample finiteness", [&]() {
1143 for (
double value :
values)
1144 if (!
std::isfinite(value))
1146 "Nonfinite sampled/fallback value");
1155CouplerInterfaceImpl::transferTags(
const TransferPtr &
t,
const LocationsPtr &
l,
1156 const std::vector<TagMap> &tags,
1159 CHKERR phase(
"location lifetime", [&]() {
1162 if (!
l ||
l->transfer.lock() !=
t ||
l->generation !=
t->generation)
1164 "Stale or foreign coupling locations");
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());
1174 if (
report.globalMissing &&
t->options.missing != Missing::KEEP)
1180 bool existed =
false,
created =
false;
1183 std::vector<double>
values;
1184 std::map<EntityHandle, std::vector<double>> oldValues;
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];
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)
1201 "Empty tag name, duplicate destination or invalid sampling");
1203 if (!
t->source.elements.empty()) {
1204 CHKERR mb().tag_get_handle(mapping.sourceTag.c_str(), source_tag);
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)
1210 PETSC_COMM_SELF, MOFEM_INVALID_DATA,
1211 "Coupling source requires a fixed positive double tag length");
1215 int source_components;
1216 CHKERR MPI_Allreduce(&local_components, &source_components, 1, MPI_INT,
1218 if (mapping.component >= source_components)
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)
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)
1230 s.existed = error == MB_SUCCESS;
1231 TagType storage = MB_TAG_SPARSE;
1232 std::vector<double> defaults(
s.components);
1233 bool has_default =
false;
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))
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)
1247 has_default = error == MB_SUCCESS &&
1248 std::all_of(defaults.begin(), defaults.end(),
1249 [](
double x) { return std::isfinite(x); });
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]) {
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)
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); }))
1272 "KEEP destination is nonfinite");
1275 s.entities.insert(e);
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);
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);
1300#ifdef WITH_MBCOUPLER
1301 std::vector<double>
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);
1310 const auto publish_error = phase(
"tag publication", [&]() {
1312 for (
size_t i = 0;
i != tags.size(); ++
i) {
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);
1320 if (!
s.entities.empty())
1321 CHKERR mb().tag_set_data(
s.tag,
s.entities,
s.values.data());
1325 if (publish_error) {
1326 CHKERR phase(
"publication rollback", [&]() {
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);
1348 CHKERR phase(
"transfer close", [&]() {
1350 if (
t &&
t->core != &
cOre)
1355#ifdef WITH_MBCOUPLER
1357 t->backendComm.reset();
1359 CHKERR mb().delete_entities(&
t -> part, 1);
1363 t->elements.clear();
1364 t->elementIndices.clear();
1378 return new CouplerInterfaceImpl(core);
std::map< EntityHandle, int > index
std::vector< int > created
std::vector< int > source
const std::vector< Vec > * points
std::vector< int > vertices
Vec t
Removal: the ring edge.
bool back
Slid back beyond the node along its edge.
std::vector< Cell > cells
unsigned long long scratchSequence
std::map< EntityHandle, size_t > offsets
int components
Inferred from the source tag length.
std::vector< double > empty
One value per component.
std::vector< std::array< double, 3 > > coordinates
std::map< EntityHandle, std::vector< double > > staged
std::vector< FieldBitNumber > row
SmartPetscObj< Vec > work
SmartPetscObj< Mat > jacobian
SmartPetscObj< Vec > values
SmartPetscObj< Mat > inverse
#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
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
#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
auto type_from_handle(const EntityHandle h)
get type from entity handle
DeprecatedCoreInterface Interface
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< int > mappedIndices
std::vector< std::vector< size_t > > fallback
std::vector< int > status
std::vector< int > winners
unsigned long long generation
boost::weak_ptr< Transfer > transfer
Source selection, search backend and routing state shared by location batches.
unsigned long long generation
~Transfer()
Release local backend resources; collective closure uses destroyTransfer.