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/)
-   -   how to calculate a derivative through UDF (https://www.cfd-online.com/Forums/fluent-udf/134287-how-calculate-derivative-through-udf.html)

Rigid April 27, 2014 12:00

how to calculate a derivative through UDF
 
Hi, all.
In my task I need to calculate a derivative from (du/dx+dv/y) by x and y.
How can i do this in my UDF ? I tried to do so

( (C_DUDX(c,t)+C_DVDY(c,t)) - (C_DUDX(c-1,t)+C_DVDY(c-1,t)) )/hx

but it is wrong, and I have no other ideas. Can anyone help me with this problem?

christophelorant April 28, 2014 04:24

Hello Rigid,

The way you're trying to compute gradient will certainly not work because it is only a valid approximation for 1D meshes. What you could do to solve your problem is to use the way Fluent itself computes other gradients: The Green-Gauss theorem.

\vec{\nabla}\phi = \frac{1}{\Omega}\sum_f{\phi_{f}\vec{A_{f}}}

With \Omega the cell volume (or surface in 2D), \phi_f the value of the variable on the face f and \vec{A_f} the face vector.

You could try the following to compute your gradient in one cell:

Code:

/* c is the index of cell where the gradient has to be computed and t the corresponding cell thread*/

Thread *tf;
real A[2], phi_f, grad[2];
int i;

grad[0] = 0;
grad[1] = 0;

c_face_loop(c, t, i)
{
    f = C_FACE(c,t,i);
    tf = C_FACE_THREAD(c,t,i);
    phi_f = 0.5 * ( C_U_G(F_C0(f,tf),THREAD_T0(tf))[0] + C_U_G(F_C1(f,tf),THREAD_T1(tf))[0] );
    phi_f += 0.5 * ( C_V_G(F_C0(f,tf),THREAD_T0(tf))[1] + C_V_G(F_C1(f,tf),THREAD_T1(tf))[1] );
    F_AREA(A,f,ft);
    grad[0] += phi_f*A[0];
    grad[1] += phi_f*A[1];
}

grad = grad/C_VOLUME(c,t);


Rigid April 29, 2014 16:32

Hello, christophelorant !

Thank you very much for your answer and code, now my udf works correct (imho).

I fixed some bugs in your code:

1. Variable f must be defined as face_t

2. String

grad = grad/C_VOLUME(c,t)

leading to compilation error, so i updated
this as following

grad[0] = grad[0]/C_VOLUME(c,t)
grad[1] = grad[1]/C_VOLUME(c,t)

and i hope this is correct.


Because in this code a "c_face_loop" is used, udf source file must be compiled
(not interpreted) in Fluent.

Regards, Rigid


All times are GMT -4. The time now is 00:46.