From f68d5f47f53a7824e2b13a75f0ec05c5aaf2bf8e Mon Sep 17 00:00:00 2001
From: pospelov <pospelov@fz-juelich.de>
Date: Thu, 20 Sep 2012 15:33:48 +0200
Subject: [PATCH] =?UTF-8?q?Changes=20in=20DoubleToComplex=20interpolating?=
 =?UTF-8?q?=20function=20and=20in=20Lattice=20to=20improve=20performance.?=
 =?UTF-8?q?=20But=20they=20are=20not=20improving=E2=80=A6.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 App/src/TestMiscellaneous.cpp                 |  4 +-
 Core/FormFactors/src/FormFactorCrystal.cpp    |  7 +-
 Core/Samples/inc/Lattice.h                    | 24 +++--
 Core/Samples/src/Lattice.cpp                  | 94 ++++++++++++++++---
 .../DoubleToComplexInterpolatingFunction.cpp  | 18 +++-
 Examples/Performance/perf_history.txt         | 19 +++-
 6 files changed, 137 insertions(+), 29 deletions(-)

diff --git a/App/src/TestMiscellaneous.cpp b/App/src/TestMiscellaneous.cpp
index 3ec8e0173e6..fc4caa92d32 100644
--- a/App/src/TestMiscellaneous.cpp
+++ b/App/src/TestMiscellaneous.cpp
@@ -30,9 +30,9 @@ TestMiscellaneous::TestMiscellaneous()
 void TestMiscellaneous::execute()
 {
 
-    //test_DoubleToComplexInterpolatingFunction();
+    test_DoubleToComplexInterpolatingFunction();
     //test_FormFactor();
-    test_DrawMesocrystal();
+    //test_DrawMesocrystal();
 }
 
 
diff --git a/Core/FormFactors/src/FormFactorCrystal.cpp b/Core/FormFactors/src/FormFactorCrystal.cpp
index acd0bcaea68..befdba97a83 100644
--- a/Core/FormFactors/src/FormFactorCrystal.cpp
+++ b/Core/FormFactors/src/FormFactorCrystal.cpp
@@ -46,8 +46,11 @@ complex_t FormFactorCrystal::evaluate_for_q(const cvector_t &q) const
     // calculate the used radius in function of the reciprocal lattice scale
     double radius = 1.1*m_max_rec_length;
     // retrieve nearest reciprocal lattice vectors
