Y2 Computational Physics Laboratory Exercise 3 The Relaxation Method! Solving Laplace’s Equation Aims and objectives: To introduce students to a useful method of solving problems dealing with electrostatic potentials. To acquire experience using two-dimensional arrays in C++ To acquire experience using iterative techniques in programming To further build on fluency programming in C++ 1 mark given for overall presentation Duration: 3 weeks Hand-in date: At the end of session in week beginning November 28th. Introduction The relaxation method is a computational way of solving so-called elliptic partialdifferential equations. In this exercise we shall be concentrating on one of the simpler equations, namely Laplace’s equation, 2 V 0, 2 2 V V 0, 2 2 x y (2 - dimensions). (1) In the electrostatic case, V (x, y) is the electrostatic potential. We begin by dividing up the space we are considering into a discrete grid and look only at the points where the gridlines cross - the mesh points. x x y y V(x,y) Dividing up the area into discrete points. Note that the shaded points are those at the border. We can then use approximations for the second derivatives of V with respect to x and y to find each element in the grid in terms of its adjacent neighbours. We perform a Taylor expansion of V around the point (x, y): 1 2V 2 V x x 2 x 2 x 1 2V 2 V V( x x, y) V( x, y) x x 2 x 2 x 1 2V 2 V V(x, y y) V( x, y) y y 2 y 2 y V(x x, y) V( x, y) V(x, y y) V( x, y) (2) 1 2V 2 V y y 2 y 2 y On the assumption that x = y, we may add these four equations together to get, after using Equation 1: V(x, y) 14 V(x x,y) V(x x,y) V(x, y y) V(x,y y) (3) We can see that this final expression for the potential, V (x, y), is simply the average of the four adjacent grid points. Our task, therefore, is to ensure all points on the grid satisfy this condition. Coding the problem You will be making use of Equation 3 to solve Laplace’s equation for the potential on a square grid. You should use a two-dimensional array to store the values of V in your program. Think of a two-dimensional array as being like a two-dimensional matrix. Here, each element of the array will correspond to one of the points on the mesh grid, and the value stored in that array element will be the potential for the point in question. Two-dimensional arrays are declared in C++ like this: float V[20][20] Here, we declare a real array, V, of size 20 by 20. The following is an example of how to address an element of the array in a program: V[0][0] = 0.0 This will assign the value 0.0 to the element (0,0) [or, with reference to our grid, x = 0, y = 0]. In your code, you will need to loop over the elements of the array. For example, you may address each element as V[i][ j], where i and j are integer indices. Equation 3 then becomes: V[i][ j] = (V[i+1][ j] + V[i 1][ j] + V[i][ j+1] + V[i] [j1]) / 4.0 (4) The fact there are two indices should suggest you will need to use nested loopsone loop inside another. After declaring the variables, the first task of the program will be to specify the boundary conditions: the known potentials around the edges of the array (shown as the shaded points in the figure above). These potentials remain constant throughout, and are the only factors determining our solution. Away from the edges, the initial values of V can be chosen quite arbitrarily, for example by setting them all equal to 1.0. Once this is done one can begin iterations, using loops, to seek a solution. Each iteration entails looping through all the points on the grid, apart from those on the boundary, setting each to the average of its four nearest neighbours, as per Equation 4. This is repeated until all the values have settled down sufficiently, usually defined as being when the greatest change made on a single pass is within a certain tolerance. There are of course problems with this method, not the least of which is our having to work with an area of finite size. Many physical problems apply to areas infinite in size, one example being the electric field surrounding a point charge. A further disadvantage is our knowing the potentials only at discrete points within the area. Despite these limitations, relaxation is still a useful tool in physics, and other sciences such as chemistry. Here, you will tackle two problems, which differ only in the boundary conditions. Problem 1what is required? Write a C++ program to solve Laplace’s equation on a square array of size 20 points by 20 points, with equal separations in the x and y directions. The boundary conditions are such that V = 0 along the line x = 0, and V = 1.0 along the line x = xmax. Along the lines y = 0 and y = ymax the potential should increase linearly from 0.0 to 1.0. You should continue to iterate (i.e., repeat the loops) until the change made to any 6 element in a single pass is less than a given tolerance (e.g. 10 of the highest potential). It is a good idea to read in the tolerance at the start of your program. You can then check what happens if the tolerance is too large. Make sure you adjust all the internal points on each pass, but remember to leave those on the borders unaltered. Your program should produce a count of the number of iterations required to converge to the required tolerance. When you are satisfied that the points are of sufficient accuracy, write a file containing the final potentials. You can then generate a contour plot of the results in Excel (you will need to select surface chart in the Chart Wizard). Excel will need a file in which the data are stored in 20 columns, each with 20 rows, i.e., in the same pattern as the grid. (See the link below for more help.) Link to ‘Creating a Surface chart’ Your write-upwhat is needed? 1. You should include a printout of your code, and a description of it. Detail should be sufficient to allow the marker to easily follow, and understand, the logic and function of the code. The code itself should be well commented. [3 marks] 2. You should include a contour plot of the final potential. Mark on the contour plot, by hand, the field lines (with directions) of the potential. You will need to recall how electric field is related to electric potential. [2 marks] Problem 2what is required? Modify your program to obtain the potential for a new set of boundary conditions. The new conditions are as shown in the figure below. The potential is zero along the lines x = 0 and y = 0 (i.e., the x and y axes). (x max,ymax ) (ymax ,0) (0,x max) (0,0) The initial border potentials for the second problem The potential on the other two sides increases linearly from zero to a common potential of 1.0 at (xmax , ymax). Your write-upwhat is needed? 3. You should include a contour plot of the final potential. Again, mark on the contour plot, by hand, the field lines (with directions) of the potential. [2 marks] 4. For the above boundary conditions, Laplace’s equation can actually be solved analytically. Try to deduce this analytic form from your results, and check that it is indeed a solution to Laplace’s equation. (Hint: examine the contour plot and think about the analytic form of the curves.) [2 marks]