|
[Sponsors] |
December 13, 2007, 07:17 |
UDF with output text file
|
#1 |
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); */ } |
|
December 13, 2007, 08:42 |
Re: UDF with output text file
|
#2 |
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.
|
|
March 23, 2011, 14:24 |
|
#3 |
Member
Join Date: Apr 2010
Location: Pisa / Italy
Posts: 62
Rep Power: 16 |
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 |
|
March 24, 2011, 09:55 |
|
#4 |
New Member
Join Date: Mar 2009
Posts: 4
Rep Power: 17 |
@Atze: are you simulating in parallel?
|
|
March 24, 2011, 11:22 |
|
#5 |
Member
Join Date: Apr 2010
Location: Pisa / Italy
Posts: 62
Rep Power: 16 |
@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... |
|
May 31, 2012, 08:16 |
do you find the reason ?
|
#6 |
Member
Join Date: Mar 2012
Location: USA
Posts: 33
Rep Power: 14 |
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!! |
|
April 24, 2013, 03:46 |
|
#7 |
New Member
Daan de Boer
Join Date: Jun 2012
Posts: 6
Rep Power: 0 |
same issue (5 times in my case)
|
|
October 15, 2015, 05:13 |
|
#8 |
New Member
asgar
Join Date: Oct 2013
Posts: 1
Rep Power: 0 |
||
March 30, 2016, 22:09 |
|
#9 |
Member
N B Khan
Join Date: Jan 2014
Posts: 39
Rep Power: 12 |
||
June 16, 2016, 12:24 |
|
#10 |
New Member
Jo
Join Date: Mar 2016
Location: Belgium
Posts: 2
Rep Power: 0 |
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) |
|
December 24, 2016, 20:40 |
|
#11 |
New Member
Abdalqader Ahmad
Join Date: Mar 2015
Location: University of Birmingham
Posts: 9
Rep Power: 11 |
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 |
|
December 25, 2016, 05:01 |
|
#12 |
Member
Join Date: Apr 2010
Location: Pisa / Italy
Posts: 62
Rep Power: 16 |
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 |
|
December 25, 2016, 05:15 |
|
#13 |
New Member
Abdalqader Ahmad
Join Date: Mar 2015
Location: University of Birmingham
Posts: 9
Rep Power: 11 |
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 |
|
December 25, 2016, 05:18 |
|
#14 |
Member
Join Date: Apr 2010
Location: Pisa / Italy
Posts: 62
Rep Power: 16 |
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 |
|
December 25, 2016, 06:07 |
|
#15 |
New Member
Abdalqader Ahmad
Join Date: Mar 2015
Location: University of Birmingham
Posts: 9
Rep Power: 11 |
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 |
|
December 11, 2017, 11:20 |
|
#16 |
New Member
mohammad
Join Date: May 2016
Location: Tehran
Posts: 17
Rep Power: 10 |
Well Done!...
|
|
July 31, 2018, 04:44 |
boundary condition in a .txt file
|
#17 |
New Member
belmerabet
Join Date: Jul 2018
Posts: 2
Rep Power: 0 |
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. |
|
December 12, 2018, 15:19 |
|
#18 | |
Member
Oula
Join Date: Apr 2015
Location: United Kingdom
Posts: 81
Rep Power: 11 |
Quote:
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. |
||
December 12, 2018, 23:46 |
|
#19 |
Senior Member
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34 |
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 |
|
|
|
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 18:18 |
[OpenFOAM] ParaView 33 canbt open OpenFoam file | hariya03 | ParaView | 7 | September 25, 2008 18:33 |
DxFoam reader update | hjasak | OpenFOAM Post-Processing | 69 | April 24, 2008 02:24 |
error while compiling the USER Sub routine | CFD user | CFX | 3 | November 25, 2002 16:16 |
PHI file structure | Eugene | Phoenics | 9 | November 2, 2001 23:00 |