From 5124461ef568e061cffb634a503b4b1323f45097 Mon Sep 17 00:00:00 2001
From: Kilian Weishaupt <kilian.weishaupt@iws.uni-stuttgart.de>
Date: Wed, 10 Feb 2016 16:07:29 +0100
Subject: [PATCH] [problem] Create restart files automatically

Write restart files just before the simulation stops
after the maximum number of time step divisions has been reached.
---
 dumux/implicit/problem.hh | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/dumux/implicit/problem.hh b/dumux/implicit/problem.hh
index 28ce5c6b31..f1da19bcdb 100644
--- a/dumux/implicit/problem.hh
+++ b/dumux/implicit/problem.hh
@@ -637,11 +637,29 @@ public:
                       << nextDt << " seconds\n";
         }
 
+        // if the simulation  run is about to abort, write restart files for the current and previous time steps:
+        // write restart file for the current time step
+        serialize();
+
+        //write restart file for the previous time step:
+        //set the time manager and the solution vector to the previous time step
+        const Scalar time = timeManager().time();
+        timeManager().setTime(time - timeManager().previousTimeStepSize());
+        const auto curSol = model_.curSol();
+        model_.curSol() = model_.prevSol();
+        //write restart file
+        serialize();
+        //reset time manager and solution vector
+        model_.curSol() = curSol;
+        timeManager().setTime(time);
+
         DUNE_THROW(Dune::MathError,
                    "Newton solver didn't converge after "
                    << maxFails
                    << " time-step divisions. dt="
-                   << timeManager().timeStepSize());
+                   << timeManager().timeStepSize()
+                   << ".\nThe solutions of the current and the previous time steps "
+                   << "have been saved to restart files.");
     }
 
     /*!
-- 
GitLab