/*---------------------------------------------------------------------------*\
  ==  == ====== ====   ====    |
                    \\     ||  | 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 "ShinJonesDepartureDiameter.H"
#include "wallBoilingModelsCoefficient.H"
#include "addToRunTimeSelectionTable.H"
#include "compressibleMomentumTransportModel.H"
#include "phaseCompressibleMomentumTransportModel.H"
#include "phaseSystem.H"
#include "fvcAverage.H"

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

namespace Foam
{
namespace wallBoilingModels
{
namespace departureDiameterModels
{
    defineTypeNameAndDebug(ShinJonesDepartureDiameter, 0);
    addToRunTimeSelectionTable
    (
        departureDiameterModel,
        ShinJonesDepartureDiameter,
        dictionary
    );
}
}
}


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

template<class ScalarFieldType>
Foam::tmp<ScalarFieldType>
Foam::wallBoilingModels::departureDiameterModels::ShinJonesDepartureDiameter::
calculate
(
    const ScalarFieldType& Tl,
    const ScalarFieldType& Tsatw,
    const ScalarFieldType& L,
    const ScalarFieldType& rhoLiquid,
    const ScalarFieldType& rhoVapour,
    const ScalarFieldType& muLiquid,
    const ScalarFieldType& nuLiquid,
    const ScalarFieldType& nuVapour,
    const ScalarFieldType& sigma,
    const ScalarFieldType& magTau
) const
{
    const ScalarFieldType dCav
    (
        pos(Tl - Tsatw)*4*sigma*Tsatw
       /(rhoVapour*max((Tl - Tsatw), scalar(1e-8))*L)
    );

    const ScalarFieldType dDepReLow
    (
        pos(dCav)*2/sqrt(3*sigma*max(dCav, scalar(1e-16))/magTau)
    );

    const ScalarFieldType dDepReMed
    (
        pos(dCav)*1.1574*pow(sigma*max(dCav, scalar(1e-16))/2, 0.3571)
       *pow(rhoLiquid, 0.1429)*pow(nuLiquid, 0.2857)/sqrt(magTau)
    );

    const ScalarFieldType dDepReHigh
    (
        pos(dCav)*3.4728*pow025(sigma*rhoLiquid*max(dCav, scalar(1e-16))/2)
       *sqrt(nuLiquid/magTau)
    );

    const ScalarFieldType ReLow
    (
        magTau*sqr(dDepReLow/2)/(rhoLiquid*sqr(nuLiquid))
    );

    const ScalarFieldType ReMed
    (
        magTau*sqr(dDepReMed/2)/(rhoLiquid*sqr(nuLiquid))
    );

    const ScalarFieldType ReHigh
    (
        magTau*sqr(dDepReHigh/2)/(rhoLiquid*sqr(nuLiquid))
    );

    auto K = coefficient<ScalarFieldType>::value(K_);

    auto dMin = coefficient<ScalarFieldType>::value(dMin_);

    return
       max
       (
           K
          *(
               neg(ReLow - 1)*dDepReLow
             + pos0(ReMed - 1)*neg(ReMed - 500)*dDepReMed
             + pos0(ReHigh - 500)*dDepReHigh
           ),
           dMin
       );
}


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

Foam::wallBoilingModels::departureDiameterModels::
ShinJonesDepartureDiameter::
ShinJonesDepartureDiameter
(
    const dictionary& dict
)
:
    departureDiameterModel(),
    phaseName_(dict.lookupOrDefault<word>("phase", "liquid")),
    K_("K", dimless, dict, 1),
    dMin_("dMin", dimLength, dict, 1e-6)
{}


Foam::wallBoilingModels::departureDiameterModels::
ShinJonesDepartureDiameter::
ShinJonesDepartureDiameter
(
    const ShinJonesDepartureDiameter& model
)
:
    departureDiameterModel(),
    phaseName_(model.phaseName_),
    K_(model.K_),
    dMin_(model.dMin_)
{}


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

Foam::wallBoilingModels::departureDiameterModels::
ShinJonesDepartureDiameter::
~ShinJonesDepartureDiameter()
{}


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

Foam::tmp<Foam::scalarField>
Foam::wallBoilingModels::departureDiameterModels::
ShinJonesDepartureDiameter::dDeparture
(
    const phaseModel& liquid,
    const phaseModel& vapour,
    const label patchi,
    const scalarField& Tl,
    const scalarField& Tsatw,
    const scalarField& L
) const
{
    const fvMesh& mesh(liquid.fluid().mesh());

    const word modelName
    (
        IOobject::groupName(momentumTransportModel::typeName, phaseName_)
    );
    const compressibleMomentumTransportModel& model =
        mesh.lookupObject<compressibleMomentumTransportModel>(modelName);

    const surfaceVectorField tau(model.devTau());
    const vectorField taup(tau.boundaryField()[patchi]);
    const scalarField magTauw(mag(taup));

    return
        calculate
        (
            Tl,
            Tsatw,
            L,
            static_cast<const scalarField&>
            (
                liquid.rho().boundaryField()[patchi]
            ),
            static_cast<const scalarField&>
            (
                vapour.rho().boundaryField()[patchi]
            ),
            static_cast<const scalarField&>
            (
                liquid.fluidThermo().mu().boundaryField()[patchi]
            ),
            liquid.fluidThermo().nu(patchi)(),
            vapour.fluidThermo().nu(patchi)(),
            liquid.fluid().sigma(phaseInterfaceKey(liquid, vapour), patchi)(),
            magTauw
        );
}


Foam::tmp<Foam::volScalarField::Internal>
Foam::wallBoilingModels::departureDiameterModels::
ShinJonesDepartureDiameter::dDeparture
(
    const phaseModel& liquid,
    const phaseModel& vapour,
    const phaseModel& solid,
    const volScalarField::Internal& Tf,
    const volScalarField::Internal& Tsatw,
    const volScalarField::Internal& L
) const
{
    const word modelName
    (
        IOobject::groupName(momentumTransportModel::typeName, phaseName_)
    );
    const compressibleMomentumTransportModel& model =
        liquid.fluid().mesh().lookupObject<compressibleMomentumTransportModel>
        (
            modelName
        );

    const volScalarField tauMag = fvc::average(mag(model.devTau()));

    return
        calculate
        (
            liquid.thermo().T()(),
            Tsatw,
            L,
            liquid.rho()(),
            vapour.rho()(),
            liquid.fluidThermo().mu()(),
            liquid.fluidThermo().nu()()(),
            vapour.fluidThermo().nu()()(),
            liquid.fluid().sigma(phaseInterfaceKey(liquid, vapour))()(),
            tauMag()
        );
}


Foam::tmp<Foam::volScalarField>
Foam::wallBoilingModels::departureDiameterModels::ShinJonesDepartureDiameter::
dDeparture
(
    const phaseModel& liquid,
    const phaseModel& vapour,
    const phaseModel& solid,
    const volScalarField& Tf,
    const volScalarField& Tsatw,
    const volScalarField& L
) const
{
    const word modelName
    (
        IOobject::groupName(momentumTransportModel::typeName, phaseName_)
    );
    const compressibleMomentumTransportModel& model =
        liquid.fluid().mesh().lookupObject<compressibleMomentumTransportModel>
        (
            modelName
        );

    const volScalarField tauMag = fvc::average(mag(model.devTau()));

    return
        calculate
        (
            liquid.thermo().T(),
            Tsatw,
            L,
            liquid.rho(),
            vapour.rho(),
            liquid.fluidThermo().mu(),
            liquid.fluidThermo().nu()(),
            vapour.fluidThermo().nu()(),
            liquid.fluid().sigma(phaseInterfaceKey(liquid, vapour))(),
            tauMag
        );
}


void Foam::wallBoilingModels::departureDiameterModels::
ShinJonesDepartureDiameter::write(Ostream& os) const
{
    departureDiameterModel::write(os);
    writeKeyword(os, "liquid") << phaseName_ << token::END_STATEMENT << nl;
    writeKeyword(os, "K") << K_ << token::END_STATEMENT << nl;
    writeKeyword(os, "dMin") << dMin_ << token::END_STATEMENT << nl;
}


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