Chapter Twenty

2-D Arrays: File I/O, PDEs; Realistic Capacitor

The main purpose of this chapter is to get more experience with arrays, and especially with the large 2-D ones used to solve realistic problems. As is often the case with realistic problems, no analytic solution exists and we must employ a variety of techniques to solve our problem. These include breaking space into a discrete lattice and applying a simple algorithm to solve a partial differential equation (PDE), file I/O, and 3-D visualization of numerical data using gnuplot. The simplicity of the solution follows from the use of large arrays that permit us to manipulate thousands or millions of variables with just a few lines of code.

20.1 PROBLEM: FIELD OF REALISTIC CAPACITOR

Figure 20.1, adapted from a first-year physics text, shows an idealized parallel-plate capacitor on the left. The equal spacing and single direction of the electric-field line (arrows) indicate that the field is uniform. On the right is a photograph of the electric field between the plates of a realistic parallel-plate capacitor, visualized by small pieces of thread suspended in oil. Observe how the field fringes out near the ends and extends beyond the ends of the capacitor. Textbooks usually analyze the ideal capacitor because only it can be treated analytically.

Problem: Determine and visualize the electric field within and surrounding a realistic capacitor, and compare to Figure 20.1.

20.2 THEORY AND MODEL: ELECTROSTATICS AND PDES

Capacitors are devices with the capacity to store electric charge. They usually consists of two conductors of similar shape separated by a small gap, with equal— but opposite–charge on each conductor. Because the electric field, which would otherwise extend over all of space, is condensed to the region between the plates, capacitors are also called condensers.

In spite of real electrical devices being three-dimensional, we model the capacitor, as shown on the left of Figure 20.2, as two wires connected to a battery that keeps the top plate at 100 volts and the bottom one at -100 volts. To permit a solution to this capacitor, we need to contain it in a limited space, which we do by placing it inside a charge-free box made from wires that are “grounded” (kept at 0 volts). After we solve this problem, we may make it more realistic still by enlarging the box so that it has no effect on the field near the capacitor. This is little work for us, but much more work for the computer.

image

Figure 20.1 Left: Electric-field lines betweens the plates of an ideal parallel-plate capacitor. The equal spacing and single direction indicate a uniform field. Right: A representation of the field between the plates of a realistic capacitor. The field tends to “fringe” and extend beyond the ends of the plates.

image

Figure 20.2 Left: A parallel-plate capacitor within a grounded box. A realistic capacitor would have the plates closer together in order to condense the field. Right: A visualization of the electric potential for this geometry. The contours projected onto the xy plane give the equipotential surfaces.

20.2.1 Laplace’s Partial Differential Equation*

Rather than solve for the electric field E(x, y), we solve for the electric potential V (x,y). This is easier because V is a scalar while E is a vector, and because the equations for V are simpler. Insofar as the field E equals the derivative (gradient) of V, the two solutions are equivalent. (Electric-field lines are perpendicular to equipotential surfaces and run from higher to lower potential values.)

Elementary physics texts show how to determine electric-field configurations using Gauss’s law. We skip the mathematical manipulations needed to prove it and just state that the solution using Gauss’s law is equivalent to finding a solution of the partial differential equation (PDE),

image

This is called Laplace’s equation, in honor of the genius who first did those mathematical manipulations. The rounded Ds in (20.1) indicate that the derivatives are partial not ordinary. The reason this is a partial DE is that the potential function V(x, y) is a function of both x and y. We cannot solve the x behavior without simultaneously solving for the y behavior. The real world is full of such equations.

20.3 ALGORITHM: FINITE DIFFERENCES

The simplest method for solving our PDE is illustrated in Figure 20.3 and is known as a finite-difference technique. There we see on the left a grid of x and y values, each uniformly spaced by a step size Δx = Δy = Δ. Rather than trying to find a solution for the continuous range of all possible values of x and y, we try to find the solution only for the finite number of values that lie on the nodes of the grid. Yet even for the small 100 × 100 grid, this requires solving for 10,000 unknowns. Though not a trivial problem, the use of arrays makes it simple.

