User Defined Programming
User Defined Programming
What are User Defined Functions Introduction to C Set-Up C User Routines in Fluent Implementing a Custom Turbulence Model Programming in other CFD Commercial Codes
Inlet zone
1) 2) 3) 4)
Determine the cell-faces belonging to the inlet zone Loop on all those faces Determine the coordinate of the face centroid Specify the x-velocity component
ME469B/6/GI 3
function inlet_parab Definitions Loop over all the inlet cell faces Evaluate the face centroid coordinates The function return the velocity
ME469B/6/GI
Final Result
ME469B/6/GI
C Programming
Typical C function:
ME469B/6/GI
C vs. FORTRAN
ME469B/6/GI
Comments are placed anywhere in the program between /* */ Statements are grouped by curly brackets { }
Variables defined within the body functions are local Variables defined outside the body functions are global and can be used by all the functions that follows Variables MUST be ALWAYS defined explicitly Integer/real/double functions have to return a integer/real/double value C is case sensitive!
ME469B/6/GI 9
Basic Statements
Arithmetic expressions in C look like FORTRAN
a = y + (i-b)/4;
ME469B/6/GI
10
Standard C types are: Integer, Real, Double, Char C allows to define additional types
typedef struct list{int id, float x[3], int id_next};
ME469B/6/GI
11
Pointers
A pointer is a variable which contain the address of another variable Pointers are defined as:
int *a_p; int a; /* pointer to an integer variable */ /* an integer variable */
Set-up a pointer
a = 1; a_p = &a; /* &a return the address of variable a */
ME469B/6/GI
12
Operators
Arithmetic Operators Logical Operators
ME469B/6/GI
13
Conditional Statements
if and if-else Example
ME469B/6/GI
14
Loop Procedure
for loops Example
ME469B/6/GI
15
C Preprocessor
It handles Macro substitutions
#define A B #define my_f(x,y) x+y*3-5
ME469B/6/GI
16
Programming in C
Additional information are: http://www.cs.cf.ac.uk/Dave/C/CE.html Plenty of books: The C Programming Language, Kernighan & Ritchie, 1988
ME469B/6/GI
17
10
Makefile makefile
src my_udf.c
lnx86
3d
my_udf.c
libudf.so
21
There are two files Makefile.udf makefile.udf to be copied in the directory my_library to be copied in the directory my_library/src
The first one does not require modifications. In the second one two macros MUST be modified
SOURCE = my_udf.c FLUENT_INC = /usr/local/Fluent.Inc
ME469B/6/GI
22
11
UDFs in FLUENT
Boundary Conditions Initial Conditions Adjust Function Source Terms Material Properties Execute on Demand User Defined Scalars User Defined Memory Pointers to threads Geometry Macros Cell and Face Variables Arithmetic and Trigonometric Functions
Available MACROS
Programming Tools
ME469B/6/GI
23
Boundary Conditions
The inlet profile specification is based on the macro DEFINE_PROFILE DEFINE_PROFILE can be used for wall boundary conditions as well to impose temperature, velocity, shear stress, etc. It is possible to specify a constant value, a position-dependent or time-dependent values and to link the values on the boundary to the values in the internal cells Internal Values
x x
BC
Note that the BCs are applied to the faces of the cells (face centroids)
ME469B/6/GI 24
12
Initial Conditions
The initial condition specification can be performed using the macro DEFINE_INIT Space-dependent conditions might be imposed The routine is executed once at the beginning of the solution process It is attached in the UDFs hooks Define User Defined Function Hooks
ME469B/6/GI
25
Initial Conditions
Note that the difference between the DEFINE_PROFILE and DEFINE_INIT is that the former performs a loop on the face-centroids belonging to a certain zone whereas the latter loops on the cell-centroids Example:
ME469B/6/GI
26
13
Adjust Function
Similar to the DEFINE_INIT but it is executed every iteration: DEFINE_ADJUST Can be used for postprocessing, clipping, etc. It is attached in the UDFs hooks Define User Defined Function Hooks
ME469B/6/GI
27
Source Terms
To add a source term in the equations: DEFINE_SOURCE Can be used for: Continuity Momentum (component by component) Turbulent quantities Energy It is different from the previous macros because it works on a cell-by-cell basis (no need to perform loops!)
ME469B/6/GI
28
14
Source Terms
Define Boundary Conditions Fluid
ME469B/6/GI
29
Source Terms
In FLUENT the source terms are written in the form S (f) = A + B f where f is the dependent variable A is treated explicitly and B f is treated implicitly In the DEFINE_SOURCE both terms A and B have to be specified
ME469B/6/GI
30
15
Material Properties
To define the properties of the materials : DEFINE_PROPERTIES Can be used for: Viscosity Density Thermal Conductivity . As for the source terms works on a cell-by-cell basis
ME469B/6/GI
31
Material Properties
Define Material Property
All the available UDFs Are shown in the menu
ME469B/6/GI
32
16
Execute on Demand
To perform operations instantaneously : EXECUTE_ON_DEMAND It is executed when activated by the user Can be used for: Debugging Postprocessing . Define User Defined Execute on Demand
ME469B/6/GI
33
ME469B/6/GI
34
17
Note that the default diffusivity for the scalars is constant and equal to 1!
ME469B/6/GI 35
ME469B/6/GI
36
18
ME469B/6/GI
37
ME469B/6/GI
38
19
I/O in UDF
User Defined Scalars and User Defined Memory are AUTOMATICALLY Stored in the Fluent Data files Additional I/O (from and to the Fluent Case and Data files) can be Accomplished using the macro DEFINE_RW_FILE
ME469B/6/GI
39
Detailed Programming
The macros introduced before are interpreted/compiled and attached to the various Fluent panels The detailed programming is based on additional macros that allow to loop on cells to retrieve field variables, etc. Loop Macros Geometry Macros Field Variable Macros Control Macros Arithmetic and Trigonometric Macros Before looking at the Macros, the Fluent Data structure in introduced
ME469B/6/GI 40
20
Data Structure
It is based on a hierarchy of structures Threads (zones) are collection of cells or faces Domain is a collections of threads
ME469B/6/GI
41
Threads
Every zone is associated to a single ID (available in the boundary conditions menu)
zone ID
Given the ID of a thread the pointer can be retrieved as:
Thread *tf = Lookup_Thread(domain, ID);
Given the thread pointer the ID of the zone can be retrieved as:
ID = THREAD_ID(thread);
ME469B/6/GI 42
21
Loop Macros
ME469B/6/GI
43
Cell-face connectivity
When looping on faces the surrounding cells can be accessed using the macros:
cell0_thread = F_C0_THREAD(face,thread) cell1_thread = F_C1_THREAD(face,thread) cell0_id = F_C0(face,thread) cell1_id = F_C1(face,thread)
cell0
face
cell1
When looping on cells adjacent faces and cells can be accessed using the macros:
for(nf=0;nf<C_NFACES(cell,thread);nf++) { face_id = C_FACE(cell,thread,nf); face_thread = C_FACE_THREAD(cell,thread,nf); }
ME469B/6/GI
22
Geometry Macros
ME469B/6/GI
46
23
Control Macros
24
Transport Equations
ME469B/6/GI
49
Damping functions
ME469B/6/GI
50
25
DEFINE_SOURCE(k_source, thread, eqn) DEFINE_SOURCE(d_source, thread, eqn) DEFINE_PROPERTY(ke_diffusivity, domain) DEFINE_PROFILE(wall_d_bc, domain) DEFINE_ADJUST(turb_adjust, domain)
ME469B/6/GI
51
Density Molecular viscosity Eddy viscosity Strain Rate Magnitude Wall distance
ME469B/6/GI
52
26
UDF Header
#include "udf.h" /* Turbulence model constants */ #define C_MU 0.09 #define SIG_TKE 1.0 #define SIG_TDR 1.3 #define C1_D 1.44 #define C2_D 1.92 /* User-defined scalars */ enum { TKE, TDR, N_REQUIRED_UDS };
ME469B/6/GI
53
Damping Functions
These are defined on a cell-by-cell basis
/* Reynolds number definitions */ real Re_y(cell_t c, Thread *t) { return C_R(c,t)*sqrt(C_UDSI(c,t,TKE))*C_WALL_DIST(c,t)/C_MU_L(c,t);} real Re_t(cell_t c, Thread *t) { return C_R(c,t)*SQR(C_UDSI(c,t,TKE))/C_MU_L(c,t)/C_UDSI(c,t,TDR);} /* Damping Functions */ real f_mu(cell_t c, Thread *t) { return tanh(0.008*Re_y(c,t))*(1.+4/pow(Re_t(c,t),0.75));} real f_1(cell_t c, Thread *t) { return 1.;} real f_2(cell_t c, Thread *t) { return (1.-2/9*exp(-Re_t(c,t)*Re_t(c,t)/36))*(1.-exp(-Re_y(c,t)/12));}
ME469B/6/GI
54
27
DEFINE_SOURCE(k_source, c, t, dS, eqn) { real G_k; G_k = C_MU_T(c,t)*SQR(Strainrate_Mag(c,t)); dS[eqn] = -2.*C_R(c,t)*C_R(c,t)*C_MU*f_mu(c,t)*C_UDSI(c,t,TKE)/C_MU_T(c,t); return G_k - C_R(c,t)*C_R(c,t)*C_MU*f_mu(c,t)*SQR(C_UDSI(c,t,TKE))/C_MU_T(c,t); }
ME469B/6/GI
55
ME469B/6/GI
56
28
Diffusivity
The diffusion terms in the scalar equations are set-up together
DEFINE_DIFFUSIVITY(ke_diffusivity, c, t, eqn) { real diff; real mu = C_MU_L(c,t); real mu_t = C_R(c,t)*C_MU*f_mu(c,t)*SQR(C_UDSI(c,t,TKE))/C_UDSI(c,t,TDR); switch (eqn) { case TKE: diff = mu_t/SIG_TKE + mu; break; case TDR: diff = mu_t/SIG_TDR + mu; break; default: diff = mu_t + mu; } return diff; }
ME469B/6/GI
57
Eddy Viscosity
The eddy viscosity is set in the adjust routine (called at the beginning of each Iteration) and it is used in the mean flow and in the scalar equations
DEFINE_ADJUST(turb_adjust, domain) { Thread *t; cell_t c; /* Set the turbulent viscosity */ thread_loop_c (t, domain) if (FLUID_THREAD_P(t)) { begin_c_loop(c,t) { C_MU_T(c,t) = C_R(c,t)*C_MU*f_mu(c,t)*SQR(C_UDSI(c,t,TKE))/C_UDSI(c,t,TDR); } end_c_loop(c,t) } }
ME469B/6/GI
58
29
The derivative is rootk/dy begin_f_loop(f,t) { rootk is the sqrt of k in c0 = F_C0(f,t); the adjacent cell center rootk = sqrt(MAX(C_UDSI(c0,t0,TKE), 0.)); dy is the distance between F_CENTROID(xw,f,t); cell and face center C_CENTROID(xc,c0,t0); NV_VV(dx, =, xc, -, xw); dy = ND_MAG(dx[0], dx[1], dx[2]); drootkdy = rootk/dy; F_PROFILE(f,t,position) = 2.*C_MU_L(c0,t0)/C_R(c0,t0)*drootkdy*drootkdy; } end_f_loop(f,t)
} ME469B/6/GI 59
Set-Up a case using the standard k-e Define two scalars (TKE, TDR) Compile and Attach the UDFs Deactivate the Equations for k and e Initialize and solve the RANS + TKE and TDR
ME469B/6/GI
60
30
Source terms
Boundary conditions
ME469B/6/GI
61
ME469B/6/GI
62
31
For postprocessing purposes the UDS have to be used instead of turbulent quantities
ME469B/6/GI 63
Additional Information
Many additional macros are available to implement different physical models combustion models particles-based models . It is formally possible to develop additional numerical methods flux discretizations variable reconstruction and clipping . Information are available in the FLUENT UDF manual (class web site)
ME469B/6/GI
64
32
CFX
CFX (v4) is a structured code; the data structure is much simpler because the field variables are accessed directly. It uses 1D arrays where the quantities are stored in succession CFX UDFs are written in FORTRAN For example: U(I,J,K) U(IJK) where IJK = (K-1)*NI*NJ+(J-1)*NI There are no macros, but examples of subroutines to perform the customization USRBC USRSRC USRDIF .
ME469B/6/GI 65
Star-CD
StarCD is an unstructured code similar to Fluent in term of data organization But similar to CFX for the organization of the UDF. StarCD has threads (as Fluent) and the UDF work normally on a cell-by-cell basis StarCD UDFs are written in FORTRAN There are no macros, but examples of subroutines to perform the customization BCDEFW SORSCA DIFFUS .
ME469B/6/GI
66
33