0

I have to use a subroutine (neqnf) included in IMSL library, which let me solve non-linear systems. (link to users manual, neqnf page here) main.f90, is:

program prova_sistema_in_un_modulo

    include "link_fnl_shared.h"
    use neqnf_int
    use modx

    implicit none

    call d_neqnf(FCN, x, xguess=x_guess, fnorm=f_norm)

end program prova_sistema_in_un_modulo

where subroutine FCN is coded in an external module, modx.f90:

module modx

implicit none

integer, parameter :: ikind = selected_real_kind(8,99)
integer :: n=3
real(kind=ikind) :: f_norm
real(kind=ikind), dimension(3) :: x, x_guess=(/ 4.0, 4.0, 4.0/)

contains

subroutine FCN(x,f,n)
    integer :: n                                  !dummy var
    real(kind=ikind), dimension(3) :: x, f        !dummy var

    f(1)=x(1)+A(x(1))+(x(2)+x(3))*(x(2)+x(3))-27.0            ! =0
    f(2)=B(x(1),x(2))+x(3)*x(3)-10.0                          ! =0
    f(3)=Z(x(2),x(3))                                         ! =0

end subroutine FCN

function A(x)
    real(kind=ikind) :: x    !dummy var
    real(kind=ikind) :: A    !function var

    A=exp(x-1.0)

end function A

function B(x,y)
    real(kind=ikind) :: x,y  !dummy var
    real(kind=ikind) :: B    !function var

    B=exp(y-2.0)/x

end function B

function C(x)
    real(kind=ikind) :: x    !dummy var
    real(kind=ikind) :: C    !function var

    C=sin(x-2.0)

end function C

function Z(x,y)
    real(kind=ikind) :: x,y  !dummy var
    real(kind=ikind) :: Z    !function var

    Z=y+C(x)+x*x-7.0

end function Z
end module modx

but I get these three errors:

Error   1    error #7061: The characteristics of dummy argument 1 of the associated actual procedure differ from the characteristics of dummy argument 1 of the dummy procedure. (12.2)   [FCN]
Error   2    error #7062: The characteristics of dummy argument 2 of the associated actual procedure differ from the characteristics of dummy argument 2 of the dummy procedure. (12.2)   [FCN]
Error   3    error #7063: The characteristics of dummy argument 3 of the associated actual procedure differ from the characteristics of dummy argument 3 of the dummy procedure. (12.2)   [FCN]

NB: if I put all code in the main program, all goes fine! while if I code using module (as I've done, the actually posted code) I get that errors! can anyone help me?

Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
mattia.b89
  • 111
  • 8
  • I use Visual Studio (2010 SP1) with Fortran IMSL (2011) – mattia.b89 Jun 14 '15 at 09:53
  • I've tried it just now, and adding `external FCN` between variable declaration and contains statement (is it right to position it there?) I get this new error beside the abovementioned three `Error 1 error #6645: The name of the module procedure conflicts with a name in the encompassing scoping unit. [FCN]` – mattia.b89 Jun 14 '15 at 10:18
  • also changing `ikind=kind(1.d0)` doesn't solve the problem (and as further more proof I use another _d__ procedure with real and all goes fine) – mattia.b89 Jun 14 '15 at 10:22
  • can you explain better your last statement? I'm not an expert of Fortran, on the contrary, it's the first time I code with this language – mattia.b89 Jun 14 '15 at 10:25
  • yes, no problem, I tried but I have the same three errors – mattia.b89 Jun 14 '15 at 10:33

1 Answers1

0

The problem is that you provide a fixed dimension for the dummy arguments x(3) and f(3) in your custom function FCN, while IMSL expects a variable dimension x(n), f(n):

subroutine FCN(x,f,n)
    integer :: n                                  !dummy var
!    real(kind=ikind), dimension(3) :: x, f       !<- wrong
    real(kind=ikind), dimension(n) :: x, f        !<- correct

    f(1)=x(1)+A(x(1))+(x(2)+x(3))*(x(2)+x(3))-27.0            ! =0
    f(2)=B(x(1),x(2))+x(3)*x(3)-10.0                          ! =0
    f(3)=Z(x(2),x(3))                                         ! =0

end subroutine FCN

A working example to reproduce this is (interface borrowed from HYBRD1):

module test_int
contains
  subroutine final(FCN, x, f, n)
    interface
      SUBROUTINE FCN (X, F, N)
        INTEGER             N
        DOUBLE PRECISION    X(N), F(N)
      END SUBROUTINE
    end interface
    integer          :: n
    double precision :: x(n), f(n)
    call FCN(x,f,n)
  end subroutine
end module

module test_fct
contains
  subroutine FCN(X, F, N) 
    integer          :: n
    double precision :: x(n), f(n)
    print *,X ; print *,F ; print *,N
  end subroutine
end module

program prova
  use, intrinsic :: iso_fortran_env
  use test_int
  use test_fct
  implicit none
  integer,parameter :: n=2
  double precision  :: x(n), f(n)
  x = [ 1.d0, 2.d0 ]
  f = [ 3.d0, 4.d0 ]
  call final(FCN, x, f, n)
end program prova
Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
  • I understand what do you said but if I change correct the array dimension the problem is here again... – mattia.b89 Jun 14 '15 at 12:26
  • Well, I don't have the correct interface available, I guessed one. Does the code I constructed compile and run on your machine? Please also post the interface for `FCN` (from `link_fnl_shared.h`, I assume). – Alexander Vogt Jun 14 '15 at 12:34
  • yes, your code works. interface for FCN is linked at the top of opening post, I guess – mattia.b89 Jun 14 '15 at 14:37
  • I don't mean the declaration of *your* function, but the interface of the library. Your function does not match what the library expects, and I need to see what should be passed to `d_neqnf`. I have guessed that part from the documentation and the open source part the subroutine is based on, but there seem to be some differences. – Alexander Vogt Jun 14 '15 at 14:42
  • The documentation only shows the interface for the single precision version. Please show the double precision interface! – Alexander Vogt Jun 14 '15 at 15:47
  • sorry for my inexperience but where can I find it? – mattia.b89 Jun 14 '15 at 15:49
  • In `link_fnl_shared.h`, I assume – Alexander Vogt Jun 14 '15 at 15:50
  • it's the first thing I watched but it contains only a line mentioning `imsl_dll.lib` that is a binary file – mattia.b89 Jun 14 '15 at 15:57
  • Maybe you need to specify `intent(in)` and `intent(out)` for the dummy arguments - they are explicitly stated in the documentation... Other then that, I'm out of options. Maybe someone with access to IMSL can help. – Alexander Vogt Jun 14 '15 at 16:23