Doubts regarding cs_field_create

Questions and remarks about code_saturne usage
Forum rules
Please read the forum usage recommendations before posting.
sofenkumarjena
Posts: 35
Joined: Thu May 19, 2022 4:37 pm

Doubts regarding cs_field_create

Post by sofenkumarjena »

Hi Team,
I am new to code saturne. I need to create a 3*3 field and a scalar field
in cs_user_parameters.c file and access the same field, recompute its value
and gradient of its value in cs_user_source_term.c file.
I am not able to get some example to do that. Kindly provide me a minimal
example for the same, which will be helpful for me.

With Thanks and Regards

Sofen
Yvan Fournier
Posts: 4070
Joined: Mon Feb 20, 2012 3:25 pm

Re: Doubts regarding cs_field_create

Post by Yvan Fournier »

Hello,

A 3x3 field can be defined using a field dimension of 9 (which assumes this is a 3x3 non-symmetric tensor field).

The available examples for lower dimension can be used.

For a scalar field, there are examples in the documentation. See https://www.code-saturne.org/documentat ... eters.html for field (solved variable or property) creation, see examples with cs_user_extra_operations.c or cs_user_initialization for access/use of fields.

Regards,

Yvan
sofenkumarjena
Posts: 35
Joined: Thu May 19, 2022 4:37 pm

Re: Doubts regarding cs_field_create

Post by sofenkumarjena »

Hi Yvan, thanks for your response. I am not able to figure-out the issue. Kindly share a minimum working example. Nothing works for me using cs_field_create . Not able to call the field later.

Sofen
Yvan Fournier
Posts: 4070
Joined: Mon Feb 20, 2012 3:25 pm

Re: Doubts regarding cs_field_create

Post by Yvan Fournier »

Hello,

At what stage are you stuck ? I recommend proceeding one step at a time.

How do you try to use the field later ? The simplest way to access a field is to use cs_field_by_name, such as you will find in the src/user_examples directory (using search or grep), or in the Docygen documentation (a subset of the same examples).

Best regards,

Yvan
sofenkumarjena
Posts: 35
Joined: Thu May 19, 2022 4:37 pm

Re: Doubts regarding cs_field_create

Post by sofenkumarjena »

Thanks Yvan,
I am defining a field in the cs_user_parameters.c file as follows. RIJ_buo is a 3x3 variable, hence used total variable as 9 in the cs_field_create function third argument

#pragma weak cs_user_parameters
void
cs_user_parameters(cs_domain_t *domain)
{
int field_type = CS_FIELD_USER;
bool has_previous = true;
cs_field_t *f1 = cs_field_create("RIJ_buo", field_type, CS_MESH_LOCATION_CELLS, 9, has_previous);
cs_field_set_key_int(f1, cs_field_key_id("rij_buo"), 1);
CS_UNUSED(domain);
}

I need to initialize this field value to some finite value at cs_user_initialization.c file, hence I used the following command .
#pragma weak cs_user_initialization
void
cs_user_initialization(cs_domain_t *domain)
{
const cs_lnum_t n_cells = cs_glob_mesh->n_cells;
const cs_real_t *cell_f_vol = cs_glob_mesh_quantities->cell_vol;
cs_real_t *rijbuo = cs_field_by_name("RIJ_buo")->val;
for (cs_lnum_t n = 0; n < n_cells; n++)
{
rijbuo[9*n + 0] = 0.0;
}
CS_UNUSED(domain);
}

Further I need to compute and save the values of this field in cs_user_source_terms.c file after each time step and calculate the divergence of this field. However I am getting error message doing so. Can you help me with minimum working example to figure out this problem.

With Regards

Sofen
Yvan Fournier
Posts: 4070
Joined: Mon Feb 20, 2012 3:25 pm

Re: Doubts regarding cs_field_create

Post by Yvan Fournier »

Hello,

There are 2 issues which are probably not directly related to your error but which should be adressed:

- remove the "pragma weak" lines. These are present on the reference files so as to reduce their priority relative to the user-defined ones on some systems. They should not br present in user code, and are removed from the templates in the installation directory (which you can add through the GUI).

- in cs_user_initialization, you are only initializing the first component of the tensor (rijbuo[9*n + 0]). Also, by defaults, all fields are initialized to 0, so this initialization is not useful if you only need to set a field to 0.

Also, please use the "code" feture of this forum to keep Posted code indented. Otherwise, it is harder to read and check the code.

