Parallel Poisson Solver Example
At the end of this class, you should be able to:
- Discretize and solve a simple elliptic PDE
- Define the initial and boundary conditions to the problem
- Implement a distributed memory parallelization
- Describe the typical workflow for HPC computations
Two-dimensional heat conduction problem
In this section we will solve a practical problem using parallel computing. Say your task is to study heat conduction in a square plate, in a two-dimensional space in a steady state, in which all four sides are at different temperatures and a heat source is located at the centre of the domain. As you may recall, this problem is mathematically described by the Poisson equation:
where
Solving the equation above numerically will only asymptotically tend towards the analytical solution. The discretized solution of the equation will give an approximation of the analytical solution.
This lesson is meant to provide an overview of the solution procedure; therefore the intricate details of the numerical methods will not be covered. Instead, we refer the interested reader to CFD Python: 12 steps to Navier-Stokes for additional information on the numerics used herein.
For the solution of the above PDE, we take the following steps:
- Discretize the spatial domain
- Transform PDE to algebraic ODE
- Define boundary conditions
- Solve the algebraic equation until convergence
Domain discretization
In this first step, we discretize the plate by dividing up the domain into a set of discrete grid points. Here, out of simplicity, we will assume that the grid points are equally spaced in both directions, thus we define the spacing between then by
This process can be thought of as the grid generation step. This step is not trivial in general and can be computationally very expensive depending on the size and complexity of the geometry. In our illustrative case, we have 6 interior grid points and 14 boundary grid points.
Transform PDEs to algebraic equations
The original equation that governs the physics of the problem is a second-order partial differential equation of the form:
Many techniques are used to discretize the governing equations, and entire textbooks are devoted to the theory behind it. Here we use the very easy finite difference method to write the PDE as a set of finite differences:
One can then rearrange the above equation to find the value of temperature at each grid point
where
Apply boundary conditions
As the PDE corresponds to a boundary-value problem, the temperature values at the boundaries need to be provided in order to solve the governing equation. We consider that the rectangular plate is subject to a constant temperature boundary condition (Dirichlet) and has a heat source (input units in
In this very simple case, the temperature is set on all sides of the domain. Since many grid points are located on the boundary, the temperature of these grid points are known. Following the schematic figure above, one might say:
Given the four point stencil used in the problem, the corner points do not need to be defined as they do not enter into the solution of the interior points.
Solve the system of linear equations until convergence
The solution of the set of discretized governing equation can be represented by a linear system of equations of the form:
Where
- Direct methods: such as the Gauss-Jordan elimination method based on matrix inversion technique
- Iterative methods: such as the Gauss-Seidel method or Jacobi iterative method
In this example we use the Jacobi iterative method. The solution to the linear system of equations will give us the temperature at all internal grid points
If an iterative method is used to solve the linear system of equations, a convergence criterion must be chosen to STOP the iteration loop. In this very simple case, as the governing equations admit an analytical solution, we could define a convergence criterion based on the error from the exact solution. However, in most general cases, the governing do not admit an analytical solution.
The most common technique to monitor iterations, and ensure that convergence is reached is by checking the residual. Once the
Where
Parallelization strategy
As mentioned earlier, the matrix
In the following, we provide a parallelized code, written in the Python programming language, to solve the 2D Poisson equation as described above. Learners without a basic knowledge of Python should consider the following a tutorial. In the code, the user specifies the following input parameters (highlighted in grey in the code below):
- Size of the domain: x_length, y_length.
- Number of interior grid points: x_node, y_node.
- Boundary conditions: T_top, T_right, T_bottom, T_left.
- Strength of the source
: Q_source - Thermal conductivity
: therm_cond - Maximum number of iterations: iterations
- Tolerance for the convergence check: epsilon
The parallelized code can be downloaded from the accompanying ARC4CFD git repository or copied from below. This code can be run on the HPC clusters or locally. The parallelization is achieved by using a distributed memory architecture with a message passing interface (MPI).
Click HERE to see the code
Since the domain size is user defined, the solution matrix, T_final, can have more grid points in either the rows (
Code upload on cluster and execution
Say you have written your code using your preferred text editor on your laptop. You now want to carry out some scaling tests on the cluster. The first step would be to transfer the code to your account on the remote machine. This is usually done by Secure File Transfer Protocol (SFTP). SFTP works in a very similar way to SSH and it allows you to put or get stuff from your remote account. Open a terminal window where your code is and type:
Upon login your terminal will, once again, change and should look something like this:
You can now navigate to the directory where you want to upload your code to, and simply type:
If you plan to run anything more than a trivially small case, you should request an interactive node. After loading the required python module using the command we learned in section 1.3 module load python/3.11.2
, you are now ready to test your code on the cluster. The code can be executed using the following script:
Results format and benchmark
Performance of the code is displayed in the console with containing following information:
- Grid points: (total number of grid points including boundary grid points).
- Cores: (computational cores used for the execution).
- Computational time: (elapsed wall-clock time for execution).
A result file in csv format is also generated in the working directory, containing the temperature field solution. It has a naming format as follows: Nodes_Cores.csv.
In the result file, the top and bottom rows, and the left and right columns correspond to the respective imposed boundary conditions.
As a benchmark, we solved the 2D poisson for 240 grid points in
Here are a few things to highlight:
- We first notice that parallelization will improve the performance of our code to a great extent.
- We also notice that increasing the number of processors by a factor of 2 does not cut the computational time required by a factor of 2. This is expected, based on what we learned above, as increasing the number of processors also increases the amount of communication required between them.
- The number of iterations to reach the desired tolerance does not change.
Visualization of results
Since this is a small problem, we can download the results to our local machine for postprocessing. Once again we will use the SFTP protocol to do that. After you have successfully logged into the cluster using SFTP, type the following command:
Once downloaded to the local machine, the results can be visualized using the software of preference. In the following we make the example of a simple Python script to plot the temperature contours:
Here is the output of the Python script:
Having finished this class, you should now be able to answer the following questions:
- What is the 2D Poisson equation?
- Why is the 2D Poisson equation relevant for CFD applications?
- What are the important steps towards the numerical solution of the 2D Poisson equation?
- How do I run a parallel job on the cluster?