https://mooseframework.inl.gov
DeleteElementsNearMeshGenerator.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 
11 #include "SetupQuadratureAction.h"
12 #include "KDTree.h"
13 
14 #include "libmesh/type_vector.h"
15 #include "libmesh/point.h"
16 #include "libmesh/elem.h"
17 #include "libmesh/fe_base.h"
18 #include "libmesh/quadrature_gauss.h"
19 
21 
24 {
26 
27  params.addClassDescription(
28  "Removes elements lying \"near\" another mesh. The proximity is examined by the distance "
29  "from "
30  "the element's centroid to the faces of elements of the \"proximity_mesh\"");
31  params.addRequiredParam<MeshGeneratorName>("proximity_mesh",
32  "Mesh providing the deletion criterion");
34  "distance",
35  "distance>0",
36  "The distance from the centroid of elements in the 'input' mesh to elements in the "
37  "'proximity_mesh' under which they are marked for deletion");
38  auto options_order = SetupQuadratureAction::getQuadratureOrderEnum();
39  options_order.assign(CONSTANT);
40  params.addParam<MooseEnum>("side_order",
41  options_order,
42  "Order of the face quadrature used to find the nearest face in the "
43  "'proximity_mesh'. Default is CONSTANT");
44 
45  return params;
46 }
47 
49  : ElementDeletionGeneratorBase(parameters),
50  _proximity_mesh(getMesh("proximity_mesh")),
51  _distance(getParam<Real>("distance"))
52 {
53 }
54 
55 std::unique_ptr<MeshBase>
57 {
58  // Build the point locator to detect elements inside
59  _pl = _proximity_mesh->sub_point_locator();
60  _pl->enable_out_of_mesh_mode();
61 
62  // Build a KNN with side Qps. This will help locate the nearest side if known to be outside
63  // NOTE: side Qps are just one option. We could have done nodes, side centroids (~ side Qp at
64  // order 0)
65  std::vector<Point> all_side_qps;
66  const auto & order = getParam<MooseEnum>("side_order");
67  const auto order_enum = Moose::stringToEnum<Order>(order);
68  for (const auto * const elem : _proximity_mesh->element_ptr_range())
69  {
70  // Build side then side Qps
71  const auto dim = elem->dim();
72  for (const auto side_i : make_range(elem->n_sides()))
73  {
74  // Internal sides cannot be closest to an external point
75  if (elem->neighbor_ptr(side_i))
76  continue;
77  const std::unique_ptr<const Elem> face = elem->build_side_ptr(side_i);
78  std::unique_ptr<libMesh::FEBase> fe(
79  libMesh::FEBase::build(dim, libMesh::FEType(elem->default_order())));
80  libMesh::QGauss qface(dim - 1, order_enum);
81  fe->attach_quadrature_rule(&qface);
82  const auto & qpoints = fe->get_xyz();
83  fe->reinit(elem, side_i);
84  all_side_qps.insert(all_side_qps.end(), qpoints.begin(), qpoints.end());
85  }
86  }
87  mooseAssert(!all_side_qps.empty(), "Should have found side Qps");
88  _kd_tree = std::make_unique<KDTree>(all_side_qps, /*max leaf size*/ 1);
89 
90  // Abide by the rules
91  // NOTE: don't use _proximity_mesh in shouldDelete()! It won't work, it's gone
92  const auto prox_mg = std::move(_proximity_mesh);
93 
94  // Perform the deletions
96 }
97 
98 bool
100 {
101  const auto delete_due_to_point = [this](const Point & pt) -> bool
102  {
103  // Proximity mesh contains the point, distance is zero
104  if ((*_pl)(pt))
105  return true;
106 
107  // Use the KNN to get the distance
108  std::vector<Real> distance_sqr(2);
109  std::vector<std::size_t> return_indices(2);
110  _kd_tree->neighborSearch(pt, /*num_search*/ 1, return_indices, distance_sqr);
111  const auto distance =
112  distance_sqr.empty() ? std::numeric_limits<Real>::max() : std::sqrt(distance_sqr[0]);
113 
114  return distance < _distance;
115  };
116 
117  // Check element centroid
118  const auto centroid = elem->vertex_average();
119  if (delete_due_to_point(centroid))
120  return true;
121 
122  // Then its nodes. For convex elements, this should be enough
123  for (const auto & node : elem->node_ref_range())
124  if (delete_due_to_point(node))
125  return true;
126 
127  return false;
128 }
std::unique_ptr< FEGenericBase< Real > > build(const unsigned int dim, const FEType &fet)
static MooseEnum getQuadratureOrderEnum()
Return the potential selections for the order of the quadrature, with an &#39;auto&#39; default.
void addRequiredRangeCheckedParam(const std::string &name, const std::string &parsed_function, const std::string &doc_string)
These methods add an range checked parameters.
std::shared_ptr< KDTree > _kd_tree
KD Tree to find the nearest side Qp in the proximity mesh.
This class deletes elements from the mesh data structure after it has been generated or read but befo...
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
static constexpr std::size_t dim
This is the dimension of all vector and tensor datastructures used in MOOSE.
Definition: Moose.h:163
const Real _distance
Distance for deletion criterion.
Real distance(const Point &p)
Order stringToEnum< Order >(const std::string &s)
Definition: Conversion.C:175
void addRequiredParam(const std::string &name, const std::string &doc_string)
This method adds a parameter and documentation string to the InputParameters object that will be extr...
auto max(const L &left, const R &right)
virtual bool shouldDelete(const Elem *elem) override
Method that returns a Boolean indicating whether an element should be removed from the mesh...
CONSTANT
virtual std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:54
DeleteElementsNearMeshGenerator(const InputParameters &parameters)
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
CTSub CT_OPERATOR_BINARY CTMul CTCompareLess CTCompareGreater CTCompareEqual _arg template * sqrt(_arg)) *_arg.template D< dtag >()) CT_SIMPLE_UNARY_FUNCTION(tanh
std::unique_ptr< MeshBase > & _proximity_mesh
Mesh used to define the proximity deletion criterion.
IntRange< T > make_range(T beg, T end)
registerMooseObject("MooseApp", DeleteElementsNearMeshGenerator)
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
Deletes elements close to another mesh.
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object...
std::unique_ptr< libMesh::PointLocatorBase > _pl
Point locator to weed out elements inside the proximity mesh.