v0.16.0
Loading...
Searching...
No Matches
JsonConfigManagerMeshes.cpp
Go to the documentation of this file.
1constexpr auto MOAB_MESH_TYPE = "MOAB_MESH";
2constexpr auto PYTHON_SCRIPT_TYPE = "PYTHON_SCRIPT";
3constexpr auto MESHES_KEY = "meshes";
4constexpr auto INPUT_FILES_KEY = "input_files";
5
6bool isPythonScriptMeshType(const std::string &type) {
7 return type == PYTHON_SCRIPT_TYPE;
8}
9
10std::string meshContext(const size_t mesh_index,
11 const char *section_key = MESHES_KEY) {
12 return "root." + std::string(section_key) + "[" +
13 boost::lexical_cast<std::string>(mesh_index) + "]";
14}
15
16const char *getMeshSectionKey(const JsonObject &root_object,
17 const std::string &file_name) {
18 const auto meshes_it = root_object.find(MESHES_KEY);
19 const auto input_files_it = root_object.find(INPUT_FILES_KEY);
20 if (meshes_it != root_object.end() && input_files_it != root_object.end()) {
21 CHKERRABORT(PETSC_COMM_SELF,
22 failConfig(file_name, "root.input_files",
23 "cannot be used together with root.meshes"));
24 }
25
26 if (meshes_it != root_object.end())
27 return MESHES_KEY;
28 if (input_files_it != root_object.end())
29 return INPUT_FILES_KEY;
30 return nullptr;
31}
32
33const JsonArray *getMeshesArray(const JsonObject &root_object,
34 const std::string &file_name,
35 const char **section_key = nullptr) {
36 const auto *selected_key = getMeshSectionKey(root_object, file_name);
37 if (section_key)
38 *section_key = selected_key;
39 if (!selected_key)
40 return nullptr;
41
42 const auto section_it = root_object.find(selected_key);
43 return &requireArray(section_it->value(), file_name,
44 makeContext("root", selected_key));
45}
46
47const JsonArray *getMeshsetsArray(const JsonObject &root_object,
48 const std::string &file_name,
49 const size_t mesh_index) {
50 const char *section_key = nullptr;
51 const auto *meshes_array =
52 getMeshesArray(root_object, file_name, &section_key);
53 if (!meshes_array)
54 return nullptr;
55
56 if (mesh_index >= meshes_array->size()) {
57 CHKERRABORT(PETSC_COMM_SELF,
58 failConfig(file_name, meshContext(mesh_index, section_key),
59 "mesh index out of range"));
60 }
61
62 const auto context = meshContext(mesh_index, section_key);
63 const auto &mesh_object =
64 requireObject((*meshes_array)[mesh_index], file_name, context);
65 if (const auto meshsets_it = mesh_object.find("meshsets");
66 meshsets_it != mesh_object.end()) {
67 return &requireArray(meshsets_it->value(), file_name,
68 makeContext(context, "meshsets"));
69 }
70
71 return nullptr;
72}
73
74MoFEMErrorCode parseMeshEntryField(const std::string &field_name,
75 const JsonValue &value,
76 const std::string &file_name,
77 const std::string &context,
78 ParsedMeshEntry &entry,
79 bool &has_non_empty_meshsets) {
81 if (field_name == "key") {
82 entry.key = requireString(value, file_name, makeContext(context, field_name));
83 } else if (field_name == "type") {
84 entry.type = requireString(value, file_name, makeContext(context, field_name));
85 } else if (field_name == "file_name") {
86 entry.fileName =
87 requireString(value, file_name, makeContext(context, field_name));
88 } else if (field_name == "meshsets") {
89 const auto &meshsets =
90 requireArray(value, file_name, makeContext(context, field_name));
91 has_non_empty_meshsets = !meshsets.empty();
92 } else {
93 CHKERR failConfig(file_name, makeContext(context, field_name),
94 "unknown input file field");
95 }
97}
98
99MoFEMErrorCode validateMeshEntry(const ParsedMeshEntry &entry,
100 const std::string &file_name,
101 const std::string &context,
102 const size_t mesh_index) {
104 if (entry.type.empty()) {
105 CHKERR failConfig(file_name, makeContext(context, "type"),
106 "missing required mesh type");
107 }
108 if (entry.fileName.empty()) {
109 CHKERR failConfig(file_name, makeContext(context, "file_name"),
110 "missing required file_name");
111 }
112
113 if (entry.type != MOAB_MESH_TYPE && !isPythonScriptMeshType(entry.type)) {
114 CHKERR failConfig(file_name, makeContext(context, "type"),
115 "unsupported mesh type '" + entry.type + "'");
116 }
117
118 if (mesh_index == 0 && entry.type != MOAB_MESH_TYPE) {
119 CHKERR failConfig(file_name, makeContext(context, "type"),
120 "primary runtime mesh must be " +
121 std::string(MOAB_MESH_TYPE));
122 }
123
124 if (isPythonScriptMeshType(entry.type) && entry.key.empty()) {
125 CHKERR failConfig(file_name, makeContext(context, "key"),
126 std::string(PYTHON_SCRIPT_TYPE) +
127 " requires a unique key");
128 }
129
131}
132
133MoFEMErrorCode collectMeshes(const JsonObject &root_object,
134 const std::string &file_name,
135 std::vector<ParsedMeshEntry> &meshes,
136 bool *has_meshsets) {
138 meshes.clear();
139 if (has_meshsets)
140 *has_meshsets = false;
141
142 const char *section_key = nullptr;
143 if (const auto *meshes_array =
144 getMeshesArray(root_object, file_name, &section_key);
145 meshes_array) {
146 meshes.reserve(meshes_array->size());
147 for (size_t i = 0; i != meshes_array->size(); ++i) {
148 const auto context = meshContext(i, section_key);
149 const auto &mesh_object =
150 requireObject((*meshes_array)[i], file_name, context);
151
152 ParsedMeshEntry entry;
153 bool has_non_empty_meshsets = false;
154 for (const auto &item : mesh_object) {
155 CHKERR parseMeshEntryField(toStdString(item.key()), item.value(),
156 file_name, context, entry,
157 has_non_empty_meshsets);
158 }
159
160 CHKERR validateMeshEntry(entry, file_name, context, i);
161 if (has_meshsets && has_non_empty_meshsets) {
162 *has_meshsets = true;
163 }
164 meshes.push_back(std::move(entry));
165 }
166 }
167
168 std::set<std::string> moab_mesh_keys;
169 std::set<std::string> python_script_keys;
170 for (const auto &entry : meshes) {
171 if (entry.type == MOAB_MESH_TYPE) {
172 if (entry.key.empty()) {
173 continue;
174 }
175 const auto inserted = moab_mesh_keys.insert(entry.key);
176 if (!inserted.second) {
177 CHKERR failConfig(file_name, makeContext("root", section_key),
178 "duplicate " + std::string(MOAB_MESH_TYPE) +
179 " key '" + entry.key + "'");
180 }
181 continue;
182 }
183 if (isPythonScriptMeshType(entry.type)) {
184 const auto inserted = python_script_keys.insert(entry.key);
185 if (!inserted.second) {
186 CHKERR failConfig(file_name, makeContext("root", section_key),
187 "duplicate " + std::string(PYTHON_SCRIPT_TYPE) +
188 " key '" + entry.key + "'");
189 }
190 }
191 }
192
194}
std::string meshContext(const size_t mesh_index, const char *section_key=MESHES_KEY)
const JsonArray * getMeshsetsArray(const JsonObject &root_object, const std::string &file_name, const size_t mesh_index)
constexpr auto MESHES_KEY
MoFEMErrorCode validateMeshEntry(const ParsedMeshEntry &entry, const std::string &file_name, const std::string &context, const size_t mesh_index)
MoFEMErrorCode collectMeshes(const JsonObject &root_object, const std::string &file_name, std::vector< ParsedMeshEntry > &meshes, bool *has_meshsets)
constexpr auto INPUT_FILES_KEY
bool isPythonScriptMeshType(const std::string &type)
const char * getMeshSectionKey(const JsonObject &root_object, const std::string &file_name)
MoFEMErrorCode parseMeshEntryField(const std::string &field_name, const JsonValue &value, const std::string &file_name, const std::string &context, ParsedMeshEntry &entry, bool &has_non_empty_meshsets)
const JsonArray * getMeshesArray(const JsonObject &root_object, const std::string &file_name, const char **section_key=nullptr)
constexpr auto MOAB_MESH_TYPE
constexpr auto PYTHON_SCRIPT_TYPE
std::string type
#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
constexpr auto field_name