//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      Sample/Aggregate/Struct2D.cpp
//! @brief     Implements class Struct2D.
//!
//! @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/Aggregate/Struct2D.h"
#include "Base/Spin/SpinMatrix.h"
#include "Sample/Element/DiffuseElement.h"
#include "Sample/Particle/IParticle.h"

double Struct2D::scalarScattering(const DiffuseElement& ele) const
{
    const auto [mean, variance] = particle()->scatteringMoments(ele);
    return variance + structureFactor(ele.meanQ()) * std::norm(mean);
}

double Struct2D::polarizedScattering(const DiffuseElement& ele) const
{
    const auto [mean, variance] = particle()->polScatteringMoments(ele);
    const SpinMatrix coherent = mean * ele.polarizer() * mean.adjoint();

    return SpinMatrix::intensityTrace(ele.analyzer(), variance)
           + structureFactor(ele.meanQ()) * SpinMatrix::intensityTrace(ele.analyzer(), coherent);
}
