/*---------------------------------------------------------------------------*\
  ==  == ====== ====   ====    |
                    \\     ||  | 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 "dispersedPhaseInterface.H"
#include "uniformDimensionedFields.H"
#include "addToRunTimeSelectionTable.H"
//~~~~~~~~~~~~~~~~~~~~~~~~~~~ Begin Code Repository ~~~~~~~~~~~~~~~~~~~~~~~~~//
#include "fvcCurl.H"
#include "phaseCompressibleMomentumTransportModel.H"
#include "dispersedVirtualMassModel.H"
#include "dispersedDragModel.H"
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~ End Code Repository ~~~~~~~~~~~~~~~~~~~~~~~~~~//

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

namespace Foam
{
    bool dispersedPhaseInterfaceAddedHeadSeparator =
        phaseInterface::addHeadSeparator(dispersedPhaseInterface::separator());

    bool dispersedPhaseInterfaceAddedOldSeparatorToSeparator =
        phaseInterface::addOldSeparatorToSeparator
        (
            "in",
            dispersedPhaseInterface::separator()
        );
}

namespace Foam
{
    defineTypeNameAndDebugWithName
    (
        dispersedPhaseInterface,
        separatorsToTypeName({separator()}).c_str(),
        0
    );
    addToRunTimeSelectionTable(phaseInterface, dispersedPhaseInterface, word);
}


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

bool Foam::dispersedPhaseInterface::same
(
    const phaseInterface& interface,
    bool strict
) const
{
    return
        (!strict || isType<dispersedPhaseInterface>(interface))
     && (strict || isA<dispersedPhaseInterface>(interface))
     && &dispersed_
     == &refCast<const dispersedPhaseInterface>(interface).dispersed_
     && phaseInterface::same(interface, false);
}


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

Foam::dispersedPhaseInterface::dispersedPhaseInterface
(
    const phaseModel& dispersed,
    const phaseModel& continuous
)
:
    phaseInterface(dispersed, continuous),
    dispersed_(dispersed)
{}


Foam::dispersedPhaseInterface::dispersedPhaseInterface
(
    const phaseSystem& fluid,
    const word& name
)
:
    phaseInterface(fluid, name),
    dispersed_(identifyPhases(fluid, name, {separator()}).first())
{}


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

Foam::dispersedPhaseInterface::~dispersedPhaseInterface()
{}


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

Foam::word Foam::dispersedPhaseInterface::name() const
{
    return dispersed().name() + '_' + separator() + '_' + continuous().name();
}


const Foam::phaseModel& Foam::dispersedPhaseInterface::dispersed() const
{
    return dispersed_;
}


const Foam::phaseModel& Foam::dispersedPhaseInterface::continuous() const
{
    return otherPhase(dispersed_);
}


Foam::scalar Foam::dispersedPhaseInterface::sign() const
{
    return &dispersed_ == &phase1() ? +1 : -1;
}


Foam::tmp<Foam::volVectorField> Foam::dispersedPhaseInterface::Ur() const
{
    return dispersed().U() - continuous().U();
}


Foam::tmp<Foam::volScalarField> Foam::dispersedPhaseInterface::Re() const
{
    return magUr()*dispersed().d()/continuous().fluidThermo().nu();
}


Foam::tmp<Foam::volScalarField> Foam::dispersedPhaseInterface::Pr() const
{
    return
         continuous().fluidThermo().nu()
        *continuous().thermo().Cp()
        *continuous().rho()
        /continuous().thermo().kappa();
}


Foam::tmp<Foam::volScalarField> Foam::dispersedPhaseInterface::Eo() const
{
    return Eo(dispersed().d());
}


Foam::tmp<Foam::volScalarField> Foam::dispersedPhaseInterface::Eo
(
    const volScalarField& d
) const
{
    const uniformDimensionedVectorField& g =
        mesh().lookupObject<uniformDimensionedVectorField>("g");

    return
        mag(dispersed().rho() - continuous().rho())
       *mag(g)
       *sqr(d)
       /sigma();
}


Foam::tmp<Foam::volScalarField> Foam::dispersedPhaseInterface::Mo() const
{
    const uniformDimensionedVectorField& g =
        mesh().lookupObject<uniformDimensionedVectorField>("g");

    return
        mag(g)
       *continuous().fluidThermo().nu()
       *pow3
        (
            continuous().fluidThermo().nu()
           *continuous().rho()
           /sigma()
        );
}


Foam::tmp<Foam::volScalarField> Foam::dispersedPhaseInterface::Ta() const
{
    return Re()*pow(Mo(), 0.23);
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~ Begin Code Repository ~~~~~~~~~~~~~~~~~~~~~~~~~//
Foam::tmp<Foam::volScalarField>
Foam::dispersedPhaseInterface::St
(
    const dimensionedScalar& CT,
    const dimensionedScalar& CLambda
)
const
{
    const fvMesh& mesh(continuous().mesh());
    const phaseSystem& fluid(mesh.lookupObject<phaseSystem>("phaseProperties"));
    const dispersedPhaseInterface interface(dispersed(), continuous());
    const momentumTransportModel& continuousTurbulence =
        mesh.lookupObject<phaseCompressible::momentumTransportModel>
        (
            IOobject::groupName
            (
                momentumTransportModel::typeName,
                continuous().name()
            )
        );

    if (!fluid.foundInterfacialModel<dragModel>(interface))
    {
        FatalErrorInFunction
            << "A drag model for " << dispersed().name() << " in "
            << continuous().name() << " is not specified. This is "
            << "required for the calculation of Stokes number."
            << exit(FatalError);
    }

    if (!fluid.foundInterfacialModel<virtualMassModel>(interface))
    {
        FatalErrorInFunction
            << "A virtual mass model for " << dispersed().name() << " in "
            << continuous().name() << " is not specified. This is "
            << "required for the calculation of Stokes number."
            << exit(FatalError);
    }

    const dragModels::dispersedDragModel& drag  =
        fluid.lookupInterfacialModel<dragModels::dispersedDragModel>(interface);

    const virtualMassModels::dispersedVirtualMassModel& vm =
        fluid.lookupInterfacialModel
        <virtualMassModels::dispersedVirtualMassModel>
        (
            interface
        );

    return
        (dispersed().rho() + continuous().rho()*vm.Cvm())/drag.Ki()
       *sqrt
        (
            sqr(continuousTurbulence.epsilon()/(CT*continuousTurbulence.k()))
          + sqr
            (
                magUr()*continuousTurbulence.epsilon()
               /(CLambda*pow(continuousTurbulence.k(), 1.5))
            )
        );
}


Foam::tmp<Foam::volScalarField>
Foam::dispersedPhaseInterface::Sr(const volScalarField& Re) const
{
    return
        sqr(dispersed().d())*mag(fvc::curl(continuous().U()))
       /(Re*continuous().fluidThermo().nu());
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~ End Code Repository ~~~~~~~~~~~~~~~~~~~~~~~~~~//


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