CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   FLUENT (https://www.cfd-online.com/Forums/fluent/)
-   -   Udf for adaptive time step, error in loop (https://www.cfd-online.com/Forums/fluent/218847-udf-adaptive-time-step-error-loop.html)

francescolab July 6, 2019 02:04

Udf for adaptive time step, error in loop
 
Could someone, please, help me with this udf code? It's write in the right way but it doesn't work and don't do function in the "else" https://pastebin.com/raw/SMazER3X
Code:

 



#include "udf.h"
#include"udf.h"
#include"metric.h"
#include"stdio.h"
#include"math.h"
#include"stdlib.h"
#include"mem.h"

DEFINE_DELTAT(deltat,d)
{
real time_step;
real tx=CURRENT_TIME;
Thread *t,*t0, *t1 =NULL;
cell_t c0, c1 =-1;
face_t f;
Domain *domain;
real x0[ND_ND];
real x1[ND_ND];
int n =0;
real dx =0.0, dy =0.0;
domain = Get_Domain(1);
n =0;
real cent[2];
double centr_x, centr_y;
double vel_x, vel_y;

if ( tx<=1 ) {
time_step=0.01;
} else {
DEFINE_ON_DEMAND(calc_distance)
{

thread_loop_f(t,domain)
{
begin_f_loop (f,t)
{
c0 = F_C0(f,t);
t0 = F_C0_THREAD(f,t);
if (BOUNDARY_FACE_THREAD_P(t))
{}
else
{
n+=1;
t1 = F_C1_THREAD(f,t);
c1 = F_C1(f,t);
C_CENTROID(x0,c0,t0);
C_CENTROID(x1,c1,t1);
dx = x1[0]-x0[0];
dy = x1[1]-x0[1];
}
}
end_f_loop (f,t)
}
DEFINE_ADJUST(velocity_profile_face,d)
{
thread_loop_f(t,d)
{
begin_f_loop(f,t)
{
F_CENTROID(cent,f,t);

centr_x = cent[0];
centr_y = cent[1];
vel_x = F_U(f,t);
vel_y = F_U(f,t);

}
end_f_loop(f,t)
}
time_step=MAX (time_step, (dx / vel_x) *0.8);
time_step=MAX (time_step, (dy / vel_y) *0.8);
}
}
}
return time_step;
}


Светлана July 6, 2019 06:29

The 'else' block starts with a 'DEFINE_ON_DEMAND(calc_distance)', is this intentional?

francescolab July 6, 2019 06:32

Quote:

Originally Posted by Светлана (Post 738154)
The 'else' block starts with a 'DEFINE_ON_DEMAND(calc_distance)', is this intentional?


Yes, but i saw this is wrong, because i read that it's a function that read value at the end of calculations, anyway i would like to understand how to write a function that in the "else" compute max of the division between velocity and cell dimension, and after calculate the time step as time_step = max (dx/velx) * CFL max (0,8 in my case)

AlexanderZ July 8, 2019 00:04

DEFINE_DELTAT DEFINE_ADJUST and DEFINE_ON_DEMAND
are independent macros, you cant use them inside each other

start from this, compile UDF, read logs and fix errors
Code:

#include "udf.h"
#include"udf.h"
#include"metric.h"
#include"stdio.h"
#include"math.h"
#include"stdlib.h"
#include"mem.h"

DEFINE_DELTAT(deltat,d)
{
real time_step;
real tx=CURRENT_TIME;
Thread *t,*t0, *t1 =NULL;
cell_t c0, c1 =-1;
face_t f;
real x0[ND_ND];
real x1[ND_ND];
int n =0;
real dx =0.0, dy =0.0;
real cent[2];
double centr_x, centr_y;
double vel_x, vel_y;
n =0;
time_step=0.01;
if ( tx<=1 ) {
 time_step=0.01;
} else {
        thread_loop_f(t,domain)
        {
        begin_f_loop (f,t)
        {
        c0 = F_C0(f,t);
        t0 = F_C0_THREAD(f,t);
        if (BOUNDARY_FACE_THREAD_P(t))
        {}
        else
        {
        n+=1;
        t1 = F_C1_THREAD(f,t);
        c1 = F_C1(f,t);
        C_CENTROID(x0,c0,t0);
        C_CENTROID(x1,c1,t1);
        dx = x1[0]-x0[0];
        dy = x1[1]-x0[1];

        F_CENTROID(cent,f,t);

        centr_x = cent[0];
        centr_y = cent[1];
        vel_x = F_U(f,t);
        vel_y = F_U(f,t);
        time_step=MAX (time_step, (dx / vel_x) *0.8);
        time_step=MAX (time_step, (dy / vel_y) *0.8);
        }
        end_f_loop(f,t)
        }

        }
}
return time_step;
}

time_step=MAX (time_step, (dx / vel_x) *0.8);
time_step=MAX (time_step, (dy / vel_y) *0.8);
are calculated on each face, if you don't want -> fix it

best regards

francescolab July 8, 2019 05:39

Quote:

Originally Posted by AlexanderZ (Post 738239)
DEFINE_DELTAT DEFINE_ADJUST and DEFINE_ON_DEMAND
are independent macros, you cant use them inside each other

start from this, compile UDF, read logs and fix errors
Code:

#include "udf.h"
#include"udf.h"
#include"metric.h"
#include"stdio.h"
#include"math.h"
#include"stdlib.h"
#include"mem.h"

DEFINE_DELTAT(deltat,d)
{
real time_step;
real tx=CURRENT_TIME;
Thread *t,*t0, *t1 =NULL;
cell_t c0, c1 =-1;
face_t f;
real x0[ND_ND];
real x1[ND_ND];
int n =0;
real dx =0.0, dy =0.0;
real cent[2];
double centr_x, centr_y;
double vel_x, vel_y;
n =0;
time_step=0.01;
if ( tx<=1 ) {
 time_step=0.01;
} else {
    thread_loop_f(t,domain)
    {
    begin_f_loop (f,t)
    {
    c0 = F_C0(f,t);
    t0 = F_C0_THREAD(f,t);
    if (BOUNDARY_FACE_THREAD_P(t))
    {}
    else
    {
    n+=1;
    t1 = F_C1_THREAD(f,t);
    c1 = F_C1(f,t);
    C_CENTROID(x0,c0,t0);
    C_CENTROID(x1,c1,t1);
    dx = x1[0]-x0[0];
    dy = x1[1]-x0[1];

    F_CENTROID(cent,f,t);

    centr_x = cent[0];
    centr_y = cent[1];
    vel_x = F_U(f,t);
    vel_y = F_U(f,t);
    time_step=MAX (time_step, (dx / vel_x) *0.8);
    time_step=MAX (time_step, (dy / vel_y) *0.8);
    }
    end_f_loop(f,t)
    }

    }
}
return time_step;
}

time_step=MAX (time_step, (dx / vel_x) *0.8);
time_step=MAX (time_step, (dy / vel_y) *0.8);
are calculated on each face, if you don't want -> fix it

best regards

This code is okay and fluent compiled it, anyway it gives me an error of this type now:

https://ibb.co/mBv9WxF
https://ibb.co/mBv9WxF

Actual code is this ( i added the definition of domain )
Quote:

#include "udf.h"
#include"udf.h"
#include"metric.h"
#include"stdio.h"
#include"math.h"
#include"stdlib.h"
#include"mem.h"

DEFINE_DELTAT(deltat,d)
{
real time_step;
real tx=CURRENT_TIME;
Thread *t,*t0, *t1 =NULL;
cell_t c0, c1 =-1;
face_t f;
real x0[ND_ND];
real x1[ND_ND];
int n =0;
real dx =0.0, dy =0.0;
real cent[2];
double centr_x, centr_y;
double vel_x, vel_y;
n =0;
Domain *domain;
domain = Get_Domain(1);
time_step=0.01;
if ( tx<=0 ) {
time_step=0.01;
} else {
thread_loop_f(t,domain)
{
begin_f_loop (f,t)
{
c0 = F_C0(f,t);
t0 = F_C0_THREAD(f,t);
if (BOUNDARY_FACE_THREAD_P(t))
{}
else
{
n+=1;
t1 = F_C1_THREAD(f,t);
c1 = F_C1(f,t);
C_CENTROID(x0,c0,t0);
C_CENTROID(x1,c1,t1);
dx = x1[0]-x0[0];
dy = x1[1]-x0[1];

F_CENTROID(cent,f,t);

centr_x = cent[0];
centr_y = cent[1];
vel_x = F_U(f,t);
vel_y = F_U(f,t);
time_step=MAX (time_step, (dx / vel_x) *0.8);
time_step=MAX (time_step, (dy / vel_y) *0.8);
}
end_f_loop(f,t)
}

}
}
return time_step;
}
https://ibb.co/mBv9WxF
https://ibb.co/mBv9WxF

AlexanderZ July 8, 2019 06:20

You've made mistake

best regards


All times are GMT -4. The time now is 19:32.