Jupyter Notebooks and Python Basics

Welcome to Computational Science, welcome to Python programming! In this course, you will implement various basic programming techniques in Python and apply them to solving physics problems.

In this course, you will use Python using a platform called "Jupyter Notebooks". Jupyter notebooks are a way to combine formatted text (like the text you are reading now), Python code (which you will use below), and the result of your code and calculations all in one place. Go through the notebooks and run the examples. Try to understand their functioning and, if necessary, add code (such as print statements or variable overviews) to make it clear for you. In addition, there are exercises and practice cells where you can program for yourself. Don't be afraid to start coding yourself, writing code and making mistakes is the best way to learn Python.

In this lecture, you will learn the basic concepts of programming in Python. To get started, we will first explain the basic concepts of what python is and how it works.

Learning objectives for this notebook:

  • Student is able to start the python interpreter and run code in a notebook
  • Student can stop and start notebook kernels
  • Student is able to create variables
  • Student can use %whos to list the variables stored in the memory of the python kernel
  • Student is able to determine the type of a variable
  • Student is able to convert between different variable types (float, int, etc)
  • Student is able to collect user input using the input() command
  • Student is able to print variable values using the print() command

Functions in Python

In this notebook, we will explore the implementation of functions in Python.

Learning objectives for this notebook:

  • Student is able to define functions with input parameters to execute a piece of code (use functions as "methods")
  • Student is able to create and use functions that return a value (or multiple values)
  • Student is able to import functions from libraries / modules
  • Student is able to use Shift-Tab to bring up the help for a function from a library
  • Student is able to predict if a variable name in a function refers to a local variable or a global variable

Python program flow control

In this notebook, we will learn how to control the flow of execution of Python code using conditional statements and loops.

Learning objectives for this notebook:

  • Student is able to create conditional branching statements using if, elif, and else
  • Student is able to formulate conditions using ==, <, >
  • Student is able to combine logical conditions using and and or
  • Student is able to create a while loop that exits on a particular condition
  • Student is able to create a for loop that loops over a range of numbers using the range() command
  • Student is able to exit loops using the break command
  • Student is able to skip the rest of a code block in a loop using continue

Scientific Computing in Python with Numpy

Numpy (numerical Python) is a library designed for performing scientific computing in Python.

In this notebook, we will introduce numpy arrays, a data structure introduced in numpy for working with vectors and matrices. We will explore how to create them, how to manipulate them, and how to use them for efficient numerical calculations using numpy functions.

