///////////////////////////////////////////////////////////////////////////////
//
// File: RiemannSolver.h
//
// For more information, please see: http://www.nektar.info
//
// The MIT License
//
// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
// Department of Aeronautics, Imperial College London (UK), and Scientific
// Computing and Imaging Institute, University of Utah (USA).
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// Description: Abstract base class for Riemann solvers with factory.
//
///////////////////////////////////////////////////////////////////////////////

#ifndef NEKTAR_SOLVERUTILS_RIEMANNSOLVER
#define NEKTAR_SOLVERUTILS_RIEMANNSOLVER

#include <LibUtilities/BasicUtils/NekFactory.hpp>
#include <LibUtilities/BasicUtils/SessionReader.h>
#include <LibUtilities/LinearAlgebra/NekTypeDefs.hpp>
#include <LibUtilities/SimdLib/traits.hpp>
#include <SolverUtils/SolverUtilsDeclspec.h>

#include <string>

namespace Nektar
{
template <typename Dim, typename DataType> class Array;

namespace SolverUtils
{
typedef std::function<const Array<OneD, const NekDouble> &()> RSScalarFuncType;
typedef std::function<const Array<OneD, const Array<OneD, NekDouble>> &()>
    RSVecFuncType;
typedef std::function<NekDouble()> RSParamFuncType;
typedef std::function<bool()> RSFlagype;

class RiemannSolver
{
public:
    SOLVER_UTILS_EXPORT void Solve(
        const int nDim, const Array<OneD, const Array<OneD, NekDouble>> &Fwd,
        const Array<OneD, const Array<OneD, NekDouble>> &Bwd,
        Array<OneD, Array<OneD, NekDouble>> &flux);

    template <typename FuncPointerT, typename ObjectPointerT>
    void SetScalar(std::string name, FuncPointerT func, ObjectPointerT obj)
    {
        m_scalars[name] = std::bind(func, obj);
    }

    void SetScalar(std::string name, RSScalarFuncType fp)
    {
        m_scalars[name] = fp;
    }

    template <typename FuncPointerT, typename ObjectPointerT>
    void SetVector(std::string name, FuncPointerT func, ObjectPointerT obj)
    {
        m_vectors[name] = std::bind(func, obj);
    }

    void SetVector(std::string name, RSVecFuncType fp)
    {
        m_vectors[name] = fp;
    }

    template <typename FuncPointerT, typename ObjectPointerT>
    void SetParam(std::string name, FuncPointerT func, ObjectPointerT obj)
    {
        m_params[name] = std::bind(func, obj);
    }

    template <typename FuncPointerT, typename ObjectPointerT>
    void SetUpdateNormalsFlag(FuncPointerT func, ObjectPointerT obj)
    {
        m_updateNormals = std::bind(func, obj);
    }

    void SetALEFlag(bool &ALE)
    {
        m_ALESolver = ALE;
    }

    void SetParam(std::string name, RSParamFuncType fp)
    {
        m_params[name] = fp;
    }

    template <typename FuncPointerT, typename ObjectPointerT>
    void SetAuxScal(std::string name, FuncPointerT func, ObjectPointerT obj)
    {
        m_auxScal[name] = std::bind(func, obj);
    }

    template <typename FuncPointerT, typename ObjectPointerT>
    void SetAuxVec(std::string name, FuncPointerT func, ObjectPointerT obj)
    {
        m_auxVec[name] = std::bind(func, obj);
    }

    void SetAuxVec(std::string name, RSVecFuncType fp)
    {
        m_auxVec[name] = fp;
    }

    std::map<std::string, RSScalarFuncType> &GetScalars()
    {
        return m_scalars;
    }

    std::map<std::string, RSVecFuncType> &GetVectors()
    {
        return m_vectors;
    }

    std::map<std::string, RSParamFuncType> &GetParams()
    {
        return m_params;
    }

    int m_spacedim;

    SOLVER_UTILS_EXPORT void CalcFluxJacobian(
        const int nDim, const Array<OneD, const Array<OneD, NekDouble>> &Fwd,
        const Array<OneD, const Array<OneD, NekDouble>> &Bwd,
        DNekBlkMatSharedPtr &FJac, DNekBlkMatSharedPtr &BJac);

    SOLVER_UTILS_EXPORT void ResetRotMatrix(void)
    {
        m_rotMat = NullNekDoubleArrayOfArray;
    }

protected:
    /// Indicates whether the Riemann solver requires a rotation to be
    /// applied to the velocity fields.
    bool m_requiresRotation;
    /// Map of scalar function types.
    std::map<std::string, RSScalarFuncType> m_scalars;
    /// Map of vector function types.
    std::map<std::string, RSVecFuncType> m_vectors;
    /// Map of parameter function types.
    std::map<std::string, RSParamFuncType> m_params;
    /// Map of auxiliary scalar function types.
    std::map<std::string, RSScalarFuncType> m_auxScal;
    /// Map of auxiliary vector function types.
    std::map<std::string, RSVecFuncType> m_auxVec;
    /// Function of normals updated flag
    std::function<bool()> m_updateNormals;
    /// Rotation matrices for each trace quadrature point.
    Array<OneD, Array<OneD, NekDouble>> m_rotMat;
    /// Rotation storage
    Array<OneD, Array<OneD, Array<OneD, NekDouble>>> m_rotStorage;
    /// Flag if using the ALE formulation
    bool m_ALESolver = false;

