Met 50
Download
1 / 19

MET 50 - PowerPoint PPT Presentation


  • 141 Views
  • Uploaded on

MET 50. Sub-programs. Using sub-programs. The codes we/you have written so far are teeny (10-40 lines). In science & engineering, many codes are huge . Thousands of lines of code. Using sub-programs. Programmers 99.999% prefer to break the overall task into smaller pieces.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' MET 50' - elsa


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Met 50

MET 50

Sub-programs


Using sub programs
Using sub-programs

The codes we/you have written so far are teeny (10-40 lines).

In science & engineering, many codes are huge.

Thousands of lines of code.

MET 50, FALL 2011, CHAPTER 6 PART 1


Using sub programs1
Using sub-programs

Programmers 99.999% prefer to break the overall task into smaller pieces.

Then we write individual “sub-programs” to do each task!

Each piece is a module.

MET 50, FALL 2011, CHAPTER 6 PART 1


Using sub programs2
Using sub-programs

  • Modular programming : http://en.wikipedia.org/wiki/Modular_programming

  • GFDL site: http://www.gfdl.noaa.gov/

    • → model development

    • → atmospheric

    • → model development

    • → The AM2 model

    • → Fortran module documentation

    • → huge list of modules (e.g., ice-model.html)

MET 50, FALL 2011, CHAPTER 6 PART 1


Using sub programs3
Using sub-programs

Example:

To make a weather forecast….

We need to pay attention to:

Solar radiation heating – compute in one code

Longwave radiation cooling – compute in one code

Cloud cover – compute in one code

Evaporation from the ocean – compute in one code

etc etc.

MET 50, FALL 2011, CHAPTER 6 PART 1


Using sub programs4
Using sub-programs

It makes sense to write a separate chunk of code for each task!

Makes initial development easier

Allows upgrades easily

MET 50, FALL 2011, CHAPTER 6 PART 1


Using sub programs5
Using sub-programs

Types of sub-program:

Functions – simpler

Subroutines – more sophisticated

MET 50, FALL 2011, CHAPTER 6 PART 1


Using sub programs6
Using sub-programs

In BOTH cases, we:

Send some information from the main program to the sub-program

The sub-program does some calculation(s)

The sub-program returns an value to the main program.

MET 50, FALL 2011, CHAPTER 6 PART 1


Using sub programs7
Using sub-programs

(A) Functions:

We have already met intrinsic functions.

Built in to the Fortran compiler.

Things like “sin(x)”

When we write: Y = SIN(X), our code is calling an (intrinsic) function.

MET 50, FALL 2011, CHAPTER 6 PART 1


Using sub programs8
Using sub-programs

We don’t get to see the code behind these.

Sometimes, we build our own functions!

The code looks like:

MET 50, FALL 2011, CHAPTER 6 PART 1


Using sub programs9
Using sub-programs

FUNCTION name (arguments)

Usual Fortran statements

Execution statements

RETURN

END FUNCTION name

name has to obey usual rules

“arguments” are values sent over from the main program

“RETURN” statement sends results back to the main program

MET 50, FALL 2011, CHAPTER 6 PART 1


Using sub programs10
Using sub-programs

Example:

FUNCTION f_to_c(Temperature)

! or… REAL FUNCTION f_to_c(Temperature)

!

! Input: Temperature = temp in deg F

! Output: f_to_c= temp in deg C

!

IMPLICIT NONE

REAL :: f_to_c, Temperature

!

f_to_c= (Temperature - 32.0)*9./5.

!

RETURN

END FUNCTION f_to_c

MET 50, FALL 2011, CHAPTER 6 PART 1


Using sub programs11
Using sub-programs

How is this module “summoned” from the main code???

PROGRAM TEMPCON

! This code reads in a temperature (F) and converts to Celcius

IMPLICIT NONE

REAL :: TEMPIN, TEMP

!

PRINT*,’ENTER A TEMPERATURE IN DEGREES F:’

READ*,TEMPIN ! example: read in TEMPIN = 82.

! Convert using function f_to_c

TEMP =f_to_c(TEMPIN)! Sends the value 82. to the function to be converted

!

PRINT*,TEMPIN,TEMP ! Prints 82. and 28. (try it!)

!

END PROGRAM TEMPCON

MET 50, FALL 2011, CHAPTER 6 PART 1


Using sub programs12
Using sub-programs

So:

YOU get to decide which pieces of code to assign to a function.

YOU get to write the code for that function.

YOU get to put code in the MAIN PROGRAM which gets information from the function.

And:

YOU get to decide where to put the “function code”.

???

MET 50, FALL 2011, CHAPTER 6 PART 1


Using sub programs13
Using sub-programs

There are two places we can put the “function code”:

At the end of the main code/after the main code.

At the end of the main code/inside the main code.

Going back to our TEMPCON and f_to_cexample…

MET 50, FALL 2011, CHAPTER 6 PART 1


Using sub programs14
Using sub-programs

At the end of the main code/after the main code.

PROGRAM TEMPCON

IMPLICIT NONE

REAL :: TEMPIN, TEMP

PRINT*,’ENTER A TEMPERATURE IN DEGREES F:’

READ*,TEMPIN

TEMP =f_to_c(TEMPIN)

PRINT*,TEMPIN,TEMP

END PROGRAM TEMPCON

FUNCTION f_to_c (Temperature)

! Input: Temperature = temp in deg F

! Output: f_to_c = temp in deg C

IMPLICIT NONE

REAL :: f_to_c, Temperature

f_to_c = (Temperature - 32.0)*9./5.

RETURN

END FUNCTION f_to_c

MET 50, FALL 2011, CHAPTER 6 PART 1


Using sub programs15
Using sub-programs

At the end of the main code/inside the main code.

PROGRAM TEMPCON

IMPLICIT NONE

REAL :: TEMPIN, TEMP

PRINT*,’ENTER A TEMPERATURE IN DEGREES F:’

READ*,TEMPIN

TEMP =f_to_c(TEMPIN)

PRINT*,TEMPIN,TEMP

CONTAINS ← new KEYWORD

FUNCTION f_to_c (Temperature)

! Input: Temperature = temp in deg F

! Output: f_to_c = temp in deg C

IMPLICIT NONE

REAL :: f_to_c, Temperature

f_to_c = (Temperature - 32.0)*9./5.

RETURN

END FUNCTION f_to_c

END PROGRAM TEMPCON

MET 50, FALL 2011, CHAPTER 6 PART 1


Using sub programs16
Using sub-programs

Main code could look like:

PROGRAM MAIN

IMPLICIT NONE

REAL :: blablabla

whatever

CONTAINS ← new KEYWORD

FUNCTION sub1 (X1)

blablabla

RETURN

END FUNCTION sub1

FUNCTION sub2 (X2)

blablabla

RETURN

END FUNCTION sub2

etc.

END PROGRAM MAIN

MET 50, FALL 2011, CHAPTER 6 PART 1


Using sub programs17
Using sub-programs

(B) Subroutines:

With a function, we can only get ONE piece of information back.

With a subroutine, we can get MULTIPLE pieces of information back  much more powerful.

MET 50, FALL 2011, CHAPTER 6 PART 1


ad