https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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"
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
23class MooseMesh;
24namespace libMesh
25{
26class ReplicatedMesh;
27class DistributedMesh;
28}
29
34{
35public:
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
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 {
151 }
155 const std::set<const MeshGenerator *, Comparator> & getSubMeshGenerators() const
156 {
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
216protected:
220 virtual void generateData();
221
226 virtual std::unique_ptr<CSG::CSGBase> generateCSG();
227
233 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
248 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>
396 buildMeshBaseObject(unsigned int dim = libMesh::invalid_uint);
397
404 [[nodiscard]] std::unique_ptr<ReplicatedMesh>
405 buildReplicatedMesh(unsigned int dim = libMesh::invalid_uint);
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
463
464private:
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::unique_ptr<CSG::CSGBase> _null_csg_base = nullptr;
515
517 std::set<const MeshGenerator *, Comparator> _parent_mesh_generators;
519 std::set<const MeshGenerator *, Comparator> _child_mesh_generators;
521 std::set<const MeshGenerator *, Comparator> _sub_mesh_generators;
522
524 std::set<std::string> _null_mesh_names;
525
527 const std::string & _save_with_name;
528
530 const bool _data_only;
531};
532
533template <typename T, typename... Args>
534T &
535MeshGenerator::declareMeshProperty(const std::string & data_name, Args &&... args)
536{
538 mooseError("Can only call declareMeshProperty() during MeshGenerator construction");
539
540 // We sort construction ordering so that we _must_ declare before retrieving
541 if (hasMeshProperty(data_name))
542 mooseError("While declaring mesh property '",
543 data_name,
544 "' with type '",
545 MooseUtils::prettyCppType<T>(),
546 "',\nsaid property has already been declared with type '",
547 setMeshPropertyHelper(data_name).type(),
548 "'");
549
550 const auto full_name = meshPropertyName(data_name);
551 auto new_T_value =
552 std::make_unique<RestartableData<T>>(full_name, nullptr, std::forward<Args>(args)...);
553 auto value =
554 &_app.registerRestartableData(std::move(new_T_value), 0, false, MooseApp::MESH_META_DATA);
555 mooseAssert(value->declared(), "Should be declared");
556
557 RestartableData<T> * T_value = dynamic_cast<RestartableData<T> *>(value);
558 mooseAssert(T_value, "Bad cast");
559
560 return T_value->set();
561}
562
563template <typename T, typename... Args>
564T &
565MeshGenerator::setMeshProperty(const std::string & data_name, Args &&... args)
566{
567 if (_app.actionWarehouse().getCurrentTaskName() != "execute_mesh_generators")
568 mooseError("Updating mesh meta data with setMeshProperty() can only be called during "
569 "MeshGenerator::generate()");
570
571 if (!hasMeshProperty(data_name))
572 mooseError("Failed to get the mesh property '", data_name, "'");
573 RestartableDataValue * value = &setMeshPropertyHelper(data_name);
574 RestartableData<T> * T_value = dynamic_cast<RestartableData<T> *>(value);
575 if (!T_value)
576 mooseError("While retrieving mesh property '",
577 data_name,
578 "' with type '",
579 MooseUtils::prettyCppType<T>(),
580 "',\nthe property was found with type '",
581 value->type(),
582 "'");
583
584 // Set the value if someone provided arguments to set it to
585 if constexpr (sizeof...(args) > 0)
586 T_value->set() = T(std::forward<Args>(args)...);
587
588 return T_value->set();
589}
590
591template <typename... Ts>
592void
593MeshGenerator::addMeshSubgenerator(const std::string & type,
594 const std::string & name,
595 Ts... extra_input_parameters)
596{
597 InputParameters subgenerator_params = _app.getFactory().getValidParams(type);
598
599 subgenerator_params.setParameters(extra_input_parameters...);
600
601 addMeshSubgenerator(type, name, subgenerator_params);
602}
const std::string & getCurrentTaskName() const
InputParameters getValidParams(const std::string &name) const
Get valid parameters for the object.
Definition Factory.C:68
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
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.
System that manages MeshGenerators.
Class that is used as a parameter to add[Parent/Child]() that allows only MeshGeneratorSystem methods...
AddParentChildKey(const AddParentChildKey &)
MeshGenerators are objects that can modify or add to an existing mesh.
virtual std::unique_ptr< MeshBase > generate()=0
Generate / modify the mesh.
T & setMeshProperty(const std::string &data_name, const T &data_value)
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::set< const MeshGenerator *, Comparator > _child_mesh_generators
The MeshGenerators that are children to this MeshGenerator.
const std::string & getSavedMeshName() const
Return the name of the saved mesh.
std::vector< std::unique_ptr< MeshBase > * > getMeshes(const std::string &param_name)
Like getMesh(), but for multiple generators.
bool hasSaveMesh() const
Return whether or not to save the current mesh.
virtual std::unique_ptr< CSG::CSGBase > generateCSG()
Generate the CSG representation of the mesh (or meshing operation) created by this mesh generator.
std::set< MeshGeneratorName > _requested_mesh_generators
The names of the MeshGenerators that were requested in the getMesh methods.
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...
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...
std::vector< std::unique_ptr< CSG::CSGBase > * > getCSGBasesByName(const std::vector< MeshGeneratorName > &mesh_generator_names)
Like getCSGBaseByName(), but for multiple generators.
std::unique_ptr< MeshBase > _null_mesh
A nullptr to use for when inputs aren't specified.
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.
std::unique_ptr< MeshBase > & getMeshByName(const MeshGeneratorName &mesh_generator_name)
Like getMesh(), but takes the name of another MeshGenerator directly.
const std::string & _save_with_name
A user-defined name to save the mesh.
std::set< MeshGeneratorName > _requested_mesh_generators_for_sub
The names of the MeshGenerators that were requested in the declareMeshForSub methods.
MooseMesh *const _mesh
Pointer to the owning mesh.
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,...
const std::set< const MeshGenerator *, Comparator > & getSubMeshGenerators() const
Gets the MeshGenerators that are children to this MeshGenerator.
bool hasGenerateCSG() const
std::unique_ptr< CSG::CSGBase > generateInternalCSG()
Internal generation method for CSG object generation.
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.
const std::vector< MeshGeneratorName > & getMeshGeneratorNamesFromParam(const std::string &param_name) const
Helper for getting a std::vector<MeshGeneratorName> parameter.
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...
bool isParentMeshGenerator(const MeshGeneratorName &name, const bool direct=true) const
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< ReplicatedMesh > buildReplicatedMesh(unsigned int dim=libMesh::invalid_uint)
Build a replicated mesh.
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< CSG::CSGBase > * > getCSGBases(const std::string &param_name)
Like getCSGBase(), but for multiple generators.
static InputParameters validParams()
virtual std::string meshPropertyPrefix(const std::string &) const override final
Override of the default prefix to use when getting mesh properties.
std::set< const MeshGenerator *, Comparator > _sub_mesh_generators
The sub MeshGenerators constructed by this MeshGenerator.
static const std::string data_only_param
The name of the private parameter for setting data only.
T & declareMeshProperty(const std::string &data_name, const T &data_value)
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...
bool isNullMeshName(const MeshGeneratorName &name) const
const std::set< const MeshGenerator *, Comparator > & getChildMeshGenerators() const
Gets the MeshGenerators that are children to this MeshGenerator.
const std::set< const MeshGenerator *, Comparator > & getParentMeshGenerators() const
Gets the MeshGenerators that are parents to this MeshGenerator.
RestartableDataValue & setMeshPropertyHelper(const std::string &data_name)
Helper for getting a writable reference to a mesh property, used in declareMeshProperty and setMeshPr...
std::vector< std::unique_ptr< MeshBase > * > getMeshesByName(const std::vector< MeshGeneratorName > &mesh_generator_names)
Like getMeshByName(), but for multiple generators.
std::unique_ptr< CSG::CSGBase > & getCSGBaseByName(const MeshGeneratorName &mesh_generator_name)
Like getCSGBase(), but takes the name of another MeshGenerator directly.
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.
const MeshGeneratorName * getMeshGeneratorNameFromParam(const std::string &param_name, const bool allow_invalid) const
Helper for getting a MeshGeneratorName parameter.
void declareMeshesForSub(const std::string &param_name)
Like declareMeshForSub(), but for multiple generators.
static void setHasGenerateData(InputParameters &params)
Sets that a mesh generator has a generateData() implementation.
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 declareMeshesForSubByName(const std::vector< MeshGeneratorName > &mesh_generator_names)
Like declareMeshForSubByName(), but for multiple generators.
void declareMeshForSubByName(const MeshGeneratorName &mesh_generator_name)
Like declareMeshForSub(), but takes the name of another MeshGenerator directly.
bool isDataOnly() const
const bool _data_only
Whether or not this mesh generator will run in data only mode.
std::unique_ptr< CSG::CSGBase > _null_csg_base
A nullptr to use for when inputs aren't specified.
std::set< std::string > _null_mesh_names
The declared "null" mesh names that will not represent a mesh in input; see declareNullMeshName()
const std::set< MeshGeneratorName > & getRequestedMeshGenerators() const
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...
bool isChildMeshGenerator(const MeshGeneratorName &name, const bool direct=true) const
std::set< const MeshGenerator *, Comparator > _parent_mesh_generators
The MeshGenerators that are parents to this MeshGenerator.
bool hasGenerateData() const
void declareNullMeshName(const MeshGeneratorName &name)
Registers the name name as a "null" mesh, which is a MeshGenerator used in InputParameters that will ...
void checkGetMesh(const MeshGeneratorName &mesh_generator_name, const std::string &param_name) const
Helper for performing error checking in the getMesh methods.
void addChildMeshGenerator(const MeshGenerator &mg, const AddParentChildKey)
Adds the MeshGenerator mg as a child.
bool hasOutput() const
void addParentMeshGenerator(const MeshGenerator &mg, const AddParentChildKey)
Adds the MeshGenerator mg as a parent.
const std::set< MeshGeneratorName > & getRequestedMeshGeneratorsForSub() const
virtual void generateData()
Generate the mesh data.
The Interface used to retrieve mesh meta data (attributes) set by the MeshGenerator system.
bool hasMeshProperty(const std::string &data_name, const std::string &prefix) const
static std::string meshPropertyName(const std::string &data_name, const std::string &prefix)
ActionWarehouse & actionWarehouse()
Return a writable reference to the ActionWarehouse associated with this app.
Definition MooseApp.h:217
virtual bool constructingMeshGenerators() const
Whether this app is constructing mesh generators.
Definition MooseApp.C:3494
Factory & getFactory()
Retrieve a writable reference to the Factory associated with this App.
Definition MooseApp.h:407
RestartableDataValue & registerRestartableData(std::unique_ptr< RestartableDataValue > data, THREAD_ID tid, bool read_only, const RestartableDataMapName &metaname="")
Definition MooseApp.C:2449
static const RestartableDataMapName MESH_META_DATA
Definition MooseApp.h:136
const InputParameters & parameters() const
Get the parameters of the object.
Definition MooseBase.h:131
const std::string & type() const
Get the type of this class.
Definition MooseBase.h:93
const std::string & name() const
Get the name of the class.
Definition MooseBase.h:103
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
const InputParameters & _pars
The object's parameters.
Definition MooseBase.h:384
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition MooseMesh.h:95
Every object that can be built by the factory should be derived from this class.
Definition MooseObject.h:31
MooseApp & _app
The MOOSE application this is associated with.
Definition MooseBase.h:375
Abstract definition of a RestartableData value.
Concrete definition of a parameter value for a specified type.
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
const unsigned int invalid_uint
Comparator for MeshGenerators that sorts by name.
bool operator()(const MeshGenerator *const &a, const MeshGenerator *const &b) const