Debian Codegraph @

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

raw
1 // Copyright(C) 1999-2024 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_TextMesh.h"
8
9 #include "Ioss_Utils.h"
10 #include <fmt/ostream.h>
11 #include <iostream>
12 #include <stdexcept>
13 #include <string>
14 #include <vector> // for vector
15
16 #include "Ioss_CodeTypes.h"
17 #include "Ioss_EntityType.h"
18 #include "Ioss_Utils.h"
19
20 #define ThrowRequireMsg(expr, message) \
21 do { \
22 if (!(expr)) { \
23 std::ostringstream internal_throw_require_oss; \
24 internal_throw_require_oss << message; \
25 throw std::logic_error(internal_throw_require_oss.str()); \
26 } \
27 } while (false)
28
29 namespace Iotm {
30
31 class AssemblyTreeFilter
32 {
33 public:
34 AssemblyTreeFilter() = delete;
35 AssemblyTreeFilter(const AssemblyTreeFilter &) = delete;
36
37 AssemblyTreeFilter(Ioss::Region *region, const Ioss::EntityType filterType,
38 const Assemblies &assemblies)
39 : m_region(region), m_type(filterType), m_assemblies(assemblies)
40 {
41 for (const std::string &assemblyName : m_assemblies.get_part_names()) {
42 m_visitedAssemblies[assemblyName] = false;
43 }
44 }
45
46 void update_list_from_assembly_tree(const AssemblyData *assembly,
47 std::vector<std::string> &list)
48 {
49 // Walk the tree without cyclic dependency
50 if (nullptr != assembly) {
51 if (!m_visitedAssemblies[assembly->name]) {
52 m_visitedAssemblies[assembly->name] = true;
53
54 const Ioss::EntityType assemblyType =
55 TextMesh::assembly_type_to_entity_type(assembly->get_assembly_type());
56 if (m_type == assemblyType) {
57 for (const std::string &assemblyMember : assembly->data) {
58 Ioss::GroupingEntity *ge = m_region->get_entity(assemblyMember, m_type);
59 if (nullptr != ge) {
60 list.push_back(ge->name());
61 }
62 }
63 }
64
65 if (Ioss::ASSEMBLY == assemblyType) {
66 for (const std::string &subAssemblyName : assembly->data) {
67 // Find the sub assembly
68 const AssemblyData *subAssembly = m_assemblies.get_group_data(subAssemblyName);
69
70 if (nullptr != subAssembly) {
71 update_list_from_assembly_tree(subAssembly, list);
72 }
73 else {
74 std::ostringstream errmsg;
75 fmt::print(errmsg, "ERROR: Could not find sub-assembly with id: {} and name: {}",
76 assembly->id, assembly->name);
77 IOSS_ERROR(errmsg);
78 }
79 }
80 }
81 }
82 }
83 }
84
85 void update_assembly_filter_list(std::vector<std::string> &assemblyFilterList)
86 {
87 for (const std::string &assemblyName : m_assemblies.get_part_names()) {
88 if (m_visitedAssemblies[assemblyName]) {
89 assemblyFilterList.emplace_back(assemblyName);
90 }
91 }
92
93 std::sort(assemblyFilterList.begin(), assemblyFilterList.end(), std::less<>());
94 auto endIter = std::unique(assemblyFilterList.begin(), assemblyFilterList.end());
95 assemblyFilterList.resize(endIter - assemblyFilterList.begin());
96 }
97
98 private:
99 Ioss::Region *m_region = nullptr;
100 Ioss::EntityType m_type = Ioss::INVALID_TYPE;
101 const Assemblies &m_assemblies;
102 mutable std::map<std::string, bool> m_visitedAssemblies;
103 };
104
105 void error_handler(const std::ostringstream &message) { throw std::logic_error((message).str()); }
106
107 TextMesh::TextMesh(int, int my_proc) : m_myProcessor(my_proc)
108 {
109 m_errorHandler = [](const std::ostringstream &errmsg) { error_handler(errmsg); };
110 initialize();
111 }
112
113 TextMesh::TextMesh(const std::string &parameters, IOSS_MAYBE_UNUSED int proc_count, int my_proc)
114 : m_myProcessor(my_proc)
115 {
116 m_errorHandler = [](const std::ostringstream &errmsg) { error_handler(errmsg); };
117
118 if (!parameters.empty()) {
119 TextMeshParser parser;
120 parser.set_error_handler(m_errorHandler);
121 m_data = parser.parse(parameters);
122 };
123
124 initialize();
125 }
126
127 TextMesh::TextMesh()
128 {
129 m_errorHandler = [](const std::ostringstream &errmsg) { error_handler(errmsg); };
130 initialize();
131 }
132
133 unsigned TextMesh::spatial_dimension() const { return m_data.spatialDim; }
134
135 void TextMesh::initialize()
136 {
137 build_part_to_topology_map();
138 build_block_partition_map();
139 build_element_connectivity_map();
140
141 m_variableCount[Ioss::NODESET] = 0;
142 m_variableCount[Ioss::SIDESET] = 0;
143 m_variableCount[Ioss::COMMSET] = 0;
144 m_variableCount[Ioss::ELEMENTBLOCK] = 0;
145 m_variableCount[Ioss::INVALID_TYPE] = 0;
146 m_variableCount[Ioss::NODEBLOCK] = 0;
147 m_variableCount[Ioss::REGION] = 0;
148 m_variableCount[Ioss::ASSEMBLY] = 0;
149 }
150
151 int64_t TextMesh::node_count() const { return m_data.nodeIds.size(); }
152
153 int64_t TextMesh::node_count_proc() const { return m_data.num_nodes_on_proc(m_myProcessor); }
154
155 int64_t TextMesh::block_count() const { return m_data.partIds.size(); }
156
157 int64_t TextMesh::nodeset_count() const { return m_data.nodesets.get_group_data().size(); }
158
159 int64_t TextMesh::nodeset_node_count(EntityId id) const
160 {
161 int64_t count = 0;
162
163 const NodesetData *nodeset = m_data.nodesets.get_group_data(id);
164 if (nullptr != nodeset) {
165 count = nodeset->data.size();
166 }
167 return count;
168 }
169
170 int64_t TextMesh::nodeset_node_count_proc(EntityId id) const
171 {
172 int64_t count = 0;
173
174 const NodesetData *nodeset = m_data.nodesets.get_group_data(id);
175 const std::set<EntityId> &myNodes = m_data.nodes_on_proc(m_myProcessor);
176
177 if (nullptr != nodeset) {
178 for (EntityId nodeId : nodeset->data) {
179 if (myNodes.count(nodeId) > 0) {
180 count++;
181 }
182 }
183 }
184 return count;
185 }
186
187 int64_t TextMesh::sideset_count() const { return m_data.sidesets.get_group_data().size(); }
188
189 int64_t TextMesh::sideset_side_count(EntityId id) const
190 {
191 int64_t count = 0;
192
193 const SidesetData *sideset = m_data.sidesets.get_group_data(id);
194 if (nullptr != sideset) {
195 count = sideset->data.size();
196 }
197 return count;
198 }
199
200 int64_t TextMesh::sideset_side_count_proc(EntityId id) const
201 {
202 int64_t count = 0;
203
204 const SidesetData *sideset = m_data.sidesets.get_group_data(id);
205 int myProc = m_myProcessor;
206 if (nullptr != sideset) {
207 for (const std::pair<EntityId, int> &elemSidePair : sideset->data) {
208 EntityId elemId = elemSidePair.first;
209 auto iter = std::find(m_data.elementDataVec.begin(), m_data.elementDataVec.end(), elemId);
210 if (iter != m_data.elementDataVec.end() && iter->proc == myProc) {
211 count++;
212 }
213 }
214 }
215 return count;
216 }
217
218 int64_t TextMesh::element_count() const { return m_data.elementDataVec.size(); }
219
220 int64_t TextMesh::element_count_proc() const
221 {
222 int64_t count = 0;
223
224 for (const auto &part : m_blockPartition) {
225 count += part.second.elemIds.size();
226 }
227
228 return count;
229 }
230
231 int64_t TextMesh::element_count(EntityId id) const
232 {
233 int64_t count = 0;
234 for (const auto &elementData : m_data.elementDataVec) {
235 if (get_part_id(elementData.partName) == id) {
236 count++;
237 }
238 }
239
240 return count;
241 }
242
243 int64_t TextMesh::element_count_proc(EntityId id) const
244 {
245 int64_t count = 0;
246 int myProc = m_myProcessor;
247 for (const auto &elementData : m_data.elementDataVec) {
248 if (get_part_id(elementData.partName) == id && elementData.proc == myProc) {
249 count++;
250 }
251 }
252
253 return count;
254 }
255
256 Topology TextMesh::get_topology_for_part(EntityId id) const
257 {
258 unsigned partId = id;
259 std::string partName = m_data.partIds.get(partId);
260
261 auto iter = m_partToTopology.find(partName);
262 ThrowRequireMsg(iter != m_partToTopology.end(),
263 "Could not find a topology associated with part: " << partName);
264
265 Topology topo = iter->second;
266 return topo;
267 }
268
269 std::pair<std::string, int> TextMesh::topology_type(EntityId id) const
270 {
271 Topology topo = get_topology_for_part(id);
272 return std::make_pair(topo.name(), topo.num_nodes());
273 }
274
275 template <typename INT> void TextMesh::raw_node_map(std::vector<INT> &map) const
276 {
277 map.resize(node_count_proc());
278 INT offset = 0;
279
280 const auto &nodeIds = m_data.nodes_on_proc(m_myProcessor);
281 for (auto id : nodeIds) {
282 map[offset++] = id;
283 }
284 }
285
286 void TextMesh::node_map(Ioss::Int64Vector &map) const { raw_node_map(map); }
287
288 void TextMesh::node_map(Ioss::IntVector &map) const { raw_node_map(map); }
289
290 int64_t TextMesh::communication_node_count_proc() const
291 {
292 int64_t count = 0;
293 const auto &nodeIds = m_data.nodes_on_proc(m_myProcessor);
294
295 for (auto id : nodeIds) {
296 size_t numProcsForNode = m_data.procs_for_node(id).size();
297 ThrowRequireMsg(numProcsForNode > 0, "Invalid node sharing for id: " << id);
298 count += numProcsForNode - 1;
299 ;
300 }
301 return count;
302 }
303
304 void TextMesh::owning_processor(int *owner, int64_t num_node)
305 {
306 const auto &nodeIds = m_data.nodes_on_proc(m_myProcessor);
307 auto iter = nodeIds.begin();
308
309 ThrowRequireMsg(num_node == (int64_t)nodeIds.size(),
310 "Unmatched data sizes in TextMesh::owning_processor()");
311
312 for (int64_t i = 0; i < num_node; i++) {
313 const auto &procs = m_data.procs_for_node(*iter);
314 owner[i] = *procs.begin();
315 iter++;
316 }
317 }
318
319 class NodeCommunicationMap
320 {
321 public:
322 NodeCommunicationMap() = delete;
323 NodeCommunicationMap(const NodeCommunicationMap &) = delete;
324
325 NodeCommunicationMap(int myProc, Ioss::Int64Vector &map, std::vector<int> &processors)
326 : m_myProcessor(myProc), m_nodeMap(map), m_processorMap(processors)
327 {
328 }
329
330 void fill_map_from_data(const TextMeshData &data)
331 {
332 m_fillIndex = 0;
333 const auto &nodeIds = data.nodes_on_proc(m_myProcessor);
334
335 for (const auto &id : nodeIds) {
336 fill_map_for_node(id, data);
337 }
338 }
339
340 void verify_map_size(const size_t minimumSize)
341 {
342 ThrowRequireMsg(m_nodeMap.size() >= minimumSize, "Insufficient size in entity vector");
343 ThrowRequireMsg(m_processorMap.size() >= minimumSize,
344 "Insufficient size in processor vector");
345 }
346
347 private:
348 void add_comm_map_pair(EntityId id, int proc)
349 {
350 m_nodeMap[m_fillIndex] = id;
351 m_processorMap[m_fillIndex] = proc;
352
353 m_fillIndex++;
354 }
355
356 void fill_map_for_node(EntityId id, const TextMeshData &data)
357 {
358 const std::set<int> &procs = data.procs_for_node(id);
359
360 for (int proc : procs) {
361 if (proc != m_myProcessor) {
362 add_comm_map_pair(id, proc);
363 }
364 }
365 }
366
367 int m_myProcessor;
368 Ioss::Int64Vector &m_nodeMap;
369 std::vector<int> &m_processorMap;
370
371 size_t m_fillIndex = 0;
372 };
373
374 void TextMesh::node_communication_map(Ioss::Int64Vector &map, std::vector<int> &processors)
375 {
376 NodeCommunicationMap commMap(m_myProcessor, map, processors);
377 commMap.verify_map_size(communication_node_count_proc());
378 commMap.fill_map_from_data(m_data);
379 }
380
381 void TextMesh::element_map(EntityId block_number, Ioss::Int64Vector &map) const
382 {
383 raw_element_map(block_number, map);
384 }
385
386 void TextMesh::element_map(EntityId block_number, Ioss::IntVector &map) const
387 {
388 raw_element_map(block_number, map);
389 }
390
391 template <typename INT> void TextMesh::raw_element_map(EntityId id, std::vector<INT> &map) const
392 {
393 auto iter = m_blockPartition.find(id);
394 ThrowRequireMsg(iter != m_blockPartition.end(),
395 "Could not find block with id: " << id << " in block partition");
396
397 map.reserve(iter->second.elemIds.size());
398
399 for (const auto &elemId : iter->second.elemIds) {
400 map.push_back(elemId);
401 }
402 }
403
404 void TextMesh::element_map(Ioss::Int64Vector &map) const { raw_element_map(map); }
405
406 void TextMesh::element_map(Ioss::IntVector &map) const { raw_element_map(map); }
407
408 template <typename INT> void TextMesh::raw_element_map(std::vector<INT> &map) const
409 {
410 INT count = element_count_proc();
411 map.resize(count);
412
413 for (auto iter = m_blockPartition.begin(); iter != m_blockPartition.end(); iter++) {
414 size_t offset = iter->second.offset;
415 size_t blockCount = 0;
416 for (const auto &elemId : iter->second.elemIds) {
417 map[offset + blockCount++] = elemId;
418 }
419 }
420 }
421
422 void TextMesh::coordinates(std::vector<double> &coord) const
423 {
424 /* create global coordinates */
425 int64_t count = node_count_proc();
426 coord.resize(count * spatial_dimension());
427 coordinates(Data(coord));
428 }
429
430 void TextMesh::coordinates(double *coord) const
431 {
432 if (!m_data.coords.has_coordinate_data())
433 return;
434
435 /* create global coordinates */
436 const auto &nodes = m_data.nodes_on_proc(m_myProcessor);
437 unsigned offset = 0;
438
439 for (auto node : nodes) {
440 const std::vector<double> &coords = m_data.coords[node];
441 for (unsigned i = 0; i < coords.size(); i++) {
442 coord[offset++] = coords[i];
443 }
444 }
445 }
446
447 void TextMesh::coordinates(std::vector<double> &x, std::vector<double> &y,
448 std::vector<double> &z) const
449 {
450 if (!m_data.coords.has_coordinate_data())
451 return;
452
453 /* create global coordinates */
454 int64_t count = node_count_proc();
455 x.reserve(count);
456 y.reserve(count);
457 z.reserve(count);
458
459 const auto &nodes = m_data.nodes_on_proc(m_myProcessor);
460
461 for (auto node : nodes) {
462 const std::vector<double> &coords = m_data.coords[node];
463
464 x.push_back(coords[0]);
465 y.push_back(coords[1]);
466 z.push_back((spatial_dimension() == 3) ? coords[2] : 0.0);
467 }
468 }
469
470 void TextMesh::coordinates(int component, std::vector<double> &xyz) const
471 {
472 /* create global coordinates */
473 size_t count = node_count_proc();
474 xyz.resize(count);
475 coordinates(component, Data(xyz));
476 }
477
478 void TextMesh::coordinates(int component, double *xyz) const
479 {
480 const auto &nodes = m_data.nodes_on_proc(m_myProcessor);
481 unsigned offset = 0;
482
483 if (component == 1) {
484 for (auto node : nodes) {
485 const std::vector<double> &coords = m_data.coords[node];
486 xyz[offset++] = coords[0];
487 }
488 }
489 else if (component == 2) {
490 for (auto node : nodes) {
491 const std::vector<double> &coords = m_data.coords[node];
492 xyz[offset++] = coords[1];
493 }
494 }
495 else if (component == 3) {
496 for (auto node : nodes) {
497 const std::vector<double> &coords = m_data.coords[node];
498 xyz[offset++] = (spatial_dimension() == 3) ? coords[2] : 0.0;
499 }
500 }
501 }
502
503 void TextMesh::nodeset_nodes(EntityId id, Ioss::Int64Vector &nodes) const
504 {
505 const NodesetData *nodeset = m_data.nodesets.get_group_data(id);
506 if (nullptr == nodeset)
507 return;
508
509 nodes.resize(nodeset_node_count_proc(id));
510
511 int64_t count = 0;
512 const std::set<EntityId> &myNodes = m_data.nodes_on_proc(m_myProcessor);
513
514 for (EntityId nodeId : nodeset->data) {
515 if (myNodes.count(nodeId) > 0) {
516 nodes[count++] = nodeId;
517 }
518 }
519 }
520
521 void TextMesh::sideset_elem_sides(EntityId id, Ioss::Int64Vector &elemSides) const
522 {
523 const SidesetData *sideset = m_data.sidesets.get_group_data(id);
524 if (nullptr == sideset)
525 return;
526
527 elemSides.resize(2 * sideset_side_count_proc(id));
528
529 int64_t count = 0;
530 int myProc = m_myProcessor;
531
532 for (const std::pair<EntityId, int> &elemSidePair : sideset->data) {
533 EntityId elemId = elemSidePair.first;
534 int side = elemSidePair.second;
535 auto iter = std::find(m_data.elementDataVec.begin(), m_data.elementDataVec.end(), elemId);
536 if (iter != m_data.elementDataVec.end() && iter->proc == myProc) {
537 elemSides[count++] = elemId;
538 elemSides[count++] = side;
539 }
540 }
541 }
542
543 std::set<std::string> TextMesh::get_blocks_touched_by_sideset(const SidesetData *sideset) const
544 {
545 std::set<std::string> touchedBlocks;
546
547 int myProc = m_myProcessor;
548
549 for (const std::pair<EntityId, int> &elemSidePair : sideset->data) {
550 EntityId elemId = elemSidePair.first;
551 auto iter = std::find(m_data.elementDataVec.begin(), m_data.elementDataVec.end(), elemId);
552 if (iter != m_data.elementDataVec.end() && iter->proc == myProc) {
553 touchedBlocks.insert(iter->partName);
554 }
555 }
556
557 return touchedBlocks;
558 }
559
560 std::vector<std::string> TextMesh::sideset_touching_blocks(EntityId setId) const
561 {
562 std::vector<std::string> touchedBlocks;
563
564 const SidesetData *sideset = m_data.sidesets.get_group_data(setId);
565 if (nullptr != sideset) {
566 std::set<std::string> blockList = get_blocks_touched_by_sideset(sideset);
567 touchedBlocks.reserve(blockList.size());
568 for (const std::string &block : blockList) {
569 touchedBlocks.push_back(block);
570 }
571 }
572
573 return touchedBlocks;
574 }
575
576 void TextMesh::connectivity(EntityId id, Ioss::Int64Vector &connect) const
577 {
578 Topology topo = get_topology_for_part(id);
579
580 int64_t npe = topo.num_nodes();
581 connect.resize(element_count_proc(id) * npe);
582
583 raw_connectivity(id, Data(connect));
584 }
585
586 void TextMesh::connectivity(EntityId id, Ioss::IntVector &connect) const
587 {
588 Topology topo = get_topology_for_part(id);
589
590 int64_t npe = topo.num_nodes();
591 connect.resize(element_count_proc(id) * npe);
592
593 raw_connectivity(id, Data(connect));
594 }
595
596 void TextMesh::connectivity(EntityId id, int64_t *connect) const
597 {
598 raw_connectivity(id, connect);
599 }
600
601 void TextMesh::connectivity(EntityId id, int *connect) const { raw_connectivity(id, connect); }
602
603 template <typename INT> void TextMesh::raw_connectivity(EntityId id, INT *connect) const
604 {
605 unsigned offset = 0;
606 auto blockIter = m_blockPartition.find(id);
607 ThrowRequireMsg(blockIter != m_blockPartition.end(),
608 "Could not find block with id: " << id << " in block partition");
609
610 for (const auto &elemId : blockIter->second.elemIds) {
611 auto connIter = m_elementConnectivity.find(elemId);
612 ThrowRequireMsg(connIter != m_elementConnectivity.end(),
613 "Could not find element with id: " << id << " in connectivity map");
614
615 for (auto nodeId : connIter->second) {
616 connect[offset++] = nodeId;
617 }
618 }
619 }
620
621 void TextMesh::set_variable_count(const std::string &type, size_t count)
622 {
623 if (type == "global") {
624 m_variableCount[Ioss::REGION] = count;
625 }
626 else if (type == "element") {
627 m_variableCount[Ioss::ELEMENTBLOCK] = count;
628 }
629 else if (type == "nodal" || type == "node") {
630 m_variableCount[Ioss::NODEBLOCK] = count;
631 }
632 else if (type == "nodeset") {
633 m_variableCount[Ioss::NODESET] = count;
634 }
635 else if (type == "surface" || type == "sideset") {
636 m_variableCount[Ioss::SIDEBLOCK] = count;
637 }
638 else if (type == "assembly") {
639 m_variableCount[Ioss::ASSEMBLY] = count;
640 }
641 else {
642 std::ostringstream errmsg;
643 fmt::print(errmsg,
644 "ERROR: (Iotm::TextMesh::set_variable_count)\n"
645 " Unrecognized variable type '{}'. Valid types are:\n"
646 " global, element, node, nodal, nodeset, surface, sideset, assembly.\n",
647 type);
648 IOSS_ERROR(errmsg);
649 }
650 }
651
652 std::vector<std::string> TextMesh::get_part_names() const
653 {
654 return m_data.partIds.get_part_names_sorted_by_id();
655 }
656
657 EntityId TextMesh::get_part_id(const std::string &name) const { return m_data.partIds.get(name); }
658
659 std::vector<std::string> TextMesh::get_nodeset_names() const
660 {
661 return m_data.nodesets.get_part_names();
662 }
663
664 std::string TextMesh::get_nodeset_name(EntityId id) const
665 {
666 const NodesetData *nodeset = m_data.nodesets.get_group_data(id);
667 ThrowRequireMsg(nullptr != nodeset, "Could not find nodeset with id" << id);
668 return nodeset->name;
669 }
670
671 EntityId TextMesh::get_nodeset_id(const std::string &name) const
672 {
673 const NodesetData *nodeset = m_data.nodesets.get_group_data(name);
674 ThrowRequireMsg(nullptr != nodeset, "Could not find nodeset with name" << name);
675 return nodeset->id;
676 }
677
678 std::vector<std::string> TextMesh::get_sideset_names() const
679 {
680 return m_data.sidesets.get_part_names();
681 }
682
683 std::string TextMesh::get_sideset_name(EntityId id) const
684 {
685 const SidesetData *sideset = m_data.sidesets.get_group_data(id);
686 ThrowRequireMsg(nullptr != sideset, "Could not find sideset with id" << id);
687 return sideset->name;
688 }
689
690 EntityId TextMesh::get_sideset_id(const std::string &name) const
691 {
692 const SidesetData *sideset = m_data.sidesets.get_group_data(name);
693 ThrowRequireMsg(nullptr != sideset, "Could not find sideset with name" << name);
694 return sideset->id;
695 }
696
697 std::vector<std::string> TextMesh::get_assembly_names() const
698 {
699 return m_data.assemblies.get_part_names();
700 }
701
702 std::string TextMesh::get_assembly_name(EntityId id) const
703 {
704 const AssemblyData *assembly = m_data.assemblies.get_group_data(id);
705 ThrowRequireMsg(nullptr != assembly, "Could not find assembly with id" << id);
706 return assembly->name;
707 }
708
709 EntityId TextMesh::get_assembly_id(const std::string &name) const
710 {
711 const AssemblyData *assembly = m_data.assemblies.get_group_data(name);
712 ThrowRequireMsg(nullptr != assembly, "Could not find assembly with name" << name);
713 return assembly->id;
714 }
715
716 Ioss::EntityType TextMesh::assembly_type_to_entity_type(const AssemblyType type)
717 {
718 if (type == AssemblyType::BLOCK) {
719 return Ioss::ELEMENTBLOCK;
720 }
721 else if (type == AssemblyType::NODESET) {
722 return Ioss::NODESET;
723 }
724 else if (type == AssemblyType::SIDESET) {
725 return Ioss::SIDESET;
726 }
727 else if (type == AssemblyType::ASSEMBLY) {
728 return Ioss::ASSEMBLY;
729 }
730
731 return Ioss::INVALID_TYPE;
732 }
733
734 Ioss::EntityType TextMesh::get_assembly_type(const std::string &name) const
735 {
736 const AssemblyData *assembly = m_data.assemblies.get_group_data(name);
737 ThrowRequireMsg(nullptr != assembly, "Could not find assembly with name" << name);
738
739 AssemblyType type = assembly->get_assembly_type();
740 return assembly_type_to_entity_type(type);
741 }
742
743 std::vector<std::string> TextMesh::get_assembly_members(const std::string &name) const
744 {
745 const AssemblyData *assembly = m_data.assemblies.get_group_data(name);
746 ThrowRequireMsg(nullptr != assembly, "Could not find assembly with name" << name);
747
748 return assembly->data;
749 }
750
751 int64_t TextMesh::assembly_count() const { return m_data.assemblies.get_group_data().size(); }
752
753 std::set<EntityId> TextMesh::get_local_element_ids_for_block(EntityId id) const
754 {
755 size_t count = element_count_proc(id);
756 std::set<EntityId> elemIds;
757
758 int myProc = m_myProcessor;
759 for (const auto &elementData : m_data.elementDataVec) {
760 if (get_part_id(elementData.partName) == id && elementData.proc == myProc) {
761 elemIds.insert(elementData.identifier);
762 }
763 }
764
765 ThrowRequireMsg(elemIds.size() == count, "Elements in ElementData vector are not unique");
766 return elemIds;
767 }
768
769 void TextMesh::build_part_to_topology_map()
770 {
771 for (const auto &elementData : m_data.elementDataVec) {
772 auto iter = m_partToTopology.find(elementData.partName);
773 if (iter == m_partToTopology.end()) {
774 m_partToTopology[elementData.partName] = elementData.topology;
775 }
776 else {
777 ThrowRequireMsg(iter->second == elementData.topology,
778 "Element with id: "
779 << elementData.identifier << " in part named: " << elementData.partName
780 << " is attempting to reset the part topology: " << iter->second
781 << " with: " << elementData.topology.name());
782 }
783 }
784 }
785
786 std::vector<EntityId> TextMesh::get_part_ids(const std::vector<std::string> &partNames)
787 {
788 std::vector<EntityId> partIds;
789
790 size_t numParts = partNames.size();
791 partIds.resize(numParts);
792
793 for (size_t i = 0; i < numParts; ++i) {
794 partIds[i] = get_part_id(partNames[i]);
795 }
796
797 return partIds;
798 }
799
800 std::vector<size_t> TextMesh::get_part_offsets(const std::vector<EntityId> &partIds)
801 {
802 std::vector<size_t> offsets;
803
804 size_t numParts = partIds.size();
805 offsets.resize(numParts);
806
807 for (size_t i = 0; i < numParts; ++i) {
808 offsets[i] = 0;
809 }
810
811 for (size_t i = 1; i < numParts; ++i) {
812 offsets[i] = offsets[i - 1] + element_count_proc(partIds[i - 1]);
813 }
814
815 return offsets;
816 }
817
818 void TextMesh::build_block_partition_map()
819 {
820 std::vector<std::string> partNames = get_part_names();
821 std::vector<EntityId> partIds = get_part_ids(partNames);
822 std::vector<size_t> offsets = get_part_offsets(partIds);
823
824 size_t numParts = partNames.size();
825
826 for (size_t i = 0; i < numParts; ++i) {
827 const std::string &name = partNames[i];
828 EntityId id = partIds[i];
829
830 m_blockPartition[id] = BlockPartition(offsets[i], name, get_local_element_ids_for_block(id));
831 }
832 }
833
834 void TextMesh::build_element_connectivity_map()
835 {
836 int myProc = m_myProcessor;
837 std::vector<EntityId> nodeIds;
838
839 for (const auto &elementData : m_data.elementDataVec) {
840 if (elementData.proc == myProc) {
841 nodeIds.clear();
842 for (auto id : elementData.nodeIds) {
843 nodeIds.push_back(id);
844 }
845
846 m_elementConnectivity[elementData.identifier] = nodeIds;
847 }
848 }
849 }
850
851 int64_t TextMesh::sideblock_side_count(EntityId id, const std::string &sideBlockName) const
852 {
853 int64_t count = 0;
854
855 const SidesetData *sideset = m_data.sidesets.get_group_data(id);
856 if (nullptr != sideset) {
857 SideBlockInfo info = sideset->get_side_block_info(sideBlockName);
858 count = info.sideIndex.size();
859 }
860 return count;
861 }
862
863 int64_t TextMesh::sideblock_side_count_proc(EntityId id, const std::string &sideBlockName) const
864 {
865 int64_t count = 0;
866
867 const SidesetData *sideset = m_data.sidesets.get_group_data(id);
868 if (nullptr != sideset) {
869 SideBlockInfo info = sideset->get_side_block_info(sideBlockName);
870 count = sideset->get_sideblock_indices_local_to_proc(info, m_myProcessor).size();
871 }
872 return count;
873 }
874
875 void TextMesh::sideblock_elem_sides(EntityId id, const std::string &sideBlockName,
876 Ioss::Int64Vector &elemSides) const
877 {
878 const SidesetData *sideset = m_data.sidesets.get_group_data(id);
879 if (nullptr == sideset)
880 return;
881
882 SideBlockInfo info = sideset->get_side_block_info(sideBlockName);
883 Ioss::ElementTopology *topology = Ioss::ElementTopology::factory(info.elementTopology, true);
884 Ioss::ElementTopology *side_topology = Ioss::ElementTopology::factory(info.sideTopology, true);
885
886 int sideOffset = Ioss::Utils::get_side_offset(topology, side_topology);
887
888 std::vector<size_t> localSideIndex =
889 sideset->get_sideblock_indices_local_to_proc(info, m_myProcessor);
890 elemSides.resize(2 * localSideIndex.size());
891
892 int64_t count = 0;
893
894 for (size_t sideIndex : localSideIndex) {
895 const SidesetData::DataType &elemSidePair = sideset->data[sideIndex];
896 EntityId elemId = elemSidePair.first;
897 int side = elemSidePair.second;
898
899 elemSides[count++] = elemId;
900 elemSides[count++] = side - sideOffset;
901 }
902 }
903
904 std::vector<SideBlockInfo>
905 TextMesh::get_side_block_info_for_sideset(const std::string &name) const
906 {
907 const SidesetData *sideset = m_data.sidesets.get_group_data(name);
908 ThrowRequireMsg(nullptr != sideset, "Could not find sideset with name" << name);
909 return sideset->get_side_block_info();
910 }
911
912 std::vector<size_t> TextMesh::get_local_side_block_indices(const std::string &name,
913 const SideBlockInfo &info) const
914 {
915 const SidesetData *sideset = m_data.sidesets.get_group_data(name);
916 ThrowRequireMsg(nullptr != sideset, "Could not find sideset with name" << name);
917 ThrowRequireMsg(name == info.parentName,
918 "SideBlock: " << info.name << " with parent: " << info.parentName
919 << " was not created from sideset: " << name);
920 return sideset->get_sideblock_indices_local_to_proc(info, m_myProcessor);
921 }
922
923 SplitType TextMesh::get_sideset_split_type(const std::string &name) const
924 {
925 const SidesetData *sideset = m_data.sidesets.get_group_data(name);
926 ThrowRequireMsg(nullptr != sideset, "Could not find sideset with name" << name);
927
928 return sideset->get_split_type();
929 }
930
931 void TextMesh::update_block_omissions_from_assemblies(
932 Ioss::Region *region, std::vector<std::string> &assemblyOmissions,
933 std::vector<std::string> &assemblyInclusions, std::vector<std::string> &blockOmissions,
934 std::vector<std::string> &blockInclusions) const
935 {
936 // Query number of assemblies...
937 if (assembly_count() > 0) {
938 std::vector<std::string> exclusions;
939 std::vector<std::string> inclusions;
940
941 AssemblyTreeFilter inclusionFilter(region, Ioss::ELEMENTBLOCK, m_data.assemblies);
942 AssemblyTreeFilter exclusionFilter(region, Ioss::ELEMENTBLOCK, m_data.assemblies);
943
944 for (const std::string &assemblyName : m_data.assemblies.get_part_names()) {
945 const AssemblyData *assembly = m_data.assemblies.get_group_data(assemblyName);
946 ThrowRequireMsg(nullptr != assembly, "Could not find assembly with name" << assemblyName);
947
948 bool omitAssembly =
949 std::binary_search(assemblyOmissions.begin(), assemblyOmissions.end(), assembly->name);
950 bool includeAssembly = std::binary_search(assemblyInclusions.begin(),
951 assemblyInclusions.end(), assembly->name);
952
953 if (omitAssembly) {
954 exclusionFilter.update_list_from_assembly_tree(assembly, exclusions);
955 }
956
957 if (includeAssembly) {
958 inclusionFilter.update_list_from_assembly_tree(assembly, inclusions);
959 }
960 }
961
962 exclusionFilter.update_assembly_filter_list(assemblyOmissions);
963 inclusionFilter.update_assembly_filter_list(assemblyInclusions);
964
965 Ioss::Utils::insert_sort_and_unique(exclusions, blockOmissions);
966 Ioss::Utils::insert_sort_and_unique(inclusions, blockInclusions);
967 }
968 }
969
970 void TextMesh::compute_block_membership_impl(
971 const SidesetData &sidesetData, const SideBlockInfo &sideBlock,
972 std::vector<std::string> &sideBlockTouchingBlockParts) const
973 {
974 std::vector<int> blockIndex(m_data.partIds.size(), 0);
975
976 std::vector<std::string> partNames = m_data.partIds.get_part_names_sorted_by_id();
977 std::sort(partNames.begin(), partNames.end());
978
979 if (blockIndex.size() == 1) {
980 blockIndex[0] = 1;
981 }
982 else {
983 for (size_t sideIndex : sideBlock.sideIndex) {
984 EntityId elemId = sidesetData.data[sideIndex].first;
985 auto elemIter = text_mesh::bound_search(
986 m_data.elementDataVec.begin(), m_data.elementDataVec.end(), elemId, ElementDataLess());
987 ThrowRequireMsg(elemIter != m_data.elementDataVec.end(),
988 "Could not find reference element " << elemId
989 << " in sideset: " << sidesetData.name);
990
991 const std::string &partName = elemIter->partName;
992
993 auto partNameIter = text_mesh::bound_search(partNames.begin(), partNames.end(), partName);
994 ThrowRequireMsg(partNameIter != partNames.end(),
995 "Could not find part: " << partName << " referenced by element: " << elemId
996 << " in list of registered parts");
997
998 unsigned index = std::distance(partNames.begin(), partNameIter);
999 ThrowRequireMsg(index < m_data.partIds.size(),
1000 "Fatal error in computing iterator distance");
1001
1002 blockIndex[index] = 1;
1003 }
1004 }
1005
1006 for (unsigned i = 0; i < blockIndex.size(); i++) {
1007 if (blockIndex[i] == 1) {
1008 sideBlockTouchingBlockParts.push_back(partNames[i]);
1009 }
1010 }
1011 }
1012
1013 void TextMesh::compute_block_membership(const std::string &sideSetName,
1014 const std::string &sideBlockName,
1015 std::vector<std::string> &block_membership) const
1016 {
1017 const SidesetData *sidesetData = m_data.sidesets.get_group_data(sideSetName);
1018 ThrowRequireMsg(nullptr != sidesetData, "Could not find sideset with name: " << sideSetName);
1019
1020 SideBlockInfo sideBlock = sidesetData->get_side_block_info(sideBlockName);
1021 block_membership.clear();
1022
1023 if (!sideBlock.touchingBlock.empty()) {
1024 block_membership.push_back(sideBlock.touchingBlock);
1025 }
1026 else {
1027 compute_block_membership_impl(*sidesetData, sideBlock, block_membership);
1028 }
1029
1030 std::sort(block_membership.begin(), block_membership.end());
1031 }
1032
1033 } // namespace Iotm