gpt4 book ai didi

c - 从 Windows 到 Linux 的语法错误 Fluent UDF

转载 作者:太空宇宙 更新时间:2023-11-04 11:46:31 25 4
gpt4 key购买 nike

我目前在 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;
}

最佳答案

cylinder_plume_inccoriolis_viscosity.c:33:9: error: z undeclared    (first use in this function)

变量z的相应声明被包裹在条件编译指令中:

    #ifdef RP_3D
real z = coords[2]; /* convenient names for coordinates: second horizontal */
#endif

目前还不清楚是什么导致宏 RP_3D 被定义(也影响了一些其他的声明),更不清楚为什么声明在它们的时候会受到条件编译指令的保护用途 不是。但似乎要使此代码正确编译,需要定义宏 RP_3D 或删除与其相关的条件编译指令。解决此问题的最快方法是放置一个

#define RP_3D 1

靠近 C 源文件的顶部。目前还不清楚它是否必须在 #include 指令之前,可以在它之前,或者不能在它之前——任何这些都是可能的,这取决于被包含的文件的内容。

或者,如果您必须支持未定义宏 RP_3D 的情况,也可以使用条件编译指令保护所有与 z 轴相关的变量的使用。也就是说,您可以确保在未声明变量时也不会引用它们,而不是确保始终声明变量。


warning: unused variable [...]

大体上,所有这些警告都可以忽略。


cylinder_plume_inccoriolis_viscosity.c:57:3: error: sal_val undeclared (first use in this function)

这没有意义。有问题的变量似乎已在三行前声明,将函数体中的用途(包括第 57 行)置于该声明的范围内。如果在解决其他错误后这种情况仍然存在,请确保这两个变量名称实际上是相同的。在其中寻找具有不同字符代码的相似字符,并检查其中的非打印字符。或者可能只是重新输入声明和用途。


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)
^

这些来自对函数 initTemperature() 的调用,出现在该函数的定义(或任何前向声明)之前。尽管它们是警告,而不是错误,但绝对应该加以解决。为此,在名为 DEFINE_INIT 的函数中,调用紧接出现在定义之前是值得怀疑的。将 DEFINE_INIT 函数移到函数 initTemperature() 之后应该可以解决该问题。


cylinder_plume_inccoriolis_viscosity.c:162:3: error: else without a previous if
else if (depth < DEPTH_BOTTOM)
^

此错误的原因尚不清楚。 else 子句确实 似乎与紧接其后的 if 语句相关联。尽管如此,最好的形式(对于所有 ifelse 语句)使用大括号括起来的 block 而不是裸语句。示例:

    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);
}

我不希望这能解决这个特定问题,但有理由希望它会导致发出不同的错误消息,更清楚地传达潜在问题的性质。

关于c - 从 Windows 到 Linux 的语法错误 Fluent UDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57465101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com