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

UDF with output text file

Register Blogs Community New Posts Updated Threads Search

Like Tree3Likes
  • 2 Post By JH.T
  • 1 Post By AlexanderZ

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   December 13, 2007, 06:17
Default UDF with output text file
  #1
manu
Guest
 
Posts: n/a
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); */ }
  Reply With Quote

Old   December 13, 2007, 07:42
Default Re: UDF with output text file
  #2
al_c
Guest
 
Posts: n/a
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.
  Reply With Quote

Old   March 23, 2011, 13:24
Default
  #3
Member
 
Join Date: Apr 2010
Location: Pisa / Italy
Posts: 62
Rep Power: 16
Atze is on a distinguished road
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
Atze is offline   Reply With Quote

Old   March 24, 2011, 08:55
Default
  #4
New Member
 
Join Date: Mar 2009
Posts: 4
Rep Power: 17
e0125583 is on a distinguished road
@Atze: are you simulating in parallel?
e0125583 is offline   Reply With Quote

Old   March 24, 2011, 10:22
Default
  #5
Member
 
Join Date: Apr 2010
Location: Pisa / Italy
Posts: 62
Rep Power: 16
Atze is on a distinguished road
@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...
Atze is offline   Reply With Quote

Old   May 31, 2012, 07:16
Default do you find the reason ?
  #6
Member
 
Join Date: Mar 2012
Location: USA
Posts: 33
Rep Power: 14
tsi07 is on a distinguished road
Hello,

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

Thanks!!
tsi07 is offline   Reply With Quote

Old   April 24, 2013, 02:46
Default
  #7
New Member
 
Daan de Boer
Join Date: Jun 2012
Posts: 6
Rep Power: 0
daandb is on a distinguished road
same issue (5 times in my case)
daandb is offline   Reply With Quote

Old   October 15, 2015, 04:13
Default
  #8
New Member
 
asgar
Join Date: Oct 2013
Posts: 1
Rep Power: 0
asgar_mec is on a distinguished road
Quote:
Originally Posted by daandb View Post
same issue (5 times in my case)
set UDF PROFILE UPDATE INTERVAL a value more bigger than MAX ITERATION PER TIME STEP
asgar_mec is offline   Reply With Quote

Old   March 30, 2016, 21:09
Default
  #9
Member
 
N B Khan
Join Date: Jan 2014
Posts: 39
Rep Power: 12
bestniaz is on a distinguished road
Quote:
Originally Posted by asgar_mec View Post
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.
bestniaz is offline   Reply With Quote

Old   June 16, 2016, 11:24
Default
  #10
New Member
 
Jo
Join Date: Mar 2016
Location: Belgium
Posts: 2
Rep Power: 0
JH.T is on a distinguished road
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)
Oula and myaghoobi2 like this.
JH.T is offline   Reply With Quote

Old   December 24, 2016, 19:40
Default
  #11
New Member
 
Abdalqader Ahmad
Join Date: Mar 2015
Location: University of Birmingham
Posts: 9
Rep Power: 11
alnossory is on a distinguished road
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
alnossory is offline   Reply With Quote

Old   December 25, 2016, 04:01
Default
  #12
Member
 
Join Date: Apr 2010
Location: Pisa / Italy
Posts: 62
Rep Power: 16
Atze is on a distinguished road
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
Atze is offline   Reply With Quote

Old   December 25, 2016, 04:15
Default
  #13
New Member
 
Abdalqader Ahmad
Join Date: Mar 2015
Location: University of Birmingham
Posts: 9
Rep Power: 11
alnossory is on a distinguished road
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
alnossory is offline   Reply With Quote

Old   December 25, 2016, 04:18
Default
  #14
Member
 
Join Date: Apr 2010
Location: Pisa / Italy
Posts: 62
Rep Power: 16
Atze is on a distinguished road
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
Atze is offline   Reply With Quote

Old   December 25, 2016, 05:07
Default
  #15
New Member
 
Abdalqader Ahmad
Join Date: Mar 2015
Location: University of Birmingham
Posts: 9
Rep Power: 11
alnossory is on a distinguished road
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
alnossory is offline   Reply With Quote

Old   December 11, 2017, 10:20
Default
  #16
New Member
 
mohammad
Join Date: May 2016
Location: Tehran
Posts: 17
Rep Power: 9
myaghoobi2 is on a distinguished road
Well Done!...
myaghoobi2 is offline   Reply With Quote

Old   July 31, 2018, 03:44
Default boundary condition in a .txt file
  #17
New Member
 
belmerabet
Join Date: Jul 2018
Posts: 2
Rep Power: 0
abdou1808 is on a distinguished road
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.
abdou1808 is offline   Reply With Quote

Old   December 12, 2018, 14:19
Default
  #18
Member
 
Oula
Join Date: Apr 2015
Location: United Kingdom
Posts: 81
Rep Power: 10
Oula is on a distinguished road
Quote:
Originally Posted by JH.T View Post
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.
Oula is offline   Reply With Quote

Old   December 12, 2018, 22:46
Default
  #19
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
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
souza.emer likes this.
AlexanderZ 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
pisoFoam compiling error with OF 1.7.1 on MAC OSX Greg Givogue OpenFOAM Programming & Development 3 March 4, 2011 17:18
[OpenFOAM] ParaView 33 canbt open OpenFoam file hariya03 ParaView 7 September 25, 2008 17:33
DxFoam reader update hjasak OpenFOAM Post-Processing 69 April 24, 2008 01:24
error while compiling the USER Sub routine CFD user CFX 3 November 25, 2002 15:16
PHI file structure Eugene Phoenics 9 November 2, 2001 22:00


All times are GMT -4. The time now is 17:00.