Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conformity issue arises when using interfaces with function pointers

Status
Not open for further replies.

isyegatech

Technical User
Apr 14, 2013
8
US
I am starting this thread to ask for help solve a problem that might come from my wrong specification of a function interface, but I don't know how to fix it.

The error message I encountered is short and simply says, "Illegal number or type of arguments to lnsrch - arguments of fmin and func do not agree."

The definition of LNSRCH, FMIN, and FUNC will be clear in the content below.

The original program code is trimmed to illustrate my intention as the following (which consists of three parts: a main program unit called MAIN, a module named MODEL, and a module named NEWTON). You should be able to reproduce the error message just putting these three program units together.

Module MODEL just defines a simple equations system in two variables---y(1)=x(1); y(2)=x(2) ---in the subprogram FUNC_SYSTEM1. Module MODEL also contains an abstract interface for future extension so that I can simply make the pointer FUNCV to reference any other equation system of the same kind as the current example equation system FUNC_SYSTEM1, with the exception only in the number of variables of the equation system.

Code:
MODULE model                                                             
    IMPLICIT NONE                            
    REAL, DIMENSION(:), POINTER :: fmin_fvecp
    ABSTRACT INTERFACE                              
        FUNCTION function_system_template(x) RESULT(y)     
        REAL, DIMENSION(:), INTENT(IN) :: x     
        REAL, DIMENSION(SIZE(x)) :: y           
        END FUNCTION                                
    END INTERFACE                                   
    PROCEDURE(function_system_template), POINTER :: funcv  
CONTAINS                                                          
    FUNCTION func_system1(x) Result(y)              
    IMPLICIT NONE                             
    REAL, DIMENSION(:), INTENT(IN) :: x   
    REAL, DIMENSION(size(x)) :: y                            
    y(1)=x(1)      
    y(2)=x(2)      
    END FUNCTION func_system1                           
END MODULE model
Module NEWTON defines the relationship among three subprograms that are key to the program's computing: BROYDEN will call FMIN to get the sum of squares of x(1) and x(2); simultaneously, in FMIN, the vector of x(1) and x(2) is assigned to an array pointer called FMIN_FVECP. This array pointer is to be used to do some side calculation in the function LNSRCH.
Code:
MODULE newton 
    USE model
    IMPLICIT NONE
    REAL, DIMENSION(:), POINTER :: fmin_fvecp
CONTAINS
    SUBROUTINE broyden(x,fmin_fvecp,funcv)           
        IMPLICIT NONE
        REAL, DIMENSION(:), INTENT(IN) :: x
        REAL, DIMENSION(size(x)), TARGET :: y
        REAL, DIMENSION(:), POINTER :: fmin_fvecp
        PROCEDURE(function_system_template), POINTER :: funcv
        fmin_fvecp=>y
        print*,fmin(x,fmin_fvecp,funcv)        ! Get the sum of squares
        print*,fmin_fvecp                      ! Show the vector x(1) and x(2)
        print*,lnsrch(x,fmin,fmin_fvecp,funcv) ! Show the figure calculated in LNSRCH
    END SUBROUTINE broyden

    FUNCTION fmin(x,fmin_fvecp,funcv) RESULT(y)
        IMPLICIT NONE
        REAL, DIMENSION(:), INTENT(IN) :: x
        REAL, DIMENSION(:), POINTER :: fmin_fvecp
        PROCEDURE(function_system_template), POINTER :: funcv
        REAL :: y
        fmin_fvecp=funcv(x)                    ! The value of FMIN_FVECP is assigend
        fmin=dot_product(fmin_fvecp,fmin_fvecp)! when FMIN is called by BROYDEN
    END FUNCTION fmin    

    FUNCTION lnsrch(x,func,a_fvecp,b_funcv) RESULT(y)
        IMPLICIT NONE
        REAL, DIMENSION(:), INTENT(IN) :: x
        REAL, DIMENSION(:), POINTER :: a_fvecp 
        PROCEDURE(function_system_template), POINTER :: b_funcv 
        INTERFACE                              
            FUNCTION func(x,fvecp,funcp)    
            IMPLICIT NONE
            REAL, DIMENSION(:), INTENT(IN) :: x
            REAL :: func
            REAL, DIMENSION(:), POINTER :: fvecp 
            PROCEDURE(function_system_template), POINTER :: funcp 
            END FUNCTION                                
        END INTERFACE
        REAL, DIMENSION(SIZE(x)) :: y
        y=x+a_fvecp+b_funcv(x)+1000.
        END FUNCTION lnsrch
    END MODULE newton
The main program unit is defined as follows:
Code:
PROGRAM main
    USE model                            
    USE newton                           
    IMPLICIT NONE  
    REAL, DIMENSION(:), allocatable :: x
    allocate(x(2))
    x=[1.,2.]                         ! The input arguments to be passed into 
    funcv=>func_system1               ! the equation system, FUNC_SYSTEM1.
    call broyden(x,fmin_fvecp,funcv)  ! Call BROYDEN to do the subsequent calcualtion
    deallocate(x)    
END PROGRAM main
Sorry for the lengthy post. Thanks for the time reading through my question. Looking forward to any input for working around the error message. Thanks.

Lee
 
Sorry that there are some typos in the code shown above (post #1). Here is an improved version of my code: [link goo.gl/f4fMd]Link[/url]. It is a single .f90 format file that reproduces my error message. Thanks.
 
Sorry, thisi s not f90 as my f95 compiler complains heaviliy (stopping the errorcount at 30)
... and as that way beyond my expertise

Norbert


The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
@Norbert(29 Apr 13 14:43): Thanks for the reply. I am learning Fortran 2003 now and trying to adapt Broyden's algorithm provided by Numerical Recipes in Fortran 90 to one in the format of Fortran 2003 so that I can feed the algorithm/subroutine arbitrary size of equation systems and solve for the root. Sorry for the confusion about the file extension name.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top