forked from treverhines/tplot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xsection.py
314 lines (257 loc) · 8.51 KB
/
xsection.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env python
import numpy as np
import mayavi.mlab as mlab
import transform
import tplot.cm
from traits.api import HasTraits, Range, Instance, on_trait_change
from traitsui.api import View, Item, Group
from mayavi.core.api import PipelineBase
from mayavi.core.ui.api import MayaviScene, SceneEditor,MlabSceneModel
def cmap_to_rgba(cmap):
x = np.linspace(0,1,256)
rgba = np.array([cmap(i) for i in x])
rgba *= 255
rgba = np.array(rgba,dtype=int)
return rgba
class VectorXSection:
def __init__(self,f,
f_args=None,
f_kwargs=None,
Nl=10,Nw=10,
base_square_x=(0,1),
base_square_y=(0,1),
transforms = ()):
if f_args is None:
f_args = ()
if f_kwargs is None:
f_kwargs = {}
self._f = f
self._f_args = f_args
self._f_kwargs = f_kwargs
self._Nl = Nl
self._Nw = Nw
self._base_square_x = base_square_x
self._base_square_y = base_square_y
self._transforms = transforms
self._plots = None
def set_f(self,f):
self._f = f
def set_f_kwargs(self,f_kwargs):
self._f_kwargs = f_kwargs
def set_f_args(self,f_args):
self._f_args = f_args
def set_base_square_x(self,a):
self._base_square_x = a
def set_base_square_y(self,a):
self._base_square_y = a
def add_transform(self,t):
self._transforms += t,
def draw(self):
x = np.linspace(self._base_square_x[0],
self._base_square_x[1],self._Nl)
y = np.linspace(self._base_square_y[0],
self._base_square_y[1],self._Nw)
x,y = np.meshgrid(x,y)
z = 0.0*x
for trans in self._transforms:
p = np.concatenate((x[:,:,None],
y[:,:,None],
z[:,:,None]),
axis=-1)
p = trans(p)
pflat = np.reshape(p,(-1,3))
x,y,z = pflat[:,0],pflat[:,1],pflat[:,2]
value = self._f(pflat,*self._f_args,**self._f_kwargs)
u,v,w = value[:,0],value[:,1],value[:,2]
m = mlab.quiver3d(x,y,z,u,v,w,mode='arrow',color=(0.4,0.4,0.4))
mlab.draw()
if self._plots is None:
self._plots = (m,trans),
else:
self._plots += (m,trans),
def redraw(self):
x = np.linspace(self._base_square_x[0],
self._base_square_x[1],self._Nl)
y = np.linspace(self._base_square_y[0],
self._base_square_y[1],self._Nw)
x,y = np.meshgrid(x,y)
z = 0.0*x
for plot,trans in self._plots:
p = np.concatenate((x[:,:,None],
y[:,:,None],
z[:,:,None]),
axis=-1)
p = trans(p)
pflat = np.reshape(p,(-1,3))
x,y,z = pflat[:,0],pflat[:,1],pflat[:,2]
value = self._f(pflat,*self._f_args,**self._f_kwargs)
u,v,w = value[:,0],value[:,1],value[:,2]
plot.mlab_source.set(x=x,y=y,z=z,
u=u,v=v,w=w)
if self._plots is None:
self._plots += (m,trans),
def view(self):
mlab.show()
class XSection:
def __init__(self,f,
f_args=None,
f_kwargs=None,
Nl=200,Nw=200,
base_square_x=(0,1),
base_square_y=(0,1),
transforms = (),
clim=None,
cmap=tplot.cm.slip):
if f_args is None:
f_args = ()
if f_kwargs is None:
f_kwargs = {}
self._f = f
self._f_args = f_args
self._f_kwargs = f_kwargs
self._Nl = Nl
self._Nw = Nw
self._clim = clim
self._rgba = cmap_to_rgba(cmap)
self._base_square_x = base_square_x
self._base_square_y = base_square_y
self._transforms = transforms
self._plots = None
def set_f(self,f):
self._f = f
def set_f_kwargs(self,f_kwargs):
self._f_kwargs = f_kwargs
def set_f_args(self,f_args):
self._f_args = f_args
def set_base_square_x(self,a):
self._base_square_x = a
def set_base_square_y(self,a):
self._base_square_y = a
def add_transform(self,t):
self._transforms += t,
def draw(self,**kwargs):
x = np.linspace(self._base_square_x[0],
self._base_square_x[1],self._Nl)
y = np.linspace(self._base_square_y[0],
self._base_square_y[1],self._Nw)
x,y = np.meshgrid(x,y)
z = 0.0*x
for trans in self._transforms:
p = np.concatenate((x[:,:,None],
y[:,:,None],
z[:,:,None]),
axis=-1)
p = trans(p)
pflat = np.reshape(p,(-1,3))
c = self._f(pflat,*self._f_args,**self._f_kwargs)
c = np.reshape(c,np.shape(p)[:-1])
if self._clim is None:
self._clim = (np.min(c),np.max(c))
m = mlab.mesh(p[:,:,0],p[:,:,1],p[:,:,2],
scalars=c,
vmin=self._clim[0],
vmax=self._clim[1],**kwargs)
m.module_manager.scalar_lut_manager.lut.table = self._rgba
mlab.colorbar()
mlab.draw()
if self._plots is None:
self._plots = (m,trans),
else:
self._plots += (m,trans),
def redraw(self):
x = np.linspace(self._base_square_x[0],
self._base_square_x[1],self._Nl)
y = np.linspace(self._base_square_y[0],
self._base_square_y[1],self._Nw)
x,y = np.meshgrid(x,y)
z = 0.0*x
for plot,trans in self._plots:
p = np.concatenate((x[:,:,None],
y[:,:,None],
z[:,:,None]),
axis=-1)
p = trans(p)
pflat = np.reshape(p,(-1,3))
c = self._f(pflat,*self._f_args,**self._f_kwargs)
c = np.reshape(c,np.shape(p)[:-1])
plot.mlab_source.set(x=p[:,:,0],
y=p[:,:,1],
z=p[:,:,2],
scalars=c)
if self._plots is None:
self._plots += (m,trans),
def view(self):
mlab.show()
if __name__ == '__main__':
import numpy as np
from numpy import arange, pi, cos, sin
from traits.api import HasTraits, Range, Instance, \
on_trait_change, Int
from traitsui.api import View, Item, Group
from mayavi.core.api import PipelineBase
from mayavi.core.ui.api import MayaviScene, SceneEditor, \
MlabSceneModel
import transform
x = np.random.random((10))
y = np.random.random((10))
z = np.random.random((10))
def foo(x):
return x
u = np.random.random((10))
v = np.random.random((10))
w = np.random.random((10))
mlab.quiver3d(x,y,z,u,v,w,mode='arrow')
mlab.show()
dphi = pi/1000.
phi = arange(0.0, 2*pi + 0.5*dphi, dphi, 'd')
def f(p,farg1,farg2):
x = p[:,0]
y = p[:,1]
z = p[:,2]
t = np.sqrt(farg1*x**2 + farg2*y**2)
return t
t = transform.identity()
print('start1')
m = VectorXSection(foo,transforms=[t])
print('end1')
m.draw()
m.view()
class InteractiveXSection(HasTraits):
# define range of f_arg values
farg1 = Range(0, 30, .6)
farg2 = Range(0, 30, .11)
scene = Instance(MlabSceneModel, ())
view = View(Item('scene', editor=SceneEditor(scene_class=MayaviScene),
height=250, width=300, show_label=False),
Group('farg1', 'farg2'),
resizable=True)
def __init__(self):
# initiate cross section instance
self.xsection = XSection(f,
Nl = 20,
Nw = 20,clim=(0,0.1),
f_args=(self.farg1,self.farg2))
# add cross sections
T = transform.identity()
T += transform.point_stretch([2.0,2.0,1.0])
T += transform.point_translation([-1.0,0.0,0.0])
self.xsection.add_transform(T)
T += transform.point_rotation_x(np.pi/2.0)
self.xsection.add_transform(T)
T += transform.point_rotation_x(np.pi/2.0)
self.xsection.add_transform(T)
T += transform.point_rotation_x(np.pi/2.0)
self.xsection.add_transform(T)
# run HasTraits initiation routine
HasTraits.__init__(self)
# When the scene is activated, or when the parameters are changed, we
# update the plot.
@on_trait_change('farg1,farg2,scene.activated')
def update_plot(self):
self.xsection.set_f_args((self.farg1,self.farg2))
if self.xsection._plots is None:
self.xsection.draw()
else:
self.xsection.redraw()
my_model = InteractiveXSection()
my_model.configure_traits()