You are on page 1of 24

How to program FDTD v0.

1 by Kyriacou Adamos, Greece

How to program the FDTD for EM Simulations (v0.1)

Author: Kyriacou Adamos

Location: Xanthi ,Greece

E-mail Address: somada141@yahoo.gr

Foreword:

The purpose of this guide is really straightforward. I want to help people with the implementation of the Finite
Difference Time Domain method. This method is well documented hence I won’t bother to explain its inner workings
(for this you must read such great books as Taflove’s Computational Electrodynamics which explains the method
greatly) but rather the kinks one might face while actually programming this method.

The reason such a guide was written is that while I was studying FDTD there were tons of points no one bothered to
clarify, not in any one of the famous books. That took me a lot of time to figure on my own and a lot of mistakes. If there
was such a guide when I was struggling I would have kissed the author’s feet (not that I want such a disgusting thing).

I know that as it is, this guide will only help total newbies to FDTD since it doesn’t cover any more advanced issues. But if
there is a positive response from the community I will update this guide to include more complex topics such as:
 Absorbing Boundary Conditions
 Source Modeling
 Implementation of other equations (such as the Bioheat Equation)

BEWARE: THIS DOCUMENT IS MERELY WHAT YOU COULD CAL L A BETA -NOT A FINAL
VERSION. IF ENOU GH PE OPLE ARE INTERESTED IN THIS GUIDE THEN I WILL CONTINUE
THIS GUIDE AND UPDATE IT.

BEWARE: The code supplied in this guide is far f rom optimized. To be honest it is probably the
most inefficient code. However it is also the simplest code. This guide’s purpose is to teach
FDTD not programming. Of course this is not the code I am using for FDTD but the firs t code I
wrote. So go ahead, learn FDTD and then improve it.

1
How to program FDTD v0.1 by Kyriacou Adamos, Greece

The Yee-Cell
It all starts with the form of the Yee cell we are going to use. In this case the Yee cell we are using is the one shown
below:

Ey

Ex Hz Ex

Ey

Ez Ez
Hx

Hy Hy
Ez Ez
Hx

Ey
Y

Ex Hz Ex

Ey

Although the above form isn’t the one shown by Taflove (at least in the 1st and 2nd Editions of his book) most people use
this when programming FDTD. So we are going to use this one because:

 It will be easier to compare our code with that of others


 It allows easier implementation of Perfect Electric Conductors (PEC)

2
How to program FDTD v0.1 by Kyriacou Adamos, Greece

The Grid/Mesh
Let’s see how we are going to make a grid. In here I will not tell you how to find a geometry modeler or how to find or
make a mesher. In here I consider that somehow you have solved that problem. In this guide we consider the mesh to
have the following form:

 The mesh is a 3D array where each value in the array represents a cell of the geometry and the surrounding
vacuum if there is any.

 What we need for an FDTD simulation is to know the dielectric parameters for the material of every single cell.
For this simulation we will not consider magnetic losses so we will need  ,    for every cell.

 My approach was to have in hand the material that resides in the center of every cell hence the aforementioned
parameters.

 A small 2D example would be

  1  1  1  1 
 
 0  0  0  0
  1  1  1  1 
 
  1   2.08   2.08  1 
 
 0  0  0  0
  1  1  1  1 
 
  1   2.08   2.08   1 

 0  0  0  0
 
  1  1  1  1 
 
  1  1  1  1 
 0  0  0  0
 
Vacuum    1  1  1   1 

Teflon (Loss Free)


What we want of course is similar to the above but in a 3D scale and with more materials of course.

 For this guide a similar to the above grid is pretty much what we need since once we have this and the sizes of
the cell in each direction we can describe our geometry fully (material-wise).

3
How to program FDTD v0.1 by Kyriacou Adamos, Greece

The Space/Time Stability Conditions


Now we will define the stability conditions. The stability conditions for the space step and the time step for a
rectangular lattice (we will not use a cubical lattice because rectangular offers more versatility when creating a mesh
and the case of the cubical mesh is covered) are:

 
 Space Step:  max  (or  max  for better results) and  x ,  y ,  z   max
