source: inundation/ga/storm_surge/pyvolution/realtime_visualisation_new.py @ 1697

Last change on this file since 1697 was 1697, checked in by steve, 20 years ago

Found problem with File_Boundary as used in validation test LWRU2. Have setup new BC Transmissive_Momentum_Set _Stage

  • Property svn:executable set to *
File size: 19.4 KB
Line 
1
2from visual import *
3
4elevation_color = (0.3,0.4,0.5)
5stage_color     = (0.1,0.4,0.99)
6friction_color  = (0.1,0.99,0.1)
7other_color     = (0.99,0.4,0.1)
8
9
10class Visualiser:
11
12    def __init__(self,domain,scale_z=1.0,rect=None,title='Test'):
13        """Create visualisation of domain
14        """
15
16        self.scene = display(title=title)
17        self.scene.center = (0.5,0.5,0.0)
18        self.scene.forward = vector(-0.0, 0.5, -0.8)
19        self.scene.ambient = 0.4
20        self.scene.lights = [(0.6, 0.3, 0.2), (0.1, -0.5, 0.4)]
21
22        self.frame  = frame()
23
24        self.domain   = domain
25        self.scale_z  = scale_z
26        self.vertices = domain.vertex_coordinates
27
28
29        # models for each quantity
30        self.vpython_z_models = {}
31        #print keys
32        for key in self.domain.quantities:
33            self.vpython_z_models[key] = faces(frame=self.frame)
34
35        #print self.z_models
36
37        if rect is None:
38            self.max_x = max(max(self.vertices[:,0]),max(self.vertices[:,2]),max(self.vertices[:,4]))
39            self.min_x = min(min(self.vertices[:,0]),min(self.vertices[:,2]),min(self.vertices[:,4]))
40            self.max_y = max(max(self.vertices[:,1]),max(self.vertices[:,3]),max(self.vertices[:,5]))
41            self.min_y = min(min(self.vertices[:,1]),min(self.vertices[:,3]),min(self.vertices[:,5]))
42        else:
43            self.max_x = rect[2]
44            self.min_x = rect[0]
45            self.max_y = rect[3]
46            self.min_y = rect[1]
47
48
49        self.range_x = self.max_x - self.min_x
50        self.range_y = self.max_y - self.min_y
51        self.range_xy = max(self.range_x, self.range_y)
52
53
54#        print 'min_x=',self.min_x
55#        print 'max_x=',self.max_x
56#        print 'range_x=',self.range_x
57#        print 'min_y=',self.min_y
58#        print 'max_y=',self.max_y
59#        print 'range_y',self.range_y
60
61        self.stage_color     = stage_color
62        self.elevation_color = elevation_color
63        self.friction_color  = friction_color
64        self.other_color     = other_color
65
66        self.qcolor = {}
67        self.scale_z = {}
68        self.coloring = {}
69        self.updating = {}
70        self.setup = {}
71
72        self.qcolor['stage']  = self.stage_color
73        self.scale_z['stage']  = 1.0
74        self.coloring['stage'] = False
75        self.updating['stage'] = True
76        self.setup['stage'] = True
77
78        self.qcolor['elevation']  = self.elevation_color
79        self.scale_z['elevation']  = 1.0
80        self.coloring['elevation'] = False
81        self.updating['elevation'] = False
82        self.setup['elevation'] = False
83
84        #print self.max_x, self.min_x, self.max_y, self.min_y,
85        #self.min_bed print self.max_bed, self.range_x, self.range_y,
86        #self.range_xy, self.range_bed
87        #print 'Calculating triangles'
88
89        self.pos     = zeros( (6*len(domain),3), Float)
90        self.colour  = zeros( (6*len(domain),3), Float)
91        self.normals = zeros( (6*len(domain),3), Float)
92
93        #print 'keys',self.domain.quantities.keys()
94        #print 'shape of stage',shape(self.stage)
95
96        self.border_model = curve(frame = self.frame,
97                                  pos=[(0,0),(0,1),(1,1),(1,0),(0,0)])
98        self.timer=label(pos=(0.75,0.75,0.5),text='Time=%10.5e'%self.domain.time,
99                         visible=False)
100
101
102        scene.autoscale=0
103        #self.update_all()
104        #scene.uniform=0
105        self.range_z  = 1.0
106        if domain.visualise_range_z == None:
107            self.setup_range_z()
108        else:
109            self.range_z = domain.visualise_range_z
110
111        self.max_z = self.range_z/2.0
112        self.min_z = -self.range_z/2.0
113        self.range_z = max(self.max_z - self.min_z, 1.0e-10)
114
115#        print self.range_z
116#        print self.max_z
117#        print self.min_z
118
119    def setup_range_z(self,qname1='elevation',qname2='stage'):
120
121        #print qname1
122        #print qname2
123        range_z = 1.0e-10
124        try:
125            q1 = self.domain.quantities[qname1].vertex_values
126            max_z = max(max(q1))
127            min_z = min(min(q1))
128            print max_z, min_z
129            range_z = max(range_z, max_z - min_z)
130        except:
131            #print 'Visualisation: could not find range of '+qname1
132            pass
133
134        try:
135            q2 = self.domain.quantities[qname2].vertex_values
136            max_z = max(max(q2))
137            min_z = min(min(q2))
138            print max_z, min_z
139            range_z = max(range_z, max_z - min_z)
140        except:
141            #print 'Visualisation: could not find range of '+qname2
142            pass
143
144        self.range_z = max(range_z,self.range_z)
145
146
147    def setup_all(self):
148
149        self.scene.select()
150
151        for key in ['elevation','stage']:
152            if self.setup[key] | self.updating[key]:
153                self.update_quantity(key)
154
155
156
157    def update_all(self):
158
159        self.scene.select()
160
161        for key in ['elevation','stage']:
162            if self.updating[key]:
163                self.update_quantity(key)
164
165
166    def update_timer(self):
167
168        self.scene.select()
169
170        if self.domain.visualise_timer == True:
171            self.timer.visible = True
172            self.timer.text='Time=%10.5e'%self.domain.time
173
174
175    def update_quantity(self,qname):
176
177        #print 'update '+qname+' arrays'
178
179        qcolor   = self.qcolor[qname]
180        scale_z  = self.scale_z[qname]
181        coloring = self.coloring[qname]
182        updating = self.updating[qname]
183
184
185        if qcolor is None:
186            qcolor = self.other_color
187
188            if qname=='elevation':
189                qcolor = self.elevation_color
190
191            if qname=='stage':
192                qcolor = self.stage_color
193
194            if qname=='friction':
195                qcolor = self.friction_color
196
197        if scale_z is None:
198            scale_z = self.scale_z
199
200        #try:
201        if coloring:
202            self.update_arrays_color(self.domain.quantities[qname].vertex_values, qcolor, scale_z)
203        else:
204            self.update_arrays(self.domain.quantities[qname].vertex_values, qcolor, scale_z)
205
206        #print 'update bed image'
207        if qname=='elevation':
208            self.pos[:,2] = self.pos[:,2]+1.0e-3
209
210        self.vpython_z_models[qname].pos    = self.pos
211        self.vpython_z_models[qname].color  = self.colour
212        self.vpython_z_models[qname].normal = self.normals
213        #except:
214        #    print 'Visualisation: Could not update quantity '+qname
215        #    pass
216
217    def update_quantity_color(self,qname,qcolor=None,scale_z=None):
218
219        #print 'update '+qname+' arrays'
220
221        if qcolor is None:
222            if qname=='elevation':
223                qcolor = self.elevation_color
224
225            if qname=='stage':
226                qcolor = self.stage_color
227
228            if qname=='friction':
229                qcolor = self.friction_color
230
231        if scale_z is None:
232            scale_z = self.scale_z
233
234        #try:
235        self.update_arrays_color(self.domain.quantities[qname].vertex_values, qcolor, scale_z)
236
237            #print 'update bed image'
238        self.vpython_z_models[qname].pos    = self.pos
239        self.vpython_z_models[qname].color  = self.colour
240        self.vpython_z_models[qname].normal = self.normals
241        #except:
242        #    print 'Visualisation: Could not update quantity '+qname
243        #    pass
244
245
246    def update_arrays(self,quantity,qcolor,scale_z):
247
248        col = asarray(qcolor)
249
250        N = len(self.domain)
251
252        min_x = self.min_x
253        min_y = self.min_y
254        range_xy = self.range_xy
255        range_z = self.range_z
256
257        vertices = self.vertices
258        pos      = self.pos
259        normals  = self.normals
260        colour   = self.colour
261
262        try:
263            update_arrays_weave(vertices,quantity,col,pos,normals,colour,N,
264                                min_x,min_y,range_xy,range_z,scale_z)
265        except:
266            update_arrays_python(vertices,quantity,col,pos,normals,colour,N,
267                                 min_x,min_y,range_xy,range_z,scale_z)
268
269
270
271    def update_arrays_color(self,quantity,qcolor,scale_z):
272
273        col = asarray(qcolor)
274
275        N = len(self.domain)
276
277        min_x = self.min_x
278        min_y = self.min_y
279        range_xy = self.range_xy
280        range_z = self.range_z
281
282        vertices = self.vertices
283        pos      = self.pos
284        normals  = self.normals
285        colour   = self.colour
286
287        #try:
288        update_arrays_color_python(vertices,quantity,col,pos,normals,colour,N,
289                                min_x,min_y,range_xy,range_z,scale_z)
290        #except:
291        #    update_arrays_color_python(vertices,quantity,col,pos,normals,colour,N,
292        #                         min_x,min_y,range_xy,range_z,scale_z)
293
294
295#==================================================================================
296
297def  update_arrays_python(vertices,quantity,col,pos,normals,colour,N,
298                          min_x,min_y,range_xy,range_z,scale_z):
299
300    from math import sqrt
301    normal = zeros( 3, Float)
302    v = zeros( (3,3), Float)
303    s = 1.0
304
305    for i in range( N ):
306
307        for j in range(3):
308            v[j,0] = (vertices[i,2*]-min_x)/range_xy
309            v[j,1] = (vertices[i,2*j+1]-min_y)/range_xy
310            v[j,2] = quantity[i,j]/range_z*scale_z*0.5
311
312        v10 = v[1,:]-v[0,:]
313        v20 = v[2,:]-v[0,:]
314
315        normal[0] = v10[1]*v20[2] - v20[1]*v10[2]
316        normal[1] = v10[2]*v20[0] - v20[2]*v10[0]
317        normal[2] = v10[0]*v20[1] - v20[0]*v10[1]
318
319        norm = sqrt( normal[0]**2 + normal[1]**2 + normal[2]**2)
320
321        normal[0] = normal[0]/norm
322        normal[1] = normal[1]/norm
323        normal[2] = normal[2]/norm
324
325        pos[6*,:] = v[0,:]
326        pos[6*i+1,:] = v[1,:]
327        pos[6*i+2,:] = v[2,:]
328        pos[6*i+3,:] = v[0,:]
329        pos[6*i+4,:] = v[2,:]
330        pos[6*i+5,:] = v[1,:]
331
332
333
334        colour[6*,:] = s*col
335        colour[6*i+1,:] = s*col
336        colour[6*i+2,:] = s*col
337        colour[6*i+3,:] = s*col
338        colour[6*i+4,:] = s*col
339        colour[6*i+5,:] = s*col
340
341        s =  15.0/8.0 - s
342
343        normals[6*,:] = normal
344        normals[6*i+1,:] = normal
345        normals[6*i+2,:] = normal
346        normals[6*i+3,:] = -normal
347        normals[6*i+4,:] = -normal
348        normals[6*i+5,:] = -normal
349
350
351
352def  update_arrays_weave(vertices,quantity,col,pos,normals,colour,
353                         N,min_x,min_y,range_xy,range_z,scale_z):
354
355    import weave
356    from weave import converters
357
358    from math import sqrt
359    normal = zeros( 3, Float)
360    v = zeros( (3,3), Float)
361    v10 = zeros( 3, Float)
362    v20 = zeros( 3, Float)
363
364    code1 = """
365
366        double s = 1.0;
367
368        for (int i=0; i<N ; i++ ) {
369            for (int j=0; j<3 ; j++) {
370                v(j,0) = (vertices(i,2*j  )-min_x)/range_xy;
371                v(j,1) = (vertices(i,2*j+1)-min_y)/range_xy;
372                v(j,2) = quantity(i,j)/range_z*scale_z*0.5;
373            }
374
375
376            for (int j=0; j<3; j++) {
377                v10(j) = v(1,j)-v(0,j);
378                v20(j) = v(2,j)-v(0,j);
379            }
380
381            normal(0) = v10(1)*v20(2) - v20(1)*v10(2);
382            normal(1) = v10(2)*v20(0) - v20(2)*v10(0);
383            normal(2) = v10(0)*v20(1) - v20(0)*v10(1);
384
385            double norm =  sqrt(normal(0)*normal(0) + normal(1)*normal(1) + normal(2)*normal(2));
386
387            normal(0) = normal(0)/norm;
388            normal(1) = normal(1)/norm;
389            normal(2) = normal(2)/norm;
390
391
392            for (int j=0; j<3; j++) {
393                pos(6*i  ,j) = v(0,j);
394                pos(6*i+1,j) = v(1,j);
395                pos(6*i+2,j) = v(2,j);
396                pos(6*i+3,j) = v(0,j);
397                pos(6*i+4,j) = v(2,j);
398                pos(6*i+5,j) = v(1,j);
399
400                colour(6*i  ,j) = s*col(j);
401                colour(6*i+1,j) = s*col(j);
402                colour(6*i+2,j) = s*col(j);
403                colour(6*i+3,j) = s*col(j);
404                colour(6*i+4,j) = s*col(j);
405                colour(6*i+5,j) = s*col(j);
406
407                normals(6*i  ,j) = normal(j);
408                normals(6*i+1,j) = normal(j);
409                normals(6*i+2,j) = normal(j);
410                normals(6*i+3,j) = -normal(j);
411                normals(6*i+4,j) = -normal(j);
412                normals(6*i+5,j) = -normal(j);
413                }
414
415            s =  15.0/8.0 - s;
416        }
417
418        """
419
420    weave.inline(code1, ['vertices','quantity','col','pos','normals','colour','N',
421                         'min_x','min_y','range_xy','range_z','scale_z','v','normal','v10','v20'],
422                 type_converters = converters.blitz, compiler='gcc');
423
424def  update_arrays_color_python(vertices,quantity,col,pos,normals,colour,N,
425                          min_x,min_y,range_xy,range_z,scale_z):
426
427    from math import sqrt
428    normal = zeros( 3, Float)
429    v = zeros( (3,3), Float)
430    s = 1.0
431
432    for i in range( N ):
433
434        for j in range(3):
435            v[j,0] = (vertices[i,2*]-min_x)/range_xy
436            v[j,1] = (vertices[i,2*j+1]-min_y)/range_xy
437            v[j,2] = quantity[i,j]/range_z*scale_z*0.5
438
439        v10 = v[1,:]-v[0,:]
440        v20 = v[2,:]-v[0,:]
441
442        normal[0] = v10[1]*v20[2] - v20[1]*v10[2]
443        normal[1] = v10[2]*v20[0] - v20[2]*v10[0]
444        normal[2] = v10[0]*v20[1] - v20[0]*v10[1]
445
446        norm = sqrt( normal[0]**2 + normal[1]**2 + normal[2]**2)
447
448        normal[0] = normal[0]/norm
449        normal[1] = normal[1]/norm
450        normal[2] = normal[2]/norm
451
452        pos[6*,:] = v[0,:]
453        pos[6*i+1,:] = v[1,:]
454        pos[6*i+2,:] = v[2,:]
455        pos[6*i+3,:] = v[0,:]
456        pos[6*i+4,:] = v[2,:]
457        pos[6*i+5,:] = v[1,:]
458
459        q0 = quantity[i,0]/0.4
460        q1 = quantity[i,1]/0.4
461        q2 = quantity[i,2]/0.4
462
463        q0r = min(q0,0.0)
464        q1r = min(q1,0.0)
465        q2r = min(q2,0.0)
466        q0b = max(q0,0.0)
467        q1b = max(q1,0.0)
468        q2b = max(q2,0.0)
469
470
471        colour[6*,:] = s*array([0.3 - q0r, 0.3 - q0, 0.3 + q0b])
472        colour[6*i+1,:] = s*array([0.3 - q1r, 0.3 - q1, 0.3 + q1b])
473        colour[6*i+2,:] = s*array([0.3 - q2r, 0.3 - q2, 0.3 + q2b])
474        colour[6*i+3,:] = s*array([0.3 - q0r, 0.3 - q0, 0.3 + q0b])
475        colour[6*i+4,:] = s*array([0.3 - q2r, 0.3 - q2, 0.3 + q2b])
476        colour[6*i+5,:] = s*array([0.3 - q1r, 0.3 - q1, 0.3 + q1b])
477
478        s =  15.0/8.0 - s
479
480        normals[6*,:] = normal
481        normals[6*i+1,:] = normal
482        normals[6*i+2,:] = normal
483        normals[6*i+3,:] = -normal
484        normals[6*i+4,:] = -normal
485        normals[6*i+5,:] = -normal
486
487
488
489def  update_arrays_color_weave(vertices,quantity,col,pos,normals,colour,
490                         N,min_x,min_y,range_xy,range_z,scale_z):
491
492    import weave
493    from weave import converters
494
495    from math import sqrt
496    normal = zeros( 3, Float)
497    v = zeros( (3,3), Float)
498    v10 = zeros( 3, Float)
499    v20 = zeros( 3, Float)
500
501    code1 = """
502        double s = 1.0;
503
504        for (int i=0; i<N ; i++ ) {
505            for (int j=0; j<3 ; j++) {
506                v(j,0) = (vertices(i,2*j  )-min_x)/range_xy;
507                v(j,1) = (vertices(i,2*j+1)-min_y)/range_xy;
508                v(j,2) = quantity(i,j)/range_z*scale_z*0.5;
509            }
510
511
512            for (int j=0; j<3; j++) {
513                v10(j) = v(1,j)-v(0,j);
514                v20(j) = v(2,j)-v(0,j);
515            }
516
517            normal(0) = v10(1)*v20(2) - v20(1)*v10(2);
518            normal(1) = v10(2)*v20(0) - v20(2)*v10(0);
519            normal(2) = v10(0)*v20(1) - v20(0)*v10(1);
520
521            double norm =  sqrt(normal(0)*normal(0) + normal(1)*normal(1) + normal(2)*normal(2));
522
523            normal(0) = normal(0)/norm;
524            normal(1) = normal(1)/norm;
525            normal(2) = normal(2)/norm;
526
527
528
529            for (int j=0; j<3; j++) {
530                pos(6*i  ,j) = v(0,j);
531                pos(6*i+1,j) = v(1,j);
532                pos(6*i+2,j) = v(2,j);
533                pos(6*i+3,j) = v(0,j);
534                pos(6*i+4,j) = v(2,j);
535                pos(6*i+5,j) = v(1,j);
536
537                normals(6*i  ,j) = normal(j);
538                normals(6*i+1,j) = normal(j);
539                normals(6*i+2,j) = normal(j);
540                normals(6*i+3,j) = -normal(j);
541                normals(6*i+4,j) = -normal(j);
542                normals(6*i+5,j) = -normal(j);
543                }
544
545            s = 0.2+fabs((v(0,2)+v(1,2)+v(2,2))/3.0);
546            for (int j=0; j<3; j++) {
547                colour(6*i  ,j) = s*col(j);
548                colour(6*i+1,j) = s*col(j);
549                colour(6*i+2,j) = s*col(j);
550                colour(6*i+3,j) = s*col(j);
551                colour(6*i+4,j) = s*col(j);
552                colour(6*i+5,j) = s*col(j);
553                }
554            s =  15.0/8.0 - s;
555        }
556
557        """
558
559    weave.inline(code1, ['vertices','quantity','col','pos','normals','colour','N',
560                         'min_x','min_y','range_xy','range_z','scale_z','v','normal','v10','v20'],
561                 type_converters = converters.blitz, compiler='gcc');
562
563
564def setup_scene():
565
566    #scene.width = 1000
567    #scene.height = 800
568
569    #Original
570    scene.center = (0.5,0.5,0.0)
571    scene.forward = vector(-0.0, 0.5, -0.8)
572
573    #Temporary (for bedslope)
574    #scene.forward = vector(0.0006, 0.7, -0.03)
575
576
577    #Temporary for hackett - begin
578    #scene.autoscale = 0
579    #scene.scale = (0.002, 0.002, 0.01) #Scale z so that countours stand out more
580    #scene.center = (300.0,500.0,-10)
581    #Temporary for hackett - end
582
583
584    scene.ambient = 0.4
585    #scene.lights = [(0.6, 0.3, 0.2), (0.1, -0.5, 0.4), (-0.1, 0.1, -0.4),
586    #               (-0.2, 0.2, 0.1)]
587
588    scene.lights = [(0.6, 0.3, 0.2), (0.1, -0.5, 0.4)]
589
590
591
592def create_surface(domain,scale_z=1.0):
593
594    domain.initialise_visualiser(scale_z)
595
596    domain.visualiser.update_quantity('elevation')
597    domain.visualiser.update_quantity('stage')
598    domain.visualiser.update_timer()
599
600def update(domain):
601
602    if domain.visualise_color_stage:
603        domain.visualiser.update_quantity_color('stage')
604    else:
605        domain.visualiser.update_quantity('stage')
606
607    domain.visualiser.update_timer()
608
609
610
611if __name__ == '__main__':
612
613    from advection import Domain as A_Domain
614    from shallow_water import Domain as SW_Domain
615    from shallow_water import Constant_height, Constant_stage
616    from mesh_factory import rectangular
617
618    points, vertices, boundary = rectangular(60, 60, len1=200, len2=200)
619    a_domain  = A_Domain(points, vertices, boundary)
620    print 'No of Elements' , a_domain.number_of_elements
621    #sw_domain = SW_Domain(points, vertices, boundary)
622    #sw_domain.set_quantity('elevation', Constant_stage(0.0))
623    #sw_domain.set_quantity('stage', Constant_stage(0.0))
624
625    a_domain.initialise_visualiser()
626    a_domain.visualiser.setup_all()
627
628
629    #sw_domain.initialise_visualiser()
630    #sw_domain.visualiser.setup['elevation']=True
631    #sw_domain.visualiser.setup_all()
632
633    time = 0.0
634    while (True):
635        time = time+0.001
636        #print 'Time = ',time
637        #sw_domain.set_quantity('stage', time)
638        a_domain.set_quantity('stage',time)
639
640        #sw_domain.visualiser.update_quantity('stage')
641        a_domain.visualiser.update_quantity('stage')
642
643        if time>1.0 :
644            break
645
646    #a_v = Visualiser(domain=a_domain, title='advection')
647    #a_v.update_all()
648
649
650    #sw_v = Visualiser(domain=sw_domain, title='shallow water')
651    #sw_v.update_all()
Note: See TracBrowser for help on using the repository browser.