Write an iterative real-space Poisson solver in Python/C

Similar documents
1 2 (3 + x 3) x 2 = 1 3 (3 + x 1 2x 3 ) 1. 3 ( 1 x 2) (3 + x(0) 3 ) = 1 2 (3 + 0) = 3. 2 (3 + x(0) 1 2x (0) ( ) = 1 ( 1 x(0) 2 ) = 1 3 ) = 1 3

Introduction to Matplotlib: 3D Plotting and Animations

Introduction. 1 Measuring time. How large is the TLB? 1.1 process or wall time. 1.2 the test rig. Johan Montelius. September 20, 2018

Partial Differential Equations II: 2D Laplace Equation on 5x5 grid

PARALLEL METHODS FOR SOLVING PARTIAL DIFFERENTIAL EQUATIONS. Ioana Chiorean

June 10, 2014 Scientific computing in practice Aalto University

Lab # 2 - ACS I Part I - DATA COMPRESSION in IMAGE PROCESSING using SVD

3.1. Solution for white Gaussian noise

1.2 Numerical Solutions of Flow Problems

Sparse Grids. Lab 1. Discretization

Introduction to Multigrid and its Parallelization

Parallel Poisson Solver in Fortran

AMS527: Numerical Analysis II

Efficient Use of Iterative Solvers in Nested Topology Optimization

An Investigation into Iterative Methods for Solving Elliptic PDE s Andrew M Brown Computer Science/Maths Session (2000/2001)

What is Multigrid? They have been extended to solve a wide variety of other problems, linear and nonlinear.

Flow in an Intake Manifold

CSI33 Data Structures

Physics 306 Computing Lab 5: A Little Bit of This, A Little Bit of That

Tutorial 1. Introduction to Using FLUENT: Fluid Flow and Heat Transfer in a Mixing Elbow

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

Programming in C. Pointers and Arrays

Regularity Analysis of Non Uniform Data

(T) x. Casts. A cast converts the value held in variable x to type T

Exercise 2: Hopeld Networks

ECE264 Fall 2013 Exam 1, September 24, 2013

BMVC 1996 doi: /c.10.41

An Introduction to Numerical Analysis

Arrays and Pointers in C. Alan L. Cox

f xx + f yy = F (x, y)

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

Numerical Dynamic Programming. Jesus Fernandez-Villaverde University of Pennsylvania

Algorithms, Probability, and Computing Special Assignment 1 HS17

Information Science. No. For each question, choose one correct answer and write its symbol (A E) in the box.

Assignment 5 Using Paraguin to Create Parallel Programs

Intersection of sets *

CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014)

As stated earlier, the declaration

Development of a Maxwell Equation Solver for Application to Two Fluid Plasma Models. C. Aberle, A. Hakim, and U. Shumlak

High Performance Computing: Tools and Applications

Math 3316, Fall 2016 Due Nov. 3, 2016

Today. Gradient descent for minimization of functions of real variables. Multi-dimensional scaling. Self-organizing maps

residual residual program final result

Domain Decomposition: Computational Fluid Dynamics

Lab 2: Support Vector Machines

Comparison of different solvers for two-dimensional steady heat conduction equation ME 412 Project 2

Benchmarking of the linear solver PASTIX for integration in LESCAPE. Lucas Lestandi

Paraguin Compiler Version 2.1

FOURTH ORDER COMPACT FORMULATION OF STEADY NAVIER-STOKES EQUATIONS ON NON-UNIFORM GRIDS

HMC CS 158, Fall 2017 Problem Set 3 Programming: Regularized Polynomial Regression

Numerical methods in physics

J. Weston, A. Gammerman, M. Stitson, V. Vapnik, V. Vovk, C. Watkins. Technical Report. February 5, 1998

CS Software Engineering for Scientific Computing. Lecture 5: More C++, more tools.

Machine Learning. Topic 5: Linear Discriminants. Bryan Pardo, EECS 349 Machine Learning, 2013

Learning and Generalization in Single Layer Perceptrons

Introduction Slide 1/20. Introduction. Fall Semester. Parallel Computing

SPECIAL TECHNIQUES-II

Introduction to Parallel Programming Message Passing Interface Practical Session Part I

Introduction to Parallel Programming

MPI Casestudy: Parallel Image Processing

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Figure (5) Kohonen Self-Organized Map

