CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > General Forums > Main CFD Forum

C - Fortran Interoperability

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   October 16, 2009, 10:33
Default C - Fortran Interoperability
  #1
mrp
New Member
 
Join Date: Mar 2009
Posts: 4
Rep Power: 17
mrp is on a distinguished road
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
mrp is offline   Reply With Quote

Old   October 16, 2009, 12:56
Default
  #2
Senior Member
 
andy
Join Date: May 2009
Posts: 322
Rep Power: 18
andy_ is on a distinguished road
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?
andy_ is offline   Reply With Quote

Old   October 16, 2009, 13:58
Default
  #3
mrp
New Member
 
Join Date: Mar 2009
Posts: 4
Rep Power: 17
mrp is on a distinguished road
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
mrp is offline   Reply With Quote

Old   October 16, 2009, 17:36
Default
  #4
Senior Member
 
andy
Join Date: May 2009
Posts: 322
Rep Power: 18
andy_ is on a distinguished road
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?
andy_ is offline   Reply With Quote

Old   October 19, 2009, 07:04
Default
  #5
mrp
New Member
 
Join Date: Mar 2009
Posts: 4
Rep Power: 17
mrp is on a distinguished road
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.

Last edited by mrp; October 19, 2009 at 07:23.
mrp is offline   Reply With Quote

Reply

Tags
c-fortran, fortran, linked list


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Double precision & User Fortran Martijn CFX 3 April 4, 2009 06:43
Intrinsic Procedure 'ISNAN' in GNU FORTRAN 77 hawk Main CFD Forum 1 April 12, 2005 23:13
visual fortran Monica Main CFD Forum 1 August 28, 2004 21:45
Fortran77 or Fortran 90 Swapnil CFX 2 November 26, 2002 16:16
Why Favoring Fortran over C/C++? Zi-Wei Chiou Main CFD Forum 35 September 26, 2001 10:34


All times are GMT -4. The time now is 15:58.