Getting Started with Essential Lahey Fortran 90

advertisement
Getting Started With
Essential Lahey Fortran 90
ECST Computer Link
College of Engineering, Computer Science, and Technology
California State University, Los Angeles
Update: 3/15/2002
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
Integer
Real
Character
Complex Assignment
Complex output
Notation
INTEGER
REAL
CHARACTER
COMPLEX
(real_a, imaginary_b)
Example
INTEGER :: a
REAL :: a
CHARACTER :: s
COMPLEX :: b
(a,b)
ARITHMETIC OPERATORS
Operator
Addition
Subtraction
Multiplication
Division
Exponentiation
Notation
+
*
/
**
Example
a+b
a-b
a*b
a/b
a**b (a to power of b)
RELATIONAL LOGIC OPERATORS
Operator
Equal to
Not equal to
Greater than
Greater than or equal
Less than
Less than or equal
Notation
==
/=
>
>=
<
<=
Example
a= =b
a/=b
a>b
a>=b
a<b
a<=b
COMBINATIONAL LOGIC OPERATORS
Operator Function
Logical AND
Logical OR
Logical Equivalence
Logical Non-equivalence
Logical NOT
Notation
.AND.
.OR.
.EQV.
.NEQV.
.NOT.
Example
a .AND. b
a .OR. b
a .EQV. b
a .NEQV. b
.NOT. a
3
SOME COMMON FORTRAN IN-BEDDED FUNCTIONS
Function
Square Root
Absolute Value
Sine Value
Cosine Value
Tangent Value
Arc Sine Value
Arc Cosine Value
Arc tangent
Exponential ex
Base e logarithm
Base 10 logarithm
Modulo
Maximum
Minimum
Notation & Argument
SQRT(x)
ABS(x)
SIN(x)
COS(x)
TAN(x)
ASIN(x)
ACOS(x)
ATAN(x)
EXP(x)
LOG(x)
LOG10(x)
MOD(a,b)
MAX(a,b)
MIN(a,b)
Comment
Square root of x
Absolute value of x
Sine of x
Cosine of x
Tangent of x
Inverse sine of x
Inverse cosine of x
Inverse Tangent of x
e to the x power
Natural log of x
Base 10 log of x
Remainder of a/b
Returns max of 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
STOP
END PROGRAM mul
! Output result to screen
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
STOP
END PROGRAM ff
! Output result to screenWS
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 (*,*)
WRITE (*,*)
WRITE (*,*)
READ (*,*)
'Please input the source voltage '
'and angle (in degrees) respectively.'
'(Example: 250 0) '
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
Theta_z = ATAN(AIMAG(Z_load)/REAL(Z_load))
Theta_i = Theta_sc - Theta_z
Theta
= Theta_sc - Theta_i
PF = COS(Theta)
Z = ABS(Z_load)
I_current = Vsource/Z
!Degrees to radians
! Angle due to load
! Current angle
! p.f. angle
! Power Factor
! Load Magnitude
! Current Magnitude
7
Preal = Vsource*I_current*PF
Preactive = Vsource*I_current*SIN(Theta)
Papparent = Vsource*I_current
! Real Power
! Reactive Power
! Apparent Power
! Output computed results
WRITE
WRITE
WRITE
WRITE
WRITE
WRITE
(*,*)
(*,*)
(*,*)
(*,*)
(*,*)
(*,*)
'Load Current: ', I_current,'Amps'
'Real Power: ', Preal,'Watts'
'Reactive Power: ', Preactive,'Vars'
'Apparent Power: ', Papparent,'VA'
'Power factor: ', PF
'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
Za
a
b
Zb
Zca
Zbc
Zc
c
c
∆-Y
Y- ∆
Source code:
PROGRAM convert
IMPLICIT NONE
REAL :: Zab, Zbc, Zca
REAL :: Za, Zb, Zc
CHARACTER (len=1) :: num
! Impedances in a Delta Connection
! Inpedance in a Y Connection
! 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
WRITE
WRITE
WRITE
WRITE
(*,*)
(*,*)
(*,*)
(*,*)
(*,*)
'Y-connection equivalent impedances:'
'Za =',Za
'Zb =',Zb
'Zc =',Zc
' '
9
CASE ('2')
WRITE (*,*) 'Enter the values of Za, Zb, Zc: '
READ (*,*) Za, Zb, Zc
Zab =
Zbc =
Zbc =
Zca =
WRITE
WRITE
WRITE
WRITE
WRITE
(Za*Zb + Zb*Zc + Zc*Za)/Zc
(Za*Zb + Zb*Zc + Zc*Za)/Za
(Za*Zb + Zb*Zc + Zc*Za)/Za
(Za*Zb + Zb*Zc + Zc*Za)/Zb
(*,*) 'Delta-connection equivalent impedances:'
(*,*) 'Zab =',Zab
(*,*) 'Zbc =',Zbc
(*,*) 'Zca =',Zca
(*,*) ' '
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
Download