-
Notifications
You must be signed in to change notification settings - Fork 0
/
coilSimulation_real.py
214 lines (174 loc) · 8.47 KB
/
coilSimulation_real.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Simulation of the magnetic field generated by a anti-helmholtz coil.
The coils are assumed to be squared packaged thin wires. Each turn is
calculated individualy, that is why this simulation is substantily slower than
the exact calculation. It is
recomended that one do the exact simulation before the running this code.
It calculates the magnetic field in the z direction at x=0 and y=0. The coils
normal vector points in the +z direction.
"""
# %% ############## Imports ########################
import pyqtgraph as pg
# import pyqtgraph.console
from pyqtgraph.dockarea import *
from pyqtgraph.Qt import QtCore, QtGui, QtWidgets
from functools import partial
import numpy as np
import sys
from coil_utils import field_loop
from pyQtUtils.pyQt_utils import slider_win, setSlider, mySlider
class Window(QtGui.QMainWindow):
"""Missing docstring."""
def __init__(self):
"""Missing docstring."""
super().__init__()
# %% plot parameters
self.z_min = -10e-2 # m
self.z_max = 10e-2 # m
self.n_points = 100 # number of points to evaluate field within z_min and z_max
if self.z_max>self.z_min:
self.z_array = np.linspace(self.z_min, self.z_max, self.n_points)
else:
raise ValueError('z_max must be bigger than z_min')
# pre-allocate Bz
self.Bz1 = np.zeros(self.n_points)
self.Bz2 = np.zeros(self.n_points)
# %% ########### variables parameters ########################
# parameter table 1
self.variables = dict(T=150, Rmin=2e-2, Dmin=4.3e-2/2,Dmax=4.3e-2/2 + 1e-2, I=1, wire_diameter=0.5e-3)
self.variables_min = dict(T=1, Rmin=0.5e-2, Dmin=1e-2, Dmax=2e-2, I=0, wire_diameter=0.1e-3)
self.variables_max = dict(T=500, Rmin=10e-2, Dmin=10e-2, Dmax=15e-2, I=10,wire_diameter=1e-2)
self.variables_step = dict(T=1, Rmin=.1e-2, Dmin=.1e-2/2, Dmax=.1e-2/2, I=.5,wire_diameter=0.05e-3)
self.variables_txt = dict(T='(turns)', Rmin='(m)',Dmin='(m)',Dmax='(m)', I='(A)',wire_diameter=' (m)')
self.table1 = list(self.variables.keys())
# %% ########## window parameters ############
self.resize(1000, 800)
self.setWindowTitle('FI226 - Coil Simulation (square packing)')
# %% ########## Init dockable area ###############
self.area = DockArea()
self.setCentralWidget(self.area)
# %% ################ Create and place docks ##################
self.d1 = Dock("Magnetic field in the z direction Bz for x=0 and y=0", size=(1, 1), closable=True)
self.d2 = Dock("Derivative of Bz with respect to z", size=(1, 1), closable=True)
self.d3 = Dock("parameter table", size=(1, 1))
self.area.addDock(self.d1, 'left')
# self.area.addDock(self.d1, 'above', self.d6)
self.area.addDock(self.d2, 'right')
self.area.addDock(self.d3, 'bottom')
# Move docks programatically after they have been placed
# self.area.moveDock(self.d4, 'top', self.d2) # move d4 to top edge of d2
# %% #################### dock 1 ########################
self.w1 = pg.LayoutWidget()
# self.d1.hideTitleBar()
# Add (and set) a plot into this dock
self.w1 = pg.PlotWidget(title=None)
self.w1.setLabel('bottom', text='z position', units='m')
self.w1.setLabel('left', text='Bz', units='G')
self.curve_plot1_1 = self.w1.plot([], pen='g')
self.curve_plot1_2 = self.w1.plot([], pen='r')
self.d1.addWidget(self.w1)
# %% #################### dock 2 ########################
self.w2 = pg.LayoutWidget()
# self.d1.hideTitleBar()
# Add (and set) a plot into this dock
self.w2 = pg.PlotWidget(title=None)
self.w2.setLabel('bottom', text='z position', units='m')
self.w2.setLabel('left', text='dBz/dz', units='G/cm')
self.curve_plot2_1 = self.w2.plot([], symbolBrush=None, pen='y')
self.curve_plot2_2 = self.w2.plot([], pen='g')
self.d2.addWidget(self.w2)
# %% #################### dock 3 ########################
# This is a parameter dock.
self.sliders = dict()
for idx, variable in enumerate(self.table1):
value = self.variables[variable]
min = self.variables_min[variable]
max = self.variables_max[variable]
step = self.variables_step[variable]
txt = self.variables_txt[variable]
self.sliders[variable] = self.createSlider(variable, min, max, step, value, txt)
self.d3.addWidget(self.sliders[variable]['btn'], row=idx, col=1)
self.d3.addWidget(self.sliders[variable]['slider'], row=idx, col=2)
self.d3.addWidget(self.sliders[variable]['txt'], row=idx, col=3)
self.d3.addWidget(self.sliders[variable]['lineEdit'], row=idx, col=4)
# %% ################### act ############################
self.update()
self.show()
def createSlider(self, variable, min, max, step, value, extra_text=None):
"""Fast slider creation.
It creates all the labels and a settings button for your slider.
"""
slider = dict()
slider['btn'] = QtGui.QPushButton('Set')
slider['btn'].setFixedWidth(40)
slider['slider'] = mySlider(QtCore.Qt.Horizontal, self)
slider['slider'].setStep(step)
slider['slider'].setMaximum(max)
slider['slider'].setMinimum(min)
slider['slider'].setValue(value)
if extra_text is None:
slider['txt'] = QtGui.QLabel(variable)
else:
slider['txt'] = QtGui.QLabel(variable+''+extra_text)
slider['txt'].setContentsMargins(20, 0, 0, 0)
slider['lineEdit'] = QtGui.QLineEdit(self)
slider['lineEdit'].setFixedWidth(100)
slider['lineEdit'].setContentsMargins(20, 0, 20, 0)
slider['lineEdit'].setText('{0}'.format(value))
# slider['lineEdit'].setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter)
# Connect
slider['btn'].clicked.connect(partial(self.openSliderWindow, slider['slider']))
slider['lineEdit'].editingFinished.connect(partial(self.update_from_text, variable, slider['lineEdit'], slider['slider'])) # Edit this
slider['slider'].valueChanged.connect(partial(self.update_from_slider, variable, slider['slider'], slider['lineEdit'])) # Edit this
return slider
def update_from_text(self, variable, lineEdit, slider):
value = float(lineEdit.text())
self.variables[variable] = value # Edit this
slider.blockSignals(True)
slider.setValue(value)
slider.blockSignals(False)
self.update()
def update_from_slider(self, variable, slider, lineEdit):
value = slider.value()
self.variables[variable] = value # Edit this
lineEdit.blockSignals(True)
lineEdit.setText('{0}'.format(value))
lineEdit.setCursorPosition(0)
lineEdit.blockSignals(False)
# print('{0} set to {1}'.format(variable, self.variables[variable]))
self.update()
def openSliderWindow(self, slider):
"""Open the slider settings window."""
self.dialog = slider_win(slider)
self.dialog.show()
# %% ################# update function #######################
def update(self):
"""Missing docstring."""
Rmin = self.variables['Rmin']
d = self.variables['wire_diameter']
Dmax = self.variables['Dmax']
Dmin = self.variables['Dmin']
I = self.variables['I']
# Calculate and plot
packing_number_D = (Dmax - Dmin)/d
Rmax = Rmin + (self.variables['T']/packing_number_D * d)
R_points = np.arange(Rmin, Rmax, d)
D_points = np.arange(Dmin, Dmax, d)
for D in D_points:
for R in R_points:
for idx, z in enumerate(self.z_array):
r_c = [0, 0, z]
self.Bz1[idx] += field_loop(r_c, [0, 0, abs(D)], [0, 0, 1], R, I)[2]
self.Bz2[idx] += field_loop(r_c, [0, 0, -abs(D)], [0, 0, -1], R, I)[2]
x = self.z_array
y = (self.Bz1 + self.Bz2)*10**4
self.curve_plot1_1.setData(x, y)
x = self.z_array[:-1]
y = np.diff(y)/np.diff(self.z_array*10**2)
self.curve_plot2_1.setData(x, y)
if __name__ == '__main__':
print('main')
root = QtWidgets.QApplication(sys.argv)
app = Window()
sys.exit(root.exec_())