/*---------------------------------------------------------------------------*\
  ==  == ====== ====   ====    |
                    \\     ||  | 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 "LuoSvendsen.H"
#include "phaseCompressibleMomentumTransportModel.H"
#include "linearInterpolationWeights.H"
#include "addToRunTimeSelectionTable.H"

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

namespace Foam
{
namespace populationBalance
{
namespace binaryBreakupModels
{
    defineTypeNameAndDebug(LuoSvendsen, 0);
    addToRunTimeSelectionTable
    (
        binaryBreakupModel,
        LuoSvendsen,
        dictionary
    );
}
}
}


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

Foam::populationBalance::binaryBreakupModels::LuoSvendsen::LuoSvendsen
(
    const populationBalanceModel& popBal,
    const dictionary& dict
)
:
    binaryBreakupModel(popBal, dict),
    gammaUpperReg2by11_(),
    gammaUpperReg5by11_(),
    gammaUpperReg8by11_(),
    C4_("C4", dimless, dict, 0.923),
    beta_("beta", dimless, dict, 2.05),
    minEddyRatio_("minEddyRatio", dimless, dict, 11.4),
    kolmogorovLengthScale_
    (
        IOobject
        (
            "kolmogorovLengthScale",
            popBal_.time().name(),
            popBal_.mesh()
        ),
        popBal_.mesh(),
        dimensionedScalar
        (
            "kolmogorovLengthScale",
            dimLength,
            Zero
        )
    )
{
    List<Tuple2<scalar, scalar>> gammaUpperReg2by11Table;
    List<Tuple2<scalar, scalar>> gammaUpperReg5by11Table;
    List<Tuple2<scalar, scalar>> gammaUpperReg8by11Table;

    gammaUpperReg2by11Table.append(Tuple2<scalar, scalar>(0.0, 1.0));
    gammaUpperReg5by11Table.append(Tuple2<scalar, scalar>(0.0, 1.0));
    gammaUpperReg8by11Table.append(Tuple2<scalar, scalar>(0.0, 1.0));

    for (scalar z = 1e-2; z <= 10.0; z = z + 1e-2)
    {
        Tuple2<scalar, scalar> gamma2by11
            (
                z,
                incGammaRatio_Q(2.0/11.0, z)
            );

        Tuple2<scalar, scalar> gamma5by11
            (
                z,
                incGammaRatio_Q(5.0/11.0, z)
            );

        Tuple2<scalar, scalar> gamma8by11
            (
                z,
                incGammaRatio_Q(8.0/11.0, z)
            );

        gammaUpperReg2by11Table.append(gamma2by11);
        gammaUpperReg5by11Table.append(gamma5by11);
        gammaUpperReg8by11Table.append(gamma8by11);
    }

    gammaUpperReg2by11_ =
        new Function1s::Table<scalar>
        (
            "gamma2by11",
            Function1s::tableBase::boundsHandling::clamp,
            linearInterpolationWeights::typeName,
            autoPtr<TableReader<scalar>>(nullptr),
            gammaUpperReg2by11Table
        );

    gammaUpperReg5by11_ =
        new Function1s::Table<scalar>
        (
            "gamma5by11",
            Function1s::tableBase::boundsHandling::clamp,
            linearInterpolationWeights::typeName,
            autoPtr<TableReader<scalar>>(nullptr),
            gammaUpperReg5by11Table
        );

    gammaUpperReg8by11_ =
        new Function1s::Table<scalar>
        (
            "gamma8by11",
            Function1s::tableBase::boundsHandling::clamp,
            linearInterpolationWeights::typeName,
            autoPtr<TableReader<scalar>>(nullptr),
            gammaUpperReg8by11Table
        );
}


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

void Foam::populationBalance::binaryBreakupModels::LuoSvendsen::precompute()
{
    kolmogorovLengthScale_ =
        pow025
        (
            pow3
            (
                popBal_.continuousPhase().fluidThermo().nu()
            )
           /popBal_.continuousTurbulence().epsilon()
        );
}


void Foam::populationBalance::binaryBreakupModels::LuoSvendsen::
addToBinaryBreakupRate
(
    volScalarField::Internal& binaryBreakupRate,
    const label i,
    const label j
)
{
    const dimensionedScalar& dSphj = popBal_.dSph(j);
    const dimensionedScalar& vi = popBal_.v(i);
    const dimensionedScalar& vj = popBal_.v(j);

    const volScalarField::Internal& rhoc = popBal_.continuousPhase().rho();

    tmp<volScalarField> tsigma(popBal_.sigmaWithContinuousPhase(i));
    const volScalarField::Internal& sigma = tsigma();

    tmp<volScalarField> tepsilonc(popBal_.continuousTurbulence().epsilon());
    const volScalarField::Internal& epsilonc = tepsilonc();

    const dimensionedScalar cf
    (
        pow(vi/vj, 2.0/3.0) + pow((1 - vi/vj), 2.0/3.0) - 1
    );

    const volScalarField::Internal b
    (
        12*cf*sigma
       /(beta_*rhoc*pow(dSphj, 5.0/3.0)*pow(epsilonc, 2.0/3.0))
    );

    const volScalarField::Internal xiMin
    (
        minEddyRatio_*kolmogorovLengthScale_/dSphj
    );

    const volScalarField::Internal tMin(b/pow(xiMin, 11.0/3.0));

    volScalarField::Internal integral(3/(11*pow(b, 8.0/11.0)));
    forAll(integral, celli)
    {
        integral[celli] *=
            2
           *pow(b[celli], 3.0/11.0)
           *tgamma(5.0/11.0)
           *(
                gammaUpperReg5by11_->value(b[celli])
              - gammaUpperReg5by11_->value(tMin[celli])
            );
    }

    binaryBreakupRate +=
        C4_
       *(1 - popBal_.alphas()())
       /vj
       *cbrt(epsilonc/sqr(dSphj))
       *integral;
}


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