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

controlDict function coded

Register Blogs Community New Posts Updated Threads Search

Like Tree4Likes
  • 1 Post By Tobermory
  • 1 Post By gionni
  • 2 Post By M.Davoodi

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 11, 2021, 13:02
Default controlDict function coded
  #1
New Member
 
Mahdi
Join Date: May 2020
Posts: 10
Rep Power: 5
M.Davoodi is on a distinguished road
Hi Guys,

I have been trying to add a small function at the end of controlDict to calculate \int(alpha.dv) above and below the line y=0 in my geometry.
So I have put an "if" condition to separate y=0 and then the integral will be simply sum of alpha*Cellvolume.
The codes works fine, but it produces different results when I am running it in a "single core" or when it is run in "parallel". REALLY ODD!

I have used other functions both in parallel and in single core with the same solver and none of them have this issue. Am I missing something here?

Does anyone have any idea what I am doing wrong?


the code is provided bellow:





functions
{

Wout
{
functionObjectLibs ("libutilityFunctionObjects.so");
type coded;
redirectType calCd;
writeControl timeStep;
writeInterval 10;
enabled yes;

codeWrite
#{

// Lookup/create variables

const volScalarField& alpha1 = mesh().lookupObject<volScalarField>("alpha");


const volVectorField& C = mesh().C();
const scalarField& volume = mesh().V();

scalar totalVolumeUp(0.0);
scalar totalVolumeDown(0.0);


forAll(C,idx)
{
if (C[idx].component(1)>0)
{


totalVolumeUp += volume[idx]*alpha1[idx];


}
if (C[idx].component(1)<0)
{

totalVolumeDown += volume[idx]*alpha1[idx];

}

}

scalarList list;
list.append(mesh().time().value()); // Time (col 0)
list.append(totalVolumeUp); // vol up (col 1)
list.append(totalVolumeDown); // vol down (col 2)
list.append(totalVolumeDown+totalVolumeUp); // sum vol (col 3)
list.append((totalVolumeDown-totalVolumeUp)/(totalVolumeDown+totalVolumeUp)); //the lifting parameter (col 4)
// Write data

string comsh;
string filename("LiftingParameter.txt");
std::stringstream doub2str; doub2str.precision(18);

comsh = "./writeData " + filename;
forAll(list, id)
{
doub2str.str(std::string());
doub2str << list[id];
comsh += " " + doub2str.str();
}

if (Pstream::master())
{
system(comsh);
}

#};
}

}

Last edited by M.Davoodi; March 12, 2021 at 04:01.
M.Davoodi is offline   Reply With Quote

Old   March 12, 2021, 04:44
Default
  #2
Senior Member
 
Join Date: Apr 2020
Location: UK
Posts: 668
Rep Power: 14
Tobermory will become famous soon enough
Quote:
totalVolumeUp += volume[idx]*alpha1[idx];
Be careful if you are calculating sums across all the cells in a parallel run ... each processor only knows about its own slice of the domain. To get the complete sum, you need to use functions like gSum, to sum across all processors. Try googling for more info.
M.Davoodi likes this.
Tobermory is offline   Reply With Quote

Old   March 12, 2021, 05:15
Default
  #3
New Member
 
Mahdi
Join Date: May 2020
Posts: 10
Rep Power: 5
M.Davoodi is on a distinguished road
Quote:
Originally Posted by Tobermory View Post
Be careful if you are calculating sums across all the cells in a parallel run ... each processor only knows about its own slice of the domain. To get the complete sum, you need to use functions like gSum, to sum across all processors. Try googling for more info.
Hi Tobermory,
Thanks for your reply. This is exactly what I was thinking and was worries about.
I agree gsum would be an excellent choice but I am not sure if there is a way that I can use the "if" condition to separate the line y=0 in combination with gsum. Are you aware of a way for doing this?

Cheers
M.Davoodi is offline   Reply With Quote

Old   March 12, 2021, 07:14
Default
  #4
New Member
 
Giovanni Luddeni
Join Date: Jan 2021
Posts: 14
Rep Power: 5
gionni is on a distinguished road
I'm not sure if it's relevant, but you could try to implement the function in the solver itself.
I had a similar issue, where I needed to evaluate the maximum of a field in a BC: doing it in the BC itself would lead to the maximum being evaluated in the partial domain only, while doing it in the solver solved the problem.
I know it doesn't answer your question, but might be an alternative way to achieve what you need.
M.Davoodi likes this.
gionni is offline   Reply With Quote

Old   March 12, 2021, 08:06
Default
  #5
New Member
 
Mahdi
Join Date: May 2020
Posts: 10
Rep Power: 5
M.Davoodi is on a distinguished road
Quote:
Originally Posted by gionni View Post
I'm not sure if it's relevant, but you could try to implement the function in the solver itself.
I had a similar issue, where I needed to evaluate the maximum of a field in a BC: doing it in the BC itself would lead to the maximum being evaluated in the partial domain only, while doing it in the solver solved the problem.
I know it doesn't answer your question, but might be an alternative way to achieve what you need.
Hi Gionni,
Thanks for the response.
I actually managed to sort the issue out by adding this line at the end of the forAll loop condition:

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

You might find it helpful too
Tobermory and gionni like this.
M.Davoodi is offline   Reply With Quote

Reply


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
[Other] mesh airfoil NACA0012 anand_30 OpenFOAM Meshing & Mesh Conversion 13 March 7, 2022 17:22
Coded function object in openfoam v5 kit607 OpenFOAM Post-Processing 3 September 29, 2020 15:43
Unknown character in name of output variable when using coded function object pvergnol OpenFOAM Post-Processing 5 August 12, 2020 13:29
Compilation errors in ThirdPartymallochoard feng_w OpenFOAM Installation 1 January 25, 2009 06:59
Problem with compile the setParabolicInlet ivanyao OpenFOAM Running, Solving & CFD 6 September 5, 2008 20:50


All times are GMT -4. The time now is 01:18.