Use array valued function as argument to another function - arrays

I have transformed a problem from a more complex f90-code into the following:
module userfunctions
implicit none
contains
function Function1(Argument,ArgumentSize)
! Input
integer, intent(in) :: ArgumentSize
real,dimension(ArgumentSize),intent(in) :: Argument
! Output
real,dimension(ArgumentSize) :: Function1
! Local
integer :: i
Function1 = Argument
! Random operation on argument, resembling generic vector function
do i=1,ArgumentSize
Function1(i) = Function1(i)**2
end do
end function Function1
function Function2(RandomFunction,Argument,ArgumentSize)
! Input
integer, intent(in) :: ArgumentSize
real,dimension(ArgumentSize), intent(in) :: Argument
! Array-type function of dimension ArgumentSize
real,external :: RandomFunction
! Evaluate RandomFunction to
real,dimension(ArgumentSize) :: Value
! Output
real :: Function2
! assign evaluation of RandomFunction to Value
Value = RandomFunction(Argument,ArgumentSize)
Function2 = dot_product(Value,Value)
end function Function2
end module userfunctions
program Fortran_Console_002
use userfunctions
implicit none
real :: Result1
real,dimension(6) :: Vector1
Vector1 = 2
Result1 = Function2(Function1,Vector1,6)
write(*,*) Result1
end program Fortran_Console_002
The result should be "96". Compiling this with Visual Studio 2013 and Intel Fortran produces the following error:
Error 1 error #6634: The shape matching rules of actual arguments and
dummy arguments have been violated. [FUNCTION1]
In real context I do need to pass an array-valued function from a subroutine to a function defined in a module (a solver for nonlinear functions which takes functions as arguments). I do know how to do this for a scalar-valued function, but failed to apply this to an array.
I use Visual Studio 2013 for this because of convenience. The real piece has to be compiled using Compaq Visual Fortran 6 on a virtual machine which is, as of my knowledge, only compatible up to fortran90.

See How to pass subroutine names as arguments in Fortran? for general rules.
Don't use external here. It is incompatible with advanced features like array valued functions. Generally, external is only for old programs in FORTRAN 77 style. Make an interface block (see the link) or try
procedure(Function1) :: RandomFunction
instead (Fortran 2003).

Related

Passing an array in Fortran function without size argument [duplicate]

This question already has an answer here:
Module calling an external procedure with implicit interface
(1 answer)
Closed 5 years ago.
I am completely new to Fortran 90 and I am trying to understand how to pass an array to a function. I looked on the web and I could not find any clear and simple enough example, so I decided to post here.
I would like the function be able to work on an array of any length (the length of the array should not be one of the parameters of the functions).
I tried to write a simple example of a function that returns the sum of the elements of an array:
function mysum(arr)
implicit none
real, dimension(:), intent(in) :: arr
real :: mysum
integer :: i,arrsize
arrsize = size(arr)
mysum=0.0
do i=1,arrsize
mysum=mysum+arr(i)
enddo
end function mysum
program test
implicit none
real, dimension(4) :: a
real :: mysum,a_sum
call random_number(a)
print *,a
a_sum=mysum(a)
print *,a_sum
end program
When I try to compile, I get the following error:
array_test.f90:17.14:
real mysum,a_sum
1
Error: Procedure 'mysum' at (1) with assumed-shape dummy argument 'arr' must have an explicit interface
What is the problem with my program?
Assumed shape dummy arguments (those with (:)) require explicit interface to the procedure to be available at the call site. That means the calling code must know how exactly the subroutine header looks like. See also Module calling an external procedure with implicit interface
That explicit interface can be provided in several ways
1.
preferred - a module procedure
module procedures
implicit none
contains
function mysum(arr)
real, dimension(:), intent(in) :: arr
real :: mysum
integer :: i,arrsize
arrsize = size(arr)
mysum=0.0
do i=1,arrsize
mysum=mysum+arr(i)
enddo
end function mysum
end module
program test
use procedures
implicit none
!no mysum declared here, it comes from the module
...
end program
2.
internal procedure - only for short simple procedures or if the procedure needs access to the host's variables. Because of the access to the host variables it is error-prone.
program test
implicit none
!no a_sum declared here, it is visible below contains
...
contains
function mysum(arr)
!implicit none inherited from the program
real, dimension(:), intent(in) :: arr
real :: mysum
integer :: i,arrsize
arrsize = size(arr)
mysum=0.0
do i=1,arrsize
mysum=mysum+arr(i)
enddo
end function mysum
end program
3.
interface block - not recommended at all, you should have some particular reason to use it
function mysum(arr)
! removed to save space
end function mysum
program test
implicit none
interface
function mysum(arr)
real, dimension(:), intent(in) :: arr
real :: mysum
end function
end interface
!no mysum declared there
!it is declared in the interface block
...
end program

