780.20: 1094 Session 1

Handouts: Background notes for Session 1; area.cpp/make_area; Recommended C++ options; "Useful Unix Commands"; GSL intro

Your goals for today (note: the goals are always over-ambitious!):

Please work in pairs with comparable computing experience. The instructors will bounce around 1094 and answer questions (don't be shy to ask about anything!).

Linux Environment

The first step is to get accustomed to the Linux environment. Go at your own pace. Use a terminal window and type at the command line for today; later you can use a file browser if you want.

  1. 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 "Fedora Core" and hit return. Your login account is your Department unix account (see instructor if you need one).
  2. Right click on background for a menu with "Open Terminal". Start up one or two terminal windows.
  3. Create a directory for 780 ("mkdir 780"), go to that directory ("cd 780"). [Take a look at the unix command summary sheet.]
  4. Figure out how to launch a web browser (e.g., Firefox has an "earth with mouse" icon), find the 780 homepage (http://www.physics.ohio-state.edu/~ntg/780/) and download session01.tarz into the 780 directory. Unpack it with "tar xfvz session01.tarz", which should create the session_01 subdirectory with some files in it. Go to the session_01 subdirectory.
  5. Editor: use vi, emacs, pico, or nedit (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?



  6. Compile and link area.cpp to create area 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.
  7. How can you verify that the program is running correctly?



    Later (not in class today!) you may be asked to carry out the "to do" list in the comments (with assistance, as needed). Do you know how to do these things now?



  8. Copy and modify area.cpp to make volume.cpp ("cp area.cpp volume.cpp"), which calculates the volume of a sphere. Test it.
  9. Start up Mathematica. If you already know some Mathematica, try to write a function to calculate the area given the radius, and return a numerical answer. For the experts: Can you write a "program" that asks for the radius like the C++ program? If you don't know how to do these things, or after you've done them, load area.nb (File -> Open), run the commands, and try to understand the definitions.

Overflows, Underflows, and Machine Precision

Follow along in the background notes on number representation as you do this section.

  1. You are to empirically determine (put your answers in the blank spaces)
    1. where under- and overflow occur for single-precision floating-point numbers;



    2. where under- and overflow occur for double-precision floating-point numbers.



    If you're really fast and know C++ already, you can try writing your own codes. Otherwise, use "flows.cpp". (Look at the code before running it!) A single-precision number in C++ is a "float" and a double-precision number in C++ is a "double".
  2. Now determine empirically
    1. the machine precision for single-precision floating-point numbers;



    2. the machine precision for double-precision floating-point numbers.



    Once again, code your own version or use "precision.cpp". Explain to your partner in a couple of sentences how the code finds the machine precision.

Using a Makefile

See the handout with the printout of the makefile "make_area" (on the back of the area.cpp printout). 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.

  1. Look at make_area in an editor. We'll go through the details of a makefile later; for now note the following:
  2. 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.
  3. Try making a copy of make_area ("cp make_area make_flows") and edit it so that it compiles and links flows.cpp.

Using the GSL Scientific Library

Links to the GSL documentation can be found on the 780.20 web page. Several example programs have been taken from the documentation and converted to C++.

  1. 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).
  2. Compile J0_test.cpp according to the directions in the file and verify that the correct answer is given. Try a different x value (or modify the code to read one in).
  3. Try to verify your answer using Mathematica (under the Help menu, start the Help Browser, choose "Master Index", and look under Bessel function).

Copying Your Session to Another Account

If you were logged into your partner's unix account, you might want to have a your own copy of everything you did. Here's one way to copy a directory (and subdirectory) to another unix account, using "secure copy" or scp.
  1. 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").
  2. 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).
  3. This is a handy command for file transfers; we'll look at it more extensively later on.

780.20: 1094 Session 1. Last modified: 03:36 pm, January 03, 2006.
furnstahl.1@osu.edu