Reputation: 4771
I'm converting some F77 files to F90. However, there are some common blocks that did not converted. I'm a novice in F90 and have no experience in F77. Could someone show me how to covert the following example code to F90?
BLOCK DATA SETUP
INTEGER A,B,C
REAL I,J,K,L
COMMON /AREA1/ A,B,C
COMMON /AREA2/ I,J,K,L
DATA A,B,C,I,J,K,L/0,1,2,10.0,-20.0,30.0,-40.0/
END
My idea is to put the arrays A, B, and C in a module. The main thing I don't get here is the AREA1 and AREA2. How are these used in F77 and how do I translate those? My first guess is to discard them and simply define A,B, & C in the module. However, are they a kind of derive type within which A, B, & C are contained?
Upvotes: 4
Views: 2097
Reputation: 8140
First up, the original code should compile in Fortran 90 and later. So if it isn't broken, don't try to fix it.
COMMON
blocks are basically global variables. In every procedure that the global variables are used, they have to be declared in the same way1, and then they are shared everywhere. Something like this:
integer a, b
common /globals/ a, b
A more modern approach would be to convert globals
into a module:
module globals
implicit none
integer a, b
end module globals
And then use
that module everywhere you need access to a
and b
program main
use globals
implicit none
a = 4
b = 2
end program main
You could either put the DATA
statement into the module, or, even easier, initialise every variable in the declaration:
module AREA1
implicit none
integer :: a = 0
integer :: b = 1
integer :: c = 2
end module AREA1
module AREA2
implicit none
real :: i = 10.0
real :: j = -20.0
real :: k = 30.0
real :: l = -40.0
end module AREA2
Then, you can replace the whole thing by just use AREA1
and use AREA2
before the implicit none
everywhere you need access to their variables.
Edit: Forgot the footnote
1The rules for common blocks are more flexible, in that the values for the different variables are stored in a common memory location, in the order that they are named. So while it is not technically necessary to always use the same COMMON
statement, you can very easily introduce bugs if you don't.
If you have differently named variables (but of the same type), then adapting that is not that hard. Say you have
integer a, b
common /globals/ a, b
in the main program and
integer i, j
common /globals/ i, j
in a subroutine. Assuming that you create a module with a
and b
, you can then in the subroutine use it in this way:
use globals, only: i => a, j => b
(Note that if you use only
, you have to list every variable that you want to use out of the module. You could use something like: only: a, i => b
if you want a
and i
.)
Of course, the following would also be compatible with the previous globals
common block, but might lead to more trouble:
integer k(2)
common /globals/ k
Upvotes: 6