www.mooseframework.org
StitchedMeshGenerator.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 
10 #include "StitchedMeshGenerator.h"
11 
12 #include "CastUniquePointer.h"
13 #include "MooseUtils.h"
14 
15 #include "libmesh/replicated_mesh.h"
16 
18 
19 template <>
22 {
24 
25  MooseEnum algorithm("BINARY EXHAUSTIVE", "BINARY");
26 
27  params.addRequiredParam<std::vector<MeshGeneratorName>>("inputs", "The input MeshGenerators.");
28  params.addParam<bool>(
29  "clear_stitched_boundary_ids", true, "Whether or not to clear the stitched boundary IDs");
30  params.addRequiredParam<std::vector<std::vector<std::string>>>(
31  "stitch_boundaries_pairs",
32  "Pairs of boundaries to be stitched together between the 1st mesh in inputs and each "
33  "consecutive mesh");
34  params.addParam<MooseEnum>(
35  "algorithm",
36  algorithm,
37  "Control the use of binary search for the nodes of the stitched surfaces.");
38 
39  return params;
40 }
41 
43  : MeshGenerator(parameters),
44  _input_names(getParam<std::vector<MeshGeneratorName>>("inputs")),
45  _clear_stitched_boundary_ids(getParam<bool>("clear_stitched_boundary_ids")),
46  _stitch_boundaries_pairs(
47  getParam<std::vector<std::vector<std::string>>>("stitch_boundaries_pairs")),
48  _algorithm(parameters.get<MooseEnum>("algorithm"))
49 {
50  // Grab the input meshes
51  _mesh_ptrs.reserve(_input_names.size());
52  for (auto & input_name : _input_names)
53  _mesh_ptrs.push_back(&getMeshByName(input_name));
54 }
55 
56 std::unique_ptr<MeshBase>
58 {
59  // We put the first mesh in a local pointer
60  std::unique_ptr<ReplicatedMesh> mesh = dynamic_pointer_cast<ReplicatedMesh>(*_mesh_ptrs[0]);
61 
62  // Reserve spaces for the other meshes (no need to store the first one another time)
63  _meshes.reserve(_input_names.size() - 1);
64 
65  // Read in all of the other meshes
66  for (MooseIndex(_input_names) i = 1; i < _input_names.size(); ++i)
67  _meshes.push_back(dynamic_pointer_cast<ReplicatedMesh>(*_mesh_ptrs[i]));
68 
69  // Stitch all the meshes to the first one
70  for (MooseIndex(_meshes) i = 0; i < _meshes.size(); i++)
71  {
72  auto boundary_pair = _stitch_boundaries_pairs[i];
73 
74  boundary_id_type first, second;
75 
76  try
77  {
78  first = MooseUtils::convert<boundary_id_type>(boundary_pair[0], true);
79  }
80  catch (...)
81  {
82  first = mesh->get_boundary_info().get_id_by_name(boundary_pair[0]);
83 
84  if (first == BoundaryInfo::invalid_id)
85  {
86  std::stringstream error;
87 
88  error << "Boundary " << boundary_pair[0] << " doesn't exist on the first mesh in " << name()
89  << "\n";
90  error << "Boundary names that do exist: \n";
91  error << " ID : Name\n";
92 
93  auto & sideset_id_name_map = mesh->get_boundary_info().get_sideset_name_map();
94 
95  for (auto & ss_name_map_pair : sideset_id_name_map)
96  error << " " << ss_name_map_pair.first << " : " << ss_name_map_pair.second << "\n";
97 
98  paramError("stitch_boundaries_pairs", error.str());
99  }
100  }
101 
102  try
103  {
104  second = MooseUtils::convert<boundary_id_type>(boundary_pair[1], true);
105  }
106  catch (...)
107  {
108  second = _meshes[i]->get_boundary_info().get_id_by_name(boundary_pair[1]);
109 
110  if (second == BoundaryInfo::invalid_id)
111  {
112  _meshes[i]->print_info();
113 
114  std::stringstream error;
115 
116  error << "Boundary " << boundary_pair[1] << " doesn't exist on mesh " << i + 1 << " in "
117  << name() << "\n";
118  error << "Boundary names that do exist: \n";
119  error << " ID : Name\n";
120 
121  auto & sideset_id_name_map = _meshes[i]->get_boundary_info().get_sideset_name_map();
122 
123  for (auto & ss_name_map_pair : sideset_id_name_map)
124  error << " " << ss_name_map_pair.first << " : " << ss_name_map_pair.second << "\n";
125 
126  paramError("stitch_boundaries_pairs", error.str());
127  }
128  }
129 
130  const bool use_binary_search = (_algorithm == "BINARY");
131 
132  mesh->stitch_meshes(*_meshes[i],
133  first,
134  second,
135  TOLERANCE,
137  /*verbose = */ true,
138  use_binary_search);
139  }
140 
141  return dynamic_pointer_cast<MeshBase>(mesh);
142 }
std::unique_ptr< MeshBase > generate() override
Generate / modify the 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::unique_ptr< MeshBase > & getMeshByName(const MeshGeneratorName &input_mesh_generator_parameter_name)
Takes the name of another MeshGenerator directly.
Definition: MeshGenerator.C:47
InputParameters validParams< StitchedMeshGenerator >()
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...
std::vector< std::vector< std::string > > _stitch_boundaries_pairs
A transformed version of _stitch_boundaries into a more logical "pairwise" structure.
std::vector< std::unique_ptr< MeshBase > * > _mesh_ptrs
StitchedMeshGenerator(const InputParameters &parameters)
const bool & _clear_stitched_boundary_ids
Whether or not to clear (remove) the stitched boundary IDs.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:31
void paramError(const std::string &param, Args... args)
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
Definition: MooseObject.h:108
const std::vector< MeshGeneratorName > & _input_names
The mesh files to read.
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:59
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an option parameter and a documentation string to the InputParameters object...
std::vector< std::unique_ptr< ReplicatedMesh > > _meshes
The meshes to be stitched together.
InputParameters validParams< MeshGenerator >()
Definition: MeshGenerator.C:16
registerMooseObject("MooseApp", StitchedMeshGenerator)
MeshGenerators are objects that can modify or add to an existing mesh.
Definition: MeshGenerator.h:30
Generates individual elements given a list of nodal positions.
MooseEnum _algorithm
Type of algorithm used to find matching nodes (binary or exhaustive)