//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      Sample/Multilayer/Slice.cpp
//! @brief     Implements class Slice.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2025
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "Sample/Multilayer/Slice.h"
#include "Base/Util/Assert.h"
#include "Sample/Material/Material.h"
#include "Sample/Multilayer/Layer.h"
#include <numbers>

Slice::Slice(size_t number, const Layer& layer, const Span& z_span, const R3& B_field)
    : PlainLayer(*layer.material(), z_span, layer.roughness())
    , m_slice_number(number)
    , m_refracting_material(layer.material()->clone())
    , m_B_field(B_field)
{
    setRms(layer.rms());
}

void Slice::setRefractingMaterial(const Material& material)
{
    m_refracting_material.reset(material.clone());
}

void Slice::initBField(R3 h_field, double h_z)
{
    ASSERT(m_refracting_material); // refracting material is initialized via slicing procedure
    static constexpr double magnetic_permeability = 4e-7 * std::numbers::pi;
    m_B_field = magnetic_permeability * (h_field + m_refracting_material->magnetization());
    m_B_field.setZ(magnetic_permeability * h_z);
}
