https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
12#include "KDTree.h"
13#include "MooseMeshUtils.h"
14
15#include "libmesh/type_vector.h"
16#include "libmesh/point.h"
17#include "libmesh/elem.h"
18#include "libmesh/fe_base.h"
19#include "libmesh/quadrature_gauss.h"
20
22
25{
27
29 "Removes elements lying \"near\" another mesh. The proximity is examined by the distance "
30 "from "
31 "the element's centroid to the faces of elements of the \"proximity_mesh\"");
32 params.addParam<std::vector<SubdomainName>>(
33 "blocks_included",
34 {},
35 "Subdomains from the input mesh that may have their elements deleted if they are close to "
36 "the proximity mesh");
37 params.addRequiredParam<MeshGeneratorName>("proximity_mesh",
38 "Mesh providing the deletion criterion");
40 "distance",
41 "distance>0",
42 "The distance from the centroid of elements in the 'input' mesh to elements in the "
43 "'proximity_mesh' under which they are marked for deletion");
45 options_order.assign("CONSTANT");
46 params.addParam<MooseEnum>("side_order",
47 options_order,
48 "Order of the face quadrature used to find the nearest face in the "
49 "'proximity_mesh'. Default is CONSTANT");
50
51 return params;
52}
53
55 : ElementDeletionGeneratorBase(parameters),
56 _blocks_included(getParam<std::vector<SubdomainName>>("blocks_included")),
57 _proximity_mesh(getMesh("proximity_mesh")),
58 _distance(getParam<Real>("distance"))
59{
60}
61
62std::unique_ptr<MeshBase>
64{
65 // Process the inclusion list of subdomains
67
68 // Build the point locator to detect elements inside
69 _pl = _proximity_mesh->sub_point_locator();
70 _pl->enable_out_of_mesh_mode();
71
72 // Build a KNN with side Qps. This will help locate the nearest side if known to be outside
73 // NOTE: side Qps are just one option. We could have done nodes, side centroids (~ side Qp at
74 // order 0)
75 std::vector<Point> all_side_qps;
76 const auto & order = getParam<MooseEnum>("side_order");
77 const auto order_enum = Moose::stringToEnum<Order>(order);
78 for (const auto * const elem : _proximity_mesh->element_ptr_range())
79 {
80 // Build side then side Qps
81 const auto dim = elem->dim();
82 for (const auto side_i : make_range(elem->n_sides()))
83 {
84 // Internal sides cannot be closest to an external point
85 if (elem->neighbor_ptr(side_i))
86 continue;
87 const std::unique_ptr<const Elem> face = elem->build_side_ptr(side_i);
88 std::unique_ptr<libMesh::FEBase> fe(
89 libMesh::FEBase::build(dim, libMesh::FEType(elem->default_order())));
90 libMesh::QGauss qface(dim - 1, order_enum);
91 fe->attach_quadrature_rule(&qface);
92 const auto & qpoints = fe->get_xyz();
93 fe->reinit(elem, side_i);
94 all_side_qps.insert(all_side_qps.end(), qpoints.begin(), qpoints.end());
95 }
96 }
97 mooseAssert(!all_side_qps.empty(), "Should have found side Qps");
98 _kd_tree = std::make_unique<KDTree>(all_side_qps, /*max leaf size*/ 1);
99
100 // Abide by the rules
101 // NOTE: don't use _proximity_mesh in shouldDelete()! It won't work, it's gone
102 const auto prox_mg = std::move(_proximity_mesh);
103
104 // Perform the deletions
106}
107
108bool
110{
111 const auto delete_due_to_point = [this](const Point & pt) -> bool
112 {
113 // Proximity mesh contains the point, distance is zero
114 if ((*_pl)(pt))
115 return true;
116
117 // Use the KNN to get the distance
118 std::vector<Real> distance_sqr(2);
119 std::vector<std::size_t> return_indices(2);
120 _kd_tree->neighborSearch(pt, /*num_search*/ 1, return_indices, distance_sqr);
121 const auto distance =
122 distance_sqr.empty() ? std::numeric_limits<Real>::max() : std::sqrt(distance_sqr[0]);
123
124 return distance < _distance;
125 };
126
127 // Check if element is in the included subdomains
128 if (_blocks_included_ids.size() &&
129 std::find(_blocks_included_ids.begin(), _blocks_included_ids.end(), elem->subdomain_id()) ==
131 return false;
132
133 // Check element centroid
134 const auto centroid = elem->vertex_average();
135 if (delete_due_to_point(centroid))
136 return true;
137
138 // Then its nodes. For convex elements, this should be enough
139 for (const auto & node : elem->node_ref_range())
140 if (delete_due_to_point(node))
141 return true;
142
143 return false;
144}
registerMooseObject("MooseApp", DeleteElementsNearMeshGenerator)
unsigned int dim
Deletes elements close to another mesh.
std::unique_ptr< libMesh::PointLocatorBase > _pl
Point locator to weed out elements inside the proximity mesh.
DeleteElementsNearMeshGenerator(const InputParameters &parameters)
virtual bool shouldDelete(const Elem *elem) override
Method that returns a Boolean indicating whether an element should be removed from the mesh.
std::vector< SubdomainID > _blocks_included_ids
Subdomain IDs to include when deleting elements.
std::unique_ptr< MeshBase > & _proximity_mesh
Mesh used to define the proximity deletion criterion.
std::shared_ptr< KDTree > _kd_tree
KD Tree to find the nearest side Qp in the proximity mesh.
const Real _distance
Distance for deletion criterion.
virtual std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
const std::vector< SubdomainName > & _blocks_included
Subdomain names to include when deleting elements.
This class deletes elements from the mesh data structure after it has been generated or read but befo...
std::unique_ptr< MeshBase > & _input
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void addRequiredRangeCheckedParam(const std::string &name, const std::string &parsed_function, const std::string &doc_string)
These methods add an range checked parameters.
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.
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...
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.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition MooseEnum.h:55
static MooseEnum getQuadratureOrderEnum()
Return the potential selections for the order of the quadrature, with an 'auto' default.
std::unique_ptr< FEGenericBase< Real > > build(const unsigned int dim, const FEType &fet)
std::vector< subdomain_id_type > getSubdomainIDs(const libMesh::MeshBase &mesh, const std::vector< SubdomainName > &subdomain_name)
Get the associated subdomainIDs for the subdomain names that are passed in.
Order stringToEnum< Order >(const std::string &s)
Definition Conversion.C:174
Real distance(const Point &p)