You are on page 1of 5

Week Nine Python for Electromagnetism

Physical laws are expressed as mathematical relations which can then be solved under desired boundary conditions to obtain the results. Consider for example the relationship between electric and magnetic elds as represented by Maxwells equations: E = E B ; B = 0 J + 0 0 ; B = 0 t t

In order to nd out the eld distribution inside a resonator cavity, we need to solve these equations by applying suitable boundary conditions dictated by the geometry of the cavity and electromagnetic properties of the material used for making it. For simple geometries, analytic solutions are possible but in most cases numerical methods are essential and that is one reason why computers are important for physics. In this weeks exercise we will be looking at ways in which to visulaise the elds encountered in electromagnetism and have a rst look at some of the numerical tools available to solve these equations. Note: Scipy contains modules for solving rst order ordinary diernetial equations scipy.integrate.ode (a Nth order equation can also be solved using SciPy by transforming it into a system of rst order equations)scipy.integrate.odeint provides a module for solving a system of rst order ODEs. Partial dierential equation solvers are not included in the Enthought distribution and are beyond the scope of this introductory course but modules such as FiPy (Finite volume approach) and SfePy (Simple Finite Element approach) are capable of solving this class of equation.

Numerical Dierentiation

The following code will calculate and display graphically the path of a charged particle (with initial position and velocity) under the inuence of external magnetic and electric elds. Download the example code from moodle and observe the eects of changing the initial parameters. In this example code the position x, y, z at t + dt is obtained using the simplest of algorithms, Eulers method. This is a method for solving ordinary dierential equations using the formula: yn+1 = yn + hf (xn , yn ) which advances a solution from xn to xn+1 = xn + h . Note that the method increments a solution through an interval h while using derivative information from only the beginning of the interval. As a result, the steps error is O(h2 ). A better solution can be obtained by using a method that calculates the average derivative over the interval such as the Runge-Kutta described in Appendix A of the mathematical methods notes. A fourth order Runge-Kutta is a method that evaluates the derivative four times; once at the initial point, twice at two trial midpoints and once at trial a endpoint. The nal value is evaluated from these derivatives using the equations: k1 = hf (xn , yn ) 1

h k1 k2 = hf (xn + , yn + ) 2 2 h k2 k3 = hf (xn + , yn + ) 2 2 k4 = hf (xn + h, yn + k3 ) yn+1 = yn + k1 k2 k3 k4 + + + 6 3 3 6

1.1

Wk9Ex1:

Modify the eld simulation.py code to use a 4th order Runge-Kutta method, plot and compare with the Eulers method result. Try increasing the total time interval to see how the errors vary with time.
1

11

16

21

26

31

from numpy import from pylab import from m p l t o o l k i t s . mplot3d import Axes3D The f o l l o w i n g example t r a c e s t h e movement o f a c h a r g e d p a r t i c l e under t h e i n f l u e n c e o f e l e c t r i c and magnetic f i e l d s . You can e d i t t h e program t o change t h e components o f t h e f i e l d s t o s e e t h e i r e f f e c t on the t r a j e c t o r y . Ex = 0 . 0 # Components o f a p p l i e d E l e c t r i c F i e l d Ey = 2 . 0 Ez = 0 . 0 Bx = 0 . 0 # Components o f a p p l i e d Magnetic f i e l d By = 0 . 0 Bz = 5 . 0 m = 2 . 0 # Mass o f t h e p a r t i c l e q = 5 . 0 # Charge x = 0.0 # i n i t i a l position x y = 0.0 # i n i t i a l position y z = 0.0 # i n i t i a l position z vx = 2 0 . 0 # i n i t i a l v e l o c i t y vx vy = 0 . 0 # i n i t i a l v e l o c i t y vy vz = 2 . 0 # i n i t i a l v e l o c i t y vz a = [] b = [] c = [] t = 0.0 dt = 0 . 0 1 w h i l e t < 6 : # t r a c e path u n t i l time r e a c h e s v a l u e Fx = q ( Ex + ( vy Bz ) ( vz By) ) vx = vx + Fx/m dt # A c c e l e r a t i o n = F/m; dv = a . dt Fy = q ( Ey ( vx Bz ) + ( vz Bx) ) vy = vy + Fy/m dt Fz = q ( Ez + ( vx By) ( vy Bx) )

36

41

vz = vz + Fz/m dt x = x + vx dt y = y + vy dt z = z + vz dt a . append ( x ) b . append ( y ) c . append ( z ) t = t + dt f i g=f i g u r e ( ) ax = Axes3D ( f i g ) ax . s e t t i t l e ( Path o f c h a r g e d p a r t i c l e under i n f l u e n c e o f e l e c t r i c and magnetic f i e l d s ) ax . s e t x l a b e l ( X ) ax . s e t y l a b e l ( Y ) ax . s e t z l a b e l ( Z ) ax . plot3D ( a , b , c , c o l o r= b l u e , l a b e l= path ) ax . l e g e n d ( l o c= l o w e r l e f t ) show ( )

46

51

eld simulation.py

Vector Fields

The code vectoreld.py is a basic script for generating a simple vector eld around a point charge using Mayavi. Download the code from moodle and run it. Exlpore the functionality of the Mayavi toolbar, the rst button opens a dialog for the Mayavi pipeline that allows you to control many aspects of the view.

Figure 1: The Mayavi toolbar, the rst button launches the Mayavi pipeline

2.1

Wk9Ex2:

Modify the code such that the vector eld is only calculated and plotted in the x,y plane. Add axis labels and a title to the plot and save a .png output of the plot.
from numpy import mgrid , array , s q r t from mayavi . mlab import q u i v e r 3 d # t h i s c r e a t e s a 3D g r i d which i s r a n g e s from 100 t o 100 i n a l l # c o o r d i n a t e s , with i n t e r s e c t i o n s a t e v e r y 20 th v a l u e # ( 100 , 80, 60, . . . ) . See h e l p mgrid f o r d e t a i l s . x , y , z = mgrid [ 1 0 0 . : 1 0 1 . : 2 0 . , 1 0 0 . : 1 0 1 . : 2 0 . , 1 0 0 . : 1 0 1 . : 2 0 . ] # We w i l l now s t o r e t h e c o o r d i n a t e s o f our c h a r g e a s a

13

18

# v e c t o r . A l l v e c t o r s i n Python s h o u l d be s t o r e d a s a r r a y s . # Note t h a t we a r e p l a c i n g t h i s c h a r g e i n between t h e g r i d p o i n t s . # This i s t o g e t t h e b e s t p o s s i b l e symmetry i n t h i s s p e c i f i c f i e l d # I f you p l a c e t h e c h a r g e o n t h e g r i d , you g e t a d i v i s i o n o f z e r o # when c a l c u l a t i n g t h e E f i e l d . qpos = a r r a y ( [ 1 0 . , 1 0 . , 1 0 . ] ) # t h e magnitude o f our c h a r g e . This c o u l d be any number # a t t h e moment . qcharge = 1.0 # C r e a t e a g r i d f o r t h e e l e c t r i c f i e l d . This has t h e s i z e o f # x , y and z , but a l l t h e v a l u e s a r e now s e t t o z e r o . Ex , Ey , Ez = x 0 , y 0 , z 0 # C a l c u l a t e t h e x , y and z d i s t a n c e t o t h e c h a r g e a t e v e r y p o i n t i n t h e g r i d : rx = x qpos [ 0 ] ry = y qpos [ 1 ] r z = z qpos [ 2 ] # Calculate the d i s t a n c e at every point in the g r i d : r = s q r t ( rx 2 + ry 2 + r z 2 )

23

28

# Calculate the Ex = ( q c h a r g e / Ey = ( q c h a r g e / Ez = ( q c h a r g e /
33

field r 2) r 2) r 2)

f o r each component a t e v e r y p o i n t i n t h e g r i d : ( rx / r ) ( ry / r ) ( rz / r )

# Draw t h e v e c t o r f i e l d i n 3D q u i v e r 3 d ( x , y , z , Ex , Ey , Ez )

vectoreld.py

2.2

Wk9Ex3:

The expressions needed for question 3 are contained within the moodle pdf EM Standing Waves in Resonant Cavities. Consider a rectangular cavity with perfectly reecting walls (dimensions a x b x d).

Figure 2: Rectangular resonant cavity

Write code to calculate the resonant frequencies as a function of the mode numbers, use reduced units scale everything to a/v (v speed of light) 4

Make a list of the 10 rst frequencies for a cavity 36 x 33 x 23 cm. This is about the size of my microwave oven, comment on the typical operating frequency of a microwave. Plot the E, B eld lines for the two rst TE modes Plot the current intensity on the wall, nd the nodes location Repeat the last two plots with the 2 rst TM modes. TE modes (Transverse Electric) no electric eld in the direction of propagation. TM modes (Transverse Magnetic) no magnetic eld in the direction of propagation. An alternative description of the physics can be found at: http:cas.web.cern.chcasDenmark2010LecturesWolski-2.pdf (page 32)

You might also like