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

Programming. Spring 2015.

[2014 home]

NB:
Schedule:
The plan:
  1. C Programming: Introduction. Why C? Why Unix POSIX? Why terminal?
    Exercises:
    • Install Ubuntu version 14.04 on your laptop and bring it to the class. That's the easiest way to get through this course, I think. Alternatively, get yourself an account on a POSIX server and bring a laptop from which you can ssh to your account.
    • 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 (or install your other favourite editor) and learn how to create, save, and edit text files with it.
    • Install the GNU C compiler with the command sudo apt-get install build-essential.
    • Make the exercise C Programming/A taste of C.
  2. Basics of compilation; Structure of a C program; 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. Contol flow: conditional statements (if, switch) and loops (while, for, do-while); Functions, blocks, and scope of variables;
  4. Pointers and C-arrays; Static, variable-length, and dynamic arrays; Memory allocation with malloc; Passing arrays as parameters to functions.
  5. User-defined datatypes: structs and unions; simple input and output; streams and redirection; simple printf-debugging.
    • Reading:
      • C syntax → Structures and Unions (concentrate on structures, we won't be using unions).
      • C syntax → Functions → Global Structure → main invocation.
      • C syntax → Miscellaneous → Command-line arguments.
      • atoi (ascii to integer) and atof (ascii to double) functions from <stdlib.h>.
      • Simple input with scanf function.
      • Unix I/O redirection.
      • 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(...) fprintf(stderr,__VA_ARGS__)
          #else
          #define TRACE(...)
          #endif
          
    • Problems 4. Hints.
  6. Make utility and makefiles; passing functions as parameters to other functions; C-preprocessor; building a program from functions in different C-files.
  7. 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. Reading the Gnuplot manual is a daunting task, admittedly, nevetheless try read the "Plotting" section.
    • Pyxplot example.
    • Problems 6. Hints.
  8. GSL: installation, including, linking.
    • Install GSL on your box with the command
      sudo apt-get install libgsl0-dev gsl-bin
      
    • Alternatively, you may 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.
  9. 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"
    • Example from the lecture.
    • gsl_odeiv2 example.
    • Problems 8. Hints.
  10. GSL: Numerical integration, QAG (Gauss-Kronrod) and CQUAD (Clenshaw-Curtis) algorithms;
  11. GSL: multidimensional root-finding, algorithms without derivatives; Multidimensional minimization, .
    • Read the multidimensional root-finding chapter in GSL manual, specifically the 'algorithms without derivatives' chapter.
    • You can read more about the Newton's and Broyden's algorithms in the Newton's method article, and the Broyden's method article.
    • Specifically, learn the following objects from the 'multidimensional root-finding' chapter:
      • gsl_multiroot_function
      • gsl_multiroot_fsolver_alloc
      • gsl_multiroot_fsolver_free
      • gsl_multiroot_fsolver_set
      • gsl_multiroot_fsolver_iterate
      • gsl_multiroot_test_residual
    • In the Multidimensional Minimization chapter read about the following structures and functions:
      • gsl_multimin_function
      • gsl_multimin_fminimizer
      • gsl_multimin_fminimizer_nmsimplex2
      • gsl_multimin_fminimizer_alloc
      • gsl_multimin_fminimizer_set
      • gsl_multimin_fminimizer_iterate
      • gsl_multimin_fminimizer_size
      • gsl_multimin_fminimizer_test_size
    • Look at the Nelder-Mead Simplex example in 'Multimin Examples'.
    • Example from the lecture.
    • Problems 10. Examples.
  12. LATEX: basics; document structure; importing graphics.
  13. 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.
    • Hint for nano users: add the following lines to your ~/.nanorc to show the otherwise invisible tabulator characters:
      syntax "makefile" "[Mm]akefile"
      color white,magenta " "
      
      The character in between the quotation mark in the second line should be the tabulator sign.
    • Example from the lecture.
    • Problems 12. 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
        
    • Problems 13.
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)