Debian Codegraph @

packages/seacas/libraries/ioss/src/unit_tests/UnitTestIotmTextMeshFixture.h

raw
1 // Copyright(C) 1999-2020, 2022, 2023, 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 #pragma once
8
9 #include "Ioss_CodeTypes.h"
10
11 #include "Ionit_Initializer.h"
12 #include "Ioss_Assembly.h"
13 #include "Ioss_CommSet.h"
14 #include "Ioss_DBUsage.h"
15 #include "Ioss_DatabaseIO.h" // for DatabaseIO
16 #include "Ioss_ElementBlock.h"
17 #include "Ioss_ElementTopology.h"
18 #include "Ioss_EntityType.h" // for EntityType, etc
19 #include "Ioss_Field.h" // for Field, etc
20 #include "Ioss_GroupingEntity.h" // for GroupingEntity
21 #include "Ioss_IOFactory.h" // for IOFactory
22 #include "Ioss_MeshType.h" // for MeshType, etc
23 #include "Ioss_NodeBlock.h"
24 #include "Ioss_NodeSet.h"
25 #include "Ioss_ParallelUtils.h"
26 #include "Ioss_PropertyManager.h"
27 #include "Ioss_Region.h"
28 #include "Ioss_SideBlock.h"
29 #include "Ioss_SideSet.h"
30 #include "Ioss_StandardElementTypes.h"
31
32 #include <gtest/gtest.h>
33 #ifdef SEACAS_HAVE_MPI
34 #include <mpi.h>
35 #endif
36 #include <string>
37 #include <unordered_map>
38
39 #include <memory>
40 #include <string>
41 #include <strings.h>
42 #include <vector>
43
44 #include "text_mesh/Iotm_TextMeshNodeset.h"
45 #include "text_mesh/Iotm_TextMeshSideset.h"
46 #include "text_mesh/Iotm_TextMeshTopologyMapping.h"
47 #include "text_mesh/Iotm_TextMeshUtils.h"
48
49 #define ThrowRequireWithMsg(expr, message) \
50 do { \
51 if (!(expr)) { \
52 std::ostringstream internal_throw_require_oss; \
53 internal_throw_require_oss << message; \
54 throw std::logic_error(internal_throw_require_oss.str()); \
55 } \
56 } while (false)
57
58 using Topology = Iotm::TopologyMapEntry;
59 using TopologyMapping = Iotm::IossTopologyMapping;
60 using EntityId = int64_t;
61 using EntityIdVector = std::vector<EntityId>;
62 using EntityIdSet = std::set<EntityId>;
63 using TextMeshData = Iotm::text_mesh::TextMeshData<EntityId, Topology>;
64 using ElementData = Iotm::text_mesh::ElementData<EntityId, Topology>;
65 using SidesetData = Iotm::text_mesh::SidesetData<EntityId, Topology>;
66 using NodesetData = Iotm::text_mesh::NodesetData<EntityId>;
67 using Coordinates = Iotm::text_mesh::Coordinates<EntityId>;
68 using TextMeshParser = Iotm::text_mesh::TextMeshParser<EntityId, TopologyMapping>;
69 using SideAdjacencyGraph = Iotm::text_mesh::SideAdjacencyGraph<EntityId, Topology>;
70 using SideBlockInfo = Iotm::text_mesh::SideBlockInfo;
71 using SideEntry = std::pair<EntityId, int>;
72 using SideVector = std::vector<SideEntry>;
73 using SplitType = Iotm::text_mesh::SplitType;
74
75 struct Adjacency
76 {
77 using NeighborVector = std::vector<std::pair<int, SideAdjacencyGraph::IndexType>>;
78 using SimpleNeighborVector = std::vector<SideAdjacencyGraph::IndexType>;
79
80 size_t elemIndex;
81 NeighborVector neighborIndices;
82
83 Adjacency(size_t elemIndex_, const NeighborVector &neighborIndices_)
84 : elemIndex(elemIndex_), neighborIndices(neighborIndices_)
85 {
86 }
87
88 Adjacency(size_t elemIndex_, const SimpleNeighborVector &neighborIndices_)
89 : elemIndex(elemIndex_), neighborIndices(get_full_neighbor_vector(neighborIndices_))
90 {
91 }
92
93 NeighborVector get_full_neighbor_vector(const SimpleNeighborVector &simpleNeighborVector)
94 {
95 NeighborVector fullNeighborVector;
96 fullNeighborVector.reserve(simpleNeighborVector.size());
97
98 for (unsigned i = 0; i < simpleNeighborVector.size(); i++) {
99 fullNeighborVector.push_back(std::make_pair(static_cast<int>(i), simpleNeighborVector[i]));
100 }
101 return fullNeighborVector;
102 }
103 };
104
105 struct SideEntryLess
106 {
107 inline bool operator()(const SideEntry &lhs, const SideEntry &rhs) const
108 {
109 if (lhs.first < rhs.first)
110 return true;
111 else if (lhs.first == rhs.first && lhs.second < rhs.second)
112 return true;
113
114 return false;
115 }
116 };
117
118 namespace Iotm {
119 namespace unit_test {
120
121 class AssemblyTreeGraph
122 {
123 public:
124 AssemblyTreeGraph() = delete;
125 AssemblyTreeGraph(const AssemblyTreeGraph &) = delete;
126
127 AssemblyTreeGraph(Ioss::Region *region) : m_region(region) {}
128
129 public:
130 std::vector<std::string> get_unique_leaf_members(const std::string &name)
131 {
132 m_leafMembers.clear();
133 m_visitedAssemblies.clear();
134
135 for (Ioss::Assembly *assembly : m_region->get_assemblies()) {
136 m_visitedAssemblies[assembly] = false;
137 }
138
139 traverse_tree(m_region->get_assembly(name));
140
141 std::sort(m_leafMembers.begin(), m_leafMembers.end(), std::less<std::string>());
142 auto endIter = std::unique(m_leafMembers.begin(), m_leafMembers.end());
143 m_leafMembers.resize(endIter - m_leafMembers.begin());
144
145 return m_leafMembers;
146 }
147
148 private:
149 void traverse_tree(const Ioss::Assembly *assembly)
150 {
151 // Walk the tree without cyclic dependency
152 if (assembly != nullptr) {
153 if (m_visitedAssemblies[assembly] == false) {
154 m_visitedAssemblies[assembly] = true;
155
156 const Ioss::EntityType assemblyType = assembly->get_member_type();
157 if (Ioss::ASSEMBLY != assemblyType) {
158 for (const Ioss::GroupingEntity *ge : assembly->get_members()) {
159 m_leafMembers.push_back(ge->name());
160 }
161 }
162 else {
163 for (const Ioss::GroupingEntity *ge : assembly->get_members()) {
164 const Ioss::Assembly *assemblyMember = dynamic_cast<const Ioss::Assembly *>(ge);
165 ThrowRequireWithMsg(nullptr != assemblyMember,
166 "Non-assembly member: " << ge->name()
167 << " in ASSEMBLY rank assembly: "
168 << assembly->name());
169 traverse_tree(assemblyMember);
170 }
171 }
172 }
173 }
174 }
175
176 Ioss::Region *m_region = nullptr;
177 mutable std::unordered_map<const Ioss::Assembly *, bool> m_visitedAssemblies;
178 mutable std::vector<std::string> m_leafMembers;
179 };
180
181 class TextMeshFixture : public ::testing::Test
182 {
183 protected:
184 using PartNameId = std::pair<std::string, unsigned>;
185
186 struct ElementInfo
187 {
188 const Ioss::ElementTopology *topology;
189 EntityIdVector connectivity;
190
191 ElementInfo() : topology(nullptr) {}
192 ElementInfo(const Ioss::ElementTopology *topology_, const EntityIdVector &connectivity_)
193 : topology(topology_), connectivity(connectivity_)
194 {
195 }
196 };
197
198 struct PartInfo
199 {
200 std::string blockName;
201 EntityIdSet ids;
202 };
203
204 TextMeshFixture(unsigned spatialDimension) : m_spatialDimension(spatialDimension)
205 {
206 Ioss::Init::Initializer io;
207 m_topologyMapping.initialize_topology_map();
208 }
209
210 ~TextMeshFixture() { delete m_region; }
211
212 int get_parallel_size() { return Ioss::ParallelUtils(get_comm()).parallel_size(); }
213
214 int get_parallel_rank() { return Ioss::ParallelUtils(get_comm()).parallel_rank(); }
215
216 void fill_mesh(const std::string &meshDesc)
217 {
218 std::pair<std::string, std::string> result = get_database_type_and_filename(meshDesc);
219
220 std::string type = result.first;
221 std::string filename = result.second;
222
223 EXPECT_EQ("textmesh", type);
224
225 create_database(filename, type);
226 create_ioss_region();
227 }
228
229 Ioss_MPI_Comm get_comm() const { return Ioss::ParallelUtils::comm_world(); }
230
231 std::string get_mesh_desc(const std::string &textMeshDesc)
232 {
233 std::string header = "textmesh:";
234 std::string meshDesc = header + textMeshDesc;
235 return meshDesc;
236 }
237
238 std::string get_mesh_desc(const std::string &textMeshDesc, unsigned dimension)
239 {
240 std::stringstream dim;
241 dim << "|dimension:" << dimension;
242
243 std::string meshDesc = get_mesh_desc(textMeshDesc) + dim.str();
244 return meshDesc;
245 }
246
247 void verify_shared_nodes(const EntityIdVector &nodeIds, int sharingProc)
248 {
249 EXPECT_EQ(nodeIds.size(), get_node_sharing_count(sharingProc));
250
251 for (EntityId nodeId : nodeIds) {
252 EXPECT_TRUE(node_is_shared_with_proc(nodeId, sharingProc));
253 }
254 }
255
256 void verify_num_elements(size_t goldCount)
257 {
258 size_t count = get_element_count();
259 EXPECT_EQ(goldCount, count);
260 }
261
262 void verify_single_element(EntityId elemId, const std::string &textMeshTopologyName,
263 const EntityIdVector &nodeIds)
264 {
265 Topology topology = m_topologyMapping.topology(textMeshTopologyName);
266 ElementInfo info = get_element_info(elemId);
267 EXPECT_TRUE(is_valid_element(info));
268 EXPECT_EQ(topology, info.topology);
269 verify_nodes_on_element(info, nodeIds);
270 }
271
272 void verify_num_sidesets(size_t goldCount)
273 {
274 ThrowRequireWithMsg(m_region != nullptr, "Ioss region has not been created");
275 size_t count = m_region->get_sidesets().size();
276 EXPECT_EQ(goldCount, count);
277 }
278
279 void verify_sideset_subset(const Ioss::SideSet *sideset, const unsigned id,
280 const std::vector<std::string> &subsetNames)
281 {
282 EXPECT_TRUE(nullptr != sideset);
283 EXPECT_EQ(id, sideset->get_property("id").get_int());
284
285 if (subsetNames.empty()) {
286 EXPECT_EQ(1u, sideset->get_side_blocks().size());
287 }
288 else {
289 EXPECT_EQ(subsetNames.size(), sideset->get_side_blocks().size());
290 }
291
292 for (std::string subsetName : subsetNames) {
293 std::transform(subsetName.begin(), subsetName.end(), subsetName.begin(), ::toupper);
294 Ioss::SideBlock *sideBlock = sideset->get_side_block(subsetName);
295 EXPECT_TRUE(nullptr != sideBlock);
296 EXPECT_EQ(id, sideBlock->get_property("id").get_int());
297 }
298 }
299
300 void verify_single_sideset(const std::string &name, const unsigned id,
301 const SideVector &goldElemSidePairs)
302 {
303 verify_single_sideset(name, id, std::vector<std::string>{}, goldElemSidePairs);
304 }
305
306 void verify_single_sideset(const std::string &name, const unsigned id,
307 const std::vector<std::string> &subsets,
308 const SideVector &goldElemSidePairs)
309 {
310 Ioss::SideSet *sideset = get_sideset(name);
311 verify_sideset_subset(sideset, id, subsets);
312
313 EXPECT_TRUE(nullptr != sideset);
314
315 SideVector elemSidePairs = get_element_side_pairs_from_sideset(sideset);
316 std::sort(elemSidePairs.begin(), elemSidePairs.end(), SideEntryLess());
317
318 for (const SideEntry &sideEntry : goldElemSidePairs) {
319 EntityId elemId = sideEntry.first;
320 int side = sideEntry.second;
321
322 ElementInfo info = get_element_info(elemId);
323 EXPECT_TRUE(is_valid_element(info));
324
325 EXPECT_TRUE(side > 0);
326 EXPECT_TRUE(side <= (int)info.topology->number_boundaries());
327
328 EXPECT_TRUE(std::binary_search(elemSidePairs.begin(), elemSidePairs.end(), sideEntry,
329 SideEntryLess()));
330 }
331 }
332
333 void verify_num_nodesets(size_t goldCount)
334 {
335 ThrowRequireWithMsg(m_region != nullptr, "Ioss region has not been created");
336 size_t count = m_region->get_nodesets().size();
337 EXPECT_EQ(goldCount, count);
338 }
339
340 void verify_single_nodeset(const std::string &name, const unsigned id,
341 const EntityIdVector &goldNodeIds)
342 {
343 Ioss::NodeSet *nodeset = get_nodeset(name);
344 EXPECT_TRUE(nullptr != nodeset);
345 EXPECT_EQ(id, nodeset->get_property("id").get_int());
346
347 EntityIdVector nodeIds = get_node_ids_from_nodeset(nodeset);
348 std::sort(nodeIds.begin(), nodeIds.end());
349
350 for (EntityId node : goldNodeIds) {
351 EXPECT_TRUE(std::binary_search(nodeIds.begin(), nodeIds.end(), node));
352 }
353 }
354
355 void verify_num_assemblies(size_t goldCount)
356 {
357 ThrowRequireWithMsg(m_region != nullptr, "Ioss region has not been created");
358 size_t count = m_region->get_assemblies().size();
359 EXPECT_EQ(goldCount, count);
360 }
361
362 void verify_single_assembly(const std::string &name, const unsigned id,
363 const std::vector<std::string> &goldMembers)
364 {
365 Ioss::Assembly *assembly = get_assembly(name);
366 EXPECT_TRUE(nullptr != assembly);
367 EXPECT_EQ(id, assembly->get_property("id").get_int());
368
369 AssemblyTreeGraph graph(m_region);
370 std::vector<std::string> leafMembers = graph.get_unique_leaf_members(name);
371 EXPECT_EQ(goldMembers.size(), leafMembers.size());
372
373 for (size_t i = 0; i < goldMembers.size(); i++) {
374 const std::string &goldMember = goldMembers[i];
375 const std::string &leafMember = leafMembers[i];
376 EXPECT_EQ(0, strcasecmp(goldMember.c_str(), leafMember.c_str()))
377 << "Comparison failure for " << name << ": " << goldMember << " <-> " << leafMember;
378 }
379 }
380
381 void verify_part_membership(const std::vector<PartInfo> golds)
382 {
383 for (const PartInfo &gold : golds) {
384 Ioss::ElementBlock *block = get_element_block(gold.blockName);
385
386 verify_block(block);
387 verify_elements_on_block(block, gold.ids);
388 }
389 }
390
391 void verify_part_ids(const std::vector<PartNameId> &golds)
392 {
393 for (const PartNameId &gold : golds) {
394 Ioss::ElementBlock *block = get_element_block(gold.first);
395
396 verify_block(block);
397 unsigned id = block->get_property("id").get_int();
398 EXPECT_EQ(id, gold.second);
399 }
400 }
401
402 void verify_nodes_on_element(const ElementInfo &info, const EntityIdVector &goldNodeIds)
403 {
404 EXPECT_EQ(goldNodeIds, info.connectivity);
405 }
406
407 void verify_coordinates(const EntityIdVector &goldNodeIds,
408 const std::vector<double> &goldCoordinates)
409 {
410 CoordinateVerifier cv(*m_region, goldNodeIds, goldCoordinates);
411 cv.verify();
412 }
413
414 void setup_text_mesh(const std::string &textMeshDesc)
415 {
416 fill_mesh(get_mesh_desc(textMeshDesc, m_spatialDimension));
417 }
418
419 std::string get_topology_name(const std::string &textMeshTopologyName)
420 {
421 return m_topologyMapping.topology(textMeshTopologyName).name();
422 }
423
424 size_t db_api_int_size() const
425 {
426 assert(m_database != nullptr);
427 return m_database->int_byte_size_api();
428 }
429
430 size_t get_node_sharing_count(int sharingProc) const
431 {
432 if (db_api_int_size() == 4) {
433 return get_node_sharing_count_impl<int>(sharingProc);
434 }
435 else {
436 return get_node_sharing_count_impl<int64_t>(sharingProc);
437 }
438 }
439
440 template <typename INT> size_t get_node_sharing_count_impl(int sharingProc) const
441 {
442 ThrowRequireWithMsg(m_region != nullptr, "Ioss region has not been created");
443
444 Ioss::CommSet *io_cs = m_region->get_commset("commset_node");
445 size_t numSharings = io_cs->get_field("entity_processor").raw_count();
446
447 std::vector<INT> entityProc;
448 io_cs->get_field_data("entity_processor", entityProc);
449
450 size_t count = 0;
451
452 for (size_t i = 0; i < numSharings; ++i) {
453 int iossSharingProc = entityProc[i * 2 + 1];
454
455 if (iossSharingProc == sharingProc) {
456 count++;
457 }
458 }
459
460 return count;
461 }
462
463 template <typename INT>
464 EntityIdVector get_element_ids_from_block_impl(const Ioss::ElementBlock *block) const
465 {
466 EntityIdVector elemIds;
467
468 std::vector<INT> ids;
469
470 block->get_field_data("ids", ids);
471
472 for (INT id : ids) {
473 elemIds.push_back(static_cast<EntityId>(id));
474 }
475
476 return elemIds;
477 }
478
479 EntityIdVector get_element_ids_from_block(const Ioss::ElementBlock *block) const
480 {
481 if (db_api_int_size() == 4) {
482 return get_element_ids_from_block_impl<int>(block);
483 }
484 else {
485 return get_element_ids_from_block_impl<int64_t>(block);
486 }
487 }
488
489 template <typename INT>
490 EntityIdVector get_node_ids_from_nodeset_impl(const Ioss::NodeSet *ns) const
491 {
492 EntityIdVector nodeIds;
493
494 std::vector<INT> ids;
495
496 ns->get_field_data("ids", ids);
497
498 for (INT id : ids) {
499 nodeIds.push_back(static_cast<EntityId>(id));
500 }
501
502 return nodeIds;
503 }
504
505 EntityIdVector get_node_ids_from_nodeset(const Ioss::NodeSet *ns) const
506 {
507 if (db_api_int_size() == 4) {
508 return get_node_ids_from_nodeset_impl<int>(ns);
509 }
510 else {
511 return get_node_ids_from_nodeset_impl<int64_t>(ns);
512 }
513 }
514
515 template <typename INT>
516 SideVector get_element_side_pairs_from_sideset_impl(const Ioss::SideSet *ss) const
517 {
518 SideVector elemSides;
519
520 for (const Ioss::SideBlock *sb : ss->get_side_blocks()) {
521 std::vector<INT> elemSideVec;
522 sb->get_field_data("element_side", elemSideVec);
523
524 for (unsigned i = 0; i < sb->entity_count(); i++) {
525 EntityId elem = elemSideVec[2 * i + 0];
526 int side = elemSideVec[2 * i + 1];
527 elemSides.push_back({elem, side});
528 }
529 }
530
531 return elemSides;
532 }
533
534 SideVector get_element_side_pairs_from_sideset(const Ioss::SideSet *ss) const
535 {
536 if (db_api_int_size() == 4) {
537 return get_element_side_pairs_from_sideset_impl<int>(ss);
538 }
539 else {
540 return get_element_side_pairs_from_sideset_impl<int64_t>(ss);
541 }
542 }
543
544 template <typename INT>
545 ElementInfo get_element_info_from_block_impl(EntityId elemId,
546 const Ioss::ElementBlock *block) const
547 {
548 const Ioss::ElementTopology *topo = nullptr;
549 EntityIdVector elemConn;
550
551 std::vector<INT> connectivity;
552 std::vector<INT> elemIds;
553
554 block->get_field_data("ids", elemIds);
555 block->get_field_data("connectivity", connectivity);
556
557 topo = block->topology();
558
559 size_t elementCount = elemIds.size();
560 int nodesPerElem = topo->number_nodes();
561
562 for (size_t i = 0; i < elementCount; ++i) {
563 INT *conn = &connectivity[i * nodesPerElem];
564 auto id = static_cast<EntityId>(elemIds[i]);
565
566 if (id == elemId) {
567 for (int j = 0; j < nodesPerElem; j++) {
568 elemConn.push_back(conn[j]);
569 }
570
571 return ElementInfo(topo, elemConn);
572 }
573 }
574
575 return ElementInfo(nullptr, EntityIdVector());
576 }
577
578 template <typename INT> ElementInfo get_element_info_impl(EntityId elemId) const
579 {
580 ThrowRequireWithMsg(m_region != nullptr, "Ioss region has not been created");
581 ElementInfo elemInfo;
582
583 EntityIdVector elemConn;
584
585 const Ioss::ElementBlockContainer &elemBlocks = m_region->get_element_blocks();
586 bool found = false;
587
588 for (const Ioss::ElementBlock *block : elemBlocks) {
589 ElementInfo info = get_element_info_from_block_impl<INT>(elemId, block);
590
591 if (is_valid_element(info)) {
592 ThrowRequireWithMsg(!found,
593 "Element with id " << elemId << " exists in more than one block!");
594 found = true;
595 elemInfo = info;
596 }
597 }
598
599 return elemInfo;
600 }
601
602 ElementInfo get_element_info(EntityId elemId) const
603 {
604 if (db_api_int_size() == 4) {
605 return get_element_info_impl<int>(elemId);
606 }
607 else {
608 return get_element_info_impl<int64_t>(elemId);
609 }
610 }
611
612 template <typename INT> size_t get_element_count_impl() const
613 {
614 ThrowRequireWithMsg(m_region != nullptr, "Ioss region has not been created");
615
616 const Ioss::ElementBlockContainer &elemBlocks = m_region->get_element_blocks();
617 size_t count = 0;
618
619 for (const Ioss::ElementBlock *block : elemBlocks) {
620 std::vector<INT> elemIds;
621 block->get_field_data("ids", elemIds);
622 count += elemIds.size();
623 }
624
625 return count;
626 }
627
628 size_t get_element_count() const
629 {
630 if (db_api_int_size() == 4) {
631 return get_element_count_impl<int>();
632 }
633 else {
634 return get_element_count_impl<int64_t>();
635 }
636 }
637
638 template <typename INT>
639 bool node_is_shared_with_proc_impl(EntityId nodeId, int sharingProc) const
640 {
641 ThrowRequireWithMsg(m_region != nullptr, "Ioss region has not been created");
642
643 Ioss::CommSet *io_cs = m_region->get_commset("commset_node");
644 size_t numSharings = io_cs->get_field("entity_processor").raw_count();
645
646 std::vector<INT> entityProc;
647 io_cs->get_field_data("entity_processor", entityProc);
648
649 for (size_t i = 0; i < numSharings; ++i) {
650 EntityId iossNodeId = entityProc[i * 2];
651 int iossSharingProc = entityProc[i * 2 + 1];
652
653 if (iossNodeId == nodeId && iossSharingProc == sharingProc) {
654 return true;
655 }
656 }
657
658 return false;
659 }
660
661 bool node_is_shared_with_proc(EntityId nodeId, int sharingProc) const
662 {
663 if (db_api_int_size() == 4) {
664 return node_is_shared_with_proc_impl<int>(nodeId, sharingProc);
665 }
666 else {
667 return node_is_shared_with_proc_impl<int64_t>(nodeId, sharingProc);
668 }
669 }
670
671 bool is_valid_element(const ElementInfo &info) const
672 {
673 bool validTopology = info.topology != nullptr &&
674 info.topology != Ioss::ElementTopology::factory(Ioss::Unknown::name);
675 bool validConnectivitySize = info.connectivity.size() != 0;
676 bool validNumNodes = validTopology ? info.topology->number_nodes() ==
677 static_cast<int>(info.connectivity.size())
678 : false;
679
680 return validConnectivitySize && validNumNodes;
681 }
682
683 Ioss::ElementBlock *get_element_block(const std::string &blockName) const
684 {
685 ThrowRequireWithMsg(m_region != nullptr, "Ioss region has not been created");
686
687 const Ioss::ElementBlockContainer &elemBlocks = m_region->get_element_blocks();
688 Ioss::ElementBlock *elemBlock = nullptr;
689
690 for (Ioss::ElementBlock *block : elemBlocks) {
691 if (strcasecmp(block->name().c_str(), blockName.c_str()) == 0) {
692 elemBlock = block;
693 }
694 }
695
696 return elemBlock;
697 }
698
699 Ioss::NodeSet *get_nodeset(const std::string &name) const
700 {
701 ThrowRequireWithMsg(m_region != nullptr, "Ioss region has not been created");
702
703 const Ioss::NodeSetContainer &nodesets = m_region->get_nodesets();
704 Ioss::NodeSet *nodeset = nullptr;
705
706 for (Ioss::NodeSet *ns : nodesets) {
707 if (strcasecmp(ns->name().c_str(), name.c_str()) == 0) {
708 nodeset = ns;
709 }
710 }
711
712 return nodeset;
713 }
714
715 Ioss::SideSet *get_sideset(const std::string &name) const
716 {
717 ThrowRequireWithMsg(m_region != nullptr, "Ioss region has not been created");
718
719 const Ioss::SideSetContainer &sidesets = m_region->get_sidesets();
720 Ioss::SideSet *sideset = nullptr;
721
722 for (Ioss::SideSet *ss : sidesets) {
723 if (strcasecmp(ss->name().c_str(), name.c_str()) == 0) {
724 sideset = ss;
725 }
726 }
727
728 return sideset;
729 }
730
731 Ioss::Assembly *get_assembly(const std::string &name) const
732 {
733 ThrowRequireWithMsg(m_region != nullptr, "Ioss region has not been created");
734
735 const Ioss::AssemblyContainer &assemblies = m_region->get_assemblies();
736 Ioss::Assembly *assembly = nullptr;
737
738 for (Ioss::Assembly *ass : assemblies) {
739 if (strcasecmp(ass->name().c_str(), name.c_str()) == 0) {
740 assembly = ass;
741 }
742 }
743
744 return assembly;
745 }
746
747 void verify_block(Ioss::ElementBlock *block) { ASSERT_TRUE(block != nullptr); }
748
749 void verify_elements_on_block(const Ioss::ElementBlock *block,
750 const std::set<EntityId> &goldIds)
751 {
752 EntityIdVector elemIds = get_element_ids_from_block(block);
753
754 ASSERT_EQ(goldIds.size(), elemIds.size());
755 for (EntityId elemId : elemIds) {
756 EXPECT_EQ(1u, goldIds.count(elemId));
757 }
758 }
759
760 void create_ioss_region()
761 {
762 if (m_region == nullptr) {
763 EXPECT_TRUE(m_database != nullptr);
764 m_region = new Ioss::Region(m_database, "input_model");
765 EXPECT_TRUE(m_region != nullptr);
766 }
767 }
768
769 void create_database(const std::string &fileName, const std::string &meshType)
770 {
771 if (m_database == nullptr) {
772 Ioss::DatabaseUsage db_usage = Ioss::READ_MODEL;
773
774 std::string meshFileName = fileName;
775 filename_substitution(meshFileName);
776 m_database = Ioss::IOFactory::create(meshType, meshFileName, db_usage, get_comm(),
777 m_propertyManager);
778 EXPECT_TRUE(m_database != nullptr);
779 EXPECT_TRUE(m_database->ok(true));
780 EXPECT_EQ(m_database->get_format(), "TextMesh");
781 }
782 }
783
784 void filename_substitution(std::string &filename)
785 {
786 // See if filename contains "%P" which is replaced by the number of processors...
787 // Assumes that %P only occurs once...
788 // filename is changed.
789 size_t pos = filename.find("%P");
790 if (pos != std::string::npos) {
791 // Found the characters... Replace with the processor count...
792 int num_proc = std::max(1, get_parallel_size());
793 std::string tmp(filename, 0, pos);
794 tmp += std::to_string(num_proc);
795 tmp += filename.substr(pos + 2);
796 filename = tmp;
797 }
798 }
799
800 std::pair<std::string, std::string>
801 get_database_type_and_filename(const std::string &meshDesc)
802 {
803 std::string type;
804 std::string filename;
805
806 size_t colon = meshDesc.find(':');
807 if (colon != std::string::npos && colon > 0) {
808 type = meshDesc.substr(0, colon);
809 filename = meshDesc.substr(colon + 1);
810 }
811 else {
812 type = "textmesh";
813 filename = meshDesc;
814 }
815
816 return std::make_pair(type, filename);
817 }
818
819 class CoordinateVerifier
820 {
821 public:
822 CoordinateVerifier(const Ioss::Region &r, const EntityIdVector &ids,
823 const std::vector<double> &coords)
824 : region(r), spatialDim(region.get_property("spatial_dimension").get_int()),
825 goldNodeIds(ids), goldCoordinates(coords)
826 {
827 fill_coordinates_from_ioss();
828 }
829
830 void verify()
831 {
832 for (size_t nodeIndex = 0; nodeIndex < goldNodeIds.size(); nodeIndex++) {
833 EntityId nodeId = goldNodeIds[nodeIndex];
834 EXPECT_TRUE(is_valid_node(nodeId));
835
836 const double *nodalCoords = get_nodal_coordinates(nodeId);
837 const double *goldCoords = &goldCoordinates[nodeIndex * spatialDim];
838
839 verify_nodal_coordinates(nodeId, goldCoords, nodalCoords);
840 }
841 }
842
843 private:
844 template <typename T>
845 size_t field_data_from_ioss(Ioss::GroupingEntity *io_entity, const std::string &io_fld_name,
846 std::vector<T> &io_field_data)
847 {
848 size_t io_entity_count = 0;
849 if (io_entity->field_exists(io_fld_name)) {
850 const Ioss::Field &io_field = io_entity->get_fieldref(io_fld_name);
851 io_entity_count = io_entity->get_field_data(io_field.get_name(), io_field_data);
852 }
853 return io_entity_count;
854 }
855
856 size_t db_api_int_size() const { return region.get_database()->int_byte_size_api(); }
857
858 template <typename INT> EntityIdVector get_node_ids_impl() const
859 {
860 const Ioss::NodeBlockContainer &node_blocks = region.get_node_blocks();
861 assert(node_blocks.size() == 1);
862
863 Ioss::NodeBlock *nb = node_blocks[0];
864
865 std::vector<INT> ids;
866 nb->get_field_data("ids", ids);
867
868 EntityIdVector nodeIds;
869 for (INT id : ids) {
870 nodeIds.push_back(static_cast<EntityId>(id));
871 }
872
873 return nodeIds;
874 }
875
876 EntityIdVector get_node_ids() const
877 {
878 if (db_api_int_size() == 4) {
879 return get_node_ids_impl<int>();
880 }
881 else {
882 return get_node_ids_impl<int64_t>();
883 }
884 }
885
886 template <typename INT> bool is_valid_node_impl(EntityId nodeId) const
887 {
888 EntityIdVector ids = get_node_ids_impl<INT>();
889 auto iter = std::find(ids.begin(), ids.end(), INT(nodeId));
890 return iter != ids.end();
891 }
892
893 bool is_valid_node(EntityId nodeId) const
894 {
895 if (db_api_int_size() == 4) {
896 return is_valid_node_impl<int>(nodeId);
897 }
898 else {
899 return is_valid_node_impl<int64_t>(nodeId);
900 }
901 }
902
903 void verify_num_nodes()
904 {
905 EntityIdVector ids = get_node_ids();
906 EXPECT_EQ(goldNodeIds.size(), ids.size());
907 }
908
909 void fill_coordinate_map(const EntityIdVector &nodeIds,
910 const std::vector<double> &coordinates)
911 {
912 std::vector<double>::const_iterator coordIter = coordinates.begin();
913 for (const EntityId &nodeId : nodeIds) {
914 m_nodalCoords[nodeId] = std::vector<double>(coordIter, coordIter + spatialDim);
915 coordIter += spatialDim;
916 }
917 }
918
919 void fill_coordinates_from_ioss()
920 {
921 std::vector<double> iossCoordinates;
922
923 const Ioss::NodeBlockContainer &node_blocks = region.get_node_blocks();
924 assert(node_blocks.size() == 1);
925
926 Ioss::NodeBlock *nb = node_blocks[0];
927
928 size_t node_count = nb->get_property("entity_count").get_int();
929
930 EntityIdVector nodeIds = get_node_ids();
931
932 size_t numIossNodes =
933 field_data_from_ioss<double>(nb, "mesh_model_coordinates", iossCoordinates);
934 ThrowRequireWithMsg(node_count == numIossNodes, "Node count mismatch");
935 ThrowRequireWithMsg(iossCoordinates.size() == numIossNodes * spatialDim,
936 "Invalid coordinate data size");
937
938 fill_coordinate_map(nodeIds, iossCoordinates);
939 }
940
941 const std::vector<double> &operator[](const EntityId nodeId) const
942 {
943 auto it = m_nodalCoords.find(nodeId);
944 return it->second;
945 }
946
947 const double *get_nodal_coordinates(const EntityId &nodeId) const
948 {
949 return Data((*this)[nodeId]);
950 }
951
952 void verify_nodal_coordinates(const EntityId &nodeId, const double *goldCoords,
953 const double *nodalCoords)
954 {
955 for (unsigned i = 0; i < spatialDim; i++) {
956 EXPECT_NEAR(goldCoords[i], nodalCoords[i], 1.0e-9) << error_message(nodeId, i);
957 }
958 }
959
960 std::string error_message(const EntityId &nodeId, unsigned coordIndex)
961 {
962 std::stringstream message;
963 message << "Proc " << region.get_database()->util().parallel_rank() << ", Node ID "
964 << nodeId << ", coord index " << coordIndex;
965 return message.str();
966 }
967
968 const Ioss::Region &region;
969
970 const unsigned spatialDim;
971
972 const EntityIdVector &goldNodeIds;
973 const std::vector<double> &goldCoordinates;
974 std::unordered_map<EntityId, std::vector<double>> m_nodalCoords;
975 };
976
977 unsigned m_spatialDimension = 3;
978 Ioss::PropertyManager m_propertyManager;
979 Ioss::DatabaseIO *m_database = nullptr;
980 Ioss::Region *m_region = nullptr;
981 IossTopologyMapping m_topologyMapping;
982 };
983
984 } // namespace unit_test
985 } // namespace Iotm
986
987 namespace {
988 class TestTextMesh : public Iotm::unit_test::TextMeshFixture
989 {
990 protected:
991 TestTextMesh() : TextMeshFixture(3) {}
992 };
993
994 class TestTextMesh2d : public Iotm::unit_test::TextMeshFixture
995 {
996 protected:
997 TestTextMesh2d() : TextMeshFixture(2) {}
998 };
999
1000 class TestTextMesh1d : public Iotm::unit_test::TextMeshFixture
1001 {
1002 protected:
1003 TestTextMesh1d() : TextMeshFixture(1) {}
1004 };
1005
1006 class TestTextMeshGraph : public Iotm::unit_test::TextMeshFixture
1007 {
1008 protected:
1009 TestTextMeshGraph() : TextMeshFixture(3) {}
1010
1011 class TextMeshGraph : public SideAdjacencyGraph
1012 {
1013 public:
1014 TextMeshGraph(const TextMeshData &data) : m_data(data) {}
1015
1016 size_t get_num_elements() const override { return m_data.elementDataVec.size(); }
1017
1018 int get_element_proc(const size_t elemIndex) const override
1019 {
1020 const ElementData &elemData = m_data.elementDataVec[elemIndex];
1021 return elemData.proc;
1022 }
1023
1024 bool element_has_any_node_on_proc(const size_t elemIndex, int proc) const override
1025 {
1026 const ElementData &elemData = m_data.elementDataVec[elemIndex];
1027
1028 for (const EntityId &nodeId : elemData.nodeIds) {
1029 const std::set<int> &procsForNode = m_data.procs_for_node(nodeId);
1030 if (procsForNode.count(proc) > 0) {
1031 return true;
1032 }
1033 }
1034
1035 return false;
1036 }
1037
1038 const std::string &get_element_block_name(const size_t elemIndex) const override
1039 {
1040 const ElementData &elemData = m_data.elementDataVec[elemIndex];
1041 return elemData.partName;
1042 }
1043
1044 const std::vector<EntityId> &get_element_node_ids(const size_t elemIndex) const override
1045 {
1046 const ElementData &elemData = m_data.elementDataVec[elemIndex];
1047 return elemData.nodeIds;
1048 }
1049
1050 const Topology &get_element_topology(const size_t elemIndex) const override
1051 {
1052 const ElementData &elemData = m_data.elementDataVec[elemIndex];
1053 return elemData.topology;
1054 }
1055
1056 EntityId get_element_id(const size_t elemIndex) const override
1057 {
1058 const ElementData &elemData = m_data.elementDataVec[elemIndex];
1059 return elemData.identifier;
1060 }
1061
1062 private:
1063 const TextMeshData &m_data;
1064 };
1065
1066 void dump_graph(std::ostream &out = std::cout) { m_graph->dump(m_data.elementDataVec, out); }
1067
1068 void setup_text_mesh_graph(const std::string &meshDesc,
1069 const std::vector<std::string> &selectedBlocks = {},
1070 int proc = SideAdjacencyGraph::ANY_PROC)
1071 {
1072 TextMeshParser parser;
1073 m_data = parser.parse(meshDesc);
1074 m_graph = std::make_shared<TextMeshGraph>(m_data);
1075 m_graph->create_graph(selectedBlocks, proc);
1076 }
1077
1078 void verify_side_adjacency(const std::vector<Adjacency> &goldNeighbors)
1079 {
1080 EXPECT_EQ(m_graph->size(), goldNeighbors.size());
1081 for (size_t i = 0; i < goldNeighbors.size(); ++i) {
1082 const auto &graphNeighborIndices = (*m_graph)[goldNeighbors[i].elemIndex];
1083 const auto &goldNeighborIndices = goldNeighbors[i].neighborIndices;
1084
1085 unsigned numActualGoldConnections = 0;
1086 for (const auto &entry : goldNeighborIndices) {
1087 if (entry.second >= 0) {
1088 numActualGoldConnections++;
1089 }
1090 }
1091
1092 EXPECT_EQ(numActualGoldConnections, graphNeighborIndices.connections.size());
1093
1094 for (const auto &entry : goldNeighborIndices) {
1095 int side = entry.first + 1;
1096 SideAdjacencyGraph::IndexType neighborElemIndex = entry.second;
1097
1098 if (neighborElemIndex >= 0) {
1099 EXPECT_LT(0, graphNeighborIndices.sideReference[side - 1]);
1100 EXPECT_TRUE(graphNeighborIndices.has_any_connection(side, neighborElemIndex));
1101 }
1102 else {
1103 EXPECT_EQ(0, graphNeighborIndices.sideReference[side - 1]);
1104 EXPECT_FALSE(graphNeighborIndices.has_any_connection(side));
1105 }
1106 }
1107 }
1108 }
1109
1110 TextMeshData m_data;
1111 std::shared_ptr<TextMeshGraph> m_graph;
1112 };
1113
1114 using TestTextMeshSkin = TestTextMesh;
1115 } // namespace