20 CHKERR PetscPushErrorHandler(PetscReturnErrorHandler,
nullptr);
21 const auto error = operation();
22 CHKERR PetscPopErrorHandler();
23 if (error != expected)
25 "Expected coupling error %d, got %d", expected, error);
30 const std::vector<std::array<double, 3>> &
coordinates,
31 int id, EntityHandle &e) {
42 CHKERR mb.tag_get_handle(GLOBAL_ID_TAG_NAME, 1, MB_TYPE_INTEGER, gid,
43 MB_TAG_DENSE | MB_TAG_CREAT);
44 CHKERR mb.tag_set_data(gid, &e, 1, &
id);
48std::vector<std::array<double, 3>> box(
bool hex,
double offset,
double scale) {
49 std::vector<std::array<double, 3>> x =
50 hex ? std::vector<std::array<double, 3>>{{0, 0, 0}, {1, 0, 0}, {1, 1, 0},
51 {0, 1, 0}, {0, 0, 1}, {1, 0, 1},
53 : std::vector<std::array<double, 3>>{
54 {0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
65 result.parallelCommId = -1;
70double affine(
const double *x) {
71 return 1. + 2. * x[0] - 3. * x[1] + 4. * x[2];
78 MB_TAG_SPARSE | MB_TAG_CREAT);
79 for (
auto e : entities) {
83 for (
int c = 0;
c !=
n; ++
c)
84 values[
c] = (
c + 1) * (linear ? affine(x) : 7.);
96 auto source_pc = std::make_unique<ParallelComm>(&
mb, m_field.
get_comm());
98 for (
bool hex : {
false,
true})
101 for (
auto target_type :
103 EntityHandle source_element, target_element;
104 const double source_offset =
106 const double target_offset =
109 : 2. * ((rank + 1) %
size);
110 CHKERR element(
mb, box(hex, source_offset, 1.), rank + 1,
115 CHKERR mb.tag_get_handle(GLOBAL_ID_TAG_NAME, gid);
117 CHKERR mb.tag_set_data(gid, &source_element, 1, &
id);
119 auto target_points = box(
false, target_offset + .1, .1);
120 for (
auto &x : target_points) {
124 CHKERR element(
mb, target_points, 100, target_element);
126 target = selection(target_element);
128 source.parallelCommId = source_pc->get_id();
131 options.
target = target_type;
133 CHKERR coupling->createTransfer(
source, options, transfer);
137 const auto expected_count =
static_cast<unsigned long long>(
139 CHKERR require(
report.globalLocated == expected_count &&
141 "Unexpected owned target count");
144 for (
int n : {1, 3, 5, 6, 9}) {
145 const std::string prefix =
"SAMPLING_" + std::to_string(++test);
150 const auto source_corners = box(hex, source_offset, 1.);
152 for (
auto x : source_corners)
153 average += affine(x.data()) / source_corners.size();
155 average += 2. * (target_offset - source_offset);
156 std::vector<Coupling::TagMap> maps{
157 {prefix +
"_E", prefix +
"_C",
160 {prefix +
"_E", prefix +
"_R",
162 {prefix +
"_V", prefix +
"_A",
164 CHKERR coupling->transferTags(transfer, locations, maps,
report);
167 for (
auto e : targets) {
170 for (
size_t mode = 0; mode != maps.size(); ++mode) {
172 CHKERR mb.tag_get_handle(maps[mode].targetTag.c_str(),
tag);
175 const double expected =
176 mode == 1 ? affine(x) : (mode == 3 ? average : 7.);
177 for (
int c = 0;
c !=
n; ++
c)
179 std::max(error, std::abs(
values[
c] - (
c + 1) * expected));
183 "coupler manufactured sampling", error, 1., {2.e-11, 0.});
187 map.targetTag +=
"_COMPONENT";
188 map.component =
n - 1;
191 double component_error = 0.;
192 for (
auto e :
report.locatedEntities) {
195 for (
size_t mode = 0; mode !=
selected.size(); ++mode) {
202 "Selected component did not produce a scalar tag");
204 const double expected =
205 mode == 1 ? affine(x) : (mode == 3 ? average : 7.);
206 component_error = std::isfinite(value)
207 ? std::max(component_error,
208 std::abs(value -
n * expected))
209 : std::numeric_limits<double>::infinity();
213 "coupler selected-component sampling", component_error, 1.,
218 double changed = 13.;
219 CHKERR mb.tag_get_handle(
"REFRESH", 1, MB_TYPE_DOUBLE, source_tag,
220 MB_TAG_SPARSE | MB_TAG_CREAT);
221 CHKERR mb.tag_set_data(source_tag, &source_element, 1, &changed);
222 CHKERR coupling->transferTags(transfer, locations,
223 {{
"REFRESH",
"REFRESH_RESULT",
226 for (
auto e :
report.locatedEntities) {
231 CHKERR require(value == changed,
232 "Location reuse cached stale source values");
238 return coupling->transferTags(transfer, locations, {},
report);
241 auto alias = transfer;
242 CHKERR coupling->destroyTransfer(transfer);
243 CHKERR coupling->destroyTransfer(transfer);
246 return coupling->transferTags(alias, refreshed, {},
report);
258 EntityHandle source_element = 0, target_element = 0;
263 CHKERR element(
mb, box(
true, 0., 1.), 1, source_element);
264 source.elements.insert(source_element);
266 if (rank ==
size - 1) {
267 auto points = box(
false, -.1, .2);
278 target.elements.insert(target_element);
286 CHKERR coupling->createTransfer(
source, options, transfer);
292 "Wrong fallback classification");
293 const double sentinel = -123.;
295 CHKERR mb.tag_get_handle(
"POLICY_TARGET", 1, MB_TYPE_DOUBLE, output,
296 MB_TAG_SPARSE | MB_TAG_CREAT);
298 CHKERR mb.tag_set_data(output, &target_element, 1, &sentinel);
299 CHKERR coupling->transferTags(transfer, locations,
300 {{
"POLICY_SOURCE",
"POLICY_TARGET",
303 if (target_element) {
305 CHKERR mb.tag_get_data(output, &target_element, 1, &value);
306 CHKERR require(value == sentinel,
307 "Fallback modified the target element tag");
311 CHKERR mb.tag_get_data(output, &e, 1, &value);
312 CHKERR require(value == 7.,
"Incorrect fallback value");
316 CHKERR mb.tag_get_handle(
"WRONG_SCHEMA", 2, MB_TYPE_DOUBLE, wrong_schema,
317 MB_TAG_SPARSE | MB_TAG_CREAT);
320 return coupling->transferTags(transfer, locations,
321 {{
"POLICY_SOURCE",
"WRONG_SCHEMA",
329 CHKERR mb.tag_get_handle(
"UNTOUCHED", 1, MB_TYPE_DOUBLE, untouched,
330 MB_TAG_SPARSE | MB_TAG_CREAT, &sentinel);
333 return coupling->transferTags(transfer, locations,
334 {{
"POLICY_SOURCE",
"UNTOUCHED",
336 {
"DOES_NOT_EXIST",
"NEVER_CREATED",
341 for (
auto e :
report.locatedEntities) {
343 CHKERR mb.tag_get_data(untouched, &e, 1, &value);
344 CHKERR require(value == sentinel,
"Failed batch partially published");
346 CHKERR require(
mb.tag_get_handle(
"NEVER_CREATED",
tag) == MB_TAG_NOT_FOUND,
347 "Failed batch left a destination definition");
348 CHKERR coupling->destroyTransfer(transfer);
351 CHKERR coupling->createTransfer(
source, options, transfer);
355 return coupling->transferTags(transfer, locations,
356 {{
"POLICY_SOURCE",
"STRICT_TARGET",
361 CHKERR require(
mb.tag_get_handle(
"STRICT_TARGET",
tag) == MB_TAG_NOT_FOUND,
362 "Strict missing policy created a tag");
363 CHKERR coupling->destroyTransfer(transfer);
366 CHKERR coupling->createTransfer(
source, options, transfer);
370 return coupling->transferTags(transfer, locations,
371 {{
"POLICY_SOURCE",
"KEEP_ABSENT",
376 CHKERR coupling->transferTags(
380 for (
auto e :
report.missingEntities) {
382 CHKERR mb.tag_get_data(untouched, &e, 1, &value);
383 CHKERR require(value == sentinel,
"KEEP overwrote existing data");
385 CHKERR coupling->destroyTransfer(transfer);
389 [&]() {
return coupling->createTransfer(
source, options, transfer); },
394 [&]() {
return coupling->createTransfer(
empty, options, transfer); },
405 CHKERR element(
mb, box(
false, 0., 1.), rank + 1, e);
407 CHKERR mb.tag_get_handle(
"BOUNDARY_SOURCE", 1, MB_TYPE_DOUBLE,
tag,
408 MB_TAG_SPARSE | MB_TAG_CREAT);
409 double value = rank + 1.;
415 CHKERR coupling->createTransfer(selection(e), options, transfer);
419 CHKERR coupling->locateTargets(transfer, selection(e), locations,
report);
420 CHKERR coupling->transferTags(transfer, locations,
421 {{
"BOUNDARY_SOURCE",
"BOUNDARY_TARGET",
426 CHKERR require(value == 1.,
427 "Boundary ownership is not deterministic by GLOBAL_ID");
428 CHKERR coupling->destroyTransfer(transfer);
435 std::unique_ptr<Coupling> coupling(
436 createInterface<Coupling>(
static_cast<MoFEM::Core &
>(m_field)));
439 CHKERR element(
mb, box(
false, 0., 1.), 10, first);
440 const EntityHandle *conn;
442 CHKERR mb.get_connectivity(first, conn, count,
false);
443 const std::vector<EntityHandle> shared(conn, conn + count);
444 const double below[3] = {0, 0, -1};
445 EntityHandle bottom, second;
446 CHKERR mb.create_vertex(below, bottom);
447 const EntityHandle next[] = {shared[0], shared[2], shared[1], bottom};
448 CHKERR mb.create_element(MBTET, next, 4, second);
450 CHKERR mb.tag_get_handle(GLOBAL_ID_TAG_NAME, gid);
451 const int second_id = 20;
452 CHKERR mb.tag_set_data(gid, &second, 1, &second_id);
453 auto source = selection(first);
454 source.elements.insert(second);
455 auto target_points = box(
false, .1, .1);
456 for (
auto &x : target_points) {
460 EntityHandle target_element;
461 CHKERR element(
mb, target_points, 100, target_element);
462 auto target = selection(target_element);
464 CHKERR mb.tag_get_handle(
"RECOVERY_ORACLE", 1, MB_TYPE_DOUBLE,
tag,
465 MB_TAG_SPARSE | MB_TAG_CREAT);
473 CHKERR mb.tag_get_handle(
"__MOFEM_COUPLER_1", 1, MB_TYPE_INTEGER, user_tag,
474 MB_TAG_SPARSE | MB_TAG_CREAT);
480 CHKERR mb.get_entities_by_handle(0, before);
484 CHKERR coupling->locateTargets(transfer, no_targets, locations,
report);
487 "Empty target produced nonempty counts");
488 CHKERR coupling->transferTags(transfer, locations,
489 {{
"RECOVERY_ORACLE",
"EMPTY_TARGET",
493 for (
int component : {-2, 1}) {
496 return coupling->transferTags(
498 {{
"RECOVERY_ORACLE",
"INVALID_COMPONENT",
504 CHKERR require(
mb.tag_get_handle(
"INVALID_COMPONENT", unexpected) ==
506 "Invalid component created a destination tag");
508 CHKERR coupling->transferTags(
510 {{
"RECOVERY_ORACLE",
"RECOVERY_RESULT",
516 for (
auto e :
report.locatedEntities) {
521 error = std::max(error, std::abs(value - (4. - 2. * x[2])));
524 "selected-element recovery oracle", error, 1., {1.e-12, 0.});
525 CHKERR coupling->destroyTransfer(transfer);
529 CHKERR mb.tag_get_handle(
"__MOFEM_COUPLER_1", collision);
532 "Recovery modified a user's scratch-name collision");
535 CHKERR mb.get_entities_by_handle(0, after);
538 "Closing a transfer leaked entities or deleted caller geometry");
542 EntityHandle smaller, excluded;
543 CHKERR mb.create_element(MBTET, shared.data(), 4, smaller);
544 CHKERR mb.create_element(MBTET, shared.data(), 4, excluded);
546 CHKERR mb.tag_set_data(gid, &smaller, 1, &
id);
548 CHKERR mb.tag_set_data(gid, &excluded, 1, &
id);
553 source.elements.erase(second);
554 source.elements.insert(smaller);
557 CHKERR coupling->transferTags(
562 for (
auto e :
report.locatedEntities) {
564 CHKERR require(value == 5.,
565 "Local GLOBAL_ID tie or source selection is incorrect");
568 value = std::numeric_limits<double>::quiet_NaN();
572 return coupling->transferTags(transfer, locations,
573 {{
"RECOVERY_ORACLE",
"NONFINITE_RESULT",
580 "Nonfinite input created a destination");
581 CHKERR coupling->destroyTransfer(transfer);
585 [&]() {
return coupling->createTransfer(
source, invalid, transfer); },
593 return coupling->createTransfer(
source, inconsistent, transfer);
598 incomplete.elements.clear();
600 [&]() {
return coupling->createTransfer(incomplete, {}, transfer); },
607 auto connected = selection(first);
608 connected.elements.insert(second);
612 EntityHandle duplicate;
614 auto disconnected = std::vector<EntityHandle>(next, next + 4);
615 disconnected[0] = duplicate;
616 CHKERR mb.set_connectivity(second, disconnected.data(), 4);
619 [&]() {
return coupling->createTransfer(connected, {}, transfer); },
621 auto restored = std::vector<EntityHandle>(next, next + 4);
622 CHKERR mb.set_connectivity(second, restored.data(), 4);
626 for (
bool hex : {
false,
true})
627 for (
double scale : {1.e-9, 1.e9}) {
628 EntityHandle scaled_source, scaled_target;
636 CHKERR coupling->createTransfer(selection(scaled_source), {}, transfer);
637 CHKERR coupling->locateTargets(transfer, selection(scaled_target),
640 "Relative tolerance failed on scaled geometry");
641 CHKERR coupling->destroyTransfer(transfer);
653 auto pc = std::make_unique<ParallelComm>(&
mb, m_field.
get_comm());
655 CHKERR pc->create_part(part);
656 auto x = box(
false, 0., 1.);
659 std::swap(x[1], x[2]);
661 EntityHandle target_element;
662 CHKERR element(
mb, x, rank + 1, target_element);
663 const EntityHandle *conn;
665 CHKERR mb.get_connectivity(target_element, conn,
n,
false);
666 std::vector<EntityHandle>
vertices(conn, conn +
n);
668 CHKERR mb.tag_get_handle(GLOBAL_ID_TAG_NAME, gid);
669 const int ids0[] = {1, 2, 3, 4}, ids1[] = {1, 3, 2, 5};
672 cells.insert(target_element);
676 auto source_points = box(
true, 0., 2.);
677 for (
auto &
v : source_points)
679 EntityHandle source_element;
680 CHKERR element(
mb, source_points, 1, source_element);
681 const auto source = selection(source_element);
683 target.parallelCommId = pc->get_id();
687 CHKERR tagData(
mb,
"SUBSET_SOURCE", 1,
source.elements,
false, source_tag);
695 "Subset omitted vertices whose owner has no selected cells");
696 CHKERR coupling->transferTags(transfer, locations,
697 {{
"SUBSET_SOURCE",
"SUBSET_TARGET",
706 CHKERR require(value == 7.,
707 "Selected shared vertex was not synchronised");
712 "Subset publication touched an unselected vertex");
713 CHKERR coupling->destroyTransfer(transfer);
716 auto narrow = box(
true, 0., 1.);
717 for (
auto &
v : narrow) {
718 v[0] = .1 + .5 *
v[0];
719 v[1] = .1 + .5 *
v[1];
722 EntityHandle fallback_source;
723 CHKERR element(
mb, narrow, 1, fallback_source);
725 const auto fallback_selection = selection(fallback_source);
726 CHKERR mb.get_connectivity(fallback_selection.elements, corners,
true);
727 CHKERR tagData(
mb,
"SHARED_FALLBACK_SOURCE", 1, corners,
true, source_tag);
731 CHKERR coupling->createTransfer(fallback_selection, options, transfer);
735 "Shared fallback did not cover selected target vertices");
736 CHKERR coupling->transferTags(
738 {{
"SHARED_FALLBACK_SOURCE",
"SHARED_FALLBACK_RESULT",
747 "Shared fallback omitted remote selected-cell adjacency");
749 CHKERR coupling->destroyTransfer(transfer);
765 const int rank =
field.get_comm_rank();
766 auto pc = std::make_unique<ParallelComm>(&
mb,
field.get_comm());
767 EntityHandle part, cell;
768 CHKERR pc->create_part(part);
769 auto x = box(
false, 0., 1.);
772 std::swap(x[1], x[2]);
774 CHKERR element(
mb, x, rank + 1, cell);
775 const EntityHandle *conn;
777 CHKERR mb.get_connectivity(cell, conn, count,
false);
778 std::vector<EntityHandle>
vertices(conn, conn + count);
780 CHKERR mb.tag_get_handle(GLOBAL_ID_TAG_NAME, gid);
781 const int ids0[] = {1, 2, 3, 4}, ids1[] = {1, 3, 2, 5};
788 Tag cell_tag, nodal_tag;
789 CHKERR mb.tag_get_handle(
"SEAM_CELL", 1, MB_TYPE_DOUBLE, cell_tag,
790 MB_TAG_SPARSE | MB_TAG_CREAT);
791 CHKERR mb.tag_get_handle(
"SEAM_NODAL", 1, MB_TYPE_DOUBLE, nodal_tag,
792 MB_TAG_SPARSE | MB_TAG_CREAT);
793 double cell_value = rank ? 6. : 2.;
794 CHKERR mb.tag_set_data(cell_tag, &cell, 1, &cell_value);
800 const double value =
owner == rank ? affine(
position) : -999.;
801 CHKERR mb.tag_set_data(nodal_tag, &
v, 1, &value);
803 auto points = box(
false, .1, .1);
806 v[2] = rank ? .1 +
v[2] : -.1 -
v[2];
808 EntityHandle target_cell;
810 auto source = selection(cell),
target = selection(target_cell);
811 source.parallelCommId = pc->get_id();
818 CHKERR coupling->createTransfer(
source, options, transfer);
820 const std::vector<Coupling::TagMap> maps{
821 {
"SEAM_CELL",
"SEAM_RECOVERED",
824 for (
int update = 0; update != 2; ++update) {
825 cell_value = rank ? 6. + 4. * update : 2.;
826 CHKERR mb.tag_set_data(cell_tag, &cell, 1, &cell_value);
827 CHKERR coupling->transferTags(transfer, locations, maps,
report);
828 for (
auto e :
report.locatedEntities) {
831 for (
size_t mode = 0; mode != maps.size(); ++mode) {
836 const double expected =
838 : 4. + 2. * update - (2. + 2. * update) *
position[2];
840 std::abs(value - expected) < 1.e-12,
841 "Distributed source seam produced partition-dependent values");
849 CHKERR mb.tag_get_data(nodal_tag, &
v, 1, &value);
850 CHKERR require(value == -999.,
851 "Source synchronization modified caller data");
855 CHKERR coupling->destroyTransfer(transfer);
859 subset.elements.clear();
861 v[2] = -std::abs(
v[2]);
862 EntityHandle subset_target;
864 CHKERR coupling->createTransfer(subset, options, transfer);
865 CHKERR coupling->locateTargets(transfer, selection(subset_target), locations,
867 CHKERR coupling->transferTags(transfer, locations, maps,
report);
868 for (
auto e :
report.locatedEntities) {
871 for (
size_t mode = 0; mode != maps.size(); ++mode) {
876 const double expected = mode ? affine(
position) : 10.;
878 std::abs(value - expected) < 1.e-12,
879 "Source owner with no selected cells lost authoritative data");
882 CHKERR coupling->destroyTransfer(transfer);
886 CHKERR mb.tag_get_handle(
"SEAM_MULTI", 3, MB_TYPE_DOUBLE, multi,
887 MB_TAG_SPARSE | MB_TAG_CREAT);
893 std::array<double, 3>
row;
894 for (
int c = 0;
c != 3; ++
c)
898 CHKERR coupling->createTransfer(
source, options, transfer);
900 CHKERR coupling->transferTags(
906 double selected_error = 0.;
907 for (
auto e :
report.locatedEntities) {
913 ? std::max(selected_error, std::abs(value - 3. * affine(
position)))
914 : std::numeric_limits<double>::infinity();
918 "selected component from shared source owners", selected_error, 1.,
920 CHKERR coupling->destroyTransfer(transfer);
921 source.parallelCommId = -1;
922 CHKERR coupling->createTransfer(
source, options, transfer);
926 return coupling->transferTags(transfer, locations, {maps.front()},
930 CHKERR coupling->destroyTransfer(transfer);
936#ifndef WITH_MBCOUPLER
939 CHKERR rejects([&]() {
return coupling->createTransfer({}, {}, transfer); },
945 CHKERR contracts(m_field);
946 CHKERR selectedSharedVertex(m_field);
947 CHKERR distributedSharing(m_field);
953int main(
int argc,
char **argv) {
955 "CouplerInterface manufactured tests");
CGALInterfaceImpl & owner
std::vector< int > source
const std::vector< Vec > * points
std::vector< int > vertices
std::vector< Cell > cells
std::vector< double > empty
One value per component.
std::vector< std::array< double, 3 > > coordinates
std::vector< FieldBitNumber > row
SmartPetscObj< Vec > values
#define CATCH_ERRORS
Catch errors.
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
@ MOFEM_ATOM_TEST_INVALID
@ MOFEM_DATA_INCONSISTENCY
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
const double c
speed of light (cm/ns)
const double v
phase velocity of light in medium (cm/ns)
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
implementation of Data Operators for Forces and Sources
MoFEMErrorCode TestingInterface::checkError< TestType::POINTWISE >(const std::string &check_name, PetscReal error, PetscReal scale, Tolerance tolerance, Sev error_severity, Sev success_severity) const
MoFEMErrorCode run(MoFEM::Interface &m_field)
virtual int get_comm_size() const =0
virtual moab::Interface & get_moab()=0
virtual MPI_Comm & get_comm() const =0
virtual int get_comm_rank() const =0
static MoFEMErrorCode Initialize(int *argc, char ***args, const char file[], const char help[])
Initializes the MoFEM database PETSc, MOAB and MPI.
moab::Interface & get_moab()
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
double relativeTolerance
Physical search distance = max(absoluteTolerance, relativeTolerance*L).
SourceLayout sourceLayout
@ ADJACENT_ELEMENT_AVERAGE
boost::shared_ptr< const Locations > LocationsPtr
@ ELEMENT_RECOVERED_LINEAR
@ VERTEX_AVERAGED_CONSTANT
virtual MoFEMErrorCode createTransfer(const Selection &source, const Options &options, TransferPtr &transfer)=0
Build a source-only search. Source elements require unique positive GLOBAL_IDs.
boost::shared_ptr< Transfer > TransferPtr
Deprecated interface functions.
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.