【发布时间】:2019-08-12 16:42:02
【问题描述】:
我目前在 ANSYS Fluent 中使用用户定义函数。这些是用 C 语言编写的,带有一些特定于 fluent 的宏。我有为 Windows 编写的工作 udf,现在正试图在 Linux 机器上运行它们。我收到以下错误。我对 Linux 和 C 相对较新,因此将不胜感激。
cylinder_plume_inccoriolis_viscosity.c:33:9: error: z undeclared (first use in this function)
if (z < anomaly_top_z + anomaly_accuracy_z) return 0;
^
cylinder_plume_inccoriolis_viscosity.c:25:7: warning: unused variable y [-Wunused-variable]
real y = coords[1]; // convenient names for coordinates: horizontal, vertical
^
cylinder_plume_inccoriolis_viscosity.c:24:10: warning: unused variable x [-Wunused-variable]
real x = coords[0];
^
cylinder_plume_inccoriolis_viscosity.c:25:7: warning: unused variable y [-Wunused-variable]
real y = coords[1]; // convenient names for coordinates: horizontal, vertical
^
cylinder_plume_inccoriolis_viscosity.c:24:10: warning: unused variable x [-Wunused-variable]
real x = coords[0];
^
cylinder_plume_inccoriolis_viscosity.c:57:3: error: sal_val undeclared (first use in this function)
sal_val = 7.0;
^
cylinder_plume_inccoriolis_viscosity.c:53:10: warning: unused variable depth [-Wunused-variable]
real depth = coords[2]; // yes, sir - the depth is always the second in coords array
cylinder_plume_inccoriolis_viscosity.c:65:2: warning: implicit declaration of function initTemperature [-Wimplicit-function-declaration]
initTemperature(domain);
cylinder_plume_inccoriolis_viscosity.c:67:6: warning: conflicting types for initTemperature [enabled by default]
void initTemperature(Domain * domain)
^
cylinder_plume_inccoriolis_viscosity.c:162:3: error: else without a previous if
else if (depth < DEPTH_BOTTOM)
^
这是没有错误行的代码:
#include "udf.h"
/*=======================================================================*/
#define DEPTH_TOP -0.2
#define DEPTH_BOTTOM -2.0
#define MU_TOP 18
#define MU_BOTTOM 1e-3
#define r 25.0
#define DIFFUSE_TOP 18
#define DIFFUSE_BOTTOM 1.43e-6
#ifdef RP_3D
double anomaly_top_z = -10.0;
double anomaly_bottom_z = 0.0;
double anomaly_accuracy_z = 0.1;
#endif
/*=======================================================================*/
enum
{salinity,
};
int is_in_plume(double coords[])
{
real x = coords[0];
real y = coords[1]; /* convenient names for coordinates: horizontal, vertical */
#ifdef RP_3D
real z = coords[2]; /* convenient names for coordinates: second horizontal */
#endif
if ((pow((x-125.0),2)+pow((y-125),2))>pow(r,2)) return 0;
if (z < anomaly_top_z + anomaly_accuracy_z) return 0;
if (z > anomaly_bottom_z - anomaly_accuracy_z) return 0;
return 1;
}
double temp_plume(double coords[])
{
real depth = coords[2]; /* yes, sir - the depth is always the second in coords array */
real temp_val;
if (is_in_plume(coords) == 1) {
temp_val = 298.15;
} else {
temp_val = 299.15;
}
return temp_val;
}
double sal_plume(double coords[])
{
real depth = coords[2]; /* yes, sir - the depth is always the second in coords array */
real sal_val;
if (is_in_plume(coords) == 1) {
sal_val = 7.0;
} else {
sal_val = 27.0;
}
return sal_val;
}
DEFINE_INIT(tempplume_init, domain)
{
initTemperature(domain);
}
void initTemperature(Domain * domain)
{
real coords[ND_ND]; /* will be used to get coordinares */
real temp_val; /* temporary variable */
Thread* thread;
cell_t cell;
int init_result = 1;
real depth;
if (init_result == -1) {
Error("Initialization failed. \n");
} else {
Message("Start looping. \n");
thread_loop_c(thread, domain) /* loop over all types ("threads") of cells in the domain */
{
Message("In loop over threads. \n");
begin_c_loop_all(cell, thread) /* loop over all cells in the thread */
C_CENTROID(coords, cell, thread); /* load coordinates of the cell into "coords" array; */
depth = coords[ND_ND - 1];
temp_val = temp_plume(coords);
C_T(cell, thread) = temp_val;
end_c_loop_all(cell, thread) /* end loop over cells */
} /* end loop over threads */
Message("Initialization complete. \n");
}
}
DEFINE_INIT(salplume_init, domain)
{
initSalinity(domain);
}
void initSalinity(Domain * domain)
{
real coords[ND_ND]; /* will be used to get coordinates */
real sal_val; /* temporary variable */
Thread* thread;
cell_t cell;
int init_result = 1;
real depth;
if (init_result == -1) {
Error("Initialization failed. \n");
} else {
Message("Start looping. \n");
thread_loop_c(thread, domain) /* loop over all types ("threads") of cells in the domain */
{
Message("In loop over threads. \n");
begin_c_loop_all(cell, thread) /* loop over all cells in the thread */
C_CENTROID(coords, cell, thread); /* load coordinates of the cell into "coords" array; */
depth = coords[ND_ND - 1];
sal_val = sal_plume(coords);
C_UDSI(cell, thread,salinity) = sal_val;
end_c_loop_all(cell, thread) /* end loop over cells */
} /* end loop over threads */
Message("Initialization complete. \n");
}
}
DEFINE_PROPERTY(salinity_dens,c,t)
{
real pho_w, sigma_t;
real sal = C_UDSI(c,t,salinity); /* in promilles */
real temp = C_T(c,t) - 273.15;
/* complex formula - see www.es.flinders.edu.au/~mattom/IntroOc/lecture03.html */
sigma_t = - 0.157406 + temp*(6.793952E-2 - temp*(9.095290E-3 - temp*(1.001685E-4 - temp*(1.120083E-6 - temp
*6.536332E-9)))) +
sal*(8.24493E-1 - temp*(4.0899E-3 - temp*(7.6438E-5 - temp*(8.2467E-7 - temp*5.3875E-9)))
- sqrt(sal)*(5.72466E-3 - temp*(1.0227E-4 - temp*1.6546E-6)) + sal*4.8314E-4);
pho_w = 1000.0 + sigma_t;
return pho_w;
}
DEFINE_PROPERTY(cell_viscosity, c, ct)
{
real mu_laminar; real depth;
real coords[ND_ND]; /* will be used to get coordinares */
C_CENTROID(coords, c, ct);
/*depth = coords[1]; */
depth = coords[ND_ND - 1]; /* -2 because y is vertical */
if (depth > DEPTH_TOP)
mu_laminar = MU_TOP;
else if (depth < DEPTH_BOTTOM)
mu_laminar = MU_BOTTOM;
else
mu_laminar = MU_BOTTOM + (depth - DEPTH_BOTTOM) *
(MU_TOP - MU_BOTTOM)/(DEPTH_TOP - DEPTH_BOTTOM);
return mu_laminar;
}
DEFINE_DIFFUSIVITY(sal_diffuse, c, ct, i)
{
real diffusivity; real depth;
real coords[ND_ND]; /* will be used to get coordinares */
C_CENTROID(coords, c, ct);
depth = coords[ND_ND - 2]; /* -2 because y is vertical */
if (depth > DEPTH_TOP)
diffusivity = DIFFUSE_TOP;
else if (depth < DEPTH_BOTTOM)
diffusivity = DIFFUSE_BOTTOM;
else
diffusivity = DIFFUSE_BOTTOM + (depth - DEPTH_BOTTOM) *
(DIFFUSE_TOP - DIFFUSE_BOTTOM)/(DEPTH_TOP - DEPTH_BOTTOM);
return diffusivity;
}
DEFINE_SOURCE(x_mom,c,t,dS,eqn)
{
real coords[ND_ND];
real y = coords[1];
real f; real theta; real source;
theta=10.0*3.14/180.0;
f=2.0*7.2921e-5*sin(theta)*50.0;
source=C_R(c,t)*f*C_V(c,t);
dS[eqn]=0.0;
return source;
}
DEFINE_SOURCE(y_mom,c,t,dS,eqn)
{
real coords[ND_ND];
real x = coords[0];
real f; real theta; real source;
theta=10.0*3.14/180.0;
f=-2.0*7.2921e-5*sin(theta)*50.0;
source=C_R(c,t)*f*C_U(c,t);
dS[eqn]=0.0;
return source;
}
【问题讨论】:
-
您是否在 2d 模式下运行软件?对于
real sal_val,您应该先运行DEFINE_INIT宏(据我记得它是在初始化时完成的),然后再运行计算。此外,我会仔细检查您的所有 UDF 是否正确挂钩(您的源项、材料属性等的动量方程 ..) -
我在 3D 中运行它。我将实现 DEFINE_INIT 宏。 UDF 在 Windows 上运行良好,但在 Linux 上编译时会显示许多语法错误。在 Linux 上运行时,我什至无法编译它以实际挂钩它。
-
好的,你如何重新编译你的 UDF ?来自 GUI/TUI 还是来自软件外部的 Makefile?
-
我最初与 ANSYS 的某个人一起解决了这个问题,她建议将我的 .c 文件保存在我正在使用的目录中,然后只编译在 Fluent 中编译的代码。加载 udf,然后创建 libudf 库。
-
是的,从头开始重建库是一个好的开始。一旦它被编译,然后在运行计算之前加载它。在 Ansys Fluent GUI 中完成所有这些工作会更容易
标签: c linux windows user-defined-functions fluent