|
[Sponsors] |
January 14, 2014, 14:11 |
UDF with data-output to file
|
#1 |
New Member
-
Join Date: Nov 2013
Posts: 11
Rep Power: 13 |
Hello everyone,
I am writing a UFD to simulate a dynamic mesh in Fluent. The function itself works fine. But I want to include a data output, so I can work with the values (e.g. in Matlab). Besides, it's important for me to have a feedback what the function does because I'm still learning C / UDFs and need to understand the language. Here is my UDF: //================================== #include "udf.h" #include <stdio.h> DEFINE_CG_MOTION(movement,dt,vel,omega,time,dtime) { real t = CURRENT_TIME; real a,w; a=1.6; w=1.5; NV_S(vel, =, 0.0); if (!Data_Valid_P()) return; vel[0]=1; vel[1] = a * sin(w*t); printf("x-Vel: %f, y-Vel: %f",vel[0],vel[1]); // Works till here! // Data output: FILE *str; str = fopen ("Output.txt","w"); if(str==NULL) { printf("Error\n"); } fprintf (str,"x-vel: %f , y-vel: %f \n", vel[0], vel[1]); fclose (str); } //============================ The last part SHOULD create a "Output.txt" file and save all the velocities line by line in it. I hoped that the \n would work but it doesn't. The file appears and the LAST velocity entry is in line one. Here is my question: How can I make this UDF write each entry in a new line? I really hope you guys can help me. |
|
January 16, 2014, 12:17 |
|
#2 |
Senior Member
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27 |
This happens because you used
str = fopen ("Output.txt","w"); This tells Fluent to open the file "Output.txt" for writing, and if the file already exists to remove the content. Replace this by str = fopen ("Output.txt","a"); then you only append information to this file. |
|
January 16, 2014, 20:58 |
|
#3 |
New Member
-
Join Date: Nov 2013
Posts: 11
Rep Power: 13 |
Hello Pakk,
you are the lucky winner of the biggest "Thank you" that i have. Everything works fine now. Deepblack |
|
March 13, 2014, 04:49 |
|
#4 |
New Member
ganhai
Join Date: Feb 2014
Posts: 14
Rep Power: 12 |
Hello Pakk,
I have the same problem with Deepblack,Accroding to you method, i successfully compile my udf.but i don't find output date file. Where should i find it. I really hope you can help me. thk! |
|
March 13, 2014, 06:20 |
|
#5 | |
Senior Member
Rick
Join Date: Oct 2010
Posts: 1,016
Rep Power: 27 |
Quote:
Usually it has to be where your cas dat files are. Daniele |
||
March 13, 2014, 07:10 |
|
#6 |
New Member
ganhai
Join Date: Feb 2014
Posts: 14
Rep Power: 12 |
I can find my cas and date file ,bu don't find the output date file.
This my udf #include "udf.h" #include "math.h" #include <stdio.h> #define pi 3.1415926 DEFINE_CG_MOTION(velocity,dt,vel,omega,time,dtime) { Thread *t; cell_t c; real N=1000.0; real r=0.03765; real alpa=10; real X[ND_ND]; real x,y,z,p; real flow_time=RP_Get_Real("flow-time"); t=DT_THREAD(dt); vel[2]=r*2*pi*(N/60)*tan((alpa*pi)/180)*sin(2*pi*(N/60.0)*time+pi/9); omega[2]=2; begin_c_loop(c,t) { C_CENTROID(X,c,t); x=X[0];x=X[1];x=X[2]; p=C_P(c,t); } end_c_loop(c,t) FILE *str; str = fopen("OutPut.txt","a"); if(str==NULL) { printf("Error\n"); } fprintf (str,"x %f , y %f ,z %f,p %f\n", X[0], X[1],X[2],p); fclose (str); } |
|
March 14, 2014, 05:02 |
|
#7 |
Senior Member
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27 |
If you want to know which folder you are using in Fluent, then in the text input box of Fluent you can type:
Code:
!cd Code:
printf("Error\n"); Code:
Message("Error\n"); A reason why your code does not do what you expect could be the location where you declare Code:
FILE *str; |
|
March 14, 2014, 21:35 |
|
#8 |
New Member
ganhai
Join Date: Feb 2014
Posts: 14
Rep Power: 12 |
Hello,Pakk.
I modify the udf as you have told me.My work floder is G:\Simulation\Workbench\YM\YM_files\dp0\FLU-3\Fluent.I still can't find .txt file. |
|
March 15, 2014, 05:27 |
|
#9 |
Senior Member
Rick
Join Date: Oct 2010
Posts: 1,016
Rep Power: 27 |
Did you try to run fluent as admin and see if the output file is created in the working directory?
|
|
March 15, 2014, 06:58 |
|
#10 |
New Member
ganhai
Join Date: Feb 2014
Posts: 14
Rep Power: 12 |
I start the fluent through Ansys workbench.The working directory don't have the output file.
|
|
March 16, 2014, 05:02 |
|
#11 |
Senior Member
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27 |
If you replace
Code:
if(str==NULL) { printf("Error\n"); } Code:
if(str==NULL) { Message("It failed\n"); } else { Message("It worked\n"); } |
|
March 16, 2014, 21:20 |
|
#12 |
New Member
ganhai
Join Date: Feb 2014
Posts: 14
Rep Power: 12 |
Hi,pakk,I don't see "it failed" or "it worked" in my fluent screen.So what wrong it is?
|
|
March 16, 2014, 21:53 |
|
#13 |
New Member
ganhai
Join Date: Feb 2014
Posts: 14
Rep Power: 12 |
Hi,pakk.I am very willing to share with you a good news.Following with you proposal,my udf can work successfully.I wrote the udf with c & c++.At the first time ,I compiled the udf write with c ,it pointed out that there was some syntax errors.So I write the same code with c++,thus it can compiled successfully in fluent.But couldn't find any output files in my working directory.Today,I found that though the c++ code can compile successfully,I could't find the code file in "G:\Simulation\Workbench\YM\YM_files\dp0\FLU-3\Fluent\libudf\src". So I understood why always can't find output file.At the last ,I modified the c code as you told me, this time,it compiled successfully and worked very well.
Thanks for you patience with my problem!Good luck to you. |
|
Tags |
data extract, data file, output data, udf exporting variable |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
patch error OF v2.2.2 mac | hewei | OpenFOAM Installation | 4 | November 30, 2013 17:55 |
Trouble compiling utilities using source-built OpenFOAM | Artur | OpenFOAM Programming & Development | 14 | October 29, 2013 11:59 |
[swak4Foam] swak4Foam-groovyBC build problem | zxj160 | OpenFOAM Community Contributions | 18 | July 30, 2013 14:14 |
ParaView Compilation | jakaranda | OpenFOAM Installation | 3 | October 27, 2008 12:46 |
DxFoam reader update | hjasak | OpenFOAM Post-Processing | 69 | April 24, 2008 02:24 |