fluent udf的相关属性定义,和属性文件读取(并行)

FLUENT提供了多种指定材料特性的方法,包括多项式函数、分段线性函数等。用户定义的函数也可以用来指定材料属性和可变的剖面。然而,使用表格式的实验数据或复杂的函数,不能很容易地分析表达是困难的。

附加的用户定义函数演示了如何将制表数据读入FLUENT并用于指定UDF中的重要属性。这个例子对应于把粘度指定为温度的函数。表格数据应该是一个ascii文件(两列格式),文件中的行数在UDF中指定。在这个例子中,数据文件被称为“viscosity.dat”,有53行(对)数据。粘度将在表格值之间线性插值。


#include "udf.h"

int NPts_mu = 53; /* number of entries in viscosity table */

/* Locate the place in the interpolation vector using bisection algorithm*/
int locate(float xx[], int n, float x)
{
int j = 0;
int jm = 0;
int jl = 0;
int ju = n+1;
int ascnd = 0;

ascnd = (xx[n] >= xx[1]);
while (ju-jl > 1)
{
jm = (ju+jl) >> 1;
if (x >= xx[jm] == ascnd)
jl = jm;
else
ju = jm;
}

if (x == xx[1])
j = 1;
else if (x == xx[n])
j = n-1;
else
j = jl;

return j;
}

float *temp_vec_mu,*visc_vec;
#define FAST_LOOKUP TRUE /* use bisection algorithm for interpolation */
#define TABLE_MESSAGES TRUE
#define DISPLAY_TABLES TRUE

/* Obtaine mu given temperature */
float get_mu_from_T(float xdata)
{
int i = 0;
int j = 0;
float xL,xU,ydata;

#if FAST_LOOKUP
j = locate(temp_vec_mu,NPts_mu,xdata);
xL = temp_vec_mu[j];
xU = temp_vec_mu[j+1];
ydata = visc_vec[j] + (xdata-xL)/(xU-xL)*( visc_vec[j+1] - visc_vec[j] );
#else
for ( i=1; i<NPts_mu ;i++ )
{
xL = temp_vec_mu[i];
xU = temp_vec_mu[i+1];
if ( (xdata>=xL)&&(xdata<=xU) )
{
ydata = visc_vec[i] + (xdata-xL)/(xU-xL)*( visc_vec[i+1] - visc_vec[i] );
break;
}
}
#endif

if ( xdata>temp_vec_mu[NPts_mu] )
{
#if TABLE_MESSAGES
Message("n temperature is above the bound of visc-array n");
#endif
ydata = visc_vec[NPts_mu];
}
if ( xdata<temp_vec_mu[1] )
{
#if TABLE_MESSAGES
Message("n temperature is below the bound of visc-array n");
#endif
ydata = visc_vec[1];
}

return ydata;
}

/* Read in the data file containing viscosity as a function of temperature */
DEFINE_ON_DEMAND(read_viscosity)
{
int i = 0;
float xdata,ydata;
FILE* fp;

fp = fopen("viscosity.dat","r");
if ( fp!=NULL )
{
#if !RP_NODE
Message(" n");
Message("Reading file viscosity.dat n");
Message(" n");
#endif
}
else
{
#if !RP_NODE
Message(" n");
Message("Error opening file n");
Message(" n");
#endif
}

temp_vec_mu = (float *) malloc(NPts_mu*sizeof(float));
visc_vec = (float *) malloc(NPts_mu*sizeof(float));

if ( (temp_vec_mu==NULL)||(visc_vec==NULL) )
{
#if !RP_NODE
Message("Memory allocation error n");
#endif
}

for ( i=1;i<=NPts_mu;i++ )
{
fscanf(fp,"%f %e n",&xdata,&ydata);
temp_vec_mu[i] = xdata;
visc_vec[i] = ydata;
#if DISPLAY_TABLES
#if !RP_NODE
Message("%.1f %e n",temp_vec_mu[i],visc_vec[i]);
#endif
}
#else
}
#if !RP_NODE
Message(" n");
#endif
#endif

fclose(fp);
}

/* Interpolate viscosity from look-up table and assign to cell value */
DEFINE_PROPERTY(viscosity,c,t)
{
float mu_lam;
float temp = C_T(c,t);

/* interpolate mu_lam */
mu_lam = get_mu_from_T(temp);

return mu_lam;
}

