ame 150 l

33
7 - 1/26/2000 AME 150 L AME 150 L Fortran Statements Functions & Simple IO

Upload: zubin

Post on 05-Jan-2016

42 views

Category:

Documents


1 download

DESCRIPTION

AME 150 L. Fortran Statements Functions & Simple IO. Elementary Input/Output. Input: READ ( unit, format ) list of variables Simple form: READ (*,*) list Read from Standard Input (keyboard unless redirected) Output: WRITE ( unit, format ) list of variables - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: AME 150 L

7 - 1/26/2000 AME 150 L

AME 150 L

Fortran Statements

Functions & Simple IO

Page 2: AME 150 L

7 - 1/26/2000 AME 150 L

Elementary Input/Output

• Input: READ (unit, format) list of variables

• Simple form: READ (*,*) list– Read from Standard Input (keyboard unless redirected)

• Output: WRITE (unit, format) list of variables

• Simple form: WRITE (*,*) list– Display on Standard Output (display unless redirected)

• Reading &/or writing is done upon execution

Page 3: AME 150 L

7 - 1/26/2000 AME 150 L

Model Program

PROGRAM model1

IMPLICIT NONE

! Declarations of input data & results

READ ( *,*) input data

results = calculations on input data

WRITE (*,*) results

STOP

END PROGRAM model1

Page 4: AME 150 L

7 - 1/26/2000 AME 150 L

Declarations

• IMPLICIT NONE ALL variables must be declared

• Declared means that the variable name must appear in an INTEGER or REAL or other TYPE statement, i.e.

REAL :: velocity, a1, x, y, z, Mach_Number

INTEGER :: index, I, j, k, Counter

Page 5: AME 150 L

7 - 1/26/2000 AME 150 L

Role of Declaration

Declaration statement tells compile what type of arithmetic to use with variable

Operand Other Operand Arithmetic

INTEGER INTEGER INTEGER

INTEGER REAL REAL

REAL REAL REAL

Page 6: AME 150 L

7 - 1/26/2000 AME 150 L

Mixed Mode Arithmetic

• Mixed Mode results for every expression takes form of most complex

• Avoid if possible (confusion can result)• Never mix CHARACTER or LOGICAL with

REAL or INTEGER

• Problems and confusion occurs more with constants than with variables

Page 7: AME 150 L

7 - 1/26/2000 AME 150 L

Mixed Mode Examples

INTEGER :: I, J, K

REAL X, Y, Z

I+J ?

Integer

X+Y ?

Real

I+X ?

Real

I / J ?

Integer

5 / 2 ?

Integer = 2 (not 2.5)

X + I / K ?

Real + Integer = Real

2.7 + 5 / 2 ?

2.7 + 2 = 4.7 (not 5.2)

Page 8: AME 150 L

7 - 1/26/2000 AME 150 L

ExponentiationReal ** Integer Repeated Multiplication (-7.5**3)

Real ** -Integer 1./(Real ** Integer )

Integer ** Integer Repeated Multiplication (5**7)

Integer ** -Integer 0 (Why?) I**-J = 1/(I**J) 0 But K /I**J not necessarily 0

Integer ** Real Never Allowed (Why?)

X ** Y Exponential (Y * log(X))

X MUST BE Positive (Why?)

Page 9: AME 150 L

7 - 1/26/2000 AME 150 L

Reminder - Hierarchy of Operations

• First, evaluate all (…) including functions

• Start evaluations without (..) with– Unary minus -– Exponentiation **– Multiply & Divide (left to right generally) * /– Add and Subtract + -

Page 10: AME 150 L

7 - 1/26/2000 AME 150 L

Arithmetic Types

• Arithmetic operation affect ONLY arithmetic variables– INTEGER– REAL– COMPLEX– Multiple precision (machine dependent)– Special TYPE based on arithmentic

Page 11: AME 150 L

7 - 1/26/2000 AME 150 L

Other Types

LOGICAL– Special Operators (AND, OR, NOT, etc)– Can create by relational operations

X > Y (read X greater than Y) is ONLY .True. or .False.

Relational Operators

== Equal to /= Not equal to

> Greater than <= Less than or equal to

< Less than >= Greater than or equal to

Page 12: AME 150 L

7 - 1/26/2000 AME 150 L

Relational Operators

• Can mix mode, but must be careful

