24
How to program FDTD v0.1 by Kyriacou Adamos, Greece 1 How to program the FDTD for EM Simulations (v0.1) Author: Kyriacou Adamos Location: Xanthi ,Greece E-mail Address: [email protected] 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 CALL A BETA-NOT A FINAL VERSION. IF ENOUGH PEOPLE ARE INTERESTED IN THIS GUIDE THEN I WILL CONTINUE THIS GUIDE AND UPDATE IT. BEWARE: The code supplied in this guide is far from 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 first code I wrote. So go ahead, learn FDTD and then improve it.

How to Program Fdtd 1824

Embed Size (px)

Citation preview

Page 1: How to Program Fdtd 1824

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

1

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

Author: Kyriacou Adamos

Location: Xanthi ,Greece

E-mail Address: [email protected]

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 CALL A BETA -NOT A FINAL

VERSION. IF ENOUGH PEOPLE ARE INTERESTED IN THIS GUIDE THEN I WILL CONTINUE

THIS GUIDE AND UPDATE IT.

BEWARE: The code supplied in this guide is far from 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 first code I

wrote. So go ahead, learn FDTD and then improve it.

Page 2: How to Program Fdtd 1824

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

2

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

Ey

Ey

Ey

ExEx

Ex Ex

Ez

Ez

Ez

Ez

Hz

Hz

Z

Y

X

Hx

Hx

Hy Hy

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)

Page 3: How to Program Fdtd 1824

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

3

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

Vacuum

Teflon (Loss Free)

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

1 1 1 1

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).

Page 4: How to Program Fdtd 1824

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

4

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

10

(or

max20

for better results) and max, ,x y z

Time Step:

2 2 2

1

1 1 1t

cx y z

Page 5: How to Program Fdtd 1824

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

5

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:

1 2 1 2

, , , , , , , 1 2, , 1 2,, , 1 2 , , 1 2, , , ,

1 2 1 2

, , 1 2, , 1 2, , , , 1 2 , , 1 2, , , , , ,, ,

, ,

z y

x z

n nn n n n

x a x b y y b z zi j k i j k i j k i j k i j ki j k i j ki j k i j k

n n n n n n

y a y b z z b x xi j k i j k i j k i j k i j ki j k i j k i j ki j k

z i j

H D H D E E D E E

H D H D E E D E E

H

1 2 1 2

, , , , , 1 2, , 1 2, 1 2, , 1 2, ,, ,, ,

1 2 1 21 1 2 1 2

, , , , , , , 1 2, , 1 2, , , 1 2 , , 1 2, ,, ,

,

y x

y z

n nn n n n

a z b x x b y yk i j k i j k i j k i j k i j k i j ki j ki j k

n nn n n n

x a x b z z b y yi j k i j k i j k i j k i j k i j k i j ki j ki j k

y i

D H D E E D E E

E C E C H H C H H

E

1 1 2 1 2 1 2 1 2

, , , , 1 2 , , 1 2 1 2, , 1 2, ,, , , , , , ,

1 2 1 21 1 2

, , , , , , , 1 2, , 1 21 2, , 1 2, ,, , , ,

z x

x y

n n n n n n

a y b x x b z zi j k i j k i j k i j k i j kj k i j k i j k i j k

n nn n n

z a z b y y b x xi j k i j k i j k i j k i ji j k i j ki j k i j k

C E C H H C H H

E C E C H H C H H

1 2

,

, , , ,

, ,, , , ,

, , , ,

, , , , , ,, , , , , , , , , ,

1 12 2

1 , 1 ,2 2x y y

n

k

i j k i j k

a i j ki j k i j k

i j k i j k

b b bi j k i j k i j k

i j k i j k i j k i j k i j k

t tC

t tt t tC C C

x y z

, ,

, ,

' '

, , , ,

, ,, , , ,

' '

, , , ,

, , , ,, , , , , ,

12

1 12 2

1 , 12 2x y

i j k

i j k

i j k i j k

a i j ki j k i j k

i j k i j k

b bi j k i j k

i j k i j k i j k i

t

t tD

t tt tD D

x y

'

, ,

, ,, , , , , ,

, 12z

i j k

bi j k

j k i j k i j k

ttD

z

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.

Page 6: How to Program Fdtd 1824

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

6

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

Ey

ExEx

Ex Ex

Ez

Ez

Ez

Ez

Hz

Hz

Hx

Hx

Hy Hy

Ey

Ey

Ey

Ey

ExEx

Ex Ex

Ez

Ez

Ez

Ez

Hz

