CFD Online Discussion Forums

CFD Online Discussion Forums (
-   Fluent UDF and Scheme Programming (
-   -   UDF with output text file (

manu December 13, 2007 07:17

UDF with output text file
I'm writind a UDF Macro (interpreted) where I define the new boundary conditions for each time-step of the simulation. I would like also to write in a txt file the variables I used to compute this boundary condition. So I use the commands: FILE *fp, fopen, fprintf, fclose. The problem is that I don't know how to close the file only at the end of the simulation, because otherwise, my txt file will be overwritten after each timestep if I include the "fclose" in the brackets of my UDF Macro. So is there a way to say to my Macro that the iteration is over, and so with a if-condition I could close my file rightly? I mean something like

if ("!!!iteration finished!!!") {fclose(fp) }

?????????????????????????????????????????????????? ???????????????????????????????????????? Here is a simplified version of my code:

#include "udf.h"

FILE *fp;

#define p_atm 1e5

DEFINE_PROFILE(pressure_outlet,t,i) { face_t f; real p = 1e5; real A[ND_ND]; real B[ND_ND]; int n = RP_Get_Integer("time-step");

A[n]= n+2; B[n]= 2*n; p= (3*A[n]+B[n])/(n+1)*p_atm;

begin_f_loop(f,t) { F_PROFILE(f,t,i) = p; /*new boundary condition */ } end_f_loop(f,t)

/*write column titles*/ if (n==0) { fp = fopen("output.txt","w"); fprintf(fp,"A \t B \t p\n"); }

fprintf(fp,"%f \t %f \t %f\n",A[n],B[n],p); /* fclose(fp); */ }

al_c December 13, 2007 08:42

Re: UDF with output text file
You can use "a" option instead of "w" option in fopen() function. For example fopen("output.txt","a"). In this case all new data will be append to the existing data.

Atze March 23, 2011 14:24


i've used fprintf to write .txt file .... but the output is written twice.... why?

0.000900 1.148102e-006
0.000900 1.148102e-006
0.001000 1.513927e-006
0.001000 1.513927e-006
0.001100 1.872173e-006
0.001100 1.872173e-006
0.001200 2.222938e-006
0.001200 2.222938e-006

e0125583 March 24, 2011 09:55

@Atze: are you simulating in parallel?

Atze March 24, 2011 11:22


no, i'm not.... my code is simply

fprintf(data,"%f %e\n",time,domega);

as reported on udf-manual..... it's strange. Now i'm importing data.txt in excel and removing odd (or pair) lines...

tsi07 May 31, 2012 07:16

do you find the reason ?

I have the same problem, but for me the value is repeted 3 times. :confused:
Have you found the solution of that strange thing ?


daandb April 24, 2013 02:46

same issue (5 times in my case)

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