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

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

namespace Foam
{
namespace fv
{

    defineTypeNameAndDebug(Fuchs, 0);
    addToRunTimeSelectionTable
    (
        decontaminationModel, Fuchs, dictionary
    );
}
}

using namespace Foam::constant::mathematical;


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

inline Foam::tmp<Foam::volScalarField>
Foam::fv::Fuchs::tauP
(
    const volScalarField& rhoP,
    const dimensionedScalar& dp,
    const volScalarField& muB
) const
{
    return rhoP*sqr(dp)/(18.0*muB);
}


inline Foam::tmp<Foam::volScalarField>
Foam::fv::Fuchs::Dp
(
    const dimensionedScalar& kB,
    const volScalarField& T,
    const dimensionedScalar& dp,
    const volScalarField& muB
) const
{
    return kB*T/(3.0*pi*muB*dp);
}


inline Foam::tmp<Foam::volScalarField>
Foam::fv::Fuchs::aS
(
    const volScalarField& tauP,
    const volScalarField& Urel,
    const volScalarField& d
) const
{
    const uniformDimensionedVectorField& g =
        interface_.mesh().lookupObject<uniformDimensionedVectorField>("g");

    return
        3.0*mag(g)*tauP
       /max(2.0*Urel*d, dimensionedScalar(dimensionSet(0, 2, -1, 0, 0), SMALL));
}


inline Foam::tmp<Foam::volScalarField>
Foam::fv::Fuchs::aC
(
    const volScalarField& tauP,
    const volScalarField& Urel,
    const volScalarField& d
) const
{
    return 18.0*Urel*tauP/sqr(d);
}


inline Foam::tmp<Foam::volScalarField>
Foam::fv::Fuchs::aD
(
    const volScalarField& Dp,
    const volScalarField& Urel,
    const volScalarField& d
) const
{
    return
        5.09*sqrt
        (
            Dp
           /max
            (
                Urel*pow3(d),
                dimensionedScalar(dimensionSet(0, 4, -1, 0, 0), SMALL)
            )
        );
}


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

Foam::tmp<Foam::volScalarField>
Foam::fv::Fuchs::rate() const
{
    const volScalarField rhoP(thermo_.rhoi(indexI_, thermo_.p(), thermo_.T()));
    const volScalarField muB(thermo_.mui(indexC_, thermo_.p(), thermo_.T()));
    const volScalarField magUrel(mag(interface_.Ur()));
    const volScalarField& Yp = gasPhase_.Y(species_[0]);
    volScalarField rateFactor
    (
        volScalarField::New
        (
            IOobject::groupName("rateFactor", gasPhase_.name()),
            interface_.mesh(),
            dimensionedScalar(inv(dimLength), Zero)
        )
    );

    if (gravitationalSedimentation_)
    {
        rateFactor += aS(tauP(rhoP, dp_, muB), magUrel, gasPhase_.d());
    }


    if (centrifugalImpaction_)
    {
        rateFactor += aC(tauP(rhoP, dp_, muB), magUrel, gasPhase_.d());
    }


    if (brownianDiffusion_)
    {
        rateFactor +=
            aD(Dp(kB_, thermo_.T(), dp_, muB), magUrel, gasPhase_.d());
    }


    Psi_ = rateFactor*gasPhase_*magUrel*Yp*gasPhase_.rho();

    return   Psi_;
}


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

Foam::fv::Fuchs::Fuchs
(
    const dictionary& dict,
    const dispersedPhaseInterface& interface,
    const hashedWordList& species
)
:
    decontaminationModel(dict, interface, species),
    kB_("kB", dimEnergy/dimTemperature, 1.38e-23),
    dp_("dp", dimLength, dict),
    gravitationalSedimentation_
    (
        dict.lookupOrDefault<Switch>("gravitationalSedimentation", true)
    ),
    centrifugalImpaction_
    (
        dict.lookupOrDefault<Switch>("centrifugalImpaction", true)
    ),
    brownianDiffusion_
    (
        dict.lookupOrDefault<Switch>("brownianDiffusion", true)
    ),
    Psi_
    (
        IOobject
        (
            IOobject::groupName("Psi", gasPhase_.name()),
            interface_.mesh().time().name(),
            interface_.mesh(),
            IOobject::NO_READ,
            IOobject::AUTO_WRITE
        ),
        interface_.mesh(),
        dimensionedScalar(dimDensity/dimTime, Zero)
    )
{}


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

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


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

