/*---------------------------------------------------------------------------*\
  ==  == ====== ====   ====    |
                    \\     ||  | 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 "BhutaniBritoParada.H"
#include "addToRunTimeSelectionTable.H"
#include "uniformDimensionedFields.H"

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

namespace Foam
{
namespace fv
{
    defineTypeNameAndDebug(BhutaniBritoParada, 0);
    addToRunTimeSelectionTable
    (
        attachmentModel, BhutaniBritoParada, dictionary
    );
}
}

using namespace Foam::constant::mathematical;


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

inline Foam::tmp<Foam::volScalarField>
Foam::fv::BhutaniBritoParada::dCrit
(
    const volScalarField& nu,
    const volScalarField& k,
    const volScalarField& epsilon,
    const volScalarField& rhoC,
    const volScalarField& rhoD
) const
{
    return
        sqrt
        (
            30*nu*rhoC*k
           /max
            (
                rhoD*epsilon,
                dimensionedScalar(dimensionSet(1, -1, -3, 0, 0), small)
            )
        );
}


inline Foam::tmp<Foam::volScalarField>
Foam::fv::BhutaniBritoParada::uPrime
(
    const volScalarField& d,
    const volScalarField& nu,
    const volScalarField& epsilon,
    const volScalarField& rhoC,
    const volScalarField& rhoD
) const
{
    return
        0.4*pow(epsilon, 4.0/9)*pow(d, 7.0/9)/cbrt(nu)
       *pow(mag(rhoD - rhoC)/rhoC, 2.0/3);
}


inline Foam::tmp<Foam::volScalarField>
Foam::fv::BhutaniBritoParada::nD
(
    const volScalarField& d,
    const volScalarField& alpha,
    const volScalarField& rhoA,
    const volScalarField& rhoD,
    const volScalarField& Y
) const
{
    return rhoA*alpha*Y*6.0/(rhoD*pi*pow(d, 3));
}


inline Foam::tmp<Foam::volScalarField>
Foam::fv::BhutaniBritoParada::Re
(
    const volScalarField& d,
    const volScalarField& u,
    const volScalarField& nu
) const
{
    return d*u/nu;
}


inline Foam::tmp<Foam::volScalarField>
Foam::fv::BhutaniBritoParada::BoM
(
    const volScalarField& dP,
    const volScalarField& dB,
    const volScalarField& rhoP,
    const volScalarField& rhoL,
    const volScalarField& epsilon,
    const volScalarField& sigma
) const
{
    const uniformDimensionedVectorField& g =
        interface_.mesh().lookupObject<uniformDimensionedVectorField>("g");

    return
        (
            sqr(dP)
           *(
                mag(rhoP - rhoL)*mag(g)
              + 1.9*rhoP*sqr(cbrt(epsilon))*cbrt(2.0/(dP + dB))
            )
          + 1.5*dP*(4*sigma/dB - dB*rhoL*mag(g))*sqr(sin(pi - 0.5*theta_))
        )
       /mag(6.0*sigma*sin(pi - 0.5*theta_)*sin(pi + 0.5*theta_));
}


inline Foam::tmp<Foam::volScalarField>
Foam::fv::BhutaniBritoParada::tInduction
(
    const volScalarField& d
) const
{
    return
        dimensionedScalar(dimensionSet(0, -B_, 1, 0, 0), 75.0)
       /theta_*pow(d, B_);
}


void Foam::fv::BhutaniBritoParada::correctZ
(
    const volScalarField& dP,
    const volScalarField& dB,
    const volScalarField& rhoP,
    const volScalarField& rhoB,
    const volScalarField& rhoL,
    const volScalarField& kL,
    const volScalarField& epsilonL,
    const volScalarField& nuL,
    const volScalarField& uPrimeB
) const
{
    const volScalarField dPCrit
    (
        IOobject::groupName("dCrit", detachedPhase_.name()),
        dCrit(nuL, kL, epsilonL, rhoL, rhoP)
    );

    const volScalarField dBCrit
    (
        IOobject::groupName("dCrit", aggregatePhase_.name()),
        dCrit(nuL, kL, epsilonL, rhoL, 0.5*rhoL)
    );

    const volScalarField uPrimeP
    (
        IOobject::groupName("UPrime", detachedPhase_.name()),
        uPrime(dP, nuL, epsilonL, rhoL, rhoP)
    );

    Z_ =
        pos(dB - dBCrit)*pos(dP - dPCrit)
       *1.25*sqr(dP + dB)*sqrt(sqr(uPrimeP) + sqr(uPrimeB))
      + (1 - pos(dB - dBCrit)*pos(dP - dPCrit))
       *pow3(0.5*(dP + dB))*sqrt(pi*8.0/15.0*epsilonL/nuL);
}


void Foam::fv::BhutaniBritoParada::correctZPrime
(
    const volScalarField& dP,
    const volScalarField& dB,
    const volScalarField& epsilonL
) const
{
    ZPrime_ = sqrt(C_)*cbrt(epsilonL)/sqr(cbrt(dB + dP));
}


void Foam::fv::BhutaniBritoParada::correctPc
(
    const volScalarField& dP,
    const volScalarField& dB,
    const volScalarField& RePrime
) const
{
    Pc_ = (1.5 + 4.0*pow(RePrime, 0.72)/15)*sqr(dP/dB);
}


void Foam::fv::BhutaniBritoParada::correctPa
(
    const volScalarField& dP,
    const volScalarField& dB,
    const volScalarField& uPrimeB,
    const volScalarField& RePrime
) const
{
    const volScalarField tInd
    (
        IOobject::groupName("inductionTime", aggregatePhase_.name()),
        tInduction(dP)
    );

    Pa_ =
        sqr
        (
            sin
            (
                2*atan
                (
                    exp
                    (
                      - (45 + 8.0*pow(RePrime, 0.72))*uPrimeB*tInd
                       /(15.0*dB*(dB/dP + 1.0))
                    )
                )
            )
        );
}


void Foam::fv::BhutaniBritoParada::correctPs
(
    const volScalarField& dP,
    const volScalarField& dB,
    const volScalarField& rhoP,
    const volScalarField& rhoL,
    const volScalarField& epsilonL
) const
{
    const phaseInterface interface(aggregatePhase_, continuousPhase_);

    const volScalarField Bo
    (
        IOobject::groupName("Bo", aggregatePhase_.name()),
        BoM(dP, dB, rhoP, rhoL, epsilonL, interface.sigma())
    );

    Ps_ = 1.0 - exp(As_*(1.0 - 1.0/Bo));
}


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

Foam::tmp<Foam::volScalarField>
Foam::fv::BhutaniBritoParada::rate() const
{
    const volScalarField& Yb = aggregatePhase_.Y(carrierSpecie_);
    const volScalarField& Yp = aggregatePhase_.Y(species_[0]);
    const volScalarField& rhoA = aggregatePhase_.rho();
    const volScalarField& rhoL = continuousPhase_.rho();
    const volScalarField rhoP(thermo_.rhoi(indexI_, thermo_.p(), thermo_.T()));
    const volScalarField rhoB(thermo_.rhoi(indexC_, thermo_.p(), thermo_.T()));
    const volScalarField dB(aggregatePhase_.d());
    const volScalarField dP(detachedPhase_.d());
    const volScalarField kL(continuousTurbulence_.k());
    const volScalarField epsL(continuousTurbulence_.epsilon());
    const volScalarField nuL(continuousPhase_.fluidThermo().nu());

    const volScalarField uPrimeB
    (
        IOobject::groupName("UPrime", aggregatePhase_.name()),
        uPrime(dB, nuL, epsL, rhoL, rhoB)
    );

    correctZ(dP, dB, rhoP, rhoB, rhoL, kL, epsL, nuL, uPrimeB);

    const volScalarField RePrime
    (
        IOobject::groupName("RePrime", aggregatePhase_.name()),
        Re(dB, uPrimeB, nuL)
    );

    correctPc(dP, dB, RePrime);

    correctPa(dP, dB, uPrimeB, RePrime);

    surfaceLoading(dB, dP, Yb, Yp, rhoB, rhoP);

    const volScalarField nBa
    (
        IOobject::groupName("availableBubbles", aggregatePhase_.name()),
        (1 - surfaceLoading_/maxLoading_)
       *nD(dB, aggregatePhase_, rhoA, rhoB, Yb)
    );

    const volScalarField nPf
    (
        IOobject::groupName("freeParticles", detachedPhase_.name()),
        nD(dP, detachedPhase_, rhoP, rhoP, detachedPhase_.Y(species_[0]))
    );

    Psi_ = Z_*Pc_*Pa_*nPf*nBa*rhoP*pi/6.0*pow3(dP);


    if (stability_)
    {
        correctPs(dP, dB, rhoP, rhoL, epsL);

        Psi_ *= Ps_;
    }


    if (detachment_)
    {
        const volScalarField nBl
        (
            IOobject::groupName("loadedBubbles", aggregatePhase_.name()),
            surfaceLoading_/maxLoading_*nD(dB, aggregatePhase_, rhoA, rhoB, Yb)
        );
        if (!stability_)
        {
            correctPs(dP, dB, rhoP, rhoL, epsL);
        }
        correctZPrime(dP, dB, epsL);

        Psi_ -= ZPrime_*(1 - Ps_)*nBl*rhoP*pi/6.0*pow3(dP);
    }


    return Psi_;
}


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

Foam::fv::BhutaniBritoParada::BhutaniBritoParada
(
    const dictionary& dict,
    const dispersedPhaseInterface& interface,
    const hashedWordList& species
)
:
    attachmentModel(dict, interface, species),
    continuousPhase_
    (
        interface_.mesh().lookupObject<phaseModel>
        (
            IOobject::groupName("alpha", dict.lookup("continuousPhase"))
        )
    ),
    continuousTurbulence_(interface_.mesh().lookupObject
        <
            phaseCompressible::momentumTransportModel
        >
        (
            IOobject::groupName
            (
                momentumTransportModel::typeName,
                continuousPhase_.name()
            )
        )
    ),
    stability_(dict.lookupOrDefault<Switch>("stability", true)),
    detachment_(dict.lookupOrDefault<Switch>("detachment", true)),
    theta_(dict.lookupOrDefault<scalar>("theta", dimless, 75.0)),
    B_(dict.lookupOrDefault<scalar>("B", dimless, 0.6)),
    C_(dict.lookupOrDefault<scalar>("C", dimless, 2.0)),
    As_(dict.lookupOrDefault<scalar>("As", dimless, 1.0)),
    Psi_
    (
        IOobject
        (
            IOobject::groupName("Psi", aggregatePhase_.name()),
            interface_.mesh().time().name(),
            interface_.mesh(),
            IOobject::NO_READ,
            IOobject::AUTO_WRITE
        ),
        interface_.mesh(),
        dimensionedScalar(dimDensity/dimTime, Zero)
    ),
    Z_
    (
        IOobject
        (
            IOobject::groupName("Z", aggregatePhase_.name()),
            interface_.mesh().time().name(),
            interface_.mesh(),
            IOobject::NO_READ,
            IOobject::AUTO_WRITE
        ),
        interface_.mesh(),
        dimensionedScalar(dimVolume/dimTime, Zero)
    ),
    ZPrime_
    (
        IOobject
        (
            IOobject::groupName("ZPrime", aggregatePhase_.name()),
            interface_.mesh().time().name(),
            interface_.mesh(),
            IOobject::NO_READ,
            IOobject::AUTO_WRITE
        ),
        interface_.mesh(),
        dimensionedScalar(inv(dimTime), Zero)
    ),
    Pc_
    (
        IOobject
        (
            IOobject::groupName("Pc", aggregatePhase_.name()),
            interface_.mesh().time().name(),
            interface_.mesh(),
            IOobject::NO_READ,
            IOobject::AUTO_WRITE
        ),
        interface_.mesh(),
        dimensionedScalar(dimless, Zero)
    ),
    Pa_
    (
        IOobject
        (
            IOobject::groupName("Pa", aggregatePhase_.name()),
            interface_.mesh().time().name(),
            interface_.mesh(),
            IOobject::NO_READ,
            IOobject::AUTO_WRITE
        ),
        interface_.mesh(),
        dimensionedScalar(dimless, Zero)
    ),
    Ps_
    (
        IOobject
        (
            IOobject::groupName("Ps", aggregatePhase_.name()),
            interface_.mesh().time().name(),
            interface_.mesh(),
            IOobject::NO_READ,
            IOobject::AUTO_WRITE
        ),
        interface_.mesh(),
        dimensionedScalar(dimless, Zero)
    )
{}


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

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


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