/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | www.openfoam.com
     \\/     M anipulation  |
-------------------------------------------------------------------------------
    Copyright (C) 2011-2017 OpenFOAM Foundation
-------------------------------------------------------------------------------
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 "GeometricScalargpuField.H"

#define TEMPLATE template<template<class> class PatchField, class GeoMesh>
#include "GeometricgpuFieldFunctionsM.C"

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

namespace Foam
{

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

template<template<class> class PatchField, class GeoMesh>
void stabilise
(
    GeometricgpuField<scalar, PatchField, GeoMesh>& result,
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf,
    const dimensioned<scalar>& ds
)
{
    stabilise(result.primitiveFieldRef(), gsf.primitiveField(), ds.value());
    stabilise(result.boundaryFieldRef(), gsf.boundaryField(), ds.value());
}


template<template<class> class PatchField, class GeoMesh>
tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> stabilise
(
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf,
    const dimensioned<scalar>& ds
)
{
    tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> tRes
    (
        new GeometricgpuField<scalar, PatchField, GeoMesh>
        (
            IOobject
            (
                "stabilise(" + gsf.name() + ',' + ds.name() + ')',
                gsf.instance(),
                gsf.db(),
                IOobject::NO_READ,
                IOobject::NO_WRITE
            ),
            gsf.mesh(),
            ds.dimensions() + gsf.dimensions()
        )
    );

    stabilise(tRes.ref(), gsf, ds);

    return tRes;
}


template<template<class> class PatchField, class GeoMesh>
tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> stabilise
(
    const tmp<GeometricgpuField<scalar, PatchField, GeoMesh>>& tgsf,
    const dimensioned<scalar>& ds
)
{
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf = tgsf();

    tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> tRes
    (
        New
        (
            tgsf,
            "stabilise(" + gsf.name() + ',' + ds.name() + ')',
            ds.dimensions() + gsf.dimensions()
        )
    );

    stabilise(tRes.ref(), gsf, ds);

    tgsf.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<template<class> class PatchField, class GeoMesh>
void pow
(
    GeometricgpuField<scalar, PatchField, GeoMesh>& Pow,
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf1,
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf2
)
{
    pow(Pow.primitiveFieldRef(), gsf1.primitiveField(), gsf2.primitiveField());
    pow(Pow.boundaryFieldRef(), gsf1.boundaryField(), gsf2.boundaryField());
}


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

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

    tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> tPow
    (
        new GeometricgpuField<scalar, PatchField, GeoMesh>
        (
            IOobject
            (
                "pow(" + gsf1.name() + ',' + gsf2.name() + ')',
                gsf1.instance(),
                gsf1.db(),
                IOobject::NO_READ,
                IOobject::NO_WRITE
            ),
            gsf1.mesh(),
            dimless
        )
    );

    pow(tPow.ref(), gsf1, gsf2);

    return tPow;
}


template<template<class> class PatchField, class GeoMesh>
tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> pow
(
    const tmp<GeometricgpuField<scalar, PatchField, GeoMesh>>& tgsf1,
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf2
)
{
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf1 = tgsf1();

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

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

    tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> tPow
    (
        New
        (
            tgsf1,
            "pow(" + gsf1.name() + ',' + gsf2.name() + ')',
            dimless
        )
    );

    pow(tPow.ref(), gsf1, gsf2);

    tgsf1.clear();

    return tPow;
}


template<template<class> class PatchField, class GeoMesh>
tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> pow
(
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf1,
    const tmp<GeometricgpuField<scalar, PatchField, GeoMesh>>& tgsf2
)
{
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf2 = tgsf2();

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

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

    tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> tPow
    (
        New
        (
            tgsf2,
            "pow(" + gsf1.name() + ',' + gsf2.name() + ')',
            dimless
        )
    );

    pow(tPow.ref(), gsf1, gsf2);

    tgsf2.clear();

    return tPow;
}

template<template<class> class PatchField, class GeoMesh>
tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> pow
(
    const tmp<GeometricgpuField<scalar, PatchField, GeoMesh>>& tgsf1,
    const tmp<GeometricgpuField<scalar, PatchField, GeoMesh>>& tgsf2
)
{
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf1 = tgsf1();
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf2 = tgsf2();

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

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

    tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> tPow
    (
        reuseTmpTmpGeometricgpuField
            <scalar, scalar, scalar, scalar, PatchField, GeoMesh>::New
        (
            tgsf1,
            tgsf2,
            "pow(" + gsf1.name() + ',' + gsf2.name() + ')',
            dimless
        )
    );

    pow(tPow.ref(), gsf1, gsf2);

    tgsf1.clear();
    tgsf2.clear();

    return tPow;
}


template<template<class> class PatchField, class GeoMesh>
void pow
(
    GeometricgpuField<scalar, PatchField, GeoMesh>& tPow,
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf,
    const dimensioned<scalar>& ds
)
{
    pow(tPow.primitiveFieldRef(), gsf.primitiveField(), ds.value());
    pow(tPow.boundaryFieldRef(), gsf.boundaryField(), ds.value());
}


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

    tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> tPow
    (
        new GeometricgpuField<scalar, PatchField, GeoMesh>
        (
            IOobject
            (
                "pow(" + gsf.name() + ',' + ds.name() + ')',
                gsf.instance(),
                gsf.db(),
                IOobject::NO_READ,
                IOobject::NO_WRITE
            ),
            gsf.mesh(),
            pow(gsf.dimensions(), ds)
        )
    );

    pow(tPow.ref(), gsf, ds);

    return tPow;
}

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

    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf = tgsf();

    tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> tPow
    (
        New
        (
            tgsf,
            "pow(" + gsf.name() + ',' + ds.name() + ')',
            pow(gsf.dimensions(), ds)
        )
    );

    pow(tPow.ref(), gsf, ds);

    tgsf.clear();

    return tPow;
}

template<template<class> class PatchField, class GeoMesh>
tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> pow
(
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf,
    const scalar& s
)
{
    return pow(gsf, dimensionedScalar(s));
}

template<template<class> class PatchField, class GeoMesh>
tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> pow
(
    const tmp<GeometricgpuField<scalar, PatchField, GeoMesh>>& tgsf,
    const scalar& s
)
{
    return pow(tgsf, dimensionedScalar(s));
}


template<template<class> class PatchField, class GeoMesh>
void pow
(
    GeometricgpuField<scalar, PatchField, GeoMesh>& tPow,
    const dimensioned<scalar>& ds,
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf
)
{
    pow(tPow.primitiveFieldRef(), ds.value(), gsf.primitiveField());
    pow(tPow.boundaryFieldRef(), ds.value(), gsf.boundaryField());
}


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

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

    tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> tPow
    (
        new GeometricgpuField<scalar, PatchField, GeoMesh>
        (
            IOobject
            (
                "pow(" + ds.name() + ',' + gsf.name() + ')',
                gsf.instance(),
                gsf.db(),
                IOobject::NO_READ,
                IOobject::NO_WRITE
            ),
            gsf.mesh(),
            dimless
        )
    );

    pow(tPow.ref(), ds, gsf);

    return tPow;
}


template<template<class> class PatchField, class GeoMesh>
tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> pow
(
    const dimensionedScalar& ds,
    const tmp<GeometricgpuField<scalar, PatchField, GeoMesh>>& tgsf
)
{
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf = tgsf();

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

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

    tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> tPow
    (
        New
        (
            tgsf,
            "pow(" + ds.name() + ',' + gsf.name() + ')',
            dimless
        )
    );

    pow(tPow.ref(), ds, gsf);

    tgsf.clear();

    return tPow;
}

template<template<class> class PatchField, class GeoMesh>
tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> pow
(
    const scalar& s,
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf
)
{
    return pow(dimensionedScalar(s), gsf);
}

template<template<class> class PatchField, class GeoMesh>
tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> pow
(
    const scalar& s,
    const tmp<GeometricgpuField<scalar, PatchField, GeoMesh>>& tgsf
)
{
    return pow(dimensionedScalar(s), tgsf);
}


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

template<template<class> class PatchField, class GeoMesh>
void atan2
(
    GeometricgpuField<scalar, PatchField, GeoMesh>& Atan2,
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf1,
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf2
)
{
    atan2
    (
        Atan2.primitiveFieldRef(),
        gsf1.primitiveField(),
        gsf2.primitiveField()
    );
    atan2
    (
        Atan2.boundaryFieldRef(),
        gsf1.boundaryField(),
        gsf2.boundaryField()
    );
}


template<template<class> class PatchField, class GeoMesh>
tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> atan2
(
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf1,
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf2
)
{
    tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> tAtan2
    (
        new GeometricgpuField<scalar, PatchField, GeoMesh>
        (
            IOobject
            (
                "atan2(" + gsf1.name() + ',' + gsf2.name() + ')',
                gsf1.instance(),
                gsf1.db(),
                IOobject::NO_READ,
                IOobject::NO_WRITE
            ),
            gsf1.mesh(),
            atan2(gsf1.dimensions(), gsf2.dimensions())
        )
    );

    atan2(tAtan2.ref(), gsf1, gsf2);

    return tAtan2;
}


