/*---------------------------------------------------------------------------*\
  ==  == ====== ====   ====    |
                    \\     ||  | 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 "droplet.H"
#include "addToRunTimeSelectionTable.H"
#include "fvcSnGrad.H"
#include "fvcSurfaceIntegrate.H"
#include "surfaceInterpolate.H"

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

namespace Foam
{
namespace fv
{
    defineTypeNameAndDebug(droplet, 0);
    addToRunTimeSelectionTable
    (
        entrainmentModel,
        droplet,
        dictionary
    );
}
}


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

Foam::tmp<Foam::volScalarField>
Foam::fv::droplet::rate
(
    const phaseModel& entrainingContinuous
) const
{
    const segregatedPhaseInterface interface
    (
        entrainingContinuous,
        pairContinuous_
    );

    const volScalarField capillaryLength
    (
        sqrt(interface.sigma()/(mag(g_)*pairContinuous_.rho()))
    );

    const volScalarField We
    (
        entrainingContinuous.rho()*magSqr(interface.UrI())*capillaryLength
       /interface.sigma()
    );

    const surfaceScalarField zeroAlphaNeighbourInvDelta
    (
        interface.mesh().deltaCoeffs()
       *neg0
        (
            fvc::interpolate(pairContinuous_)
          - 0.5*mag(interface.mesh().delta())*mag(fvc::snGrad(pairContinuous_))
          - pairContinuous_.residualAlpha()
        )
    );

    volScalarField surfaceSum
    (
        IOobject
        (
            "surfaceSum",
            interface.mesh().time().name(),
            interface.mesh()
        ),
        interface.mesh(),
        dimensionedScalar(inv(dimLength), Zero)
    );

    surfaceSum.internalFieldRef() = fvc::surfaceSum(zeroAlphaNeighbourInvDelta);

    const volScalarField zeroAlphaCorrection
    (
        0.5*pos(pairContinuous_ - pairContinuous_.residualAlpha())
       *pairContinuous_*surfaceSum
    );

    return
        (mag(interface.gradAlpha()) + zeroAlphaCorrection)
       *(
            pairContinuous_.rho()
           *pairContinuous_.fluidThermo().nu()
           /capillaryLength
        )
       *min(coefficient_*We, scalar(0.5));
 }


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

Foam::fv::droplet::droplet
(
    const dictionary& dict,
    const dispersedPhaseInterface& interface
)
:
    entrainmentModel(dict, interface),
    coefficient_("coefficient", dimless, dict, 0.01)
{}


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

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


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