14 #include "libmesh/elem.h" 15 #include "libmesh/partitioner.h" 23 params.
addRequiredParam<MeshGeneratorName>(
"input",
"The mesh we want to modify");
25 "Changes the subdomain ID of elements based on the output of the partitioner");
26 params.
addParam<std::vector<SubdomainName>>(
27 "included_subdomains",
"Only change subdomain ID only for elements in those subdomains");
30 "Offset to apply to the subdomain IDs. Default type is a " 31 "short integer, do not use a large value!");
33 "Number of partitioners to get from the partitioner");
40 _input(getMesh(
"input")),
45 std::unique_ptr<MeshBase>
48 std::unique_ptr<MeshBase>
mesh = std::move(
_input);
50 if (getParam<unsigned int>(
"num_partitions") !=
n_processors())
52 "Partitioner may error with num_partitions != number of MPI processors");
55 std::set<SubdomainID> restricted_ids;
56 bool has_restriction = getParam<std::vector<SubdomainName>>(
"included_subdomains").size();
59 const auto & names = getParam<std::vector<SubdomainName>>(
"included_subdomains");
60 for (
const auto &
name : names)
64 paramError(
"included_subdomains",
"The block '",
name,
"' was not found in the mesh");
74 *
mesh, *block_mesh,
getParam<std::vector<SubdomainName>>(
"included_subdomains"));
76 block_mesh =
mesh->clone();
79 const auto & partitioner =
mesh->partitioner();
82 partitioner->partition(*block_mesh, getParam<unsigned int>(
"num_partitions"));
83 auto pl = block_mesh->sub_point_locator();
86 for (
const auto & elem :
mesh->element_ptr_range())
88 if (has_restriction && restricted_ids.count(elem->subdomain_id()) == 0)
92 const Elem *
const block_mesh_elem = (*pl)(elem->vertex_average());
95 elem->subdomain_id() =
_offset + block_mesh_elem->processor_id();
98 mesh->unset_is_prepared();
static InputParameters validParams()
MeshGenerator for defining subdomains from the output of the partitioner.
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.
void convertBlockToMesh(MeshBase &source_mesh, MeshBase &target_mesh, const std::vector< SubdomainName > &target_blocks)
Convert a list of blocks in a given mesh to a standalone new mesh.
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.
registerMooseObject("MooseApp", SubdomainsFromPartitionerGenerator)
SubdomainID getSubdomainID(const SubdomainName &subdomain_name, const MeshBase &mesh)
Gets the subdomain ID associated with the given SubdomainName.
processor_id_type n_processors() const
const std::string & name() const
Get the name of the class.
const subdomain_id_type _offset
Offset to start numbering subdomains with.
std::unique_ptr< MeshBase > & _input
static InputParameters validParams()
bool hasSubdomainName(const MeshBase &input_mesh, const SubdomainName &name)
Whether a particular subdomain name exists in the mesh.
SubdomainsFromPartitionerGenerator(const InputParameters ¶meters)
void paramWarning(const std::string ¶m, Args... args) const
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...
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
MeshGenerators are objects that can modify or add to an existing mesh.