Fluent12 Lecture08 Udf
Fluent12 Lecture08 Udf
User-Defined Functions
Introductory FLUENT
Training
ANSYS, Inc. Proprietary
2009 ANSYS, Inc. All rights reserved.
8-1
User-Defined Functions
Outline
Training Manual
8-2
User-Defined Functions
Introduction
Training Manual
Pre-Defined Macros
Allows access to field variable, material property, and cell geometry data and many
utilities
8-3
User-Defined Functions
Initialize
Begin Loop
Userdefined
ADJUST
Solver?
Source terms
User Defined
INITIALIZE
Training Manual
Segregated
PBCS
Solve U-Momentum
Source terms
Solve V-Momentum
Solve Mass
& Momentum
Solve W-Momentum
Repeat
Exit Loop
DBCS
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-4
User-Defined Functions
Training Manual
The cell zones and face zones of a model (in the finite-volume
scheme) are accessed in UDFs as Thread data types
Thread is a FLUENT-defined data type
Domain
Domain
Cell
Fluid (cell
thread or
zone)
Cell
Thread
face
face
Thread
Thread
Cells
Faces
8-5
User-Defined Functions
Training Manual
Domain
Thread
cell_t
face_t
Node
*d;
*t;
c;
f;
*node;
Fluid cell-thread
(control-volume ensemble)
Boundary face-thread
(boundary-face ensemble)
Nodes
Internal face-thread
(internal-face ensemble)
associated with cell-threads
8-6
User-Defined Functions
Training Manual
d: a domain pointer
ct, t: a cell thread pointer
ft,f_thread: a face thread
pointer
c: a cell thread variable
f: a face thread variable
8-7
User-Defined Functions
Training Manual
8-8
User-Defined Functions
Training Manual
The DEFINE_PROFILE macro allows the Header file udf.h must be included at the top
of the program by the #include command
function x_velocity to
be defined.
#include "udf.h"
All UDFs begin with a DEFINE_
DEFINE_PROFILE(x_velocity,thread,nv)
macro
{
x_velocity will appear in the
float x[3]; /* an array for the
solver GUI
coordinates */
thread and nv are arguments of
float y;
the DEFINE_PROFILE macro,
face_t f;
/* f is a face
which are used to identify the
thread index */
zone and variable being defined,
respectively
begin_f_loop(f, thread)
The macro begin_f_loop loops
{
over all faces f, pointed by thread
F_CENTROID(x,f,thread);
The F_CENTROID macro assigns
y = x[1];
cell position vector to x[]
F_PROFILE(f, thread, nv)
The F_PROFILE macro applies the
= 20.*(1.y*y/(.0745*.0745));
velocity component to face f
}
The code is store as a text file
end_f_loop(f, thread)
inlet_bc.c
}
ANSYS, Inc. Proprietary
2009 ANSYS, Inc. All rights reserved.
8-9
User-Defined Functions
User-Defined
Interpreted UDF
Functions
Define
Compiled
User-Defined
Functions
Interpreted
/define/user-defined/functions/manage
Training Manual
8-10
User-Defined Functions
Training Manual
8-11
User-Defined Functions
Training Manual
Open the boundary condition panel for the surface to which you
would like to apply the UDF
Switch from Constant to udf x_velocity in the drop-down list.
The macro name is the first argument of DEFINE_PROFILE in the UDF
code
8-12
User-Defined Functions
Training Manual
You can change the Profile Update Interval in the Run Calculation
panel (default value is 1).
This setting controls how often (either iterations or time steps if
unsteady) the UDF profile is updated.
8-13
User-Defined Functions
Training Manual
The figure on the left shows the velocity field through the 2D elbow.
The figure on the right shows the velocity vectors at the inlet. Notice
the imposed parabolic velocity profile.
8-14
User-Defined Functions
Training Manual
Initialization
User-Defined
Function Hooks
Solution adjustment
Executes every iteration.
Execute-on-Demand capability
Does not participate in the solver iterations
8-15
User-Defined Functions
Training Manual
#include "udf.h
DEFINE_INIT(my_init_function, domain)
{
cell_t c;
Thread *ct;
real xc[ND_ND];
thread_loop_c(ct,domain)
{
begin_c_loop (c,ct)
{
C_CENTROID(xc,c,ct);
if (sqrt(ND_SUM(pow(xc[0]-0.5,2.),
pow(xc[1] - 0.5,2.),
pow(xc[2] - 0.5,2.))) < 0.25)
C_T(c,ct) = 600.;
else
C_T(c,ct) = 300.;
}
end_c_loop (c,ct)
}
}
8-16
User-Defined Functions
DEFINE Macros
Training Manual
8-17
User-Defined Functions
Training Manual
Returns nodes/cell
Returns faces/cell
Returns nodes/face
Returns coordinates of cell centroid
in array x[]
F_CENTROID(x,f,t); Returns coordinates of face centroid
in array x[]
F_AREA(A,f,t);
Returns area vector in array A[]
C_VOLUME(c,t);
Returns cell volume
C_VOLUME_2D(c,t); Returns cell volume (axisymmetric domain)
C_NNODES(c,t);
C_NFACES(c,t);
F_NNODES(f,t);
C_CENTROID(x,c,t);
8-18
User-Defined Functions
Training Manual
C_R(c,t); Density
C_P(c,t); Pressure
C_U(c,t); U-velocity
C_V(c,t); V-velocity
C_W(c,t); W-velocity
C_T(c,t); Temperature
C_H(c,t); Enthalpy
C_K(c,t); Turbulent kinetic energy (k)
C_D(c,t); Turbulent dissipation rate ()
C_O(c,t); Specific dissipation of k ()
C_YI(c,t,i);
Species mass fraction
C_UDSI(c,t,i); UDS scalars
C_UDMI(c,t,i); UDM scalars
C_DUDX(c,t);
C_DUDY(c,t);
C_DUDZ(c,t);
C_DVDX(c,t);
C_DVDY(c,t);
C_DVDZ(c,t);
C_DWDX(c,t);
C_DWDY(c,t);
C_DWDZ(c,t);
Velocity derivative
Velocity derivative
Velocity derivative
Velocity derivative
Velocity derivative
Velocity derivative
C_MU_L(c,t);
C_MU_T(c,t);
C_MU_EFF(c,t);
C_K_L(c,t);
Laminar viscosity
Turbulent viscosity
Effective viscosity
Laminar thermal
conductivity
Turbulent thermal
conductivity
Effective thermal
conductivity
Specific heat
Gas constant
C_K_T(c,t);
C_K_EFF(c,t);
Velocity derivative
Velocity derivative
Velocity derivative
C_CP(c,t);
C_RGAS(c,t);
8-19
User-Defined Functions
Training Manual
C_R(c,t); Density
C_P(c,t); Pressure
C_U(c,t); U-velocity
C_V(c,t); V-velocity
C_W(c,t); W-velocity
C_T(c,t); Temperature
C_H(c,t); Enthalpy
C_K(c,t); Turbulent kinetic energy (k)
C_D(c,t); Turbulent dissipation rate ()
C_O(c,t); Specific dissipation of k ()
C_YI(c,t,i);
Species mass fraction
C_UDSI(c,t,i); UDS scalars
C_UDMI(c,t,i); UDM scalars
C_DUDX(c,t);
C_DUDY(c,t);
C_DUDZ(c,t);
C_DVDX(c,t);
C_DVDY(c,t);
C_DVDZ(c,t);
C_DWDX(c,t);
C_DWDY(c,t);
C_DWDZ(c,t);
Velocity derivative
Velocity derivative
Velocity derivative
Velocity derivative
Velocity derivative
Velocity derivative
Laminar viscosity
Turbulent viscosity
Effective viscosity
Laminar thermal
conductivity
C_K_T(c,t);
Turbulent thermal
conductivity
C_K_EFF(c,t); Effective thermal
conductivity
C_CP(c,t);
Specific heat
C_RGAS(c,t);
Gas constant
C_DIFF_L(c,t); Laminar species
diffusivity
C_DIFF_EFF(c,t,i); Effective species
diffusivity
C_MU_L(c,t);
C_MU_T(c,t);
C_MU_EFF(c,t);
C_K_L(c,t);
Velocity derivative
Velocity derivative
Velocity derivative
8-20
User-Defined Functions
Training Manual
Define
User-Defined
Memory
8-21
User-Defined Functions
Training Manual
Define
User-Defined
Scalars
Unsteady function
DEFINE_UDS_UNSTEADY(name,cell,thread,index,apu,su)
If statements are required in order to associate multiple flux and transient
functions with each UDS
Example
Can be used to solve the electromagnetic field equations.
ANSYS, Inc. Proprietary
2009 ANSYS, Inc. All rights reserved.
8-22
User-Defined Functions
Additional Macros
Training Manual
Still many more macros are available in the following categories and
are documented in UDF Manual:
Turbulence Models
Multiphase models
Reacting flows
Dynamic mesh
Input/Output
8-23
User-Defined Functions
Training Manual
8-24
User-Defined Functions
Training Manual
8-25