0

I found this subroutine on this link : generate a sequence array in fortran

I want to use this subroutine for my homework. But I can't use it and don't know why it didn't play

Error code is as follows

Error: Keyword argument requires explicit interface for procedure 'linspace' at (1)
     SUBROUTINE linspace(from, to, array)

        REAL(8), intent(in) :: from, to
        REAL(8), intent(out) :: array(:)
        REAL(8) :: range
        integer :: n, i

        n = size(array)
        range  = to - from

        IF (n == 0) return
        IF (n == 1) then
          array(1) = from
          return
        END IF

        DO i=2, n
          array(i) = from + range * (i - 1) / (n - 1)
        END DO

      END SUBROUTINE linspace
 PROGRAM decay

    IMPLICIT NONE
    REAL :: k, c0, dt
    REAL, DIMENSION(15) :: c_e, c_i
    REAL(8) :: t(5)

    k = 0.0001
    c0 = 100
    dt = 60*60
    c_e(1) = 100
    c_i(1) = 100

    call linspace(from=0.0, to=3600.0, array=t)

    print *, t

 STOP
 END PROGRAM decay
jack
  • 1,658
  • 1
  • 7
  • 18
kglim1213
  • 3
  • 1

1 Answers1

1

The error message is very precise: you need an explicit interface because the dummy argument array(:) is an assumed shape argument.

A detailed answer about explicit interfaces can be found here.


Long story short: the easiest solution would be to include the subroutine linspace in the conntains section of the program.

PROGRAM decay

    IMPLICIT NONE
    REAL :: k, c0, dt
    REAL, DIMENSION(15) :: c_e, c_i
    REAL(8) :: t(5)

    k = 0.0001
    c0 = 100
    dt = 60*60
    c_e(1) = 100
    c_i(1) = 100

    call linspace(from=0.0, to=3600.0, array=t)

    print *, t

contains
  SUBROUTINE linspace(from, to, array)

    REAL(8), intent(in) :: from, to
    REAL(8), intent(out) :: array(:)
    REAL(8) :: range
    integer :: n, i

    n = size(array)
    range  = to - from

    IF (n == 0) return
    IF (n == 1) then
      array(1) = from
      return
    END IF

    DO i=2, n
      array(i) = from + range * (i - 1) / (n - 1)
    END DO

  END SUBROUTINE linspace
 END PROGRAM decay
jack
  • 1,658
  • 1
  • 7
  • 18
  • Please be careful when answering questions about explicit interfaces. We get _a lot_ of such questions and it's generally much more useful to use the duplicate system instead of repeating and fragmenting the knowledge base. – francescalus Jan 19 '21 at 08:35
  • The error message is very precise, but it isn't complaining about the assumed shape (note that the argument isn't deferred shape) argument. That is a problem, true, but the message refers to the use of keywords. – francescalus Jan 19 '21 at 08:42
  • I found the solution from your comments. You comment me this anwser, so I can solve it. But I want to split this code. Therefore, I search the solution and finally find. Solution is "Use module" Thank you very much! – kglim1213 Jan 19 '21 at 09:25
  • @francescalus I will keep that in mind for the next question. (I also edited deferred shape -> assumed shape) – jack Jan 19 '21 at 10:12
  • @francescalus Would it actually help if I remove my answer? – jack Jan 19 '21 at 11:16
  • You won't be able to delete your answer, without a moderator flag (it's accepted which blocks non-moderator deletion - and it's possible a moderator would even decline such a flag). I wouldn't worry too much about it: I'd probably delete if it were my answer and I could, but I'm not going to vote down (or delete even if possible), so I'd certainly say it's your choice (and someone has decided to vote it up). (I will probably vote to delete the _question_ when that's available to me: with the error message being so explicit in the question itself it's not so obscure as to be useful to keep.) – francescalus Jan 19 '21 at 11:26