• Can use them on CHARACTER data type

• Cannot mix Character with Arithemetic

• Always .true. or .false. Never .maybe.

• Relational operators are lower in the hierarchy than all arithmetic

• There is no .approximately. or .close to.

Page 13: AME 150 L

7 - 1/26/2000 AME 150 L

The Real Curse

• All computers use binary

• We use decimal

• We know 1./3. = 0.333333333…

• and 1./5. = 0.2 (exact)

• In Binary, both repeat indefinitely

Page 14: AME 150 L

7 - 1/26/2000 AME 150 L

PROGRAM reportimplicit noneREAL :: third, fourth,fifththird=1./3.fourth=1./4.fifth=1./5.WRITE(*,*)third, fourth,fifthWRITE(*,"(3o20)" ) third, fourth,fifthstopEND PROGRAM report

Output: Decimal 0.33333334 0.25000000 0.20000000 Octal 7652525253 7640000000 7623146315 HexaDecimal 3EAAAAAB 3E800000 3E4CCCCD

Page 15: AME 150 L

7 - 1/26/2000 AME 150 L

Storage of REAL Variables• Engineering or Science notation, 1.23 105

– 1.23 has significant figures (in decimal)– 5 is the exponent (power of 10)

• Normalized Floating Point 0.123 106

– fraction is between 1 and 1/10th

• Computer data is binary 1+fraction 2exponent

– fraction is less than 1, but number is 1.bbbb– both fraction & exponent can be + or -

Page 16: AME 150 L

7 - 1/26/2000 AME 150 L

Examples• Integer

13 = 23+22+20= 11012=158=d16

• Real13.0=23*(20+2-1+2-3)exponent=8=10002

and the number =1+.5+.125=1.62510=1.1012

• Negative numbers -- use 2's complement– Change all 1s to 0s and all 0s to 1's (ones

complement) and then add 1

Page 18: AME 150 L

7 - 1/26/2000 AME 150 L

Sample Calculation

• Given: 3 coefficients a,b,c of quadratic equation: ax2+bx+c=0

• Find 2 roots of the equation x1, x2 where

• More in B: p87-91, D: p156-161

1,2

2 42

b acbxa

Page 19: AME 150 L

7 - 1/26/2000 AME 150 L

Trial 1

PROGRAM T1

REAL :: a,b,c,x1,x2

READ(*,*)a,b,c

x1=(-b+sqrt(b**2-4.*a*c))/(2.*a)

x2=(-b-sqrt(b**2-4.*a*c))/(2.*a)

WRITE(*,*)"x1,x2=",x1,x2

END PROGRAM T1

Page 20: AME 150 L

7 - 1/26/2000 AME 150 L

Problems with T11) a=0 -- divide by 0

must test for a=0, and if so, the equation is

bx+c=0, so that only one solution exists x=-c/b (as long as b 0 -- not important since c=0 too)

2) a 0, but b2-4ac <0, so the two roots are a complex conjugate pair

3) a 0, and b2-4ac =0, so there are two equal roots

Page 21: AME 150 L

7 - 1/26/2000 AME 150 L

First Repair

• Fix the b2-4ac <0 problem firstb2-4ac (and its square root) is computed twice

Let's test for negative first

Do an intermediate calculation

disc = b**2-4.*a*c

and make sure disc >0 before taking SQRT

Need something like "if disc is less than 0, then"

IF (disc < 0) THEN

Page 22: AME 150 L

7 - 1/26/2000 AME 150 L

Trial 2

PROGRAM T1

REAL :: a,b,c,x1,x2

READ(*,*)a,b,c

x1=(-b+sqrt(b**2-4.*a*c))/(2.*a) !These lines will

x2=(-b-sqrt(b**2-4.*a*c))/(2.*a) !be replaced

WRITE(*,*)"x1,x2=",x1,x2

END PROGRAM T1

Page 23: AME 150 L

7 - 1/26/2000 AME 150 L

Creating T2.f90PROGRAM T2

REAL :: a,b,c,x1,x2,disc !declare a new variable

READ(*,*)a,b,c

disc = b**2 - 4. * a * c !calculate this 1 time

IF (disc >= 0 ) THEN

x1 = ( -b + sqrt(disc) )/(2.*a) !Executed when (…) is .TRUE.

x2 = ( -b - sqrt(disc) )/(2.*a)

