HOMEWORK - FORTRAN Lab 1
You will write your fortran code in stages, a little at a
time. This is the top-down approach to programming. To
ensure that you do this, I ask that you give a different name to each
version of your code. You must get each version of the code
compiled and running, before you enhance the code to create the next
version. This is a form of version control.
This way, the marker can see all the versions of your
code, can confirm that each version compiled and ran successfully, and
can mark each one separately. As is stated at the end of this web
page, to earn full marks for this assignment, you need to save
(and we need to see) all the versions of your code.
The HW assignments for Fortran weeks 1 and 2 all involve the wind-power (wp.f95) fortran code. We are completing most of
these assignments together in class, but there are a few additional
ones that you need to finish. Below are the
assignments for FORTRAN week 1.
Assignment (based on your in-lab work)
- In this part, we learn about main programs, writing to
the screen, commenting, compiling, and running your code. You do
this by demonstrating that you successfully created the
first version of fortran code for wind power, that we all did
together during Lab (see online lab PowerPoint notes for detailed
instructions).
We also start by learning about the "emacs" text editing program, and will use emacs to write all our fortran source code.
This first piece of code holds the main fortran program (windpowermain), and writes one line to the screen that says "Welcome to Wind Power".
Save this file with the name "wp01.f95" , where the "01" tells me that it is version 1.
- Run NX as we learned in class, and then get the
"terminal" prgram running (the blue icon showing a computer screen in
the bottom, 2nd from left, of the NX window). This puts you into
Linux on eidolon.
- Then, change directories to your "fortran" directory, by
typing "cd fortran". Then type "ls", to list the files that you
have inside your "fortran" directory. It should list
"wp01.f95", among others.
- Then, when you compile this first version (as we did during class),
also name your executable to include the version number. For
example, when you invoke the FORTRAN compiler, you to do this by typing
in the Linux command line:
gfortran wp01.f95 -o runwp01
where the "-o" tells the compiler that you want the executable code to
have the name "runwp01", which is my shorthand for "wo Run Version 01".
-
Then test-run your executable by typing in the Linux command line:
./runwp01
where the "." and the "/" are needed before the executable name, so that the computer can find the right path to your program.
- If your progam doesn't run, compare your fortran to my code from my PowerPoint notes
, and make any corrections using emacs (from the terminal, type emacs wp01.f95 & to get the colored-syntax display in emacs), and
re-compile and run on Linux. You must make this version run
successfully before you move to the next part of the assignment.
- Next, we learn about type declarations.
- Save your wp01.f95 program again to ensure you have a copy of this first version, and then in emacs Save As a new: "wp02.f95". In this new version, add the following after the first comment line:
!
!============= Main Program ===========
- add the following lines after the "program windpowermain" line:
! declare variables
implicit none !enforce strong typing
real :: power = 0.0 !power (W) output from turbine
- After the 'Welcome" line, also add:
! Save results
write(*,*) "power =", power !display result
.
- Save, and compile as a new executable.
gfortran wp02.f95 -o runwp02
.
- Run your program by typing the unix command
./runwp02
It should produce the output as shown in the lecture notes. - Fix any bugs, save, compile, run.
.
- Here, we learn about debugging, the value of "version control", and about arrays. Here is help on interpreting error messages.
- In emacs, Save As "wp03.f95",
namely, save it as a new version. Leave the previous version (2)
unchanged because we know it runs successfully. We can always
revert to version (2) if we make so many mistakes in version (3) that
we can't easily fix them.
- For the "Welcome" line, change the write statement from "write(*,*)" to "write(*, a)", and leave the remainder of that line unchanged. Compile it into an executable named "runwp03", and run it. We will discuss in class how to interpret the resulting error message.
- Next, fix that error by replacing a with *,
and make a different error by deleting the closing quotation
marks " at the end of the welcome line. Comple again as version 3
and execute, and interpret the error message as discussed in class.
Note that emacs indicates fortran syntax via coloured text.
This helps you to identify that some quotation marks might be
missing (if the character string doesn't end where it is supposed to).
- To illustrate version control, suppose that we want to give up on our attempt at version 3. So delete "wp03.f95", then open "wp02.f95", and Save As "wp03.f95". This gives us a fresh start on version 3, by starting from version 2 which we know runs successfully.
- Add
array declarations for real numbers for weather sounding heights (z),
wind directions (dir) and wind speeds (speed) (see Lab PowerPoint notes
for details). Also add a character array declaration to hold the
whole input sounding array as characters. Although we are
declaring these arrays now, we will not use them until later.
Save, compile, debug, run, debug, save. At this point, your output should still say only "Welcome to Wind Power"
"power = 0.000000"
- Next,
we learn about operators and subroutines. We will divide the
whole wind-power problem into smaller parts, each part of which is
handled by a separate subroutine. We will create all needed
subroutines, but initially they will be "stubs" that just give
debugging printout to the screen to help you see which subroutines are
running. We will also insert into the main program the calls to
these subroutines.
Start by creating a new version of your source code (namely, start with wp03.f95 and Save As wp04.f95). Again, follow the directions from the PowerPoint slides. We will make subroutines for :
- welcome !welcome the user
- getturbinespecs !get turbine specifications
- getsounding !get atmospheric sounding of winds
- findpower !calculate the wind power
- saveresults !display the wind power
As an example, subroutine welcome might say
subroutine welcome
implicit none !enforce strong typing
write(*,*)
write(*,*) "Welcome to Wind Power"
end subroutine welcome
Write
similar stubs for the other subroutines, but instead of saying welcome,
you should give the name of the subroutine and say what it does.
- Save, compile, debug, run, debug, save. Your output from runwp04 should be as shown in the PowerPoint slides from lab.
- Next,
we learn more about input and output (I/O). We will learn how to
write lines to the screen to prompt the user to enter something, and
then read what the user enters.
Create a new version 05. Within subroutine getturbinespecs, we will ask the user to enter the specifications of the wind turbine (its hub height zhub and turbine radius r).
After reading these values, echo (print) them to the screen to
ensure the user that the values have been read successfully.
Save, compile, debug, run, debug, save. Do this as a new
version (version 05). See PowerPoints from lecture to learn details.
- Similar to the previous exercise, but as a new version 06, in subroutine getsounding
prompt the user to enter the name of the file holding the sounding (the
list of winds at different heights), and then read that name as a
variable called soundingfilename, and echo it to the screen. Save, compile, debug, run, debug, save. Do this as a new version (version 06).
See PowerPoints from lecture to learn details. At this
stage, we are just asking for the name of the sounding file, but are
not opening or reading it.
- Next, learn about opening data files that are on disk. (See details in PowerPoints for lab). Do this as a new version 07. In the getsounding subroutine, open the file that is saved in the soundingfilename variable, and display any I/O error flag to the screen. Save, compile, debug, run, debug, save.
- Next, learn about conditionals (if tests). (See details in PowerPoints for lab). Do this as a new version 08. In the getsounding
subroutine, add code to use the I/O error flag, and write one statement
to the screen if the file was not openable, and write a different
statement if the file was openable. Save, compile, debug, run, debug, save.
- Similar to the previous exercise, but as new version 09. In subroutine getturbinespecs,
add code to check that the turbine blade radius is less than the the
hub height (because if the blade radius is too long, then the blade
will hit the ground). If the radius is too large, allow the user
to enter a new radius. Save, compile, debug, run, debug, save.
- Next, learn about "while" loops, and about reading lines from disk files. As new version 10, modify getsounding
to create a loop to read every line of the file, check for I/O errors
(and exit the loop if it is an error), and write each line to the
screen as you read it. (see PowerPoint for details). Save,
compile, debug, run, debug, save.
- Learn about the "do while" loop, and use it in getturbinespecs to allow the user to continue entering new turbine radius values until a valid one is finally entered. This is version 11 (see PowerPoint for details). Save, compile, debug, run, debug, save.
- Learn about counting do loops, and about rewinding the disk file. As version 12, add code to getsounding
to reset the file back to the beginning, and read only the first 5
lines of the file. (These are the heading lines that don't have
any sounding numbers in them, so this counting do loop allows us to
skip past them.)
Marking:
The bottom line is that we will look for the following files in the "fortran" directory under your user directory on eidolon:
- Twelve ascii text files holding your fortran code versions:
- wp01.f95
- wp02.f95
- wp03.f95
- wp04.f95
- wp05.f95
- wp06.f95
- wp07.f95
- wp08.f95
- wp09.f95
- wp10.f95
- wp11.f95
- wp12.f95
- ... and twelve executable files:
- runwp01
- runwp02
- runwp03
- runwp04
- runwp05
- runwp06
- runwp07
- runwp08
- runwp09
- runwp10
- runwp11
- runwp12
Deadline is 11:59 pm on Friday at the end of the Fortran-1 week. The marker will grade your work by looking at EACH of
your fortran wp versions on eidolon, and compiling
them himself/herself to be sure they run.
Also, marks will be deducted if your code is not adequately
documented with in-line comments. See the online PowerPoint lab
notes for details.
It
is important that you finish all 12 steps by the start of the next week
of FORTRAN, because we will expand upon this past work to continue
writing the wind-power program.
Hints:
- here are some tips on how to read compiler error messages .
- look at the sample medium-size FORTRAN program and the sample large FORTRAN program. These are not a wind-power programs, but they show how to use many of the fortran commands that we will use soon.
- refer to the the tutorials listed below (Reading Assignment) .
- check the full, online fortran users reference manual listed in
the Resources link of the course web page. Although this is for a
different compiler than the one we are using, its FORTRAN 95
description should be close enough.
- Check the code segments in the online PowerPoint slides for Fortran-1 if you have difficulties.
Reading Assignment
- Skim chapters 2 - 4 of the following FORTRAN tutorials.
(courtesy of Stephen Brooks, Univ. of St. Andrews, Scotland. http://www-solar.mcs.st-and.ac.uk/%7Esteveb/course/course.html )
- Basics: program structure, data types, programming style, conditional statements, loops, arrays .
- Arithmetic expressions, intrinsic functions, simple input and output (I/O) .
- Characters, strings, print .
- Internal subroutines and functions .
- Modules .
- More about arrays, dynamic allocation of arrays, key words .
- More about I/O .
- Case statement .
- Read in the course pack the
3 papers on "Binary Calculations and Pitfalls", on how numbers are
stored in binary on computers, including the paper on "Higher
Arithmetic".
- Read in the course pack
the Good Programming Practice paper by Chapman 2008 "Program Design",
which covers "Top-down Design Techniques" and "Flowcharts" .
-end-
Copyright © 2010 by Roland Stull.
UBC.