## How to calculate scalar based on frozen average velocity?

Questions and remarks about code_saturne usage
Forum rules
Tsubasa
Posts: 131
Joined: Fri Nov 20, 2020 4:09 am

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

Hello Yvan,

Yes, your code works very well, at least for velocity field.
Probably you just mistyped "ve" instead of "vel" in

Code: Select all

``cs_lnum_t n_vals = cs_glob_mesh->n_cells * ve->dim``
Let me check one aspect in;

Code: Select all

``cs_glob_mesh->n_cells * ve->dim``
This "*" is not pointer, is it?
Does it mean just "multiplication"?
So the code means "the number of cells *(time) 3", doesn't it?
___________________________________________________________________________

In next step, I will do it for "mass flux".
SInce the dimention of mass flux is 1 dimension,
I guess the previsou code should work, but there is an error message when compilng.
Should I also change something for mass flux?

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");

cs_lnum_t n_vals = cs_glob_mesh->n_cells * vel->dim;  // n_cells * 3 for vector field
for (cs_lnum_t i = 0; i < n_vals; i++) {
vel->val[i] = vel_m->val[i];
}
for (cs_lnum_t i = 0; i < cs_glob_mesh->n_cells; i++) {
inner_mass_flux->val[i] = inner_mass_flux_m->val[i];
boundary_mass_flux->val[i] = boundary_mass_flux_m->val[i];
}
}
``````
I think the problem comes from

Code: Select all

``boundary_mass_flux->val[i] = boundary_mass_flux_m->val[i];``
becasue the code workd without this line.

I think, the number of cell for boundary mass flux is not same to inner mass flux and velocities.
For boundary mass flux, we should code "cs_glob_mesh->n_b_cells" instead "cs_glob_mesh->n_cells", right?
At least now, this code works.
___________________________________________________________________________

Also I want to check one thing.
For inner mass flux, is the number of cells "cs_glob_mesh->n_cells"?
I am worrying that there is a symbol for "the numbre of inner cell"? (e.g. n_b_cells for boundary mass flux)

Best regares,
Tsubasa
Last edited by Tsubasa on Mon Sep 06, 2021 1:28 pm, edited 1 time in total.
Tsubasa
Posts: 131
Joined: Fri Nov 20, 2020 4:09 am

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

Hello Yvan,

I would like to let you know that,
to overrite instant fields to average fields,
I finally used this code, which I think it is correct,

Code: Select all

``````void
cs_user_initialization(cs_domain_t     *domain)
{
// for velocity
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 * vel->dim;  // n_cells * 3 for vector field
for (cs_lnum_t i = 0; i < n_vals; i++) {
vel->val[i] = vel_m->val[i];
}

// for pressure
cs_field_t *pressure = cs_field_by_name("pressure");
cs_field_t *pressure_m = cs_field_by_name("meanPre");
for (cs_lnum_t i = 0; i < cs_glob_mesh->n_cells; i++) {
pressure->val[i] = pressure_m->val[i];
}

// for innar mass flux
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");
for (cs_lnum_t i = 0; i < cs_glob_mesh->n_cells; i++) {
inner_mass_flux->val[i] = inner_mass_flux_m->val[i];
}

// for boundary mass flux
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_b_cells; i++) {
boundary_mass_flux->val[i] = boundary_mass_flux_m->val[i];
}
}
``````
This code works, and it was succeeded to overwrite them.
However, strange things happned.
When I check the scalar transport, the scalar does not flow along with the average flow.
It goes along with the instant field at last time step before our code was applied.
As you can see, the instant velocity field is replaced with average flow,
but the scalr transport goes to different direction.
And finally the scalar magnitude become divergence like 1.0e10 and -1.0e10 in almost all over the cells as can be seen in next post.

For scalar transport, I did
1st: noram simulation to calculate average flow without scalar
2nd: simulation with frozen velocity field without scalar just in case to make sure overrwiting
3rd: simulation wirh frozen velocity field with scalar transport.
Would you give me tips?

Best regards,
Tsubasa
Tsubasa
Posts: 131
Joined: Fri Nov 20, 2020 4:09 am

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

This is a scalar transport which became divergent finally.
Yvan Fournier
Posts: 3525
Joined: Mon Feb 20, 2012 3:25 pm

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

Hello,

You code is almost correct, but for the mass fluxes:

- The loop shoud be on n_i_faces (not n_cells) for the interior faces mass flow
- The loop shoud be on n_b_faces (not n_b_cells) for the boundary faces mass flow

Once this is corrected let's see how the code behaves.

Also, the pressure should not be important (I think), because in a frozen velocity scheme, it is not required anymore (unless you compute some specific pressure-depedent forces).

Best regards,

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

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

Hello Yvan,

Thanks to your help, I finally succeeded in getting scalar transport on frozen average field.
Thank you so much for your help.

I post the code just in case when someone want to do same thing;

Code: Select all

``````void
cs_user_initialization(cs_domain_t     *domain)
{
// for velocity
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 * vel->dim;  // n_cells * 3 for vector field
for (cs_lnum_t i = 0; i < n_vals; i++) {
vel->val[i] = vel_m->val[i];
}

// for pressure
//cs_field_t *pressure = cs_field_by_name("pressure");
//cs_field_t *pressure_m = cs_field_by_name("meanPre");
//for (cs_lnum_t i = 0; i < cs_glob_mesh->n_cells; i++) {
//pressure->val[i] = pressure_m->val[i];
//}

// for innar mass flux
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");
for (cs_lnum_t i = 0; i < cs_glob_mesh->n_i_faces ; i++) {
inner_mass_flux->val[i] = inner_mass_flux_m->val[i];
}

// for boundary mass flux
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_b_faces ; i++) {
boundary_mass_flux->val[i] = boundary_mass_flux_m->val[i];
}
}``````
Best regards,
Tsubasa
Tsubasa
Posts: 131
Joined: Fri Nov 20, 2020 4:09 am

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

Hello Yvan,

Let me check a thing for scalar transport.

Thanks for you, I suceeded to caculate scalar transport on frozen velocity.
Now, I am littele bit confused about "Prescribed (outgoing) flux" for scalar in GUI.
In here, I want scalar to be flow out naturally, not forcibly.
So I just impose Neumann condition (0 gradient) for outlet boundary. In this case, should I put "0.0" in "flux" in GUI, shouldn't I? or Does "0.0" mean "no flux" (equal to wall condition) for outlet boudanry?

Best regards,
Tsubasa