IOSS 2.0
Loading...
Searching...
No Matches
Iotm_TextMeshAssembly.h
Go to the documentation of this file.
1// Copyright(C) 1999-2020, 2022 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#pragma once
6
7#include "iotm_export.h"
8
9// ####################### Start Clang Header Tool Managed Headers ########################
10// clang-format off
11#include <ctype.h> // for toupper
12#include <stddef.h> // for size_t
13#include <algorithm> // for remove, etc
14#include <iterator> // for insert_iterator
15#include <map>
16#include <set> // for set
17#include <sstream> // for operator<<, etc
18#include <string> // for basic_string, etc
19#include <utility> // for pair
20#include <vector> // for vector
21#include <unordered_map>
22#include <sstream> // for ostringstream
23#include <iostream>
24#include <functional>
25#include <stdexcept>
26#include <numeric>
27
28#include "Iotm_TextMeshFuncs.h"
30
31// clang-format on
32// ####################### End Clang Header Tool Managed Headers ########################
33namespace Iotm {
34 namespace text_mesh {
35
36 using ErrorHandler = std::function<void(const std::ostringstream &)>;
37
39
40 inline std::ostream &operator<<(std::ostream &out, const AssemblyType &t)
41 {
42 switch (t) {
43 case AssemblyType::ASSEMBLY: return out << "ASSEMBLY"; break;
44 case AssemblyType::BLOCK: return out << "ELEMENT_BLOCK"; break;
45 case AssemblyType::SIDESET: return out << "SIDESET"; break;
46 case AssemblyType::NODESET: return out << "NODESET"; break;
47 default: return out << "INVALID"; break;
48 }
49 return out << "INVALID[" << (unsigned)t << "]";
50 }
51
52 using AssemblyDataType = std::string;
53
54 struct IOTM_EXPORT AssemblyData : public EntityGroupData<AssemblyDataType>
55 {
57
60
61 private:
63 };
64
65 template <typename EntityId> class Assemblies : public EntityGroup<AssemblyData>
66 {
67 public:
69
71 : EntityGroup<AssemblyData>("ASSEMBLY", "ASSEMBLY_", {"BLOCK_", "SURFACE_", "NODELIST_"})
72 {
73 }
74
75 bool is_cyclic(const std::string &assembly) const
76 {
78 return check_for_cycle(assembly);
79 }
80
81 bool is_cyclic() const
82 {
83 for (const std::string &assembly : BaseClass::get_part_names())
84 if (is_cyclic(assembly)) {
85 return true;
86 }
87
88 return false;
89 }
90
91 std::vector<std::string> &&get_forward_traversal_list(const std::string &assembly) const
92 {
94 fill_traversal(assembly);
95
96 return std::move(m_traversalList);
97 }
98
99 std::vector<std::string> &&get_reverse_traversal_list(const std::string &assembly) const
100 {
102 fill_traversal(assembly);
103 std::reverse(m_traversalList.begin(), m_traversalList.end());
104
105 return std::move(m_traversalList);
106 }
107
108 private:
109 // Walk the tree without cyclic dependency
110 void fill_traversal(const std::string &assembly) const
111 {
112 const AssemblyData *assemblyData = BaseClass::get_group_data(assembly);
113 if (nullptr != assemblyData) {
114 if (m_visitedNodes[assembly] == false) {
115 m_visitedNodes[assembly] = true;
116 m_traversalList.push_back(assembly);
117
118 if (assemblyData->get_assembly_type() == AssemblyType::ASSEMBLY) {
119 for (const std::string &member : assemblyData->data) {
120 fill_traversal(member);
121 }
122 }
123 }
124 }
125 }
126
127 bool check_for_cycle(const std::string &assembly) const
128 {
129 bool isCyclic = false;
130 const AssemblyData *assemblyData = BaseClass::get_group_data(assembly);
131 if (nullptr != assemblyData) {
132 if (m_visitedNodes[assembly] == true) {
133 isCyclic = true;
134 }
135 else {
136 m_visitedNodes[assembly] = true;
137
138 if (assemblyData->get_assembly_type() == AssemblyType::ASSEMBLY) {
139 for (const std::string &member : assemblyData->data) {
140 isCyclic |= check_for_cycle(member);
141 }
142 }
143 }
144 }
145 return isCyclic;
146 }
147
148 void initialize_graph() const
149 {
150 m_traversalList.clear();
152
153 for (const std::string &name : BaseClass::get_part_names()) {
154 m_visitedNodes[name] = false;
155 }
156 }
157
158 mutable std::unordered_map<std::string, bool> m_visitedNodes;
159 mutable std::vector<std::string> m_traversalList;
160 };
161
162 class IOTM_EXPORT AssemblyParser
163 {
164 public:
166 {
167 ErrorHandler errorHandler = [](const std::ostringstream &errmsg) {
168 default_error_handler(errmsg);
169 };
170 set_error_handler(errorHandler);
171 }
172
173 void set_error_handler(ErrorHandler errorHandler) { m_errorHandler = errorHandler; }
174
175 std::string get_name() { return m_name; }
176
178
179 const std::vector<std::string> &get_assembly_data() { return m_members; }
180
181 // Expected format for assembly string data is:
182 // "name=<name>; type=<assembly|block|sideset|nodeset>; member=member_1,...,member_n;"
183 // Only type keyword is required
184 void parse(const std::string &parseData)
185 {
186 auto options = get_tokens(parseData, ";");
187
188 for (const auto &option : options) {
189 parse_option_group(option);
190 }
191 }
192
193 void verify_parse() const
194 {
196 std::ostringstream errmsg;
197 errmsg << "Error! Assembly with name: " << m_name << " does not have a defined type.";
198 m_errorHandler(errmsg);
199 }
200 }
201
202 private:
203 void parse_option(std::string optionName, const std::string &optionValue)
204 {
205 convert_to_lowercase(optionName);
206
207 if (optionName == "name") {
208 parse_name(optionValue);
209 }
210 else if (optionName == "type") {
211 parse_assembly_type(optionValue);
212 }
213 else if (optionName == "member") {
214 parse_assembly_members(optionValue);
215 }
216 else {
217 std::ostringstream errmsg;
218 errmsg << "Unrecognized assembly option: " << optionName;
219 m_errorHandler(errmsg);
220 }
221 }
222
223 void parse_option_group(const std::string &option)
224 {
225 if (!option.empty()) {
226 auto optionTokens = get_tokens(option, "=");
227
228 if (optionTokens.size() != 2) {
229 std::ostringstream errmsg;
230 errmsg << "Unrecognized assembly option: " << option;
231 m_errorHandler(errmsg);
232 }
233
234 parse_option(optionTokens[0], optionTokens[1]);
235 }
236 }
237
238 void parse_name(const std::string &data) { m_name = data; }
239
240 void parse_assembly_type(std::string type)
241 {
243
244 if (type == "assembly") {
246 }
247 else if (type == "block") {
249 }
250 else if (type == "sideset") {
252 }
253 else if (type == "nodeset") {
255 }
256 else {
257 std::ostringstream errmsg;
258 errmsg << "Unrecognized assembly type: " << type;
259 m_errorHandler(errmsg);
260 }
261 }
262
263 void parse_assembly_members(const std::string &data)
264 {
265 std::vector<std::string> assemblyData = get_tokens(data, ",");
266 for (std::string &member : assemblyData) {
267 convert_to_uppercase(member);
268 }
269
270 m_members = assemblyData;
271 }
272
273 std::vector<std::string> m_members{};
274 std::string m_name{};
277 };
278
279 } // namespace text_mesh
280} // namespace Iotm
std::vector< std::string > && get_forward_traversal_list(const std::string &assembly) const
Definition Iotm_TextMeshAssembly.h:91
std::vector< std::string > m_traversalList
Definition Iotm_TextMeshAssembly.h:159
void fill_traversal(const std::string &assembly) const
Definition Iotm_TextMeshAssembly.h:110
bool is_cyclic(const std::string &assembly) const
Definition Iotm_TextMeshAssembly.h:75
std::vector< std::string > && get_reverse_traversal_list(const std::string &assembly) const
Definition Iotm_TextMeshAssembly.h:99
std::unordered_map< std::string, bool > m_visitedNodes
Definition Iotm_TextMeshAssembly.h:158
bool check_for_cycle(const std::string &assembly) const
Definition Iotm_TextMeshAssembly.h:127
Assemblies()
Definition Iotm_TextMeshAssembly.h:70
void initialize_graph() const
Definition Iotm_TextMeshAssembly.h:148
bool is_cyclic() const
Definition Iotm_TextMeshAssembly.h:81
EntityGroup< AssemblyData > BaseClass
Definition Iotm_TextMeshAssembly.h:68
void parse_assembly_members(const std::string &data)
Definition Iotm_TextMeshAssembly.h:263
void parse(const std::string &parseData)
Definition Iotm_TextMeshAssembly.h:184
void parse_assembly_type(std::string type)
Definition Iotm_TextMeshAssembly.h:240
void parse_option_group(const std::string &option)
Definition Iotm_TextMeshAssembly.h:223
AssemblyType get_assembly_type() const
Definition Iotm_TextMeshAssembly.h:177
const std::vector< std::string > & get_assembly_data()
Definition Iotm_TextMeshAssembly.h:179
std::string get_name()
Definition Iotm_TextMeshAssembly.h:175
void verify_parse() const
Definition Iotm_TextMeshAssembly.h:193
AssemblyType m_assemblyType
Definition Iotm_TextMeshAssembly.h:275
void set_error_handler(ErrorHandler errorHandler)
Definition Iotm_TextMeshAssembly.h:173
ErrorHandler m_errorHandler
Definition Iotm_TextMeshAssembly.h:276
void parse_name(const std::string &data)
Definition Iotm_TextMeshAssembly.h:238
std::vector< std::string > m_members
Definition Iotm_TextMeshAssembly.h:273
AssemblyParser()
Definition Iotm_TextMeshAssembly.h:165
void parse_option(std::string optionName, const std::string &optionValue)
Definition Iotm_TextMeshAssembly.h:203
std::string m_name
Definition Iotm_TextMeshAssembly.h:274
const std::vector< std::string > & get_part_names() const
Definition Iotm_TextMeshEntityGroup.h:111
const std::vector< AssemblyData > & get_group_data() const
Definition Iotm_TextMeshEntityGroup.h:109
size_t size() const
Definition Iotm_TextMeshEntityGroup.h:107
EntityGroup(const std::string &type, const std::string &namePrefix, const std::vector< std::string > &invalidNamePrefixes)
Definition Iotm_TextMeshEntityGroup.h:64
Definition Iotm_TextMeshAdjacencyGraph.h:39
void convert_to_uppercase(std::string &str)
Definition Iotm_TextMeshFuncs.h:96
std::function< void(const std::ostringstream &)> ErrorHandler
Definition Iotm_TextMeshAdjacencyGraph.h:41
void convert_to_lowercase(std::string &str)
Definition Iotm_TextMeshFuncs.h:101
std::string AssemblyDataType
Definition Iotm_TextMeshAssembly.h:52
AssemblyType
Definition Iotm_TextMeshAssembly.h:38
@ INVALID_ASSEMBLY
Definition Iotm_TextMeshAssembly.h:38
@ NODESET
Definition Iotm_TextMeshAssembly.h:38
@ ASSEMBLY
Definition Iotm_TextMeshAssembly.h:38
@ SIDESET
Definition Iotm_TextMeshAssembly.h:38
@ BLOCK
Definition Iotm_TextMeshAssembly.h:38
std::ostream & operator<<(std::ostream &out, const AssemblyType &t)
Definition Iotm_TextMeshAssembly.h:40
std::vector< std::string > get_tokens(const std::string &str, const std::string &separators)
Definition Iotm_TextMeshFuncs.h:76
void default_error_handler(const std::ostringstream &message)
Definition Iotm_TextMeshFuncs.h:40
A namespace for the textmesh database format.
Definition Iotm_DatabaseIO.C:95
Definition Iotm_TextMeshAssembly.h:55
AssemblyType assemblyType
Definition Iotm_TextMeshAssembly.h:62
AssemblyDataType DataType
Definition Iotm_TextMeshAssembly.h:56
AssemblyType get_assembly_type() const
Definition Iotm_TextMeshAssembly.h:59
void set_assembly_type(AssemblyType type_)
Definition Iotm_TextMeshAssembly.h:58
Definition Iotm_TextMeshEntityGroup.h:43
std::vector< DataType > data
Definition Iotm_TextMeshEntityGroup.h:51