-
Notifications
You must be signed in to change notification settings - Fork 11
/
dynamics_Boris_f2py.py
225 lines (182 loc) · 6.9 KB
/
dynamics_Boris_f2py.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#-Begin-preamble-------------------------------------------------------
#
# CERN
#
# European Organization for Nuclear Research
#
#
# This file is part of the code:
#
# PyECLOUD Version 8.7.1
#
#
# Main author: Giovanni IADAROLA
# BE-ABP Group
# CERN
# CH-1211 GENEVA 23
# SWITZERLAND
# giovanni.iadarola@cern.ch
#
# Contributors: Eleonora Belli
# Philipp Dijkstal
# Lorenzo Giacomel
# Lotta Mether
# Annalisa Romano
# Giovanni Rumolo
# Eric Wulff
#
#
# Copyright CERN, Geneva 2011 - Copyright and any other
# appropriate legal protection of this computer program and
# associated documentation reserved in all countries of the
# world.
#
# Organizations collaborating with CERN may receive this program
# and documentation freely and without charge.
#
# CERN undertakes no obligation for the maintenance of this
# program, nor responsibility for its correctness, and accepts
# no liability whatsoever resulting from its use.
#
# Program and documentation are provided solely for the use of
# the organization to which they are distributed.
#
# This program may not be copied or otherwise distributed
# without permission. This message must be retained on this and
# any other authorized copies.
#
# The material cannot be sold. CERN should be given credit in
# all references.
#
#-End-preamble---------------------------------------------------------
from numpy import squeeze
import scipy.io as sio
from . import int_field_for as iff
from .boris_step import boris_step
def crprod(bx, by, bz, cx, cy, cz):
ax = by * cz - bz * cy
ay = bz * cx - bx * cz
az = bx * cy - by * cx
return ax, ay, az
class B_none():
def __init__(self, B0x, B0y, B0z):
self.B0x = B0x
self.B0y = B0y
self.B0z = B0z
def get_B(self, xn, yn):
Bx_n = 0 * xn + self.B0x
By_n = 0 * xn + self.B0y
Bz_n = 0 * xn + self.B0z
return Bx_n, By_n, Bz_n
class B_quad():
def __init__(self, B0x, B0y, B0z, fact_Bmap):
self.B0x = B0x
self.B0y = B0y
self.B0z = B0z
self.fact_Bmap = fact_Bmap
def get_B(self, xn, yn):
Bx_n = self.fact_Bmap * yn.copy()
By_n = self.fact_Bmap * xn.copy()
Bx_n = Bx_n + self.B0x
By_n = By_n + self.B0y
Bz_n = 0 * xn + self.B0z
return Bx_n, By_n, Bz_n
class B_file():
def __init__(self, B0x, B0y, B0z, fact_Bmap, B_map_file):
print('Loading B map')
dict_Bmap = sio.loadmat(B_map_file)
self.Bmap_x = fact_Bmap * squeeze(dict_Bmap['Bx'].real)
self.Bmap_y = fact_Bmap * squeeze(dict_Bmap['By'].real)
if 'Bz' in dict_Bmap.keys():
self.has_Bz = True
self.Bmap_z = fact_Bmap * squeeze(dict_Bmap['Bz'].real)
else:
self.has_Bz = False
self.xx = squeeze(dict_Bmap['xx'].T)
self.yy = squeeze(dict_Bmap['yy'].T)
self.B0x = B0x
self.B0y = B0y
self.B0z = B0z
self.xmin = min(self.xx)
self.ymin = min(self.yy)
self.dx = self.xx[1] - self.xx[0]
self.dy = self.yy[1] - self.yy[0]
def get_B(self, xn, yn):
Bx_n, By_n = iff.int_field(xn, yn, self.xmin, self.ymin,\
self.dx, self.dy, self.Bmap_x, self.Bmap_y)
if self.has_Bz:
Bz_n, _ = iff.int_field(xn, yn, self.xmin, self.ymin,
self.dx, self.dy, self.Bmap_z, self.Bmap_y)
else:
Bz_n = 0. * xn
# the rescaling factor has already been applied to the map
Bx_n = Bx_n + self.B0x
By_n = By_n + self.B0y
Bz_n = Bz_n + self.B0z
return Bx_n, By_n, Bz_n
class pusher_Boris():
def __init__(self, Dt, B0x, B0y, B0z, \
B_map_file, fact_Bmap, N_sub_steps=1):
print("Tracker: Boris")
self.Dt = Dt
self.N_sub_steps = N_sub_steps
self.Dtt = Dt / float(N_sub_steps)
self.B0x = B0x
self.B0y = B0y
self.B0z = B0z
self.fact_Bmap = fact_Bmap
if B_map_file is None:
self.B_ob = B_none(B0x, B0y, B0z)
elif B_map_file == 'analytic_qaudrupole_unit_grad':
print("B map analytic quadrupole")
self.B_ob = B_quad(B0x, B0y, B0z, fact_Bmap)
else:
self.B_ob = B_file(B0x, B0y, B0z, fact_Bmap, B_map_file)
print("N_subst_init=%d" % self.N_sub_steps)
#@profile
def step(self, MP_e, Ex_n, Ey_n, Ez_n=0., Bx_n=0., By_n=0., Bz_n=0.):
if MP_e.N_mp > 0:
xn1 = MP_e.x_mp[0:MP_e.N_mp]
yn1 = MP_e.y_mp[0:MP_e.N_mp]
zn1 = MP_e.z_mp[0:MP_e.N_mp]
vxn1 = MP_e.vx_mp[0:MP_e.N_mp]
vyn1 = MP_e.vy_mp[0:MP_e.N_mp]
vzn1 = MP_e.vz_mp[0:MP_e.N_mp]
mass = MP_e.mass
charge = MP_e.charge
if Ez_n == 0.:
Ez_n = 0. * xn1
for ii in range(self.N_sub_steps):
Bx_n, By_n, Bz_n = self.B_ob.get_B(xn1, yn1)
boris_step(self.Dtt, xn1, yn1, zn1, vxn1, vyn1, vzn1,
Ex_n, Ey_n, Ez_n, Bx_n, By_n, Bz_n, mass, charge)
MP_e.x_mp[0:MP_e.N_mp] = xn1
MP_e.y_mp[0:MP_e.N_mp] = yn1
MP_e.z_mp[0:MP_e.N_mp] = zn1
MP_e.vx_mp[0:MP_e.N_mp] = vxn1
MP_e.vy_mp[0:MP_e.N_mp] = vyn1
MP_e.vz_mp[0:MP_e.N_mp] = vzn1
return MP_e
def stepcustomDt(self, MP_e, Ex_n, Ey_n, Ez_n=0., Bx_n=0., By_n=0., Bz_n=0., Dt_substep=None, N_sub_steps=None):
if MP_e.N_mp > 0:
xn1 = MP_e.x_mp[0:MP_e.N_mp]
yn1 = MP_e.y_mp[0:MP_e.N_mp]
zn1 = MP_e.z_mp[0:MP_e.N_mp]
vxn1 = MP_e.vx_mp[0:MP_e.N_mp]
vyn1 = MP_e.vy_mp[0:MP_e.N_mp]
vzn1 = MP_e.vz_mp[0:MP_e.N_mp]
mass = MP_e.mass
charge = MP_e.charge
if Ez_n == 0.:
Ez_n = 0. * xn1
for ii in range(N_sub_steps):
Bx_n, By_n, Bz_n = self.B_ob.get_B(xn1, yn1)
boris_step(Dt_substep, xn1, yn1, zn1, vxn1, vyn1, vzn1,
Ex_n, Ey_n, Ez_n, Bx_n, By_n, Bz_n, mass, charge)
MP_e.x_mp[0:MP_e.N_mp] = xn1
MP_e.y_mp[0:MP_e.N_mp] = yn1
MP_e.z_mp[0:MP_e.N_mp] = zn1
MP_e.vx_mp[0:MP_e.N_mp] = vxn1
MP_e.vy_mp[0:MP_e.N_mp] = vyn1
MP_e.vz_mp[0:MP_e.N_mp] = vzn1
return MP_e