1

I ran into some piece of Fortran code rather difficult to understand.
1. What is the name of structure of code / (i1,i1=0,nn-1) /?
How can I print it directly in the code to see its content?
2. I'm looking for ways to change value of nn without re-compiling, how should I do this? nn supposed to be the length of array omega.
3. How should I setup the length of omega in case of changeable nn? I mean when I'll have no parameter (nn=20) anymore.

  program test_20140919
  ! test
  implicit none
  integer nn
  parameter (nn=20)
  real omega(nn)

  call test_real(nn, 2.0, 4.0, omega)

  print *, omega
  end program test_20140919

  !c ===

  subroutine test_real(nn, o1, o2, omega)      
  integer nn
  real o1, o2
  real omega(nn)

  print *, nn
  omega = o1 +  (o2*o1)*(/ (i1,i1=0,nn-1) /)/real(nn-1)
  print *, real(nn)
  return
  end

I've compiled this with line gfortran test.f -ffree-form -o test in terminal.

UPD Revised version of the code due to answers from Vladimir F:

      module subs

        implicit none

      contains

        subroutine test_real(nn, o1, o2, omega)      
          integer nn
          real o1, o2
          real :: omega(:)

          if (.not. allocated(omega)) allocate(omega(nn))
          omega = o1 +  (o2*o1)*(/ (i1,i1=0,nn-1) /)/real(nn-1)
          print *, real(nn)

        end subrotine

      end module

      program test_20140920
      ! test

        use subs

        implicit none

        integer nn
        real, allocatable :: omega(:)

        read(*,*) nn

        allocate(omega(nn))

        call test_real(nn, 2.0, 4.0, omega)

        print *, omega
      end program test_20140920
aestet
  • 314
  • 1
  • 3
  • 13

1 Answers1

3

1) This is (/ ... /) is an array constructor.

The expression (i1,i1=0,nn-1) is an implied-do loop.

2) Read it using a read statement

     integer :: nn

     read(*,*) nn

3) Use an allocatable array

real, allocatable :: omega(:)

...

allocate(omega(nn))

I recommend you to read a Fortran tutorial or a Fortran book and familiarize yourself with these concepts.

  • 2) I've expected using of command-line-parameters not simple reading from input. 3) It's ok, but in that case I should add an interface for the subroutione to the code. – aestet Sep 19 '14 at 22:03
  • Expected, but didn't specify it. Look for `get_command_argument()` in your compiler's manual. 3) No interface block is necessary or even desirable, learn modules. Inside the subroutine use an assumed shape array `omega(:)`. No need for it to be allocatable in the subroutine. – Vladimir F Героям слава Sep 19 '14 at 22:10
  • Could you explain, why using of modules is more preferred than using of interfaces? My first idea was it's easier to separate main program from auxilary subroutines in several files. – aestet Sep 21 '14 at 16:15
  • 1
    This is too much for a short comment. Start here http://stackoverflow.com/questions/11953087/proper-use-of-modules-in-fortran and here http://stackoverflow.com/questions/8412834/correct-use-of-modules-subroutines-and-functions-in-fortran – Vladimir F Героям слава Sep 21 '14 at 16:19