v0.16.0
Loading...
Searching...
No Matches
json_config_manager.cpp
Go to the documentation of this file.
1/**
2 * @file json_config_manager.cpp
3 * @brief Atom test for JsonConfigManager
4 */
5
6#include <MoFEM.hpp>
7
8using namespace MoFEM;
9
10static char help[] = "JSON config manager atom test\n";
11
12namespace {
13
14double readRealOption(const char *name) {
15 double value = 0;
16 PetscBool found = PETSC_FALSE;
17 CHKERRABORT(PETSC_COMM_WORLD,
18 PetscOptionsGetReal(PETSC_NULLPTR, PETSC_NULLPTR, name, &value,
19 &found));
20 if (!found) {
21 throw std::runtime_error("Expected PETSc real option to be set: " +
22 std::string(name));
23 }
24 return value;
25}
26
27int readIntOption(const char *name) {
28 int value = 0;
29 PetscBool found = PETSC_FALSE;
30 CHKERRABORT(PETSC_COMM_WORLD,
31 PetscOptionsGetInt(PETSC_NULLPTR, PETSC_NULLPTR, name, &value,
32 &found));
33 if (!found) {
34 throw std::runtime_error("Expected integer option to be set: " +
35 std::string(name));
36 }
37 return value;
38}
39
40std::string readStringOption(const char *name) {
41 char value[256] = "";
42 PetscBool found = PETSC_FALSE;
43 CHKERRABORT(PETSC_COMM_WORLD,
44 PetscOptionsGetString(PETSC_NULLPTR, PETSC_NULLPTR, name, value,
45 sizeof(value), &found));
46 if (!found) {
47 throw std::runtime_error("Expected PETSc string option to be set: " +
48 std::string(name));
49 }
50 return value;
51}
52
53bool readOptionalStringOption(const char *name, std::string &value) {
54 char buffer[PETSC_MAX_PATH_LEN] = "";
55 PetscBool found = PETSC_FALSE;
56 CHKERRABORT(PETSC_COMM_WORLD,
57 PetscOptionsGetString(PETSC_NULLPTR, PETSC_NULLPTR, name, buffer,
58 sizeof(buffer), &found));
59 if (!found)
60 return false;
61 value.assign(buffer);
62 return true;
63}
64
65bool hasOption(const char *name) {
66 PetscBool found = PETSC_FALSE;
67 CHKERRABORT(PETSC_COMM_WORLD,
68 PetscOptionsHasName(PETSC_NULLPTR, PETSC_NULLPTR, name, &found));
69 return found == PETSC_TRUE;
70}
71
72void assertNear(const double actual, const double expected, const double tol,
73 const char *message) {
74 if (std::abs(actual - expected) > tol) {
75 std::ostringstream oss;
76 oss << message << " expected=" << std::setprecision(16) << expected
77 << " actual=" << actual;
78 throw std::runtime_error(oss.str());
79 }
80}
81
82void assertAttributeValues(const std::vector<double> &attrs,
83 std::initializer_list<double> expected,
84 const char *label) {
85 if (attrs.size() != expected.size()) {
86 std::ostringstream oss;
87 oss << label << " attributes size mismatch expected=" << expected.size()
88 << " actual=" << attrs.size();
89 throw std::runtime_error(oss.str());
90 }
91
92 size_t index = 0;
93 for (const auto value : expected) {
94 std::ostringstream oss;
95 oss << label << " User" << index + 1;
96 assertNear(attrs[index], value, 1e-12, oss.str().c_str());
97 ++index;
98 }
99}
100
101void assertBlockAttributes(MoFEM::Interface &m_field, const int meshset_id,
102 std::initializer_list<double> expected,
103 const char *label) {
105 if (it->getMeshsetId() != meshset_id) {
106 continue;
107 }
108
109 std::vector<double> attrs;
110 CHKERRABORT(PETSC_COMM_WORLD, it->getAttributes(attrs));
111 assertAttributeValues(attrs, expected, label);
112 return;
113 }
114
115 throw std::runtime_error(std::string(label) + " BLOCKSET not found");
116}
117
118int assertBlockAttributesByName(MoFEM::Interface &m_field,
119 const std::string &meshset_name,
120 std::initializer_list<double> expected,
121 const char *label) {
123 if (it->getName() != meshset_name) {
124 continue;
125 }
126
127 std::vector<double> attrs;
128 CHKERRABORT(PETSC_COMM_WORLD, it->getAttributes(attrs));
129 assertAttributeValues(attrs, expected, label);
130 return it->getMeshsetId();
131 }
132
133 throw std::runtime_error(std::string(label) + " BLOCKSET not found by name");
134}
135
136} // namespace
137
138int main(int argc, char *argv[]) {
139
140 MoFEM::Core::Initialize(&argc, &argv, (char *)0, help);
141
142 try {
143 moab::Core mb_instance;
144 moab::Interface &moab = mb_instance;
145
146 MoFEM::Core core(moab);
147 MoFEM::Interface &m_field = core;
148 auto *json_config = m_field.getInterface<JsonConfigManager>();
149
150 CHKERR json_config->getSubInterfaceOptions();
151
152 const std::string expected_ksp_type =
153 readStringOption("-expected_ksp_type");
154 const std::string expected_pc_type =
155 readStringOption("-expected_pc_type");
156 std::string expected_file_name;
157
158 if (readStringOption("-ksp_type") != expected_ksp_type) {
159 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
160 "Unexpected -ksp_type value");
161 }
162 if (readStringOption("-pc_type") != expected_pc_type) {
163 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
164 "Unexpected -pc_type value");
165 }
166 assertNear(readRealOption("-ksp_rtol"), 1e-8, 1e-14,
167 "Unexpected -ksp_rtol");
168 if (readIntOption("-order") != 2) {
169 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
170 "Unexpected -order value");
171 }
172 if (readIntOption("-atom_test") != 1) {
173 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
174 "Unexpected -atom_test value");
175 }
176 if (!hasOption("-log_no_color")) {
177 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
178 "Expected -log_no_color option to be set");
179 }
180 if (readOptionalStringOption("-expected_file_name", expected_file_name)) {
181 if (readStringOption("-file_name") != expected_file_name) {
182 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
183 "Unexpected -file_name value");
184 }
185 }
186
187 MeshsetsManager *meshsets_manager_ptr;
188 CHKERR m_field.getInterface(meshsets_manager_ptr);
189
190 CHKERR meshsets_manager_ptr->addMeshset(BLOCKSET, 1001, "elastic_domain");
191 CHKERR meshsets_manager_ptr->addMeshset(BLOCKSET, 1002, "trans_iso_domain");
192 CHKERR meshsets_manager_ptr->addMeshset(BLOCKSET, 1003, "thermal_domain");
193 CHKERR meshsets_manager_ptr->addMeshset(BLOCKSET, 1004, "moisture_domain");
194 CHKERR meshsets_manager_ptr->addMeshset(BLOCKSET, 1005, "interface_domain");
195 CHKERR meshsets_manager_ptr->addMeshset(BLOCKSET, 1006, "bodyforces_domain");
196 CHKERR meshsets_manager_ptr->addMeshset(BLOCKSET, 1007, "generic_block");
197 CHKERR meshsets_manager_ptr->addMeshset(BLOCKSET, 1008, "int_electric_custom");
198 CHKERR meshsets_manager_ptr->addMeshset(BLOCKSET, 2001, "displacement");
199 CHKERR meshsets_manager_ptr->addMeshset(BLOCKSET, 2002, "force");
200 CHKERR meshsets_manager_ptr->addMeshset(BLOCKSET, 2003, "temperature");
201 CHKERR meshsets_manager_ptr->addMeshset(BLOCKSET, 3001, "pressure");
202 CHKERR meshsets_manager_ptr->addMeshset(BLOCKSET, 3002, "heatflux");
203
204 CHKERR meshsets_manager_ptr->setMeshsetFromFile();
205 CHKERR json_config->applyMeshsets();
206
207 const bool shared_types_same_blockset_test =
208 hasOption("-shared_types_same_blockset_test");
209 if (shared_types_same_blockset_test) {
210 const auto elastic_params =
211 json_config->getParamsFromBlockset("MAT_ELASTIC", 1001);
212 if (elastic_params.at("young") != 1000.0 ||
213 elastic_params.at("poisson") != 0.3) {
214 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
215 "Unexpected shared-block MAT_ELASTIC params from JSON");
216 }
217
218 const auto thermal_params =
219 json_config->getParamsFromBlockset("MAT_THERMAL", 1001);
220 if (thermal_params.at("conductivity") != 11.0 ||
221 thermal_params.at("heatcapacity") != 22.0) {
222 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
223 "Unexpected shared-block MAT_THERMAL params from JSON");
224 }
225
226 const int elastic_id = assertBlockAttributesByName(
227 m_field, "MAT_ELASTIC_elastic_domain_1001", {1000.0, 0.3},
228 "MAT_ELASTIC");
229 const int thermal_id = assertBlockAttributesByName(
230 m_field, "MAT_THERMAL_elastic_domain_3003", {11.0, 22.0},
231 "MAT_THERMAL");
232 if (elastic_id == thermal_id) {
233 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
234 "Expected distinct blocksets for shared source meshset name");
235 }
236
237 const auto thermal_target_params =
238 json_config->getParamsFromBlockset("MAT_THERMAL", thermal_id);
239 if (thermal_target_params.at("conductivity") != 11.0 ||
240 thermal_target_params.at("heatcapacity") != 22.0) {
241 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
242 "Unexpected shared-block MAT_THERMAL params for target id");
243 }
244 }
245
246 if (!shared_types_same_blockset_test) {
247 const bool expect_multiple_python_scripts =
248 hasOption("-expect_multiple_python_scripts");
249 if (json_config->getMoabMeshByKey("primary") != "json_primary_mesh.h5m") {
250 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
251 "Unexpected keyed MOAB mesh lookup result");
252 }
253 if (!json_config->getMoabMeshByKey("missing_mesh").empty()) {
254 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
255 "Unexpected missing keyed MOAB mesh lookup result");
256 }
257 if (expect_multiple_python_scripts) {
258 if (json_config->getMoabMeshByKey("background_mesh") !=
259 "rectangle_quad.h5m") {
260 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
261 "Unexpected auxiliary keyed MOAB mesh lookup result");
262 }
263 if (json_config->getPythonScriptByKey("python_script_1") !=
264 "./sdf_files/sdf_hertz_2d_plane.py" ||
265 json_config->getPythonScriptByKey("python_script_2") !=
266 "./sdf_files/sdf_hertz_2d_plane_2.py") {
267 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
268 "Unexpected keyed Python script lookup result");
269 }
270 } else {
271 if (json_config->getPythonScriptByKey("python_script") !=
272 "./sdf_files/sdf_hertz_2d_plane.py") {
273 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
274 "Unexpected keyed Python script lookup result");
275 }
276 }
277 if (!json_config->getPythonScriptByKey("missing_script").empty()) {
278 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
279 "Unexpected missing keyed Python script lookup result");
280 }
281
282 const auto elastic_params =
283 json_config->getParamsFromBlockset("hencky", 1001);
284 if (elastic_params.at("young") != 1000.0 ||
285 elastic_params.at("poisson") != 0.3) {
286 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
287 "Unexpected hencky params from JSON");
288 }
289
290 const auto generic_params =
291 json_config->getParamsFromBlockset("GENERIC_BLOCK", 1007);
292 if (generic_params.at("user1") != 9.0 ||
293 generic_params.at("user1b") != 8.0) {
294 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
295 "Unexpected GENERIC_BLOCK params from JSON");
296 }
297
298 const auto int_electric_custom_params =
299 json_config->getParamsFromBlockset("INT_ELECTRIC_CUSTOM", 1008);
300 if (int_electric_custom_params.at("charge_density") != 12.0 ||
301 int_electric_custom_params.at("mobility") != 13.0 ||
302 int_electric_custom_params.at("scale") != 14.0) {
303 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
304 "Unexpected INT_ELECTRIC_CUSTOM params from JSON");
305 }
306
307 const auto displacement_params =
308 json_config->getParamsFromBlockset("DISPLACEMENT", 2001);
309 if (displacement_params.at("flag1") != 1.0 ||
310 displacement_params.at("flag2") != 1.0 ||
311 displacement_params.at("flag3") != 0.0 ||
312 displacement_params.at("value3") != 3.0) {
313 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
314 "Unexpected DISPLACEMENT params from JSON");
315 }
316
317 const auto force_params =
318 json_config->getParamsFromBlockset("FORCE", 2002);
319 if (force_params.at("force_magnitude") != 10.0 ||
320 force_params.at("moment_magnitude") != 11.0 ||
321 force_params.at("moment_my") != 1.0) {
322 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
323 "Unexpected FORCE params from JSON");
324 }
325
326 const auto temperature_params =
327 json_config->getParamsFromBlockset("TEMPERATURE", 2003);
328 if (temperature_params.at("flag1") != 1.0 ||
329 temperature_params.at("value1") != 123.0) {
330 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
331 "Unexpected TEMPERATURE params from JSON");
332 }
333
334 const auto pressure_params =
335 json_config->getParamsFromBlockset("PRESSURE", 3001);
336 if (pressure_params.at("flag2") != 1.0 ||
337 pressure_params.at("value1") != 12.5) {
338 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
339 "Unexpected PRESSURE params from JSON");
340 }
341
342 const auto heatflux_params =
343 json_config->getParamsFromBlockset("HEATFLUX", 3002);
344 if (heatflux_params.at("flag1") != 1.0 ||
345 heatflux_params.at("value1") != 99.0) {
346 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
347 "Unexpected HEATFLUX params from JSON");
348 }
349
350 if (!json_config->getParamsFromBlockset("UNKNOWN_TYPE", 1007).empty()) {
351 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
352 "Unexpected params returned for unknown blockset type");
353 }
354
355 assertBlockAttributes(m_field, 1001, {1000.0, 0.3, 1e-5, 7.0},
356 "hencky");
357 assertBlockAttributes(m_field, 1002, {1.0, 2.0, 3.0, 4.0, 5.0},
358 "MAT_ELASTIC_TRANSISO");
359 assertBlockAttributes(m_field, 1003, {11.0, 22.0}, "MAT_THERMAL");
360 assertBlockAttributes(m_field, 1005, {10.0, 20.0, 30.0, 40.0},
361 "MAT_INTERF");
362 assertBlockAttributes(m_field, 1006, {100.0, 1.0, 2.0, 3.0},
363 "BODYFORCES");
364 assertBlockAttributes(m_field, 1007, {9.0, 8.0}, "GENERIC_BLOCK");
365 assertBlockAttributes(m_field, 1008, {12.0, 13.0, 14.0},
366 "INT_ELECTRIC_CUSTOM");
367 assertBlockAttributes(m_field, 2001, {1.0, 1.0, 0.0, 1.0, 2.0, 3.0},
368 "DISPLACEMENT");
369 assertBlockAttributes(m_field, 2002,
370 {10.0, 11.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0},
371 "FORCE");
372 assertBlockAttributes(m_field, 2003, {1.0, 123.0}, "TEMPERATURE");
373 assertBlockAttributes(m_field, 3001, {1.0, 12.5}, "PRESSURE");
374 assertBlockAttributes(m_field, 3002, {1.0, 99.0}, "HEATFLUX");
375 }
376 }
378
380}
int main()
#define CATCH_ERRORS
Catch errors.
@ BLOCKSET
@ MOFEM_ATOM_TEST_INVALID
Definition definitions.h:40
#define CHKERR
Inline error check.
MoFEMErrorCode addMeshset(const CubitBCType cubit_bc_type, const int ms_id, const std::string name="")
Add CUBIT meshset to manager.
#define _IT_CUBITMESHSETS_BY_SET_TYPE_FOR_LOOP_(MESHSET_MANAGER, CUBITBCTYPE, IT)
Iterator that loops over a specific Cubit MeshSet having a particular BC meshset in a moFEM field.
static char help[]
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)
PetscErrorCode PetscOptionsGetString(PetscOptions *, const char pre[], const char name[], char str[], size_t size, PetscBool *set)
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.
MoFEMErrorCode getSubInterfaceOptions()
Interface for managing meshsets containing materials and boundary conditions.
MoFEMErrorCode setMeshsetFromFile(const string file_name, const bool clean_file_options=true)
add blocksets reading config file
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.
double tol