Docsity

Repetitive Exection - Mechanical Engineering - Lecture Slides, Slides of Mechanical Engineering

Partial preview of the text

Download Repetitive Exection - Mechanical Engineering - Lecture Slides and more Slides Mechanical Engineering in PDF only on Docsity!

MET 50

Repetitive EXECUTION

MET 50

fabulous

“do loops”

  • Example: Compute quantities at multiple

locations around the globe.

  • Compute pressure tendency over the last 3 hours.
    • At the surface in New York.
    • At 1 km elevation over New York.
    • At 2 km elevation over New York.
    • At 3 km elevation over New York.
    • Etc. 4

How can we do these repetitive computations (e.g., a thousand times) in Fortran? A. Cut and paste the code a thousand times?

  • Nah! B. Or: Invent Fortran code structure to loop thru our lines of calculation code a thousand times?
  • Epic idea!
  • This is the DO LOOP 5

meaning… DO NUM = 1, 4 (insert code you’re here, e.g., calculations) END DO means:

  1. Set NUM=1; do the calculations; “go back to start of loop”
  2. Set NUM=2; do the calculations; “go back to start of loop”
  3. Set NUM=3; do the calculations; “go back to start of loop”
  4. Set NUM=4; do the calculations; DO NOT “go back to start of loop” Once NUM has reached the maximum value (“4” in this example), finish the calculations and EXIT THE LOOP. 7

example…

DO NUM = 1, 4 PRINT*, ‘value is’,NUM END DO Would produce: (since NUM =1) value is 1 (since NUM =2) value is 2 (since NUM =3) value is 3 (since NUM =4) value is 4 8

DO NUM = start value, end value, step

(insert code you’re here, e.g., calculations)

END DO

OK to not write “step”

  • compiler will assume “step = 1” if nothing is

written

DO NUM = 1,10 implies DO NUM = 1,10,1 (^10)

RULES:

DO NUM = start value, end value, step

1) “start value”“end value”“step” may NOT be

modified once you are in the loop

2) It is OK to jump OUT of a LOOP (see below)

3) Cannot jump INTO a loop

11

Looks dense…instead write like this:

DO “from west coast to east coast” DO “from Mexican border to Canadian border ” DO “from ground level to tropopause” (fancy meteorological calculations) ENDDO ENDDO ENDDO 13

Which is executed inside out:

DO “ from west coast to east coast ” – set at 1 st value DO “ from Mexican border to Canadian border ” – set at 1 st value DO “ from ground level to tropopause ” – do for ALL values (fancy meteorological calculations) ENDDO ENDDO ENDDO 14

Which is executed inside out:

DO “ from west coast to east coast ” – do for rest of values DO “ from Mexican border to Canadian border ” – again do for ALL values DO “ from ground level to tropopause ” – again do for ALL values (fancy meteorological calculations) ENDDO ENDDO ENDDO 16

DO I=1,1000 I= DO J=1, 2000 J= DO K=1,50 K=1 TO 50 (fancy meteorological calculations) ENDDO ENDDO ENDDO Then… DO I=1,1000 I= DO J=1, 2000 J= DO K=1,50 K=1 TO 50 (fancy meteorological calculations) ENDDO ENDDO ENDDO Etc. 17

3 versions - general form on last slide:

(B)

DO

IF (something) EXIT! Which means jump out of loop! (insert code you’re here, e.g., calculations) END DO (C) DO (insert code you’re here, e.g., calculations) IF (something) EXIT! Which means jump out of loop! END DO 19

Example: IMPLICIT … REAL … ! DO READ*, input some MET data IF (we’re at the end of the data) EXIT! (calculations e.g., averaging, finding max values etc.) END DO (next set of statements) 20