Debian Codegraph @

packages/seacas/libraries/ioss/src/text_mesh/Iotm_TextMeshSidesetSplitter.h

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 #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 #include <strings.h>
26
27 #include "Iotm_TextMeshFuncs.h"
28 #include "Iotm_TextMeshDataTypes.h"
29 #include "Iotm_TextMeshEntityGroup.h"
30
31 // clang-format on
32 // ####################### End Clang Header Tool Managed Headers ########################
33 namespace Iotm {
34 namespace text_mesh {
35
36 using ErrorHandler = std::function<void(const std::ostringstream &)>;
37
38 struct SideBlockInfo
39 {
40 std::string name{};
41 std::string parentName{};
42 std::string sideTopology{};
43 std::string elementTopology{};
44 std::string touchingBlock{};
45 std::vector<size_t> sideIndex{};
46 unsigned numNodesPerSide{0};
47 };
48
49 enum SplitType { TOPOLOGY, ELEMENT_BLOCK, NO_SPLIT, INVALID_SPLIT };
50
51 inline std::ostream &operator<<(std::ostream &out, const SplitType &t)
52 {
53 switch (t) {
54 case SplitType::TOPOLOGY: return out << "TOPOLOGY"; break;
55 case SplitType::ELEMENT_BLOCK: return out << "ELEMENT_BLOCK"; break;
56 case SplitType::NO_SPLIT: return out << "NO_SPLIT"; break;
57 default: return out << "INVALID"; break;
58 }
59 return out << "INVALID[" << (unsigned)t << "]";
60 }
61
62 template <typename EntityId> using SidesetDataType = std::pair<EntityId, int>;
63
64 template <typename EntityId, typename Topology> struct SidesetData;
65
66 template <typename EntityId, typename Topology> class SidesetSplitter
67 {
68 public:
69 explicit SidesetSplitter(SplitType splitType) : m_splitType(splitType)
70 {
71 ErrorHandler errorHandler = [](const std::ostringstream &errmsg) {
72 default_error_handler(errmsg);
73 };
74 set_error_handler(errorHandler);
75 }
76
77 SidesetSplitter() : m_splitType(INVALID_SPLIT)
78 {
79 ErrorHandler errorHandler = [](const std::ostringstream &errmsg) {
80 default_error_handler(errmsg);
81 };
82 set_error_handler(errorHandler);
83 }
84
85 void set_error_handler(ErrorHandler errorHandler) { m_errorHandler = errorHandler; }
86
87 void split(const SidesetData<EntityId, Topology> &sideset,
88 const std::vector<ElementData<EntityId, Topology>> &elementData)
89 {
90 m_splitMap.clear();
91 m_sidesetName = sideset.name;
92
93 if (get_split_type() == SplitType::TOPOLOGY) {
94 split_by_topology(sideset, elementData);
95 }
96 else if (get_split_type() == SplitType::ELEMENT_BLOCK) {
97 split_by_element_block(sideset, elementData);
98 }
99 else if (get_split_type() == SplitType::NO_SPLIT) {
100 split_by_no_split(sideset, elementData);
101 }
102 else {
103 std::ostringstream errmsg;
104 errmsg << "Invalid split type: " << get_split_type();
105 m_errorHandler(errmsg);
106 }
107
108 build_index_proc_map(sideset, elementData);
109 }
110
111 std::vector<SideBlockInfo> get_side_block_info() const
112 {
113 std::vector<SideBlockInfo> infoVec;
114 infoVec.reserve(m_splitMap.size());
115
116 for (auto iter = m_splitMap.begin(); iter != m_splitMap.end(); iter++) {
117 const std::string &sideBlockName = iter->first;
118 SideBlockInfo info = get_side_block_info(sideBlockName);
119 infoVec.push_back(info);
120 }
121 return infoVec;
122 }
123
124 std::vector<size_t> get_indices_local_to_proc(const std::vector<size_t> &index,
125 int proc) const
126 {
127 std::vector<size_t> indexForProc;
128 indexForProc.reserve(index.size());
129
130 for (size_t elemPairIndex : index) {
131 if (is_index_local_to_proc(elemPairIndex, proc)) {
132 indexForProc.push_back(elemPairIndex);
133 }
134 }
135
136 indexForProc.resize(indexForProc.size());
137 return indexForProc;
138 }
139
140 SideBlockInfo get_side_block_info(const std::string &name) const
141 {
142 SideBlockInfo info;
143
144 auto iter = m_splitMap.find(name);
145 if (iter != m_splitMap.end()) {
146 const SplitData &splitData = iter->second;
147
148 info.name = name;
149 info.parentName = splitData.sidesetName;
150 info.sideTopology = splitData.sideTopology;
151 info.elementTopology = splitData.elemTopology;
152 info.numNodesPerSide = splitData.sideNodeCount;
153 info.touchingBlock = splitData.touchingBlock;
154 info.sideIndex = splitData.index;
155 }
156 return info;
157 }
158
159 SplitType get_split_type() const { return m_splitType; }
160 void set_split_type(SplitType inputSplitType) { m_splitType = inputSplitType; }
161
162 private:
163 void build_index_proc_map(const SidesetData<EntityId, Topology> &sideset,
164 const std::vector<ElementData<EntityId, Topology>> &elementData)
165 {
166 for (size_t i = 0; i < sideset.data.size(); ++i) {
167 const SidesetDataType<EntityId> &elemSidePair = sideset.data[i];
168 EntityId elemId = elemSidePair.first;
169
170 auto iter = bound_search(elementData.begin(), elementData.end(), elemId,
171 ElementDataLess<EntityId, Topology>());
172 if (iter == elementData.end()) {
173 std::ostringstream errmsg;
174 errmsg << "Error! Sideset with id: " << sideset.id << " and name: " << sideset.name
175 << " has reference to invalid element '" << elemId << "'.";
176 m_errorHandler(errmsg);
177 }
178
179 m_indexProcMap[i] = iter->proc;
180 }
181 }
182
183 bool is_index_local_to_proc(size_t elemPairIndex, int proc) const
184 {
185 auto iter = m_indexProcMap.find(elemPairIndex);
186
187 if (iter == m_indexProcMap.end()) {
188 std::ostringstream errmsg;
189 errmsg << "Sideset with name: " << m_sidesetName << " is referencing an invalid index "
190 << elemPairIndex;
191 m_errorHandler(errmsg);
192 }
193
194 return iter->second == proc;
195 }
196
197 struct SplitData
198 {
199 bool metaDataSet{false};
200 std::string sidesetName{};
201 std::string touchingBlock{};
202 std::string elemTopology{};
203 std::string sideTopology{};
204 int sideNodeCount{-1};
205 std::vector<size_t> index{};
206
207 SplitData() : metaDataSet(false), sideNodeCount(-1) {}
208 };
209
210 void fill_split_data(std::string key, size_t index,
211 const ElementData<EntityId, Topology> &elemData, int side)
212 {
213 convert_to_upper_case(key);
214
215 SplitData &splitData = m_splitMap[key];
216
217 splitData.index.push_back(index);
218
219 if (!splitData.metaDataSet) {
220 splitData.sidesetName = m_sidesetName;
221 splitData.elemTopology = elemData.topology.name();
222 splitData.sideTopology = elemData.topology.side_topology_name(side);
223 splitData.sideNodeCount = elemData.topology.side_topology_num_nodes(side);
224
225 if (get_split_type() == ELEMENT_BLOCK) {
226 splitData.touchingBlock = elemData.partName;
227 }
228
229 splitData.metaDataSet = true;
230 }
231 }
232
233 using Criterion =
234 std::function<std::string(const SidesetData<EntityId, Topology> &sideset,
235 const ElementData<EntityId, Topology> &elemData, int side)>;
236
237 void split_by_criterion(const SidesetData<EntityId, Topology> &sideset,
238 const std::vector<ElementData<EntityId, Topology>> &elementData,
239 Criterion criterion)
240 {
241 for (size_t index = 0; index < sideset.data.size(); ++index) {
242 const SidesetDataType<EntityId> &elemSidePair = sideset.data[index];
243 EntityId elemId = elemSidePair.first;
244 int side = elemSidePair.second;
245
246 auto iter = bound_search(elementData.begin(), elementData.end(), elemId,
247 ElementDataLess<EntityId, Topology>());
248 if (iter == elementData.end()) {
249 std::ostringstream errmsg;
250 errmsg << "Error! Sideset with id: " << sideset.id << " and name: " << sideset.name
251 << " has reference to invalid element '" << elemId << "'.";
252 m_errorHandler(errmsg);
253 }
254
255 std::string key = criterion(sideset, *iter, side);
256 fill_split_data(key, index, *iter, side);
257 }
258 }
259
260 void split_by_topology(const SidesetData<EntityId, Topology> &sideset,
261 const std::vector<ElementData<EntityId, Topology>> &elementData)
262 {
263 Criterion criterion = [](const SidesetData<EntityId, Topology> &sideSet,
264 const ElementData<EntityId, Topology> &elemData, int side) {
265 if (sideSet.has_default_exodus_name()) {
266 return "SURFACE_" + elemData.topology.name() + "_" +
267 elemData.topology.side_topology_name(side) + "_" + std::to_string(sideSet.id);
268 }
269 return sideSet.name + "_" + elemData.topology.name() + "_" +
270 elemData.topology.side_topology_name(side);
271 };
272
273 split_by_criterion(sideset, elementData, criterion);
274 }
275
276 void split_by_element_block(const SidesetData<EntityId, Topology> &sideset,
277 const std::vector<ElementData<EntityId, Topology>> &elementData)
278 {
279 Criterion criterion = [](const SidesetData<EntityId, Topology> &sideSet,
280 const ElementData<EntityId, Topology> &elemData, int side) {
281 if (sideSet.has_default_exodus_name()) {
282 return "SURFACE_" + elemData.partName + "_" +
283 elemData.topology.side_topology_name(side) + "_" + std::to_string(sideSet.id);
284 }
285 return sideSet.name + "_" + elemData.partName + "_" +
286 elemData.topology.side_topology_name(side);
287 };
288
289 split_by_criterion(sideset, elementData, criterion);
290 }
291
292 void split_by_no_split(const SidesetData<EntityId, Topology> &sideset,
293 const std::vector<ElementData<EntityId, Topology>> & /* elementData */)
294 {
295 std::vector<size_t> splitIndex(sideset.data.size());
296 std::iota(std::begin(splitIndex), std::end(splitIndex), 0);
297 SplitData &splitData = m_splitMap[sideset.name];
298
299 splitData.index = splitIndex;
300 splitData.sidesetName = m_sidesetName;
301 splitData.elemTopology = "unknown";
302 splitData.sideTopology = "unknown";
303 splitData.sideNodeCount = -1;
304 splitData.metaDataSet = true;
305 }
306
307 SplitType m_splitType{INVALID_SPLIT};
308 std::string m_sidesetName{};
309
310 std::unordered_map<size_t, int> m_indexProcMap;
311 std::unordered_map<std::string, SplitData> m_splitMap;
312 ErrorHandler m_errorHandler;
313 };
314
315 } // namespace text_mesh
316 } // namespace Iotm