Docsity

Fortran Programming Basics - Mechanical Engineering - Lecture Slides, Slides of Mechanical Engineering

Partial preview of the text

Download Fortran Programming Basics - Mechanical Engineering - Lecture Slides and more Slides Mechanical Engineering in PDF only on Docsity!

MET 50

Fortran programming basics

The basic layout of all programs is as follows

(p.33)

PROGRAM name = heading (i.e., title) Specifications (e.g., define constants) Executions (the main part) Subprograms (see much later in the semester) END PROGRAM statement

Let’s look at each…

Examples…

  • Areaofcircle - OK
  • Temp_conversion (for deg F to deg C) - OK
  • 2Version_speed – NOT OK (“2”)
  • Mybro’scode – NOT OK (“ ’ ”)

Specifications…

  • Any quantity in your program MUST be given a name – by you.
  • Includes known things
    • e.g., “g” – acceleration of gravity (9.8 m/s 2 )
    • things like data that you might input.
  • Also includes unknowns – the things you are trying to calculate.

Memory …

  • Once we do this, the compiler allocates a memory location to each variable.

Specifications…

  • Always choose meaningful names.
  • Gravity → GRAV
  • Value of π → PI
  • Coriolis parameter → CORIOLIS
  • Kinetic energy → KE (but this starts with a “K” which can be a problem because…)

IN OTHER WORDS, A Fortran program will

assume a quantity is one of these (and nothing else).

Rule:

When you choose a name for a quantity starting with the

letters I,J,K,L,M,N, Fortran will make that quantity an

INTEGER.

Example:

  • If we decide to name “gravity” as “KGRAV”, it will be treated as an integer!
  • Its value (entered as 9.8) would be recorded in memory as either “9” if truncated – or “10” if rounded up.
  • Will give an error in calculations 
  • If we name kinetic energy as “KE”, it will be treated as an integer!

We must be careful to avoid these problems…see below

REAL data…

  • Not a whole number!
  • Positive or negative
    • MUST include minus sign if negative
  • MUST include a decimal (e.g., 9.81 or 17.0)
  • Often write in exponent form:
  • 6371 x 103 → 6.371E
  • 1.48x 10 -17^ → 1.48E-

COMPLEX data…

  • Let’s deal with this later 

LOGICAL data…

  • Let’s deal with this later (or never) 

To repeat…when you choose a name for a

quantity starting with the letters I,J,K,L,M,N, Fortran will make that quantity an INTEGER.

Otherwise it is a REAL data type.

We often choose names to make use of this…but

we also make mistakes.

Examples of format:

REAL : : grav

INTEGER : : timeofday

Says the following:

  1. “grav” will be a REAL number

  2. “timeofday” will be an INTEGER number

Statements like these MUST go @ start of code

(“specifications”).

As a safeguard, your first statement should be

this:

IMPLICIT NONE

Which says to the compiler … “do not assume