OGS
MeshSubset.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#pragma once
5
6#include <cassert>
7#include <vector>
8
9#include "Mesh.h"
10#include "Node.h"
11#include "Elements/Element.h"
12
13namespace MeshLib
14{
17{
18public:
24 MeshSubset(const Mesh& msh, std::vector<Node*> const& vec_items,
25 const bool use_taylor_hood_elements = false)
26 : _msh(msh),
27 _nodes(vec_items),
28 _use_taylor_hood_elements(use_taylor_hood_elements)
29 {
30 // If the mesh nodes and the given nodes point to the same vector, they
31 // must be equal.
32 if (&_msh.getNodes() == &_nodes)
33 {
34 return;
35 }
36
37 //
38 // Testing if the given nodes belong to the mesh.
39 //
40 {
41 // Need sorted version of the large vector.
42 auto sorted_nodes = _msh.getNodes(); // full copy of pointers.
43 sort(begin(sorted_nodes), end(sorted_nodes));
44
45 // Then proceed with the search function.
46 auto node_is_part_of_mesh =
47 [&mesh_nodes = sorted_nodes](MeshLib::Node* const& n)
48 {
49 auto it = lower_bound(begin(mesh_nodes), end(mesh_nodes), n);
50 if (it == end(mesh_nodes))
51 {
52 ERR("A node {:d} ({:g}, {:g}, {:g}) in mesh subset is not "
53 "a part of the mesh.",
54 n->getID(), (*n)[0], (*n)[1], (*n)[2]);
55 return false;
56 }
57 return true;
58 };
59 if (!std::all_of(begin(_nodes), end(_nodes), node_is_part_of_mesh))
60 {
61 OGS_FATAL("The mesh subset construction failed.");
62 }
63 }
64 }
65
67 std::size_t getMeshID() const { return _msh.getID(); }
68
70
71 std::vector<Element*>::const_iterator elementsBegin() const
72 {
73 return _msh.getElements().cbegin();
74 }
75
76 std::vector<Element*>::const_iterator elementsEnd() const
77 {
78 return _msh.getElements().cend();
79 }
80
81 std::vector<Node*> const& getNodes() const { return _nodes; }
82
83 Mesh const& getMesh() const { return _msh; }
84
85private:
86 Mesh const& _msh;
87 std::vector<Node*> const& _nodes;
89};
90
91namespace views
92{
93inline auto meshLocations(MeshSubset const& mesh_subset,
94 MeshItemType const item_type)
95{
96 return mesh_subset.getNodes() | ids |
97 ranges::views::transform(
98 [mesh_id = mesh_subset.getMeshID(),
99 item_type](std::size_t const node_id)
100 { return Location{mesh_id, item_type, node_id}; });
101}
102} // namespace views
103} // namespace MeshLib
#define OGS_FATAL(...)
Definition Error.h:19
A subset of nodes on a single mesh.
Definition MeshSubset.h:17
std::vector< Node * > const & _nodes
Definition MeshSubset.h:87
Mesh const & _msh
Definition MeshSubset.h:86
MeshSubset(const Mesh &msh, std::vector< Node * > const &vec_items, const bool use_taylor_hood_elements=false)
Definition MeshSubset.h:24
bool useTaylorHoodElements() const
Definition MeshSubset.h:69
std::vector< Element * >::const_iterator elementsEnd() const
Definition MeshSubset.h:76
Mesh const & getMesh() const
Definition MeshSubset.h:83
std::size_t getMeshID() const
return this mesh ID
Definition MeshSubset.h:67
std::vector< Element * >::const_iterator elementsBegin() const
Definition MeshSubset.h:71
bool const _use_taylor_hood_elements
Definition MeshSubset.h:88
std::vector< Node * > const & getNodes() const
Definition MeshSubset.h:81
MeshLib specific, lazy, non-owning, non-mutating, composable range views.
Definition Mesh.h:214
constexpr ranges::views::view_closure ids
For an element of a range view return its id.
Definition Mesh.h:216
auto meshLocations(Mesh const &mesh, MeshItemType const item_type)
Definition Mesh.h:227