分享一些常用的UDF

下面分享一些简单的 udf,带领小白快速入门。

1、Lee

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "udf.h"
#include "sg_mphase.h" //包含体积分数的宏
#define T_SAT 30.27 //饱和温度373.15 K
#define LAT_HT 1.e3 //汽化潜热1000 J/kg


// 液相质量源项
DEFINE_SOURCE(liq_src, cell, pri_th, dS, eqn)
{
Thread *mix_th, *sec_th;
real m_dot_l;// 定义相变质量kg/(m2 s)
// mix_th存储主相的Thread指针;sec_th存储次相指针,注意ID
mix_th = THREAD_SUPER_THREAD(pri_th); //指向混合区域的主相
sec_th = THREAD_SUB_THREAD(mix_th, 1); //指向单相区的气相指针
thi_th = THREAD_SUB_THREAD(mix_th, 2); //指向单相区的液相指针
// 温度大于饱和温度,表示为蒸发
if (C_T(cell, sec_th) >= T_SAT)
{
//计算蒸发量,注意液相质量流量为负值,0.1为coeff值
m_dot_l = -0.25 * C_VOF(cell, sec_th) * C_R(cell, sec_th) * fabs(C_T(cell, sec_th) - T_SAT) / T_SAT;
//对体积分数求导
dS[eqn] = -0.25 * C_R(cell, sec_th) * fabs(C_T(cell, sec_th) - T_SAT) / T_SAT;
}
else
{
//计算冷凝量,液相质量分数为正值
m_dot_l = 0.1 * C_VOF(cell, sec_th) * C_R(cell, sec_th) * fabs(T_SAT - C_T(cell, sec_th)) / T_SAT;
//若对体积分数求导得到的值为正,干脆直接赋零值
dS[eqn] = 0.;
}
return m_dot_l;
}



// 气相质量源项,解释同上
DEFINE_SOURCE(vap_src, cell, sec_th, dS, eqn)
{
Thread *mix_th, *pri_th;
real m_dot_v;
mix_th = THREAD_SUPER_THREAD(pri_th); //指向混合区域的主相
sec_th = THREAD_SUB_THREAD(mix_th, 1); //指向单相区的气相指针
thi_th = THREAD_SUB_THREAD(mix_th, 2); //指向单相区的液相指针
if (C_T(cell, thi_th) <= T_SAT)
{
m_dot_v = 0.1 * C_VOF(cell,thi_th) * C_R(cell, thi_th) * fabs(C_T(cell, thi_th) - T_SAT) / T_SAT;
dS[eqn] = 0.;
}
else
{
m_dot_v = -0.1 * C_VOF(cell, thi_th) * C_R(cell, thi_th) * fabs(T_SAT - C_T(cell, thi_th)) / T_SAT;
dS[eqn] = -0.1 * C_R(cell, thi_th) * fabs(C_T(cell, thi_th) - T_SAT) / T_SAT;
}
return m_dot_v;
}


//混合相的能量源项
DEFINE_SOURCE(enrg_src, cell, mix_th, dS, eqn)
{
Thread *pri_th, *sec_th, *thi_th;
real m_dot;
pri_th = THREAD_SUB_THREAD(mix_th, 0);
sec_th = THREAD_SUB_THREAD(mix_th, 1);
thi_th = THREAD_SUB_THREAD(mix_th, 2);
if (C_T(cell, mix_th) >= T_SAT)
{
// 得到蒸发量,Lee模型
m_dot = -0.25 * C_VOF(cell, sec_th) * C_R(cell, sec_th) *
fabs(C_T(cell, sec_th) - T_SAT) / T_SAT;
//对温度求导,若为负则保留,否则赋值零
dS[eqn] = -0.1 * C_VOF(cell, sec_th) * C_R(cell, sec_th) / T_SAT;
}
else
{
//得到冷凝量
m_dot = 0.1 * C_VOF(cell, thi_th) * C_R(cell, thi_th) *
fabs(T_SAT - C_T(cell, thi_th)) / T_SAT;
dS[eqn] = -0.1 * C_VOF(cell, thi_th) * C_R(cell, thi_th) / T_SAT;
}

// 返回热量值,利用潜热与质量流量的乘积得到
return LAT_HT * m_dot;
}