10 20

1
 Time Step: t 
1 1 1
c  2 2
x y z
2

4
How to program FDTD v0.1 by Kyriacou Adamos, Greece

The FDTD formulas


The next thing to do is to see the functions we are going to program. The 6 functions of FDTD for rectangular lattices are
the following:

n1 2 n1 2
H x i , j , k  Da i , j , k  H x i , j , k  Dbz
i, j ,k 
 Ey
n

i , j , k 1 2
Ey
n

i , j , k 1 2  D by
i, j ,k

 E z i , j 1 2, k  E z i , j 1 2, k
n n

Hy
n1 2

i, j ,k
 Da i , j , k  H y
n1 2

i, j ,k
 Dbx
i, j ,k

 E z i 1 2, j , k  E z i 1 2, j , k  Dbz
n n
 i, j ,k
E
n
x i , j , k 1 2  E x i , j , k 1 2
n

n1 2 n1 2
H z i , j , k  Da i , j , k  H z i , j , k  Dby
i, j ,k
E
n
x i , j 1 2, k  E x i , j 1 2, k
n
 D bx i , j , k 
 Ey
n

i 1 2, j , k
Ey
n

i 1 2, j , k 
n1
E x i , j , k  C a i , j , k  E x i , j , k  Cby
n

i, j ,k
 n1 2 n1 2
 H z i , j 1 2, k  H z i , j 1 2, k  Cbz  i, j ,k 
 Hy
n1 2

i , j , k 1 2
 Hy
n1 2

i , j , k 1 2 
H C  
n1 n n1 2 n1 2 n1 2 n1 2
Ey  Ca i , j , k  E y  Cbz x i , j , k 1 2  H x i , j , k 1 2 bx i , j , k  H z i 1 2, j , k  H z i 1 2, j , k
i, j ,k i, j ,k i, j ,k

n1
E z i , j , k  C a i , j , k  E z i , j , k  Cbx
n
i, j ,k 
 Hy
n1 2

i 1 2, j , k
 Hy
n1 2

i 1 2, j , k C by
i, j ,k
H
n1 2
x i , j 1 2, k
n1 2
 H x i , j 1 2, k 
   t    i , j , k  t 
Ca i , j , k  1  i , j , k  1 
 2   i , j , k   2   i , j , k 

 t    i , j , k  t   t    i , j , k  t   t    i , j , k  t 
   1   1   1 
   y   , Cb    z  
Cbx , Cby
i, j ,k    x   2   i , j , k  2   i , j , k  y 2   i , j , k 
 i, j ,k    i, j ,k    i, j ,k  
i, j ,k i, j ,k

  '  t   i', j , k  t 
Da i , j , k  1  i , j , k  1  
 2   2  i , j , k 
 i , j , k  
 t   i', j , k  t   t   i', j , k  t   t   i', j , k  t 
   1   1   1 
   y   , Db    z  
Dbx , Dby
i, j ,k    x   2  i , j , k  2  i , j , k  z i, j ,k 2  i , j , k 
 i, j ,k    i, j ,k    i, j ,k  
i, j ,k

What are we going to do with the ‘+1/2’ in time?


Since in FDTD we use time steps and time is the product of the time step value and the number of times steps elapsed
we don’t abide by the half step rule. Instead we will calculate both H and E in the same time step. To be more specific in
the time loop where we increase n we will firstly update the H fields and then the E fields since E is to be calculated half
a time step after. However since time doesn’t show up in the formulas we do it as noted above. See the code below for
it is where it will become clearer.

What are we going to do with the ‘+1/2’ in space?


Because we are going to use 3D arrays for every field component and i, j, k will act as indexes then we can’t have ½ as
an index. Moreover because the E and H field component values will be stored in different arrays and not in one array
the increments +1/2 and -1/2 will be changed. To what? You will see later.

5
How to program FDTD v0.1 by Kyriacou Adamos, Greece

Array Sizes
Firstly we need to see how we will declare our arrays and what dimensions will those have. Before we actually write the
code let’s take a look at 3 Yee cells glued together in the y direction.

Ey Ey Ey

