【问题标题】:Syntax Errors Fluent UDF from Windows to Linux语法错误 Fluent UDF 从 Windows 到 Linux
【发布时间】: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


【解决方案1】:
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 函数 after 函数 initTemperature() 应该可以解决该问题。


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

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

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

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

【讨论】:

  • RP_3D是软件在3d模式下运行时自动设置的
  • @Scab, RP_3D 就像宏一样使用。如果它实际上是一个变量,那将不会像您想的那样起作用。条件编译指令在 compile 时进行评估,因此不能考虑任何因运行而异的信息。
  • 当你编译 UDF 时,你实际上可以在 3d 或 2d 模式下编译它。您可以从软件的 GUI 执行此操作,也可以使用适当的 Makefile 在其外部构建它(示例通常由 Ansys 提供)
  • 好吧,解决与此相关的错误的第三种方法是使用条件编译指令来保护 z 轴相关变量的所有 使用。就目前的代码而言,变量的声明有时会被禁止,有时不会,但程序总是会尝试使用它们。我会将其添加到答案中。
  • 更清楚地说,如果 OP 从 GUI 或 TUI 编译它的 UDF,如果软件以 3d 模式启动,则设置 RP_3D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-10
  • 1970-01-01
  • 1970-01-01
  • 2013-05-09
  • 1970-01-01
  • 2021-03-16
相关资源
最近更新 更多