ELSE ! The ELSE clause is executed

!Do something !when (…) is .FALSE.

END IF

WRITE(*,*)"x1,x2=",x1,x2

END PROGRAM T2

Page 24: AME 150 L

7 - 1/26/2000 AME 150 L

Complex Conjugate Roots

• When disc < 0, then calculate i*sqrt(-disc)

– where i2 = -1

• Complex Conjugate RootsReal Part: xreal = -b/(2.*a)

Imaginary part ximag= SQRT(-disc)

Page 25: AME 150 L

7 - 1/26/2000 AME 150 L

Finishing T2.f90

REAL :: a,b,c,x1,x2,disc,xreal,ximag !declare a new variables

disc = b**2 - 4. * a * c !calculate this 1 time

IF (disc >= 0 ) THEN

WRITE(*,*)"x1,x2=",x1,x2 !move the Real root write

ELSE ! The ELSE clause is executed

xreal = -b/(2.*a) !calculate real part

ximag = sqrt(-disc)/(2.*a) !and imaginary part

WRITE(*,*)"Complex Conjugate roots",xreal,"+-I*",ximag

END IF

END PROGRAM T2

Page 26: AME 150 L

7 - 1/26/2000 AME 150 L

Special Case a=0

• Test first for a /= 0, and do "t2.f90" calculations if .TRUE.

• IF (a==0) [i.e. (a/= 0) .FALSE. Use ELSE]Write a message

Calculate one root (answer = -c/b)

• Save these as t3.f90• t4.f90 is cleaned up (and no extra calculations)

Page 27: AME 150 L

7 - 1/26/2000 AME 150 L

Weird Problem

• Case a,b,c = 1,2,1 has roots -1,-1• Case a,b,c = .33333,.66666,.33333 also has

roots -1,-1• BUT a,b,c=.66667, 1.33333,.66667 caused an

arithmetic exception and dumped core• AND a,b,c=0.333333,.666667,.333333 had

roots x1, x2 = -0.998264432, -1.00173867

ARITHMETIC ROUND-OFF

Page 28: AME 150 L

7 - 1/26/2000 AME 150 L

No Logical Close-to

• disc==0 is hard to make happen

• REAL number calculation tries its best to round off, but it is hard

• Sample error - counting with REAL

Page 29: AME 150 L

7 - 1/26/2000 AME 150 L

PROGRAM xcountIMPLICIT noneREAL :: x,xmin,xmax,deltax,nstepsINTEGER :: Count

WRITE(*,*)"How many steps do you want?"READ(*,*)nsteps

!Initialize the variablesxmin=0xmax=100.deltax=(xmax-xmin)/nstepsCount=0x=xminDO x=x+deltax Count=Count+1 IF (x == xmax)EXITEND DOWRITE(*,*)x, CountStopEND PROGRAM xcount

Page 30: AME 150 L

7 - 1/26/2000 AME 150 L

What is happening?

• Sometimes the program works, sometimes it doesn't

• All failures occur for cases where(xmax-xmin)/nsteps is a repeating binary

number (like 0.333333 is for 1/3.)

0.333333*3 =0.999999 -- close to, but not == 1

• The relation real1 == real2 is a problem

Page 31: AME 150 L

7 - 1/26/2000 AME 150 L

Machine Epsilon• Epsilon (): Math. a small, positive number

• Machine epsilon refers to the least significant bit -- often it is 2-precision in REAL numbers on your computer

• Example: If there is 23 bits in a REAL fraction, machine_epsilon=1.19209E-07

Page 32: AME 150 L

7 - 1/26/2000 AME 150 L

Using Machine Epsilon

• Differences in real numbersx1-x2 < eps

• Need the absolute value of the differenceABS(x1-x2) < eps

• What happens if x1,x2 is very large (small)ABS(x1-x2) / ABS(x1+x2) < eps

• Is this ok?

Page 33: AME 150 L

7 - 1/26/2000 AME 150 L

Machine Epsilon (continued)

• Problem is, there is a divide (slow), and if both x1 & x2 are 0, a divide by 0 error

ABS(x1-x2) < eps*ABS(x1+x2)

• Almost, but certainly, both x1 and x2 =0 should pass this test

ABS(x1-x2) <= eps*ABS(x1+x2)

is .TRUE. when x1 is approximately= x2