Fluids

Fluids

Topics related to Fluent, CFX, Turbogrid and more.

Fluent UDF: Passing information to a face loop within a cell loop

TAGGED: 

    • Kenneth.Fung
      Subscriber

      I have a relatively simple UDF that does the following:
      1. Loops over all the cell threads.
         2. Loops over all cells of each cell thread.
           3. Calculates centroid of each cell.
           4. Loops over all face threads.
              5. Loops over all faces of each face thread.
                 6. Calculate centroid of each face.
                 7. Assign a UDM value to the cell if a condition between the cell and face centroids is met.

      The UDF looks something like below, where I have used an example condition which compares the z-coordinate difference between the cell and face centroids. There would be other conditions which stop the loop and move on to the next cell if the UDM value of that cell is 1, but I've omitted these lines for clarity. 

      #if !RP_HOST
      
      Domain *domain;
      Thread *tc, *tf;
      face_t f;
      cell_t c;
      
      domain = Get_Domain(ROOT_DOMAIN_ID);
      
      real xc[ND_ND];
      real xf[ND_ND];
      
      thread_loop_c(tc,domain)
      {
          begin_c_loop(c,tc)
          {
              C_CENTROID(xc,c,tc);
      	thread_loop_f(tf,domain)
              {
      	    begin_f_loop(f,tf)
      	    {
                      F_CENTROID(xf,f,tf);
                      if (fabs(xc[2] - xf[2]) < 0.1)
                          C_UDMI(c,tc,0) = 1;
                  }
                  end_f_loop(f,tf)
              }
          }
          end_c_loop(c,tc)
      }
      
      #endif

      The code works in serial, but I could not get it to work in parallel. The issue is that the face loop may be done on a different compute node to the node of the cell loop, so that the face loop compute node does not have information calculated in the outside cell loop.

      How can I pass the cell centroid information from the cell loop into the face loop (and each face thread loop) so that the above code works in parallel?

      I've considered intializing and creating a 3D array which contains the centroid of all the faces of each face thread, and then looping over the cells, but this does not seem very efficient as most of the array would need to be filled with null values since the number of faces on each thread would be different. I've also considered using a pointer, but I don't know how to feed information about the pointer into the face thread loop.

    • Amine Ben Hadj Ali
      Ansys Employee

      I would recommend doing the face loop via c_face_loop.

      • Kenneth.Fung
        Subscriber

        I'm not exactly sure how that's relevant, as I'm trying to loop over the faces of the mesh, and not the faces of the cells?

    • Amine Ben Hadj Ali
      Ansys Employee

      Your looping is not ideal as there is no warranty that you will get the data of the faces of the cells you are currenlty in! For that reason I recommend using c_face_loop inside the Thread Cell Loop. We have done that before when trying to get distance of cell centroids to face centroids. You can aslo check the macros under Connectivity Macros in the Customization Manual if they are helpful for your task.

       

      • Kenneth.Fung
        Subscriber

        I'm still a bit confused as to why I would use c_face_loop as this loops over the faces of the cells, whereas I'm just interested in the cell centroids and the boundary face centroids. Suppose I wanted to print the distance between the centroid of every cell to the centroid of every boundary face. How would you achieve this with c_face_loop? It seems that I would run into the same issue since I would not be able to pass information into the begin_f_loop once I start looping over the boundary faces.

    • Amine Ben Hadj Ali
      Ansys Employee

      Thanks for your last comment.

      Based on your UDF you shared in the beginning of the thread: It will be very expensive. I think about serializing the cell looping so start with node-0 and the other nodes to follow and then to send recive information between the nodes. 

      Aslo I recommend not do the face loop over all faces: here you are probably interested in getting the distances to boundary face thread anot to interface face threads.

      Do you require the distance from the cell centroid to wall boundaries?

      • Kenneth.Fung
        Subscriber

         

         

        Thanks for the advice! Are you able to elaborate or link me to the documentation on how to serialize the cell loop and pass information to the other nodes?

        For context, I am trying to build a solar ray tracer for cells to determine which cells are “shaded” and which cells are “exposed”. The built-in solar ray tracer in Fluent only works on surfaces but not on interior cells. I’ve considered using a diffuse radiation model but I found it to be very unreliable.

        In my current serial code, I trace a ray from every cell centroid, and then loop over all the wall faces. I then run an algorithm to determine whether the ray intersects each face. If it intersects a face, a UDM value of 1 is assigned to the cell, and then it skips to the next cell in the loop and repeats the process. This is why I need to pass information (i.e. the cell centroid) from the begin_c_loop into thread_loop_f and begin_f_loop.

        I understand the computation will be expensive. However, for each cell, there will be several “if” conditions that need to be satisfied before it considers each face (e.g. a simple one being that the cell centroid would need to be below the face centroid), so the actual computation will hopefully be manageable. I can also consider dividing the mesh into boxes, and determine whether the ray intersects a box before looping over the faces that lie within the box. However, I’m currently just trying to get a basic code working in parallel before I consider ways to reduce the computational time.

         

         

    • Amine Ben Hadj Ali
      Ansys Employee

      The procedure will be similar in writing content into a File sequentially in parallel as mentioned in Customization Manual.

      • Kenneth.Fung
        Subscriber

         

        I tried following the similar steps to count the number of cells in each cell thread, but I am getting strange numbers:

        #include “udf.h”
        #include “prox.h”

        DEFINE_ON_DEMAND(compute)
        {
        #if !RP_HOST

        Domain *domain;
        Thread *tc;

        domain = Get_Domain(ROOT_DOMAIN_ID);

        if (I_AM_NODE_ZERO_P)
        {
        int ntc = 0;
        int nc;
        thread_loop_c(tc,domain)
        {
        ntc +=1;
        nc = 0;
        begin_c_loop(c,tc)
        {
        nc += 1;
        }
        end_c_loop(c,tc)
        Message(“Number of cells: %d\n”,nc);
        }
        Message(“Number of cell threads: %d\n”,ntc);
        }
        #endif
        }

        The total number of cells in my single cell zone domain is 39585, but the above gives me 10372. If I replace the cell loop with “nc = THREAD_N_ELEMENTS(tc)“, I get 10624 cells. Not sure what’s going on here.

    • Amine Ben Hadj Ali
      Ansys Employee

      Regarding Radiation Modeling: Why not using MC?

      • Kenneth.Fung
        Subscriber

        I did try MC but was not able to get good results. Also, the result would be dependent on the number of histories. Given that I would be sweeping through different times of the year for different meshes that may vary significant in cell size, it would not be practical to run a radiation model given the number of parameters involved.

        I'm also considering doing the analysis in Python, but I'm having trouble determining the cell and face centroid data from the HDF5 files. I've made a separate post about it here: /forum/forums/topic/determine-cell-and-face-centroids-of-hdf5-fluent-files-in-python-using-h5py/

    • Amine Ben Hadj Ali
      Ansys Employee

      MC is the most superior Radiation Model and is showing excellent results and well validated. On top of that one can model polar distribution with it.

       

      Your loop does only consider the Node Zero related internal and external cells.

      I cannot follow-up on your UDF on this open forum. I hope that other forum users can  chime in and comment on your UDF.

Viewing 6 reply threads
  • The topic ‘Fluent UDF: Passing information to a face loop within a cell loop’ is closed to new replies.