PHYS 580   COMPUTATIONAL PHYSICS 

Sample Fortran programs

Clink on the links below; you can save these files to disk and compile them.

Note: very important! To view properly, you must view as page source (open the file, and then ctrl+u) You can also right-click on the link and choose "Save Target As..."

Basic Fortran: formatting, simple input and output

Example 1

Example 2

Example 3

Simple example of numerical differentiation of sine function

Data types, declaring variables, and intrinsic functions

Example 4

Example 5

Example 6 (intrinsic functions)

Do loops

Example 7

Declaring and dimensioning arrays

Example 8

Example 9

Example 10 (how to give multiple arrays the same dimension--use of parameter statement)

Simple example of integration using trapezoidal rule

Control statements (IF...THEN) and "logical" data type

Example 11

Example 12

Example 13  (logical data type)

Revised example of trapezoidal rule with error traps

Subroutines and functions

Example 14 (subroutines)

Example 15 (functions)

Example 16 (advanced used of functions and subroutines)

Do...while...end do  loop

Example 17

Character data type

Example 18

Writing to files and formatting

Example 19

Example 20 (simple formatting)

Example 21 (more formatting)

Opening and closing files

Example 22

Example 23 (advanced opening and closing with error traps)

Example of Gauss-Legendre quadrature: calling program and Gauss-Legendre subroutine (compile both together)

More on dimensioning arrays

Example 24 (useful example for project #2!)

Benchmark programs for testing optimization

Matrix multiplication (for tests of speed and optimization) requires library ranlib.f

Eigenvalues (requires library eiglib.f)

Package of C++ programs for benchmarking against above programs (thanks to Edgar Teran); you must do

gunzip speed.tar.gz

tar -xvf speed.tar

make -f speedeigen.mk

make -f speedmult.mk

 

Matlab script for benchmarking against above (thanks to Hai Ah Nam)

 

BLAS benchmark program for matrix multiplication  (requires ranlib.f)  (speedup is compiler dependent; small speedup on GNU f77 compiler, large speedup for Intel compiler)

 NEW! Some examples of advanced programming in Fortran 90/95

Example 1a: Basic use of modules: mymodule.f90  and exampleusemodule.f90

To compile: gfortran -o mytest.x  mymodule.f90 exampleusemodule.f90

NB: when compiling, the modules MUST be compiled before they are "used"!

Example 1b:  Modules, derived types, dynamical memory allocation

Example 2: Pointers

Example 3: Select case

Last modified Nov 8, 2017