Fortran error 256 when calling a function to return an array [duplicate]

I want to write a function that returns an allocatable array in fortran
program test
implicit none
real a(3)
real, allocatable :: F18(:)
a = (/1,2,3/)
print *, F18(a)
end program test
function F18(A)
implicit none
real A(:) ! An assumed shape array
real F18(size(A,1)) ! The function result itself is
! the second dimension of A.
F18 =A !
end function F18
It is expected to print "1 2 3" on the screen but I got an error:
forrtl: severe (157): Program Exception - access violation
What's the problem?
Also, I have tried code like this:
program test
implicit none
real a(3)
real, allocatable :: F18(:)
a = (/1,2,3/)
print *, F18(a,3)
end program test
function F18(A,n)
implicit none
integer n
real A(:) ! An assumed shape array
real F18(size(A,1)) ! The function result itself is
! the second dimension of A.
F18 =A !
end function F18
During compiling I got:
Intel(R) Visual Fortran Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 14.0.4.237 Build 20140805
Copyright (C) 1985-2014 Intel Corporation. All rights reserved.
D:\Fortran\Elephant.f90(6): error #6351: The number of subscripts is incorrect. [F18]
print *, F18(a,3)
-------------^
compilation aborted for D:\Fortran\Elephant.f90 (code 1)
I am really confused with fortran's function.
What is the right way to call Array-valued Functions in fortran?
#Fortranner
You need to make the properties of the function known to the caller. The easiest way is to put it into a module and 'use' that module. In your examples, in your main program you are declaring an array 'F18', which is not the function.
module mystuff
contains
function F18(A,n)
implicit none
integer n
real A(:) ! An assumed shape array
real F18(size(A,1)) ! The function result itself is
! the second dimension of A.
F18 =A !
end function F18
end module mystuff
program test
use mystuff
implicit none
real a(3)
a = (/1,2,3/)
print *, F18(a,3)
end program test

Fortran cast 2D Array to 1D array and pass it

I am working with Fortran 2008 with the Intel 17 Compiler.
I am getting a 3D (x,y,z) assumed shape array in a subroutine and want to pass xy slices as a 1D array to another function that multiplies it with a matrix:
SUBROUTINE xy_gyro_average_3d(this,infield,barfield)
class(xy_GyroAverager_t) :: this
REAL,DIMENSION(:,:,:), INTENT(IN),contiguous,TARGET :: infield
REAL,DIMENSION(:,:,:,:,:), INTENT(OUT),contiguous,TARGET :: barfield
INTEGER :: n,m,k,li0,lj0
type(Error_t) :: err
REAL,DIMENSION(:),POINTER :: inptr,outptr
li0=SIZE(infield,1)
lj0=SIZE(infield,2)
DO n=1,this%ln0
DO m=1,this%lm0
DO k=1,this%lk0
inptr(1:li0*lj0) => infield(:,:,k)
outptr(1:li0*lj0) => barfield(:,:,k,m,n)
CALL this%gyromatrix(k,m,n)%multiply_with(invec=inptr,&
& outvec=outptr,err=err)
if (err%err) call write_err(err)
END DO ! k
END DO ! m
END DO ! n
END SUBROUTINE xy_gyro_average_3d
The multiply_with routine looks (simplified) like this:
SUBROUTINE multiply_with(this,invec,outvec,err)
CLASS(Matrix_t) :: this
real, dimension(1:), intent(IN) :: invec
real, dimension(1:),intent(OUT) :: outvec
CLASS(Error_t),INTENT(OUT) :: err
CALL this%multiply_with__do(invec,outvec,err)
END SUBROUTINE multiply_with
I am getting a warning about the pointer allocation and then an error about the types of actual and dummy argument not matching:
src/GyroAverager.F90(294): warning #8589: Fortran 2008 specifies that if a bound remapping list is specified, data target must be of rank one. [INFIELD]
inptr(1:SIZE(infield(:,:,k))) => infield(:,:,k)
----------------------------------------------^
src/GyroAverager.F90(295): warning #8589: Fortran 2008 specifies that if a bound remapping list is specified, data target must be of rank one. [BARFIELD]
outptr(1:li0*lj0) => barfield(:,:,k,m,n)
----------------------------------^
src/GyroAverager.F90(297): error #6633: The type of the actual argument differs from the type of the dummy argument. [INPTR]
CALL this%gyromatrix(k,m,n)%multiply_with(invec=inptr,&
-------------------------------------------------------------^
src/GyroAverager.F90(298): error #6633: The type of the actual argument differs from the type of the dummy argument. [OUTPTR]
& outvec=outptr,err=err)
Now I think the warning should not be a problem as long as the arrays infield and barfield are marked as contiguous (is there any way to do this to get rid of the warning messages though?).
What exactly is the problem with the error message? The pointers inptr and outptr have the same dimension as the dummy arguments invec and outvec.
But I read that if you pass a pointer but do not receive it as a pointer the array it is pointing to is passed instead. So in the end is it still passed as a 2D array instead of a 1D or is the problem somewhere else?
It was working before when the header of the multiply_with routine looked like this:
SUBROUTINE multiply_with(this,invec,outvec,err)
CLASS(Matrix_t) :: this
class(*), dimension(1:),intent(IN) :: invec
class(*), dimension(1:),intent(OUT) :: outvec
CLASS(Error_t),INTENT(OUT) :: err
and then the routines called by multiply_with would get the type via a select type later. But I reduced the code as we are always working with real arrays.
What is the correct way to do this?

