教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 政务民生 >

中科大FLUENT讲稿 第七章 自定义函数(10)

来源:网络收集 时间:2026-07-16
导读: return (drag_force); } else if (Re w = log10(Re); drag_force = 18.0 + 2.367*pow(Re,0.82-0.05*w) ; return (drag_force); } else /* Note: suggested valid range 20 drag_force = 18.0 + 3.483*pow(Re,0.6305

return (drag_force);

}

else if (Re < 20.0) {

w = log10(Re);

drag_force = 18.0 + 2.367*pow(Re,0.82-0.05*w) ; return (drag_force); } else

/* Note: suggested valid range 20 < Re < 260 */ {

drag_force = 18.0 + 3.483*pow(Re,0.6305) ; return (drag_force); } }

7.5.5 计算初始化

初始化函数DEFINE_INIT在FLUENT默认的初始化之后调用。有些流场变量无法在FLUENT默认的初始化中实现,则可以通过初始化函数实现。

/***********************************************************************/ /* UDF for initializing flow field variables */

/***********************************************************************/ #include \

DEFINE_INIT(my_init_function, domain) {

cell_t c;

Thread *thread; real xc[ND_ND];

thread_loop_c (thread,domain) {

begin_c_loop_all (c,thread) {

C_CENTROID(xc,c,thread);

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,thread) = 400.; else

C_T(c,thread) = 300.; }

end_c_loop_all (c,thread) } }

函数ND_SUM(a,b,c)用于求解函数参数列表中前面两参数(2D),或三参数(3D)之和。两嵌套的loop循环,能够扫描全场,给全场赋初始值。

7.5.6 壁面热流量

宏DEFINE_HEAT_FLUX能够定义壁面与邻近网格之间的热流量。热流量是按照下面两式计算,本例只求解qid 。

qid=cid[0]+cid[1]×C_T(c0, t0)-cid[2]×F_T(f, t)-cid[3]×pow(F_T(f,t), 4) qir=cir[0]+cir[1]×C_T(c0, t0)-cir[2]×F_T(f, t)-cir[3]×pow(F_T(f,t), 4) C语言源程序为:

/***********************************************************************/ /* UDF for specifying the diffusive heat flux between a wall and neighboring cells */

/***********************************************************************/ #include \

static real h = 0.; /* heat transfer coefficient (W/m^2 C) */ DEFINE_ADJUST(htc_adjust, domain) {

/* Define the heat transfer coefficient. */ h = 120; }

DEFINE_HEAT_FLUX(heat_flux, f, t, c0, t0, cid, cir) {

cid[0] = 0.; cid[1] = h; cid[2] = h; cid[3] = 0.; }

7.5.7 使用用户自定义标量进行后处理

宏DEFINE_ADJUST在每一步迭代执行前调用,所以可以在其中求解标量的微积分,更新与计算结果有关的流场变量等。下例中计算求解温度四次方的导数,并将值存于用户自定义标量之中,以用于后处理。

/***********************************************************************/ /* UDF for computing the magnitude of the gradient of T^4 */

/***********************************************************************/ #include \

/* Define which user-defined scalars to use. */ enum { T4,

MAG_GRAD_T4, N_REQUIRED_UDS

};

DEFINE_ADJUST(adjust_fcn, domain) {

Thread *t; cell_t c; face_t f;

/* Make sure there are enough user-defined scalars. */ if (n_uds < N_REQUIRED_UDS)

Internal_Error(\/* Fill first UDS with temperature raised to fourth power. */ thread_loop_c (t,domain) {

if (NULL != THREAD_STORAGE(t,SV_UDS_I(T4))) {

begin_c_loop (c,t) {

real T = C_T(c,t);

C_UDSI(c,t,T4) = pow(T,4.); }

end_c_loop (c,t) } }

thread_loop_f (t,domain) {

if (NULL != THREAD_STORAGE(t,SV_UDS_I(T4))) {

begin_f_loop (f,t) {

real T = 0.;

if (NULL != THREAD_STORAGE(t,SV_T)) T = F_T(f,t);

else if (NULL != THREAD_STORAGE(t->t0,SV_T)) T = C_T(F_C0(f,t),t->t0); F_UDSI(f,t,T4) = pow(T,4.); }

end_f_loop (f,t) } }

/* 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; 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)值的求解。由于函数中用到了用户自定义内存变量,所以需要事先激活用户自定义内存变量,内存变量个数不应小于所使用的内存变量个数。

…… 此处隐藏:2679字,全部文档内容请下载后查看。喜欢就下载吧 ……
中科大FLUENT讲稿 第七章 自定义函数(10).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/447792.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)