-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_results.py
153 lines (130 loc) · 5.48 KB
/
plot_results.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
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import os
import plotly.graph_objects as go
from scipy import interpolate
## Field maps at constant z faces
folder = 'results/both_magnets'
files = [file for file in os.listdir(folder) if file.endswith('Bfield.csv')]
def compileResults(files):
results = pd.DataFrame()
for file in files:
filename = f'{folder}/{file}'
results = pd.concat([results, pd.read_csv(filename)])
# Sort results by x, y, and z coordinates
results = results.sort_values(['x', 'y', 'z'])
# Add columns for normal and tangential values
results['r'] = np.sqrt(results['x'] ** 2 + results['y'] ** 2)
results['theta'] = np.arctan(results['y'] / results['x'])
results['Br'] = (results['Bx'] * results['x'] + results['By'] * results['y']) / results['r']
results['Btheta'] = (results['By'] * results['x'] - results['Bx'] * results['y']) / results['r']
return results
results = compileResults(files)
results.to_csv('results/compiledResults.csv')
fields = ['Bx', 'By', 'Bz', 'magnitude', 'Br', 'Btheta']
for z, z_grp in results.groupby('z'):
x = z_grp['x'].to_numpy()
y = z_grp['y'].to_numpy()
r = z_grp['r'].to_numpy()
theta = z_grp['theta'].to_numpy()
for field in fields:
B = z_grp[field].to_numpy()
fig = plt.figure()
ax = fig.add_subplot()
ax.set_aspect('equal', adjustable='box')
plt.tricontourf(x, y, B, levels=20)
plt.colorbar(label=f'{field} (T)')
plt.axhline(color='white')
plt.axvline(color='white')
plt.xlabel('x (cm)')
plt.ylabel('y (cm)')
plt.title(f'z={z} cm')
plt.grid('on')
plt.savefig(f'images/{field}_z={z}_cm.png', dpi=200)
## Interpolate to find the location of the field center
if field == 'Br' or field == 'Bz':
B_function = interpolate.interp2d(np.unique(x), np.unique(y), B, kind='cubic')
x_interp = np.linspace(min(x), max(x), 200)
y_interp = np.linspace(min(y), max(y), 200)
B_interp = B_function(x_interp, y_interp)
if field == 'Br':
yMaxIndex, xMaxIndex = np.where(np.abs(B_interp) == np.amin(np.abs(B_interp)))
if field == 'Bz':
yMaxIndex, xMaxIndex = np.where(np.abs(B_interp) == np.amax(np.abs(B_interp)))
xMax = x_interp[xMaxIndex]
yMax = y_interp[yMaxIndex]
print(f'The {field}-field interpolated center of the plane z={z} cm is at (x, y) = {xMax}, {yMax}')
## Plots showing symmetry
if field == 'Bz':
xLim = 32
yLim = 16
xSteps = int(xLim / 2 + 1) # spacing of 4 cm
ySteps = int(yLim / 2 + 1) # spacing of 4 cm
x_interp = np.linspace(-xLim, xLim, xSteps)
y_interp = np.linspace(-yLim, yLim, ySteps)
B_interp = B_function(x_interp, y_interp)
# Delta B is the difference between opposite points around the center
DeltaB = B_interp - np.flip(B_interp)
DeltaBNormalized = DeltaB / B_interp
fig = plt.figure()
ax = fig.add_subplot()
ax.set_aspect('equal', adjustable='box')
plt.contourf(x_interp, y_interp, DeltaBNormalized, levels=10)
plt.colorbar(label=f'Normalized symmetry $\Delta${field}/{field}')
plt.axhline(color='white')
plt.axvline(color='white')
plt.xlabel('x (cm)')
plt.ylabel('y (cm)')
plt.title(f'z={z} cm')
plt.grid('on')
plt.savefig(f'images/Delta{field}_z={z}_cm.png', dpi=200)
## Flux lines streamplot
z_plane = 0.0 # cm
streamplotResults = results[results['x'] == 0.8]
x = np.linspace(min(streamplotResults['x']), max(streamplotResults['x']), 23)
y = np.linspace(min(streamplotResults['y']), max(streamplotResults['y']), 16)
z = np.linspace(min(streamplotResults['z']), max(streamplotResults['z']), 5)
Z, Y = np.meshgrid(z, y)
By = np.reshape(streamplotResults['By'].to_numpy(), (16, 5))
Bz = np.reshape(streamplotResults['Bz'].to_numpy(), (16, 5))
## Flux lines
plt.figure()
plt.streamplot(Z, Y, Bz, By, density=0.5)
ax.set_aspect('equal', adjustable='box')
plt.title('Flux lines at x=0.8')
plt.xlabel('z (cm)')
plt.ylabel('y (cm)')
plt.savefig('images/flux_lines.png', dpi=200)
## Isocontours
isocontourFields = ['Br', 'Bz']
for field in isocontourFields:
fig = go.Figure(data=go.Isosurface(
x=results['x'].to_numpy(),
y=results['y'].to_numpy(),
z=results['z'].to_numpy(),
value=results[field].to_numpy(),
opacity=0.6,
surface_count=20,
caps=dict(x_show=False, y_show=False, z_show=False),
colorscale='BlueRed',
colorbar={'title': field}
))
fig.show()
## Linear plot along central axis
os.listdir(folder)
linearFiles = [file for file in os.listdir(folder) if file.endswith('Bfield_linear.csv')]
linearResults = compileResults(linearFiles)
linearResults.to_csv('results/linearResultsCompiled.csv')
linearResults['dBdz'] = linearResults['Bz'].diff()/linearResults['z'].diff() * 20
linearFields = ['Bx', 'By', 'Bz', 'magnitude', 'dBdz']
for (x, y), xy_grp in linearResults.groupby(['x', 'y']):
fig, ax = plt.subplots()
for field in linearFields:
xy_grp.plot(ax=ax, x='z', y=field)
ax.axvline(color='black')
ax.set_xlabel('z (cm)')
ax.set_ylabel('Magnetic Field (T)')
ax.set_title(f'(x, y)=({x}, {y}) cm')
ax.grid('on')
fig.savefig('images/linearField.png', dpi=200)