Ex Hz Ex Hz Ex Hz Ex

Ey Ey Ey

Ez Ez Ez Ez
Hx Hx Hx

Hy Hy Hy Hy
Ez Ez Ez Ez
Hx Hx Hx

Ey Ey Ey
Y

Ex Hz Ex Hz Ex Hz Ex

Ey Ey Ey

In this guide we use the following name to refer to the number of cells on every direction:

 NOC _ X : The number of cells of the entire grid in the x direction

 NOC _ Y : The number of cells of the entire grid in the y direction

 NOC _ Z : The number of cells of the entire grid in the x direction

The abbreviation NOC stands for Number Of Cells.

We can see of course that when many cells are glued to one another we have common field components. In the above
case we have common H y , E x , E z . By counting those common while keeping in mind that we have a 1x3x1 grid
meaning that the above variables are NOC_X=1, NOC_Y=3, NOC_Z=1, we see that in total we have:

 H y : NOC _ X   NOC _ Y 1  NOC _ Z 1  3 1  1  4

 E x : NOC _ X   NOC _ Y 1  NOC _ Z 1  1  3 1   1 1   8

 E y : NOC _ X   NOC _ Y 1  NOC _ Z 1  1  3 1   1 1   8

6
How to program FDTD v0.1 by Kyriacou Adamos, Greece

The rule for the number of field components in a grid is:


 For H components: The number of components in each direction is equal to the number of cells in that direction
except for the direction of the component where there is one extra component. For example:

H y :  NOC _ X    NOC _ Y  1   NOC _ Z 

 For E components: The number of components in each direction is equal to the number of cells in that direction
PLUS 1 except in the direction of the component where we don’t add an extra component. For example:

E x :  NOC _ X    NOC _ Y  1   NOC _ Z  1

The above rule derives from the fact that there are common field components between cells in every direction. This is
different for E and H fields because of their different positions in the cell.

Now we can see the array sizes for an FDTD grid with dimensions  NOC _ X    NOC _ Y    NOC _ Z  :

 The constants needed for the calculations of the E field components:

Ca, Cbx , Cby , Cbz :  NOC _ X    NOC _ Y    NOC _ Z 

 The arrays containing the E field values:

E x :  NOC _ X    NOC _ Y  1   NOC _ Z  1


E y :  NOC _ X  1   NOC _ Y    NOC _ Z  1
E z :  NOC _ X  1   NOC _ Y  1   NOC _ Z 

 The arrays containing the H field values:

H x :  NOC _ X  1   NOC _ Y    NOC _ Z 


H y :  NOC _ X    NOC _ Y  1   NOC _ Z 
H z :  NOC _ X    NOC _ Y    NOC _ Z  1

7
How to program FDTD v0.1 by Kyriacou Adamos, Greece

The FDTD formulas : The indexes problem

Well let’s take a look at another jolly bunch of Yee cells in a


1x2x2 grid.

Since we know the dimensions of the grid we can very well


know the dimensions of all the field component arrays.

BEWARE: As I said before we deal with Python here when it


comes to programming. So an important thing to remember
is that ARRAY INDEXES START FROM 0.

What we are going to do is see how to calculate an H and an E


value.

Calculating an E field component:

We want to calculate the value E x 0,1,1


. The formula for Ex is:

n1
E x i , j , k  Ca i , j , k  E x i , j , k  Cby
n

i, j ,k
 n1 2 n1 2
 H z i , j 1 2, k  H z i , j 1 2, k  Cbz  i, j ,k 
 Hy
n1 2

i , j , k 1 2
 Hy
n1 2

i , j , k 1 2 
For the time being we do not deal with the Ca and Cb constants. We know which H values are needed for this Ex value.
In this case the indexes are:

 H z n1 2  H z n1 2
 i , j 1 2, k 0,1,1

H  n1 2
z i , j 1 2, k  H z 0,0,1
n 1 2

n1 n1 
E x i, j ,k  E x 0,1,1  n1 2 n1 2
 H y i , j , k 1 2  H y 0,1,1
 n1 2 n1 2
H y  Hy
 i , j , k 1 2 0,1,0