2、ke 模型

2.1 ke1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "udf.h"

#define Cu 0.09
#define Us 2.2
#define zs 10
#define a 0.15
#define ZG 350

DEFINE_PROFILE(k_profile,t,i)
{
real z,x[ND_ND],Uz,Iz;
face_t f;

begin_f_loop(f,t)
{
F_CENTROID(x,f,t);
z = x[2];
Uz = Us*pow(z/zs,a);
Iz = 0.1*pow(z/ZG,-a-0.05);

F_PROFILE(f,t,i) = pow(Iz*Uz,2);
}
end_f_loop(f,thread)
}

DEFINE_PROFILE(e_profile,t,i)
{
real z,x[ND_ND],Uz,Iz,kz;
face_t f;

begin_f_loop(f,t)
{
F_CENTROID(x,f,t);
z = x[2];
Uz = Us*pow(z/zs,a);
Iz = 0.1*pow(z/ZG,-a-0.05);
kz = pow(Iz*Uz,2);

F_PROFILE(f,t,i) = pow(Cu,0.5)*kz*Us/zs*a*pow(z/zs,a-1);
}
end_f_loop(f,thread)
}

2.2 ke2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "udf.h"

#define Cu 0.09
#define Us 2.2
#define zs 10
#define a 0.10
#define ZG 250

DEFINE_PROFILE(k_profile,t,i)
{
real z,x[ND_ND],Uz,Iz;
face_t f;

begin_f_loop(f,t)
{
F_CENTROID(x,f,t);
z = x[2];
Uz = Us*pow(z/zs,a);
if(z<5)
Iz = 0.18;
else
Iz = 0.1*pow(z/ZG,-a-0.05);
F_PROFILE(f,t,i) = 1.5*pow(Uz*Iz,2);
}
end_f_loop(f,thread)
}

DEFINE_PROFILE(e_profile,t,i)
{
real z,x[ND_ND],Uz,Iz,kz,lz;
face_t f;

begin_f_loop(f,t)
{
F_CENTROID(x,f,t);
z = x[2];
Uz = Us*pow(z/zs,a);
if(z<5)
Iz = 0.18;
else
Iz = 0.1*pow(z/ZG,-a-0.05);
kz = 1.5*pow(Uz*Iz,2);
F_PROFILE(f,t,i) = F_PROFILE(f,t,i) = pow(Cu,0.5)*kz*Us/zs*a*pow(z/zs,a-1);
}
end_f_loop(f,thread)
}

3、质量入口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "udf.h"
DEFINE_PROFILE(inlet_mlih2,thread,i)
{
face_t f;
real flow_time = CURRENT_TIME;
begin_f_loop(f,thread)
{
if(flow_time <= 38)
F_PROFILE(f,thread,i) = 4.76*0.75;
else
F_PROFILE(f,thread,i) = 0;
}
end_f_loop(f,thread)
}
DEFINE_PROFILE(inlet_mh2,thread,i)
{
face_t f;
real flow_time = CURRENT_TIME;
begin_f_loop(f,thread)
{
if(flow_time <= 38)
F_PROFILE(f,thread,i) = 4.76*0.25;
else
F_PROFILE(f,thread,i) = 0;
}
end_f_loop(f,thread)
}

4、速度入口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "udf.h"
DEFINE_PROFILE(inlet_x,thread,index)
{
real y,v0,h0;
real x[ND_ND];
face_t f;
v0=2.2;
h0=10;
begin_f_loop(f,thread)
{
F_CENTROID(x,f,thread);
y=x[2];
F_PROFILE(f,thread,index) = v0*pow(y/h0,0.10);
}
end_f_loop(f,thread)

}

5、时间步长

1
2
3
4
5
6
7
8
9
10
11
12
#include "udf.h"

DEFINE_DELTAT(mydeltat,domain)
{
real time_step;
real flow_time = CURRENT_TIME;
if(flow_time<120)
time_step = 1;
else
time_step = 0.0005;
return time_step;
}

分享一些常用的UDF
http://example.com/2024/03/17/010 常用的udf/
作者
DB
发布于
2024年3月17日
许可协议