Due Thursday, February 10, 11:59 P.M.
The purpose of this lab is to introduce you to writing, compiling, and running Fortran-95 programs using the Intel Fortran compiler.
As with Maple and Matlab, in order to run the Intel Fortran compiler (ifort) you may need to make some changes to your environment variables. Make sure your PATH variable contains the /usr/intel/fc_80/bin directory and your MANPATH contains /usr/intel/fc_80/man. Now either start a new shell or type
source ~/.zprofileto update your shell environment without restarting. You should now be able to run ifort by typing ifort at the command prompt.
The steps listed below describe everything you need to do in order to write, compile, and run a minimal Fortran-95 program. You will need to complete steps almost identical to these for every Fortran program you write.
vi hello.f90The .f90 extension indicates to the compiler that this is a Fortran program using free-form source format. Fortran programs can also end with .f to indicate that they use fixed-form source format, but you should never write new programs this way.
! Author: George Butler ! Date: Feb 2, 2005 ! Purpose: This is my first Fortran program.
PROGRAM helloThis tells the compiler that you are starting a new Fortran program called hello.
IMPLICIT none
This line tells the compiler that it should disable implicit typing,
i.e. that it should not allow you to use any variables that you have
not explicitly declared. While not required, this statement has the
following benefits:
WRITE(*,*) " Hello World"
This statement will write the message contained in double quotes to
the screen after you complie and execute your program. The blank line
serves to provide a visual division between the declaration section (which
contains the program name and variable names) and the execution section
(which contains commands that will be executed when you run your program).
Note that the lines you wrote in this step and the previous one are both
indented. You should try to format your source code so that indentation
mirrors the semantic hierarchy of the statements. For example, both the
IMPLICIT none statement and the WRITE statement are
logically contained in the program hello, so they should have
a higher level of indentation than the PROGRAM statement.
END PROGRAMThis tells the compiler that it has reached the end of the program called hello. This time, the blank line serves to separate the execution section from the termination section.
! Author: George Butler
! Date: Feb 2, 2005
! Purpose: This is my first Fortran program.
PROGRAM hello
IMPLICIT none
WRITE(*,*) " Hello World"
END PROGRAM
Note that the compiler doesn't care about how you use spacing, or about
whether you use comments in your program. For example, the following
Fortran program is functionally equivalent to the one given above:
PROGRAM hello;IMPLICIT none;WRITE(*,*)" Hello World";END PROGRAMHowever, the second program is harder to read, harder to modify, and uglier than the first.
ifort hello.f90This will create a binary executable file called a.out in the current working directory.
./a.outYou should see the message that you entered following the WRITE statement printed to the screen. If not, ask me or Dan for assistance. Otherwise, congratulations! You have just written, compiled, and executed your first Fortran program.
ifort -o hello hello.f90 ./hello
You will be writing a short Fortran program for this lab, which will perform a small but useful computation. Write a program called bmi that prompts the user for their height (in feet and inches) and weight (in pounds), and prints out the corresponding body mass index (BMI). Note that BMI is defined as body mass (in kg) divided by the square of height (in m). You can download the executable for my Fortran implementation of the program bmi here or copy the file ~gbutler/www-home/m171/spring_2005/bin/bmi into your home directory. Your own program should duplicate the behavior of mine almost exactly (your answers should equal mine to at least a few decimal places). Once you have your program working, complete the following steps to demonstrate that you know how to compile and execute Fortran code. Note that these steps will not be required in future labs.
After completing the above steps, delete your executable and any other extraneous files you have lying in your lab directory, and submit your lab using the ~gbutler/bin/m171_submit command.