Fluids

Fluids

Need help, cpp compile for ansys fluent UDF

    • xin.yang
      Subscriber

      My problem is:

      (1) my udf is writte in CPP since I need to use vector type in my code.

      (2) Ansys fluent 19.2 and 20.2.

      (3) 2D parallel.

      (4) Fluent error: 

      C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\sys/stat.h(239): error C2375: '_stat64i32': redefinition; different linkage

      C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\sys/stat.h(185): note: see declaration of '_stat64i32'

      C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\sys/stat.h(242): error C2065: '_stat64i32': undeclared identifier

       

      (5) If I remove '#include ' and all other codes related to vector in my code. The cpp file is able to be compiled.

       

      So, does anyone know how to resolve this issue?

       

      Many thanks in advance.

      Bill.

    • Rob
      Ansys Employee

      Is that from the built in compiler? Please confirm the versions as there isn't a 20.2 build, we have 2020R2, 2021R2 and 2022R2 amongst others. 

    • xin.yang
      Subscriber

      It is not the built in compiler. I also tried built in complier. It doesnt work as well. 

      It's 2020R2.

    • xin.yang
      Subscriber

      Some updates from me.

      1, I think it may be because of ansy fluent does not fully support cpp language. As far as I know all the example udfs in fluent are based on c language.

      2, If ansys could tell us how to properly setup visual studio to make it fully support fluent udf that would be great. Could you provide any guidance on this? I think there is little information we can find in ansys help document or google for this.

      3, A possible solution from me: fully rewritten the cpp code to remove all the data types and functions that need call cpp header files, for example, iostream, string, vector, memory. I have fully rewritten my cpp code to replace all these functions by c-based code and keep the cpp based classes. The udf is running OK. Although this can resolve the issue, it is not the perfect way to do.

    • Rob
      Ansys Employee

      You can use C++ if the libraries are linked correctly on Windows, how is something you need to take up with Microsoft. Similarly setting up VS Studio. The reason we added the CLANG compiler was to avoid the whole VS Studio mess, a mess that changes with every update from Microsoft.  

       

    • DrAmine
      Ansys Employee

       

       

      All is a matter of the linked libraries.

       

      Something like below can be compiler and exectued in Ansys Fluent under Windows using the built-in compiler:

       

      #include “udf.h”
      #include vector (put this vector between < and > )

      DEFINE_ON_DEMAND(vector_test)
      {
        std::vector vec = {1.0,0.0,0.0};

        Message0(“Vector = %f %f %f\n”, vec[0], vec[1], vec[2]);
      }

       

       

    • xin.yang
      Subscriber

      Thanks for your reply, Dr Amine and Rob.

      1, I suppose you are suggesting using built-in compiler for cpp code needs cpp-based header file.

      2, I just tried to compile the 'define_on_demand' file, there are lots of errors from fluent  2020R2.

      3, I tried the following three codes, all returns lots of errors from fluent console:

      Code 1:

      #include "udf.h"
      #include \    // right?
      DEFINE_ON_DEMAND(on_demand_calc)
      {
          std::vector kk = { 1.0,2.0,3.0 }; // This is not typical vector initialization, need be vector
          Message("Vector={%lf, %lf, %lf}\n", kk[0], kk[1], kk[2]); // no Message0
      }

      Code 2:

      #include "udf.h"

      DEFINE_ON_DEMAND(on_demand_calc)
      {
          std::vector kk = { 1.0,2.0,3.0 }; 
          Message("Vector={%lf, %lf, %lf}\n", kk[0], kk[1], kk[2]); // no Message0
      }

      Code 3:

      #include "udf.h"
      #include
      DEFINE_ON_DEMAND(on_demand_calc)
      {
          std::vector kk = { 1.0,2.0,3.0 }; 
          Message("Vector={%lf, %lf, %lf}\n", kk[0], kk[1], kk[2]); // no Message0
      }

      4, The following code is working OK using in-built compiler after I remove 'vector' and use 'array'. But obviously, this is not using vector.

      Code:

      #include "udf.h"

      DEFINE_ON_DEMAND(on_demand_calc)
      {
          double kk[3] = { 1.0,2.0,3.0 };
          Message("Vector={%lf, %lf, %lf}\n", kk[0], kk[1], kk[2]);
      }

    • xin.yang
      Subscriber

      Sorry, there are a few display issue to input symbols in this forum. For code 2 and 3, i corrected the vector initialization using "std::vector" in replace of "std::vector"

    • xin.yang
      Subscriber

    • DrAmine
      Ansys Employee

      The code I pasted did work for me using built.in compiler on Windows 10 machine. You can avoid all this by sticking to what you refer as code 3 (so standard C).

Viewing 9 reply threads
  • You must be logged in to reply to this topic.