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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

common vs. parameter

Status
Not open for further replies.

Rednas1

Technical User
Oct 17, 2008
3
NL
Hello all,

I'm working with an optimization program written in Fortran 77 and am interested in exploring the possibilities of Fortran 90 and translating my program. Since it is a rather lengthy and complicated program I'm encountering problems with the use of COMMON blocks to pass constants throughout the program. Now I understood that in Fortran 90 I can define a module e.g. "input" in which I define all constants as parameters. By stating "use input" in any other part of the program I have access to these parameters. However, some of the constants are user supplied before running the program. In the current program I call a subroutine "input" at the start of the program which defines all constants and reads the user supplied constants from an input file. Next I define a set of common blocks to be used in other parts of the program. I understand that I can define all pre-known constants (such as e.g. pi) as parameters, but is there a way to avoid the use of common blocks and still have access to the user supplied constants throughout the program?

Thanks in advance for the help.
 
Put them as module variables and "use" the module.

You can put the common blocks as module variables. That will keep the references the same.
 
I'm not sure. Imagine the following program:

--------------------------------------------
program main
use pi
implicit real (a-z)
write(6, *) pi
end program main

module input
implicit double precision (a-z)
real, parameter :: pi = 3.14
end module input

--------------------------------------------

This'll make pi available for my main program and any other module I use. But now imagine I need to insert a read statement to read a variable "speed" from an external file. As far as I found out I can incorporate subroutine and functions in a module, but I cannot just add "read...." below the parameter definition of "pi". At least that'll return an error that I cannot put such a statement there.

Thanks
 
>but I cannot just add "read...." below the parameter definition of "pi"

Here is module_example.f95, with constant and subroutne
Code:
module my_input
  implicit double precision (a-z)
  real, parameter :: my_pi = 3.14
contains
  subroutine my_read
    write(*, *) "Reading speed from external file..."
  end subroutine
end module my_input

program main
    use my_input
    implicit real (a-z)
    write(*, *) "Constant PI from the module my_input:", my_pi
    write(*, *) "Calling function from the module my_input:"
    call my_read
end program main
When I compile it with
Code:
$ g95 module_example.f95 -o module_example -ffree-form
it works and gives at output
Code:
$ module_example
 Constant PI from the module my_input: 3.14
 Calling function from the module my_input:
 Reading speed from external file...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top