}
/* Fill second UDS with magnitude of gradient. */ thread_loop_c (t,domain) {
if (NULL != THREAD_STORAGE(t,SV_UDS_I(T4)) && NULL != T_STORAGE_R_NV(t,SV_UDSI_G(T4))) {
begin_c_loop (c,t) {
C_UDSI(c,t,MAG_GRAD_T4) = NV_MAG(C_UDSI_G(c,t,T4)); }
end_c_loop (c,t) } }
thread_loop_f (t,domain) {
if (NULL != THREAD_STORAGE(t,SV_UDS_I(T4)) && NULL != T_STORAGE_R_NV(t->t0,SV_UDSI_G(T4))) {
begin_f_loop (f,t) {
F_UDSI(f,t,MAG_GRAD_T4) = C_UDSI(F_C0(f,t),t->t0,MAG_GRAD_T4); }
end_f_loop (f,t) } } }
条件语句if (NULL != THREAD_STORAGE(t,SV_UDS_I(T4)))检测用户自定义标量中是否已存贮温度的四次方;条件语句NULL != T_STORAGE_R_ NV (t,SV_UDSI_G(T4))检测温度四次方的梯度是否已存入用户自定义标量之中。本例使用了用户自定义标量,所以首先要激活两个用户自定义标量输运方程。 7.5.8 可执行UDFs
本例名为demo_calc的UDF,计算打印当前数据库中的最低温度,最高温度和平均温度,并计算下面的函数,存于用户定义的内存变量中。
f?T??T?Tmin
Tmax?Tmin/**********************************************************************/ /* demand.c */
/* UDF to calculate temperature field function and store in */
/* user-defined memory. Also print min, max, avg temperatures. */
/**********************************************************************/ #include \
extern Domain* domain;
DEFINE_ON_DEMAND(demo_calc) {
float tavg = 0.; float tmax = 0.; float tmin = 0.;
float temp,volume,vol_tot; Thread *t;
114
cell_t c;
/* Loop over all cells in the domain */ thread_loop_c(t,domain) {
/* Compute max, min, volume-averaged temperature */
begin_c_loop(c,t) {
volume = C_VOLUME(c,t); /* get cell volume */ temp = C_T(c,t); /* get cell temperature */ if (temp < tmin || tmin == 0.) tmin = temp; if (temp > tmax || tmax == 0.) tmax = temp; vol_tot += volume; tavg += temp*volume;} end_c_loop(c,t) tavg /= vol_tot;
printf(\/* Compute temperature function and store in user-defined memory */ /* (location index 0) */
begin_c_loop(c,t) {temp = C_T(c,t);
C_UDMI(c,t,0) = (temp-tmin)/(tmax-tmin);} end_c_loop(c,t)}}
函数中使用了嵌套的loop循环,循环内部的函数体实现温度最值,平均值和函数f(T)值的求解。由于函数中用到了用户自定义内存变量,所以需要事先激活用户自定义内存变量,内存变量个数不应小于所使用的内存变量个数。
第六节 UDFs的应用
本章列举了UDFs的几个应用实例。在例子中,我们将边界条件UDFs,源项UDFs,物理性质UDFs和反应速率UDFs应用于实际的相对较简单的物理模型。我们分析这些实例的目的,是为了读者更好的了解UDFs的功能,并能熟练的掌握UDFs。
7.6.1 边界条件UDFs的应用
本节包含两个Interpreted型的例子: 1. 涡轮叶片入口速度分布 2. 管道入口瞬态速度分布 7.6.1.1 涡轮叶片入口速度分布
如图6.1.1,涡轮周围流体网格采用三角无结构网格,定义涡轮腔体左端为速度入口,右端为压力出口边界条件。我们分别计算恒定和抛物型入口速度边界分布的情况,比较两种情况下流场速度的分布。
图6.1.2和图6.1.3为恒定速度入口边界条件计算的结果。入口速度大小为20 m/sec,可以看出恒定速度场在涡轮叶片周围产生扭曲变形。
115
图6.1.1 涡轮叶片周围的网格分布
图6.1.2 恒定速度入口条件下的速度大小分布
图6.1.3 恒定速度入口条件下的速度矢量分布
抛物型速度入口边界条件速度分布满足下面的关系式:
?y? vx?20?20??
?0.0745?其中,y在入口中央取值为0.0,在入口上部和底部分别为+0.0745,-0.0745,根据入口速度分布式速度中央大小为20 m/sec,上下部值都为20 m/sec。
定义抛物型速度入口边界条件的C源程序如下:
/*************************************************************************/ /* vprofile.c */
/* UDF for specifying a steady-state velocity profile boundary condition */
/*************************************************************************/ #include \
DEFINE_PROFILE(inlet_x_velocity, thread, position) {real x[ND_ND]; /* this will hold the position vector */ real y;
116
2face_t f;
begin_f_loop(f, thread)
{F_CENTROID(x,f,thread); y = x[1];
F_PROFILE(f, thread, position) = 20. - y*y/(.0745*.0745)*20.;} end_f_loop(f, thread) }
本例使用Interpreted型UDFs,按照前面相关章节叙述对C源程序进行编译连接,操作在Interpreted UDFs面板中完成。然后在Velocity Inlet面板的X_Velocity下拉列表中,选择udf inlet_x_velocity,在计算的时候FLUENT就能按照抛物性速度入口边界条件进行计算,计算结果见图6.1.4和图6.1.5。
图6.1.4 抛物型速度入口条件下的速度大小分布
117
图6.1.5 抛物型速度入口条件下的速度矢量分布
7.6.2.1 管道入口瞬态速度分布
在本例中,入口速度满足关系式:vx?20?5sin(10t),管道长1m,半径0.2m,管道内流体为空气,密度1 kg/m3,粘性系数2×105 kg/m-s。入口速度分布与时间有关,随时间正弦变化。C源程序如下: /***********************************************************************/ /* unsteady.c */
/* UDF for specifying a transient velocity profile boundary condition */
/***********************************************************************/ #include \
DEFINE_PROFILE(unsteady_velocity, thread, position) {face_t f;
begin_f_loop(f, thread)
{real t = RP_Get_Real(\
F_PROFILE(f, thread, position) = 20. + 5.0*sin(10.*t);} end_f_loop(f, thread)}
函数名为unsteady_velocity,变量flow-time存贮流场当前历经时间,函数RP_GET_REAL得到当前时间,由于本例为非稳态问题,需要选择非稳态解法器,如下激活非稳态解法器:
Define?Models?Solver…
本例使用Interpreted型UDFs,在面板Interpreted UDFs中编译连接UDF,在面板Velocity Inlet中X-Velocity下拉列表选择函数udf unsteady_velocity。进行FLUENT默认初始化之后,就可以在Iterate面板进行迭代计算。
Solve?Iterate…
-
118