/
******************************************************************************
* Created on : 2017-5-30
17:12
* author :
硫酸亚铜
*需要在solution->Run
Calculation->Options勾选Data Sampling for Time
Statistics
*否则会报错
******************************************************************************/
#include "udf.h"
#define FLUID_ID 7 / *需要计算平均值的计算域的ID*/
DEFINE_ON_DEMAND(averageValue)
{
#if !RP_HOST
Domain* domain = Get_Domain(1); / *获取计算域*/
Thread* t;
/ *获取指针*/
cell_t
c; /
*标定单元*/
#else
int i; /
*用来循环计数*/
#endif
#if !RP_NODE
FILE *fp = NULL;
/ *定义一个文件指针*/
char filename[] = "average.txt";
/ *文件名*/
#endif
#if PARALLEL
int size; /
*传递变量的数据,实际上计算节点上的单元数*/
real* pAverage;
/ *pAverage表示压力平均*/
real* uAverage;
/ *uAverage表示x方向的速度分量*/
real* vAverage;
/ *vAverage表示y方向的速度分量*/
real* wAverage;
/ *wAverage表示z方向的速度分量*/
real position[ND_ND]; /
*记录单元的位置*/
int pe; / *用来保存主机和计算节点*/
#endif
#if !RP_HOST
t = Lookup_Thread(domain, FLUID_ID);
#endif
#if !RP_NODE
if ((fp = fopen(filename, "w")) == NULL)
/ *判断文件是否打开成功*/
Message("\n Warning: Unable to open %s for writing\n",
filename); /
*文件打开失败*/
else
Message("\nWriting averageValue to %s...", filename); /
*打开文件成功*/
#endif
#if !PARALLEL /
*在串行情况下,输出文件*/
begin_c_loop(c, t)
fprintf(fp,
"average pressure:%g\n \
The average velocity in the x direction is:%g \n \
The average velocity in the y direction is:%g \n \
The average velocity in the x direction is:%g \n", \
C_STORAGE_R(c, t, SV_P_MEAN) / delta_time_sampled,
\
C_STORAGE_R(c, t, SV_U_MEAN) / delta_time_sampled,
\
C_STORAGE_R(c, t, SV_V_MEAN) / delta_time_sampled,
\
C_STORAGE_R(c, t, SV_W_MEAN) /
delta_time_sampled);
end_c_loop(c,t)
#endif
#if RP_NODE
size=THREAD_N_ELEMENTS_INT(t); / *得到分块计算域内单元的数目*/
pAverage = (real *)malloc(size * sizeof(real)); /
*为平均压力分配空间*/
uAverage = (real *)malloc(size * sizeof(real)); /
*速度在x方向分量的平均值分配空间*/
vAverage = (real *)malloc(size * sizeof(real)); /
*速度在y方向分量的平均值分配空间*/
wAverage = (real *)malloc(size * sizeof(real)); /
*速度在z方向分量的平均值分配空间*/
begin_c_loop_int(c, t)
{
pAverage[c] = C_STORAGE_R(c, t, SV_P_MEAN) /
delta_time_sampled;
/ *得到压力的平均值*/
uAverage[c] = C_STORAGE_R(c, t, SV_U_MEAN) /
delta_time_sampled;
/ *得到速度x方向的速度分量的平均值*/
vAverage[c] = C_STORAGE_R(c, t, SV_V_MEAN) /
delta_time_sampled;
/ *得到速度y方向的速度分量的平均值*/
wAverage[c]
= C_STORAGE_R(c, t, SV_W_MEAN) /
delta_time_sampled;
/ *得到速度z方向的速度分量的平均值*/
}
end_c_loop_int(c, t)
/ ***************************************
*判断是否是0计算节点
*当前节点为0计算节点,则pe为node_host,
*通过0计算节点向主机节点发送数据
*当前节点为非0计算节点,则pe为node_zero
*其他计算节点向0计算节点发送数据
****************************************/
pe = (I_AM_NODE_ZERO_P) ? node_host: node_zero;
/ ********************************************
*当前节点为0节点,则当前节点向主机发送数据
*当前节点为非0节点,则当前节点向0节点发送数据
*********************************************/
PRF_CSEND_INT(pe, &size, 1, myid);
PRF_CSEND_REAL(pe, pAverage, size, myid);
PRF_CSEND_REAL(pe, uAverage, size, myid);
PRF_CSEND_REAL(pe, vAverage, size, myid);
PRF_CSEND_REAL(pe, wAverage, size, myid);
/ *发送数据以后,释放内存*/
free(pAverage);
free(uAverage);
free(vAverage);
free(wAverage);
if (I_AM_NODE_ZERO_P) /
*如果是0计算节点*/
{
compute_node_loop_not_zero(pe) / *循环非0节点*/
{
PRF_CRECV_INT(pe, &size, 1, pe);
/ *0计算节点从其他计算节点接收各分块节点的单元数*/
pAverage = (real *)malloc(size * sizeof(real)); /
*为平均压力分配空间*/
uAverage = (real *)malloc(size * sizeof(real)); /
*速度在x方向分量的平均值分配空间*/
vAverage = (real *)malloc(size * sizeof(real)); /
*速度在y方向分量的平均值分配空间*/
wAverage = (real *)malloc(size * sizeof(real)); /
*速度在z方向分量的平均值分配空间*/
/ *0计算节点从其他计算节点接收各结算节点的单元对应的变量*/
PRF_CRECV_REAL(pe, pAverage, size, pe);
PRF_CRECV_REAL(pe, uAverage, size, pe);
PRF_CRECV_REAL(pe, vAverage, size, pe);
PRF_CRECV_REAL(pe, wAverage, size, pe);
PRF_CSEND_INT(node_host, &size, 1, myid); /
*0计算节点将从各分块节点单元数发送给主机节点*/
/ *0计算节点将从其他计算节点接收各结算节点的单元对应的变量发送给主机节点*/
PRF_CSEND_REAL(node_host, pAverage, size, myid);
PRF_CSEND_REAL(node_host, uAverage, size, myid);
PRF_CSEND_REAL(node_host, vAverage, size, myid);
PRF_CSEND_REAL(node_host, wAverage, size, myid);
/ *释放内存*/
free((char *)pAverage);
free((char *)uAverage);
free((char *)vAverage);
free((char *)wAverage);
}
}
#endif
#if RP_HOST
compute_node_loop(pe) /
*仅仅作为一个循环计数器的角色*/
{
/ *接收各节点的数据*/
PRF_CRECV_INT(node_zero, &size, 1, node_zero);
pAverage = (real *)malloc(size * sizeof(real));
uAverage = (real *)malloc(size * sizeof(real));
vAverage = (real *)malloc(size * sizeof(real));
wAverage = (real *)malloc(size * sizeof(real));
PRF_CRECV_REAL(node_zero, pAverage, size, node_zero);
PRF_CRECV_REAL(node_zero, uAverage, size, node_zero);
PRF_CRECV_REAL(node_zero, vAverage, size, node_zero);
PRF_CRECV_REAL(node_zero, wAverage, size, node_zero);
/ *将数据写入文件当中*/
for (i=0; i
fprintf(fp,
"Time Sampled(s):%g \
\naverage pressure:%g
\
\nThe average velocity in the x direction is:%g \
\nThe average velocity in the y direction is:%g \
\nThe
average velocity in the x direction is:%g \n", \
delta_time_sampled, \
pAverage[i], \
uAverage[i], \
vAverage[i], \
wAverage[i]);
/ *释放内存*/
free(pAverage);
free(uAverage);
free(vAverage);
free(wAverage);
}
#endif
#if !RP_NODE
fclose(fp); / *关闭文件*/
Message("Done\n"); / *输出关闭文件完成*/
#endif
}