|
| 1 | +from pylab import * |
| 2 | +import glob, os |
| 3 | +import pyvista as pv |
| 4 | +from clawpack.geoclaw import topotools |
| 5 | +from clawpack.visclaw import animation_tools |
| 6 | +from clawpack.geoclaw import fgout_tools |
| 7 | +from clawpack.visclaw import colormaps |
| 8 | + |
| 9 | +tan = [0.8,0.5,0.2] |
| 10 | +light_blue = [0.0,1.0,1.0] |
| 11 | +cmap_massfrac = colormaps.make_colormap({0:light_blue, 0.1:light_blue, |
| 12 | + 1:tan}) |
| 13 | + |
| 14 | +def plot_topo(): |
| 15 | + topo = topotools.Topography('basal_topo.tt3') |
| 16 | + z = array([0.]) |
| 17 | + x = topo.x |
| 18 | + y = -topo.y |
| 19 | + X,Y,Z = meshgrid(x, y, z, indexing='ij') |
| 20 | + topoxyz = pv.StructuredGrid(X,Y,Z) |
| 21 | + |
| 22 | + B = flipud(topo.Z) |
| 23 | + B = fliplr(B) |
| 24 | + topoxyz.point_data['B'] = B.flatten(order='C') |
| 25 | + |
| 26 | + warpfactor = 5 # amplification of elevations |
| 27 | + |
| 28 | + topowarp = topoxyz.warp_by_scalar('B', factor=warpfactor) |
| 29 | + |
| 30 | + # plot topo alone: |
| 31 | + p = pv.Plotter() |
| 32 | + #p.add_mesh(topoxyz,cmap='gist_earth',clim=(-10,100)) |
| 33 | + p.add_mesh(topowarp,cmap='gist_earth',clim=(-20,400)) |
| 34 | + p.add_title('Topography') |
| 35 | + p.show(window_size=(1500,1500)) |
| 36 | + |
| 37 | +def plot_topo_fgout(): |
| 38 | + fgno = 1 # which fgout grid |
| 39 | + |
| 40 | + outdir = '_output1' |
| 41 | + format = 'binary32' # format of fgout grid output |
| 42 | + fgout_grid = fgout_tools.FGoutGrid(fgno, outdir, format, qmap='dclaw') |
| 43 | + |
| 44 | + fgout = fgout_grid.read_frame(1) |
| 45 | + |
| 46 | + x = fgout.x |
| 47 | + y = fgout.y |
| 48 | + z = array([0.]) |
| 49 | + X,Y,Z = meshgrid(x, y, z, indexing='ij') |
| 50 | + topoxyz = pv.StructuredGrid(X,Y,Z) |
| 51 | + |
| 52 | + B = fgout.B |
| 53 | + topoxyz.point_data['B'] = B.flatten(order='C') |
| 54 | + warpfactor = 5 # amplification of elevations |
| 55 | + topowarp = topoxyz.warp_by_scalar('B', factor=warpfactor) |
| 56 | + |
| 57 | + h = fgout.h |
| 58 | + topoxyz.point_data['h'] = h.flatten(order='C') |
| 59 | + warpfactor = 5 # amplification of elevations |
| 60 | + hwarp = topoxyz.warp_by_scalar('h', factor=warpfactor) |
| 61 | + |
| 62 | + eta = fgout.eta |
| 63 | + topoxyz.point_data['eta'] = B.flatten(order='C') |
| 64 | + warpfactor = 5 # amplification of elevations |
| 65 | + etawarp = topoxyz.warp_by_scalar('eta', factor=warpfactor) |
| 66 | + |
| 67 | + p = pv.Plotter() |
| 68 | + #p.add_mesh(topowarp,cmap='gist_earth',clim=(-20,400)) |
| 69 | + p.add_mesh(topowarp,color='g') |
| 70 | + #p.add_mesh(hwarp,color='r') |
| 71 | + #p.add_mesh(etawarp,color='b') |
| 72 | + p.add_title('Topography fgout.B') |
| 73 | + p.show(window_size=(1500,1500)) |
| 74 | + |
| 75 | + |
| 76 | +def plot_fgout(save_fig=False): |
| 77 | + |
| 78 | + global etamesh |
| 79 | + |
| 80 | + fgno = 1 # which fgout grid |
| 81 | + outdir = '_output' |
| 82 | + format = 'binary32' # format of fgout grid output |
| 83 | + warpfactor = 5 # amplification of elevations |
| 84 | + |
| 85 | + fgout_frames = glob.glob(os.path.join(outdir, \ |
| 86 | + 'fgout%s.t*' % str(fgno).zfill(4))) |
| 87 | + nfgout = len(fgout_frames) |
| 88 | + fgframes = range(1, nfgout+1) |
| 89 | + print('Found %i fgout frames' % nfgout) |
| 90 | + |
| 91 | + # Instantiate object for reading fgout frames: |
| 92 | + fgout_grid = fgout_tools.FGoutGrid(fgno, outdir, format, qmap='dclaw') |
| 93 | + |
| 94 | + fgout = fgout_grid.read_frame(1) |
| 95 | + |
| 96 | + x = fgout.x |
| 97 | + y = fgout.y |
| 98 | + z = array([0.]) |
| 99 | + X,Y,Z = meshgrid(x, y, z, indexing='ij') |
| 100 | + topoxyz = pv.StructuredGrid(X,Y,Z) |
| 101 | + |
| 102 | + B = fgout.B |
| 103 | + topoxyz.point_data['B'] = B.flatten(order='C') |
| 104 | + topowarp = topoxyz.warp_by_scalar('B', factor=warpfactor) |
| 105 | + |
| 106 | + p = pv.Plotter() |
| 107 | + #p.add_mesh(topowarp,cmap='gist_earth',clim=(-20,400)) |
| 108 | + p.add_mesh(topowarp,color='lightgreen') |
| 109 | + |
| 110 | + |
| 111 | + def set_frame(tsec): |
| 112 | + global etamesh |
| 113 | + fgframeno = int(round(tsec)) + 1 |
| 114 | + fgout = fgout_grid.read_frame(fgframeno) |
| 115 | + tsec = fgout.t |
| 116 | + print('Frame %i, t = %.1f seconds' % (fgframeno, fgout.t)) |
| 117 | + |
| 118 | + # replace land surface by nan in eta so it only shows water: |
| 119 | + # (big tolerance for landslide, would normally be smaller) |
| 120 | + eta = where(fgout.h>1, fgout.eta, nan) |
| 121 | + |
| 122 | + topoxyz.point_data['eta'] = eta.flatten(order='C') |
| 123 | + etawarp = topoxyz.warp_by_scalar('eta', factor=warpfactor) |
| 124 | + p.remove_actor(etamesh) |
| 125 | + #etamesh = p.add_mesh(etawarp,color='c') # dirt and water both cyan |
| 126 | + |
| 127 | + # color the mesh based on mass fraction, using cmap_massfrac |
| 128 | + h = fgout.h |
| 129 | + massfrac = divide(fgout.hm, h, where=h>0, out=nan*ones(h.shape)) |
| 130 | + etamesh = p.add_mesh(etawarp,scalars=massfrac, |
| 131 | + colormap=cmap_massfrac, clim=(0,0.6)) |
| 132 | + |
| 133 | + p.add_title('Frame %i at time %.1f seconds' % (fgframeno,tsec)) |
| 134 | + if save_fig: |
| 135 | + # to capture each frame after moving slider: |
| 136 | + fname = 'PyVistaFrame%s.png' % str(fgframeno).zfill(4) |
| 137 | + p.screenshot(fname) |
| 138 | + print('Saving ', fname) |
| 139 | + |
| 140 | + p.add_slider_widget(set_frame, [0,100], value=0, |
| 141 | + pointa=(0.4,0.8), pointb=(0.9,0.8), |
| 142 | + title='Time (sec)') |
| 143 | + |
| 144 | + p.camera_position = 'xz' |
| 145 | + p.camera.azimuth = 45 |
| 146 | + p.camera.elevation = 20 |
| 147 | + p.show(window_size=(1500,1500)) |
0 commit comments