// Aarhus University / Physics / Subatomic Physics / Nuclear Theory >

Programming. Spring 2016.

[2015 home]

NB:
Schedule:
The Plan:
  1. C Programming: Introduction. Why C? i) Fast and lean programs; ii) Ubiquitous (omnipresent) and popular; iii) C++, Java, C# derive from C; iv) GNU Scientific Library (GSL). Computer language benchmarks. Why Unix POSIX? Top 500 statistics. Why terminal?
    Exercises:
    • Install Ubuntu version 14.04 or 15.10 (or your other favourite POSIX system) on your laptop and bring it to the class. That's the easiest way to get through this course, I think. Alternatively,
    • Learn how to start a terminal.
    • Learn the following commands (file utilities) from GNU Core Utilities: cd, cp, ls, mkdir, mv, rm.
    • Read about the command line completion.
    • Install the Nano text editor with the command sudo apt-get install nano and learn how to create, save, and edit text files with it (alternatively, install your favourite text editor).
    • Install the GNU C compiler with the command sudo apt-get install build-essential.
    • Problems 0: make the exercise C Programming/A taste of C.
  2. Structure of a C program; Basics of compilation; Variables and basic data types (char, int, float, double, long double, complex); Operators (+,-,*,/); Simple output with the printf function; C math functions, math library (remember -lm); type-generic math tgmath.h.
  3. Make utility and makefiles; Control flow: conditional statements (if, switch) and loops (while, for, do-while); Scope of variables: file, function, block.
  4. Pointers and C-arrays; Static, variable-length, and dynamic arrays; Memory allocation with malloc; Passing arrays as parameters to functions.
  5. [lectures/io] User-defined datatypes: structs and unions; simple input and output; streams and redirection.
  6. [lectures/passf] Passing functions as parameters to other functions; C-preprocessor; building a program from functions in different C-files; simple printf-debugging (tracing).
    • Reading:
      • C preprocessor → including files; macro definitions;
      • Debugging;
      • Simple printf(...) or fprintf(stderr,...) debugging amounts to

        ...inserting printf commands that output more or less carefully chosen status information at key points in the program flow, observing that information and deducing what's wrong based on that information. ...Although often looked down on by users of modern debugging software, printf() debugging continues to prove itself indispensable.

        1. Switch on the necessary warnings (e.g. -Wall -Werror).
        2. Read carefully the error/warning messages from the compiler and act upon them. If a message seems cryptic, try the clang compiler (install it with sudo apt-get install clang). Clang largely takes the same options as GCC.
        3. Insert fprintf(stderr,...) command at the strategic points of the program and localize the source of the error.
        4. It is of advantage to define a TRACE macro (read the C preprocessor chapter) and use it instead of directly using fprintf function. The macro can then be simply disabled after debugging. For example:
          #ifndef NDEBUG
          #define TRACE fprintf
          #else
          #define TRACE(...)
          #endif
          
          or, more elegantly,
          #ifndef NDEBUG
          //#define TRACE(args...) fprintf(stderr,args)
          #define TRACE(...) fprintf(stderr,__VA_ARGS__)
          #else
          #define TRACE(...)
          #endif
          
    • Problems 5 (io). Hints: io.
  7. [lectures/gnuplot] Gnuplot and/or Pyxplot.
    • Install Gnuplot and/or Pyxplot on your box with the command(s)
      sudo apt-get --yes install gnuplot-x11
      sudo apt-get --yes install pyxplot gv
      
      The --yes option automatically answers 'yes' on all questions apt-get might ask.

      Some of you might have managed to install a special version of gnuplot, called gnuplot-nox where 'nox' means 'no X-window'. This version, predictably, has no x-window terminal. If you plan to use gnuplot interactively, you have to install 'gnuplot', not 'gnuplot-nox'.

      The two plotting utilities have very similar scripting languages but otherwise are somewhat different. Pyxplot uses LaTeX to render all text on the plot which is rather convenient. Gnuplot has more terminals. Look at their web-pages, gnuplot and pyxplot (specifically, the examples), and choose the one you like most (or both).

    • Read Pyxplot users' guide → "first steps with Pyxplot" (most of it applies also to Gnuplot) and/or Gnuplot documentation, the "Plotting" section.
    • Problems 6. Hints:tabulate.
  8. [lectures/gsl-sf-gamma] GSL: installation, including, linking.
    • Install GSL on your box. First search for the GSL developement library in you system,
      apt-cache search libgsl | grep development
      
      It should return either libgsl0-dev or libgls-dev, depending on your system. Install this package,
      sudo apt-get install libgsl0-dev
      
      or
      sudo apt-get install libgsl-dev
      
    • Alternatively, you can download the source code and build the library on your box. It will probably take more time though.
    • Read the GSL manual chapters 'Introduction', 'Using the library', 'Vectors and Matrices', and 'Eigensystems/Real Symmetric Matrices'.
    • GSL Bessel function example.
    • Problems 7. Hints: standing waves.
  9. [lectures/kummer-odeiv] GSL: vectors and matrices; vector and matrix operations; BLAS; Ordinary Differential Equations Initial Value (odeiv) problem.
    • Read the corresponding chapters in the GSL manual; in particular, learn
      • The interface to the BLAS routines.
      • The data type gsl_odeiv2_system with the fields function, jacobian, dimension, params to hold an Ordinary Differential Equations Initial Value System.
      • The names of the available stepping algorithms: gsl_odeiv2_step_rk2, gsl_odeiv2_step_rk4, gsl_odeiv2_step_rk45, ... , gsl_odeiv2_step_bsimp. Note which of the algorithms require the Jacobian of the system.
      • The driver functions "gsl_odeiv2_driver_alloc_y_new" and "gsl_odeiv2_driver_apply"
    • Problems 8. Hints: (orbit).
  10. [lectures/igamma-integ] GSL: Numerical integration.
    • Read the Introduction to numerical integration in GSL manual;
    • Learn the interfaces to the following structures and functions:
      • gsl_function
      • gsl_integration_qag (adaptive Gauss-Kronrod integrator)
      • gsl_integration_qags (singular integrand)
      • gsl_integration_qagi (infinite interval)
      • gsl_integration_qagiu (infinite upper interval)
      • gsl_integration_qagil (infinite lower interval)
      • gsl_integration_cquad (new fashionable algorithm)
    • Look at the 'Numerical Integration Examples'.
    • Problems 9 (oscillator variational integral). Hints: oscillator.
  11. [lectures/hydrogen-integ-multimin] GSL: Multidimensional minimization.
    • Read the multidimensional minimization chapter in GSL manual, specifically the 'algorithms without derivatives' chapter.
    • You can read more about the Nelder-Mead (downhill-simplex) algorithm in Wikipedia: Nelder-Mead method.
    • Learn the interfaces to the following structures and functions:
      • gsl_multimin_function
      • gsl_multimin_fminimizer
      • gsl_multimin_fminimizer_nmsimplex2
      • gsl_multimin_fminimizer_alloc|free
      • gsl_multimin_fminimizer_set
      • gsl_multimin_fminimizer_iterate
      • gsl_multimin_fminimizer_size
      • gsl_multimin_fminimizer_test_size
    • Understand the Nelder-Mead Simplex example in 'Multimin Examples'.
    • Problems 10 (radioactive decay); examples/decay-multimin.
  12. [lectures/isqrt-multiroot] GSL: nonlinear equations (multidimensional root-finding); archiving projects with tar.
    • Read the multidimensional root-finding chapter in GSL manual, specifically the 'algorithms without derivatives' chapter.
    • Learn the following objects from the 'multidimensional root-finding' chapter:
      • gsl_multiroot_function
      • gsl_multiroot_fsolver_alloc|free
      • gsl_multiroot_fsolver_set
      • gsl_multiroot_fsolver_iterate
      • gsl_multiroot_test_residual
    • Problems 11 (Schrödinger equation for hydrogen atom); Hint: examples.
  13. LATEX: basics; document structure; importing graphics.
    • Install the Tex Live distribution of the TeX typesetting system with the command
      sudo apt-get install texlive texlive-latex-extra
      
    • Read the chapter 'Basics', 'Document Structure', and 'Importing Graphics' in the LaTeX wikibook.
    • Another interesting reading is A brief and concise description of RevTeX 4 package
    • Latex: math; displayed equation; floatings: figures, tables; cross-references; the bibliography.
    • Browse through the sections 'Mathematics', 'Advanced mathematics', 'Floats, Figures and Captions', 'Labels and Cross-referencing', and 'Bibliography Management/Embedded system' in the LaTeX wikibook.
    • Problems 12. Hints: Examples.
  14. Tips on programming and debugging; FAQ.
    • Good coding style:
      1. The code is segmented into small methods/functions spanning no more than a screenful.
      2. The names of functions, structures.members, and variables are meaningful and self-describing.
      3. The logic of the code is easy to follow and easy to modify (by somebody else); only a small number of comments are needed in a good code.
      4. Defensive programming: #include<assert.h> and use assertions plentifully: you can easily disable all assertions by simply recompiling the code with -DNDEBUG c-flag:
        CFLAGS += -DNDEBUG
        
Lecturer:
Dmitri Fedorov
Literature:
  1. Wikibooks, C programming (the link also provides a PDF version).
  2. GNU Scientific Library manual.
  3. Gnuplot documentation.
  4. Pyxplot User's Manual.
  5. Wikibooks, LaTeX.
Links:
Wikipedia

Copyleft © D.V.Fedorov (fedorov @ phys au dk)