Ansys Assistant will be unavailable on the Learning Forum starting January 30. An upgraded version is coming soon. We apologize for any inconvenience and appreciate your patience. Stay tuned for updates.
Fluids

Fluids

Topics related to Fluent, CFX, Turbogrid and more.

Local variable doesn’t update in UDF in DPM setup

    • Smruti R.
      Subscriber

      I have written the following code for getting the total number of active particles in a domain (while using DPM in FLuent). The problem that I am facing is while printing the total number of particles, the moment the control comes out of the inner loop, the variable total_particles gets reinitialized back to 0 (even within the same function). Could someone please advise as in what is going wrong?

      I have tried putting total_particles as a global variable too, but that too didn't help.

      #include "udf.h"
      #include "dpm.h"
       
      /* Name of the output file */
      #define FILE_NAME "active_particles.txt"
       
      DEFINE_EXECUTE_AT_END(write_total_particles)
      {
          int total_particles = 0;
          Particle *p;
          Injection *inj;
          
          /* Loop over all injections */
          loop(inj, Get_dpm_injections())
          {
              Message("total particles = %d, inside loop 1\n", total_particles);
              /* Loop over all particles in the injection */
              loop(p, inj->p)
              {
                  total_particles = total_particles + 1;
                  Message("total particles = %d, inside loop 2\n", total_particles);
              }
          }
          
          /* Write total count from node 0 */
          if (I_AM_NODE_ZERO_P)
          {
              FILE *fpp = fopen(FILE_NAME, "a");
              if (fpp)
              {
                  fprintf(fpp, "%f\t%d\n", CURRENT_TIME, total_particles);
                  fclose(fpp);
              }
             else
             {
                    Message("Warning : Hey there, Could not open %s\n", FILE_NAME);
             }
          }
      }
    • Smruti R.
      Subscriber

      To add more clarity to my current explanation, here is what happens. 

      Let's say, there are 30 injections in total & every injection has a separate number of particles. Now, when the control completed counting the particles for the first injection, while coming out of the inner loop (i.e. loop(p, inj->p)), it should store the number of particles & that should be added to the number of particles in the subsequent injections. However, everytime, the control iterates through the injection loop (i.e. loop(inj, Get_dpm_injections())), the variable total_particles gets initialized to 0. And the total number doesn't add up. 

      So, could someone please look into as in what's going wrong?

    • Rob
      Forum Moderator

      Are you trying to count the total parcels in the domain or what's just been injected?

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