From 0fe13971babb3d63b4ceb95fca263f9f1dd09cc4 Mon Sep 17 00:00:00 2001
From: Gennady Pospelov <g.pospelov@fz-juelich.de>
Date: Tue, 16 Dec 2014 14:38:22 +0100
Subject: [PATCH] First iteration is done with specular simulation.

---
 Core/Algorithms/inc/SpecularSimulation.h      |  10 +-
 Core/Algorithms/src/SpecularSimulation.cpp    | 156 +++++++++++-------
 .../PythonAPI/src/SpecularSimulation.pypp.cpp |  10 ++
 .../SpecularSimulation.py                     |  18 +-
 4 files changed, 115 insertions(+), 79 deletions(-)

diff --git a/Core/Algorithms/inc/SpecularSimulation.h b/Core/Algorithms/inc/SpecularSimulation.h
index e757efb9926..2ad1a897049 100644
--- a/Core/Algorithms/inc/SpecularSimulation.h
+++ b/Core/Algorithms/inc/SpecularSimulation.h
@@ -33,8 +33,8 @@
 class BA_CORE_API_ SpecularSimulation : public ICloneable, public IParameterized
 {
 public:
-    typedef boost::shared_ptr<const ILayerRTCoefficients> LayerRTCoefficients;
-    typedef std::vector<LayerRTCoefficients> MultiLayerRTCoefficients;
+    typedef boost::shared_ptr<const ILayerRTCoefficients> LayerRTCoefficients_t;
+    typedef std::vector<LayerRTCoefficients_t> MultiLayerRTCoefficients_t;
 
     SpecularSimulation();
     SpecularSimulation(const ISample& sample);
@@ -77,7 +77,7 @@ public:
     //! returns vector containing Kz coefficients for all alpha_i angles for given layer index
     std::vector<complex_t > getScalarKz(int i_layer = 0) const;
 
-//    const ILayerRTCoefficients *getLayerRTCoefficients(int i_alpha, int i_layer) const;
+    LayerRTCoefficients_t getLayerRTCoefficients(int i_alpha, int i_layer) const;
 
 protected:
     SpecularSimulation(const SpecularSimulation& other);
@@ -100,9 +100,9 @@ protected:
     double m_lambda;
 
 #ifndef GCCXML_SKIP_THIS
-    OutputData<SpecularMatrix::MultiLayerCoeff_t> *m_scalar_data;
+//    OutputData<SpecularMatrix::MultiLayerCoeff_t> *m_scalar_data;
 
-//    OutputData<MultiLayerRTCoefficients> m_data;
+    OutputData<MultiLayerRTCoefficients_t> m_data;
 #endif
 };
 
diff --git a/Core/Algorithms/src/SpecularSimulation.cpp b/Core/Algorithms/src/SpecularSimulation.cpp
index 1226065da46..03f5f8e9ed1 100644
--- a/Core/Algorithms/src/SpecularSimulation.cpp
+++ b/Core/Algorithms/src/SpecularSimulation.cpp
@@ -23,7 +23,7 @@ SpecularSimulation::SpecularSimulation()
     , m_sample(0)
     , m_alpha_i_axis(0)
     , m_lambda(0.0)
-    , m_scalar_data(0)
+//    , m_scalar_data(0)
 {
     init_parameters();
 }
@@ -34,7 +34,7 @@ SpecularSimulation::SpecularSimulation(const ISample& sample)
     , m_sample(sample.clone())
     , m_alpha_i_axis(0)
     , m_lambda(0.0)
-    , m_scalar_data(0)
+//    , m_scalar_data(0)
 {
     init_parameters();
 }
@@ -46,7 +46,7 @@ SpecularSimulation::SpecularSimulation(SampleBuilder_t sample_builder)
     , m_sample_builder(sample_builder)
     , m_alpha_i_axis(0)
     , m_lambda(0.0)
-    , m_scalar_data(0)
+//    , m_scalar_data(0)
 {
     init_parameters();
 }
@@ -60,7 +60,8 @@ SpecularSimulation::SpecularSimulation(const SpecularSimulation& other)
 {
     if(other.m_sample) m_sample = other.m_sample->clone();
     if(other.m_alpha_i_axis) m_alpha_i_axis = other.m_alpha_i_axis->clone();
-    if(other.m_scalar_data) m_scalar_data = other.m_scalar_data->clone();
+//    if(other.m_scalar_data) m_scalar_data = other.m_scalar_data->clone();
+    m_data.copyFrom(other.m_data);
 
     init_parameters();
 }
