/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | www.openfoam.com
     \\/     M anipulation  |
-------------------------------------------------------------------------------
    Copyright (C) 2011-2017 OpenFOAM Foundation
    Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

#include "DimensionedScalargpuField.H"

#define TEMPLATE template<class GeoMesh>
#include "DimensionedgpuFieldFunctionsM.C"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

namespace Foam
{

// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

template<class GeoMesh>
tmp<DimensionedgpuField<scalar, GeoMesh>> stabilise
(
    const DimensionedgpuField<scalar, GeoMesh>& dsf,
    const dimensioned<scalar>& ds
)
{
    auto tres =
        tmp<DimensionedgpuField<scalar, GeoMesh>>::New
        (
			IOobject
			(
				"stabilise(" + dsf.name() + ',' + ds.name() + ')',
				dsf.instance(),
				dsf.db()
			),
            dsf.mesh(),
            dsf.dimensions() + ds.dimensions()
        );

    stabilise(tres.ref().field(), dsf.field(), ds.value());

    return tres;
}


template<class GeoMesh>
tmp<DimensionedgpuField<scalar, GeoMesh>> stabilise
(
    const tmp<DimensionedgpuField<scalar, GeoMesh>>& tdsf,
    const dimensioned<scalar>& ds
)
{
    const DimensionedgpuField<scalar, GeoMesh>& dsf = tdsf();

    tmp<DimensionedgpuField<scalar, GeoMesh>> tres = New
    (
        tdsf,
        "stabilise(" + dsf.name() + ',' + ds.name() + ')',
        dsf.dimensions() + ds.dimensions()
    );

    stabilise(tres.ref().field(), dsf.field(), ds.value());

    tdsf.clear();

    return tres;
}


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

BINARY_TYPE_OPERATOR(scalar, scalar, scalar, +, '+', add)
BINARY_TYPE_OPERATOR(scalar, scalar, scalar, -, '-', subtract)

BINARY_OPERATOR(scalar, scalar, scalar, *, '*', multiply)
BINARY_OPERATOR(scalar, scalar, scalar, /, '|', divide)

BINARY_TYPE_OPERATOR_SF(scalar, scalar, scalar, /, '|', divide)

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

template<class GeoMesh>
tmp<DimensionedgpuField<scalar, GeoMesh>> pow
(
    const DimensionedgpuField<scalar, GeoMesh>& dsf1,
    const DimensionedgpuField<scalar, GeoMesh>& dsf2
)
{
    if (!dsf1.dimensions().dimensionless())
    {
        FatalErrorInFunction
            << "Base field is not dimensionless: " << dsf1.dimensions()
            << exit(FatalError);
    }

    if (!dsf2.dimensions().dimensionless())
    {
        FatalErrorInFunction
            << "Exponent field is not dimensionless: " << dsf2.dimensions()
            << exit(FatalError);
    }

    auto tres =
        tmp<DimensionedgpuField<scalar, GeoMesh>>::New
        (
			IOobject
			(
				"pow(" + dsf1.name() + ',' + dsf2.name() + ')',
				dsf1.instance(),
				dsf1.db()
			),
            dsf1.mesh(),
            dimless
        );

    pow(tres.ref().field(), dsf1.field(), dsf2.field());

    return tres;
}


template<class GeoMesh>
tmp<DimensionedgpuField<scalar, GeoMesh>> pow
(
    const tmp<DimensionedgpuField<scalar, GeoMesh>>& tdsf1,
    const DimensionedgpuField<scalar, GeoMesh>& dsf2
)
{
    const DimensionedgpuField<scalar, GeoMesh>& dsf1 = tdsf1();

    if (!dsf1.dimensions().dimensionless())
    {
        FatalErrorInFunction
            << "Base field is not dimensionless: " << dsf1.dimensions()
            << exit(FatalError);
    }

    if (!dsf2.dimensions().dimensionless())
    {
        FatalErrorInFunction
            << "Exponent field is not dimensionless: " << dsf2.dimensions()
            << exit(FatalError);
    }

    tmp<DimensionedgpuField<scalar, GeoMesh>> tres = New
    (
        tdsf1,
        "pow(" + dsf1.name() + ',' + dsf2.name() + ')',
        dimless
    );

    pow(tres.ref().field(), dsf1.field(), dsf2.field());

    tdsf1.clear();

    return tres;
}


template<class GeoMesh>
tmp<DimensionedgpuField<scalar, GeoMesh>> pow
(
    const DimensionedgpuField<scalar, GeoMesh>& dsf1,
    const tmp<DimensionedgpuField<scalar, GeoMesh>>& tdsf2
)
{
    const DimensionedgpuField<scalar, GeoMesh>& dsf2 = tdsf2();

    if (!dsf1.dimensions().dimensionless())
    {
        FatalErrorInFunction
            << "Base field is not dimensionless: " << dsf1.dimensions()
            << exit(FatalError);
    }

    if (!dsf2.dimensions().dimensionless())
    {
        FatalErrorInFunction
            << "Exponent field is not dimensionless: " << dsf2.dimensions()
            << exit(FatalError);
    }

    tmp<DimensionedgpuField<scalar, GeoMesh>> tres = New
    (
        tdsf2,
        "pow(" + dsf1.name() + ',' + dsf2.name() + ')',
        dimless
    );

    pow(tres.ref().field(), dsf1.field(), dsf2.field());

    tdsf2.clear();

    return tres;
}


template<class GeoMesh>
tmp<DimensionedgpuField<scalar, GeoMesh>> pow
(
    const tmp<DimensionedgpuField<scalar, GeoMesh>>& tdsf1,
    const tmp<DimensionedgpuField<scalar, GeoMesh>>& tdsf2
)
{
    const DimensionedgpuField<scalar, GeoMesh>& dsf1 = tdsf1();
    const DimensionedgpuField<scalar, GeoMesh>& dsf2 = tdsf2();

    if (!dsf1.dimensions().dimensionless())
    {
        FatalErrorInFunction
            << "Base field is not dimensionless: " << dsf1.dimensions()
            << exit(FatalError);
    }

    if (!dsf2.dimensions().dimensionless())
    {
        FatalErrorInFunction
            << "Exponent field is not dimensionless: " << dsf2.dimensions()
            << exit(FatalError);
    }

    auto tres =
        reuseTmpTmpDimensionedgpuField<scalar, scalar, scalar, scalar, GeoMesh>::
        New
        (
            tdsf1,
            tdsf2,
            "pow(" + dsf1.name() + ',' + dsf2.name() + ')',
            dimless
        );

    pow(tres.ref().field(), dsf1.field(), dsf2.field());

    tdsf1.clear();
    tdsf2.clear();

    return tres;
}


template<class GeoMesh>
tmp<DimensionedgpuField<scalar, GeoMesh>> pow
(
    const DimensionedgpuField<scalar, GeoMesh>& dsf,
    const dimensionedScalar& ds
)
{
    if (!ds.dimensions().dimensionless())
    {
        FatalErrorInFunction
            << "Exponent is not dimensionless: " << ds.dimensions()
            << exit(FatalError);
    }

    auto tres =
        tmp<DimensionedgpuField<scalar, GeoMesh>>::New
        (
			IOobject
			(
				"pow(" + dsf.name() + ',' + ds.name() + ')',
				dsf.instance(),
				dsf.db()
			),
            dsf.mesh(),
            pow(dsf.dimensions(), ds)
        );

    pow(tres.ref().field(), dsf.field(), ds.value());

    return tres;
}


template<class GeoMesh>
tmp<DimensionedgpuField<scalar, GeoMesh>> pow
(
    const tmp<DimensionedgpuField<scalar, GeoMesh>>& tdsf,
    const dimensionedScalar& ds
)
{
    if (!ds.dimensions().dimensionless())
    {
        FatalErrorInFunction
            << "Exponent is not dimensionless: " << ds.dimensions()
            << exit(FatalError);
    }

    const DimensionedgpuField<scalar, GeoMesh>& dsf = tdsf();

    tmp<DimensionedgpuField<scalar, GeoMesh>> tres = New
    (
        tdsf,
        "pow(" + dsf.name() + ',' + ds.name() + ')',
        pow(dsf.dimensions(), ds)
    );

    pow(tres.ref().field(), dsf.field(), ds.value());

    tdsf.clear();

    return tres;
}


template<class GeoMesh>
tmp<DimensionedgpuField<scalar, GeoMesh>> pow
(
    const DimensionedgpuField<scalar, GeoMesh>& dsf,
    const scalar& s
)
{
    return pow(dsf, dimensionedScalar(s));
}


template<class GeoMesh>
tmp<DimensionedgpuField<scalar, GeoMesh>> pow
(
    const tmp<DimensionedgpuField<scalar, GeoMesh>>& tdsf,
    const scalar& s
)
{
    return pow(tdsf, dimensionedScalar(s));
}


template<class GeoMesh>
tmp<DimensionedgpuField<scalar, GeoMesh>> pow
(
    const dimensionedScalar& ds,
    const DimensionedgpuField<scalar, GeoMesh>& dsf
)
{
    if (!ds.dimensions().dimensionless())
    {
        FatalErrorInFunction
            << "Base scalar is not dimensionless: " << ds.dimensions()
            << exit(FatalError);
    }

    if (!dsf.dimensions().dimensionless())
    {
        FatalErrorInFunction
            << "Exponent field is not dimensionless: " << dsf.dimensions()
            << exit(FatalError);
    }

    auto tres =
        tmp<DimensionedgpuField<scalar, GeoMesh>>::New
        (
			IOobject
			(
				"pow(" + ds.name() + ',' + dsf.name() + ')',
				dsf.instance(),
				dsf.db()
			),
            dsf.mesh(),
            dimless
        );

    pow(tres.ref().field(), ds.value(), dsf.field());

    return tres;
}


template<class GeoMesh>
tmp<DimensionedgpuField<scalar, GeoMesh>> pow
(
    const dimensionedScalar& ds,
    const tmp<DimensionedgpuField<scalar, GeoMesh>>& tdsf
)
{
    const DimensionedgpuField<scalar, GeoMesh>& dsf = tdsf();

    if (!ds.dimensions().dimensionless())
    {
        FatalErrorInFunction
            << "Base scalar is not dimensionless: " << ds.dimensions()
            << exit(FatalError);
    }

    if (!dsf.dimensions().dimensionless())
    {
        FatalErrorInFunction
            << "Exponent field is not dimensionless: " << dsf.dimensions()
            << exit(FatalError);
    }

    tmp<DimensionedgpuField<scalar, GeoMesh>> tres = New
    (
        tdsf,
        "pow(" + ds.name() + ',' + dsf.name() + ')',
        dimless
    );

    pow(tres.ref().field(), ds.value(), dsf.field());

    tdsf.clear();

    return tres;
}

template<class GeoMesh>
tmp<DimensionedgpuField<scalar, GeoMesh>> pow
(
    const scalar& s,
    const DimensionedgpuField<scalar, GeoMesh>& dsf
)
{
    return pow(dimensionedScalar(s), dsf);
}

template<class GeoMesh>
tmp<DimensionedgpuField<scalar, GeoMesh>> pow
(
    const scalar& s,
    const tmp<DimensionedgpuField<scalar, GeoMesh>>& tdsf
)
{
    return pow(dimensionedScalar(s), tdsf);
}


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

template<class GeoMesh>
tmp<DimensionedgpuField<scalar, GeoMesh>> atan2
(
    const DimensionedgpuField<scalar, GeoMesh>& dsf1,
    const DimensionedgpuField<scalar, GeoMesh>& dsf2
)
{
    auto tres =
        tmp<DimensionedgpuField<scalar, GeoMesh>>::New
        (
			IOobject
			(
				"atan2(" + dsf1.name() + ',' + dsf2.name() + ')',
				dsf1.instance(),
				dsf1.db()
			),
            dsf1.mesh(),
            atan2(dsf1.dimensions(), dsf2.dimensions())
        );

    atan2(tres.ref().field(), dsf1.field(), dsf2.field());

    return tres;
}


template<class GeoMesh>
tmp<DimensionedgpuField<scalar, GeoMesh>> atan2
(
    const tmp<DimensionedgpuField<scalar, GeoMesh>>& tdsf1,
    const DimensionedgpuField<scalar, GeoMesh>& dsf2
)
{
    const DimensionedgpuField<scalar, GeoMesh>& dsf1 = tdsf1();

    tmp<DimensionedgpuField<scalar, GeoMesh>> tres = New
    (
        tdsf1,
        "atan2(" + dsf1.name() + ',' + dsf2.name() + ')',
        atan2(dsf1.dimensions(), dsf2.dimensions())
    );

    atan2(tres.ref().field(), dsf1.field(), dsf2.field());

    tdsf1.clear();

    return tres;
}


template<class GeoMesh>
tmp<DimensionedgpuField<scalar, GeoMesh>> atan2
(
    const DimensionedgpuField<scalar, GeoMesh>& dsf1,
    const tmp<DimensionedgpuField<scalar, GeoMesh>>& tdsf2
)
{
    const DimensionedgpuField<scalar, GeoMesh>& dsf2 = tdsf2();

    tmp<DimensionedgpuField<scalar, GeoMesh>> tres = New
    (
        tdsf2,
		"atan2(" + dsf1.name() + ',' + dsf2.name() + ')',
        atan2(dsf1.dimensions(), dsf2.dimensions())
    );

    atan2(tres.ref().field(), dsf1.field(), dsf2.field());

    tdsf2.clear();

    return tres;
}

template<class GeoMesh>
tmp<DimensionedgpuField<scalar, GeoMesh>> atan2
(
    const tmp<DimensionedgpuField<scalar, GeoMesh>>& tdsf1,
    const tmp<DimensionedgpuField<scalar, GeoMesh>>& tdsf2
)
{
    const DimensionedgpuField<scalar, GeoMesh>& dsf1 = tdsf1();
    const DimensionedgpuField<scalar, GeoMesh>& dsf2 = tdsf2();

    auto tres =
        reuseTmpTmpDimensionedgpuField<scalar, scalar, scalar, scalar, GeoMesh>::
        New
        (
            tdsf1,
            tdsf2,
            "atan2(" + dsf1.name() + ',' + dsf2.name() + ')',
            atan2(dsf1.dimensions(), dsf2.dimensions())
        );

    atan2(tres.ref().field(), dsf1.field(), dsf2.field());

    tdsf1.clear();
    tdsf2.clear();

    return tres;
}


template<class GeoMesh>
tmp<DimensionedgpuField<scalar, GeoMesh>> atan2
(
    const DimensionedgpuField<scalar, GeoMesh>& dsf,
    const dimensionedScalar& ds
)
{
    auto tres =
        tmp<DimensionedgpuField<scalar, GeoMesh>>::New
        (
			IOobject
			(
				"atan2(" + dsf.name() + ',' + ds.name() + ')',
				dsf.instance(),
				dsf.db()
			),
            dsf.mesh(),
            atan2(dsf.dimensions(), ds)
        );

    atan2(tres.ref().field(), dsf.field(), ds.value());

    return tres;
}

template<class GeoMesh>
tmp<DimensionedgpuField<scalar, GeoMesh>> atan2
(
    const tmp<DimensionedgpuField<scalar, GeoMesh>>& tdsf,
    const dimensionedScalar& ds
)
{
    const DimensionedgpuField<scalar, GeoMesh>& dsf = tdsf();

    tmp<DimensionedgpuField<scalar, GeoMesh>> tres = New
    (
        tdsf,
        "atan2(" + dsf.name() + ',' + ds.name() + ')',
        atan2(dsf.dimensions(), ds)
    );

    atan2(tres.ref().field(), dsf.field(), ds.value());

    tdsf.clear();

    return tres;
}

template<class GeoMesh>
tmp<DimensionedgpuField<scalar, GeoMesh>> atan2
(
    const DimensionedgpuField<scalar, GeoMesh>& dsf,
    const scalar& s
)
{
    return atan2(dsf, dimensionedScalar(s));
}

template<class GeoMesh>
tmp<DimensionedgpuField<scalar, GeoMesh>> atan2
(
    const tmp<DimensionedgpuField<scalar, GeoMesh>>& tdsf,
    const scalar& s
)
{
    return atan2(tdsf, dimensionedScalar(s));
}


template<class GeoMesh>
tmp<DimensionedgpuField<scalar, GeoMesh>> atan2
(
    const dimensionedScalar& ds,
    const DimensionedgpuField<scalar, GeoMesh>& dsf
)
{
    auto tres =
        tmp<DimensionedgpuField<scalar, GeoMesh>>::New
        (
			IOobject
			(
				"atan2(" + ds.name() + ',' + dsf.name() + ')',
				dsf.instance(),
				dsf.db()
			),
            dsf.mesh(),
            atan2(ds, dsf.dimensions())
        );

    atan2(tres.ref().field(), ds.value(), dsf.field());

    return tres;
}


template<class GeoMesh>
tmp<DimensionedgpuField<scalar, GeoMesh>> atan2
(
    const dimensionedScalar& ds,
    const tmp<DimensionedgpuField<scalar, GeoMesh>>& tdsf
)
{
    const DimensionedgpuField<scalar, GeoMesh>& dsf = tdsf();

    tmp<DimensionedgpuField<scalar, GeoMesh>> tres = New
    (
        tdsf,
        "atan2(" + ds.name() + ',' + dsf.name() + ')',
        atan2(ds, dsf.dimensions())
    );

    atan2(tres.ref().field(), ds.value(), dsf.field());

    tdsf.clear();

    return tres;
}

template<class GeoMesh>
tmp<DimensionedgpuField<scalar, GeoMesh>> atan2
(
    const scalar& s,
    const DimensionedgpuField<scalar, GeoMesh>& dsf
)
{
    return atan2(dimensionedScalar(s), dsf);
}

template<class GeoMesh>
tmp<DimensionedgpuField<scalar, GeoMesh>> atan2
(
    const scalar& s,
    const tmp<DimensionedgpuField<scalar, GeoMesh>>& tdsf
)
{
    return atan2(dimensionedScalar(s), tdsf);
}


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

UNARY_FUNCTION(scalar, scalar, pow3, pow3)
UNARY_FUNCTION(scalar, scalar, pow4, pow4)
UNARY_FUNCTION(scalar, scalar, pow5, pow5)
UNARY_FUNCTION(scalar, scalar, pow6, pow6)
UNARY_FUNCTION(scalar, scalar, pow025, pow025)
UNARY_FUNCTION(scalar, scalar, sqrt, sqrt)
UNARY_FUNCTION(scalar, scalar, cbrt, cbrt)
UNARY_FUNCTION(scalar, scalar, sign, sign)
UNARY_FUNCTION(scalar, scalar, pos, pos)
UNARY_FUNCTION(scalar, scalar, pos0, pos0)
UNARY_FUNCTION(scalar, scalar, neg, neg)
UNARY_FUNCTION(scalar, scalar, neg0, neg0)
UNARY_FUNCTION(scalar, scalar, posPart, posPart)
UNARY_FUNCTION(scalar, scalar, negPart, negPart)

UNARY_FUNCTION(scalar, scalar, exp, trans)
UNARY_FUNCTION(scalar, scalar, log, trans)
UNARY_FUNCTION(scalar, scalar, log10, trans)
UNARY_FUNCTION(scalar, scalar, sin, trans)
UNARY_FUNCTION(scalar, scalar, cos, trans)
UNARY_FUNCTION(scalar, scalar, tan, trans)
UNARY_FUNCTION(scalar, scalar, asin, trans)
UNARY_FUNCTION(scalar, scalar, acos, trans)
UNARY_FUNCTION(scalar, scalar, atan, trans)
UNARY_FUNCTION(scalar, scalar, sinh, trans)
UNARY_FUNCTION(scalar, scalar, cosh, trans)
UNARY_FUNCTION(scalar, scalar, tanh, trans)
UNARY_FUNCTION(scalar, scalar, asinh, trans)
UNARY_FUNCTION(scalar, scalar, acosh, trans)
UNARY_FUNCTION(scalar, scalar, atanh, trans)
UNARY_FUNCTION(scalar, scalar, erf, trans)
UNARY_FUNCTION(scalar, scalar, erfc, trans)
UNARY_FUNCTION(scalar, scalar, lgamma, trans)
UNARY_FUNCTION(scalar, scalar, j0, trans)
UNARY_FUNCTION(scalar, scalar, j1, trans)
UNARY_FUNCTION(scalar, scalar, y0, trans)
UNARY_FUNCTION(scalar, scalar, y1, trans)


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

#define BesselFunc(func)                                                       \
                                                                               \
template<class GeoMesh>                                                        \
tmp<DimensionedgpuField<scalar, GeoMesh>> func                                    \
(                                                                              \
    const int n,                                                               \
    const DimensionedgpuField<scalar, GeoMesh>& dsf                               \
)                                                                              \
{                                                                              \
    if (!dsf.dimensions().dimensionless())                                     \
    {                                                                          \
        FatalErrorInFunction                                                   \
            << "dsf not dimensionless"                                         \
            << abort(FatalError);                                              \
    }                                                                          \
                                                                               \
    auto tres =                                                                \
        tmp<DimensionedgpuField<scalar, GeoMesh>>::New                            \
        (                                                                      \
			IOobject														   \
			(																   \
				#func "(" + name(n) + ',' + dsf.name() + ')',				   \
				dsf.instance(), 											   \
				dsf.db()													   \
			),																   \
            dsf.mesh(),                                                        \
            dimless                                                            \
        );                                                                     \
                                                                               \
    func(tres.ref().field(), n, dsf.field());                                  \
                                                                               \
    return tres;                                                               \
}                                                                              \
                                                                               \
                                                                               \
template<class GeoMesh>                                                        \
tmp<DimensionedgpuField<scalar, GeoMesh>> func                                    \
(                                                                              \
    const int n,                                                               \
    const tmp<DimensionedgpuField<scalar, GeoMesh>>& tdsf                         \
)                                                                              \
{                                                                              \
    const DimensionedgpuField<scalar, GeoMesh>& dsf = tdsf();                     \
                                                                               \
    if (!dsf.dimensions().dimensionless())                                     \
    {                                                                          \
        FatalErrorInFunction                                                   \
            << " : dsf not dimensionless"                                      \
            << abort(FatalError);                                              \
    }                                                                          \
                                                                               \
    tmp<DimensionedgpuField<scalar, GeoMesh>> tres                                \
    (                                                                          \
        New                                                                    \
        (                                                                      \
            tdsf,                                                              \
			#func "(" + name(n) + ',' + dsf.name() + ')',					   \
            dimless                                                            \
        )                                                                      \
    );                                                                         \
                                                                               \
    func(tres.ref().field(), n, dsf.field());                                  \
                                                                               \
    tdsf.clear();                                                              \
                                                                               \
    return tres;                                                               \
}

BesselFunc(jn)
BesselFunc(yn)

#undef BesselFunc


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

} // End namespace Foam

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

#include "undefFieldFunctionsM.H"

// ************************************************************************* //
