Debian Codegraph @

packages/seacas/libraries/ioss/src/Ioss_FaceGenerator.h

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 #pragma once
8
9 #include <algorithm>
10 #include <array>
11 #include <cassert>
12 #include <cstddef>
13 #include <map>
14 #include <string>
15 #include <vector>
16
17 #include "Ioss_Region.h"
18 #include "ioss_export.h"
19
20 #define FG_USE_ROBIN
21 #if defined FG_USE_STD
22 #include <unordered_set>
23 #elif defined FG_USE_HOPSCOTCH
24 #include <hopscotch_set.h>
25 #elif defined FG_USE_ROBIN
26 #include <robin_set.h>
27 #endif
28
29 #include <utility>
30
31 namespace Ioss {
32 class ElementBlock;
33 class IOSS_EXPORT Face
34 {
35 public:
36 Face() = default;
37 Face(size_t id, std::array<size_t, 4> conn) : hashId_(id), connectivity_(conn) {}
38 explicit Face(std::array<size_t, 4> conn);
39
40 void add_element(size_t element_id) const
41 {
42 assert(element_id != 0);
43 if (element[0] == 0) {
44 element[0] = element_id;
45 }
46 else if (element[1] == 0) {
47 element[1] = element_id;
48 }
49 else {
50 face_element_error(element_id);
51 }
52 }
53
54 int element_count() const { return (element[0] != 0) + (element[1] != 0); }
55
56 void add_element(size_t element_id, size_t face_ordinal) const
57 {
58 add_element(element_id * 10 + face_ordinal);
59 }
60
61 void face_element_error(size_t element_id) const;
62
63 size_t hashId_{0};
64
65 // NOTE: Not used at all by `Face` or `FaceGenerator` class, but are used by
66 // skinner to give a consistent element id in cases where there
67 // is a hash collision (face.id).
68
69 // NOTE: For interior faces, this will not be the same value for each
70 // face where the `hashId_` *will* be consistent for interior faces.
71 // Should only use this as an id if `elementCount_` is 1.
72
73 // NOTE: This could be used to do parallel or block boundary
74 // collision since it is calculated as 10*element_id + local_face,
75 // you could recover element_id and local_face and then set up
76 // parallel communication maps. May need to save the proc it is
77 // shared with also (which is available in git history)
78 mutable std::array<size_t, 2> element{};
79 std::array<size_t, 4> connectivity_{};
80 };
81
82 struct IOSS_EXPORT FaceHash
83 {
84 size_t operator()(const Face &face) const { return face.hashId_; }
85 };
86
87 struct IOSS_EXPORT FaceEqual
88 {
89 bool operator()(const Face &left, const Face &right) const
90 {
91 if (left.hashId_ != right.hashId_) {
92 return false;
93 }
94 // Hash (hashId_) is equal
95 // Check whether same vertices (can be in different order)
96 // Most (All?) of the time, there are no hashId_ collisions, so this test will not
97 // find a difference and the function will return 'true'
98 // However, for some reason, removing this check does not change the execution time
99 // appreiciably...
100
101 // TODO: Loop can probably be replaced by std::all_of...
102 for (auto lvert : left.connectivity_) {
103 if (std::find(right.connectivity_.cbegin(), right.connectivity_.cend(), lvert) ==
104 right.connectivity_.cend()) {
105 // Not found, therefore not the same.
106 return false;
107 }
108 }
109 return true;
110 }
111 };
112
113 #if defined FG_USE_STD
114 using FaceUnorderedSet = std::unordered_set<Face, FaceHash, FaceEqual>;
115 #elif defined FG_USE_HOPSCOTCH
116 // using FaceUnorderedSet = tsl::hopscotch_set<Face, FaceHash, FaceEqual>;
117 using FaceUnorderedSet = tsl::hopscotch_pg_set<Face, FaceHash, FaceEqual>;
118 #elif defined FG_USE_ROBIN
119 // using FaceUnorderedSet = tsl::robin_set<Face, FaceHash, FaceEqual>;
120 using FaceUnorderedSet = tsl::robin_pg_set<Face, FaceHash, FaceEqual>;
121 #endif
122 class IOSS_EXPORT FaceGenerator
123 {
124 public:
125 explicit FaceGenerator(Ioss::Region &region);
126
127 static size_t id_hash(size_t global_id);
128
129 template <typename INT>
130 void generate_faces(INT /*dummy*/, bool block_by_block = false, bool local_ids = false);
131 template <typename INT>
132 void generate_block_faces(const ElementBlockContainer &ebs, INT /*dummy*/,
133 bool local_ids = false);
134
135 FaceUnorderedSet &faces(const std::string &name = "ALL") { return faces_[name]; }
136 FaceUnorderedSet &faces(const ElementBlock *block);
137
138 void clear(const std::string &name) { faces_[name].clear(); }
139 void clear(const ElementBlock *block);
140
141 //! Given a local node id (0-based), return the hashed value.
142 size_t node_id_hash(size_t local_node_id) const { return hashIds_[local_node_id]; }
143
144 void progress(const std::string &output) const;
145
146 private:
147 template <typename INT> void hash_node_ids(const std::vector<INT> &node_ids);
148 void hash_local_node_ids(size_t count);
149 template <typename INT> void generate_model_faces(INT /*dummy*/, bool local_ids);
150
151 Ioss::Region &region_;
152 std::map<std::string, FaceUnorderedSet> faces_;
153 std::vector<size_t> hashIds_;
154 };
155
156 } // namespace Ioss