/*---------------------------------------------------------------------------*\
  ==  == ====== ====   ====    |
                    \\     ||  | 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 "addonInterfaceTurbulenceDamping.H"
#include "addToRunTimeSelectionTable.H"
#include "surfaceInterpolate.H"
#include "fvcGrad.H"
#include "fvcMeshPhi.H"
#include "fvmSup.H"
#include "fvmLaplacian.H"
#include "fvmDiv.H"
#include "kOmegaSST.H"

typedef
    Foam::RASModels::kOmegaSST<Foam::phaseCompressibleMomentumTransportModel>
    kOmegaSSTBase;


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

namespace Foam
{
    namespace fv
    {
        defineTypeNameAndDebug(addonInterfaceTurbulenceDamping, 0);

        addToRunTimeSelectionTable
        (
            fvModel,
            addonInterfaceTurbulenceDamping,
            dictionary
        );
    }
}


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

Foam::tmp<Foam::volScalarField::Internal>
Foam::fv::addonInterfaceTurbulenceDamping::interfaceFraction
(
    const volScalarField& alpha
) const
{
    return volScalarField::Internal::New
    (
        IOobject::groupName("A", phase_.name()),
        phase_.fluid().resolvedInterface().interfaceIndicator(phase_).ref()
    );
}


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

Foam::fv::addonInterfaceTurbulenceDamping::addonInterfaceTurbulenceDamping
(
    const word& sourceName,
    const word& modelType,
    const fvMesh& mesh,
    const dictionary& dict
)
:
    interfaceTurbulenceDamping(sourceName, modelType, mesh, dict),
    useBetaStar_(dict.lookupOrDefault<Switch>("useBetaStar", false))
{
    if (isA<kOmegaSSTBase>(this->turbulence_))
    {
        Info<< "        Detected kOmegaSST model " << endl;

        if (useBetaStar_)
        {
            Info<< "        Using betaStar formulation" << nl << endl;
        }
        else
        {
            Info<< "        Using beta formulation" << nl << endl;
        }
    }
    else
    {
        Info<< "        Detected kOmega model " << nl << endl;
    }
}


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

void Foam::fv::addonInterfaceTurbulenceDamping::addSup
(
    const volScalarField& alpha,
    const volScalarField& rho,
    const volScalarField& field,
    fvMatrix<scalar>& eqn
) const
{
    if (field.name() != IOobject::groupName("omega", phaseName_))
    {
        FatalErrorInFunction
            << "Support for field " << field.name() << " is not implemented"
            << exit(FatalError);
    }

    if (debug)
    {
        Info<< type() << ": applying source to " << eqn.psi().name() << endl;
    }

    const volScalarField::Internal aSqrnu
    (
        alpha*sqr(phase_.fluidThermo().nu()()())
    );

    if (isA<kOmegaSSTBase>(this->turbulence_))
    {
        const kOmegaSSTBase& sst =
            dynamic_cast<const kOmegaSSTBase&>(turbulence_);

        // Code copied from SSTBase::correct()
        const volScalarField CDkOmega
        (
            (2*sst.alphaOmega2_)
           *(fvc::grad(sst.k_) & fvc::grad(sst.omega_))/sst.omega_
        );
        const volScalarField F1(sst.F1(CDkOmega));
        const volScalarField::Internal beta(sst.beta(F1));

        if (useBetaStar_)
        {
            eqn +=
                rho()*interfaceFraction(alpha)*beta*aSqrnu
               /(sqr(betaStar_)*pow4(delta_));
        }
        else
        {
            eqn += rho()*interfaceFraction(alpha)*aSqrnu/(beta*pow4(delta_));
        }
    }
    else
    {
        eqn += rho()*interfaceFraction(alpha)*aSqrnu/(beta_*pow4(delta_));
    }
}


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