Hi,
I just came upon a situation for which I can not invent a "perfect" Fortran
solution:
I have this library function which performs some operation on the (x,y)
coordinates of N data points. It however does not accept these coordinates
as an argument (an array of extent (2,N)), but rather asks for a dummy
procedure argument, call it get_coords, which has the prescribed interface:
INTERFACE
function library_function(get_coords,...)
interface
function get_coords(point) result(coords)
integer, intent(in) :: point
real, dimension(2) :: coords
end function get_coords
end interface
...
end function library_function
END INTERFACE
I am writing my own function which needs to use this library function, but I
want my function to accept the coordinates as a rank-2 *assumed-shape* array
(this may change the answer to my question). The problem arises of how to
make the above function get_coords so that is *shares* the coordinate array
with my function.
*If* we could pass an internal argument as an actual argument for a
procedure dummy argument, my program would look like:
subroutine my_func(coords_array,...)
real, dimension(:,:) :: coords_array ! Will be of size (2,N)
...
call library_function(get_coords,...)
...
contains
function get_coords(point) result(coords)
integer, intent(in) :: point
real, dimension(2) :: coords
coords=coords_array(1:2,point) ! This is what it is all about!
end function get_coords
end subroutine my_func
But of course this is illegal in standard Fortran. Is there any other way to
share the coordinates between these two procedures. If the coordinate array
was an assumed-size array, I guess one can somehow share the first address
of the array.
But for assumed-shape arrays the only thing I can think of is a
pointer-based approach:
module my_module
private
real, pointer, dimension(:,:) :: p_coords_array ! A pointer
...
! My syntax may be wrong here. It's been a while since I've used array
pointers
contains
subroutine my_func(coords_array,...)
real, dimension(:,:):: coords_array ! Will be of size (2,N)
target :: coords_array ! ???
...
p_coords_array=>coords_array
call library_function(get_coords,...)
...
end subroutine my_func
function get_coords(point) result(coords)
integer, intent(in) :: point
real, dimension(2) :: coords
coords=p_coords_array(1:2,point) ! This is what it is all about!
end function get_coords
end module my_module
Do you see another way?
Thanks,
Aleksandar
_____________________________________________
Aleksandar Donev
http://www.pa.msu.edu/~donev/
[log in to unmask]
(517) 432-6770
Department of Physics and Astronomy
Michigan State University
East Lansing, MI 48824-1116
_____________________________________________
|