Essential Lahey Fortran 90

Document Sample
Essential Lahey Fortran 90 Powered By Docstoc
					           Getting Started With
        Essential Lahey Fortran 90
 ECST Computer Link
 College of Engineering, Computer Science, and Technology
 California State University, Los Angeles
                                                                                   Update: 9/12/2012
                                                                                   Author: OSO


INTRODUCTION

Essential Lahey Fortran 90 is a Programming Language just like C/C++. It is
essentially a Compiler that interprets sequences of Fortran language statements and has
them executed by the computer. After successful execution, results are output on the
screen. Fortran is DOS based and may not come with any text editor for writing the
source codes. But this can easily be overcome since all operating systems have some kind
of text editor. The procedure for opening a text editor in MS Windows will be explained
shortly.
Because of it’s DOS based, Fortran does not easily plot graphs like Matlab. So it is
recommended to export output data to other applications (e.g. excel) for graphical
manipulation.

GETTING STARTED WITH FORTRAN 90

From Start on MS Windows OS, select Essential Lahey Fortran 90. The window of
Figure 1 below appears




                               Figure 1: Fortran Start-up Window
As the figure shows, Fortran 90 start-up window does nothing but point to the default
directory of the system. Fortran 90 does not open any special text editor for you to start
inserting your program codes. The Start -up window’s main purpose is to compile your
source code once it’s been written. But it provides three useful tips to the novice to get
started That is:
        - Type elf90 to see all options &switches
        - Usage: elf90 filenames [switches]
       - To quit, type exit

NOTE: It is recommended that you work on two separate windows:
- An MS-DOS window for editing your source codes (programs)
- The Essential Lahey Fortran 90 Start-up window just for compiling, running, and
executing your programs.

OPENING A TEXT EDITOR

- Select Start/Command prompt .
- type edit at the MS-DOS prompt to bring up an MS-DOS window. See procedure on
Figure 2.
If done successfully, a DOS - based text editor appears. Insert your program codes and
saved the file to the appropriate directory. See saving procedure below.




                              Figure 2: Opening a text editor in DOS

SAVING A FORTRAN 90 SOURCE CODE FILE

A Fortran 90 file is saved with extension “.f90”. Example: my_first_program.f90

COMPILING A SOURCE CODE

From the Fortran window prompt:
- Locate the source file you want to compile. Use dos commands to do so as follows:
       - dir to list the files in the current (default) directory.
       - Type the drive name where your source file was saved, e. g. a:\..
       - use cd command to move to the directory where you saved the source code.
       Example: if your source code was saved on a:\ drive under the directory name
       fortran_directory, type cd fortran_directory. Type dir to list files under
       fortran_directory and make sure my_first_program.f90 source file is there
- compile the source code. For instance, to compile my_first_program.f90, type:
elf90 my_first_program.f90




                                                                                         2
RUNNING THE EXECUTABLE FILE
IF my_first_program.f90 compiles correctly, Fortran will create a few other files. Among
them, an executable named my_first_program.exe. That is the file you will actually run.
To do so, type my_first_program. You may exclude the .exe extension. Follow the
procedures as prompted by the program.

DATA TYPES NOTATION

Type                          Notation                      Example
Integer                       INTEGER                       INTEGER :: a
Real                          REAL                          REAL :: a
Character                     CHARACTER                     CHARACTER :: s
Complex Assignment            COMPLEX                       COMPLEX :: b
Complex output                (real_a, imaginary_b)         (a,b)


ARITHMETIC OPERATORS

        Operator                       Notation                      Example
        Addition                          +                             a+b
       Subtraction                        -                             a-b
      Multiplication                      *                              a*b
        Division                          /                              a/b
      Exponentiation                     **                    a**b (a to power of b)


RELATIONAL LOGIC OPERATORS

        Operator                       Notation                       Example
         Equal to                        ==                            a= =b
       Not equal to                       /=                            a/=b
       Greater than                        >                            a>b
   Greater than or equal                 >=                            a>=b
        Less than                          <                            a<b
    Less than or equal                   <=                            a<=b


COMBINATIONAL LOGIC OPERATORS

   Operator Function                   Notation                      Example
      Logical AND                       .AND.                        a .AND. b
       Logical OR                        .OR.                         a .OR. b
   Logical Equivalence                  .EQV.                        a .EQV. b
 Logical Non-equivalence               .NEQV.                       a .NEQV. b
      Logical NOT                       .NOT.                         .NOT. a


                                                                                        3
SOME COMMON FORTRAN IN-BEDDED FUNCTIONS

