/*---------------------------------------------------------------------------*\
  ==  == ====== ====   ====    |
                    \\     ||  | 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 "addonPhaseTurbulenceStabilisation.H"
#include "phaseSystem.H"
#include "fvcDdt.H"
#include "fvmDdt.H"
#include "addToRunTimeSelectionTable.H"

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

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

        addToRunTimeSelectionTable
        (
            fvModel,
            addonPhaseTurbulenceStabilisation,
            dictionary
        );
    }
}


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

template<class Type>
void Foam::fv::addonPhaseTurbulenceStabilisation::addAlphaRhoSup
(
    const volScalarField& alpha,
    const volScalarField& rho,
    const VolField<Type>& field,
    fvMatrix<Type>& eqn,
    tmp<VolField<Type>>
    (phaseCompressible::momentumTransportModel::*psi)() const
) const
{
    if (debug)
    {
        Info<< type() << ": applying source to " << eqn.psi().name() << endl;
    }

    eqn -=
        fvm::ddt(phase_.residualAlpha()*rho, eqn.psi())
      - fvc::ddt(phase_.residualAlpha()*rho, eqn.psi());
}


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

Foam::fv::addonPhaseTurbulenceStabilisation::addonPhaseTurbulenceStabilisation
(
    const word& sourceName,
    const word& modelType,
    const fvMesh& mesh,
    const dictionary& dict
)
:
    fvModel(sourceName, modelType, mesh, dict),
    phaseName_(dict.lookup("phase")),
    phase_
    (
        mesh.lookupObject<phaseModel>(IOobject::groupName("alpha", phaseName_))
    ),
    turbulence_
    (
        mesh.lookupType<phaseCompressible::momentumTransportModel>
        (
            phaseName_
        )
    )
{
    const word kName(IOobject::groupName("k", phaseName_));
    const word epsilonName(IOobject::groupName("epsilon", phaseName_));
    const word omegaName(IOobject::groupName("omega", phaseName_));
    const word RName(IOobject::groupName("R", phaseName_));

    if (mesh.foundObject<volScalarField>(kName))
    {
        fieldNames_.append(kName);
    }

    if (mesh.foundObject<volScalarField>(epsilonName))
    {
        fieldNames_.append(epsilonName);
    }

    if (mesh.foundObject<volScalarField>(omegaName))
    {
        fieldNames_.append(omegaName);
    }

    if (mesh.foundObject<volSymmTensorField>(RName))
    {
        fieldNames_.append(RName);
    }
}


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

Foam::wordList Foam::fv::addonPhaseTurbulenceStabilisation::addSupFields() const
{
    return fieldNames_;
}


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


void Foam::fv::addonPhaseTurbulenceStabilisation::addSup
(
    const volScalarField& alpha,
    const volScalarField& rho,
    const volSymmTensorField& field,
    fvMatrix<symmTensor>& eqn
) const
{
    if
    (
        field.name() == IOobject::groupName("R", phase_.name())
    )
    {
        addAlphaRhoSup<symmTensor>
        (
            alpha,
            rho,
            field,
            eqn,
            &phaseCompressible::momentumTransportModel::R
        );
    }
    else
    {
        FatalErrorInFunction
            << "Support for field " << field.name() << " is not implemented"
            << exit(FatalError);
    }
}


void Foam::fv::addonPhaseTurbulenceStabilisation::topoChange
(
    const polyTopoChangeMap&
)
{}


void Foam::fv::addonPhaseTurbulenceStabilisation::mapMesh
(
    const polyMeshMap& map
)
{}


void Foam::fv::addonPhaseTurbulenceStabilisation::distribute
(
    const polyDistributionMap&
)
{}


bool Foam::fv::addonPhaseTurbulenceStabilisation::movePoints()
{
    return true;
}


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