IOSS 2.0
Iotm_TextMeshEntityGroup.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// ####################### Start Clang Header Tool Managed Headers ########################
8// clang-format off
9#include <ctype.h> // for toupper
10#include <stddef.h> // for size_t
11#include <algorithm> // for remove, etc
12#include <iterator> // for insert_iterator
13#include <map>
14#include <set> // for set
15#include <sstream> // for operator<<, etc
16#include <string> // for basic_string, etc
17#include <utility> // for pair
18#include <vector> // for vector
19#include <unordered_map>
20#include <sstream> // for ostringstream
21#include <iostream>
22#include <functional>
23#include <stdexcept>
24#include <numeric>
25#if defined(_WIN32) && !defined(__MINGW32__)
26#include <string.h>
27#define strcasecmp _stricmp
28#define strncasecmp _strnicmp
29#else
30#include <strings.h>
31#endif
32
33#include "Iotm_TextMeshFuncs.h"
34
35// clang-format on
36// ####################### End Clang Header Tool Managed Headers ########################
37namespace Iotm {
38 namespace text_mesh {
39
40 using ErrorHandler = std::function<void(const std::ostringstream &)>;
41
42 template <typename T> struct EntityGroupData
43 {
44 using DataType = T;
45 static constexpr unsigned INVALID_ID = std::numeric_limits<unsigned>::max();
46
47 bool hasInputName = false;
48 unsigned id = INVALID_ID;
49 std::string name = "";
50 std::string type = "";
51 std::vector<DataType> data{};
52
53 bool has_valid_id() const { return id != 0 && id != INVALID_ID; }
54 bool has_name() const { return !name.empty(); }
55 };
56
57 // Base grouping class for sidesets, nodesets, assemblies, etc
58 template <typename GroupData> class EntityGroup
59 {
60 private:
61 using DataType = typename GroupData::DataType;
62
63 public:
64 EntityGroup(const std::string &type, const std::string &namePrefix,
65 const std::vector<std::string> &invalidNamePrefixes)
66 : m_idsAssigned(false), m_type(type), m_exodusPrefix(namePrefix),
67 m_invalidPrefixes(invalidNamePrefixes)
68 {
69 set_error_handler([](const std::ostringstream &errmsg) { default_error_handler(errmsg); });
70 }
71
72 virtual ~EntityGroup() {}
73
74 virtual void set_error_handler(ErrorHandler errorHandler) { m_errorHandler = errorHandler; }
75
76 GroupData *add_group_data(const std::string &name, const std::vector<DataType> &data)
77 {
78 GroupData groupData;
79 groupData.data = data;
80 groupData.type = m_type;
81
82 if (!name.empty()) {
83 verify_name(name);
84 groupData.name = name;
85 groupData.hasInputName = true;
86 }
87
88 m_groupDataVec.push_back(groupData);
89
90 return &m_groupDataVec.back();
91 }
92
94 {
98
99 if (m_groupDataVec.size() != m_groupDataMap.size()) {
100 std::ostringstream errmsg;
101 errmsg << "Error populating " << m_type << " map";
102 m_errorHandler(errmsg);
103 }
104 m_idsAssigned = true;
105 }
106
107 size_t size() const { return m_groupDataVec.size(); }
108
109 const std::vector<GroupData> &get_group_data() const { return m_groupDataVec; }
110
111 const std::vector<std::string> &get_part_names() const { return m_partNames; }
112
113 const std::string &get_group_type() const { return m_type; }
114
115 const GroupData *get_group_data(unsigned id) const
116 {
117 if (is_assigned(id)) {
118 auto iter = m_parts.find(id);
119 return &m_groupDataVec[m_groupDataMap[iter->second]];
120 }
121
122 return nullptr;
123 }
124
125 const GroupData *get_group_data(std::string name) const
126 {
128 if (is_registered(name)) {
129 return &m_groupDataVec[m_groupDataMap[name]];
130 }
131
132 return nullptr;
133 }
134
135 bool is_registered(const std::string &name) const { return m_ids.count(name) > 0; }
136
137 protected:
139
140 unsigned get_unassigned_id() const
141 {
142 unsigned nextPartId = 1;
143 while (is_assigned(nextPartId))
144 nextPartId++;
145 return nextPartId;
146 }
147
148 void validate_group_meta_data(const GroupData &groupData)
149 {
150 if (!groupData.has_name()) {
151 std::ostringstream errmsg;
152 errmsg << m_type << " has no name";
153 m_errorHandler(errmsg);
154 }
155
156 if (!groupData.has_valid_id()) {
157 std::ostringstream errmsg;
158 errmsg << m_type << " named " << groupData.name << " has invalid id";
159 m_errorHandler(errmsg);
160 }
161
162 if (is_registered(groupData.name)) {
163 std::ostringstream errmsg;
164 errmsg << "Multiple declarations of " << m_type << ": " << groupData.name;
165 m_errorHandler(errmsg);
166 }
167 }
168
169 void assign(size_t index)
170 {
171 GroupData &groupData = m_groupDataVec[index];
172
173 convert_to_uppercase(groupData.name);
174 validate_group_meta_data(groupData);
175
176 m_partNames.push_back(groupData.name);
177 m_ids[groupData.name] = groupData.id;
178 m_parts[groupData.id] = groupData.name;
179 m_groupDataMap[groupData.name] = index;
180 }
181
183 {
184 for (size_t i = 0; i < m_groupDataVec.size(); i++) {
185 GroupData &groupData = m_groupDataVec[i];
186 if (groupData.has_name()) {
187 std::pair<unsigned, bool> result =
188 get_id_from_part_name(groupData.name, m_exodusPrefix);
189
190 if (result.second) {
191 groupData.id = result.first;
192 assign(i);
193 }
194 }
195 }
196 }
197
199 {
200 for (size_t i = 0; i < m_groupDataVec.size(); i++) {
201 GroupData &groupData = m_groupDataVec[i];
202 if (!groupData.has_name()) {
203 unsigned id = get_unassigned_id();
204
205 std::ostringstream oss;
206 oss << m_exodusPrefix;
207 oss << id;
208 std::string name = oss.str();
209
210 groupData.id = id;
211 groupData.name = name;
212 assign(i);
213 }
214 }
215 }
216
218 {
219 for (size_t i = 0; i < m_groupDataVec.size(); i++) {
220 GroupData &groupData = m_groupDataVec[i];
221 if (groupData.has_name()) {
222 std::pair<unsigned, bool> result =
223 get_id_from_part_name(groupData.name, m_exodusPrefix);
224
225 if (!result.second) {
226 groupData.id = get_unassigned_id();
227 assign(i);
228 }
229 }
230 }
231 }
232
233 bool is_assigned(unsigned id) const { return m_parts.count(id) > 0; }
234
235 void verify_name(const std::string &name)
236 {
237 for (const std::string &invalidPrefix : m_invalidPrefixes) {
238 const unsigned prefixLength = invalidPrefix.length();
239 const std::string namePrefix = name.substr(0, prefixLength);
240
241 if (strcasecmp(namePrefix.c_str(), invalidPrefix.c_str()) == 0) {
242 std::ostringstream errmsg;
243 errmsg << "Invalid name '" << name << "' for a " << m_type << " part";
244 m_errorHandler(errmsg);
245 }
246 }
247 }
248
249 std::vector<std::string> m_partNames{};
250 mutable std::unordered_map<std::string, unsigned> m_ids;
251 mutable std::unordered_map<unsigned, std::string> m_parts;
252 mutable bool m_idsAssigned{false};
253 mutable std::unordered_map<std::string, size_t> m_groupDataMap;
254
255 std::string m_type{};
256 std::string m_exodusPrefix{};
257 std::vector<std::string> m_invalidPrefixes{};
258 std::vector<GroupData> m_groupDataVec{};
259
261 };
262
263 } // namespace text_mesh
264} // namespace Iotm
void verify_name(const std::string &name)
Definition Iotm_TextMeshEntityGroup.h:235
std::vector< AssemblyData > m_groupDataVec
Definition Iotm_TextMeshEntityGroup.h:258
const std::string & get_group_type() const
Definition Iotm_TextMeshEntityGroup.h:113
const std::vector< std::string > & get_part_names() const
Definition Iotm_TextMeshEntityGroup.h:111
void assign_id_for_non_default_exodus_name()
Definition Iotm_TextMeshEntityGroup.h:217
const GroupData * get_group_data(std::string name) const
Definition Iotm_TextMeshEntityGroup.h:125
unsigned get_unassigned_id() const
Definition Iotm_TextMeshEntityGroup.h:140
std::vector< std::string > m_invalidPrefixes
Definition Iotm_TextMeshEntityGroup.h:257
typename AssemblyData::DataType DataType
Definition Iotm_TextMeshEntityGroup.h:61
std::unordered_map< unsigned, std::string > m_parts
Definition Iotm_TextMeshEntityGroup.h:251
void finalize_parse()
Definition Iotm_TextMeshEntityGroup.h:93
void assign_id_from_default_exodus_name()
Definition Iotm_TextMeshEntityGroup.h:182
void validate_group_meta_data(const GroupData &groupData)
Definition Iotm_TextMeshEntityGroup.h:148
void assign(size_t index)
Definition Iotm_TextMeshEntityGroup.h:169
std::unordered_map< std::string, size_t > m_groupDataMap
Definition Iotm_TextMeshEntityGroup.h:253
ErrorHandler m_errorHandler
Definition Iotm_TextMeshEntityGroup.h:260
const GroupData * get_group_data(unsigned id) const
Definition Iotm_TextMeshEntityGroup.h:115
std::vector< std::string > m_partNames
Definition Iotm_TextMeshEntityGroup.h:249
bool is_assigned(unsigned id) const
Definition Iotm_TextMeshEntityGroup.h:233
virtual ~EntityGroup()
Definition Iotm_TextMeshEntityGroup.h:72
GroupData * add_group_data(const std::string &name, const std::vector< DataType > &data)
Definition Iotm_TextMeshEntityGroup.h:76
virtual void set_error_handler(ErrorHandler errorHandler)
Definition Iotm_TextMeshEntityGroup.h:74
bool m_idsAssigned
Definition Iotm_TextMeshEntityGroup.h:252
bool is_registered(const std::string &name) const
Definition Iotm_TextMeshEntityGroup.h:135
std::string m_type
Definition Iotm_TextMeshEntityGroup.h:255
const std::vector< GroupData > & get_group_data() const
Definition Iotm_TextMeshEntityGroup.h:109
size_t size() const
Definition Iotm_TextMeshEntityGroup.h:107
std::unordered_map< std::string, unsigned > m_ids
Definition Iotm_TextMeshEntityGroup.h:250
std::string m_exodusPrefix
Definition Iotm_TextMeshEntityGroup.h:256
void assign_id_and_name_for_empty_name()
Definition Iotm_TextMeshEntityGroup.h:198
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 default_error_handler(const std::ostringstream &message)
Definition Iotm_TextMeshFuncs.h:40
std::pair< unsigned, bool > get_id_from_part_name(const std::string &name, const std::string &prefix)
Definition Iotm_TextMeshFuncs.h:126
A namespace for the textmesh database format.
Definition Iotm_DatabaseIO.C:95
Definition Iotm_TextMeshEntityGroup.h:43
T DataType
Definition Iotm_TextMeshEntityGroup.h:44
bool hasInputName
Definition Iotm_TextMeshEntityGroup.h:47
std::string name
Definition Iotm_TextMeshEntityGroup.h:49
static constexpr unsigned INVALID_ID
Definition Iotm_TextMeshEntityGroup.h:45
std::string type
Definition Iotm_TextMeshEntityGroup.h:50
std::vector< DataType > data
Definition Iotm_TextMeshEntityGroup.h:51
bool has_valid_id() const
Definition Iotm_TextMeshEntityGroup.h:53
bool has_name() const
Definition Iotm_TextMeshEntityGroup.h:54