https://mooseframework.inl.gov
SBMSurfaceMeshBuilder.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "SBMSurfaceMeshBuilder.h"
11 #include "InputParameters.h"
12 #include "SBMBndEdge2.h"
13 #include "SBMBndTri3.h"
14 #include "SBMUtils.h"
15 
16 // Register object
17 registerMooseObject("ShiftedBoundaryMethodApp", SBMSurfaceMeshBuilder);
18 
21 {
23  params.addClassDescription(
24  "Constructs a KDTree and boundary elements from a pre-existing surface mesh. "
25  "The surface mesh is specified in the Mesh block.");
26 
27  params.addRequiredParam<std::string>(
28  "surface_mesh",
29  "The name of the surface mesh saved via the MeshGenerator's `save_mesh_as` parameter.");
30 
32  params.addParam<int>(
33  "leaf_max_size",
34  10,
35  "Maximum number of points allowed in a leaf node of the KDTree. "
36  "Smaller values yield deeper trees with faster queries but slower build times; "
37  "larger values result in shallower trees with faster builds but slower queries. "
38  "Benchmarking is recommended to find an optimal tradeoff for your use case.");
39 
41  params.addParam<bool>(
42  "check_watertightness",
43  false,
44  "Check if the mesh is watertight. If false, the mesh may not be suitable for In-Out tests.");
45 
47  params.addParam<bool>(
48  "build_kd_tree",
49  true,
50  "Whether to build a kd-tree or not. If false, the kd-tree will not be built, "
51  "and the mesh will be used directly for queries.");
52 
53  return params;
54 }
55 
57  : GeneralUserObject(parameters),
58  _leaf_max_size(getParam<int>("leaf_max_size")),
59  _bnd_mesh_name(getParam<std::string>("surface_mesh")),
60  _check_watertightness(getParam<bool>("check_watertightness")),
61  _build_kd_tree(getParam<bool>("build_kd_tree")),
62  _dim_embedding_mesh(_fe_problem.mesh().dimension() /*MooseMesh*/)
63 {
64 }
65 
66 void
68 {
69  auto & mesh_generator_system = _app.getMeshGeneratorSystem();
70 
71  _mesh = mesh_generator_system.getSavedMesh(_bnd_mesh_name);
72 
73  const auto expected_dim_embedding_mesh = _mesh->mesh_dimension() + 1;
74 
75  if (!_mesh->is_replicated())
76  mooseError(
77  "The mesh is distributed. Please use a serial mesh for SBMSurfaceMeshBuilder, which is "
78  "important for our In-Out test later.");
79 
80  if (_dim_embedding_mesh != expected_dim_embedding_mesh)
81  mooseError("The original mesh dimension (",
83  ") does not match the expected mesh dimension (",
84  expected_dim_embedding_mesh,
85  ").");
86 
87  const std::size_t num_elems = _mesh->n_elem();
88 
89  _centroids.resize(num_elems);
90  _boundary_elements.resize(num_elems);
91 
92  int i = 0;
93  for (auto elem_it = _mesh->active_elements_begin(); elem_it != _mesh->active_elements_end();
94  ++elem_it)
95  {
96  const Elem * elem = *elem_it;
97 
98  const auto centroid = elem->vertex_average();
99  if (_build_kd_tree)
100  _centroids[i] = centroid;
101 
102  if (elem->type() == EDGE2)
103  _boundary_elements[i] = std::make_unique<SBMBndEdge2>(elem);
104  else if (elem->type() == TRI3)
105  _boundary_elements[i] = std::make_unique<SBMBndTri3>(elem);
106  else
107  mooseError("Unsupported element type in SBMSurfaceMeshBuilder::initialSetup()");
108 
109  ++i;
110  }
111 
112  if (_build_kd_tree)
113  _kd_tree = std::make_unique<KDTree>(_centroids, _leaf_max_size);
114 
116  {
117  bool watertight = checkWatertightness();
118  if (!watertight)
119  mooseInfo("The mesh is not watertight. It may not be suitable for In-Out tests.");
120  else
121  mooseInfo("The mesh is watertight. It is suitable for In-Out tests.");
122  }
123 }
124 
125 bool
127 {
128  return SBMUtils::checkWatertightnessFromRawElems(std::vector<const Elem *>(
129  _mesh->active_element_ptr_range().begin(), _mesh->active_element_ptr_range().end()));
130 }
131 
132 KDTree &
134 {
135  mooseAssert(_kd_tree, "KDTree not built; callers must guard with hasKDTree() first.");
136  return *_kd_tree;
137 }
138 
139 const std::vector<std::unique_ptr<SBMBndElementBase>> &
141 {
142  return _boundary_elements;
143 }
144 
145 const std::vector<Point> &
147 {
148  return _centroids;
149 }
void mooseInfo(Args &&... args) const
std::vector< Point > _centroids
The centroids of the elements in the boundary mesh are stored in a vector of Points. The sequence is the same as in _boundary_elements.
virtual void initialSetup() override
static InputParameters validParams()
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
SBMSurfaceMeshBuilder(const InputParameters &parameters)
bool checkWatertightness() const
Checks whether the boundary mesh is "closed" – i.e.
MeshBase & mesh
registerMooseObject("ShiftedBoundaryMethodApp", SBMSurfaceMeshBuilder)
void addRequiredParam(const std::string &name, const std::string &doc_string)
TRI3
const std::string _bnd_mesh_name
The name of a mesh saved via MeshGenerator save_mesh_as parameter.
bool checkWatertightnessFromRawElems(const std::vector< const Elem *> &bd_elements)
Definition: SBMUtils.C:22
KDTree & getKDTree() const
Returns a mutable reference because KDTree::neighborSearch is non-const (nanoflann&#39;s knnSearch thread...
std::vector< std::unique_ptr< SBMBndElementBase > > _boundary_elements
The boundary elements are stored in a vector of unique pointers to SBMBndElementBase.
const std::vector< Point > & getCentroids() const
Get the centroids of the boundary elements.
const std::vector< std::unique_ptr< SBMBndElementBase > > & getBoundaryElements() const
Get the SBM boundary elements.
const int _leaf_max_size
Configures KDTree leaf node size for performance tuning.
const unsigned int _dim_embedding_mesh
The dimension of the embedding mesh (2D or 3D).
const bool _build_kd_tree
whether we want to build a kd-tree or not
EDGE2
std::unique_ptr< MeshBase > _mesh
Holds the mesh to ensure boundary element pointers remain valid.
void mooseError(Args &&... args) const
void addClassDescription(const std::string &doc_string)
MeshGeneratorSystem & getMeshGeneratorSystem()
std::unique_ptr< KDTree > _kd_tree
The KDTree is constructed using the centroids of the elements in the boundary mesh.
const bool _check_watertightness
The flag to check the watertightness of the mesh.
void ErrorVector unsigned int
static InputParameters validParams()