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

Description
    This fvModel mimics a degassing boundary condition, which includes the
    following sources/sinks applied in the cells next to the boundary marked by
    patchName:

    - All phases included in the degassingPhases list are considered as
      degassing phases and get a sink. The rate is calculated such that the
      whole phase fraction is removed within one time step. Along with the mass
      sink, there are sinks defined for all transported variables accordingly.
    - Exactly one phase has to be specified as a continuous phase either in
      fvModels or phaseProperties and gets a sink/source that is calculated,
      such that the sink of the degassing phases (positive rate) and the volume
      flux of the degassing phases across the boundaries is also counteracted.
      Hence, this phase will give way for the degassing phases injected into the
      domain.

    The boundary patch, to which the degassing option is applied to, has to be
    a wall with slip for the velocity field for the liquid and gas. Pressure
    should be specified by pRefValue.

    Note: This fvModel is developed and tested for incompressible fluid only.

Usage
    \table
        Property | Description | Required | Default value
        degassingPhases | degassing phase names | yes | -
        patchNames | Names of outlet boundary patches | yes
        continuousPhase | continuous phase name | no  | none diameter model
        alphaName | Name of the phase fraction/number density field | no | alpha
    \endtable

    Example specification:
    \verbatim
    <fvModelName>
    {
        type    degassingPhases;
        libs    ("libaddonMultiphaseEulerFvModels.so");

        degassingPhases (air1 air2);
        continuousPhase water;
        cellZone
        {
            type        patchCells;
            patches     (outlet1 outlet2);
        }
    }
    \endverbatim

SourceFiles
    degassingPhases.C

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

#ifndef degassingPhases_H
#define degassingPhases_H

#include "fvModel.H"
#include "fvCellZone.H"
#include "surfaceFields.H"
#include "volFields.H"
#include "phaseSystem.H"

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

namespace Foam
{
namespace fv
{

/*---------------------------------------------------------------------------*\
                       Class degassingPhases Declaration
\*---------------------------------------------------------------------------*/

class degassingPhases
:
    public fvModel
{
    // Private Member Data

        //- Names of degassing phases
        wordList degassingPhases_;

        //- Name of the phase that gives way
        word continuousPhase_;

        //- Name of phase fraction/number density fields
        word alphaName_;

        //- Constant access to phaseSystem
        const phaseSystem& fluid_;

        //- Names of alpha fields
        wordList fieldNames_;

        //- The cellZone the fvModel applies to
        fvCellZone zone_;


    // Private Member Functions

        //- Non-virtual read
        void readCoeffs(const dictionary& dict);

        //- Check if the phase is a degassing phase
        bool isDegassingPhase(const word& fieldName) const;

        //- Add source terms to a phase equation
        template<class Type>
        void addSupType
        (
            const volScalarField& alpha,
            const volScalarField& rho,
            const VolField<Type>& field,
            fvMatrix<Type>& eqn
        ) const;


public:

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


    // Constructors

        //- Construct from components
        degassingPhases
        (
            const word& name,
            const word& modelType,
            const fvMesh& mesh,
            const dictionary& dict
        );

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


    //- Destructor
    virtual ~degassingPhases();


    // Member Functions

        // Checks

            //- Return true if the option adds a source term to the given
            //  field's transport equation
            virtual bool addsSupToField(const word& fieldName) const;

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


        // Sources

            //- Add source terms to a phase equation with constant density
            void addSup
            (
                const volScalarField& alpha,
                const volScalarField& field,
                fvMatrix<scalar>& eqn
            ) const;

            //- Add a source term to a phase equation
            FOR_ALL_FIELD_TYPES
            (
                DEFINE_FV_MODEL_ADD_ALPHA_RHO_FIELD_SUP,
                fv::degassingPhases
            );


        // Mesh changes

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

            //- 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&);


        // IO

            //- Read source dictionary
            virtual bool read(const dictionary& dict);


    // Member Operators

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


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

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

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

#endif

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