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
use variables defined in cs_user_modules.f90 in a C routine
Forum rules
Please read the forum usage recommendations before posting.
Please read the forum usage recommendations before posting.
-
- Posts: 4208
- Joined: Mon Feb 20, 2012 3:25 pm
Re: use variables defined in cs_user_modules.f90 in a C routine
Hello,
Actually, using "bind(C, name="c_name") with a Fortran variable declaration and declaring that in C should work.
For example, in Fortran:
With the match in C:
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
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
Code: Select all
extern double 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
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
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
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 :
and tried to used this array in cs_user_postprocess.c.
Unfortunately I got an error message related to this pointer at execution :
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.
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)
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.
- Attachments
-
- cs_user_postprocess.c
- (4.27 KiB) Downloaded 167 times
-
- cs_user_modules.f90
- (3.26 KiB) Downloaded 178 times
-
- Posts: 4208
- Joined: Mon Feb 20, 2012 3:25 pm
Re: use variables defined in cs_user_modules.f90 in a C routine
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
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
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
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