/*---------------------------------------------------------------------------*\
  ==  == ====== ====   ====    |
                    \\     ||  | Multiphase Code Repository by HZDR
  ======   //   ||  || ===//   | Website: https://doi.org/10.14278/rodare.767
  ||  ||  //    ||  // || \\   | License: GPL-3.0-or-later
  ==  == ====== ====   ==  ==  |
-------------------------------------------------------------------------------
License
    This file is part of the Multiphase Code Repository by HZDR.

    Copyright (C) 2025 by Helmholtz-Zentrum Dresden-Rossendorf e.V. (HZDR),
    Website: https://hzdr.de

    Multiphase Code Repository by HZDR is based on the free software for
    computational fluid dynamics (CFD) from the OpenFOAM Foundation.
    Copyright (C) 2025 by OpenFOAM Foundation, Website: https://openfoam.org

    If you are interested in which files are original OpenFOAM Foundation
    files, which OpenFOAM Foundation files were modified, and which files were
    newly created, see FILES.md.

    Multiphase Code Repository by HZDR 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.

    Multiphase Code Repository by HZDR 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 the Multiphase Code Repository by HZDR. If not, see
    <http://www.gnu.org/licenses/>.

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

#include "Castro.H"
#include "phaseSystem.H"
#include "addToRunTimeSelectionTable.H"
#include "phaseCompressibleMomentumTransportModel.H"

// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

namespace Foam
{
namespace fv
{
    defineTypeNameAndDebug(Castro, 0);
    addToRunTimeSelectionTable(entrainmentModel, Castro, dictionary);

}
}

using Foam::constant::mathematical::pi;


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

inline Foam::tmp<Foam::volScalarField>
Foam::fv::Castro::fL
(
    const volScalarField& k,
    const volScalarField& eps,
    const dimensionedScalar l
) const
{
    const dimensionedScalar kappa(2*pi/l);
    const volScalarField L(pow(k, 1.5)/eps);

    return kappa*L/sqrt(sqr(kappa*L) + 6.78);
}


inline Foam::tmp<Foam::volScalarField>
Foam::fv::Castro::Frl
(
    const volScalarField& ul_l,
    const dimensionedScalar l
) const
{
    return ul_l/sqrt(mag(g_)*l);
}


inline Foam::tmp<Foam::volScalarField>
Foam::fv::Castro::ul
(
    const volScalarField& eps,
    const volScalarField& fL_l,
    const dimensionedScalar l
) const
{
    return sqrt(2.0)*pow(l*eps, 1.0/3.0)*pow(fL_l, 4.0/3.0);
}


inline Foam::tmp<Foam::volScalarField>
Foam::fv::Castro::dHinze
(
    const dispersedPhaseInterface& interface,
    const volScalarField& eps
) const
{
    return
        0.725*pow(interface.sigma()/interface.continuous().rho(), 0.6)
       /pow(eps, 0.4);
}


inline Foam::tmp<Foam::volScalarField>
Foam::fv::Castro::ze
(
    const volScalarField& Frl_l,
    const dimensionedScalar l
) const
{
    return 1.168*l*pow(Frl_l, 2.0/3.0);
}


inline Foam::tmp<Foam::volScalarField>
Foam::fv::Castro::PB
(
    const dispersedPhaseInterface& interface,
    const volScalarField& ul_l,
    const dimensionedScalar l
) const
{
    const volScalarField Xi
    (
        sqrt(2*interface.sigma()/(interface.continuous().rho()*l*sqr(ul_l)))
    );

    return erfc(Xi) + 2/sqrt(pi)*Xi*exp(-sqr(Xi));
}


inline Foam::tmp<Foam::volScalarField>
Foam::fv::Castro::IQ(const volScalarField& Frl_l) const
{
    return 0.808*(5.46*pow(Frl_l, 4.0/3.0) - 1);
}


inline Foam::tmp<Foam::volScalarField>
Foam::fv::Castro::Jl
(
    const dispersedPhaseInterface& interface,
    const volScalarField& fL_l,
    const volScalarField& ul_l,
    const volScalarField& Frl_l,
    const volScalarField& ze_l,
    const dimensionedScalar l
) const
{
    return
        0.8413*fL_l*pow3(ul_l)*IQ(Frl_l)*PB(interface, ul_l, l)
       /(l*mag(g_)*ze_l);
}


Foam::tmp<Foam::volScalarField>
Foam::fv::Castro::J
(
    const dispersedPhaseInterface& interface,
    const volScalarField& k,
    const volScalarField& eps
) const
{
    tmp<volScalarField> tJ
    (
        volScalarField::New
        (
            IOobject::groupName("tTotalEntrainmentFlux", interface.name()),
            interface_.mesh(),
            dimensionedScalar(dimVelocity, Zero)
        )
    );

    dimensionedScalar l = dimensionedScalar
    (
        dimLength,
        0.1*gMin(dHinze(interface, eps).ref().internalField())
    );
    const dimensionedScalar L11 = dimensionedScalar
    (
        dimLength,
        0.43*pow(gAverage(k.internalField()), 1.5)/gAverage(eps.internalField())
    );
    const scalar deltal(pow(10*L11.value()/l.value(), inv(N_)));

    while (l.value() < 10*L11.value())
    {
        const dimensionedScalar lprime(l*deltal);
        const dimensionedScalar lmean(l*sqr(deltal));

        const volScalarField fL_l(fL(k, eps, l));
        const volScalarField ul_l(ul(eps, fL_l, l));
        const volScalarField Frl_l(Frl(ul_l, l));
        const volScalarField ze_l(ze(Frl_l, l));

        const volScalarField fL_lm(fL(k, eps, lmean));
        const volScalarField ul_lm(ul(eps, fL_lm, lmean));
        const volScalarField Frl_lm(Frl(ul_lm, lmean));
        const volScalarField ze_lm(ze(Frl_lm, lmean));

        const volScalarField fL_lp(fL(k, eps, lprime));
        const volScalarField ul_lp(ul(eps, fL_lp, lprime));
        const volScalarField Frl_lp(Frl(ul_lp, lprime));
        const volScalarField ze_lp(ze(Frl_lp, lprime));

        // Simpson's integration rule on log scale
        tJ.ref() +=
            1.0/6.0
           *(lprime.value() - l.value())
           *(
                l*Jl(interface, fL_l, ul_l, Frl_l, ze_l, l)
              + 4*lmean*Jl(interface, fL_lm, ul_lm, Frl_lm, ze_lm, lmean)
              + lprime*Jl(interface, fL_lp, ul_lp, Frl_lp, ze_lp, lprime)
            );

        l = lprime;
    }

    return tJ;
}


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

Foam::tmp<Foam::volScalarField>
Foam::fv::Castro::rate
(
    const phaseModel& entrainingContinuous
) const
{
    // Construct interface (disperse dispersedIn entraining phase) for sigma,
    // e.g. (airD dispersedIn water)

    const dispersedPhaseInterface interfaceDisperseEntraining
    (
        pairDisperse_,
        entrainingContinuous
    );

    // Get entraining phase turbulence properties
    const phaseCompressible::momentumTransportModel&
        turbulenceEntrainingContinuous
        (
            interface_.mesh().lookupObject
            <
                phaseCompressible::momentumTransportModel
            >
            (
                IOobject::groupName
                (
                    momentumTransportModel::typeName,
                    entrainingContinuous.name()
                )
            )
        );

    const volScalarField kEntrainingContinuous
    (
        max
        (
            turbulenceEntrainingContinuous.k(),
            dimensionedScalar(dimensionSet(0, 2, -2, 0, 0), small)
        )
    );

    const volScalarField epsilonEntrainingContinuous
    (
        max
        (
            turbulenceEntrainingContinuous.epsilon(),
            dimensionedScalar(dimensionSet(0, 2, -3, 0, 0), small)
        )
    );

    // Update J_ once per time step for computational efficiency
    if (rateUpdateTime_ != pairContinuous_.time().value())
    {
        J_ =
            J
            (
                interfaceDisperseEntraining,
                kEntrainingContinuous,
                epsilonEntrainingContinuous
            );

        rateUpdateTime_ = pairContinuous_.time().value();
    }


    return
        Sl0_*J_*entrainingContinuous*dimensionedScalar(inv(dimLength), 1.0)
       *pairDisperse_.rho();
}


// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

Foam::fv::Castro::Castro
(
    const dictionary& dict,
    const dispersedPhaseInterface& interface
)
:
    entrainmentModel(dict, interface),
    Sl0_("Sl0", dimless, dict, 1),
    N_(dict.lookupOrDefault<scalar>("N", dimless, 10)),
    J_
    (
        volScalarField::New
        (
            IOobject::groupName("totalEntrainmentFlux", interface.name()),
            interface.mesh(),
            dimensionedScalar(dimVelocity, Zero)
        )
    ),
    rateUpdateTime_(-1)
{}


// * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //

Foam::fv::Castro::~Castro()
{}


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