www.mooseframework.org
FileMesh.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 "FileMesh.h"
11 #include "Parser.h"
12 #include "MooseUtils.h"
13 #include "Moose.h"
14 #include "MooseApp.h"
15 
16 #include "libmesh/exodusII_io.h"
17 #include "libmesh/nemesis_io.h"
18 #include "libmesh/parallel_mesh.h"
19 
20 registerMooseObject("MooseApp", FileMesh);
21 
22 template <>
25 {
27  params.addRequiredParam<MeshFileName>("file", "The name of the mesh file to read");
28  params.addClassDescription("Read a mesh from a file.");
29  return params;
30 }
31 
33  : MooseMesh(parameters),
34  _file_name(getParam<MeshFileName>("file")),
35  _dim(getParam<MooseEnum>("dim")),
36  _read_mesh_timer(registerTimedSection("readMesh", 2))
37 {
38 }
39 
40 FileMesh::FileMesh(const FileMesh & other_mesh)
41  : MooseMesh(other_mesh),
42  _file_name(other_mesh._file_name),
43  _dim(other_mesh._dim),
44  _read_mesh_timer(other_mesh._read_mesh_timer)
45 {
46 }
47 
49 
50 std::unique_ptr<MooseMesh>
52 {
53  return libmesh_make_unique<FileMesh>(*this);
54 }
55 
56 void
58 {
59  TIME_SECTION(_read_mesh_timer);
60 
61  getMesh().set_mesh_dimension(getParam<MooseEnum>("dim"));
62 
63  if (_is_nemesis)
64  {
65  // Nemesis_IO only takes a reference to DistributedMesh, so we can't be quite so short here.
66  DistributedMesh & pmesh = cast_ref<DistributedMesh &>(getMesh());
67  Nemesis_IO(pmesh).read(_file_name);
68 
69  getMesh().allow_renumbering(false);
70 
71  // Even if we want repartitioning when load balancing later, we'll
72  // begin with the default partitioning defined by the Nemesis
73  // file.
74  bool skip_partitioning_later = getMesh().skip_partitioning();
75  getMesh().skip_partitioning(true);
76  getMesh().prepare_for_use();
77  getMesh().skip_partitioning(skip_partitioning_later);
78  }
79  else // not reading Nemesis files
80  {
81  // See if the user has requested reading a solution from the file. If so, we'll need to read
82  // the mesh with the exodus reader instead of using mesh.read(). This will read the mesh on
83  // every processor
84 
85  if (_app.setFileRestart() && (_file_name.rfind(".exd") < _file_name.size() ||
86  _file_name.rfind(".e") < _file_name.size()))
87  {
89 
90  _exreader = libmesh_make_unique<ExodusII_IO>(getMesh());
91  _exreader->read(_file_name);
92 
93  getMesh().allow_renumbering(false);
94  getMesh().prepare_for_use();
95  }
96  else
97  {
98  // If we are reading a mesh while restarting, then we might have
99  // a solution file that relies on that mesh partitioning and/or
100  // numbering. In that case, we need to turn off repartitioning
101  // and renumbering, at least at first.
103  bool restarting = _file_name.rfind(".cpa") < _file_name.size() ||
104  _file_name.rfind(".cpr") < _file_name.size();
105 
106  const bool skip_partitioning_later = restarting && getMesh().skip_partitioning();
107  const bool allow_renumbering_later = restarting && getMesh().allow_renumbering();
108 
109  if (restarting)
110  {
111  getMesh().skip_partitioning(true);
112  getMesh().allow_renumbering(false);
113  }
114 
116  mooseError("cannot locate mesh file '", _file_name, "'");
117  getMesh().read(_file_name);
118 
119  if (restarting)
120  {
121  getMesh().allow_renumbering(allow_renumbering_later);
122  getMesh().skip_partitioning(skip_partitioning_later);
123  }
124  }
125  }
126 }
127 
128 void
129 FileMesh::read(const std::string & file_name)
130 {
131  if (dynamic_cast<DistributedMesh *>(&getMesh()) && !_is_nemesis)
132  getMesh().read(file_name, /*mesh_data=*/NULL, /*skip_renumber=*/false);
133  else
134  getMesh().read(file_name, /*mesh_data=*/NULL, /*skip_renumber=*/true);
135 }
virtual std::unique_ptr< MooseMesh > safeClone() const override
A safer version of the clone() method that hands back an allocated object wrapped in a smart pointer...
Definition: FileMesh.C:51
bool _is_nemesis
True if a Nemesis Mesh was read in.
Definition: MooseMesh.h:899
bool & setFileRestart()
This method is here so we can determine whether or not we need to use a separate reader to read the m...
Definition: MooseApp.h:306
virtual void buildMesh() override
Must be overridden by child classes.
Definition: FileMesh.C:57
std::string _file_name
the file_name from whence this mesh came
Definition: FileMesh.h:40
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
virtual ~FileMesh()
Definition: FileMesh.C:48
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
std::string convertLatestCheckpoint(std::string orig, bool base_only=true)
Replaces "LATEST" placeholders with the latest checkpoint file name.
Definition: MooseUtils.C:45
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...
const PerfID _read_mesh_timer
Timers.
Definition: FileMesh.h:50
bool checkFileReadable(const std::string &filename, bool check_line_endings=false, bool throw_on_unreadable=true)
Checks to see if a file is readable (exists and permissions)
Definition: MooseUtils.C:146
InputParameters validParams< MooseMesh >()
Definition: MooseMesh.C:63
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:2567
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:74
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:31
FileMesh(const InputParameters &parameters)
Definition: FileMesh.C:32
void read(const std::string &file_name)
Definition: FileMesh.C:129
MooseApp & _app
The MooseApp this object is associated with.
Definition: MooseObject.h:177
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...
registerMooseObject("MooseApp", FileMesh)
bool pathExists(const std::string &path)
Definition: MooseUtils.C:139
std::unique_ptr< ExodusII_IO > _exreader
Auxiliary object for restart.
Definition: FileMesh.h:43
InputParameters validParams< FileMesh >()
Definition: FileMesh.C:24