From 6087ecbc25caac1c06a221cadb612379e378ad22 Mon Sep 17 00:00:00 2001
From: Dmitry Yurov <d.yurov@fz-juelich.de>
Date: Fri, 20 Oct 2017 15:22:21 +0200
Subject: [PATCH] Remove refractiveIndex methods from Layer class

Redmine: #1858
and adapt Layer class to new HomogeneousMaterial::refractiveIndex signature
---
 Core/Computation/RoughMultiLayerComputation.cpp |  8 ++++----
 Core/Computation/RoughMultiLayerComputation.h   |  2 +-
 Core/Multilayer/Layer.cpp                       | 14 ++------------
 Core/Multilayer/Layer.h                         |  3 ---
 Core/Multilayer/SpecularMagnetic.cpp            |  2 +-
 Core/Multilayer/SpecularMatrix.cpp              |  2 +-
 Tests/UnitTests/Core/Sample/LayerTest.h         |  9 +++------
 7 files changed, 12 insertions(+), 28 deletions(-)

diff --git a/Core/Computation/RoughMultiLayerComputation.cpp b/Core/Computation/RoughMultiLayerComputation.cpp
index 8e1c71d5242..681880e6302 100644
--- a/Core/Computation/RoughMultiLayerComputation.cpp
+++ b/Core/Computation/RoughMultiLayerComputation.cpp
@@ -77,7 +77,7 @@ double RoughMultiLayerComputation::evaluate(const SimulationElement& sim_element
     std::vector<complex_t > sterm( mp_multilayer->numberOfLayers()-1 );
 
     for (size_t i=0; i<mp_multilayer->numberOfLayers()-1; i++){
-        rterm[i] = get_refractive_term(i);
+        rterm[i] = get_refractive_term(i, wavelength);
         sterm[i] = get_sum8terms(i, sim_element);
     }
 
@@ -104,10 +104,10 @@ double RoughMultiLayerComputation::evaluate(const SimulationElement& sim_element
     return (autocorr+crosscorr.real())*M_PI/4./wavelength/wavelength;
 }
 
-complex_t RoughMultiLayerComputation::get_refractive_term(size_t ilayer) const
+complex_t RoughMultiLayerComputation::get_refractive_term(size_t ilayer, double wavelength) const
 {
-    return mp_multilayer->layer(ilayer  )->refractiveIndex2() -
-           mp_multilayer->layer(ilayer+1)->refractiveIndex2();
+    return mp_multilayer->layer(ilayer  )->material()->refractiveIndex2(wavelength) -
+           mp_multilayer->layer(ilayer+1)->material()->refractiveIndex2(wavelength);
 }
 
 complex_t RoughMultiLayerComputation::get_sum8terms(
diff --git a/Core/Computation/RoughMultiLayerComputation.h b/Core/Computation/RoughMultiLayerComputation.h
index d2223ea0ec4..3d869ce3d36 100644
--- a/Core/Computation/RoughMultiLayerComputation.h
+++ b/Core/Computation/RoughMultiLayerComputation.h
@@ -40,7 +40,7 @@ public:
 
 private:
     double evaluate(const SimulationElement& sim_element) const;
-    complex_t get_refractive_term(size_t ilayer) const;
+    complex_t get_refractive_term(size_t ilayer, double wavelength) const;
     complex_t get_sum8terms(size_t ilayer, const SimulationElement& sim_element) const;
 };
 
diff --git a/Core/Multilayer/Layer.cpp b/Core/Multilayer/Layer.cpp
index f3c8acf9ee7..2369397c5c2 100644
--- a/Core/Multilayer/Layer.cpp
+++ b/Core/Multilayer/Layer.cpp
@@ -65,16 +65,6 @@ void Layer::setMaterial(HomogeneousMaterial material)
     m_material = std::move(material);
 }
 
