2011
ANSYS FLUENT GUIDE Advanced FLUENT User-Defined Functions You can access and manipulate almost every solver data and fully control the solution process in UDF-s written in C programming Language.
Saeed Sadeghi (
[email protected]) Pardad Petro Danesh (www.petrodanesh.ir) Pardad Petro Danesh (www.petrodanesh.ir) | Confidential 1/12/2011
Introduction
2
Table of Contents 1
Introduction .................................................................................................................................... 4
2
User Access to the FLUENT Solver................................................................................................... 4
3
C Programming ................................................................................................................................ 5
4
Cfd Programming in FLUENT ........................................................................................................... 7
5
UDF Basics ....................................................................................................................................... 7
6
Using UDFs in the Solvers ................................................................................................................ 8
7
Data structure overview.................................................................................................................. 8 7.1
The Domain ............................................................................................................................. 8
7.2
The Threads ............................................................................................................................. 9
7.3
Cell and Face Datatypes .......................................................................................................... 9
7.4
Some Additional info on Faces ................................................................................................ 9
8
Fluent UDF Data Structure Summary ............................................................................................ 10
9
Geometry Macros ......................................................................................................................... 11
10
Cell Field Variable Macros ......................................................................................................... 12
11
Face Field Variable Macros ....................................................................................................... 13
12
Looping and Thread Macros...................................................................................................... 14
13
Macros ....................................................................................................................................... 15
13.1
DEFINE Macros ...................................................................................................................... 15
13.2
Othe UDF Applications .......................................................................................................... 16
14
User Defined Memories ............................................................................................................ 17
15
User Defined Scalars.................................................................................................................. 18
16
Interpreted vs. Compiled UDF’s ................................................................................................ 19
17
UDF Technical Support .............................................................................................................. 19
18
Example: Parabolic Inlet Velocity Profile .................................................................................. 20
18.1
Step 1: Prepare the Source Code .......................................................................................... 20
18.2
Step 3: Interpret or Compile the UDF ................................................................................... 20
18.3
Step 4: Activate the UDF ....................................................................................................... 21
18.4
Steps 5 and 6: Run the Calculations ...................................................................................... 21
18.5
Numerical Solution of the Example ....................................................................................... 21
19
Example: Checking Effect of Porous Zone on Momentum Equation ........................................ 22
19.1
Using Fluent Built-in Porous Zone ......................................................................................... 22
19.2
Using Momentum Source Terms........................................................................................... 23
20
Example: Modeling Fuel Cell ..................................................................................................... 24 Saeed Sadeghi (
[email protected]) Pardad Petro Danesh (www.petrodanesh.ir)
Introduction
3
20.1
Preparation............................................................................................................................ 24
20.2
Mesh ...................................................................................................................................... 24
20.3
General Settings .................................................................................................................... 24
20.4
Models ................................................................................................................................... 25
20.5
Materials ............................................................................................................................... 25
20.6
Boundary Conditions ............................................................................................................. 26
20.6.1
Setting Velocity Inlet ..................................................................................................... 29
20.6.2
Pressure Outlet.............................................................................................................. 29
20.7
Writing UDF ........................................................................................................................... 30
20.7.1 20.8 21
Compiling the UDF......................................................................................................... 32
Checking Oxygen Mass Balance ............................................................................................ 37 Fluent Frequently Asked Questions .......................................................................................... 39
21.1
My UDF won't interpret or compile - what is wrong? .......................................................... 39
21.2
How to Set Environment Variables ....................................................................................... 39
21.2.1
On Windows 32 Bit ........................................................................................................ 39
21.2.2
On Windows 64 Bit ........................................................................................................ 39
Saeed Sadeghi (
[email protected]) Pardad Petro Danesh (www.petrodanesh.ir)
Introduction
4
1 Introduction • What is a User Defined Function? o A UDF is a routine (programmed by the user) written in C which can be dynamically linked with the solver. Standard C functions • e.g., trigonometric, exponential, control blocks, do-loops, file i/o, etc. Pre-Defined Macros • Allows access to field variable, material property, and cell geometry data. • Why build UDF’s? o Standard interface cannot be programmed to anticipate all needs. Customization of boundary conditions, source terms, reaction rates, material properties, etc. Adjust functions (once per iteration) Execute on Demand functions Solution Initialization
2 User Access to the FLUENT Solver
Saeed Sadeghi (
[email protected]) Pardad Petro Danesh (www.petrodanesh.ir)
C Programming
5
3 C Programming •
Basic syntax rules: o Each statement must be terminated with a semicolon ; o Comments can be inserted anywhere between /* and */ o Variables must be explicitly declared (unlike in FORTRAN) o Compound statements must be enclosed by { } o Functions have the following format: return-value-type function-name (parameter-list) { function body }
•
o Macros are defined in the header files, they can be used just like functions Built-in data types: int, float, double, enum, Boolean: int niter, a;
/* declaring ‘niter’ and ‘a’ as integers */
float dx[10];
/* ‘dx’ is a real array with 10 members, the array index always starts from dx[0] */
enum { X, Y, Z }; /* X, Y, Z are enumeration constants 0, 1, 2 */ • •
pointer is a special kind of variable that contains the memory address, not content, of another variable Pointers are declared using the * notation:
int *ip; •
• •
•
/* ip is declared as a pointer to integer */
We can make a pointer point to the address of predefined variable as follows: int a=1; int *ip; ip = &a; /* &a returns the address of variable a */ printf(“content of address pointed to by ip = %d\n”, *ip); Pointers are also used to point to the beginning of an array o Thus pointers are used to address arrays in C Array variables can be defined using the notation name[size], where name is the variable name and size is an integer which defines the number of elements in the array (from 0 to size-1) Operators =
(assignment)
+, -, *, /, % (modulus) , >=,