ANSYS FLUENT 培训教材 第七节:UDF 安世亚太科技(北京)有限公司
概要 FLUENT UDF简介 FLUENT 数据结构和宏 两个例子 UDF 支持
简介 什么是UDF? 为什么使用 UDFs? UDF 是用户自己用C语言写的一个函数,可以和FLUENT动态链接 三角函数,指数,控制块,Do循环,文件读入/输出等 预定义宏 允许获得流场变量,材料属性,单元几何信息及其他 为什么使用 UDFs? 标准的界面不能编程模拟所有需求: 定制边界条件,源项,反应速率,材料属性等 定制物理模型 用户提供的模型方程 调整函数 执行和需求函数 初始化
可以使用UDF的位置 User-Defined Properties User-Defined BCs User Defined INITIALIZE Segregated PBCS Exit Loop Repeat Check Convergence Update Properties Solve Turbulence Equation(s) Solve Species Solve Energy Initialize Begin Loop DBCS Solve Other Transport Equations as required Solver? Solve Mass Continuity; Update Velocity Solve U-Momentum Solve V-Momentum Solve W-Momentum Solve Mass & Momentum Solve Mass, Momentum, Energy, Species User-defined ADJUST Source terms Source terms This slide takes a birds-eye-view of the FLUENT solvers. The solution procedure begins with initialization, following which an iteration loop is opened. For keeping things simple, we are considering only steady state analysis procedure here. Within the loop we solve the conservation equations for mass, momentum and energy either as coupled or in sequence based on the choice of the solver. Other scalar equations like turbulence, species and user-defined-scalars are solved sequentially after the basic flow equations. We then update boundary conditions, properties based on the recent solution Next the state of convergence is checked and a decision is made whether or not to continue through the iteration loop. Now, for users to customize the code, we needed to design ‘access-point’s for the user. Those are shown in blue boxes. These are: The user can initialize the fields his way; Can take control at the top of the iteration loop for making some special computation; Can add source terms to all equations; Can defined most properties with custom functions; Can prescribe special boundary conditions. And what are not shown here are that the user can make model specific changes - for example, can modify convective flux function for scalars, various multiphase functions, modify turbulent viscosity or even time steps in unsteady analysis.
5 UDF 数据结构 (1) 在UDF中,体域和面域通过Thread数据类型获得 Thread 是 FLUENT 定义的数据类型 为了在thread (zone)中获得数据,我们需要提供正确的指针,并使用循环宏获得thread中的每个成员(cell or face) Domain Cells Cell Thread face Thread Faces Domain Cell Boundary (face thread or zone) Fluid (cell thread or zone) 5
6 UDF 数据结构(2) cell_t 声明了识别单元的整型数据类型 face_t声明了识别面的整型数据类型 Type Variable Meaning of the declaration Domain *d; d is a pointer to domain thread Thread *t; t is a pointer to thread cell_t c; c is cell thread variable face_t f; f is a face thread variable Node *node; node is a pointer to a node. Boundary face-thread (boundary-face ensemble) Fluid cell-thread (control-volume ensemble) Internal face-thread (internal-face ensemble) associated with cell-threads Nodes 6
7 UDF中的循环宏 几个经常用到的循环宏为: 对域d中所有单元thread循环: thread_loop_c(ct,d) { } 对域d中所有面thread循环: thread_loop_f(ft,d) { } 对thread t中所有单元循环: begin_c_loop(c, t) {…} end_c_loop (c,t) 对面thread中所有面循环 begin_f_loop(f, f_thread) { … } end_f_loop(f, f_thread) 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 7
例子 – 抛物线分布的速度入口 在二维弯管入口施加抛物线分布的速度 x 方向的速度定义为 需要通过宏获得入口的中心点, 通过另外一个宏赋予速度条件
第1步 – 准备源代码 DEFINE_PROFILE 宏允许定义x_velocity函数 所有的UDFs 以 DEFINE_ 宏开始 x_velocity 将在 GUI中出现 thread 和 nv DEFINE_PROFILE 宏的参数, 分别用来识别域和变量 begin_f_loop宏通过thread指针,对所有的面f循环 F_CENTROID宏赋单元位置向量给 x[] F_PROFILE 宏在面 f上施加速度分量 代码以文本文件保存 inlet_bc.c Header file “udf.h” must be included at the top of the program by the #include command #include "udf.h" DEFINE_PROFILE(x_velocity,thread,nv) { float x[3]; /* an array for the coordinates */ float y; face_t f; /* f is a face thread index */ begin_f_loop(f, thread) F_CENTROID(x,f,thread); y = x[1]; F_PROFILE(f, thread, nv) = 20.*(1.- y*y/(.0745*.0745)); } end_f_loop(f, thread)
第 2 步 – 解释或编译 UDF 把 UDF 源码加入到源文件列表中 编译UDF 把 UDF 源码加入到源文件列表中 点击 Build进行编译和链接 如果没有错误,点击Load读入库文件 如需要,也可以卸载库文件 /define/user-defined/functions/manage 解释UDF 把 UDF 源码加入到源文件列表中 点击 Interpret FLUENT 窗口会出现语言 如果没有错误,点击 Close Define User-Defined Functions Compiled Define User-Defined Functions Interpreted
解释 vs. 编译 用户函数可以在运行时读入并解释,也可以编译形成共享库文件并和FLUENT链接 解释 vs. 编译 解释器是占用内存的一个大型程序 通过逐行即时执行代码 优势 – 不需要第三方编译器 劣势 – 解释过程慢,且占用内存 编译 UDF 代码一次转换为机器语言 运行效率高. 创建共享库,和其他求解器链接 克服解释器的缺陷 只有在没安装C编译器时使用解释方式 Explain for simple problems interpreted codes is the way to go. Does not require Makefiles. However, handles only a subset of the C language.
第3 步– 在 FLUENT GUI中hook UDF 把 Constant 改为 udf x_velocity 宏的名字为 DEFINE_PROFILE 中第一个参数
第4步 – 运行 可以在运行窗口中改变速度分布的更新间隔(默认为1) 运行 calculation 这个设置控制了流场多久(迭代或时间步)更新一次 运行 calculation
结果 左图为速度矢量图 右图为入口的速度矢量图,注意速度分布是抛物线型的
其他 UDF Hooks 除了边界条件、源项、材料属性外,UDF 还可用于 初始化 求解调整 壁面热流量 用户定义表面反应或体积反应 每次初始化执行一次 求解调整 每次迭代执行一次 壁面热流量 以传热系数方式定义流体侧的扩散和辐射热流量 应用于所有壁面 用户定义表面反应或体积反应 Case/ data 文件的读写 读入顺序必须和写出顺序一致 Execute-on-Demand 功能 不参与求解迭代 Define User-Defined Function Hooks
例 2 – 定制初始化 #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) 在球内设定初始温度600 K 球中心点位于 (0.5, 0.5, 0.5), 半径为 0.25, 其余区域为300 K 域指针通过变量传递到UDF thread_loop_c 宏用来获得所有单元threads (zones), begin_c_loop 宏获得每个单元thread中的单元
DEFINE 宏 DEFINE 宏的例子 DEFINE_ADJUST(name,domain); general purpose UDF called every iteration DEFINE_INIT(name,domain); UDF used to initialize field variables DEFINE_ON_DEMAND(name); an ‘execute-on-demand’ function DEFINE_RW_FILE(name,fp); customize reads/writes to case/data files DEFINE_PROFILE(name,thread,index); boundary profiles DEFINE_SOURCE(name,cell,thread,dS,index); equation source terms DEFINE_HEAT_FLUX(name,face,thread,c0,t0,cid,cir); heat flux DEFINE_PROPERTY(name,cell,thread); material properties DEFINE_DIFFUSIVITY(name,cell,thread,index); UDS and species diffusivities DEFINE_UDS_FLUX(name,face,thread,index); defines UDS flux terms DEFINE_UDS_UNSTEADY(name,cell,thread,index,apu,su); UDS transient terms DEFINE_SR_RATE(name,face,thread,r,mw,yi,rr); surface reaction rates DEFINE_VR_RATE(name,cell,thread,r,mw,yi,rr,rr_t); volumetric reaction rates DEFINE_SCAT_PHASE_FUNC(name,cell,face); scattering phase function for DOM DEFINE_DELTAT(name,domain); variable time step size for unsteady problems DEFINE_TURBULENT_VISCOSITY(name,cell,thread); calculates turbulent viscosity DEFINE_TURB_PREMIX_SOURCE(name,cell,thread,turbflamespeed,source); turbulent flame speed DEFINE_NOX_RATE(name,cell,thread,nox); NOx production and destruction rates It is not necessary and you should not go into a detailed description on each of the above DEFINE macros. Pick a couple and describe what they are used for.
几何和时间宏 C_NNODES(c,t); Returns nodes/cell C_NFACES(c,t); Returns faces/cell F_NNODES(f,t); Returns nodes/face C_CENTROID(x,c,t); 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) real flow_time(); Returns actual time int time_step; Returns time step number RP_Get_Real(“physical-time-step”); Returns time step size Don’t discuss all in detail.
流场变量宏 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); Velocity derivative C_DUDY(c,t); Velocity derivative C_DUDZ(c,t); Velocity derivative C_DVDX(c,t); Velocity derivative C_DVDY(c,t); Velocity derivative C_DVDZ(c,t); Velocity derivative C_DWDX(c,t); Velocity derivative C_DWDY(c,t); Velocity derivative C_DWDZ(c,t); Velocity derivative C_MU_L(c,t); Laminar viscosity C_MU_T(c,t); Turbulent viscosity C_MU_EFF(c,t); Effective viscosity C_K_L(c,t); 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 Don’t discuss all in detail.
流场变量宏 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); Velocity derivative C_DUDY(c,t); Velocity derivative C_DUDZ(c,t); Velocity derivative C_DVDX(c,t); Velocity derivative C_DVDY(c,t); Velocity derivative C_DVDZ(c,t); Velocity derivative C_DWDX(c,t); Velocity derivative C_DWDY(c,t); Velocity derivative C_DWDZ(c,t); Velocity derivative C_MU_L(c,t); Laminar viscosity C_MU_T(c,t); Turbulent viscosity C_MU_EFF(c,t); Effective viscosity C_K_L(c,t); 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 Don’t discuss all in detail.
UDM 对每个单元由用户分配内存 定义多达500个变量 可以通过 UDFs获得: 数据信息存在 FLUENT data 文件中 C_UDMI(cell,thread,index); F_UDMI(face,thread,index); 数据信息存在 FLUENT data 文件中 Define User-Defined Memory
UDS FLUENT 可以求解多达50个用户 定义标量的输运方程 例子 UDS 变量的数量 UDS 在哪个域内求解 通量函数 非稳态函数 DEFINE_UDS_FLUX(name,face,thread,index) 非稳态函数 DEFINE_UDS_UNSTEADY(name,cell,thread,index,apu,su) 例子 能用来求解磁流体方程 Define User-Defined Scalars The setup for UDS are summarized in these two slides.
其他宏 还有许多其他宏: 湍流模型 多相流模型 化学反应流 动网格 输入/输出
UDF 技术支持 由于 UDFs 可能非常复杂, ANSYS 不对用户的UDFs精度及求解稳定性负责 支持限于UDFs 和 FLUENT 求解器间的通讯 Explain for simple problems interpreted codes is the way to go. Does not require Makefiles. However, handles only a subset of the C language.
A Pera Global Company © PERA China 谢谢 www.peraglobal.com.cn A Pera Global Company © PERA China