From 3f4ad215c0bf44fafe64c204a302962e545b647e Mon Sep 17 00:00:00 2001
From: Benjamin Faigle <benjamin.faigle@posteo.de>
Date: Tue, 11 Oct 2011 12:54:41 +0000
Subject: [PATCH] changes in restart behaviour: - restart mechanism is now
 called from init(..) method in time manager for all models via
 problem().restart(...); - hence, calls to problem or models in application
 files and in the default start procedure are deleted (included in
 timeManager) - timeManager().init(...) now has a real restart bool defining
 if a restart was called by the user. Bool is true if restart is desired. -
 Decoupled models: to enable restart, the writer is only called with the
 access function to avoid segmentation fault. - Adapt tutorial to the change

git-svn-id: svn://svn.iws.uni-stuttgart.de/DUMUX/dumux/trunk@6737 2fb0f335-1f38-0410-981e-8018bf24f1b0
---
 doc/handbook/tutorial-coupled.tex             |  6 +++--
 doc/handbook/tutorial-decoupled.tex           | 13 +++++-----
 dumux/common/start.hh                         | 24 +++++++------------
 dumux/common/timemanager.hh                   | 14 +++++++----
 dumux/decoupled/common/impetproblem.hh        | 12 ++++++----
 test/boxmodels/2p/test_2p.cc                  |  8 +++----
 test/boxmodels/richards/test_richards.cc      |  8 +++----
 .../generalproblem/test_generalproblem_2p.cc  |  5 ++--
 test/decoupled/2p/test_impes.cc               |  7 ++----
 test/decoupled/2p/test_transport.cc           |  6 ++---
 test/decoupled/2p2c/test_dec2p2c.cc           | 10 +++-----
 test/decoupled/2p2c/test_multiphysics2p2c.cc  | 10 +++-----
 .../2padaptive/test_impes_adaptive.cc         | 10 +++-----
 tutorial/tutorial_coupled.cc                  | 11 ++++-----
 tutorial/tutorial_decoupled.cc                | 12 ++++------
 15 files changed, 66 insertions(+), 90 deletions(-)

diff --git a/doc/handbook/tutorial-coupled.tex b/doc/handbook/tutorial-coupled.tex
index bc91752fe8..37aa108afe 100644
--- a/doc/handbook/tutorial-coupled.tex
+++ b/doc/handbook/tutorial-coupled.tex
@@ -209,8 +209,10 @@ parse the time when the simulation ends and the initial time step size.
 After this, a grid is created in line
 \ref{tutorial-coupled:create-grid} and the problem is instantiated for
 its leaf grid view in line \ref{tutorial-coupled:instantiate-problem}.
-Finally, on line \ref{tutorial-coupled:begin-restart} a state written to
-disk by a previous simulation run is restored if requested by the user.
+Finally, on line \ref{tutorial-coupled:initTimeManager} the time
+manager is created with the parsed starting parameters. If requested by
+the user, a state written to disk by a previous simulation run can be 
+restored if via the restart flag.
 The simulation procedure is started in line
 \ref{tutorial-coupled:execute}.
 
diff --git a/doc/handbook/tutorial-decoupled.tex b/doc/handbook/tutorial-decoupled.tex
index 1d1f762ef0..ffab4f3c6d 100644
--- a/doc/handbook/tutorial-decoupled.tex
+++ b/doc/handbook/tutorial-decoupled.tex
@@ -70,13 +70,14 @@ parse the time when the simulation ends. As the maximum time-step in the
 sequential model is strictly bound by a CFL-criterion, the first time-step
 size is initialized with the simulation time.
 
-After this, a grid is created in line \ref{tutorial-decoupled:create-grid} 
-and the time manager controlling the simulation run is instantiated 
-with the start parameters in line \ref{tutorial-decoupled:initTimeManager}.
+After this, a grid is created in line \ref{tutorial-decoupled:create-grid}. 
 The problem is instantiated with the time manager and information about the grid
 (via its leaf grid view) on line \ref{tutorial-decoupled:instantiate-problem}.
