UDF第3章写UDF详解

发布时间 : 星期五 文章UDF第3章写UDF详解更新完毕开始阅读

在Section 3.10.1-3.10.5中,提供了描述上面提到的五种不同的函数任务中每一种的UDF源代码例子。

3.10.1 返回值的函数(Function that Return a Value)

下面的UDF是一个返回值到FLUENT求解器的函数例子。名为cell_viscosity的函数计算了依赖温度的粘度值(mu_lam)并返回这个值到求解器。

/********************************************************************/ /* UDF that returns a value to the solver */ /* Specifies a temperature-dependent viscosity property */ /********************************************************************/#include \

DEFINE_PROPERTY(cell_viscosity, cell, thread) {

real mu_lam;

real temp = C_T(cell, thread);

if (temp > 288.) mu_lam = 5.5e-3; else if (temp > 286.)

mu_lam = 143.2135 - 0.49725 * temp; else

mu_lam = 1.;

return mu_lam; }

cell_viscosity使用了DEFINE_PROPERTY 宏(在Section 4.3.6中描述)来定义。DEFINE_PROPERTY返回一个udf.h中指定的real数据类型。两个real变量传入函数:通过函数计算的层流粘度mu_lam; 和C_T(cell,thread)的值,它是在考虑中的单元的温度值。温度值在它下降范围的基础上被检测,mu_lam的适当值被计算。在函数结尾,mu_lam的计

算值被返回。

3.10.2修改自变量的函数(Function that Modify an Argument)

下面的UDF是一个修改一个自变量的函数的例子。名字为user_rate的函数为一个两种气态物质的的简单系统产生一个自定义的体积反应速率。Real指针rr作为自变量传递给函数,指针指向的变量在函数内被修改。

/**************************************************************/ /* UDF that modifies one of its arguments */ /* Specifies a reaction rate in a porous medium */ /**************************************************************/

#include \

#define K1 2.0e-2 #define K2 5.

DEFINE_VR_RATE(user_rate, c, t, r, mole_weight, species_mf, rr, rr_t) {

real s1 = species_mf[0]; real mw1 = mole_weight[0];

if (FLUID_THREAD_P(t) && THREAD_VAR(t).fluid.porous) *rr = K1*s1/pow((1.+K2*s1),2.0)/mw1; else *rr = 0.; }

user_rate使用了DEFINE_VR _RATE宏(见Section 4.3.14)来定义。该函数执行一个当前考虑的单元是否在多孔区域的测试,这个反应速率只应用于多孔区域。real指针变量rr是一个传递给函数的自变量。UDF使用废弃操作符 * 分配反应速率值给废弃指针 *rr。指针rr指向的目标是设置反应速率。通过这个操作,存储在内存中这个指针上的字符的地址被改变

了,不再是指针地址本身。(关于废弃指针的详细内容见[3])。

3.10.3返回一个值和修改一个自变量的函数(Functions that Return a Value ans Modify an Argument)

下面的UDF是一个修改它的自变量并返回一个值到FLUENT求解器的函数例子。名字为user_swirl的函数修改ds自变量,指定旋转速度源项并返回它到求解器。 /**************************************************************/ /* UDF that returns a value and modifies an argument */ /* Specifies a swirl-velocity source term */ /**************************************************************/

#include \

#define OMEGA 50. /* rotational speed of swirler */

#define WEIGHT 1.e20 /* weighting coefficients in linearized equation */

DEFINE_SOURCE(user_swirl, cell, thread, dS, eqn) {

real w_vel, x[ND_ND], y, source;

C_CENTROID(x, cell, thread); y = x[1];

w_vel = y*OMEGA;

source = WEIGHT*(w_vel - C_WSWIRL(cell,thread)); dS[eqn] = -WEIGHT;

return source; }

/* linear w-velocity at the cell */

user_swirl使用DEFINE_SOURCE宏来定义(在Section 4.3.8中描述)。DEFINE_SOURCE返回一个在udf.h中指定的数据类型。函数采用自变量ds(它是数组的名字)并设置由eqn指定的元素为关于w速度(w_vel)导数的值。(这是z动量方程源项)。这个函数也计算了旋转速度源项的值source,并返回这个值到求解器。 3.10.4修改FLUENT变量的函数

下面的UDF是一个修改存储在内存中FLUENT变量的函数例子。名字为inlet_x_velocity的函数使用F_PROFILE(a Fluent Inc. provided utility(应用程序)“Translate by 金山词霸”)来修改存储在内存中的x速度分布边界条件。

/********************************************************************/ /* UDF that modifies a FLUENT solver variable */ /* Specifies a steady-state velocity profile boundary condition */ /********************************************************************/

#include \

DEFINE_PROFILE(inlet_x_velocity, thread, index) {

real x[ND_ND]; real y; face_t f;

begin_f_loop(f, thread) {

F_CENTROID(x,f,thread); y = x[1];

F_PROFILE(f, thread, index) = 20. - y*y/(.0745*.0745)*20.; }

end_f_loop(f, thread) }

/* this will hold the position vector */

联系合同范文客服:xxxxx#qq.com(#替换为@)