    SOLVER_UTILS_EXPORT RiemannSolver();
    SOLVER_UTILS_EXPORT RiemannSolver(
        const LibUtilities::SessionReaderSharedPtr &pSession);

    SOLVER_UTILS_EXPORT virtual ~RiemannSolver(){};

    virtual void v_Solve(const int nDim,
                         const Array<OneD, const Array<OneD, NekDouble>> &Fwd,
                         const Array<OneD, const Array<OneD, NekDouble>> &Bwd,
                         Array<OneD, Array<OneD, NekDouble>> &flux) = 0;

    SOLVER_UTILS_EXPORT void GenerateRotationMatrices(
        const Array<OneD, const Array<OneD, NekDouble>> &normals);
    void FromToRotation(Array<OneD, const NekDouble> &from,
                        Array<OneD, const NekDouble> &to, NekDouble *mat);
    SOLVER_UTILS_EXPORT void rotateToNormal(
        const Array<OneD, const Array<OneD, NekDouble>> &inarray,
        const Array<OneD, const Array<OneD, NekDouble>> &normals,
        const Array<OneD, const Array<OneD, NekDouble>> &vecLocs,
        Array<OneD, Array<OneD, NekDouble>> &outarray);
    SOLVER_UTILS_EXPORT void rotateFromNormal(
        const Array<OneD, const Array<OneD, NekDouble>> &inarray,
        const Array<OneD, const Array<OneD, NekDouble>> &normals,
        const Array<OneD, const Array<OneD, NekDouble>> &vecLocs,
        Array<OneD, Array<OneD, NekDouble>> &outarray);
    SOLVER_UTILS_EXPORT bool CheckScalars(std::string name);
    SOLVER_UTILS_EXPORT bool CheckVectors(std::string name);
    SOLVER_UTILS_EXPORT bool CheckParams(std::string name);
    SOLVER_UTILS_EXPORT bool CheckAuxScal(std::string name);
    SOLVER_UTILS_EXPORT bool CheckAuxVec(std::string name);

    SOLVER_UTILS_EXPORT virtual void v_CalcFluxJacobian(
        const int nDim, const Array<OneD, const Array<OneD, NekDouble>> &Fwd,
        const Array<OneD, const Array<OneD, NekDouble>> &Bwd,
        const Array<OneD, const Array<OneD, NekDouble>> &normals,
        DNekBlkMatSharedPtr &FJac, DNekBlkMatSharedPtr &BJac);
};

/// A shared pointer to an EquationSystem object
typedef std::shared_ptr<RiemannSolver> RiemannSolverSharedPtr;
/// Datatype of the NekFactory used to instantiate classes derived
/// from the RiemannSolver class.
typedef LibUtilities::NekFactory<std::string, RiemannSolver,
                                 const LibUtilities::SessionReaderSharedPtr &>
    RiemannSolverFactory;
SOLVER_UTILS_EXPORT RiemannSolverFactory &GetRiemannSolverFactory();

template <class T, typename = typename std::enable_if<
                       std::is_floating_point_v<T> ||
                       tinysimd::is_vector_floating_point_v<T>>::type>
inline void rotateToNormalKernel(T *in, T *rotMat, T *out)
{

    // Apply rotation matrices.
    out[0] = in[0] * rotMat[0] + in[1] * rotMat[1] + in[2] * rotMat[2];

    out[1] = in[0] * rotMat[3] + in[1] * rotMat[4] + in[2] * rotMat[5];

    out[2] = in[0] * rotMat[6] + in[1] * rotMat[7] + in[2] * rotMat[8];
}

template <class T, typename = typename std::enable_if<
                       std::is_floating_point_v<T> ||
                       tinysimd::is_vector_floating_point_v<T>>::type>
inline void rotateFromNormalKernel(T *in, T *rotMat, T *out)
{

    // Apply rotation matrices.
    out[0] = in[0] * rotMat[0] + in[1] * rotMat[3] + in[2] * rotMat[6];

    out[1] = in[0] * rotMat[1] + in[1] * rotMat[4] + in[2] * rotMat[7];

    out[2] = in[0] * rotMat[2] + in[1] * rotMat[5] + in[2] * rotMat[8];
}

} // namespace SolverUtils
} // namespace Nektar

#endif
