www.mooseframework.org
FileMeshGenerator.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 "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 
20 
23 {
25 
26  params.addRequiredParam<MeshFileName>("file", "The filename to read.");
27  params.addParam<std::vector<std::string>>(
28  "exodus_extra_element_integers",
29  "The variable names in the mesh file for loading extra element integers");
30  params.addParam<bool>("use_for_exodus_restart",
31  false,
32  "True to indicate that the mesh file this generator is reading can be used "
33  "for restarting variables");
34  params.addParam<bool>("skip_partitioning",
35  false,
36  "True to skip partitioning, only after this mesh generator, "
37  "because the mesh was pre-split for example.");
38  params.addParam<bool>("allow_renumbering",
39  true,
40  "Whether to allow the mesh to renumber nodes and elements. Note that this "
41  "parameter is only relevant for non-exodus files, e.g. if reading from "
42  "checkpoint for example. For exodus we always disallow renumbering.");
43  params.addParam<bool>("clear_spline_nodes",
44  false,
45  "If clear_spline_nodes=true, IsoGeometric Analyis spline nodes "
46  "and constraints are removed from an IGA mesh, after which only "
47  "C^0 Rational-Bernstein-Bezier elements will remain.");
48  params.addClassDescription("Read a mesh from a file.");
49  return params;
50 }
51 
53  : MeshGenerator(parameters),
54  _file_name(getParam<MeshFileName>("file")),
55  _skip_partitioning(getParam<bool>("skip_partitioning")),
56  _allow_renumbering(getParam<bool>("allow_renumbering"))
57 {
58 }
59 
60 std::unique_ptr<MeshBase>
62 {
63  auto mesh = buildMeshBaseObject();
64 
65  // Figure out if we are reading an Exodus file, but not Tetgen (*.ele)
66  bool exodus = (_file_name.rfind(".exd") < _file_name.size() ||
67  _file_name.rfind(".e") < _file_name.size()) &&
68  _file_name.rfind(".ele") == std::string::npos;
69  bool has_exodus_integers = isParamValid("exodus_extra_element_integers");
70  bool restart_exodus = (getParam<bool>("use_for_exodus_restart") && _app.getExodusFileRestart());
71  if (exodus)
72  {
73  auto exreader = std::make_shared<ExodusII_IO>(*mesh);
75 
76  if (has_exodus_integers)
77  exreader->set_extra_integer_vars(
78  getParam<std::vector<std::string>>("exodus_extra_element_integers"));
79 
80  if (restart_exodus)
81  {
82  _app.setExReaderForRestart(std::move(exreader));
83  exreader->read(_file_name);
84  mesh->allow_renumbering(false);
85  }
86  else
87  {
88  if (mesh->processor_id() == 0)
89  {
90  exreader->read(_file_name);
91 
92  if (getParam<bool>("clear_spline_nodes"))
93  MeshTools::clear_spline_nodes(*mesh);
94  }
95  MeshCommunication().broadcast(*mesh);
96  }
97  // Skip partitioning if the user requested it
99  mesh->skip_partitioning(true);
100  mesh->prepare_for_use();
101  mesh->skip_partitioning(false);
102  }
103  else
104  {
105  if (_pars.isParamSetByUser("exodus_extra_element_integers"))
106  mooseError("\"exodus_extra_element_integers\" should be given only for Exodus mesh files");
107  if (_pars.isParamSetByUser("use_for_exodus_restart"))
108  mooseError("\"use_for_exodus_restart\" should be given only for Exodus mesh files");
109 
110  // Supports old suffix (xxxx_mesh.cpr -> xxxx-mesh.cpr) and LATEST
111  const auto file_name = deduceCheckpointPath(*this, _file_name);
113 
114  mesh->skip_partitioning(_skip_partitioning);
115  mesh->allow_renumbering(_allow_renumbering);
116  mesh->read(file_name);
117 
118  // Load the meta data if it is available
120  }
121 
122  return dynamic_pointer_cast<MeshBase>(mesh);
123 }
124 
125 std::string
126 FileMeshGenerator::deduceCheckpointPath(const MooseObject & object, const std::string & file_name)
127 {
128  // Just exists, use it
129  if (MooseUtils::pathExists(file_name))
130  return file_name;
131 
132  // xxxx_mesh.cpr -> xxxx-mesh.cpr
133  const std::string old_ending = "_mesh.cpr";
134  if (std::equal(old_ending.rbegin(), old_ending.rend(), file_name.rbegin()))
135  {
136  const std::string new_ending = "-mesh.cpr";
137  auto new_path = file_name;
138  new_path.replace(new_path.size() - old_ending.size(), old_ending.size(), new_ending, 0);
139  if (MooseUtils::pathExists(new_path))
140  {
141  std::stringstream warning;
142  warning
143  << "The supplied checkpoint " << std::filesystem::path(file_name).filename()
144  << " uses the previous default checkpoint suffix of \"" << old_ending
145  << "\".\nThe new default checkpoint suffix is \"" << new_ending << "\".\n\n"
146  << "Your supplied checkpoint was not found, but one with the new ending was found in\n\""
147  << new_path << "\".\n\n"
148  << "The above checkpoint is being used instead.\nYou should modify your input "
149  "accordingly.";
150  object.paramWarning("file", warning.str());
151  return new_path;
152  }
153  }
154 
155  // LATEST
156  return MooseUtils::convertLatestCheckpoint(file_name) + object.getMooseApp().checkpointSuffix();
157 }
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:95
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.
void setExReaderForRestart(std::shared_ptr< ExodusII_IO > &&exreader)
Set the Exodus reader to restart variables from an Exodus mesh file.
Definition: MooseApp.h:430
std::string convertLatestCheckpoint(std::string orig)
Replaces "LATEST" placeholders with the latest checkpoint file name.
Definition: MooseUtils.C:146
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:425
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:1881
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:33
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:256
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
MooseApp & _app
The MOOSE application this is associated with.
Definition: MooseBase.h:84
static InputParameters validParams()
Definition: MeshGenerator.C:23
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 by the user.
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
registerMooseObject("MooseApp", FileMeshGenerator)
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 option 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:240
MeshGenerators are objects that can modify or add to an existing mesh.
Definition: MeshGenerator.h:32
static InputParameters validParams()
const bool _allow_renumbering
Whether to allow renumbering (for non-exodus files) when the mesh is read and prepared for use...