13#include "libmesh/elem.h"
14#include "libmesh/mesh_refinement.h"
24 params.
addClassDescription(
"Mesh generator which refines one or more blocks in an existing mesh");
26 params.
addRequiredParam<std::vector<SubdomainName>>(
"block",
"The list of blocks to be refined");
29 "Minimum amount of times to refine each block, corresponding to their index in 'block'. A "
30 "single value can be specified to refine all blocks.");
32 "enable_neighbor_refinement",
34 "Toggles whether neighboring level one elements should be refined or not. Defaults to true. "
35 "False may lead to unsupported mesh non-conformality without great care.");
37 "max_element_volume", 1e8,
"If elements are above that size, they will be refined more");
44 _input(getMesh(
"input")),
45 _block(getParam<
std::vector<SubdomainName>>(
"block")),
46 _refinement(getParam<
std::vector<unsigned
int>>(
"refinement")),
47 _enable_neighbor_refinement(getParam<bool>(
"enable_neighbor_refinement")),
48 _max_element_volume(getParam<Real>(
"max_element_volume"))
53 paramError(
"refinement",
"The blocks and refinement parameter vectors should be the same size");
56std::unique_ptr<MeshBase>
60 const auto block_ids =
64 if (!
_input->preparation().has_cached_elem_data)
66 std::set<SubdomainID> mesh_blocks;
67 _input->subdomain_ids(mesh_blocks);
69 for (std::size_t i = 0; i < block_ids.size(); ++i)
73 getParam<std::vector<SubdomainName>>(
"block")[i],
74 "' was not found within the mesh.\nBlock ID: ",
76 "\nBlock IDs in the mesh: ",
79 std::unique_ptr<MeshBase>
mesh = std::move(
_input);
82 if (max > 0 && !
mesh->is_replicated() && !
mesh->is_prepared())
85 mesh->prepare_for_use();
89 if (max > 0 && !mesh_ptr->is_replicated() && !mesh_ptr->is_prepared())
92 mesh_ptr->prepare_for_use();
95 bool found_element_to_refine =
true;
96 bool refined_on_size =
false;
97 while (found_element_to_refine)
99 found_element_to_refine =
false;
100 for (
auto bid : block_ids)
101 for (
auto & elem : mesh_ptr->active_subdomain_elements_ptr_range(bid))
104 elem->set_refinement_flag(Elem::REFINE);
105 found_element_to_refine =
true;
108 mesh_ptr->comm().max(found_element_to_refine);
110 if (found_element_to_refine)
116 refined_on_size =
true;
120 if (refined_on_size || max > 0)
121 mesh_ptr->unset_is_prepared();
126std::unique_ptr<MeshBase>
128 std::unique_ptr<MeshBase> & mesh,
129 std::vector<unsigned int> refinement,
131 unsigned int ref_step)
134 return dynamic_pointer_cast<MeshBase>(
mesh);
135 for (std::size_t i = 0; i < block_ids.size(); i++)
137 if (refinement[i] > 0 && refinement[i] > ref_step)
139 for (
const auto & elem :
mesh->active_subdomain_elements_ptr_range(block_ids[i]))
140 elem->set_refinement_flag(Elem::REFINE);
registerMooseObject("MooseApp", RefineBlockGenerator)
void ErrorVector unsigned int
MeshGenerators are objects that can modify or add to an existing mesh.
static InputParameters validParams()
void paramError(const std::string ¶m, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
MeshGenerator for refining one or more blocks.
virtual std::unique_ptr< MeshBase > recursive_refine(const std::vector< subdomain_id_type > block_ids, std::unique_ptr< MeshBase > &mesh, const std::vector< unsigned int > refinement, const unsigned int max, unsigned int ref_step=0)
The actual function refining the blocks.
virtual std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
const bool _enable_neighbor_refinement
Toggles whether neighboring level one elements should be refined or not. Defaults to true.
RefineBlockGenerator(const InputParameters ¶meters)
static InputParameters validParams()
std::vector< unsigned int > _refinement
The amount of times to refine each block, corresponding to their index in 'block'.
const std::vector< SubdomainName > _block
List of block(s) to refine.
const Real _max_element_volume
maximum element size over which elements are refined
std::unique_ptr< MeshBase > & _input
Input mesh to refine.
unsigned char & face_level_mismatch_limit()
bool hasSubdomainID(const MeshBase &input_mesh, const SubdomainID &id)
Whether a particular subdomain ID exists in the mesh.
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.
std::string stringify(const T &t)
conversion to string