//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      Sample/Aggregate/Crystal1D.cpp
//! @brief     Implements class Crystal1D.
//!
//! @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/Aggregate/Crystal1D.h"
#include "Base/Util/Assert.h"
#include "Sample/Correlation/Profiles1D.h"
#include "Sample/Correlation/Profiles2D.h"
#include <algorithm>
#include <limits>

namespace {

// maximum value for qx*Lambdax
const int nmax = 20;
// minimum number of neighboring reciprocal lattice points to use
const int min_points = 4;

} // namespace

//! Constructor of interference function of one-dimensional lattice.
//! @param particle: particle (or Mixture) populating the lattice
//! @param length: lattice constant in nanometers
//! @param xi: rotation of lattice with respect to x-axis in radians
//! @param length_density: density of independent parallel lattices on surface
Crystal1D::Crystal1D(const IParticle& particle, double length, double xi, double linear_density)
    : Ordered2D(particle)
    , LongRangeOrdered2D(particle, 0)
    , m_length(length)
    , m_xi(xi)
    , m_linear_density(linear_density)
    , m_na{0}
{
    Crystal1D::validate();
}

Crystal1D::~Crystal1D() = default;

void Crystal1D::setDecayFunction(const Profile1D& decay)
{
    m_decay.reset(decay.clone());
    double decay_length = m_decay->decayLength();
    double qa_max = m_length * nmax / decay_length / (2 * pi);
    m_na = static_cast<int>(std::lround(std::abs(qa_max) + 0.5));
    m_na = std::max(m_na, min_points);
}

Crystal1D* Crystal1D::clone() const
{
    auto* result = new Crystal1D(*m_particle, m_length, m_xi, m_linear_density);
    instructLRClone(result);
    if (m_decay)
        result->setDecayFunction(*m_decay);
    return result;
}

void Crystal1D::validate() const
{
    if (m_length <= 0)
        throw std::runtime_error("length <= 0");
    if (m_linear_density <= 0)
        throw std::runtime_error("linear density <= 0");

    Struct::validate();
    m_validated = true;
}

double Crystal1D::decayLength() const
{
    return m_decay ? m_decay->decayLength() : std::numeric_limits<double>::infinity();
}

double Crystal1D::iff_without_dw(const R3& q) const
{
    ASSERT(m_decay);
    double result = 0.0;
    double qxr = q.x();
    double qyr = q.y();
    double qx_frac;
    double xi = m_xi;
    double a = m_length;
    double a_rec = (2 * pi) / a;

    // rotate the q vector to xi angle
    // so that qx_prime is along the a axis of lattice
    double qx_prime = qxr * std::cos(xi) + qyr * std::sin(xi);

    // calculate reciprocal vector fraction
    int qa_int = static_cast<int>(qx_prime / a_rec);
    qx_frac = qx_prime - qa_int * a_rec;

    for (int i = -m_na; i < m_na + 1; ++i) {
        double qx = qx_frac + i * a_rec;
        result += m_decay->decayFT(qx);
    }
    return result / a;
}
