We have an exciting announcement about badges coming in May 2025. Until then, we will temporarily stop issuing new badges for course completions and certifications. However, all completions will be recorded and fulfilled after May 2025.
Fluids

Fluids

Topics related to Fluent, CFX, Turbogrid and more.

UDF not extracting data from 3D model accurately in Fluent

    • tmkhare
      Subscriber

      Hello all,

      I'm trying to write a simple UDF to extract the face area magnitude and outlet flow rate for the outlet face of a straight pipe. It is a transient simulation. The problem I'm facing is that the UDF is giving output of 0.0 for both parameters mentioned above, randomly for certain timesteps. I agree that the outlet flow rate value can be zero at certain time steps but the face area magnitude cannot be zero ever, and that's what is puzzling me as to why it is giving a 0.0 value as output.

      The UDF is executed at the end of every time step. I print the values extracted by the UDF in a text file at every time step. Eventually I want to use these values extracted by the UDF for calculations performed by a 3-element Windkessel model at every time step, and therefore it is important that the UDF extracts correct values at every time step.

      Pipe diameter = 4cm, length = 30cm
      Inlet BC: transient velocity profile
      Outlet BC: transient pressure profile
      Time step = 0.001 s

      I have attached photos of the geometry, the inlet and outlet boundary conditions profiles, the UDF code written in C that I'm using, and a photo of the output file which shows the erroneous output values (highlighted in yellow). I would really appreciate if someone can point what is going wrong with the UDF.

      You can access th UDF code written in C from the link: UDF Code for Outlet Face Data Extraction

      Looking forward to hearing from you all.

      Thank you.

       

    • Rob
      Forum Moderator

      Staff aren't allowed to follow links or download files. I've not seen any similar issues so suspect there's something in your code. Why are you using a UDF rather than an Expression/Report Definition and monitor? 

      • tmkhare
        Subscriber

        Hi Rob,

        I'm using a UDF to extract the flow rate information for outlet face as a trial. I eventually want to implement a 3-element Windkessel model at the outlet using a UDF and that model requires flow rate information from outlet face as an input at every time step. Therefore, this is a trial run to see if I can extract values of flow rate correctly using a UDF. Next step would be to jump to a more complex UDF. Considering you can't download the UDF code, I have inserted it below. Please let me know if you find anything is wrong in it. Thank you.

        #include "udf.h"
        #include

        DEFINE_EXECUTE_AT_END(print_outlet_flow)
        {
            Domain   *d;
            Thread   *t;
            face_t    f;
            real      area_vec[ND_ND];
            real      face_area;
            real      vol_flow;
            real      total_area     = 0.0;
            real      total_vol_flow = 0.0;
            static int first = 1;
            FILE     *fp;
            int       outlet_zone_id = 6;  /*zone-ID of outlet face*/

            /* get the domain and the outlet thread */
            d = Get_Domain(1);
            t = Lookup_Thread(d, outlet_zone_id);

            /* open file once for header, then append thereafter */
            if (first)
            {
                fp = fopen("outlet_flow.txt", "w");
                if (!fp) { Message("Error: could not open outlet_flow.txt for writing\n"); return; }
                fprintf(fp, "Time\tTotalArea\tTotalVolFlow\n");
                first = 0;
            }
            else
            {
                fp = fopen("outlet_flow.txt", "a");
                if (!fp) { Message("Error: could not open outlet_flow.txt for appending\n"); return; }
            }

            /* loop over all faces in the outlet thread */
            begin_f_loop(f, t)
            {
                /* get face‐area vector and magnitude */
                F_AREA(area_vec, f, t);
                face_area = NV_MAG(area_vec);

                /* get volumetric flux through this face */
                vol_flow = F_FLUX(f, t);

                total_area     = total_area + face_area;
                total_vol_flow = total_vol_flow + vol_flow;
            }
            end_f_loop(f, t);

            /* write time, total area, and total volumetric flow rate */
            fprintf(fp, "%e\t%e\t%e\n",
                    CURRENT_TIME,
                    total_area,
                    total_vol_flow);

            fclose(fp);
        }

    • Rob
      Forum Moderator

      You may want to read up on parallel effects as that may alter how you write some of this. Looking at the code, it looks like the area & flow loop isn't being triggered so zero is returned to the fprintf section. Not sure why, and debugging isn't something staff will get involved with: the wider community are free to do so. 

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