MercuryDPM  Alpha
 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-2014, 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 #include "Mercury3D.h"
30 #include <sys/time.h>
31 #include <string.h>
32 
38 {
39 public:
40 
41  Mercury3DRestart() //: Mercury3D()
42  {
44  maxWallTime_ = 10;
45  clusterCommand_ = "";
46  }
47 
48  void setClusterCommand(std::string clusterCommand)
49  {
50  clusterCommand_ = clusterCommand;
51  }
52 
53  std::string getClusterCommand() const
54  {
55  return clusterCommand_;
56  }
57 
58  void setMaxWallTime(Mdouble maxWallTime)
59  {
60  maxWallTime_ = maxWallTime;
61  }
62 
64  {
65  return maxWallTime_;
66  }
67 
69  {
70  return initialWallTime_;
71  }
72 
76  int readNextArgument(int& i, int argc, char *argv[])
77  {
78  if (!strcmp(argv[i], "-restart") || !strcmp(argv[i], "-r"))
79  {
80  setName(argv[i + 1]);
81 
82  //if a restart file is given
83  std::size_t found = getName().find(".restart");
84  if (found==std::string::npos) {
85  std::cout << "Reading file " << restartFile.getName() << std::endl;
87  } else {
88  std::cout << "Reading file " << argv[i + 1] << std::endl;
89  readRestartFile(argv[i + 1]);
90  //setName(getName().substr(0,found));
91  std::cout << "Read file " << getName() << std::endl;
92  }
93  std::cout << "tmax= " << getTimeMax() << std::endl;
94  restartFile.getFstream().precision(18);
95  setAppend(true);
96  printTime();
97  }
98  else
99  {
100  return Mercury3D::readNextArgument(i, argc, argv);
101  }
102  return true;
103  }
104 
107  double getWallTime() const
108  {
109  struct timeval time;
110  if (gettimeofday(&time, NULL))
111  {
112  std::cerr << "Error in getWallTime: Wall time could not be read" << std::endl;
113  return 0;
114  }
115  return (double) time.tv_sec + (double) time.tv_usec * .000001;
116  }
117 
118 private:
119 
120  /* writeOutputFiles is modified to force a restart when maxWallTime_ is
121  * reached. This is done by resetting the final simulation time to the
122  * current time.
123  */
125  {
127 
129  {
132  closeFiles();
133  std::cout << "Exiting for restarting after "
134  << getWallTime() - initialWallTime_ << "s" << std::endl;
135 
136  //set the restart command
137  std::stringstream com("");
138  //check if filename contaion a dot
139  //this is so the code works for autonumbered files
140  std::size_t found = getName().find('.');
141  if (found==std::string::npos) {
142  com << clusterCommand_ << " ./" << getName() << " -r " << getName();
143  } else {
144  com << clusterCommand_ << " ./" << getName().substr (0,found) << " -r " << getName();
145  }
146  //std::cout << com << std::endl;
147  std::cout << "system output:" << system(com.str().c_str()) << std::endl;
148  exit(0);
149  }
150  }
151 
155  std::string clusterCommand_;
156 
160  double maxWallTime_;
161 
166 
167 };
168 #endif
std::string clusterCommand_
Command required to execute code on a cluster; for einder, the command is ~/bin/sclusterscriptexecute...
bool readRestartFile(bool restarted=true)
Reads all the particle data corresponding to the current saved time step. Which is what the restart f...
Definition: DPMBase.cc:1629
File restartFile
An instance of class File to handle in- and output into a .restart file.
Definition: Files.h:219
double Mdouble
void closeFiles()
Closes all files (ene, data, fstat, restart, stat) that were opened to read or write.
Definition: Files.cc:252
void setClusterCommand(std::string clusterCommand)
void setAppend(bool newAppendFlag)
Allows to set the append option.
Definition: DPMBase.cc:651
virtual void writeOutputFiles()
Writes the simulation data onto all the files i.e. .data, .ene, .fstat ...
Definition: DPMBase.cc:2139
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)
std::fstream & getFstream()
Allows to access the member variable File::fstream_.
Definition: File.cc:151
void setName(const std::string &name)
Allows to set the name of all the files (ene, data, fstat, restart, stat)
Definition: Files.cc:150
This adds on the hierarchical grid code for 3D problems.
Definition: Mercury3D.h:35
bool readNextArgument(int &i, int argc, char *argv[]) override
Reads the next command line argument.
Definition: MercuryBase.cc:419
Mdouble getInitialWallTime() const
virtual void actionsAfterSolve()
A virtual function which allows to define operations to be executed after the solve().
Definition: DPMBase.cc:786
std::string getClusterCommand() const
virtual void printTime() const
Displays the current simulation time onto your screen for example.
Definition: DPMBase.cc:886
void writeOutputFiles()
Writes the simulation data onto all the files i.e. .data, .ene, .fstat ...
This class adds to Mercury3D the ability to restart after a certain wall time.
int readNextArgument(int &i, int argc, char *argv[])
the -r option is used to restart the code; this should probably be moved to DPMBase ...
double maxWallTime_
Time in seconds after which the code is automatically restarted for einder, this should be set to ~10...
virtual void finishStatistics()
no implementation but can be overidden in its derived classes.
Definition: DPMBase.cc:838
const std::string & getName() const
Returns the name of the file. Does not allow to change it though.
Definition: Files.cc:132
Mdouble getMaxWallTime() const
Mdouble getTimeMax() const
Allows the user to access the total simulation time during the simulation. Cannot change it though...
Definition: DPMBase.cc:214
const std::string & getName() const
Allows to access the file name, e.g., "problem.data".
Definition: File.cc:163