From 9de01052c6c904bf0e81e605981c9c8ef8f95e38 Mon Sep 17 00:00:00 2001
From: "Joachim Wuttke (o)" <j.wuttke@fz-juelich.de>
Date: Mon, 30 Jan 2023 17:12:15 +0100
Subject: [PATCH] simplify call to DepthprobeComputation

---
 Sim/Computation/DepthprobeComputation.cpp | 75 ++++++++++-------------
 Sim/Computation/DepthprobeComputation.h   |  8 +--
 Sim/Computation/IComputation.h            |  2 +-
 Sim/Simulation/DepthprobeSimulation.cpp   |  5 +-
 4 files changed, 39 insertions(+), 51 deletions(-)

diff --git a/Sim/Computation/DepthprobeComputation.cpp b/Sim/Computation/DepthprobeComputation.cpp
index cb3e2492531..fc09f13ba29 100644
--- a/Sim/Computation/DepthprobeComputation.cpp
+++ b/Sim/Computation/DepthprobeComputation.cpp
@@ -22,59 +22,52 @@
 
 DepthprobeComputation::DepthprobeComputation(const ReSample& re_sample,
                                              const SimulationOptions& options,
-                                             ProgressHandler& progress,
-                                             DepthprobeElementIter begin_it,
-                                             DepthprobeElementIter end_it)
+                                             ProgressHandler& progress)
     : IComputation(re_sample, options, progress)
-    , m_begin_it(begin_it)
-    , m_end_it(end_it)
 {
 }
 
 DepthprobeComputation::~DepthprobeComputation() = default;
 
