The University of Queensland Homepage
UQ VisLab UQ VisLab

 Chris's Guide to Fortran


Chris's guide to Fortran


Chris's guide to fortran
Hello everybody, im one of the tutors, if your feeling unsure about matlab or c or whatever, consider fortran seriously Here is some commands you might find useful if you were doing some code in fortran, say for the exam or just for general interest (hehehehehe, im hopeful)

General
Start a program with
Program (program name)
and and a program with
end Program (program name)

Types
The way you should declare variables is with the type followed by ::

Real :: (Real variable list)
Integer :: (Integer variable list)

Specail types
Complex :: (complex list, these are too hard to use in practice, so try and avoid, unless ur really good)
Character*n :: (these are words, sentences that are n characters long, try and just use constant string in general)
Double Precision :: (Double variable list keep in mind for what you will be doing, you really don't need any higher precision than reals give)

Arrays
The easy way to declare arrays is when you define your variables add brackets with the dimensions in the brackets
example
Integer :: L(10,10), 10 by 10 lattice
here L(4,7) gives the 4th row the 7th column

File input/output
open (1,file="filename") opens a file label 1
close (1) closes a file (done at end program neway)
read (1,*,end=100) (variable list) reads from input 1 into ur variable list
write (1,100) (variable list) writes variables to output 1 with format specified at line 100

just a note, generally input # and output # are files

Format Notes
We define the following formats
Fn.m - n characters long with m places after the decimal
In - n character long integer (white spaced)
En.m - n like scientific, eg .5E2 = 50
(in this case n and m are intedgers)
'Text' - will print out character const
An - characters.

Note if you cant be bothered with formats while printing to screen, then we use, instead of write
print*,(variables and constant list)

Structural Notes
do, end do, basically is a loop, this can either be with a variable counter or without, the following examples
do i = 1, 100, 2
(statements, if one of ur statements is exit, it
will get out of the loop, i can also be real)
end do
or
do
(statements, in this case you need an exit or and
end = (label number) in a read to get out of the loop
somewhere or it will go on forever)
end do
if statements
if ( (condition) ) then
(Statements, ^ never forget the then)
else
(Statements, else is not really required but...
end if ( <- is required)

Other functions such as Sin, Cos, Acos ,Log, Exp ... are known to fortran, I hope this all helps. Thats fortran in a nutshell, in my opinion it is the easiest language to do most things in SC3, a few other packages i can recommend, but just so this all doesn't get too abstract here is some example code

program example
real :: bob,fred
open (1,file="Bobs.dat")
open (2,file="Freds.dat")
do
read (1,*,end=100),bob
if (bob >= 0) then
fred = bob*bob
else
fred = -bob*bob
end if
write (2,10), fred,bob
10 format ('fred has ',F8.6,' while bob has ',F8.6)
print*, 'Hey i just printed ',fred, ' to a file'
end do
100 print*, 'Dudes, im finished'
end program example


The only thing I have left out is compiling which is done with the following command
f90 (filename, or list) -o (prog name)

Now just run it (:
If you have been following, what this program does should be really simple to follow.

So thats chris's guide to fortran
If you have any questions mail me
Also, take a look at Chris's Guide to Mathematica

Take me Home contry Roads