/*---------------------------------------------------------------------------*\
  ==  == ====== ====   ====    |
                    \\     ||  | 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::fv::EulerFilmTransfer

Description
    Film-Euler transfer model

    FvModel for transferring mass, momentum and energy from a continuous phase
    in the multiphaseEuler region to the film region and vice versa, based on
    local morphology criteria.

    The transfer occurs over a user-defined time scale and is triggered by
    a set of threshold parameters, including:
    - Maximum allowable film thickness in the film region.
    - Minimum volume fraction of the target phase in the multiphaseEuler region.
    - Minimum film thickness in the multiphaseEuler region.

Usage
    Example usage:
    \verbatim
    EulerFilmTransfer
    {
        type        EulerFilmTransfer;
        libs        ("libaddonFilmEulerTransfer.so");

        filmPatch   film;
        phase       liquid;

        morphologyTransfer
        {
            timeScale           0.1;
            deltaFactorToFilm   0.9;
            alphaToFilm         0.86;

            filmThickness
            {
                deltaMin        1e-6;
                nLayers         10;
                yDiff           1e-3;
            }
        }
    }
    \endverbatim

Usage
    \table
        Property           | Description               | Required | Default
        type     | type name: EulerFilmTransfer        | yes      |
        filmPatch          | Name of film patch        | yes      |
        phase    | Name of phase to be transferred     | yes      |
        timeScale          | Transfer timescale        | yes      |
        deltaFactorToFilm  | Film thickness threshold  | no       | great
        alphaToFilm        | Volume fraction threshold | no       | -great
        deltaMin           | Film thickness threshold  | no       | -great
        nLayers  | Cell layers for integration         | no       | 10
        yDiff    | Minimum difference in wall distance | no       | minDeltaY
    \endtable

See also
    Foam::fvc::integrateWallNormal
    Foam::functionObject::field::filmThickness

SourceFiles
    EulerFilmTransfer.C

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

#ifndef EulerFilmTransfer_H
#define EulerFilmTransfer_H

#include "fvModel.H"
#include "multiphaseEuler.H"

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

namespace Foam
{
namespace fv
{

class EulerFilmTransfer
:
    public fvModel
{
    // Private Data

        //- The Euler model
        const solvers::multiphaseEuler& Euler_;

        //- Film patch name
        word filmPatchName_;

        //- Film patch index
        const label filmPatchi_;

        //- The name of the transferred phase
        word phaseName_;

        //- Reference to the transferred phase model
        const phaseModel& phase_;

        //- Reference to the transferred phase thermo
        const rhoFluidThermo& thermo_;

        //- Current time index (used for updating)
        mutable label curTimeIndex_;

        //- Optional morphology transfer dictionary
        dictionary morphologyTransferDict_;

        //- Optional film thickness dictionary
        dictionary filmThicknessDict_;

        volScalarField::Internal transferRate_;


    // Private Member Functions

        //- Return the transfer rate from the film transferRateFunc
        template<class Type, class TransferRateFunc>
        inline tmp<VolInternalField<Type>> filmEulerTransferRate
        (
            TransferRateFunc transferRateFunc,
            const dimensionSet& dimProp
        ) const;

        //- Return the transfer rate of field f
        template<class Type, class FieldType>
        inline tmp<Field<Type>> TransferRate(const FieldType& f) const;


public:

    //- Runtime type information
    TypeName("EulerFilmTransfer");


    // Constructors

        //- Construct from explicit source name and mesh
        EulerFilmTransfer
        (
            const word& sourceName,
            const word& modelType,
            const fvMesh& mesh,
            const dictionary& dict
        );

        //- Disallow default bitwise copy construction
        EulerFilmTransfer
        (
            const EulerFilmTransfer&
        ) = delete;


    // Member Functions

            label filmPatchIndex() const
            {
                return filmPatchi_;
            }

            const volScalarField& alpha() const
            {
                return phase_;
            }


        // Checks

            //- Return the list of fields for which the option adds source term
            //  to the transport equation
            virtual wordList addSupFields() const;


        // Correct

            //- Solve the film and update the sources
            virtual void correct();


        // Calculate film thickness using transportFilmThickness
        tmp<volScalarField> filmThickness() const;


        // Sources

            //- Add implicit contribution to phase continuity equation
            virtual void addSup
            (
                const volScalarField& alpha,
                const volScalarField& rho,
                fvMatrix<scalar>& eqn
            ) const;

            //- Add implicit contribution to phase energy equation
            virtual void addSup
            (
                const volScalarField& alpha,
                const volScalarField& rho,
                const volScalarField& he,
                fvMatrix<scalar>& eqn
            ) const;

            //- Add implicit contribution to mixture momentum equation
            virtual void addSup
            (
                const volScalarField& rho,
                const volVectorField& U,
                fvMatrix<vector>& eqn
            ) const;


        // Transfer to film

            //- Return the mass transfer rate
            tmp<scalarField> rhoTransferRate() const;

            //- Return the energy transfer rate
            tmp<scalarField> heTransferRate() const;

            //- Return the momentum transfer rate
            tmp<vectorField> UTransferRate() const;


        // Mesh changes

            //- Update topology using the given map
            virtual void topoChange(const polyTopoChangeMap&);

            //- Update from another mesh using the given map
            virtual void mapMesh(const polyMeshMap&);

            //- Redistribute or update using the given distribution map
            virtual void distribute(const polyDistributionMap&);

            //- Update for mesh motion
            virtual bool movePoints();


    // Member Operators

        //- Disallow default bitwise assignment
        void operator=(const EulerFilmTransfer&) = delete;
};


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

} // End namespace fv
} // End namespace Foam

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

#endif

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