-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMagneticCoil.py
186 lines (169 loc) · 5.17 KB
/
MagneticCoil.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
import numpy as np
import matplotlib.pyplot as plt
from scipy import special as sp
def singlecoil(a, r):
"Magnetic field due to a single coil"
# The coil is shifted from origin by a distance d along z-axis
# Hence, the center of the coil is at (0,0,d) with normal along +ve z-axis
# a is the radius of the coil in units of d
# r is the list of coordinates where magnetic field is to be calculated in units of d
# r is in general of form (x,y,z) w.r.t origin in units of d
# B[0] and B[1] give Axial and Radial Magnetic Fields respectively in inverse units of
# (mu I)/(2 pi d)
x=r[0]
y=r[1]
z=r[2]
rho=np.sqrt(x**2+y**2) #radial distance in units of d
B=[0, 0]
k=np.sqrt((4*a*rho)/((a+rho)**2+(z-1)**2))
K=sp.ellipk(k**2)
E=sp.ellipe(k**2)
B[0]=(1/np.sqrt((a+rho)**2+(z-1)**2))*(K+E*(a**2-rho**2-(z-1)**2)/((a-rho)**2+(z-1)**2))
if rho==0:
B[1]=0
else:
B[1]=((z-1)/rho)*(1/np.sqrt((a+rho)**2+(z-1)**2))*(-K+E*(a**2-rho**2-(z-1)**2)/((a-rho)**2+(z-1)**2))
return B
def antihelm(a, r):
"Anti-Helmholtz Coils"
# The net magnetic field is difference of fields of two single coils
# The single coils are kept at positions +z and -z (in units of d) from center
Bres=[0, 0]
x=r[0]
y=r[1]
z=r[2]
Bres1=singlecoil(a, [x, y, z])
Bres2=singlecoil(a, [x, y, -z])
Bres[0]=Bres1[0]-Bres2[0]
Bres[1]=Bres1[1]-Bres2[1]
return Bres
def adjustB(a, z, d):
"Ajusted Field"
#z = np.linspace(-1,1,1000)
Badj=2*10**(-5)*antihelm(a, [0, 0, z])[0]/d
return Badj
def adjustBrad(a, z, d):
"Ajusted Field"
#z = np.linspace(-1,1,1000)
Badj=2*10**(-5)*antihelm(a, [z, 0, 0])[1]/d
return Badj
def adjustB2(a, z, d):
"Ajusted Field"
#z = np.linspace(-1,1,1000)
Badj=2*10**(-5)*helm(a, [0, 0, z])[0]/d
return Badj
#Call the function
Z=np.linspace(-0.004,0.004,50000)
Bfin=np.zeros(50000)
Bfinrad=np.zeros(50000)
len=0.0
for i in range(0,9):
d=0.002*i+0.025
for j in range(0,9):
rad=0.025+0.002*j
a=rad/d
z2=Z/d
len=len+2*np.pi*rad
Bfin=Bfin+adjustB(a, z2, d)
#Bfinrad=Bfinrad+adjustBrad(a, z2, d)
print(len)
z = np.linspace(-1,1,1000)
Bnew=adjustB(0.71, z, 0.045)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
#for i in range(0,1500):
# a0=i/500+0.25
# Bplot=adjustB(a0, z, 0.05)
# plt.plot(z, np.gradient(Bplot,z), 'r')
# plot the function
#plt.plot(Z, Bfin, 'r')
#plt.plot(z, Bnew, 'r')
#plt.plot(Z, np.gradient(Bfin, Z), 'r', Z, np.gradient(Bfinrad, Z), 'b')
plt.plot(Z, np.gradient(Bfin, Z), 'r')
print (Bfin[25001])
"""
B=antihelm(0.5, [0, 0, 0])
print (B)
z = np.linspace(-1,1,1000)
R = np.linspace(0.25,2.5,1000)
#Bz=antihelm(R,[0, 0, 1])
#print R, Bz
Bz=np.zeros([1000, 1000])
grad=np.zeros(1000)
max=-np.inf
dgrad=np.zeros([1000, 1000])
k=0
for i in range(0,999):
r0=i/500+0.25
Bz[i]=antihelm(r0,[0, 0, z])[0]
grad[i]=np.gradient(Bz[i])[500]
dgrad[i]=np.gradient(np.gradient(Bz[i]))
if grad[i]>max:
max=grad[i]
k=i
print (max, k/100+0.25, grad[100], dgrad[100][250])
BvalZ=antihelm(1.13, [0, 0, z])
gradZ=np.gradient(BvalZ[0], z)
Bhelm=helm(1.75, [0, 0, z])
#print max(BvalZ)
#Cgrad=appgrad(0.5, d)
linB=linearField(1.13, [0, 0, z])
#for i in R:
# temp=antihelm(i, [0, 0, z])
# Bz[i]=temp[0]
# GradZ[i]=np.gradient(Bz[i])
#DGradZ[i]=np.gradient(GradZ[i])
#fig = plt.figure()
#ax = fig.add_subplot(1, 1, 1)
#ax.spines['left'].set_position('center')
#ax.spines['bottom'].set_position('zero')
#ax.spines['right'].set_color('none')
#ax.spines['top'].set_color('none')
#ax.xaxis.set_ticks_position('bottom')
#ax.yaxis.set_ticks_position('left')
# plot the function
#plt.plot(z, gradZ, 'r')
#plt.plot(z, Bhelm[0], 'b')
#plt.plot(z, np.gradient(np.gradient(BvalZ[0])), 'r')
#plt.plot(R, DGradZ, 'r')
#plt.plot(z,np.gradient(BvalZ[0]), 'r', z, np.gradient(BvalZ[1]), 'b')
Z=np.linspace(-0.04,0.04,50000)
Bfin=np.zeros(50000)
len=0.0
for i in range(0,10):
d=0.002*i+0.020
for j in range(0,10):
rad=0.040-0.002*j
a=rad/d
z2=Z/d
len=len+2*np.pi*rad
Bfin=Bfin+adjustB2(a, z2, d)
print(len)
z = np.linspace(-1,1,1000)
Bnew=adjustB2(0.71, z, 0.045)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
#for i in range(0,1500):
# a0=i/500+0.25
# Bplot=adjustB(a0, z, 0.05)
# plt.plot(z, np.gradient(Bplot,z), 'r')
# plot the function
plt.plot(Z, Bfin, 'r')
#plt.plot(z, Bnew, 'r')
#plt.plot(Z, np.gradient(Bfin, Z), 'r')
print (Bfin[5001])
"""
# show the plot
plt.show()