How can I get the derivatives of velocity field?

Questions and remarks about code_saturne usage
Forum rules
Please read the forum usage recommendations before posting.
Mohammad
Posts: 114
Joined: Thu Oct 25, 2018 12:18 pm

How can I get the derivatives of velocity field?

Post by Mohammad »

Hi,
I want to calculate the derivatives of velocity field and store them.
For example, I want this expression in every iteration:
Image
(∂u/∂y + ∂v/∂x)
How can I get this expression from the code?

Thanks
Last edited by Mohammad on Fri Oct 11, 2019 7:02 am, edited 2 times in total.
joubanba
Posts: 9
Joined: Fri Oct 04, 2019 9:42 am

Re: How can I get the derivatives of velocity field?

Post by joubanba »

Hello,

You can get a gradient of a field(scalar, vector or tensor) by using the following Code_Saturne functions:

cs_field_gradient_scalar()/cs_field_gradient_vector()/cs_field_gradient_tensor() in C language
or call field_gradient_scalar() in fortran 90.

The following link of Code_Saturne documentation web site shows how to get the gradient of the temperature field:

https://www.code-saturne.org/cms/sites/ ... nce_p.html

Best regards,

Jamal
Mohammad
Posts: 114
Joined: Thu Oct 25, 2018 12:18 pm

Re: How can I get the derivatives of velocity field?

Post by Mohammad »

Thank you Jamal!
Now, the problem is how to get each component of calculated gradient to sum the mentioned expression?
Image
(∂u/∂y + ∂v/∂x)
Last edited by Mohammad on Fri Oct 11, 2019 7:02 am, edited 1 time in total.
joubanba
Posts: 9
Joined: Fri Oct 04, 2019 9:42 am

Re: How can I get the derivatives of velocity field?

Post by joubanba »

Hello,

I can't see your attached images.

Jamal
Mohammad
Posts: 114
Joined: Thu Oct 25, 2018 12:18 pm

Re: How can I get the derivatives of velocity field?

Post by Mohammad »

If you can't see, I want (∂u/∂y + ∂v/∂x)
It's one of strain rate matrix components.
C.FLAG.
Posts: 32
Joined: Fri Apr 08, 2016 2:19 pm

Re: How can I get the derivatives of velocity field?

Post by C.FLAG. »

Hello,

If you take a look at the link provided, you will see some source code such as :

Code: Select all

    call field_gradient_scalar (ivarfl(ivar), 0, imrgra, inc, iccocg, grad)
    ! - Compute reconstructed value in boundary cells
    do ifac = 1, nfabor
      iel = ifabor(ifac)
      treco(ifac) =   cvar_scal(iel)       &
                    + diipb(1,ifac)*grad(1,iel)  &
                    + diipb(2,ifac)*grad(2,iel)  &
                    + diipb(3,ifac)*grad(3,iel)
    enddo
In this case, the gradient of a scalar is a vector. Grad(1,iel) is the gradient in direction X inside cell iel. Grad(2,iel) for direction Y. Grad(3,iel) for direction Z.

EDIT: for the gradient of the velocity field, take a look here : https://github.com/code-saturne/code_sa ... til.c#L744

Regards,
Cédric
Mohammad
Posts: 114
Joined: Thu Oct 25, 2018 12:18 pm

Re: How can I get the derivatives of velocity field?

Post by Mohammad »

Thank you!
I used those codes to sum the three components of gradient of velocity field at each point.
So I wrote the following C code in cs_user_postprocess.c file.

But it does not give me any output at all!
My CS version is 5.0.9.

Code: Select all

