From 0dd5c21b4ce5b459d4f9fa345ee5e9c01af29f39 Mon Sep 17 00:00:00 2001
From: Dmitri Naumov <github@naumov.de>
Date: Thu, 23 May 2019 00:01:50 +0200
Subject: [PATCH] [PL] TL; Extract initialization part of the loop.

---
 Applications/CLI/ogs.cpp |  1 +
 ProcessLib/TimeLoop.cpp  | 69 +++++++++++++++++++++-------------------
 ProcessLib/TimeLoop.h    |  1 +
 3 files changed, 39 insertions(+), 32 deletions(-)

diff --git a/Applications/CLI/ogs.cpp b/Applications/CLI/ogs.cpp
index 3dbb95bfb62..1970b221063 100644
--- a/Applications/CLI/ogs.cpp
+++ b/Applications/CLI/ogs.cpp
@@ -235,6 +235,7 @@ int main(int argc, char* argv[])
             INFO("Solve processes.");
 
             auto& time_loop = project.getTimeLoop();
+            time_loop.initialize();
             solver_succeeded = time_loop.loop();
 
 #ifdef USE_INSITU
diff --git a/ProcessLib/TimeLoop.cpp b/ProcessLib/TimeLoop.cpp
index d11763bfe17..8c1c1c019a3 100644
--- a/ProcessLib/TimeLoop.cpp
+++ b/ProcessLib/TimeLoop.cpp
@@ -379,44 +379,33 @@ double TimeLoop::computeTimeStepping(const double prev_dt, double& t,
     return dt;
 }
 
-/*
- * TODO:
- * Now we have a structure inside the time loop which is very similar to the
- * nonlinear solver. And admittedly, the control flow inside the nonlinear
- * solver is rather complicated. Maybe in the future con can introduce an
- * abstraction that can do both the convergence checks of the coupling loop and
- * of the nonlinear solver.
- */
-bool TimeLoop::loop()
+/// initialize output, convergence criterion, etc.
+void TimeLoop::initialize()
 {
-    // initialize output, convergence criterion, etc.
+    int process_id = 0;
+    for (auto& process_data : _per_process_data)
     {
-        int process_id = 0;
-        for (auto& process_data : _per_process_data)
-        {
-            auto& pcs = process_data->process;
-            _output->addProcess(pcs, process_id);
+        auto& pcs = process_data->process;
+        _output->addProcess(pcs, process_id);
 
-            process_data->process_id = process_id;
-            setTimeDiscretizedODESystem(*process_data);
+        process_data->process_id = process_id;
+        setTimeDiscretizedODESystem(*process_data);
 
-            if (auto* conv_crit =
-                    dynamic_cast<NumLib::ConvergenceCriterionPerComponent*>(
-                        process_data->conv_crit.get()))
-            {
-                conv_crit->setDOFTable(pcs.getDOFTable(process_id),
-                                       pcs.getMesh());
-            }
+        if (auto* conv_crit =
+                dynamic_cast<NumLib::ConvergenceCriterionPerComponent*>(
+                    process_data->conv_crit.get()))
+        {
+            conv_crit->setDOFTable(pcs.getDOFTable(process_id), pcs.getMesh());
+        }
 
-            // Add the fixed times of output to time stepper in order that
-            // the time stepping is performed and the results are output at
-            // these times. Note: only the adaptive time steppers can have the
-            // the fixed times.
-            auto& timestepper = process_data->timestepper;
-            timestepper->addFixedOutputTimes(_output->getFixedOutputTimes());
+        // Add the fixed times of output to time stepper in order that
+        // the time stepping is performed and the results are output at
+        // these times. Note: only the adaptive time steppers can have the
+        // the fixed times.
+        auto& timestepper = process_data->timestepper;
+        timestepper->addFixedOutputTimes(_output->getFixedOutputTimes());
 
-            ++process_id;
-        }
+        ++process_id;
     }
 
     // init solution storage
@@ -438,6 +427,22 @@ bool TimeLoop::loop()
                         _start_time, *_output, &Output::doOutput);
     }
 
+}
+
+/*
+ * TODO:
+ * Now we have a structure inside the time loop which is very similar to the
+ * nonlinear solver. And admittedly, the control flow inside the nonlinear
+ * solver is rather complicated. Maybe in the future con can introduce an
+ * abstraction that can do both the convergence checks of the coupling loop and
+ * of the nonlinear solver.
+ */
+bool TimeLoop::loop()
+{
+    // All _per_process_data share the first process.
+    bool const is_staggered_coupling =
+        !isMonolithicProcess(*_per_process_data[0]);
+
     double t = _start_time;
     std::size_t accepted_steps = 0;
     std::size_t rejected_steps = 0;
diff --git a/ProcessLib/TimeLoop.h b/ProcessLib/TimeLoop.h
index 6d9db035ee9..4221c2f5990 100644
--- a/ProcessLib/TimeLoop.h
+++ b/ProcessLib/TimeLoop.h
@@ -47,6 +47,7 @@ public:
              std::unique_ptr<ChemistryLib::PhreeqcIO>&& chemical_system,
              const double start_time, const double end_time);
 
+    void initialize();
     bool loop();
 
     ~TimeLoop();
-- 
GitLab