Hz

Hx

Hx

Hy Hy

Ey

Ey

Ey

Ey

ExEx

Ex Ex

Ez

Ez

Ez

Ez

Hz

Hz

Z

Y

X

Hx

Hx

Hy Hy

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 , ,y x zH E E . 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:

yH : NOC _ X NOC _ Y 1 NOC _ Z 1 3 1 1 4

xE : NOC _ X NOC _ Y 1 NOC _ Z 1 1 3 1 1 1 8

yE : NOC _ X NOC _ Y 1 NOC _ Z 1 1 3 1 1 1 8

Page 7: How to Program Fdtd 1824

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

7

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:

: _ _ 1 _yH NOC X NOC Y 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:

: _ _ 1 _ 1xE NOC X NOC Y NOC Z

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:

, , , : _ _ _x y zb b bCa C C C NOC X NOC Y NOC Z

The arrays containing the E field values:

: _ _ 1 _ 1

: _ 1 _ _ 1

: _ 1 _ 1 _

x

y

z

E NOC X NOC Y NOC Z

E NOC X NOC Y NOC Z

E NOC X NOC Y NOC Z

The arrays containing the H field values:

: _ 1 _ _

: _ _ 1 _

: _ _ _ 1

x

y

z

H NOC X NOC Y NOC Z

H NOC X NOC Y NOC Z

H NOC X NOC Y NOC Z

Page 8: How to Program Fdtd 1824

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

8

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 value0,1,1xE . The formula for Ex is:

1 2 1 21 1 2 1 2

, , , , , , , 1 2, , 1 2, , , 1 2 , , 1 2, ,, ,y z

n nn n n n

x a x b z z b y yi j k i j k i j k i j k i j k i j k i j ki j ki j kE C E C H H C H H

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:

1 2 1 2

, 1 2, 0,1,1

1 2 1 2

, 1 2, 0,0,11 1

1 2 1 2, , 0,1,1

, , 1 2 0,1,1

1 2 1 2

, , 1 2 0,1,0

n n

z zi j k

n n

z zi j kn n

n nx xi j k

y yi j k

n n

y yi j k

H H

H H

E EH H

H H

Calculating an H field component:

We want to calculate the value1,0,0xH . The formula for Ex is:

1 2 1 2

, , , , , , , 1 2, , 1 2,, , 1 2 , , 1 2, , , ,z y

n nn n n n

x a x b y y b z zi j k i j k i j k i j k i j ki j k i j ki j k i j kH D H D E E D E E

Page 9: How to Program Fdtd 1824

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

9

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:

, , 1 2 1,0,1

1 2 1 2 , , 1 2 1,0,0

, , 1,0,0

, 1 2, 1,1,0

, 1 2, 1,0,0

n n

y yi j k

n n

y yn n i j kx xi j k

n n

z zi j k

n n

z zi j k

E E

E EH H

E E

E E

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.

Page 10: How to Program Fdtd 1824

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

10

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.

Page 11: How to Program Fdtd 1824

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

11

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:

Page 12: How to Program Fdtd 1824

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

12

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.

Page 13: How to Program Fdtd 1824

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

13

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:

, , 0

' '

, , 0

i j k

i j k

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

, ,

, ,, , , ,0 0 0

1

, ,x x y y z z

a ai j k

b b b b b bi j ki j k i j k

D D

t t tD D D D D D

x y z

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* Ey i, j, k 1 Ey i, j, k Dby* Ez i, j 1, k Ez i, j, k

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 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

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 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 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

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

Page 14: How to Program Fdtd 1824

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

14

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.

Page 15: How to Program Fdtd 1824

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

15

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.

Page 16: How to Program Fdtd 1824

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

16

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 1st value to the last)

but we calculate from the inner values for the other two directions (starting from the 2nd value to the one before the

last). For example for Ex we have:

Page 17: How to Program Fdtd 1824

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

17

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.

Page 18: How to Program Fdtd 1824

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

18

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

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

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.

Page 19: How to Program Fdtd 1824

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

19

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.

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

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.

Page 20: How to Program Fdtd 1824

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

20

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 _ , NOC _Y Z .

The size of the cell edges in the three directions in millimeters in the form of three variables namedCELL_SIZEx,CELL_SIZEy,CELL_SIZEz .

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))

Page 21: How to Program Fdtd 1824

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

21

#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.

Page 22: How to Program Fdtd 1824

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

22

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])

Page 23: How to Program Fdtd 1824

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

23

#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])

Page 24: How to Program Fdtd 1824

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

24

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.