Learning objectives for this notebook:

  • Student is able to create (multidimensional) numpy arrays from a list of numbers
  • Student is able to use indexing and slicing with (multidimensional) numpy arrays
  • Student is able to iterate over a numpy array
  • Student is able to perform mathematical operations on numpy arrays
  • Student is able to use functions for creating arrays (eg. np.zeros(), np.linspace(), np.random.random()
  • Student is able to use numpy functions for vectorized calculations
  • Student is able to demonstrate the speed increase of vectorized calculations using time()

Data in Python: Loading, Plotting, and Fitting

In this notebook, we will explore how to load and save datafiles in Python using Numpy, and how to plot and explore data and functions with a library called Matplotlib.

Learning objectives for this notebook:

  • Student is able to load data from ASCII text files
  • Student is able to save data to ASCII text files
  • Student is able to generate plots of data and functions
  • Student is able to produce plots with labels, grids, horizontal and verical lines, and legends
  • Student is able to save plots as high-quality PDF files
  • Student is able to use matplotlib to create a plot that enables active zooming
  • Student is able to fit a model to data using "fitting by hand"
  • Student is able to use curve_fit to perform a least-squares fit of a model to a dataset
  • Student is able to calculate the statistical error in the parameters of a least-squares fit

Developing and testing algorithms

In the previous lectures we focused on implementing small pieces of code to perform specific well-defined tasks for you. However, in general, computer programs can perform highly complicated data processing and can comprise of millions line of code (not in this course though). Usually, we say that a computer program is an *algorithm*, i.e., it is "a finite sequence of well-defined, computer-implementable instructions, typically to solve a class of problems or to perform a computation". In order to develop these codes computer scientists use what is called algorithmic thinking. In essence, algorithmic thinking is the process of solving a problem in a systematic way. In this lecture notebook we develop some algorithmic thinking and go through the basic steps that are required to make a computer program from scratch.

Learning objectives for this notebook:

  • Student can design an algorithm in a systematic way
  • Student can implement a algorithm in Python
  • Student can test algorithm outcome
  • Student can benchmark algorithm computational speed

Additional Programming Concepts in Python - Optional

In this notebook, you will learn about additional programming concepts in Python. They are not part of the learning objectives of the course, but you may run into them at some point, or wonder what they are, or find them fun and useful if you already have some programming experience.

The topics we will cover include:

  • More about data types
  • More matrix routines
  • Mutable objects and "call by reference"
  • Interactive plots with ipywidgets
  • More about functions

Good coding practices

In this notebook, we outline some example of "good coding practices": things you should do to make your code robust and understandable to others.

Learning objectives:

  • Student is able to write code that has a clear and understandable structure
  • Student is able to write code with descriptive variable names
  • Student is able to write code with explanatory comments
  • Student is able to write code that avoids "hard coding"

Numerical Differentiation

In this notebook, you will explore taking numerical derivatives and implementating various interpolation schemes in one and two dimensions.

Learning objectives: After finishing this notebook, you should be able to:

  1. Write a function to calculate the numerical derivative of an arbitrary function using both the forward difference and the central difference approximation
  2. Interpret the error made in central and forward numerical derivatives
  3. Calculate the numerical derivative of a dataset

Numerical Integration

In this notebook, you will implement the different techniques for the numerical computation of integrals in python and explore aspects of numerical integration.

Learning objectives: After finishing this notebook, you should be able to:

  1. Implement integration of a function with the trapezoidal rule
  2. Implement integration of a function with Simpson's rule
  3. Calculate the integral of a numerical dataset

Root finding

In this lecture you will implement two techniques to determine the root of a non-linear function of one variable. The root of a linear functions can be solved analytically. For non-linear functions (e.g. sin$x = x/2$) this is sometimes possible, but in the most general case it is hard to write down a simple solution (sometimes a series expansions give you an analytical answer, but still this would require a computer to calculate it with the appropriate accuracy for you). In these cases you need a computer to do the job for you. In this notebook we will show you two methods that can be implemented to find the roots of a function of one variable, i.e., $f(x)=0$. In general, the sought solution can be multi-dimensional, which makes it more difficult to solve, but also similar methods can be implemented in that case. Root solving methods also can be used for finding the extrema of a function. Instead of solving for the roots of $f(x)$, solving the roots the derivative $f′(x)$ function, yields the local or global extrema of the function $f(x)$.

Learning objectives: After finishing this lecture, you should be able to:

  1. Find the root of a one-variable function with the bisection method
  2. Find the root of a one-variable function with Newton's method

Linear algebra

In this lecture, you will implement linear algebra techniques in Python. You will see that for solving physics problems using linear algebra, the most complicated part is to translate the physical problem into a matrix-vector equation. When put in a standardized form, many linear algebra tools are at your disposal in the numpy package.

Learning objectives: After finishing this lecture, you should be able to:

  1. Solve a set of linear equations
  2. Know when a set of linear equations can be solved
  3. Determine eigenvalues and eigenvectors of a matrix

TN2513 Lecture 7: Basics of the Discrete Fourier Transform

In this lecture, we will explore how the Fast Fourier Transform works in python, with an emphasis of how to use it, what it does, and how to interpret the data it produces.

At the end of the notebook, we will also explore an example of using the FFT to find a weak singal in a noisy dataset.

Learning objectives: After finishing this lecture, you should be able to:

  1. Calculate the FFT of a dataset
  2. Calculate the correct frequency vector for the transformed data
  3. Convert the fourier-transformed data into a physically meaningful power spectrum with the correct units
  4. Use the FFT to determine the frequency and amplitude of weak periodic signals in noisy data

Applications of the Fourier Transform

In the previous lecture notebook, we looked into detail about how the 1D FFT works in Python, and saw an example of using the FFT to detect a weak sinusoidal signal in a noisy dataset.

In this lecture notebook, you will explore the application of the 1D FFT for filtering signals, and also learn about the 2D FFT and and application of it in calculating diffraction patterns.

Learning objectives: After finishing this notebook, you should be able to:

  1. Use the FFT to filter numerical data
  2. Interpret artifacts in your filtered data arising from the FFT
  3. Calculate the 2D Fourier transform of 2D data
  4. Construct 2D images of mask patterns and calculate the far-field diffraction pattern

Random numbers

In this lecture, you will implement random numbers in Python and learn how to make computations with them. First we characterize the statistical properties of random numbers by calculating the mean and standard deviation. Second, we perform numerical integration with random numbers.

Despite the fact that random numbers are not well defined, they vary between realizations, you can make calculations with them just as 'exact' as with normal numbers.

Learning objectives: After finishing this lecture, you should be able to:

  1. Make random numbers and know how to analyze them
  2. Implement Monte Carlo integration

Monte Carlo simulations

In this lecture, you will implement Monte Carlo simulations in Python.

  1. Implement Monte Carlo simulation to model physical systems

Ordinary Differential Equations Part 1

In this lecture, we will explore numerical methods of solving ordinary differential equations (ODEs).

Learning objectives: After completing this lecture, you should be able to:

  1. Implement your own code for numerical integration of a first order ODE using Euler's method
  2. Implement numerical integration of coupled and second-order differential equations
  3. Identify failure of the convergence of Euler integration
  4. Implement your own code for second-order and fourth-order Runge-Kutta integration of ODEs
  5. Implement solutions of ODEs using the solve_ivp() routine from the scipy library
  6. Apply numerical integration to a physical problem and interpret your results

Ordinary Differential Equations Part 2

In this lecture, we will explore applying the techniques for solving initial value problems that we learned in lecture 11. We will also learn how to use numerical integration of ODEs to solve boundary value problems.

Learning objectives: After completing this lecture, you should be able to:

  1. Use numerical integration to find the steady-state of the driven damped Harmonic oscillator
  2. Solve boundary-value problems using the "shooting method"
  3. Use numerical integration to solve problems with nonlinear damping

Partial Differential Equations 1: Boundary Value Problems

In this lecture, we will explore numerically solving partial differential equations (PDEs) that have the form of boundary value problems using various techniques of relaxation.

Learning objectives: After completing this lecture, you should be able to:

  1. Implement the Jacobi and Gauss-Seidel methods of solving PDEs
  2. Speed up calculation by implementing successive over-relaxation (SOR)
  3. Solve for the electrostatic potential $\phi$ for 2-dimensional (2D) problems using SOR including fixed charges
  4. Calculate the surface charge density on the surfaces of a simulation of the 2D poission equation in physical units (C/m$^2$)

TN2513 Lecture 14: Initial Value Problems with PDEs

In this lecture, we will explore numerically solving partial differential equations (PDEs) that have the form of an initial value problem using the forward-time centered-space (FTCS) and the spectral method.

Learning objectives: After completing this lecture, you should be able to:

  1. Implement the FTCS method for solving initial-value PDEs
  2. Solve and interpret the results from simulations of the 1-dimensional diffusion equation using FCTS
  3. Identify the failure of the FTCS method for solving some problems due to numerical instability
  4. Implement the spectral method for solving initial-value PDEs
  5. Solve and interpret the results form simulations of the wave equation using the spectral method