0% found this document useful (0 votes)
7 views26 pages

Lecture 5.3 - Introduction To Fortran 90-3

Tt

Uploaded by

Claudius Maximus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views26 pages

Lecture 5.3 - Introduction To Fortran 90-3

Tt

Uploaded by

Claudius Maximus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Introduction to

Fortran 90-3
Other Declarations
Define constants to be used in program:
PARAMETER (PI=3.1415927, NAME=‘UDSM’)
PARAMETER (PI4=PI/2, FLAG=.TRUE.)
 these cannot be changed in assignments
 can use parameters to define other parameters
Pass a function or subroutine name as an argument:
INTRINSIC SIN – the SIN function will be passed
as an argument to a subprogram (subroutine or
function)
EXTERNAL CP260 – the CP260 function defined
in a FUNCTION subprogram will be passed as an
argument to another subprogram
CP 260 Lectures - Fortran 2
Example 8
program projectile
implicit none ! To be followed by Variable declaration
! define constants
real, parameter :: g = 9.82 real :: a, t, u, x, y, pi, g
real, parameter :: pi = 3.1415927 real :: theta, v, vx, vy
real :: a, t, u, x, y parameter (g = 9.82)
real :: theta, v, vx, vy parameter (pi = 3.1415927)
! Read values for a, t, and u from terminal
write(*,*) ‘Enter values of a, t, u’
read(*,*) a, t, u
! convert angle to radians
a = a * pi / 180.0
x = u * cos(a) * t
y = u * sin(a) * t – 0.5 * g * t * t
vx = u * cos(a)
vy = u * sin(a) - g * t
v = sqrt(vx * vx + vy * vy)
theta = atan(vy / vx) * 180.0 / pi
write(*,*) ’x: ’, x, ’ y: ’, y
write(*,*) ’v: ’, v, ’ theta: ’, theta
end program projectile CP 260 Lectures - Fortran 3
Name Intrinsic FunctionsAction
Common
ABS(A) absolute value of any A
ACOS(X) inverse cosine in the range (0,p) in radians
AIMAG(Z) imaginary part of Z
AINT(X [,KIND]) truncates fractional part towards zero, returning real
ANINT(X [,KIND]) nearest integer, returning real
ASIN(X) inverse sine in the range (-p/2,p/2) in radians
ATAN(X) inverse tangent in the range (-p/2,p/2) in radians
ATAN2(Y,X) inverse tangent of Y/X in the range (-p,p) in radians
CMPLX(X [,Y][,KIND] converts to complex X+iY; if Y is absent, 0 is used
CONJG(Z) complex conjugate of Z
COS(W) cosine of argument in radians
COSH(X) hyperbolic cosine
EXP(W) exponential function
FLOOR(X) greatest integer less than X
INT(A [,KIND]) converts to integer, truncating (real part) towards zero
KIND(A) integer function, returns the KIND of the argument
LOG(W) natural logarithm: if W is real it must be positive

CP 260 Lectures - Fortran 4


Flow Control
Assigning grades to examinees is a repetitive
action, i.e. the same criteria is used for all students

Marks Letter Grade


69.5 and above A
59.5 to 69.5 B+
49.5 to 59.5 B
39.5 to 49.5 C
34.5 to 39.5 D
0 to 34.5 E

CP 260 Lectures - Fortran 5


Flow Control
Calculation of Heat Transfer, Mass Transfer, Momentum
Transfer and Pressure Drop in fluid flow can be done
using different formulae, depending on the flow conditions.

(a)

(b)

(c) Laminar Transition Turbulent


CP 260 Lectures - Fortran 6
Flow Control
d) Steady or Unsteady Flow: Fluid flow can be
steady or unsteady, depending on the fluid’s
velocity. In steady fluid flow, the velocity of the
fluid is constant at any point. When the flow is
unsteady, the fluid’s velocity can differ between
any two points.
e) Viscous or Non-viscous Flow: Liquid flow can
be viscous or non-viscous. Viscosity is a measure
of the thickness of a fluid, and very gloppy fluids
such as motor oil or shampoo are called viscous
fluids.
CP 260 Lectures - Fortran 7
Flow Control
f) Compressible or incompressible
g) Rotational or irrotational
h) Newtonian or Non-Newtonian
h) Developing or Fully developed

