-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_field_fitting.py
160 lines (121 loc) · 3.93 KB
/
test_field_fitting.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
# %%
import time
import numpy as np
import scipy.linalg as scylin
import matplotlib.pyplot as mplt
import matplotlib.gridspec as mgs
path = '/home/imas/repos/id-sabia/model-03/simulation/magnetic/'
path += 'fieldmaps_and_kickmaps/'
fname = '2021-03-14_DeltaSabia_'
fname += 'EllipticalPolarization_'
fname += 'Kh=2.23_Kv=5.58_X=-7_7mm_Y=-4_4mm_Z=-1000_1000mm'
fname += '.fld'
X, Y, Z, Bx, By, Bz = np.loadtxt(path+fname, skiprows=21, unpack=True)
# %%
x = np.sort(list({x for x in X}))
y = np.sort(list({y for y in Y}))
z = np.sort(list({z for z in Z}))
X = X.reshape((z.size, y.size, x.size))
Y = Y.reshape((z.size, y.size, x.size))
Z = Z.reshape((z.size, y.size, x.size))
Bx = Bx.reshape((z.size, y.size, x.size))
By = By.reshape((z.size, y.size, x.size))
Bz = Bz.reshape((z.size, y.size, x.size))
# %%
idx = np.argmin(np.abs(x))
idy = np.argmin(np.abs(y))
idz = np.argmin(np.abs(z))
fig = mplt.figure(figsize=(6, 5))
gs = mgs.GridSpec(1, 1, left=0.12, right=0.98, bottom=0.12)
ax = fig.add_subplot(gs[0, 0])
ax.plot(z, Bx[:, idy, idx])
# ax.plot(y, Y[0, :, 0])
# ax.plot(x, X[0, 0, :])
mplt.show()
# %%
def field_by_on_axis(kx, kz, cmat, phimat, x, y, z):
bx = np.zeros(x.shape, dtype=float)
by = np.zeros(x.shape, dtype=float)
bz = np.zeros(x.shape, dtype=float)
for nx in range(cmat.shape[0]):
for nz in range(1, cmat.shape[1]):
kxn = nx*kx
kzn = nz*kz
kyn = np.sqrt(kxn*kxn + kzn*kzn)
cosx = np.cos(kxn*x)
cosy = np.cosh(kyn*y)
cosz = np.cos(kzn*z + phimat[nx, nz])
byn = cmat[nx, nz] * cosx
byn *= cosy
byn *= cosz
sinx = np.sin(kxn*x)
siny = np.sinh(kyn*y)
bxn = cmat[nx, nz] * sinx
bxn *= kxn / kyn
bxn *= siny
bxn *= cosz
sinz = np.sin(kzn*z + phimat[nx, nz])
bzn = cmat[nx, nz] * cosx
bzn *= kzn / kyn
bzn *= siny
bzn *= sinz
bx += bxn
by += bzn
bz += bzn
return bx, by, bz
def field_bx_on_axis(ky, kz, cmat, phimat, x, y, z):
bx = np.zeros(x.shape, dtype=float)
by = np.zeros(x.shape, dtype=float)
bz = np.zeros(x.shape, dtype=float)
for ny in range(cmat.shape[0]):
for nz in range(1, cmat.shape[1]):
kyn = ny*ky
kzn = nz*kz
kxn = np.sqrt(kyn*kyn + kzn*kzn)
cosx = np.cosh(kxn*x)
cosy = np.cos(kyn*y)
cosz = np.cos(kzn*z + phimat[nx, nz])
bxn = cmat[nx, nz] * cosx
bxn *= cosy
bxn *= cosz
sinx = np.sinh(kxn*x)
siny = np.sin(kyn*y)
byn = (cmat[nx, nz]*kyn/kxn) * sinx
byn *= siny
byn *= cosz
sinz = np.sin(kzn*z + phimat[nx, nz])
bzn = (cmat[nx, nz]*kzn/kxn) * cosx
bzn *= siny
bzn *= sinz
bx += bxn
by += bzn
bz += bzn
return bx, by, bz
def field_bz_on_axis_dby(kx, kz, cmat, phimat, x, y, z):
bx = np.zeros(x.shape, dtype=float)
by = np.zeros(x.shape, dtype=float)
bz = np.zeros(x.shape, dtype=float)
for nx in range(cmat.shape[0]):
for nz in range(1, cmat.shape[1]):
kxn = nx*kx
kzn = nz*kz
kyn = np.sqrt(kxn*kxn + kzn*kzn)
cosx = np.cos(kxn*x)
cosy = np.cosh(kyn*y)
sinz = np.sin(kzn*z + phimat[nx, nz])
bzn = cmat[nx, nz] * cosx
bzn *= cosy
bzn *= cosz
sinx = np.sinh(kxn*x)
siny = np.sin(kyn*y)
cosz = np.cos(kzn*z + phimat[nx, nz])
byn = (cmat[nx, nz]*kyn/kxn) * sinx
byn *= siny
byn *= cosz
bzn = (cmat[nx, nz]*kzn/kxn) * cosx
bzn *= siny
bzn *= sinz
bx += bxn
by += bzn
bz += bzn
return bx, by, bz