How to get coefa and coefb values of user created field ?

Questions and remarks about code_saturne usage
Forum rules
Please read the forum usage recommendations before posting.
Post Reply
kdbs
Posts: 3
Joined: Fri Jun 04, 2021 2:14 pm

How to get coefa and coefb values of user created field ?

Post by kdbs »

Hello,

I want to compute the gradient of a user created scalar field by using the function gradient_s.

My concern is about how to get coefa and coefb of this field, do I have to manually create it based on the boundary conditions or are they automatically stored somewhere ? I took a look at different posts on this forum and I don't really understand what they stand for...

If it is possible to have a basic example, it would certainly help me a lot... Thanks.

PS : I'm working on CS 6.0.6

Best regards,

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

Re: How to get coefa and coefb values of user created field ?

Post by Yvan Fournier »

Hello,

If you are using a field, why do you need the low-level gradient_s an not the simpler-to use field_gradient_scalar function ?

Best regards,

Yvan
kdbs
Posts: 3
Joined: Fri Jun 04, 2021 2:14 pm

Re: How to get coefa and coefb values of user created field ?

Post by kdbs »

Hi,

Well, I thought that using field_gradient_scalar was impossible for fields which are not fluid variables (like velocity, pressure,...) Plus I didn't know the ID of my field.

But now I found the ID (in setup.log) and tried to use field_gradient_scalar. It did not work (see the attached error.txt file).

Just to make sure, I also tried with a fluid variable, ivarfl(ik), which works fine.

I also attached the routine where I intend to compute the gradient of my field (see line 349).

Note that I tried to pass f_id as argument of call field_gradient_scalar(...) with setting the value f_id prior to the call, of course.
Maybe the problem is about defining boundary conditions for my field.

Thanks in advance for your help,
Killian
Attachments
cs_user_source_terms.f90
(38.56 KiB) Downloaded 98 times
error.txt
(1.14 KiB) Downloaded 99 times
Yvan Fournier
Posts: 4070
Joined: Mon Feb 20, 2012 3:25 pm

Re: How to get coefa and coefb values of user created field ?

Post by Yvan Fournier »

Hello,

You can obtain a field's id by its name programmatically using "field_get_id" in Fortran or "cs_field_id_by_name" in C (or the field structure using cs_field_by_name in C).

In older versions of the code, a field needed to be a variable to compute is gradient using field_gradient_scalar, but in more recent versions, if no boundary conditions are available (which is the case when the field does not represent a solved variable), default homogeneous Neumann conditions should be used.
This change dates back to November 2018, so any v6.0 release should handle this in theory.

Do you have a small test case so I can check if this is the cause of the crash ? Unless you have the possibility of running a debug build (configured with --enable-debug) under a debugger, in which case you can check directly where exactly the crash happens.

Best regards,

Yvan
kdbs
Posts: 3
Joined: Fri Jun 04, 2021 2:14 pm

Re: How to get coefa and coefb values of user created field ?

Post by kdbs »

Hello,

Since I don't currently have a debugger installed, I send you the files of a small test case in a .zip file (including MESH/ file, DATA/ files with the setup.xml, and the SRC/ files). I really look forward to understand the cause of the crash.

Thanks a lot for your help.

Regards,

Killian
Attachments
test_case.zip
(885.92 KiB) Downloaded 96 times
Yvan Fournier
Posts: 4070
Joined: Mon Feb 20, 2012 3:25 pm

Re: How to get coefa and coefb values of user created field ?

Post by Yvan Fournier »

Hello,

The problem is not due to coefa and coefb, but other fields.

In your user source terms, you modify imrgra, which should not be done after cs_user_parameters.
This leads to trying to use cocg terms (see theoretical doc if you need details, otherwise it is simply a cell covariance array which is computed and stored differently depending on the gradient type).

Starting with version 6.1 (and switching to v7.0 is recommended), this could probably work, as these coefficients are computed "on demand". In v6.0, they are computed based on initial options, so if you change those options (imgra) later, you will get a crash.

Also, some of your variables are not initialized in cs_user_source_terms.f90, which also lead to incorrect tests and crashes:
- iprev
- nswrgp (in the call to gradient_s)
- there might be more (fixing iprev leads to nswrgp, I didi not check further)

Without those, you get crashes for other reasons, so I'll let you check those.

Besides proofreading your code or systematically initializing variables, this sort of error is easy to locate and fix using a debug build of the code and Valgrind. Without those tools, good luck...

Best regards,

Yvan
Post Reply