-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcharges.py
421 lines (332 loc) · 11.3 KB
/
charges.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
"""Classes of charges used for the MovingChargesField class.
Charge class is abstract class for LinearAcceleratingCharge,
LinearDeceleratingCharge, LinearVelocityCharge, OrbittingCharge,
and OscillatingCharge classes.
"""
import numpy as np
from abc import ABC, abstractmethod
import scipy.constants as constants
# Constants
eps = constants.epsilon_0
pi = constants.pi
e = constants.e
c = constants.c
u_0 = constants.mu_0
class Charge(ABC):
def __init__(self, pos_charge=True):
self.pos_charge = pos_charge
@abstractmethod
def xpos(self, t):
pass
@abstractmethod
def ypos(self, t):
pass
@abstractmethod
def zpos(self, t):
pass
@abstractmethod
def xvel(self, t):
pass
@abstractmethod
def yvel(self, t):
pass
@abstractmethod
def zvel(self, t):
pass
@abstractmethod
def xacc(self, t):
pass
@abstractmethod
def yacc(self, t):
pass
@abstractmethod
def zacc(self, t):
pass
def retarded_time(self, tr, t, X, Y, Z):
"""Returns equation to solve for retarded time - Griffiths Eq. 10.55"""
return ((X-self.xpos(tr))**2 + (Y-self.ypos(tr))**2 + (Z-self.zpos(tr))**2)**0.5 - c*(t-tr)
class OscillatingCharge(Charge):
def __init__(self, pos_charge=True, start_position=(-2e-9, 0, 0),
direction=(1, 0, 0), amplitude=2e-9, max_speed=0.9*c, start_zero=False, stop_t=None):
super().__init__(pos_charge)
self.start_position = np.array(start_position)
self.direction = np.array(direction) \
/ np.linalg.norm(np.array(direction))
self.amplitude = amplitude
self.w = max_speed/amplitude
self.start_zero = start_zero
self.stop_t = stop_t
def xpos(self, t):
xpos = self.start_position[0] \
+ self.direction[0]*self.amplitude*(1-np.cos(self.w*t))
if self.start_zero:
xpos[t < 0] = self.start_position[0]
if self.stop_t is not None:
xpos[t > self.stop_t] = self.start_position[0] \
+ self.direction[0]*self.amplitude * \
(1-np.cos(self.w*self.stop_t))
return xpos
def ypos(self, t):
ypos = self.start_position[1] \
+ self.direction[1]*self.amplitude*(1-np.cos(self.w*t))
if self.start_zero:
ypos[t < 0] = self.start_position[1]
if self.stop_t is not None:
ypos[t > self.stop_t] = self.start_position[1] \
+ self.direction[1]*self.amplitude * \
(1-np.cos(self.w*self.stop_t))
return ypos
def zpos(self, t):
zpos = self.start_position[2] \
+ self.direction[2]*self.amplitude*(1-np.cos(self.w*t))
if self.start_zero:
zpos[t < 0] = self.start_position[2]
if self.stop_t is not None:
zpos[t > self.stop_t] = self.start_position[2] \
+ self.direction[2]*self.amplitude * \
(1-np.cos(self.w*self.stop_t))
return zpos
def xvel(self, t):
xvel = self.direction[0]*self.amplitude*self.w*np.sin(self.w*t)
if self.start_zero:
xvel[t < 0] = 0
if self.stop_t is not None:
xvel[t > self.stop_t] = 0
return xvel
def yvel(self, t):
yvel = self.direction[1]*self.amplitude*self.w*np.sin(self.w*t)
if self.start_zero:
yvel[t < 0] = 0
if self.stop_t is not None:
yvel[t > self.stop_t] = 0
return yvel
def zvel(self, t):
zvel = self.direction[2]*self.amplitude*self.w*np.sin(self.w*t)
if self.start_zero:
zvel[t < 0] = 0
if self.stop_t is not None:
zvel[t > self.stop_t] = 0
return zvel
def xacc(self, t):
xacc = self.direction[0]*self.amplitude*self.w**2*np.cos(self.w*t)
if self.start_zero:
xacc[t < 0] = 0
if self.stop_t is not None:
xacc[t > self.stop_t] = 0
return xacc
def yacc(self, t):
yacc = self.direction[1]*self.amplitude*self.w**2*np.cos(self.w*t)
if self.start_zero:
yacc[t < 0] = 0
if self.stop_t is not None:
yacc[t > self.stop_t] = 0
return yacc
def zacc(self, t):
zacc = self.direction[2]*self.amplitude*self.w**2*np.cos(self.w*t)
if self.start_zero:
zacc[t < 0] = 0
if self.stop_t is not None:
zacc[t > self.stop_t] = 0
return zacc
def get_period(self):
return 2*np.pi/self.w
class OrbittingCharge(Charge):
def __init__(self, pos_charge=True, phase=0, amplitude=2e-9,
max_speed=0.9*c, start_zero=False):
"""Second charge start position is negative x."""
super().__init__(pos_charge)
self.amplitude = amplitude
self.w = max_speed/self.amplitude
self.start_zero = start_zero
self.phase = phase
def xpos(self, t):
xpos = self.amplitude*np.cos(self.w*t+self.phase)
if self.start_zero:
xpos[t < 0] = self.amplitude*np.cos(self.phase)
return xpos
def ypos(self, t):
ypos = self.amplitude*np.sin(self.w*t+self.phase)
if self.start_zero:
ypos[t < 0] = self.amplitude*np.sin(self.phase)
return ypos
def zpos(self, t):
return 0
def xvel(self, t):
xvel = -self.amplitude*self.w*np.sin(self.w*t+self.phase)
if self.start_zero:
xvel[t < 0] = 0
return xvel
def yvel(self, t):
yvel = self.amplitude*self.w*np.cos(self.w*t+self.phase)
if self.start_zero:
yvel[t < 0] = 0
return yvel
def zvel(self, t):
return 0
def xacc(self, t):
xacc = -self.amplitude*self.w**2*np.cos(self.w*t+self.phase)
if self.start_zero:
xacc[t < 0] = 0
return xacc
def yacc(self, t):
yacc = -self.amplitude*self.w**2*np.sin(self.w*t+self.phase)
if self.start_zero:
yacc[t < 0] = 0
return yacc
def zacc(self, t):
return 0
def get_period(self):
return 2*np.pi/self.w
class OscillatingOrbittingCharge(Charge):
def __init__(self, pos_charge=True, phase=0, radius=2e-9,
w=60*2*np.pi, max_speed=0.9*c, start_zero=False):
"""Second charge start position is negative x."""
super().__init__(pos_charge)
self.phase = phase
self.radius = radius
self.w = w
self.max_speed = max_speed
self.A = max_speed/(radius*w)
self.start_zero = start_zero
def xpos(self, t):
xpos = self.radius*np.cos(self.A*np.cos(self.w*t)+self.phase)
if self.start_zero:
xpos[t < 0] = self.radius*np.cos(self.phase)
return xpos
def ypos(self, t):
ypos = self.radius*np.sin(self.A*np.cos(self.w*t)+self.phase)
if self.start_zero:
ypos[t < 0] = self.radius*np.sin(self.phase)
return ypos
def zpos(self, t):
return 0
def xvel(self, t):
xvel = self.max_speed * \
(np.sin(t*self.w)*np.sin(self.A*np.cos(t*self.w)+self.phase))
if self.start_zero:
xvel[t < 0] = 0
return xvel
def yvel(self, t):
yvel = -self.max_speed * \
(np.sin(t*self.w)*np.cos(self.A*np.cos(t*self.w)+self.phase))
if self.start_zero:
yvel[t < 0] = 0
return yvel
def zvel(self, t):
return 0
def xacc(self, t):
xacc = self.max_speed*self.w*(np.cos(t*self.w)*np.sin(self.A*np.cos(t*self.w)+self.phase)
- self.A*np.sin(t*self.w)**2*np.cos(self.A*np.cos(t*self.w)+self.phase))
if self.start_zero:
xacc[t < 0] = 0
return xacc
def yacc(self, t):
yacc = - self.max_speed*self.w*(np.cos(t*self.w)*np.cos(self.A*np.cos(t*self.w)+self.phase)
+ self.A*np.sin(t*self.w)**2*np.sin(self.A*np.cos(t*self.w)+self.phase))
if self.start_zero:
yacc[t < 0] = 0
return yacc
def zacc(self, t):
return 0
def get_period(self):
return 2*np.pi/self.w
class LinearAcceleratingCharge(Charge):
"""Point charge accelerates in x direction starting at origin."""
def __init__(self, pos_charge=True, acceleration=0.1*c, stop_t=None):
super().__init__(pos_charge)
self.acceleration = acceleration
if stop_t is None:
self.stop_t = 0.9999*c/acceleration # so v is never greater than c
else:
self.stop_t = stop_t
def xpos(self, t):
xpos = 0.5*self.acceleration*t**2
xpos[t < 0] = 0
xpos[t > self.stop_t] = 0.5*self.acceleration*self.stop_t**2 + \
self.acceleration*self.stop_t * (t[t > self.stop_t]-self.stop_t)
return xpos
def ypos(self, t):
return 0
def zpos(self, t):
return 0
def xvel(self, t):
xvel = self.acceleration*t
xvel[t < 0] = 0
xvel[t > self.stop_t] = self.acceleration*self.stop_t
return xvel
def yvel(self, t):
return 0
def zvel(self, t):
return 0
def xacc(self, t):
xacc = self.acceleration*np.ones(t.shape)
xacc[t < 0] = 0
xacc[t > self.stop_t] = 0
return xacc
def yacc(self, t):
return 0
def zacc(self, t):
return 0
class LinearDeceleratingCharge(Charge):
"""Point charge decelerates in x direction starting at origin.."""
def __init__(self, pos_charge=True, deceleration=0.1*c, initial_speed=0.999*c, stop_t=None):
super().__init__(pos_charge)
self.deceleration = deceleration
self.initial_speed = initial_speed
if stop_t is None:
self.stop_t = initial_speed/deceleration # so v is zero at stop
else:
self.stop_t = stop_t
def xpos(self, t):
xpos = self.initial_speed*t
xpos[t > 0] = self.initial_speed*t[t > 0] - \
0.5*self.deceleration*t[t > 0]**2
xpos[t > self.stop_t] = self.initial_speed * \
self.stop_t - 0.5*self.deceleration*self.stop_t**2
return xpos
def ypos(self, t):
return 0
def zpos(self, t):
return 0
def xvel(self, t):
xvel = self.initial_speed*np.ones(t.shape)
xvel[t > 0] = self.initial_speed - self.deceleration*t[t > 0]
xvel[t > self.stop_t] = 0
return xvel
def yvel(self, t):
return 0
def zvel(self, t):
return 0
def xacc(self, t):
xacc = np.zeros(t.shape)
xacc[t > 0] = -self.deceleration
xacc[t > self.stop_t] = 0
return xacc
def yacc(self, t):
return 0
def zacc(self, t):
return 0
class LinearVelocityCharge(Charge):
"""Point charge decelerates in x direction starting at origin.."""
def __init__(self, pos_charge=True, speed=0.99*c, init_pos=0):
super().__init__(pos_charge)
self.speed = speed
self.init_pos = init_pos
def xpos(self, t):
return self.speed*t + self.init_pos
def ypos(self, t):
return 0
def zpos(self, t):
return 0
def xvel(self, t):
return self.speed
def yvel(self, t):
return 0
def zvel(self, t):
return 0
def xacc(self, t):
return 0
def yacc(self, t):
return 0
def zacc(self, t):
return 0