1 | from visual import * |
---|
2 | |
---|
3 | class Surface: |
---|
4 | def __init__(self,domain,scale_z=1.0): |
---|
5 | """Create visualisation of domain |
---|
6 | """ |
---|
7 | |
---|
8 | self.frame = frame() |
---|
9 | autoscale=0 |
---|
10 | self.bed_model = faces(frame=self.frame) |
---|
11 | self.stage_model = faces(frame=self.frame) |
---|
12 | self.domain = domain |
---|
13 | self.scale_z = scale_z |
---|
14 | |
---|
15 | self.vertices = domain.vertex_coordinates |
---|
16 | self.bed = domain.quantities['elevation'].vertex_values |
---|
17 | self.stage = domain.quantities['stage'].vertex_values |
---|
18 | self.xmomentum = domain.quantities['xmomentum'].vertex_values |
---|
19 | self.ymomentum = domain.quantities['ymomentum'].vertex_values |
---|
20 | |
---|
21 | self.max_x = max(max(self.vertices[:,0],self.vertices[:,2],self.vertices[:,4])) |
---|
22 | self.min_x = min(min(self.vertices[:,0],self.vertices[:,2],self.vertices[:,4])) |
---|
23 | self.max_y = max(max(self.vertices[:,1],self.vertices[:,3],self.vertices[:,5])) |
---|
24 | self.min_y = min(min(self.vertices[:,1],self.vertices[:,3],self.vertices[:,5])) |
---|
25 | self.range_x = self.max_x - self.min_x |
---|
26 | self.range_y = self.max_y - self.min_y |
---|
27 | |
---|
28 | self.range_xy = max(self.range_x, self.range_y) |
---|
29 | |
---|
30 | self.max_bed = max(max(self.bed)) |
---|
31 | self.min_bed = min(min(self.bed)) |
---|
32 | self.range_bed = max(self.max_bed - self.min_bed, 1e-10)*2.0 |
---|
33 | |
---|
34 | print 'min_bed=',self.min_bed |
---|
35 | print 'max_bed=',self.max_bed |
---|
36 | print 'range_bed=',self.range_bed |
---|
37 | |
---|
38 | #print self.max_x, self.min_x, self.max_y, self.min_y, |
---|
39 | #self.min_bed print self.max_bed, self.range_x, self.range_y, |
---|
40 | #self.range_xy, self.range_bed |
---|
41 | #print 'Calculating triangles' |
---|
42 | |
---|
43 | self.pos = zeros( (6*len(domain),3), Float) |
---|
44 | self.colour = zeros( (6*len(domain),3), Float) |
---|
45 | self.normals = zeros( (6*len(domain),3), Float) |
---|
46 | |
---|
47 | #print 'keys',self.domain.quantities.keys() |
---|
48 | #print 'shape of stage',shape(self.stage) |
---|
49 | |
---|
50 | self.border_model = curve(frame = self.frame, pos=[(0,0),(0,1),(1,1),(1,0),(0,0)]) |
---|
51 | self.timer=label(pos=(0.75,0.5,0.5),text='Time=%10.5e'%self.domain.time) |
---|
52 | |
---|
53 | #self.update_all() |
---|
54 | |
---|
55 | def update_all(self): |
---|
56 | |
---|
57 | self.update_bed() |
---|
58 | self.update_stage() |
---|
59 | |
---|
60 | def update_timer(self): |
---|
61 | self.timer.text='Time=%10.5e'%self.domain.time |
---|
62 | |
---|
63 | def update_bed(self): |
---|
64 | |
---|
65 | #print 'update bed arrays' |
---|
66 | c0 = 0.3 |
---|
67 | c1 = 0.3 |
---|
68 | c2 = 0.3 |
---|
69 | self.update_arrays(self.bed, (c0,c1,c2) ) |
---|
70 | |
---|
71 | #print 'update bed image' |
---|
72 | self.bed_model.pos = self.pos |
---|
73 | self.bed_model.color = self.colour |
---|
74 | self.bed_model.normal = self.normals |
---|
75 | |
---|
76 | |
---|
77 | def update_stage(self): |
---|
78 | c0 = 0.1 |
---|
79 | c1 = 0.4 |
---|
80 | c2 = 0.99 |
---|
81 | #print 'update stage arrays' |
---|
82 | self.update_arrays(self.stage, (c0,c1,c2) ) |
---|
83 | #print 'update stage image' |
---|
84 | self.stage_model.pos = self.pos |
---|
85 | self.stage_model.color = self.colour |
---|
86 | self.stage_model.normal = self.normals |
---|
87 | |
---|
88 | def update_xmomentum(self): |
---|
89 | c0 = 0.1 |
---|
90 | c1 = 0.4 |
---|
91 | c2 = 0.99 |
---|
92 | #print 'update stage arrays' |
---|
93 | self.update_arrays(self.xmomentum, (c0,c1,c2) ) |
---|
94 | #print 'update stage image' |
---|
95 | self.stage_model.pos = self.pos |
---|
96 | self.stage_model.color = self.colour |
---|
97 | self.stage_model.normal = self.normals |
---|
98 | |
---|
99 | def update_ymomentum(self): |
---|
100 | c0 = 0.1 |
---|
101 | c1 = 0.4 |
---|
102 | c2 = 0.99 |
---|
103 | #print 'update stage arrays' |
---|
104 | self.update_arrays(self.ymomentum, (c0,c1,c2) ) |
---|
105 | #print 'update stage image' |
---|
106 | self.stage_model.pos = self.pos |
---|
107 | self.stage_model.color = self.colour |
---|
108 | self.stage_model.normal = self.normals |
---|
109 | |
---|
110 | def update_arrays(self,quantity,qcolor): |
---|
111 | |
---|
112 | col = asarray(qcolor) |
---|
113 | |
---|
114 | N = len(self.domain) |
---|
115 | |
---|
116 | scale_z = self.scale_z |
---|
117 | min_x = self.min_x |
---|
118 | min_y = self.min_y |
---|
119 | range_xy = self.range_xy |
---|
120 | range_bed = self.range_bed |
---|
121 | |
---|
122 | vertices = self.vertices |
---|
123 | pos = self.pos |
---|
124 | normals = self.normals |
---|
125 | colour = self.colour |
---|
126 | |
---|
127 | try: |
---|
128 | update_arrays_weave(vertices,quantity,col,pos,normals,colour,N, |
---|
129 | min_x,min_y,range_xy,range_bed,scale_z) |
---|
130 | except: |
---|
131 | update_arrays_python(vertices,quantity,col,pos,normals,colour,N, |
---|
132 | min_x,min_y,range_xy,range_bed,scale_z) |
---|
133 | |
---|
134 | |
---|
135 | |
---|
136 | def update_arrays_python(vertices,quantity,col,pos,normals,colour,N, |
---|
137 | min_x,min_y,range_xy,range_bed,scale_z): |
---|
138 | |
---|
139 | from math import sqrt |
---|
140 | normal = zeros( 3, Float) |
---|
141 | v = zeros( (3,3), Float) |
---|
142 | s = 1.0 |
---|
143 | |
---|
144 | for i in range( N ): |
---|
145 | |
---|
146 | for j in range(3): |
---|
147 | v[j,0] = (vertices[i,2*j ]-min_x)/range_xy |
---|
148 | v[j,1] = (vertices[i,2*j+1]-min_y)/range_xy |
---|
149 | v[j,2] = quantity[i,j]/range_bed*scale_z |
---|
150 | |
---|
151 | v10 = v[1,:]-v[0,:] |
---|
152 | v20 = v[2,:]-v[0,:] |
---|
153 | |
---|
154 | normal[0] = v10[1]*v20[2] - v20[1]*v10[2] |
---|
155 | normal[1] = v10[2]*v20[0] - v20[2]*v10[0] |
---|
156 | normal[2] = v10[0]*v20[1] - v20[0]*v10[1] |
---|
157 | |
---|
158 | norm = sqrt( normal[0]**2 + normal[1]**2 + normal[2]**2) |
---|
159 | |
---|
160 | normal[0] = normal[0]/norm |
---|
161 | normal[1] = normal[1]/norm |
---|
162 | normal[2] = normal[2]/norm |
---|
163 | |
---|
164 | pos[6*i ,:] = v[0,:] |
---|
165 | pos[6*i+1,:] = v[1,:] |
---|
166 | pos[6*i+2,:] = v[2,:] |
---|
167 | pos[6*i+3,:] = v[0,:] |
---|
168 | pos[6*i+4,:] = v[2,:] |
---|
169 | pos[6*i+5,:] = v[1,:] |
---|
170 | |
---|
171 | |
---|
172 | |
---|
173 | colour[6*i ,:] = s*col |
---|
174 | colour[6*i+1,:] = s*col |
---|
175 | colour[6*i+2,:] = s*col |
---|
176 | colour[6*i+3,:] = s*col |
---|
177 | colour[6*i+4,:] = s*col |
---|
178 | colour[6*i+5,:] = s*col |
---|
179 | |
---|
180 | s = 15.0/8.0 - s |
---|
181 | |
---|
182 | normals[6*i ,:] = normal |
---|
183 | normals[6*i+1,:] = normal |
---|
184 | normals[6*i+2,:] = normal |
---|
185 | normals[6*i+3,:] = -normal |
---|
186 | normals[6*i+4,:] = -normal |
---|
187 | normals[6*i+5,:] = -normal |
---|
188 | |
---|
189 | |
---|
190 | |
---|
191 | def update_arrays_weave(vertices,quantity,col,pos,normals,colour, |
---|
192 | N,min_x,min_y,range_xy,range_bed,scale_z): |
---|
193 | |
---|
194 | import weave |
---|
195 | from weave import converters |
---|
196 | |
---|
197 | from math import sqrt |
---|
198 | normal = zeros( 3, Float) |
---|
199 | v = zeros( (3,3), Float) |
---|
200 | v10 = zeros( 3, Float) |
---|
201 | v20 = zeros( 3, Float) |
---|
202 | |
---|
203 | code1 = """ |
---|
204 | double s = 1.0; |
---|
205 | |
---|
206 | for (int i=0; i<N ; i++ ) { |
---|
207 | for (int j=0; j<3 ; j++) { |
---|
208 | v(j,0) = (vertices(i,2*j )-min_x)/range_xy; |
---|
209 | v(j,1) = (vertices(i,2*j+1)-min_y)/range_xy; |
---|
210 | v(j,2) = quantity(i,j)/range_bed*scale_z; |
---|
211 | } |
---|
212 | |
---|
213 | |
---|
214 | for (int j=0; j<3; j++) { |
---|
215 | v10(j) = v(1,j)-v(0,j); |
---|
216 | v20(j) = v(2,j)-v(0,j); |
---|
217 | } |
---|
218 | |
---|
219 | normal(0) = v10(1)*v20(2) - v20(1)*v10(2); |
---|
220 | normal(1) = v10(2)*v20(0) - v20(2)*v10(0); |
---|
221 | normal(2) = v10(0)*v20(1) - v20(0)*v10(1); |
---|
222 | |
---|
223 | double norm = sqrt(normal(0)*normal(0) + normal(1)*normal(1) + normal(2)*normal(2)); |
---|
224 | |
---|
225 | normal(0) = normal(0)/norm; |
---|
226 | normal(1) = normal(1)/norm; |
---|
227 | normal(2) = normal(2)/norm; |
---|
228 | |
---|
229 | for (int j=0; j<3; j++) { |
---|
230 | pos(6*i ,j) = v(0,j); |
---|
231 | pos(6*i+1,j) = v(1,j); |
---|
232 | pos(6*i+2,j) = v(2,j); |
---|
233 | pos(6*i+3,j) = v(0,j); |
---|
234 | pos(6*i+4,j) = v(2,j); |
---|
235 | pos(6*i+5,j) = v(1,j); |
---|
236 | |
---|
237 | colour(6*i ,j) = s*col(j); |
---|
238 | colour(6*i+1,j) = s*col(j); |
---|
239 | colour(6*i+2,j) = s*col(j); |
---|
240 | colour(6*i+3,j) = s*col(j); |
---|
241 | colour(6*i+4,j) = s*col(j); |
---|
242 | colour(6*i+5,j) = s*col(j); |
---|
243 | |
---|
244 | normals(6*i ,j) = normal(j); |
---|
245 | normals(6*i+1,j) = normal(j); |
---|
246 | normals(6*i+2,j) = normal(j); |
---|
247 | normals(6*i+3,j) = -normal(j); |
---|
248 | normals(6*i+4,j) = -normal(j); |
---|
249 | normals(6*i+5,j) = -normal(j); |
---|
250 | } |
---|
251 | |
---|
252 | s = 15.0/8.0 - s; |
---|
253 | } |
---|
254 | |
---|
255 | """ |
---|
256 | |
---|
257 | weave.inline(code1, ['vertices','quantity','col','pos','normals','colour','N', |
---|
258 | 'min_x','min_y','range_xy','range_bed','scale_z','v','normal','v10','v20'], |
---|
259 | type_converters = converters.blitz, compiler='gcc'); |
---|
260 | |
---|
261 | |
---|
262 | scene.width = 1000 |
---|
263 | scene.height = 800 |
---|
264 | |
---|
265 | #Original |
---|
266 | scene.center = (0.5,0.5,0) |
---|
267 | scene.forward = vector(-0.1, 0.5, -0.5) |
---|
268 | |
---|
269 | #Temporary (for bedslope) |
---|
270 | #scene.forward = vector(0.0006, 0.7, -0.03) |
---|
271 | |
---|
272 | |
---|
273 | #Temporary for hackett - begin |
---|
274 | #scene.autoscale = 0 |
---|
275 | #scene.scale = (0.002, 0.002, 0.01) #Scale z so that countours stand out more |
---|
276 | #scene.center = (300.0,500.0,-10) |
---|
277 | #Temporary for hackett - end |
---|
278 | |
---|
279 | |
---|
280 | scene.ambient = 0.4 |
---|
281 | #scene.lights = [(0.6, 0.3, 0.2), (0.1, -0.5, 0.4), (-0.1, 0.1, -0.4), |
---|
282 | # (-0.2, 0.2, 0.1)] |
---|
283 | |
---|
284 | scene.lights = [(0.6, 0.3, 0.2), (0.1, -0.5, 0.4)] |
---|
285 | |
---|
286 | |
---|
287 | |
---|
288 | def create_surface(domain): |
---|
289 | |
---|
290 | surface = Surface(domain) |
---|
291 | domain.surface = surface |
---|
292 | |
---|
293 | surface.update_bed() |
---|
294 | surface.update_stage() |
---|
295 | #surface.update_xmomentum() |
---|
296 | #surface.update_ymomentum |
---|
297 | surface.update_timer() |
---|
298 | |
---|
299 | def update(domain): |
---|
300 | |
---|
301 | #print 'start visual update' |
---|
302 | surface = domain.surface |
---|
303 | surface.update_stage() |
---|
304 | #surface.update_xmomentum() |
---|
305 | #surface.update_ymomentum() |
---|
306 | surface.update_timer() |
---|
307 | #print 'end visual update' |
---|