Function                      Notation & Argument           Comment
Square Root                   SQRT(x)                       Square root of x
Absolute Value                ABS(x)                        Absolute value of x
Sine Value                    SIN(x)                        Sine of x
Cosine Value                  COS(x)                        Cosine of x
Tangent Value                 TAN(x)                        Tangent of x
Arc Sine Value                ASIN(x)                       Inverse sine of x
Arc Cosine Value              ACOS(x)                       Inverse cosine of x
Arc tangent                   ATAN(x)                       Inverse Tangent of x
Exponential ex                EXP(x)                        e to the x power
Base e logarithm              LOG(x)                        Natural log of x
Base 10 logarithm             LOG10(x)                      Base 10 log of x
Modulo                        MOD(a,b)                      Remainder of a/b
Maximum                       MAX(a,b)                      Returns max of a, b
Minimum                       MIN(a,b)                      Returns min of a, b


BASIC FORTRAN PROGRAMMING STRUCTURE

The basic Fortran Programming structure will be illustrated with a simple example.

EXAMPLE 1:
Write a program that prompts the user to enter two integers to standard input
(keyboard). The program then multiplies the two integers and displays the result to
standard output (screen).

                                 Program source code:
PROGRAM mul
IMPLICIT NONE
INTEGER :: i, j, k
        WRITE (*,*) 'This program multiplies two integers'
        WRITE (*,*) 'Enter the integers: '
        READ (*,*) i, j

         k = i*j         ! Take the product of the two numbers

        WRITE (*, *) 'Result =',k          ! Output result to screen
STOP
END PROGRAM mul

The source code starts with PROGRAM mul and ends with STOP then END
PROGRAM mul. mul is the name of the program. All Fortran programs follow this
basic structure. IMPLICIT NONE is used to turn off default typing in Fortran. Must be
used to prevent errors in a program where variables types must explicitly be declared.
Note also that comments are preceded by “!”



                                                                                     4
COMPILING MUL.F90 SOURCE CODE

The procedure below compiles mul.f90 source code:

Z:\FORTRAN>elf90 mul.f90

The line above indicates that mul.f90 was saved in z:\ drive under a directory created by
the user, and named FORTRAN.
elf90 mul.f90 is the command that compiles the source code.
Note that Z:\FORTRAN directory was set in the Fortran window before compiling the
program. See section “Compiling a Source Code” for details.

RUNNING A PROGRAM

The procedure below runs mul.f90 source code if it was compiled successfully:

Z:\FORTRAN> mul

PROGRAM OUTPUT

This program multiplies two integers
Enter the integers: -57 13

Result =            -741

Program completed
Press Enter to Continue.


MORE PROGRAMMING EXAMPLES

EXAMPLE 2: SQRT COMPUTATION

Compute the square root of the product of two real numbers. If one of the input numbers
is negative, the program must announce that the result will be a complex number.

The program uses IF statement as well as logical .AND., .OR. statements. If one of the
input numbers is negative, the program announces that the result will be a complex
number.
NOTE: Fortran does not compute the square root of a negative number to obtain a
complex number. For this reason, the program switches to a slightly different
computation method (i.e. multiplying the arguments of SQRT by -1)
See program code on next page




                                                                                            5
PROGRAM ff

! Purpose: Compute the square root of the product of two real numbers

IMPLICIT NONE
REAL :: x, y
REAL :: z          ! Square Root of product(x,y)

           WRITE (*,*) 'Please enter the two real values: '
           READ (*,*) x, y

           IF (((x < 0) .AND. (y>0)).OR. ((y<0) .AND. (x>0))) THEN
           WRITE (*,*) 'The product is negative ==> Complex Answer'
           z = SQRT(-x*y)
           WRITE (*,*) 'Result =',z,'j'
           END IF
                                   ! Multiply the two integers and
           z = SQRT(x*y)           ! take the square root of the product

        WRITE (*, *) 'Result =',z         ! Output result to screenWS
STOP
END PROGRAM ff

EXAMPLES OF SQRT RESULTS OUTPUT

a)
Please enter the two real values: 12.33 -4.7

The product is negative ==> Complex Answer
Result =    7.61256    j

SQRT argument negative (see "SQRT Function" in the Essential Lahey
Fortran 90
 Reference).
 Error occurred at line 18 of file ff.f90.
Program completed

Note the error message in the result although the argument of SQRT() was made positive.

b)
Please enter the two real values: -8.67 -13

Result =       10.6165

Program completed
Press Enter to Continue.

