revision: v0.14
Mercury3DRestart.h
Go to the documentation of this file.
1 //Copyright (c) 2013-2020, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 //based on /storage2/usr/people/sluding/MDCC/C3DshearXL30/MU0_LONG2
27 #ifndef MERCURY3DRESTART_H
28 #define MERCURY3DRESTART_H
29 
30 #include "Mercury3D.h"
31 #include <sys/time.h>
32 #include <string.h>
33 
38 class Mercury3DRestart : public Mercury3D
39 {
40 public:
41 
42  Mercury3DRestart() //: Mercury3D()
43  {
45  maxWallTime_ = 10;
46  clusterCommand_ = "";
47  }
48 
49  void setClusterCommand(std::string clusterCommand)
50  {
51  clusterCommand_ = clusterCommand;
52  }
53 
54  std::string getClusterCommand() const
55  {
56  return clusterCommand_;
57  }
58 
59  void setMaxWallTime(Mdouble maxWallTime)
60  {
61  maxWallTime_ = maxWallTime;
62  }
63 
65  {
66  return maxWallTime_;
67  }
68 
70  {
71  return initialWallTime_;
72  }
73 
77  bool readNextArgument(int& i, int argc, char* argv[])
78  {
79  if (!strcmp(argv[i], "-restart") || !strcmp(argv[i], "-r"))
80  {
81  setName(argv[i + 1]);
82 
83  //if a restart file is given
84  std::size_t found = getName().find(".restart");
85  if (found == std::string::npos)
86  {
87  logger(INFO, "Reading file %\n", Flusher::NO_FLUSH, restartFile.getName());
89  }
90  else
91  {
92  logger(INFO, "Reading file %\n", Flusher::NO_FLUSH, argv[i + 1]);
93  readRestartFile(argv[i + 1]);
94  //setName(getName().substr(0,found));
95  logger(INFO, "Read file %\n", Flusher::NO_FLUSH, getName());
96  }
97  logger(INFO, "tmax= %", getTimeMax());
98  //restartFile.getFstream().precision(18);
99  setAppend(true);
100  printTime();
101  }
102  else
103  {
104  return Mercury3D::readNextArgument(i, argc, argv);
105  }
106  return true;
107  }
108 
111  double getWallTime() const
112  {
113  struct timeval time;
114  if (gettimeofday(&time, NULL))
115  {
116  logger(WARN, "Error in getWallTime: Wall time could not be read");
117  return 0;
118  }
119  return (double) time.tv_sec + (double) time.tv_usec * .000001;
120  }
121 
122 private:
123 
124  /* writeOutputFiles is modified to force a restart when maxWallTime_ is
125  * reached. This is done by resetting the final simulation time to the
126  * current time.
127  */
129  {
131 
133  {
136  closeFiles();
137  logger(INFO, "Exiting for restarting after %s\n", Flusher::NO_FLUSH, getWallTime() - initialWallTime_);
138 
139  //set the restart command
140  std::stringstream com("");
141  //check if filename contaion a dot
142  //this is so the code works for autonumbered files
143  std::size_t found = getName().find('.');
144  if (found == std::string::npos)
145  {
146  com << clusterCommand_ << " ./" << getName() << " -r " << getName();
147  }
148  else
149  {
150  com << clusterCommand_ << " ./" << getName().substr(0, found) << " -r " << getName();
151  }
152  //std::cout << com << std::endl;
153  logger(INFO, "system output:%", system(com.str().c_str()));
154  exit(0);
155  }
156  }
157 
161  std::string clusterCommand_;
162 
166  double maxWallTime_;
167 
172 
173 };
174 
175 #endif
Mercury3DRestart::getMaxWallTime
Mdouble getMaxWallTime() const
Definition: Mercury3DRestart.h:64
Mercury3DRestarter::writeOutputFiles
void writeOutputFiles() override
Writes simulation data to all the main Mercury files: .data, .ene, .fstat, .xballs and ....
Definition: Mercury3DRestart.cpp:47
DPMBase::setName
void setName(const std::string &name)
Allows to set the name of all the files (ene, data, fstat, restart, stat)
Definition: DPMBase.cc:420
File::getName
const std::string & getName() const
Allows to access the file name, e.g., "problem.data".
Definition: File.cc:165
DPMBase::readRestartFile
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:2921
Mercury3DRestart::getClusterCommand
std::string getClusterCommand() const
Definition: Mercury3DRestart.h:54
Mercury3DRestarter
Definition: Mercury3DRestart.cpp:31
Flusher::FLUSH
@ FLUSH
logger
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
Mercury3D
This adds on the hierarchical grid code for 3D problems.
Definition: Mercury3D.h:37
Mercury3DRestart::writeOutputFiles
void writeOutputFiles()
Writes simulation data to all the main Mercury files: .data, .ene, .fstat, .xballs and ....
Definition: Mercury3DRestart.h:128
DPMBase::restartFile
File restartFile
An instance of class File to handle in- and output into a .restart file.
Definition: DPMBase.h:1451
DPMBase::closeFiles
void closeFiles()
Closes all files (ene, data, fstat, restart, stat) that were opened to read or write.
Definition: DPMBase.cc:500
INFO
LL< Log::INFO > INFO
Info log level.
Definition: Logger.cc:55
DPMBase::writeOutputFiles
virtual void writeOutputFiles()
Writes simulation data to all the main Mercury files: .data, .ene, .fstat, .xballs and ....
Definition: DPMBase.cc:3881
DPMBase::solve
void solve()
The work horse of the code.
Definition: DPMBase.cc:4003
Mercury3DRestart::Mercury3DRestart
Mercury3DRestart()
Definition: Mercury3DRestart.h:42
Mercury3DRestart::maxWallTime_
double maxWallTime_
Definition: Mercury3DRestart.h:166
Mercury3DRestart::readNextArgument
bool readNextArgument(int &i, int argc, char *argv[])
Definition: Mercury3DRestart.h:77
Mercury3DRestart.h
Mercury3DRestart::clusterCommand_
std::string clusterCommand_
Definition: Mercury3DRestart.h:161
double
ERROR
LL< Log::ERROR > ERROR
Error log level.
Definition: Logger.cc:53
WARN
LL< Log::WARN > WARN
Warning log level.
Definition: Logger.cc:54
main
int main(int argc, char *argv[])
Definition: Mercury3DRestart.cpp:69
DPMBase::getName
const std::string & getName() const
Returns the name of the file. Does not allow to change it though.
Definition: DPMBase.cc:397
constants::i
const std::complex< Mdouble > i
Definition: ExtendedMath.h:51
DPMBase::actionsAfterSolve
virtual void actionsAfterSolve()
A virtual function which allows to define operations to be executed after the solve().
Definition: DPMBase.cc:1862
Mercury3DRestart::time
double time
Definition: liveStatistics.cpp:36
Mercury3DRestart::initialWallTime_
double initialWallTime_
Definition: Mercury3DRestart.h:171
Mercury3DRestart::getWallTime
double getWallTime() const
Definition: Mercury3DRestart.h:111
Mercury3DRestart::setMaxWallTime
void setMaxWallTime(Mdouble maxWallTime)
Definition: Mercury3DRestart.h:59
DPMBase::finishStatistics
virtual void finishStatistics()
Definition: DPMBase.cc:1916
MercuryBase::readNextArgument
bool readNextArgument(int &i, int argc, char *argv[]) override
Reads the next command line argument.
Definition: MercuryBase.cc:402
Mercury3DRestart
Definition: liveStatistics.cpp:32
DPMBase::printTime
virtual void printTime() const
Displays the current simulation time and the maximum simulation duration.
Definition: DPMBase.cc:1961
Mercury3DRestart::getInitialWallTime
Mdouble getInitialWallTime() const
Definition: Mercury3DRestart.h:69
DPMBase::setSaveCount
void setSaveCount(unsigned int saveCount)
Sets File::saveCount_ for all files (ene, data, fstat, restart, stat)
Definition: DPMBase.cc:406
Mercury3DRestart::setClusterCommand
void setClusterCommand(std::string clusterCommand)
Definition: Mercury3DRestart.h:49
Mercury3D.h
DPMBase::getTimeMax
Mdouble getTimeMax() const
Returns the maximum simulation duration.
Definition: DPMBase.cc:885
DPMBase::setAppend
void setAppend(bool newAppendFlag)
Sets whether the "append" option is on or off.
Definition: DPMBase.cc:1513