1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 (...

41
1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 (subset F): Basics Example programs in detail
  • date post

    15-Jan-2016
  • Category

    Documents

  • view

    219
  • download

    0

Transcript of 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 (...

Page 1: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

1

BIL106E Introduction to

Scientific & Engineering Computing

Organizational matters

Fortran 90 (subset F):

Basics

Example programs in detail

Page 2: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

2

Top-down programming

4 basic steps

1. Specify the problem clearly

2. Analyze the problem and break it down into

its fundamental elements

3. Code the program according to the plan

developed at step 2

4. Test the program exhaustively, and repeat

steps 2 and 3 as necessary until the

program works in all situations that you can

envisage

Page 3: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

3

Program = Data Types + Algorithms

Data types: what you work on

Algorithms:what you do with them

Page 4: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

4

Structure of a program

Heading (program, module, etc.) specification part execution part subprogram part end program statement

Page 5: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

5

program Radioactive_DecayRadioactive_Decay!----------------------------------------------------------! This program calculates the amount of a radioactive substance that ! remains after a specified time, given an initial amount and its ! half-life. Variables used are:! InitalAmount : initial amount of substance (mg)! HalfLife : half-life of substance (days)! Time : time at which the amount remaining is calculated (days)! AmountRemaining : amount of substance remaining (mg)! Input: InitialAmount, HalfLife, Time! Output: AmountRemaining!----------------------------------------------------------

implicit nonereal :: InitialAmount, HalfLife, Time, AmountRemaining! Get values for InitialAmount, HalfLife, and Time.print *, "Enter initial amount (mg) of substance, its half-life days)"print *, "and time (days) at which to find amount remaining:"read *, InitialAmount, HalfLife, Time! Compute the amount remaining at the specified time.AmountRemaining = InitialAmount * 0.5 ** (Time / HalfLife)! Display AmountRemaining.print *, "Amount remaining =", AmountRemaining, "mg"

end program Radioactive_DecayRadioactive_Decay

Page 6: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

6

Data Types

Five basic types:1. integer2. real3. complex4. character5. logical

Data types of ‘container’ classes

Page 7: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

7

Integers

a whole number (positive, negative or zero)

no decimal point Examples

0123-23456+123789

Page 8: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

8

Reals

Numbers with decimal fractions There has to be decimal point Examples

1.23456-0.0019875678.

Another representation:1.952e3 (1.952*10^3) 0.1952e4123.456e-8

Page 9: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

9

Character

Sequence of symbols from the Fortran character set

Enclosed between double quotes Examples

"This is a string""I do, I don't""1234abc345"

Page 10: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

10

Logical

Can take only two values:

.TRUE.

.FALSE.

Page 11: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

11

Identifiers Names used to identify programs,

constants, variables, etc. Identifiers must begin with a letter This can be followed by up to 30

letters, digits, underscores F_World is Case sensitive lower or upper case letters are importantlower or upper case letters are important

YourName not equalnot equal yourname But most of the FORTRAN compiler are case insensitive.

PROGRAM, program, proGRAM, pRoGrAm are all the same.

Page 12: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

12

Identifiers

ExamplesCurrentDecay_Ratepressurean_identifier_with_a_long_namethe_best_program

Page 13: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

13

Constants

1029 is an integer constant

12.3 is a real constant

"What a nice day!" is a character

constant

Page 14: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

14

VariablesVariables:: It is usual for people to associate a name or phrase with a piece of information. For example, the phrase "today's date" has an associated numeric value which varies day by day. This is similar to the concept of a program variable; a program variable is some object (named by the programmer) which uniquely identifies a piece of data stored in memory. Variables are value containers Compiler associates with a variable

a memory location Value of a variable at any time is

the value stored in the associated memory location at that time

Page 15: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

15

Variables

123.5623456

MEMORY

Hello World

1234567

Message

Payment

Page 16: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

16

Declarations of Variables

Form:type-specifier :: list

Declares that the identifiers in the list have the specified type

Type statements must appear in the specification part of the program

Examplesinteger :: number_years, counts, monthsreal :: Mass, Velocity, Accelerationcharacter (len=12) :: MyName, YourName

Page 17: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

17

implicit none

It should be placed at the beginning of the specification part

You have to declare all variables you will be using in the program!

Page 18: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

18

Variable initialization

All variables are initially undefined Initialization in the declarations

Examples:real :: W=1.2, z=5.67, mass=4.56integer :: year=1998, count=0

Page 19: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

19

Named constants

Form:type-specifier, parameter :: list

Examplesinteger, parameter :: INITCOUNT = 30real, parameter :: G = 9.81

It's a good idea to write named constants in upper case

Page 20: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

20

Arithmetic operations

Variables and constants can be processed by using operations and functions appropriated to their types.

Operations

Page 21: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

21

Operator Operation

+ Addition, unary plus

- Subtraction, unary minus

* Multiplication

/ Division

** Exponentiation

Page 22: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

22

Operations Examples

To calculate B2 - 4ACB**2 - 4*A*C

Types are important: 9/4 = 2

9.0/4.0 = 2.25 Mixed-mode expressions:

3 + 8.0/5 * 3 + 8.0/5.0 * 3 + 1.6 * 3.0 + 1.6 * 4.6 =???

Page 23: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

23

Priority rules

All exponentiations are performed first; consecutive exponentiations are performed from right to left

All multiplications and divisions are performed next; in the order in which they appear from left to right

Additions and subtractions are performed last, in the order in which they appear from left to right

**

*, /

+, -

Page 24: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

24

Some examples

2 ** 3 ** 2 = 512 10/5 *2 = 2 * 2 = 4

To calculate 51/3

5.0**(1.0/3.0) but not 5.0**1.0/3.0

Page 25: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

25

Library functions

abs(x) Absolute value of x cos(x) Cosine of x radians exp(x) Exponential function int(x) Integer part of x sqrt(x) Square root of x

Page 26: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

26

Assignment statement Form:

variable = expression Assigns the value of expression to variable Assignment is not a statement of algebraic

equality; it is a replacement statement Examples

Density = 2000.0Volume = 3.2Mass = Density*VolumeWeightRatio = log(Mass/90.)

Page 27: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

27

Programs need to communicate with users! Two kinds of I/O (for the moment!):

– Formatted I/O– List-directed I/O

List-directed outputprint *, output-listwrite (unit=*, fmt=*) output-list

List-directed inputread *, input-listread (unit=*, fmt=*) input-list

Page 28: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

28

List-directed I/O

Examplesprint *, "Tell me your birthday"write (unit=*, fmt=*) a, b, c**2read *, day, month, year

Page 29: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

29

Page 30: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

30

Page 31: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

31

Page 32: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

32

Page 33: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

33

Page 34: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

34

Page 35: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

35

Page 36: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

36

Page 37: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

37

Page 38: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

38

Page 39: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

39

! Asks the hour and the minute values of the ! present time and displays it as a sentence.program showtimeinteger :: hh,mm print *, " Input the hour, in the 24 hours format. (hh)" read *, hh print *, " Input the minute. (mm)" read *, mm print *, "" print *, " THE TIME IS ",mm," MINUTES AFTER ",hh end program showtime

Page 40: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

40

CLASSWORK

In Einstein’s famous equation E=mc2, the energy E is in joules if the mass m is in kilograms and c is the speed of light in meters per second (=2.9979*108). Write a program to calculate the energy equivalent of a given mass. Roughly how much energy is equivalent to the mass of a sugar cube (approximately 1 gram)?

Page 41: 1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.

41

A body that experiences a uniform acceleration moves a distance s and a velocity v in a time t, respectively, where s and v are given by the formulae:, , v=at+uwhere a is the acceleration in m/s2, and u is the initial velocity in m/s. A body falling freely under gravity is in such a situation, with a=g=9.81 m/s2.Write a program that asks for the user initial downward velocity (in m/s) and time of flight (in s and v) of the body. The program should then calculate and print the height and the velocity from which the body fell.

uts2

at 2