Calculating an H field component:

We want to calculate the value H x 1,0,0 . The formula for Ex is:

n1 2 n1 2
H x i , j , k  Da i , j , k  H x i , j , k  Dbz
i, j ,k 
 Ey
n

i , j , k 1 2
Ey
n

i , j , k 1 2  D
by
i, j ,k

 E z i , j 1 2, k  E z i , j 1 2, k
n n

8
How to program FDTD v0.1 by Kyriacou Adamos, Greece

For the time being we do not deal with the Da and Db constants.
We know which H values are needed for this Ex value. In this case
the indexes are:

E n n
y i , j , k 1 2  E y 1,0,1

 n n
n1 2 n1 2  y i , j , k 1 2
E  Ey
H x i , j , k  H x 1,0,0  1,0,0

Ez n
 E z 1,1,0
n

 i , j 1 2, k

E n  E z 1,0,0
n
 z i , j 1 2, k

The index rule for calculating E field components is:

When it comes to calculating E values of i, j, k indexes then the indexes of the necessary H value follow this rule:

 When the H index has a +1/2 this becomes 0 (practically you delete it)

 When the H index has a -1/2 this becomes -1

The index rule for calculating H field components is:

When it comes to calculating H values of i, j, k indexes then the indexes of the necessary E value follow this rule:

 When the H index has a -1/2 this becomes 0 (practically you delete it)

 When the H index has a +1/2 this becomes +1

These two rules solve the index problem. You will see the full results later when we present the formulas in their
programming form.

9
How to program FDTD v0.1 by Kyriacou Adamos, Greece

The FDTD formulas : Time


Well this might be a bit hard to get at first but it really is a simple notion. FDTD is a recursive method hence it needs past
values of the field components to calculate the new ones. This is due to the central difference approximation used to
convert the time derivative to a finite difference.

Moreover when programming we can’t just keep huge arrays of values for every time step. If we do that the memory
will full up in no time especially in huge grids which occurs often in FDTD. So what do we do?

Time management in FDTD:


In our method what we are going to do is the following:

 We create and initialize (fill with zeros) our arrays for every field component.

 In every loop (every time step) we calculate the next field value in time (for every component) using the old one
(which is already saved in the array from the last step) and overwrite the old value with the new.

The above way works like a charm. The way we implement this is really simple but we will see it in the final FDTD
formulas.

10
How to program FDTD v0.1 by Kyriacou Adamos, Greece

The FDTD formulas : Materials


The way I handle materials is mine. This means that even though it worked for me it doesn’t guarantee anything. There
may be 100 better and more accurate ways. To date this has worked fine so this is what I use in this guide. If in the
future I discover better ways for this or if I discover that mine is wrong I will include them in the next guide version.

We have said before that the way we make the mesh is to define the material in the center of every cell. However the E
field components need some constants whose values are defined by the material characteristics. If you take a look at a
Yee cell you will see that all E field components lie in such positions that they are bound to be shared between multiple
cells.

The question is:

Which materials do we assign to every field component?

First of all let me note that there is no such a problem when it comes to H fields for our guide. You will see why later.
But in E components this is not the case. Every E field component may belong to 1-4 cells simultaneously depending on
the grid.

My method here is this: STARTING FROM THE 0, 0, 0 CELL MEANING THE CELL IN THE LOWER LEFT CORNER, THE E
FIELD COMPONENTS ‘BELONG’ TO THE CELL CLOSER TO THE FIRST.

To get what I mean take a look here:

11
How to program FDTD v0.1 by Kyriacou Adamos, Greece

BEWARE: There is a good chance that my way of handling materials is completely stupid but it has worked fine for me
until now. If someone knows of a guaranteed way that will yield better results, please contact me since I’m still learning.

As you can see for our simulations we don’t care for materials concerning H fields since we consider dielectric materials
with permittivity equal to 1 (more on that later). We do however care for the materials assigned to the E fields hence
the above approximation.

To implement the above then the rule is that for the material pointers of a field component with i, j, k pointers we
subtract 1 from the pointers on whose direction we have two field components per cell. For example the Ex field
component has 2 components on the y and z direction but only one on the x direction. Therefore we subtract 1 from the
j and k pointers but not from i. See the formulae below to understand what am I taking about.

