12 #include "libmesh/elem.h" 23 "Converts one or more blocks (subdomains) from a mesh into a stand-alone mesh with a " 24 "single block in it.");
26 params.
addRequiredParam<MeshGeneratorName>(
"input",
"The mesh we want to modify");
29 "The (list of) blocks (or 'subdomains') we wish to have moved to a new mesh (by name, not " 37 _input(getMesh(
"input")),
38 _target_blocks(getParam<
std::vector<SubdomainName>>(
"target_blocks"))
42 std::unique_ptr<MeshBase>
45 std::unique_ptr<MeshBase>
mesh = std::move(
_input);
47 if (!
mesh->is_replicated())
48 mooseError(
"This generator does not support distributed meshes.");
55 std::set<SubdomainID> mesh_blocks;
56 mesh->subdomain_ids(mesh_blocks);
64 "' was not found within the mesh.");
68 std::unordered_map<dof_id_type, dof_id_type> old_new_node_map;
70 for (
const auto target_block_id : target_block_ids)
73 for (
auto elem :
mesh->active_subdomain_elements_ptr_range(target_block_id))
75 if (elem->level() != 0)
76 mooseError(
"Refined blocks are not supported by this generator. " 77 "Can you re-organize mesh generators to refine after converting the block?");
80 auto copy = elem->build(elem->type());
89 auto & n = elem->node_ref(i);
91 if (old_new_node_map.count(n.id()))
96 copy->set_node(copy_n_index++, new_mesh->node_ptr(old_new_node_map[n.id()]));
106 Node * node = new_mesh->add_point(elem->point(i));
109 copy->set_node(copy_n_index++, node);
112 old_new_node_map[n.id()] = node->id();
118 new_mesh->add_elem(copy.release());
BlockToMeshConverterGenerator(const InputParameters ¶meters)
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 ...
std::unique_ptr< T_DEST, T_DELETER > dynamic_pointer_cast(std::unique_ptr< T_SRC, T_DELETER > &src)
These are reworked from https://stackoverflow.com/a/11003103.
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::unique_ptr< MeshBase > & _input
Mesh that comes from another generator.
Creates a new mesh out of one or more subdomains/blocks from another mesh.
const SubdomainID INVALID_BLOCK_ID
const std::vector< SubdomainName > _target_blocks
Blocks to extract to form a mesh.
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
static InputParameters validParams()
registerMooseObject("MooseApp", BlockToMeshConverterGenerator)
static InputParameters validParams()
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
std::unique_ptr< MeshBase > buildMeshBaseObject(unsigned int dim=libMesh::invalid_uint)
Build a MeshBase object whose underlying type will be determined by the Mesh input file block...
MeshGenerators are objects that can modify or add to an existing mesh.
auto index_range(const T &sizable)