/*---------------------------------------------------------------------------*\
  ==  == ====== ====   ====    |
                    \\     ||  | 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/>.

Class
    Foam::blendingMethods::psiSurfPhaseTransfer

Description
    This blending method is based on psiSurf, with a modification for phase
    transfer (e.g. entrainment).

    The morphology of the phaseInterface is not of interest, as in entrainment,
    one phase is defined as continuous and the other one disperse, and this
    stays valid at any time. It is the interaction of the continuous phase of
    the pair and another continuous phase (i.e. air and water) that creates
    entrainment. Therefore, this blending function computes the blending
    coefficients for a phaseInterface based on interaction of the continuous
    phase of this pair and another continuous phase, defined as
    'entrainingContinuous'.

    As it is not allowed to use signed interfacial models for an dispersed phase
    interface, the coefficient (1 - f1 -f2) of psiSurf that equals 1 at the
    interface is switched with f2 in this function.

Usage
    \table
        Name | Description | Required | Default
        interfaceContinuous | Name of interfaceContinuous phase | yes | None
        entrainingContinuous | Name of entrainingContinuous phase | yes | None
        critAlpha.* | centre of transition region for * | no | 0.3
        deltaAlpha.* | half-width of transition region for * | no | 0.05
        n | number of grid cells covering the transition region | no | 5
        f | factor to determine half-width of transition region | no | 0.1
    \endtable

SourceFiles
    psiSurfPhaseTransfer.C

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

#ifndef psiSurfPhaseTransfer_H
#define psiSurfPhaseTransfer_H

#include "blendingMethod.H"

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

namespace Foam
{
namespace blendingMethods
{

/*---------------------------------------------------------------------------*\
                    Class psiSurfPhaseTransfer Declaration
\*---------------------------------------------------------------------------*/

class psiSurfPhaseTransfer
:
    public blendingMethod
{
    // Private Data

        //- Name of the continuous phase of phase interface
        const word interfaceContinuous_;

        //- Name of the continuous phase interacting with interfaceContinuous
        const word entrainingContinuous_;

        //- List of names made of interfaceContinuous and entrainingContinuous
        wordList continuousPhaseNames_;

        //- Centre of transition region (phase fraction)
        HashTable<dimensionedScalar, word, word::hash> critAlpha_;

        //- Half-with of transition region (phase fraction)
        HashTable<dimensionedScalar, word, word::hash> deltaAlpha_;

        //- Number of grid cells covering the transition region (phase gradient)
        const label nCells_;

        //- Factor to determine half-width of transition region (phase gradient)
        const scalar fDelta_;


    // Private Member Functions

        inline tmp<volScalarField> limit01(const volScalarField& field) const
        {
            return min(max(field, Zero), scalar(1.0));
        }

        inline tmp<volScalarField> blendingFunction
        (
            const volScalarField& field
        ) const
        {
            //return 0.5*(1.0 + cos(Foam::constant::mathematical::pi*field));
            return 1.0 - 3.0*pow(field, 2.0) + 2.0*pow(field, 3.0);
        }

        tmp<volScalarField> f1in2
        (
            const phaseModel& phase1,
            const phaseModel& phase2
        ) const;

        Foam::tmp<volScalarField> phiSurf
        (
            const phaseModel& phase
        ) const;


protected:

    // Protected Member Functions

        //- Evaluate the blending function
        virtual tmp<volScalarField> fContinuous
        (
            const UPtrList<const volScalarField>& alphas,
            const label phaseSet,
            const label systemSet
        ) const;

        //- Evaluate the blending function. Filters out phases that cannot
        //  become continuous from the sets, then calls fContinuous
        virtual tmp<volScalarField> f
        (
            const UPtrList<const volScalarField>& alphas,
            const label phaseSet,
            const label systemSet
        ) const;


public:

    //- Runtime type information
    TypeNameNoDebug("psiSurfPhaseTransfer");


    // Constructors

        //- Construct from a dictionary and two phases
        psiSurfPhaseTransfer
        (
            const dictionary& dict,
            const phaseInterface& interface
        );


    //- Destructor
    ~psiSurfPhaseTransfer();


    // Member Functions

        //- Return whether or not a phase can be considered continuous
        virtual bool canBeContinuous(const label index) const;

        //- Return whether or not this interface can segregate
        virtual bool canSegregate() const;
};


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

} // End namespace blendingMethods
} // End namespace Foam

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

#endif

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