Also, is the field you are using supposed to be a solved variable or an auxiliary property ?

Finally, what error message do you get (please check the forum usage recommendations fo details on information to provide).

Regards,

Yvan
sofenkumarjena
Posts: 35
Joined: Thu May 19, 2022 4:37 pm

Re: Doubts regarding cs_field_create

Post by sofenkumarjena »

Thanks Yvan for the reply. The problem is not yet solve. Let me put minimal working example for details understanding.
Problem statement: Want to create a user field, compute and store its value at each time step, and calculate the divergence of the field for some source term computation.
I named the field as : "rijbuo". I defined the field in the cs_users_parameters.c file as follows
BEGIN_C_DECLS
void cs_user_parameters(cs_domain_t *domain)
{
int field_type = CS_FIELD_USER;
bool has_previous = true;
cs_field_t *f1;
f1 = cs_field_create("rij_buo", field_type, CS_MESH_LOCATION_CELLS, 6, has_previous);
int k_var = cs_field_key_id("first_moment_id");
cs_field_set_key_int(f1, k_var, 1);
cs_field_lock_key(f1, k_var);
CS_UNUSED(domain);
}
END_C_DECLS

Then initialize the user field in the cs_user_initialization.c file as follows

BEGIN_C_DECLS
void cs_user_initialization(cs_domain_t *domain)
{
const cs_lnum_t n_cells = cs_glob_mesh->n_cells;
const cs_real_t *cell_f_vol = cs_glob_mesh_quantities->cell_vol;
cs_field_t *rijbuo_init = cs_field_by_name("rij_buo");
for (cs_lnum_t n = 0; n < n_cells; n++)
{
rijbuo_init->val[6*n+0] = 0.1;
rijbuo_init->val[6*n+1] = 0.2;
rijbuo_init->val[6*n+2] = 0.3;
rijbuo_init->val[6*n+3] = 0.4;
rijbuo_init->val[6*n+4] = 0.5;
rijbuo_init->val[6*n+5] = 0.6;
}
CS_UNUSED(domain);
}

END_C_DECLS

But while doing the postprocessing, I am not able to find the above field in paraview. If a field is created and initialize or computed it must available for post processing right ? Where is the issue ? If you can explain in details, it would have helpful. I stuck here for a long time. Please help me with minimal working example.

With Regards

SOfen
Yvan Fournier
Posts: 4070
Joined: Mon Feb 20, 2012 3:25 pm

Re: Doubts regarding cs_field_create

Post by Yvan Fournier »

Hello,

To activate post-processing for a given field, you need to also add :

Code: Select all

  const int k_post = cs_field_key_id("post_vis");

  cs_field_set_key_int_bits(f, k_post, CS_POST_ON_LOCATION);
Where f can be obtained in the usual manner (cs_field_by_name).

For probes, you cal also add:

Code: Select all

   cs_field_set_key_int_bits(f, k_post, CS_POST_MONITOR);
If you define a field as a property (using cs_parameters_add_property instead of cs_field_create), some such settings might be set by default (they are for solved variables, I don't remember for properties).

For the divergence, see the cs_divergence series function, but to call it, you need to compute other arrays first. I did not see a directly usable example in the existing user examples, so the best solution is to "grep" for cs_divergence or cs_tensor_divergence in the source code and see how that is used.

Regards,

Yvan
sofenkumarjena
Posts: 35
Joined: Thu May 19, 2022 4:37 pm

Re: Doubts regarding cs_field_create

Post by sofenkumarjena »

Thanks Yvan for the response. After few trials, things are working. I am facing one more issue. I am computing the gradient of the user filed. My user field is defined at cell center. to compute the gradient, I too need to define the value of the user field over the boundary location. Can you please inform how to do looping the value of the user field for all boundary cells.

Sofen
Yvan Fournier
Posts: 4070
Joined: Mon Feb 20, 2012 3:25 pm

Re: Doubts regarding cs_field_create

Post by Yvan Fournier »

Hello,

For "solved variable" fields, the boundary conditions are those you define. For property fields, a homogeneous Neumann is assumed. To provide different boundary condition types, you need to use lower-level functions than cs_field_gradient... (directly use cs_gradient_...) In this case, you need to build the coefa and coefb arrays.

You can look at the code of the cs_field_gradient function (in cs_field_operator.c) for the call to the lower-level functions, and the theory documentation and/or code in condli.f90 to see how the coefa/coefb can be used.

Regards,

Yvan
Post Reply