CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > ANSYS > FLUENT > Fluent UDF and Scheme Programming

Parallelization of “DEFINE ON_DEMAND” & “DEFINE_SOURCE” UDF to apply momentum source

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes
  • 1 Post By kavatar

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   January 13, 2021, 07:00
Default Parallelization of “DEFINE ON_DEMAND” & “DEFINE_SOURCE” UDF to apply momentum source
  #1
New Member
 
Krishna
Join Date: Oct 2016
Posts: 12
Rep Power: 10
kavatar is on a distinguished road
Hello,

I have a text file storing x-momentum source at every cell center of the domain. I store the source term in order of global cell index in the text file. i.e. value in first line correspond to source of cell with index 0, value of second line correspond to source of cell with index 1 and so on. I want to read this text file and add source term to x-momentum equation. My UDF for serial run (it works fine)

#include "udf.h"
float xSource[194875]; //global array variable

DEFINE_ON_DEMAND(read_source)
{
int i;

FILE *xSourceFile; // declare a FILE pointer
xSourceFile = fopen("Fx.txt", "r"); // open file for reading
Message("xSourceFile file opened\n");

/* loop to read file */
i=0; // initialize before to loop
for (i=0;i<194875;i++)
{
fscanf (xSourceFile, "%f", xSource+i); // one value per line
}
fclose(xSourceFile);
Message("xSourceFile file closed\n");
}

DEFINE_SOURCE(xmom_source,c,t,dS,eqn)
{
dS[eqn]=0.0;
return xSource[c]; //xSource array has values in global index order
}


The above code works fine in serial. To run the same case in parallel I have made following code:

#include "udf.h"
float xSource[194875]; //global array variable

DEFINE_ON_DEMAND(read_source)
{
#if !RP_NODE
int i;

FILE *xSourceFile; // declare a FILE pointer
xSourceFile = fopen("Fx.txt", "r"); // open file for reading
Message("xSourceFile file opened\n");

/* loop to read file */
i=0; // initialize before to loop
for (i=0;i<194875;i++)
{
fscanf (xSourceFile, "%f", xSource+i); // one value per line
}
fclose(xSourceFile);
Message("xSourceFile file closed\n");
#endif

host_to_node_float(xSource,194875);
}

DEFINE_SOURCE(xmom_source,c,t,dS,eqn)
{
#if !RP_HOST
dS[eqn]=0.0;
return xSource[c]; //xSource array has values in global index order
#endif
}


When I use the second udf in parallel case the results are not similar to what I get in serial run with first udf. Can anyone identity the problem in the second udf which can be used in parallel run?
Even when i compile the second udf it gives a warning as "warning C4716: xmom_source': must return a value"
Thanks in advance
kavatar is offline   Reply With Quote

Old   January 14, 2021, 01:17
Default
  #2
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
not sure, but the problem may come from your source array and fluent partitioning of domain for parallel computation

it could be, that for each divided for parallel computation zone fluent starts counting cells (c variable) from the beginning
__________________
best regards


******************************
press LIKE if this message was helpful
AlexanderZ is offline   Reply With Quote

Old   January 14, 2021, 01:50
Default
  #3
New Member
 
Krishna
Join Date: Oct 2016
Posts: 12
Rep Power: 10
kavatar is on a distinguished road
Hello Alexander,

You are absolutely right. I checked it and realized that each partition has cells with cell index ranging from 0,1,2.......Thus, above udf will definitely not work in parallel.

So could you please suggest a way to sail through the problem described below ?.

I mean I have a text file. Each line of this text file contains four entries namely (x, y, z, Fx), here x, y, z are cell centers of the mesh in whole domain and Fx is x-momentum source term. I can get the order of rows in any order according to need (Right now I stored data in the order of global cell index. Global cell index mean the order of "c" in which the cells are being passed to Define_Source udf by the solver in serial run. I stored them by reading value of "c" into an global array from define_source udf).

"I can use the distance formula to find the distance between cell being passed to define_source udf and all the entries in the text file. But it will be huge computational complex since for each value of 'c' I will have to compare distance with each cell in the mesh". So this method doesn't look appropriate.

I hope I am clear about the query.

Thanks for giving your time !!
kavatar is offline   Reply With Quote

Old   January 14, 2021, 08:29
Default
  #4
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27
pakk will become famous soon enough
Add a UDM, and when you load the data, find the cell that corresponds to the coordinate center, and fill the UDM with the appropriate data. If the mesh in your data is the same as your mesh in Fluent, this should work. You don't need to do a search for the closest cell yourself, there is a macro in Fluent that finds the cell for a given coordinate.
pakk is offline   Reply With Quote

Old   January 15, 2021, 01:58
Default
  #5
New Member
 
Krishna
Join Date: Oct 2016
Posts: 12
Rep Power: 10
kavatar is on a distinguished road
Hello,

I have worked out this problem. Each cell has unique global cell index given by C_ID(c,t) while running case in parallel. This global cell index does not change while partitioning. So instead of comparing local cell index "c" (which start from zero for each node during partition), one can compare global cell index and read the data from text file as:

Fx[globalCellIndex]=SourceTerm;

This way I can use global cell index to return data from define_source udf.

Thanks for you valuable suggestion !!
zlwdml3344 likes this.
kavatar is offline   Reply With Quote

Reply

Tags
define_on_demand, define_source, parallelization


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] Tabulated thermophysicalProperties library chriss85 OpenFOAM Community Contributions 62 October 2, 2022 04:50
[swak4Foam] Installation Problem with OF 6 version Aurel OpenFOAM Community Contributions 14 November 18, 2020 17:18
[foam-extend.org] problem when installing foam-extend-1.6 Thomas pan OpenFOAM Installation 7 September 9, 2015 22:53
OpenFOAM on MinGW crosscompiler hosted on Linux allenzhao OpenFOAM Installation 127 January 30, 2009 20:08
DecomposePar links against liblamso0 with OpenMPI jens_klostermann OpenFOAM Bugs 11 June 28, 2007 18:51


All times are GMT -4. The time now is 21:19.