2. Shell and other kinds of programming

Shell programming: advanced parts of the bash tutorial

As you go through the tutorial, copy the example commands into your jupyter notebook and test them to make sure you understand how they work. Feel free to do that more than once, changing the given examples as you wish. An example from the "Using Shell Arrays" section of the tutorial would look like this:

You can also save the commands into a shell script and execute it as a "command" in your own filespace:

Writing and compiling programs

Let's try a very simple C program. "Running" a program requires that you first

Usually, the compiler can take care of all of these steps at once.

The above command cc (that stands for "C Compiler") has many switches. Without them, it defaults to a full compile-and-link cycle and places the executable into an executable called a.out, which can get confusing if there is more than one program. This is better:

The link stage of the cc command found all the libraries it needed, including the system library that provided the atan() function.

Homework

Use bash commands ar, nm, grep, find, cut, and bash loops as needed to find which of the *.a or *.so files in /usr/lib64 and/or in /lib and/or /lib64 and all of their subdirectories contains a subroutine to calculate arctan(). Ignore symbolic link files, just use the actual library file, to remove duplication.

Solution

We need to link to the libm*.so library to get the atan() function, and to libc*.so to initiate the running of a main(). It so happens that these are on the default list, so specifying any libraries is not necessary. However, the following should fail to link, and should produce a few errors:

To fix the errors, we need to tell the linker where to look. The compiler/linker switch -lxxx forces it to look for a file libxxx.so.\* in the usual directories. The directory to look in can be also set, using the -L/some_directory/subdir switch. Strictly speaking, in our case we should use -L/lib -lm-2.17 but

so this (simplified) version without specific version numbers should be sufficient:

Using make

The make command treats spaces and TAB characters differently than the jupyter notebook. The following javascript commands should reconfigure jupyter to treat TABS as characters, not replace them with 4 spaces, as it does by default. This is necessary because the syntax of Makefiles mandates the use of TABs.

This was super simple, and for such a simple "project" of only one source-code file probably unnecessary, but we can very quickly make this Makefile more robust and demonstrate some of the more advanced features of make. In larger projects, makefiles save a lot of time.

For details, consult this makefile tutorial.

Gaussian packet evolution: a C project

More homework! This is a week-long project to graphically illustrate what happens to a Gaussian wave packet (we will make it up by adding a few individual waves) impacting on a barrier. We want to observe how some of the packet is reflected, and some undergoes quantum-mechanical tunneling, so both a reflected and a transmitted packet emerges after the impact. We will run the program repeatedly, changing the number of wave making up the packet, the number of points to plot, the initial energy of the incoming packet, etc. The physics of this scattering process will get discussed next, but first let's set up the mechanics of the new project:

The second part of the homework is to try to figure out what is going on in an old Fortran program that does the same thing. We do not know either Fortran or C syntax, but hopefully the computational content, i.e. the algorithmic core, will be self-explanatory. The task is to "port", or convert this old code to C, and learn some basic C programming in the process.

Now that we have a skeleton of a C program working, let's examine the old Fortran code to see how it works and what its output looks like.

Perhaps it's better to just see things graphically. gnuplot is a very good, universally available plotting program, and with just a few keystrokes we can generate good-looking graphs.

We can even create an "animation" of sorts, by using a macro file.

Another way of using gnuplot is to load it right into the notebook itself.

Homework: Developing the numerical part of the C code

Start by copying the skeleton of packet.c code from above, so that you can start making changes, recompile, and re-run. If you continue working in this jupyter notebook, the next two cells will get executed repeatedly. You can also open a separate editor window to modify the packet.c file and use the make to re-build the code after every change.