How to calculate scalar based on frozen average velocity?

Questions and remarks about code_saturne usage
Forum rules
Please read the forum usage recommendations before posting.
Tsubasa
Posts: 130
Joined: Fri Nov 20, 2020 4:09 am

Re: How to calculate scalar based on frozen average velocity?

Post by Tsubasa »

Hello Yvan,

What do you mean "this" in the following tips?
You can try this (in cs_user_time_moments) , but as I was explaining, since the cell velocity and ts time moment are not stored in the same restart file, this might not work (the best solution is to try and check).
Does "this" mean the following code?

Code: Select all

  const int kr = cs_field_key_id_try("restart_name");
  cs_field_t *vel = cs_field_by_name("velocity");
  cs_field_set_key_str(vel, kr, "ave_vel");
Sorry this is a tribial question.

Best regards,
Tsubasa
Yvan Fournier
Posts: 3498
Joined: Mon Feb 20, 2012 3:25 pm

Re: How to calculate scalar based on frozen average velocity?

Post by Yvan Fournier »

Hello,

Yes, it was the code I was referring to.

Best regards,

Yvan
Tsubasa
Posts: 130
Joined: Fri Nov 20, 2020 4:09 am

Re: How to calculate scalar based on frozen average velocity?

Post by Tsubasa »

Hello Yvan,

As far as I understand from a previous your post, by adding the following code in "cs_user_initialization.c", instantaneou velocity field is replaced with mean velocity field, isn't it? So scalar transport can be calculated in average field, can't it? And by the same manner, other instantaneous fields can be replaced with average fields, correct?

Code: Select all

void
cs_user_initialization(cs_domain_t     *domain)
{
  cs_field_t *vel = cs_field_by_name("velocity");
  cs_field_t *vel_m = cs_field_by_name("velocity");

  for (cs_lnum_t i = 0; i < cs_glob_mesh->n_cells; i++) {
  vel->val[i] = vel_m->val[i];
 }
}
I did it for velocity field at a first test (I did not apply to other fields now), but it seemes that scalar transport was calculated in instataneous velocity field.
Would you give me tips?

Best regards,
Tsubasa
Yvan Fournier
Posts: 3498
Joined: Mon Feb 20, 2012 3:25 pm

Re: How to calculate scalar based on frozen average velocity?

Post by Yvan Fournier »

Hello,

You code is almost correct (I did not check whether the error is in the example I gave or in your adaptation).

