1
How to create complex FDTD FORTRAN/C codes simply with MATHEMATICA®
Salvador Gonzalez Garcia, Member IEEE, Rafael Gómez López, and Amelia Rubio Bretones, Senior Member IEEE
Abstract— In this paper we show how the symbolic capabilities of the commercial package MATHEMATICA® can be used to facilitate the generation of FORTRAN/C codes to solve Maxwell differential equations in the time domain with finite-difference schemes. Index Terms—FDTD methods, programming, MATHEMATICA.
I. INTRODUCTION The Finite Difference Time Domain (FDTD) method [1] has become a standard simulation technique in computational electromagnetics. It has been extended and improved for accurately treating almost any kind of problem involving complex materials with arbitrary geometry, and it continues to be an active field of research [2]. The method has become so powerful that it is used as the computational core of not only many in-house tools but also commercial packages
The authors are with the Dept. Electromagnetism and Matter Physics. University of Granada. 18071 Granada (Spain). (corresponding author e-mail:
[email protected]).
2
used to design electromagnetic systems1 (CFDTD, CST, EMPIRE, XFDTD, etc.). At the same time, the FDTD fundamentals are simple enough to have become part of the syllabi of many undergraduate courses in electrodynamics, since it directly solves Maxwell differential equations in the time domain by replacing derivatives by differences. Furthermore, the timedomain nature of FDTD-based programs enables the visualization of the causal evolution of the solution, aiding both the students and the researchers to develop a strong intuition for complex electromagnetic phenomena, without getting lost in the math. The construction of basic FORTRAN/C FDTD programs for simple materials is an easy task, and the resulting codes can run extremely fast in today’s computers, using state-of-the-art compilers. Nevertheless, when the method is to be applied to more realistic situations, involving complex materials truncated by appropriate boundary conditions, the programming task becomes cumbersome even for experienced programmers. The motivation for this work arose when we began to build FORTRAN codes extending the original FDTD and the Perfectly Matched Layer (PML) [3] techniques to handle anisotropic, chiral and bi-anisotropic media (with higher orders of approximation) [4]-[8]. We realized that, after getting the starting set of analytical equations and choosing an appropriate approximation, valuable time was lost struggling against the messy mixture of indexes, in exhausting sessions of debugging hand-coded programs. Nevertheless, the starting point was similar in all cases: a set of finite-difference equations which, written in a compact operational form, did not greatly differ between them [9][10]. Thus we thought that it might be worth our effort to construct an automatic code generator able to create FORTRAN/C codes just from the original analytical equations. To help us in this task, we chose the commercial package MATHEMATICA® [11],
1
For an up-to-date state-of-the-art visit http://www.fdtd.org
3
among other possibilities, for its ability to define operators and their algebraic properties in a very straightforward manner, for its symbolic manipulation and simplification capabilities, and for being able to generate its output as FORTRAN/C statements (in addition to the authors’ fondness for this extraordinary tool). In this work, we show how to handle MATHEMATICA® to create FDTD codes with low effort even for complex situations. In Section II we briefly describe the classical component approach to build an FDTD code. Then, in Section III we employ the operational form of Maxwell equations for source-free linear isotropic homogeneous lossy media to illustrate the main steps of the code generator. We begin by writing the FDTD numerical equations also in an operational form completely analogous to the analytical one after defining a set of centered average and difference operators [9][10]. These operators, together with their linear properties, are specified in a simple manner in MATHEMATICA®, which, after manipulating and simplifying the initial operational equations, directly puts out the well-known Yee-FDTD algorithm as FORTRAN/C statements. Although no apparent advantage over a hand-coded program is appreciable in this initial simple case, in Section IV we give some examples of how powerful this procedure can be in more complicated situations in the field of research. For the first example, we extend this procedure to anisotropic materials simply by redefining the difference operators and the field positions at the unit cell, and subsequently using the same algorithm to generate the FORTRAN/C code described in the previous section. We next show how a slight change in the definition of the operators can increase the spatial order of accuracy of the final algorithm from 2 to 4, without altering the kernel of the code generator. Finally, some conclusions are presented in Section V. The examples that we give here can be downloaded from http://maxwell.ugr.es/mathfdtd,
4
where we plan to maintain a database with material on this topic. These MATHEMATICA® notebooks are written with a structure which may not be the most elegant or optimal one, but has been chosen for its simplicity. The readers are welcome to improve these codes or create new ones that we will be pleased to include in our website.
II. FDTD CLASSICAL APPROACH Let us assume, for simplicity, the source-free Maxwell curl equations with linear isotropic homogeneous electric and magnetic lossy media in Cartesian coordinates in an unbounded region
∂ t E − ε −1curl H + σε −1 E = 0 , ∂ t H + µ −1curl E + σ * µ −1 H = 0
(1)
The well-known classical construction of the Yee-FDTD scheme [1] starts by developing Eq. (1) into components. Then all the derivatives are replaced by second-order-accurate centered differences at the staggered integer and semi-integer positions defined in the Yee unit cell (Fig. 1), and the fields affected by the electric and magnetic conductivities are averaged in time ∂ λψ (λ∆λ ,…) →
ψ ( (λ + 12 ) ∆λ ,…) − ψ ( (λ − 12 ) ∆λ ,…)
σψ (n∆t ,…) → σ
∆λ
, λ = { x, y , z , t }
ψ ( (n + 12 ) ∆t ,…) + ψ ( (n − 12 ) ∆t ,…)
(2)
2
The FORTRAN/C code is built by mapping the integer and semi-integer analytical indexes to computational integer ones (for instance in Fig. 1 all the enclosed components are chosen to share the same computational position (i,j,k)), to finally get instructions (using a notation similar to Taflove’s [2]) such as Ez (i, j , k ) = Caez (i, j , k ) * Ez (i, j , k ) + Cbez (i, j , k ) * (( Hx(i, j , k ) − Hx(i, j + 1, k )) / deltay + ( Hy (i + 1, j , k ) − Hy (i, j , k )) / deltaz )
(3)
5
Caez, Cbez1, Cbez 2 are matrices of constants depending on the permittivity, permeability and
conductivities sampled at each spatial position, and deltay, deltaz the space steps. With this layout, a straight FDTD code with some simple boundary conditions (e.g. perfect conductor) is readily built. Nevertheless, if we need to extend the method to more complex situations the building of the code is not so straightforward.
III. FDTD
®
OPERATIONAL APPROACH: MATHEMATICA IMPLEMENTATION
In this section, we describe the MATHEMATICA® core of a FDTD code-generator for the simple problem presented in Section II, since it will be the stepping stone to the more complex situations described in Section IV. For this to be accomplished in a systematic manner, let us first express the FDTD numerical scheme approximating the analytical Eq. (1) in vector operator form
δ t E − ε −1curln H + ε −1σ pt E = 0 , µ δ t H + µ −1curln E + σ * µ −1 pt H = 0
(4)
with δ λ , pλ being the centered finite difference and average operators in the variable
λ = { x, y, z , t} defined by δ λψ (λ∆λ ,…)= pλψ (n∆t ,…)=
ψ ( (λ + 12 ) ∆λ ,…) − ψ ( (λ − 12 ) ∆λ ,…) ∆λ
ψ ( (λ + ) ∆λ ,…) + ψ ( (λ − 12 ) ∆λ ,…)
(5)
1 2
2
and with curln being the discrete operator resulting from the replacement of each space derivative of the curl operator in Eq. (1) by the centered finite difference operator defined above.
6
The main steps to build a FORTRAN/C code for equation (4) with MATHEMATICA® can be summarized as follows:
1) First, the linear numerical difference and average operators are defined in a generalized manner capable of shifting any variable n,i,j,k in steps of ½ In[2]:=
In[4]:=
dd@a_. f_@u__D, hi_, hj_, hk_, hn_D := 1 hi ∆x + hj ∆y + hk ∆z + hn ∆t hi hj hk hn , j → j+ , k → k+ , n→ n+ Ja JJf@uD ê. 9i → i + =N − 2 2 2 2 hi hj hk hn , j → j− , k → k− , n→ n− Jf@uD ê. 9i → i − =NNN; 2 2 2 2 Dif@f_, hi_, hj_, hk_, hn_D := Distribute@dd@ Expand@fD, hi, hj, hk, hnDD; mm@a_. f_@u__D, hi_, hj_, hk_, hn_D := 1 2 hi hj hk hn , j → j+ , k → k+ , n→ n+ Ja JJf@uD ê. 9i → i + =N + 2 2 2 2 hi hj hk hn , j → j− , k → k− , n→ n− Jf@uD ê. 9i → i − =NNN; 2 2 2 2 M@f_, hi_, hj_, hk_, hn_D := Distribute@ mm@ Expand@fD, hi, hj, hk, hnDD;
2) The second-order approximation to the time derivative and the identity is then defined as In[6]:= δt@f_D :=
Simplify@ Dif@f, 0, 0, 0, 1DD; pt@f_D := Simplify@ M@f, 0, 0, 0, 1DD;
3) Also, the second-order approximation to the space derivatives and the space identities (the latter are not needed now but will be used in the next section for the anisotropic case) is specified In[7]:=
ddx@f_D := Simplify@ Dif@f, 1, 0, 0, 0DD; ddy@f_D := Simplify@ Dif@f, 0, 1, 0, 0DD; ddz@f_D := Simplify@ Dif@f, 0, 0, 1, 0DD;
7
In[10]:=
mmx@f_D := Simplify@ M@f, 1, 0, 0, 0DD; mmy@f_D := Simplify@ M@f, 0, 1, 0, 0DD; mmz@f_D := Simplify@ M@f, 0, 0, 1, 0DD;
4) Finally, the actual second-order approximation to the space derivatives and the Yee cell distribution compatible with it, is given (the redundancy in the definitions is to facilitate the generalization to anisotropic materials presented in Section IV) In[13]:= δx@f_D :=
Simplify@ddx@fDD; δy@f_D := Simplify@ddy@fDD; δz@f_D := Simplify@ddz@fDD; pointsE = 88i → i + 1 ê 2