780.20: 1094 Session 9

Handouts: Printout of nonlinear.nb notebook, excerpt from Landau/Paez Chapter 5, printout of GslSpline and test files, "Using GSL Interpolation Functions", ode_test.cpp printout, sample .bashrc file

In this session, we'll look at using a GSL adaptive differential equation solver and then look at interpolation.

Your goals for today and ...

Please work in pairs (more or less). The instructors will bounce around 1094 and answer questions.


Follow-up to Session 8

Work on these tasks for the first 30 minutes of the session only, then move on.

  1. Take a look at the private_vs_public.cpp code, which is a self-contained class and main program. Notice how the main program can access and change the x value and use the xsq function. But try changing from x to y in the statements getting, printing, and changing the value of x. What happens? Why does the get_y function work?


  2. Complete the Session 8 sections on "Damped, Driven Pendulum" and "Looking for Chaos".
  3. The Mathematica notebook nonlinear.nb looks at the same type of analysis as diffeq_pendulum.cpp only it uses the Duffing equation. Look through it and follow along with the printout. This exercise is mostly just to expose you to how to do lots of useful things with Mathematica, for future reference. What questions do you have about Poincare sections or the power spectrum?


  4. If you're familiar with Mathematica and have time, you could convert nonlinear.nb to study the pendulum instead. But for now, just run the "answer" notebook called pendulum.nb.


Bash Shell Stuff

Here are some things you should know about using the Bash shell. (I'm assuming everyone is using this. If not, type bash at a prompt and you will be in the Bash shell!)

  1. Bash aliases. The .bashrc file sits in your home directory and is "sourced" when you start an interactive terminal. Take a look at the sample .bashrc file printout (the file bashrc, without the ".", is in the session 9 zip file). Copy any aliases of interest to your own .bashrc (or the whole thing using cp bashrc ~/.bashrc if you don't have one already) and activate the changes with the command: source ~/.bashrc. Try some aliases such as ll (for "long listing") and df, which shows info about the disk file system.
  2. Create your own alias in .bashrc to automatically log you in (via ssh) to a computer you use a lot (e.g., fox.mps.ohio-state.edu). What line did you add to .bashrc?


  3. Finding a command. Suppose you want to know both the defined alias and the original command for du. Use "type -a du" and you should be told all of the possibilities. Try this on some other commands.
  4. The ulimit command. Type "ulimit -a". Can you figure out how to change the stack size to "unlimited"? [Note: changing the stack size is not allowed on Cygwin.]
  5. History. To re-execute the last command that contained "string", use !string. E.g., !make will re-run the last make command. If you type "history", you'll get a list of recent commands, with numbers. To re-execute number 25, type !25. Put the H function from the sample bashrc in your own .bashrc and activate the change. This command searches your history for matches, e.g., "H string" will find those lines with "string" somewhere. You can then use the numbers to re-execute the one you want. Try it! (E.g., try "H ulimit")
  6. Line editing. If you use the up-arrow to bring back a line that you want to change, you can use emacs-style commands to move around on the line. For example, Ctrl-a takes you to the start of the line while Ctrl-e takes you to the end. Ctrl-b and Ctrl-f move you back and forward a character at a time, while Alt-b and Alt-f move you back and forward a word at a time. Ctrl-k kills to the end of the line. Ctrl-d deletes the current character and Alt-d deletes the current word. Try these out!

Introduction to Rsync

Rsync is a backup or mirroring tool that only copies the differences of files that have changed. It can do this transfer in compressed form and using ssh for security (both recommended!). So updates are fast, efficient, and secure. Here we'll make a trial run so that you get the basic idea.

  1. Take a look in the bashrc file for the definitions "rsyncbackup" and "rsyncmirror". Make sure they are in your own .bashrc file (and run "source .bashrc" to activate them). These aliases use some of the common rsync options. The difference is that rsyncmirror deletes files at the destination that don't exist at the source; be careful of this!
  2. Create a directory in your home directory called "780_backup" to use for testing rsync (i.e., "mkdir 780_backup").
  3. We'll create a backup of the session 9 directory on fox. (Since all your files are mounted on all of the physics public computers, you don't need to copy to another computer, but this is a demonstration!) Go to the parent directory of session_09. Give this command:
      rsyncbackup session_09 fox.mps.ohio-state.edu:780_backup
      
    If you are asked if you want to continue connecting, answer yes. You'll probably be asked for your password as well (it is the same one everywhere). You should see a list of files transferred and some statistics. [If you don't have a Department unix account, backup to your home directory with
      rsyncbackup session_09 ~
      
    (where ~ is your home directory; to go there use cd ~).]
  4. Check that the directory was transferred. Now go back to the original session_09 directory and change one file. (E.g., edit one of the .cpp files and add a comment.) Then repeat the rsync transfer (from the appropriate directory), using "!rsync" to avoid typing the entire alias. You should find that only the changed file is updated.