@@ -70,7 +71,7 @@ SpecularSimulation::~SpecularSimulation()
 {
     delete m_sample;
     delete m_alpha_i_axis;
-    delete m_scalar_data;
+//    delete m_scalar_data;
 }
 
 
@@ -171,15 +172,54 @@ const IAxis *SpecularSimulation::getAlphaAxis() const
 }
 
 
+//std::vector<complex_t > SpecularSimulation::getScalarR(int i_layer) const
+//{
+//    if(!m_scalar_data)
+//        throw RuntimeErrorException("SpecularSimulation::getScalarR() -> Error. No scalar coefficients.");
+
+//    std::vector<complex_t > result;
+//    result.resize(m_alpha_i_axis->getSize());
+//    for(size_t i=0; i<m_scalar_data->getAllocatedSize(); ++i) {
+//        result[i] = (*m_scalar_data)[i][i_layer].getScalarR();
+//    }
+//    return result;
+//}
+
+
+//std::vector<complex_t > SpecularSimulation::getScalarT(int i_layer) const
+//{
+//    if(!m_scalar_data)
+//        throw RuntimeErrorException("SpecularSimulation::getScalarR() -> Error. No scalar coefficients.");
+
+//    std::vector<complex_t > result;
+//    result.resize(m_alpha_i_axis->getSize());
+//    for(size_t i=0; i<m_scalar_data->getAllocatedSize(); ++i) {
+//        result[i] = (*m_scalar_data)[i][i_layer].getScalarT();
+//    }
+//    return result;
+//}
+
+
+//std::vector<complex_t> SpecularSimulation::getScalarKz(int i_layer) const
+//{
+//    if(!m_scalar_data)
+//        throw RuntimeErrorException("SpecularSimulation::getScalarR() -> Error. No scalar coefficients.");
+
+//    std::vector<complex_t > result;
+//    result.resize(m_alpha_i_axis->getSize());
+//    for(size_t i=0; i<m_scalar_data->getAllocatedSize(); ++i) {
+//        result[i] = (*m_scalar_data)[i][i_layer].getScalarKz();
+//    }
+//    return result;
+//}
+
+
 std::vector<complex_t > SpecularSimulation::getScalarR(int i_layer) const
 {
-    if(!m_scalar_data)
-        throw RuntimeErrorException("SpecularSimulation::getScalarR() -> Error. No scalar coefficients.");
-
     std::vector<complex_t > result;
     result.resize(m_alpha_i_axis->getSize());
-    for(size_t i=0; i<m_scalar_data->getAllocatedSize(); ++i) {
-        result[i] = (*m_scalar_data)[i][i_layer].getScalarR();
+    for(size_t i=0; i<m_data.getAllocatedSize(); ++i) {
+        result[i] = m_data[i][i_layer]->getScalarR();
     }
     return result;
 }
@@ -187,13 +227,10 @@ std::vector<complex_t > SpecularSimulation::getScalarR(int i_layer) const
 
 std::vector<complex_t > SpecularSimulation::getScalarT(int i_layer) const
 {
-    if(!m_scalar_data)
-        throw RuntimeErrorException("SpecularSimulation::getScalarR() -> Error. No scalar coefficients.");
-
     std::vector<complex_t > result;
     result.resize(m_alpha_i_axis->getSize());
-    for(size_t i=0; i<m_scalar_data->getAllocatedSize(); ++i) {
-        result[i] = (*m_scalar_data)[i][i_layer].getScalarT();
+    for(size_t i=0; i<m_data.getAllocatedSize(); ++i) {
+        result[i] = m_data[i][i_layer]->getScalarT();
     }
     return result;
 }
@@ -201,25 +238,27 @@ std::vector<complex_t > SpecularSimulation::getScalarT(int i_layer) const
 
 std::vector<complex_t> SpecularSimulation::getScalarKz(int i_layer) const
 {
-    if(!m_scalar_data)
-        throw RuntimeErrorException("SpecularSimulation::getScalarR() -> Error. No scalar coefficients.");
-
     std::vector<complex_t > result;
     result.resize(m_alpha_i_axis->getSize());
-    for(size_t i=0; i<m_scalar_data->getAllocatedSize(); ++i) {
-        result[i] = (*m_scalar_data)[i][i_layer].getScalarKz();
+    for(size_t i=0; i<m_data.getAllocatedSize(); ++i) {
+        result[i] = m_data[i][i_layer]->getScalarKz();
     }
     return result;
 }
 
 
-//const ILayerRTCoefficients *SpecularSimulation::getLayerRTCoefficients(int i_alpha, int i_layer) const
-//{
-//    if(!m_scalar_data)
-//        throw RuntimeErrorException("SpecularSimulation::getScalarR() -> Error. No scalar coefficients.");
+SpecularSimulation::LayerRTCoefficients_t SpecularSimulation::getLayerRTCoefficients(int i_alpha, int i_layer) const
+{
+    if((size_t)i_alpha >= m_data.getAllocatedSize())
+        throw RuntimeErrorException("SpecularSimulation::getLayerRTCoefficients() -> Error. Wrong i_alpha.");
+
+    if((size_t)i_layer >= m_data[i_alpha].size())
+        throw RuntimeErrorException("SpecularSimulation::getLayerRTCoefficients() -> Error. Wrong i_layer.");
+
+    return m_data[i_alpha][i_layer];
+}
+
 
-//    return  &(*m_scalar_data)[i_alpha][i_layer];
-//}
 
 
 void SpecularSimulation::init_parameters()
@@ -246,38 +285,14 @@ void SpecularSimulation::updateSample()
 }
 
 
-void SpecularSimulation::collectRTCoefficientsScalar(const MultiLayer *multilayer)
-{
-    delete m_scalar_data;
-    m_scalar_data = new OutputData<SpecularMatrix::MultiLayerCoeff_t>();
-
-    m_scalar_data->addAxis(*m_alpha_i_axis);
-
-    OutputData<SpecularMatrix::MultiLayerCoeff_t>::iterator it = m_scalar_data->begin();
-//    OutputData<MultiLayerRTCoefficients>::iterator it = m_data.begin();
-    while (it != m_scalar_data->end()) {
-        double alpha_i = m_scalar_data->getValueOfAxis(0,it.getIndex());
-        kvector_t kvec;
-        kvec.setLambdaAlphaPhi(m_lambda, -alpha_i, 0.0);
-
-        SpecularMatrix::MultiLayerCoeff_t coeffs;
-        SpecularMatrix matrixCalculator;
-        matrixCalculator.execute(*multilayer, kvec, coeffs);
-
-        *it = coeffs;
-        ++it;
-
-    } // alpha_i
-
-}
-
-
 //void SpecularSimulation::collectRTCoefficientsScalar(const MultiLayer *multilayer)
 //{
-//    m_data.clear();
-//    m_data.addAxis(*m_alpha_i_axis);
+//    delete m_scalar_data;
+//    m_scalar_data = new OutputData<SpecularMatrix::MultiLayerCoeff_t>();
+
+//    m_scalar_data->addAxis(*m_alpha_i_axis);
 
-//    OutputData<MultiLayerRTCoefficients>::iterator it = m_data.begin();
+//    OutputData<SpecularMatrix::MultiLayerCoeff_t>::iterator it = m_scalar_data->begin();
 //    while (it != m_scalar_data->end()) {
 //        double alpha_i = m_scalar_data->getValueOfAxis(0,it.getIndex());
 //        kvector_t kvec;
@@ -291,11 +306,38 @@ void SpecularSimulation::collectRTCoefficientsScalar(const MultiLayer *multilaye
 //        ++it;
 
 //    } // alpha_i
-
 //}
 
 
-void SpecularSimulation::collectRTCoefficientsMatrix(const MultiLayer *multilayer)
+void SpecularSimulation::collectRTCoefficientsScalar(const MultiLayer *multilayer)
+{
+    m_data.clear();
+    m_data.addAxis(*m_alpha_i_axis);
+
+    OutputData<MultiLayerRTCoefficients_t>::iterator it = m_data.begin();
+    while (it != m_data.end()) {
+        double alpha_i = m_data.getValueOfAxis(0,it.getIndex());
+        kvector_t kvec;
+        kvec.setLambdaAlphaPhi(m_lambda, -alpha_i, 0.0);
+
+        SpecularMatrix::MultiLayerCoeff_t coeffs;
+        SpecularMatrix matrixCalculator;
+        matrixCalculator.execute(*multilayer, kvec, coeffs);
+
+        MultiLayerRTCoefficients_t ml_coeffs;
+        ml_coeffs.resize(coeffs.size());
+        for(size_t i_layer=0; i_layer<coeffs.size(); ++i_layer) {
+            ml_coeffs[i_layer] = LayerRTCoefficients_t(new ScalarRTCoefficients(coeffs[i_layer]));
+        }
+
+        *it = ml_coeffs;
+        ++it;
+
+    } // alpha_i
+}
+
+
+void SpecularSimulation::collectRTCoefficientsMatrix(const MultiLayer * /*multilayer*/)
 {
     throw NotImplementedException("SpecularSimulation::collectRTCoefficientsMatrix() -> Error. Not implemented.");
 }
diff --git a/Core/PythonAPI/src/SpecularSimulation.pypp.cpp b/Core/PythonAPI/src/SpecularSimulation.pypp.cpp
index b61032c8a3a..a3c7cfb2e6a 100644
--- a/Core/PythonAPI/src/SpecularSimulation.pypp.cpp
+++ b/Core/PythonAPI/src/SpecularSimulation.pypp.cpp
@@ -199,6 +199,16 @@ void register_SpecularSimulation_class(){
                 , getAlphaAxis_function_type( &::SpecularSimulation::getAlphaAxis )
                 , bp::return_value_policy< bp::reference_existing_object >() );
         
+        }
+        { //::SpecularSimulation::getLayerRTCoefficients
+        
+            typedef ::boost::shared_ptr< const ILayerRTCoefficients > ( ::SpecularSimulation::*getLayerRTCoefficients_function_type)( int,int ) const;
+            
+            SpecularSimulation_exposer.def( 
+                "getLayerRTCoefficients"
+                , getLayerRTCoefficients_function_type( &::SpecularSimulation::getLayerRTCoefficients )
+                , ( bp::arg("i_alpha"), bp::arg("i_layer") ) );
+        
         }
         { //::SpecularSimulation::getSample
         
diff --git a/Examples/python/simulation/ex05_BeamAndDetector/SpecularSimulation.py b/Examples/python/simulation/ex05_BeamAndDetector/SpecularSimulation.py
index 175f1bf5b78..c57e3a40d33 100644
--- a/Examples/python/simulation/ex05_BeamAndDetector/SpecularSimulation.py
+++ b/Examples/python/simulation/ex05_BeamAndDetector/SpecularSimulation.py
@@ -1,5 +1,5 @@
 """
-R and T coefficients in multilayer, Specular simulation
+R and T coefficients in multilayer, Specular simulation.
 """
 import numpy
 import matplotlib
@@ -51,11 +51,7 @@ def get_simulation():
     Create and return off-specular simulation with beam and detector defined
     """
     simulation = SpecularSimulation()
-    # simulation.setDetectorParameters(20, phi_f_min*degree, phi_f_max*degree, 200, alpha_f_min*degree, alpha_f_max*degree)
-    # defining the beam  with incidence alpha_i varied between alpha_i_min and alpha_i_max
-    # alpha_i_axis = FixedBinAxis("alpha_i", 1000, alpha_i_min*degree, alpha_i_max*degree)
     simulation.setBeamParameters(1.54*angstrom, 1000, alpha_i_min*degree, alpha_i_max*degree)
-    # simulation.setBeamIntensity(1e9)
     return simulation
 
 
@@ -95,18 +91,6 @@ def run_simulation():
         pylab.legend(['|R| layer #'+str(layer_index), '|T| layer #'+str(layer_index)], loc='upper right')
         nplot = nplot + 1
 
-    # special plot fo validation |R_top| + |T_bottom| sum
-    # R_top = simulation.getScalarR(0)
-    # T_bottom = simulation.getScalarT(sample.getNumberOfLayers()-1)
-    # R_plus_T = []
-    # for i in range(0, len(R_top)):
-    #     R_plus_T.append(numpy.abs(R_top[i])+numpy.abs(T_bottom[i]))
-    #
-    # pylab.subplot(2, 2, 4)
-    # pylab.ylim(ymax=50.0, ymin=1e-06)
-    # pylab.semilogy(alpha_angles, R_plus_T)
-    # pylab.legend(['|R| layer #'+str(layer_index), '|T| layer #'+str(layer_index)], loc='lower right')
-
 
     pylab.show()
 
-- 
GitLab