v0.16.3
Loading...
Searching...
No Matches
mesh_data_transfer.cpp
Go to the documentation of this file.
1/** \file mesh_data_transfer.cpp
2 * \brief Independent file fixtures and oracles for the unified transfer tool.
3 */
4#include <MoFEM.hpp>
5#include <array>
6#include <cmath>
7#include <limits>
8#include <memory>
9using namespace MoFEM;
10
11namespace {
12constexpr int source_components = 5;
13
14std::vector<std::array<double, 3>> sourceCoordinates(int rank) {
15 std::vector<std::array<double, 3>> x{{0, 0, 0}, {1, 0, 0}, {0, 1, 0},
16 {0, 0, 1}, {0, 0, 0}, {.4, .3, .2}};
17 for (auto &v : x)
18 v[0] += 2 * rank;
19 return x;
20}
21
22MoFEMErrorCode prepare(MPI_Comm comm) {
24 int rank;
25 CHKERR MPI_Comm_rank(comm, &rank);
26 for (bool source : {true, false}) {
27 moab::Core mb;
28 auto pc = std::make_unique<ParallelComm>(&mb, comm);
29 EntityHandle part;
30 CHKERR pc->create_part(part);
31 Tag gid;
32 CHKERR mb.tag_get_handle(GLOBAL_ID_TAG_NAME, 1, MB_TYPE_INTEGER, gid,
33 MB_TAG_DENSE | MB_TAG_CREAT);
34 auto x = sourceCoordinates(rank);
35 if (!source) {
36 x.resize(4);
37 for (auto &v : x) {
38 v[0] = 2 * rank + .1 + .1 * (v[0] - 2 * rank);
39 v[1] = .1 + .1 * v[1];
40 v[2] = .1 + .1 * v[2];
41 }
42 }
43 std::vector<EntityHandle> vertices;
44 for (size_t i = 0; i != x.size(); ++i) {
45 EntityHandle v;
46 CHKERR mb.create_vertex(x[i].data(), v);
47 vertices.push_back(v);
48 const int id = (source ? 1 : 501) + 20 * rank + i;
49 CHKERR mb.tag_set_data(gid, &v, 1, &id);
50 }
51 EntityHandle cell;
52 CHKERR mb.create_element(MBTET, vertices.data(), 4, cell);
53 const int cell_id = (source ? 100 : 600) + rank;
54 CHKERR mb.tag_set_data(gid, &cell, 1, &cell_id);
55 CHKERR mb.add_entities(part, vertices.data(), vertices.size());
56 CHKERR mb.add_entities(part, &cell, 1);
57 if (source) {
58 Tag tag;
59 CHKERR mb.tag_get_handle("INDEX_TEST", source_components, MB_TYPE_DOUBLE,
60 tag, MB_TAG_SPARSE | MB_TAG_CREAT);
61 // Coincident vertices carry different values; the final vertex has no tag.
62 Range tagged;
63 tagged.insert(vertices.data(), vertices.data() + vertices.size() - 1);
64 tagged.insert(cell);
65 for (auto e : tagged) {
66 int id;
67 CHKERR mb.tag_get_data(gid, &e, 1, &id);
68 std::array<double, source_components> row;
69 for (int c = 0; c != source_components; ++c)
70 row[c] = 10. * id + c;
71 CHKERR mb.tag_set_data(tag, &e, 1, row.data());
72 }
73 }
74 CHKERR mb.write_file(source ? "mesh_data_transfer_source.h5m"
75 : "mesh_data_transfer_target.h5m",
76 nullptr, "PARALLEL=WRITE_PART", pc->partition_sets());
77 }
79}
80
81MoFEMErrorCode checkCopy(TestingInterface &testing, const char *file,
82 const char *name, int component) {
84 moab::Core mb;
85 CHKERR mb.load_file(file);
87 CHKERR mb.get_entities_by_type(0, MBVERTEX, vertices);
88 CHKERR mb.get_entities_by_type(0, MBTET, cells);
89 CHKERR testing.checkValue<TestType::SCALAR>("copied vertices",
90 vertices.size(), 12., {0., 0.});
91 CHKERR testing.checkValue<TestType::SCALAR>("copied cells", cells.size(), 2.,
92 {0., 0.});
93 Tag gid, tag, source;
94 CHKERR mb.tag_get_handle(GLOBAL_ID_TAG_NAME, gid);
95 CHKERR mb.tag_get_handle(name, tag);
96 CHKERR mb.tag_get_handle("INDEX_TEST", source);
97 int n;
98 CHKERR mb.tag_get_length(tag, n);
99 CHKERR testing.checkValue<TestType::SCALAR>(
100 "copied tag length", n, component < 0 ? source_components : 1, {0., 0.});
101 double error = 0.;
102 const auto compare = [&](double actual, double expected) {
103 error = std::isfinite(actual) ? std::max(error, std::abs(actual - expected))
104 : std::numeric_limits<double>::infinity();
105 };
106 Range entities = vertices;
107 entities.merge(cells);
108 for (auto e : entities) {
109 int id;
110 CHKERR mb.tag_get_data(gid, &e, 1, &id);
111 if (mb.type_from_handle(e) == MBVERTEX) {
112 double x[3];
113 CHKERR mb.get_coords(&e, 1, x);
114 const auto expected = sourceCoordinates(id / 20).at(id % 20 - 1);
115 for (int d = 0; d != 3; ++d)
116 compare(x[d], expected[d]);
117 if (id % 20 == 6) {
118 std::array<double, source_components> value;
119 CHKERR testing.checkValue<TestType::SCALAR>(
120 "untagged entity remains untagged",
121 mb.tag_get_data(tag, &e, 1, value.data()), MB_TAG_NOT_FOUND,
122 {0., 0.});
123 continue;
124 }
125 }
126 std::vector<double> value(n);
127 CHKERR mb.tag_get_data(tag, &e, 1, value.data());
128 for (int c = 0; c != n; ++c)
129 compare(value[c], 10. * id + (component < 0 ? c : component));
130 std::array<double, source_components> original;
131 CHKERR mb.tag_get_data(source, &e, 1, original.data());
132 for (int c = 0; c != source_components; ++c)
133 compare(original[c], 10. * id + c);
134 }
135 CHKERR testing.checkError<TestType::POINTWISE>(
136 "exact tag copy and preserved source", error, 0., {0., 0.});
138}
139
140MoFEMErrorCode checkTransfer(TestingInterface &testing, bool nodal) {
142 moab::Core mb;
143 CHKERR mb.load_file(nodal ? "mesh_data_transfer_component.h5m"
144 : "mesh_data_transfer_cell_component.h5m");
145 Tag tag, gid;
146 CHKERR mb.tag_get_handle("SELECTED", tag);
147 CHKERR mb.tag_get_handle(GLOBAL_ID_TAG_NAME, gid);
148 int n;
149 CHKERR mb.tag_get_length(tag, n);
150 CHKERR testing.checkValue<TestType::SCALAR>("transferred scalar length", n,
151 1., {0., 0.});
152 Range entities;
153 CHKERR mb.get_entities_by_type(0, nodal ? MBVERTEX : MBTET, entities);
154 CHKERR testing.checkValue<TestType::SCALAR>(
155 "transferred entities", entities.size(), nodal ? 8. : 2., {0., 0.});
156 double error = 0.;
157 for (auto e : entities) {
158 double value, expected;
159 CHKERR mb.tag_get_data(tag, &e, 1, &value);
160 if (nodal) {
161 double x[3];
162 CHKERR mb.get_coords(&e, 1, x);
163 const int rank = static_cast<int>(x[0] / 2.);
164 expected =
165 200. * rank + 10. * (1. + x[0] - 2 * rank + 2 * x[1] + 3 * x[2]) + 4.;
166 } else {
167 int id;
168 CHKERR mb.tag_get_data(gid, &e, 1, &id);
169 expected = 10. * (100 + id - 600);
170 }
171 error = std::isfinite(value) ? std::max(error, std::abs(value - expected))
172 : std::numeric_limits<double>::infinity();
173 }
174 CHKERR testing.checkError<TestType::POINTWISE>(
175 "selected-component interpolation", error, 1., {1.e-11, 0.});
177}
178} // namespace
179
180int main(int argc, char **argv) {
181 MoFEM::Core::Initialize(&argc, &argv, nullptr,
182 "Mesh tag transfer file fixtures\n");
183 try {
184 moab::Core mb;
185 MoFEM::Core core(mb);
186 std::unique_ptr<TestingInterface> owned_testing(
188 CHKERR owned_testing->addTestCommandLine(
189 "", 1, "prepare", [&]() { return prepare(PETSC_COMM_WORLD); });
190 CHKERR owned_testing->addTestCommandLine("", 2, "copy", [&]() {
191 return checkCopy(*owned_testing, "mesh_data_transfer_copy.h5m",
192 "INDEX_COPY", -1);
193 });
194 CHKERR owned_testing->addTestCommandLine("", 3, "extract", [&]() {
195 return checkCopy(*owned_testing, "mesh_data_transfer_extract.h5m",
196 "INDEX_TEST_4", 4);
197 });
198 CHKERR owned_testing->addTestCommandLine("", 4, "alias", [&]() {
199 return checkCopy(*owned_testing, "mesh_data_transfer_alias.h5m",
200 "INDEX_TEST", -1);
201 });
202 CHKERR owned_testing->addTestCommandLine(
203 "", 5, "nodal", [&]() { return checkTransfer(*owned_testing, true); });
204 CHKERR owned_testing->addTestCommandLine(
205 "", 6, "cell", [&]() { return checkTransfer(*owned_testing, false); });
206 CHKERR owned_testing->addTestCommandLine("", 7, "parallel_copy", [&]() {
207 return checkCopy(*owned_testing, "mesh_data_transfer_parallel.h5m",
208 "INDEX_COPY", -1);
209 });
210 CHKERR owned_testing->runTests();
211 }
214 return 0;
215}
std::vector< int > source
std::vector< int > vertices
std::vector< Cell > cells
MPI_Comm comm
moab::Interface & mb
Tag tag
std::vector< FieldBitNumber > row
Definition Schur.cpp:2488
std::string name
int main()
#define CATCH_ERRORS
Catch errors.
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
FTensor::Index< 'i', SPACE_DIM > i
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
TestingInterface * createInterface< TestingInterface >(const Core &core)
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
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
Definition Core.cpp:123