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 #include "CSGBase.h"
16 
17 // Included so mesh generators don't need to include this when constructing MeshBase objects
18 #include "MooseMesh.h"
19 
20 #include "libmesh/mesh_base.h"
21 #include "libmesh/parameters.h"
22 
23 class MooseMesh;
24 namespace libMesh
25 {
26 class ReplicatedMesh;
27 class DistributedMesh;
28 }
29 
34 {
35 public:
39  struct Comparator
40  {
41  bool operator()(const MeshGenerator * const & a, const MeshGenerator * const & b) const
42  {
43  return a->name() < b->name();
44  }
45  };
46 
48 
55  static void setHasGenerateData(InputParameters & params);
60  static bool hasGenerateData(const InputParameters & params);
61 
68  static void setHasGenerateCSG(InputParameters & params);
73  static bool hasGenerateCSG(const InputParameters & params);
74 
76  static const std::string data_only_param;
77 
79 
83  virtual std::unique_ptr<MeshBase> generate() = 0;
84 
88  [[nodiscard]] std::unique_ptr<CSG::CSGBase> generateInternalCSG();
89 
94  [[nodiscard]] std::unique_ptr<MeshBase> generateInternal();
95 
99  const std::set<MeshGeneratorName> & getRequestedMeshGenerators() const
100  {
102  }
107  const std::set<MeshGeneratorName> & getRequestedMeshGeneratorsForSub() const
108  {
110  }
111 
117  {
118  friend class MeshGeneratorSystem;
121  };
122 
129  void addParentMeshGenerator(const MeshGenerator & mg, const AddParentChildKey);
136  void addChildMeshGenerator(const MeshGenerator & mg, const AddParentChildKey);
137 
141  const std::set<const MeshGenerator *, Comparator> & getParentMeshGenerators() const
142  {
144  }
148  const std::set<const MeshGenerator *, Comparator> & getChildMeshGenerators() const
149  {
150  return _child_mesh_generators;
151  }
155  const std::set<const MeshGenerator *, Comparator> & getSubMeshGenerators() const
156  {
157  return _sub_mesh_generators;
158  }
159 
167  bool isParentMeshGenerator(const MeshGeneratorName & name, const bool direct = true) const;
168 
176  bool isChildMeshGenerator(const MeshGeneratorName & name, const bool direct = true) const;
177 
184  bool isNullMeshName(const MeshGeneratorName & name) const { return _null_mesh_names.count(name); }
185 
189  bool hasSaveMesh() const;
190 
194  bool hasOutput() const;
195 
199  const std::string & getSavedMeshName() const;
200 
204  bool hasGenerateData() const { return hasGenerateData(_pars); }
205 
209  bool hasGenerateCSG() const { return hasGenerateCSG(_pars); }
210 
214  bool isDataOnly() const { return _data_only; }
215 
216 protected:
220  virtual void generateData();
221 
226  virtual std::unique_ptr<CSG::CSGBase> generateCSG();
227 
232  template <typename T, typename... Args>
234  T & declareMeshProperty(const std::string & data_name, Args &&... args);
235  template <typename T>
236  T & declareMeshProperty(const std::string & data_name, const T & data_value)
237  {
238  return declareMeshProperty<T, const T &>(data_name, data_value);
239  }
241 
247  template <typename T, typename... Args>
249  T & setMeshProperty(const std::string & data_name, Args &&... args);
250  template <typename T>
251  T & setMeshProperty(const std::string & data_name, const T & data_value)
252  {
253  return setMeshProperty<T, const T &>(data_name, data_value);
254  }
256 
262  template <typename T>
263  T & copyMeshProperty(const std::string & target_data_name,
264  const std::string & source_data_name,
265  const std::string & source_mesh)
266  {
267  return declareMeshProperty(target_data_name, getMeshProperty<T>(source_data_name, source_mesh));
268  }
269 
275  template <typename T>
276  T & copyMeshProperty(const std::string & source_data_name, const std::string & source_mesh)
277  {
278  return copyMeshProperty<T>(source_data_name, source_data_name, source_mesh);
279  }
280 
295  [[nodiscard]] std::unique_ptr<MeshBase> & getMesh(const std::string & param_name,
296  const bool allow_invalid = false);
302  [[nodiscard]] std::vector<std::unique_ptr<MeshBase> *> getMeshes(const std::string & param_name);
310  [[nodiscard]] std::unique_ptr<MeshBase> &
311  getMeshByName(const MeshGeneratorName & mesh_generator_name);
317  [[nodiscard]] std::vector<std::unique_ptr<MeshBase> *>
318  getMeshesByName(const std::vector<MeshGeneratorName> & mesh_generator_names);
319 
329  [[nodiscard]] std::unique_ptr<CSG::CSGBase> & getCSGBase(const std::string & param_name);
330 
339  [[nodiscard]] std::unique_ptr<CSG::CSGBase> &
340  getCSGBaseByName(const MeshGeneratorName & mesh_generator_name);
341 
350  [[nodiscard]] std::vector<std::unique_ptr<CSG::CSGBase> *>
351  getCSGBases(const std::string & param_name);
352 
361  [[nodiscard]] std::vector<std::unique_ptr<CSG::CSGBase> *>
362  getCSGBasesByName(const std::vector<MeshGeneratorName> & mesh_generator_names);
363 
374  void declareMeshForSub(const std::string & param_name);
378  void declareMeshesForSub(const std::string & param_name);
382  void declareMeshForSubByName(const MeshGeneratorName & mesh_generator_name);
386  void declareMeshesForSubByName(const std::vector<MeshGeneratorName> & mesh_generator_names);
387 
395  [[nodiscard]] std::unique_ptr<MeshBase>
397 
404  [[nodiscard]] std::unique_ptr<ReplicatedMesh>
406 
414  [[nodiscard]] std::unique_ptr<DistributedMesh>
416 
435  template <typename... Ts>
436  void addMeshSubgenerator(const std::string & type,
437  const std::string & name,
438  Ts... extra_input_parameters);
439 
448  void
449  addMeshSubgenerator(const std::string & type, const std::string & name, InputParameters params);
450 
459  void declareNullMeshName(const MeshGeneratorName & name);
460 
462  MooseMesh * const _mesh;
463 
464 private:
471  virtual std::string meshPropertyPrefix(const std::string &) const override final
472  {
473  return name();
474  }
475 
479  void checkGetMesh(const MeshGeneratorName & mesh_generator_name,
480  const std::string & param_name) const;
481 
485  const MeshGeneratorName * getMeshGeneratorNameFromParam(const std::string & param_name,
486  const bool allow_invalid) const;
490  const std::vector<MeshGeneratorName> &
491  getMeshGeneratorNamesFromParam(const std::string & param_name) const;
492 
497  RestartableDataValue & setMeshPropertyHelper(const std::string & data_name);
498 
500  std::set<MeshGeneratorName> _requested_mesh_generators;
502  std::set<MeshGeneratorName> _requested_mesh_generators_for_sub;
505  std::vector<std::pair<std::string, std::unique_ptr<MeshBase> *>> _requested_meshes;
508  std::vector<std::pair<std::string, std::unique_ptr<CSG::CSGBase> *>> _requested_csg_bases;
509 
511  std::unique_ptr<MeshBase> _null_mesh = nullptr;
512 
514  std::set<const MeshGenerator *, Comparator> _parent_mesh_generators;
516  std::set<const MeshGenerator *, Comparator> _child_mesh_generators;
518  std::set<const MeshGenerator *, Comparator> _sub_mesh_generators;
519 
521  std::set<std::string> _null_mesh_names;
522 
524  const std::string & _save_with_name;
525 
527  const bool _data_only;
528 };
529 
530 template <typename T, typename... Args>
531 T &
532 MeshGenerator::declareMeshProperty(const std::string & data_name, Args &&... args)
533 {
535  mooseError("Can only call declareMeshProperty() during MeshGenerator construction");
536 
537  // We sort construction ordering so that we _must_ declare before retrieving
538  if (hasMeshProperty(data_name))
539  mooseError("While declaring mesh property '",
540  data_name,
541  "' with type '",
542  MooseUtils::prettyCppType<T>(),
543  "',\nsaid property has already been declared with type '",
544  setMeshPropertyHelper(data_name).type(),
545  "'");
546 
547  const auto full_name = meshPropertyName(data_name);
548  auto new_T_value =
549  std::make_unique<RestartableData<T>>(full_name, nullptr, std::forward<Args>(args)...);
550  auto value =
551  &_app.registerRestartableData(std::move(new_T_value), 0, false, MooseApp::MESH_META_DATA);
552  mooseAssert(value->declared(), "Should be declared");
553 
554  RestartableData<T> * T_value = dynamic_cast<RestartableData<T> *>(value);
555  mooseAssert(T_value, "Bad cast");
556 
557  return T_value->set();
558 }
559 
560 template <typename T, typename... Args>
561 T &
562 MeshGenerator::setMeshProperty(const std::string & data_name, Args &&... args)
563 {
564  if (_app.actionWarehouse().getCurrentTaskName() != "execute_mesh_generators")
565  mooseError("Updating mesh meta data with setMeshProperty() can only be called during "
566  "MeshGenerator::generate()");
567 
568  if (!hasMeshProperty(data_name))
569  mooseError("Failed to get the mesh property '", data_name, "'");
571  RestartableData<T> * T_value = dynamic_cast<RestartableData<T> *>(value);
572  if (!T_value)
573  mooseError("While retrieving mesh property '",
574  data_name,
575  "' with type '",
576  MooseUtils::prettyCppType<T>(),
577  "',\nthe property was found with type '",
578  value->type(),
579  "'");
580 
581  // Set the value if someone provided arguments to set it to
582  if constexpr (sizeof...(args) > 0)
583  T_value->set() = T(std::forward<Args>(args)...);
584 
585  return T_value->set();
586 }
587 
588 template <typename... Ts>
589 void
590 MeshGenerator::addMeshSubgenerator(const std::string & type,
591  const std::string & name,
592  Ts... extra_input_parameters)
593 {
594  InputParameters subgenerator_params = _app.getFactory().getValidParams(type);
595 
596  subgenerator_params.setParameters(extra_input_parameters...);
597 
598  addMeshSubgenerator(type, name, subgenerator_params);
599 }
virtual bool constructingMeshGenerators() const
Whether this app is constructing mesh generators.
Definition: MooseApp.C:3600
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:76
bool isChildMeshGenerator(const MeshGeneratorName &name, const bool direct=true) const
const InputParameters & _pars
The object&#39;s parameters.
Definition: MooseBase.h:366
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.
std::vector< std::unique_ptr< CSG::CSGBase > * > getCSGBases(const std::string &param_name)
Like getCSGBase(), but for multiple generators.
Comparator for MeshGenerators that sorts by name.
Definition: MeshGenerator.h:39
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:130
const InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseBase.h:131
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:162
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:57
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
bool hasGenerateData() const
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:401
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::string & name() const
Get the name of the class.
Definition: MooseBase.h:103
const std::set< MeshGeneratorName > & getRequestedMeshGeneratorsForSub() const
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:2555
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:92
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:93
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:211
std::vector< std::unique_ptr< MeshBase > * > getMeshesByName(const std::vector< MeshGeneratorName > &mesh_generator_names)
Like getMeshByName(), but for multiple generators.
std::vector< std::pair< std::string, std::unique_ptr< CSG::CSGBase > * > > _requested_csg_bases
The CSGBase objects that were requested by this MeshGenerator; used to verify that any input meshes t...
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:357
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...
static void setHasGenerateCSG(InputParameters &params)
Sets that a mesh generator has a generateCSG() implementation.
Definition: MeshGenerator.C:94
bool hasGenerateCSG() const
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...
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 and optionally a file path to the top-level block p...
Definition: MooseBase.h:271
std::unique_ptr< CSG::CSGBase > & getCSGBaseByName(const MeshGeneratorName &mesh_generator_name)
Like getCSGBase(), but takes the name of another MeshGenerator directly.
std::unique_ptr< CSG::CSGBase > & getCSGBase(const std::string &param_name)
Takes the name of a MeshGeneratorName parameter and then gets a pointer to the CSGBase object that Me...
static void setHasGenerateData(InputParameters &params)
Sets that a mesh generator has a generateData() implementation.
Definition: MeshGenerator.C:82
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< CSG::CSGBase > generateInternalCSG()
Internal generation method for CSG object generation.
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:41
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:33
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.
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 &)
virtual std::unique_ptr< CSG::CSGBase > generateCSG()
Generate the CSG representation of the mesh (or meshing operation) created by this mesh generator...
std::vector< std::unique_ptr< CSG::CSGBase > * > getCSGBasesByName(const std::vector< MeshGeneratorName > &mesh_generator_names)
Like getCSGBaseByName(), but for multiple generators.
const std::set< MeshGeneratorName > & getRequestedMeshGenerators() const
Definition: MeshGenerator.h:99