# gSum( ) vs sum()

 Register Blogs Members List Search Today's Posts Mark Forums Read

 November 2, 2010, 13:29 gSum( ) vs sum() #1 Senior Member   Nima Samkhaniani Join Date: Sep 2009 Location: Tehran, Iran Posts: 1,266 Blog Entries: 1 Rep Power: 24 hi foamer could please tell me whats the difference between gSum() and sum() ?

 November 2, 2010, 15:01 #2 Super Moderator     Philip Cardiff Join Date: Mar 2009 Location: Dublin, Ireland Posts: 1,089 Rep Power: 34 Hi, I think that gSum() is global sum, which means that gSum() sums over all the processors in a parallel run. Philip reza2031, babakflame, meshman and 4 others like this.

 June 1, 2018, 02:04 #3 New Member   javad haqqani Join Date: Dec 2017 Posts: 16 Rep Power: 8 Now how can one perform a partial summation? I need to sum up half of a volScalarField.

June 1, 2018, 04:21
#4
Super Moderator

Philip Cardiff
Join Date: Mar 2009
Location: Dublin, Ireland
Posts: 1,089
Rep Power: 34
Quote:
 Originally Posted by maksjood Now how can one perform a partial summation? I need to sum up half of a volScalarField.
Depending on the cells of interest, you could do something like this (assuming a volScalarField called "vf" exists; I am using a simple criterion but these can be changed to whatever you want):

Code:
```scalar sumVf = 0.0;

forAll(vf, cellI)
{
// Sum up cells with a cell index less than 5 (you can use your own criteria here)
if (cellI < 5)
{
sumVf += vf[cellI];
}
}

// Sync sum across processors
reduce(sumVf, sumOp<scalar>());

Info<< "The global sumVf is: " << sumVf << endl;```

 June 1, 2018, 05:23 #5 New Member   javad haqqani Join Date: Dec 2017 Posts: 16 Rep Power: 8 I've done that; but I need Vf's dimension. Plus, i'm dealing with very small numbers and the vf[cellI] gives a very large value when the real velue is very small as 1e-100.

June 1, 2018, 06:37
#6
Super Moderator

Philip Cardiff
Join Date: Mar 2009
Location: Dublin, Ireland
Posts: 1,089
Rep Power: 34
Quote:
 Originally Posted by maksjood I've done that; but I need Vf's dimension. Plus, i'm dealing with very small numbers and the vf[cellI] gives a very large value when the real velue is very small as 1e-100.
Code:
`vf.dimensions();`
will return the dimensions of the vf field.

As regards "vf[cellI] gives a very large value when the real value is very small as 1e-100": this should not be the case: if vf[cellI] is equal to 1e-100 then that is what it should return; if you are getting some other large number then I suggest you check that you are not indexing outside the bounds of the array.

February 4, 2023, 20:34
#7
Senior Member

Join Date: Nov 2019
Posts: 205
Rep Power: 7
Dear Philip
Thanks for explaining how to use "reduce" command properly. I have another question;

Code:
```    volScalarField kineticEnergy0("kineticEnergy0", 0.5*magSqr(U));

Info << "size of-kineticEnergy0 = " << kineticEnergy0.size() << endl;```
I have 4,000,000 mesh and I am using 4 CPU for parallel run using simple decomposition, consequently size of kineticEnergy0 (using Info) is 1,000,000 , so I believe I have to sum up between CPUs to become 4,000,000 mesh and then I average it using below command;

Code:
` scalar kineticEnergy0Ave = gAverage(kineticEnergy0);`
Could you please tell me how to using "reduce" command for volScalarField?

Thanks,

Quote:
 Originally Posted by bigphil Depending on the cells of interest, you could do something like this (assuming a volScalarField called "vf" exists; I am using a simple criterion but these can be changed to whatever you want): Code: ```scalar sumVf = 0.0; forAll(vf, cellI) { // Sum up cells with a cell index less than 5 (you can use your own criteria here) if (cellI < 5) { sumVf += vf[cellI]; } } // Sync sum across processors reduce(sumVf, sumOp()); Info<< "The global sumVf is: " << sumVf << endl;```

 February 7, 2023, 05:32 #8 Super Moderator     Philip Cardiff Join Date: Mar 2009 Location: Dublin, Ireland Posts: 1,089 Rep Power: 34 The total energy can be calculated as Code: ``` const volScalarField kineticEnergy("kineticEnergy", 0.5*magSqr(U)); const scalar totalKineticEnergy = gSum(kineticEnergy);``` and the arithmetic average as Code: ` const scalar averageKineticEnergy = gAverage(kineticEnergy);` Note this average is a simple arithmetic average, and it probably makes more sense to calculate a volume-weighted average, e.g. Code: ` const scalar volumeWeightedAverageKineticEnergy = gAverage(kineticEnergy.primitiveField()*mesh.V())/gSum(mesh.V());`

 Tags gsum(), sum()