PHYS 1107 Lecture Notes - Lecture 9: Odometer, Intrinsic Function, Absolute Value

27 views33 pages
5 Jul 2018
School
Department
Course
Professor
PY 502, Computational Physics (Fall 2016)
Brief Introduction to the Fortran 90 programming language
Anders W. Sandvik, Department of Physics, Boston University
1 Introduction
There are several versions of Fortran in use today; Fortran 77/90/95/2003/2008, where the numbers
indicate (approximately) the year the standard was published. There are also some other variants,
e.g., “High Performance Fortran”, which contains additional language elements for parallel com-
puting.
This brief introduction focuses on Fortran 90. Fortran 95 and higher versions mainly adds advanced
features that will not be needed in this class. Often these versions are refereed to as one single
version 90/95. Fortran 2003 and 2008 added advanced features, e.g., related to object oriented
programming, which will not be used in this course.
Since many useful subroutines are available in Fortran 77, some of the dierences between the 77
and 90 versions will be pointed out as well. In general, the versions are backward compatible, so
that Fortran 77 code is also valid Fortran 90 (however, some Fortran 77 features that are considered
outdated or redundant are noted in Fortran 95 as candidates for removal in future revisions). A
fortran 90/95 program can be compiled together with files containing Fortran 77 code.
Only the very basic language features will be discussed here, mainly in the form of simple examples.
The intention is to explain those language elements that are needed to quickly get started in scientific
programming, and to give an overview of Fortran 90 for those that may be experienced in another
language. For more details and more advanced language elements, consult any text on Fortran 90,
e.g., Fortran 90/95 Explained, by M. Metcalf and J. Reid (Oxford University Press, second edition,
2002) or Fortran 90/95 for Scientists and Engineers, by S. Chapman (McGraw Hill, 2004). There
are also numerous resouces on-line.
This tutorial is organized in seven sections: 1) Introduction, 2) Variables and declarations, 3) Pro-
gram control constructs, 4) Procedures and program structure, 5) Intrinsic procedures, 6) External
files and output formatting, and 7) Allocatable, assumed-shape, and automatic arrays. Many of
the elementary language features are introduced as they are needed, and therefore the whole text
should be read from start to finish by those who do not have any prior knowledge of Fortran.
Careful examination of the example programs is also strongly recommended; they are also available
for download at the web-site of the course: physics.bu.edu/~py502.
1.1 Why Fortran 90?
There is a common misconception that Fortran is an old-fashioned language, which is being replaced
by more modern languages such as C++. It is true that Fortran is an old language; in fact it was the
first real high-level programming language (i.e., it did not require the user to be familiar with the
inner workings of the computer). However, the language has evolved considerably over the years.
1
Distributing prohibited | Downloaded by Cfisd Center ([email protected])
lOMoARcPSD|2520155
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 33 pages and 3 million more documents.