-If demanded, on line \ref{tutorial-decoupled:mainRestart} a state written to
-disk by a previous simulation run is restored on request by the user.
+The time manager controlling the simulation run is instantiated 
+with the start parameters in line \ref{tutorial-decoupled:initTimeManager}.
+If demanded, the time manager also restarts a state written to
+disk by a previous simulation run via the last flag, using the appropriate 
+starting time (which has to be the same as the restart file).
 Finally, the simulation proceedure is started by the time manager in line
 \ref{tutorial-decoupled:execute}.
 
@@ -150,7 +151,7 @@ subsection \label{decoupled-problem:boundary}), the problem class also contains
 general information about the current simulation. First, the name used by
 the \texttt{VTK-writer} to generate output is defined in the method of line
 \ref{tutorial-decoupled:name}, and line \ref{tutorial-decoupled:restart} indicates
-weather restart files are written. As decoupled schemes usually feature small 
+wether restart files are written. As decoupled schemes usually feature small 
 timesteps, the method controlling the output in line \ref{tutorial-decoupled:output}
 is very useful. The divisor of the modulo operation defines after how many timesteps
 output should be written out -- the default ``1'' resembles output after each 
diff --git a/dumux/common/start.hh b/dumux/common/start.hh
index ff60b2e846..13de33e3c4 100644
--- a/dumux/common/start.hh
+++ b/dumux/common/start.hh
@@ -114,12 +114,12 @@ int startFromDGF(int argc, char **argv)
         // deal with the restart stuff
         int argIdx = 1;
         bool restart = false;
