-
Notifications
You must be signed in to change notification settings - Fork 1
/
mpl_utils.py
193 lines (143 loc) · 5.74 KB
/
mpl_utils.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
import math as mth
import numpy as np
import matplotlib.pyplot as plt
import scipy.special as sp
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.widgets import Slider, Button, RadioButtons
from matplotlib import cm, colors
import matplotlib
try:
matplotlib.rcParams.update({
"pgf.texsystem": "pdflatex",
'font.family': 'serif',
'text.usetex': True,
'pgf.rcfonts': False,
})
except:
print('Could not Latex-ify labels ')
import physics_utils
import vtk_utils
# Define some limits for axes and deformations
limAxis = 15
limDef = 5
def matplotlib_render(A, beta2, beta3, beta4, m2, m3, m4, theta, phi):
r = physics_utils.calculate_r(A, beta2, m2, beta3, m3, beta4, m4, theta)
x = physics_utils._x(r, theta, phi)
y = physics_utils._y(r, theta, phi)
z = physics_utils._z(r, theta)
R = physics_utils._R(x, y, z)
N = R/R.max()
# Make the plot
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'), figsize=(6,5))
plt.subplots_adjust(bottom=0.30, top=0.99)
im = ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=cm.jet(N), shade=True)
# Set axis limits
ax.axes.set_xlim3d( left=-1*limAxis, right=limAxis)
ax.axes.set_ylim3d(bottom=-1*limAxis, top=limAxis)
ax.axes.set_zlim3d(bottom=-1*limAxis, top=limAxis)
# Make a colorbar
m = cm.ScalarMappable(cmap=cm.jet)
m.set_array(R)
m.set_clim(vmin=4, vmax=20)
fig.colorbar(m)
# Make some sliders for changing variables
axcolor = 'lightgoldenrodyellow'
aA = plt.axes([0.35,0.23,0.5,0.03], facecolor=axcolor)
aB2 = plt.axes([0.35,0.18,0.5,0.03], facecolor=axcolor)
aB3 = plt.axes([0.35,0.13,0.5,0.03], facecolor=axcolor)
aB4 = plt.axes([0.35,0.08,0.5,0.03], facecolor=axcolor)
aM2 = plt.axes([0.07,0.18,0.15,0.03], facecolor=axcolor)
aM3 = plt.axes([0.07,0.13,0.15,0.03], facecolor=axcolor)
aM4 = plt.axes([0.07,0.08,0.15,0.03], facecolor=axcolor)
sA = Slider(aA,'A',0,250, valinit=A, valstep=1)
sB2 = Slider(aB2,r'$\beta_2$',-1*limDef,limDef, valinit=beta2)
sB3 = Slider(aB3,r'$\beta_3$',-1*limDef,limDef, valinit=beta3)
sB4 = Slider(aB4,r'$\beta_4$',-1*limDef,limDef, valinit=beta4)
sM2 = Slider(aM2,r'$m_2$',-2, 2, valinit=m2, valstep=1)
sM3 = Slider(aM3,r'$m_3$',-3, 3, valinit=m3, valstep=1)
sM4 = Slider(aM4,r'$m_4$',-4, 4, valinit=m4, valstep=1)
resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
reset_button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
exportax = plt.axes([0.9, 0.025, 0.1, 0.04])
export_button = Button(exportax, 'Export', color=axcolor, hovercolor='0.975')
renderax = plt.axes([0.7, 0.025, 0.1, 0.04])
render_button = Button(renderax, 'Render', color=axcolor, hovercolor='0.975')
# Define function for updating plot when changing sliders
def update(val):
A = sA.val
beta2 = sB2.val
beta3 = sB3.val
beta4 = sB4.val
m2 = sM2.val
m3 = sM3.val
m4 = sM4.val
ax.clear()
r = physics_utils.calculate_r(A, beta2, m2, beta3, m3, beta4, m4, theta)
x = physics_utils._x(r, theta, phi)
y = physics_utils._y(r, theta, phi)
z = physics_utils._z(r, theta)
R = physics_utils._R(x, y, z)
N = R/R.max()
im = ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=cm.jet(N), shade=True)
ax.axes.set_xlim3d( left=-1*limAxis, right=limAxis)
ax.axes.set_ylim3d(bottom=-1*limAxis, top=limAxis)
ax.axes.set_zlim3d(bottom=-1*limAxis, top=limAxis)
fig.canvas.draw_idle()
def reset(event):
sA.reset()
sB2.reset()
sB3.reset()
sB4.reset()
def mpl_render(event):
A = sA.val
beta2 = sB2.val
beta3 = sB3.val
beta4 = sB4.val
m2 = sM2.val
m3 = sM3.val
m4 = sM4.val
r = physics_utils.calculate_r(A, beta2, m2, beta3, m3, beta4, m4, theta)
x = physics_utils._x(r, theta, phi)
y = physics_utils._y(r, theta, phi)
z = physics_utils._z(r, theta)
R = physics_utils._R(x, y, z)
N = R/R.max()
ax = plt.figure(figsize=(12,12)).add_subplot(projection='3d')
# fig, ax = plt.subplots(subplot_kw=dict(projection='3d'), figsize=(6,5))
plt.subplots_adjust(bottom=0.0, top=0.99)
im = ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=cm.jet(N), shade=True)
ax.axes.set_xlim3d( left=-1*limAxis, right=limAxis)
ax.axes.set_ylim3d(bottom=-1*limAxis, top=limAxis)
ax.axes.set_zlim3d(bottom=-1*limAxis, top=limAxis)
plt.tight_layout()
plt.show()
# fig.canvas.draw_idle()
def vtk_export(event):
A = sA.val
beta2 = sB2.val
beta3 = sB3.val
beta4 = sB4.val
m2 = sM2.val
m3 = sM3.val
m4 = sM4.val
# ax.clear()
initial_values=dict()
initial_values.update({'A':A, 'b2':beta2, 'b3':beta3, 'b4':beta4, 'm2':m2, 'm3':m3, 'm4':m4})
r = physics_utils.calculate_r(A, beta2, m2, beta3, m3, beta4, m4, theta)
nuclear_shape = vtk_utils.add_spherical_function(r, add_gridlines=False)
actor_dict = dict()
actor_dict.update({'shape': nuclear_shape})
render_window = vtk_utils.render(actors=actor_dict, initial_values=initial_values)
vtk_utils.write_gltf(render_window)
# Update plot when slider is changed
sA.on_changed(update)
sB2.on_changed(update)
sB3.on_changed(update)
sB4.on_changed(update)
sM2.on_changed(update)
sM3.on_changed(update)
sM4.on_changed(update)
reset_button.on_clicked(reset)
render_button.on_clicked(mpl_render)
export_button.on_clicked(vtk_export)
plt.show()