MercuryDPM  Trunk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
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  std::cout << "Reading file " << restartFile.getName() << std::endl;
89  }
90  else
91  {
92  std::cout << "Reading file " << argv[i + 1] << std::endl;
93  readRestartFile(argv[i + 1]);
94  //setName(getName().substr(0,found));
95  std::cout << "Read file " << getName() << std::endl;
96  }
97  std::cout << "tmax= " << getTimeMax() << std::endl;
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  std::cerr << "Error in getWallTime: Wall time could not be read" << std::endl;
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  std::cout << "Exiting for restarting after "
138  << getWallTime() - initialWallTime_ << "s" << std::endl;
139 
140  //set the restart command
141  std::stringstream com("");
142  //check if filename contaion a dot
143  //this is so the code works for autonumbered files
144  std::size_t found = getName().find('.');
145  if (found == std::string::npos)
146  {
147  com << clusterCommand_ << " ./" << getName() << " -r " << getName();
148  }
149  else
150  {
151  com << clusterCommand_ << " ./" << getName().substr(0, found) << " -r " << getName();
152  }
153  //std::cout << com << std::endl;
154  std::cout << "system output:" << system(com.str().c_str()) << std::endl;
155  exit(0);
156  }
157  }
158 
162  std::string clusterCommand_;
163 
167  double maxWallTime_;
168 
173 
174 };
175 
176 #endif
bool readNextArgument(int &i, int argc, char *argv[])
the -r option is used to restart the code; this should probably be moved to DPMBase ...
std::string clusterCommand_
Command required to execute code on a cluster; for einder, the command is ~/bin/sclusterscriptexecute...
double Mdouble
Definition: GeneralDefine.h:34
const std::complex< Mdouble > i
Definition: ExtendedMath.h:50
const std::string & getName() const
Returns the name of the file. Does not allow to change it though.
Definition: DPMBase.cc:389
void setClusterCommand(std::string clusterCommand)
void setAppend(bool newAppendFlag)
Sets whether the "append" option is on or off.
Definition: DPMBase.cc:1482
virtual void writeOutputFiles()
Writes simulation data to all the main Mercury files: .data, .ene, .fstat, .xballs and ...
Definition: DPMBase.cc:3846
double getWallTime() const
Returns wall time (uses sys command, thus might not work on Windows)
double initialWallTime_
Internal variable (i.e.
void setMaxWallTime(Mdouble maxWallTime)
This adds on the hierarchical grid code for 3D problems.
Definition: Mercury3D.h:36
bool readNextArgument(int &i, int argc, char *argv[]) override
Reads the next command line argument.
Definition: MercuryBase.cc:396
void closeFiles()
Closes all files (ene, data, fstat, restart, stat) that were opened to read or write.
Definition: DPMBase.cc:492
Mdouble getInitialWallTime() const
virtual void actionsAfterSolve()
A virtual function which allows to define operations to be executed after the solve().
Definition: DPMBase.cc:1831
std::string getClusterCommand() const
virtual void printTime() const
Displays the current simulation time and the maximum simulation duration.
Definition: DPMBase.cc:1930
void writeOutputFiles()
Writes simulation data to all the main Mercury files: .data, .ene, .fstat, .xballs and ...
void setName(const std::string &name)
Allows to set the name of all the files (ene, data, fstat, restart, stat)
Definition: DPMBase.cc:412
This class adds to Mercury3D the ability to restart after a certain wall time.
File restartFile
An instance of class File to handle in- and output into a .restart file.
Definition: DPMBase.h:1385
double maxWallTime_
Time in seconds after which the code is automatically restarted for einder, this should be set to ~10...
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:2896
virtual void finishStatistics()
Definition: DPMBase.cc:1885
Mdouble getMaxWallTime() const
Mdouble getTimeMax() const
Returns the maximum simulation duration.
Definition: DPMBase.cc:855
const std::string & getName() const
Allows to access the file name, e.g., "problem.data".
Definition: File.cc:166