CFD Online Discussion Forums

CFD Online Discussion Forums (
-   FLUENT (
-   -   Iteration Number in Fluent 5 (

Alfonso Ferrandez March 6, 2000 12:43

Iteration Number in Fluent 5
Dear all,

is there an easy way to determine what iteration number the solver is at within a timestep? I'm writing a UDF that relies on executing ONLY for the first iteration of each time-step, and although I've played with the value of "UDF Profile Update Interval", Fluent always seems to execute the UDF for EVERY iteration!

If there is a simpler way of solving this "minuscule" problem? If so, I would certainly like to hear from any of you. It's so simple it's driving me mad!

Thanks! AL

Kai Kang March 6, 2000 17:57

Re: Iteration Number in Fluent 5
I have never tried before, just an idea:

For transient simulation you can obtain the simulation time, RP_Get_REAL("flow_time"), you can declare a variable in your UDF as static to save the flow_time, each time the UDF is called, compared the newer time with whatever save in this variable and if the same, then return, otherwise execute the rest part of the UDF and update that variable.

Or write out to a file and save the time there.

Alfonso Ferrandez March 6, 2000 18:23

Re: Iteration Number in Fluent 5
Unfortunately, this UDF is a DEFINE_SOURCE type, which means it's called many times (loops across all cells in internal fluid) and therefore I can't tell whether the UDF has been executed again because of a cell in the domain, or because it's a different iteration.

And, to use the write to file approach, I have the same dilemma... what do I write if I don't know iteration number, or cell being set...

And also, I was trying to avoid read/write files because it's a very time consuming algorithm we're talking about...

Thanks anyway! Any more ideas? :) Cheerio! AL

Greg Perkins March 6, 2000 22:22

Re: Iteration Number in Fluent 5
There's a couple of things you might like to try.

Recently Nelson Carter ( sent me this code to determine the current iteration number:-

int current_iter = (nres == 0) ? (0) : ((int) count2[nres - 1]);

This works fine. For your transient case you'll probably need to check something else with the timestep, flowtime etc.

To check whether you're UDF is called again at the same iteration or at a new iteration use a static variable. Here is some code I'm using to do something every 25 iterations (current_iter as defined above):-

static int disp = FALSE;

if ((current_iter % 25) == 0)


if (disp == FALSE)


/* --- do what ever you like */

disp = TRUE;



else disp = FALSE;

You can change the number 25 to whatever you like...

Hope it helps!

Alfonso Ferrandez March 7, 2000 06:23

Re: Iteration Number in Fluent 5
Thanks Greg!

With your help and a little modification I managed to make it work! And now, for all of those who might need to do this again, here's a snippet of code that will make it much easier for you. As an example a use a SOURCE UDF... this code does nothing else than showing you the basic structure, the actual agorithm is up to you :)

/*********** Code Snippet ****************/

static int control_t = 0; static int first_iter;

DEFINE_SOURCE(xmom_source_dynamic, cell, thread, dS, eqn) {

int time_step, iter, current_iter;

time_step = RP_Get_Integer("time-step");

current_iter = (nres == 0) ? (0) : ((int) count2[nres - 1]);

if (control_t != time_step) first_iter = current_iter;

if (first_iter == current_iter) /* Execute for first iteration of each timestep ONLY*/


/* do things...*/

control_t = time_step; /* Note change in timestep */




cxprintf(stdout,"Internal iteration %d\n", current_iter); /* Or simply do nothing! */


return f; }

/**************** END OF CODE SNIPPET *****************/

I hope this goes towards helping the "UDF Sharing" spirit that is going around the forum lately :)

Cheerio! AL

Jonas Larsson March 7, 2000 07:29

Re: Iteration Number in Fluent 5
Thanks for sharing this! Why don't you also upload it to - I've opened an FTP area there for sharing UDFs etc. If people use it I will design a more user-friendly web-interface to it.

Alfonso Ferrandez March 7, 2000 10:12

Re: Iteration Number in Fluent 5
Hi Jonas!

I've done as you indicated. I put a few more comments, and a "description header" I made up, since I don't think anybody has agreed on a standard yet...

Enjoy! AL

Jonas Larsson March 7, 2000 10:55

Re: Iteration Number in Fluent 5
Many thanks! I've moved it to the public "fluent" directory at where anyone can download it.

Here is a link to it:

I like your header! It is short but still gives the essentials.

All times are GMT -4. The time now is 20:55.