CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > ANSYS > FLUENT > Fluent UDF and Scheme Programming

UDF with data-output to file

Register Blogs Community New Posts Updated Threads Search

Like Tree2Likes
  • 1 Post By Deepblack
  • 1 Post By pakk

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   January 14, 2014, 14:11
Question UDF with data-output to file
  #1
New Member
 
-
Join Date: Nov 2013
Posts: 11
Rep Power: 13
Deepblack is on a distinguished road
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.
soheil_r7 likes this.
Deepblack is offline   Reply With Quote

Old   January 16, 2014, 12:17
Default
  #2
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27
pakk will become famous soon enough
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.
pakk is offline   Reply With Quote

Old   January 16, 2014, 20:58
Default
  #3
New Member
 
-
Join Date: Nov 2013
Posts: 11
Rep Power: 13
Deepblack is on a distinguished road
Hello Pakk,

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

Deepblack
Deepblack is offline   Reply With Quote

Old   March 13, 2014, 04:49
Default
  #4
New Member
 
ganhai
Join Date: Feb 2014
Posts: 14
Rep Power: 12
france is on a distinguished road
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!
france is offline   Reply With Quote

Old   March 13, 2014, 06:20
Default
  #5
Senior Member
 
ghost82's Avatar
 
Rick
Join Date: Oct 2010
Posts: 1,016
Rep Power: 27
ghost82 will become famous soon enough
Quote:
Originally Posted by france View Post
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
ghost82 is offline   Reply With Quote

Old   March 13, 2014, 07:10
Default
  #6
New Member
 
ganhai
Join Date: Feb 2014
Posts: 14
Rep Power: 12
france is on a distinguished road
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);
}
france is offline   Reply With Quote

Old   March 14, 2014, 05:02
Default
  #7
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27
pakk will become famous soon enough
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...
pakk is offline   Reply With Quote

Old   March 14, 2014, 21:35
Default
  #8
New Member
 
ganhai
Join Date: Feb 2014
Posts: 14
Rep Power: 12
france is on a distinguished road
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.
france is offline   Reply With Quote

Old   March 15, 2014, 05:27
Default
  #9
Senior Member
 
ghost82's Avatar
 
Rick
Join Date: Oct 2010
Posts: 1,016
Rep Power: 27
ghost82 will become famous soon enough
Did you try to run fluent as admin and see if the output file is created in the working directory?
ghost82 is offline   Reply With Quote

Old   March 15, 2014, 06:58
Default
  #10
New Member
 
ganhai
Join Date: Feb 2014
Posts: 14
Rep Power: 12
france is on a distinguished road
I start the fluent through Ansys workbench.The working directory don't have the output file.
france is offline   Reply With Quote

Old   March 16, 2014, 05:02
Default
  #11
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27
pakk will become famous soon enough
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?
pakk is offline   Reply With Quote

Old   March 16, 2014, 21:20
Default
  #12
New Member
 
ganhai
Join Date: Feb 2014
Posts: 14
Rep Power: 12
france is on a distinguished road
Hi,pakk,I don't see "it failed" or "it worked" in my fluent screen.So what wrong it is?
france is offline   Reply With Quote

Old   March 16, 2014, 21:53
Default
  #13
New Member
 
ganhai
Join Date: Feb 2014
Posts: 14
Rep Power: 12
france is on a distinguished road
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.
france is offline   Reply With Quote

Reply

Tags
data extract, data file, output data, udf exporting variable


Posting Rules
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 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


All times are GMT -4. The time now is 15:43.