12
How to program FDTD v0.1 by Kyriacou Adamos, Greece

The FDTD formulas (5)


Since we seem to have all the necessary formulas we will start with a little programming. All the programming in this
guide is done in Python. But be warned: when it comes to for loops, Python is REALLY slow as opposed to other
compiled languages like FORTRAN and C. However the code below can be easily ported to another language. Python
was used initially for its versatility and because when the code is optimized speed increases rapidly. In addition to
Python the library numpy is also necessary.

But before we do so we will introduce the hypotheses we will make for this guide:

 All materials we will use have relative permittivity equal to 1 and we consider no magnetic losses (in most cases
this is correct and only a few materials do not agree with the above hypothesis). Hence:

i , j , k    0
i', j , k   '  0

so the constants for the calculations of the H fields become :

Da i , j , k  Da  1
 t   t   t 
Dbx  Dbx    , Dby  Dby    , Dbz  Dbz   
 0  x   0  y   0  z 
i, j ,k i, j ,k i, j ,k

We now write the formulas for the calculation of the field components in a more programming-like form:

Hx i, j, k   Da* Hx i, j, k   Dbz *  E y i, j, k  1  E y i, j, k   Dby *  E z i, j  1, k   E z i, j, k 


Hy i, j, k   Da* Hy i, j, k   Dbx *  E z i  1, j, k   E z i, j, k   Dbz *  E x i, j, k  1  E x i, j, k 
Hz i, j, k   Da* Hz i, j, k   Dby *  E x i, j  1, k   E x i, j, k   Dbx *  E y i  1, j, k   E y i, j, k 

E x i, j, k   Ca i, j-1, k-1 * E x i, j, k  


Cby i, j-1, k-1 *  Hz i, j, k   Hz i, j  1, k  
Cbz i, j-1, k-1 *  Hy i, j, k   Hy i, j, k  1

E y i, j, k   Ca i-1, j, k-1 * E y i, j, k  


Cbz i-1, j, k-1 *  Hx i, j, k   Hx i, j, k  1 
Cbx i-1, j, k-1 *  Hz i, j, k   Hz i  1, j, k 

E z i, j, k   Ca i  1, j  1, k  * E z i, j, k  
Cbx i  1, j  1, k  *  Hy i, j, k   Hy i  1, j, k  
Cby i  1, j  1, k  *  Hx i, j, k   Hx i, j  1, k 

BEWARE: Please note that Da , Db are no longer arrays but constants since they do not change for every cell.

13
How to program FDTD v0.1 by Kyriacou Adamos, Greece

The Boundaries Problem


Although I wish I could say that we can now simply loop over the arrays and voila, that wouldn’t be true. There is still
the boundaries problem. I will not explain the necessity of boundary conditions here (once more read Taflove’s book).
Up to this point there will not be any Absorbing Boundary Conditions (ABCs from now on). We will stick to the pure
FDTD and then we will see.

Although at his point we won’t show how to program ABCs (in order to keep it simple) that doesn’t mean we
can do as we please. So what we will do is give the code for FDTD as if we mean to add the ABCs later. The
consequences will be that there will be numerical reflection when energy hits the boundaries of the computational
domain. People pay attention to which values we do calculate with FDTD and which not. I did not and after putting in
the Mur ABCs things didn’t work and it took me 2 weeks to figure out what the problem was. Don’t be as stupid!!!

However we need to define what we can and can’t compute in this case. Taking a look at the FDTD formulas you will
notice the following:

 Every H field component needs the four surrounding co-plane E field values.

As you can see, every H can be calculated by the values of E that already belong in the same cell. Hence for any grid we
can calculate any H regardless on the number of cells. This can be expressed like this: THERE ARE NO BOUNDARY
ISSUES WITH H FIELDS. SO WE LOOP THROUGH ALL OF THEM FROM FIRST TO LAST.

14
How to program FDTD v0.1 by Kyriacou Adamos, Greece

 Every E field component needs the four surrounding H values on the two planes it belongs as shown here:

