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: 175
Joined: Fri Nov 20, 2020 4:09 am

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

Post by Tsubasa »

Hello Yvan,

At first I want to try your suggestion in ver.6, but is it easy to do this in ver.7?
If I use ver.6, where can I check the name of the matching field name, and use cs_field_by_name()?

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

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

Post by Yvan Fournier »

Hello,

Which of the various suggestions are you referring to ? Defining a time moment for the mass flux can only be done after v7.0 (in v7.1/Git master), at least in the usual places. It can probably be done in v7 or v6 in cs_user_finalize_setup instead of cs_user_time_moments.

Regards,

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

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

Post by Tsubasa »

Hello Yvan,

I referred the following suggestion.
Hello,

Yes, it is impossible in v7.0, or at least not guaranteed : you might be able to declare a time average for the mass flux if you add the call to cs_time_moment_define_by_field_ids not in cs_user_time_moments, but in cs_user_finalize_setup (both in cs_user_parameters.c). Not sure it will work, but there might be a chance.

In v7.1, you will be able to add the definition in the usual place (i.e. cs_user_time_moments).

Best regards,

Yvan
Now I am trying to put like the following code in cs_user_finalize_setup in the file of "cs_user_parameters.c" with ver.6 (of course I will edit the code for mass flux).

Code: Select all

