v0.16.3
Loading...
Searching...
No Matches
coupler_interface.cpp
Go to the documentation of this file.
1/** \file coupler_interface.cpp
2 * \brief Manufactured interpolation, ownership and transaction checks.
3 */
4#include <MoFEM.hpp>
5using namespace MoFEM;
7
8namespace {
9
10MoFEMErrorCode require(bool valid, const char *message) {
12 if (!valid)
13 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID, "%s", message);
15}
16
17template <typename F>
18MoFEMErrorCode rejects(F operation, MoFEMErrorCode expected) {
20 CHKERR PetscPushErrorHandler(PetscReturnErrorHandler, nullptr);
21 const auto error = operation();
22 CHKERR PetscPopErrorHandler();
23 if (error != expected)
24 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
25 "Expected coupling error %d, got %d", expected, error);
27}
28
29MoFEMErrorCode element(moab::Interface &mb,
30 const std::vector<std::array<double, 3>> &coordinates,
31 int id, EntityHandle &e) {
33 std::vector<EntityHandle> vertices;
34 for (const auto &x : coordinates) {
35 EntityHandle v;
36 CHKERR mb.create_vertex(x.data(), v);
37 vertices.push_back(v);
38 }
39 CHKERR mb.create_element(vertices.size() == 4 ? MBTET : MBHEX,
40 vertices.data(), vertices.size(), e);
41 Tag gid;
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);
46}
47
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},
52 {1, 1, 1}, {0, 1, 1}}
53 : std::vector<std::array<double, 3>>{
54 {0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
55 for (auto &v : x) {
56 v[0] = offset + scale * v[0];
57 v[1] *= scale;
58 v[2] *= scale;
59 }
60 return x;
61}
62
63Coupling::Selection selection(EntityHandle e) {
65 result.parallelCommId = -1;
66 result.elements.insert(e);
67 return result;
68}
69
70double affine(const double *x) {
71 return 1. + 2. * x[0] - 3. * x[1] + 4. * x[2];
72}
73
74MoFEMErrorCode tagData(moab::Interface &mb, const std::string &name, int n,
75 const Range &entities, bool linear, Tag &tag) {
77 CHKERR mb.tag_get_handle(name.c_str(), n, MB_TYPE_DOUBLE, tag,
78 MB_TAG_SPARSE | MB_TAG_CREAT);
79 for (auto e : entities) {
80 std::vector<double> values(n);
81 double x[3];
82 CHKERR mb.get_coords(&e, 1, x);
83 for (int c = 0; c != n; ++c)
84 values[c] = (c + 1) * (linear ? affine(x) : 7.);
85 CHKERR mb.tag_set_data(tag, &e, 1, values.data());
86 }
88}
89
90MoFEMErrorCode sampling(MoFEM::Interface &m_field) {
92 auto &mb = m_field.get_moab();
93 auto coupling = m_field.getInterface<Coupling>();
94 auto testing = m_field.getInterface<TestingInterface>();
95 const int rank = m_field.get_comm_rank(), size = m_field.get_comm_size();
96 auto source_pc = std::make_unique<ParallelComm>(&mb, m_field.get_comm());
97 int test = 0;
98 for (bool hex : {false, true})
99 for (auto layout : {Coupling::SourceLayout::REPLICATED,
101 for (auto target_type :
103 EntityHandle source_element, target_element;
104 const double source_offset =
105 layout == Coupling::SourceLayout::REPLICATED ? 0 : 2. * rank;
106 const double target_offset =
108 ? 0
109 : 2. * ((rank + 1) % size);
110 CHKERR element(mb, box(hex, source_offset, 1.), rank + 1,
111 source_element);
112 // Replica identities must agree even when local entity handles differ.
114 Tag gid;
115 CHKERR mb.tag_get_handle(GLOBAL_ID_TAG_NAME, gid);
116 int id = 1;
117 CHKERR mb.tag_set_data(gid, &source_element, 1, &id);
118 }
119 auto target_points = box(false, target_offset + .1, .1);
120 for (auto &x : target_points) {
121 x[1] += .1;
122 x[2] += .1;
123 }
124 CHKERR element(mb, target_points, 100, target_element);
125 Coupling::Selection source = selection(source_element),
126 target = selection(target_element);
128 source.parallelCommId = source_pc->get_id();
129 Coupling::Options options;
130 options.sourceLayout = layout;
131 options.target = target_type;
132 Coupling::TransferPtr transfer;
133 CHKERR coupling->createTransfer(source, options, transfer);
134 Coupling::LocationsPtr locations;
136 CHKERR coupling->locateTargets(transfer, target, locations, report);
137 const auto expected_count = static_cast<unsigned long long>(
138 size * (target_type == Coupling::Target::VERTICES ? 4 : 1));
139 CHKERR require(report.globalLocated == expected_count &&
140 !report.globalMissing,
141 "Unexpected owned target count");
143 CHKERR mb.get_connectivity(source.elements, vertices, true);
144 for (int n : {1, 3, 5, 6, 9}) {
145 const std::string prefix = "SAMPLING_" + std::to_string(++test);
146 Tag constant, nodal;
147 CHKERR tagData(mb, prefix + "_E", n, source.elements, false,
148 constant);
149 CHKERR tagData(mb, prefix + "_V", n, vertices, true, nodal);
150 const auto source_corners = box(hex, source_offset, 1.);
151 double average = 0;
152 for (auto x : source_corners)
153 average += affine(x.data()) / source_corners.size();
154 // Targets on another rank sample that rank's source geometry.
155 average += 2. * (target_offset - source_offset);
156 std::vector<Coupling::TagMap> maps{
157 {prefix + "_E", prefix + "_C",
159 {prefix + "_V", prefix + "_L", Coupling::Sampling::VERTEX_LINEAR},
160 {prefix + "_E", prefix + "_R",
162 {prefix + "_V", prefix + "_A",
164 CHKERR coupling->transferTags(transfer, locations, maps, report);
165 double error = 0;
166 Range targets = report.locatedEntities;
167 for (auto e : targets) {
168 double x[3];
169 CHKERR mb.get_coords(&e, 1, x);
170 for (size_t mode = 0; mode != maps.size(); ++mode) {
171 Tag tag;
172 CHKERR mb.tag_get_handle(maps[mode].targetTag.c_str(), tag);
173 std::vector<double> values(n);
174 CHKERR mb.tag_get_data(tag, &e, 1, values.data());
175 const double expected =
176 mode == 1 ? affine(x) : (mode == 3 ? average : 7.);
177 for (int c = 0; c != n; ++c)
178 error =
179 std::max(error, std::abs(values[c] - (c + 1) * expected));
180 }
181 }
182 CHKERR testing->checkError<TestType::POINTWISE>(
183 "coupler manufactured sampling", error, 1., {2.e-11, 0.});
184 // Select the last source component before each sampling operation.
185 auto selected = maps;
186 for (auto &map : selected) {
187 map.targetTag += "_COMPONENT";
188 map.component = n - 1;
189 }
190 CHKERR coupling->transferTags(transfer, locations, selected, report);
191 double component_error = 0.;
192 for (auto e : report.locatedEntities) {
193 double x[3];
194 CHKERR mb.get_coords(&e, 1, x);
195 for (size_t mode = 0; mode != selected.size(); ++mode) {
196 Tag tag;
197 int length;
198 double value;
199 CHKERR mb.tag_get_handle(selected[mode].targetTag.c_str(), tag);
200 CHKERR mb.tag_get_length(tag, length);
201 CHKERR require(length == 1,
202 "Selected component did not produce a scalar tag");
203 CHKERR mb.tag_get_data(tag, &e, 1, &value);
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();
210 }
211 }
212 CHKERR testing->checkError<TestType::POINTWISE>(
213 "coupler selected-component sampling", component_error, 1.,
214 {2.e-11, 0.});
215 }
216 // Locations can be reused after source values change.
217 Tag source_tag;
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",
225 report);
226 for (auto e : report.locatedEntities) {
227 Tag tag;
228 double value;
229 CHKERR mb.tag_get_handle("REFRESH_RESULT", tag);
230 CHKERR mb.tag_get_data(tag, &e, 1, &value);
231 CHKERR require(value == changed,
232 "Location reuse cached stale source values");
233 }
234 Coupling::LocationsPtr refreshed;
235 CHKERR coupling->locateTargets(transfer, target, refreshed, report);
236 CHKERR rejects(
237 [&]() {
238 return coupling->transferTags(transfer, locations, {}, report);
239 },
241 auto alias = transfer;
242 CHKERR coupling->destroyTransfer(transfer);
243 CHKERR coupling->destroyTransfer(transfer);
244 CHKERR rejects(
245 [&]() {
246 return coupling->transferTags(alias, refreshed, {}, report);
247 },
249 }
251}
252
253MoFEMErrorCode policies(MoFEM::Interface &m_field) {
255 auto &mb = m_field.get_moab();
256 auto coupling = m_field.getInterface<Coupling>();
257 int rank = m_field.get_comm_rank(), size = m_field.get_comm_size();
258 EntityHandle source_element = 0, target_element = 0;
260 source.parallelCommId = target.parallelCommId = -1;
261 // A rank-zero-only source and a target on the last rank exercise empty ranks.
262 if (rank == 0) {
263 CHKERR element(mb, box(true, 0., 1.), 1, source_element);
264 source.elements.insert(source_element);
265 }
266 if (rank == size - 1) {
267 auto points = box(false, -.1, .2);
268 for (auto &x : points) {
269 x[1] += .2;
270 x[2] += .2;
271 }
272 // Only the first vertex lies outside; all element-centroid samples lie inside.
273 points[0][0] = -.01;
274 points[1][0] = .3;
275 points[2][0] = .2;
276 points[3][0] = .2;
277 CHKERR element(mb, points, 100, target_element);
278 target.elements.insert(target_element);
279 }
280 Tag tag;
281 CHKERR tagData(mb, "POLICY_SOURCE", 1, source.elements, false, tag);
282 Coupling::Options options;
285 Coupling::TransferPtr transfer;
286 CHKERR coupling->createTransfer(source, options, transfer);
287 Coupling::LocationsPtr locations;
289 CHKERR coupling->locateTargets(transfer, target, locations, report);
290 CHKERR require(report.globalFallback == 1 && report.globalLocated == 3 &&
291 !report.globalMissing,
292 "Wrong fallback classification");
293 const double sentinel = -123.;
294 Tag output;
295 CHKERR mb.tag_get_handle("POLICY_TARGET", 1, MB_TYPE_DOUBLE, output,
296 MB_TAG_SPARSE | MB_TAG_CREAT);
297 if (target_element)
298 CHKERR mb.tag_set_data(output, &target_element, 1, &sentinel);
299 CHKERR coupling->transferTags(transfer, locations,
300 {{"POLICY_SOURCE", "POLICY_TARGET",
302 report);
303 if (target_element) {
304 double value;
305 CHKERR mb.tag_get_data(output, &target_element, 1, &value);
306 CHKERR require(value == sentinel,
307 "Fallback modified the target element tag");
309 CHKERR mb.get_connectivity(target.elements, vertices, true);
310 for (auto e : vertices) {
311 CHKERR mb.tag_get_data(output, &e, 1, &value);
312 CHKERR require(value == 7., "Incorrect fallback value");
313 }
314 }
315 Tag wrong_schema;
316 CHKERR mb.tag_get_handle("WRONG_SCHEMA", 2, MB_TYPE_DOUBLE, wrong_schema,
317 MB_TAG_SPARSE | MB_TAG_CREAT);
318 CHKERR rejects(
319 [&]() {
320 return coupling->transferTags(transfer, locations,
321 {{"POLICY_SOURCE", "WRONG_SCHEMA",
323 report);
324 },
326
327 // An invalid later map must leave the first destination untouched.
328 Tag untouched;
329 CHKERR mb.tag_get_handle("UNTOUCHED", 1, MB_TYPE_DOUBLE, untouched,
330 MB_TAG_SPARSE | MB_TAG_CREAT, &sentinel);
331 CHKERR rejects(
332 [&]() {
333 return coupling->transferTags(transfer, locations,
334 {{"POLICY_SOURCE", "UNTOUCHED",
336 {"DOES_NOT_EXIST", "NEVER_CREATED",
338 report);
339 },
341 for (auto e : report.locatedEntities) {
342 double value;
343 CHKERR mb.tag_get_data(untouched, &e, 1, &value);
344 CHKERR require(value == sentinel, "Failed batch partially published");
345 }
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);
349
351 CHKERR coupling->createTransfer(source, options, transfer);
352 CHKERR coupling->locateTargets(transfer, target, locations, report);
353 CHKERR rejects(
354 [&]() {
355 return coupling->transferTags(transfer, locations,
356 {{"POLICY_SOURCE", "STRICT_TARGET",
358 report);
359 },
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);
364
366 CHKERR coupling->createTransfer(source, options, transfer);
367 CHKERR coupling->locateTargets(transfer, target, locations, report);
368 CHKERR rejects(
369 [&]() {
370 return coupling->transferTags(transfer, locations,
371 {{"POLICY_SOURCE", "KEEP_ABSENT",
373 report);
374 },
376 CHKERR coupling->transferTags(
377 transfer, locations,
378 {{"POLICY_SOURCE", "UNTOUCHED", Coupling::Sampling::ELEMENT_CONSTANT}},
379 report);
380 for (auto e : report.missingEntities) {
381 double value;
382 CHKERR mb.tag_get_data(untouched, &e, 1, &value);
383 CHKERR require(value == sentinel, "KEEP overwrote existing data");
384 }
385 CHKERR coupling->destroyTransfer(transfer);
388 CHKERR rejects(
389 [&]() { return coupling->createTransfer(source, options, transfer); },
393 CHKERR rejects(
394 [&]() { return coupling->createTransfer(empty, options, transfer); },
397}
398
401 auto &mb = m_field.get_moab();
402 auto coupling = m_field.getInterface<Coupling>();
403 const int rank = m_field.get_comm_rank();
404 EntityHandle e;
405 CHKERR element(mb, box(false, 0., 1.), rank + 1, e);
406 Tag tag;
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.;
410 CHKERR mb.tag_set_data(tag, &e, 1, &value);
411 Coupling::Options options;
414 Coupling::TransferPtr transfer;
415 CHKERR coupling->createTransfer(selection(e), options, transfer);
416 Coupling::LocationsPtr locations;
418 // Several sources contain every point; smallest GLOBAL_ID must win.
419 CHKERR coupling->locateTargets(transfer, selection(e), locations, report);
420 CHKERR coupling->transferTags(transfer, locations,
421 {{"BOUNDARY_SOURCE", "BOUNDARY_TARGET",
423 report);
424 CHKERR mb.tag_get_handle("BOUNDARY_TARGET", tag);
425 CHKERR mb.tag_get_data(tag, &e, 1, &value);
426 CHKERR require(value == 1.,
427 "Boundary ownership is not deterministic by GLOBAL_ID");
428 CHKERR coupling->destroyTransfer(transfer);
430}
431
432MoFEMErrorCode contracts(MoFEM::Interface &m_field) {
434 auto &mb = m_field.get_moab();
435 std::unique_ptr<Coupling> coupling(
436 createInterface<Coupling>(static_cast<MoFEM::Core &>(m_field)));
437 auto testing = m_field.getInterface<TestingInterface>();
438 EntityHandle first;
439 CHKERR element(mb, box(false, 0., 1.), 10, first);
440 const EntityHandle *conn;
441 int count;
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);
449 Tag gid;
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) {
457 x[1] += .1;
458 x[2] += .1;
459 }
460 EntityHandle target_element;
461 CHKERR element(mb, target_points, 100, target_element);
462 auto target = selection(target_element);
463 Tag tag;
464 CHKERR mb.tag_get_handle("RECOVERY_ORACLE", 1, MB_TYPE_DOUBLE, tag,
465 MB_TAG_SPARSE | MB_TAG_CREAT);
466 double value = 2.;
467 CHKERR mb.tag_set_data(tag, &first, 1, &value);
468 value = 6.;
469 CHKERR mb.tag_set_data(tag, &second, 1, &value);
470 // A user scratch-name collision exists on just one rank.
471 if (m_field.get_comm_rank() == 0) {
472 Tag user_tag;
473 CHKERR mb.tag_get_handle("__MOFEM_COUPLER_1", 1, MB_TYPE_INTEGER, user_tag,
474 MB_TAG_SPARSE | MB_TAG_CREAT);
475 }
476 Coupling::TransferPtr transfer;
477 Coupling::LocationsPtr locations;
479 Range before;
480 CHKERR mb.get_entities_by_handle(0, before);
481 CHKERR coupling->createTransfer(source, {}, transfer);
482 Coupling::Selection no_targets;
483 no_targets.parallelCommId = -1;
484 CHKERR coupling->locateTargets(transfer, no_targets, locations, report);
485 CHKERR require(!report.globalLocated && !report.globalMissing &&
486 !report.globalFallback,
487 "Empty target produced nonempty counts");
488 CHKERR coupling->transferTags(transfer, locations,
489 {{"RECOVERY_ORACLE", "EMPTY_TARGET",
491 report);
492 CHKERR coupling->locateTargets(transfer, target, locations, report);
493 for (int component : {-2, 1}) {
494 CHKERR rejects(
495 [&]() {
496 return coupling->transferTags(
497 transfer, locations,
498 {{"RECOVERY_ORACLE", "INVALID_COMPONENT",
500 report);
501 },
503 Tag unexpected;
504 CHKERR require(mb.tag_get_handle("INVALID_COMPONENT", unexpected) ==
505 MB_TAG_NOT_FOUND,
506 "Invalid component created a destination tag");
507 }
508 CHKERR coupling->transferTags(
509 transfer, locations,
510 {{"RECOVERY_ORACLE", "RECOVERY_RESULT",
512 report);
513 double error = 0;
514 Tag result;
515 CHKERR mb.tag_get_handle("RECOVERY_RESULT", result);
516 for (auto e : report.locatedEntities) {
517 double x[3];
518 CHKERR mb.get_coords(&e, 1, x);
519 CHKERR mb.tag_get_data(result, &e, 1, &value);
520 // The three shared-face values are 4; the upper vertex value is 2.
521 error = std::max(error, std::abs(value - (4. - 2. * x[2])));
522 }
523 CHKERR testing->checkError<TestType::POINTWISE>(
524 "selected-element recovery oracle", error, 1., {1.e-12, 0.});
525 CHKERR coupling->destroyTransfer(transfer);
526 if (m_field.get_comm_rank() == 0) {
527 Tag collision;
528 DataType type;
529 CHKERR mb.tag_get_handle("__MOFEM_COUPLER_1", collision);
530 CHKERR mb.tag_get_data_type(collision, type);
531 CHKERR require(type == MB_TYPE_INTEGER,
532 "Recovery modified a user's scratch-name collision");
533 }
534 Range after;
535 CHKERR mb.get_entities_by_handle(0, after);
536 CHKERR require(
537 before == after,
538 "Closing a transfer leaked entities or deleted caller geometry");
539
540 // More than one local candidate contains a point; selection excludes another
541 // overlapping element whose smaller GLOBAL_ID must not influence the result.
542 EntityHandle smaller, excluded;
543 CHKERR mb.create_element(MBTET, shared.data(), 4, smaller);
544 CHKERR mb.create_element(MBTET, shared.data(), 4, excluded);
545 int id = 5;
546 CHKERR mb.tag_set_data(gid, &smaller, 1, &id);
547 id = 1;
548 CHKERR mb.tag_set_data(gid, &excluded, 1, &id);
549 value = 5.;
550 CHKERR mb.tag_set_data(tag, &smaller, 1, &value);
551 value = 99.;
552 CHKERR mb.tag_set_data(tag, &excluded, 1, &value);
553 source.elements.erase(second);
554 source.elements.insert(smaller);
555 CHKERR coupling->createTransfer(source, {}, transfer);
556 CHKERR coupling->locateTargets(transfer, target, locations, report);
557 CHKERR coupling->transferTags(
558 transfer, locations,
559 {{"RECOVERY_ORACLE", "LOCAL_TIE", Coupling::Sampling::ELEMENT_CONSTANT}},
560 report);
561 CHKERR mb.tag_get_handle("LOCAL_TIE", result);
562 for (auto e : report.locatedEntities) {
563 CHKERR mb.tag_get_data(result, &e, 1, &value);
564 CHKERR require(value == 5.,
565 "Local GLOBAL_ID tie or source selection is incorrect");
566 }
567 // Missing sparse values and nonfinite source values must not be published.
568 value = std::numeric_limits<double>::quiet_NaN();
569 CHKERR mb.tag_set_data(tag, &smaller, 1, &value);
570 CHKERR rejects(
571 [&]() {
572 return coupling->transferTags(transfer, locations,
573 {{"RECOVERY_ORACLE", "NONFINITE_RESULT",
575 report);
576 },
578 CHKERR require(mb.tag_get_handle("NONFINITE_RESULT", result) ==
579 MB_TAG_NOT_FOUND,
580 "Nonfinite input created a destination");
581 CHKERR coupling->destroyTransfer(transfer);
582 Coupling::Options invalid;
583 invalid.relativeTolerance = -1.;
584 CHKERR rejects(
585 [&]() { return coupling->createTransfer(source, invalid, transfer); },
587
588 if (m_field.get_comm_size() > 1) {
589 Coupling::Options inconsistent;
590 inconsistent.absoluteTolerance = m_field.get_comm_rank() * 1.e-12;
591 CHKERR rejects(
592 [&]() {
593 return coupling->createTransfer(source, inconsistent, transfer);
594 },
596 auto incomplete = source;
597 if (m_field.get_comm_rank() == 0)
598 incomplete.elements.clear();
599 CHKERR rejects(
600 [&]() { return coupling->createTransfer(incomplete, {}, transfer); },
602 }
603
604 if (m_field.get_comm_size() > 1) {
605 // Coincident coordinates are insufficient to establish equal replicas:
606 // disconnect one corner of the second cell on a single replica.
607 auto connected = selection(first);
608 connected.elements.insert(second);
609 if (m_field.get_comm_rank() == 1) {
610 double position[3];
611 CHKERR mb.get_coords(&shared[0], 1, position);
612 EntityHandle duplicate;
613 CHKERR mb.create_vertex(position, duplicate);
614 auto disconnected = std::vector<EntityHandle>(next, next + 4);
615 disconnected[0] = duplicate;
616 CHKERR mb.set_connectivity(second, disconnected.data(), 4);
617 }
618 CHKERR rejects(
619 [&]() { return coupling->createTransfer(connected, {}, transfer); },
621 auto restored = std::vector<EntityHandle>(next, next + 4);
622 CHKERR mb.set_connectivity(second, restored.data(), 4);
623 }
624
625 // Physical scale must not become a dimensionless inside tolerance.
626 for (bool hex : {false, true})
627 for (double scale : {1.e-9, 1.e9}) {
628 EntityHandle scaled_source, scaled_target;
629 CHKERR element(mb, box(hex, 0., scale), 1, scaled_source);
630 auto points = box(false, .1 * scale, .1 * scale);
631 for (auto &x : points) {
632 x[1] += .1 * scale;
633 x[2] += .1 * scale;
634 }
635 CHKERR element(mb, points, 2, scaled_target);
636 CHKERR coupling->createTransfer(selection(scaled_source), {}, transfer);
637 CHKERR coupling->locateTargets(transfer, selection(scaled_target),
638 locations, report);
639 CHKERR require(!report.globalMissing,
640 "Relative tolerance failed on scaled geometry");
641 CHKERR coupling->destroyTransfer(transfer);
642 }
644}
645
646MoFEMErrorCode selectedSharedVertex(MoFEM::Interface &m_field) {
648 if (m_field.get_comm_size() != 2) {
650 }
651 auto &mb = m_field.get_moab();
652 const int rank = m_field.get_comm_rank();
653 auto pc = std::make_unique<ParallelComm>(&mb, m_field.get_comm());
654 EntityHandle part;
655 CHKERR pc->create_part(part);
656 auto x = box(false, 0., 1.);
657 if (rank == 1) {
658 x[3][2] = -1.;
659 std::swap(x[1], x[2]);
660 }
661 EntityHandle target_element;
662 CHKERR element(mb, x, rank + 1, target_element);
663 const EntityHandle *conn;
664 int n;
665 CHKERR mb.get_connectivity(target_element, conn, n, false);
666 std::vector<EntityHandle> vertices(conn, conn + n);
667 Tag gid;
668 CHKERR mb.tag_get_handle(GLOBAL_ID_TAG_NAME, gid);
669 const int ids0[] = {1, 2, 3, 4}, ids1[] = {1, 3, 2, 5};
670 CHKERR mb.tag_set_data(gid, vertices.data(), n, rank == 0 ? ids0 : ids1);
671 Range cells;
672 cells.insert(target_element);
673 CHKERR mb.add_entities(part, cells);
674 CHKERR mb.add_entities(part, vertices.data(), vertices.size());
675 CHKERR pc->resolve_shared_ents(0, cells, 3, 0);
676 auto source_points = box(true, 0., 2.);
677 for (auto &v : source_points)
678 v[2] -= 1.;
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();
684 if (rank == 1)
685 target.elements = cells;
686 Tag source_tag;
687 CHKERR tagData(mb, "SUBSET_SOURCE", 1, source.elements, false, source_tag);
688 auto coupling = m_field.getInterface<Coupling>();
689 Coupling::TransferPtr transfer;
690 CHKERR coupling->createTransfer(source, {}, transfer);
691 Coupling::LocationsPtr locations;
693 CHKERR coupling->locateTargets(transfer, target, locations, report);
694 CHKERR require(report.globalLocated == 4 && !report.globalMissing,
695 "Subset omitted vertices whose owner has no selected cells");
696 CHKERR coupling->transferTags(transfer, locations,
697 {{"SUBSET_SOURCE", "SUBSET_TARGET",
699 report);
700 Tag result;
701 CHKERR mb.tag_get_handle("SUBSET_TARGET", result);
702 double value;
703 if (rank == 1)
704 for (auto v : vertices) {
705 CHKERR mb.tag_get_data(result, &v, 1, &value);
706 CHKERR require(value == 7.,
707 "Selected shared vertex was not synchronised");
708 }
709 else
710 CHKERR require(mb.tag_get_data(result, &vertices[3], 1, &value) ==
711 MB_TAG_NOT_FOUND,
712 "Subset publication touched an unselected vertex");
713 CHKERR coupling->destroyTransfer(transfer);
714 // Both cell centroids are inside this source, all target corners outside.
715 // Shared vertices must use both selected cells, including the remote one.
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];
720 v[2] -= .5;
721 }
722 EntityHandle fallback_source;
723 CHKERR element(mb, narrow, 1, fallback_source);
724 Range corners;
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);
728 Coupling::Options options;
730 target.elements = cells;
731 CHKERR coupling->createTransfer(fallback_selection, options, transfer);
732 CHKERR coupling->locateTargets(transfer, target, locations, report);
733 CHKERR require(report.globalFallback == 5 && !report.globalMissing &&
734 !report.globalLocated,
735 "Shared fallback did not cover selected target vertices");
736 CHKERR coupling->transferTags(
737 transfer, locations,
738 {{"SHARED_FALLBACK_SOURCE", "SHARED_FALLBACK_RESULT",
740 report);
741 CHKERR mb.tag_get_handle("SHARED_FALLBACK_RESULT", result);
742 for (auto v : vertices) {
743 double position[3];
744 CHKERR mb.get_coords(&v, 1, position);
745 CHKERR mb.tag_get_data(result, &v, 1, &value);
746 CHKERR require(std::abs(value - (.75 + position[2])) < 1.e-12,
747 "Shared fallback omitted remote selected-cell adjacency");
748 }
749 CHKERR coupling->destroyTransfer(transfer);
751}
752
753// Independent partition-seam oracle: shared recovered values must average
754// both selected cells, and nodal interpolation must ignore stale ghost data.
755MoFEMErrorCode distributedSharing(MoFEM::Interface &m_field) {
757 if (m_field.get_comm_size() != 2) {
759 }
760 // Isolate sharing tags from the other ownership fixtures.
761 moab::Core database;
762 MoFEM::Core core(database);
763 MoFEM::Interface &field = core;
764 auto &mb = field.get_moab();
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.);
770 if (rank) {
771 x[3][2] = -1.;
772 std::swap(x[1], x[2]);
773 }
774 CHKERR element(mb, x, rank + 1, cell);
775 const EntityHandle *conn;
776 int count;
777 CHKERR mb.get_connectivity(cell, conn, count, false);
778 std::vector<EntityHandle> vertices(conn, conn + count);
779 Tag gid;
780 CHKERR mb.tag_get_handle(GLOBAL_ID_TAG_NAME, gid);
781 const int ids0[] = {1, 2, 3, 4}, ids1[] = {1, 3, 2, 5};
782 CHKERR mb.tag_set_data(gid, vertices.data(), count, rank ? ids1 : ids0);
783 Range cells;
784 cells.insert(cell);
785 CHKERR mb.add_entities(part, cells);
786 CHKERR mb.add_entities(part, vertices.data(), vertices.size());
787 CHKERR pc->resolve_shared_ents(0, cells, 3, 0);
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);
795 for (auto v : vertices) {
796 int owner;
797 CHKERR pc->get_owner(v, owner);
798 double position[3];
799 CHKERR mb.get_coords(&v, 1, position);
800 const double value = owner == rank ? affine(position) : -999.;
801 CHKERR mb.tag_set_data(nodal_tag, &v, 1, &value);
802 }
803 auto points = box(false, .1, .1);
804 for (auto &v : points) {
805 v[1] += .1;
806 v[2] = rank ? .1 + v[2] : -.1 - v[2];
807 }
808 EntityHandle target_cell;
809 CHKERR element(mb, points, 100, target_cell);
810 auto source = selection(cell), target = selection(target_cell);
811 source.parallelCommId = pc->get_id();
812 Coupling::Options options;
814 auto coupling = field.getInterface<Coupling>();
815 Coupling::TransferPtr transfer;
816 Coupling::LocationsPtr locations;
818 CHKERR coupling->createTransfer(source, options, transfer);
819 CHKERR coupling->locateTargets(transfer, target, locations, report);
820 const std::vector<Coupling::TagMap> maps{
821 {"SEAM_CELL", "SEAM_RECOVERED",
823 {"SEAM_NODAL", "SEAM_LINEAR", Coupling::Sampling::VERTEX_LINEAR}};
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) {
829 double position[3];
830 CHKERR mb.get_coords(&e, 1, position);
831 for (size_t mode = 0; mode != maps.size(); ++mode) {
832 Tag result;
833 double value;
834 CHKERR mb.tag_get_handle(maps[mode].targetTag.c_str(), result);
835 CHKERR mb.tag_get_data(result, &e, 1, &value);
836 const double expected =
837 mode ? affine(position)
838 : 4. + 2. * update - (2. + 2. * update) * position[2];
839 CHKERR require(
840 std::abs(value - expected) < 1.e-12,
841 "Distributed source seam produced partition-dependent values");
842 }
843 }
844 for (auto v : vertices) {
845 int owner;
846 CHKERR pc->get_owner(v, owner);
847 if (owner != rank) {
848 double value;
849 CHKERR mb.tag_get_data(nodal_tag, &v, 1, &value);
850 CHKERR require(value == -999.,
851 "Source synchronization modified caller data");
852 }
853 }
854 }
855 CHKERR coupling->destroyTransfer(transfer);
856 // Source vertex owners can have no selected source cells.
857 auto subset = source;
858 if (!rank)
859 subset.elements.clear();
860 for (auto &v : points)
861 v[2] = -std::abs(v[2]);
862 EntityHandle subset_target;
863 CHKERR element(mb, points, 101, subset_target);
864 CHKERR coupling->createTransfer(subset, options, transfer);
865 CHKERR coupling->locateTargets(transfer, selection(subset_target), locations,
866 report);
867 CHKERR coupling->transferTags(transfer, locations, maps, report);
868 for (auto e : report.locatedEntities) {
869 double position[3];
870 CHKERR mb.get_coords(&e, 1, position);
871 for (size_t mode = 0; mode != maps.size(); ++mode) {
872 Tag result;
873 double value;
874 CHKERR mb.tag_get_handle(maps[mode].targetTag.c_str(), result);
875 CHKERR mb.tag_get_data(result, &e, 1, &value);
876 const double expected = mode ? affine(position) : 10.;
877 CHKERR require(
878 std::abs(value - expected) < 1.e-12,
879 "Source owner with no selected cells lost authoritative data");
880 }
881 }
882 CHKERR coupling->destroyTransfer(transfer);
883 // Selecting one component must also exchange the correct component from
884 // authoritative owners of a multi-component tag across a real partition seam.
885 Tag multi;
886 CHKERR mb.tag_get_handle("SEAM_MULTI", 3, MB_TYPE_DOUBLE, multi,
887 MB_TAG_SPARSE | MB_TAG_CREAT);
888 for (auto v : vertices) {
889 int owner;
890 double position[3];
891 CHKERR pc->get_owner(v, owner);
892 CHKERR mb.get_coords(&v, 1, position);
893 std::array<double, 3> row;
894 for (int c = 0; c != 3; ++c)
895 row[c] = owner == rank ? (c + 1) * affine(position) : -999.;
896 CHKERR mb.tag_set_data(multi, &v, 1, row.data());
897 }
898 CHKERR coupling->createTransfer(source, options, transfer);
899 CHKERR coupling->locateTargets(transfer, target, locations, report);
900 CHKERR coupling->transferTags(
901 transfer, locations,
902 {{"SEAM_MULTI", "SEAM_SELECTED", Coupling::Sampling::VERTEX_LINEAR, 2}},
903 report);
905 CHKERR mb.tag_get_handle("SEAM_SELECTED", selected);
906 double selected_error = 0.;
907 for (auto e : report.locatedEntities) {
908 double position[3], value;
909 CHKERR mb.get_coords(&e, 1, position);
910 CHKERR mb.tag_get_data(selected, &e, 1, &value);
911 selected_error =
912 std::isfinite(value)
913 ? std::max(selected_error, std::abs(value - 3. * affine(position)))
914 : std::numeric_limits<double>::infinity();
915 }
916 CHKERR field.getInterface<TestingInterface>()
918 "selected component from shared source owners", selected_error, 1.,
919 {1.e-12, 0.});
920 CHKERR coupling->destroyTransfer(transfer);
921 source.parallelCommId = -1;
922 CHKERR coupling->createTransfer(source, options, transfer);
923 CHKERR coupling->locateTargets(transfer, target, locations, report);
924 CHKERR rejects(
925 [&]() {
926 return coupling->transferTags(transfer, locations, {maps.front()},
927 report);
928 },
930 CHKERR coupling->destroyTransfer(transfer);
932}
933
936#ifndef WITH_MBCOUPLER
937 auto coupling = m_field.getInterface<Coupling>();
938 Coupling::TransferPtr transfer;
939 CHKERR rejects([&]() { return coupling->createTransfer({}, {}, transfer); },
941#else
942 CHKERR sampling(m_field);
943 CHKERR policies(m_field);
944 CHKERR boundary(m_field);
945 CHKERR contracts(m_field);
946 CHKERR selectedSharedVertex(m_field);
947 CHKERR distributedSharing(m_field);
948#endif
950}
951} // namespace
952
953int main(int argc, char **argv) {
954 MoFEM::Core::Initialize(&argc, &argv, nullptr,
955 "CouplerInterface manufactured tests");
956 try {
957 moab::Core mb;
958 MoFEM::Core core(mb);
959 MoFEM::Interface &m_field = core;
960 CHKERR run(m_field);
961 }
964 return 0;
965}
CGALInterfaceImpl & owner
TetMeshData result
bool selected
Definition CGALTools.cpp:97
Report & report
std::vector< int > source
int boundary
const std::vector< Vec > * points
std::vector< int > size
double length
std::vector< int > vertices
std::vector< Cell > cells
moab::Interface & mb
Tag tag
const Field * target
ProjectedField & field
std::vector< double > empty
One value per component.
std::vector< std::array< double, 3 > > coordinates
std::string type
std::vector< FieldBitNumber > row
Definition Schur.cpp:2488
SmartPetscObj< Vec > values
std::string name
int main()
#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_MOAB_ERROR
Definition definitions.h:41
@ MOFEM_ATOM_TEST_INVALID
Definition definitions.h:40
@ 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
PetscBool testing
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
Definition Common.hpp:10
MoFEMErrorCode TestingInterface::checkError< TestType::POINTWISE >(const std::string &check_name, PetscReal error, PetscReal scale, Tolerance tolerance, Sev error_severity, Sev success_severity) const
double scale
Definition plastic.cpp:124
MoFEMErrorCode run(MoFEM::Interface &m_field)
Definition sdf_python.cpp:7
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
Core (interface) class.
Definition Core.hpp:83
static MoFEMErrorCode Initialize(int *argc, char ***args, const char file[], const char help[])
Initializes the MoFEM database PETSc, MOAB and MPI.
Definition Core.cpp:68
moab::Interface & get_moab()
Definition Core.hpp:327
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
Definition Core.cpp:123
double relativeTolerance
Physical search distance = max(absoluteTolerance, relativeTolerance*L).
boost::shared_ptr< const Locations > LocationsPtr
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.