https://mooseframework.inl.gov
SubdomainsFromPartitionerGenerator.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 "CastUniquePointer.h"
12 #include "MooseMeshUtils.h"
13 
14 #include "libmesh/elem.h"
15 #include "libmesh/partitioner.h"
16 
18 
21 {
23  params.addRequiredParam<MeshGeneratorName>("input", "The mesh we want to modify");
24  params.addClassDescription(
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");
28  params.addParam<subdomain_id_type>("offset",
29  0,
30  "Offset to apply to the subdomain IDs. Default type is a "
31  "short integer, do not use a large value!");
32  params.addRequiredParam<unsigned int>("num_partitions",
33  "Number of partitioners to get from the partitioner");
34  return params;
35 }
36 
38  const InputParameters & parameters)
39  : MeshGenerator(parameters),
40  _input(getMesh("input")),
41  _offset(getParam<subdomain_id_type>("offset"))
42 {
43 }
44 
45 std::unique_ptr<MeshBase>
47 {
48  std::unique_ptr<MeshBase> mesh = std::move(_input);
49 
50  if (getParam<unsigned int>("num_partitions") != n_processors())
51  paramWarning("num_partitions",
52  "Partitioner may error with num_partitions != number of MPI processors");
53 
54  // Process the block restriction, if any
55  std::set<SubdomainID> restricted_ids;
56  bool has_restriction = getParam<std::vector<SubdomainName>>("included_subdomains").size();
57  if (has_restriction)
58  {
59  const auto & names = getParam<std::vector<SubdomainName>>("included_subdomains");
60  for (const auto & name : names)
61  {
62  // check that the subdomain exists in the mesh
64  paramError("included_subdomains", "The block '", name, "' was not found in the mesh");
65 
66  restricted_ids.insert(MooseMeshUtils::getSubdomainID(name, *mesh));
67  }
68  }
69 
70  // Extract the mesh
71  auto block_mesh = buildMeshBaseObject();
72  if (has_restriction)
74  *mesh, *block_mesh, getParam<std::vector<SubdomainName>>("included_subdomains"));
75  else
76  block_mesh = mesh->clone();
77 
78  // Get the partitioner
79  const auto & partitioner = mesh->partitioner();
80 
81  // Partition this mesh
82  partitioner->partition(*block_mesh, getParam<unsigned int>("num_partitions"));
83  auto pl = block_mesh->sub_point_locator();
84 
85  // Loop over the elements
86  for (const auto & elem : mesh->element_ptr_range())
87  {
88  if (has_restriction && restricted_ids.count(elem->subdomain_id()) == 0)
89  continue;
90 
91  // Get the element from point locator
92  const Elem * const block_mesh_elem = (*pl)(elem->vertex_average());
93 
94  // Get a new subdomain id from the partition of the block mesh
95  elem->subdomain_id() = _offset + block_mesh_elem->processor_id();
96  }
97 
98  mesh->unset_is_prepared();
99  return dynamic_pointer_cast<MeshBase>(mesh);
100 }
MeshGenerator for defining subdomains from the output of the partitioner.
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
Definition: MooseBase.h:467
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition: MooseBase.h:416
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.
MeshBase & mesh
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
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)
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...
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.
Definition: MooseBase.h:103
const subdomain_id_type _offset
Offset to start numbering subdomains with.
static InputParameters validParams()
Definition: MeshGenerator.C:23
bool hasSubdomainName(const MeshBase &input_mesh, const SubdomainName &name)
Whether a particular subdomain name exists in the mesh.
SubdomainsFromPartitionerGenerator(const InputParameters &parameters)
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...
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 paramWarning(const std::string &param, 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.
Definition: MeshGenerator.h:33