/*
Sample data file "viscosity.dat":

500.0 3.03460E-05
700.0 3.94144E-05
1000.0 5.07830E-05
1200.0 5.71540E-05
1500.0 6.62424E-05
1800.0 7.51254E-05
2100.0 8.19802E-05
2400.0 9.23992E-05
2700.0 1.00744E-04
3000.0 1.08866E-04
3500.0 1.21942E-04
4000.0 1.34458E-04
4500.0 1.46520E-04
5000.0 1.58330E-04
5500.0 1.70196E-04
6000.0 1.82438E-04
6500.0 1.94962E-04
7000.0 2.06702E-04
7200.0 2.10910E-04
7500.0 2.16686E-04
8000.0 2.25596E-04
8500.0 2.34400E-04
9000.0 2.43092E-04
9500.0 2.51136E-04
10000.0 2.57698E-04
10500.0 2.61604E-04
11000.0 2.61396E-04
11500.0 2.55446E-04
12000.0 2.42620E-04
12500.0 2.22876E-04
13000.0 1.97606E-04
13500.0 1.69612E-04
14000.0 1.41774E-04
14500.0 1.16876E-04
15000.0 9.62224E-05
15500.0 8.01648E-05
16000.0 6.83390E-05
16500.0 6.00368E-05
17000.0 5.44842E-05
17500.0 5.09870E-05
18000.0 4.89790E-05
18500.0 4.80194E-05
19000.0 4.80168E-05
19500.0 4.83648E-05
20000.0 4.86812E-05
20500.0 4.89374E-05
21000.0 4.89330E-05
21500.0 4.84956E-05
22000.0 4.75116E-05
22500.0 4.59512E-05
23000.0 4.51756E-05
23500.0 4.43232E-05
24000.0 4.40476E-05
*/
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Fluent UDF并行读写文件可以通过以下步骤实现: 1. 定义一个文件句柄,指向你要读写的文件。可以使用标准的 C 文件 I/O 函数,如 fopen、fclose、fread 和 fwrite。 2. 使用 Fluent UDF 函数 hook_cold_init 来打开文件,并将文件句柄存储在一个全局变量中。这个函数只会在 Fluent 初始化时调用一次。 3. 在 Fluent UDF 函数 hook_loop 中,使用 Fluent API 函数,如 RP_Get_Real,来获取模拟时间步长的当前值。根据需要,可以将此值与一个预定义的时间间隔进行比较,以判断是否应该读写文件。 4. 如果需要读写文件,则使用 Fluent UDF 函数 hook_compute_fluxes 来读写数据。这个函数可以并行执行,因此可以在多个处理器上同时读写文件。 5. 在 Fluent UDF 函数 hook_cold_shutdown 中关闭文件句柄,并释放任何分配的内存。 以下是一个示例代码,用于并行读取和写入一个文本文件: ``` #include "udf.h" #include <stdio.h> #include <stdlib.h> #define FILENAME "data.txt" #define INTERVAL 10.0 FILE *fp; DEFINE_ON_DEMAND(open_file) { fp = fopen(FILENAME, "w"); if (fp == NULL) { Message("Error opening file.\n"); return; } fclose(fp); } DEFINE_ON_DEMAND(close_file) { if (fp != NULL) { fclose(fp); } } DEFINE_EXECUTE_AT_END(write_file) { real time = RP_Get_Real("flow-time"); if (time >= INTERVAL) { int i, myid, nproc; char filename[256]; sprintf(filename, "%s.%d", FILENAME, PRF_GRP_ID()); fp = fopen(filename, "w"); if (fp == NULL) { Message("Error opening file.\n"); return; } myid = PRF_GRP_ID(); nproc = PRF_NPROCS(); for (i = 0; i < 1000; i++) { if (i % nproc == myid) { fprintf(fp, "%d\n", i); } } fclose(fp); } } DEFINE_ON_DEMAND(read_file) { char line[256]; fp = fopen(FILENAME, "r"); if (fp == NULL) { Message("Error opening file.\n"); return; } while (fgets(line, sizeof(line), fp) != NULL) { Message("Line: %s", line); } fclose(fp); } ``` 在此示例中,我们使用了三个 Fluent UDF 函数:open_file、close_file 和 read_file。这些函数是通过在 Fluent 命令行中输入相应的文本来调用的,例如: ``` udf > define_on_demand open_file udf > define_on_demand close_file udf > define_on_demand read_file ``` 我们还定义了一个新的 Fluent UDF 函数 write_file,它使用 RP_Get_Real 函数获取当前模拟时间步长,然后在指定的时间间隔后并行写入数据到一个新的文件中。每个处理器只会写入一部分数据,以确保数据的一致性。 要使用 write_file 函数,请将以下文本添加到 Fluent 的启动脚本中: ``` (rp-var-define 'udf/execute-at-end-functions '((write_file))) ``` 这将确保 write_file 函数在每个时间步长结束时被调用。 请注意,此示例仅演示了如何并行读取和写入一个文本文件。如果你需要读写其他类型的文件,例如二进制文件或 HDF5 文件,你需要使用相应的库函数来实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值