-void DepthprobeComputation::runProtected()
+void DepthprobeComputation::run(DepthprobeElement& ele)
 {
-    for (auto it = m_begin_it; it != m_end_it; ++it) {
-        DepthprobeElement& ele = *it;
-        if (!ele.isCalculated())
-            continue;
+    if (!ele.isCalculated())
+        return;
 
-        const IAxis& z_positions = *ele.getZPositions();
-        const size_t n_z = z_positions.size();
-        const size_t n_layers = m_re_sample.numberOfSlices();
-        size_t start_z_ind = n_z;
-        std::valarray<double> intensities(0.0, n_z);
+    const IAxis& z_positions = *ele.getZPositions();
+    const size_t n_z = z_positions.size();
+    const size_t n_layers = m_re_sample.numberOfSlices();
+    size_t start_z_ind = n_z;
+    std::valarray<double> intensities(0.0, n_z);
 
-        Fluxes fluxes = m_re_sample.fluxesIn(ele.getKi());
+    Fluxes fluxes = m_re_sample.fluxesIn(ele.getKi());
 
-        double z_layer_bottom(0.0);
-        double z_layer_top(0.0);
-        for (size_t i_layer = 0; i_layer < n_layers && start_z_ind != 0; ++i_layer) {
-            z_layer_bottom = m_re_sample.sliceBottomZ(i_layer);
-            z_layer_top = i_layer ? m_re_sample.sliceTopZ(i_layer) : 0;
+    double z_layer_bottom(0.0);
+    double z_layer_top(0.0);
+    for (size_t i_layer = 0; i_layer < n_layers && start_z_ind != 0; ++i_layer) {
+        z_layer_bottom = m_re_sample.sliceBottomZ(i_layer);
+        z_layer_top = i_layer ? m_re_sample.sliceTopZ(i_layer) : 0;
 
-            // get R & T coefficients for current layer
-            const auto* flux = dynamic_cast<const ScalarFlux*>(fluxes[i_layer]);
-            ASSERT(flux);
-            const complex_t R = flux->getScalarR();
-            const complex_t T = flux->getScalarT();
-            const complex_t kz_out = flux->getScalarKz();
-            const complex_t kz_in = -kz_out;
+        // get R & T coefficients for current layer
+        const auto* flux = dynamic_cast<const ScalarFlux*>(fluxes[i_layer]);
+        ASSERT(flux);
+        const complex_t R = flux->getScalarR();
+        const complex_t T = flux->getScalarT();
+        const complex_t kz_out = flux->getScalarKz();
+        const complex_t kz_in = -kz_out;
 
-            // Compute intensity for z's of the layer
-            size_t ip1_z = start_z_ind;
-            for (; ip1_z > 0; --ip1_z) {
-                const size_t i_z = ip1_z - 1;
-                if (i_layer + 1 != n_layers && z_positions[i_z] <= z_layer_bottom)
-                    break;
-                const double z = z_positions[i_z] - z_layer_top;
-                intensities[i_z] = std::norm(R * exp_I(kz_out * z) + T * exp_I(kz_in * z));
-            }
-            start_z_ind = ip1_z;
+        // Compute intensity for z's of the layer
+        size_t ip1_z = start_z_ind;
+        for (; ip1_z > 0; --ip1_z) {
+            const size_t i_z = ip1_z - 1;
+            if (i_layer + 1 != n_layers && z_positions[i_z] <= z_layer_bottom)
+                break;
+            const double z = z_positions[i_z] - z_layer_top;
+            intensities[i_z] = std::norm(R * exp_I(kz_out * z) + T * exp_I(kz_in * z));
         }
-        ele.setIntensities(std::move(intensities));
-
-        stepProgress();
+        start_z_ind = ip1_z;
     }
+    ele.setIntensities(std::move(intensities));
+
+    stepProgress();
 }
diff --git a/Sim/Computation/DepthprobeComputation.h b/Sim/Computation/DepthprobeComputation.h
index efc7108151f..b3190a4f174 100644
--- a/Sim/Computation/DepthprobeComputation.h
+++ b/Sim/Computation/DepthprobeComputation.h
@@ -33,14 +33,10 @@ class DepthprobeComputation : public IComputation {
 
 public:
     DepthprobeComputation(const ReSample& re_sample, const SimulationOptions& options,
-                          ProgressHandler& progress, DepthprobeElementIter begin_it,
-                          DepthprobeElementIter end_it);
+                          ProgressHandler& progress);
     ~DepthprobeComputation() override;
 
-    void runProtected() override;
-
-private:
-    DepthprobeElementIter m_begin_it, m_end_it;
+    void run(DepthprobeElement& ele);
 };
 
 #endif // BORNAGAIN_SIM_COMPUTATION_DEPTHPROBECOMPUTATION_H
diff --git a/Sim/Computation/IComputation.h b/Sim/Computation/IComputation.h
index 754c6fe568a..41a5d3388aa 100644
--- a/Sim/Computation/IComputation.h
+++ b/Sim/Computation/IComputation.h
@@ -34,7 +34,7 @@ public:
     virtual ~IComputation();
 
     //! Runs computation. May throw.
-    virtual void runProtected() = 0;
+    virtual void runProtected() {}
 
 protected:
     void stepProgress() const;
diff --git a/Sim/Simulation/DepthprobeSimulation.cpp b/Sim/Simulation/DepthprobeSimulation.cpp
index 24b8d2feed6..48f590f668d 100644
--- a/Sim/Simulation/DepthprobeSimulation.cpp
+++ b/Sim/Simulation/DepthprobeSimulation.cpp
@@ -139,9 +139,8 @@ void DepthprobeSimulation::initElementVector()
 
 void DepthprobeSimulation::runComputation(const ReSample& re_sample, size_t iElement)
 {
-    const auto& begin = m_depth_eles.begin() + static_cast<long>(iElement);
-    DepthprobeComputation(re_sample, options(), progress(), begin, begin + static_cast<long>(1))
-        .runProtected();
+    DepthprobeElement& ele = *(m_depth_eles.begin() + static_cast<long>(iElement));
+    DepthprobeComputation(re_sample, options(), progress()).run(ele);
 }
 
 void DepthprobeSimulation::normalize(size_t start, size_t n_elements)
-- 
GitLab