Computational Physics Lectures: How to write a scientific project, with examples

Morten Hjorth-Jensen [1, 2]

[1] Department of Physics, University of Oslo
[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

Aug 23, 2017

Overarching aims first

An essential part of this course is to enable you to do science via numerical experiments and develop projects which allow you to study complex systems. The aim is to enhance what we call algorithminc thinking.

Algorithm : A finite set of unambiguous instructions that, given some set of initial conditions, can be performed in a prescribed sequence to achieve a certain goal.

What do we mean with computing?

Computing means solving scientific problems using computers. It covers numerical as well as symbolic computing. Computing is also about developing an understanding of the scientific process by enhancing algorithmic thinking when solving problems.

And this competence is about:

All these elements (and many more) will hopefully aid you in maturing and gaining a better understanding of the scientific process per se. Writing good reports is a central element in achieving such insights.

The standard situation we meet on a daily basis

The standard situation we meet at an almost daily basis:

Again, a proper presentation of obtained results via good scientic reports, aids in including all the above aspects.


When working on the projects, we recommend strongly that you form teams of two to three participants. You must have a github account where we can monitor your progress and give you appropriate feedback.

Furthermore, when setting up your git repository for a given numerical project, you should create a folder where selected benchmarks are placed. These benchmarks could represent a calculation with specific input parameters. This makes your work reproducible, and allows us to see that your programs reproduce selected benchmarks. Furthermore, developing a habit of producing benchmarks, allows you to keep track of the results produced by different versions of your codes.

If you have not used version control before now, it is thus time to do so. Proper version control is central to a good ethical scientific conduct. We do require that you use some kind of version control software when working on the projects. We recommend strongly github. All lectures and additional material is available at the github address of the course

Programming languages

We recommend that you use either C++ or Fortran2008 for your projects. You can use Python as programming language, but normally the efficiency of Python for the problems addressed in this course is lower than for codes written in Fortran or C++. We recommend however that use Python as a scripting language for running codes and making plots, as well as using the ipython notebooks provided by us.

Some basic ingredients for a successful numerical project

When building up a numerical project there are several elements you should think of, amongst these we take the liberty of mentioning the following:

  1. How to structure a code in terms of functions
  2. How to make a module
  3. How to read input data flexibly from the command line
  4. How to create graphical/web user interfaces
  5. How to write unit tests (test functions)
  6. How to refactor code in terms of classes (instead of functions only), in our case you think of a system and a solver class
  7. How to conduct and automate large-scale numerical experiments
  8. How to write scientific reports in various formats (LaTeX, HTML)

More basic ingredients

The conventions and techniques outlined here will save you a lot of time when you incrementally extend software over time from simpler to more complicated problems. In particular, you will benefit from many good habits:
  1. New code is added in a modular fashion to a library (modules)
  2. Programs are run through convenient user interfaces
  3. It takes one quick command to let all your code undergo heavy testing
  4. Tedious manual work with running programs is automated,
  5. Your scientific investigations are reproducible, scientific reports with top quality typesetting are produced both for paper and electronic devices.

The report: how to write a good scienfitic/technical report

What should it contain? A typical structure.

Keep always a good log of what you do.

The report, the abstract

The abstract gives the reader a quick overview of what has been done and the most important results. Here is a typical example taken from a scientific article

We study the collective motion of a suspension of rodlike microswimmers in a two-dimensional film of viscoelastic fluids. We find that the fluid elasticity has a small effect on a suspension of pullers, while it significantly affects the pushers. The attraction and orientational ordering of the pushers are enhanced in viscoelastic fluids. The induced polymer stresses break down the large-scale flow structures and suppress velocity fluctuations. In addition, the energy spectra and induced mixing in the suspension of pushers are greatly modified by fluid elasticity.

The report, the introduction

What should I focus on? Introduction.

You don't need to answer all questions in a chronological order. When you write the introduction you could focus on the following aspects

The report, discussion of methods

What should I focus on? Methods sections.

The report, results part

What should I focus on? Results.

The report, conclusions and perspectives

What should I focus on? Conclusions.

The report, appendices

What should I focus on? additional material.

You can consider moving parts of the material from the methods section to the appendix. You can also place additional material on your webpage.

The report, references

What should I focus on? References.

Where do I find scientific articles, books etc and examples of reports

How can I use Python and matplotlib to make figures for my report

Writing scripts in for example Python to produce high-quality figures allows you in a fast and efficient way to produce scientific results that can be included in a report. Furthermore, many operations can easily be automated, avoding thereby tedious repetitions of commands, as well as possible errors. Here we present a simple Python program which solves parts of project 2 for one quantum mechanical particle in a harmonic oscillator potential. The code plots the radial distribution of the three lowest-lying states, in addition to displaying the lowest three eigenvalues. It is easy to modify the trapping potential and run numerical experiments and test different boundary conditions. The plot is obtained using matplotlib, a Python plotting library which produces publication quality figures in a variety of formats and interactive environments across platforms.

The Python code

The code sets up the Hamiltonian matrix by defining the the minimun and maximum values of \( r \) with a maximum value of integration points. These are set in the initialization function. It plots the eigenfunctions of the three lowest eigenstates.

Procrastination... the curse of all?

Enjoy this video

And research shows that procrastinating enhances creativity!!

© 1999-2017, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license