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

application     laplacianFoam;

startFrom       startTime;

startTime       0;

stopAt          endTime;

endTime         3;

deltaT          1e-7;

writeControl    runTime;

writeInterval   0.5;

purgeWrite      0;

writeFormat     ascii;

writePrecision  12;

writeCompression off;

timeFormat      general;

timePrecision   8;

runTimeModifiable true;


functions                              
{                                     
    errorNorm                           
    {                                  
        type coded;                       
        libs (utilityFunctionObjects);    
        writeControl writeTime;           
                                      
        name analyticalSolution;          
                                      
        codeWrite                         
        #{                               
            const volScalarField& T_find = mesh().lookupObject<volScalarField>("T"); 

            const volVectorField& C = mesh().C(); 

            const surfaceVectorField& Cf = mesh().Cf(); 

            const scalarField& V = mesh().V();   

            volScalarField MMS_diff                     
            (                                
                IOobject                       
                (                              
                    "MMS_diff",                
                    mesh().time().timeName(),    
                    mesh(),                      
                    IOobject::NO_READ,           
                    IOobject::AUTO_WRITE         
                ),                             
                mesh(),                         
                dimensionedScalar ("MMS_diff_", dimless, 0.0) 
            );                               


            const Time& time = mesh().time(); 
            // Gets the current time value 
            const scalar t = time.value(); 
                                    

            forAll(MMS_diff, cellI)          
            {                               
                // Gets the x component of the current cell 
                const scalar x = C[cellI].x(); 

                //Gets the y component of the current cell 
                const scalar y = C[cellI].y(); 
                                     
                const scalar solution = 225.0 + 150*Foam::cos(0.10000000000000001*t + x*x + y*y); 
                                    
                                                    
                MMS_diff[cellI] = mag(solution - T_find[cellI]); 
            }                               

            forAll(MMS_diff.boundaryField(), patchI)      
            {                               
                forAll(MMS_diff.boundaryField()[patchI], faceI)      
                {                             
                    // Gets the x component of the current cell 
                    const scalar x = Cf.boundaryField()[patchI][faceI].x(); 

                    //Gets the y component of the current cell 
                    const scalar y = Cf.boundaryField()[patchI][faceI].y(); 
                                     
                    const scalar solution = 225.0 + 150*Foam::cos(0.10000000000000001*t + x*x + y*y); 
                                  
                                                      
                    MMS_diff.boundaryFieldRef()[patchI][faceI] = mag(solution - T_find.boundaryField()[patchI][faceI]); 
                }                             
            }                               

            Info << "L1 norm is: "    << gSum( MMS_diff*V )/gSum(V)            << endl;     

            Info << "L2 norm is: "    << sqrt( gSum(MMS_diff*MMS_diff*V)/gSum(V) )    << endl;     

            Info << "Linf norm is: "  << gMax( MMS_diff )                 << endl;     

                              
            MMS_diff.write();                
        #};                               
    }                                   
}

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