-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrk.py
49 lines (40 loc) · 1.28 KB
/
rk.py
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
"""
Created: 2018-08-08
Modified: 2019-03-07
Author: Christopher Albert <[email protected]>
"""
from numpy import zeros, array, arange, append, hstack
from scipy.integrate import RK45
from common import r0, th0, ph0, pph0, timesteps, get_val, get_der
import common
from plotting import plot_orbit
dt, nt = timesteps(steps_per_bounce = 1, nbounce = 100)
z = zeros([2,1])
z[:,0] = [r0, th0]
Hplot = zeros(1)
[Hplot[0], pth, vpar] = get_val(array([r0,th0,ph0,pph0]))
def zdot(t, z):
global Ath, dAth, Aph, dAph, Bmod, dBmod, hth, dhth, hph, dhph,\
H, dH, pth, pph, dpth, vpar, dvpar
[H, pth, vpar, dHdx, dHdpph, dpthdx,
dpthdpph, dvpardx, dvpardpph] = get_der(array([z[0],z[1],ph0,pph0]))
ret = zeros(2)
ret[0] = -dHdx[1]/dpthdx[0]
ret[1] = dHdx[0]/dpthdx[0]
return ret
from time import time
tic = time()
tvec = zeros(1)
integ = RK45(zdot, 0.0, z[:,0], nt*dt, rtol=1e-6, atol=1e-12, max_step=dt)
common.neval = 0
maxnt = 10000000
for kt in arange(maxnt):
if (integ.status == 'finished'):
break
integ.step()
tvec = append(tvec, integ.t)
z = hstack([z, array([integ.y]).T])
Hplot = append(Hplot, H)
print('Field evaluations: {}'.format(common.neval))
print('Time taken: {}'.format(time()-tic))
plot_orbit(z)