You are on page 1of 4
ansr20%8 Foevan 90 Tuoi 9. Modules In typical engineering programming applications, it often the case that there are parameters, variables, and subprograms that must be shared by several program units. Fortran 90 provides a special program unit known as a MODULE that conveniently packages collections of declarations and subprograms so that they may be imported into other program units. The functionality of the module unit is similar to that of the C header file. Simple modules A form of a module is as follows MODULE name specifications END MODULE name Note that this is the simplest form of the module unit, More general forms exist which allow the inclusion of subprograms and procedures, We will only address the simplest form of the module unit here. A program module is made accessible to the various program units by way of the USE statement. Consider the following program which makes use of a module in the computation of the area of a circle: MODULE Circle | Module containing definitions of variables needed to 1 compute the area of a circle of radius r REAL, PARAMETER : 3.1415927 REAL :: radius END MODULE Circle PROGRAM Area ‘This program computes the area of a circle given the input radius | Uses: MODULE Circle FUNCTION Area_Circle (r) USE Circle, ONLY IMPLICIT NONE radius INTERFACE FUNCTION Area_Circle (r) REAL, INTENT(IN) :: END FUNCTION Area_Circle END INTERFACE 1 Prompt user for radius of circle wite(*, "(A)", ADVANCE = "NO") “Enter the radius of the circle: read(*,*) radius | Write out area of circle using function call write(*,100) “Area of circle with radius", radius, " is", & ‘Area_Circle(radius) 100 format (A, 2x, F6.2, A, 2x, F11.2) hps:web stanford edulclastine200ctutoral_90/09_modules. html us 311612018 Forvan 90 Tutorial END PROGRAM Area ~Area_Circle- 1 1 Function to compute the area of a circle of given radius 1 FUNCTION Area_Circle(r) USE Circle, ONLY : Pi IMPLICIT NONE REAL :: Area Circle REAL, INTENT(IN) Area_Circle = Pit ir tr END FUNCTION Area_Circle In this example, we define a module which declares the type of the two real variables Pi and radius. In addition, in this module, we declare the value of the parameter Pi, To make use of the module in the main program, we employ the USE statement, We use a special form of the USE statement that specifies that we are only interested in the value radius in the main program, namely, USE Circle, ONLY : radius, Similarly, we make use of only the parameter Pi in the function subprogram Area_Cirele by way of the USE statement appearing in that routine. ‘The USE statement *** MUST *** appear at the beginning of the declaration part of the program unit making use of the module!! It must appear EVEN BEFORE the statement IMPLICIT NONE! Modules for dynamic array allocation here are times when you may wish to declare an array to be allocatable, However, you may wish to allocate its memory storage in a subprogram rather than in the main program, Unfortunately, allocatable arrays are not allowed to be passed as formal arguments to subprograms. The module construct provides a way for you to pass dynamically allocatable arrays to several program units. Consider the following example: MODULE Dyn_Array 1 Module containing definitions needed to dynamically allocate 1 the values of an array INTEGER :: 0 REAL, DIMENSION(:), ALLOCATABLE :: A END MODULE Dyn_Array PROGRAM Play with Array This program calls a subroutine to read in the values of 2 dynamically allocated array A and calls several 10 intrinsic array functions | Uses: MODULE Dyn_Array 1 ‘SUBROUTINE Get_Data ! SUBROUTINE Dealloc_Array USE Dyn_Array IMPLICIT NONE INTERFACE hps:web stanford edulclastine200ctutoral_90/09_modules. html 216 311612018 Forvan 90 Tutorial SUBROUTINE Get_Data END SUBROUTINE Get_Data END INTERFACE INTERFACE SUBROUTINE Dealloc_Array END SUBROUTINE Dealloc Array END INTERFACE | Declare local variables REAL :: Prod_A, Sum_A ! Call subroutine to read in data for array A CALL Get_Data | Use intrinsic functions to generate data Prod_A = PRODUCT(A) | Write out product of elements of array A write(*,16) "The product of the elements of array A area", & Prod_A | Use intrinsic function to generate more data Sum_A = SUN(A) I write out st weite(*,108) of elements of array A he sun of the elements of array A are”, & Suna 1 Now, deallocate memory containing array A CALL Dealloc_Array | Place for format statement to live 100 format (A, 2x, F11.2) END PROGRAM Play with Array ~Get_Data~ Subroutine to read in the number of values to fill A array, allocate space, and to assign the values to A 1 1 SUBROUTINE Get_Data USE Dyn_Array Uses: MODULE Dyn_Array IMPLICIT NONE 1 Declare local variables INTEGER :: Allocatestatus 1 Read in number of array elements from user write(*,"(A)', ADVANCE = "NO") "Input the number of elenents desired: read(*,*) n | Allocate storage for array A accordingly ALLOCATE( A(n), STAT = Allocatestatus) IF (Allocatestatus /= @) STOP "*** Not enough memory **** 1 Read in values for A weite(*, "(A)", ADVANCE read(*,*) A 0") "Input array values: hps:web stanford edulclastine200ctutoral_90/09_modules. html 38 311612018 Forvan 90 Tutorial END SUBROUTINE Get_Data ~Dealloc_Array- 1 1 Subroutine to deallocate array space 1 Uses: MODULE Dyn_Array SUBROUTINE Dealloc_Array USE Dyn_Array IMPLICIT NONE | Declare local variables INTEGER :: DeAllocatestatus | Deallocate storage for array A DEALLOCATE( A, STAT = DeALlocateStatus) IF (DeAllocatestatus /= ) STOP **** Trouble deallocating ***" END SUBROUTINE Dealloc_Array Here we make use of the module Dyn_Array to declare the array A an allocatable real array. The integer n will be the size of the array when its value is input by the user in subroutine Get_Data. Note how the subroutines, used to allocate and deallocate memory use no formal arguments. This is because all of the arguments needed are passed via the module Dyn_Array. Copyright © 1996-7 by Stanford University. All rights reserved. hps:web stanford edulclastine200ctutoral_90/09_modules. html 418

You might also like