UDF with data-output to file
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. |
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. |
Hello Pakk,
you are the lucky winner of the biggest "Thank you" that i have. Everything works fine now. Deepblack |
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! |
Quote:
Usually it has to be where your cas dat files are. Daniele |
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); } |
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; |
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. |
Did you try to run fluent as admin and see if the output file is created in the working directory?
|
I start the fluent through Ansys workbench.The working directory don't have the output file.
|
If you replace
Code:
if(str==NULL) Code:
if(str==NULL) |
Hi,pakk,I don't see "it failed" or "it worked" in my fluent screen.So what wrong it is?
|
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. |
All times are GMT -4. The time now is 08:38. |