Page 1 of 1

Calculating Velocity Gradient in Post-Processing

Posted: Thu Jun 06, 2024 6:33 am
by StandardRANSUser001
Hi Everyone,

I would like to calculate the gradients of the velocity field in post-processing in CS. V8. I can see several examples in cs_user_parameters.c, (i.e. predict_vel_divergence), but this function only calculates the divergence of the velocity field, and it does not provide a gradient tensor.

It would be really handy to have these quantities saved in post-processing!

Any guidance on this would be greatly appreciated.

Best regards,
Sean

Re: Calculating Velocity Gradient in Post-Processing

Posted: Thu Jun 06, 2024 11:04 pm
by finzeo
Hello,

Do you want to calculate it to observe it graphically? If so, ParaView has the 'Gradient Of Unstructured DataSet' filter. You set the velocity field as the input.

Re: Calculating Velocity Gradient in Post-Processing

Posted: Fri Jun 07, 2024 12:18 am
by StandardRANSUser001
Hi Finzeo,

Thank you for your suggestion with Paraview.

I would like to be able to view and analyse these gradients in Paraview, but I want to ensure that the gradients are calculated consistently with the same formulation as those used in the solver.

My preference is to still develop a function in Code Saturne to process this in the post-procesing stage. There are several other quantities that I would like to calculate in post-processing, and developing a function for this is very helpful.

Best regards,
Sean Hanrahan

Re: Calculating Velocity Gradient in Post-Processing

Posted: Fri Jun 07, 2024 5:26 am
by finzeo
Hello,

In that case, I think it would be useful for you to review the example file cs_user_extra_operations-vorticity_field.c, and use only the part that is used for the velocity gradient calculation; it uses the cs_field_gradient_vector function, which takes the gradient reconstruction type set for the field in question (through the eqp->imrgra parameter). By default, a cs_user_extra_operations-*.c file is executed at the end of each time step, but you could set it to execute this part when necessary and save it in a field for post-processing.

Re: Calculating Velocity Gradient in Post-Processing

Posted: Tue Jun 18, 2024 6:23 am
by StandardRANSUser001
Hi Finzo and Yvon,

Thanks for your advice on this.

I have implemented a few tensors that I would like to keep track of in run time, and I would like to check that I have correctly implemented the cs_field API correctly. This works has been somewhat based off the discussion at viewtopic.php?t=2963&start=10.

In these calculations, Sij and wij are the mean-strain rate and the mean-rotation tensors, and T1ij and T2ij are Pope's invariant tensors. I can view these fields in paraview after runtime.

In the current implementation, I gradually run out of memory as the program progresses. I am hoping that you could provide any guidance on how to better utilise the cs_field API in these user defined functions? In this work I have defined strain as a full tensor, rather than a symmetric one. This is so I can use for loops to iterate through the calculation of Pope's Invariants.

Any guidance on this would be greatly appreciated.

Best regards,
Sean Hanrahan


Additional Note:
In the cs_user_parameters.c file, I have included the following details.

/*Build new field*/

// Strain, Pope's Invariants - T1
int field_type = CS_FIELD_USER;
bool has_previous = true;
cs_field_t *f1, *f2, *f3, *f4, *f5;
f1 = cs_field_create("rij_buo", field_type, CS_MESH_LOCATION_CELLS, 6, has_previous);
f2 = cs_field_create("sij", field_type, CS_MESH_LOCATION_CELLS, 9, has_previous);
f3 = cs_field_create("wij", field_type, CS_MESH_LOCATION_CELLS, 9, has_previous);
f4 = cs_field_create("T1ij", field_type, CS_MESH_LOCATION_CELLS, 9, has_previous);
f5 = cs_field_create("T2ij", field_type, CS_MESH_LOCATION_CELLS, 9, has_previous);

int k_var = cs_field_key_id("first_moment_id");
const int k_post = cs_field_key_id("post_vis");
cs_field_set_key_int(f1, k_var, 1);
cs_field_lock_key(f1, k_var);
cs_field_set_key_int_bits(f1, k_post, CS_POST_ON_LOCATION);

cs_field_set_key_int(f2, k_var, 1);
cs_field_lock_key(f2, k_var);
cs_field_set_key_int_bits(f2, k_post, CS_POST_ON_LOCATION);

cs_field_set_key_int(f3, k_var, 1);
cs_field_lock_key(f3, k_var);
cs_field_set_key_int_bits(f3, k_post, CS_POST_ON_LOCATION);

cs_field_set_key_int(f4, k_var, 1);
cs_field_lock_key(f4, k_var);
cs_field_set_key_int_bits(f4, k_post, CS_POST_ON_LOCATION);

cs_field_set_key_int(f5, k_var, 1);
cs_field_lock_key(f5, k_var);
cs_field_set_key_int_bits(f5, k_post, CS_POST_ON_LOCATION);

Re: Calculating Velocity Gradient in Post-Processing

Posted: Wed Jun 19, 2024 12:56 am
by Yvan Fournier
Hello,

I have not looked at your files yet, but if you suspect a memory leak, add

Code: Select all

export CS_MEM_LOG=memtrace.log
In you execution environment, and when the code runs, it will log all allocations and frees using BFT_MALLOC and BFT_FREE.
Run a few iterations, check the list of non-freed variables at the end, and search backwards from the end of the file to see where this was allocated.

You can of course se more general tools, such as Valgrind ar a build with AddressSanitizer, bu the built-in option has low overhrad and is sufficient in many cases.

Also, in cs_user_extra_operations-vorticity_field.c, remove :
BFT_FREE(vort)
as this is not directly allocated using BFT_MALLOC in your user-defined file.

Best regards,

Yvan