GSL Differential Equation Solver

The program ode_test.cpp demonstrates the GSL adaptive differential equation solver by solving the Van der Pol oscillator, another nonlinear differential equation (see the Session 9 background notes for the equation).

  1. Take a look at the code and figure out where the values of mu and the initial conditions are set. Change mu to 2 and the initial conditions to x0=1.0 and v0=0.0 (y[0] and y[1]). Note the different choices for "stepping algorithms", how the function is set up and that a Jacobian is defined, and how the equation is stepped along in time. Next time we'll see how to rewrite this code with classes.
  2. Use the makefile to compile and link the code. Run it.
  3. Create three output files using the initial conditions [x0=1.0, v0=0.0], [x0=0.1, v0=0.0], and [x0=-1.5, v0=2.0] (just change values and recompile each time). Notice how we've used a stringstream to uniquely name each file.
  4. Use gnuplot to make phase-space plots of all three cases on a single plot. Print it out and attach it. What do you observe? This is called an isolated attractor.





GSL Interpolation Routines

We'll use the Landau/Paez text's example of a theoretical scattering cross section as a function of energy to try out the GSL interpolation routines. The (x,y) data, with x-->E and y-->sigmath, is given in the bottom row of Table 5.1 (see the Chapter 5 excerpt; note we are NOT fitting sigmaexp).

  1. Start with the gsl_spline_test_class.cpp code (and corresponding makefile). Take a look at the printout and try running the code. Note that we've used a Spline class as a "wrapper" for the GSL functions, just as we did earlier with the Hamiltonian class. Compare the implementation to the example on the "Using GSL Interpolation Functions" handout.
  2. Instead of the sample function in the code, you will change the program to interpolate the Table 5.1 data from the handout. Set npts and the (x,y) arrays equal to the appropriate values when you declare them. Declare them on separate lines. An array x[4] can be initialized with the values 1., 2., 3., and 4. with the declaration:
    double x[4] = {1., 2., 3., 4. };
  3. Use the code to generate a cubic spline interpolation for the cross section from 0 to 200 MeV in steps of 5 MeV. Output this data to a file for plotting with gnuplot and try it out.
  4. Now modify the Spline class to allow for a polynomial interpolation and change the gsl_spline_test_class.cpp main program to generate linear and polynomial interpolations as well and add the results to the output file.
  5. Generate a graph with all three interpolations plotted. For each curve, how does the resonance energy Er (the peak position) and gamma (full width at half maximum) as seen on the graph compare to the theoretical predictions of (78,55)?




Cubic Splining [Save for PS#4]

Here we'll look at how to use cubic splines to define a function from arrays of x and y values. A question that always arises is: How many points do we need? Or, what may be more relevant, how accurate will our function (or its derivatives) be for a given spacing of x points?

  1. We'll re-use the Spline class from the last section and the original gsl_spline_test_class.cpp function, which splined an array.
  2. The goal is to modify the code so that it splines the ground-state hydrogen wave function: u(r) = 2*r*exp(-r)
  3. Your task is to determine how many (equally spaced) points to use to represent the wave function. Suppose you need the derivative of the wave function to be accurate to one part in 106 for 1 < r < 4 (absolute, not relative error) Devise (and carry out!) a plan that will tell you the spacing and the number of points needed to reach this goals. What did you do?



  4. Now suppose you need integrals over the wave function to be accurate to 0.01%. Devise (and carry out!) a plan that will tell you the spacing and the number of points needed to reach this goals. (To try out integrals, use one of the GSL integration routines on an integral involving u(r) that you know the answer to; there's an obvious one!)




780.20: 1094 Session 9. Last modified: 07:34 am, February 11, 2009.
furnstahl.1@osu.edu