// Aarhus University / Physics / Subatomic / Theory >

Practical Programming F17Q3. Spring 2017.
[ 2016 home | Wiki ]
[ NB | Lectures : 0 1 2 3 4 5 6 7 8 9 10 11 ]
NB

Schedule
(included from here)

Plan
  1. Introduction; UNIX/POSIX systems; C programming language; Free software.
    • Prerequisite: an access to a POSIX system. You've got several options:
      1. Ubuntu. The latest version is 16.10. The latest long term support version is 16.04. You can either install it directly on your laptop (default) or as a virtual machine (VirtualBox is a popular hypervisor).
      2. Most of other Linux-based systems (like Debian or Fedora) would do just as well. Though some small details might differ.
      3. MacOS with homebrew is a proven possibility. Though, since it is FreeBSD-based, certain things are different from Ubuntu.
      4. If you have an account on a POSIX server you can well work on it. Bring a laptop from which you can ssh to your account.
      5. Microsoft Windows with Cygwin is also a possibility. Less explored though. Some things will certainly be different.
    • Reading
      • Learn how to start a terminal.
      • Read about the man utility. Run man man and read the DESCRIPTION of the utility in your system. Hints: man usually uses less as the default pager; you quit from less by pressing the q-key.
      • Learn the following commands (file utilities) from the GNU Core Utilities: cp, ls, mkdir, mv, rm, and the bash built-in command cd. Hint: type, for example, man 1 mkdir in your terminal or into Google-search in your browser.
      • Read about the command line completion.
    • Exercises
      • Install your favourite text editor. If you don't have one, install the Nano text editor with the command
        sudo apt-get install nano.
      • Learn to create, save, and edit text files with your favourite text editor.
      • Install the GNU C compiler — gcc — with the command
        sudo apt-get install build-essential.
      • Problems "hello". See also the exercise C Programming/A taste of C. Hint: examples/hello.
    • Hints
      • You quit from man by pressing q.
      • Directory abbreviations:
        ~ home directory
        .. parent directory
        . current working directory
        $HOME home directory
      • cd - changes into the previous directory.

  2. [lectures/math] 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. [lectures/make] Make utility and makefiles; Conditional statements if, switch and loops while, for, do-while; Scope of variables: file, function, block.
  4. [lectures/pointers] Pointers; Arrays and structs.
  5. [lectures/io] Passing functions as arguments; Simple input and output; Streams and redirections.
  6. [lectures/gnuplot] C-preprocessor; building a program from functions in different C-files; simple printf-debugging (tracing); gnuplot/pyxplot.
    1. You have to make the folder with your homeworks (and later the examination project) visible in the internet via a browser. There are many possibilities, for example, (bitbucket is the coolest, you think?),
    2. Go to our Wiki page and put a link to your homework-folder-in-the-internet in the Homeworks-page at the Wiki.
    3. Note "printf debugging".
    4. Also:
    5. Install Gnuplot and/or Pyxplot on your box. If you are one MacOS with homebrew you should probably use the command
      brew install gnuplot
      
      On Debian based systems, like Ubuntu, the command is
      sudo apt-get --yes install gnuplot
      
      and/or
      sudo apt-get --yes install pyxplot gv
      
      Actually it seems there appeared a new gnuplot version 5, so if you want the latest, do
      sudo apt-get --yes install gnuplot5
      
      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).

      Note: it seems the developement of pyxplot has stagnated at the moment, therefore gnuplot might indeed be a better option.

    6. Questions 5.
    7. Problems "io-gnuplot". Hints: examples/io-gnuplot.

  7. [lectures/gsl-sf]
    GNU Scientific Library (GSL): installation, including, linking; vectors and matrices.
    1. 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
      
      The HomeBrew users should (apparently) use the command
      brew install gsl
      

      Alternatively, you can download the source code and build the library on your box. It will probably take more time though.

    2. Read the GSL manual chapters 'Introduction', 'Using the library', 'Vectors and Matrices', and 'Eigensystems/Real Symmetric Matrices'.
    3. Questions 6.
    4. Problems "eigen". Hints: examples/eigen.

  8. [lectures/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
    • Questions 7.
    • Problems "orbit". Hints: (examples/odeiv-orbit).

  9. [lectures/latex]
    LATEX: basics; document structure; importing graphics.
    1. Install LaTeX on your system:
      1. Debian based systems: install Tex Live distribution with the command
        sudo apt-get install texlive texlive-latex-extra
        
      2. MacOS with HomeBrew: install MacTeX distribution. It seems that you first need to install homebrew cask and then
        brew cask install mactex
        
        or
        brew cask install basictex # allegedly a small but functional version of MacTeX
        
    2. Read the chapter 'Basics', 'Document Structure', and 'Importing Graphics' in the LaTeX wikibook.
    3. Another interesting reading is A brief and concise description of RevTeX 4 package.
    4. Browse through the sections 'Mathematics', 'Advanced mathematics', 'Floats, Figures and Captions', 'Labels and Cross-referencing', and 'Bibliography Management/Embedded system' in the LaTeX wikibook.
    5. Learn how to do the following things in LaTeX: math; displayed equation; floating figures and tables; cross-references; the bibliography.
    6. Questions 8.
    7. Problems "latex". Hints: lectures/latex.

  10. [lectures/integ-gamma]
    GSL: Numerical integration.
    1. Read the Introduction to numerical integration in GSL manual;
    2. Learn the interfaces to the following structures and functions:
      • gsl_function (providing the function to integrate)
      • 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)
    3. Look at the 'Numerical Integration Examples'.
    4. Questions 9.
    5. Problems "integ". Hints: lectures/integ-gamma.

  11. [lectures/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'.
    • Questions 10.
    • Problems "optimization". Hint: examples/multimin-decay.

  12. [lectures/multiroot]
    GSL: nonlinear equations (multidimensional root-finding).
    • 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
    • Questions 11.
    • Problems "multiroot" (Schrödinger equation for hydrogen atom: shooting method); Hint: examples/multiroot-H.

  13. 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)

Sear wikipedia for c language.