www.mooseframework.org
FileMesh.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #include "FileMesh.h"
16 #include "Parser.h"
17 #include "MooseUtils.h"
18 #include "Moose.h"
19 #include "MooseApp.h"
20 
21 #include "libmesh/exodusII_io.h"
22 #include "libmesh/nemesis_io.h"
23 #include "libmesh/parallel_mesh.h"
24 
25 template <>
28 {
30  params.addRequiredParam<MeshFileName>("file", "The name of the mesh file to read");
31  params.addClassDescription("Read a mesh from a file.");
32  return params;
33 }
34 
36  : MooseMesh(parameters), _file_name(getParam<MeshFileName>("file"))
37 {
38  getMesh().set_mesh_dimension(getParam<MooseEnum>("dim"));
39 }
40 
41 FileMesh::FileMesh(const FileMesh & other_mesh)
42  : MooseMesh(other_mesh), _file_name(other_mesh._file_name)
43 {
44 }
45 
47 
48 MooseMesh &
50 {
51  return *(new FileMesh(*this));
52 }
53 
54 void
56 {
57  std::string _file_name = getParam<MeshFileName>("file");
58 
59  Moose::perf_log.push("Read Mesh", "Setup");
60  if (_is_nemesis)
61  {
62  // Nemesis_IO only takes a reference to DistributedMesh, so we can't be quite so short here.
63  DistributedMesh & pmesh = cast_ref<DistributedMesh &>(getMesh());
64  Nemesis_IO(pmesh).read(_file_name);
65 
66  getMesh().allow_renumbering(false);
67 
68  // Even if we want repartitioning when load balancing later, we'll
69  // begin with the default partitioning defined by the Nemesis
70  // file.
71  bool skip_partitioning_later = getMesh().skip_partitioning();
72  getMesh().skip_partitioning(true);
73  getMesh().prepare_for_use();
74  getMesh().skip_partitioning(skip_partitioning_later);
75  }
76  else // not reading Nemesis files
77  {
78  // See if the user has requested reading a solution from the file. If so, we'll need to read
79  // the mesh with the exodus reader instead of using mesh.read(). This will read the mesh on
80  // every processor
81 
82  if (_app.setFileRestart() && (_file_name.rfind(".exd") < _file_name.size() ||
83  _file_name.rfind(".e") < _file_name.size()))
84  {
86 
87  _exreader = libmesh_make_unique<ExodusII_IO>(getMesh());
88  _exreader->read(_file_name);
89 
90  getMesh().allow_renumbering(false);
91  getMesh().prepare_for_use();
92  }
93  else
94  {
95  auto slash_pos = _file_name.find_last_of("/");
96  auto path = _file_name.substr(0, slash_pos);
97  auto file = _file_name.substr(slash_pos + 1);
98 
99  bool restarting;
100  // If we are reading a mesh while restarting, then we might have
101  // a solution file that relies on that mesh partitioning and/or
102  // numbering. In that case, we need to turn off repartitioning
103  // and renumbering, at least at first.
104  if (file == "LATEST")
105  {
106  std::list<std::string> dir_list(1, path);
107  std::list<std::string> files = MooseUtils::getFilesInDirs(dir_list);
108 
109  // Fill in the name of the LATEST file so we can open it and read it.
110  _file_name = MooseUtils::getLatestMeshCheckpointFile(files);
111  restarting = true;
112  }
113  else
114  restarting = _file_name.rfind(".cpa") < _file_name.size() ||
115  _file_name.rfind(".cpr") < _file_name.size();
116 
117  const bool skip_partitioning_later = restarting && getMesh().skip_partitioning();
118  const bool allow_renumbering_later = restarting && getMesh().allow_renumbering();
119 
120  if (restarting)
121  {
122  getMesh().skip_partitioning(true);
123  getMesh().allow_renumbering(false);
124  }
125 
126  MooseUtils::checkFileReadable(_file_name);
127  getMesh().read(_file_name);
128 
129  if (restarting)
130  {
131  getMesh().allow_renumbering(allow_renumbering_later);
132  getMesh().skip_partitioning(skip_partitioning_later);
133  }
134  }
135  }
136 
137  Moose::perf_log.pop("Read Mesh", "Setup");
138 }
139 
140 void
141 FileMesh::read(const std::string & file_name)
142 {
143  if (dynamic_cast<DistributedMesh *>(&getMesh()) && !_is_nemesis)
144  getMesh().read(file_name, /*mesh_data=*/NULL, /*skip_renumber=*/false);
145  else
146  getMesh().read(file_name, /*mesh_data=*/NULL, /*skip_renumber=*/true);
147 }
bool _is_nemesis
True if a Nemesis Mesh was read in.
Definition: MooseMesh.h:886
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:271
virtual void buildMesh() override
Must be overridden by child classes.
Definition: FileMesh.C:55
std::string _file_name
the file_name from whence this mesh came
Definition: FileMesh.h:46
std::list< std::string > getFilesInDirs(const std::list< std::string > &directory_list)
Retrieves the names of all of the files contained within the list of directories passed into the rout...
Definition: MooseUtils.C:466
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
virtual ~FileMesh()
Definition: FileMesh.C:46
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 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:121
InputParameters validParams< MooseMesh >()
Definition: MooseMesh.C:65
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:2408
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:74
PerfLog perf_log
Perflog to be used by applications.
std::string getLatestMeshCheckpointFile(const std::list< std::string > &checkpoint_files)
Returns the most recent checkpoint or mesh file given a list of files.
Definition: MooseUtils.C:495
FileMesh(const InputParameters &parameters)
Definition: FileMesh.C:35
void read(const std::string &file_name)
Definition: FileMesh.C:141
virtual MooseMesh & clone() const override
Clone method.
Definition: FileMesh.C:49
MooseApp & _app
The MooseApp this object is associated with.
Definition: MooseObject.h:108
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...
std::unique_ptr< ExodusII_IO > _exreader
Auxiliary object for restart.
Definition: FileMesh.h:48
InputParameters validParams< FileMesh >()
Definition: FileMesh.C:27