c)
Please enter the two real values: 98 33.49

Result =       57.2889

Program completed
Press Enter to Continue.




                                                                                      6
EXAMPLE 3: SINGLE PHASE POWER COMPUTATION

Compute the current, real, reactive, apparent power, and the power factor supplied to the
load of the single phase circuit below.

               I


+
       VsS                                Z = R+jX (ZZ)
-



Source Code:

PROGRAM power

! Purpose: Compute the current, real, reactive, apparent power, and
!          the power factor of supplied to a load.

IMPLICIT NONE

! Declare input parameters

REAL :: Vsource, Theta_source, Theta_sc   ! Voltage and angle of source
COMPLEX :: Z_load   ! To be input in the form (R,X) --> R+jX

! Declare Output values

REAL :: I_current, Z, PF, Preal, Preactive, Papparent
REAL :: Theta, Theta_z, Theta_i

         ! Initialize input parameters

         WRITE (*,*)    'Please input the source voltage '
         WRITE (*,*)    'and angle (in degrees) respectively.'
         WRITE (*,*)    '(Example: 250 0) '
         READ (*,*)     Vsource, Theta_source

         WRITE (*, *) 'Input complex components of load Z of the form'
         WRITE (*,*) 'Z = R+jX (input example: 1200, 75) '
         READ (*,*) Z_load

         ! Compute output results

         Theta_sc = Theta_source*3.141593/180                   !Degrees to radians
         Theta_z = ATAN(AIMAG(Z_load)/REAL(Z_load))             ! Angle due to load
         Theta_i = Theta_sc - Theta_z                           ! Current angle
         Theta   = Theta_sc - Theta_i                           ! p.f. angle
         PF = COS(Theta)                                        ! Power Factor
         Z = ABS(Z_load)                                        ! Load Magnitude
         I_current = Vsource/Z                                  ! Current Magnitude



                                                                                       7
        Preal = Vsource*I_current*PF                     ! Real Power
        Preactive = Vsource*I_current*SIN(Theta)         ! Reactive Power
        Papparent = Vsource*I_current                    ! Apparent Power

        ! Output computed results

        WRITE   (*,*)   'Load Current: ', I_current,'Amps'
        WRITE   (*,*)   'Real Power: ', Preal,'Watts'
        WRITE   (*,*)   'Reactive Power: ', Preactive,'Vars'
        WRITE   (*,*)   'Apparent Power: ', Papparent,'VA'
        WRITE   (*,*)   'Power factor: ', PF
        WRITE   (*,*)   'At an angle of: ', Theta*180/3.141593,'degrees'

        STOP
END PROGRAM power

REMARK: Note the computation of Theta_z in the program. AIMAG(Z_load) is the
imaginary part of Z_load REAL(Z_load), the real part.


POWER COMPUTATION OUTPUT EXAMPLE

Z:\FORTRAN>power

Please input the source voltage
and angle (in degrees) respectively.
(Example: 250 0) 450 30

Input complex components of load Z of the form
 Z = R+jX (input example: 1200, 75) 2100, 173

Load Current:    0.213562 Amps
Real Power:     95.7786 Watts
Reactive Power:     7.89033 Vars
Apparent Power:     96.1030 VA
Power factor:    0.996624
At an angle of:     4.70945 degrees

Program completed
Press Enter to Continue.




                                                                               8
EXAMPLE 4: DELTA-TO-Y Y-TO-DELTA IMPEDANCES CONVERSION

                                                              Zab
a                                   b           a                            b


      Za                    Zb

                                                    Zca              Zbc


                    Zc
                                                               c

               c
                   -Y                                        Y-

Source code:

PROGRAM convert

