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

Description
    Compares two given fields and writes out the deviation. Returns error in
    post-process mode if magnitude of deviation exceeds specified threshold.
    Works for volume as well as surface fields.

    Example of function object specification:
    \verbatim
    deviation.U
    {
        type            deviation;
        libs            ("libaddonFieldFunctionObjects.so");
        field           U;
        referenceField  U.ref;
        maxAbsDev       1e-6;

        executeControl  writeTime;
        writeControl    writeTime;
    }
    \endverbatim

Usage
    \table
        Property       | Description             | Required | Default
        type           | type name: deviation    | yes      |
        field          | field name              | yes      |
        referenceField | reference field name    | yes      |
        maxAbsDev      | max. absolute deviation | yes      |
    \endtable

See also
    Foam::functionObject::fieldExpression

SourceFiles
    deviation.C

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

#ifndef functionObjects_deviation_H
#define functionObjects_deviation_H

#include "fieldExpression.H"

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

namespace Foam
{
namespace functionObjects
{

/*---------------------------------------------------------------------------*\
                          Class deviation Declaration
\*---------------------------------------------------------------------------*/

class deviation
:
    public fieldExpression
{
    // Private Data

        //- Name of the reference field
        word referenceFieldName_;

        //- Resulting maximum absolute deviation
        scalar resultMaxAbsDev_;

        //- Threshold for maximum absolute deviation
        scalar maxAbsDev_;


    // Private Member Functions

        //- Subtract the field from the reference field and register the result
        template<class Type>
        bool calcDeviation();

        //- Subtract the field from the reference field and return true if
        //  successful. Returns fatal error in post-processing mode if deviation
        //  is above threshold.
        virtual bool calc();


public:

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


    // Constructors

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


    //- Destructor
    virtual ~deviation();
};


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

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

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

#ifdef NoRepository
    #include "deviationTemplates.C"
#endif

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

#endif

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