template<template<class> class PatchField, class GeoMesh>
tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> atan2
(
    const tmp<GeometricgpuField<scalar, PatchField, GeoMesh>>& tgsf1,
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf2
)
{
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf1 = tgsf1();

    tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> tAtan2
    (
        New
        (
            tgsf1,
            "atan2(" + gsf1.name() + ',' + gsf2.name() + ')',
            atan2(gsf1.dimensions(), gsf2.dimensions())
        )
    );

    atan2(tAtan2.ref(), gsf1, gsf2);

    tgsf1.clear();

    return tAtan2;
}


template<template<class> class PatchField, class GeoMesh>
tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> atan2
(
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf1,
    const tmp<GeometricgpuField<scalar, PatchField, GeoMesh>>& tgsf2
)
{
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf2 = tgsf2();

    tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> tAtan2
    (
        New
        (
            tgsf2,
            "atan2(" + gsf1.name() + ',' + gsf2.name() + ')',
            atan2( gsf1.dimensions(), gsf2.dimensions())
        )
    );

    atan2(tAtan2.ref(), gsf1, gsf2);

    tgsf2.clear();

    return tAtan2;
}

template<template<class> class PatchField, class GeoMesh>
tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> atan2
(
    const tmp<GeometricgpuField<scalar, PatchField, GeoMesh>>& tgsf1,
    const tmp<GeometricgpuField<scalar, PatchField, GeoMesh>>& tgsf2
)
{
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf1 = tgsf1();
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf2 = tgsf2();

    tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> tAtan2
    (
        reuseTmpTmpGeometricgpuField
            <scalar, scalar, scalar, scalar, PatchField, GeoMesh>::New
        (
            tgsf1,
            tgsf2,
            "atan2(" + gsf1.name() + ',' + gsf2.name() + ')',
            atan2(gsf1.dimensions(), gsf2.dimensions())
        )
    );

    atan2(tAtan2.ref(), gsf1, gsf2);

    tgsf1.clear();
    tgsf2.clear();

    return tAtan2;
}


template<template<class> class PatchField, class GeoMesh>
void atan2
(
    GeometricgpuField<scalar, PatchField, GeoMesh>& tAtan2,
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf,
    const dimensioned<scalar>& ds
)
{
    atan2(tAtan2.primitiveFieldRef(), gsf.primitiveField(), ds.value());
    atan2(tAtan2.boundaryFieldRef(), gsf.boundaryField(), ds.value());
}


template<template<class> class PatchField, class GeoMesh>
tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> atan2
(
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf,
    const dimensionedScalar& ds
)
{
    tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> tAtan2
    (
        new GeometricgpuField<scalar, PatchField, GeoMesh>
        (
            IOobject
            (
                "atan2(" + gsf.name() + ',' + ds.name() + ')',
                gsf.instance(),
                gsf.db(),
                IOobject::NO_READ,
                IOobject::NO_WRITE
            ),
            gsf.mesh(),
            atan2(gsf.dimensions(), ds)
        )
    );

    atan2(tAtan2.ref(), gsf, ds);

    return tAtan2;
}

template<template<class> class PatchField, class GeoMesh>
tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> atan2
(
    const tmp<GeometricgpuField<scalar, PatchField, GeoMesh>>& tgsf,
    const dimensionedScalar& ds
)
{
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf = tgsf();

    tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> tAtan2
    (
        New
        (
            tgsf,
            "atan2(" + gsf.name() + ',' + ds.name() + ')',
            atan2(gsf.dimensions(), ds)
        )
    );

    atan2(tAtan2.ref(), gsf, ds);

    tgsf.clear();

    return tAtan2;
}

template<template<class> class PatchField, class GeoMesh>
tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> atan2
(
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf,
    const scalar& s
)
{
    return atan2(gsf, dimensionedScalar(s));
}

template<template<class> class PatchField, class GeoMesh>
tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> atan2
(
    const tmp<GeometricgpuField<scalar, PatchField, GeoMesh>>& tgsf,
    const scalar& s
)
{
    return atan2(tgsf, dimensionedScalar(s));
}


template<template<class> class PatchField, class GeoMesh>
void atan2
(
    GeometricgpuField<scalar, PatchField, GeoMesh>& tAtan2,
    const dimensioned<scalar>& ds,
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf
)
{
    atan2(tAtan2.primitiveFieldRef(), ds.value(), gsf.primitiveField());
    atan2(tAtan2.boundaryFieldRef(), ds.value(), gsf.boundaryField());
}


