1 / 16

More on F90

More on F90. Outline: Parameter statements Comments Program layout-- execution part if/then/else Case statements do loops goto Intrinsic functions Input/output Calling subroutines: call rs example Array assignment For the first project…. I. Parameter statements.

Download Presentation

More on F90

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More on F90 Outline: Parameter statements Comments Program layout-- execution part if/then/else Case statements do loops goto Intrinsic functions Input/output Calling subroutines: call rs example Array assignment 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 Just a single if statement… no then or else: if (x<0) x=0 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=ex 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. Example: calling rs from the execution part • First look at subroutine itself… comments and declaration • part show us what to do… 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) Input matrix Real variable arrays but of higher precision Output eigenvalues Output eigenvectors

  14. 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 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)

  15. XI: Array assignment REAL, DIMENSION(10,10) :: a,b,c REAL :: d INTEGER :: i,j Assigns the value 2.0 to each element of a a=2.0 d=2.0 Assigns 2.0 to the real scalar variable d do i=1,10 do j=1,10 b(i,j) = real(i+j) enddo 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…

More Related