void
cs_user_postprocess_values(const char            *mesh_name,
                           int                    mesh_id,
                           int                    cat_id,
                           cs_probe_set_t        *probes,
                           cs_lnum_t              n_cells,
                           cs_lnum_t              n_i_faces,
                           cs_lnum_t              n_b_faces,
                           cs_lnum_t              n_vertices,
                           const cs_lnum_t        cell_list[],
                           const cs_lnum_t        i_face_list[],
                           const cs_lnum_t        b_face_list[],
                           const cs_lnum_t        vertex_list[],
                           const cs_time_step_t  *ts)
{
  /* cs_lnumt is local mesh id*/
     cs_real_t *s_cell;
     cs_real_33_t *gradv; //
     BFT_MALLOC(s_cell, n_cells, cs_real_t);
     BFT_MALLOC(gradv, n_cells, cs_real_33_t); 
     cs_field_gradient_vector(CS_F_(u),
			      false, //use_previous_t,
			      CS_GRADIENT_ITER, //gradient type
			      CS_HALO_STANDARD, //HALO type
			      1, //inc
			      gradv);

      for (cs_lnum_t i = 0; i < n_cells; i++) {
         cs_lnum_t iel = cell_list[i];
	 s_cell[i] = (gradv[iel][0][0] + gradv[iel][1][1] + gradv[iel][2][2]);
      }
      cs_post_write_var(mesh_id,
                      CS_POST_WRITER_ALL_ASSOCIATED,  /* writer id filter */
                      "ugrad",                  /* var_name */
                      1,                              /* var_dim */
                      true,                           /* interlace, */
                      false,                          /* use_parent */
                      CS_POST_TYPE_cs_real_t,         /* var_type */
                      s_cell,                         /* cel_vals */
                      NULL,                           /* i_face_vals */
                      NULL,                           /* b_face_vals */
                      ts);
    BFT_FREE(s_cell);  

}
I also included the following headers:

Code: Select all

#include "cs_defs.h"
#include "cs_field.h"
#include "cs_gradient.h"
#include "cs_halo.h"
#include "cs_field_operator.h"
The other question is :
gradv[iel][0][2] means du/dz or dw/dx?
Yvan Fournier
Posts: 4070
Joined: Mon Feb 20, 2012 3:25 pm

Re: How can I get the derivatives of velocity field?

Post by Yvan Fournier »

Hello,

Do you have other outputs active ? What system are you running on ? (i.e. some users have had issues with user functions not being called on Ubuntu systems). Your code looks OK, so I do not see any obvious mistake (but there can always be a subtle one).

Could you use a "printf" or bft_printf" statement (or even better, a debugger) to check if the function is called (as it should be) ?

As for the second question, I am not sure. I think it is du/dz judging by a quick look at a usage pattern in the sources, but I recommend checking the sources more carefully, or checking on a case where you are sure nothing interesting happens in the x or z direction (i.e. the matching component is 0).

Best regards,

Yvan
Mohammad
Posts: 114
Joined: Thu Oct 25, 2018 12:18 pm

Re: How can I get the derivatives of velocity field?

Post by Mohammad »

Hello,
Thanks for your helps,

I'm using Ubuntu 18.04.
It seems that the problem solved by using cs_field_gradient_scalar instead of cs_field_gradient_vector!

But I have a new problem:
When the calculation ends, the CS says:

Code: Select all

 ****************************
  Saving calculation results
 ****************************

 Error in calculation stage.
After checking listing and error files there is an error there which says:

Code: Select all

IGSEGV signal (forbidden memory area access) intercepted!

Call stack:
   1: 0x7f1e920b9818 <+0x312818>                      (libsaturne.so.5)
   2: 0x7f1e8f5dbecf <GOMP_parallel+0x3f>             (libgomp.so.1)
   3: 0x7f1e920c68c8 <+0x31f8c8>                      (libsaturne.so.5)
   4: 0x7f1e920caac4 <cs_gradient_scalar+0x334>       (libsaturne.so.5)
   5: 0x7f1e91eb8757 <cs_field_gradient_scalar+0x127> (libsaturne.so.5)
   6: 0x5582d41cc6bc <cs_user_postprocess_values+0x8c> (cs_solver)
   7: 0x7f1e91ee7c82 <cs_post_time_step_output+0xaa2> (libsaturne.so.5)
   8: 0x7f1e91ee8801 <cs_post_write_vars+0x11>        (libsaturne.so.5)
   9: 0x7f1e91e40921 <caltri_+0x208c>                 (libsaturne.so.5)
  10: 0x7f1e91e1d58d <cs_run+0x57d>                   (libsaturne.so.5)
  11: 0x7f1e91e1cee5 <main+0x125>                     (libsaturne.so.5)
  12: 0x7f1e90f68b97 <__libc_start_main+0xe7>         (libc.so.6)
  13: 0x5582d41cc2ba <_start+0x2a>                    (cs_solver)
End of stack
The outputs are generated perfectly and I can access the results using paraview ,but what about this error?!
How can the results be generated while this error is occurred?

Thank you!
Yvan Fournier
Posts: 4070
Joined: Mon Feb 20, 2012 3:25 pm

Re: How can I get the derivatives of velocity field?

Post by Yvan Fournier »

Hello,

If you are running in parallel, you need to allocate using n_cells_with_ghosts and not n_cells..

If this is not the cause of the crash, running under a debugger or under Valgrind would help.

Regards,

Yvan
Post Reply