Machine Learning: Think Big and Parallel

Introduction to Languages for Scientific Computing, winter semester 14/15: Final Exam

Using graph theoretic measures to predict the performance of associative memory models

Numerical Methods to Solve 2-D and 3-D Elliptic Partial Differential Equations Using Matlab on the Cluster maya

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Performance Comparison between Blocking and Non-Blocking Communications for a Three-Dimensional Poisson Problem

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

Exercise set #2 (29 pts)

INTRODUCTION TO MATLAB, SIMULINK, AND THE COMMUNICATION TOOLBOX

Embedding Python in Your C Programs

Introduction to C An overview of the programming language C, syntax, data types and input/output

User-Defined Function

Structured-grid multigrid with Taylor-Hood finite elements

Course Information and Introduction

Biometrics Technology: Image Processing & Pattern Recognition (by Dr. Dickson Tong)

Tilings of the Euclidean plane

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

Homework # 1 Due: Feb 23. Multicore Programming: An Introduction

Finite Element Methods for the Poisson Equation and its Applications

Python for C programmers

Issues with Curve Detection Grouping (e.g., the Canny hysteresis thresholding procedure) Model tting They can be performed sequentially or simultaneou

Practical Introduction to Message-Passing Interface (MPI)

Telecommunication and Informatics University of North Carolina, Technical University of Gdansk Charlotte, NC 28223, USA

6 Initializing Abstract Models with Data Command Files Model Data The set Command Simple Sets... 68

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Arrays and Pointers (part 2) Be extra careful with pointers!

A tutorial for blockcluster R package Version 1.01

CUDA. Fluid simulation Lattice Boltzmann Models Cellular Automata

Introduction to Computing Systems Fall Lab # 3

Lecture 2 September 3

Kurt Schmidt. October 30, 2018

A Gentle Introduction. Optimisation

Arrays and Pointers (part 2) Be extra careful with pointers!

June Wesley, Cambridge Univ. Press,

Pointers and scanf() Steven R. Bagley

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

Phys Techniques of Radio Astronomy Part 1: Python Programming

The Python interpreter

Transcription:

Write an iterative real-space Poisson solver in Python/C Ask Hjorth Larsen asklarsen@gmail.com October 10, 2018 The Poisson equation is 2 φ(r) = ρ(r). (1) This is a second-order linear dierential equation which in physics relates an electrostatic potential φ(r) to a continuous charge distribution ρ(r). The goal of this tutorial is to write a Python code which calls a C extension to solve the Poisson equation in two dimensions on a uniform real-space grid. To keep life simple, we will do this for zero boundary conditions 1, and only for rectangular domains with equal spacing in the x and y directions. Grids and Laplacian Dene rst a simple 1D grid (e.g. using np.linspace) and a function whose second derivative you know analytically. How can you calculate the second derivative using nite dierences? Next we represent a function f(x, y) as a two-dimensional array f ij f(x i, y j ), where x i = x 0 + ih and y j = y 0 + jh dene a uniform rectangular grid with spacing h, and where i = 0,..., I and j = 0,..., J are integer indices. Choose a 2D grid and a suitable charge distribution ρ(x, y) whose potential you might want to calculate later on, then plot it with imshow from matplotlib. It may be helpful to use numpy functions like x = np.linspace(start, stop, npoints) 1 It is common to solve with zero boundary conditions and later correct the boundary conditions by pre/postprocessing. This is simple because the equation is linear. 1

