CSCE150 Fortran Lab12
Document Sample


CSCE150
Fortran Lab12
1
Outline
• Modules
example 1
• Derived Data Types
example 2
• exercise 1
• exercise 2
2
Modules
• Fortran 90 has a new type of
subprogram called modules
• to provide the programmers with a way
of packing commonly used functions or
shared data into a single unit
• A module must be saved in a separate
.f90 file.
Syntax of a module
MODULE module_name
IMPLICIT NONE
[Declaration of data ]
CONTAINS
[Declaration of subroutines or functions]
END MODULE module_name
Use modules in other program
units
• To use a particular module in a
program unit, a USE statement must
be included at the very beginning.
• For example:
PROGRAM main_program
USE my_module
IMPLICIT NONE
..........
END PROGRAM
example 1
MODULE DegreeRadianConversion
IMPLICIT NONE
REAL, PARAMETER :: PI = 3.1415926
example1_module.f90
CONTAINS
REAL FUNCTION DegreeToRadian(Degree)
IMPLICIT NONE
REAL, INTENT(IN) :: Degree
DegreeToRadian = Degree*PI/180
END FUNCTION DegreeToRadian
END MODULE DegreeRadianConversion
example1.f90
PROGRAM lab12_example1
USE DegreeRadianConversion
IMPLICIT NONE
REAL:: degree, radian
WRITE (*,*) ‘ Please enter the degree’
READ (*,*) degree
radian = DegreeToRadian(degree)
WRITE(*,*) ‘The converted radian is‘, radian
END PROGRAM
compile
f90 example1_module.f90 example1.f90 –o example1
• Attention:
The name of the module file is listed before
the main program file.
Derived Data Types
• Built-in (intrinsic) data types:
INTEGER
REAL
LOGICAL
CHARACTER
• User-defined (derived) data type
is a combination of intrinsic data types
and previously defined derived data
types
Define a derived data type
TYPE :: type_name
INTEGER :: component_1
CHARACTER :: component_2
… ( other component definitions)
END TYPE type_name
Declare variables/arrays of a
derived data type
TYPE (type_name) :: var1, var2
OR
TYPE (type_name), DIMENTION(10) :: array1
Initialize variables of a derived
data type
var1 = type_name (values of components)
Access components of a derived
data type
Variable_name%component
e.g. var1%component_1
var2%component_1
example 2
• None of intrinsic unary and binary
operators is defined for derived data
types.
• Fortran permits defining new unary
and binary operators for both
intrinsic and derived data types.
Define +, - for a derived data
type
MODULE types
IMPLICIT NONE example2_module.f90
TYPE :: my_type
INTEGER :: x1, x2
END TYPE my_type
! Declare interface operators
INTERFACE OPERATOR (+)
MODULE PROCEDURE addition
END INTERFACE
INTERFACE OPERATOR (-)
MODULE PROCEDURE subtraction
END INTERFACE
! Now define the implementing functions
CONTAINS
TYPE (my_type) FUNCTION addition (num1, num2)
IMPLICIT NONE
TYPE (my_type), INTENT(IN) :: num1, num2
! Calculate the addition
addition%x1 = num1%x1+num2%x1
addition%x2 = num1%x2+num2%x2
END FUNCTION addition
TYPE (my_type) FUNCTION subtraction (num1, num2)
IMPLICIT NONE
TYPE (my_type), INTENT(IN) :: num1, num2
! Calculate the subtration
subtraction%x1 = num1%x1-num2%x1
subtraction%x2 = num1%x2-num2%x2
END FUNCTION subtraction
END MODULE types
PROGRAM lab12_example2
USE types example2.f90
IMPLICIT NONE
TYPE (my_type) :: number1, number2, sum, sub
! Get the first my_type variable from keyboard
WRITE (*,*) 'Enter x1, x2 for the first my_type variable'
READ (*,*) number1%x1, number1%x2
! Get the second my_type variable from keyboard
WRITE (*,*) 'Enter x1, x2 for the second my_type variable'
READ (*,*) number2%x1, number2%x2
! test + (addition) operator here
sum = number1 + number2
! Output result of addition
WRITE (*,102) number1, ‘+', number2, ' =', sum
102 FORMAT ('[',I4,I4,']',A,'[',I4,I4,']',A,'[',I4,I4,']')
! test - (subtraction) operator here
sub = number1 - number2
! Output result of subtraction
WRITE (*,102) number1, ‘-', number2, ' =', sub
END PROGRAM
exercise 1
• Write a Fortran program to convert
radian to degree using module
• The formula is
degree = radian * 180 / 3.1415
• Refer to example 1
exercise 2
• Write a Fortran program to define *,
/ for a derived data type
• Refer to example 2
Related docs
Get documents about "