-
Notifications
You must be signed in to change notification settings - Fork 68
/
fdtd-3d.tex
2292 lines (2036 loc) · 87.3 KB
/
fdtd-3d.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\chapter{Three-Dimensional FDTD \label{chap:3d}}
%\setcounter{page}{1}
\renewcommand{\thefootnote}{\fnsymbol{footnote}}
\footnotetext{Lecture notes by John Schneider. {\tt
fdtd-3d.tex}}
\section{Introduction}
With an understanding of the FDTD implementation of TE$^z$ and TM$^z$
grids, the additional steps needed to implement a three-dimensional
(3D) grid are almost trivial. A 3D grid can be viewed as stacked
layers of TE$^z$ and TM$^z$ grids which are offset a half spatial step
in the $z$ direction. The update equations for the $H_z$ and $E_z$
nodes are nearly identical to those which have been given
already---the only difference is an additional index to specify the
$z$ location. The update equations for the other field components
require slight changes to account for variations in the $z$ direction
(i.e., in the governing equations the partial derivative with respect to
$z$ is no longer zero).
We begin this chapter by discussing the implementation of 3D arrays in
C. This is followed by details concerning the arrangement of nodes in
3D and the associated update equations. The chapter concludes with
the code for an incremental dipole in a homogeneous space.
\section{3D Arrays in C \label{sec:3darrays}}
For fields in a 3D space, it is, of course, natural to specify the
location of a node using three indices representing the
displacement in the $x$, $y$, and $z$ directions. However, as was
done for 2D grids, we will use a macro to translate the given indices
into an offset into a 1D array. The memory associated with the 1D
array will be allocated dynamically and the amount of memory will be
precisely what is needed to store all the elements of the 3D
``array.'' (We will refer to the macro as a 3D array since, other
than the cleaner specification of the indices, its use in the code is
indistinguishable from a traditional 3D array.)
For 3D arrays, incrementing the third index by one changes the
variable being specified to the next consecutive variable in memory.
Thinking of the third index as corresponding to the $z$ direction,
this implies that nodes that are adjacent to each other in the $z$
direction are also adjacent to each other in memory. On the other
hand, when the first or second index is incremented by one, that will
{\em not} correspond to the next variable in memory. When the second
index is incremented, one must move forward in memory an amount
corresponding to the number of variables in the third dimension. For
example, if the array size in the third dimension was $32$ elements,
then incrementing the second index by one would require that the
offset in memory be advanced by $32$. This is the same as in the 2D
case where we can think of the size of the third dimension as
corresponding to the number of columns (or, said another way, the
number of elements in a row).
When the first index is incremented by one, the offset in memory must
account for the array size in both the second and third dimension. To
illustrate this, consider Fig.\ \ref{fig:3Dmacro} which shows the
elements of the 3D array {\tt Ez}. The array is $3\times 4\times 3$,
corresponding to the dimensions in the $x$, $y$ and $z$ directions.
In reality, these elements will map to elements of a 1D array called
{\tt ez} which is shown in \ref{fig:3Darray}. Since {\tt ez} is a 1D
array, it takes a single index (or offset). Note that if one holds
the $m$ and $n$ indices fixed (corresponding to the $x$ and $y$
directions) but increments the $p$ index (corresponding to a movement
in the $z$ direction), the index of {\tt ez} changes by one. However,
if $m$ and $p$ are held fixed and $n$ is incremented by one, the index
of {\tt ez} changed by $3$ which correspond to the number of elements
in the $z$ directions. Finally, if $n$ and $p$ are held fixed but $m$
is incremented by one, the index of {\tt ez} changed by $12$ which is
the product of the dimensions in the $y$ and $z$ directions.
Three-dimensional arrays can be thought of as a collection of 2D
arrays. For the way in which we perform the indexing, the 2D arrays
correspond to constant-$x$ planes. Each of these 2D arrays must be
large enough to hold the product of the number of elements along the
$y$ and $z$ directions.
\begin{figure}
\begin{center}
\epsfig{width=4.5in,file=Figures/Fdtd-3d/3d-array-macro.eps}
\end{center} \caption{Depiction of elements of an array with
dimensions $3\times 4\times 3$ in the $x$, $y$, and $z$ directions,
respectively. The indices $m$, $n$, and $p$, are used to specify
the $x$, $y$, and $z$ locations, respectively. The element at the
``origin'' has indices $(0,0,0)$ and is shown in the upper left
corner of the bottom plane.} \label{fig:3Dmacro}
\end{figure}
\begin{figure}
\begin{center}
\epsfig{width=4.5in,file=Figures/Fdtd-3d/3d-array.eps} \end{center}
\caption{The 1D array {\tt ez} is used to store the elements of {\tt
Ez}. The three indices for each elements of {\tt Ez} shown in Fig.\
\ref{fig:3Dmacro} map to the single index shown here.}
\label{fig:3Darray}
\end{figure}
The construct we use for 3D arrays largely parallels that which was
used for 2D arrays. The allocation macro {\tt ALLOC\_3D()} is shown
in Fragment \ref{frag:alloc3d}. The only difference between this and
the allocation macros shown previously is the addition of another
argument to specify the size of the array in the third dimension (this
is the argument {\tt NUMZ}). This dimension is multiplied by the other
two dimensions and used as the first argument of {\tt calloc()}.
\begin{fragment}
Macro for allocating memory for a 3D array.
\label{frag:alloc3d}
\codemiddle
\begin{lstlisting}
#define ALLOC_3D(PNTR, NUMX, NUMY, NUMZ, TYPE) \
PNTR = (TYPE *)calloc((NUMX) * (NUMY) * (NUMZ), sizeof(TYPE)); \
if (!PNTR) { \
perror("ALLOC_3D"); \
fprintf(stderr, \
"Allocation failed for " #PNTR ". Terminating...\n"); \
exit(-1); \
}
\end{lstlisting}
\end{fragment}
To illustrate the construction and use of a 3D array, the code in
Fragment \ref{frag:3dArrayDemo} shows how one could create a $6\times
7\times 8$ array. In this example the array dimensions are set in
{\tt \#define}-statements in lines $1$--$3$. Line $5$ provides the
macro {\tt Ez()} which takes three (dummy) arguments. The
preprocessor will replace all occurrences of {\tt Ez()} with the
expression involving {\tt ez[]} shown at the right. The pointer {\tt
ez} is defined in line $6$ and initially at run-time does not have any
memory associated with it. However, after line $9$ has executed {\tt
ez} will point to a block of memory that is sufficient to hold all the
elements of the array and, at this point, {\tt ez} can be treated as a
1D array (but we never use {\tt ez} directly in the code---instead, we
use the macro {\tt Ez()} to access array elements). The nested
for-loops starting at line $11$ merely set each element equal to the
product of the indices for that element. Note that this order of
nesting is the one that should be used in practice: the inner-most
loop should be over the $z$ index and the outer-most loop should be
over the $x$ index. (This order helps minimize page faults and hence
maximize performance.)
\begin{fragment}
Demonstration of the construction and manipulation of a $6\times
7\times 8$ array.
\label{frag:3dArrayDemo}
\codemiddle
\begin{lstlisting}
#define num_rows 8
#define num_columns 7
#define num_planes 6
#define Ez(M, N, P) ez[((M) * num_columns + (N)) * num_rows + (P)]
\end{lstlisting}
\mbox{}\hspace{0.5in}$\vdots$
\begin{lstlisting}[firstnumber = last]
double *ez;
int m, n, p;
ALLOC_3D(ez, num_planes, num_columns, num_rows, double);
for (m = 0; m < num_planes; m++)
for (n = 0; n < num_columns; n++)
for (p = 0; p < num_rows; p++)
Ez(m, n, p) = m * n * p;
\end{lstlisting}
\end{fragment}
\section{Governing Equations and the 3D Grid}
As has been the case previously, Ampere's and Faraday's laws are the
relevant governing equations in constructing the FDTD algorithm.
These equations are
\begin{equation}
-\sigma_m\Hvec -\mu \frac{\partial \Hvec}{\partial t} =
\nabla \times \Evec =
\left|
\begin{array}{ccc}
\unitvec{x} & \unitvec{y} & \unitvec{z} \\
\frac{\partial}{\partial x}&
\frac{\partial}{\partial y}&
\frac{\partial}{\partial z} \\
E_x & E_y & E_z
\end{array}
\right|,
\label{eq:faraday3d}
\end{equation}
\begin{equation}
\sigma \Evec + \epsilon \frac{\partial \Evec}{\partial t} =
\nabla \times \Hvec =
\left|
\begin{array}{ccc}
\unitvec{x} & \unitvec{y} & \unitvec{z} \\
\frac{\partial}{\partial x}&
\frac{\partial}{\partial x}&
\frac{\partial}{\partial z} \\
H_x & H_y & H_z
\end{array}
\right|.
\label{eq:ampere3d}
\end{equation}
The components of these equations, when approximated by
finite-differences at the appropriate points in space-time, yield the
discretized update equations.
The necessary arrangement of nodes is show in Fig.\ \ref{fig:nodes3d}.
This grouping of six nodes can be considered the fundamental building
block of a 3D grid. The following notation is used:
\begin{eqnarray}
H_x(x,y,z,t) \!&=&\! H_x(m\Delx, n\Dely, p\Delz, q\Delt) =
\fdtd{H_x}{m,n,p}{q}, \\
H_y(x,y,z,t) \!&=&\! H_y(m\Delx, n\Dely, p\Delz, q\Delt) =
\fdtd{H_y}{m,n,p}{q}, \\
H_z(x,y,z,t) \!&=&\! H_z(m\Delx, n\Dely, p\Delz, q\Delt) =
\fdtd{H_z}{m,n,p}{q}, \\
E_x(x,y,z,t) \!&=&\! E_x(m\Delx, n\Dely, p\Delz, q\Delt) =
\fdtd{E_x}{m,n,p}{q}, \\
E_y(x,y,z,t) \!&=&\! E_y(m\Delx, n\Dely, p\Delz, q\Delt) =
\fdtd{E_y}{m,n,p}{q}, \\
E_z(x,y,z,t) \!&=&\! E_z(m\Delx, n\Dely, p\Delz, q\Delt) =
\fdtd{E_z}{m,n,p}{q}.
\end{eqnarray}
In Fig.\ \ref{fig:nodes3d} the temporal location of the nodes is not
specified. It is assumed the electric-field nodes exist at integer
multiples of the time step and the magnetic-field nodes exists
one-half of a temporal step away from the electric field nodes. As we
will see when we implement the 3D algorithm in a computer program, the
halves are suppressed and these six nodes will all have the same
indices. Note that, for any given set of indices the electric-field
nodes are displaced a half step in the direction in which they point
while magnetic-field nodes are displaced a half step in the two
directions they do not point.
\begin{figure}
\begin{center}
\epsfig{width=4.5in,file=Figures/Fdtd-3d/fdtd-3d-nodes.eps}
\end{center} \caption{Arrangement of nodes in three dimensions. In
a computer program all these nodes would have the same $m$, $n$, and
$p$ indices (the one-halves would be discarded from the
equations---the offset would be understood). Electric-field nodes
are displaced a half step in the direction in which they point while
magnetic-field nodes are displaced a half step in the two directions
they do not point. It is also implicitly understood that the
electric- and magnetic-field nodes are offset from each other a half
step in time.} \label{fig:nodes3d}
\end{figure}
Another view of a portion of the 3D grid is shown in Fig.\
\ref{fig:yeeCube}. This type of depiction is typically call the Yee
cube or Yee cell. This cube consists of electric-field nodes on the
edges of the cube (hence four nodes of each electric-field component)
and magnetic-field nodes on the faces (two nodes of each
magnetic-field component). In a 3D grid one can shift the origin of
this cube so that magnetic-field nodes are along the edges and
electric-field nodes are on the faces. Although this is done by some
authors, we will use the arrangement shown in Fig.\
\ref{fig:yeeCube}.
\begin{figure}
\begin{center}
\epsfig{width=4.5in,file=Figures/Fdtd-3d/yee-cube.eps} \end{center}
\caption{The nodes in a 3D FDTD grid are often drawn in the form of
a Yee cube or Yee cell. In this depiction the nodes do not all have
the same indices. As drawn here the cube would consist of four
$E_x$ nodes, four $E_y$ nodes, and four $E_z$ nodes, i.e., the
electric fields are along the cube edges. Magnetic fields are on
the cube faces and hence there would be two $H_x$ nodes, two $H_y$
nodes, and two $H_z$ nodes.} \label{fig:yeeCube}
\end{figure}
With the arrangement of nodes shown in Figs.\ \ref{fig:nodes3d} and
\ref{fig:yeeCube}, the components of \refeq{eq:faraday3d} and
\refeq{eq:ampere3d} expressed at the appropriate evaluation points are
\begin{eqnarray}
-\sigma_m H_x - \mu\frac{\partial H_x}{\partial t} &=&
\left. \frac{\partial E_z}{\partial y} - \frac{\partial E_y}{\partial
z} \right|_{x=m\Delx,y=(n+1/2)\Dely,z=(p+1/2)\Delz,t=q\Delt}, \\
-\sigma_m H_y - \mu\frac{\partial H_y}{\partial t} &=&
\left.\frac{\partial E_x}{\partial z} -\frac{\partial E_z}{\partial x}
\right|_{x=(m+1/2)\Delx,y=n\Dely,z=(p+1/2)\Delz,t=q\Delt}, \\
-\sigma_m H_z - \mu\frac{\partial H_z}{\partial t} &=&
\left.\frac{\partial E_y}{\partial x} -\frac{\partial E_x}{\partial y}
\right|_{x=(m+1/2)\Delx,y=(n+1/2)\Dely,z=p\Delz,t=q\Delt}, \\
\sigma E_x + \epsilon\frac{\partial E_x}{\partial t} &=&
\left.\frac{\partial H_z}{\partial y} - \frac{\partial H_y}{\partial
z} \right|_{x=(m+1/2)\Delx,y=n\Dely,z=p\Delz,t=(q+1/2)\Delt}, \\
\sigma E_y + \epsilon\frac{\partial E_y}{\partial t} &=&
\left.\frac{\partial H_x}{\partial z} -\frac{\partial H_z}{\partial x}
\right|_{x=m\Delx,y=(n+1/2)\Dely,z=p\Delz,t=(q+1/2)\Delt}, \\
\sigma E_z + \epsilon\frac{\partial E_z}{\partial t} &=&
\left.\frac{\partial H_y}{\partial x} -\frac{\partial H_x}{\partial y}
\right|_{x=m\Delx,y=n\Dely,z=(p+1/2)\Delz,t=(q+1/2)\Delt}.
\end{eqnarray}
In these equations, ignoring loss for a moment, the temporal
derivative of each field-component is always given by the spatial
derivative of two components of the ``other field.'' Also, the
components of one field are related to the two orthogonal components
of the other field. As has been done previously, the loss term can be
approximated by the average of the field at two times steps.
Given our experience with 1- and 2D grids, the 3D update equations can
be written simply by inspection of the governing equations in the
continuous world. The update equations are
\begin{multline}
\fdtdh{H_x}{m,n+\half,p+\half}{q+\half} =
\frac{1-\frac{\sigma_m\Delt}{2\mu}}{1+\frac{\sigma_m\Delt}{2\mu}}
\fdtdh{H_x}{m,n+\half,p+\half}{q-\half} \\
\hspace{.68in}\mbox{} +
\frac{1}{1+\frac{\sigma_m\Delt}{2\mu}}\left(
\frac{\Delt}{\mu\Delz}
\left\{\fdtdh{E_y}{m,n+\half,p+1}{q}-
\fdtdh{E_y}{m,n+\half,p}{q}\right\}\right. \\
\left.\mbox{} -
\frac{\Delt}{\mu\Dely}
\left\{\fdtdh{E_z}{m,n+1,p+\half}{q}-
\fdtdh{E_z}{m,n,p+\half}{q}\right\}\right),
\end{multline}
\begin{multline}
\fdtdh{H_y}{m+\half,n,p+\half}{q+\half} =
\frac{1-\frac{\sigma_m\Delt}{2\mu}}{1+\frac{\sigma_m\Delt}{2\mu}}
\fdtdh{H_y}{m+\half,n,p+\half}{q-\half} \\
\hspace{.68in}\mbox{} +
\frac{1}{1+\frac{\sigma_m\Delt}{2\mu}}\left(
\frac{\Delt}{\mu\Delx}
\left\{\fdtdh{E_z}{m+1,n,p+\half}{q}-
\fdtdh{E_z}{m,n,p+\half}{q}\right\}\right.\\
\left.\mbox{} -
\frac{\Delt}{\mu\Delz}
\left\{\fdtdh{E_x}{m+\half,n,p+1}{q}-
\fdtdh{E_x}{m+\half,n,p}{q}\right\}\right),
\end{multline}
\begin{multline}
\lefteqn{\fdtdh{H_z}{m+\half,n+\half,p}{q+\half} =
\frac{1-\frac{\sigma_m\Delt}{2\mu}}{1+\frac{\sigma_m\Delt}{2\mu}}
\fdtdh{H_z}{m+\half,n+\half,p}{q-\half}}
\\
\hspace{.68in}\mbox{} +
\frac{1}{1+\frac{\sigma_m\Delt}{2\epsilon}}
\left(
\frac{\Delt}{\mu\Dely}
\left\{
\fdtdh{E_x}{m+\half,n+1,p}{q} - \fdtdh{E_x}{m+\half,n,p}{q}
\right\} \right.\\
\left.\mbox{}-
\frac{\Delt}{\epsilon\Delx}
\left\{
\fdtdh{E_y}{m+1,n+\half,p}{q} - \fdtdh{E_y}{m,n+\half,p}{q}
\right\}
\right).
\end{multline}
\begin{multline}
\fdtdh{E_x}{m+\half,n,p}{q+1} =
\frac{1-\frac{\sigma\Delt}{2\epsilon}}{1+\frac{\sigma\Delt}{2\epsilon}}
\fdtdh{E_x}{m+\half,n,p}{q} \\
\hspace{.08in}\mbox{} +
\frac{1}{1+\frac{\sigma\Delt}{2\epsilon}}
\left(
\frac{\Delt}{\epsilon\Dely}
\left\{\fdtdh{H_z}{m+\half,n+\half,p}{q+\half}-
\fdtdh{H_z}{m+\half,n-\half,p}{q+\half}\right\}\right.
\\
\left. \mbox{} -
\frac{\Delt}{\epsilon\Delz}
\left\{\fdtdh{H_y}{m+\half,n,p+\half}{q+\half}-
\fdtdh{H_y}{m+\half,n,p-\half}{q+\half}\right\}\right),
\label{eq:exThreeDUpdate}
\end{multline}
\begin{multline}
\fdtdh{E_y}{m,n+\half,p}{q+1} =
\frac{1-\frac{\sigma\Delt}{2\epsilon}}{1+\frac{\sigma\Delt}{2\epsilon}}
\fdtdh{E_y}{m,n+\half}{q} \\
\hspace{.08in}\mbox{} +
\frac{1}{1+\frac{\sigma\Delt}{2\epsilon}}
\left(
\frac{\Delt}{\epsilon\Delz}
\left\{\fdtdh{H_x}{m,n+\half,p+\half}{q+\half}-
\fdtdh{H_x}{m,n+\half,p-\half}{q+\half}\right\}
\right. \\
\left. \mbox{} -
\frac{\Delt}{\epsilon\Delx}
\left\{\fdtdh{H_z}{m+\half,n+\half,p}{q+\half}-
\fdtdh{H_z}{m-\half,n+\half,p}{q+\half}\right\}\right),
\end{multline}
\begin{multline}
\lefteqn{\fdtdh{E_z}{m,n,p+\half}{q+1} =
\frac{1-\frac{\sigma\Delt}{2\epsilon}}{1+\frac{\sigma\Delt}{2\epsilon}}
\fdtdh{E_z}{m,n,p+\half}{q}} \\
\hspace{.08in}\mbox{} +
\frac{1}{1+\frac{\sigma\Delt}{2\epsilon}}
\left(
\frac{\Delt}{\epsilon\Delx}
\left\{
\fdtdh{H_y}{m+\half,n,p+\half}{q+\half} -
\fdtdh{H_y}{m-\half,n,p+\half}{q+\half}
\right\}\right.
\\
\hspace{1.0in} \mbox{} -
\left.
\frac{\Delt}{\epsilon\Dely}
\left\{
\fdtdh{H_x}{m,n+\half,p+\half}{q+\half} -
\fdtdh{H_x}{m,n-\half,p+\half}{q+\half}
\right\}
\right).
\end{multline}
The coefficients in the update equations are assumed constant (in
time) but may be functions of position. Consistent with the notation
adopted previously and assuming a uniform grid in which
$\Delx=\Dely=\Delz=\delta$, the magnetic-field update coefficients can
be expressed as
\begin{eqnarray}
\chxh(m,n+1/2,p+1/2) &=&
\left.
\frac{1-\frac{\sigma_m\Delt}{2\mu}}{1+\frac{\sigma_m\Delt}{2\mu}}
\right|_{m\delta,(n+1/2)\delta,(p+1/2)\delta},
\label{eq:chxhDef}
\\
\chxe(m,n+1/2,p+1/2) &=&
\left.
\frac{1}{1+\frac{\sigma_m\Delt}{2\mu}}\frac{\Delt}{\mu\delta}
\right|_{m\delta,(n+1/2)\delta,(p+1/2)\delta}, \\
\chyh(m+1/2,n,p+1/2) &=&
\left.
\frac{1-\frac{\sigma_m\Delt}{2\mu}}{1+\frac{\sigma_m\Delt}{2\mu}}
\right|_{(m+1/2)\delta,n\delta,(p+1/2)\delta},
\label{eq:chyhDef}
\\
\chye(m+1/2,n,p+1/2) &=&
\left.
\frac{1}{1+\frac{\sigma_m\Delt}{2\mu}}\frac{\Delt}{\mu\delta}
\right|_{(m+1/2)\delta,n\delta,(p+1/2)\delta}, \\
\chzh(m+1/2,n+1/2,p) &=&
\left.
\frac{1-\frac{\sigma_m\Delt}{2\mu}}{1+\frac{\sigma_m\Delt}{2\mu}}
\right|_{(m+1/2)\delta,(n+1/2)\delta,p\delta},
\label{eq:chzhDef}
\\
\chze(m+1/2,n+1/2,p) &=&
\left.
\frac{1}{1+\frac{\sigma_m\Delt}{2\mu}}\frac{\Delt}{\mu\delta}
\right|_{(m+1/2)\delta,(n+1/2)\delta,p\delta}.
\end{eqnarray}
For the electric-field update equations the coefficients are
\begin{align}
\cexe(m+1/2,n,p) &=
\left.
\frac{1-\frac{\sigma\Delt}{2\epsilon}}{1+\frac{\sigma\Delt}{2\epsilon}}
\right|_{(m+1/2)\delta,n\delta,p\delta},
\label{eq:cexeDef}
\\
\cexh(m+1/2,n,p) &=
\left.
\frac{1}{1+\frac{\sigma\Delt}{2\epsilon}}\frac{\Delt}{\epsilon\delta}
\right|_{(m+1/2)\delta,n\delta,p\delta}, \\
\ceye(m,n+1/2,p) &=
\left.
\frac{1-\frac{\sigma\Delt}{2\epsilon}}{1+\frac{\sigma\Delt}{2\epsilon}}
\right|_{m\delta,(n+1/2)\delta,p\delta},
\label{eq:ceyeDef}
\\
\ceyh(m,n+1/2,p) &=
\left.
\frac{1}{1+\frac{\sigma\Delt}{2\epsilon}}
\frac{\Delt}{\epsilon\delta}
\right|_{m\delta,(n+1/2)\delta,p\delta},\\
\ceze(m,n,p+1/2) &=
\left.
\frac{1-\frac{\sigma\Delt}{2\epsilon}}{1+\frac{\sigma\Delt}{2\epsilon}}
\right|_{m\delta,n\delta,(p+1/2)\delta},
\label{eq:cezeDef}
\\
\cezh(m,n,p+1/2) &=
\left.
\frac{1}{1+\frac{\sigma\Delt}{2\epsilon}}
\frac{\Delt}{\epsilon\delta}
\right|_{m\delta,n\delta,(p+1/2)\delta}.
\end{align}
These coefficients can be related to the Courant number
$c\Delt/\delta$. For a uniform grid in three dimensions the Courant
limit is $1/\sqrt{3}$. There are rigorous derivations of this limit
but there is also a simple empirical argument. It takes three
time-steps to communicate information across the diagonal of a cube in
the grid. The distance traveled across this diagonal is
$\sqrt{3}\delta$. To ensure stability we must have that the distance
traveled in the continuous world over these three time steps is less
than the distance over which the grid can communicate information.
Thus, we must have $c3\Delt\leq \sqrt{3}\delta$ or, rearranging,
$S_c\leq 1/\sqrt{3}$.
As has been done previously, the explicit reference to time is
dropped. Additionally, so that the indexing can be easily handled
within a computer program, the spatial offsets of one-half are dropped
explicitly but left implicitly understood. Thus, all one-halves are
discarded from the left side of the update equations. Nodes on the
right side of the equation will also have the one-halves dropped if
the node is within the same group of nodes as the node being updated
(where a group of nodes is as shown in Fig.\
\ref{fig:nodes3d}). However, if the node on the right side is
contained within a group that is a neighbor to the group that contains
the node being updated, the one-half is replaced with a one. To
illustrate further the grouping of nodes in three dimensions, Fig.\
\ref{fig:3dComputer} shows six groups of nodes and the corresponding
set of indices for each group. The update-equation coefficients are
evaluated at a point that is collocated with the node being updated.
Thus, the 3D update equations can be written (assuming a suitable
collection of macros which will be considered later):
\begin{verbatim}
Hx(m, n, p) = Chxh(m, n, p) * Hx(m, n, p) +
Chxe(m, n, p) * ((Ey(m, n, p + 1) - Ey(m, n, p)) -
(Ez(m, n + 1, p) - Ez(m, n, p)));
Hy(m, n, p) = Chyh(m, n, p) * Hy(m, n, p) +
Chye(m, n, p) * ((Ez(m + 1, n, p) - Ez(m, n, p)) -
(Ex(m, n, p + 1) - Ex(m, n, p)));
Hz(m, n, p) = Chzh(m, n, p) * Hz(m, n, p) +
Chze(m, n, p) * ((Ex(m, n + 1, p) - Ex(m, n, p)) -
(Ey(m + 1, n, p) - Ey(m, n, p)));
Ex(m, n, p) = Cexe(m, n, p) * Ex(m, n, p) +
Cexh(m, n, p) * ((Hz(m, n, p) - Hz(m, n - 1, p)) -
(Hy(m, n, p) - Hy(m, n, p - 1)));
Ey(m, n, p) = Ceye(m, n, p) * Ey(m, n, p) +
Ceyh(m, n, p) * ((Hx(m, n, p) - Hx(m, n, p - 1)) -
(Hz(m, n, p) - Hz(m - 1, n, p)));
Ez(m, n, p) = Ceze(m, n, p) * Ez(m, n, p) +
Cezh(m, n, p) * ((Hy(m, n, p) - Hy(m - 1, n, p)) -
(Hx(m, n, p) - Hx(m, n - 1, p)));
\end{verbatim}
\begin{figure}
\begin{center}
\epsfig{width=4.5in,file=Figures/Fdtd-3d/fdtd-3d-computer.eps}
\end{center}
\caption{Arrangement of six groups of nodes where all of the nodes
within the group have the same set of indices. The nodes in a group
are joined by gray lines and their indices are shown as an
ordered triplet in the center of the group.} \label{fig:3dComputer}
\end{figure}
In our construction of 3D grids, the faces of the grid will always be
terminated such that there are two electric-field components
tangential to the face and one magnetic field normal to it. This is
illustrated in Fig.\ \ref{fig:faces3d}. The computational domain
shown in this figure is one which we describe as having dimensions of
$5\times 9\times 7$ in the $x$, $y$, and $z$ directions, respectively.
Even though we call this a $5\times 9\times 7$ grid, none of the
arrays associated with this computational domain actually have these
dimensions! The fields of a computational domain that is $M\times
N\times P$ would have dimensions of
\begin{align}
E_x: & \quad (M-1) \times N \times P \\
E_y: & \quad M \times (N-1) \times P \\
E_z: & \quad M \times N \times (P-1) \\
H_x: & \quad M \times (N-1) \times (P-1) \\
H_y: & \quad (M-1) \times N \times (P-1) \\
H_z: & \quad (M-1) \times (N-1) \times P
\end{align}
Note that the electric fields have one less element in the direction
in which they point than the nominal size of this grid. This is
because of the inherent displacement of electric-field nodes in the
direction in which they point. Rather than having an additional node
essentially sticking beyond the rest of the grid, the array is
truncated in this direction. Recall that the displacement of the
magnetic-field nodes is in the two directions in which they do not
point. Thus the magnetic-field arrays are truncated in the two
directions they do not point. In terms of Yee cubes, an $M\times
N\times P$ grid would consists of $(M-1)\times (N-1)\times (P-1)$
complete cubes.
\begin{figure}
\begin{center}
\epsfig{width=5.5in,file=Figures/Fdtd-3d/fdtd-3d-faces.eps}
\end{center} \caption{Faces of a computational domain which is
$5\times 9\times 7$ in the $x$, $y$, and $z$ directions,
respectively. On the constant-$x$ face the tangential fields are
$E_y$ and $E_z$, on the constant-$y$ face they are $E_x$ and $E_z$,
and on the constant-$z$ face they are $E_x$ and $E_y$. There are also
magnetic-field nodes which exist on these faces but their orientation
is normal to the face.} \label{fig:faces3d}
\end{figure}
\section{3D Example}
Here we provide the code to implement a simple 3D simulation in which
a short dipole source is embedded in a homogeneous domain. The dipole
is merely an additive source applied to an $E_x$ node in the center of
the grid. First-order ABC's are used to terminate the grid. Since
there are two tangential electric fields on each face of the
computational domain, the ABC must be applied to two fields per face.
The {\tt main()} function is shown in Program \ref{pro:3dDemo}. The
overall structure is little changed from previous simulations. The
ABC, the grid, the source function, and the snapshot code are
initialized by calling initialization functions outside of the
time-stepping loop. Within the time-stepping loop the magnetic fields
are updated, the electric fields are updated, the source function is
applied to the $E_x$ node at the center of the grid, the ABC is
applied, and then, assuming it is the appropriate time step, a
snapshot is taken. Actually, as we will see, two different snapshots
are taken. There are many ways one might choose to display these 3D
vector fields. We will merely record one field component over a 2D
plane (or perhaps multiple planes).
\begin{program}
{\tt 3ddemo.c} 3D simulation of an electric dipole realized with an
additive source applied to an $E_x$ node.
\label{pro:3dDemo}
\codemiddle
\begin{lstlisting}
/* 3D simulation with dipole source at center of grid. */
#include "fdtd-alloc.h"
#include "fdtd-macro.h"
#include "fdtd-proto.h"
#include "ezinc.h"
int main()
{
Grid *g;
ALLOC_1D(g, 1, Grid); // allocate memory for grid structure
gridInit(g); // initialize 3D grid
abcInit(g); // initialize ABC
ezIncInit(g);
snapshot3dInit(g); // initialize snapshots
/* do time stepping */
for (Time = 0; Time < MaxTime; Time++) {
updateH(g); // update magnetic fields
updateE(g); // update electric fields
Ex((SizeX - 1) / 2, SizeY / 2, SizeZ / 2) += ezInc(Time, 0.0);
abc(g); // apply ABC
snapshot3d(g); // take a snapshot (if appropriate)
} // end of time-stepping
return 0;
}
\end{lstlisting}
\end{program}
The code used to realize the source function, i.e., the Ricker
wavelet, is unchanged from before and hence not shown (ref.\ Program
\ref{pro:ricker}). The header {\tt fdtd-alloc.h} merely provides the
three allocation macros {\tt ALLOC\_1D()}, {\tt ALLOC\_2D()}, and {\tt
ALLOC\_3D()} and hence is not shown here. Similarly, the header
{\tt fdtd-grid1.h}, which defines the elements of the {\tt Grid}
structure, is unchanged from before and thus not shown (ref.\ Program
\ref{pro:fdtdgrid1h}). The header {\tt fdtd-proto.h} provides the
prototypes for the various functions. Since these prototypes simply
show that each function takes a single argument (i.e., a pointer to a
{\tt Grid} structure), that header file is also not shown.
The header {\tt fdtd-macro.h} shown in Program \ref{pro:fdtdmacro}
provides macros for all the types of grids we have considered so far.
In this particular program we only need the macros for the 3D arrays,
but having created this collection of macros we are well prepared to
use it, unchanged, to tackle a wide variety of FDTD problems. As was
done in the previous chapter, there are macros which assume that the
{\tt Grid} structure is named {\tt g} while there is another set of
macros that allows the name of the {\tt Grid} to be specified
explicitly.
\begin{program} {\tt fdtd-macro.h} Header that provides the macros to
access the elements of any of the arrays that have been considered
thus far. One set of macros assumes the name of the {\tt Grid} is
{\tt g}. Another set allows the name of the {\tt Grid} to be
specified as an additional argument.
\label{pro:fdtdmacro}
\codemiddle
\begin{lstlisting}
#ifndef _FDTD_MACRO_H
#define _FDTD_MACRO_H
#include "fdtd-grid1.h"
/* macros that permit the "Grid" to be specified */
/* one-dimensional grid */
#define Hy1G(G, M) G->hy[M]
#define Chyh1G(G, M) G->chyh[M]
#define Chye1G(G, M) G->chye[M]
#define Ez1G(G, M) G->ez[M]
#define Ceze1G(G, M) G->ceze[M]
#define Cezh1G(G, M) G->cezh[M]
/* TMz grid */
#define Hx2G(G, M, N) G->hx[(M) * (SizeYG(G) - 1) + N]
#define Chxh2G(G, M, N) G->chxh[(M) * (SizeYG(G) - 1) + N]
#define Chxe2G(G, M, N) G->chxe[(M) * (SizeYG(G) - 1) + N]
#define Hy2G(G, M, N) G->hy[(M) * SizeYG(G) + N]
#define Chyh2G(G, M, N) G->chyh[(M) * SizeYG(G) + N]
#define Chye2G(G, M, N) G->chye[(M) * SizeYG(G) + N]
#define Ez2G(G, M, N) G->ez[(M) * SizeYG(G) + N]
#define Ceze2G(G, M, N) G->ceze[(M) * SizeYG(G) + N]
#define Cezh2G(G, M, N) G->cezh[(M) * SizeYG(G) + N]
/* TEz grid */
#define Ex2G(G, M, N) G->ex[(M) * SizeYG(G) + N]
#define Cexe2G(G, M, N) G->cexe[(M) * SizeYG(G) + N]
#define Cexh2G(G, M, N) G->cexh[(M) * SizeYG(G) + N]
#define Ey2G(G, M, N) G->ey[(M) * (SizeYG(G) - 1) + N]
#define Ceye2G(G, M, N) G->ceye[(M) * (SizeYG(G) - 1) + N]
#define Ceyh2G(G, M, N) G->ceyh[(M) * (SizeYG(G) - 1) + N]
#define Hz2G(G, M, N) G->hz[(M) * (SizeYG(G) - 1) + N]
#define Chzh2G(G, M, N) G->chzh[(M) * (SizeYG(G) - 1) + N]
#define Chze2G(G, M, N) G->chze[(M) * (SizeYG(G) - 1) + N]
/* 3D grid */
#define HxG(G, M, N, P) G->hx[((M) * (SizeYG(G) - 1) + N) * (SizeZG(G) - 1) + P]
#define ChxhG(G, M, N, P) G->chxh[((M) * (SizeYG(G) - 1) + N) * (SizeZG(G) - 1) + P]
#define ChxeG(G, M, N, P) G->chxe[((M) * (SizeYG(G) - 1) + N) * (SizeZG(G) - 1) + P]
#define HyG(G, M, N, P) G->hy[((M) * SizeYG(G) + N) * (SizeZG(G) - 1) + P]
#define ChyhG(G, M, N, P) G->chyh[((M) * SizeYG(G) + N) * (SizeZG(G) - 1) + P]
#define ChyeG(G, M, N, P) G->chye[((M) * SizeYG(G) + N) * (SizeZG(G) - 1) + P]
#define HzG(G, M, N, P) G->hz[((M) * (SizeYG(G) - 1) + N) * SizeZG(G) + P]
#define ChzhG(G, M, N, P) G->chzh[((M) * (SizeYG(G) - 1) + N) * SizeZG(G) + P]
#define ChzeG(G, M, N, P) G->chze[((M) * (SizeYG(G) - 1) + N) * SizeZG(G) + P]
#define ExG(G, M, N, P) G->ex[((M) * SizeYG(G) + N) * SizeZG(G) + P]
#define CexeG(G, M, N, P) G->cexe[((M) * SizeYG(G) + N) * SizeZG(G) + P]
#define CexhG(G, M, N, P) G->cexh[((M) * SizeYG(G) + N) * SizeZG(G) + P]
#define EyG(G, M, N, P) G->ey[((M) * (SizeYG(G) - 1) + N) * SizeZG(G) + P]
#define CeyeG(G, M, N, P) G->ceye[((M) * (SizeYG(G) - 1) + N) * SizeZG(G) + P]
#define CeyhG(G, M, N, P) G->ceyh[((M) * (SizeYG(G) - 1) + N) * SizeZG(G) + P]
#define EzG(G, M, N, P) G->ez[((M) * SizeYG(G) + N) * (SizeZG(G) - 1) + P]
#define CezeG(G, M, N, P) G->ceze[((M) * SizeYG(G) + N) * (SizeZG(G) - 1) + P]
#define CezhG(G, M, N, P) G->cezh[((M) * SizeYG(G) + N) * (SizeZG(G) - 1) + P]
#define SizeXG(G) G->sizeX
#define SizeYG(G) G->sizeY
#define SizeZG(G) G->sizeZ
#define TimeG(G) G->time
#define MaxTimeG(G) G->maxTime
#define CdtdsG(G) G->cdtds
#define TypeG(G) G->type
/* macros that assume the "Grid" is "g" */
/* one-dimensional grid */
#define Hy1(M) Hy1G(g, M)
#define Chyh1(M) Chyh1G(g, M)
#define Chye1(M) Chye1G(g, M)
#define Ez1(M) Ez1G(g, M)
#define Ceze1(M) Ceze1G(g, M)
#define Cezh1(M) Cezh1G(g, M)
/* TMz grid */
#define Hx2(M, N) Hx2G(g, M, N)
#define Chxh2(M, N) Chxh2G(g, M, N)
#define Chxe2(M, N) Chxe2G(g, M, N)
#define Hy2(M, N) Hy2G(g, M, N)
#define Chyh2(M, N) Chyh2G(g, M, N)
#define Chye2(M, N) Chye2G(g, M, N)
#define Ez2(M, N) Ez2G(g, M, N)
#define Ceze2(M, N) Ceze2G(g, M, N)
#define Cezh2(M, N) Cezh2G(g, M, N)
/* TEz grid */
#define Hz2(M, N) Hz2G(g, M, N)
#define Chzh2(M, N) Chzh2G(g, M, N)
#define Chze2(M, N) Chze2G(g, M, N)
#define Ex2(M, N) Ex2G(g, M, N)
#define Cexe2(M, N) Cexe2G(g, M, N)
#define Cexh2(M, N) Cexh2G(g, M, N)
#define Ey2(M, N) Ey2G(g, M, N)
#define Ceye2(M, N) Ceye2G(g, M, N)
#define Ceyh2(M, N) Ceyh2G(g, M, N)
/* 3D grid */
#define Hx(M, N, P) HxG(g, M, N, P)
#define Chxh(M, N, P) ChxhG(g, M, N, P)
#define Chxe(M, N, P) ChxeG(g, M, N, P)
#define Hy(M, N, P) HyG(g, M, N, P)
#define Chyh(M, N, P) ChyhG(g, M, N, P)
#define Chye(M, N, P) ChyeG(g, M, N, P)
#define Hz(M, N, P) HzG(g, M, N, P)
#define Chzh(M, N, P) ChzhG(g, M, N, P)
#define Chze(M, N, P) ChzeG(g, M, N, P)
#define Ex(M, N, P) ExG(g, M, N, P)
#define Cexe(M, N, P) CexeG(g, M, N, P)
#define Cexh(M, N, P) CexhG(g, M, N, P)
#define Ey(M, N, P) EyG(g, M, N, P)
#define Ceye(M, N, P) CeyeG(g, M, N, P)
#define Ceyh(M, N, P) CeyhG(g, M, N, P)
#define Ez(M, N, P) EzG(g, M, N, P)
#define Ceze(M, N, P) CezeG(g, M, N, P)
#define Cezh(M, N, P) CezhG(g, M, N, P)
#define SizeX SizeXG(g)
#define SizeY SizeYG(g)
#define SizeZ SizeZG(g)
#define Time TimeG(g)
#define MaxTime MaxTimeG(g)
#define Cdtds CdtdsG(g)
#define Type TypeG(g)
#endif
\end{lstlisting}
\end{program}
The file {\tt update3d.c} is shown in Program \ref{pro:update3d}.
When {\tt updateE()} or {\tt updateH()} are called they begin by
checking the {\tt Type} of the grid. These same functions can be
called whether updating a 1D, 2D, or 3D grid. However, for the 1D
grid there is the assumption that one is dealing with a $z$-polarized
wave and for 2D propagation one has either TM$^z$- or
TE$^z$-polarization. (A rotation of coordinate systems can be used to
map any 1D simulation to one that is $z$-polarized or any 2D
simulation to one that is either TE$^z$- or TM$^z$-polarized.)
\begin{program}
{\tt update3d.c} Function that can be used to update any of the grids.
\label{pro:update3d}
\codemiddle
\begin{lstlisting}
#include "fdtd-macro.h"
#include <stdio.h>
/* update magnetic field */
void updateH(Grid *g) {
int mm, nn, pp;
if (Type == oneDGrid) {
for (mm = 0; mm < SizeX - 1; mm++)
Hy1(mm) = Chyh1(mm) * Hy1(mm)
+ Chye1(mm) * (Ez1(mm + 1) - Ez1(mm));
} else if (Type == tmZGrid) {
for (mm = 0; mm < SizeX; mm++)
for (nn = 0; nn < SizeY - 1; nn++)
Hx2(mm, nn) = Chxh2(mm, nn) * Hx2(mm, nn)
- Chxe2(mm, nn) * (Ez2(mm, nn + 1) - Ez2(mm, nn));
for (mm = 0; mm < SizeX - 1; mm++)
for (nn = 0; nn < SizeY; nn++)
Hy2(mm, nn) = Chyh2(mm, nn) * Hy2(mm, nn)
+ Chye2(mm, nn) * (Ez2(mm + 1, nn) - Ez2(mm, nn));
} else if (Type == teZGrid) {
for(mm = 0; mm < SizeX - 1; mm++)
for(nn = 0; nn < SizeY - 1; nn++)
Hz2(mm, nn) = Chzh2(mm, nn) * Hz2(mm, nn) -
Chze2(mm, nn) * ((Ey2(mm + 1, nn) - Ey2(mm, nn)) -
(Ex2(mm, nn + 1) - Ex2(mm, nn)));
} else if (Type == threeDGrid) {
for (mm = 0; mm < SizeX; mm++)
for (nn = 0; nn < SizeY - 1; nn++)
for (pp = 0; pp < SizeZ - 1; pp++)
Hx(mm, nn, pp) = Chxh(mm, nn, pp) * Hx(mm, nn, pp) +
Chxe(mm, nn, pp) * ((Ey(mm, nn, pp + 1) - Ey(mm, nn, pp)) -
(Ez(mm, nn + 1, pp) - Ez(mm, nn, pp)));
for (mm = 0; mm < SizeX - 1; mm++)
for (nn = 0; nn < SizeY; nn++)
for (pp = 0; pp < SizeZ - 1; pp++)
Hy(mm, nn, pp) = Chyh(mm, nn, pp) * Hy(mm, nn, pp) +
Chye(mm, nn, pp) * ((Ez(mm + 1, nn, pp) - Ez(mm, nn, pp)) -
(Ex(mm, nn, pp + 1) - Ex(mm, nn, pp)));
for (mm = 0; mm < SizeX - 1; mm++)
for (nn = 0; nn < SizeY - 1; nn++)
for (pp = 0; pp < SizeZ; pp++)
Hz(mm, nn, pp) = Chzh(mm, nn, pp) * Hz(mm, nn, pp) +
Chze(mm, nn, pp) * ((Ex(mm, nn + 1, pp) - Ex(mm, nn, pp)) -
(Ey(mm + 1, nn, pp) - Ey(mm, nn, pp)));
} else {
fprintf(stderr, "updateH: Unknown grid type. Terminating...\n");
}
return;
} /* end updateH() */
/* update electric field */
void updateE(Grid *g) {
int mm, nn, pp;
if (Type == oneDGrid) {
for (mm = 1; mm < SizeX - 1; mm++)
Ez1(mm) = Ceze1(mm) * Ez1(mm)
+ Cezh1(mm) * (Hy1(mm) - Hy1(mm - 1));
} else if (Type == tmZGrid) {
for (mm = 1; mm < SizeX - 1; mm++)
for (nn = 1; nn < SizeY - 1; nn++)
Ez2(mm, nn) = Ceze2(mm, nn) * Ez2(mm, nn) +
Cezh2(mm, nn) * ((Hy2(mm, nn) - Hy2(mm - 1, nn)) -
(Hx2(mm, nn) - Hx2(mm, nn - 1)));
} else if (Type == teZGrid) {
for(mm = 1; mm < SizeX - 1; mm++)
for(nn = 1; nn < SizeY - 1; nn++)
Ex2(mm, nn) = Cexe2(mm, nn) * Ex2(mm, nn) +
Cexh2(mm, nn) * (Hz2(mm, nn) - Hz2(mm, nn - 1));
for(mm = 1; mm < SizeX - 1; mm++)
for(nn = 1; nn < SizeY - 1; nn++)
Ey2(mm, nn) = Ceye2(mm, nn) * Ey2(mm, nn) -
Ceyh2(mm, nn) * (Hz2(mm, nn) - Hz2(mm - 1, nn));
} else if (Type == threeDGrid) {
for (mm = 0; mm < SizeX - 1; mm++)
for (nn = 1; nn < SizeY - 1; nn++)
for (pp = 1; pp < SizeZ - 1; pp++)
Ex(mm, nn, pp) = Cexe(mm, nn, pp) * Ex(mm, nn, pp) +
Cexh(mm, nn, pp) * ((Hz(mm, nn, pp) - Hz(mm, nn - 1, pp)) -
(Hy(mm, nn, pp) - Hy(mm, nn, pp - 1)));
for (mm = 1; mm < SizeX - 1; mm++)
for (nn = 0; nn < SizeY - 1; nn++)
for (pp = 1; pp < SizeZ - 1; pp++)
Ey(mm, nn, pp) = Ceye(mm, nn, pp) * Ey(mm, nn, pp) +
Ceyh(mm, nn, pp) * ((Hx(mm, nn, pp) - Hx(mm, nn, pp - 1)) -
(Hz(mm, nn, pp) - Hz(mm - 1, nn, pp)));
for (mm = 1; mm < SizeX - 1; mm++)
for (nn = 1; nn < SizeY - 1; nn++)
for (pp = 0; pp < SizeZ - 1; pp++)
Ez(mm, nn, pp) = Ceze(mm, nn, pp) * Ez(mm, nn, pp) +
Cezh(mm, nn, pp) * ((Hy(mm, nn, pp) - Hy(mm - 1, nn, pp)) -
(Hx(mm, nn, pp) - Hx(mm, nn - 1, pp)));
} else {
fprintf(stderr, "updateE: Unknown grid type. Terminating...\n");
}
return;
} /* end updateE() */
\end{lstlisting}
\end{program}
The code to realize the first-order ABC is shown in Program
\ref{pro:abc3dfirst}. A first-order ABC requires that a single
``old'' value be recorded for each electric field that is tangential
to a face of the grid. There are two tangential components per face.
For example, at the ``$x=0$'' face, $E_y$ and $E_z$ are the tangential
components. These fields are stored in arrays named {\tt Eyx0(n, p)}
and {\tt Ezx0(n, p)}. The ``{\tt x0}'' part of the name specifies
that these values are at the start of the grid in the $x$-direction.
Since these old fields are recorded over a constant-$x$ face, only the
indices corresponding to the $y$ and $z$ directions are specified
(hence these arrays only take two indices). The array {\tt Eyx1(n,
p)} and {\tt Ezx1(n, p)} correspond to the tangential field at the
end of the grid in the $x$-direction. There are similarly named
arrays for the other two directions.
\begin{program}
{\tt abc3dfirst.c} The code used to implement a first-order ABC on
each face of the 3D domain.
\label{pro:abc3dfirst}
\codemiddle
\begin{lstlisting}
#include "fdtd-alloc.h"
#include "fdtd-macro.h"
/* Macros to access stored "old" value */
#define Eyx0(N, P) eyx0[(N) * (SizeZ) + (P)]
#define Ezx0(N, P) ezx0[(N) * (SizeZ - 1) + (P)]
#define Eyx1(N, P) eyx1[(N) * (SizeZ) + (P)]
#define Ezx1(N, P) ezx1[(N) * (SizeZ - 1) + (P)]
#define Exy0(M, P) exy0[(M) * (SizeZ) + (P)]
#define Ezy0(M, P) ezy0[(M) * (SizeZ - 1) + (P)]
#define Exy1(M, P) exy1[(M) * (SizeZ) + (P)]
#define Ezy1(M, P) ezy1[(M) * (SizeZ - 1) + (P)]
#define Exz0(M, N) exz0[(M) * (SizeY) + (N)]
#define Eyz0(M, N) eyz0[(M) * (SizeY - 1) + (N)]
#define Exz1(M, N) exz1[(M) * (SizeY) + (N)]
#define Eyz1(M, N) eyz1[(M) * (SizeY - 1) + (N)]
/* global variables not visible outside of this package */