Debian Codegraph @

packages/seacas/libraries/ioss/src/Ioss_ElementTopology.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 "Ioss_CodeTypes.h" // for IntVector
8 #include "Ioss_ElementPermutation.h" // for ElementPermutation
9 #include "Ioss_ElementTopology.h"
10 #include "Ioss_Super.h" // for Super
11 #include "Ioss_Utils.h"
12
13 #include <cassert>
14 #include <cstddef>
15 #include <fmt/ostream.h>
16 #include <ostream>
17 #include <string>
18 #include <utility>
19 #include <vector>
20
21 void Ioss::ETRegistry::insert(const Ioss::ETM_VP &value, bool delete_me)
22 {
23 m_registry.insert(value);
24 if (delete_me) {
25 m_deleteThese.push_back(value.second);
26 }
27 }
28
29 Ioss::ETRegistry::~ETRegistry()
30 {
31 for (const auto &entry : m_deleteThese) {
32 delete entry;
33 }
34 }
35
36 // ========================================================================
37 Ioss::ElementTopology::ElementTopology(std::string type, std::string master_elem_name,
38 bool delete_me)
39 : name_(std::move(type)), masterElementName_(std::move(master_elem_name))
40 {
41 registry().insert(Ioss::ETM_VP(name_, this), delete_me);
42 std::string lname = Ioss::Utils::lowercase(name_);
43 if (lname != name_) {
44 alias(name_, lname);
45 }
46 alias(name_, masterElementName_);
47 }
48
49 void Ioss::ElementTopology::alias(const std::string &base, const std::string &syn)
50 {
51 registry().insert(Ioss::ETM_VP(syn, factory(base)), false);
52 std::string lsyn = Ioss::Utils::lowercase(syn);
53 if (lsyn != syn) {
54 alias(base, lsyn);
55 }
56 }
57
58 Ioss::ETRegistry &Ioss::ElementTopology::registry()
59 {
60 static ETRegistry registry_;
61 return registry_;
62 }
63
64 bool Ioss::ElementTopology::edges_similar() const { return true; }
65 bool Ioss::ElementTopology::faces_similar() const { return true; }
66
67 Ioss::ElementTopology *Ioss::ElementTopology::factory(const std::string &type, bool ok_to_fail)
68 {
69 std::string ltype = Ioss::Utils::lowercase(type);
70
71 Ioss::ElementTopology *inst = nullptr;
72 auto iter = registry().find(ltype);
73
74 if (iter == registry().end()) {
75 std::string base1 = "super";
76 if (ltype.compare(0, 5, base1) == 0) {
77 // A super element can have a varying number of nodes. Create
78 // an IO element type for this super element. The node count
79 // should be encoded in the 'type' as 'super42' for a 42-node
80 // superelement.
81
82 Ioss::Super::make_super(ltype);
83 iter = registry().find(ltype);
84 }
85 else {
86 // See if 'type' contains a '-'. Some codes create their
87 // own topologies by adding a "-something" onto the end of a
88 // standard topology.
89 size_t dash = ltype.find('-');
90 if (dash != std::string::npos) {
91 std::string sub_type = ltype.substr(0, dash);
92 iter = registry().find(sub_type);
93 }
94 }
95 }
96
97 // See if we can recognize an element topology consisting of the first 3 or 4 letters
98 // of the name concatenated with the digits at the end of the name (if any)...
99 if (iter == registry().end()) {
100 auto first_three = ltype.substr(0, 3);
101 auto first_four = ltype.substr(0, 4);
102 auto node_count_str = Ioss::Utils::get_trailing_digits(ltype);
103 if (!node_count_str.empty()) {
104 first_three += node_count_str;
105 first_four += node_count_str;
106 }
107
108 iter = registry().find(first_four);
109 if (iter == registry().end()) {
110 iter = registry().find(first_three);
111 }
112 }
113
114 if (iter == registry().end()) {
115 if (!ok_to_fail) {
116 std::ostringstream errmsg;
117 fmt::print(errmsg, "ERROR: The topology type '{}' is not supported.", type);
118 IOSS_ERROR(errmsg);
119 }
120 }
121 else {
122 inst = (*iter).second;
123 }
124 return inst;
125 }
126
127 Ioss::ElementTopology *Ioss::ElementTopology::factory(unsigned int unique_id)
128 {
129 // Given a unique id obtained from 'get_unique_id', return the
130 // topology type that it refers to...
131 for (auto &entry : registry()) {
132 if (Ioss::Utils::hash(entry.second->name()) == unique_id) {
133 return entry.second;
134 }
135 }
136 return nullptr;
137 }
138
139 unsigned int Ioss::ElementTopology::get_unique_id(const std::string &type)
140 {
141 // Return a unique integer id corresponding to this topology type.
142 // Basically used to simplify some parallel calculations so they can
143 // deal with int instead of strings...
144 if (type == "unknown") {
145 return 0;
146 }
147 unsigned int hash_val = 0;
148 std::string ltype = Ioss::Utils::lowercase(type);
149 auto iter = registry().find(ltype);
150 if (iter == registry().end()) {
151 fmt::print(Ioss::WarnOut(), "The topology type '{}' is not supported.\n", type);
152 }
153 else {
154 Ioss::ElementTopology *inst = (*iter).second;
155 hash_val = Ioss::Utils::hash(inst->name());
156 }
157 return hash_val;
158 }
159
160 /** \brief Get the names of element topologies known to Ioss.
161 *
162 * \returns The list of known element topology names.
163 */
164 Ioss::NameList Ioss::ElementTopology::describe()
165 {
166 Ioss::NameList names;
167 describe(&names);
168 return names;
169 }
170
171 /** \brief Get the names of element topologies known to Ioss.
172 *
173 * \param[out] names The list of known element topology names.
174 * \returns The number of known element topologies.
175 */
176 int Ioss::ElementTopology::describe(NameList *names)
177 {
178 int count = 0;
179 for (auto &entry : registry()) {
180 names->push_back(entry.first);
181 count++;
182 }
183 return count;
184 }
185
186 Ioss::IntVector Ioss::ElementTopology::face_edge_connectivity(int face_number) const
187 {
188 assert(face_number > 0 && face_number <= number_faces());
189
190 int nface_edge = number_edges_face(face_number);
191 Ioss::IntVector fcon(nface_edge);
192
193 // This works for 2D elements, 3D elements override
194 for (int i = 0; i < nface_edge; i++) {
195 fcon[i] = i;
196 }
197
198 return fcon;
199 }
200
201 Ioss::IntVector Ioss::ElementTopology::element_edge_connectivity() const
202 {
203 int nedge = number_edges();
204 Ioss::IntVector econ(nedge);
205 for (int i = 0; i < nedge; i++) {
206 econ[i] = i;
207 }
208
209 return econ;
210 }
211
212 bool Ioss::ElementTopology::is_alias(const std::string &my_alias) const
213 {
214 std::string low_my_alias = Ioss::Utils::lowercase(my_alias);
215 auto iter = registry().find(low_my_alias);
216 if (iter == registry().end()) {
217 return false;
218 }
219 return this == (*iter).second;
220 }
221
222 bool Ioss::ElementTopology::is_element() const
223 {
224 // NOTE: This is overridden in some derived classes.
225 // The definition here is the default if not overridden.
226 return spatial_dimension() == parametric_dimension();
227 }
228
229 int Ioss::ElementTopology::number_boundaries() const
230 {
231 if (parametric_dimension() == 3 && spatial_dimension() == 3) {
232 return number_faces();
233 }
234
235 if (parametric_dimension() == 2 && spatial_dimension() == 2) {
236 return number_edges();
237 }
238
239 if (parametric_dimension() == 1 && !is_element()) {
240 return number_corner_nodes();
241 }
242
243 if (is_element()) {
244 if (parametric_dimension() == 2) {
245 assert(spatial_dimension() == 3);
246 // A shell has faces and edges in its boundary...
247 return number_faces() + number_edges();
248 }
249 if (parametric_dimension() == 1) {
250 return 2; // For bar/beam/... boundary is nodes; for ShellLine it is edges
251 }
252 }
253 else {
254 if (parametric_dimension() == 2) {
255 assert(spatial_dimension() == 3);
256 return number_edges();
257 }
258 }
259 return 0;
260 }
261
262 Ioss::IntVector Ioss::ElementTopology::boundary_connectivity(int bnd_number) const
263 {
264 if (parametric_dimension() == 3 && spatial_dimension() == 3) {
265 return face_connectivity(bnd_number);
266 }
267
268 if (parametric_dimension() == 2 && spatial_dimension() == 2) {
269 return edge_connectivity(bnd_number);
270 }
271
272 if (is_element()) {
273 if (parametric_dimension() == 2) {
274 assert(spatial_dimension() == 3);
275 // A shell has faces and edges in its boundary...
276 if (bnd_number > number_faces()) {
277 return edge_connectivity(bnd_number - number_faces());
278 }
279 return face_connectivity(bnd_number);
280 }
281 if (parametric_dimension() == 1) {
282 if (number_edges() > 1) {
283 return edge_connectivity(bnd_number);
284 }
285 // Spring-type element -- has node as boundary.
286 return Ioss::IntVector{bnd_number - 1};
287 }
288 }
289 else {
290 if (parametric_dimension() == 2) {
291 assert(spatial_dimension() == 3);
292 return edge_connectivity(bnd_number);
293 }
294 if (parametric_dimension() == 1) {
295 // Spring/line-type element -- has node as boundary.
296 return Ioss::IntVector{bnd_number - 1};
297 }
298 }
299 return {};
300 }
301
302 Ioss::ElementTopology *Ioss::ElementTopology::boundary_type(int bnd_number) const
303 {
304 if (parametric_dimension() == 3 && spatial_dimension() == 3) {
305 return face_type(bnd_number);
306 }
307
308 if (parametric_dimension() == 2 && spatial_dimension() == 2) {
309 return edge_type(bnd_number);
310 }
311
312 if (is_element()) {
313 if (parametric_dimension() == 2) {
314 // A shell has faces and edges in its boundary...
315 if (bnd_number == 0) {
316 return nullptr;
317 }
318
319 assert(spatial_dimension() == 3);
320 if (bnd_number > number_faces()) {
321 return edge_type(bnd_number - number_faces());
322 }
323 return face_type(bnd_number);
324 }
325 if (parametric_dimension() == 1) {
326 if (number_edges() > 1) {
327 return edge_type(bnd_number);
328 }
329 // Spring-type element -- has node as boundary.
330 return Ioss::ElementTopology::factory("node");
331 }
332 }
333 else {
334 if (parametric_dimension() == 2) {
335 assert(spatial_dimension() == 3);
336 return edge_type(bnd_number);
337 }
338 if (parametric_dimension() == 1) {
339 assert(spatial_dimension() == 3 || spatial_dimension() == 2);
340 return Ioss::ElementTopology::factory("node");
341 }
342 }
343 return nullptr;
344 }
345
346 bool Ioss::ElementTopology::equal_(const Ioss::ElementTopology &rhs, bool quiet) const
347 {
348 if (this->name_ != rhs.name_) {
349 if (!quiet) {
350 fmt::print(Ioss::OUTPUT(), "Element Topology: NAME mismatch ({} vs. {})\n",
351 this->name_.c_str(), rhs.name_.c_str());
352 }
353 return false;
354 }
355
356 if (this->masterElementName_ != rhs.masterElementName_) {
357 if (!quiet) {
358 fmt::print(Ioss::OUTPUT(), "Element Topology: MASTER ELEMENT NAME mismatch ({} vs. {})\n",
359 this->masterElementName_.c_str(), rhs.masterElementName_.c_str());
360 }
361 return false;
362 }
363
364 return true;
365 }
366
367 bool Ioss::ElementTopology::operator==(const Ioss::ElementTopology &rhs) const
368 {
369 return equal_(rhs, true);
370 }
371
372 bool Ioss::ElementTopology::operator!=(const Ioss::ElementTopology &rhs) const
373 {
374 return !(*this == rhs);
375 }
376
377 bool Ioss::ElementTopology::equal(const Ioss::ElementTopology &rhs) const
378 {
379 return equal_(rhs, false);
380 }
381
382 Ioss::ElementPermutation *Ioss::ElementTopology::permutation() const
383 {
384 auto *perm = Ioss::ElementPermutation::factory(base_topology_permutation_name());
385 assert(perm != nullptr);
386 if (validate_permutation_nodes()) {
387 if (static_cast<int>(perm->num_permutation_nodes()) != number_corner_nodes()) {
388 std::ostringstream errmsg;
389 fmt::print(errmsg,
390 "ERROR: The permutation node count: {} for topology '{}' does not match expected "
391 "value: {}.",
392 perm->num_permutation_nodes(), name(), number_corner_nodes());
393 IOSS_ERROR(errmsg);
394 }
395 }
396 return perm;
397 }
398
399 const std::string &Ioss::ElementTopology::base_topology_permutation_name() const
400 {
401 return topology_shape_to_permutation_name(shape());
402 }
403
404 const std::string &
405 Ioss::ElementTopology::topology_shape_to_permutation_name(Ioss::ElementShape topoShape)
406 {
407 static ElementShapeMap shapeToPermutationNameMap_ = {
408 {ElementShape::UNKNOWN, "none"}, {ElementShape::POINT, "none"},
409 {ElementShape::SPHERE, "sphere"}, {ElementShape::LINE, "line"},
410 {ElementShape::SPRING, "spring"}, {ElementShape::TRI, "tri"},
411 {ElementShape::QUAD, "quad"}, {ElementShape::TET, "tet"},
412 {ElementShape::PYRAMID, "pyramid"}, {ElementShape::WEDGE, "wedge"},
413 {ElementShape::HEX, "hex"}, {ElementShape::SUPER, "super"}};
414
415 auto iter = shapeToPermutationNameMap_.find(topoShape);
416 if (iter == shapeToPermutationNameMap_.end()) {
417 std::ostringstream errmsg;
418 fmt::print(errmsg, "ERROR: The topology shape '{}' is not supported.",
419 Ioss::Utils::shape_to_string(topoShape));
420 IOSS_ERROR(errmsg);
421 }
422
423 return iter->second;
424 }