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(
18 PETSC_COMM_WORLD,
19 PetscOptionsGetReal(PETSC_NULLPTR, PETSC_NULLPTR, name, &value, &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, PetscOptionsGetInt(PETSC_NULLPTR, PETSC_NULLPTR,
31 name, &value, &found));
32 if (!found) {
33 throw std::runtime_error("Expected integer option to be set: " +
34 std::string(name));
35 }
36 return value;
37}
38
39std::string readStringOption(const char *name) {
40 char value[256] = "";
41 PetscBool found = PETSC_FALSE;
42 CHKERRABORT(PETSC_COMM_WORLD,
43 PetscOptionsGetString(PETSC_NULLPTR, PETSC_NULLPTR, name, value,
44 sizeof(value), &found));
45 if (!found) {
46 throw std::runtime_error("Expected PETSc string option to be set: " +
47 std::string(name));
48 }
49 return value;
50}
51
52bool readOptionalStringOption(const char *name, std::string &value) {
53 char buffer[PETSC_MAX_PATH_LEN] = "";
54 PetscBool found = PETSC_FALSE;
55 CHKERRABORT(PETSC_COMM_WORLD,
56 PetscOptionsGetString(PETSC_NULLPTR, PETSC_NULLPTR, name, buffer,
57 sizeof(buffer), &found));
58 if (!found)
59 return false;
60 value.assign(buffer);
61 return true;
62}
63
64bool hasOption(const char *name) {
65 PetscBool found = PETSC_FALSE;
66 CHKERRABORT(PETSC_COMM_WORLD,
67 PetscOptionsHasName(PETSC_NULLPTR, PETSC_NULLPTR, name, &found));
68 return found == PETSC_TRUE;
69}
70
71void assertNear(const double actual, const double expected, const double tol,
72 const char *message) {
73 if (std::abs(actual - expected) > tol) {
74 std::ostringstream oss;
75 oss << message << " expected=" << std::setprecision(16) << expected
76 << " actual=" << actual;
77 throw std::runtime_error(oss.str());
78 }
79}
80
81void assertAttributeValues(const std::vector<double> &attrs,
82 std::initializer_list<double> expected,
83 const char *label) {
84 if (attrs.size() != expected.size()) {
85 std::ostringstream oss;
86 oss << label << " attributes size mismatch expected=" << expected.size()
87 << " actual=" << attrs.size();
88 throw std::runtime_error(oss.str());
89 }
90
91 size_t index = 0;
92 for (const auto value : expected) {
93 std::ostringstream oss;
94 oss << label << " User" << index + 1;
95 assertNear(attrs[index], value, 1e-12, oss.str().c_str());
96 ++index;
97 }
98}
99
100void assertBlockAttributes(MoFEM::Interface &m_field, const int meshset_id,
101 std::initializer_list<double> expected,
102 const char *label) {
104 if (it->getMeshsetId() != meshset_id) {
105 continue;
106 }
107
108 std::vector<double> attrs;
109 CHKERRABORT(PETSC_COMM_WORLD, it->getAttributes(attrs));
110 assertAttributeValues(attrs, expected, label);
111 return;
112 }
113
114 throw std::runtime_error(std::string(label) + " BLOCKSET not found");
115}
116
117int assertBlockAttributesByName(MoFEM::Interface &m_field,
118 const std::string &meshset_name,
119 std::initializer_list<double> expected,
120 const char *label) {
122 if (it->getName() != meshset_name) {
123 continue;
124 }
125
126 std::vector<double> attrs;
127 CHKERRABORT(PETSC_COMM_WORLD, it->getAttributes(attrs));
128 assertAttributeValues(attrs, expected, label);
129 return it->getMeshsetId();
130 }
131
132 throw std::runtime_error(std::string(label) + " BLOCKSET not found by name");
133}
134
135} // namespace
136
137int main(int argc, char *argv[]) {
138
139 MoFEM::Core::Initialize(&argc, &argv, (char *)0, help);
140
141 try {
142 moab::Core mb_instance;
143 moab::Interface &moab = mb_instance;
144
145 MoFEM::Core core(moab);
146 MoFEM::Interface &m_field = core;
147 auto *json_config = m_field.getInterface<JsonConfigManager>();
148
149 CHKERR json_config->getSubInterfaceOptions();
150
151 const std::string expected_ksp_type =
152 readStringOption("-expected_ksp_type");
153 const std::string expected_pc_type = readStringOption("-expected_pc_type");
154 std::string expected_file_name;
155
156 if (readStringOption("-ksp_type") != expected_ksp_type) {
157 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
158 "Unexpected -ksp_type value");
159 }
160 if (readStringOption("-pc_type") != expected_pc_type) {
161 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
162 "Unexpected -pc_type value");
163 }
164 assertNear(readRealOption("-ksp_rtol"), 1e-8, 1e-14,
165 "Unexpected -ksp_rtol");
166 if (readIntOption("-order") != 2) {
167 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
168 "Unexpected -order value");
169 }
170 if (readIntOption("-atom_test") != 1) {
171 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
172 "Unexpected -atom_test value");
173 }
174 if (!hasOption("-log_no_color")) {
175 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
176 "Expected -log_no_color option to be set");
177 }
178 if (readOptionalStringOption("-expected_file_name", expected_file_name)) {
179 if (readStringOption("-file_name") != expected_file_name) {
180 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
181 "Unexpected -file_name value");
182 }
183 }
184
185 MeshsetsManager *meshsets_manager_ptr;
186 CHKERR m_field.getInterface(meshsets_manager_ptr);
187
188 CHKERR meshsets_manager_ptr->addMeshset(BLOCKSET, 1001, "elastic_domain");
189 CHKERR meshsets_manager_ptr->addMeshset(BLOCKSET, 1002, "trans_iso_domain");
190 CHKERR meshsets_manager_ptr->addMeshset(BLOCKSET, 1003, "thermal_domain");
191 CHKERR meshsets_manager_ptr->addMeshset(BLOCKSET, 1004, "moisture_domain");
192 CHKERR meshsets_manager_ptr->addMeshset(BLOCKSET, 1005, "interface_domain");
193 CHKERR meshsets_manager_ptr->addMeshset(BLOCKSET, 1006,
194 "bodyforces_domain");
195 CHKERR meshsets_manager_ptr->addMeshset(BLOCKSET, 1007, "generic_block");
196 CHKERR meshsets_manager_ptr->addMeshset(BLOCKSET, 1008,
197 "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
205 {"charge_density", "mobility", "scale"});
206 CHKERR meshsets_manager_ptr->setMeshsetFromFile();
207 CHKERR json_config->applyMeshsets();
208
209 const bool shared_types_same_blockset_test =
210 hasOption("-shared_types_same_blockset_test");
211 if (shared_types_same_blockset_test) {
212 const auto elastic_params =
213 json_config->getParamsFromBlockset("MAT_ELASTIC", 1001);
214 if (elastic_params.at("young") != 1000.0 ||
215 elastic_params.at("poisson") != 0.3) {
216 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
217 "Unexpected shared-block MAT_ELASTIC params from JSON");
218 }
219 const auto elastic_string_params =
220 json_config->getStringParamsFromBlockset("MAT_ELASTIC", 1001);
221 if (elastic_string_params.at("model_spec") != "incompressible") {
222 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
223 "Unexpected shared-block MAT_ELASTIC string params from JSON");
224 }
225
226 const auto thermal_params =
227 json_config->getParamsFromBlockset("MAT_THERMAL", 1001);
228 if (thermal_params.at("conductivity") != 11.0 ||
229 thermal_params.at("heatcapacity") != 22.0) {
230 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
231 "Unexpected shared-block MAT_THERMAL params from JSON");
232 }
233 const auto thermal_string_params =
234 json_config->getStringParamsFromBlockset("MAT_THERMAL", 1001);
235 if (thermal_string_params.at("load_history") !=
236 "~/thermal_load_case.txt") {
237 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
238 "Unexpected shared-block MAT_THERMAL string params from JSON");
239 }
240
241 const int elastic_id = assertBlockAttributesByName(
242 m_field, "MAT_ELASTIC_elastic_domain_1001", {1000.0, 0.3},
243 "MAT_ELASTIC");
244 const int thermal_id = assertBlockAttributesByName(
245 m_field, "MAT_THERMAL_elastic_domain_3003", {11.0, 22.0},
246 "MAT_THERMAL");
247 if (elastic_id == thermal_id) {
248 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
249 "Expected distinct blocksets for shared source meshset name");
250 }
251
252 const auto thermal_target_params =
253 json_config->getParamsFromBlockset("MAT_THERMAL", thermal_id);
254 if (thermal_target_params.at("conductivity") != 11.0 ||
255 thermal_target_params.at("heatcapacity") != 22.0) {
256 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
257 "Unexpected shared-block MAT_THERMAL params for target id");
258 }
259 const auto thermal_target_string_params =
260 json_config->getStringParamsFromBlockset("MAT_THERMAL", thermal_id);
261 if (thermal_target_string_params.at("load_history") !=
262 "~/thermal_load_case.txt") {
263 SETERRQ(
264 PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
265 "Unexpected shared-block MAT_THERMAL string params for target id");
266 }
267 }
268
269 if (!shared_types_same_blockset_test) {
270 const bool expect_multiple_python_scripts =
271 hasOption("-expect_multiple_python_scripts");
272 if (json_config->getMoabMeshByKey("primary") != "json_primary_mesh.h5m") {
273 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
274 "Unexpected keyed MOAB mesh lookup result");
275 }
276 if (!json_config->getMoabMeshByKey("missing_mesh").empty()) {
277 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
278 "Unexpected missing keyed MOAB mesh lookup result");
279 }
280 if (expect_multiple_python_scripts) {
281 if (json_config->getMoabMeshByKey("background_mesh") !=
282 "rectangle_quad.h5m") {
283 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
284 "Unexpected auxiliary keyed MOAB mesh lookup result");
285 }
286 if (json_config->getPythonScriptByKey("python_script_1") !=
287 "./sdf_files/sdf_hertz_2d_plane.py" ||
288 json_config->getPythonScriptByKey("python_script_2") !=
289 "./sdf_files/sdf_hertz_2d_plane_2.py") {
290 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
291 "Unexpected keyed Python script lookup result");
292 }
293 } else {
294 if (json_config->getPythonScriptByKey("python_script") !=
295 "./sdf_files/sdf_hertz_2d_plane.py") {
296 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
297 "Unexpected keyed Python script lookup result");
298 }
299 }
300 if (!json_config->getPythonScriptByKey("missing_script").empty()) {
301 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
302 "Unexpected missing keyed Python script lookup result");
303 }
304
305 const auto elastic_params =
306 json_config->getParamsFromBlockset("hencky", 1001);
307 if (elastic_params.at("young") != 1000.0 ||
308 elastic_params.at("poisson") != 0.3) {
309 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
310 "Unexpected hencky params from JSON");
311 }
312 if (hasOption("-expect_string_params")) {
313 const auto elastic_string_params =
314 json_config->getStringParamsFromBlockset("hencky", 1001);
315 if (elastic_string_params.at("model_spec") != "incompressible" ||
316 elastic_string_params.at("load_history") != "~/load_case1.txt") {
317 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
318 "Unexpected hencky string params from JSON");
319 }
320 }
321
322 const auto generic_params =
323 json_config->getParamsFromBlockset("GENERIC_BLOCK", 1007);
324 if (generic_params.at("user1") != 9.0 ||
325 generic_params.at("user1b") != 8.0) {
326 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
327 "Unexpected GENERIC_BLOCK params from JSON");
328 }
329
330 const auto int_electric_custom_params =
331 json_config->getParamsFromBlockset("INT_ELECTRIC_CUSTOM", 1008);
332 if (int_electric_custom_params.at("charge_density") != 12.0 ||
333 int_electric_custom_params.at("mobility") != 13.0 ||
334 int_electric_custom_params.at("scale") != 14.0) {
335 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
336 "Unexpected INT_ELECTRIC_CUSTOM params from JSON");
337 }
338
339 const auto mooney_rivlin_params =
340 json_config->getParamsFromBlockset("MAT_MOONEY_RIVLIN", 1004);
341 const bool has_mooney_rivlin_params = !mooney_rivlin_params.empty();
342
343 const auto displacement_params =
344 json_config->getParamsFromBlockset("DISPLACEMENT", 2001);
345 if (displacement_params.at("flag1") != 1.0 ||
346 displacement_params.at("flag2") != 1.0 ||
347 displacement_params.at("flag3") != 0.0 ||
348 displacement_params.at("value3") != 3.0) {
349 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
350 "Unexpected DISPLACEMENT params from JSON");
351 }
352
353 const auto force_params =
354 json_config->getParamsFromBlockset("FORCE", 2002);
355 if (force_params.at("force_magnitude") != 10.0 ||
356 force_params.at("moment_magnitude") != 11.0 ||
357 force_params.at("moment_my") != 1.0) {
358 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
359 "Unexpected FORCE params from JSON");
360 }
361
362 const auto temperature_params =
363 json_config->getParamsFromBlockset("TEMPERATURE", 2003);
364 if (temperature_params.at("flag1") != 1.0 ||
365 temperature_params.at("value1") != 123.0) {
366 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
367 "Unexpected TEMPERATURE params from JSON");
368 }
369
370 const auto pressure_params =
371 json_config->getParamsFromBlockset("PRESSURE", 3001);
372 if (pressure_params.at("flag2") != 1.0 ||
373 pressure_params.at("value1") != 12.5) {
374 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
375 "Unexpected PRESSURE params from JSON");
376 }
377
378 const auto heatflux_params =
379 json_config->getParamsFromBlockset("HEATFLUX", 3002);
380 if (heatflux_params.at("flag1") != 1.0 ||
381 heatflux_params.at("value1") != 99.0) {
382 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
383 "Unexpected HEATFLUX params from JSON");
384 }
385
386 if (!json_config->getParamsFromBlockset("UNKNOWN_TYPE", 1007).empty()) {
387 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
388 "Unexpected params returned for unknown blockset type");
389 }
390 if (!json_config->getStringParamsFromBlockset("UNKNOWN_TYPE", 1007)
391 .empty()) {
392 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
393 "Unexpected string params returned for unknown blockset type");
394 }
395
396 assertBlockAttributes(m_field, 1001, {1000.0, 0.3, 1e-5, 7.0}, "hencky");
397 assertBlockAttributes(m_field, 1002, {1.0, 2.0, 3.0, 4.0, 5.0},
398 "MAT_ELASTIC_TRANSISO");
399 assertBlockAttributes(m_field, 1003, {11.0, 22.0}, "MAT_THERMAL");
400 if (has_mooney_rivlin_params) {
401 assertBlockAttributes(m_field, 1004, {2.0, 3.0, 4.0, 0.5},
402 "MAT_MOONEY_RIVLIN");
403 }
404 assertBlockAttributes(m_field, 1005, {10.0, 20.0, 30.0, 40.0},
405 "MAT_INTERF");
406 assertBlockAttributes(m_field, 1006, {100.0, 1.0, 2.0, 3.0},
407 "BODYFORCES");
408 assertBlockAttributes(m_field, 1007, {9.0}, "GENERIC_BLOCK");
409 assertBlockAttributes(m_field, 1008, {12.0, 13.0, 14.0},
410 "INT_ELECTRIC_CUSTOM");
411 assertBlockAttributes(m_field, 2001, {1.0, 1.0, 0.0, 1.0, 2.0, 3.0},
412 "DISPLACEMENT");
413 assertBlockAttributes(
414 m_field, 2002, {10.0, 11.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0}, "FORCE");
415 assertBlockAttributes(m_field, 2003, {1.0, 123.0}, "TEMPERATURE");
416 assertBlockAttributes(m_field, 3001, {1.0, 12.5}, "PRESSURE");
417 assertBlockAttributes(m_field, 3002, {1.0, 99.0}, "HEATFLUX");
418 }
419 }
421
423}
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[]
double tol
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.
static MoFEMErrorCode addCanonicalAttributeNames(const std::vector< std::string > &names)
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.