当前位置:
文档之家› FLUENT_UDF官方培训教程
FLUENT_UDF官方培训教程
A Pera Global Company © PERA China
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)
/define/userdefined/functions/manage
把 UDF 源码加入到源文件列表中
点击 Interpret FLUENT 窗口会出现语言 如果没有错误,点击 Close
A Pera Global Company © PERA China
解释 vs. 编译
用户函数可以在运行时读入并解释,也可以编译形成共享库文件并和 FLUENT链接
A Pera Global Company © PERA China
}
}
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
其他 UDF Hooks
除了边界条件、源项、材料属性外, UDF 还可用于 – 初始化
• 每次初始化执行一次
Define
User-Defined
Function Hooks
– 求解调整
• 每次迭代执行一次
– 壁面热流量
• 以传热系数方式定义流体侧的扩散和 辐射热流量 • 应用于所有壁面
– 用户定义表面反应或体积反应 – Case/ data 文件的读写
*d; *t; c; f; *node;
d is a pointer to domain thread t is a pointer to thread c is cell thread variable f is a face thread variable node is a pointer to a node.
Repeat
Solve Mass Continuity; Update Velocity
Exit Loop
Check Convergence Update Properties User-Defined Properties
Solve Energy Solve Species Solve Turbulence Equation(s) Solve Other Transport Equations as required
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 */
ANSYS FLUENT 培训教材 第七节:UDF
安世亚太科技(北京)有限公司
A Pera Global Company © PERA China
概要
FLUENT UDF简介 FLUENT 数据结构和宏 两个例子 UDF 支持
A Pera Global Company © PERA China
简介
什么是UDF?
– UDF 是用户自己用C语言写的一个函数,可以和FLUENT动态链接
• 标准C 函数
▪ 三角函数,指数,控制块,Do循环,文件读入/输出等
• 预定义宏
▪ 允许获得流场变量,材料属性,单元几何信息及其他
为什么使用 UDFs?
– 标准的界面不能编程模拟所有需求:
• 定制边界条件,源项,反应速率,材料属性等
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)
}
A Pera Global Company © PERA China
• 读入顺序必须和写出顺序一致
– Execute-on-Demand 功能
• 不参与求解迭代
A Pera Global Company © PERA China
例 2 – 定制初始化
#include "udf.h“
在球内设定初始温度600 K 球 中心点位于 (0.5, 0.5, 0.5), 半 径为 0.25, 其余区域为300 K
第4步 – 运行
可以在运行窗口中改变速度分布的更新间隔(默认为1)
– 这个设置控制了流场多久(迭代或时间步)更新一次
运行 calculation
A Pera Global Company © PERA China
结果
左图为速度矢量图 右图为入口的速度矢量图,注意速度分布是抛物线型的
A Pera Global Company © PERA China
第 2 步 – 解释或编译 UDF
编译UDF
Define User-Defined Functions Compiled
解释UDF
Define User-Defined Functions Interpreted
把 UDF 源码加入到源文件列表中 点击 Build进行编译和链接 如果没有错误,点击Load读入库文件 如需要,也可以卸载库文件
例子 – 抛物线分布的速度入口
在二维弯管入口施加抛物线分布的速度 x 方向的速度定义为
需要通过宏获得入口的中心点, 通过另外一个宏赋予速度条件
A Pera Global Company © PERA China
第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
A Pera Global Company © PERA China
UDF 数据结构(2)
cell_t 声明了识别单元的整型数据类型
face_t声明了识别面的整型数据类型
Type Variable Meaning of the declaration
Domain Thread cell_t face_t Node
域指针通过变量传递到UDF
thread_loop_c 宏用来获得 所有单元threads (zones), begin_c_loop 宏获得每个 单元thread中的单元
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) } }
face Thread
Boundary (face thread or zone)
Fluid (cell thread or zone) Cells Faces
为了在thread (zone)中获得数据,我们需要提供正确的指针,并 使用循环宏获得thread中的每个成员(cell or face)
A Pera Global Company © PERA China
第3 步– 在 FLUENT GUI中hook UDF
打开边界条件面板,选择你要施加UDF的边界
把 Constant 改为 udf x_velocity
宏的名字为 DEFINE_PROFILE 中第一个参数
A Pera Global Company © PERA China
• 定制物理模型
• 用户提供的模型方程 • 调整函数 • 执行和需求函数 • 初始化
A Pera Global Company © PERA China
可以使用UDF的位置
Segregated Userdefined ADJUST PBCS DBCS
Initialize
Begin Loop
Solver? Source terms
解释 vs. 编译
– 解释
• 解释器是占用内存的一个大型程序 • 通过逐行即时执行代码 • 优势 – 不需要第三方编译器 • 劣势 – 解释过程慢,且占用内存
– 编译
• UDF 代码一次转换为机器语言 • 运行效率高. • 创建共享库,和其他求解器链接 • 克服解释器的缺陷