Debian Codegraph @

packages/seacas/libraries/ioss/src/text_mesh/Iotm_DatabaseIO.C

raw
1 // Copyright(C) 1999-2020, 2022, 2023 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 "Iotm_DatabaseIO.h"
8
9 #include "Ioss_CodeTypes.h" // for Int64Vector, IntVector
10 #include "Ioss_SideBlock.h" // for SideBlock
11 #include "Ioss_SmartAssert.h"
12 #include <cassert> // for assert
13 #include <cmath> // for sqrt
14 #include <fmt/format.h>
15 #include <fmt/ostream.h>
16 #include <iostream> // for ostringstream
17 #include <stdlib.h>
18 #include <string> // for string, operator==, etc
19 #include <vector>
20
21 #include "Ioss_Assembly.h" // for Assembly
22 #include "Ioss_CommSet.h" // for CommSet
23 #include "Ioss_DBUsage.h" // for DatabaseUsage
24 #include "Ioss_DatabaseIO.h" // for DatabaseIO
25 #include "Ioss_ElementBlock.h" // for ElementBlock
26 #include "Ioss_EntityType.h" // for EntityType, etc
27 #include "Ioss_Field.h" // for Field, etc
28 #include "Ioss_GroupingEntity.h" // for GroupingEntity
29 #include "Ioss_IOFactory.h" // for IOFactory
30 #include "Ioss_Map.h" // for Map, MapContainer
31 #include "Ioss_NodeBlock.h" // for NodeBlock
32 #include "Ioss_NodeSet.h" // for NodeSet
33 #include "Ioss_ParallelUtils.h" // for ParallelUtils
34 #include "Ioss_Property.h" // for Property
35 #include "Ioss_PropertyManager.h" // for PropertyManager
36 #include "Ioss_Region.h" // for Region
37 #include "Ioss_SideSet.h" // for SideSet
38 #include "Ioss_Utils.h"
39 #include "Ioss_VariableType.h" // for VariableType
40 #include "Iotm_TextMesh.h" // for TextMesh
41 #include "Iotm_TextMeshSidesetSplitter.h"
42
43 namespace {
44 template <typename INT>
45 void map_global_to_local(const Ioss::Map &map, size_t count, size_t stride, INT *data)
46 {
47 for (size_t i = 0; i < count; i += stride) {
48 int64_t local = map.global_to_local(data[i], true);
49 data[i] = local;
50 }
51 }
52
53 template <typename INT>
54 void fill_transient_data(size_t component_count, double *data, INT *ids, size_t count,
55 double offset = 0.0)
56 {
57 if (component_count == 1) {
58 for (size_t i = 0; i < count; i++) {
59 data[i] = std::sqrt((double)ids[i]) + offset;
60 }
61 }
62 else {
63 for (size_t i = 0; i < count; i++) {
64 for (size_t j = 0; j < component_count; j++) {
65 data[i * component_count + j] = j + std::sqrt((double)ids[i]) + offset;
66 }
67 }
68 }
69 }
70
71 void fill_transient_data(const Ioss::GroupingEntity *entity, const Ioss::Field &field, void *data,
72 void *id_data, size_t count, double offset = 0.0)
73 {
74 const Ioss::Field &ids = entity->get_fieldref("ids");
75 if (ids.is_type(Ioss::Field::INTEGER)) {
76 fill_transient_data(field.raw_storage()->component_count(), reinterpret_cast<double *>(data),
77 reinterpret_cast<int *>(id_data), count, offset);
78 }
79 else {
80 fill_transient_data(field.raw_storage()->component_count(), reinterpret_cast<double *>(data),
81 reinterpret_cast<int64_t *>(id_data), count, offset);
82 }
83 }
84
85 void fill_constant_data(const Ioss::Field &field, void *data, double value)
86 {
87 auto *rdata = reinterpret_cast<double *>(data);
88 size_t count = field.raw_count();
89 size_t component_count = field.raw_storage()->component_count();
90 for (size_t i = 0; i < count * component_count; i++) {
91 rdata[i] = value;
92 }
93 }
94 } // namespace
95 namespace Iotm {
96 // ========================================================================
97 const IOFactory *IOFactory::factory()
98 {
99 static IOFactory registerThis;
100 return &registerThis;
101 }
102
103 IOFactory::IOFactory() : Ioss::IOFactory("textmesh") {}
104
105 Ioss::DatabaseIO *IOFactory::make_IO(const std::string &filename, Ioss::DatabaseUsage db_usage,
106 Ioss_MPI_Comm communicator,
107 const Ioss::PropertyManager &props) const
108 {
109 return new DatabaseIO(nullptr, filename, db_usage, communicator, props);
110 }
111
112 // ========================================================================
113 DatabaseIO::DatabaseIO(Ioss::Region *region, const std::string &filename,
114 Ioss::DatabaseUsage db_usage, Ioss_MPI_Comm communicator,
115 const Ioss::PropertyManager &props)
116 : Ioss::DatabaseIO(region, filename, db_usage, communicator, props)
117 {
118 if (is_input()) {
119 dbState = Ioss::STATE_UNKNOWN;
120 }
121 else {
122 std::ostringstream errmsg;
123 fmt::print(errmsg, "Text mesh option is only valid for input mesh.");
124 IOSS_ERROR(errmsg);
125 }
126
127 if (props.exists("USE_CONSTANT_DF")) {
128 m_useVariableDf = false;
129 }
130 }
131
132 DatabaseIO::~DatabaseIO() { delete m_textMesh; }
133
134 void DatabaseIO::read_meta_data_nl()
135 {
136 if (m_textMesh == nullptr) {
137 if (get_filename() == "external") {
138 std::ostringstream errmsg;
139 fmt::print(errmsg, "ERROR: (text mesh) 'external' specified for mesh, but "
140 "set_text_mesh was not called to set the external mesh.\n");
141 IOSS_ERROR(errmsg);
142 }
143 else {
144 m_textMesh = new TextMesh(get_filename(), util().parallel_size(), util().parallel_rank());
145 }
146 }
147
148 assert(m_textMesh != nullptr);
149
150 Ioss::Region *this_region = get_region();
151 auto glob_node_count = m_textMesh->node_count();
152 auto glob_elem_count = m_textMesh->element_count();
153
154 this_region->property_add(Ioss::Property("global_node_count", glob_node_count));
155 this_region->property_add(Ioss::Property("global_element_count", glob_elem_count));
156
157 const int64_t two_billion = 2ll << 30;
158 if ((glob_node_count > two_billion || glob_elem_count > two_billion) &&
159 int_byte_size_api() == 4) {
160 std::ostringstream errmsg;
161 fmt::print(errmsg,
162 "ERROR: The node count is {} and the element count is {}.\n"
163 " This exceeds the capacity of the 32-bit integers ({})\n"
164 " which are being requested by the client.\n"
165 " The mesh requires 64-bit integers which can be requested by setting the "
166 "`INTEGER_SIZE_API=8` property.",
167 fmt::group_digits(glob_node_count), fmt::group_digits(glob_elem_count),
168 fmt::group_digits(two_billion));
169 IOSS_ERROR(errmsg);
170 }
171
172 spatialDimension = m_textMesh->spatial_dimension();
173 nodeCount = m_textMesh->node_count_proc();
174 elementCount = m_textMesh->element_count_proc();
175 elementBlockCount = m_textMesh->block_count();
176 nodesetCount = m_textMesh->nodeset_count();
177 sidesetCount = m_textMesh->sideset_count();
178 assemblyCount = m_textMesh->assembly_count();
179
180 get_step_times_nl();
181
182 add_transient_fields(this_region);
183 get_nodeblocks();
184 get_elemblocks();
185 get_nodesets();
186 get_sidesets();
187 get_commsets();
188 get_assemblies();
189
190 this_region->property_add(
191 Ioss::Property(std::string("title"), std::string("TextMesh: ") += get_filename()));
192 }
193
194 bool DatabaseIO::begin_nl(Ioss::State /* state */) { return true; }
195
196 bool DatabaseIO::end_nl(Ioss::State /* state */) { return true; }
197
198 bool DatabaseIO::begin_state_nl(int /* state */, double time)
199 {
200 currentTime = time;
201 return true;
202 }
203
204 int64_t DatabaseIO::get_field_internal(const Ioss::NodeBlock *nb, const Ioss::Field &field,
205 void *data, size_t data_size) const
206 {
207 size_t num_to_get = field.verify(data_size);
208
209 Ioss::Field::RoleType role = field.get_role();
210 if (role == Ioss::Field::MESH) {
211 if (field.get_name() == "mesh_model_coordinates") {
212 // Cast 'data' to correct size -- double
213 auto *rdata = static_cast<double *>(data);
214 m_textMesh->coordinates(rdata);
215 }
216 else if (field.get_name() == "mesh_model_coordinates_x") {
217 // Cast 'data' to correct size -- double
218 auto *rdata = static_cast<double *>(data);
219 m_textMesh->coordinates(1, rdata);
220 }
221 else if (field.get_name() == "mesh_model_coordinates_y") {
222 // Cast 'data' to correct size -- double
223 auto *rdata = static_cast<double *>(data);
224 m_textMesh->coordinates(2, rdata);
225 }
226 else if (field.get_name() == "mesh_model_coordinates_z") {
227 // Cast 'data' to correct size -- double
228 auto *rdata = static_cast<double *>(data);
229 m_textMesh->coordinates(3, rdata);
230 }
231
232 // NOTE: The implicit_ids field is ONLY provided for backward-
233 // compatibility and should not be used unless absolutely
234 // required. For text mesh, the implicit_ids and ids are the same.
235 else if (field.get_name() == "ids" || field.get_name() == "implicit_ids") {
236 // Map the local ids in this node block
237 // (1...node_count) to global node ids.
238 get_node_map().map_implicit_data(data, field, num_to_get, 0);
239 }
240 else if (field.get_name() == "owning_processor") {
241 int *owner = static_cast<int *>(data);
242 m_textMesh->owning_processor(owner, num_to_get);
243 }
244 else if (field.get_name() == "connectivity") {
245 // Do nothing, just handles an idiosyncrasy of the GroupingEntity
246 }
247 else if (field.get_name() == "connectivity_raw") {
248 // Do nothing, just handles an idiosyncrasy of the GroupingEntity
249 }
250 else {
251 num_to_get = Ioss::Utils::field_warning(nb, field, "input");
252 }
253 return num_to_get;
254 }
255
256 const Ioss::Field &id_fld = nb->get_fieldref("ids");
257 std::vector<char> ids(id_fld.get_size());
258 get_field_internal(nb, id_fld, Data(ids), id_fld.get_size());
259 fill_transient_data(nb, field, data, Data(ids), num_to_get, currentTime);
260
261 return num_to_get;
262 }
263
264 int64_t DatabaseIO::get_field_internal(const Ioss::Region * /*region*/, const Ioss::Field &field,
265 void *data, size_t /*data_size*/) const
266 {
267 Ioss::Field::RoleType role = field.get_role();
268 if (role == Ioss::Field::TRANSIENT) {
269 // Fill the field with arbitrary data...
270 (reinterpret_cast<double *>(data))[0] = static_cast<double>(rand());
271 }
272 return 1;
273 }
274
275 int64_t DatabaseIO::get_field_internal(const Ioss::ElementBlock *eb, const Ioss::Field &field,
276 void *data, size_t data_size) const
277 {
278 size_t num_to_get = field.verify(data_size);
279
280 int64_t id = eb->get_property("id").get_int();
281 int64_t element_count = eb->entity_count();
282 Ioss::Field::RoleType role = field.get_role();
283
284 if (role == Ioss::Field::MESH) {
285 // Handle the MESH fields required for an ExodusII file model.
286 // (The 'genesis' portion)
287
288 if (field.get_name() == "connectivity" || field.get_name() == "connectivity_raw") {
289 assert(field.raw_storage()->component_count() == m_textMesh->topology_type(id).second);
290
291 // The text mesh connectivity is returned in a vector. Ids are global
292 if (field.is_type(Ioss::Field::INTEGER)) {
293 int *connect = static_cast<int *>(data);
294 m_textMesh->connectivity(id, connect);
295 if (field.get_name() == "connectivity_raw") {
296 map_global_to_local(get_node_map(),
297 element_count * field.raw_storage()->component_count(), 1, connect);
298 }
299 }
300 else {
301 auto *connect = static_cast<int64_t *>(data);
302 m_textMesh->connectivity(id, connect);
303 if (field.get_name() == "connectivity_raw") {
304 map_global_to_local(get_node_map(),
305 element_count * field.raw_storage()->component_count(), 1, connect);
306 }
307 }
308 }
309 else if (field.get_name() == "ids" || field.get_name() == "implicit_ids") {
310 // Map the local ids in this element block
311 // (eb_offset+1...eb_offset+1+element_count) to global element ids.
312 get_element_map().map_implicit_data(data, field, num_to_get, eb->get_offset());
313 }
314 else {
315 num_to_get = Ioss::Utils::field_warning(eb, field, "input");
316 }
317 }
318
319 else if (role == Ioss::Field::ATTRIBUTE) {
320 if (element_count > 0) {
321 int attribute_count = eb->get_property("attribute_count").get_int();
322 if (attribute_count > 0) {
323 auto *attr = static_cast<double *>(data);
324 for (size_t i = 0; i < num_to_get; i++) {
325 attr[i] = 1.0;
326 }
327 }
328 }
329 }
330
331 else if (role == Ioss::Field::TRANSIENT) {
332 // Fill the field with arbitrary data...
333 const Ioss::Field &id_fld = eb->get_fieldref("ids");
334 std::vector<char> ids(id_fld.get_size());
335 get_field_internal(eb, id_fld, Data(ids), id_fld.get_size());
336 fill_transient_data(eb, field, data, Data(ids), num_to_get, currentTime);
337 }
338 else if (role == Ioss::Field::REDUCTION) {
339 num_to_get = Ioss::Utils::field_warning(eb, field, "input reduction");
340 }
341 return num_to_get;
342 }
343
344 int64_t DatabaseIO::get_field_internal(const Ioss::SideBlock *ef_blk, const Ioss::Field &field,
345 void *data, size_t data_size) const
346 {
347 size_t num_to_get = field.verify(data_size);
348
349 int64_t id = ef_blk->get_property("id").get_int();
350 size_t entity_count = ef_blk->entity_count();
351 if (num_to_get != entity_count) {
352 std::ostringstream errmsg;
353 fmt::print(errmsg, "Partial field input not implemented for side blocks");
354 IOSS_ERROR(errmsg);
355 }
356
357 Ioss::Field::RoleType role = field.get_role();
358
359 if (role == Ioss::Field::MESH) {
360
361 if (field.get_name() == "ids") {
362 // A sideset' is basically an exodus sideset. A
363 // sideset has a list of elements and a corresponding local
364 // element side (1-based) The side id is: side_id =
365 // 10*element_id + local_side_number This assumes that all
366 // sides in a sideset are boundary sides.
367 std::vector<int64_t> elem_side;
368 m_textMesh->sideblock_elem_sides(id, ef_blk->name(), elem_side);
369 if (field.is_type(Ioss::Field::INTEGER)) {
370 int *ids = static_cast<int *>(data);
371 for (size_t i = 0; i < num_to_get; i++) {
372 ids[i] = static_cast<int>(10 * elem_side[2 * i + 0] + elem_side[2 * i + 1]);
373 }
374 }
375 else {
376 auto *ids = static_cast<int64_t *>(data);
377 for (size_t i = 0; i < num_to_get; i++) {
378 ids[i] = 10 * elem_side[2 * i + 0] + elem_side[2 * i + 1];
379 }
380 }
381 }
382
383 else if (field.get_name() == "element_side" || field.get_name() == "element_side_raw") {
384 // Since we only have a single array, we need to allocate an extra
385 // array to store all of the data. Note also that the element_id
386 // is the global id but only the local id is stored so we need to
387 // map from local_to_global prior to generating the side id...
388
389 std::vector<int64_t> elem_side;
390 m_textMesh->sideblock_elem_sides(id, ef_blk->name(), elem_side);
391 if (field.get_name() == "element_side_raw") {
392 map_global_to_local(get_element_map(), elem_side.size(), 2, Data(elem_side));
393 }
394
395 if (field.is_type(Ioss::Field::INTEGER)) {
396 int *element_side = static_cast<int *>(data);
397 for (size_t i = 0; i < num_to_get; i++) {
398 element_side[2 * i + 0] = static_cast<int>(elem_side[2 * i + 0]);
399 element_side[2 * i + 1] = static_cast<int>(elem_side[2 * i + 1]);
400 }
401 }
402 else {
403 auto *element_side = static_cast<int64_t *>(data);
404 for (size_t i = 0; i < num_to_get; i++) {
405 element_side[2 * i + 0] = elem_side[2 * i + 0];
406 element_side[2 * i + 1] = elem_side[2 * i + 1];
407 }
408 }
409 }
410
411 else if (field.get_name() == "distribution_factors") {
412 if (m_useVariableDf) {
413 const Ioss::Field &id_fld = ef_blk->get_fieldref("ids");
414 std::vector<char> ids(id_fld.get_size());
415 get_field_internal(ef_blk, id_fld, Data(ids), id_fld.get_size());
416 fill_transient_data(ef_blk, field, data, Data(ids), num_to_get);
417 }
418 else {
419 fill_constant_data(field, data, 1.0);
420 }
421 }
422
423 else {
424 num_to_get = Ioss::Utils::field_warning(ef_blk, field, "input");
425 }
426 }
427 else if (role == Ioss::Field::TRANSIENT) {
428 const Ioss::Field &id_fld = ef_blk->get_fieldref("ids");
429 std::vector<char> ids(id_fld.get_size());
430 get_field_internal(ef_blk, id_fld, Data(ids), id_fld.get_size());
431 fill_transient_data(ef_blk, field, data, Data(ids), num_to_get, currentTime);
432 }
433 return num_to_get;
434 }
435
436 int64_t DatabaseIO::get_field_internal(const Ioss::NodeSet *ns, const Ioss::Field &field,
437 void *data, size_t data_size) const
438 {
439 size_t num_to_get = field.verify(data_size);
440
441 int64_t id = ns->get_property("id").get_int();
442 Ioss::Field::RoleType role = field.get_role();
443 if (role == Ioss::Field::MESH) {
444
445 if (field.get_name() == "ids" || field.get_name() == "ids_raw") {
446 std::vector<int64_t> nodes;
447 m_textMesh->nodeset_nodes(id, nodes);
448 if (field.get_name() == "ids_raw") {
449 map_global_to_local(get_node_map(), nodes.size(), 1, Data(nodes));
450 }
451
452 if (field.is_type(Ioss::Field::INTEGER)) {
453 int *ids = static_cast<int *>(data);
454 #ifdef _MSC_VER
455 #pragma warning(push)
456 #pragma warning(disable : 4244)
457 #endif
458 std::copy(nodes.begin(), nodes.end(), ids);
459 #ifdef _MSC_VER
460 #pragma warning(pop)
461 #endif
462 }
463 else {
464 auto *ids = static_cast<int64_t *>(data);
465 std::copy(nodes.begin(), nodes.end(), ids);
466 }
467 }
468 else if (field.get_name() == "distribution_factors") {
469 if (m_useVariableDf) {
470 const Ioss::Field &id_fld = ns->get_fieldref("ids");
471 std::vector<char> ids(id_fld.get_size());
472 get_field_internal(ns, id_fld, Data(ids), id_fld.get_size());
473 fill_transient_data(ns, field, data, Data(ids), num_to_get);
474 }
475 else {
476 fill_constant_data(field, data, 1.0);
477 }
478 }
479 else {
480 num_to_get = Ioss::Utils::field_warning(ns, field, "input");
481 }
482 }
483 else if (role == Ioss::Field::TRANSIENT) {
484 const Ioss::Field &id_fld = ns->get_fieldref("ids");
485 std::vector<char> ids(id_fld.get_size());
486 get_field_internal(ns, id_fld, Data(ids), id_fld.get_size());
487 fill_transient_data(ns, field, data, Data(ids), num_to_get, currentTime);
488 }
489 return num_to_get;
490 }
491
492 int64_t DatabaseIO::get_field_internal(const Ioss::CommSet *cs, const Ioss::Field &field,
493 void *data, size_t data_size) const
494 {
495 size_t num_to_get = field.verify(data_size);
496 size_t entity_count = cs->entity_count();
497 assert(num_to_get == entity_count);
498
499 // Return the <entity (node or face), processor> pair
500 if (field.get_name() == "entity_processor" || field.get_name() == "entity_processor_raw") {
501 // Check type -- node or face
502 std::string type = cs->get_property("entity_type").get_string();
503
504 if (type == "node") {
505 // Allocate temporary storage space
506 Ioss::Int64Vector entities(num_to_get);
507 Ioss::IntVector procs(num_to_get);
508 m_textMesh->node_communication_map(entities, procs);
509
510 // and store in 'data' ...
511 if (field.is_type(Ioss::Field::INTEGER)) {
512 int *entity_proc = static_cast<int *>(data);
513
514 size_t j = 0;
515 for (size_t i = 0; i < entity_count; i++) {
516 assert(entities[i] > 0);
517 entity_proc[j++] = entities[i];
518 entity_proc[j++] = procs[i];
519 }
520
521 if (field.get_name() == "entity_processor_raw") {
522 map_global_to_local(get_node_map(), 2 * entity_count, 2, entity_proc);
523 }
524 }
525 else {
526 auto *entity_proc = static_cast<int64_t *>(data);
527
528 size_t j = 0;
529 for (size_t i = 0; i < entity_count; i++) {
530 assert(entities[i] > 0);
531 entity_proc[j++] = entities[i];
532 entity_proc[j++] = procs[i];
533 }
534
535 if (field.get_name() == "entity_processor_raw") {
536 map_global_to_local(get_node_map(), 2 * entity_count, 2, entity_proc);
537 }
538 }
539 }
540 else {
541 std::ostringstream errmsg;
542 fmt::print(errmsg, "Invalid commset type {}", type);
543 IOSS_ERROR(errmsg);
544 }
545 }
546 else if (field.get_name() == "ids") {
547 // Do nothing, just handles an idiosyncrasy of the GroupingEntity
548 }
549 else {
550 num_to_get = Ioss::Utils::field_warning(cs, field, "input");
551 }
552 return num_to_get;
553 }
554
555 int64_t DatabaseIO::get_field_internal(const Ioss::Assembly *assembly, const Ioss::Field &field,
556 void * /* data */, size_t data_size) const
557 {
558 size_t num_to_get = field.verify(data_size);
559 if (num_to_get > 0) {
560
561 Ioss::Field::RoleType role = field.get_role();
562 if (role == Ioss::Field::MESH) {
563 if (field.get_name() == "ids") {
564 // Map the local ids in this node block
565 // (1...node_count) to global node ids.
566 // get_map(EX_ASSEMBLY).map_implicit_data(data, field, num_to_get, 0);
567 }
568
569 else if (field.get_name() == "connectivity") {
570 // Do nothing, just handles an idiosyncrasy of the GroupingEntity
571 }
572 else if (field.get_name() == "connectivity_raw") {
573 // Do nothing, just handles an idiosyncrasy of the GroupingEntity
574 }
575 else {
576 num_to_get = Ioss::Utils::field_warning(assembly, field, "input");
577 }
578 }
579 else if (role == Ioss::Field::TRANSIENT) {
580 // Check if the specified field exists on this assembly.
581 // Note that 'higher-order' storage types (e.g. SYM_TENSOR)
582 // exist on the database as scalars with the appropriate
583 // extensions.
584
585 // Read in each component of the variable and transfer into
586 // 'data'. Need temporary storage area of size 'number of
587 // items in this assembly.
588 // num_to_get =
589 // read_transient_field(EX_ASSEMBLY, m_variables[EX_ASSEMBLY], field, assembly, data);
590 }
591 else if (role == Ioss::Field::REDUCTION) {
592 // get_reduction_field(EX_ASSEMBLY, field, assembly, data);
593 }
594 else if (role == Ioss::Field::ATTRIBUTE) {
595 // num_to_get = read_attribute_field(EX_ASSEMBLY, field, assembly, data);
596 }
597 }
598 return num_to_get;
599 }
600
601 const Ioss::Map &DatabaseIO::get_node_map() const
602 {
603 // Allocate space for node number map and read it in...
604 // Can be called multiple times, allocate 1 time only
605 if (nodeMap.map().empty()) {
606 nodeMap.set_size(nodeCount);
607 std::vector<int64_t> map;
608 m_textMesh->node_map(map);
609 nodeMap.set_map(Data(map), map.size(), 0, true);
610 }
611 return nodeMap;
612 }
613
614 const Ioss::Map &DatabaseIO::get_element_map() const
615 {
616 // Allocate space for element number map and read it in...
617 // Can be called multiple times, allocate 1 time only
618 if (elemMap.map().empty()) {
619 elemMap.set_size(elementCount);
620 std::vector<int64_t> map;
621 m_textMesh->element_map(map);
622 elemMap.set_map(Data(map), map.size(), 0, true);
623 }
624 return elemMap;
625 }
626
627 void DatabaseIO::get_nodeblocks()
628 {
629 std::string block_name = "nodeblock_1";
630 auto block =
631 new Ioss::NodeBlock(this, block_name, m_textMesh->node_count_proc(), spatialDimension);
632 block->property_add(Ioss::Property("id", 1));
633 block->property_add(Ioss::Property("guid", util().generate_guid(1)));
634 get_region()->add(block);
635 add_transient_fields(block);
636 }
637
638 void DatabaseIO::get_step_times_nl()
639 {
640 int time_step_count = m_textMesh->timestep_count();
641 for (int i = 0; i < time_step_count; i++) {
642 get_region()->add_state(i);
643 }
644 }
645
646 std::vector<double> DatabaseIO::get_db_step_times_nl()
647 {
648 std::vector<double> timesteps;
649
650 int time_step_count = m_textMesh->timestep_count();
651 timesteps.reserve(time_step_count);
652 for (int i = 0; i < time_step_count; i++) {
653 timesteps.push_back(i);
654 }
655
656 return timesteps;
657 }
658
659 void DatabaseIO::update_block_omissions_from_assemblies()
660 {
661 m_textMesh->update_block_omissions_from_assemblies(
662 get_region(), assemblyOmissions, assemblyInclusions, blockOmissions, blockInclusions);
663 }
664
665 void DatabaseIO::get_elemblocks()
666 {
667 // Attributes of an element block are:
668 // -- id
669 // -- name
670 // -- element type
671 // -- number of elements
672 // -- number of attributes per element
673 // -- number of nodes per element (derivable from type)
674 // -- number of faces per element (derivable from type)
675 // -- number of edges per element (derivable from type)
676
677 std::vector<std::string> blockNames = m_textMesh->get_part_names();
678 int order = 0;
679
680 for (const std::string &name : blockNames) {
681 int64_t id = m_textMesh->get_part_id(name);
682 std::string type = m_textMesh->topology_type(id).first;
683 size_t element_count = m_textMesh->element_count_proc(id);
684 auto block = new Ioss::ElementBlock(this, name, type, element_count);
685
686 block->property_add(Ioss::Property("id", id));
687 block->property_add(Ioss::Property("guid", util().generate_guid(id)));
688 block->property_add(Ioss::Property("original_block_order", order));
689
690 block->property_add(Ioss::Property("global_entity_count", m_textMesh->element_count(id)));
691
692 get_region()->add(block);
693 add_transient_fields(block);
694
695 order++;
696 }
697
698 if (!assemblyOmissions.empty() || !assemblyInclusions.empty()) {
699 update_block_omissions_from_assemblies();
700 }
701
702 assert(blockOmissions.empty() || blockInclusions.empty()); // Only one can be non-empty
703
704 // Handle all block omissions or inclusions...
705 if (!blockOmissions.empty()) {
706 for (const auto &name : blockOmissions) {
707 auto block = get_region()->get_element_block(name);
708 if (block != nullptr) {
709 block->property_add(Ioss::Property(std::string("omitted"), 1));
710 }
711 }
712 }
713
714 if (!blockInclusions.empty()) {
715 const auto &blocks = get_region()->get_element_blocks();
716 for (auto &block : blocks) {
717 block->property_add(Ioss::Property(std::string("omitted"), 1));
718 }
719
720 // Now, erase the property on any blocks in the inclusion list...
721 for (const auto &name : blockInclusions) {
722 auto block = get_region()->get_element_block(name);
723 if (block != nullptr) {
724 block->property_erase("omitted");
725 }
726 }
727 }
728 }
729
730 void DatabaseIO::get_nodesets()
731 {
732 // Attributes of a nodeset are:
733 // -- id
734 // -- name
735 // -- number of nodes
736 // -- number of distribution factors (see next comment)
737 // ----the #distribution factors should equal #nodes or 0, any
738 // other value does not make sense. If it is 0, then a substitute
739 // list will be created returning 1.0 for the factor
740
741 // In a parallel execution, it is possible that a nodeset will have
742 // no nodes or distribution factors on a particular processor...
743
744 // Get nodeset metadata
745 std::vector<std::string> nodesetNames = m_textMesh->get_nodeset_names();
746 for (const std::string &name : nodesetNames) {
747 int64_t id = m_textMesh->get_nodeset_id(name);
748 int64_t number_nodes = m_textMesh->nodeset_node_count_proc(id);
749
750 auto nodeset = new Ioss::NodeSet(this, name, number_nodes);
751 nodeset->property_add(Ioss::Property("id", id));
752 nodeset->property_add(Ioss::Property("guid", util().generate_guid(id)));
753 get_region()->add(nodeset);
754 add_transient_fields(nodeset);
755 }
756 }
757
758 void DatabaseIO::get_sidesets()
759 {
760 std::vector<std::string> sidesetNames = m_textMesh->get_sideset_names();
761 for (const std::string &name : sidesetNames) {
762 int64_t id = m_textMesh->get_sideset_id(name);
763 auto sideset = new Ioss::SideSet(this, name);
764 sideset->property_add(Ioss::Property("id", id));
765 sideset->property_add(Ioss::Property("guid", util().generate_guid(id)));
766 get_region()->add(sideset);
767
768 get_region()->add_alias(name, Ioss::Utils::encode_entity_name("sideset", id), Ioss::SIDESET);
769
770 std::vector<SideBlockInfo> infoVec = m_textMesh->get_side_block_info_for_sideset(name);
771
772 for (const SideBlockInfo &info : infoVec) {
773 size_t sideCount = m_textMesh->get_local_side_block_indices(name, info).size();
774 auto sideblock = new Ioss::SideBlock(this, info.name, info.sideTopology,
775 info.elementTopology, sideCount);
776 sideset->add(sideblock);
777
778 // Note that all sideblocks within a specific
779 // sideset might have the same id.
780 assert(sideblock != nullptr);
781 sideblock->property_add(Ioss::Property("id", id));
782 sideblock->property_add(Ioss::Property("guid", util().generate_guid(id)));
783
784 // If splitting by element block, need to set the
785 // element block member on this side block.
786 auto split_type = m_textMesh->get_sideset_split_type(name);
787 if (split_type == text_mesh::SplitType::ELEMENT_BLOCK) {
788 Ioss::ElementBlock *block = get_region()->get_element_block(info.touchingBlock);
789 sideblock->set_parent_element_block(block);
790 }
791
792 if (split_type != text_mesh::SplitType::NO_SPLIT) {
793 std::string storage = "Real[";
794 storage += std::to_string(info.numNodesPerSide);
795 storage += "]";
796 sideblock->field_add(
797 Ioss::Field("distribution_factors", Ioss::Field::REAL, storage, Ioss::Field::MESH));
798 }
799
800 add_transient_fields(sideblock);
801 }
802 }
803 }
804
805 void DatabaseIO::get_commsets()
806 {
807 if (util().parallel_size() > 1) {
808 // Get size of communication map...
809 size_t my_node_count = m_textMesh->communication_node_count_proc();
810
811 // Create a single node commset
812 auto *commset = new Ioss::CommSet(this, "commset_node", "node", my_node_count);
813 commset->property_add(Ioss::Property("id", 1));
814 commset->property_add(Ioss::Property("guid", util().generate_guid(1)));
815 get_region()->add(commset);
816 }
817 }
818
819 void DatabaseIO::get_assemblies()
820 {
821 // Get assembly metadata
822 std::vector<std::string> assemblyNames = m_textMesh->get_assembly_names();
823 for (const std::string &name : assemblyNames) {
824 int64_t id = m_textMesh->get_assembly_id(name);
825
826 auto assembly = new Ioss::Assembly(this, name);
827 assembly->property_add(Ioss::Property("id", id));
828 assembly->property_add(Ioss::Property("guid", util().generate_guid(id)));
829 get_region()->add(assembly);
830 }
831
832 // Now iterate again and populate member lists...
833 for (const std::string &name : assemblyNames) {
834 Ioss::Assembly *assem = get_region()->get_assembly(name);
835 assert(assem != nullptr);
836 Ioss::EntityType type = m_textMesh->get_assembly_type(name);
837 const std::vector<std::string> members = m_textMesh->get_assembly_members(name);
838
839 for (const auto &member : members) {
840 auto *ge = get_region()->get_entity(member, type);
841 if (ge != nullptr) {
842 assem->add(ge);
843 }
844 else {
845 std::ostringstream errmsg;
846 fmt::print(errmsg,
847 "Error: Failed to find entity of type {} with name {} for Assembly {}.\n",
848 Ioss::Utils::entity_type_to_string(type), member, assem->name());
849 IOSS_ERROR(errmsg);
850 }
851 }
852 SMART_ASSERT(assem->member_count() == members.size())(assem->member_count())(members.size());
853 }
854 }
855
856 unsigned DatabaseIO::entity_field_support() const
857 {
858 return Ioss::NODEBLOCK | Ioss::ELEMENTBLOCK | Ioss::REGION | Ioss::NODESET | Ioss::SIDESET |
859 Ioss::ASSEMBLY;
860 }
861
862 void DatabaseIO::add_transient_fields(Ioss::GroupingEntity *entity)
863 {
864 Ioss::EntityType type = entity->type();
865 size_t var_count = m_textMesh->get_variable_count(type);
866 for (size_t i = 0; i < var_count; i++) {
867 std::string var_name = entity->type_string() + "_" + std::to_string(i + 1);
868 entity->field_add(Ioss::Field(var_name, Ioss::Field::REAL, "scalar", Ioss::Field::TRANSIENT));
869 }
870 }
871
872 void DatabaseIO::compute_block_membership_nl(Ioss::SideBlock *efblock,
873 std::vector<std::string> &block_membership) const
874 {
875 m_textMesh->compute_block_membership(efblock->owner()->name(), efblock->name(),
876 block_membership);
877 }
878
879 } // namespace Iotm