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

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

namespace Foam
{
namespace blendingMethods
{
    defineTypeName(psiSurf);

    addToRunTimeSelectionTable
    (
        blendingMethod,
        psiSurf,
        dictionary
    );
}
}


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

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

    return
        blendingFunction
        (
            limit01
            (
                (dispersed/(dispersed + continuous) - crit1 + delta1)
               /(2*delta1)
            )
        );
}


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

    static const dimensionedScalar deltaGrad = fDelta_*critGrad;

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


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


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

    return volScalarField::null();
}


Foam::tmp<Foam::volScalarField> Foam::blendingMethods::psiSurf::f
(
    const UPtrList<const volScalarField>& alphas,
    const label phaseSet,
    const label systemSet
) const
{
    const phaseModel& phase1 = interface_.phase1();
    const phaseModel& phase2 = interface_.phase2();

    const word phases12 =
        IOobject::groupName
        (
            interface_.phase1().name(),
            interface_.phase2().name()
        );

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

    if (phaseSet == 0b01 && systemSet == 0b11)
    {
        return volScalarField::New
        (
            IOobject::groupName
            (
                IOobject::groupName("f2DispersedIn1", typeName),
                phases12
            ),
            phiSurf(phase2)*f1in2(phase2,phase1)
        );
    }

    return constant(alphas, 0);
}


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

Foam::blendingMethods::psiSurf::psiSurf
(
    const dictionary& dict,
    const phaseInterface& interface
)
:
    blendingMethod(dict, interface),
    nCells_(dict.lookupOrDefault<label>("n", 5)),
    fDelta_(dict.lookupOrDefault<scalar>("f", dimless, 0.1))
{
    forAllConstIter(phaseInterface, interface, iter)
    {
        const phaseModel& phase = iter();
        const word& name = phase.name();

        const word nameCrit
        (
            IOobject::groupName("critAlpha", name)
        );

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

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

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


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

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


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

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


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


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