{
    /* Moment <U> calculated starting from time step 1000. */
    /* resulting field is a vector */
    int moment_f_id[] = {CS_F_(vel)->id};
    int moment_c_id[] = {-1};
    int n_fields = 1;
    cs_time_moment_define_by_field_ids("U_mean",
                                       n_fields,
                                       moment_f_id,
                                       moment_c_id,
                                       CS_TIME_MOMENT_MEAN,
                                       1000, /* nt_start */
                                       -1,   /* t_start */
                                       CS_TIME_MOMENT_RESTART_AUTO,
                                       NULL);
Yvan Fournier
Posts: 4070
Joined: Mon Feb 20, 2012 3:25 pm

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

Post by Yvan Fournier »

Hello,

The code you added should activate the time moment for mean velocity (which can also be done simply with the GUI).

For the mass fluxes, you need the following:

Code: Select all

{
    int moment_f_id[] = {cs_field_by_name("inner_mass_flux")->id};
    int moment_c_id[] = {-1};
    int n_fields = 1;
    cs_time_moment_define_by_field_ids("inner_mass_flux_mean",
                                       n_fields,
                                       moment_f_id,
                                       moment_c_id,
                                       CS_TIME_MOMENT_MEAN,
                                       1000, /* nt_start */
                                       -1,   /* t_start */
                                       CS_TIME_MOMENT_RESTART_AUTO,
                                       NULL);
}
{
    int moment_f_id[] = {cs_field_by_name("boundary_mass_flux")->id};
    int moment_c_id[] = {-1};
    int n_fields = 1;
    cs_time_moment_define_by_field_ids("boundary_mass_flux_mean",
                                       n_fields,
                                       moment_f_id,
                                       moment_c_id,
                                       CS_TIME_MOMENT_MEAN,
                                       1000, /* nt_start */
                                       -1,   /* t_start */
                                       CS_TIME_MOMENT_RESTART_AUTO,
                                       NULL);
}
Best regards,

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

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

Post by Tsubasa »

Hello Yvan,

To output the results, I added the following codes.

Code: Select all

cs_field_set_key_int_bits(cs_field_by_name("inner_mass_flux_mean"),
                          cs_field_key_id("post_vis"),
                          CS_POST_ON_LOCATION);
cs_field_set_key_int_bits(cs_field_by_name("boundary_mass_flux_mean"),
                          cs_field_key_id("post_vis"),
                          CS_POST_ON_LOCATION);
However, only the "results.boundary_mass_flux_mean" can be obtained in postprocessing.
Are there some mistakes in the following code?
I wonder why I cannot get the output of "inner_mass_flux_mean" .

Code: Select all

void
cs_user_finalize_setup(cs_domain_t   *domain)
{
	{
	    int moment_f_id[] = {cs_field_by_name("inner_mass_flux")->id};
	    int moment_c_id[] = {-1};
	    int n_fields = 1;
	    cs_time_moment_define_by_field_ids("inner_mass_flux_mean",
	                                       n_fields,
 	                                       moment_f_id,
	                                       moment_c_id,
	                                       CS_TIME_MOMENT_MEAN,
	                                       1, /* nt_start */
	                                       -1,   /* t_start */
	                                       CS_TIME_MOMENT_RESTART_AUTO,
	                                       NULL);
	}
cs_field_set_key_int_bits(cs_field_by_name("inner_mass_flux_mean"),
                          cs_field_key_id("post_vis"),
                          CS_POST_ON_LOCATION);

	{
	    int moment_f_id[] = {cs_field_by_name("boundary_mass_flux")->id};
	    int moment_c_id[] = {-1};
	    int n_fields = 1;
	    cs_time_moment_define_by_field_ids("boundary_mass_flux_mean",
	                                       n_fields,
	                                       moment_f_id,
	                                       moment_c_id,
	                                       CS_TIME_MOMENT_MEAN,
	                                       1, /* nt_start */
	                                       -1,   /* t_start */
	                                       CS_TIME_MOMENT_RESTART_AUTO,
	                                       NULL);
	}
cs_field_set_key_int_bits(cs_field_by_name("boundary_mass_flux_mean"),
                          cs_field_key_id("post_vis"),
                          CS_POST_ON_LOCATION); 
}
Yvan Fournier
Posts: 4070
Joined: Mon Feb 20, 2012 3:25 pm

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

Post by Yvan Fournier »

Hello,

You cannot get the output of a a field defined on internal faces unless you define an output mesh on internal faces, and even then, you need to go through user subroutines. This is mostly intentional, as on any 3D mesh of significant size, you probably do not want to asks you visualization tool to display all internal faces, as this will be cluttered and really swamp your graphics.

If the field seems to appear in the logs, then all should be well. IF you really need to visualize it, you can do so with the cs_user_postprocess_values user-defiend function.

Regards,

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

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

Post by Tsubasa »

Hello Yvan,

OK, I got it.
As you said, visualization of mass flux is not important, but I tried it as a trial.

After I added your code in subroutine, a simulation can be done without errors,
and I found the "inner_mass_flux" and "boundary_mass_flux" in "setup.jog".

Code: Select all


Fields of type: postprocess
---------------

  Field                    Dim. Location             Id   Type flag
  ------------------------ ---- -------------------- ---- ---------
  inner_mass_flux_mean     1    interior_faces       18   48   (postprocess, accumulator)
  boundary_mass_flux_mean  1    boundary_faces       19   48   (postprocess, accumulator)
So next step is to edit the subroutine to read average velocity, average pressure and average mass flux when restarting for the scalar convection?

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

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

Post by Yvan Fournier »

Hello,

Yes, that is the next step.

As some fields are in the main restart file and others in the auxiliary one, renaming migh not work, so the simplest solution is to overwrite the values of the instant fields with the averaged ones (accessing fields by name is simplest) in cs_user_initialization.c or cs_user_initialization.f90.

Best regards,

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

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

Post by Tsubasa »

Hello Yvan,

Thank you for everything.

Is there a exmaple script to replace the instant value with average value in
https://www.code-saturne.org/cms/sites/ ... e7b6e9cc8a?

Do I need this kind of codes?

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");
Actually, I am completely new to C and Fortran, and I've just started to study them.
I would appreciate if you could let me know how to overwrite and tips,
and I am so sorry to disturb you many times.

Best regards,
Hamada
Yvan Fournier
Posts: 4070
Joined: Mon Feb 20, 2012 3:25 pm

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

Post by Yvan Fournier »

Hello,

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).

If it fails for some fields,

Code: Select all

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];
}
would be the solution (in cs_user_initialization.c). With equivalent code (and the correct sizes, n_cells for cells, n_i_faces and n_b_faces for faces) for mass fluxes.

Best regards,

Yvan
Post Reply