Debian Codegraph @

packages/seacas/libraries/ioss/src/catalyst_tests/Iocatalyst_BlockMeshSet.C

raw
1 // Copyright(C) 1999-2020 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6
7 #include <Ioss_CopyDatabase.h>
8 #include <Ioss_DatabaseIO.h>
9 #include <Ioss_ElementBlock.h>
10 #include <Ioss_IOFactory.h>
11 #include <Ioss_MeshCopyOptions.h>
12 #include <Ioss_NodeBlock.h>
13 #include <Ioss_StructuredBlock.h>
14 #include <Ioss_Utils.h>
15 #include <catalyst/Iocatalyst_CatalystManager.h>
16 #include <catalyst/Iocatalyst_DatabaseIO.h>
17 #include <catalyst_tests/Iocatalyst_BlockMeshSet.h>
18 #include <unordered_set>
19
20 namespace Iocatalyst {
21
22 void BlockMeshSet::addBlockMesh(const BlockMesh &blockMesh) { bms.push_back(blockMesh); }
23
24 int BlockMeshSet::getNumLocalPointsInMeshSet()
25 {
26 std::unordered_set<BlockMesh::ID> pids;
27 for (auto bm : bms) {
28 BlockMesh::IDList ids = bm.getPartitionPointIDs();
29 for (auto id : ids) {
30 auto gid = bm.getGlobalIDForPointID(id);
31 if (pids.find(gid) == pids.end()) {
32 pids.insert(gid);
33 }
34 }
35 }
36 return pids.size();
37 }
38
39 void BlockMeshSet::writeCatalystIOSSFile(IOSSparams &iop)
40 {
41 CatalystManager::getInstance().reset();
42 iop.isCatalyst = true;
43
44 // Cat Writes
45 writeIOSSFile(iop);
46 Ioss::PropertyManager cdbProps = Ioss::PropertyManager(iop.dbProps);
47 cdbProps.add(Ioss::Property("CATALYST_CONDUIT_NODE", iop.getCatalystConduitNode()));
48
49 // Cat Reads here
50 Ioss::DatabaseIO *cdbi =
51 Ioss::IOFactory::create(CATALYST_DATABASE_TYPE, CATALYST_DUMMY_DATABASE, Ioss::READ_RESTART,
52 Ioss::ParallelUtils::comm_world(), cdbProps);
53 if (cdbi == nullptr || !cdbi->ok(true)) {
54 return;
55 }
56 Ioss::PropertyManager properties = Ioss::PropertyManager(iop.dbProps);
57 Ioss::DatabaseIO *cdbo = Ioss::IOFactory::create(iop.dbType, iop.fileName, Ioss::WRITE_RESULTS,
58 Ioss::ParallelUtils::comm_world(), properties);
59
60 if (cdbo == nullptr || !cdbo->ok(true)) {
61 std::ostringstream errmsg;
62 errmsg << "Unable to open IOSS database " + iop.fileName + "\n";
63 IOSS_ERROR(errmsg);
64 }
65
66 Ioss::Region cor(cdbo);
67 Ioss::Region cir(cdbi);
68 Ioss::MeshCopyOptions options;
69 options.data_storage_type = 1;
70 Ioss::copy_database(cir, cor, options);
71 }
72
73 Ioss::DatabaseIO *BlockMeshSet::getCatalystDatabase(IOSSparams &iop)
74 {
75 CatalystManager::getInstance().reset();
76 iop.isCatalyst = true;
77
78 // Write to Cat database
79 writeIOSSFile(iop);
80
81 Ioss::PropertyManager cdbProps = Ioss::PropertyManager(iop.dbProps);
82
83 // Get Conduit
84 cdbProps.add(Ioss::Property("CATALYST_CONDUIT_NODE", iop.getCatalystConduitNode()));
85
86 // Read to Cat Database
87 Ioss::DatabaseIO *cdbi =
88 Ioss::IOFactory::create(CATALYST_DATABASE_TYPE, CATALYST_DUMMY_DATABASE, Ioss::READ_RESTART,
89 Ioss::ParallelUtils::comm_world(), cdbProps);
90 if (cdbi == nullptr || !cdbi->ok(true)) {
91 return nullptr;
92 }
93
94 return cdbi;
95 }
96
97 void BlockMeshSet::writeIOSSFile(IOSSparams &iop)
98 {
99 openIOSSDatabase(iop);
100 switchStateDefineModel(iop);
101 switchStateModel(iop);
102 switchStateDefineTransient(iop);
103 switchStateTransient(iop);
104 closeIOSSDatabase(iop);
105 }
106
107 void BlockMeshSet::openIOSSDatabase(IOSSparams &iop)
108 {
109 Ioss::PropertyManager properties = Ioss::PropertyManager(iop.dbProps);
110 std::string dbType = iop.dbType;
111 if (iop.isCatalyst) {
112 dbType = CATALYST_DATABASE_TYPE;
113 }
114 iop.databaseIO = Ioss::IOFactory::create(dbType, iop.fileName, Ioss::WRITE_RESULTS,
115 Ioss::ParallelUtils::comm_world(), properties);
116
117 if (iop.databaseIO == nullptr || !iop.databaseIO->ok(true)) {
118 std::ostringstream errmsg;
119 errmsg << "Unable to open IOSS database " + iop.fileName + "\n";
120 IOSS_ERROR(errmsg);
121 }
122 iop.region = std::unique_ptr<Ioss::Region>(new Ioss::Region(iop.databaseIO));
123 }
124
125 void BlockMeshSet::closeIOSSDatabase(IOSSparams &iop)
126 {
127 iop.region.reset();
128 iop.databaseIO = nullptr;
129 }
130
131 void BlockMeshSet::switchStateDefineModel(IOSSparams &iop)
132 {
133 iop.region->begin_mode(Ioss::STATE_DEFINE_MODEL);
134 if (iop.isStructured()) {
135 writeStructuredBlockDefinitions(iop);
136 }
137 else {
138 writeUnstructuredBlockDefinitions(iop);
139 }
140 iop.region->end_mode(Ioss::STATE_DEFINE_MODEL);
141 }
142
143 void BlockMeshSet::switchStateModel(IOSSparams &iop)
144 {
145 iop.region->begin_mode(Ioss::STATE_MODEL);
146 if (iop.isStructured()) {
147 writeStructuredBlockBulkData(iop);
148 }
149 else {
150 writeUnstructuredBlockBulkData(iop);
151 }
152 if (iop.isCatalyst) {
153 saveConduitNode(iop);
154 }
155 iop.region->end_mode(Ioss::STATE_MODEL);
156 }
157
158 void BlockMeshSet::switchStateDefineTransient(IOSSparams &iop)
159 {
160 iop.region->begin_mode(Ioss::STATE_DEFINE_TRANSIENT);
161 if (iop.isStructured()) {
162 writeStructuredTransientFieldDefinitions(iop);
163 }
164 else {
165 writeUnstructuredTransientFieldDefinitions(iop);
166 }
167 iop.region->end_mode(Ioss::STATE_DEFINE_TRANSIENT);
168 }
169 void BlockMeshSet::switchStateTransient(IOSSparams &iop)
170 {
171 iop.region->begin_mode(Ioss::STATE_TRANSIENT);
172 int tstep = iop.region->add_state(0.0);
173 iop.region->begin_state(tstep);
174 if (iop.isStructured()) {
175 writeStructuredTransientBulkData(iop);
176 }
177 else {
178 writeUnstructuredTransientBulkData(iop);
179 }
180 iop.region->end_state(tstep);
181 if (iop.isCatalyst) {
182 saveConduitNode(iop);
183 }
184 iop.region->end_mode(Ioss::STATE_TRANSIENT);
185 }
186
187 void BlockMeshSet::saveConduitNode(IOSSparams &iop)
188 {
189 auto c_node = reinterpret_cast<conduit_node *>(
190 ((Iocatalyst::DatabaseIO *)iop.databaseIO)->get_catalyst_conduit_node());
191 auto cpp_node = conduit_cpp::cpp_node(c_node);
192 iop.conduitNode.set(cpp_node);
193 }
194
195 void BlockMeshSet::writeStructuredBlockDefinitions(IOSSparams &iop)
196 {
197 int spatialDims = 3;
198 for (auto bm : bms) {
199 Ioss::IJK_t parentOffsets = {{(int)bm.getPartitionStart().i, (int)bm.getPartitionStart().j,
200 (int)bm.getPartitionStart().k}};
201 Ioss::IJK_t globalSizes = {
202 {(int)bm.getExtents().i, (int)bm.getExtents().j, (int)bm.getExtents().k}};
203 Ioss::IJK_t localSizes = {{(int)bm.getPartitionExtents().i, (int)bm.getPartitionExtents().j,
204 (int)bm.getPartitionExtents().k}};
205 Ioss::StructuredBlock *iossBlock =
206 new Ioss::StructuredBlock(iop.databaseIO, getStructuredBlockName(bm.getID()), spatialDims,
207 localSizes, parentOffsets, globalSizes);
208 int node_count = (bm.getPartitionExtents().i + 1) * (bm.getPartitionExtents().j + 1) *
209 (bm.getPartitionExtents().k + 1);
210 Ioss::NodeBlock *nodeBlock = new Ioss::NodeBlock(
211 iop.databaseIO, getStructuredNodeBlockName(bm.getID()), node_count, spatialDims);
212 iop.region->add(iossBlock);
213 iop.region->add(nodeBlock);
214 }
215 }
216
217 void BlockMeshSet::writeStructuredBlockBulkData(IOSSparams &iop)
218 {
219 std::vector<double> coordx;
220 std::vector<double> coordy;
221 std::vector<double> coordz;
222 BlockMesh::Point origin;
223 origin.x = 0.0;
224 origin.y = 0.0;
225 origin.z = 0.0;
226 for (auto bm : bms) {
227 const int numI = bm.getPartitionExtents().i + 1;
228 const int numJ = bm.getPartitionExtents().j + 1;
229 const int numK = bm.getPartitionExtents().k + 1;
230 const int numPoints = numI * numJ * numK;
231
232 coordx.resize(numPoints);
233 coordy.resize(numPoints);
234 coordz.resize(numPoints);
235
236 for (int k = 0; k < numK; ++k) {
237 const int kOffset = k * numI * numJ;
238 for (int j = 0; j < numJ; ++j) {
239 const int kjOffset = kOffset + j * numI;
240 for (int i = 0; i < numI; ++i) {
241 coordx[kjOffset + i] =
242 i * bm.BLOCK_LENGTH + bm.getOrigin().i + bm.getPartitionStart().i;
243 coordy[kjOffset + i] =
244 j * bm.BLOCK_LENGTH + bm.getOrigin().j + bm.getPartitionStart().j;
245 coordz[kjOffset + i] =
246 k * bm.BLOCK_LENGTH + bm.getOrigin().k + bm.getPartitionStart().k;
247 }
248 }
249 }
250
251 auto iossBlock = iop.region->get_structured_block(getStructuredBlockName(bm.getID()));
252 iossBlock->put_field_data("mesh_model_coordinates_x", coordx);
253 iossBlock->put_field_data("mesh_model_coordinates_y", coordy);
254 iossBlock->put_field_data("mesh_model_coordinates_z", coordz);
255 }
256 }
257
258 void BlockMeshSet::writeStructuredTransientFieldDefinitions(IOSSparams &iop)
259 {
260 for (auto bm : bms) {
261 // Modify this to access field dict in "bm" and populate ioss block with those fields (as
262 // well).
263 auto iossBlock = iop.region->get_structured_block(getStructuredBlockName(bm.getID()));
264 iossBlock->field_add(Ioss::Field(IOSS_CELL_FIELD, Ioss::Field::REAL, IOSS_SCALAR_STORAGE,
265 Ioss::Field::TRANSIENT));
266 iossBlock->get_node_block().field_add(Ioss::Field(
267 IOSS_POINT_FIELD, Ioss::Field::REAL, IOSS_SCALAR_STORAGE, Ioss::Field::TRANSIENT));
268 }
269 }
270
271 void BlockMeshSet::writeStructuredTransientBulkData(IOSSparams &iop)
272 {
273
274 std::vector<double> values;
275 for (auto bm : bms) {
276 values.clear();
277 auto iossBlock = iop.region->get_structured_block(getStructuredBlockName(bm.getID()));
278 for (int j = 0; j < iossBlock->get_field(IOSS_CELL_FIELD).raw_count(); j++) {
279 values.push_back(bm.getPartition().id);
280 }
281 iossBlock->put_field_data(IOSS_CELL_FIELD, values);
282
283 values.clear();
284 for (int j = 0; j < iossBlock->get_node_block().get_field(IOSS_POINT_FIELD).raw_count();
285 j++) {
286 values.push_back(bm.getPartition().id);
287 }
288 iossBlock->get_node_block().put_field_data(IOSS_POINT_FIELD, values);
289 }
290 }
291
292 void BlockMeshSet::writeUnstructuredBlockDefinitions(IOSSparams &iop)
293 {
294 int spatialDims = 3;
295 Ioss::NodeBlock *nodeBlock =
296 new Ioss::NodeBlock(iop.databaseIO, "nodeblock", getNumLocalPointsInMeshSet(), spatialDims);
297 iop.region->add(nodeBlock);
298 for (auto bm : bms) {
299 Ioss::ElementBlock *elemBlock = new Ioss::ElementBlock(
300 iop.databaseIO, getUnstructuredBlockName(bm.getID()), "hex8", bm.getNumPartitionBlocks());
301 iop.region->add(elemBlock);
302 }
303 }
304
305 void BlockMeshSet::writeUnstructuredBlockBulkData(IOSSparams &iop)
306 {
307 Ioss::NodeBlock *nodeBlock = iop.region->get_node_block("nodeblock");
308 std::vector<double> coordx;
309 std::vector<double> coordy;
310 std::vector<double> coordz;
311 BlockMesh::IDList globalPointIds;
312 std::unordered_set<BlockMesh::ID> pids;
313 for (auto bm : bms) {
314 BlockMesh::IDList ids = bm.getPartitionPointIDs();
315 for (auto id : ids) {
316 auto gid = bm.getGlobalIDForPointID(id);
317 if (pids.find(gid) == pids.end()) {
318 BlockMesh::Point point = bm.getPointCoordsForPointID(id);
319 coordx.push_back(point.x);
320 coordy.push_back(point.y);
321 coordz.push_back(point.z);
322 globalPointIds.push_back(gid);
323 pids.insert(gid);
324 }
325 }
326 }
327 nodeBlock->put_field_data("mesh_model_coordinates_x", coordx);
328 nodeBlock->put_field_data("mesh_model_coordinates_y", coordy);
329 nodeBlock->put_field_data("mesh_model_coordinates_z", coordz);
330 nodeBlock->put_field_data("ids", globalPointIds);
331
332 for (auto bm : bms) {
333 Ioss::ElementBlock *elemBlock =
334 iop.region->get_element_block(getUnstructuredBlockName(bm.getID()));
335
336 std::vector<int> connectivity(8 * bm.getNumPartitionBlocks());
337 BlockMesh::IDList globalElemIds;
338 BlockMesh::IDList ids = bm.getPartitionBlockIDs();
339
340 for (int i = 0; i < ids.size(); i++) {
341 BlockMesh::BlockConn conn = bm.getBlockConnectivityPointIDs(ids[i]);
342 globalElemIds.push_back(bm.getGlobalIDForBlockID(ids[i]));
343 for (int j = 0; j < conn.size(); j++) {
344 connectivity[(i * 8) + j] = bm.getGlobalIDForPointID(conn[j]);
345 }
346 }
347 elemBlock->put_field_data("connectivity", connectivity);
348 elemBlock->put_field_data("ids", globalElemIds);
349 }
350 }
351
352 void BlockMeshSet::writeUnstructuredTransientFieldDefinitions(IOSSparams &iop)
353 {
354 iop.region->field_add(Ioss::Field(IOSS_GLOBAL_FIELD, Ioss::Field::REAL, IOSS_SCALAR_STORAGE,
355 Ioss::Field::TRANSIENT));
356 for (auto bm : bms) {
357 auto elemBlock = iop.region->get_element_block(getUnstructuredBlockName(bm.getID()));
358 elemBlock->field_add(Ioss::Field(IOSS_CELL_FIELD, Ioss::Field::REAL, IOSS_SCALAR_STORAGE,
359 Ioss::Field::TRANSIENT));
360 auto nodeBlock = iop.region->get_node_block("nodeblock");
361 nodeBlock->field_add(Ioss::Field(IOSS_POINT_FIELD, Ioss::Field::REAL, IOSS_SCALAR_STORAGE,
362 Ioss::Field::TRANSIENT));
363
364 writeUnstructuredAddedTransientFields(bm, iop);
365 }
366 }
367
368 void BlockMeshSet::writeUnstructuredAddedTransientFields(BlockMesh bm, IOSSparams &iop)
369 {
370 writeUnstructuredAddedCellTransientFields(bm, iop);
371 writeUnstructuredAddedPointTransientFields(bm, iop);
372 }
373
374 void BlockMeshSet::writeUnstructuredAddedCellTransientFields(BlockMesh bm, IOSSparams &iop)
375 {
376 auto cell_fields = bm.getTransientCellFieldMap();
377 auto elemBlock = iop.region->get_element_block(getUnstructuredBlockName(bm.getID()));
378 for (auto itr = cell_fields->begin(); itr != cell_fields->end(); ++itr) {
379 elemBlock->field_add(
380 Ioss::Field(itr->first, Ioss::Field::REAL, IOSS_SCALAR_STORAGE, Ioss::Field::TRANSIENT));
381 }
382 }
383
384 void BlockMeshSet::writeUnstructuredAddedPointTransientFields(BlockMesh bm, IOSSparams &iop)
385 {
386 auto point_fields = bm.getTransientPointFieldMap();
387 auto nodeBlock = iop.region->get_node_block("nodeblock");
388 for (auto itr = point_fields->begin(); itr != point_fields->end(); ++itr) {
389 nodeBlock->field_add(
390 Ioss::Field(itr->first, Ioss::Field::REAL, IOSS_SCALAR_STORAGE, Ioss::Field::TRANSIENT));
391 }
392 }
393
394 void BlockMeshSet::writeUnstructuredTransientBulkData(IOSSparams &iop)
395 {
396 std::vector<double> values;
397 values.push_back(3.14);
398 iop.region->put_field_data(IOSS_GLOBAL_FIELD, values);
399 for (auto bm : bms) {
400 values.clear();
401 auto elemBlock = iop.region->get_element_block(getUnstructuredBlockName(bm.getID()));
402 for (int j = 0; j < elemBlock->get_field(IOSS_CELL_FIELD).raw_count(); j++) {
403 values.push_back(bm.getPartition().id);
404 }
405 elemBlock->put_field_data(IOSS_CELL_FIELD, values);
406
407 auto nodeBlock = iop.region->get_node_block("nodeblock");
408 values.clear();
409 for (int j = 0; j < nodeBlock->get_field(IOSS_POINT_FIELD).raw_count(); j++) {
410 values.push_back(bm.getPartition().id);
411 }
412 nodeBlock->put_field_data(IOSS_POINT_FIELD, values);
413
414 writeUnstructuredAddedTransientFieldsBulkData(bm, iop);
415 }
416 }
417
418 void BlockMeshSet::writeUnstructuredAddedTransientFieldsBulkData(BlockMesh bm, IOSSparams &iop)
419 {
420 writeUnstructuredAddedCellTransientFieldsBulkData(bm, iop);
421 writeUnstructuredAddedPointTransientFieldsBulkData(bm, iop);
422 }
423
424 void BlockMeshSet::writeUnstructuredAddedCellTransientFieldsBulkData(BlockMesh bm,
425 IOSSparams &iop)
426 {
427 auto cell_fields = bm.getTransientCellFieldMap();
428 auto elemBlock = iop.region->get_element_block(getUnstructuredBlockName(bm.getID()));
429 std::vector<double> values;
430 for (auto itr = cell_fields->begin(); itr != cell_fields->end(); ++itr) {
431 int num_elements = elemBlock->get_field(itr->first).raw_count();
432 for (int j = 0; j < num_elements; j++) {
433 values.push_back(itr->second + j * 0.1);
434 }
435 elemBlock->put_field_data(itr->first, values);
436 values.clear();
437 }
438 }
439
440 void BlockMeshSet::writeUnstructuredAddedPointTransientFieldsBulkData(BlockMesh bm,
441 IOSSparams &iop)
442 {
443 auto point_fields = bm.getTransientPointFieldMap();
444 auto nodeBlock = iop.region->get_node_block("nodeblock");
445 std::vector<double> values;
446 for (auto itr = point_fields->begin(); itr != point_fields->end(); ++itr) {
447 int num_nodes = nodeBlock->get_field(itr->first).raw_count();
448 for (int j = 0; j < num_nodes; j++) {
449 values.push_back(itr->second + j * 0.1);
450 }
451 nodeBlock->put_field_data(itr->first, values);
452 values.clear();
453 }
454 }
455
456 std::string BlockMeshSet::getStructuredBlockName(int index)
457 {
458 return "StructuredBlock" + std::to_string(index);
459 }
460
461 std::string BlockMeshSet::getStructuredNodeBlockName(int index)
462 {
463 return "StructuredNodeBlock" + std::to_string(index);
464 }
465
466 std::string BlockMeshSet::getUnstructuredBlockName(int index)
467 {
468 return "UnstructuredBlock" + std::to_string(index);
469 }
470
471 } // namespace Iocatalyst