CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   Main CFD Forum (http://www.cfd-online.com/Forums/main/)
-   -   C - Fortran Interoperability (http://www.cfd-online.com/Forums/main/69238-c-fortran-interoperability.html)

mrp October 16, 2009 09:33

C - Fortran Interoperability
 
Hi all

Does anybody knows how to send a linked list from fortran to C, allocate this list in C and then recover the data in fortran?


Thanks in advance


Marcio

andy_ October 16, 2009 11:56

Fortran-C interfaces vary from compiler to compiler. You will have to look in the manual for the details for your particular compiler.

It may also help if you state why you want/need to use C to "allocate this list" and what that means exactly when you are mixing the two languages. The C approach and the Fortran approach for handling state are different. And what version of Fortran are you using?

mrp October 16, 2009 12:58

Hi, andy_. Thanks for your reply.

I'm working with intel compilers, version 11.1.046 for both C and Fortran. For C-Fortran interface I'm using the intrinsic
module iso_c_binding.

Here's the situation:

We have a solver for multiphase flows based on Structured Adaptive Mesh Refinement (SAMR) which is written in Fortran.
The interface between fluid phases will be modeled with front tracking. The geometric representation of the front tracking
will be done with the GNU Triangulated Surface Library (GTS), which is written in C. That is: normal and curvature computing,
surface smoothing/remeshing, among other tasks will be performed by GTS. The problem is that the SAMR code is heavily dependent
on linked lists. Therefore, sending information from GTS to the SAMR code requires linked lists. Since the main code is Fortran,
I have to create the linked list variable in the fortran part, then send it as an argument to the C function which will insert
the information in it. Inside the C function everything works fine, and the information is added to the list. However, when I
attempt to retrieve the data inside the fortran code I get just thrash data.

The C function receives a c_ptr variable to represent the list. Then, in the fortran part, I use c_f_pointer to transfer the data
to the actual linked list. I wrote the relevant part of the code bellow:

type(c_ptr) :: plist

type(list), pointer :: pointer_list
type(list), target :: target_list
! ......
call get_gts_mesh(file_name, plist)
! ......
call c_f_pointer(plist, target_list, [list_size])

pointer_list => target_list

do
if(.not.associated(pointer_list%next)) exit
!
!do some work with the list data

!
pointer_list=>pointer_list%next
end do


Any help will be wellcome


TIA


Marcio

andy_ October 16, 2009 16:36

I am not familiar with the details so please bear this in mind.

Are you expecting a single call to c_f_pointer to convert all the pointers inside the list from C pointers to Fortran pointers rather than simply the single base pointer? Is the structure of the list elements the same in the C code and the Fortran code? If so, how is this achieved?

mrp October 19, 2009 06:04

Actually I expected c_f_pointer could convert the entire list, but it surely does not. As for the structure, I create the struct and function in C. In the fortran part I just create a subroutine interface in which the list is declared as a c_ptr (which is defined in the module iso_c_binding) type. It works fine if I use arrays instead of lists, even if the arrays vars are declared in the fortran part and allocated in the C part.


All times are GMT -4. The time now is 16:02.