Download presentation
Presentation is loading. Please wait.
Published byLeon Austin Modified over 9 years ago
1
More on F90 Outline: I.Parameter statements II.Comments III.Program layout-- execution part IV.if/then/else V.Case statements VI.do loops VII.goto VIII.Intrinsic functions IX.Input/output X.Calling subroutines: call rs example XI.Array assignment XII.For the first project…
2
I. Parameter statements Not really parameters, but instead constants Can be integer, real, etc. REAL, PARAMETER :: pi=3.141592 INTEGER, PARAMETER :: MAX=100 Once set, parameters cannot be changed within your code!
3
II. Comments Comments start with c or ! No function other than notes for programmer Still important! ! Program to compute eigenfunctions and eigenvalues ! for a particle in a one-dimensional potential Comments are especially important for anyone else who inherits your code!
4
III. Program layout-- execution part PROGRAM program-name IMPLICIT NONE [specification part] [execution part] [subprogram part] END PROGRAM program-name The execution part is control statements, subroutine calls, functions, and intrinsic functions
5
IV. If/then/else Control statement Can nest as many as one needs if (x<0) x=0 Just a single if statement… no then or else: Or, when several conditions are desired: if (x<0) then x=0 else if (x>100) x=100 endif Notice indentation! Terminate with “endif”
6
V. Select Case construct Essentially an alternative to if/then/else Another example of control statements INTEGER :: option select case(option) case(1) x=0 case(2) x=100 case default x=1 end select
7
VI. Do loops… Another control statement Useful for repeated calculations INTEGER :: n REAL :: x,y do n=1,100 x=pi*n y=cos(x) enddo Integer n takes on Values 1,2,3,…,100 Again indentation
8
VII. Goto statements Often best to avoid as much as possible Very often used in conjunction with “if” statements REAL :: x if (x<0) goto 100 … 100 x=0 Statements that Are only done if x>0 Line number 100 (just a tag or label)
9
VIII. Intrinsic functions Mathematical functions Intrinsic to f90 compiler REAL :: x,y y=cos(x) y=exp(x) y=atan(x) Cosine of argument x y=e x y=tan -1 (x) Links to intrinsic function list given in my webpage
10
IX. Input/output Input data usually from “read” statement… Input file Output results from “write” statement… screen or output file Everything needs a unit number Format statements… useful, sometimes needed, can be a pain REAL :: x,y y=cos(x) y=exp(x) y=atan(x)
11
IX. Input/output Input data usually from “read” statement… Input file Output results from “write” statement… screen or output file Everything needs a unit number Format statements… useful, sometimes needed, can be a pain INTEGER :: n,i,j,k REAL :: x,y,z open(unit=10,file=‘input.dat’) open(unit=11,file=‘output.dat’) read(10,*) i,j,k,x,y n=I+j+k z=x+y write(11,*) n,z write(6,*) n,z Unformatted input and output unit=6 reserved for output to screen
12
X. Calling a subroutine Subroutines for frequently-used applications… portable Example: rs….. Diagonalize a real-symmetric matrix Arguments represent common variables subroutine rs(nm,n,a,w,matz,z,fv1,fv2,ierr) Arguments of the subroutine Arguments need to be of same type/dimension as in subroutine
13
subroutine rs(nm,n,a,w,matz,z,fv1,fv2,ierr) integer n,nm,ierr,matz double precision a(nm,n),w(n),z(nm,n),fv1(n),fv2(n) Real variable arrays but of higher precision Example: calling rs from the execution part Input matrix Output eigenvalues Output eigenvectors First look at subroutine itself… comments and declaration part show us what to do…
14
subroutine rs(nm,n,a,w,matz,z,fv1,fv2,ierr) integer n,nm,ierr,matz double precision a(nm,n),w(n),z(nm,n),fv1(n),fv2(n) Call statement in your code for project #1 First look at subroutine itself… comments and declaration part show us what to do… INTEGER, PARAMETER:: ndim=10 INTEGER :: ierr REAL*8, DIMENSION(ndim) :: w,fv1,fv2 REAL*8, DIMENSION(ndim,ndim) :: hami,z … call rs(ndim,ndim,hamil,w,z,fv1,fv2,ierr) Input H matrix Output
15
XI: Array assignment REAL, DIMENSION(10,10) :: a,b,c REAL :: d INTEGER :: i,j a=2.0 d=2.0 Assigns the value 2.0 to each element of a Assigns 2.0 to the real scalar variable d do i=1,10 do j=1,10 b(i,j) = real(i+j) enddo c=a+b The real value of i+j, since both i and j are integers Assigns all elements of c
16
XII: For the first project… Define elements of hamil(nmax,nmax) using nested do loops Call rs subroutine to diagonalize hamil Analyze the eigenvalues w returned from hamil Analyze and plot the five lowest eigenfunctions… gnuplot Compare to estimates obtained from perturbation theory for the eigenvalues Requires intrinsic functions dexp and dcos… (double-precisio exponential and cosine functions…
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.