X, Y = np.meshgrid(x, y) Now X and Y are useful for dening 2D functions like X**2 + Y**2. Interactive interpreters and Jupyter notebooks are good for playing around and trying things, but if some day we would like to make our solver available as a Python module, we should write it as a Python module. Put the code into a Python le and make sure things work as a standalone code. The Laplacian 2 can be discretized onto our grid by locally approximating the second-order derivatives from the neighbouring grid points: ( ) 2 2 φ(x, y) = x 2 + 2 y 2 φ(x, y) 1 h 2 [φ i+1,j + φ i,j+1 + φ i 1,j + φ i,j 1 4φ ij ]. (2) This is called the 5-point stencil. Each value is evaluated from the 5 closest grid points. It approximates the Laplacian within an error of O(h 2 ), so we say it is a second-order stencil. 2 I. Implement a function which calculates the Laplacian of a function f ij on the grid. Clearly the stencil only works on the interiour of the grid. Recall that we earlier endeavoured to x the boundary conditions to zero. Poisson solver In our grid representation the Poisson equation reads φ i+1,j + φ i,j+1 + φ i 1,j + φ i,j 1 4φ ij = ρ ij h 2. (3) Isolating φ ij, we dene an iterative map in which the potential φ k+1 ij (k + 1)th step is calculated from that of the kth step according to at the φ k+1 ij 1 4 (φk i 1,j + φ k i+1,j + φ k i,j+1 + φ k i,j 1 ρ ij h 2 ). (4) Lo and behold, as we iterate this enough times, φ k ij matter which starting potential we chose. approaches the solution no Implement a function which iterates (4), then solve the Poisson equation as described above for some chosen density ρ(x, y). 2 By using more neighbours, it is possible to formulate more accurate stencils with order O(h 2n ) where n is the number of nearest neighbours used. 2

If the Poisson solver works, it should be easy to verify that the residual R[φ k ] = 2 φ k (x, y) ρ(x, y) approaches zero. Also, the norm of the residual is a useful measure for whether the calculation has converged. Be sure to make some plots while considering this. Package the code into a function solve(rho, phi, h, epsilon) so the user can provide dierent arrays, grid spacing h, and a tolerance ɛ which controls how well the calculation is converged. This can be done using basic Python loops or using numpy. How much faster is the numpy version? GaussSeidel solver If we loop over i and j in increasing order, we will already have passed (i 1, j) and (i, j 1) by the time we reach (i, j). Hence we already have the new and improved φ k+1 i 1,j and φk+1 i,j 1 of which we can take immediate advantage: φ k+1 ij 1 4 (φk+1 i 1,j + φk i+1,j + φ k i,j+1 + φ k+1 i,j 1 ρ ijh 2 ). (5) This iterative scheme is called the GaussSeidel method. The former is called the Jacobi method. How many iterations does it take to solve the Poisson equation for a given tolerance with GaussSeidel versus Jacobi? The GaussSeidel method will require fewer iterations. We easily wrote the Jacobi update using Numpy arrays, but it is not straightforward to write the GaussSeidel update likewise. Sometimes there is a function that can solve the problem for us, but not always. Indeed there will always be some types of functions that cannot be done as eciently as in C: Complex element-wise mathematical operations on the same array, like np.sqrt(1 + A**2 * (1 + A)) lead to several loops over the same chunk of memory, and would be faster to do in C using a single loop. Any operations where the each element successively depend on each other in a tight loop. In our case, both these things are a problem to some extent. For high-performance computing it is normally ne to write most code in Python, but a few parts will be very performance critical and hence nice to have in a low-level language like C. We now have an excuse to write the GaussSeidel update scheme in C. 3

Calling C from Python There are many ways to call C from Python. The simplest is probably to use the module ctypes. First we need to know how to write a function in C. Here is some inspiration: # include < stdio.h > int multiply_ vector ( double a, double *v, int len ) int i ; for ( i =0; i < len ; i ++) v [ i ] *= a ; int multiply ( double a, double b ) double c = a * b ; printf ( " multiply % f % f --> % f \ n ", a, b, c ); return c ; int main ( int argc, char ** argv ) double x = multiply (2.0, 3.0); printf ( " got % f \ n ", x ); double v [5] = 1.0, 2.0, 0., -1., 42.; multiply_ vector (0.5, v, 5); int i ; for ( i =0; i < 5; i ++) printf ( " % f \ n ", v [ i ]); return 0; This le, which we shall call example.c, can be compiled by gcc example.c. This will produce a binary called a.out which will run and print some things. Write a hello world program in C, then compile and run it. Add a function which computes something simple (e.g., a + b) and verify that it works. In C, arrays are represented as a shape (here two integers I, J) plus the data (here a double* pointer to the rst element, with the n m elements following in immediate succession). 4

Pass a numpy array to C. In Numpy you get elements like array[i, j]. But in C you just have a pointer to a chunk of data (and the shape). How do you get element i, j then? Once we have gured out how the 2D index i, j is mapped to the single index 0 n IJ, it will be possible to loop over arrays and write the solver. Write a GaussSeidel solver in C. 5