Professional Documents
Culture Documents
Introduction
15.0 Release
June 4, 2014
Release 15.0
June 4, 2014
Release 15.0
June 4, 2014
Release 15.0
Adiabatic wall
inlet
outlet
Heated wall T = T(x)
T(x) = +
June 4, 2014
Release 15.0
June 4, 2014
Release 15.0
Basics of UDFs
15.0 Release
June 4, 2014
Release 15.0
Programming practices
Simplicity, Clarity and Generality
Naming conventions
Macros and comments
Algorithms and data structures
Design principles
Testing
7
June 4, 2014
Release 15.0
Initialize
Begin Loop
Userdefined
ADJUST
Solver?
SOLVERS
Solve U-Momentum
Solve V-Momentum
User Defined
INITIALIZE
Source terms
Repeat
Solve W-Momentum
PBCS
DBCS
Source terms
Solve Mass
&
Momentum
Solve Mass,
Momentum,
Energy,
Species
Source terms
Check Convergence
Solve Energy
Update Properties
User-Defined Properties
Solve Species
Source
terms
User-Defined BCs
Solve Other Transport Equations as required
8
June 4, 2014
Release 15.0
Data Structures
Each type of UDF is passed different values and data structures
depending on its function.
Most UDFs will need solver data. This is stored in a hierarchy:
Domains are collections of threads that make the whole mesh
Threads (or zones) are collections of cells or faces
Cells and faces are made of Nodes
June 4, 2014
Release 15.0
Geometrical Entities
The parts of a mesh are:
June 4, 2014
Release 15.0
Data Structures
The terms Thread and Zone are interchangeable. The boundary
and cell zones defined in the User Interface are stored internally
as threads.
Zones have an associated integer ID seen in the settings
panel
In a UDF, the corresponding thread can be identified using
this ID
Threads are an internal data type which stores information
about the mesh, models, properties, BCs all in one place
A cell has information about its bounding faces and a face has
access to its neighbouring cells.
11
June 4, 2014
Release 15.0
Introduction to the
C Programming Language
15.0 Release
June 4, 2014
Release 15.0
June 4, 2014
Release 15.0
June 4, 2014
Release 15.0
C Programming Pointer
A pointer is a special kind of variable that contains an address in memory, not
content, of another variable
Pointers are declared using the * notation
int *ip; /* declares a pointer named ip that points to an
integer variable*/
Message Macro:
Defined in udf.h
Similar to printf in C
Recommended to use
Output:
15
June 4, 2014
Release 15.0
BUT: An array name cannot be set to a new value. It is fixed to the value
set when it is defined as an array.
ar = ip;
16
Release 15.0
C Programming Operators
Arithmetic Operators
=
+,
%
++
--
(assignment)
-, *, /,
(modulo reduction)
(increment) /* i++ is post-increment (i=i+1)
(decrement) /* j- is post-decrement (j=j-1)
*/
*/
E.g.: x = 1;
Logical Operators
<, >, >=, <= , ==, !=
Assignments
+=, *=, -=, /=
17
June 4, 2014
E.g.: x += y; /* is equivalent
to x = x + y */
Release 15.0
for loop:
if ( logical-expression )
{ statements }
else
{ statements }
if ( x < 0 )
y = x/50;
else
{
x = -x;
y = x/25;
}
2013 ANSYS, Inc.
while loop:
while ( logical-expression )
{statements}
E.g.:
18
June 4, 2014
E.g.:
for ( k=0; k < 10; k++)
{statements}
while ( x < 5 )
{ executed while the condition is true }
Release 15.0
E.g.:
real y = 2;
real x1 = (y < 0 ? 5 : 10);
real x2 = (y >= 0 ? 5 : 10);
Result: x1 = 10 and x2 = 5
19
June 4, 2014
Release 15.0
Element n;
};
char model_name[25];
int
number_of_wheels;
real top_speed;
};
June 4, 2014
Release 15.0
Simplified:
typedef struct
{
char model_name[25];
int
number_of_wheels;
real top_speed;
} car_model;
June 4, 2014
int id;
} Thread;
E.g.:
Thread *ct;
Note ->
22
operator: t->id
June 4, 2014
Release 15.0
Introduction to UDF
Implementation Using C
15.0 Release
June 4, 2014
Release 15.0
June 4, 2014
Release 15.0
face_t
cell_t
Thread
Domain
stores data associated with a collection of face and cell threads in a mesh
Each individual cell can be accessed by using a cell index of type cell_t and the cell thread
(the zone which contains the cell)
Each individual face (boundary or internal) can be accessed by using a face index of type
face_t and the face thread (the zone which contains the face)
25
June 4, 2014
Release 15.0
Some mesh or solver data types have a capital letter. These are actually data
structures in the C language and are usually passed between functions as pointers to
these structures.
Examples of how these data types are defined are shown below:
Type
Domain
Thread
cell_t
face_t
Node
26
Example
*d;
*t;
c;
f;
*node;
June 4, 2014
Declaration
d is a pointer to a Domain structure
t is a pointer to a Thread structure
c is cell index, a simple integer
f is a face index, a simple integer
node is pointer to a Node structure
Release 15.0
June 4, 2014
Release 15.0
June 4, 2014
Release 15.0
thread_loop_c(t,d){}
thread_loop_f(t,d){}
begin_c_loop(c,t)
{}
end_c_loop(c,t)
begin_f_loop(f,t)
{}
end_f_loop(f,t)
29
June 4, 2014
Release 15.0
Density
C_DUDX(c,t);
Velocity derivative
C_P(c,t);
Pressure
C_DUDY(c,t);
Velocity derivative
C_U(c,t);
U-velocity
C_DUDZ(c,t);
Velocity derivative
C_V(c,t);
V-velocity
C_DVDX(c,t);
Velocity derivative
C_W(c,t);
W-velocity
C_DVDY(c,t);
Velocity derivative
C_T(c,t);
Temperature
C_DVDZ(c,t);
Velocity derivative
C_H(c,t);
Enthalpy
C_DWDX(c,t);
Velocity derivative
C_K(c,t);
C_DWDY(c,t);
Velocity derivative
C_D(c,t);
C_DWDZ(c,t);
Velocity derivative
C_YI(c,t,i);
C_MU_L(c,t);
Laminar viscosity
C_UDSI(c,t,i);
C_MU_T(c,t);
Turbulent viscosity
C_UDMI(c,t,i);
C_MU_EFF(c,t);
Effective viscosity
30
June 4, 2014
Release 15.0
Geometry Macros
C_CENTROID(pos,c,t)
F_CENTROID(pos,f,t)
F_AREA(A,f,t)
NV_MAG(A)
C_VOLUME(c,t)
C_VOLUME_2D(c,t)
A Hex cell
Faces
Nodes
Node Coordinates:
NODE_X(nn)
NODE_Y(nn)
NODE_Z(nn)
31
Many more
macros available,
see UDF Manual.
Release 15.0
C0
C1
A faces area vector
points from C0 to C1
June 4, 2014
Release 15.0
C0
C1
A faces area vector
points from C0 to C1
The faces that bound a cell can be accessed using this loop :
int nf;
for(nf = 0; nf < C_NFACES(c,ct); nf++)
{
f = C_FACE(c,ct,nf);
ft= C_FACE_THREAD(c,ct,nf);
}
33
June 4, 2014
Release 15.0
if (!Data_Valid_P()) return;
34
June 4, 2014
if(NNULLP(THREAD_T1(ft)))
{ ... Do things on neighbouring
cell thread.
}
Release 15.0
35
Profiles
: DEFINE_PROFILE
Source Terms
: DEFINE_SOURCE
Properties
: DEFINE_PROPERTY
User-defined Scalars
: DEFINE_UDS_UNSTEADY , DEFINE_UDS_FLUX
DEFINE_DIFFUSIVITY
Initialization
: DEFINE_INIT
Global Functions
: DEFINE_ADJUST
DEFINE_ON_DEMAND
DEFINE_RW_FILE
June 4, 2014
Additional Information
Many additional macros are available to implement different physical
models including:
Combustion models
Particle-based models
Turbulence models
Radiation models
etc.
June 4, 2014
Release 15.0
path\ANSYS Inc\v150\fluent\fluent15.0.0\src
directory.
At the beginning of every UDF source code file, the header file MUST be included.
#include udf.h
37
There are many more header files stored in the same directory which can be
browsed by users but most are already included automatically within udf.h.
June 4, 2014
Release 15.0