/*---------------------------------------------------------------------------*\
  ==  == ====== ====   ====    |
                    \\     ||  | 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 "psiSurfPhaseTransfer.H"
#include "fvcGrad.H"
#include "addToRunTimeSelectionTable.H"

#include "phaseSystem.H"

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

namespace Foam
{
namespace blendingMethods
{
    defineTypeName(psiSurfPhaseTransfer);

    addToRunTimeSelectionTable
    (
        blendingMethod,
        psiSurfPhaseTransfer,
        dictionary
    );
}
}


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

Foam::tmp<Foam::volScalarField>
Foam::blendingMethods::psiSurfPhaseTransfer::f1in2
(
    const phaseModel& phase1,
    const phaseModel& phase2
) const
{
    const dimensionedScalar crit1(critAlpha_[phase1.name()]);
    const dimensionedScalar delta1(deltaAlpha_[phase1.name()]);

    return
        blendingFunction
        (
            limit01
            (
                (phase1/(phase1 + phase2) - crit1 + delta1)/(2*delta1)
            )
        );
}


Foam::tmp<Foam::volScalarField>
Foam::blendingMethods::psiSurfPhaseTransfer::phiSurf
(
    const phaseModel& phase
) const
{
    static const dimensionedScalar critGrad
    (
        1/(nCells_*cbrt(average(phase.mesh().V())))
    );

    static const dimensionedScalar deltaGrad(fDelta_*critGrad);

    return
        blendingFunction
        (
            limit01
            (
                (mag(fvc::grad(phase)) - critGrad + deltaGrad)/(2*deltaGrad)
            )
        );
}


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

Foam::tmp<Foam::volScalarField>
Foam::blendingMethods::psiSurfPhaseTransfer::fContinuous
(
    const UPtrList<const volScalarField>& alphas,
    const label phaseSet,
    const label systemSet
) const
{
    NotImplemented;

    return volScalarField::null();
}


Foam::tmp<Foam::volScalarField> Foam::blendingMethods::psiSurfPhaseTransfer::f
(
    const UPtrList<const volScalarField>& alphas,
    const label phaseSet,
    const label systemSet
) const
{
    const word phases12 =
        IOobject::groupName
        (
            interface_.phase1().name(),
            interface_.phase2().name()
        );

    const phaseModel& interfaceContinuous =
        interface_.fluid().phases()[interfaceContinuous_];

    const phaseModel& entrainingContinuous =
        interface_.fluid().phases()[entrainingContinuous_];

    if (phaseSet == 0b10 && systemSet == 0b11)
    {
        return volScalarField::New
        (
            IOobject::groupName
            (
                IOobject::groupName("f1DispersedIn2", typeName),
                phases12
            ),
            phiSurf(entrainingContinuous)
           *f1in2(entrainingContinuous, interfaceContinuous)
        );
    }

    if (phaseSet == 0b01 && systemSet == 0b11)
    {
        return volScalarField::New
        (
            IOobject::groupName
            (
                IOobject::groupName("f2DispersedIn1", typeName),
                phases12
            ),
            scalar(1)
          - phiSurf(entrainingContinuous)
           *f1in2(entrainingContinuous, interfaceContinuous)
          - phiSurf(interfaceContinuous)
           *f1in2(interfaceContinuous, entrainingContinuous)
        );
    }

    return constant(alphas, 0);
}


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

Foam::blendingMethods::psiSurfPhaseTransfer::psiSurfPhaseTransfer
(
    const dictionary& dict,
    const phaseInterface& interface
)
:
    blendingMethod(dict, interface),
    interfaceContinuous_
    (
        dict.lookupBackwardsCompatible
        (
            {"pairContinuous", "interfaceContinuous"}
        )
    ),
    entrainingContinuous_(dict.lookup("entrainingContinuous")),
    continuousPhaseNames_(0),
    nCells_
    (
        dict.lookupOrDefault<label>("n", 5)
    ),
    fDelta_
    (
        dict.lookupOrDefault<scalar>("f", dimless, 0.1)
    )
{
    continuousPhaseNames_.append(interfaceContinuous_);
    continuousPhaseNames_.append(entrainingContinuous_);

    forAllConstIter(wordList, continuousPhaseNames_, iter)
    {
        const word nameCrit
        (
            IOobject::groupName("critAlpha", *iter)
        );

        critAlpha_.insert
        (
            *iter,
            dimensionedScalar
            (
                nameCrit,
                dimless,
                dict.lookupOrDefault(nameCrit, 0.3)
            )
        );

        const word nameDelta
        (
            IOobject::groupName("deltaAlpha", *iter)
        );

        deltaAlpha_.insert
        (
            *iter,
            dimensionedScalar
            (
                nameDelta,
                dimless,
                dict.lookupOrDefault(nameDelta, 0.05)
            )
        );
    }
}


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

Foam::blendingMethods::psiSurfPhaseTransfer::~psiSurfPhaseTransfer()
{}


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

bool
Foam::blendingMethods::psiSurfPhaseTransfer::canBeContinuous
(
    const label index
) const
{
    return true;
}


bool Foam::blendingMethods::psiSurfPhaseTransfer::canSegregate() const
{
    return false;
}


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