From 6cab0fb1fffd6583c1aeed420eee61f849f34067 Mon Sep 17 00:00:00 2001
From: Dmitri Naumov <github@naumov.de>
Date: Wed, 3 Jun 2020 01:05:13 +0200
Subject: [PATCH] [PL] Output; Non-modifying fixed output times alg.

The previous implementation would miss a fixed output time
if the time step was repeated.
---
 ProcessLib/Output/CreateOutput.cpp |  7 +++----
 ProcessLib/Output/Output.cpp       | 16 +++++++++++++---
 ProcessLib/Output/Output.h         |  2 +-
 3 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/ProcessLib/Output/CreateOutput.cpp b/ProcessLib/Output/CreateOutput.cpp
index 0e1ac5bac28..88487ce9da8 100644
--- a/ProcessLib/Output/CreateOutput.cpp
+++ b/ProcessLib/Output/CreateOutput.cpp
@@ -55,8 +55,6 @@ std::unique_ptr<Output> createOutput(
     // Construction of output times
     std::vector<Output::PairRepeatEachSteps> repeats_each_steps;
 
-    std::vector<double> fixed_output_times;
-
     //! \ogs_file_param{prj__time_loop__output__timesteps}
     if (auto const timesteps = config.getConfigSubtreeOptional("timesteps"))
     {
@@ -133,6 +131,7 @@ std::unique_ptr<Output> createOutput(
         }
     }
 
+    std::vector<double> fixed_output_times;
     auto fixed_output_times_ptr =
         //! \ogs_file_param{prj__time_loop__output__fixed_output_times}
         config.getConfigParameterOptional<std::vector<double>>(
@@ -140,8 +139,8 @@ std::unique_ptr<Output> createOutput(
     if (fixed_output_times_ptr)
     {
         fixed_output_times = std::move(*fixed_output_times_ptr);
-        // Remove possible duplicated elements and sort in descending order.
-        BaseLib::makeVectorUnique(fixed_output_times, std::greater<>());
+        // Remove possible duplicated elements and sort.
+        BaseLib::makeVectorUnique(fixed_output_times);
     }
 
     bool const output_iteration_results =
diff --git a/ProcessLib/Output/Output.cpp b/ProcessLib/Output/Output.cpp
index 5526d622a96..f18565d9e6c 100644
--- a/ProcessLib/Output/Output.cpp
+++ b/ProcessLib/Output/Output.cpp
@@ -85,11 +85,19 @@ bool Output::shallDoOutput(int timestep, double const t)
         return make_output;
     }
 
-    const double specific_time = _fixed_output_times.back();
+    auto const fixed_output_time = std::lower_bound(
+        cbegin(_fixed_output_times), cend(_fixed_output_times), t);
+    if (fixed_output_time == cend(_fixed_output_times))
+    {
+        return make_output;
+    }
+    DBUG(
+        "Fixed time output; Found {} in list of output times for current time "
+        "{}.",
+        *fixed_output_time, t);
     const double zero_threshold = std::numeric_limits<double>::min();
-    if (std::fabs(specific_time - t) < zero_threshold)
+    if (std::fabs(*fixed_output_time - t) < zero_threshold)
     {
-        _fixed_output_times.pop_back();
         make_output = true;
     }
 
@@ -117,6 +125,8 @@ Output::Output(std::string output_directory, std::string output_file_prefix,
       _mesh_names_for_output(mesh_names_for_output),
       _meshes(meshes)
 {
+    assert(
+        std::is_sorted(cbegin(_fixed_output_times), cend(_fixed_output_times)));
 }
 
 void Output::addProcess(ProcessLib::Process const& process,
diff --git a/ProcessLib/Output/Output.h b/ProcessLib/Output/Output.h
index 07ae325e3e3..4a14a38a9b5 100644
--- a/ProcessLib/Output/Output.h
+++ b/ProcessLib/Output/Output.h
@@ -104,7 +104,7 @@ private:
     std::vector<PairRepeatEachSteps> _repeats_each_steps;
 
     //! Given times that steps have to reach.
-    std::vector<double> _fixed_output_times;
+    std::vector<double> const _fixed_output_times;
 
     std::multimap<Process const*, MeshLib::IO::PVDFile> _process_to_pvd_file;
 
-- 
GitLab