780.20: 2082 Session 1

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

Your goals for today:

Please work in pairs with comparable computing experience. RJF will bounce around 2082 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". 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 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., Mozilla), 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 will 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, 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

Read the background notes on number representation as you do this section.

  1. You are to empirically determine
    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". 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 know 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).

780.20: 2082 Session 1. Last modified: 07:53 am, January 05, 2005.