-complex_t Layer::refractiveIndex() const
-{
-    return m_material.refractiveIndex();
-}
-
-complex_t Layer::refractiveIndex2() const
-{
-    return m_material.refractiveIndex2();
-}
-
 void Layer::addLayout(const ILayout& layout)
 {
     ILayout* clone = layout.clone();
@@ -153,13 +143,13 @@ SafePointerVector<Layer> Layer::slice(ZLimits limits, Layer::ELayerType layer_ty
 
 complex_t Layer::scalarReducedPotential(kvector_t k, double n_ref) const
 {
-    complex_t n = m_material.refractiveIndex();
+    complex_t n = m_material.refractiveIndex(2.0 * M_PI / k.mag());
     return ScalarReducedPotential(n, k, n_ref);
 }
 
 Eigen::Matrix2cd Layer::polarizedReducedPotential(kvector_t k, double n_ref) const
 {
-    complex_t n = m_material.refractiveIndex();
+    complex_t n = m_material.refractiveIndex(2.0 * M_PI / k.mag());
     kvector_t b_field = bField();
     return PolarizedReducedPotential(n, b_field, k, n_ref);
 }
diff --git a/Core/Multilayer/Layer.h b/Core/Multilayer/Layer.h
index 153e9f37019..d2c5fdfada2 100644
--- a/Core/Multilayer/Layer.h
+++ b/Core/Multilayer/Layer.h
@@ -51,9 +51,6 @@ public:
     const HomogeneousMaterial* material() const override final { return &m_material; }
     void setMaterial(HomogeneousMaterial material);
 
-    complex_t refractiveIndex() const;
-    complex_t refractiveIndex2() const; //!< squared refractive index
-
     void addLayout(const ILayout& decoration);
     size_t numberOfLayouts() const { return m_layouts.size(); }
     std::vector<const ILayout*> layouts() const;
diff --git a/Core/Multilayer/SpecularMagnetic.cpp b/Core/Multilayer/SpecularMagnetic.cpp
index 74733e328e7..a7c34dc402e 100644
--- a/Core/Multilayer/SpecularMagnetic.cpp
+++ b/Core/Multilayer/SpecularMagnetic.cpp
@@ -40,7 +40,7 @@ void SpecularMagnetic::calculateEigenvalues(
     const MultiLayer& sample, const kvector_t k, std::vector<MatrixRTCoefficients>& coeff)
 {
     double mag_k = k.mag();
-    double n_ref = sample.layer(0)->refractiveIndex().real();
+    double n_ref = sample.layer(0)->material()->refractiveIndex(2 * M_PI / mag_k).real();
     double sign_kz = k.z() > 0.0 ? -1.0 : 1.0;
     for(size_t i=0; i<coeff.size(); ++i) {
         coeff[i].m_scatt_matrix = sample.layer(i)->polarizedReducedPotential(k, n_ref);
diff --git a/Core/Multilayer/SpecularMatrix.cpp b/Core/Multilayer/SpecularMatrix.cpp
index d28508fc1d9..66c88107774 100644
--- a/Core/Multilayer/SpecularMatrix.cpp
+++ b/Core/Multilayer/SpecularMatrix.cpp
@@ -49,7 +49,7 @@ void SpecularMatrix::execute(const MultiLayer& sample, const kvector_t k,
     coeff.clear();
     coeff.resize(N);
 
-    double n_ref = sample.layer(0)->refractiveIndex().real();
+    double n_ref = sample.layer(0)->material()->refractiveIndex(2 * M_PI / k.mag()).real();
 
     // Calculate refraction angle, expressed as lambda or k_z, for each layer.
     double sign_kz_out = k.z() > 0.0 ? -1.0 : 1.0;
diff --git a/Tests/UnitTests/Core/Sample/LayerTest.h b/Tests/UnitTests/Core/Sample/LayerTest.h
index 2f1973bb723..5779f8fa9c4 100644
--- a/Tests/UnitTests/Core/Sample/LayerTest.h
+++ b/Tests/UnitTests/Core/Sample/LayerTest.h
@@ -13,26 +13,23 @@ class LayerTest : public ::testing::Test
 TEST_F(LayerTest, LayerGetAndSet)
 {
     HomogeneousMaterial air("air",0,0);
-
     Layer layer(air, 10*Units::nanometer);
-    EXPECT_EQ(air.getName(), layer.material()->getName());
+    EXPECT_EQ(air, *layer.material());
     EXPECT_EQ(0u, layer.layouts().size());
     EXPECT_EQ(10, layer.thickness());
     EXPECT_EQ(layer.numberOfLayouts(), 0u);
-    EXPECT_EQ(complex_t(1, 0), layer.refractiveIndex());
     EXPECT_EQ(BornAgain::LayerType, layer.getName());
 
     layer.setThickness(20.0);
+    EXPECT_EQ(air, *layer.material());
     EXPECT_EQ(20, layer.thickness());
     EXPECT_EQ(BornAgain::LayerType, layer.getName());
-    EXPECT_EQ(complex_t(1, 0), layer.refractiveIndex());
 
     std::unique_ptr<Layer> clone(layer.clone());
-    EXPECT_EQ(air.getName(), clone->material()->getName());
+    EXPECT_EQ(air, *clone->material());
     EXPECT_EQ(0u, clone->layouts().size());
     EXPECT_EQ(20, clone->thickness());
     EXPECT_EQ(clone->numberOfLayouts(), 0u);
-    EXPECT_EQ(complex_t(1, 0), clone->refractiveIndex());
     EXPECT_EQ(BornAgain::LayerType, clone->getName());
 }
 
-- 
GitLab