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

Description
    Calculates and writes out integral (integer moments) or mean properties
    (mean, variance, standard deviation) of a size distribution determined by a
    population balance model. Requires solver post-processing.

    The following function object specification for example returns the first
    moment of the volume-based number density function which is equivalent to
    the phase fraction of the particulate phase:

    \verbatim
    populationBalanceMoments
    {
        type                populationBalanceMoments;
        libs                ("libaddonMultiphaseEulerFunctionObjects.so");
        executeControl      timeStep;
        writeControl        writeTime;
        populationBalance   bubbles;
        momentType          integerMoment;
        coordinateType      volume;
        order               1;
    }
    \endverbatim

Usage
    \table
        Property          | Description              | Required  | Default
        populationBalance | population balance name  | yes       |
        momentType        | desired moment of the distribution\\
                                                     | yes       |
        coordinateType    | particle property        | yes       |
        weightType        | number/volume/area concentration\\
                                                     | no\\
                                                     | numberConcentration
        order             | order of integer moment  | for integer moments |
        meanType          | arithmetic or geometric  | for non-integer moments\\
                                                                 | arithmetic
    \endtable

See also
    Foam::populationBalanceModel
    Foam::functionObjects::fvMeshFunctionObject
    Foam::functionObject

SourceFiles
    populationBalanceMoments.C

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

#ifndef populationBalanceMoments_H
#define populationBalanceMoments_H

#include "fvMeshFunctionObject.H"
#include "populationBalanceModel.H"

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

namespace Foam
{
namespace functionObjects
{

/*---------------------------------------------------------------------------*\
                  Class populationBalanceMoments Declaration
\*---------------------------------------------------------------------------*/

class populationBalanceMoments
:
    public fvMeshFunctionObject
{
public:

    //- Enumeration for the moment types
    enum class momentType
    {
        integerMoment,
        mean,
        variance,
        stdDev
    };

    //- Names of the moment types
    static const NamedEnum<momentType, 4> momentTypeNames_;

    //- Enumeration for the coordinate types
    enum class coordinateType
    {
        volume,
        area,
        diameter
    };

    //- Names of the coordinate types
    static const NamedEnum<coordinateType, 3> coordinateTypeNames_;

    //- Enumeration for the weight types
    enum class weightType
    {
        numberConcentration,
        volumeConcentration,
        areaConcentration
    };

    //- Names of the weight types
    static const NamedEnum<weightType, 3> weightTypeNames_;

    //- Enumeration for the mean types
    enum class meanType
    {
        arithmetic,
        geometric,
        notApplicable
    };

    //- Names of the mean types
    static const NamedEnum<meanType, 3> meanTypeNames_;


private:

    // Private Data

        //- Name of the population balance
        const word popBalName_;

        //- Moment type
        momentType momentType_;

        //- Coordinate type
        coordinateType coordinateType_;

        //- Weight type
        weightType weightType_;

        //- Mean type
        meanType meanType_;

        //- Integer moment order
        int order_;

        //- Result field
        autoPtr<volScalarField> fldPtr_;


    // Private Member Functions

        //- Coordinate type symbolic name for shorter field names
        word coordinateTypeSymbolicName();

        //- Weight type symbolic name for shorter field names
        word weightTypeSymbolicName();

        //- Default field name
        word defaultFldName();

        //- Integer moment field name
        word integerMomentFldName();

        //- Set dimensions
        void setDimensions(volScalarField& fld, momentType momType);

        //- Total concentration
        tmp<volScalarField> totalConcentration(const populationBalanceModel&);

        //- Mean value
        tmp<volScalarField> mean(const populationBalanceModel&);

        //- Variance
        tmp<volScalarField> variance(const populationBalanceModel&);

        //- Standard deviation
        tmp<volScalarField> stdDev(const populationBalanceModel&);


public:

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


    // Constructors

        //- Construct from Time and dictionary
        populationBalanceMoments
        (
            const word& name,
            const Time& runTime,
            const dictionary&
        );

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


    //- Destructor
    virtual ~populationBalanceMoments();


    // Member Functions

        //- Read the data
        virtual bool read(const dictionary&);

        //- Return the list of fields required
        virtual wordList fields() const
        {
            return wordList::null();
        }

        //- Calculate the moment fields
        virtual bool execute();

        //- Write the moment fields
        virtual bool write();


    // Member Operators

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


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

} // End namespace functionObjects
} // End namespace Foam

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

#endif

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