[1639] | 1 | |
---|
[1265] | 2 | from visual import * |
---|
| 3 | |
---|
[1290] | 4 | elevation_color = (0.3,0.4,0.5) |
---|
[1471] | 5 | stage_color = (0.1,0.4,0.99) |
---|
| 6 | friction_color = (0.1,0.99,0.1) |
---|
| 7 | other_color = (0.99,0.4,0.1) |
---|
[1462] | 8 | |
---|
[1290] | 9 | |
---|
[1363] | 10 | class Visualiser: |
---|
[1407] | 11 | |
---|
[1639] | 12 | def __init__(self,domain,scale_z=1.0,rect=None,title='Test'): |
---|
[1265] | 13 | """Create visualisation of domain |
---|
| 14 | """ |
---|
| 15 | |
---|
[1639] | 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 | |
---|
[1265] | 22 | self.frame = frame() |
---|
[1280] | 23 | |
---|
[1639] | 24 | self.domain = domain |
---|
[1266] | 25 | self.scale_z = scale_z |
---|
[1265] | 26 | self.vertices = domain.vertex_coordinates |
---|
[1280] | 27 | |
---|
[1363] | 28 | |
---|
[1290] | 29 | # models for each quantity |
---|
[1565] | 30 | self.vpython_z_models = {} |
---|
[1414] | 31 | #print keys |
---|
[1565] | 32 | for key in self.domain.quantities: |
---|
[1639] | 33 | self.vpython_z_models[key] = faces(frame=self.frame) |
---|
[1265] | 34 | |
---|
[1414] | 35 | #print self.z_models |
---|
[1280] | 36 | |
---|
[1510] | 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] |
---|
[1265] | 47 | |
---|
[1510] | 48 | |
---|
[1639] | 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 | |
---|
[1290] | 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 |
---|
[1265] | 60 | |
---|
[1599] | 61 | self.stage_color = stage_color |
---|
| 62 | self.elevation_color = elevation_color |
---|
| 63 | self.friction_color = friction_color |
---|
| 64 | self.other_color = other_color |
---|
[1271] | 65 | |
---|
[1599] | 66 | self.qcolor = {} |
---|
| 67 | self.scale_z = {} |
---|
| 68 | self.coloring = {} |
---|
| 69 | self.updating = {} |
---|
[1688] | 70 | self.setup = {} |
---|
[1280] | 71 | |
---|
[1599] | 72 | self.qcolor['stage'] = self.stage_color |
---|
| 73 | self.scale_z['stage'] = 1.0 |
---|
| 74 | self.coloring['stage'] = False |
---|
| 75 | self.updating['stage'] = True |
---|
[1688] | 76 | self.setup['stage'] = True |
---|
[1363] | 77 | |
---|
[1599] | 78 | self.qcolor['elevation'] = self.elevation_color |
---|
| 79 | self.scale_z['elevation'] = 1.0 |
---|
| 80 | self.coloring['elevation'] = False |
---|
| 81 | self.updating['elevation'] = False |
---|
[1688] | 82 | self.setup['elevation'] = False |
---|
[1599] | 83 | |
---|
[1266] | 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' |
---|
[1265] | 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 | |
---|
[1266] | 93 | #print 'keys',self.domain.quantities.keys() |
---|
| 94 | #print 'shape of stage',shape(self.stage) |
---|
[1265] | 95 | |
---|
[1280] | 96 | self.border_model = curve(frame = self.frame, |
---|
| 97 | pos=[(0,0),(0,1),(1,1),(1,0),(0,0)]) |
---|
[1363] | 98 | self.timer=label(pos=(0.75,0.75,0.5),text='Time=%10.5e'%self.domain.time, |
---|
| 99 | visible=False) |
---|
[1272] | 100 | |
---|
[1363] | 101 | |
---|
[1583] | 102 | scene.autoscale=0 |
---|
[1276] | 103 | #self.update_all() |
---|
[1290] | 104 | #scene.uniform=0 |
---|
[1363] | 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 |
---|
[1265] | 110 | |
---|
[1363] | 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 | |
---|
[1462] | 115 | # print self.range_z |
---|
| 116 | # print self.max_z |
---|
| 117 | # print self.min_z |
---|
[1363] | 118 | |
---|
[1290] | 119 | def setup_range_z(self,qname1='elevation',qname2='stage'): |
---|
| 120 | |
---|
| 121 | #print qname1 |
---|
| 122 | #print qname2 |
---|
[1363] | 123 | range_z = 1.0e-10 |
---|
[1290] | 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 |
---|
[1363] | 129 | range_z = max(range_z, max_z - min_z) |
---|
[1290] | 130 | except: |
---|
[1471] | 131 | #print 'Visualisation: could not find range of '+qname1 |
---|
[1290] | 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 |
---|
[1363] | 139 | range_z = max(range_z, max_z - min_z) |
---|
[1290] | 140 | except: |
---|
[1471] | 141 | #print 'Visualisation: could not find range of '+qname2 |
---|
[1290] | 142 | pass |
---|
| 143 | |
---|
[1363] | 144 | self.range_z = max(range_z,self.range_z) |
---|
| 145 | |
---|
[1599] | 146 | |
---|
| 147 | def setup_all(self): |
---|
| 148 | |
---|
[1639] | 149 | self.scene.select() |
---|
| 150 | |
---|
[1688] | 151 | for key in ['elevation','stage']: |
---|
| 152 | if self.setup[key] | self.updating[key]: |
---|
| 153 | self.update_quantity(key) |
---|
[1599] | 154 | |
---|
[1688] | 155 | |
---|
| 156 | |
---|
[1265] | 157 | def update_all(self): |
---|
| 158 | |
---|
[1639] | 159 | self.scene.select() |
---|
| 160 | |
---|
[1688] | 161 | for key in ['elevation','stage']: |
---|
| 162 | if self.updating[key]: |
---|
| 163 | self.update_quantity(key) |
---|
[1265] | 164 | |
---|
[1599] | 165 | |
---|
[1272] | 166 | def update_timer(self): |
---|
| 167 | |
---|
[1639] | 168 | self.scene.select() |
---|
| 169 | |
---|
[1363] | 170 | if self.domain.visualise_timer == True: |
---|
| 171 | self.timer.visible = True |
---|
| 172 | self.timer.text='Time=%10.5e'%self.domain.time |
---|
[1265] | 173 | |
---|
[1363] | 174 | |
---|
[1599] | 175 | def update_quantity(self,qname): |
---|
[1266] | 176 | |
---|
[1290] | 177 | #print 'update '+qname+' arrays' |
---|
| 178 | |
---|
[1599] | 179 | qcolor = self.qcolor[qname] |
---|
| 180 | scale_z = self.scale_z[qname] |
---|
| 181 | coloring = self.coloring[qname] |
---|
| 182 | updating = self.updating[qname] |
---|
| 183 | |
---|
| 184 | |
---|
[1290] | 185 | if qcolor is None: |
---|
[1471] | 186 | qcolor = self.other_color |
---|
[1387] | 187 | |
---|
[1290] | 188 | if qname=='elevation': |
---|
[1471] | 189 | qcolor = self.elevation_color |
---|
[1290] | 190 | |
---|
| 191 | if qname=='stage': |
---|
[1471] | 192 | qcolor = self.stage_color |
---|
| 193 | |
---|
[1462] | 194 | if qname=='friction': |
---|
[1471] | 195 | qcolor = self.friction_color |
---|
[1290] | 196 | |
---|
[1387] | 197 | if scale_z is None: |
---|
| 198 | scale_z = self.scale_z |
---|
[1290] | 199 | |
---|
[1599] | 200 | #try: |
---|
| 201 | if coloring: |
---|
| 202 | self.update_arrays_color(self.domain.quantities[qname].vertex_values, qcolor, scale_z) |
---|
| 203 | else: |
---|
[1387] | 204 | self.update_arrays(self.domain.quantities[qname].vertex_values, qcolor, scale_z) |
---|
[1290] | 205 | |
---|
[1599] | 206 | #print 'update bed image' |
---|
[1688] | 207 | if qname=='elevation': |
---|
[1697] | 208 | self.pos[:,2] = self.pos[:,2]+1.0e-3 |
---|
[1688] | 209 | |
---|
[1697] | 210 | self.vpython_z_models[qname].pos = self.pos |
---|
[1599] | 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 |
---|
[1265] | 216 | |
---|
[1387] | 217 | def update_quantity_color(self,qname,qcolor=None,scale_z=None): |
---|
[1265] | 218 | |
---|
[1290] | 219 | #print 'update '+qname+' arrays' |
---|
[1265] | 220 | |
---|
[1290] | 221 | if qcolor is None: |
---|
| 222 | if qname=='elevation': |
---|
[1471] | 223 | qcolor = self.elevation_color |
---|
[1265] | 224 | |
---|
[1290] | 225 | if qname=='stage': |
---|
[1471] | 226 | qcolor = self.stage_color |
---|
| 227 | |
---|
[1462] | 228 | if qname=='friction': |
---|
[1471] | 229 | qcolor = self.friction_color |
---|
[1265] | 230 | |
---|
[1387] | 231 | if scale_z is None: |
---|
| 232 | scale_z = self.scale_z |
---|
[1290] | 233 | |
---|
[1295] | 234 | #try: |
---|
[1387] | 235 | self.update_arrays_color(self.domain.quantities[qname].vertex_values, qcolor, scale_z) |
---|
[1290] | 236 | |
---|
| 237 | #print 'update bed image' |
---|
[1565] | 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 |
---|
[1295] | 241 | #except: |
---|
| 242 | # print 'Visualisation: Could not update quantity '+qname |
---|
| 243 | # pass |
---|
[1290] | 244 | |
---|
| 245 | |
---|
[1387] | 246 | def update_arrays(self,quantity,qcolor,scale_z): |
---|
[1265] | 247 | |
---|
[1266] | 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 |
---|
[1280] | 255 | range_z = self.range_z |
---|
[1266] | 256 | |
---|
| 257 | vertices = self.vertices |
---|
| 258 | pos = self.pos |
---|
| 259 | normals = self.normals |
---|
| 260 | colour = self.colour |
---|
| 261 | |
---|
| 262 | try: |
---|
[1272] | 263 | update_arrays_weave(vertices,quantity,col,pos,normals,colour,N, |
---|
[1280] | 264 | min_x,min_y,range_xy,range_z,scale_z) |
---|
[1266] | 265 | except: |
---|
[1272] | 266 | update_arrays_python(vertices,quantity,col,pos,normals,colour,N, |
---|
[1280] | 267 | min_x,min_y,range_xy,range_z,scale_z) |
---|
[1266] | 268 | |
---|
| 269 | |
---|
[1510] | 270 | |
---|
[1387] | 271 | def update_arrays_color(self,quantity,qcolor,scale_z): |
---|
[1266] | 272 | |
---|
[1290] | 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 | |
---|
[1295] | 287 | #try: |
---|
| 288 | update_arrays_color_python(vertices,quantity,col,pos,normals,colour,N, |
---|
[1290] | 289 | min_x,min_y,range_xy,range_z,scale_z) |
---|
[1295] | 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) |
---|
[1290] | 293 | |
---|
| 294 | |
---|
| 295 | #================================================================================== |
---|
| 296 | |
---|
[1272] | 297 | def update_arrays_python(vertices,quantity,col,pos,normals,colour,N, |
---|
[1280] | 298 | min_x,min_y,range_xy,range_z,scale_z): |
---|
[1266] | 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*j ]-min_x)/range_xy |
---|
| 309 | v[j,1] = (vertices[i,2*j+1]-min_y)/range_xy |
---|
[1280] | 310 | v[j,2] = quantity[i,j]/range_z*scale_z*0.5 |
---|
[1266] | 311 | |
---|
[1265] | 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 | |
---|
[1266] | 325 | pos[6*i ,:] = 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,:] |
---|
[1265] | 331 | |
---|
| 332 | |
---|
| 333 | |
---|
[1266] | 334 | colour[6*i ,:] = 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 |
---|
[1265] | 340 | |
---|
[1266] | 341 | s = 15.0/8.0 - s |
---|
[1265] | 342 | |
---|
[1266] | 343 | normals[6*i ,:] = 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 |
---|
[1265] | 349 | |
---|
| 350 | |
---|
| 351 | |
---|
[1272] | 352 | def update_arrays_weave(vertices,quantity,col,pos,normals,colour, |
---|
[1280] | 353 | N,min_x,min_y,range_xy,range_z,scale_z): |
---|
[1265] | 354 | |
---|
[1266] | 355 | import weave |
---|
| 356 | from weave import converters |
---|
[1265] | 357 | |
---|
[1266] | 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) |
---|
[1265] | 363 | |
---|
[1266] | 364 | code1 = """ |
---|
[1510] | 365 | |
---|
[1266] | 366 | double s = 1.0; |
---|
[1265] | 367 | |
---|
[1266] | 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; |
---|
[1280] | 372 | v(j,2) = quantity(i,j)/range_z*scale_z*0.5; |
---|
[1266] | 373 | } |
---|
[1265] | 374 | |
---|
| 375 | |
---|
[1266] | 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 | } |
---|
[1265] | 380 | |
---|
[1266] | 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); |
---|
[1265] | 384 | |
---|
[1266] | 385 | double norm = sqrt(normal(0)*normal(0) + normal(1)*normal(1) + normal(2)*normal(2)); |
---|
[1265] | 386 | |
---|
[1266] | 387 | normal(0) = normal(0)/norm; |
---|
| 388 | normal(1) = normal(1)/norm; |
---|
| 389 | normal(2) = normal(2)/norm; |
---|
[1265] | 390 | |
---|
[1387] | 391 | |
---|
[1266] | 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); |
---|
[1265] | 399 | |
---|
[1266] | 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); |
---|
[1265] | 406 | |
---|
[1266] | 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 | } |
---|
[1265] | 414 | |
---|
[1266] | 415 | s = 15.0/8.0 - s; |
---|
| 416 | } |
---|
[1278] | 417 | |
---|
[1266] | 418 | """ |
---|
| 419 | |
---|
| 420 | weave.inline(code1, ['vertices','quantity','col','pos','normals','colour','N', |
---|
[1280] | 421 | 'min_x','min_y','range_xy','range_z','scale_z','v','normal','v10','v20'], |
---|
[1266] | 422 | type_converters = converters.blitz, compiler='gcc'); |
---|
| 423 | |
---|
[1290] | 424 | def update_arrays_color_python(vertices,quantity,col,pos,normals,colour,N, |
---|
| 425 | min_x,min_y,range_xy,range_z,scale_z): |
---|
[1266] | 426 | |
---|
[1290] | 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*j ]-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*i ,:] = 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 | |
---|
[1295] | 459 | q0 = quantity[i,0]/0.4 |
---|
| 460 | q1 = quantity[i,1]/0.4 |
---|
| 461 | q2 = quantity[i,2]/0.4 |
---|
[1290] | 462 | |
---|
[1295] | 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) |
---|
[1290] | 469 | |
---|
[1295] | 470 | |
---|
| 471 | colour[6*i ,:] = 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 | |
---|
[1290] | 478 | s = 15.0/8.0 - s |
---|
| 479 | |
---|
| 480 | normals[6*i ,:] = 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 | |
---|
| 489 | def 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 | |
---|
[1295] | 528 | |
---|
[1290] | 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 | |
---|
[1295] | 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 | } |
---|
[1290] | 544 | |
---|
[1295] | 545 | s = 0.2+fabs((v(0,2)+v(1,2)+v(2,2))/3.0); |
---|
| 546 | for (int j=0; j<3; j++) { |
---|
[1290] | 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 | |
---|
[1266] | 563 | |
---|
[1407] | 564 | def setup_scene(): |
---|
[1265] | 565 | |
---|
[1407] | 566 | #scene.width = 1000 |
---|
| 567 | #scene.height = 800 |
---|
[1265] | 568 | |
---|
[1407] | 569 | #Original |
---|
| 570 | scene.center = (0.5,0.5,0.0) |
---|
| 571 | scene.forward = vector(-0.0, 0.5, -0.8) |
---|
[1265] | 572 | |
---|
[1407] | 573 | #Temporary (for bedslope) |
---|
| 574 | #scene.forward = vector(0.0006, 0.7, -0.03) |
---|
[1265] | 575 | |
---|
| 576 | |
---|
[1407] | 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 |
---|
[1265] | 582 | |
---|
| 583 | |
---|
[1407] | 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)] |
---|
[1266] | 587 | |
---|
[1407] | 588 | scene.lights = [(0.6, 0.3, 0.2), (0.1, -0.5, 0.4)] |
---|
[1266] | 589 | |
---|
[1265] | 590 | |
---|
[1407] | 591 | |
---|
| 592 | def create_surface(domain,scale_z=1.0): |
---|
| 593 | |
---|
[1471] | 594 | domain.initialise_visualiser(scale_z) |
---|
[1265] | 595 | |
---|
[1471] | 596 | domain.visualiser.update_quantity('elevation') |
---|
| 597 | domain.visualiser.update_quantity('stage') |
---|
| 598 | domain.visualiser.update_timer() |
---|
[1276] | 599 | |
---|
[1265] | 600 | def update(domain): |
---|
| 601 | |
---|
[1295] | 602 | if domain.visualise_color_stage: |
---|
[1471] | 603 | domain.visualiser.update_quantity_color('stage') |
---|
[1295] | 604 | else: |
---|
[1471] | 605 | domain.visualiser.update_quantity('stage') |
---|
[1295] | 606 | |
---|
[1471] | 607 | domain.visualiser.update_timer() |
---|
[1639] | 608 | |
---|
| 609 | |
---|
| 610 | |
---|
| 611 | if __name__ == '__main__': |
---|
| 612 | |
---|
| 613 | from advection import Domain as A_Domain |
---|
| 614 | from shallow_water import Domain as SW_Domain |
---|
[1688] | 615 | from shallow_water import Constant_height, Constant_stage |
---|
[1639] | 616 | from mesh_factory import rectangular |
---|
| 617 | |
---|
[1688] | 618 | points, vertices, boundary = rectangular(60, 60, len1=200, len2=200) |
---|
[1639] | 619 | a_domain = A_Domain(points, vertices, boundary) |
---|
[1688] | 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)) |
---|
[1639] | 624 | |
---|
| 625 | a_domain.initialise_visualiser() |
---|
[1688] | 626 | a_domain.visualiser.setup_all() |
---|
[1639] | 627 | |
---|
| 628 | |
---|
[1688] | 629 | #sw_domain.initialise_visualiser() |
---|
| 630 | #sw_domain.visualiser.setup['elevation']=True |
---|
| 631 | #sw_domain.visualiser.setup_all() |
---|
[1639] | 632 | |
---|
[1688] | 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 | |
---|
[1639] | 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() |
---|