Moving average using UDF using N_TIME macro giving out calculation error.

    • pmjsjs


      I'm trying to obtain the pressure values at a particular interface inside the domain in a transient 2D model using UDF. Also I want the moving average(aka rolling average) of these obtained pressure values over few timesteps(not iterations). After obtaining this averaged value I use them back as an input into the simulation.

      I have tried a code that I have pasted below. I was able to successfully obtain the pressure values at the interface at end of every timestep using DEFINE_EXECUTE_AT_END macro. I'm using N_TIME macro to obtain the integer value of the timestep and calculate the moving average of the pressure values over few timesteps . But, UDF calculates the moving average of the obtained pressure values only for the current timestep, instead, I need it to be calculated over a few timesteps. Though I'm using N_TIME in a loop, the values of previous timesteps are not obtained. I have attached below the code few lines of the transcript of the calculations for one timestep for your reference. Please help me where I went wrong.

      Thanks in advance!

      #include "udf.h"

      #define NumTempsToAvg 6 /* this number is the number of timesteps taken for average */

      real Pr;

      int temps[NumTempsToAvg];

      real sum = 0.0;



      int i;

      Domain *d;

      real coord[ND_ND];

      face_t f;

      cell_t c0;

      int ID = 23;

      Thread *thread, *t0;

      d = Get_Domain(1);

      thread = Lookup_Thread(d, ID);

      begin_f_loop(f, thread)


      c0 = F_C0(f,thread);

      t0 = THREAD_T0(thread);


      printf("x = %f y = %f ",coord[0], coord[1]);

      Pr += F_P(c0,t0);

      printf("Pr = %f ", Pr); 



      Pr=Pr/14; /* average over 14 face*/

      printf("Avgpr = %f ", Pr); 

      i= N_TIME; /* this marco obtains the integer value of the timestep*/

      for ( i= 1; i < NumTempsToAvg; i++)


      temps[i] = Pr;

      sum += temps[i];

      printf("the sum is %f ", sum);

      printf("movavg: %f ", sum / NumTempsToAvg);

      sum -= temps[i];

      printf("the minussum is %f ", sum);

      temps[i] = Pr;

      sum += temps[i];

      printf("the addsum is %f ", sum);



      calculation transcript for one timestep(for reference)

      Avgpr = -5031.508294

      the sum is -312826  movavg: -52137.666667  the minussum is -307795 the addsum is -312826

      the sum is -317857 movavg: -52976.166667  the minussum is -312826 the addsum is -317857

      the sum is -322888  moving: -53814.666667  the minussum is -317857  the addsum is -322888

      the sum is -327919  movavg: -54653.166667  the minussum is -322888  the addsum is -327919

      the sum is -332950  movavg: -55491.666667  the minussum is -327919  the addsum is -332950

    • Karthik R
      Hello To obtain the moving average, you will need to store your variable of interest (pressure, temperature, etc.) from previous timesteps (all the previous time-step data you wish to include) into separate UDMIs. You can use some of the Time-Dependent Macros for extracting the data from Fluent. Here is a link to these Macros.
      UDMIs: https://ansyshelp.ansys.com/account/secured?returnurl=/Views/Secured/corp/v211/en/flu_udf/flu_udf_sec_udm_macros.html?q=UDMI
      Time-Dependent Macros: https://ansyshelp.ansys.com/account/secured?returnurl=/Views/Secured/corp/v211/en/flu_udf/flu_udf_RPVariables.html?q=N_TIME

    • pmjsjs
      Thanks for the response.
      I'm obtaining the pressure by using the flow variable macro for cell - F_P(c0,t0) and tried storing it using C_UDMI(c0,t0,0) and then used C_STORAGE_R(c,t,SV_P) to store it. After running few timestep when I obtained the plot of user-defined memory 0 the plot is showing zero throughout the plot.
      I'm using the C_STORAGE_R(c,t,SV_P) to obtain the values of pressure and then divide by the timing sampling value to obtain the time-averaged value. Im not sure if this logic is correct. I'm a novice and recently started C programming. Also, can you advice me if the storage macro can be assigned a variable as mentioned below.
      The code gets compiled in the fluent but while running the simulation I'm getting the following error - Process 18096: Received signal SIGSEGV. However, without the C_STORAGE_R macro, the simulation runs even though plot of user-defined memory 0 gives out only zero. Please advice where I went wrong in these macros.

      Pr += F_P(c0,t0);
      C_UDMI(c0,t0,0) = Pr;
      avg = C_STORAGE_R(c0,t0,SV_P); /* where avg is a variable declared as real avg .*/
Viewing 2 reply threads
  • You must be logged in to reply to this topic.