https://mooseframework.inl.gov
MeshGenerator.h
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 
10 #pragma once
11 
12 #include "MooseObject.h"
13 #include "MeshMetaDataInterface.h"
14 #include "MooseApp.h"
15 
16 // Included so mesh generators don't need to include this when constructing MeshBase objects
17 #include "MooseMesh.h"
18 
19 #include "libmesh/mesh_base.h"
20 #include "libmesh/parameters.h"
21 
22 class MooseMesh;
23 namespace libMesh
24 {
25 class ReplicatedMesh;
26 class DistributedMesh;
27 }
28 
33 {
34 public:
38  struct Comparator
39  {
40  bool operator()(const MeshGenerator * const & a, const MeshGenerator * const & b) const
41  {
42  return a->name() < b->name();
43  }
44  };
45 
47 
54  static void setHasGenerateData(InputParameters & params);
59  static bool hasGenerateData(const InputParameters & params);
60 
62  static const std::string data_only_param;
63 
65 
69  virtual std::unique_ptr<MeshBase> generate() = 0;
70 
75  [[nodiscard]] std::unique_ptr<MeshBase> generateInternal();
76 
80  const std::set<MeshGeneratorName> & getRequestedMeshGenerators() const
81  {
83  }
88  const std::set<MeshGeneratorName> & getRequestedMeshGeneratorsForSub() const
89  {
91  }
92 
98  {
99  friend class MeshGeneratorSystem;
102  };
103 
110  void addParentMeshGenerator(const MeshGenerator & mg, const AddParentChildKey);
117  void addChildMeshGenerator(const MeshGenerator & mg, const AddParentChildKey);
118 
122  const std::set<const MeshGenerator *, Comparator> & getParentMeshGenerators() const
123  {
125  }
129  const std::set<const MeshGenerator *, Comparator> & getChildMeshGenerators() const
130  {
131  return _child_mesh_generators;
132  }
136  const std::set<const MeshGenerator *, Comparator> & getSubMeshGenerators() const
137  {
138  return _sub_mesh_generators;
139  }
140 
148  bool isParentMeshGenerator(const MeshGeneratorName & name, const bool direct = true) const;
149 
157  bool isChildMeshGenerator(const MeshGeneratorName & name, const bool direct = true) const;
158 
165  bool isNullMeshName(const MeshGeneratorName & name) const { return _null_mesh_names.count(name); }
166 
170  bool hasSaveMesh() const;
171 
175  bool hasOutput() const;
176 
180  const std::string & getSavedMeshName() const;
181 
185  bool hasGenerateData() const { return hasGenerateData(_pars); }
186 
190  bool isDataOnly() const { return _data_only; }
191 
192 protected:
196  virtual void generateData();
197 
202  template <typename T, typename... Args>
204  T & declareMeshProperty(const std::string & data_name, Args &&... args);
205  template <typename T>
206  T & declareMeshProperty(const std::string & data_name, const T & data_value)
207  {
208  return declareMeshProperty<T, const T &>(data_name, data_value);
209  }
211 
217  template <typename T, typename... Args>
219  T & setMeshProperty(const std::string & data_name, Args &&... args);
220  template <typename T>
221  T & setMeshProperty(const std::string & data_name, const T & data_value)
222  {
223  return setMeshProperty<T, const T &>(data_name, data_value);
224  }
226 
232  template <typename T>
233  T & copyMeshProperty(const std::string & target_data_name,
234  const std::string & source_data_name,
235  const std::string & source_mesh)
236  {
237  return declareMeshProperty(target_data_name, getMeshProperty<T>(source_data_name, source_mesh));
238  }
239 
245  template <typename T>
246  T & copyMeshProperty(const std::string & source_data_name, const std::string & source_mesh)
247  {
248  return copyMeshProperty<T>(source_data_name, source_data_name, source_mesh);
249  }
250 
265  [[nodiscard]] std::unique_ptr<MeshBase> & getMesh(const std::string & param_name,
266  const bool allow_invalid = false);
272  [[nodiscard]] std::vector<std::unique_ptr<MeshBase> *> getMeshes(const std::string & param_name);
280  [[nodiscard]] std::unique_ptr<MeshBase> &
281  getMeshByName(const MeshGeneratorName & mesh_generator_name);
287  [[nodiscard]] std::vector<std::unique_ptr<MeshBase> *>
288  getMeshesByName(const std::vector<MeshGeneratorName> & mesh_generator_names);
289 
300  void declareMeshForSub(const std::string & param_name);
304  void declareMeshesForSub(const std::string & param_name);
308  void declareMeshForSubByName(const MeshGeneratorName & mesh_generator_name);
312  void declareMeshesForSubByName(const std::vector<MeshGeneratorName> & mesh_generator_names);
313 
321  [[nodiscard]] std::unique_ptr<MeshBase>
323 
330  [[nodiscard]] std::unique_ptr<ReplicatedMesh>
332 
340  [[nodiscard]] std::unique_ptr<DistributedMesh>
342 
361  template <typename... Ts>
362  void addMeshSubgenerator(const std::string & type,
363  const std::string & name,
364  Ts... extra_input_parameters);
365 
374  void
375  addMeshSubgenerator(const std::string & type, const std::string & name, InputParameters params);
376 
385  void declareNullMeshName(const MeshGeneratorName & name);
386 
388  MooseMesh * const _mesh;
389 
390 private:
397  virtual std::string meshPropertyPrefix(const std::string &) const override final
398  {
399  return name();
400  }
401 
405  void checkGetMesh(const MeshGeneratorName & mesh_generator_name,
406  const std::string & param_name) const;
407 
411  const MeshGeneratorName * getMeshGeneratorNameFromParam(const std::string & param_name,
412  const bool allow_invalid) const;
416  const std::vector<MeshGeneratorName> &
417  getMeshGeneratorNamesFromParam(const std::string & param_name) const;
418 
423  RestartableDataValue & setMeshPropertyHelper(const std::string & data_name);
424 
426  std::set<MeshGeneratorName> _requested_mesh_generators;
428  std::set<MeshGeneratorName> _requested_mesh_generators_for_sub;
431  std::vector<std::pair<std::string, std::unique_ptr<MeshBase> *>> _requested_meshes;
432 
434  std::unique_ptr<MeshBase> _null_mesh = nullptr;
435 
437  std::set<const MeshGenerator *, Comparator> _parent_mesh_generators;
439  std::set<const MeshGenerator *, Comparator> _child_mesh_generators;
441  std::set<const MeshGenerator *, Comparator> _sub_mesh_generators;
442 
444  std::set<std::string> _null_mesh_names;
445 
447  const std::string & _save_with_name;
448 
450  const bool _data_only;
451 };
452 
453 template <typename T, typename... Args>
454 T &
455 MeshGenerator::declareMeshProperty(const std::string & data_name, Args &&... args)
456 {
458  mooseError("Can only call declareMeshProperty() during MeshGenerator construction");
459 
460  // We sort construction ordering so that we _must_ declare before retrieving
461  if (hasMeshProperty(data_name))
462  mooseError("While declaring mesh property '",
463  data_name,
464  "' with type '",
465  MooseUtils::prettyCppType<T>(),
466  "',\nsaid property has already been declared with type '",
467  setMeshPropertyHelper(data_name).type(),
468  "'");
469 
470  const auto full_name = meshPropertyName(data_name);
471  auto new_T_value =
472  std::make_unique<RestartableData<T>>(full_name, nullptr, std::forward<Args>(args)...);
473  auto value =
474  &_app.registerRestartableData(std::move(new_T_value), 0, false, MooseApp::MESH_META_DATA);
475  mooseAssert(value->declared(), "Should be declared");
476 
477  RestartableData<T> * T_value = dynamic_cast<RestartableData<T> *>(value);
478  mooseAssert(T_value, "Bad cast");
479 
480  return T_value->set();
481 }
482 
483 template <typename T, typename... Args>
484 T &
485 MeshGenerator::setMeshProperty(const std::string & data_name, Args &&... args)
486 {
487  if (_app.actionWarehouse().getCurrentTaskName() != "execute_mesh_generators")
488  mooseError("Updating mesh meta data with setMeshProperty() can only be called during "
489  "MeshGenerator::generate()");
490 
491  if (!hasMeshProperty(data_name))
492  mooseError("Failed to get the mesh property '", data_name, "'");
494  RestartableData<T> * T_value = dynamic_cast<RestartableData<T> *>(value);
495  if (!T_value)
496  mooseError("While retrieving mesh property '",
497  data_name,
498  "' with type '",
499  MooseUtils::prettyCppType<T>(),
500  "',\nthe property was found with type '",
501  value->type(),
502  "'");
503 
504  // Set the value if someone provided arguments to set it to
505  if constexpr (sizeof...(args) > 0)
506  T_value->set() = T(std::forward<Args>(args)...);
507 
508  return T_value->set();
509 }
510 
511 template <typename... Ts>
512 void
513 MeshGenerator::addMeshSubgenerator(const std::string & type,
514  const std::string & name,
515  Ts... extra_input_parameters)
516 {
517  InputParameters subgenerator_params = _app.getFactory().getValidParams(type);
518 
519  subgenerator_params.setParameters(extra_input_parameters...);
520 
521  addMeshSubgenerator(type, name, subgenerator_params);
522 }
virtual bool constructingMeshGenerators() const
Whether this app is constructing mesh generators.
Definition: MooseApp.C:3492
std::unique_ptr< DistributedMesh > buildDistributedMesh(unsigned int dim=libMesh::invalid_uint)
Build a distributed mesh that has correct remote element removal behavior and geometric ghosting func...
std::unique_ptr< MeshBase > & getMeshByName(const MeshGeneratorName &mesh_generator_name)
Like getMesh(), but takes the name of another MeshGenerator directly.
std::set< std::string > _null_mesh_names
The declared "null" mesh names that will not represent a mesh in input; see declareNullMeshName() ...
RestartableDataValue & setMeshPropertyHelper(const std::string &data_name)
Helper for getting a writable reference to a mesh property, used in declareMeshProperty and setMeshPr...
static const std::string data_only_param
The name of the private parameter for setting data only.
Definition: MeshGenerator.h:62
bool isChildMeshGenerator(const MeshGeneratorName &name, const bool direct=true) const
T & setMeshProperty(const std::string &data_name, Args &&... args)
Method for updating attributes to the mesh meta-data store, which can only be invoked in the MeshGene...
const unsigned int invalid_uint
T & copyMeshProperty(const std::string &source_data_name, const std::string &source_mesh)
Method for copying attribute from input mesh meta-data store to current mesh meta-data store...
std::unique_ptr< ReplicatedMesh > buildReplicatedMesh(unsigned int dim=libMesh::invalid_uint)
Build a replicated mesh.
Comparator for MeshGenerators that sorts by name.
Definition: MeshGenerator.h:38
const bool _data_only
Whether or not this mesh generator will run in data only mode.
const std::string & _save_with_name
A user-defined name to save the mesh.
bool hasSaveMesh() const
Return whether or not to save the current mesh.
static const RestartableDataMapName MESH_META_DATA
Definition: MooseApp.h:123
virtual void generateData()
Generate the mesh data.
InputParameters getValidParams(const std::string &name) const
Get valid parameters for the object.
Definition: Factory.C:68
static std::string meshPropertyName(const std::string &data_name, const std::string &prefix)
bool hasOutput() const
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
static constexpr std::size_t dim
This is the dimension of all vector and tensor datastructures used in MOOSE.
Definition: Moose.h:153
const std::set< const MeshGenerator *, Comparator > & getChildMeshGenerators() const
Gets the MeshGenerators that are children to this MeshGenerator.
std::unique_ptr< MeshBase > _null_mesh
A nullptr to use for when inputs aren&#39;t specified.
T & setMeshProperty(const std::string &data_name, const T &data_value)
MeshGenerator(const InputParameters &parameters)
Definition: MeshGenerator.C:56
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
bool hasGenerateData() const
virtual const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:57
std::set< const MeshGenerator *, Comparator > _child_mesh_generators
The MeshGenerators that are children to this MeshGenerator.
Factory & getFactory()
Retrieve a writable reference to the Factory associated with this App.
Definition: MooseApp.h:424
bool isNullMeshName(const MeshGeneratorName &name) const
MooseMesh *const _mesh
Pointer to the owning mesh.
void addChildMeshGenerator(const MeshGenerator &mg, const AddParentChildKey)
Adds the MeshGenerator mg as a child.
void declareMeshForSub(const std::string &param_name)
Declares that a MeshGenerator referenced in the InputParameters is to be used as a dependency of a su...
std::vector< std::pair< std::string, std::unique_ptr< MeshBase > * > > _requested_meshes
The meshes that were requested by this MeshGenerator; used to verify that any input meshes that are r...
void addParentMeshGenerator(const MeshGenerator &mg, const AddParentChildKey)
Adds the MeshGenerator mg as a parent.
bool isParentMeshGenerator(const MeshGeneratorName &name, const bool direct=true) const
const std::set< MeshGeneratorName > & getRequestedMeshGeneratorsForSub() const
Definition: MeshGenerator.h:88
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
void addMeshSubgenerator(const std::string &type, const std::string &name, Ts... extra_input_parameters)
Construct a "subgenerator", a different MeshGenerator subclass that will be added to the same MooseAp...
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:28
virtual std::string meshPropertyPrefix(const std::string &) const override final
Override of the default prefix to use when getting mesh properties.
void declareMeshesForSubByName(const std::vector< MeshGeneratorName > &mesh_generator_names)
Like declareMeshForSubByName(), but for multiple generators.
RestartableDataValue & registerRestartableData(std::unique_ptr< RestartableDataValue > data, THREAD_ID tid, bool read_only, const RestartableDataMapName &metaname="")
Definition: MooseApp.C:2447
std::set< MeshGeneratorName > _requested_mesh_generators_for_sub
The names of the MeshGenerators that were requested in the declareMeshForSub methods.
std::unique_ptr< MeshBase > & getMesh(const std::string &param_name, const bool allow_invalid=false)
Takes the name of a MeshGeneratorName parameter and then gets a pointer to the Mesh that MeshGenerato...
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:88
virtual std::unique_ptr< MeshBase > generate()=0
Generate / modify the mesh.
const std::string & type() const
Get the type of this class.
Definition: MooseBase.h:51
std::set< MeshGeneratorName > _requested_mesh_generators
The names of the MeshGenerators that were requested in the getMesh methods.
ActionWarehouse & actionWarehouse()
Return a writable reference to the ActionWarehouse associated with this app.
Definition: MooseApp.h:237
std::vector< std::unique_ptr< MeshBase > * > getMeshesByName(const std::vector< MeshGeneratorName > &mesh_generator_names)
Like getMeshByName(), but for multiple generators.
std::vector< std::unique_ptr< MeshBase > * > getMeshes(const std::string &param_name)
Like getMesh(), but for multiple generators.
MooseApp & _app
The MOOSE application this is associated with.
Definition: MooseBase.h:84
static InputParameters validParams()
Definition: MeshGenerator.C:23
std::unique_ptr< MeshBase > generateInternal()
Internal generation method - this is what is actually called within MooseApp to execute the MeshGener...
virtual std::string type() const =0
String identifying the type of parameter stored.
const std::string & getCurrentTaskName() const
std::set< const MeshGenerator *, Comparator > _sub_mesh_generators
The sub MeshGenerators constructed by this MeshGenerator.
const std::set< const MeshGenerator *, Comparator > & getSubMeshGenerators() const
Gets the MeshGenerators that are children to this MeshGenerator.
Concrete definition of a parameter value for a specified type.
Class that is used as a parameter to add[Parent/Child]() that allows only MeshGeneratorSystem methods...
Definition: MeshGenerator.h:97
const std::set< const MeshGenerator *, Comparator > & getParentMeshGenerators() const
Gets the MeshGenerators that are parents to this MeshGenerator.
void checkGetMesh(const MeshGeneratorName &mesh_generator_name, const std::string &param_name) const
Helper for performing error checking in the getMesh methods.
std::set< const MeshGenerator *, Comparator > _parent_mesh_generators
The MeshGenerators that are parents to this MeshGenerator.
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
const InputParameters & _pars
Parameters of this object, references the InputParameters stored in the InputParametersWarehouse.
static void setHasGenerateData(InputParameters &params)
Sets that a mesh generator has a generateData() implementation.
Definition: MeshGenerator.C:80
const InputParameters & parameters() const
Get the parameters of the object.
T & declareMeshProperty(const std::string &data_name, Args &&... args)
Methods for writing out attributes to the mesh meta-data store, which can be retrieved from most othe...
T & declareMeshProperty(const std::string &data_name, const T &data_value)
The Interface used to retrieve mesh meta data (attributes) set by the MeshGenerator system...
System that manages MeshGenerators.
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...
bool operator()(const MeshGenerator *const &a, const MeshGenerator *const &b) const
Definition: MeshGenerator.h:40
bool isDataOnly() const
void declareMeshesForSub(const std::string &param_name)
Like declareMeshForSub(), but for multiple generators.
void declareNullMeshName(const MeshGeneratorName &name)
Registers the name name as a "null" mesh, which is a MeshGenerator used in InputParameters that will ...
T & copyMeshProperty(const std::string &target_data_name, const std::string &source_data_name, const std::string &source_mesh)
Method for copying attribute from input mesh meta-data store to current mesh meta-data store...
const std::string & getSavedMeshName() const
Return the name of the saved mesh.
void declareMeshForSubByName(const MeshGeneratorName &mesh_generator_name)
Like declareMeshForSub(), but takes the name of another MeshGenerator directly.
bool hasMeshProperty(const std::string &data_name, const std::string &prefix) const
MeshGenerators are objects that can modify or add to an existing mesh.
Definition: MeshGenerator.h:32
const std::vector< MeshGeneratorName > & getMeshGeneratorNamesFromParam(const std::string &param_name) const
Helper for getting a std::vector<MeshGeneratorName> parameter.
Abstract definition of a RestartableData value.
const MeshGeneratorName * getMeshGeneratorNameFromParam(const std::string &param_name, const bool allow_invalid) const
Helper for getting a MeshGeneratorName parameter.
Definition: MeshGenerator.C:92
void setParameters(const std::string &name, const T &value, Ts... extra_input_parameters)
Given a series of parameters names and values, sets each name to the corresponding value...
AddParentChildKey(const AddParentChildKey &)
const std::set< MeshGeneratorName > & getRequestedMeshGenerators() const
Definition: MeshGenerator.h:80