-
Notifications
You must be signed in to change notification settings - Fork 1
/
Trap.py
394 lines (313 loc) · 9.79 KB
/
Trap.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
import warnings, itertools
from contextlib import contextmanager
import logging
import numpy as np
import pandas as pd
import xarray as xr
from collections import OrderedDict
from scipy import optimize, constants as ct
from .Electrode import SimulatedElectrode
from .utils import expand_tensor
try:
import cvxopt, cvxopt.modeling
except ImportError:
warnings.warn("cvxopt not found, optimizations will fail", ImportWarning)
cvxopt = None
logger = logging.getLogger("Electrode")
class Trap:
'''A collection of Electrodes.
Parameters
--------
electrodes: ordered dictionary of Electrode, eg. {'DC1': DC1, 'DC2': DC2}
mass: mass of the trapped particle
charge: charge of the trapped particle
scale: length scale of the potential, 1 um from bem
Data structure
--------
electrodes: ordered dictionary of trap electrodes
config: ordered dictionary of trap configuration
'''
def __init__(self, electrodes = OrderedDict(), mass = 40*ct.atomic_mass, charge = ct.elementary_charge, scale = 1.e-6, Omega = 50.E6, **kwargs):
self.electrodes = electrodes
self.config = OrderedDict([('mass', mass),
('charge', charge),
('scale', scale),
('Omega', Omega)])
def update_electrodes(self, elecs):
if type(elecs) is not list:
elecs = [elecs]
for elec in elecs:
self.electrodes.update({elec.name: elec})
@property
def names(self):
'''List of names of the electrodes.
'''
return [el.name for key, el in self.electrodes.items()]
@names.setter
def names(self,names):
'''names is in dictionary format, example: names = {'DC1': 'DC1.1', 'DC2': 'DC2.1'}
'''
for key, el in self.electrodes.items():
el.name = names[key]
# for ei, ni in zip(self,names):
# ei.name = ni
@property
def V_dcs(self):
'''Array of dc voltages of the electrodes
'''
return pd.Series({el.name: el.V_dc for key, el in self.electrodes.items()})
@V_dcs.setter
def V_dcs(self, voltages):
'''Voltages is in dictionary format, example: voltages = {'DC1': 1, 'DC2': 0}
'''
for key, el in self.electrodes.items():
el.V_dc = voltages[el.name]
@property
def V_rfs(self):
'''Array of rf voltages of the electrodes.
'''
return pd.Series({el.name: el.V_rf for key, el in self.electrodes.items()})
@V_rfs.setter
def V_rfs(self, voltages):
'''Voltages is in dictionary format, example: voltages = {'RF1': 100, 'RF2': -100}
'''
for key, el in self.electrodes.items():
el.V_rf = voltages[el.name]
# def __getitem__(self, name_or_index):
# '''Electrode lookup.
# Returns
# ------
# Electrode
# The electrode given by its name or index.
# None if not found by name
# Raises
# ------
# IndexError
# If electrode index does not exist
# '''
# try:
# return list.__getitem__(self,name_or_index)
# except TypeError:
# for ei in self:
# if ei.name == name_or_index:
# return ei
# Electrode = __getitem__
@contextmanager
def with_voltages(self, V_dcs=None, V_rfs=None):
'''Returns a contextmanager with temporary voltage setting.
This is a convenient way to temporarily change the voltages
and they are reset to their old values.
Parameters
------
V_dcs : dictionary format
dc voltages for specific electrodes, or don't include in the dictionary/or None to keep the same
V_rfs : dictionary format
dc voltages for specific electrodes, or don't include in the dictionary/or None to keep the same
Returns
------
contextmanager
Example
------
>>> t = Trap()
>>> with t.with_voltages(V_dcs = {'DC1': 1, 'DC2': 0}, V_rfs = {'RF1': 100, 'RF2': -100}):
print(t.potential([0,0,1]))
'''
try:
if V_dcs is not None:
V_dcs, self.V_dcs = self.V_dcs, V_dcs
if V_rfs is not None:
V_rfs, self.V_rfs = self.V_rfs, V_rfs
yield
finally:
if V_dcs is not None:
self.V_dcs = V_dcs
if V_rfs is not None:
self.V_rfs = V_rfs
@contextmanager
def with_config(self, new_config=None):
'''Returns a contextmanager with temporary config setting.
This is a convenient way to temporarily change the configs
and they are reset to their old values.
Parameters
------
config : dictionary format
Returns
------
contextmanager
Example
------
>>> t = Trap()
>>> with t.with_config({'scale' = 1.e-3}):
print(t.potential([0,0,1]))
'''
try:
if new_config is not None:
old_config = self.config.copy()
for key, value in new_config.items():
self.config[key] = value
yield
finally:
if new_config is not None:
self.config = old_config
def dc_potential(self, x = None, y = None, z = None, derivative=0, expand=False):
'''Electrical potential derivative from the DC voltages contribution.
Parameters
-------
x, y, z: array_like, shape (n,1) for each
Positions to evaluate the potential at.
derivative: int
Derivative order
expand: bool
If True, return the fully expanded tensor, else return the reduced form.
Returns
------
potential: xarray
Potential at (x, y, z)
If expand == False, shape (n, l) and l = 2*derivative+1 is the derivative index
Else, shape (n, 3, ..., 3) and returns the fully expanded tensorial form
See Also
------
system.electrical_potential
utils.expand_tensor
Note
-----
Haven't implement the higher order derivative method yet
'''
pot = []
for key, ei in self.electrodes.items():
vi = getattr(ei, 'V_dc', None)
if vi:
pot.append(ei.potential(x, y, z, derivative,voltage=vi))
pot = sum(pot)
if expand:
pot = expand_tensor(pot) # when derivative >= 2, return numpy.array instead of xarray.DataArray
pass
return pot
def rf_potential(self, x = None, y = None, z = None, derivative=0, expand=False):
'''Electrical potential derivative from the RF voltages contribution.
Parameters
-------
x, y, z: array_like, shape (n,1) for each
Positions to evaluate the potential at.
derivative: int
Derivative order
expand: bool
If True, return the fully expanded tensor, else return the reduced form.
Returns
------
See `dc_potential`
See Also
------
system.electrical_potential
utils.expand_tensor
Note
-----
Haven't implement the higher order derivative method yet
'''
pot = []
for key, ei in self.electrodes.items():
vi = getattr(ei, 'V_rf', None)
if vi:
pot.append(ei.potential(x, y, z, derivative,voltage=vi))
pot = sum(pot)
if expand:
pot = expand_tensor(pot)
pass
return pot
def time_dependent_potential(self, x = None, y = None, z = None, derivative=0, t=0., expand=False):
'''Electric potential at an instant. No pseudopotential averaging.
V_dc + cos(omega*t)*V_rf
Parameters
-------
x, y, z: array_like, shape (n,1) for each
Positions to evaluate the potential at.
derivative: int
Derivative order
t: float
Time instant
omega: float
RF frequency
expand: bool
Expand to full tensor form if True
Returns
-------
See `dc_potential`
See Also
-------
system.time_potential
Note
-----
Haven't implement the higher order derivative method yet
Include the frequency of the rf potential as well
'''
omega = self.config['Omega']
dc = self.dc_potential(x, y, z, derivative, expand)
rf = self.rf_potential(x, y, z, derivative, expand)
return dc + np.cos(omega*t)*rf
def pseudo_potential(self, x = None, y = None, z = None, derivative = 0):
'''The pseudopotential/ ponderomotive potential
Parameters
-------
x, y, z: array_like, shape (n,1) for each
Positions to evaluate the potential at.
derivative: int <= 2
Derivative order. Currently only implemented up to 2nd order
Returns
------
potential, array, shape (n, 3, ..., 3)
Pseudopotential derivative. Fully expanded since this is not generally harmonic
'''
q = self.config['charge']
m = self.config['mass']
l = self.config['scale']
o = self.config['Omega']
rf_scale = np.sqrt(q/m)/(2*l*o)
p = [self.rf_potential(x, y, z, derivative=i, expand=True) for i in range(1, derivative+2)] # pseudopotential is proportional to field (derivative = 1) squared
if derivative == 0:
return rf_scale**2 * xr.dot(p[0], p[0], dims = 'l')
# below return numpy array
elif derivative == 1:
return rf_scale**2 * 2 * np.einsum("ijkl, ijklm->ijkm",p[0],p[1])
elif derivative == 2:
return rf_scale**2 * 2 * (np.einsum("ijklm,ijkln -> ijkmn",p[1],p[1]) + np.einsum("ijkl,ijklmn->ijkmn",p[0],p[2]))
else:
raise ValueError("only know how to generate pseupotentials up to 2nd order")
return
def total_potential(self, x = None, y = None, z = None, derivative=0):
'''Combined electrical and pseudo potential.
Parameters
------
x, y, z: array_like, shape (n,1) for each
Positions to evaluate the potential at.
derivative :
Order of derivative
Returns
------
potential: array
'''
dc = self.dc_potential(x, y, z, derivative, expand=True)
rf = self.pseudo_potential(x, y, z, derivative)
return dc + rf
def individual_potential_contribution(self, x = None, y = None, z = None, derivative=0):
'''Individual contributions to the electrical potential from all the electrodes.
Returns an array of the contributions by each electrode in the trap to the potential at points x
Each electrode is taken to have unit voltage while grounding all other electrodes
Parameters
-------
x, y, z: array_like, shape (n,1) for each
Positions to evaluate the potential at.
derivative: int
Derivative order
Returns
-------
potential_matrix : dictionary, m keys, each value is a shape (n,l) array
`m` is the electrode index (index into `self`). `n` is the point index,
`l = 2*derivative + 1` is the derivative index
See Also
-------
system.individual_potential
'''
potential_matrix = OrderedDict()
for key, ei in self.electrodes.items():
potential_matrix.update({key: ei.potential(x, y, z, derivative)})
return potential_matrix