In you piece of code, for vel_m, the name of the field you access should not be "velocity" (the instantaneous value), but the mean velocity field (whose name may depend on your setup, but which you can easily search for in a previous run's "setup.log" file.

After that, you'll need to do the same for mass fluxes, but doing one step at a time is safe.

Best regards,

Yvan
Tsubasa
Posts: 130
Joined: Fri Nov 20, 2020 4:09 am

Re: How to calculate scalar based on frozen average velocity?

Post by Tsubasa »

Hello Yvan,

To activate the code correctly, I changed the name of vel_m to "meanVel" which I set up in GUI. These are the code and "setup.log" of a previous computation.

Code: Select all

void
cs_user_initialization(cs_domain_t     *domain)
{
  cs_field_t *vel = cs_field_by_name("velocity");
  cs_field_t *vel_m = cs_field_by_name("meanVel");

  for (cs_lnum_t i = 0; i < cs_glob_mesh->n_cells; i++) {
  vel->val[i] = vel_m->val[i];
 }
}

Code: Select all

  Field                    Dim. Location             Id   Type flag
  ------------------------ ---- -------------------- ---- ---------
  meanVel                  3    cells                15   48   (postprocess, accumulator)
  meanPre                  1    cells                16   48   (postprocess, accumulator)
  inner_mass_flux_mean     1    interior_faces       20   48   (postprocess, accumulator)
  boundary_mass_flux_mean  1    boundary_faces       21   48   (postprocess, accumulator)
These code can be carried out without errors when computing.
However, in visualization by Paraview, something strange happend.
When I checked the instantaneous velocity field (this is should be "meanVel" field of the previsou computation), the field seems to a field mixed with instantanesou and mean velocity filed. You can see this in following pictures. First and second pictures are instantanesou and mean velocity field at last time step in the previous computation. Third picture is a "instantaneous" field which should be meanVel field.
For some meshs, meanVel field is taken into account, but instantanesou field also can be seen.
Are there some mistakes?
vel_instantaneous.png
vel_mean.png
vel_scalar.png
Best regards,
Tsubasa
Yvan Fournier
Posts: 3498
Joined: Mon Feb 20, 2012 3:25 pm

Re: How to calculate scalar based on frozen average velocity?

Post by Yvan Fournier »

Hello,

If you are running in parallel, just as a precaution, you can replace "n_cells" with 'n_cells_with_ghosts' in the loop where you copy mean to instantaneous velocity. just to make sure ghost cells are handled immediately an side effects are avoided.

If this is not enough, could you post the run_solver.log file for 10 or 20 restarted iterations ?

Also, the instant/mean mass flux may have an impact. So first, can you do the same operation (copy) of mean to instantaneous values for the mass fluxes ?

Best regards,

Yvan
Tsubasa
Posts: 130
Joined: Fri Nov 20, 2020 4:09 am

Re: How to calculate scalar based on frozen average velocity?

Post by Tsubasa »

Hello,

I changed the name to "n_cells_with_ghosts",
but same problem can be seen.
You can see the log file.
run_solver.log
(129.49 KiB) Downloaded 8 times
_________________________________________________________________

For mass flux. is the code correct below?
I am not sure how it should be wrriten for " inner_mass_flux->val = inner_mass_flux_m->val" and "inner_mass_flux_m".

Code: Select all

void
cs_user_initialization(cs_domain_t     *domain)
{
  cs_field_t *vel = cs_field_by_name("velocity");
  cs_field_t *vel_m = cs_field_by_name("meanVel");

  cs_field_t *inner_mass_flux = cs_field_by_name("inner_mass_flux");
  cs_field_t *inner_mass_flux_m = cs_field_by_name("inner_mass_flux_mean");

  cs_field_t *boundary_mass_flux = cs_field_by_name("boundary_mass_flux");
  cs_field_t *boundary_mass_flux_m = cs_field_by_name("boundary_mass_flux_mean");

  for (cs_lnum_t i = 0; i < cs_glob_mesh->n_cells_with_ghosts; i++) {
  vel->val[i] = vel_m->val[i];
  inner_mass_flux->val[i] = inner_mass_flux_m->val[i];
  boundary_mass_flux->val[i] = boundary_mass_flux_m->val[i];
 }
}
Would you check this?
At least now, some error message can be seen by this code.

_______________________________________________________________________

Whwn I use the following code to check where the code works or not, it seems that part of instantaneous velocity field was replaced with new value (in this case:1e5). You can see the result in picture. Blue zone cannot be replaced by mean velocity.
In my case, computation was done with 4 parallelization. I checked how it works without parallelization, and same problem happend. So problem is not due to parallelization.

Code: Select all

void
cs_user_initialization(cs_domain_t     *domain)
{
  cs_field_t *vel = cs_field_by_name("velocity");
  cs_field_t *vel_m = cs_field_by_name("meanVel");

  for (cs_lnum_t i = 0; i < cs_glob_mesh->n_cells; i++) {
  vel->val[i] = 1e5; 
 }
}
a.png

Best regards,
Hamada
Last edited by Tsubasa on Sun Sep 05, 2021 3:17 pm, edited 1 time in total.
Yvan Fournier
Posts: 3498
Joined: Mon Feb 20, 2012 3:25 pm

Re: How to calculate scalar based on frozen average velocity?

Post by Yvan Fournier »

Hello,

This is quite strange. Would you have a small version of the test case (including mesh and restart files) I could check this on ?

Regards,

Yvan
Tsubasa
Posts: 130
Joined: Fri Nov 20, 2020 4:09 am

Re: How to calculate scalar based on frozen average velocity?

Post by Tsubasa »

Hello Yvan,

You can download the directry below.
Becasue the file size is large if I include mesh and restarting file, I upload separately mesh, "before restarting file" and "after restarting file". You can get the result by just using setup.xml which I already set up.
https://filesender.renater.fr/?s=downlo ... b79116fd82

If you need other files, please let me know.

Best regards,
Tsubasa
Yvan Fournier
Posts: 3498
Joined: Mon Feb 20, 2012 3:25 pm

Re: How to calculate scalar based on frozen average velocity?

Post by Yvan Fournier »

Hello,

I downloaded the files, and did not run the case yet, but looking at them, I probably have a simple explanation:

Since velocity is a vector field, you need to copy 3 values per cell, no just one. So you are only copying the first 1/3 of values.
The following version should work better:

Code: Select all

void
cs_user_initialization(cs_domain_t     *domain)
{
  cs_field_t *vel = cs_field_by_name("velocity");
  cs_field_t *vel_m = cs_field_by_name("meanVel");

  cs_lnum_t n_vals = cs_glob_mesh->n_cells * ve->dim;  // n_cells * 3 for vector field
  for (cs_lnum_t i = 0; i < n_vals; i++) {
    vel->val[i] = 1e5; 
 }
}
Best regards,

Yvan
Post Reply