As you can see now when it comes to calculating E values there is an ever-present problem. No matter how big the grid
there will be E values on the outer cells (the cells that make-up the outer faces) that could not be computed since there
are some H missing. THERE ARE BOUNDARY ISSUES WITH E FIELDS. So what do we do?

The answer of course is: We calculate all the E values that can be calculated with FDTD and enforce ABCs on those
remaining.

Note: In this case only the E field circled can be calculated by FDTD. All the other are missing 1 or 2 H fields and
therefore need ABCs.

15
How to program FDTD v0.1 by Kyriacou Adamos, Greece

The Boundaries Problem(2)


Now let’s see what we mean by saying “We calculate only the inner E fields with FDTD”. After you study ABCs (and you
better should) you will find that for every field component that requires ABCs, we enforce it only on the faces that lie in
the directions tangent to this field component. This means:

 For Ex we enforce ABCs on y and z

 For Ey we enforce ABCs on x and z

 For Ez we enforce ABCs on x and y (only E here. We said H don’t need ABCs)

Now why is this important? Because the above will define which are the inner values that will be calculated with FDTD
for every component.

Now let’s translate the phrase “we enforce ABCs on”. What we mean is on those sides we only calculate the inner
values. Let’s see such an example for the Ex field component:

I think you can get the idea now.

The rule for which values we calculate and which we don’t is:
For a field component we calculate all the values on the direction it is on (we start from the 1 st value to the last)
but we calculate from the inner values for the other two directions (starting from the 2 nd value to the one before the
last). For example for Ex we have:

16
How to program FDTD v0.1 by Kyriacou Adamos, Greece

for x  0, endx
for y  1, endy-1
for z  1, endz-1

Note: The above is not code at all. It is merely a representation of what should be done in this case. In the other cases
we only change which directions doesn’t get the -1. You can say this: WHEN A FIELD COMPONENT NEEDS ABCs THEN
YOU CALCULATE THE INNER VALUES ONLY ON THE SIDES WHERE ABCs ARE TO BE ENFORCED.

17
How to program FDTD v0.1 by Kyriacou Adamos, Greece

Programming Flowchart (1)


Now that we now all the details let’s see the procedure we are going to follow for the programming part. A flowchart of
our procedure would be:

Flowchart Part 1: Preprocessing

Grid
• We obtain/load the information from the grid meaning the
number of cells in each direction, the cell edge sizes for each
direction and the material parameters at each cell.

Array Creation and Initialization


• We create the necessary arrays according to the number of
cells in each direction and initialize all array by filling them
with zeros.

Constant Declaration
• We declare some standard constants that are needed for any
EM simulation such as speed of light, vacuum permitivity etc.

Parameter Calculation
• We calculate the needed parameters such as the time step and
the material parameters and of course the maximum number
of time steps.

The full details of the above steps will be given in the code.

18
How to program FDTD v0.1 by Kyriacou Adamos, Greece

Programming Flowchart (2)


After completing the first part we only have to proceed with the actual FDTD simulation.

Flowchart Part 2: FDTD Main Loop

The steps below are repeated for every time step. THE ORDER IN WHICH THE STEPS ARE PRESENTED MUST BE
FOLLOWED TO THE LETTER. IF YOU DON’T THEN NUMERICAL INSTABILITY WILL PRESENT ITSELF.

Source
• We enforce the values that implement our source by changing
the appropriate values from their previous value to the ones
desired (In this guide we will not delve in source simulation).

Calculation of all H field components


• We calculate ALL H field components for ALL their values in the
arrays without leaving any of them out.

Calculation of inner E field components


• We calculate ALL E field components but NOT ALL OF THEM.
We only calculate those that are allowed by the boundary
conditions limitations presented above.

Boundary Conditions
• After we have calculated all the H fields and the inner E fields
we now calculate the remaining E components values but NOT
WITH THE FDTD FORMULAS but with those given by the ABCs.

The full details of the above steps will be given in the code.

19
How to program FDTD v0.1 by Kyriacou Adamos, Greece

