Skip to content
Snippets Groups Projects
Commit b150525e authored by Beerwerth, Randolf's avatar Beerwerth, Randolf
Browse files

Switch SpecularScalarStrategy to unique_ptr

parent 8491fa6c
No related branches found
No related tags found
No related merge requests found
...@@ -46,7 +46,7 @@ void SpecularScalarTerm::eval(SpecularSimulationElement& elem, ...@@ -46,7 +46,7 @@ void SpecularScalarTerm::eval(SpecularSimulationElement& elem,
const std::vector<Slice>& slices) const const std::vector<Slice>& slices) const
{ {
auto coeff = std::make_unique<SpecularScalarStrategy>()->Execute(slices, elem.produceKz(slices)); auto coeff = std::make_unique<SpecularScalarStrategy>()->Execute(slices, elem.produceKz(slices));
elem.setIntensity(std::norm(coeff.front().getScalarR())); elem.setIntensity(std::norm(coeff.front()->getScalarR()));
} }
SpecularMatrixTerm::~SpecularMatrixTerm() = default; SpecularMatrixTerm::~SpecularMatrixTerm() = default;
......
...@@ -41,13 +41,13 @@ ScalarFresnelMap::getCoefficients(const kvector_t& kvec, size_t layer_index) con ...@@ -41,13 +41,13 @@ ScalarFresnelMap::getCoefficients(const kvector_t& kvec, size_t layer_index) con
{ {
if (!m_use_cache) { if (!m_use_cache) {
auto coeffs = std::make_unique<SpecularScalarStrategy>()->Execute(m_slices, kvec); auto coeffs = std::make_unique<SpecularScalarStrategy>()->Execute(m_slices, kvec);
return std::make_unique<const ScalarRTCoefficients>(coeffs[layer_index]); return std::unique_ptr<const ScalarRTCoefficients>(coeffs[layer_index]->clone());
} }
const auto& coef_vector = getCoefficientsFromCache(kvec); const auto& coef_vector = getCoefficientsFromCache(kvec);
return std::make_unique<const ScalarRTCoefficients>(coef_vector[layer_index]); return std::unique_ptr<const ScalarRTCoefficients>(coef_vector[layer_index]->clone());
} }
const std::vector<ScalarRTCoefficients>& const std::vector<std::unique_ptr<ScalarRTCoefficients>>&
ScalarFresnelMap::getCoefficientsFromCache(kvector_t kvec) const ScalarFresnelMap::getCoefficientsFromCache(kvector_t kvec) const
{ {
std::pair<double, double> k2_theta(kvec.mag2(), kvec.theta()); std::pair<double, double> k2_theta(kvec.mag2(), kvec.theta());
......
...@@ -48,8 +48,8 @@ private: ...@@ -48,8 +48,8 @@ private:
std::unique_ptr<const ILayerRTCoefficients> getCoefficients(const kvector_t& kvec, std::unique_ptr<const ILayerRTCoefficients> getCoefficients(const kvector_t& kvec,
size_t layer_index) const override; size_t layer_index) const override;
const std::vector<ScalarRTCoefficients>& getCoefficientsFromCache(kvector_t kvec) const; const std::vector<std::unique_ptr<ScalarRTCoefficients>>& getCoefficientsFromCache(kvector_t kvec) const;
mutable std::unordered_map<std::pair<double, double>, std::vector<ScalarRTCoefficients>, mutable std::unordered_map<std::pair<double, double>, std::vector<std::unique_ptr<ScalarRTCoefficients>>,
Hash2Doubles> m_cache; Hash2Doubles> m_cache;
}; };
......
...@@ -42,18 +42,24 @@ const LayerRoughness* GetBottomRoughness(const std::vector<Slice>& slices, ...@@ -42,18 +42,24 @@ const LayerRoughness* GetBottomRoughness(const std::vector<Slice>& slices,
const double pi2_15 = std::pow(M_PI_2, 1.5); const double pi2_15 = std::pow(M_PI_2, 1.5);
} // namespace } // namespace
std::vector<ScalarRTCoefficients> SpecularScalarStrategy::Execute(const std::vector<Slice>& slices, std::vector<std::unique_ptr<ScalarRTCoefficients>>
kvector_t k) SpecularScalarStrategy::Execute(const std::vector<Slice>& slices, kvector_t k)
{ {
std::vector<complex_t> kz = KzComputation::computeReducedKz(slices, k); std::vector<complex_t> kz = KzComputation::computeReducedKz(slices, k);
return computeTR(slices, kz); return Execute(slices, kz);
} }
std::vector<ScalarRTCoefficients> SpecularScalarStrategy::Execute(const std::vector<Slice>& slices, std::vector<std::unique_ptr<ScalarRTCoefficients>>
const std::vector<complex_t>& kz) SpecularScalarStrategy::Execute(const std::vector<Slice>& slices, const std::vector<complex_t>& kz)
{ {
assert(slices.size() == kz.size()); if(slices.size() != kz.size())
return computeTR(slices, kz); throw std::runtime_error("Number of slices does not match the size of the kz-vector");
std::vector<std::unique_ptr<ScalarRTCoefficients>> result;
for(auto& coeff : computeTR(slices, kz))
result.push_back( std::make_unique<ScalarRTCoefficients>(coeff) );
return result;
} }
namespace namespace
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "ScalarRTCoefficients.h" #include "ScalarRTCoefficients.h"
#include "Vectors3D.h" #include "Vectors3D.h"
#include <vector> #include <vector>
#include <memory>
class Slice; class Slice;
...@@ -31,14 +32,14 @@ public: ...@@ -31,14 +32,14 @@ public:
//! Computes refraction angles and transmission/reflection coefficients //! Computes refraction angles and transmission/reflection coefficients
//! for given coherent wave propagation in a multilayer. //! for given coherent wave propagation in a multilayer.
//! Roughness is modelled by tanh profile [see e.g. Phys. Rev. B, vol. 47 (8), p. 4385 (1993)]. //! Roughness is modelled by tanh profile [see e.g. Phys. Rev. B, vol. 47 (8), p. 4385 (1993)].
BA_CORE_API_ std::vector<ScalarRTCoefficients> Execute(const std::vector<Slice>& slices, BA_CORE_API_ std::vector<std::unique_ptr<ScalarRTCoefficients>>
kvector_t k); Execute(const std::vector<Slice>& slices, kvector_t k);
//! Computes transmission/reflection coefficients //! Computes transmission/reflection coefficients
//! for given set of z-components of wave-vectors in a multilayer. //! for given set of z-components of wave-vectors in a multilayer.
//! Roughness is modelled by tanh profile [see e.g. Phys. Rev. B, vol. 47 (8), p. 4385 (1993)]. //! Roughness is modelled by tanh profile [see e.g. Phys. Rev. B, vol. 47 (8), p. 4385 (1993)].
BA_CORE_API_ std::vector<ScalarRTCoefficients> Execute(const std::vector<Slice>& slices, BA_CORE_API_ std::vector<std::unique_ptr<ScalarRTCoefficients>>
const std::vector<complex_t>& kz); Execute(const std::vector<Slice>& slices, const std::vector<complex_t>& kz);
}; // namespace SpecularMatrix }; // namespace SpecularMatrix
#endif // SPECULARSCALARSTRATEGY_H #endif // SPECULARSCALARSTRATEGY_H
...@@ -47,7 +47,6 @@ TEST_F(SpecularMagneticTest, zerofield) ...@@ -47,7 +47,6 @@ TEST_F(SpecularMagneticTest, zerofield)
Layer substr_layer_scalar(substr_material_scalar); Layer substr_layer_scalar(substr_material_scalar);
multi_layer_scalar.addLayer(air_layer); multi_layer_scalar.addLayer(air_layer);
multi_layer_scalar.addLayer(substr_layer_scalar); multi_layer_scalar.addLayer(substr_layer_scalar);
std::vector<ScalarRTCoefficients> coeffs_scalar;
MultiLayer multi_layer_zerofield; MultiLayer multi_layer_zerofield;
Material substr_material_zerofield = HomogeneousMaterial("Substrate", 7e-6, 2e-8, substr_field); Material substr_material_zerofield = HomogeneousMaterial("Substrate", 7e-6, 2e-8, substr_field);
...@@ -60,8 +59,8 @@ TEST_F(SpecularMagneticTest, zerofield) ...@@ -60,8 +59,8 @@ TEST_F(SpecularMagneticTest, zerofield)
ProcessedSample sample_zerofield(multi_layer_zerofield, options); ProcessedSample sample_zerofield(multi_layer_zerofield, options);
// k1 // k1
coeffs_scalar = std::make_unique<SpecularScalarStrategy>()->Execute(sample_scalar.slices(), k1); auto coeffs_scalar = std::make_unique<SpecularScalarStrategy>()->Execute(sample_scalar.slices(), k1);
ScalarRTCoefficients RTScalar = coeffs_scalar[1]; ScalarRTCoefficients RTScalar = *dynamic_cast<const ScalarRTCoefficients*>(coeffs_scalar[1].get());
Eigen::Vector2cd TPS = RTScalar.T1plus() + RTScalar.T2plus(); Eigen::Vector2cd TPS = RTScalar.T1plus() + RTScalar.T2plus();
Eigen::Vector2cd RPS = RTScalar.R1plus() + RTScalar.R2plus(); Eigen::Vector2cd RPS = RTScalar.R1plus() + RTScalar.R2plus();
Eigen::Vector2cd TMS = RTScalar.T1min() + RTScalar.T2min(); Eigen::Vector2cd TMS = RTScalar.T1min() + RTScalar.T2min();
...@@ -85,7 +84,7 @@ TEST_F(SpecularMagneticTest, zerofield) ...@@ -85,7 +84,7 @@ TEST_F(SpecularMagneticTest, zerofield)
// k2 // k2
coeffs_scalar = std::make_unique<SpecularScalarStrategy>()->Execute(sample_scalar.slices(), k2); coeffs_scalar = std::make_unique<SpecularScalarStrategy>()->Execute(sample_scalar.slices(), k2);
RTScalar = coeffs_scalar[1]; RTScalar = *dynamic_cast<const ScalarRTCoefficients*>(coeffs_scalar[1].get());
TPS = RTScalar.T1plus() + RTScalar.T2plus(); TPS = RTScalar.T1plus() + RTScalar.T2plus();
RPS = RTScalar.R1plus() + RTScalar.R2plus(); RPS = RTScalar.R1plus() + RTScalar.R2plus();
TMS = RTScalar.T1min() + RTScalar.T2min(); TMS = RTScalar.T1min() + RTScalar.T2min();
...@@ -109,7 +108,7 @@ TEST_F(SpecularMagneticTest, zerofield) ...@@ -109,7 +108,7 @@ TEST_F(SpecularMagneticTest, zerofield)
// k3 // k3
coeffs_scalar = std::make_unique<SpecularScalarStrategy>()->Execute(sample_scalar.slices(), k3); coeffs_scalar = std::make_unique<SpecularScalarStrategy>()->Execute(sample_scalar.slices(), k3);
RTScalar = coeffs_scalar[1]; RTScalar = *dynamic_cast<const ScalarRTCoefficients*>(coeffs_scalar[1].get());
TPS = RTScalar.T1plus() + RTScalar.T2plus(); TPS = RTScalar.T1plus() + RTScalar.T2plus();
RPS = RTScalar.R1plus() + RTScalar.R2plus(); RPS = RTScalar.R1plus() + RTScalar.R2plus();
TMS = RTScalar.T1min() + RTScalar.T2min(); TMS = RTScalar.T1min() + RTScalar.T2min();
......
...@@ -33,7 +33,7 @@ void SpecularMagneticTest_v2::testZeroField(const kvector_t& k, const ProcessedS ...@@ -33,7 +33,7 @@ void SpecularMagneticTest_v2::testZeroField(const kvector_t& k, const ProcessedS
EXPECT_EQ(coeffs_scalar.size(), coeffs_zerofield.size()); EXPECT_EQ(coeffs_scalar.size(), coeffs_zerofield.size());
for (size_t i = 0; i < coeffs_scalar.size(); ++i) { for (size_t i = 0; i < coeffs_scalar.size(); ++i) {
const ScalarRTCoefficients& RTScalar = coeffs_scalar[i]; const ScalarRTCoefficients& RTScalar = *dynamic_cast<const ScalarRTCoefficients*>(coeffs_scalar[i].get());
Eigen::Vector2cd TPS = RTScalar.T1plus() + RTScalar.T2plus(); Eigen::Vector2cd TPS = RTScalar.T1plus() + RTScalar.T2plus();
Eigen::Vector2cd RPS = RTScalar.R1plus() + RTScalar.R2plus(); Eigen::Vector2cd RPS = RTScalar.R1plus() + RTScalar.R2plus();
Eigen::Vector2cd TMS = RTScalar.T1min() + RTScalar.T2min(); Eigen::Vector2cd TMS = RTScalar.T1min() + RTScalar.T2min();
......
...@@ -32,6 +32,14 @@ protected: ...@@ -32,6 +32,14 @@ protected:
EXPECT_NEAR(coeff1.t_r(1).real(), coeff2.t_r(1).real(), 1e-10); EXPECT_NEAR(coeff1.t_r(1).real(), coeff2.t_r(1).real(), 1e-10);
EXPECT_NEAR(coeff1.t_r(1).imag(), coeff2.t_r(1).imag(), 1e-10); EXPECT_NEAR(coeff1.t_r(1).imag(), coeff2.t_r(1).imag(), 1e-10);
} }
std::vector<ScalarRTCoefficients> getCoeffs(std::vector<std::unique_ptr<ScalarRTCoefficients>>&& inputCoeffs)
{
std::vector<ScalarRTCoefficients> result;
for(auto& coeff : inputCoeffs)
result.push_back( *dynamic_cast<const ScalarRTCoefficients*>(coeff.get()) );
return result;
}
const Material air = HomogeneousMaterial("air", 1e-8, 1e-8); const Material air = HomogeneousMaterial("air", 1e-8, 1e-8);
const Material amat = HomogeneousMaterial("material A", 2e-6, 8e-7); const Material amat = HomogeneousMaterial("material A", 2e-6, 8e-7);
const Material bmat = HomogeneousMaterial("material B (high absorption)", 3e-5, 2e-4); const Material bmat = HomogeneousMaterial("material B (high absorption)", 3e-5, 2e-4);
...@@ -62,8 +70,8 @@ TEST_F(RTTest, SplitLayer) ...@@ -62,8 +70,8 @@ TEST_F(RTTest, SplitLayer)
ProcessedSample sample_1(sample1, options); ProcessedSample sample_1(sample1, options);
ProcessedSample sample_2(sample2, options); ProcessedSample sample_2(sample2, options);
coeffs1 = std::make_unique<SpecularScalarStrategy>()->Execute(sample_1.slices(), k); coeffs1 = getCoeffs( std::make_unique<SpecularScalarStrategy>()->Execute(sample_1.slices(), k) );
coeffs2 = std::make_unique<SpecularScalarStrategy>()->Execute(sample_2.slices(), k); coeffs2 = getCoeffs( std::make_unique<SpecularScalarStrategy>()->Execute(sample_2.slices(), k) );
// printCoeffs( coeffs1 ); // printCoeffs( coeffs1 );
// printCoeffs( coeffs2 ); // printCoeffs( coeffs2 );
...@@ -99,8 +107,8 @@ TEST_F(RTTest, SplitBilayers) ...@@ -99,8 +107,8 @@ TEST_F(RTTest, SplitBilayers)
ProcessedSample sample_1(sample1, options); ProcessedSample sample_1(sample1, options);
ProcessedSample sample_2(sample2, options); ProcessedSample sample_2(sample2, options);
coeffs1 = std::make_unique<SpecularScalarStrategy>()->Execute(sample_1.slices(), k); coeffs1 = getCoeffs( std::make_unique<SpecularScalarStrategy>()->Execute(sample_1.slices(), k) );
coeffs2 = std::make_unique<SpecularScalarStrategy>()->Execute(sample_2.slices(), k); coeffs2 = getCoeffs( std::make_unique<SpecularScalarStrategy>()->Execute(sample_2.slices(), k) );
// printCoeffs( coeffs1 ); // printCoeffs( coeffs1 );
// printCoeffs( coeffs2 ); // printCoeffs( coeffs2 );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment