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

#include "Sample/SoftParticle/FuzzySphere.h"
#include "Base/Util/Assert.h"
#include "Sample/LibFF/SomeFormfactor.h"
#include "Sample/Shape/TruncatedEllipsoidNet.h"

FuzzySphere::FuzzySphere(double mean, double sigma)
    : SoftFormfactor()
    , m_radius(mean)
    , m_sigma(sigma)
{
    FuzzySphere::validate();
}

complex_t FuzzySphere::formfactorAtBottom(C3 q) const
{
    ASSERT(m_validated);
    // Use analytic square for Debye-Waller factor
    auto q2 = q.analytic_square();
    auto dw = std::exp(-q2 * m_sigma * m_sigma / 2.0);
    return dw * SampleUtil::someff::ffSphere(q, m_radius);
}

void FuzzySphere::validate() const
{
    if (m_radius <= 0)
        throw std::runtime_error("radius <= 0");
    if (m_sigma <= 0)
        throw std::runtime_error("sigma <= 0");

    m_shape3D =
        std::make_unique<TruncatedEllipsoidNet>(m_radius, m_radius, m_radius, 2 * m_radius, 0);

    m_validated = true;
}