Code (1)
Well now I think we are ready for the actual code. At this point we have already obtained:

 The number of cells in each direction in the form of three variables named NOC _ X ,NOC _ Y , NOC _ Z .
 The size of the cell edges in the three directions in millimeters in the form of three variables named CEL L _SIZEx, CEL L _SIZEy,CEL L _SIZE z .
 A 3D array where each value represents a cell and gives the material parameters in its center named CubicMesh (the name is misleading. Initially
my FDTD program was made for cubical lattices but I changed it afterwards to a rectangular lattice. However the name was used in way too many
folders, files and code expressions so I didn’t change it).

Below is the part of the code described by the first flowchart:

from math import sin,cos,pi,sqrt


from numpy import *

#We define some standard parameters


c=2.99792458*10**8 #Speed of light
Epsilon0=8.854*10**-12 #The dielectric permittivity of vacuum e0
Mu0=12.56637061*10**-7 #The magnetic permeability of vacuum m0
Dx=CELL_SIZEx/1000 #Here we convert our cell edge sizes to meters from millimeters
Dy=CELL_SIZEy/1000
Dz=CELL_SIZEz/1000
Dt=(c*sqrt((Dx**-2)+(Dy**-2)+(Dz**-2)))**-1 #Here we calculate the time-step. This is done according to a standard formula for FDTD simulations which #ensures stability
of the algorithm.

N_STEPS=350 #This is the maximum number of steps. No

#Here we calculate the coefficients that appear in the formulas for the calculation of the field components. Those are Ca,Cbx,Cby,Cbz,Da,Dbx,Dby,Dbz. The Ca,Cb are
#arrays with sizes equal to the CubicMesh array since we need to calculate these coefficients for every cell.
Ca=zeros((NOC_X,NOC_Y,NOC_Z))
Cbx=zeros((NOC_X,NOC_Y,NOC_Z))
Cby=zeros((NOC_X,NOC_Y,NOC_Z))
Cbz=zeros((NOC_X,NOC_Y,NOC_Z))

20
How to program FDTD v0.1 by Kyriacou Adamos, Greece

#Here we loop through the entire Ca array and calculate each value. Note that since the Ca and Cb arrays are of one size we only loop through one of them and calculate
#them all in every cell
for x in xrange(Ca.shape[0]):
for y in xrange(Ca.shape[1]):
for z in xrange(Ca.shape[2]):
Epsilon=float(CubicMesh[x][y][z].matEpsilon)*Epsilon0 #We find out the Epsilon and Mu for every cell from the CubicMesh. Note that the final Epsilon and Mu that
#take place in the calculation of Ca,Cb are multiplied by the vacuum constants e0 and Mu0 because that the way it is.
Sigma=float(CubicMesh[x][y][z].matSigma)
Ca[x,y,z]=((1-((Sigma*Dt)/(2*Epsilon))) / (1+((Sigma*Dt)/(2*Epsilon)))) #Here we calculate the Ca,Cb
Cbx[x,y,z]=((Dt/(Epsilon*Dx)) / (1+((Sigma*Dt)/(2*Epsilon))))
Cby[x,y,z]=((Dt/(Epsilon*Dy)) / (1+((Sigma*Dt)/(2*Epsilon))))
Cbz[x,y,z]=((Dt/(Epsilon*Dz)) / (1+((Sigma*Dt)/(2*Epsilon))))

#On the other hand the Da, Db values are constants so we only calculate one of each.
Da=1
Dbx=Dt/(Dx*Mu0)
Dby=Dt/(Dy*Mu0)
Dbz=Dt/(Dz*Mu0)

#We create and initialize the arrays that will hold the field components Ex,Ey,Ez,Hx,Hy,Hz. Each component has different array sizes as defined by the Yee #cell.
Ex=zeros((NOC_X,NOC_Y+1,NOC_Z+1))
Ey=zeros((NOC_X+1,NOC_Y,NOC_Z+1))
Ez=zeros((NOC_X+1,NOC_Y+1,NOC_Z))

Hx=zeros((NOC_X+1,NOC_Y,NOC_Z))
Hy=zeros((NOC_X,NOC_Y+1,NOC_Z))
Hz=zeros((NOC_X,NOC_Y,NOC_Z+1))

