CSCE150 Fortran Lab12 - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

CSCE150 Fortran Lab12

Description:

To use a particular module in a program unit, a USE statement must be included ... The name of the module file is listed before the main program file. Derived ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 20
Provided by: cse92
Category:

less

Transcript and Presenter's Notes

Title: CSCE150 Fortran Lab12


1
CSCE150 Fortran Lab12
2
Outline
  • Modules
  • example 1
  • Derived Data Types
  • example 2
  • exercise 1
  • exercise 2

3
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.

4
Syntax of a module
  • MODULE module_name
  • IMPLICIT NONE
  • Declaration of data
  • CONTAINS
  • Declaration of subroutines or functions
  • END MODULE module_name

5
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

6
example 1
  • MODULE DegreeRadianConversion
  • IMPLICIT NONE
  • REAL, PARAMETER PI 3.1415926
  • CONTAINS
  • REAL FUNCTION DegreeToRadian(Degree)
  • IMPLICIT NONE
  • REAL, INTENT(IN) Degree
  • DegreeToRadian DegreePI/180
  • END FUNCTION DegreeToRadian
  • END MODULE DegreeRadianConversion

example1_module.f90
7
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

8
compile
  • f90 example1_module.f90 example1.f90 o example1
  • Attention
  • The name of the module file is listed before the
    main program file.

9
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

10
Define a derived data type
  • TYPE type_name
  • INTEGER component_1
  • CHARACTER component_2
  • ( other component definitions)
  • END TYPE type_name

11
Declare variables/arrays of a derived data type
  • TYPE (type_name) var1, var2
  • OR
  • TYPE (type_name), DIMENTION(10) array1

12
Initialize variables of a derived data type
  • var1 type_name (values of components)

Access components of a derived data type
Variable_namecomponent e.g. var1component_1
var2component_1
13
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.

14
Define , - for a derived data type
  • MODULE types
  • IMPLICIT NONE
  • 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

example2_module.f90
15
  • ! 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
  • additionx1 num1x1num2x1
  • additionx2 num1x2num2x2
  • END FUNCTION addition
  • TYPE (my_type) FUNCTION subtraction (num1,
    num2)
  • IMPLICIT NONE
  • TYPE (my_type), INTENT(IN) num1, num2
  • ! Calculate the subtration
  • subtractionx1 num1x1-num2x1
  • subtractionx2 num1x2-num2x2
  • END FUNCTION subtraction
  • END MODULE types

16
  • PROGRAM lab12_example2
  • USE types
  • 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 (,) number1x1, number1x2
  • ! Get the second my_type variable from keyboard
  • WRITE (,) 'Enter x1, x2 for the second my_type
    variable'
  • READ (,) number2x1, number2x2
  • ! 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,'')

example2.f90
17
(No Transcript)
18
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

19
exercise 2
  • Write a Fortran program to define , / for a
    derived data type
  • Refer to example 2
Write a Comment
User Comments (0)
About PowerShow.com