v0.16.3
Loading...
Searching...
No Matches
checkpoints.cpp
Go to the documentation of this file.
1/** \file checkpoints.cpp
2 * \brief Reload tutorial checkpoints and check the global mesh independently.
3 */
4#include <MoFEM.hpp>
5#include <cmath>
6#include <iomanip>
7using namespace MoFEM;
8
9namespace {
10using PointMap = std::map<int, std::array<double, 3>>;
11using CellKey = std::array<int, 4>;
12struct Snapshot {
13 PointMap points;
14 std::set<CellKey> cells;
15 std::set<std::array<int, 3>> skin;
16 double volume = 0;
17};
18
19Snapshot read(const char *file, PetscInt order, bool require_positive = true) {
20 moab::Core mb;
21 CHK_MOAB_THROW(mb.load_file(file), "Reload tutorial checkpoint");
22 Snapshot result;
23 std::map<std::array<int, 3>, int> incidence;
25 CHK_MOAB_THROW(mb.get_entities_by_type(0, MBVERTEX, vertices), "Vertices");
26 CHK_MOAB_THROW(mb.get_entities_by_type(0, MBTET, cells), "Cells");
27 for (auto v : vertices) {
28 int id;
29 std::array<double, 3> x;
30 CHK_MOAB_THROW(mb.tag_get_data(mb.globalId_tag(), &v, 1, &id), "Vertex ID");
31 CHK_MOAB_THROW(mb.get_coords(&v, 1, x.data()), "Coordinates");
32 if (id <= 0 || !result.points.emplace(id, x).second)
34 "Duplicate or missing vertex ID");
35 }
36 for (auto cell : cells) {
37 const EntityHandle *conn;
38 int count;
39 CellKey ids;
40 CHK_MOAB_THROW(mb.get_connectivity(cell, conn, count, true),
41 "Connectivity");
42 if (count != 4)
43 CHK_THROW_MESSAGE(MOFEM_ATOM_TEST_INVALID, "Expected linear tetrahedra");
44 CHK_MOAB_THROW(mb.tag_get_data(mb.globalId_tag(), conn, 4, ids.data()),
45 "Cell IDs");
46 const auto &x = result.points.at(ids[0]);
47 std::array<double, 3> a, b, c;
48 for (int d = 0; d != 3; ++d) {
49 a[d] = result.points.at(ids[1])[d] - x[d];
50 b[d] = result.points.at(ids[2])[d] - x[d];
51 c[d] = result.points.at(ids[3])[d] - x[d];
52 }
53 const double det = a[0] * (b[1] * c[2] - b[2] * c[1]) -
54 a[1] * (b[0] * c[2] - b[2] * c[0]) +
55 a[2] * (b[0] * c[1] - b[1] * c[0]);
56 std::sort(ids.begin(), ids.end());
57 if ((require_positive && det <= 0) || !result.cells.insert(ids).second)
59 "Nonpositive or duplicate cell");
60 result.volume += det / 6.;
61 for (int opposite = 0; opposite != 4; ++opposite) {
62 std::array<int, 3> face;
63 int n = 0;
64 for (int d = 0; d != 4; ++d)
65 if (d != opposite)
66 face[n++] = ids[d];
67 ++incidence[face];
68 }
69 }
70 for (const auto &[face, count] : incidence)
71 if (count == 1)
72 result.skin.insert(face);
73 // Each checkpoint must carry evidence of a freshly validated hybrid build.
74 Tag summary_tag;
75 CHK_MOAB_THROW(mb.tag_get_handle("FUN3_HYBRID_COUNTS", summary_tag),
76 "Hybrid stage summary");
77 Range summaries;
78 CHK_MOAB_THROW(mb.get_entities_by_type_and_tag(0, MBENTITYSET, &summary_tag,
79 nullptr, 1, summaries),
80 "Hybrid summary meshsets");
81 if (summaries.empty())
82 CHK_THROW_MESSAGE(MOFEM_ATOM_TEST_INVALID, "Missing hybrid stage summary");
83 std::map<std::array<int, 3>, std::set<std::array<int, 3>>> graph;
84 for (const auto &cell : result.cells) {
85 std::vector<std::array<int, 3>> interior;
86 for (int opposite = 0; opposite != 4; ++opposite) {
87 std::array<int, 3> face;
88 int n = 0;
89 for (int d = 0; d != 4; ++d)
90 if (d != opposite)
91 face[n++] = cell[d];
92 if (incidence.at(face) == 2)
93 interior.push_back(face);
94 }
95 for (const auto &face : interior)
96 graph[face].insert(interior.begin(), interior.end());
97 }
98 for (auto set : summaries) {
99 std::array<double, 5> data;
100 CHK_MOAB_THROW(mb.tag_get_data(summary_tag, &set, 1, data.data()),
101 "Hybrid stage counts");
102 if (order < 1 || data[0] != order)
104 "Checkpoint hybrid order differs from requested order");
105 const double face_dofs = NBFACETRI_L2(order - 1);
106 const double block_rows =
107 result.cells.size() *
110 const double schur_rows = graph.size() * face_dofs;
111 double nnz = 0;
112 for (const auto &[face, neighbors] : graph)
113 nnz += face_dofs * face_dofs * neighbors.size();
114 if (data[1] != block_rows + schur_rows || data[2] != block_rows ||
115 data[3] != schur_rows || data[4] != nnz)
117 "Hybrid algebra does not match checkpoint topology");
118 }
119 return result;
120}
121
122Snapshot readStep(const char *stage, PetscInt step, PetscInt order,
123 bool require_positive = true) {
124 std::ostringstream name;
125 name << stage << "_" << std::setw(4) << std::setfill('0') << step << ".h5m";
126 return read(name.str().c_str(), order, require_positive);
127}
128
132 PetscInt steps = 10;
133 PetscInt hybrid_order = 2;
134 double twist_degrees = 60;
135 std::array<double, 3> scale{2, 1, .75};
136 CHKERR PetscOptionsGetInt(nullptr, nullptr, "-stretch_steps", &steps,
137 nullptr);
138 CHKERR PetscOptionsGetInt(nullptr, nullptr, "-hybrid_order", &hybrid_order,
139 nullptr);
140 CHKERR PetscOptionsGetReal(nullptr, nullptr, "-twist_degrees", &twist_degrees,
141 nullptr);
142 for (int d = 0; d != 3; ++d) {
143 const std::string option = std::string("-scale_") + "xyz"[d];
144 CHKERR PetscOptionsGetReal(nullptr, nullptr, option.c_str(), &scale[d],
145 nullptr);
146 }
147 const auto sphere = read("01_sphere.h5m", hybrid_order);
148 const auto partitioned = read("02_partitioned.h5m", hybrid_order);
149 auto previous = read("03_distributed.h5m", hybrid_order);
150 if (sphere.points.empty() || sphere.cells.empty() ||
151 sphere.points != partitioned.points || sphere.points != previous.points ||
152 sphere.cells != partitioned.cells || sphere.cells != previous.cells)
153 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
154 "Partitioning or deletion changed source topology/IDs");
155 int changes = 0, subsequent_changes = 0;
156 int surface_changes = 0, subsequent_surface_changes = 0;
157 for (PetscInt step = 1; step <= steps; ++step) {
158 // Old connectivity can become invalid during nonlinear vertex motion;
159 // the reconstructed and synchronised meshes must have positive cells.
160 const auto stretched = readStep("04_ellipsoid", step, hybrid_order, false);
161 const auto reconstructed = readStep("05_reconstructed", step, hybrid_order);
162 auto synchronised = readStep("06_synchronised", step, hybrid_order);
163 if (previous.cells != stretched.cells ||
164 stretched.points != reconstructed.points ||
165 stretched.points != synchronised.points ||
166 reconstructed.cells != synchronised.cells ||
167 reconstructed.skin != synchronised.skin ||
168 stretched.points.size() != sphere.points.size())
169 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
170 "A stretch/reconstruction step lost vertex identity or topology");
171 const double fraction = static_cast<double>(step) / steps;
172 std::array<double, 3> axis_scale;
173 double volume_scale = 1;
174 for (int d = 0; d != 3; ++d) {
175 axis_scale[d] = 1. + fraction * (scale[d] - 1.);
176 volume_scale *= axis_scale[d];
177 }
178 for (const auto &[id, original] : sphere.points) {
179 const double angle =
180 .5 * twist_degrees * std::acos(-1.) / 180. * fraction * original[0];
181 const std::array<double, 3> rotated{
182 original[0],
183 std::cos(angle) * original[1] - std::sin(angle) * original[2],
184 std::sin(angle) * original[1] + std::cos(angle) * original[2]};
185 for (int d = 0; d != 3; ++d) {
186 const double expected = axis_scale[d] * rotated[d];
187 if (std::abs(stretched.points.at(id)[d] - expected) >
188 1e-12 * (1. + std::abs(expected)))
189 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
190 "Incremental stretch/twist reached the wrong coordinates");
191 }
192 }
193 // Twist preserves the analytic ellipsoid, but its discrete hull volume
194 // changes slightly as points move along the surface and diagonals flip.
195 const double expected_volume = sphere.volume * volume_scale;
196 if (std::abs(reconstructed.volume - expected_volume) >
197 .05 * expected_volume)
198 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
199 "Reconstructed volume is inconsistent with the ellipsoid");
200 if (stretched.skin != reconstructed.skin) {
201 ++surface_changes;
202 if (step > 1)
203 ++subsequent_surface_changes;
204 }
205 if (stretched.cells != reconstructed.cells) {
206 ++changes;
207 if (step > 1)
208 ++subsequent_changes;
209 }
210 previous = std::move(synchronised);
211 }
212 if (!changes || (steps > 1 && !subsequent_changes))
213 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
214 "The stretching fixture did not produce successive connectivity "
215 "changes");
216 if (twist_degrees != 0 &&
217 (!surface_changes || (steps > 1 && !subsequent_surface_changes)))
218 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
219 "The twist fixture did not change the surface skin");
220 if (twist_degrees == 0 && subsequent_surface_changes)
221 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
222 "Affine stretching unexpectedly changed the convex-hull skin");
223 MOFEM_LOG("WORLD", Sev::inform)
224 << "Verified " << steps << " checkpoint steps, " << changes
225 << " with connectivity changes, " << surface_changes
226 << " with skin changes";
228}
229} // namespace
230
231int main(int argc, char *argv[]) {
232 MoFEM::Core::Initialize(&argc, &argv, nullptr, "Check mesh checkpoints\n");
233 try {
234 moab::Core mb;
235 MoFEM::Core core(mb);
236 CHKERR run(core);
237 }
240 return 0;
241}
TetMeshData result
FaceSet skin
const std::vector< Vec > * points
std::vector< int > size
bool angle
Radians between candidate face and target plane.
std::vector< int > vertices
std::vector< Cell > cells
moab::Interface & mb
std::map< EntityHandle, std::pair< bool, std::vector< double > > > previous
It end
Definition Schur.cpp:2568
std::string name
int main()
constexpr double a
#define CATCH_ERRORS
Catch errors.
#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_ATOM_TEST_INVALID
Definition definitions.h:40
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
constexpr int order
#define MOFEM_LOG(channel, severity)
Log.
#define NBFACETRI_L2(P)
Number of base functions on triangle for L2 space.
#define NBVOLUMETET_DEMKOWICZ_HDIV(P)
#define NBFACETRI_DEMKOWICZ_HDIV(P)
#define NBVOLUMETET_L2(P)
Number of base functions on tetrahedron for L2 space.
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
PetscErrorCode PetscOptionsGetInt(PetscOptions *, const char pre[], const char name[], PetscInt *ivalue, PetscBool *set)
PetscErrorCode PetscOptionsGetReal(PetscOptions *, const char pre[], const char name[], PetscReal *dval, PetscBool *set)
double scale
Definition plastic.cpp:124
MoFEMErrorCode run(MoFEM::Interface &m_field)
Definition sdf_python.cpp:7
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
Deprecated interface functions.
std::vector< std::array< int, 4 > > cells
Zero-based point indices.
Definition CGALTools.hpp:19
std::vector< std::array< double, 3 > > points
Definition CGALTools.hpp:17
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.