/*--------------------------------*- C++ -*----------------------------------*\
  ==  == ====== ====   ====    |
                    \\     ||  | Multiphase Cases Repository by HZDR
  ======   //   ||  || ===//   | Website: https://doi.org/10.14278/rodare.811
  ||  ||  //    ||  // || \\   | License: Creative Commons Attribution 4.0
  ==  == ====== ====   ==  ==  |
-------------------------------------------------------------------------------
Description
    Computes and stores the volumetric mass transfer coefficient kLa.

\*---------------------------------------------------------------------------*/

libs            ("libutilityFunctionObjects.so");
type            coded;
dispersedPhase  air;
continuousPhase water;
C               0.4;
GammaO2         2e-9;
executeControl  timeStep;
writeControl    writeTime;

codeOptions
#{
    -I$(FOAM_MODULES)/multiphaseEuler/phaseSystem/lnInclude \
    -I$(LIB_SRC)/MomentumTransportModels/momentumTransportModels/lnInclude \
    -I$(LIB_SRC)/MomentumTransportModels/compressible/lnInclude \
    -I$(LIB_SRC)/MomentumTransportModels/phaseCompressible/lnInclude \
    -I$(LIB_SRC)/physicalProperties/lnInclude \
    -I$(LIB_SRC)/thermophysicalModels/basic/lnInclude \
    -I$(LIB_SRC)/twoPhaseModels/compressibleTwoPhases/lnInclude \
    -I$(LIB_SRC)/twoPhaseModels/twoPhaseMixture/lnInclude
#};

codeInclude
#{
    #include "volFields.H"
    #include "phaseSystem.H"
    #include "phaseCompressibleMomentumTransportModel.H"
#};

codeData
#{
    word   dispersedPhase_;
    word   continuousPhase_;
    scalar C_;
    autoPtr<dimensionedScalar> GammaO2_;
    autoPtr<volScalarField> kL_;
    autoPtr<volScalarField> a_;
    autoPtr<volScalarField> kLa_;
#};

codeRead
#{
    dispersedPhase_ = dict.lookup<word>("dispersedPhase");
    continuousPhase_ = dict.lookup<word>("continuousPhase");

    // Lamont and Scott constant
    C_ = dict.lookup<scalar>("C");

    // Oxygen molecular diffusion coefficient
    GammaO2_.set(new dimensionedScalar("GammaO2", dimKinematicViscosity, dict));

    // Liquid side mass transfer coefficient (m/s)
    kL_.set
    (
        new volScalarField
        (
            IOobject
            (
                "kL",
                mesh().time().name(),
                mesh(),
                IOobject::NO_READ,
                IOobject::AUTO_WRITE
            ),
            mesh(),
            dimensionedScalar("kL", dimLength/dimTime, Zero)
        )
    );

    // Specific internal area (1/m)
    a_.set
    (
        new volScalarField
        (
            IOobject
            (
                "a",
                mesh().time().name(),
                mesh(),
                IOobject::NO_READ,
                IOobject::AUTO_WRITE
            ),
            mesh(),
            dimensionedScalar("a", inv(dimLength), Zero)
        )
    );

    // Volumetric mass transfer coefficient (1/s)
    kLa_.set
    (
        new volScalarField
        (
            IOobject
            (
                "kLa",
                mesh().time().name(),
                mesh(),
                IOobject::NO_READ,
                IOobject::AUTO_WRITE
            ),
            mesh(),
            dimensionedScalar("kLa", inv(dimTime), Zero)
        )
    );
#};

codeExecute
#{
    const phaseSystem& fluid =
        mesh().lookupObject<phaseSystem>(phaseSystem::propertiesName);

    const phaseModel& continuousPhase = fluid.phases()[continuousPhase_];
    const phaseModel& dispersedPhase = fluid.phases()[dispersedPhase_];
    const phaseCompressible::momentumTransportModel& continuousTurbulence
        (
            mesh().lookupObject<phaseCompressible::momentumTransportModel>
            (
                IOobject::groupName
                (
                    momentumTransportModel::typeName,
                    continuousPhase_
                )
            )
        );

    kL_() =
        C_*sqrt(GammaO2_())
       *pow
        (
            continuousTurbulence.epsilon()/continuousPhase.fluidThermo().nu(),
            0.25
        );

    a_() = 6.0*dispersedPhase/dispersedPhase.d();
    kLa_() = a_()*kL_();
#};

codeWrite
#{
    kL_->write();
    a_->write();
    kLa_->write();

    Info<< "massTransferCoeff: Written kL, a, and kLa fields at time = "
        << mesh().time().userTimeValue() << endl;
#};

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