CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   Fluent UDF and Scheme Programming (https://www.cfd-online.com/Forums/fluent-udf/)
-   -   UDF with data-output to file (https://www.cfd-online.com/Forums/fluent-udf/128522-udf-data-output-file.html)

Deepblack January 14, 2014 13:11

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.

pakk January 16, 2014 11:17

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.

Deepblack January 16, 2014 19:58

Hello Pakk,

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

Deepblack

france March 13, 2014 03:49

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!

ghost82 March 13, 2014 05:20

Quote:

Originally Posted by france (Post 479696)
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.

Daniele

france March 13, 2014 06:10

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

pakk March 14, 2014 04:02

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
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
Code:

printf("Error\n");
you should use
Code:

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
Code:

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...

france March 14, 2014 20:35

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.

ghost82 March 15, 2014 04:27

Did you try to run fluent as admin and see if the output file is created in the working directory?

france March 15, 2014 05:58

I start the fluent through Ansys workbench.The working directory don't have the output file.

pakk March 16, 2014 04:02

If you replace
Code:

if(str==NULL)
{
printf("Error\n");
}

by
Code:

if(str==NULL)
{
Message("It failed\n");
} else {
Message("It worked\n");
}

Do you then see "it failed" or "it worked" in your Fluent screen?

france March 16, 2014 20:20

Hi,pakk,I don't see "it failed" or "it worked" in my fluent screen.So what wrong it is?

france March 16, 2014 20:53

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.