Approximating derivatives numerically is quite easy. In fact, in Chapter 15 we show how to do it in order to find a solution of an ordinary differential equation. In the present case, the second partial derivatives are approximated simply as [CP05]: (20.1), we obtain a finite-difference approximation to the Laplace equation:

image

where Δ is the spacing of the grid in Figure 20.3. After we substitute this expression, and the analogous one for the second y derivative, into Laplace’s equation

image

image

Figure 20.3 Left: The capacitor’s field is computed only for those (x, y) values on the grid. The voltage of the plates and containing box are kept constant. Right: The algorithm for Laplace’s equation in which the potential at the point (x,y) = (i,j)Δ equals the average of the potential values at the four nearest-neighbor points.

Equation (20.4) is the specific implementation of the equation we need to solve in terms of the 2-D Java array elements V[i][j] in which we store the potential values for x = iΔ and y = jΔ. This equation is visualized on the right of Figure 20.3, which shows that the potential at point (i,j) is the average of the potential values at the four nearest neighbors. If we wrote down this equation for each of the 10,000 points on our grid, we would have 10,000 simultaneous equations for 10,000 simultaneous unknown values of Vij.

Even though it is possible to solve these 10,000 simultaneous equations in a direct fashion, we will follow a simple, indirect approach know as a relaxation technique. Equation (20.4) states that a solution for the potential must be the average of the potential values at the four nearest neighbors. With that in mind, we try an iterative approach to finding the solution:

1.  Start with an initial guess V(x, y) = 0 every place except on the plates.

2.  To obtain an improved guess, apply (20.4) to all points in space, except the plates and box.

3.  Keep applying (20.4) to obtain an even more approved guess until the improvement becomes smaller than some preset level of tolerance.

There is no guarantee that this relaxation method converges for arbitrary choices of Δ and geometries. Yet if we do come up with a solution that ends up satisfying (20.4), then we have our solution and it’s nobody’s business how we got it!

20.4 IMPLEMENTATION: LAPLACE.JAVA

The pseudocode for our solution involves an outermost loop for iterations, and inner loops over Nmax, x, and y values:

image

This algorithm is well suited to the use of arrays because the indices vary, but not the equations. This means, as we see in Listing 20.1’s Laplace.java, that it will not take many lines of code to implement. We store the potential V(x, y) in an array V[i][j], with the first index spanning all possible x values and the second index corresponding to all possible y values (measured in steps of Δ). The array is declared as V[Nmax][Nmax] on line with 7, and with Nmax = 100 steps for the sample program. The voltages of the capacitor plates are kept fixed by lines 13-15, while the boundary conditions for the box are imposed on lines 16-20. The index 25 corresponds to the x value for the left edge of the capacitor, while 75 corresponds to the y value for the lower capacitor and 63 to the y value for the upper capacitor.

Listing 20.1 Laplace.java

image

The only tricky parts in Laplace.java are ensuring that we do not let the algorithm change the value of the externally applied voltages, and that we do specify any indices that lie outside of the array. Recall, Nmax-1 is the maximum value possible for an index of the array V[Nmax][Nmax], and so only Nmax-2 is used in the sweeps over space on lines 22–23. Once there are no errors, we just let the program repeat itself 1,000 times and relax into what we hope will be a stable value for the potential.

20.4.1 Laplace.java: Orientation and Visualization

Before trying to modify a complicated or subtle program, it is good to make sure that it runs, and to orient yourself as to how it goes about its business. Likewise, it is good to do some visualizations of the results so that you get a feel for what its output should be. For that purpose, make a copy of Laplace.java to your personal directory and read through it to get a general idea of what it does.

1.  Make a paper listing of Laplace.java and draw boxes on it illuminating the following structures in the code:

a.  the loop that initializes the potential to 0;

b.  the loop that keeps the box’s potential at 0;

c.  the loops that sets the voltage on the capacitor plates;

d.  the loops that sweep over all x and y values;

e.  the loop that implements the algorithm (20.4);

f.  the statements that output data;

g.  the statement that outputs blank lines.

2.  Before you make any modifications, compile and execute Laplace.java. You should get the statement data stored in Laplace.dat printed to your screen.