Well I hope you understood what went up there. We simply declare some standard parameters and use the mesh information to calculate the material
parameters necessary for the simulation. So now we are done with the geometry grid and are ready to start with the simulation.

21
How to program FDTD v0.1 by Kyriacou Adamos, Greece

Code (2)
Well now that the parameters are all set we go on with the simulation.

Below is the part of the code described by the second flowchart:

for n in range(N_STEPS+1):

#Hx
for i in xrange(Hx.shape[0]):
for j in xrange(Hx.shape[1]):
for k in xrange(Hx.shape[2]):
Hx[i,j,k]=Da*Hx[i,j,k]+Dbz*(Ey[i,j,k+1]-Ey[i,j,k])-Dby*(Ez[i,j+1,k]-Ez[i,j,k])

#Hy
for i in xrange(Hy.shape[0]):
for j in xrange(Hy.shape[1]):
for k in xrange(Hy.shape[2]):
Hy[i,j,k]=Da*Hy[i,j,k]+Dbx*(Ez[i+1,j,k]-Ez[i,j,k])-Dbz*(Ex[i,j,k+1]-Ex[i,j,k])

#Hz
for i in xrange(Hz.shape[0]):
for j in xrange(Hz.shape[1]):
for k in xrange(Hz.shape[2]):
Hz[i,j,k]=Da*Hz[i,j,k]+Dby*(Ex[i,j+1,k]-Ex[i,j,k])-Dbx*(Ey[i+1,j,k]-Ey[i,j,k])

22
How to program FDTD v0.1 by Kyriacou Adamos, Greece

#Ex
for i in xrange(Ex.shape[0]):
for j in xrange(1,Ex.shape[1]-1):
for k in xrange(1,Ex.shape[2]-1):
Ex[i,j,k]=Ca[i,j-1,k-1]*Ex[i,j,k]+Cby[i,j-1,k-1]*(Hz[i,j,k]-Hz[i,j-1,k])-Cbz[i,j-1,k-1]*(Hy[i,j,k]-Hy[i,j,k-1])

#Ey
for i in xrange(1,Ey.shape[0]-1):
for j in xrange(Ey.shape[1]):
for k in xrange(1,Ey.shape[2]-1):
Ey[i,j,k]=Ca[i-1,j,k-1]*Ey[i,j,k]+Cbz[i-1,j,k-1]*(Hx[i,j,k]-Hx[i,j,k-1])-Cbx[i-1,j,k-1]*(Hz[i,j,k]-Hz[i-1,j,k])

#Ez
for i in xrange(1,Ez.shape[0]-1):
for j in xrange(1,Ez.shape[1]-1):
for k in xrange(Ez.shape[2]):
Ez[i,j,k]=Ca[i-1,j-1,k]*Ez[i,j,k]+Cbx[i-1,j-1,k]*(Hy[i,j,k]-Hy[i-1,j,k])-Cby[i-1,j-1,k]*(Hx[i,j,k]-Hx[i,j-1,k])

23
How to program FDTD v0.1 by Kyriacou Adamos, Greece

Things to note:

 As you can see we calculate the H field components first and then the E field components. By doing so we
simulate the half time step required by FDTD. Hence H is always calculated half a step before E.
 You will also see that we don’t just keep “records” of the field components for each time step but rather at
every time step the old values are replaced/overwritten by the new ones. Therefore if one needs to use the field
component values at every time step then one should do so at every step hence including his code in the time
loop.
 Up to this point there is no convergence criterion but a maximum number of iteration. Normally in FDTD we
need a way of knowing when our simulation has reached its steady state. In EM simulations with frequencies of
GHz this happens in a few nanoseconds (up to a couple hundred depending on the geometry size and the size of
the cell). This however convolutes the code and will not be done yet.

Also note:
 Note the fact that we calculate all H field components (covering the entire grid) but not all E. See above
Boundaries Problem (2) for an explanation.
 Pay attention to which material parameter value we use every time for calculation of the E components. Refer
to Materials for this.

24

You might also like