IMPLICIT NONE

 REAL :: Zab, Zbc, Zca                  ! Impedances in a Delta Connection
 REAL :: Za, Zb, Zc                     ! Inpedance in a Y Connection
 CHARACTER (len=1) :: num                         ! Selection identifier

           WRITE (*,*) 'Convert Three-phase impedances:'
           WRITE (*,*) 'Delta to Y equivalent impedances and vice versa.'
           WRITE (*,*) ' '
 DO
           WRITE (*,*) 'Press 1 to convert from Delta to Y'
           WRITE (*,*) 'Press 2 to convert from Y to Delta: '
           WRITE (*,*) 'Press ANY OTHER KEY to quit.'
           WRITE (*,*) 'Choice: '
           READ (*,*) num

            SELECT CASE (num)

           CASE ('1')
           WRITE (*,*) 'Enter the values of Zab, Zbc, Zca: '
           READ (*,*) Zab, Zbc, Zca

           Za = (Zab*Zca)/(Zab+Zbc+Zca)
           Zb = (Zbc*Zab)/(Zab+Zbc+Zca)
           Zc= (Zbc*Zca)/(Zab+Zbc+Zca)

           WRITE    (*,*)   'Y-connection equivalent impedances:'
           WRITE    (*,*)   'Za =',Za
           WRITE    (*,*)   'Zb =',Zb
           WRITE    (*,*)   'Zc =',Zc
           WRITE    (*,*)   ' '



                                                                                 9
        CASE ('2')

        WRITE (*,*) 'Enter the values of Za, Zb, Zc: '
        READ (*,*) Za, Zb, Zc

        Zab =   (Za*Zb + Zb*Zc + Zc*Za)/Zc
        Zbc =   (Za*Zb + Zb*Zc + Zc*Za)/Za
        Zbc =   (Za*Zb + Zb*Zc + Zc*Za)/Za
        Zca =   (Za*Zb + Zb*Zc + Zc*Za)/Zb
        WRITE   (*,*) 'Delta-connection equivalent impedances:'
        WRITE   (*,*) 'Zab =',Zab
        WRITE   (*,*) 'Zbc =',Zbc
        WRITE   (*,*) 'Zca =',Zca
        WRITE   (*,*) ' '

        CASE DEFAULT
        WRITE (*,*) 'You have chosen to quit. Good bye...'
        EXIT
        END SELECT
END DO
STOP
END PROGRAM     convert

A CONVERSION COMPUTATION EXAMPLE
Z:\FORTRAN>convert

Convert Three-phase impedances:
Delta to Y equivalent impedances and vice versa.

Press 1 to convert from Delta to Y
Press 2 to convert from Y to Delta:
Press ANY OTHER KEY to quit.
Choice: 1

Enter the values of Zab, Zbc, Zca: 200 350 80

Y-connection equivalent impedances:
Za =    25.3968    ohms
Zb =    111.111    ohms
Zc =    44.4444    ohms

Press 1 to convert from Delta to Y
Press 2 to convert from Y to Delta:
Press ANY OTHER KEY to quit.
Choice: 2

Enter the values of Za, Zb, Zc: 250 250 250

Delta-connection equivalent impedances:
Zab =    750.000    ohms
Zbc =    750.000    ohms
Zca =    750.000    ohms




                                                                  10
Press 1 to convert from Delta to Y
Press 2 to convert from Y to Delta:
Press ANY OTHER KEY to quit.
Choice: k

You have chosen to quit. Good bye...

Program completed
Press Enter to Continue.




                                       11
                                              TABLE OF CONTENTS

INTRODUCTION .............................................................................................................. 1
GETTING STARTED WITH FORTRAN 90 .................................................................... 1
OPENING A TEXT EDITOR ............................................................................................ 2
SAVING A FORTRAN 90 SOURCE CODE FILE ........................................................... 2
COMPILING A SOURCE CODE ...................................................................................... 2
RUNNING THE EXECUTABLE FILE ............................................................................. 3
DATA TYPES NOTATION............................................................................................... 3
ARITHMETIC OPERATORS ........................................................................................... 3
RELATIONAL LOGIC OPERATORS .............................................................................. 3
COMBINATIONAL LOGIC OPERATORS ..................................................................... 3
SOME COMMON FORTRAN IN-BEDDED FUNCTIONS ............................................ 4
BASIC FORTRAN PROGRAMMING STRUCTURE ..................................................... 4
EXAMPLE 1: ..................................................................................................................... 4
COMPILING mul.f90 source code ..................................................................................... 5
RUNNING A PROGRAM ................................................................................................. 5
EXAMPLE 2: SQRT COMPUTATION ............................................................................ 5
EXAMPLES OF SQRT RESULTS OUTPUT ................................................................... 6
EXAMPLE 3: SINGLE PHASE POWER COMPUTATION............................................ 7
POWER COMPUTATION OUTPUT EXAMPLE ............................................................ 8
EXAMPLE 4: DELTA-TO-Y Y-TO-DELTA IMPEDANCES CONVERSION .............. 9
A CONVERSION COMPUTATION EXAMPLE ........................................................... 10


                                                TABLE OF FIGURES

Figure 1: Fortran Start-up Window..................................................................................... 1
Figure 2: Opening a text editor in DOS .............................................................................. 2




                                                                                                                              12

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:10
posted:9/12/2012
language:English
pages:12