Electronica

Páginas: 36 (8820 palabras) Publicado: 17 de mayo de 2012
University of Cambridge Department of Physics

Computational Physics
Self-study guide 2 Programming in Fortran 95

Dr. Rachael Padman Michaelmas 2007

Contents

1. THE BASICS
1.1 A very simple program 1.2 Running the program 1.3 Variables and expressions 1.4 Other variable types: integer, complex and character 1.5 Intrinsic functions 1.6 Logical controls 1.7 Advanced use of if andlogical comparisons 1.8 Repeating ourselves with loops: do 1.9 The stop statement 1.10 Arrays 1.11 Array arithmetic

3
3 4 5 8 11 13 15 16 17 17 19

2 GOOD PROGRAMMING STYLE
2.1 Readability 2.2 Self-checking code 2.3 Write clear code that relates to the physics

21
21 22 22

3. INPUT TO AND OUTPUT FROM A F95 PROGRAM
3.1 F95 statements for I/O

24
24

4 GRAPHICS AND VISUALISATION
4.1Plotting a data file 4.2 Getting help 4.3 Further examples 4.4 Printing graphs into PostScript files

27
27 28 28 29

SUGGESTED EXERCISE 1 5. PROGRAM ORGANISATION: FUNCTIONS AND SUBROUTINES
5.1 Functions 5.2 Formal definition 5.3 Subroutines 5.4 Local and global variables 5.5 Passing arrays to subroutines and functions 5.5.1 Size and shape of array known 5.5.2 Arrays of unknown shape andsize 5.6 The intent and save attributes

30 31
31 33 34 34 35 35 35 36

6. USING MODULES
6.1 Modules 6.2 public and private attributes

38
38 41

7 NUMERICAL PRECISION AND MORE ABOUT VARIABLES
7.1 Entering numerical values 7.2 Numerical Accuracy

42
42 42

1

8 USE OF NUMERICAL LIBRARIES: NAG
8.1 A simple NAG example 8.2 A non-trivial NAG example: matrix determinant

44
44 449 SOME MORE TOPICS
9.1 The case statement and more about if 9.2 Other forms of do loops

47
47 48

SUGGESTED EXERCISE 2

49

Acknowledgements: This handout was originally prepared by Dr. Paul Alexander, and has been updated and maintained by Dr Peter Haynes of the TCM group.

2

1. The Basics
In this section we will look at the basics of what a program is and how to make theprogram run or execute. The non-trivial example programs can be found in the directory: $PHYTEACH/part_2/examples with the name of the file the same as that of the program discussed in this guide. Some sections are more advanced and are indicated clearly indicated by a thick black line to the right of the text. These can be skipped certainly on a first reading and indeed you will be able to tacklethe problems without using the material they discuss.

1.1 A very simple program A program is a set of instructions to the computer to perform a series of operations. Those operations will often be mathematical calculations, decisions based on equalities and inequalities, or special instructions to say write output to the screen. The program consists of “source code” which is “stored” in a textfile. This code contains the instructions in a highly structured form. Each computer language has a different set of rules (or syntax) for specifying these operations. Here we will only consider the Fortran 90/95 (F95 for short) programming language and syntax. • Using emacs enter the following text into a file called ex1.f90, the .f90 part of the file name is the extension indicating that this isprogram source code written in the Fortran 90/95 language program ex1 ! ! My first program ! write(*,*) ’Hello there’ end program ex1 This is a complete F95 program. The first and last lines introduce the start of the program and show where it ends. Between the first and last lines are the program “statements”. The lines beginning with an exclamation mark are special statements called comments.They are not instructions to the computer, but instead are there to enable us (the programmer) to improve the readability of the program and help explain what the program is doing. The line beginning write is a statement giving a specific instruction to print to the screen.

3

Note that except within quotes: ⇒ Upper and lower case are NOT significant (different from Unix commands and files) ⇒...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • Electronica
  • Electron
  • Electronica
  • Electronica
  • Electronica
  • Electronica
  • Electronica
  • Electrones

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS