CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM

gSum( ) vs sum()

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

Like Tree10Likes
  • 7 Post By bigphil
  • 2 Post By bigphil
  • 1 Post By bigphil

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   November 2, 2010, 13:29
Default gSum( ) vs sum()
  #1
Senior Member
 
Nima Samkhaniani
Join Date: Sep 2009
Location: Tehran, Iran
Posts: 1,266
Blog Entries: 1
Rep Power: 24
nimasam is on a distinguished road
hi foamer
could please tell me whats the difference between gSum() and sum() ?
nimasam is offline   Reply With Quote

Old   November 2, 2010, 15:01
Default
  #2
Super Moderator
 
bigphil's Avatar
 
Philip Cardiff
Join Date: Mar 2009
Location: Dublin, Ireland
Posts: 1,089
Rep Power: 34
bigphil will become famous soon enoughbigphil will become famous soon enough
Hi,

I think that gSum() is global sum, which means that gSum() sums over all the processors in a parallel run.

Philip
bigphil is offline   Reply With Quote

Old   June 1, 2018, 02:04
Default
  #3
New Member
 
javad haqqani
Join Date: Dec 2017
Posts: 16
Rep Power: 8
maksjood is on a distinguished road
Now how can one perform a partial summation? I need to sum up half of a volScalarField.
maksjood is offline   Reply With Quote

Old   June 1, 2018, 04:21
Default
  #4
Super Moderator
 
bigphil's Avatar
 
Philip Cardiff
Join Date: Mar 2009
Location: Dublin, Ireland
Posts: 1,089
Rep Power: 34
bigphil will become famous soon enoughbigphil will become famous soon enough
Quote:
Originally Posted by maksjood View Post
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;
farzadmech and wht like this.
bigphil is offline   Reply With Quote

Old   June 1, 2018, 05:23
Default
  #5
New Member
 
javad haqqani
Join Date: Dec 2017
Posts: 16
Rep Power: 8
maksjood is on a distinguished road
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.
maksjood is offline   Reply With Quote

Old   June 1, 2018, 06:37
Default
  #6
Super Moderator
 
bigphil's Avatar
 
Philip Cardiff
Join Date: Mar 2009
Location: Dublin, Ireland
Posts: 1,089
Rep Power: 34
bigphil will become famous soon enoughbigphil will become famous soon enough
Quote:
Originally Posted by maksjood View Post
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.

Also, I suggest you give more details about your problem and your attempted solutions.
Tobermory likes this.
bigphil is offline   Reply With Quote

Old   February 4, 2023, 20:34
Default
  #7
Senior Member
 
Farzad Faraji
Join Date: Nov 2019
Posts: 205
Rep Power: 7
farzadmech is on a distinguished road
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,
Farzad

Quote:
Originally Posted by bigphil View Post
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;
farzadmech is offline   Reply With Quote

Old   February 7, 2023, 05:32
Default
  #8
Super Moderator
 
bigphil's Avatar
 
Philip Cardiff
Join Date: Mar 2009
Location: Dublin, Ireland
Posts: 1,089
Rep Power: 34
bigphil will become famous soon enoughbigphil will become famous soon enough
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());
bigphil is offline   Reply With Quote

Reply

Tags
gsum(), sum()

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Transient simulation not converging skabilan OpenFOAM Running, Solving & CFD 14 December 16, 2019 23:12
How to write k and epsilon before the abnormal end xiuying OpenFOAM Running, Solving & CFD 8 August 27, 2013 15:33
Convergence moving mesh lr103476 OpenFOAM Running, Solving & CFD 30 November 19, 2007 14:09
IcoFoam parallel woes msrinath80 OpenFOAM Running, Solving & CFD 9 July 22, 2007 02:58
Could anybody help me see this error and give help liugx212 OpenFOAM Running, Solving & CFD 3 January 4, 2006 18:07


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