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

how to check UDFs code errors?

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree2Likes
  • 1 Post By CeesH
  • 1 Post By gjm89

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   December 9, 2014, 09:17
Default how to check UDFs code errors?
  #1
New Member
 
Join Date: Jun 2014
Posts: 3
Rep Power: 11
gjm89 is on a distinguished road
Hi, I would like to know how to check my UDFs coding in order to impute it in Fluent. For example, I have just been trying to input a parabolic velocity profile as an inlet of a straight pipe (emulating a fully developed velocity profile). I saw a couple of examples online, so I used them as template, I wrote the ".c" file and compile it and load it in Fluent, it worked.

However, if I want to check any errors within the code I tried to compile it on Visual Studio, before even opening Fluent, so that I could know whether I had some errors on my coding or not. However, it seems to me that Fluent uses a C programming based language, but it is not exactly C language, because the way I need to define functions and variables is not the same. This is a basic example of UDF:


#include "udf.h"
#define Q 1.5625e-5 //unit m3/sec
#define Diameter 4.5e-3 //unit m
DEFINE_PROFILE(axialVelocity,t,i)
{
real x[ND_ND];
real r,Area;
face_t f;
Area=(M_PI/4.0)*pow(Diameter,2);
begin_f_loop(f,t)
{
F_CENTROID(x,f,t);
r=sqrt(pow(x[0],2)+pow(x[1],2));
F_PROFILE(f,t,i)=(2.0*Q/Area)*(1-pow(2.0*r/Diameter,2));
}
end_f_loop(f,t)
}


When I try to compile this on Visual Studio, it doesn't even know what "real" is. I have been going through a C language manual, and there is no variable that could be defined as "real", the only types of variables are:

int
char
float
double


So I wonder how should I approach this issue. Also, if I just write the code on a ".txt" file and save it as a ".c" it works, and Fluent is able to compile it if the code is correct. However, if there is a mistake in the code, it gives and error, but it wouldn't even say in which line of code the error is... I find this pretty disappointed. I know in CFX, you can easily create UDFs within even needing to do a ".c" file, and if you happen to create a ".c" file and compile it, it would give you and error and where the error is.

I would like to know which editor and compiler people usually use to create and check their UDFs before compiling and loading them into Fluent. And I would like to know which type of C language these UDFs should be written in, since I would need to learn about it.

Thanks a lot in advance for you time and help.

Sincerely,

Guillermo Jimeno
gjm89 is offline   Reply With Quote

Old   December 9, 2014, 09:49
Default
  #2
Senior Member
 
Cees Haringa
Join Date: May 2013
Location: Delft
Posts: 607
Rep Power: 0
CeesH is on a distinguished road
I use visual studio and code::blocks. Anyway, types like real are indeed not part of regular C. You can define your own types for debugging, of course, but debugging the code as implemented in FLUENT is, as far as I know, not possible in any editor. The only reason I use VS or CB is because it does point out the obvious syntax errors (forgotten parenthesis and so on), and because I can check whether a function works if I write it as a standalone script, before modifying it to something hookable to FLUENT.

Regarding errors, FLUENT does give the line the error occurs in, and typically the reason (if it is a syntax error at least). For other errors, like occurance of NANs, I can only recommend to write a stand-alone version of the script, and test if the calculated output is correct.
CeesH is offline   Reply With Quote

Old   December 9, 2014, 21:42
Default Thanks and further inquiries
  #3
New Member
 
Join Date: Jun 2014
Posts: 3
Rep Power: 11
gjm89 is on a distinguished road
Hi CeesH! Thanks a lot for your quick and useful reply. I'll follow your advise and limit VS purpose to check simple code errors.

Apologies for my lack of knowledge, but... what exactly is a stand-alone scrip? And how could I create it from my UDF code? Can that be compile and tested on VS?

On a different note, in order to teach myself the specific C language that Fluent uses, I would like to know how I should refer to and define variables in order for Fluent to understand it. Like how exactly Fluent calls viscosity, density, radial coordinates, etc, etc. Is there anywhere where I could find this information? Like a table of variables and how they are called by Fluent?
I know there is such kind of document available for CFX:

ANSYS Help Viewer > CFX > 16.Reference Guide > 16.2.Variables in ANSYS CFX

There you can find all kinds of variables. Is there an equivalent document for Fluent? I have been trying to look for it, but I couldn't find it.

Thanks a lot once again for you time and help.

Sincerely,

Guillermo
gjm89 is offline   Reply With Quote

Old   December 10, 2014, 04:44
Default
  #4
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 26
pakk will become famous soon enough
"real" (and "DEFINE_PROFILE", "face_t", "begin_f_loop", "F_CENTROID", "F_PROFILE" and "end_f_loop") are defined in "udf.h", which you include in the first line of your code.

If you compile this in fluent, fluent knows where the file "udf.h" is located.

It should be possible to compile the udf in visual studio (I never tried it), but the problem is that visual studio does not know where the file "udf.h" is located. Once you let visual studio know where to find that file (and I have no idea how that works), it should be possible to compile it.


