Page 1 of 1

use variables defined in cs_user_modules.f90 in a C routine

Posted: Tue Mar 15, 2022 3:21 pm
by jtache
Hello,

I search in the forum and user guide but I didn't find an answer. I'm using CS6.1.
I would like to import a module defined in cs_user_modules.f90 (specially vars which are used by a number of other Fortran routines) in a C routine (cs_user_postprocess.c).
How could I do that?

Thanks for your help!

Jeremie

Re: use variables defined in cs_user_modules.f90 in a C routine

Posted: Wed Mar 16, 2022 12:48 am
by Yvan Fournier
Hello,

Actually, using "bind(C, name="c_name") with a Fortran variable declaration and declaring that in C should work.

For example, in Fortran:

Code: Select all

real(kind=c_double) :: w2
bind(C, name='c_w2'') :: w2
With the match in C:

Code: Select all

extern double c_w2;
Where in this case, the C name could be aligned with Fortran for clarity (w2 instead of c_w2)

If you have an array instead of a constant in Fortran passing a "modern Fortran" array (i.e. with a "dimension(:) :: x" type declaration) is more tricky than a basic "dimension(*) :: y" array, as the first type has metadata, while the second one is just a pointer. In that case, you need to check the iso_c_binding documentation for details. In that example, "c_loc(x)" can probably give you the pointer to raw data, as in C.

Also, if you have more complex structures, with allocatable or pointer members in Fortran, mapping them with iso_c_bindings may be more difficult, and not always possible, at least not 1 to 1.

Best regards,

Yvan

Re: use variables defined in cs_user_modules.f90 in a C routine

Posted: Mon Mar 21, 2022 5:00 pm
by jtache
Thank you very much for your detailed guideline.
Indeed it worked well with constant and arrays with constant dimensions.
I'm now in the process of working with allocatable arrays, it's a little bit more difficult...
I've seen some elements in EXAMPLES directory that should help me.

Best regards,
Jeremie

Re: use variables defined in cs_user_modules.f90 in a C routine

Posted: Wed Mar 23, 2022 2:19 pm
by jtache
Dear Yvan,

I am finally stuck with allocatable arrays in Fortran for using them in C.
I used sthe subroutines provided in the cs_user_module-user-arrays.f90 routine in EXAMPLES to declare, init and pass array pointer to C with th ehelp of c_loc().
I initialized my array in cs_user_initialization.f90 :

Code: Select all

CALL init_train_elt(a_number, ncel)
and tried to used this array in cs_user_postprocess.c.
Unfortunately I got an error message related to this pointer at execution :
SIGSEGV signal (forbidden memory area access) intercepted!
.
I don't understand what I did wrong maybe because I'm not a pure C-developper.
Could you please have a look on the sources and tell me if you see something strange?
Any help would be welcome.

Re: use variables defined in cs_user_modules.f90 in a C routine

Posted: Wed Mar 23, 2022 6:13 pm
by Yvan Fournier
Hello,

I thinks the issue may be due to the fact that you are using a double pointer in the C code. Handling Using a pointer of pointers is a complex way of handling multi-dimensional arrays in C, and requires allocating and computing values for the intermediate array). It is much simpler single-dimensional array, with a n equivalence between

c_array[j*m + i]

and

f_array(i, j)

when f_array's dimensions are (m, n)

Best regards,

Yvan

Re: use variables defined in cs_user_modules.f90 in a C routine

Posted: Thu Mar 24, 2022 11:30 am
by jtache
Hello Yvan,

I understand the limit. Rewriting my code with one-dimensional Fortran arrays (and simple pointer in C), it worked perfectly.
Thanks for your help!

Best regards,
Jeremie