-    std::vector<kvector_t> rec_vectors =
-            m_lattice.getReciprocalLatticeVectorsWithinRadius(q_real, radius);
+//    std::vector<kvector_t> rec_vectors =
+//            m_lattice.getReciprocalLatticeVectorsWithinRadius(q_real, radius);
+    std::vector<kvector_t> rec_vectors;
+    m_lattice.getReciprocalLatticeVectorsWithinRadius(q_real, radius, rec_vectors);
+
     // perform convolution on these lattice vectors
     complex_t result(0.0, 0.0);
     for (std::vector<kvector_t>::const_iterator it = rec_vectors.begin(); it != rec_vectors.end(); ++it) {
diff --git a/Core/Samples/inc/Lattice.h b/Core/Samples/inc/Lattice.h
index 05f415bf284..64698421fc9 100644
--- a/Core/Samples/inc/Lattice.h
+++ b/Core/Samples/inc/Lattice.h
@@ -58,12 +58,20 @@ public:
     Coordinate3D<int> getNearestReciprocalLatticeVectorCoordinates(const kvector_t &vector_in) const;
 
     //! get a list of lattice vectors within a specified distance of a given vector
-    std::vector<kvector_t> getLatticeVectorsWithinRadius(
-            const kvector_t &input_vector, double radius) const;
+//    std::vector<kvector_t> getLatticeVectorsWithinRadius(
+//            const kvector_t &input_vector, double radius) const;
+
+//    //! get a list of reciprocal lattice vectors within a specified distance of a given vector
+//    std::vector<kvector_t> getReciprocalLatticeVectorsWithinRadius(
+//            const kvector_t &input_vector, double radius) const;
+
+    void getLatticeVectorsWithinRadius(
+            const kvector_t &input_vector, double radius, std::vector<kvector_t> &result) const;
 
     //! get a list of reciprocal lattice vectors within a specified distance of a given vector
-    std::vector<kvector_t> getReciprocalLatticeVectorsWithinRadius(
-            const kvector_t &input_vector, double radius) const;
+    void getReciprocalLatticeVectorsWithinRadius(
+            const kvector_t &input_vector, double radius, std::vector<kvector_t> &result) const;
+
 
     //! get a list of rotation angles within a specified range that hit a maximal set of small Bragg peaks
     std::vector<double> collectBraggAngles(size_t size, double max_radius, const TRange<double> &phi_range, const TRange<double> &z_range) const;
@@ -82,10 +90,14 @@ public:
 
 private:
     Lattice &operator=(const Lattice &lattice);
-    std::vector<kvector_t> getVectorsWithinRadius(const kvector_t &input_vector,
+//    std::vector<kvector_t> getVectorsWithinRadius(const kvector_t &input_vector,
+//            const Coordinate3D<int> &nearest_coords, double radius,
+//            const kvector_t &v1, const kvector_t &v2, const kvector_t &v3,
+//            const kvector_t &rec1, const kvector_t &rec2, const kvector_t &rec3) const;
+    void getVectorsWithinRadius(const kvector_t &input_vector,
             const Coordinate3D<int> &nearest_coords, double radius,
             const kvector_t &v1, const kvector_t &v2, const kvector_t &v3,
-            const kvector_t &rec1, const kvector_t &rec2, const kvector_t &rec3) const;
+            const kvector_t &rec1, const kvector_t &rec2, const kvector_t &rec3, std::vector<kvector_t> &result) const;
     void computeReciprocalVectors() const;
     void computeInverseLatticeVectors() const;
     void computeInverseReciprocalLatticeVectors() const;
diff --git a/Core/Samples/src/Lattice.cpp b/Core/Samples/src/Lattice.cpp
index 00a7d57ae60..bdf74a2ebe3 100644
--- a/Core/Samples/src/Lattice.cpp
+++ b/Core/Samples/src/Lattice.cpp
@@ -85,27 +85,51 @@ Coordinate3D<int> Lattice::getNearestReciprocalLatticeVectorCoordinates(const kv
     return Coordinate3D<int>(c1, c2, c3);
 }
 
-std::vector<kvector_t> Lattice::getLatticeVectorsWithinRadius(
-        const kvector_t& input_vector, double radius) const
+//std::vector<kvector_t> Lattice::getLatticeVectorsWithinRadius(
+//        const kvector_t& input_vector, double radius) const
+//{
+//    if (!m_cache_ok) {
+//        initialize();
+//    }
+//    Coordinate3D<int> nearest_coords = getNearestLatticeVectorCoordinates(input_vector);
+//    return getVectorsWithinRadius(input_vector, nearest_coords, radius, m_a1, m_a2, m_a3, m_b1, m_b2, m_b3);
+//}
+
+//std::vector<kvector_t> Lattice::getReciprocalLatticeVectorsWithinRadius(
+//        const kvector_t& input_vector, double radius) const
+//{
+//    if (!m_cache_ok) {
+//        initialize();
+//    }
+//    Coordinate3D<int> nearest_coords = getNearestReciprocalLatticeVectorCoordinates(input_vector);
+//    return getVectorsWithinRadius(input_vector, nearest_coords, radius, m_b1, m_b2, m_b3, m_a1, m_a2, m_a3);
+
+//}
+
+
+void Lattice::getLatticeVectorsWithinRadius(
+        const kvector_t& input_vector, double radius, std::vector<kvector_t> &result) const
 {
     if (!m_cache_ok) {
         initialize();
     }
     Coordinate3D<int> nearest_coords = getNearestLatticeVectorCoordinates(input_vector);
-    return getVectorsWithinRadius(input_vector, nearest_coords, radius, m_a1, m_a2, m_a3, m_b1, m_b2, m_b3);
+    getVectorsWithinRadius(input_vector, nearest_coords, radius, m_a1, m_a2, m_a3, m_b1, m_b2, m_b3, result);
 }
 
-std::vector<kvector_t> Lattice::getReciprocalLatticeVectorsWithinRadius(
-        const kvector_t& input_vector, double radius) const
+void Lattice::getReciprocalLatticeVectorsWithinRadius(
+        const kvector_t& input_vector, double radius, std::vector<kvector_t> &result) const
 {
     if (!m_cache_ok) {
         initialize();
     }
     Coordinate3D<int> nearest_coords = getNearestReciprocalLatticeVectorCoordinates(input_vector);
-    return getVectorsWithinRadius(input_vector, nearest_coords, radius, m_b1, m_b2, m_b3, m_a1, m_a2, m_a3);
+    getVectorsWithinRadius(input_vector, nearest_coords, radius, m_b1, m_b2, m_b3, m_a1, m_a2, m_a3, result);
 
 }
 
+
+
 std::vector<double> Lattice::collectBraggAngles(size_t size, double max_radius,
         const TRange<double>& phi_range, const TRange<double>& z_range) const
 {
@@ -118,7 +142,9 @@ std::vector<double> Lattice::collectBraggAngles(size_t size, double max_radius,
         max_radius *= (double)size/max_nbr_angles;
     }
     double radius = std::max(max_radius, z_range.getMax());
-    std::vector<kvector_t> rec_vectors = getReciprocalLatticeVectorsWithinRadius(kvector_t(0.0, 0.0, 0.0), radius);
+    //std::vector<kvector_t> rec_vectors = getReciprocalLatticeVectorsWithinRadius(kvector_t(0.0, 0.0, 0.0), radius);
+    std::vector<kvector_t> rec_vectors;
+    getReciprocalLatticeVectorsWithinRadius(kvector_t(0.0, 0.0, 0.0), radius, rec_vectors);
     for (size_t i=0; i<rec_vectors.size(); ++i) {
         kvector_t rvec = rec_vectors[i];
         double phi = rvec.phi();
@@ -157,24 +183,64 @@ void Lattice::computeReciprocalVectors() const
     m_b3 = 2*M_PI/DotProduct(m_a3, a12)*a12;
 }
 
-std::vector<kvector_t> Lattice::getVectorsWithinRadius(const kvector_t &input_vector,
+//std::vector<kvector_t> Lattice::getVectorsWithinRadius(const kvector_t &input_vector,
+//        const Coordinate3D<int> &nearest_coords, double radius, const kvector_t& v1,
+//        const kvector_t& v2, const kvector_t& v3, const kvector_t& rec1,
+//        const kvector_t& rec2, const kvector_t& rec3) const
+//{
+//    int max_X = (int)std::floor( rec1.mag()*radius/(2*M_PI) );
+//    int max_Y = (int)std::floor( rec2.mag()*radius/(2*M_PI) );
+//    int max_Z = (int)std::floor( rec3.mag()*radius/(2*M_PI) );
+
+//    std::vector<kvector_t> result;
+//    Coordinate3D<int> coords(0,0,0);
+//    for (int index_X = -max_X; index_X <= max_X; ++index_X)
+//    {
+//        coords[0] = index_X + nearest_coords[0];
+//        for (int index_Y = -max_Y; index_Y <= max_Y; ++index_Y)
+//        {
+//            coords[1] = index_Y + nearest_coords[1];
+//            for (int index_Z = -max_Z; index_Z <= max_Z; ++index_Z)
+//            {
+//                coords[2] = index_Z + nearest_coords[2];
+////                Coordinate3D<int> coords(index_X + nearest_coords[0],
+////                        index_Y + nearest_coords[1], index_Z + nearest_coords[2]);
+//                if (mp_selection_rule && !mp_selection_rule->coordinateSelected(coords)) continue;
+//                kvector_t latticePoint = coords[0]*v1 + coords[1]*v2 + coords[2]*v3;
+//                if ((latticePoint - input_vector).mag() <= radius)
+//                {
+//                    result.push_back(latticePoint);
+//                }
+//            }
+//        }
+//    }
+//    return result;
+//}
+
+
+void Lattice::getVectorsWithinRadius(const kvector_t &input_vector,
         const Coordinate3D<int> &nearest_coords, double radius, const kvector_t& v1,
         const kvector_t& v2, const kvector_t& v3, const kvector_t& rec1,
-        const kvector_t& rec2, const kvector_t& rec3) const
+        const kvector_t& rec2, const kvector_t& rec3, std::vector<kvector_t> &result) const
 {
     int max_X = (int)std::floor( rec1.mag()*radius/(2*M_PI) );
     int max_Y = (int)std::floor( rec2.mag()*radius/(2*M_PI) );
     int max_Z = (int)std::floor( rec3.mag()*radius/(2*M_PI) );
 
-    std::vector<kvector_t> result;
+//    std::vector<kvector_t> result;
+    result.clear();
+    Coordinate3D<int> coords(0,0,0);
     for (int index_X = -max_X; index_X <= max_X; ++index_X)
     {
+        coords[0] = index_X + nearest_coords[0];
         for (int index_Y = -max_Y; index_Y <= max_Y; ++index_Y)
         {
+            coords[1] = index_Y + nearest_coords[1];
             for (int index_Z = -max_Z; index_Z <= max_Z; ++index_Z)
             {
-                Coordinate3D<int> coords(index_X + nearest_coords[0],
-                        index_Y + nearest_coords[1], index_Z + nearest_coords[2]);
+                coords[2] = index_Z + nearest_coords[2];
+//                Coordinate3D<int> coords(index_X + nearest_coords[0],
+//                        index_Y + nearest_coords[1], index_Z + nearest_coords[2]);
                 if (mp_selection_rule && !mp_selection_rule->coordinateSelected(coords)) continue;
                 kvector_t latticePoint = coords[0]*v1 + coords[1]*v2 + coords[2]*v3;
                 if ((latticePoint - input_vector).mag() <= radius)
@@ -184,9 +250,11 @@ std::vector<kvector_t> Lattice::getVectorsWithinRadius(const kvector_t &input_ve
             }
         }
     }
-    return result;
+//    return result;
 }
 
+
+
 void Lattice::computeInverseLatticeVectors() const
 {
     computeInverseVectors(m_a1, m_a2, m_a3, m_amin1, m_amin2, m_amin3);
diff --git a/Core/Tools/src/DoubleToComplexInterpolatingFunction.cpp b/Core/Tools/src/DoubleToComplexInterpolatingFunction.cpp
index ec04cfa5348..5f441c0c711 100644
--- a/Core/Tools/src/DoubleToComplexInterpolatingFunction.cpp
+++ b/Core/Tools/src/DoubleToComplexInterpolatingFunction.cpp
@@ -42,13 +42,21 @@ complex_t DoubleToComplexInterpolatingFunction::evaluate(double value)
     }
     if (value < m_lower_limit) value = m_lower_limit;
     else if (value > m_upper_limit) value = m_upper_limit;
-    std::map<double, complex_t>::const_iterator found_it = m_value_map.find(value);
-    if (found_it != m_value_map.end()) {
-        return found_it->second;
-    }
+
+//    std::map<double, complex_t>::const_iterator found_it = m_value_map.find(value);
+//    if (found_it != m_value_map.end()) {
+//        return found_it->second;
+//    }
+//    std::map<double, complex_t>::const_iterator lower_it = m_value_map.lower_bound(value);
+//    --lower_it;
+//    std::map<double, complex_t>::const_iterator upper_it = m_value_map.upper_bound(value);
+
     std::map<double, complex_t>::const_iterator lower_it = m_value_map.lower_bound(value);
+    if( (*lower_it).first == value ) {
+        return (*lower_it).second;
+    }
+    std::map<double, complex_t>::const_iterator upper_it = lower_it;
     --lower_it;
-    std::map<double, complex_t>::const_iterator upper_it = m_value_map.upper_bound(value);
 
     double interpolating_factor = (value - (*lower_it).first)/((*upper_it).first-(*lower_it).first);
     if(m_interpolating_mode == Nearest) {
diff --git a/Examples/Performance/perf_history.txt b/Examples/Performance/perf_history.txt
index 758e94b3e0d..4013b523bd3 100644
--- a/Examples/Performance/perf_history.txt
+++ b/Examples/Performance/perf_history.txt
@@ -85,4 +85,21 @@
 2012-09-19 15:59:04 | jcnsopc73  | macosx64, 2800 MHz      | 81300.8 | 4.65116 | 4.13223 | 0.90497 | 
 2012-09-19 16:00:24 | jcnsopc73  | macosx64, 2800 MHz      | 81967.2 | 4.662   | 4.08163 | 0.89686 | 
 
-
+# after DoubleToComplexInterpolating function
+2012-09-20 13:11:07 | jcnsopc73  | macosx64, 2800 MHz      | 81300.8 | 4.80769 | 4.24628 | 0.91324 | 
+2012-09-20 13:52:36 | jcnsopc73  | macosx64, 2800 MHz      | 80645.2 | 4.77327 | 4.22833 | 0.90497 |
+
+# minor improvement in Lattice (debug=off)
+# added coords3d
+2012-09-20 14:17:58 | jcnsopc73  | macosx64, 2800 MHz      | 79051.4 | 4.80769 | 4.22833 | 0.91743 |
+# function put result in vector
+2012-09-20 14:26:37 | jcnsopc73  | macosx64, 2800 MHz      | 81632.7 | 4.75059 | 4.158   | 0.92592 | 
+2012-09-20 14:26:58 | jcnsopc73  | macosx64, 2800 MHz      | 80645.2 | 4.72813 | 4.13223 | 0.92592 |
+# debug=on
+2012-09-20 14:57:17 | jcnsopc73  | macosx64, 2800 MHz      | 253165  | 14.8148 | 14.4928 | 4.25532 | 
+2012-09-20 14:57:58 | jcnsopc73  | macosx64, 2800 MHz      | 266667  | 14.5985 | 14.7059 | 4.25532 | 
+2012-09-20 15:07:28 | jcnsopc73  | macosx64, 2800 MHz      | 243902  | 13.8889 | 13.986  | 8       | 
+2012-09-20 15:07:59 | jcnsopc73  | macosx64, 2800 MHz      | 250000  | 14.1844 | 13.7931 | 28.5714 | 
+2012-09-20 15:09:33 | jcnsopc73  | macosx64, 2800 MHz      | 240964  | 13.7931 | 13.6986 | 6.45161 | 
+2012-09-20 15:10:48 | jcnsopc73  | macosx64, 2800 MHz      | 243902  | 14.1844 | 13.5135 | 3.84615 | 
+2012-09-20 15:11:03 | jcnsopc73  | macosx64, 2800 MHz      | 250000  | 14.1844 | 13.8889 | 4       | 
-- 
GitLab