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