template<template<class> class PatchField, class GeoMesh>
tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> atan2
(
    const dimensionedScalar& ds,
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf
)
{
    tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> tAtan2
    (
        new GeometricgpuField<scalar, PatchField, GeoMesh>
        (
            IOobject
            (
                "atan2(" + ds.name() + ',' + gsf.name() + ')',
                gsf.instance(),
                gsf.db(),
                IOobject::NO_READ,
                IOobject::NO_WRITE
            ),
            gsf.mesh(),
            atan2(ds, gsf.dimensions())
        )
    );

    atan2(tAtan2.ref(), ds, gsf);

    return tAtan2;
}


template<template<class> class PatchField, class GeoMesh>
tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> atan2
(
    const dimensionedScalar& ds,
    const tmp<GeometricgpuField<scalar, PatchField, GeoMesh>>& tgsf
)
{
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf = tgsf();

    tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> tAtan2
    (
        New
        (
            tgsf,
            "atan2(" + ds.name() + ',' + gsf.name() + ')',
            atan2(ds, gsf.dimensions())
        )
    );

    atan2(tAtan2.ref(), ds, gsf);

    tgsf.clear();

    return tAtan2;
}

template<template<class> class PatchField, class GeoMesh>
tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> atan2
(
    const scalar& s,
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf
)
{
    return atan2(dimensionedScalar(s), gsf);
}

template<template<class> class PatchField, class GeoMesh>
tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> atan2
(
    const scalar& s,
    const tmp<GeometricgpuField<scalar, PatchField, GeoMesh>>& tgsf
)
{
    return atan2(dimensionedScalar(s), tgsf);
}


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

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<template<class> class PatchField, class GeoMesh>                      \
void func                                                                      \
(                                                                              \
    GeometricgpuField<scalar, PatchField, GeoMesh>& gsf,                          \
    const int n,                                                               \
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf1                    \
)                                                                              \
{                                                                              \
    func(gsf.primitiveFieldRef(), n, gsf1.primitiveField());                   \
    func(gsf.boundaryFieldRef(), n, gsf1.boundaryField());                     \
}                                                                              \
                                                                               \
template<template<class> class PatchField, class GeoMesh>                      \
tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> func                          \
(                                                                              \
    const int n,                                                               \
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf                     \
)                                                                              \
{                                                                              \
    if (!gsf.dimensions().dimensionless())                                     \
    {                                                                          \
        FatalErrorInFunction                                                   \
            << "gsf not dimensionless"                                         \
            << abort(FatalError);                                              \
    }                                                                          \
                                                                               \
    tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> tFunc                     \
    (                                                                          \
        new GeometricgpuField<scalar, PatchField, GeoMesh>                        \
        (                                                                      \
            IOobject                                                           \
            (                                                                  \
                #func "(" + gsf.name() + ')',                                  \
                gsf.instance(),                                                \
                gsf.db(),                                                      \
                IOobject::NO_READ,                                             \
                IOobject::NO_WRITE                                             \
            ),                                                                 \
            gsf.mesh(),                                                        \
            dimless                                                            \
        )                                                                      \
    );                                                                         \
                                                                               \
    func(tFunc.ref(), n, gsf);                                                 \
                                                                               \
    return tFunc;                                                              \
}                                                                              \
                                                                               \
template<template<class> class PatchField, class GeoMesh>                      \
tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> func                          \
(                                                                              \
    const int n,                                                               \
    const tmp<GeometricgpuField<scalar, PatchField, GeoMesh>>& tgsf               \
)                                                                              \
{                                                                              \
    const GeometricgpuField<scalar, PatchField, GeoMesh>& gsf = tgsf();           \
                                                                               \
    if (!gsf.dimensions().dimensionless())                                     \
    {                                                                          \
        FatalErrorInFunction                                                   \
            << " : gsf not dimensionless"                                      \
            << abort(FatalError);                                              \
    }                                                                          \
                                                                               \
    tmp<GeometricgpuField<scalar, PatchField, GeoMesh>> tFunc                     \
    (                                                                          \
        New                                                                    \
        (                                                                      \
            tgsf,                                                              \
            #func "(" + gsf.name() + ')',                                      \
            dimless                                                            \
        )                                                                      \
    );                                                                         \
                                                                               \
    func(tFunc.ref(), n, gsf);                                                 \
                                                                               \
    tgsf.clear();                                                              \
                                                                               \
    return tFunc;                                                              \
}

BesselFunc(jn)
BesselFunc(yn)

#undef BesselFunc


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

} // End namespace Foam

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

#include "undefFieldFunctionsM.H"

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