780.20: 1094 Session 1
Handouts: Background notes for Session 1;
area.cpp/make_area and other .cpp printouts; Recommended C++ options;
"Useful Unix Commands"; GSL intro
Your goals for today (note: the goals are always over-ambitious!):
- Get used to the GNU/Linux environment (using Linux directly
or via Cygwin on Windows)
- Download, unzip, compile and run some simple C++ programs,
then modify and repeat
- Understand how underflow, overflow, and machine precision
limits can be determined "experimentally"
- Try out a makefile: using g++ compiler warnings
- Try out a program that uses a function from
the Gnu Scientific Library (GSL)
- Try out Python
Please work in pairs with comparable computing and physics experience.
The instructors will bounce around 1094 and answer questions (don't be
shy to ask about anything!).
Hand in this sheet at the end of class.
GNU-Unix Environment with Cygwin (If you want to use Windows)
- Log on with your Physics Department Windows account.
(If you don't know your username and/or password, please let one of the
instructors know.)
- We recommend that you use a shared disk set up for Physics 780.20.
To do so, click on the Start menu and right-click on My Computer.
Select "Map Network Drive" and type \\phypcs\physics 780
for the Folder. Select a drive letter (Z: is a good choice).
Now you can create a working directory ("folder") on this drive with
your name (or make up a name for your group).
- Start up a web browser. Go to the 780.20 home page
(http://www.physics.ohio-state.edu/~ntg/780/) and find the "1094
Sessions" section. Download session01.zip and unzip it
into your 780 folder (double clicking on session01.zip in an Explorer window,
then "Extract all files" is one way to do this) or just
open the .zip file with Winzip.
(You will also be able to unzip it from the Cygwin command line.)
- Double-click on the Cygwin icon (or Start -> All Programs -> Cygwin
-> Cygwin Bash Shell) to start a "Bash shell" window. Ignore
the error messages. At the $ prompt, type startxwin.sh &
to start the X-windows systems. Again, ignore messages and hit OK
if you get a pop-up warning. You should get a white-background
"xterm" window. "Ctrl-middle mouse button" in the window gives
a menu of options; select "Enable Scrollbar" (scrolling is easiest
using the mouse wheel). You can start additional xterm windows
by typing the command xterm & at the prompt.
- Type pwd ("present working directory") to see where
you are. You'll probably get something like /cygdrive/u,
which corresponds to the Windows U: drive.
Go to the Z drive (if that is where your working folder is) with
cd ../z and then go to your working directory
you created above and the session_01 subdirectory.
[Take a look at the unix command summary sheet.]
GNU-Unix Environment (If you want to use Linux)
- If your computer is in Windows, logoff and "restart".
After the Dell screen, there will be a countdown --- hit any key to
stop it.
At the Grub
prompt, use the arrows to highlight "Red Hat Enterprise Linux Client"
and hit return.
Your login account is your Department unix account (see instructor if
you need one).
- Right click on background for a menu with "Konsole". Start up
one or two terminal windows.
- Create a directory for 780 ("mkdir 780"), go to that directory
("cd 780"). [Take a look at the unix command summary sheet.]
- Figure out how to
launch a web browser (e.g., Firefox has an "earth with mouse" icon
or type firefox & at a command line),
find the 780 homepage
(http://www.physics.ohio-state.edu/~ntg/780/) and
download session01.zip into the 780 directory.
Unpack it with "unzip session01.zip", which should create the
session_01 subdirectory with some files in it.
Go to the session_01 subdirectory.
A Simple Program
- Editor: use nedit or emacs or vi or ...
(I suggest using nedit if you don't
know any of these) to look at the simple program area.cpp.
E.g., type nedit area.cpp &. (The "&" runs the editor in the
"background" so the terminal is still available.) Questions about
the code? (If you are new to C++, you're not expected to understand
everything; see the discussion in the background notes for some
details.) Why use "area" and "radius" for variables rather than "A"
and "R", which are quicker to type?
- Compile and link area.cpp to create area (or area.exe on Windows) using
g++ -o area area.cpp
(the executable would be called a.out if you left out "-o area").
Run it by typing "area" (or "./area" if you get an error
message about not finding the file; why does this work?).
Add "<< endl" to the end of the output line (starting "cout ...")
and recompile and rerun. What did this do?
- Modify the program to create some errors and see what
the compiler says to get used to error messages.
Some possibilities (try these then invent your own):
- remove a semi-colon (leaving out a ; is the most common
error you will make!)
- comment out using namespace std; (using //)
- remove a } (or change it from "}" to ")" or "]")
Each time, try compiling again. The compiler should give an error
message with the line number it thinks is a problem and an explanation
(which is often not helpful!). Fix the error and make sure the program
still works. Note that "undeclared"
errors are reported as being at lines that are incorrect. Why?
- List two ways to verify that the program is
running correctly (i.e., giving correct answers):
Later (not in class today!) you may be asked to
carry out the "to do" list in the comments (with assistance, as needed).
Which of them do you know how to do now?
- Copy, rename, and modify area.cpp to make a program
volume.cpp, which calculates
the volume of a sphere. Test it. [Warning: Many people
get this wrong the first time. Test carefully!]
- Let's try Python!
For the experts:
Write a program ("script") to duplicate what area.cpp does.
For everyone else: Look at area0.py in the editor,
and run it (python area0.py is one way).
Think about the comparison with the C++ version; which is better
to use?
Overflows, Underflows, and Machine Precision
Refer to the background notes on number representation as you do this
section.
-
If you're know C++ already and you're really fast, you can
try writing your own codes. Otherwise, use "flows.cpp".
(Look at the code before running it! Where does the output go?
A single-precision number in C++ is a "float" and a
double-precision number in C++ is a "double".
You are to empirically determine (put your answers in the blank
spaces)
- where under- and overflow occur for single-precision
floating-point numbers;
- where under- and overflow occur for double-precision
floating-point numbers.
- Now for the machine precision, which is calculated crudely in
"precision.cpp".
[Note: This code has an intentional bug. Compare to
flows.cpp to track it down.]
Determine empirically
- the machine precision for single-precision
floating-point numbers;
- the machine precision for double-precision
floating-point numbers. [Note: you have to change
the code for this to work correctly. Hint:
the correct answer is smaller than 1e-12.]
Explain in a couple of sentences how the code finds
the machine precision (note that there is an output file):
Using a Makefile
See the handout with the printout of the makefile "make_area".
Makefiles are useful tools to organize the code for computational
physics projects.
They contain instructions for how to build an executable (what files
have the code, what options to use in compiling, what libraries to link
to).
Here we have only one source (.cpp) file, but the makefile
keeps track of all the g++ options.
- Look at make_area in an editor. We'll go through the details of
a makefile later; for now note the following:
- Comments start with "#" and continuation lines
are indicated by "\" (so "SRCS= \" and "area.cpp" is all on one line).
[Warning: There can't be any spaces after the "\".]
- The list of options defined by "WARNFLAGS" are options to g++
(see handout for details).
- Run the makefile using "make -f make_area". It will try to
compile area.cpp and then link to create area. Check that it works.
- Make a copy of make_area ("cp make_area make_flows") and
edit it so that it compiles and links flows.cpp.
[Look for all places "area" appears in the file.]
Using the GSL Scientific Library
A link to the GSL documentation can be
found on the 780.20 web page. Here we look at our first
example program, which has been adapted
from the documentation.
- Examine the file "J0_test.cpp" in
an editor. This test program calculates the cylindrical Bessel
function J0(x) at x=5 using the GSL library function
"gsl_sf_bessel_J0" (you would find out the name of the function by looking
at the web page with the GSL reference manual).
Look up "Bessel function Wikipedia" using Google. How can
you use this to check you are calculating the right thing?
- Compile J0_test.cpp according to the directions in the file and
verify that the correct answer is given.
- Now calculate for x=3
(modify the code for this value or add code to read in any x).
Answer:
- Verify your answer using MATLAB or Mathematica or Python.
If MATLAB, under the Help menu, select "MATLAB Help", choose
"Search Results", and search for "Bessel Functions".
If Mathematica, under the Help menu,
start the Help Browser, choose "Master Index", and look under Bessel
function).
Did you succeed?
Some More Python (if time permits)
- Create volume0.py.
- Look at and run the Python versions of the flows and precision
codes (included in session01.zip).
- Try running Python interactively. At the prompt, just
type python and return. You will get a
>>> prompt. Try import area1 and then
help(area1). Use quit() to exit.
- Tkinter demo: Look at hello_world_gui.py and run it. Try to
change the text and font.
Copying Your Session to Another Account
If you were logged into your partner's account, you should make
your own copy of everything you did.
On Windows,
the easiest way to get your own copy is to work on the share disk
and then copy the working directory to your own account
(after logging in). On Linux (or from Cygwin to a unix account),
use "secure copy" or scp:
- If you are currently in the directory "session_01", go up one
directory using "cd .." (the two dots mean the directory immediately
above). You should now be in the directory you originally created
(e.g., "780").
- If your username is "myname", then you can transfer the entire
session_01 directory to your account with the command:
scp -pr session_01 myname@fox:
Answer "yes" if asked about connecting and then enter your password
when requested.
The "p" and "r" are options to scp that preserve the date and
permissions of the file (this is the "p") and transfer all
subdiretories recursively (this is the "r"). Don't forget the @ or
the : at the end. This will create the directory "session_01" on
the top-level directory on the account "myname" (with the same name).
780.20: 1094 Session 1.
Last modified: 08:30 am, January 05, 2009.
furnstahl.1@osu.edu