UDF with data-output to file

Like Tree

2Likes

UDF with data-output to file New Member Join Date: Nov 2013 Rep Power: 12

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.

#include "udf.h"
#include

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");

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.

soheil_r7 likes this. Senior Member Join Date: Nov 2013 Posts: 1,965 Rep Power: 26

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.

anan12345 likes this. New Member Join Date: Nov 2013 Rep Power: 12

you are the lucky winner of the biggest "Thank you" that i have.
Everything works fine now.

New Member ganhai Join Date: Feb 2014 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!

Senior Member Join Date: Oct 2010 Posts: 1,016 Rep Power: 26 Originally Posted by france

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!

I think it should be in the working directory, which you choose when you start fluent.
Usually it has to be where your cas dat files are.

New Member ganhai Join Date: Feb 2014 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
#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);
>

Senior Member Join Date: Nov 2013 Posts: 1,965 Rep Power: 26

If you want to know which folder you are using in Fluent, then in the text input box of Fluent you can type:

Fluent might have a problem with creating the file. I see that in your code you tried to show an error message in that case, but you did it slightly wrong: instead of

printf("Error\n");
you should use
Message("Error\n");

Then this warning should be shown in Fluent if it is unable to open the file.

A reason why your code does not do what you expect could be the location where you declare

FILE *str;

These declarations should happen at the top of the function (so before "t=DT_THREAD(dt);" in your case). I would expect the compiler to complain about it.

New Member ganhai Join Date: Feb 2014 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.

Senior Member Join Date: Oct 2010 Posts: 1,016 Rep Power: 26 Did you try to run fluent as admin and see if the output file is created in the working directory? New Member ganhai Join Date: Feb 2014 Rep Power: 12 I start the fluent through Ansys workbench.The working directory don't have the output file. Senior Member Join Date: Nov 2013 Posts: 1,965 Rep Power: 26 If you replace
if(str==NULL) if(str==NULL) < Message("It failed\n"); >else Do you then see "it failed" or "it worked" in your Fluent screen? New Member ganhai Join Date: Feb 2014 Rep Power: 12 Hi,pakk,I don't see "it failed" or "it worked" in my fluent screen.So what wrong it is? New Member ganhai Join Date: Feb 2014 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.

« Previous Thread | Next Thread »
Display Modes
Linear Mode
Switch to Hybrid Mode
Switch to Threaded Mode
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
patch error OF v2.2.2 mac hewei OpenFOAM Installation 4 November 30, 2013 16:55
Trouble compiling utilities using source-built OpenFOAM Artur OpenFOAM Programming & Development 14 October 29, 2013 10:59
[swak4Foam] swak4Foam-groovyBC build problem zxj160 OpenFOAM Community Contributions 18 July 30, 2013 13:14
ParaView Compilation jakaranda OpenFOAM Installation 3 October 27, 2008 11:46
DxFoam reader update hjasak OpenFOAM Post-Processing 69 April 24, 2008 01:24
All times are GMT -4. The time now is 22:05 .
LinkBack
LinkBack URL
About LinkBacks
Bookmark & Share
Digg this Thread!
Add Thread to del.icio.us
Bookmark in Technorati
Tweet this threadTweet this thread