CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   Fluent UDF and Scheme Programming (https://www.cfd-online.com/Forums/fluent-udf/)
-   -   Moving average using UDF (https://www.cfd-online.com/Forums/fluent-udf/236731-moving-average-using-udf.html)

josamuel June 12, 2021 18:36

Moving average using UDF
 
Hi all,

I'm trying to obtain the pressure values at a particular interface inside the domain in a transient 2D model using UDF. This calculation happens for every iteration. And I also 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 for every iteration. But, UDF calculates the moving average of the obtained pressure values for every iteration, instead, I need it to be calculated over a few timesteps. 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;

DEFINE_ADJUST(Pint, domain)

{
int i;
i= N_TIME; /* obtains the integer value of the timestep*/

real coord[ND_ND];
face_t f;
cell_t c0;
int ID = 23;
Thread *thread, *t0;
thread = Lookup_Thread(domain, ID);
begin_f_loop(f, thread)
{
c0 = F_C0(f,thread);
t0 = THREAD_T0(thread);
F_CENTROID(coord,c0,t0);

printf("x = %f\t y = %f\n\n ",coord[0], coord[1]);
Pr += F_P(c0,t0);

printf("Pr = %f\n\n", Pr);
}

end_f_loop(f,thread)

Pr=Pr/14; /* this averages over the 14 faces */
printf("Pr = %f\n", Pr);

for ( i= 1; i < NumTempsToAvg; i++)
{
temps[i] = Pr; /* array name is temps */
sum += temps[i];
printf("the sum is %f\n\n", sum);
printf("avg: %f\n", sum / NumTempsToAvg); /* moving average */
sum -= temps[i];
temps[i] = Pr;
sum += temps[i];
}

}

gensqy June 17, 2021 00:09

You can try using DEFINE_EXECUTE_AT_END, I believe it runs at the end of every timestep in a transient run.

josamuel June 25, 2021 14:36

Thanks Gensqy,

I used DEFINE_EXECUTE_AT_END and obtained values at the end of the timestep. Thanks for the idea and reply. However, my final goal of this UDF is to get the moving average. Unfortunately, it still ends up showing some errors. Any leads would be helpful.

thanks in advance.

AlexanderZ June 27, 2021 21:41

show code you are using
show compilation log file
explain in details problems/errors which you have


All times are GMT -4. The time now is 12:31.