6810: 1077 Session 1

Handouts: area.cpp/make_area and other .cpp/.py printouts; Recommended C++ options; "Useful Unix Commands"; GSL intro

Please work in pairs with comparable computing and physics experience. The instructors will bounce around 1077 and answer questions (ask about anything!). Fill out and hand in this sheet at the end of class.


GNU-Unix Environment with Cygwin (If you want to use Windows)

  1. Log on to your Physics Department Windows account. (If you don't know your username and/or password, please let one of the instructors know.)
  2. We recommend that you use a shared disk set up for Physics 6810. To do so, click on the Start menu and right-click on 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.
  3. Start up a web browser. Go to the 6810 home page (http://www.physics.ohio-state.edu/~ntg/6810/) and find the "1077 Sessions" section. Click on session01.zip and unzip it into your 6810 folder (choose "Open with Windows Explorer" and then select it and "Extract all files" is one way to do this). (You will also be able to unzip it from the Cygwin command line if you save it to your 6810 folder.)
  4. Click on Start -> Cygwin64 Terminal (or Start -> All Programs -> Cygwin -> Cygwin64 Terminal) to start a "Bash shell" window. Ignore any error messages. At the prompt ($ or #, it may take up to a minute the first time), type startxwin.exe to start the X-windows system. Again, ignore messages and hit OK if you get a pop-up warning. You should get a white-background "xterm" window, which scrolls using the mouse wheel and can be resized as desired. You can start additional xterm windows by typing the command xterm & at the prompt.
  5. Take a look at the unix command summary handout. Type pwd ("present working directory") to see where you are. Change to the 6810 directory with (if your working folder is on the Z drive) cd /cygdrive/z and then use cd to go to your working directory you created above and then the session_01 subdirectory. Use the tab key for "name completion" (try it!).

GNU-Unix Environment (If you want to use Linux)

  1. If your computer is in Windows, logoff and "restart". Just after the Dell screen, there will be a countdown --- hit any key to stop it. At the Grub prompt, use the arrows to highlight "Linux" 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 in Terminal". Start up one or two terminal windows.
  3. Take a look at the unix command summary sheet. Create a directory for 6810 ("mkdir 6810"), go to that directory ("cd 6810").
  4. 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 6810 homepage (http://www.physics.ohio-state.edu/~ntg/6810/) and download session01.zip into the 6810 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 C++ Program (look at the printout first!)

  1. Mark on the printout what you don't understand about the code for the simple program area.cpp. (If you are new to C++ (or just rusty), you might not understand much; see the discussion in the background notes for some details.) Editor: use nedit or emacs or vi or ... (I suggest using nedit if you don't know any of these) to look at area.cpp. E.g., type nedit area.cpp &. (The "&" runs the editor in the "background" so the terminal is still available.) Why use "area" and "radius" for variables rather than "A" and "R", which are quicker to type?



  2. 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?
  3. To get used to error messages, modify the program to create some errors and see what the compiler says (try these then invent your own):
    1. remove a semi-colon (leaving out a ; is the most common error you will make!)
    2. comment out using namespace std; (using //)
    3. 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 "not declared" errors are reported as being at different lines than where the error is made. Why aren't they noticed immediately?
  4. List two ways to verify that the program is giving correct answers using known special cases and scaling arguments (e.g., multiply radius by factors of 10):



    Later (in PS#1) you will be asked to carry out some of the "to do" list in the comments (with assistance, as needed). Which, if any, of them do you know how to do now?



  5. Copy, rename, and modify area.cpp to make a program volume.cpp, which calculates the volume of a sphere using V = (4/3)πr3. Test it. [Warning: Many people get this wrong the first time. Test carefully!]
  6. Let's try Python! Look at area0.py in the editor, and run it (python area0.py is one way). Compare with the C++ version. Do they have the same pseudocode? List at least three differences between the C++ and Python implementations.

Overflows, Underflows, and Machine Precision

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

  1. Look at the printout for the code "flows.cpp". 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 (in base 10, not powers of 2) (put your answers in the blank spaces and compare to the notes)
    1. where underflow and overflow occur for single-precision floating-point numbers;



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



  2. 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 (in base 10)
    1. the machine precision for single-precision floating-point numbers;



    2. 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 briefly what the machine precision is and how it is found (note the 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.

  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. Did it work?
  3. 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. Ask an instructor about changing them all with the editor.] Success?



Using the GSL Scientific Library

A link to the full GSL documentation can be found on the 6810 web page. Here we look at our first example program, which has been adapted from the documentation.

  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). Look up "Bessel function Wikipedia" using Google. How can you use a graph on the page to check you are calculating the right thing?

  2. Compile J0_test.cpp according to the directions in the file and verify that the correct answer is given. Is it?
  3. Now calculate for x=3 (modify the code for this value or add code to read in any x). Answer:

  4. 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?

  5. Discuss with your partner how a more general program could be structured, and make at least two suggestions here.

Some More Python (if time permits)

  1. Create volume0.py.
  2. Look at and run the Python versions of the flows and precision codes (included in session01.zip).
  3. 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. (On Linux you will need to type /usr/bin/python.)
  4. Tkinter demo: Look at hello_world_gui.py and run it. Try to change the text and font.

Sharing Your Session Using Dropbox

If you were logged into your partner's account, you should make your own copy of everything you did. You could do this directly on Windows or Linux, but we're going to use Dropbox to share files (also your homework with the instructors!).
  1. If you don't already have a Dropbox account, each partner should create one at dropbox.com. (In middle of page, click "Sign up", enter your name, an email address you can access now, and a password, agree to the Terms, and click "Sign up" again.) Cancel any automatic download.
  2. Click on the "Set up Dropbox" icon on the upper left, then click on it one more time on the "Get Started" page. This will finally take you to the page you will usually work with. There will be a "Getting Started.pdf" file and possibly a Photos folder. Create a new folder (second icon, at the left of Search Dropbox) and call it "Last name_6810_01" (where you substitute your own last name :).
  3. Now Upload your files to this folder (first icon, at the left of Search Dropbox). Use "Choose files" and find your Session 1 folder and select all of the files to upload.
  4. Now select "Share a folder" (third icon, at the left of Search Dropbox). This will first verify your email address. Then you can select your Session 1 folder, put your partner's email in "Invite collaborators to the folder", and click "Share folder". Your partner will get an email and be able to access the folder (and files) on his/her account.
  5. If you install Dropbox on your own computer, it will automagically synchronize a local version of the file with the one at dropbox.com!

6810: 1077 Session 1. Last modified: 10:28 am, February 12, 2014.
furnstahl.1@osu.edu