Fortran recursive function won't return array [duplicate]

I want to write a function that returns an allocatable array in fortran
program test
implicit none
real a(3)
real, allocatable :: F18(:)
a = (/1,2,3/)
print *, F18(a)
end program test
function F18(A)
implicit none
real A(:) ! An assumed shape array
real F18(size(A,1)) ! The function result itself is
! the second dimension of A.
F18 =A !
end function F18
It is expected to print "1 2 3" on the screen but I got an error:
forrtl: severe (157): Program Exception - access violation
What's the problem?
Also, I have tried code like this:
program test
implicit none
real a(3)
real, allocatable :: F18(:)
a = (/1,2,3/)
print *, F18(a,3)
end program test
function F18(A,n)
implicit none
integer n
real A(:) ! An assumed shape array
real F18(size(A,1)) ! The function result itself is
! the second dimension of A.
F18 =A !
end function F18
During compiling I got:
Intel(R) Visual Fortran Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 14.0.4.237 Build 20140805
Copyright (C) 1985-2014 Intel Corporation. All rights reserved.
D:\Fortran\Elephant.f90(6): error #6351: The number of subscripts is incorrect. [F18]
print *, F18(a,3)
-------------^
compilation aborted for D:\Fortran\Elephant.f90 (code 1)
I am really confused with fortran's function.
What is the right way to call Array-valued Functions in fortran?
#Fortranner
You need to make the properties of the function known to the caller. The easiest way is to put it into a module and 'use' that module. In your examples, in your main program you are declaring an array 'F18', which is not the function.
module mystuff
contains
function F18(A,n)
implicit none
integer n
real A(:) ! An assumed shape array
real F18(size(A,1)) ! The function result itself is
! the second dimension of A.
F18 =A !
end function F18
end module mystuff
program test
use mystuff
implicit none
real a(3)
a = (/1,2,3/)
print *, F18(a,3)
end program test

Function that returns a 1D array but takes two arguments in Fortran90 [duplicate]

I want to write a function that returns an allocatable array in fortran
program test
implicit none
real a(3)
real, allocatable :: F18(:)
a = (/1,2,3/)
print *, F18(a)
end program test
function F18(A)
implicit none
real A(:) ! An assumed shape array
real F18(size(A,1)) ! The function result itself is
! the second dimension of A.
F18 =A !
end function F18
It is expected to print "1 2 3" on the screen but I got an error:
forrtl: severe (157): Program Exception - access violation
What's the problem?
Also, I have tried code like this:
program test
implicit none
real a(3)
real, allocatable :: F18(:)
a = (/1,2,3/)
print *, F18(a,3)
end program test
function F18(A,n)
implicit none
integer n
real A(:) ! An assumed shape array
real F18(size(A,1)) ! The function result itself is
! the second dimension of A.
F18 =A !
end function F18
During compiling I got:
Intel(R) Visual Fortran Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 14.0.4.237 Build 20140805
Copyright (C) 1985-2014 Intel Corporation. All rights reserved.
D:\Fortran\Elephant.f90(6): error #6351: The number of subscripts is incorrect. [F18]
print *, F18(a,3)
-------------^
compilation aborted for D:\Fortran\Elephant.f90 (code 1)
I am really confused with fortran's function.
What is the right way to call Array-valued Functions in fortran?
#Fortranner
You need to make the properties of the function known to the caller. The easiest way is to put it into a module and 'use' that module. In your examples, in your main program you are declaring an array 'F18', which is not the function.
module mystuff
contains
function F18(A,n)
implicit none
integer n
real A(:) ! An assumed shape array
real F18(size(A,1)) ! The function result itself is
! the second dimension of A.
F18 =A !
end function F18
end module mystuff
program test
use mystuff
implicit none
real a(3)
a = (/1,2,3/)
print *, F18(a,3)
end program test

Resources