www.mooseframework.org
SetupMeshAction.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 "SetupMeshAction.h"
11 #include "MooseApp.h"
12 #include "MooseMesh.h"
13 #include "FileMesh.h"
14 #include "FEProblem.h"
15 #include "ActionWarehouse.h"
16 #include "Factory.h"
17 
18 registerMooseAction("MooseApp", SetupMeshAction, "setup_mesh");
19 registerMooseAction("MooseApp", SetupMeshAction, "set_mesh_base");
20 registerMooseAction("MooseApp", SetupMeshAction, "init_mesh");
21 
22 template <>
25 {
27  params.set<std::string>("type") = "FileMesh";
28 
29  params.addParam<bool>("second_order",
30  false,
31  "Converts a first order mesh to a second order "
32  "mesh. Note: This is NOT needed if you are reading "
33  "an actual first order mesh.");
34 
35  params.addParam<std::vector<SubdomainID>>("block_id", "IDs of the block id/name pairs");
36  params.addParam<std::vector<SubdomainName>>(
37  "block_name", "Names of the block id/name pairs (must correspond with \"block_id\"");
38 
39  params.addParam<std::vector<BoundaryID>>("boundary_id", "IDs of the boundary id/name pairs");
40  params.addParam<std::vector<BoundaryName>>(
41  "boundary_name", "Names of the boundary id/name pairs (must correspond with \"boundary_id\"");
42 
43  params.addParam<bool>("construct_side_list_from_node_list",
44  false,
45  "If true, construct side lists from the nodesets in the mesh (i.e. if "
46  "every node on a give side is in a nodeset then add that side to a "
47  "sideset");
48 
49  params.addParam<std::vector<std::string>>(
50  "displacements",
51  "The variables corresponding to the x y z displacements of the mesh. If "
52  "this is provided then the displacements will be taken into account during "
53  "the computation. Creation of the displaced mesh can be suppressed even if "
54  "this is set by setting 'use_displaced_mesh = false'.");
55  params.addParam<bool>(
56  "use_displaced_mesh",
57  true,
58  "Create the displaced mesh if the 'displacements' "
59  "parameter is set. If this is 'false', a displaced mesh will not be created, "
60  "regardless of whether 'displacements' is set.");
61  params.addParam<std::vector<BoundaryName>>("ghosted_boundaries",
62  "Boundaries to be ghosted if using Nemesis");
63  params.addParam<std::vector<Real>>("ghosted_boundaries_inflation",
64  "If you are using ghosted boundaries you will want to set "
65  "this value to a vector of amounts to inflate the bounding "
66  "boxes by. ie if you are running a 3D problem you might set "
67  "it to '0.2 0.1 0.4'");
68 
69  params.addParam<unsigned int>(
70  "uniform_refine", 0, "Specify the level of uniform refinement applied to the initial mesh");
71  params.addParam<bool>("skip_partitioning",
72  false,
73  "If true the mesh won't be partitioned. This may cause large load "
74  "imbalanced but is currently required if you "
75  "have a simulation containing uniform refinement, adaptivity and stateful "
76  "material properties");
77 
78  // groups
79  params.addParamNamesToGroup("displacements ghosted_boundaries ghosted_boundaries_inflation",
80  "Advanced");
81  params.addParamNamesToGroup("second_order construct_side_list_from_node_list skip_partitioning",
82  "Advanced");
83  params.addParamNamesToGroup("block_id block_name boundary_id boundary_name", "Add Names");
84 
85  return params;
86 }
87 
89 
90 void
92 {
93  std::vector<BoundaryName> ghosted_boundaries =
94  getParam<std::vector<BoundaryName>>("ghosted_boundaries");
95 
96  for (const auto & bnd_name : ghosted_boundaries)
97  mesh->addGhostedBoundary(mesh->getBoundaryID(bnd_name));
98 
99  if (isParamValid("ghosted_boundaries_inflation"))
100  {
101  std::vector<Real> ghosted_boundaries_inflation =
102  getParam<std::vector<Real>>("ghosted_boundaries_inflation");
103  mesh->setGhostedBoundaryInflation(ghosted_boundaries_inflation);
104  }
105 
106  mesh->ghostGhostedBoundaries();
107 
108  if (getParam<bool>("second_order"))
109  mesh->getMesh().all_second_order(true);
110 
111 #ifdef LIBMESH_ENABLE_AMR
112  unsigned int level = getParam<unsigned int>("uniform_refine");
113 
114  // Did they specify extra refinement levels on the command-line?
115  level += _app.getParam<unsigned int>("refinements");
116 
117  mesh->setUniformRefineLevel(level);
118 #endif // LIBMESH_ENABLE_AMR
119 
120  // Add entity names to the mesh
121  if (_pars.isParamValid("block_id") && _pars.isParamValid("block_name"))
122  {
123  std::vector<SubdomainID> ids = getParam<std::vector<SubdomainID>>("block_id");
124  std::vector<SubdomainName> names = getParam<std::vector<SubdomainName>>("block_name");
125  std::set<SubdomainName> seen_it;
126 
127  if (ids.size() != names.size())
128  mooseError("You must supply the same number of block ids and names parameters");
129 
130  for (unsigned int i = 0; i < ids.size(); ++i)
131  {
132  if (seen_it.find(names[i]) != seen_it.end())
133  mooseError("The following dynamic block name is not unique: " + names[i]);
134  seen_it.insert(names[i]);
135  mesh->setSubdomainName(ids[i], names[i]);
136  }
137  }
138  if (_pars.isParamValid("boundary_id") && _pars.isParamValid("boundary_name"))
139  {
140  std::vector<BoundaryID> ids = getParam<std::vector<BoundaryID>>("boundary_id");
141  std::vector<BoundaryName> names = getParam<std::vector<BoundaryName>>("boundary_name");
142  std::set<SubdomainName> seen_it;
143 
144  if (ids.size() != names.size())
145  mooseError("You must supply the same number of boundary ids and names parameters");
146 
147  for (unsigned int i = 0; i < ids.size(); ++i)
148  {
149  if (seen_it.find(names[i]) != seen_it.end())
150  mooseError("The following dynamic boundary name is not unique: " + names[i]);
151  mesh->setBoundaryName(ids[i], names[i]);
152  seen_it.insert(names[i]);
153  }
154  }
155 
156  if (getParam<bool>("construct_side_list_from_node_list"))
157  mesh->getMesh().get_boundary_info().build_side_list_from_node_list();
158 
159  // Here we can override the partitioning for special cases
160  if (getParam<bool>("skip_partitioning"))
161  mesh->getMesh().skip_partitioning(getParam<bool>("skip_partitioning"));
162 }
163 
164 void
166 {
167  // Create the mesh object and tell it to build itself
168  if (_current_task == "setup_mesh")
169  {
170  // switch non-file meshes to be a file-mesh if using a pre-split mesh configuration.
171  if (_app.isUseSplit())
172  {
173  auto split_file = _app.parameters().get<std::string>("split_file");
174 
175  // Get the split_file extension, if there is one, and use that to decide
176  // between .cpr and .cpa
177  std::string split_file_ext;
178  auto pos = split_file.rfind(".");
179  if (pos != std::string::npos)
180  split_file_ext = split_file.substr(pos + 1, std::string::npos);
181 
182  // If split_file already has the .cpr or .cpa extension, we go with
183  // that, otherwise we strip off the extension and append ".cpr".
184  if (split_file != "" && split_file_ext != "cpr" && split_file_ext != "cpa")
185  split_file = MooseUtils::stripExtension(split_file) + ".cpr";
186 
187  if (_type != "FileMesh")
188  {
189  if (split_file == "")
190  {
191  if (_app.processor_id() == 0)
192  mooseError(
193  "Cannot use split mesh for a non-file mesh without specifying --split-file on "
194  "command line");
195  }
196 
197  _type = "FileMesh";
198  auto new_pars = validParams<FileMesh>();
199 
200  // Keep existing parameters where possible
202 
203  new_pars.set<MeshFileName>("file") = split_file;
204  new_pars.set<MooseApp *>("_moose_app") = _moose_object_pars.get<MooseApp *>("_moose_app");
205  _moose_object_pars = new_pars;
206  }
207  else
208  {
209  if (split_file != "")
210  _moose_object_pars.set<MeshFileName>("file") = split_file;
211  else
212  _moose_object_pars.set<MeshFileName>("file") =
213  MooseUtils::stripExtension(_moose_object_pars.get<MeshFileName>("file")) + ".cpr";
214  }
215  }
216 
218  }
219 
220  else if (_current_task == "set_mesh_base")
221  _mesh->setMeshBase(_mesh->buildMeshBaseObject());
222 
223  else if (_current_task == "init_mesh")
224  {
225  _mesh->init();
226 
227  if (isParamValid("displacements") && getParam<bool>("use_displaced_mesh"))
228  {
229  _displaced_mesh = _mesh->safeClone();
230  _displaced_mesh->getMesh().allow_remote_element_removal(
231  _mesh->getMesh().allow_remote_element_removal());
232 
233  std::vector<std::string> displacements = getParam<std::vector<std::string>>("displacements");
234  if (displacements.size() < _displaced_mesh->dimension())
235  mooseError(
236  "Number of displacements must be greater than or equal to the dimension of the mesh!");
237  }
238 
239  setupMesh(_mesh.get());
240 
241  if (_displaced_mesh)
242  setupMesh(_displaced_mesh.get());
243  }
244 }
SetupMeshAction(InputParameters params)
InputParameters validParams< SetupMeshAction >()
bool isParamValid(const std::string &name) const
Definition: Action.h:115
std::shared_ptr< MooseObject > create(const std::string &obj_name, const std::string &name, InputParameters parameters, THREAD_ID tid=0, bool print_deprecated=true)
Build an object (must be registered) - THIS METHOD IS DEPRECATED (Use create<T>()) ...
Definition: Factory.C:87
virtual void act() override
Method to add objects to the simulation or perform other setup tasks.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
InputParameters & parameters()
Get the parameters of the object.
Definition: MooseApp.h:86
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
registerMooseAction("MooseApp", SetupMeshAction, "setup_mesh")
Base class for MOOSE-based applications.
Definition: MooseApp.h:59
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
std::string stripExtension(const std::string &s)
Removes any file extension from the fiven string s (i.e.
Definition: MooseUtils.C:280
Factory & _factory
The Factory associated with the MooseApp.
Definition: Action.h:186
InputParameters _pars
Input parameters for the action.
Definition: Action.h:171
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:2567
void setSubdomainName(SubdomainID subdomain_id, SubdomainName name)
This method returns a writable reference to a subdomain name based on the id parameter.
Definition: MooseMesh.C:1118
std::shared_ptr< MooseMesh > & _displaced_mesh
Definition: Action.h:213
bool isUseSplit() const
Whether or not we are running with pre-split (distributed mesh)
Definition: MooseApp.C:877
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:74
const std::string & _current_task
The current action (even though we have seperate instances for each action)
Definition: Action.h:210
InputParameters validParams< MooseObjectAction >()
void applyParameters(const InputParameters &common, std::vector< std::string > exclude=std::vector< std::string >())
Method for applying common parameters.
void setupMesh(MooseMesh *mesh)
void setBoundaryName(BoundaryID boundary_id, BoundaryName name)
This method returns a writable reference to a boundary name based on the id parameter.
Definition: MooseMesh.C:1130
const T & getParam(const std::string &name)
Retrieve a parameter for the object.
Definition: MooseApp.h:935
std::shared_ptr< MooseMesh > & _mesh
Definition: Action.h:212
std::string _type
The Object type that is being created.
InputParameters validParams< FileMesh >()
Definition: FileMesh.C:24
InputParameters _moose_object_pars
The parameters for the object to be created.
void setGhostedBoundaryInflation(const std::vector< Real > &inflation)
This sets the inflation amount for the bounding box for each partition for use in ghosting boundaries...
Definition: MooseMesh.C:2379
processor_id_type processor_id()
Definition: MooseApp.h:288
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...
void addGhostedBoundary(BoundaryID boundary_id)
This will add the boundary ids to be ghosted to this processor.
Definition: MooseMesh.C:2373
void ghostGhostedBoundaries()
Actually do the ghosting of boundaries that need to be ghosted to this processor. ...
Definition: MooseMesh.C:2455
MooseApp & _app
The MOOSE application this is associated with.
Definition: Action.h:183
BoundaryID getBoundaryID(const BoundaryName &boundary_name) const
Get the associated BoundaryID for the boundary name.
Definition: MooseMesh.C:1007
void setUniformRefineLevel(unsigned int)
Set uniform refinement level.
Definition: MooseMesh.C:2367
void addParamNamesToGroup(const std::string &space_delim_names, const std::string group_name)
This method takes a space delimited list of parameter names and adds them to the specified group name...
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.