Numerically Solving Partial Differential Equations Michael Lavell Department of Applied Mathematics and Statistics Abstract The physics describing the fundamental principles of fluid dynamics can be written in the form of partial differential equations. This study approximates each of the three types of PDE s using finite difference methods and solves each type in parallel on Hyades, a supercomputer at UC Santa Cruz. A 1D advection equation and a 2D diffusion equation are solved with MPI and a 2D poisson equation is solved with openmp. 1 Introduction Partial differential equations are a formulation of how a set of variables and their derivatives behave with respect to each other. When these variables represent properties of a given system, it is possible to predict how the system will behave over time. For example, if u is pressure, the advection equation u t + au x = 0 represents the propagation of pressure through a 1D spatial domain with finite propagation speed a. Achieving exact solutions for such problems is incredibly difficult and sometimes impossible. However, there are tools that allow us to build numerical models capable of solving partial differential equations, and subsequently, predict how systems evolve. In this study, we use the finite difference scheme to discretize the spatial domain, temporal domain, and the three different types partial differential equations. Using the parallel libraries, MPI and openmp, we can split the computational work load between a team processors on the Hyades cluster. This allows us to quickly pursue a numerical solution of a given problem. 1
2 Pursuing Numerical Solution to PDEs The finite difference scheme provides a means of discretizing the spatial and temporal domains of the system. Using this method, we construct numerical schemes that approximate the solution at a given point in time and space and explicitly define the advancement of a single cell for the three types of partial differential equations: hyperbolic, parabolic, and elliptic. U n i u(x i, t n ) The term u(x i, t n ) generally represents a state variable, such as pressure or temperature, however, in the case of the poisson equation it is a time invariant variable, such as gravitational or electrostatic potential. 2.1 Hyperbolic Equation We solve the 1D advection equation u t + au x = 0 defined on Ω = [x a, x b ], t 0 with the discretization x i = x a + (i 1 ) x, i = 0, 1,..., N + 1 2 t n = n t, n = 0, 1,..., M establishing a cell centered configuration with guard cells. The advection equation describes the finite propagation of information through space. Using a strict set of cells at t = n, we can approximate a cell at t = n + 1. We use the Centered, Downwind, and Upwind schemes to study the advection of a continuous and discontinuous profile. Upwind: U n+1 = Ui n a t x (U i n Ui 1 n ) Downwind: Ui n+1 = Ui n a x (U i+1 n U i n) Centererd: Ui n+1 = Ui n a t x (U i+1 n U i 1 n ) The Upwind scheme is the only stable scheme as illustrated in Figure 1. Using MPI, we split the 1 dimensional array Ui n between a team of processors in order to minimize computation time. Data must be passed between processors since the boundary cells on a processor require the boundary cells from the next processor in order to advance. 2.2 Parabolic Equation The parabolic type PDE we will consider is the diffusion equation u t = κ 2 u where κ is the diffusion coefficient. Derived using finite difference methods, the Forward Euler method explicitly defines the solution Ui n+1 at the next time step. Ui,j n+1 = Ui,j n + t +U n i,j+1 h 2 Ui+1,j n 4Ui,j n +Ui 1,j n Ui,j 1 n 2
Figure 1: The Upwind scheme is stable. The shock maintains it s sharp profile and the continuous sine profile holds it shape after a full period. The Downwind and Centered schemes are unstable and are solved for only 10 iterations to show divergent behavior. 3
The discretized 2-dimensional spacial domain is split evenly along the y-axis and distributed evenly amongst the available number of processors. Advancing a cell on the boundary, U i,k where k = (N + 2)/(number of processors), requires U i,k+1. Since this is true for i = 0, N + 1, an array is passed between processors and when implemented with MPI it is necessary to define a new MPI TYPE. A processor in the middle of the domain will send and receive the new data type from both the processor in front of and behind it. The diffusion equation evolves the initial profile given by ( ( )) x2 u 0 (x, y) = A exp 2σx 2 + y2 2σy 2. This can be interpreted as a pressure distribution with a region of high pressure in the middle and low pressure on the boundary. As expected the profile diffuses with time. See Figure 3. Figure 2: 2D diffusion initial profile. Due to poor communication when running on more than two processors, the diffusion solver did not scale well. Figure 4 shows the incorrect solution for the diffusion solver on 8 processors. 2.3 Elliptic Equation The third equation we solve is the Poisson equation. 2 u = f 4
Figure 3: Diffusion equation. The evolution of a normal distribution of a state variable over 500,000 iterations. On two processors: 9.71093750 seconds. Figure 4: Broken diffusion solver due to poor communication between processors. 5
Unlike the parabolic or hyperbolic PDE s, the elliptic PDE is independent of time. In fact, it is the steady state solution of the diffusion equation. Time invariant properties such as gravitational potential and electrostatic potential are described using the poisson equation: Gravitational Potential: 2 φ = 4πGρ Electrostatic Potential: 2 φ = ρ ɛ Solving these equations for the scalar gravitational and electrostatic potential field requires knowing the mass distribution and charge density distribution respectively. We will study a system with two point masses and a system with a single mass that varies with radius. Using a five point stencil for 2 u, we discretize the poisson equation. Let h = x = y, then U i+1,j 2U i,j + U i 1, x 2 + U i,j+1 2U i,j + U i,j 1 y 2 = f i,j U new = 1 4 [U i,j 1 + U i,j+1 + U i+1,j + U i 1,j h 2 f i,j ]. This scheme, run in parallel with openmp, achieves convergent solutions for both initial scalar fields. Figure 5: Poisson equation. Potential well of a mass or charge distribution that decreases in density as radius increases. 6
Figure 6: Poisson equation. Potential well of two point masses/charges. 3 Code Execution To set up the diffusion and advection solvers edit the runtime parameters located in the main section of pyrun pde.py in the /pyrun directory. Then, execute with python pyrun pde.py This will make clean, compile, and execute the Fortran code located in objects and plot the results. The poisson solver is located in the /gravity directory. Choose the desired spatial discretization and the source term in the main section of pyplot.py and match these parameters with the equivalent variables initialized in grav solver.f90. Enter the following to compile, execute, and plot. gfortran -fopenmp grav solver.f90 -o solver.exe./solver.exe python pyplot.py 7
4 Discussion Numerically solving partial differential equations using the finite difference approximation produces a scheme that is easily parallelizable and scales well. We look at the 1D advection equation partitioned into N = 256 cells for a brief scaling analysis. Running on two processors the program required 15.3 seconds to converge to the true solution. Running on eight processors the same calculation required 7.3 seconds. Unlike the diffusion solver, the advection solver efficiently communicates information between processors. Large computational fluid dynamics codes require efficient communication, convergent numerical schemes, and appropriately managed computer architectures. Solving partial differential equation s in parallel on supercomputers is a vital first step towards building robust numerical models. 8