Already have an account? Log in
Fortran 77 can be considered an old-fasihoned language, but Fortran 90 and 95 are definitely not.
These versions of the language incorporate modern elements such as pointers, abstract data types,
allocatable arrays, and operator overloading (however, most of these features will not be essential
for programming in this course). The language is still evolving, and new standards beyond the
2008 version can be expected. Note, however, that the basic language does not change with each
new revision, but refinements and new useful features are incorporated.
Fortran 90 is very well suited for scientific computing because of its large number of ecient
intrinsic mathematical functions and its very versatile array features (functions on vectors, matrices,
and higher dimensional arrays), which are directly accessible without importing special libraries.
Fortran is a very widely used programming language in physics (as well as science and engineering
more broadly) and hence all physics students should have at least a basic knowledge of it. As a
language to be used in a course for students with diverse backgrounds in programming, Fortran 90
has the advantage of being easier to learn than most other modern languages.
1.2 Compilers
A problem with Fortran 90/95 used to be that only expensive commercial compilers were available
(a reasonably good free Fortran 77 compiler has been available for a long time; the Gnu g77, which
is included in most Linux distributions). The situation is now improving, as two free open-source
compilers have become available in the past few years; g95 (can be downloaded from www.g95.org)
and gfortran (http://gcc.gnu.org/wiki/GFortran). These compiler contain elements up to Fortran
2008. Gfortran is the compiler recommended for this course (and is installed on the CAS 327
workstations), but G95 should also be fine to use. A commercial Fortran 90 compiler, f90, is
available on the BU Physics Department server buphy.
The gfortran compiler is invoked with the command gfortran code.f90,wherecode.f90 is the
program file (more than one code file can also be supplied). The executable file produced has the
default name a.out; to give it another name use the directive -o, e.g., gfortran code.f90 -o
program.x. The program is then executed by the command program.x (or ./program.x on most
Linux systems).
Complex programs for which fast run times are essential should be compiled using optimization,
with the -O directive, e.g., gfortran -O code.f90.
All files containing Fortran 90 code should have the extension .f90. Most Fortran 90/95 compilers
treat files ending in the standard Fortran 77 extension .f as Fortran 77 code and hence will generate
errors when compiling a program containing Fortran 90 elements.
1.3 Code formatting
Fortran 77 requires that program statements are only written starting from position 8 (position 7
for some compilers, including g77) on lines with 80 positions (some compilers have extensions to
allow longer lines). Fortran 90 allows lines of length 132 and statements can start from position 1.
In Fortran 77 there can be only one statement per line, whereas Fortran 90 allows for several
2
Distributing prohibited | Downloaded by Cfisd Center ([email protected])
lOMoARcPSD|2520155
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 33 pages and 3 million more documents.

Already have an account? Log in
statements separated by ;on the same line. However, for clarity it is recommended to write several
statements per line only in the case of very short statements.
An initial program program-name statement is recommended in Fortran 90 (as well as in Fortran
77) but is not required. An end statement is required at the end of the main program (as well as
in every subroutine and function), but a longer end program program-name is recommended. In
example codes reproduced here, the program statement will not be used (for brevity) and the end
statement will also some times be left out.
All text appearing after a !character is neglected by the compiler. This is to enable comments
in the code, for the benefit of other users of the program, and, not the least, to help the author of
the code to remember what is going on in hers/his creation. However, excessive use of comments
should be avoided as it makes it dicult to follow the actual code.
For statements that cannot be written on a single line, the character &indicates continuation on
the following line (up to 40 lines are allowed for a statement).
2 Variable types and declarations
Unless the type of a variable (floating point, integer, complex, logical, character) is explicitly de-
clared, it is of the type single-precision floating-point (real), except if the name begins with i,...,n,
in which case the type is integer (note that most Fortran compilers make no distinction between
upper and lower case letters). In order to avoid common programming errors due to misspelled
variable names, it is recommended to begin each program section (main program, subroutines,
functions) with the implicit none statement, which makes the use of undeclared variables (such
as misspelled ones) illegal (resulting in a compile-time error message). For the sake of brevity, the
implicit none statement will be left out in the short programs reproduced here (it, as well as the
compulsory end statement, do appear in the actual program files on the course web page).
2.1 Integers
A standard integer uses 4 bytes (32 bits) and can hold the numbers 231 to 231
1. This is
illustrated in the following program (integera.f90):
integer :: i
i=-2**31
write(*,*)i
i=i-1
write(*,*)i
When compiled (if it compiles.... se below) and run it produces:
-2147483648
2147483647
3
Distributing prohibited | Downloaded by Cfisd Center ([email protected])
lOMoARcPSD|2520155
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 33 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Brief introduction to the fortran 90 programming language. Anders w. sandvik, department of physics, boston university. There are several versions of fortran in use today; fortran 77/90/95/2003/2008, where the numbers indicate (approximately) the year the standard was published. There are also some other variants, e. g. , high performance fortran , which contains additional language elements for parallel com- puting. Fortran 95 and higher versions mainly adds advanced features that will not be needed in this class. Often these versions are refereed to as one single version 90/95. Fortran 2003 and 2008 added advanced features, e. g. , related to object oriented programming, which will not be used in this course. Since many useful subroutines are available in fortran 77, some of the di erences between the 77 and 90 versions will be pointed out as well. A fortran 90/95 program can be compiled together with les containing fortran 77 code.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related textbook solutions