Mercury3DRestart.h
Go to the documentation of this file.
1 // This file is part of the MercuryDPM project (https://www.mercurydpm.org).
2 // Copyright (c), The MercuryDPM Developers Team. All rights reserved.
3 // License: BSD 3-Clause License; see the LICENSE file in the root directory.
4 
5 //based on /storage2/usr/people/sluding/MDCC/C3DshearXL30/MU0_LONG2
6 #ifndef MERCURY3DRESTART_H
7 #define MERCURY3DRESTART_H
8 
9 #include "Mercury3D.h"
10 #include <sys/time.h>
11 #include <string.h>
12 
17 class Mercury3DRestart : public Mercury3D
18 {
19 public:
20 
21  Mercury3DRestart() //: Mercury3D()
22  {
24  maxWallTime_ = 10;
25  clusterCommand_ = "";
26  }
27 
28  void setClusterCommand(std::string clusterCommand)
29  {
30  clusterCommand_ = clusterCommand;
31  }
32 
34  {
35  return clusterCommand_;
36  }
37 
38  void setMaxWallTime(Mdouble maxWallTime)
39  {
40  maxWallTime_ = maxWallTime;
41  }
42 
44  {
45  return maxWallTime_;
46  }
47 
49  {
50  return initialWallTime_;
51  }
52 
56  bool readNextArgument(int& i, int argc, char* argv[]) override
57  {
58  if (!strcmp(argv[i], "-restart") || !strcmp(argv[i], "-r"))
59  {
60  setName(argv[i + 1]);
61 
62  //if a restart file is given
63  std::size_t found = getName().find(".restart");
64  if (found == std::string::npos)
65  {
66  logger(INFO, "Reading file %\n", Flusher::NO_FLUSH, restartFile.getName());
68  }
69  else
70  {
71  logger(INFO, "Reading file %\n", Flusher::NO_FLUSH, argv[i + 1]);
72  readRestartFile(argv[i + 1]);
73  //setName(getName().substr(0,found));
74  logger(INFO, "Read file %\n", Flusher::NO_FLUSH, getName());
75  }
76  logger(INFO, "tmax= %", getTimeMax());
77  //restartFile.getFstream().precision(18);
78  setAppend(true);
79  printTime();
80  }
81  else
82  {
83  return Mercury3D::readNextArgument(i, argc, argv);
84  }
85  return true;
86  }
87 
90  double getWallTime() const
91  {
92  struct timeval time;
93  if (gettimeofday(&time, NULL))
94  {
95  logger(WARN, "Error in getWallTime: Wall time could not be read");
96  return 0;
97  }
98  return (double) time.tv_sec + (double) time.tv_usec * .000001;
99  }
100 
101 private:
102 
103  /* writeOutputFiles is modified to force a restart when maxWallTime_ is
104  * reached. This is done by resetting the final simulation time to the
105  * current time.
106  */
107  void writeOutputFiles() override
108  {
110 
112  {
115  closeFiles();
116  logger(INFO, "Exiting for restarting after %s\n", Flusher::NO_FLUSH, getWallTime() - initialWallTime_);
117 
118  //set the restart command
119  std::stringstream com("");
120  //check if filename contaion a dot
121  //this is so the code works for autonumbered files
122  std::size_t found = getName().find('.');
123  if (found == std::string::npos)
124  {
125  com << clusterCommand_ << " ./" << getName() << " -r " << getName();
126  }
127  else
128  {
129  com << clusterCommand_ << " ./" << getName().substr(0, found) << " -r " << getName();
130  }
131  //std::cout << com << std::endl;
132  logger(INFO, "system output:%", system(com.str().c_str()));
133  exit(0);
134  }
135  }
136 
141 
145  double maxWallTime_;
146 
151 
152 };
153 
154 #endif
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Logger< MERCURYDPM_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
@ WARN
@ INFO
virtual void writeOutputFiles()
Writes simulation data to all the main Mercury files: .data, .ene, .fstat, .xballs and ....
Definition: DPMBase.cc:4115
void setName(const std::string &name)
Allows to set the name of all the files (ene, data, fstat, restart, stat)
Definition: DPMBase.cc:400
const std::string & getName() const
Returns the name of the file. Does not allow to change it though.
Definition: DPMBase.cc:377
File restartFile
An instance of class File to handle in- and output into a .restart file.
Definition: DPMBase.h:1499
virtual void printTime() const
Displays the current simulation time and the maximum simulation duration.
Definition: DPMBase.cc:1959
void closeFiles()
Closes all files (ene, data, fstat, restart, stat) that were opened to read or write.
Definition: DPMBase.cc:481
virtual void finishStatistics()
Definition: DPMBase.cc:1914
Mdouble getTimeMax() const
Returns the maximum simulation duration.
Definition: DPMBase.cc:879
virtual void actionsAfterSolve()
A virtual function which allows to define operations to be executed after the solve().
Definition: DPMBase.cc:1860
void setAppend(bool newAppendFlag)
Sets whether the "append" option is on or off.
Definition: DPMBase.cc:1513
bool readRestartFile(ReadOptions opt=ReadOptions::ReadAll)
Reads all the particle data corresponding to a given, existing . restart file (for more details regar...
Definition: DPMBase.cc:3043
const std::string & getName() const
Allows to access the file name, e.g., "problem.data".
Definition: File.cc:143
Definition: liveStatistics.cpp:11
void setMaxWallTime(Mdouble maxWallTime)
Definition: Mercury3DRestart.h:38
void writeOutputFiles() override
Writes simulation data to all the main Mercury files: .data, .ene, .fstat, .xballs and ....
Definition: Mercury3DRestart.h:107
double maxWallTime_
Definition: Mercury3DRestart.h:145
std::string getClusterCommand() const
Definition: Mercury3DRestart.h:33
std::string clusterCommand_
Definition: Mercury3DRestart.h:140
void setClusterCommand(std::string clusterCommand)
Definition: Mercury3DRestart.h:28
Mdouble getInitialWallTime() const
Definition: Mercury3DRestart.h:48
double time
Definition: liveStatistics.cpp:15
Mercury3DRestart()
Definition: Mercury3DRestart.h:21
double initialWallTime_
Definition: Mercury3DRestart.h:150
bool readNextArgument(int &i, int argc, char *argv[]) override
Definition: Mercury3DRestart.h:56
Mdouble getMaxWallTime() const
Definition: Mercury3DRestart.h:43
double getWallTime() const
Definition: Mercury3DRestart.h:90
This adds on the hierarchical grid code for 3D problems.
Definition: Mercury3D.h:16
bool readNextArgument(int &i, int argc, char *argv[]) override
Reads the next command line argument.
Definition: MercuryBase.cc:381
bool found
Definition: MergeRestartFiles.py:24
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286