field

Relaxation Method

Introduction

It looks like I will be quite busy for a while so I won’t have much time for the blog. I might only post easy things like this one for a while… anyway, here it is, with only brief explanations and the JavaScript code. I didn’t feel the need and didn’t have the time and patience to write a C++ program for this topic. It’s just too simple and besides, it’s nice to see the code action in the page.

Short Description

Links

So, this post is about the relaxation method combined with a multigrid method applied on the Laplace equation.

The methods are easy and I suppose Wikipedia pages are a good start, but here there are several other links for help, I just googled them, you can find many more on the subject. First, a paper1. A web page is here2 and another one here3.

Very short theory description

You can find the Laplace equation (or more general, the Poisson equation) in various topics, for example originating from Gauss law if you use the electric potential instead of the electric field, or in the heat equation in a steady state when the time derivative drops out (also see: diffusion equation).

What is nice about this equation, apart from the superposition principle is that the solutions are harmonic functions which means that the value in a point is the average of the points around it (for a more rigorous explanation, please see the Wikipedia page). This allows us to use a relaxation method to solve it, although there are faster methods that one could find (for example, one could use Fourier transform). Despite this, the method is easy to understand and can be a start for other methods of solving the equation.

The method is very easy, first the equation is discretized using a finite difference method then one iteratively averages the points in the discretized space. This can be coupled with a multigrid method by starting out with a coarse grid first.

Code in action

That should be enough theory, here is the result in action, on a very simple model on a square with the boundary condition of two sides with the field with value 1 and the other two with value -1:

Canvas is not supported by your browser!

The code just iterates relaxation until the difference between the old solution and the new one is under a certain threshold, then the resolution is increased and the same is done again until it reaches a certain resolution, then it starts again.

It could be instructive to change the code to not use the multigrid method, but instead to start from the beginning with the smallest mesh size.

The Code

Here is the model class:

There is not much to say about it apart of what I already described. I guess you could change it with some other model…

Here is the Relaxation class:

The code uses the Gauss-Seidel method instead of the slower Jacobi method. As a note, the interpolation method I made in the MakeGridSmaller method is very crude, it shifts the values up and to the left than they should be, one can do much better and it should be done better in ‘real life’ code. I didn’t have patience to implement something better in this short time I allocated for this post.

In order to be fully functional, the code needs also functions to create the objects, successively apply the methods and display the results, and here it is:

Everything is packed into an anonymous closure and that’s about it:

Conclusion

Maybe I was too brief on this subject but it’s weekend… as usual, if you find something wrong, please point it out.

There are many things one might try to improve such algorithm, for example the interpolation when switching to a finer mesh could be improved and using over-relaxation will speed up the convergence.


  1. Relaxation Methods for Partial Differential Equations: Applications to Electrostatics by David G. Robertson 
  2. Poisson’s Equation and Relaxation Methods part of a Computational Physics lecture 
  3. Partial differential equations Numerical Methods, Natural Sciences Tripos 1B by Stuart Dalziel 
Print Friendly, PDF & Email

Leave a Reply

%d bloggers like this: