Loading [MathJax]/jax/input/TeX/config.js
OGS
IdentifySubdomainMesh.cpp
Go to the documentation of this file.
1
10#include <range/v3/range/conversion.hpp>
11#include <unordered_map>
12#include <vector>
13
14#include "BaseLib/RunTime.h"
16#include "MeshLib/Mesh.h"
17#include "MeshLib/Node.h"
19#include "MeshNodeSearcher.h"
20
21namespace
22{
25std::vector<std::size_t> identifySubdomainMeshNodes(
26 MeshLib::Mesh const& subdomain_mesh,
27 MeshGeoToolsLib::MeshNodeSearcher const& mesh_node_searcher)
28{
29 // Convert nodes pointers needed for the mesh_node_searcher algorithm.
30 auto const& nodes = subdomain_mesh.getNodes();
31 std::vector<MathLib::Point3dWithID*> subdomain_points{begin(nodes),
32 end(nodes)};
33
34 auto const& bulk_node_ids =
35 mesh_node_searcher.getMeshNodeIDs(subdomain_points);
36
37 if (bulk_node_ids.size() != subdomain_mesh.getNumberOfNodes())
38 {
40 "Expected to find exactly one node in the bulk mesh for each node "
41 "of the subdomain; Found {:d} nodes in the bulk mesh out of {:d} "
42 "nodes in the subdomain.",
43 bulk_node_ids.size(), subdomain_mesh.getNumberOfNodes());
44 }
45
46 return bulk_node_ids;
47}
48
52std::vector<std::size_t> findElementsInMesh(
53 std::vector<std::size_t> const& node_ids,
54 std::vector<std::vector<std::size_t>> const& connected_element_ids_per_node)
55{
56 //
57 // Count how often an element is shared by all nodes.
58 //
59 std::unordered_map<std::size_t, int> element_counts(8);
60 for (auto const node_id : node_ids)
61 {
62 for (auto const element_id : connected_element_ids_per_node[node_id])
63 {
64 element_counts[element_id]++;
65 }
66 }
67
68 //
69 // Elements which are shared by as many nodes as the input nodes are the
70 // desired elements.
71 //
72 auto const nnodes = node_ids.size();
73 std::vector<std::size_t> element_ids;
74 for (auto const& pair : element_counts)
75 {
76 if (pair.second == static_cast<int>(nnodes))
77 {
78 element_ids.push_back(pair.first);
79 }
80 }
81
82 return element_ids;
83}
84
88std::vector<std::vector<std::size_t>> identifySubdomainMeshElements(
89 MeshLib::Mesh const& subdomain_mesh, MeshLib::Mesh const& bulk_mesh)
90{
91 auto const& bulk_node_ids = *MeshLib::bulkNodeIDs(subdomain_mesh);
92
93 // Allocate space for all elements for random insertion.
94 std::vector<std::vector<std::size_t>> bulk_element_ids_map(
95 subdomain_mesh.getNumberOfElements());
96
97 // For each node a vector of connected element ids of that node.
98 std::vector<std::vector<std::size_t>> connected_element_ids_per_node(
99 bulk_mesh.getNumberOfNodes());
100 for (auto const node_id : bulk_mesh.getNodes() | MeshLib::views::ids)
101 {
102 connected_element_ids_per_node[node_id] =
103 bulk_mesh.getElementsConnectedToNode(node_id) |
104 MeshLib::views::ids | ranges::to<std::vector>;
105 }
106
107 auto const& elements = subdomain_mesh.getElements();
108#pragma omp parallel for
109 for (std::ptrdiff_t j = 0; j < std::ssize(elements); ++j)
110 {
111 auto* const e = elements[j];
112 std::vector<std::size_t> element_node_ids(e->getNumberOfBaseNodes());
113 for (unsigned n = 0; n < e->getNumberOfBaseNodes(); ++n)
114 {
115 element_node_ids[n] = MeshLib::getNodeIndex(*e, n);
116 }
117 std::vector<std::size_t> element_node_ids_bulk(
118 e->getNumberOfBaseNodes());
119 std::transform(begin(element_node_ids), end(element_node_ids),
120 begin(element_node_ids_bulk),
121 [&bulk_node_ids](std::size_t const id)
122 { return bulk_node_ids[id]; });
123
124 std::vector<std::size_t> bulk_element_ids = findElementsInMesh(
125 element_node_ids_bulk, connected_element_ids_per_node);
126
127 if (bulk_element_ids.empty())
128 {
129 ERR("No element could be found for the subdomain element {:d}. "
130 "Corresponding bulk mesh node ids are:",
131 e->getID());
132 for (auto const i : element_node_ids_bulk)
133 {
134 ERR("\t{:d}", i);
135 }
136 OGS_FATAL(
137 "Expect at least one element to be found in the bulk mesh.");
138 }
139
140 bulk_element_ids_map[e->getID()] = std::move(bulk_element_ids);
141 }
142
143 return bulk_element_ids_map;
144}
145
148 MeshLib::Mesh& mesh, std::string_view property_name,
149 std::vector<std::size_t> const& values,
150 MeshLib::MeshItemType const mesh_item_type, bool const force_overwrite)
151{
152 auto& properties = mesh.getProperties();
153 if (!properties.existsPropertyVector<std::size_t>(property_name))
154 {
155 addPropertyToMesh(mesh, property_name, mesh_item_type, 1, values);
156 return;
157 }
158
159 //
160 // Check the existing property against new values.
161 //
162 auto& original_property =
163 *properties.getPropertyVector<std::size_t>(property_name);
164 if (std::equal(begin(original_property), end(original_property),
165 begin(values), end(values)))
166 {
167 INFO(
168 "There is already a '{:s}' property present in the subdomain mesh "
169 "'{:s}' and it is equal to the newly computed values.",
170 property_name, mesh.getName());
171 return;
172 }
173
174 //
175 // Property differs. Notify and update if forced.
176 //
177 WARN(
178 "There is already a '{:s}' property present in the subdomain mesh "
179 "'{:s}' and it is not equal to the newly computed values.",
180 property_name,
181 mesh.getName());
182
183 if (!force_overwrite)
184 {
185 OGS_FATAL("The force overwrite flag was not specified, exiting.");
186 }
187
188 INFO("Overwriting '{:s}' property.", property_name);
189 original_property.resize(values.size());
190 std::copy(begin(values), end(values), begin(original_property));
191}
192} // namespace
193
194namespace MeshGeoToolsLib
195{
197 MeshLib::Mesh const& bulk_mesh,
198 MeshNodeSearcher const& mesh_node_searcher,
199 bool const force_overwrite = false)
200{
201 BaseLib::RunTime time;
202 time.start();
203 auto const& bulk_node_ids =
204 identifySubdomainMeshNodes(subdomain_mesh, mesh_node_searcher);
205 INFO("identifySubdomainMesh(): identifySubdomainMeshNodes took {:g} s",
206 time.elapsed());
207
208 updateOrCheckExistingSubdomainProperty(
210 bulk_node_ids, MeshLib::MeshItemType::Node, force_overwrite);
211
212 time.start();
213 auto const& bulk_element_ids =
214 identifySubdomainMeshElements(subdomain_mesh, bulk_mesh);
215 INFO("identifySubdomainMesh(): identifySubdomainMeshElements took {:g} s",
216 time.elapsed());
217
218 // The bulk_element_ids could be of two types: one element per entry---this
219 // is the expected case for the boundary meshes; multiple elements per
220 // entry---this happens if the subdomain mesh lies inside the bulk mesh and
221 // has lower dimension.
222 // First find out the type, then add/check the CellData or FieldData.
223 if (all_of(begin(bulk_element_ids), end(bulk_element_ids),
224 [](std::vector<std::size_t> const& v) { return v.size() == 1; }))
225 {
226 // All vectors are of size 1, so the data can be flattened and
227 // stored in CellData or compared to existing CellData.
228 std::vector<std::size_t> unique_bulk_element_ids;
229 unique_bulk_element_ids.reserve(bulk_element_ids.size());
230 transform(begin(bulk_element_ids), end(bulk_element_ids),
231 back_inserter(unique_bulk_element_ids),
232 [](std::vector<std::size_t> const& v) { return v[0]; });
233
234 updateOrCheckExistingSubdomainProperty(
235 subdomain_mesh,
237 unique_bulk_element_ids, MeshLib::MeshItemType::Cell,
238 force_overwrite);
239 }
240 else
241 {
242 // Some of the boundary elements are connected to multiple bulk
243 // elements; Store the array in FieldData with additional CellData array
244 // for the number of elements, which also provides the offsets.
245 std::vector<std::size_t> flat_bulk_element_ids;
246 flat_bulk_element_ids.reserve(2 * bulk_element_ids.size()); // Guess.
247 std::vector<std::size_t> number_of_bulk_element_ids;
248 number_of_bulk_element_ids.reserve(bulk_element_ids.size());
249
250 for (std::vector<std::size_t> const& v : bulk_element_ids)
251 {
252 number_of_bulk_element_ids.push_back(v.size());
253 flat_bulk_element_ids.insert(end(flat_bulk_element_ids), begin(v),
254 end(v));
255 }
256
257 updateOrCheckExistingSubdomainProperty(
258 subdomain_mesh, "number_bulk_elements", number_of_bulk_element_ids,
259 MeshLib::MeshItemType::Cell, force_overwrite);
260 updateOrCheckExistingSubdomainProperty(
261 subdomain_mesh,
263 flat_bulk_element_ids, MeshLib::MeshItemType::IntegrationPoint,
264 force_overwrite);
265 }
266}
267} // namespace MeshGeoToolsLib
Definition of the Element class.
#define OGS_FATAL(...)
Definition Error.h:26
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
Definition of the Mesh class.
Definition of the Node class.
Definition of the RunTime class.
Count the running time.
Definition RunTime.h:29
double elapsed() const
Get the elapsed time in seconds.
Definition RunTime.h:42
void start()
Start the timer.
Definition RunTime.h:32
std::vector< std::size_t > getMeshNodeIDs(GeoLib::GeoObject const &geoObj) const
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition Mesh.h:108
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:111
Properties & getProperties()
Definition Mesh.h:136
const std::string getName() const
Get name of the mesh.
Definition Mesh.h:105
std::size_t getNumberOfNodes() const
Get the number of nodes.
Definition Mesh.h:102
std::vector< Element const * > const & getElementsConnectedToNode(std::size_t node_id) const
Definition Mesh.cpp:256
std::size_t getNumberOfElements() const
Get the number of elements.
Definition Mesh.h:99
void identifySubdomainMesh(MeshLib::Mesh &subdomain_mesh, MeshLib::Mesh const &bulk_mesh, MeshNodeSearcher const &mesh_node_searcher, bool const force_overwrite=false)
constexpr ranges::views::view_closure ids
For an element of a range view return its id.
Definition Mesh.h:227
constexpr std::string_view getBulkIDString(MeshItemType mesh_item_type)
Definition Properties.h:185
std::size_t getNodeIndex(Element const &element, unsigned const idx)
Definition Element.cpp:219
PropertyVector< std::size_t > const * bulkNodeIDs(Mesh const &mesh)
Definition Mesh.cpp:292
std::vector< std::size_t > identifySubdomainMeshNodes(MeshLib::Mesh const &subdomain_mesh, MeshGeoToolsLib::MeshNodeSearcher const &mesh_node_searcher)
void updateOrCheckExistingSubdomainProperty(MeshLib::Mesh &mesh, std::string_view property_name, std::vector< std::size_t > const &values, MeshLib::MeshItemType const mesh_item_type, bool const force_overwrite)
Updates or checks the existing mesh's property with the given values.
std::vector< std::vector< std::size_t > > identifySubdomainMeshElements(MeshLib::Mesh const &subdomain_mesh, MeshLib::Mesh const &bulk_mesh)
std::vector< std::size_t > findElementsInMesh(std::vector< std::size_t > const &node_ids, std::vector< std::vector< std::size_t > > const &connected_element_ids_per_node)