Skip to content

Commit 006c11f

Browse files
committed
add examples/radial_slide/pyvista_plotting.py for 3D plot using PyVista, requires new fgout capabilities
1 parent e75f191 commit 006c11f

File tree

2 files changed

+152
-3
lines changed

2 files changed

+152
-3
lines changed
+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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))

examples/radial_slide/setrun.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ def setrun(claw_pkg='dclaw'):
126126

127127
if clawdata.output_style==1:
128128
# Output nout frames at equally spaced times up to tfinal:
129-
clawdata.num_output_times = 40 #240
130-
clawdata.tfinal = 160. #240.
129+
clawdata.num_output_times = 10 #240
130+
clawdata.tfinal = 100. #240.
131131
clawdata.output_t0 = True # output at initial (or restart) time?
132132

133133
elif clawdata.output_style == 2:
@@ -472,7 +472,8 @@ def setrun(claw_pkg='dclaw'):
472472
fgout = fgout_tools.FGoutGrid()
473473
fgout.fgno = 1
474474
fgout.point_style = 2 # will specify a 2d grid of points
475-
fgout.output_format = 'binary32' # 4-byte, float32
475+
#fgout.output_format = 'binary32' # 4-byte, float32
476+
fgout.output_format = 'ascii' # 4-byte, float32
476477
fgout.nx = 300
477478
fgout.ny = 300
478479
fgout.x1 = 0. # specify edges (fgout pts will be cell centers)
@@ -482,6 +483,7 @@ def setrun(claw_pkg='dclaw'):
482483
fgout.tstart = 0.
483484
fgout.tend = 100.
484485
fgout.nout = 101
486+
fgout.q_out_vars = [1,4,8]
485487
fgout_grids.append(fgout) # written to fgout_grids.data
486488

487489

0 commit comments

Comments
 (0)