https://mooseframework.inl.gov
BlockToMeshConverterGenerator.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 "libmesh/elem.h"
13 #include "MooseMeshUtils.h"
14 
16 
19 {
21 
22  params.addClassDescription(
23  "Converts one or more blocks (subdomains) from a mesh into a stand-alone mesh with a "
24  "single block in it.");
25 
26  params.addRequiredParam<MeshGeneratorName>("input", "The mesh we want to modify");
27  params.addRequiredParam<std::vector<SubdomainName>>(
28  "target_blocks",
29  "The (list of) blocks (or 'subdomains') we wish to have moved to a new mesh (by name, not "
30  "ID)");
31 
32  return params;
33 }
34 
36  : MeshGenerator(parameters),
37  _input(getMesh("input")),
38  _target_blocks(getParam<std::vector<SubdomainName>>("target_blocks"))
39 {
40 }
41 
42 std::unique_ptr<MeshBase>
44 {
45  std::unique_ptr<MeshBase> mesh = std::move(_input);
46 
47  if (!mesh->is_replicated())
48  mooseError("This generator does not support distributed meshes.");
49 
50  auto new_mesh = buildMeshBaseObject();
51 
52  const auto target_block_ids = MooseMeshUtils::getSubdomainIDs((*mesh), _target_blocks);
53 
54  // Check that the block ids/names exist in the mesh
55  std::set<SubdomainID> mesh_blocks;
56  mesh->subdomain_ids(mesh_blocks);
57 
58  for (const auto i : index_range(target_block_ids))
59  if (target_block_ids[i] == Moose::INVALID_BLOCK_ID || !mesh_blocks.count(target_block_ids[i]))
60  {
61  paramError("target_blocks",
62  "The target_block '",
63  _target_blocks[i],
64  "' was not found within the mesh.");
65  }
66 
67  // know which nodes have already been inserted, by tracking the old mesh's node's ids'
68  std::unordered_map<dof_id_type, dof_id_type> old_new_node_map;
69 
70  for (const auto target_block_id : target_block_ids)
71  {
72 
73  for (auto elem : mesh->active_subdomain_elements_ptr_range(target_block_id))
74  {
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?");
78 
79  // make a deep copy so that mutiple meshes' destructors don't segfault at program termination
80  auto copy = elem->build(elem->type());
81 
82  // index of node in the copy element must be managed manually as there is no intelligent
83  // insert method
84  dof_id_type copy_n_index = 0;
85 
86  // correctly assign new copies of nodes, loop over nodes
87  for (dof_id_type i : elem->node_index_range())
88  {
89  auto & n = elem->node_ref(i);
90 
91  if (old_new_node_map.count(n.id()))
92  {
93  // case where we have already inserted this particular point before
94  // then we need to find the already-inserted one and hook it up right
95  // to it's respective element
96  copy->set_node(copy_n_index++, new_mesh->node_ptr(old_new_node_map[n.id()]));
97  }
98  else
99  {
100  // case where we've NEVER inserted this particular point before
101  // add them both to the element and the mesh
102 
103  // Nodes' IDs are their indexes in the nodes' respective mesh
104  // If we set them as invalid they are automatically assigned
105  // Add to mesh, auto-assigning a new id.
106  Node * node = new_mesh->add_point(elem->point(i));
107 
108  // Add to element copy (manually)
109  copy->set_node(copy_n_index++, node);
110 
111  // remember the (old) ID
112  old_new_node_map[n.id()] = node->id();
113  }
114  }
115 
116  // it is ok to release the copy element into the mesh because derived meshes class
117  // (ReplicatedMesh, DistributedMesh) manage their own elements, will delete them
118  new_mesh->add_elem(copy.release());
119  }
120  }
121 
122  return dynamic_pointer_cast<MeshBase>(new_mesh);
123 }
BlockToMeshConverterGenerator(const InputParameters &parameters)
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:435
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.
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.
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...
Creates a new mesh out of one or more subdomains/blocks from another mesh.
const SubdomainID INVALID_BLOCK_ID
Definition: MooseTypes.C:20
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()
Definition: MeshGenerator.C:23
registerMooseObject("MooseApp", BlockToMeshConverterGenerator)
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...
Definition: MooseBase.h:267
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...
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.
Definition: MeshGenerator.h:32
auto index_range(const T &sizable)
uint8_t dof_id_type