CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   Fluent UDF and Scheme Programming (https://www.cfd-online.com/Forums/fluent-udf/)
-   -   UDF with output text file (https://www.cfd-online.com/Forums/fluent-udf/46820-udf-output-text-file.html)

manu December 13, 2007 06: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 07: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 13:24

hi,

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

example:
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 08:55

@Atze: are you simulating in parallel?

Atze March 24, 2011 10:22

@e0125583

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

data=fopen("data.txt","a");
fprintf(data,"%f %e\n",time,domega);
fclose(data);

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 ?
 
Hello,

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

Thanks!!

daandb April 24, 2013 02:46

same issue (5 times in my case)

asgar_mec October 15, 2015 04:13

Quote:

Originally Posted by daandb (Post 422736)
same issue (5 times in my case)

set UDF PROFILE UPDATE INTERVAL a value more bigger than MAX ITERATION PER TIME STEP

bestniaz March 30, 2016 21:09

Quote:

Originally Posted by asgar_mec (Post 568333)
set UDF PROFILE UPDATE INTERVAL a value more bigger than MAX ITERATION PER TIME STEP

I think its due to parallel processing...
Same happened with me...12times repeatition
But When I run the same UDF on serial processing then result was Ok.

JH.T June 16, 2016 11:24

The "problem" of using fprintf in parallel calculation is that every node will execute the fprintf command, resulting in a number of identical lines in your txt file. For example, if you start a calculation with 12 processes and your code includes:

fp = fopen("message.txt","a");
fprintf(fp,"TEMPERATURE is %f K\n",T_cur);
fclose(fp);

the message.txt file will contain 12 times 'TEMPERATURE is ... K' and this each time fprintf is called...

A workaround could be to parallelize the code:

int myid;
int node_zero = 0;
#define I_AM_NODE_ZERO_P (myid == node_zero)

if I_AM_NODE_ZERO_P
{
fp = fopen("message.txt","a");
fprintf(fp,"TEMPERATURE is %f K\n",T_cur);
fclose(fp);
}

In this way, only node 0 will write a message to the txt file. You can find more info in the Fluent UDF manual (sections 7.5.3 and 7.7)

alnossory December 24, 2016 19:40

He
I have calculated a heat flux on a pipe surface using Optics software and saved it in a text file. I need to write a UDF that allows me to call the heat flux distribution from the text file and apply it on the pipe surface in Fluent.
Is there any one can help me to this?
Regards

Atze December 25, 2016 04:01

Hi,
I work with optical tools too. Usually I define a .csv file with 4 columns [x,y,z,rad.intensity] and import it in fluent (or cfx) as a boundary profile. xyz don't have to perfectly match the nodes of your mesh. To see how to correctly write it I suggest you to export a boundary profile from cfdpost and check it.

Happy holydays :)

alnossory December 25, 2016 04:15

Hi Atze
have you used a UDF to import it to the CFX or without using UDF.
if without how?
you too have good holiday

Atze December 25, 2016 04:18

Hi,
Without udf. You have to import profile (in the menu it is something like "expand profile"). Select the csv and then you have to apply it to your boundary as a source or fixed temp or other. Cfx create a function with your csv field automatically

alnossory December 25, 2016 05:07

Many thanks, your reply was really helpful. I got the idea, I thought I have to write UDF.
Many thanks again and have good holiday.

Regards

myaghoobi2 December 11, 2017 10:20

Well Done!...

abdou1808 July 31, 2018 03:44

boundary condition in a .txt file
 
Hello
I have a boundary condition that is variable in time, and I would like to read a text file where I have my data.
I tried an equation and it worked, but with a text file I can not.
Can you help me please.

Oula December 12, 2018 14:19

Quote:

Originally Posted by JH.T (Post 605223)
The "problem" of using fprintf in parallel calculation is that every node will execute the fprintf command, resulting in a number of identical lines in your txt file. For example, if you start a calculation with 12 processes and your code includes:

fp = fopen("message.txt","a");
fprintf(fp,"TEMPERATURE is %f K\n",T_cur);
fclose(fp);

the message.txt file will contain 12 times 'TEMPERATURE is ... K' and this each time fprintf is called...

A workaround could be to parallelize the code:

int myid;
int node_zero = 0;
#define I_AM_NODE_ZERO_P (myid == node_zero)

if I_AM_NODE_ZERO_P
{
fp = fopen("message.txt","a");
fprintf(fp,"TEMPERATURE is %f K\n",T_cur);
fclose(fp);
}

In this way, only node 0 will write a message to the txt file. You can find more info in the Fluent UDF manual (sections 7.5.3 and 7.7)

Dear JH.T, Thank you so much for your valuable comment it was really helpful. I found a code and would like to ask you few question about some parts of it.

FILE* fptemp = NULL;

DEFINE_EXECUTE_AT_END(write_temp_to_file)

{

#if !RP_HOST what does this mean?

Also
what are the below commands used for?
int cnt;

char header[16];

Another thing
fprintf(fptemp, "%16s", "time"); what does "%16s" mean?

fprintf(fptemp, "%16.5e", time); and what does "%16.5e" mean?

Finally,
fprintf(fptemp, "\n"); what does "/n" refer to?

sprintf(header, "face%03d", cnt); what is "face%03d"?

I need to understand those parts in order to build my own code. Any help is greatly appreciated.

AlexanderZ December 12, 2018 22:46

macro #if !RP_HOST is using to made code works in parallel.
int and char are variable types
int -> integer (number 1,2,3 ...), char is a variable type for words and symbols
%16s means write in console up to 16 first letters of the variable with type string
%16.5f means write in console up to 16 first numbers before dot and 5 after of the variable with type float
\n is used inside messages to go to the next line
%03d - first 3 numbers of the variable with type int

BUT Oula, I've started to develop your code about half a year ago, but still didn't look into manuals, shame on you
USE Ansys Fluent Customization manual, where you can find informatino about ALL these things you've asked

UDF is based on C language, so you may find everything easily searching command in GOOGLE using scheme command + C language

best regards


All times are GMT -4. The time now is 23:52.