Week 3: (Finally) Implementing Yee’s Algorithm
March 18, 2024
Hey friends! Welcome back to my blog, where I’ll be tracking my progress on my EM field simulation project. We left off last week with a comparison of the different algorithms commonly used to find solutions to Maxwell’s equations. Out of the variety of available methods, Yee’s algorithm looked promising as a realistic starting point. I promised to implement Yee’s algorithm in this week’s blog, and I’m glad to say that this task has been completed. In this blog, I’ll be focusing less on the theoretical aspects of Yee’s algorithm (which were covered in last week’s entry). Instead, I’ll focus on the technical aspects of implementing Yee’s algorithm.
Despite its apparent simplicity, Yee’s algorithm looked relatively challenging to implement, especially in 2D. Thus, I decided to start with a 1D version of the algorithm to map out the general ideas of the algorithm and find any potential roadblocks I would face during implementation.
Finite Differencing
The program stores the calculated E and B fields in multidimensional arrays. Because we are working in 1D space with an additional dimension of time, the arrays need to be two-dimensional. The fields at each time step are calculated using our information about the fields at previous timesteps. The implementation is very similar to most standard dynamic programming problems.
Staggered Grids
The above image shows how the B fields are calculated. The outermost loop steps through time, while the inner loop iterates through the different x-values. The indexing may look strange because of how I chose to implement the staggered grid, but notice how the time-derivative of the B field is related to the x-derivative of the E field. This resembles the relation between the fields as apparent in the finite difference equations we studied last week.
Moreover, as we found out last week, Yee’s algorithm uses a “staggered grid” to calculate the E and B fields at discrete points in spacetime (shown below). However, we cannot have fractional indices in our arrays, so directly implementing the staggered grid will have to take some creativity.
For my program, I shifted all the half-indices in space to the left by half a unit.
I’ll explain further. Looking at the diagram we can see that the B field is calculated at fractional length indices. In order to store the B field in an array, we then assign index 0 in the array to x = 0.5, index 1 to x = 1.5, and so on. This solves the problem of implementing the staggered grid.
Testing
I tested my program with a variety of physical situations such as sinusoidal EM waves and interfering wave pulses. Although my program generally worked well, it was limited greatly by the CFL stability condition, which put a limit on the timestep size. This meant that to simulate evolution over a longer period of time, I had to extend the number of timesteps, increasing the time and space complexity of the program. Also, because I ran out of time, I didn’t properly implement boundary conditions, which meant that the simulation sometimes came up with strange solutions.
I also implemented Yee’s algorithm in 2D, but I mostly expanded on the 1D version here because it’s the same as the 2D version but easier to explain. Next week, we will work on improving the algorithm and testing out other algorithms. Bye!
Leave a Reply
You must be logged in to post a comment.