CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM (
-   -   Problem with functionObjects in parallel (

anishtain4 February 9, 2013 09:44

Problem with functionObjects in parallel
I'm using this functionObjects in controlDict so I can extract them later via foamLog to check some qualities of my solution:


        type            coded;
        functionObjectLibs ( "" );
        redirectType        error;
                const volVectorField& U = mesh().lookupObject<volVectorField>("U");
                const scalarField& mu = mesh().lookupObject<volScalarField>("mu");
                const scalarField& rho = mesh().lookupObject<volScalarField>("rho");
                const scalarField& p = mesh().lookupObject<volScalarField>("p");
                scalar C = average(sqrt(1.4*p/rho));
                scalar TKE =0.5*average(magSqr(U-average(U))).value();
                scalar lambdaf=1.0/average(magSqr(diag(fvc::grad(U))));
                scalar Reyf = average(rho/mu)*sqrt(4.0*TKE*lambdaf/3.0);
                Info << "TKE = " << TKE << endl;
                Info << "Reyf = " << Reyf << endl;
                Info << "MaPrime = " << 2.0*TKE/C/3.0 << endl;



They perform good in single process but in parallel I have to reduce them between processors with something like:


But I want to average them not sum, or I should divide this sum to number of processors which I don't know how to get number of processors?
I'm doing this manually now which is
Info << TKE/16.0 << endl;
and I know that I can use dict.lookup but that involves reading file in every single time step which is heavily inefficient

niklas February 9, 2013 16:16


            const volScalarField& p = mesh().lookupObject<volScalarField>("p");
            const scalarField& vols = mesh().V();
            scalar pSum = gSum(vols*p);
            scalar volTot = gSum(vols);
            scalar pAv = pSum/volTot;

and if you want density averaged properties you simply add rho to the calculation
you could simply write

            scalar pAv = gAverage(p);

anishtain4 February 11, 2013 15:27

Thanks niklas but my problem is not with the calculation, my problem is in the parallel case, the calculations you have mentioned would be calculated on every sub-domain which one would be printed out in the screen when you use Info<< ? Obviously you should reduce it among different processes and THIS is my problem, if I use sumOp it should be divided to number of processes or I should use some other operator other than sumOp.

niklas February 12, 2013 02:28

just try my code. Did you try it?

Did you notise the g in front of sum, gSum.
Can you guess what the g stands for.

The Info stream is only for processor 0, you also have an Sout stream, then it will
write the statement for every processor.

I think that you are not using the -parallel flag when you are running the parallel case.

anishtain4 February 12, 2013 06:27

Thanks niklas, that all make sense now but unfortunately it does not work in functionobjects of

btw: I use -parallel flag

All times are GMT -4. The time now is 12:24.