/*--------------------------------*- C++ -*----------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     | Website:  https://openfoam.org
    \\  /    A nd           | Version:  11
     \\/     M anipulation  |
\*---------------------------------------------------------------------------*/
FoamFile
{
    format      ascii;
    class       dictionary;
    location    "system";
    object      controlDict;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

application     foamRun;

solver          functions;

subSolver       incompressibleFluid;

startFrom       latestTime;

startTime       0;

subSolverTime   0;

stopAt          endTime;

endTime         0.2;

deltaT          0.0001;

writeControl    timeStep;

writeInterval   50;

purgeWrite      0;

writeFormat     ascii;

writePrecision  6;

writeCompression off;

timeFormat      general;

timePrecision   6;

runTimeModifiable true;

functions
{
    #includeFunc scalarTransport(T, diffusivity=constant, D = 0.01)

    mixingQualityCheck
    {
        type coded;

        // Load the library containing the 'coded' functionObject
        libs            ("libutilityFunctionObjects.so");

        codeInclude
        #{
            #include "volFields.H"
        #};

        codeExecute
        #{
            const volScalarField& T
            (
                mesh().lookupObject<volScalarField>("T")
            );

            const scalar maxT = max(T).value();
            const scalar meanT = T.weightedAverage(mesh().V()).value();

            const scalar mixingQuality = meanT/maxT;
            Info << "mixingQuality = " << mixingQuality << endl;

            if (mixingQuality > 0.9)
            {
                const_cast<Time&>(mesh().time()).writeAndEnd();
            }
        #};
    }
}

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