-        double restartTime = 0;
+        double startTime = 0;
         if (std::string("--restart") == argv[argIdx]) {
             restart = true;
             ++argIdx;
 
-            std::istringstream(argv[argIdx++]) >> restartTime;
+            std::istringstream(argv[argIdx++]) >> startTime;
         }
 
         if (argc - argIdx != 3) {
@@ -145,13 +145,11 @@ int startFromDGF(int argc, char **argv)
         // instantiate and run the concrete problem
         TimeManager timeManager;
         Problem problem(timeManager, gridPtr->leafView());
-        timeManager.init(problem, 0, dt, tEnd, !restart);
+        timeManager.init(problem, startTime, dt, tEnd, restart);
 
         // print all properties
         Dumux::Properties::print<TypeTag>();
 
-        if (restart)
-            problem.restart(restartTime);
         timeManager.run();
         return 0;
 
@@ -200,12 +198,12 @@ int startWithGrid(const typename GET_PROP_TYPE(TypeTag, PTAG(Grid)) &grid,
         // deal with the restart stuff
         int argIdx = 1;
         bool restart = false;
-        double restartTime = 0;
+        double startTime = 0;
         if (std::string("--restart") == argv[argIdx]) {
             restart = true;
             ++argIdx;
 
-            std::istringstream(argv[argIdx++]) >> restartTime;
+            std::istringstream(argv[argIdx++]) >> startTime;
         }
 
         if (argc - argIdx != 2) {
@@ -219,13 +217,11 @@ int startWithGrid(const typename GET_PROP_TYPE(TypeTag, PTAG(Grid)) &grid,
         // instantiate and run the concrete problem
         TimeManager timeManager;
         Problem problem(timeManager, grid.leafView());
-        timeManager.init(problem, 0, dt, tEnd, !restart);
+        timeManager.init(problem, startTime, dt, tEnd, restart);
 
         // print all properties
         Dumux::Properties::print<TypeTag>();
 
-        if (restart)
-            problem.restart(restartTime);
         timeManager.run();
         return 0;
 
@@ -277,12 +273,12 @@ int startFromInputFile(int argc, char **argv)
        // deal with the restart stuff
        int argIdx = 1;
        bool restart = false;
-       double restartTime = 0;
+       double startTime = 0;
        if (std::string("--restart") == argv[argIdx]) {
            restart = true;
            ++argIdx;
 
-           std::istringstream(argv[argIdx++]) >> restartTime;
+           std::istringstream(argv[argIdx++]) >> startTime;
        }
 
        if (argc - argIdx != 1) {
@@ -330,13 +326,11 @@ int startFromInputFile(int argc, char **argv)
        Problem problem(timeManager,
                gridPtr->leafView(),
                Params::tree());
-       timeManager.init(problem, 0, dt, tEnd, !restart);
+       timeManager.init(problem, startTime, dt, tEnd, restart);
        
        // print all properties
        Dumux::Properties::print<TypeTag>();
 
-       if (restart)
-           problem.restart(restartTime);
        timeManager.run();
        return 0;
 
diff --git a/dumux/common/timemanager.hh b/dumux/common/timemanager.hh
index 1bbf74c580..6494086995 100644
--- a/dumux/common/timemanager.hh
+++ b/dumux/common/timemanager.hh
@@ -100,14 +100,13 @@ public:
      * \param tStart The start time \f$\mathrm{[s]}\f$ of the simulation (typically 0)
      * \param dtInitial The initial time step size \f$\mathrm{[s]}\f$
      * \param tEnd The time at which the simulation is finished \f$\mathrm{[s]}\f$
-     * \param writeInitialSol Specifies whether the initial condition
-     *                        should be written to disk
+     * \param restart Specifies whether the initial condition should be written to disk
      */
     void init(Problem &problem,
               Scalar tStart,
               Scalar dtInitial,
               Scalar tEnd,
-              bool writeInitialSol = true)
+              bool restart = false)
     {
         problem_ = &problem;
         time_ = tStart;
@@ -120,12 +119,17 @@ public:
         // initialize the problem
         problem_->init();
 
-        // write initial condition (if requested)
-        if (writeInitialSol) {
+        // restart problem if necessary
+        if(restart)
+            problem_->restart(tStart);
+        else
+        {
+            // write initial condition (if problem is not restarted)
             time_ -= timeStepSize_;
             problem_->writeOutput();
             time_ += timeStepSize_;
         }
+
     }
 
     /*!
diff --git a/dumux/decoupled/common/impetproblem.hh b/dumux/decoupled/common/impetproblem.hh
index fd87754aa0..169324822e 100644
--- a/dumux/decoupled/common/impetproblem.hh
+++ b/dumux/decoupled/common/impetproblem.hh
@@ -733,7 +733,7 @@ public:
         std::cerr << "Serialize to file " << res.fileName() << "\n";
 
         timeManager().serialize(res);
-        resultWriter_->serialize(res);
+        resultWriter().serialize(res);
         model().serialize(res);
 
         res.serializeEnd();
@@ -744,21 +744,25 @@ public:
      *        from disk.
      *
      * It is the inverse of the serialize() method.
+     * @param tRestart Restart time
      */
-    void deserialize(double t)
+    void restart(double tRestart)
     {
         typedef Dumux::Restart Restarter;
 
         Restarter res;
-        res.deserializeBegin(asImp_(), t);
+        res.deserializeBegin(asImp_(), tRestart);
         std::cerr << "Deserialize from file " << res.fileName() << "\n";
 
         timeManager().deserialize(res);
-        resultWriter_->deserialize(res);
+        resultWriter().deserialize(res);
         model().deserialize(res);
 
         res.deserializeEnd();
     };
+    //! Use restart() function instead!
+    void deserialize(double tRestart) DUNE_DEPRECATED
+    { restart(tRestart);}
     // \}
 
     void addOutputVtkFields()
diff --git a/test/boxmodels/2p/test_2p.cc b/test/boxmodels/2p/test_2p.cc
index 871ff00b33..b9c591f8a7 100644
--- a/test/boxmodels/2p/test_2p.cc
+++ b/test/boxmodels/2p/test_2p.cc
@@ -175,12 +175,12 @@ int main(int argc, char** argv)
         // deal with the restart stuff
         int argPos = 1;
         bool restart = false;
-        double restartTime = 0;
+        double startTime = 0;
         if (std::string("--restart") == argv[argPos]) {
             restart = true;
             ++argPos;
 
-            std::istringstream(argv[argPos++]) >> restartTime;
+            std::istringstream(argv[argPos++]) >> startTime;
         }
 
         if (argc - argPos != 2) {
@@ -246,9 +246,7 @@ int main(int argc, char** argv)
         // instantiate and run the concrete problem
         TimeManager timeManager;
         Problem problem(timeManager, grid->leafView(), lowerLeftLens, upperRightLens);
-        timeManager.init(problem, 0, dt, tEnd, !restart);
-        if (restart)
-            problem.restart(restartTime);
+        timeManager.init(problem, startTime, dt, tEnd, restart);
         timeManager.run();
         return 0;
 #ifdef NDEBUG
diff --git a/test/boxmodels/richards/test_richards.cc b/test/boxmodels/richards/test_richards.cc
index 974e8c1607..e835b5372d 100644
--- a/test/boxmodels/richards/test_richards.cc
+++ b/test/boxmodels/richards/test_richards.cc
@@ -62,12 +62,12 @@ int main(int argc, char** argv)
         // deal with the restart stuff
         int argPos = 1;
         bool restart = false;
-        double restartTime = 0;
+        double startTime = 0;
         if (std::string("--restart") == argv[argPos]) {
             restart = true;
             ++argPos;
 
-            std::istringstream(argv[argPos++]) >> restartTime;
+            std::istringstream(argv[argPos++]) >> startTime;
         }
 
         if (argc - argPos != 3) {
@@ -97,9 +97,7 @@ int main(int argc, char** argv)
         // instantiate and run the concrete problem
         TimeManager timeManager;
         Problem problem(timeManager, gridPtr->leafView(), lowerLeftLens, upperRightLens);
-        timeManager.init(problem, 0, dt, tEnd, !restart);
-        if (restart)
-            problem.restart(restartTime);
+        timeManager.init(problem, startTime, dt, tEnd, restart);
         timeManager.run();
         return 0;
     }
diff --git a/test/common/generalproblem/test_generalproblem_2p.cc b/test/common/generalproblem/test_generalproblem_2p.cc
index 0538a32324..30048bf679 100644
--- a/test/common/generalproblem/test_generalproblem_2p.cc
+++ b/test/common/generalproblem/test_generalproblem_2p.cc
@@ -74,7 +74,6 @@ int main(int argc, char** argv)
         if (argc < 4)
             usage(argv[0]);
 
-        // deal with the restart stuff
         int argPos = 1;
         bool useBoxModel = false;
         bool useDecoupledModel = false;
@@ -133,7 +132,7 @@ int main(int argc, char** argv)
             BoxTimeManager timeManager;
             Dumux::GeneralLensProblem<BoxTypeTag> problem(timeManager, grid->leafView(), lowerLeftLens, upperRightLens);
             problem.setName("generallens_box");
-            timeManager.init(problem, 0, dt, tEnd, true);
+            timeManager.init(problem, 0, dt, tEnd, false);
             timeManager.run();
             return 0;
         }
@@ -142,7 +141,7 @@ int main(int argc, char** argv)
             DecoupledTimeManager timeManager;
             Dumux::GeneralLensProblem<DecoupledTypeTag> problem(timeManager, grid->leafView(), lowerLeftLens, upperRightLens);
             problem.setName("generallens_decoupled");
-            timeManager.init(problem, 0, dt, tEnd, true);
+            timeManager.init(problem, 0, dt, tEnd, false);
             timeManager.run();
             return 0;
         }
diff --git a/test/decoupled/2p/test_impes.cc b/test/decoupled/2p/test_impes.cc
index e1acad1a05..9b66a14449 100644
--- a/test/decoupled/2p/test_impes.cc
+++ b/test/decoupled/2p/test_impes.cc
@@ -110,11 +110,8 @@ int main(int argc, char** argv)
         TimeManager timeManager;
         Problem problem(timeManager, grid.leafView());
 
-        // load restart file if necessarry
-        if (restart)
-            problem.deserialize(restartTime);
-
-        timeManager.init(problem, 0, dt, tEnd, !restart);
+        // use restart file if necessarry
+        timeManager.init(problem, restartTime, dt, tEnd, restart);
         timeManager.run();
         return 0;
     }
diff --git a/test/decoupled/2p/test_transport.cc b/test/decoupled/2p/test_transport.cc
index 90f19e76cc..f996ca14bc 100644
--- a/test/decoupled/2p/test_transport.cc
+++ b/test/decoupled/2p/test_transport.cc
@@ -97,10 +97,8 @@ int main(int argc, char** argv)
         TimeManager timeManager;
         Problem problem(timeManager, gridPtr->leafView());
 
-        // load restart file if necessarry
-        if (restart)
-            problem.deserialize(restartTime);
-        timeManager.init(problem, 0, dt, tEnd, !restart);
+        // use restart file if necessarry
+        timeManager.init(problem, restartTime, dt, tEnd, restart);
         timeManager.run();
 
         return 0;
diff --git a/test/decoupled/2p2c/test_dec2p2c.cc b/test/decoupled/2p2c/test_dec2p2c.cc
index 99fd5619a9..81dc53f827 100644
--- a/test/decoupled/2p2c/test_dec2p2c.cc
+++ b/test/decoupled/2p2c/test_dec2p2c.cc
@@ -68,7 +68,7 @@ int main(int argc, char** argv)
         // deal with the restart stuff
         int argPos = 1;
         bool restart = false;
-        double restartTime = 0;
+        double startTime = 0;
         // deal with start parameters
         double tEnd= 3e3;
         double firstDt = 200;
@@ -79,7 +79,7 @@ int main(int argc, char** argv)
                 restart = true;
                 ++argPos;
 
-                std::istringstream(argv[argPos++]) >> restartTime;
+                std::istringstream(argv[argPos++]) >> startTime;
             }
             if (argc - argPos == 2)
             {
@@ -116,12 +116,8 @@ int main(int argc, char** argv)
         TimeManager timeManager;
         Problem problem(timeManager, grid.leafView(), L, H);
 
-        // load restart file if necessarry
-        if (restart)
-            problem.deserialize(restartTime);
-
         // initialize the simulation
-        timeManager.init(problem, 0, firstDt, tEnd, !restart);
+        timeManager.init(problem, startTime, firstDt, tEnd, restart);
         // run the simulation
         timeManager.run();
         return 0;
diff --git a/test/decoupled/2p2c/test_multiphysics2p2c.cc b/test/decoupled/2p2c/test_multiphysics2p2c.cc
index 3c047a685b..ee49f5aeaf 100644
--- a/test/decoupled/2p2c/test_multiphysics2p2c.cc
+++ b/test/decoupled/2p2c/test_multiphysics2p2c.cc
@@ -67,7 +67,7 @@ int main(int argc, char** argv)
         // deal with the restart stuff
         int argPos = 1;
         bool restart = false;
-        double restartTime = 0;
+        double startTime = 0;
         // deal with start parameters
         double tEnd= 3e3;
         double firstDt = 200;
@@ -78,7 +78,7 @@ int main(int argc, char** argv)
                 restart = true;
                 ++argPos;
 
-                std::istringstream(argv[argPos++]) >> restartTime;
+                std::istringstream(argv[argPos++]) >> startTime;
             }
             if (argc - argPos == 2)
             {
@@ -109,12 +109,8 @@ int main(int argc, char** argv)
         TimeManager timeManager;
         Problem problem(timeManager, grid.leafView(), L, H);
 
-        // load restart file if necessarry
-        if (restart)
-            problem.deserialize(restartTime);
-
         // initialize the simulation
-        timeManager.init(problem, 0, firstDt, tEnd, !restart);
+        timeManager.init(problem, startTime, firstDt, tEnd, !restart);
         // run the simulation
         timeManager.run();
         return 0;
diff --git a/test/decoupled/2padaptive/test_impes_adaptive.cc b/test/decoupled/2padaptive/test_impes_adaptive.cc
index 64c2b9028d..d19d9c0d3b 100644
--- a/test/decoupled/2padaptive/test_impes_adaptive.cc
+++ b/test/decoupled/2padaptive/test_impes_adaptive.cc
@@ -77,12 +77,12 @@ int main(int argc, char** argv)
         // deal with the restart stuff
         int argPos = 1;
         bool restart = false;
-        double restartTime = 0;
+        double startTime = 0;
         if (std::string("--restart") == argv[argPos]) {
             restart = true;
             ++argPos;
 
-            std::istringstream(argv[argPos++]) >> restartTime;
+            std::istringstream(argv[argPos++]) >> startTime;
         }
 
         if (argc - argPos != 1) {
@@ -136,11 +136,7 @@ int main(int argc, char** argv)
         problem.setGrid(*gridPtr);
         problem.gridAdapt().setLevels(Params::tree().get<int>("levelMin"), Params::tree().get<int>("levelMax"));
 
-        // load restart file if necessarry
-        if (restart)
-            problem.deserialize(restartTime);
-
-        timeManager.init(problem, 0, dt, tEnd, !restart);
+        timeManager.init(problem, startTime, dt, tEnd, !restart);
         timeManager.run();
         return 0;
     }
diff --git a/tutorial/tutorial_coupled.cc b/tutorial/tutorial_coupled.cc
index a480304819..6180582823 100644
--- a/tutorial/tutorial_coupled.cc
+++ b/tutorial/tutorial_coupled.cc
@@ -54,12 +54,12 @@ int main(int argc, char** argv)
         // parse restart time if restart is requested
         int argPos = 1;
         bool restart = false;
-        double restartTime = 0;
+        double startTime = 0;
         if (std::string("--restart") == argv[argPos]) {
             restart = true;
             ++argPos;
-
-            std::istringstream(argv[argPos++]) >> restartTime;
+            // use restart time as start time
+            std::istringstream(argv[argPos++]) >> startTime;
         }
 
         // read the initial time step and the end time
@@ -78,10 +78,7 @@ int main(int argc, char** argv)
 
         // instantiate the problem on the leaf grid
         Problem problem(timeManager, gridPtr->leafView()); /*@\label{tutorial-coupled:instantiate-problem}@*/
-        timeManager.init(problem, 0, dt, tEnd, !restart);
-        // load some previously saved state from disk
-        if (restart)
-            problem.restart(restartTime); /*@\label{tutorial-coupled:begin-restart}@*/
+        timeManager.init(problem, startTime, dt, tEnd, restart); /*@\label{tutorial-coupled:initTimeManager}@*/
         // run the simulation
         timeManager.run(); /*@\label{tutorial-coupled:execute}@*/
         return 0;
diff --git a/tutorial/tutorial_decoupled.cc b/tutorial/tutorial_decoupled.cc
index 68eab2cd96..0a90347992 100644
--- a/tutorial/tutorial_decoupled.cc
+++ b/tutorial/tutorial_decoupled.cc
@@ -71,12 +71,12 @@ int main(int argc, char** argv)
         // deal with the restart stuff
         int argPos = 1;
         bool restart = false;
-        double restartTime = 0;
+        double startTime = 0.;
         if (std::string("--restart") == argv[argPos]) {
             restart = true;
             ++argPos;
-
-            std::istringstream(argv[argPos++]) >> restartTime;
+            // use restart time as start time
+            std::istringstream(argv[argPos++]) >> startTime;
         }
         // output in case of wrong numbers of input parameters
         if (argc - argPos != 1) {
@@ -100,11 +100,7 @@ int main(int argc, char** argv)
         Problem problem(timeManager, gridPtr->leafView()); /*@\label{tutorial-decoupled:instantiate-problem}@*/
 
         // define simulation parameters
-        timeManager.init(problem, 0, dt, tEnd, !restart); /*@\label{tutorial-decoupled:initTimeManager}@*/
-
-        // load restart file if necessary
-        if (restart)    /*@\label{tutorial-decoupled:mainRestart}@*/
-            problem.deserialize(restartTime);
+        timeManager.init(problem, startTime, dt, tEnd, restart); /*@\label{tutorial-decoupled:initTimeManager}@*/
 
         // run the simulation
         timeManager.run();    /*@\label{tutorial-decoupled:execute}@*/
-- 
GitLab