3.  Open the file Laplace.dat with an editor. It should contain the solution V (x, y) in a format that does not bother telling you the x and y values. There are 100 values of V followed by a blank line. These are the values of V[0-99][0]. The second 100 numbers are V[0-99][1], and so forth. The plotting program gnuplot automatically uses the row and column indices as the values for x and y it plots, so you do not have to enter them.

4.  Use gnuplot (or whatever you have available) to construct 3-D surface plots of the potential versus both x and y. Experiment with the options to create the best visualization. We show you how to do this with gnuplot in §11.4, with the explicit command given in §11.4.2. Your plot should look like the RHS of Figure 20.2. The height of the surface represents the value of the potential V (x, y) for the x and y values along the horizontal plane. Take stock of how the potential of the bounding box is always zero, and that of the plates are 100 and -100 volts. On the horizontal plane we have placed contour lines that correspond to curves along which the potential is constant. These contours are also called equipotential surfaces. In general, the field is seen to be compressed between the plate and to vary more slowly outside the plates than within.

5.  This program uses a Java PrintWriter object to write to a file. The object is created on line 10 and written to on line 30:

10 PrintWriter w = new PrintWriter (new FileOutputStream (“Laplace.dat”), true) ; 30 w.println(““+V[i][j]+””);

While we discuss Java I/O and its complications in Chapter 13, we note a few points here. As you can decipher from line 10, the Printwriter is connected to the file Lapace.dat on the computer’s hard disk via Java’s FileOutputStream. The Printwriter object is assigned the variable w to it, so that values of the potential are written to the file by appending the println dot operator to w on line 30.

20.5 EXPLORATION: 2-D CAPACITOR

1.  The program Laplace.java assumes that the potential must have relaxed to its final value after 1,000 space iterations. Here is how to decide if this is a good assumption:

a.  Modify a copy of Laplace.java to output to file Laplace2.dat.

b.  Rather than try to discern small changes in highly compressed sur face plots, use a measure of precision with a numerical value. If you look at the surface plot of the potential in Figure 20.2, you will see that a line along the diagonal samples the whole range of the potential. On this account, define a variable

image

  and print out the value of trace for each iteration. If trace changes in the sixth decimal place, you probably have five good places of precision.

c.  Next modify the program so that it always obtains at least three places of precision. Define tol = 0.001 as the desired relative precision and modify Laplace.java so that it stops iterating when the relative change from one iteration to the next is less than tol:

image

You stop the iteration by issuing the break command or by using a while loop.

2.  Now that you know the code is accurate, make it simulate a more realistic capacitor in which the plate separation is 1/10th of the plate length. You should find the field more condensed between the plates.

3.  Compare the results of your simulation to the sketch on the right of Figure 20.1. Whereas we are computing the electric potential, while the sketch is of the electric field, some processing is necessary. We make use of the fact that the electric-field lines point from regions of higher to lower potential, and that the field lines are perpendicular to the equipotential surfaces (the contours in our plots). Though you may try to draw lines perpendicular to the contours projected onto the base of the 3-D plot, it would be easier to look down directly onto the base. Make a 2-D graph, like Figure 20.4, of just the contour lines. The needed gnuplot commands are:

gnu> set nosurface

gnu> set view 0,0,1

4.  Draw in the field lines by hand, starting from the plate at -100 volts, always keeping the field lines perpendicular to the equipotential surfaces and going down hill.

image

Figure 20.4 Contour plot of capacitor’s equipotential surfaces. The electric-field lines are perpendicular to these contours and point toward lower potential.

20.6 EXPLORATION: 3-D CAPACITOR*

Our solution for the capacitor is general and realistic. However, our model is only two-dimensional (the “plates” are lines not squares). Extend Laplace.java so that the plates have finite dimensions in both the x and y directions. You will now have to solve for V (x, y, z) and make three 3-D surfaces of V (x, y, z) versus x – y, x-z, and y -z.

20.7 KEY WORDS

boundary conditions

equipotential surfaces

electric field

electrostatics

electric potential

equipotential lines

capacitor

gnuplot

hidden lines

finite-difference method

initial conditions

postscript

partial derivatives

reference pass

value pass

contour lines

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset