Single Particle Motion via Computer Simulation Stefano Markidis (markidis@pdc.kth.se) HPCViz Department - CSC School - KTH Useful Info I work for KTH HPC computer center (PDC) and department (http:// www.pdc.kth.se/), located in TEKNIKRINGEN 14, PLAN 4. You can contact me by email markidis@pdc.kth.se. The material for this class is available at: http://www.pdc.kth.se/education/computational-plasma-physics/ computational-plasma-physics You will need to download Matlab/Octave codes to complete your assignments! • • • • Introduction Equation of motion of a charged particle Crash course on Matlab/Octave Numerical solution of equation of motion for charged particles: • • • • Outline Constant uniform fields (E x B drift) Spatially varying magnetic field Magnetic dipole (Earth-like magnetic dipole) Guiding center approximation. Numerical solution of guiding center approximation. Introduction • We will study the trajectory and dynamics of charged particles (electron, proton, alpha particles) in a given static magnetic field configuration. • We will solve numerically the equations that govern the motion of particles using programming language Matlab or Octave (free version of Matlab). • I will teach basic commands of Matlab/Octave, so you can understand the code for today’s and next lecture and learn how to modify to solve the assignment • I will present an approximated model of charged particle motion in a (strong) magnetic field (Guide Center approximation), its equation and numerical solution with Matlab/Octave Equation of Motion dp = q(E + v × B) + Fnon−EM dt p = momentum = m γ v (we will assume non relativistic motion for today γ = 1) q = particle charge E = electric field acting on the particle B = magnetic field on the particle Fnon-EM = other forces acting on particle (gravitational, ...) E = const and B = 0 (neglect the non-EM forces) dv q = E dt m Constant acceleration in the direction of E. a e E E = 0 and B = const uniform The equation on the right describe helical motion that: • • is constant in the direction of B circular in the (x,y) plane qB ωc = m 2πm T = qB The centre of gyro-motion is called guiding center (GC). guiding center v⊥ ρ= = |ωc | � m vx2 + vy2 |q|N z E = 0 and B = const (continued) The equation of motion is: dv m = qv × B dt Taking the inner product with v of both sides of the equation 2 dv d mv m ·v = ( )=0 dt dt 2 Consequently, the kinetic energy and the speed (module of velocity) remain constant. We will use this results in the computer experiments. Computer Simulation of Single Particle Motion • Use Matlab programming language when available (computer labs, student licenses). Otherwise we will use Octave (like I do). • Matlab and Octave provide: • Efficient ODE solvers are already implemented. Caveat: Matlab and Octave use different name for ODE solvers and sightly different syntax. • Plotting subroutines. Matlab/Octave • Matlab is not free but available to all KTH students as joint student license. • Octave is free (http://www.gnu.org/software/octave/) but less fancy than Matlab (plots looks no as good as the one in Matlab). Matlab/Octave Crash Course Good tutorial available at http://www4.ncsu.edu/~mahaider/ NCSU_RTG_Site/RTM_Matlab_Intro.pdf (caveat some functions might not working on Octave). Today I will talk about how to use: • • • • Matlab and Octave file, and how to run command line Arrays and matrices Plots Control statements • Matlab/Octave Files and Start Typically, you will write your Matlab/Octave code using a text editor (Matlab has one, for Octave you can use emacs, vi, and other editor). The file name has extension .m i.e. TrajMagnDipole.m • When you start Matlab/Octave, a command line will appear: >> • you probably need to change directory (where you saved your program) >>cd your_directory • and then run the code >>TrajMagnDipole without .m ! • Matlab and Octave will keep in memory the varaiables of your main code you launched, so you can use those variables for doing some extra-processing Arrays and Matrices Everything in Matlab/Octave is an array or a matrix. Scalar are vector of size 1. In the first particle trajectory program, we will use arrays for storing in memory: - Particle positions x, y, z - time t In my next lecture we will use: - Electric field on the cell centers Eg - Electrostatic potential Phi - Energy and momentum histories histTotEnergy, ... Today we will use matrices for storing the solution of our problem: - ODE solution matrix x_sol In my next lecture we will use: - Matrix for saving interpolation values mat - Matrices for saving the output. histEfield, ... Assignment of Arrays and Matrices To enter a matrix, stick some rows together. For example, a 2x2 matrix B could be entered as: 1 2 B=[1 2; 3 4] This enters the matrix B = 3 4 To simplify typing vectors, some shortcuts are introduced. For example, to obtain the vector t = [0 0.5 1 1.5 2 .. 9 9.5 10], you can type t=[0:.5:10], which creates a vector from 0 to 10 in 0.5 increments. Or, to get a vector containing equally spaced entries, you can use the linspace command. For example, to get 400 elements from 0 to 100 equally spaced, you could type r = linspace(0,100,400). Extract Parts of Vectors and Arrays For instance, to obtain the 5th entry of a vector r, type >> r(5) For a range of values, i.e. from a to b, type >> r(a:b) For a matrix, the syntax is (row, column). So to get the i; j entry of the matrix B, type >> B(i,j). Similarly, you can pull out ranges of values as you could with a vector. If you wanted the entire range of rows, or columns, use the colon (:). So the rowspace of the fourth column of A would be A(:,4). Useful Functions for Array Matrices • size() returns the dimension of a matrix/vector. If you just want the length of a vector, use length(), numel(). • • eye(n) creates the nxn identity matrix. • • ones(n,m) creates a matrix of ones. • randn(n,m) creates a nxm matrix from a random normal gaussian distribution. zeros(n,m) creates an nxm matrix of zeros. Useful for initializing vectors and matrices. rand(n,m) creates a nxm matrix with entries from a random uniform distribution on the unit interval. Operations All the standard operations, sin; cos; tan; arctan; log; ln; and exp exist in MATLAB/Octave. You can apply these to vectors. If you wanted the sine of a vector, just do b=sin(a). Moreover you will have sum, mean, cov to calculate the sum, the average ad covariance of a vector. Matrix multiplication works as you expect, so long as the dimensions match of course. To multiply 2 matrices A*B just do >> C=A*B You can also do element wise operations on matrices and vectors using the dot notation. For example >> C=A.*B will create a matrix C whose elements are denoted as cij =aij*bij . The dot notation works with multiplication, *, division, /, and exponentiation. Plots Today we will use >> plot3(x, y, z, options) to plot the trajectories. x, y, z vectors contain the particle coordinate at different times steps The most basic way to plot 2D data is using the plot command: >> plot(time,x, options) where time is a vector denoting the independent vector and x is the data you want to plot and options is where you can specify color, thickness,..., i.e. plot(time,x,‘r.’) use red dot line Other plots you will use in my next lecture: - hist ----> make an histogram - semilogy ----> make a plot with logarithmic y-axis - bar -----> make bar plot - pcolor ----> make a contourplot - surf ----> make a surface plot Control Statements: for and while There are two kinds of loops we will use: for and while loops. A for loop is used to repeat a statement of a group of statements for a fixed number of times. An example: for it=1:NT num = 1/(it+1); end A while loop is used to execute a statement or a group of statements for an indefinite number of times until the condition specified by while is no longer satisfied. An example: while num < 10000 num = 2^i; v = [v; num]; i = i+1; end Control Statements: if Frequently you'll have to write statements that will occur only if some condition is true. Typically you'll have an if statement, and if it isn't satisfied, a corresponding else or elseif statement. Along with the standard greater than, and less than conditions (a>b, b<a, a>=b, b<=a), there are also: - and a & b Example i=6; j=21; - or a | b if i >5 - not-equal a ~= b k=i; - equal (notice the two equal signs!): a==b elseif (i>1) & (j==20) k=5i+j; else k=1; end Functions • • In Matlab/Octave, the equations are passed to the ODE solver via function Function are typically defined in separate .m file returned value (can be scalar or vector) name of the function same of .m file input argument function x_res = Newton_Lorenz(x_vect,t) ... ... endfunction Matlab and Octave ODE solvers - Octave uses lsode: vector with independent variable initial values [x_sol,t] = lsode("Newton_LorenzOct",[x0 ;y0; z0; u0; v0; w0],time); - Matlab uses ode23: 2 component independent variable initial and final time initial values [t,x_sol] = ode23(@Newton_LorenzMat, [0 final_time], [x0 ;y0; z0; u0; v0; w0]); Newton_LorenzOct and Newton_LorenzMat will have different ordering in initial values and independent variables function x_res = Newton_LorenzOct(x_vect,t) function x_res = Newton_LorenzMat(t,x_vect) Equation of Motion Function dx =v dt dv q = (E + v × B) dt m function x_res = ExBfunc(x_vect,t) % function x_res = ExBfunc(t, x_vect) % % % % % % x_vect(1) x_vect(2) x_vect(3) x_vect(4) x_vect(5) x_vect(6) = = = = = = x y z u v w global Bx; global By; global Bz; global Ex; global Ey; global Ez; global qom; x_res = zeros(6,1); x_res(1) x_res(2) x_res(3) x_res(4) x_res(5) x_res(6) = = = = = = x_vect(4); % dx/dt = u x_vect(5); % dy/dt = v x_vect(6); % dz/dt = w qom*(Ex + x_vect(5)*Bz - x_vect(6)*By); qom*(Ey + x_vect(6)*Bx - x_vect(4)*Bz); qom*(Ez + x_vect(4)*By - x_vect(5)*Bx); endfunction Motion in Constant and Uniform Fields (EcrossBdrift.m) B = 1.0 main code (EcrossBdrift.m) simulation parameters ... % EM Bz = Bx = By = Ex = Ey = Ez = fields 1.0 ; % B in z direction 0.0 ; 0.0 ; 0.2 ; % E in x direction 0.0 ; 0.0 ; % initial position x0 = 0; y0 = 0; z0 = 0; % initial velocity u0 = 0.5; v0 = 0.0; w0 = 0.0; ... E = 0.2 z x function for ODE solver (ExB function) x_res(1) x_res(2) x_res(3) x_res(4) x_res(5) x_res(6) = = = = = = x_vect(4); % dx/dt = u x_vect(5); % dy/dt = v x_vect(6); % dz/dt = w qom*(Ex + x_vect(5)*Bz - x_vect(6)*By); % du/dt = qom*(E + qom*(Ey + x_vect(6)*Bx - x_vect(4)*Bz); % dv/dt = qom*(E + qom*(Ez + x_vect(4)*By - x_vect(5)*Bx); % dw/dt = qom*(E + vel x B)_x vel x B)_y vel x B)_z Solution Matrix from ODE Solvers The lsode (Octave) and ode23 (Matlab) solvers will return a matrix [x_sol, time] (Octave) and [x_sol, time] (Matlab) - time is a vector with different time values - x_sol is a matrix containing the results x_sol x y z u v w time 0 0.1 ... E x B drift z Velocity y x Trajectory x0, y0 v v time x octave-3.2.3:96> mean(x_sol(:,5)) ans = -0.20135 Is -0.20135, the value we expected for the drift velocity ? Grad-B Drift (GradBdrift.m) B = (1.0 x) k main code (GradBdrift.m) simulation set-up z x function for ODE solver (gradBfunc.m). Here we define the B gradient. ... % charge to mass ratio qom = 1 % EM fields Bz = 1.0 ; % B in z direction Bx = 0.0 ; % here we have spatial variation By = 0.0 ; Ex = 0.0 ; % no electric field just B gradient Ey = 0.0 ; Ez = 0.0 ; % initial position x0 = 1.0; y0 = 0; z0 = 0; % initial velocity u0 = 0.3; v0 = 0.0; w0 = 0.0; ... ... Bz = 1*x_vect(1); x_res(1) = x_vect(4); % dx/dt = u ... Grad B Drift Results Trajectory v y u time x0, y0 octave-3.2.3:88> mean(x_sol(:,5)) ans = 0.039055 What do you expect this grad B drift velocity value ? x Try This at Home ! % charge to mass ratio qom = 1 % EM fields Bz = 1.0 ; % B in z direction Bx = 0.0 ; By = 0.0 ; Ex = 0.0 ; Ey = 0.0 ; Ez = 0.0 ; % initial position x0 = 0.1; y0 = 0; z0 = 0; 1 x0 is very close to null and negative B regions ! % EM Bz = Bx = By = Ex = Ey = Ez = 2 fields 1.0 ; % B in z direction 0.0 ; 0.0 ; 0.0 ; 0.0 ; 0.0 ; % initial position x0 = 0.1; y0 = 0; z0 = 0; % initial velocity u0 = 0.01; v0 = 0.0; w0 = 0.0; % initial velocity u0 = 0.3; v0 = 0.0; w0 = 0.0; tfin = 400 time = 0:0.01:tfin; tfin = 50 As third example, we calculate numerically the trajectory of charged particles in a simplified model of Earth’s magnetic field Bdip µ0 = (3(M · r̂) r̂ − M) 3 4πr r = xx̂ + yŷ + zẑ M = Magnetic moment z M = −Mẑ At magnetic equator B0 = 3.07*10-5 T, so: x µ0 M/4π = Bdip (x = Re, y =0, z=0) 3 B 0 Re B0 Re3 = − 5 (3xzx̂ + 3yzŷ + (2z 2 − x2 − y 2 )ẑ) r Matlab/Octave Magnetic Dipole (Newton_Lorenz.m) ... % x_vect(1) = x % x_vect(2) = y % x_vect(3) = z fac1 Bx = By = Bz = ... = -B0*Re^3/(x_vect(1)^2 + x_vect(2)^2 + x_vect(3)^2)^2.5; 3*x_vect(1)*x_vect(3)*fac1; 3*x_vect(2)*x_vect(3)*fac1; (2*x_vect(3)^2 -x_vect(1)^2- x_vect(2)^2)*fac1; Octave Code - Newton Lorenz Eqs (GC_TrajMagnDipole.m) close all; clear all; global B0; global q; global m; global Re; % parameters e = 1.602176565e-19; % Elementary charge (Coulomb) m_pr = 1.672621777e-27; % Proton mass (kg) m_el = 9.10938291e-31; % Electron mass (kg) c = 299792458; % speed of light (m/s) % Earth parameters B0 = 3.07e-5 % Tesla Re = 6378137 % meter (Earth radius) % use electron/proton m = m_pr % we choose proton, change to m_el when simulating electron q = e % positive charge, change to -e when simulating electron % Trajectory of a proton with 10MeV kinetic energy in dipole field K = 1e7 % kinetic energy in eV K = K*e; % convert to Joule % Find corresponding speed: v_mod = c/sqrt(1+(m*c^2)/K) % initial position: equatorial plane 4Re from Earth x0 = 4*Re; y0 = 0; z0 = 0; pitch_angle = 30.0 % initial angle between velocity and mag.field (degrees) % initial velocity u0 = 0.0; v0 = v_mod*sin(pitch_angle*pi/180); w0 = v_mod*cos(pitch_angle*pi/180); tfin = 10 % in s time = 0:0.01:tfin; [x_sol,t] = lsode("Newton_Lorenz",[x0 ;y0; z0; u0; v0; w0],time); % solve equation of motion plot3(x_sol(:,1)/Re,x_sol(:,2)/Re,x_sol(:,3)/Re,'r'); % plot trajectory in 3D Simulation parameters simulation ODE function (Newton_Lorenz.m) function x_res = Newton_Lorenz(x_vect,t) % % % % % % x_vect(1) x_vect(2) x_vect(3) x_vect(4) x_vect(5) x_vect(6) = = = = = = x y z u v w global B0; global q; global m; global Re; x_res = zeros(6,1); % calculate 3 Cartesian components of the magnetic field fac1 = -B0*Re^3/(x_vect(1)^2 + x_vect(2)^2 + x_vect(3)^2)^2.5; Bx = 3*x_vect(1)*x_vect(3)*fac1; By = 3*x_vect(2)*x_vect(3)*fac1; Bz = (2*x_vect(3)^2 -x_vect(1)^2- x_vect(2)^2)*fac1; qom = q/m; x_res(1) = x_res(2) = x_res(3) = x_res(4) = x_res(5) = x_res(6) = x_vect(4); % dx/dt = u x_vect(5); % dy/dt = v x_vect(6); % dz/dt = w qom*(x_vect(5)*Bz - x_vect(6)*By); % du/dt = qom*(vel x B)_x qom*(x_vect(6)*Bx - x_vect(4)*Bz); % dv/dt = qom*(vel x B)_y qom*(x_vect(4)*By - x_vect(5)*Bx); % dw/dt = qom*(vel x B)_z endfunction Magnetic dipole Case p+ 10 MeV pitch angle = o 30 at t = 50 s plot3(x_sol(:,1)/Re,x_sol(:,2)/Re,x_sol(:,3)/Re); xlabel('x[Re]') ylabel('y[Re]') zlabel('z[Re]') title('Proton 10 MeV starting from x = 4Re, y = 0, z = 0') hold on; sphere(40); hold off; Is Kinetic Energy Conserved ? Kinetic Energy [J] time [s] 2 u 2 v 2 w plot(time,0.5*m*(x_sol(:,4).^2 + x_sol(:,5).^2 + x_sol(:,6).^2)) Yes! arged particle 65 Charged particles can be trapped in the magnetic mirrors formed by a dipolar planetary magnetic field. Their motion is the superposition of three components: 1) the gyration around a field line 2) the bounce between magnetic mirrors in opposite hemispheres 3) azimuthal drift produced by the transverse magnetic field gradient. rged particles can be trapped in the magnetic bottle formed by ry magnetic field. Their motion is the superposition of three Mirror effect in Earth’s Dipole A 10 MeV proton with a pitch angle of θ = 30◦ starting at 4RE is bounce motion trapped in the Earth magnetic field. The particle performs a hierarchy of three periodic motions: gyration about the field line, bouncing between the mirror points, and a slow (toroidal) drift in the equatorial plane drift motion Bounce Motion Bounce motion proceeds along the field lines. The motion slows down as it moves towards locations with stronger magnetic field, reflecting back at the mirror points. The bounce motion is much slower than the cyclotron motion. time [s] plot(time,x_sol(:,3)/Re) xlabel('time [s]') ylabel('z [Re]') Bounce Period As you saw in class period, for pitch angle approximately = frequency: 3v⊥ ωB = √ 2R0 R0 = equatorial distance to the guiding line Therefore, the bounce perod is: √ 2π 2R0 τB = 3v⊥ o 90 the bounce Drift Motion The drift motion takes particles across field lines (perpendicular to the bounce motion). In general drift motion is faster at larger distances. Particles in dipole magnetic field lines are trapped on closed drift shells as long as they are not perturbed by collisions or interactions with EM waves x0 = 2Re x0 = 4Re x [Re] plot(x_sol(:,1)/Re,x_sol(:,2)/Re,'r') Van Allen Radiation Belts The Earth's Van Allen Belts consist of highly energetic ionized particles trapped in the Earth's geomagnetic fields. They can be found from 1000 km above the ground up to a distance of 6 Re. These belts are composed of of electrons with Energies up to several MeV and proton with energies up to several hundred MeV. The dynamics of these particles has been presented in the simulations From: http://en.wikipedia.org/wiki/Van_Allen_radiation_belt An Atlas 5 rocket launches NASA's Radiation Belt Storm Probes (RBSP) mission, twin probes that will study the effects of the solar wind stream on electrons and ions in Earth's radiation belts, such as how they change during geomagnetic storms. Launch took place in the pre-dawn hours of August 30 Guiding-Center Equations The guiding center is the geometric center of cyclotron motion. We will calculate the trajectory of the guiding center. The particle position r is substituted with:r = R + ρ Assuming that the cyclotron radius is much smaller than the length scale of the field we can expand B around R to first order in Taylor series: B(r) = B(R) + (ρ · ∇)B This expression is substitute into the Newton-Lorenz equation and the equation is averaged over a gyro-period, eliminating rapidly oscillating terms containing ρ and its derivatives Guiding-Center Equations curvature gradient drift and motion along the field 2 dR mv = (1 + 2 dt 2qB 2 v// ) b̂ 2 v × ∇B + v// b̂ mirror force dv// µ = − b̂ · ∇B dt m µ= 2 mv⊥ 2B is the magnetic moment The unknowns are the three coordinate of the guiding center and the parallel (to B) velocity Octave Code for solving GC equations (GC_TrajMagnDipole.m) close all; clear all; global B0; global q; global m; global Re; global v_mod; global v_par0; % parameters e = 1.602176565e-19; % Elementary charge (Coulomb) m_pr = 1.672621777e-27; % Proton mass (kg) m_el = 9.10938291e-31; % Electron mass (kg) c = 299792458; % speed of light (m/s) % Earth parameters B0 = 3.07e-5 % Tesla Re = 6378137 % meter (Earth radius) % use electron/proton m = m_pr % replace m_pr with m_el for electron q = e % Trajectory of a proton with 10MeV kinetic energy in dipole field K = 1e7 % kinetic energy in eV K = K*e; % convert to Joule % Find corresponding speed: v_mod = c/sqrt(1+(m*c^2)/K) % initial position x0 = 4*Re; y0 = 0; z0 = 0; Simulation parameters pitch_angle = 30.0 % initial angle between velocity and mag.field (degrees) % initial parallel velocity v_par0 = v_mod*cos(pitch_angle*pi/180); tfin = 50.0 % in s time = 0:0.01:tfin; lsode_options("integration method","stiff"); [x_sol,t] = lsode("Newton_LorenzGC",[x0 ;y0; z0; v_par0],time); plot3(x_sol(:,1)/Re,x_sol(:,2)/Re,x_sol(:,3)/Re); simulation Guiding Center Function Newton_Lorenz.m function x_res = Newton_LorenzGC(x_vect,t) % x_vect(1) = x % x_vect(2) = y % x_vect(3) = z % x_vect(4) = vpar = parallel (to B) velocity global B0; global q; global m; global Re; global v_mod; global v_par0; x_res = zeros(4,1); vsq = v_mod^2; % This remains unchanged in present of a static B field fac1 = -B0*Re^3/(x_vect(1)^2 + x_vect(2)^2 + x_vect(3)^2)^2.5; Bx = 3*x_vect(1)*x_vect(3)*fac1; By = 3*x_vect(2)*x_vect(3)*fac1; Bz = (2*x_vect(3)^2 -x_vect(1)^2- x_vect(2)^2)*fac1; B_mod = sqrt(Bx*Bx + By*By + Bz*Bz); mu = m*(vsq-x_vect(4)^2)/(2*B_mod); % magnetic moment is an adiabatic invariant d = 0.001*Re; % calculate grad(module of B) gradB_x = (getBmod((x_vect(1)+d),x_vect(2),x_vect(3)) - getBmod((x_vect(1)-d),x_vect(2),x_vect(3)))/(2*d); gradB_y = (getBmod(x_vect(1),(x_vect(2)+d),x_vect(3)) - getBmod(x_vect(1),(x_vect(2)-d),x_vect(3)))/(2*d); gradB_z = (getBmod(x_vect(1),x_vect(2),(x_vect(3)+d)) - getBmod(x_vect(1),x_vect(2),(x_vect(3)-d)))/(2*d); % b unit vector b_unit_x = Bx/B_mod; b_unit_y = By/B_mod; b_unit_z = Bz/B_mod; % b unit vector cross gradB bxgB_x = b_unit_y*gradB_z - b_unit_z*gradB_y; bxgB_y = b_unit_z*gradB_x - b_unit_x*gradB_z; bxgB_z = b_unit_x*gradB_y - b_unit_y*gradB_x; % b unit vector inner product gradB dotpr = b_unit_x*gradB_x + b_unit_y*gradB_y + b_unit_z*gradB_z; fac = m/(2*q*B_mod^2)*(vsq + x_vect(4)^2); x_res(1) = fac*bxgB_x + x_vect(4)*b_unit_x; x_res(2) = fac*bxgB_y + x_vect(4)*b_unit_y; x_res(3) = fac*bxgB_z + x_vect(4)*b_unit_z; x_res(4) = -mu/m*dotpr; endfunction we calculate μ ! we need an extra function getBmod.m Case p+ 10 MeV pitch angle = o 30 at t = 50 s gyro-motion averaged out Guiding Center Results Full Newton Lorenz equation x [Re] GC approximation x [Re] Advantages of GC approach • For numerical stability reason, the time step in the computer simulations needs to be a fraction of the fastest time scale in the system (in this case the gyro-period). T = 2*pi*m/(abs(e)*B); .When we simulate electron the gyro-period can become very small imposing a very small simulation time step (simulation will take long time). • Because the GC approximation removes the gyration motion from the model, we take time step that are larger than the gyro-period and the simulation will take less time. Validity of GC approximation • We derived the GC equation assuming that the Larmor radius is much smaller than the length scale of the field. • In presence of magnetic null or weak magnetic field, GC approximation is not valid. This approximation is often used in simulation of fusion devices where you have very strong magnetic field. • When you have highly energetic particles, the Larmor radius might become comparable or larger than the length scale of the magnetic field (in the case of Earth’s magnetic dipole). In this case, the GC approximation is not valid.