Beruflich Dokumente
Kultur Dokumente
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.
http://www.pdc.kth.se/education/computational-plasma-physics/
computational-plasma-physics
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
qB 2m v m vx2 + vy2
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).
Plotting subroutines.
Matlab/Octave
Matlab is not free but available to all KTH students as joint student
license.
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
>>
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().
2 component
- Matlab uses ode23: 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
function x_res = ExBfunc(x_vect,t)
% function x_res = ExBfunc(t, x_vect)
dx % x_vect(1) = x
=v %
%
x_vect(2)
x_vect(3)
=
=
y
z
dt %
%
%
x_vect(4)
x_vect(5)
x_vect(6)
=
=
=
u
v
w
endfunction
Motion in Constant and Uniform Fields
(EcrossBdrift.m) ...
% EM
Bz =
Bx =
fields
1.0 ; % B in z direction
0.0 ;
By = 0.0 ;
B = 1.0 Ex = 0.2 ; % E in x direction
main code Ey =
Ez =
0.0 ;
0.0 ;
% initial velocity
u0 = 0.5;
v0 = 0.0;
w0 = 0.0;
...
z E = 0.2
function for ODE solver (ExB function)
x x_res(1) = x_vect(4); % dx/dt = u
x_res(2) = x_vect(5); % dy/dt = v
x_res(3) = x_vect(6); % dz/dt = w
x_res(4) = qom*(Ex + x_vect(5)*Bz - x_vect(6)*By); % du/dt = qom*(E + vel x B)_x
x_res(5) = qom*(Ey + x_vect(6)*Bx - x_vect(4)*Bz); % dv/dt = qom*(E + vel x B)_y
x_res(6) = qom*(Ez + x_vect(4)*By - x_vect(5)*Bx); % dw/dt = qom*(E + 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
time x y z u v w
0
0.1
...
z
E x B drift Velocity
y
x
Trajectory
x0, y0 v
v
time
octave-3.2.3:96> mean(x_sol(:,5))
ans = -0.20135
Is -0.20135, the value we
x
expected for the drift velocity ?
Grad-B Drift (GradBdrift.m)
...
% charge to mass ratio
qom = 1
% EM fields
B = (1.0 x) k Bz = 1.0 ; % B in z direction
Bx = 0.0 ; % here we have spatial variation
By = 0.0 ;
main code Ex = 0.0 ; % no electric field just B gradient
Ey = 0.0 ;
(GradBdrift.m) Ez = 0.0 ;
% initial position
u time
y
octave-3.2.3:88> mean(x_sol(:,5))
ans = 0.039055
x0, y0
B0 Re3
Bdip = 5 (3xz y + (2z 2 x2 y 2 )
x + 3yz z)
r
Matlab/Octave Magnetic Dipole
(Newton_Lorenz.m)
...
% x_vect(1) = x
% x_vect(2) = y
% x_vect(3) = z
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 simulation
plot3(x_sol(:,1)/Re,x_sol(:,2)/Re,x_sol(:,3)/Re,'r'); % plot trajectory in 3D
ODE function (Newton_Lorenz.m)
function x_res = Newton_Lorenz(x_vect,t)
% x_vect(1) = x
% x_vect(2) = y
% x_vect(3) = z
% x_vect(4) = u
% x_vect(5) = v
% x_vect(6) = w
Magnetic dipole
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_vect(4); % dx/dt = u
x_res(2) = x_vect(5); % dy/dt = v
x_res(3) = x_vect(6); % dz/dt = w
x_res(4) = qom*(x_vect(5)*Bz - x_vect(6)*By); % du/dt = qom*(vel x B)_x
x_res(5) = qom*(x_vect(6)*Bx - x_vect(4)*Bz); % dv/dt = qom*(vel x B)_y
x_res(6) = qom*(x_vect(4)*By - x_vect(5)*Bx); % dw/dt = qom*(vel x B)_z
endfunction
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 ? Yes!
time [s]
u 2 v2 w 2
plot(time,0.5*m*(x_sol(:,4).^2 + x_sol(:,5).^2 + x_sol(:,6).^2))
arged particle 65
plot(time,x_sol(:,3)/Re)
xlabel('time [s]')
ylabel('z [Re]')
Bounce Period
As you saw in class period, for pitch angle approximately = 90 o the bounce
frequency:
3v
B =
2R0
R0 = equatorial distance to the guiding line
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.
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;
we calculate !
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 we need an extra function getBmod.m
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
Case p+ 10 MeV pitch angle = o
30 at t = 50 s
x [Re] 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).
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 Earths magnetic dipole). In this case, the GC
approximation is not valid.