https://mooseframework.inl.gov
FileMeshGenerator.C
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 #include "FileMeshGenerator.h"
11 #include "CastUniquePointer.h"
12 
13 #include "libmesh/replicated_mesh.h"
14 #include "libmesh/face_quad4.h"
15 #include "libmesh/exodusII_io.h"
16 #include "libmesh/mesh_communication.h"
17 #include "libmesh/mesh_tools.h"
18 #include "libmesh/sparse_matrix.h"
19 
21 
24 {
26 
27  params.addRequiredParam<MeshFileName>("file", "The filename to read.");
28  params.addParam<std::vector<std::string>>(
29  "exodus_extra_element_integers",
30  "The variable names in the mesh file for loading extra element integers");
31  params.addParam<bool>("use_for_exodus_restart",
32  false,
33  "True to indicate that the mesh file this generator is reading can be used "
34  "for restarting variables");
35  params.addParam<bool>("skip_partitioning",
36  false,
37  "True to skip partitioning, only after this mesh generator, "
38  "because the mesh was pre-split for example.");
39  params.addParam<bool>("allow_renumbering",
40  true,
41  "Whether to allow the mesh to renumber nodes and elements, if not "
42  "overridden by a global parameter or by a requirement (e.g. an exodus "
43  "restart or a constraint matrix) that disables renumbering.");
44  params.addParam<bool>("clear_spline_nodes",
45  false,
46  "If clear_spline_nodes=true, IsoGeometric Analyis spline nodes "
47  "and constraints are removed from an IGA mesh, after which only "
48  "C^0 Rational-Bernstein-Bezier elements will remain.");
49  params.addParam<bool>("discontinuous_spline_extraction",
50  false,
51  "If discontinuous_spline_extraction=true, "
52  "Rational-Bernstein-Bezier elements extracted from a spline mesh "
53  "will be disconnected from neighboring elements, coupled only via "
54  "their extraction operators. This may be less efficient than the "
55  "default C^0 extracted mesh, but may be necessary if the extracted "
56  "mesh is non-conforming.");
57  params.addParam<MatrixFileName>(
58  "constraint_matrix", "", "The name of a constraint matrix file to apply to the mesh");
59  params.addParam<Real>("constraint_preconditioning",
60  0.0,
61  "Whether to attempt preconditioning with constraint matrix application");
62  params.addClassDescription("Read a mesh from a file.");
63  params.addParamNamesToGroup(
64  "clear_spline_nodes discontinuous_spline_extraction constraint_matrix",
65  "IsoGeometric Analysis (IGA) and other mesh constraint options");
66  return params;
67 }
68 
70  : MeshGenerator(parameters),
71  _file_name(getParam<MeshFileName>("file")),
72  _matrix_file_name(getParam<MatrixFileName>("constraint_matrix")),
73  _matrix_preconditioning(getParam<Real>("constraint_preconditioning")),
74  _skip_partitioning(getParam<bool>("skip_partitioning")),
75  _allow_renumbering(getParam<bool>("allow_renumbering"))
76 {
78  paramError("constraint_preconditioning",
79  "The 'constraint_preconditioning' parameter is only applicable to "
80  "meshes loaded with a corresponding 'constraint_matrix'.");
82  paramError("constraint_preconditioning",
83  "This version of MOOSE only supports a 'constraint_preconditioning' "
84  "value of 1.0 if it is enabled. Non-binary preconditioning values "
85  "are reserved for future use.");
86 }
87 
88 std::unique_ptr<MeshBase>
90 {
91  auto mesh = buildMeshBaseObject();
92 
93  // Maybe we'll reallow renumbering after constraints are applied?
94  bool eventually_allow_renumbering = _allow_renumbering && mesh->allow_renumbering();
95 
96  // If we have a constraint matrix, we need its numbering to match
97  // the numbering in the mesh file
98  if (!_matrix_file_name.empty())
99  mesh->allow_renumbering(false);
100 
101  // Figure out if we are reading an Exodus file, but not Tetgen (*.ele)
102  bool exodus = (_file_name.rfind(".exd") < _file_name.size() ||
103  _file_name.rfind(".e") < _file_name.size()) &&
104  _file_name.rfind(".ele") == std::string::npos;
105  bool has_exodus_integers = isParamValid("exodus_extra_element_integers");
106  bool restart_exodus = (getParam<bool>("use_for_exodus_restart") && _app.getExodusFileRestart());
107  if (exodus)
108  {
109  auto exreader = std::make_shared<libMesh::ExodusII_IO>(*mesh);
111 
112  if (has_exodus_integers)
113  exreader->set_extra_integer_vars(
114  getParam<std::vector<std::string>>("exodus_extra_element_integers"));
115 
116  if (restart_exodus)
117  {
118  _app.setExReaderForRestart(std::move(exreader));
119  exreader->read(_file_name);
120  eventually_allow_renumbering = false;
121  mesh->allow_renumbering(false);
122  }
123  else
124  {
125  if (mesh->processor_id() == 0)
126  {
127  if (getParam<bool>("discontinuous_spline_extraction"))
128  exreader->set_discontinuous_bex(true);
129 
130  exreader->read(_file_name);
131 
132  if (getParam<bool>("clear_spline_nodes"))
133  MeshTools::clear_spline_nodes(*mesh);
134  }
136  }
137  // Skip partitioning if the user requested it
138  if (_skip_partitioning)
139  mesh->skip_partitioning(true);
140  mesh->prepare_for_use();
141  mesh->skip_partitioning(false);
142  }
143  else
144  {
145  if (_pars.isParamSetByUser("exodus_extra_element_integers"))
146  mooseError("\"exodus_extra_element_integers\" should be given only for Exodus mesh files");
147  if (_pars.isParamSetByUser("use_for_exodus_restart"))
148  mooseError("\"use_for_exodus_restart\" should be given only for Exodus mesh files");
149 
150  const auto file_name = deduceCheckpointPath(*this, _file_name);
152 
153  mesh->skip_partitioning(_skip_partitioning);
154  mesh->allow_renumbering(_allow_renumbering);
155  mesh->read(file_name);
156 
157  // Load the meta data if it is available
159  }
160 
161  if (!_matrix_file_name.empty())
162  {
163  auto matrix = SparseMatrix<Number>::build(mesh->comm());
164  matrix->read_matlab(_matrix_file_name);
165 
166  // In the future we might deduce matrix orientation via matrix
167  // size; for now we simply hardcode that the Flex IGA standard for
168  // projection operator matrices is the transpose of our standard
169  // for constraint equations.
170  matrix->get_transpose(*matrix);
171  mesh->copy_constraint_rows(*matrix, _matrix_preconditioning);
172 
173  // libMesh should probably update this in copy_constraint_rows();
174  // once it does this will be a redundant sweep we can remove.
175  mesh->cache_elem_data();
176  }
177 
178  mesh->allow_renumbering(eventually_allow_renumbering);
179 
180  return dynamic_pointer_cast<MeshBase>(mesh);
181 }
182 
183 std::string
184 FileMeshGenerator::deduceCheckpointPath(const MooseObject & object, const std::string & file_name)
185 {
186  // Just exists, use it
187  if (MooseUtils::pathExists(file_name))
188  return file_name;
189 
190  // LATEST
191  return MooseUtils::convertLatestCheckpoint(file_name) + object.getMooseApp().checkpointSuffix();
192 }
const MeshFileName & _file_name
the path/name of the file containing the mesh
const bool _skip_partitioning
whether to skip partitioning after loading the mesh
static const RestartableDataMapName MESH_META_DATA
Definition: MooseApp.h:123
Generates a mesh by reading it from an file.
MeshBase & 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::string convertLatestCheckpoint(std::string orig)
Replaces "LATEST" placeholders with the latest checkpoint file name.
Definition: MooseUtils.C:153
static std::unique_ptr< SparseMatrix< Number > > build(const Parallel::Communicator &comm, const SolverPackage solver_package=libMesh::default_solver_package(), const MatrixBuildType matrix_build_type=MatrixBuildType::AUTOMATIC)
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...
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
bool getExodusFileRestart() const
Whether or not we need to use a separate Exodus reader to read the mesh BEFORE we create the mesh...
Definition: MooseApp.h:453
FileMeshGenerator(const InputParameters &parameters)
void possiblyLoadRestartableMetaData(const RestartableDataMapName &name, const std::filesystem::path &folder_base)
Loads the restartable meta data for name if it is available with the folder base folder_base.
Definition: MooseApp.C:2528
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:28
bool checkFileReadable(const std::string &filename, bool check_line_endings=false, bool throw_on_unreadable=true, bool check_for_git_lfs_pointer=true)
Checks to see if a file is readable (exists and permissions)
Definition: MooseUtils.C:250
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
const MatrixFileName & _matrix_file_name
the path/name of any file containing a matrix of mesh constraints
MooseApp & _app
The MOOSE application this is associated with.
Definition: MooseBase.h:84
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
static InputParameters validParams()
Definition: MeshGenerator.C:23
const Real _matrix_preconditioning
Whether (and later, how much) libMesh should try constraint matrix preconditioning.
static std::string deduceCheckpointPath(const MooseObject &object, const std::string &file_name)
Helper for deducing a checkpoint file given the path.
bool isParamSetByUser(const std::string &name) const
Method returns true if the parameter was set by the user.
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
registerMooseObject("MooseApp", FileMeshGenerator)
void broadcast(MeshBase &) const
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.
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object...
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 pathExists(const std::string &path)
Definition: MooseUtils.C:243
MeshGenerators are objects that can modify or add to an existing mesh.
Definition: MeshGenerator.h:32
static InputParameters validParams()
void setExReaderForRestart(std::shared_ptr< libMesh::ExodusII_IO > &&exreader)
Set the Exodus reader to restart variables from an Exodus mesh file.
Definition: MooseApp.h:458
const bool _allow_renumbering
Whether to allow renumbering (for non-exodus files) when the mesh is read and prepared for use...
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...