www.mooseframework.org
Checkpoint.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 // C POSIX includes
16 #include <sys/stat.h>
17 
18 // Moose includes
19 #include "Checkpoint.h"
20 #include "FEProblem.h"
21 #include "MooseApp.h"
23 #include "RestartableData.h"
24 #include "MooseMesh.h"
25 
26 #include "libmesh/checkpoint_io.h"
27 #include "libmesh/enum_xdr_mode.h"
28 
29 template <>
32 {
33  // Get the parameters from the base classes
35 
36  // Typical checkpoint options
37  params.addParam<unsigned int>("num_files", 2, "Number of the restart files to save");
38  params.addParam<std::string>(
39  "suffix",
40  "cp",
41  "This will be appended to the file_base to create the directory name for checkpoint files.");
42 
43  // Advanced settings
44  params.addParam<bool>("binary", true, "Toggle the output of binary files");
45  params.addParamNamesToGroup("binary", "Advanced");
46  return params;
47 }
48 
50  : FileOutput(parameters),
51  _num_files(getParam<unsigned int>("num_files")),
52  _suffix(getParam<std::string>("suffix")),
53  _binary(getParam<bool>("binary")),
54  _parallel_mesh(_problem_ptr->mesh().isDistributedMesh()),
55  _restartable_data(_app.getRestartableData()),
56  _recoverable_data(_app.getRecoverableData()),
57  _material_property_storage(_problem_ptr->getMaterialPropertyStorage()),
58  _bnd_material_property_storage(_problem_ptr->getBndMaterialPropertyStorage()),
59  _restartable_data_io(RestartableDataIO(*_problem_ptr))
60 {
61 }
62 
63 std::string
65 {
66  // Get the time step with correct zero padding
67  std::ostringstream output;
68  output << directory() << "/" << std::setw(_padding) << std::setprecision(0) << std::setfill('0')
69  << std::right << timeStep();
70  return output.str();
71 }
72 
73 std::string
75 {
76  return _file_base + "_" + _suffix;
77 }
78 
79 void
81 {
82  // Start the performance log
83  Moose::perf_log.push("Checkpoint::output()", "Output");
84 
85  // Create the output directory
86  std::string cp_dir = directory();
87  mkdir(cp_dir.c_str(), S_IRWXU | S_IRGRP);
88 
89  // Create the output filename
90  std::string current_file = filename();
91 
92  // Create the libMesh Checkpoint_IO object
93  MeshBase & mesh = _es_ptr->get_mesh();
94  CheckpointIO io(mesh, _binary);
95 
96  // Set libHilbert renumbering flag to false. We don't support
97  // N-to-M restarts regardless, and if we're *never* going to do
98  // N-to-M restarts then libHilbert is just unnecessary computation
99  // and communication.
100  const bool renumber = false;
101 
102  // Create checkpoint file structure
103  CheckpointFileNames current_file_struct;
104  if (_binary)
105  {
106  current_file_struct.checkpoint = current_file + "_mesh.cpr";
107  current_file_struct.system = current_file + ".xdr";
108  }
109  else
110  {
111  current_file_struct.checkpoint = current_file + "_mesh.cpa";
112  current_file_struct.system = current_file + ".xda";
113  }
114  current_file_struct.restart = current_file + ".rd";
115 
116  // Write the checkpoint file
117  io.write(current_file_struct.checkpoint);
118 
119  // Write the system data, using ENCODE vs WRITE based on xdr vs xda
120  _es_ptr->write(current_file_struct.system,
121  EquationSystems::WRITE_DATA | EquationSystems::WRITE_ADDITIONAL_DATA |
122  EquationSystems::WRITE_PARALLEL_FILES,
123  renumber);
124 
125  // Write the restartable data
127  current_file_struct.restart, _restartable_data, _recoverable_data);
128 
129  // Remove old checkpoint files
130  updateCheckpointFiles(current_file_struct);
131 
132  // Stop the logging
133  Moose::perf_log.pop("Checkpoint::output()", "Output");
134 }
135 
136 void
138 {
139  // Update the list of stored files
140  _file_names.push_back(file_struct);
141 
142  // Remove un-wanted files
143  if (_file_names.size() > _num_files)
144  {
145  // Extract the filenames to be removed
146  CheckpointFileNames delete_files = _file_names.front();
147 
148  // Remove these filenames from the list
149  _file_names.pop_front();
150 
151  // Get thread and proc information
152  processor_id_type proc_id = processor_id();
153 
154  // Delete checkpoint files (_mesh.cpr)
155  if (proc_id == 0)
156  {
157  std::ostringstream oss;
158  oss << delete_files.checkpoint;
159  std::string file_name = oss.str();
160  int ret = remove(file_name.c_str());
161  if (ret != 0)
162  mooseWarning("Error during the deletion of file '", file_name, "': ", std::strerror(ret));
163  }
164 
165  if (_parallel_mesh)
166  {
167  std::ostringstream oss;
168  oss << delete_files.checkpoint << '-' << n_processors() << '-' << proc_id;
169  std::string file_name = oss.str();
170  int ret = remove(file_name.c_str());
171  if (ret != 0)
172  mooseWarning("Error during the deletion of file '", file_name, "': ", std::strerror(ret));
173  }
174  else
175  {
176  if (proc_id == 0)
177  {
178  std::ostringstream oss;
179  oss << delete_files.checkpoint << "-1-0";
180  std::string file_name = oss.str();
181  int ret = remove(file_name.c_str());
182  if (ret != 0)
183  mooseWarning("Error during the deletion of file '", file_name, "': ", std::strerror(ret));
184  }
185  }
186 
187  // Delete the system files (xdr and xdr.0000, ...)
188  if (proc_id == 0)
189  {
190  std::ostringstream oss;
191  oss << delete_files.system;
192  std::string file_name = oss.str();
193  int ret = remove(file_name.c_str());
194  if (ret != 0)
195  mooseWarning("Error during the deletion of file '", file_name, "': ", std::strerror(ret));
196  }
197  {
198  std::ostringstream oss;
199  oss << delete_files.system << "." << std::setw(4) << std::setprecision(0) << std::setfill('0')
200  << proc_id;
201  std::string file_name = oss.str();
202  int ret = remove(file_name.c_str());
203  if (ret != 0)
204  mooseWarning("Error during the deletion of file '", file_name, "': ", std::strerror(ret));
205  }
206 
207  unsigned int n_threads = libMesh::n_threads();
208 
209  // Remove the restart files (rd)
210  {
211  for (THREAD_ID tid = 0; tid < n_threads; tid++)
212  {
213  std::ostringstream oss;
214  oss << delete_files.restart << "-" << proc_id;
215  if (n_threads > 1)
216  oss << "-" << tid;
217  std::string file_name = oss.str();
218  int ret = remove(file_name.c_str());
219  if (ret != 0)
220  mooseWarning("Error during the deletion of file '", file_name, "': ", std::strerror(ret));
221  }
222  }
223  }
224 }
void mooseWarning(Args &&...args) const
Definition: MooseObject.h:89
Checkpoint(const InputParameters &parameters)
Class constructor.
Definition: Checkpoint.C:49
A structure for storing the various output files associated with checkpoint output.
Definition: Checkpoint.h:34
void writeRestartableData(std::string base_file_name, const RestartableDatas &restartable_datas, std::set< std::string > &_recoverable_data)
Write out the restartable data.
Class for doing restart.
RestartableDataIO _restartable_data_io
RestrableData input/output interface.
Definition: Checkpoint.h:104
virtual void output(const ExecFlagType &type) override
Outputs a checkpoint file.
Definition: Checkpoint.C:80
std::string checkpoint
Filename for CheckpointIO file.
Definition: Checkpoint.h:37
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
std::string _file_base
The base filename from the input paramaters.
Definition: FileOutput.h:87
void updateCheckpointFiles(CheckpointFileNames file_struct)
Definition: Checkpoint.C:137
const std::string _suffix
Directory suffix.
Definition: Checkpoint.h:83
std::string system
Filename for EquationsSystems::write.
Definition: Checkpoint.h:40
unsigned int _padding
Number of digits to pad the extensions.
Definition: FileOutput.h:93
virtual std::string filename() override
Returns the base filename for the checkpoint files.
Definition: Checkpoint.C:64
EquationSystems * _es_ptr
Reference the the libMesh::EquationSystems object that contains the data.
Definition: Output.h:180
bool _parallel_mesh
True if running with parallel mesh.
Definition: Checkpoint.h:89
std::string restart
Filename for restartable data filename.
Definition: Checkpoint.h:43
PerfLog perf_log
Perflog to be used by applications.
bool _binary
True if outputing checkpoint files in binary format.
Definition: Checkpoint.h:86
InputParameters validParams< Checkpoint >()
Definition: Checkpoint.C:31
std::set< std::string > & _recoverable_data
Reference to the recoverable data.
Definition: Checkpoint.h:95
std::string directory()
Retrieve the checkpoint output directory.
Definition: Checkpoint.C:74
const RestartableDatas & _restartable_data
Reference to the restartable data.
Definition: Checkpoint.h:92
std::deque< CheckpointFileNames > _file_names
Vector of checkpoint filename structures.
Definition: Checkpoint.h:107
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...
InputParameters validParams< FileOutput >()
Definition: FileOutput.C:28
ExecFlagType
Execution flags - when is the object executed/evaluated.
Definition: MooseTypes.h:90
An outputter with filename support.
Definition: FileOutput.h:32
virtual int timeStep()
Get the current time step.
Definition: Output.C:233
unsigned int THREAD_ID
Definition: MooseTypes.h:79
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...
unsigned int _num_files
Max no. of output files to store.
Definition: Checkpoint.h:80