CP 260 Lectures - Fortran 8


Flow Control
Fortran includes a number of constructs to
control the program flow
DO: controls the repeated execution of a
block of statements or constructs
IF: conditionally executes one statement
based on the value of a logical expression
CASE: conditionally executes one block of
constructs or statements depending on the
value of a scalar expression

CP 260 Lectures - Fortran 9


DO Construct -1
Controls the repeated execution of a block of statements or
constructs
This construct is called a loop
DO counter= begin, ending, stepsize
Statements to be executed
END DO
i is a counter
begin is the minimum value that the loop variable will take
ending is the maximum value that the loop variable will
take
stepsize is the increment for the loop variable

CP 260 Lectures - Fortran 10


DO Construct -1 Example 1(a)
Print the odd integers starting from 1 and less than
100
DO mn=1, 100, 2
WRITE (*, 50) mn
END DO
50 format(‘MN = ',i4)
MN = 1
MN = 3
…
MN= 99

CP 260 Lectures - Fortran 11


DO Construct -1 Example 1(b)
Print the odd integers starting from 1 and less than
100
50 format('I = ',i4)
DO i=1, 100, 2
WRITE (*, 50) i
END DO
I= 1
I = 3
…
I = 99

CP 260 Lectures - Fortran 12


DO Construct -1 Example 1(c)
Print the odd integers starting from 1 and less than
100
DO mn=1, 100, 2
WRITE (*, ‘(a,i4)’) ‘MN = ‘,MN
END DO
MN = 1
MN = 3
…
MN = 99

CP 260 Lectures - Fortran 13


DO Construct -1 Example 2
 Summing numbers
Program CP260
Implicit none
Integer:: i, total ! These are simple variables – with one value
Real:: num, sum ! These are simple variables – with one value
Write(*,*) ‘How many numbers to be processed (total)?’
Read(*,*) total
Sum=0.0 ! Initializing summation
do i=1, total
write (*, *)’Enter number’, i
read(*,*) num
sum=sum+num
end do
write (*, 50) num,sum
50 format (‘Number = ‘, f7.2,‘Sum of numbers = ',f7.3)
End program CP260
CP 260 Lectures - Fortran 14
DO Construct -2 (Fortran77)
Controls the repeated execution of a block of statements
or constructs
This construct is called a loop
DO label i= begin, ending, stepsize
Statements to be executed
label continue / closing statement
i, begin, ending, & stepsize are as before
label is an integer (1-1000) that shows end of the loop.
continue is an in-built (reserved) word to instruct the
program to proceed after reaching ending

CP 260 Lectures - Fortran 15


DO Construct -2 Example
Print the odd integers starting from 1 and less than 100

Alternative 1 Alternative 2
DO 35 i=1, 100, 2 DO 35 i=1, 100, 2
WRITE (*, 50) i 35 WRITE (*, 50) i
35 Continue 50 format('I = ',i4)
50 format('I = ',i4)

I = 1
I = 3
…
I = 99

CP 260 Lectures - Fortran 16


DO Construct -2 Example
Print the odd integers starting from 1 and less than
100
Alternative 1 Alternative 3
DO 35 i=1, 100, 2 DO 35 i=1, 100, 2
WRITE (*, 50) i WRITE (*, 50) i 35
35 Continue 50 format('I = ',i4)
50 format('I = ',i4)

I = 1
I = 3
…
I = 99
CP 260 Lectures - Fortran 17
DO Construct -3
Implied DO Loop
The implied DO loop is a shorthand notation
useful for the following:
specify iteration of part of an I/O list
initialize an array
transfer part of an array
transfer array items in a sequence different from
the order of subscript progression
Cuts back on the number of lines of code required

CP 260 Lectures - Fortran 18


Implied DO Loop
In a data transfer statement, an implied-DO list
acts as though it were a part of an I/O statement
within a DO loop. It takes the following form:
(list, do-var = expr1, expr2 [,expr3])
list
 a list of variables, expressions, or constants
do-var
 the name of a scalar integer or real variable
 the variable must not be one of the input items in list
expr
 scalar numeric expressions of type integer or real. They do not all
have to be the same type, or the same type as the DO variable.

CP 260 Lectures - Fortran 19


Implied DO Loop
print the odd integers starting from 1 and less
than 100
WRITE (*, 100) (k, k=1, 100,
2)
100 Format(‘K = ’, I4)
> K= 1
> K= 3
> …
> K = 99
the implied DO can be nested
WRITE (6,150) ((FORM(m,n), n=1,10),
m=1,10,2)

CP 260 Lectures - Fortran 20


Implied DO Loop
 Summing five numbers
Program CP230
Implicit none
Integer:: i, total ! These are simple variables – with one value
Real:: num(50) ! This is an array/matrix variable – with many 50 values
Write(*,*) ‘How many numbers to be processed (total)?’
Read(*,*) total
Sum=0.0 ! Initializing summation
do i=1, total
write (*, *)’Enter number’, i
read(*,*) num(i) Normal Do loop
sum=sum+num(i)
end do
write (*, 50) (num(i), i=1,total) Implied Do loop
write (*, 35) sum
50 format (‘Number = ‘,f7.3)
35 format (‘-------------------------------’,/‘Sum of Numbers = ',f7.2)
End program CP230

CP 260 Lectures - Fortran 21


DO WHILE Construct
execute a range of a DO statements while a
specified condition remains true
i = 99
DO WHILE (i>0)
WRITE (*, "(a,I4)") 'I = ',i
i = i - 2
END DO

CP 260 Lectures - Fortran 22


DO While and Implied DO Construct
 Summing numbers
Program Trail
Implicit none
Integer:: x, tot
Real:: num, sum
Write(*,*) ‘Enter total numbers to be processed, tot?’
Read(*,*) tot
Sum=0.0 ! Initializing summation
x=tot ! Initializing counter
Do while (x>0)
write (*, *)’Enter number’, x
read(*,*) num(x)
sum=sum+num(x)
i=i-1
end do Implied Do loop
write (*, 50) (num(x), x=1,tot)
write (*, 35) sum
50 format (‘Number = ‘,f7.3)
35 format (‘-------------------------------’,/‘Sum of Numbers = ',f7.2)
End program Trail
CP 260 Lectures - Fortran 23
Nested DO loops
Nested Do loops are used to create arrays
(Matrices/ Tables)
program matris2
• The program make use of Implicit none
Integer:: i,j,m(10,10),n(10),k(10)
array type of variables
Do i=1,3
• There is no loop overlap print*,’Enter a number’
(Inner do – loop must be Read(*,*) k(i)
completed before the Do j=1,5
outer loop – check the print*,’Enter a number’
label colours) Read(*,*) n(j)
m(i,j)=k(i)+2*n(j)
End do
End do
Implied Do loop write(*,*)((m(i,j),j=1,5),i=1,3)
End program matris2
CP 260 Lectures - Fortran 24
Nested DO loops
Nested Do loops are used to create arrays
(Matrices/ Tables)
program matris2
• The program make use of Implicit none
Integer:: i,j,m(10,10),n(10),k(10)
array type of variables
Do 30 i=1,3
• There is no loop overlap print*,’Enter a number’
(Inner do – loop must be Read(*,*) k(i)
completed before the do 60 j=1,5
outer loop – check the print*,’Enter a number’
label colours) Read(*,*) n(j)
m(i,j)=k(i)+2*n(j)
60 End do
30 End do
Implied Do loop write(*,*)((m(i,j),j=1,5),i=1,3)
End program matris2
CP 260 Lectures - Fortran 25
Nested DO loops
Nested Do loops are used to create arrays
(Matrices/ Tables)
program matris2
Implicit none
• The program make use of
Integer:: i,j,m(10,10),n(10),k(10)
array type of variables Do 30 i=1,3
• There is loop overlap print*,’Enter a number’
(outer do – loop is ending Read(*,*) k(i)
within the Inner loop – do 60 j=1,5
print*,’Enter a number’
check the label colours)
Read(*,*) n(j)
m(i,j)=k(i)+2*n(j)
30 End do
60 End do
Implied Do loop write(*,*)((m(i,j),j=1,5),i=1,3)
End program matris2
CP 260 Lectures - Fortran 26

You might also like