There is an entire section in the Fluent help dedicated to UDF's, the "UDF manual". It also includes tables of variables with their names and short descriptions.
pakk is offline   Reply With Quote

Old   December 10, 2014, 07:23
Default
  #5
Senior Member
 
Cees Haringa
Join Date: May 2013
Location: Delft
Posts: 607
Rep Power: 0
CeesH is on a distinguished road
with stand-alone script I mean something that would run independent of FLUENT, just a separate executable.

Say I'd write a UDF for a first order reaction rate, that would look something like:

Code:
#include "udf.h"
#define K1 0.001

DEFINE_VR_RATE(...all those things...)
{

int i = 0;

real C_MOLE = (C_YI(c,t,i)/mole_weight[i]) * C_R(c,t);

*rate = K1*C_MOLE;
*rr_t = *rate;}
As a standalone I might write it

Code:
#include "stdio.h"
#define K1 0.001
int main()
{
int i = 0;

double C_MOLE = (C_YI(c,t,i)/180) * 1000;

rate = K1*C_MOLE;
return rate
}
This would be just to test if the correct value is returned. For this particular function it may not be that useful, but for longer functions I do find it important - if errors occur, at least I know it's not because the calculation is wrong (it could still be that FLUENT macros return different units than I expected of course..). Pointing to udf.h in visual studio would make this a bit less alterations. But I never tried that.

Regarding learning C, you can use the FLUENT manual for some basic understanding. But of course it doesn't hurt learning the actual language by following a C course or buying a good instruction book. In the end, there is no difference between C and `FLUENT C'
souza.emer likes this.
CeesH is offline   Reply With Quote

Old   December 10, 2014, 18:57
Default
  #6
New Member
 
Join Date: Jun 2014
Posts: 3
Rep Power: 11
gjm89 is on a distinguished road
Thank you guys a lot for the replies, it really helped.
Now everything is becoming clearer to me. May I ask one more question, which might or not be directly related to UDFs. Im just aiming to input a fully developed velocity profile in the inlet of my geometry. For a 2D geometry I created the following UDFs, which worked perfectly well, since the 2D geometry was just half of the symmetry centre section (the system is axisymmetric), the origin of the geometry was in the symmetry axis of the inlet surface.

Code:
#include "udf.h"

#define v0 0.0075  //unit m/sec
#define radius 0.01  //unit m

DEFINE_PROFILE(inlet_x_velocity, thread, position) 
{
  real x[ND_ND];		/* this will hold the position vector */
  real y;
  face_t f;

  begin_f_loop(f, thread)
    {
      F_CENTROID(x,f,thread);
      y = x[1];
      F_PROFILE(f, thread, position) = 2*v0*(1-(pow(y/radius,2)));   
    }
  end_f_loop(f, thread)
}
Then I tried to create the same velocity profile UDF for a 3D geometry. It didnt work that well, since the origin of coordinates is not in the inlet surface, and is not at the same level either: the centre of the inlet surface is at [x y z] = [-100 20 0]. Here is the UDF I tried to use

Code:
#include "udf.h"

#define v0 0.0075  //unit m/sec
#define radius 0.01  //unit m

DEFINE_PROFILE(inlet_x_velocity, thread, position) 
{
  real x[ND_ND];		/* this will hold the position vector */
  real y;
  real z;
  real r;
  face_t f;

  begin_f_loop(f, thread)
    {
      F_CENTROID(x,f,thread);
      y = x[1];
      z = x[2];
      r = sqrt(x[1]*x[1] + x[2]*x[2]);

      F_PROFILE(f, thread, position) = 2*v0*(1-(pow(r/radius,2)));   
    }
  end_f_loop(f, thread)
}
I was wondering whether there is a MACRO that I could use telling Fluent to take the centre of the selected surface (inlet in this case) as the origin of coordinates for the calculation of this profile, or if there is a way in Fluent to define another origin of coordinates that I could point my UDF to (I know this is possible in CFX).

On a different note, am I right saying that when defining the x array in a UDF, for 2D:

real x[ND_ND]

xx = x[0];
yy = x[1];


And, for 3D:

real x[ND_ND]

xx = x[0];
yy = x[1];
zz = x[2];

?

Where xx, yy and zz are the actually x, y and z coordinates respectively.

Thank you so much for all the help.

Regards,

Guillermo
Mohammad_Jamali likes this.
gjm89 is offline   Reply With Quote

Reply

Tags
compile a udf, compile error, fluent - udf, udf and programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Building OpenFOAM1.7.0 from source ata OpenFOAM Installation 46 March 6, 2022 14:21
OF 1.6 | Ubuntu 9.10 (64bit) | GLIBCXX_3.4.11 not found piprus OpenFOAM Installation 22 February 25, 2010 14:43
Please check the following code Nagi FLUENT 0 February 12, 2009 10:18
Fluent UDF's / dynamic meshing errors Peter FLUENT 2 November 6, 2005 04:36
Design Integration with CFD? John C. Chien Main CFD Forum 19 May 17, 2001 16:56


All times are GMT -4. The time now is 10:51.