//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      Sample/SoftParticle/GaussSphere.cpp
//! @brief     Implements class GaussSphere.
//!
//! @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/GaussSphere.h"
#include "Base/Util/Assert.h"
#include "Sample/Shape/BoxNet.h"
#include "Sample/Shape/TruncatedEllipsoidNet.h"
#include <limits>
#include <numbers>

using std::numbers::pi;

GaussSphere::GaussSphere(double mean_radius)
    : SoftFormfactor()
    , m_mean_radius(mean_radius)
{
    GaussSphere::validate();
}

void GaussSphere::validate() const
{
    if (m_mean_radius < 0)
        throw std::runtime_error("mean_radius < 0");

    const double R = m_mean_radius;
    m_shape3D = std::make_unique<TruncatedEllipsoidNet>(R, R, R, 2 * R, 0);

    m_validated = true;
}
complex_t GaussSphere::formfactorAtBottom(C3 q) const
{
    ASSERT(m_validated);
    // Use analytic square for Gaussian form factor
    const auto qR = q * m_mean_radius;
    const auto q2R2 = qR.analytic_square();
    return pow(m_mean_radius, 3) * std::exp(-q2R2 / 4.0 / pi);
}
