MercuryDPM  Beta
 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  void setMaxWallTime(Mdouble maxWallTime)
54  {
55  maxWallTime_ = maxWallTime;
56  }
57 
59  {
60  return maxWallTime_;
61  }
62 
64  {
65  return initialWallTime_;
66  }
67 
71  int readNextArgument(int& i, int argc, char *argv[])
72  {
73  if (!strcmp(argv[i], "-restart") || !strcmp(argv[i], "-r"))
74  {
75  setName(argv[i + 1]);
76  std::cout << "Reading file " << restartFile.getName() << std::endl;
78  std::cout << "tmax= " << getTimeMax() << std::endl;
79  restartFile.getFstream().precision(18);
80  setAppend(true);
81  printTime();
82  }
83  else
84  {
85  return Mercury3D::readNextArgument(i, argc, argv);
86  }
87  return true;
88  }
89 
92  double getWallTime() const
93  {
94  struct timeval time;
95  if (gettimeofday(&time, NULL))
96  {
97  std::cerr << "Error in getWallTime: Wall time could not be read" << std::endl;
98  return 0;
99  }
100  return (double) time.tv_sec + (double) time.tv_usec * .000001;
101  }
102 
103 private:
104 
105  /* writeOutputFiles is modified to force a restart when maxWallTime_ is
106  * reached. This is done by resetting the final simulation time to the
107  * current time.
108  */
110  {
112 
114  {
117  closeFiles();
118  std::cout << "Exiting for restarting after "
119  << getWallTime() - initialWallTime_ << "s" << std::endl;
120 
121  //set the restart command
122  std::stringstream com("");
123  //check if filename contaion a dot
124  //this is so the code works for autonumbered files
125  std::size_t found = getName().find('.');
126  if (found==std::string::npos) {
127  com << clusterCommand_ << " ./" << getName() << " -r " << getName();
128  } else {
129  com << clusterCommand_ << " ./" << getName().substr (0,found) << " -r " << getName();
130  }
131  //std::cout << com << std::endl;
132  std::cout << "system output:" << system(com.str().c_str()) << std::endl;
133  exit(0);
134  }
135  }
136 
140  std::string clusterCommand_;
141 
145  double maxWallTime_;
146 
151 
152 };
153 #endif
std::string clusterCommand_
Command required to execute code on a cluster; for einder, the command is ~/bin/sclusterscriptexecute...
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:250
void setClusterCommand(std::string clusterCommand)
void setAppend(bool newAppendFlag)
Allows to set the append option.
Definition: DPMBase.cc:544
virtual void writeOutputFiles()
Writes the simulation data onto all the files i.e. .data, .ene, .fstat ...
Definition: DPMBase.cc:1848
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:150
int readRestartFile()
Reads all the particle data corresponding to the current saved time step. Which is what the restart f...
Definition: DPMBase.cc:1375
void setName(const std::string &name)
Allows to set the name of all the files (ene, data, fstat, restart, stat)
Definition: Files.cc:149
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:662
virtual void printTime() const
Displays the current simulation time onto your screen for example.
Definition: DPMBase.cc:758
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:710
const std::string & getName() const
Returns the name of the file. Does not allow to change it though.
Definition: Files.cc:131
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:194
const std::string & getName() const
Allows to access the file name, e.g., "problem.data".
Definition: File.cc:162