This example is adapted from $CLAW/amrclaw/examples/advection_2d_square to illustrate the use of IPython notebooks.
The setrun.py and setplot.py files are taken from that example but some parameters are modified in the notebook below.
The 2-dimensional advection equation $q_t + uq_x + vq_y = 0$ is solved in the unit square with periodic boundary conditions. The constant advection velocities $u$ and $v$ are specified in setrun.py but could be changed below.
Have plots appear in notebook:
%pylab inline
Populating the interactive namespace from numpy and matplotlib
Set the environment variable CLAW used by the Fortran Makefile:
import os
CLAW = os.environ['CLAW']
print CLAW
/Users/rjl/git/clawpack
Make sure we're in the right directory:
example_dir = os.getcwd()
print example_dir
/Users/rjl/git/clawpack/apps/notebooks/amrclaw/advection_2d_square
%%system
make .exe
["make: Nothing to be done for `.exe'."]
from clawpack.clawutil.runclaw import runclaw
import setrun
outdir = '_output'
rundata = setrun.setrun()
The file setrun.py in this directory contains the basic runtime parameters which could be used directly.
If you change these parameters, select "Run all" from the Cell menu to redo the run and the plots.
Here we adjust the mesh to be $40 \times 40$ and add a second gauge at $(0.6,0.8)$ in addition to the one specified in setrun.py
rundata.clawdata.num_cells = [40,40]
rundata.gaugedata.gauges.append([2, 0.6, 0.8, 0., 10.])
The advection velocities could be changed here (in setrun.py, $(u,v) = (0.5,1)$.
rundata.probdata.u = 0.5
rundata.probdata.v = 1.0
Change the limiter if you want to experiment with other methods. A list of 1 limiter is required since this is a scalar problem with only 1 wave in the Riemann solution.
In particular, set the limiter to ['none'] to see the oscillations that arise if Lax-Wendroff is used with no limiter.
rundata.clawdata.limiter = ['vanleer']
rundata.write()
runclaw(xclawcmd='xamr', outdir=outdir, overwrite=True, restart=False, rundir='.')
==> runclaw: Will take data from /Users/rjl/git/clawpack/apps/notebooks/amrclaw/advection_2d_square ==> runclaw: Will write output to /Users/rjl/git/clawpack/apps/notebooks/amrclaw/advection_2d_square/_output ==> runclaw: Removing all old fort files in /Users/rjl/git/clawpack/apps/notebooks/amrclaw/advection_2d_square/_output ==> runclaw: Finished executing ==> runclaw: Done executing /Users/rjl/git/clawpack/apps/notebooks/amrclaw/advection_2d_square/xamr via clawutil.runclaw.py ==> runclaw: Output is in /Users/rjl/git/clawpack/apps/notebooks/amrclaw/advection_2d_square/_output
from clawpack.visclaw.data import ClawPlotData
import setplot
plotdir = '_plots'
plotdata = ClawPlotData()
plotdata = setplot.setplot(plotdata)
plotdata.outdir = outdir
plotdata.plotdir = plotdir
plotdata.plotframe(2); show()
Reading Frame 2 at t = 0.4 from outdir = /Users/rjl/git/clawpack/apps/notebooks/amrclaw/advection_2d_square/_output
plotdata.showitems()
Current plot figures, axes, and items: --------------------------------------- figname = pcolor, figno = 0 axesname = AXES1, axescmd = subplot(1,1,1) itemname = ITEM1, plot_type = 2d_pcolor figname = contour, figno = 1 [Not showing] axesname = AXES1, axescmd = subplot(1,1,1) itemname = ITEM1, plot_type = 2d_contour figname = cells, figno = 2 [Not showing] axesname = AXES1, axescmd = subplot(1,1,1) itemname = ITEM1, plot_type = 2d_patch figname = q, figno = 300 axesname = AXES1, axescmd = subplot(1,1,1) itemname = ITEM1, plot_type = 1d_plot
plotitem = plotdata.getitem('ITEM1','AXES1','pcolor')
plotitem.amr_patchedges_show = [1] # show patch edges on all levels
plotitem.amr_celledges_show = [1,0] # only show grid cells on coarsest level
for frameno in range(4):
plotdata.plotframe(frameno)
show()
Reading Frame 0 at t = 0 from outdir = /Users/rjl/git/clawpack/apps/notebooks/amrclaw/advection_2d_square/_output
Reading Frame 1 at t = 0.2 from outdir = /Users/rjl/git/clawpack/apps/notebooks/amrclaw/advection_2d_square/_output
Reading Frame 3 at t = 0.6 from outdir = /Users/rjl/git/clawpack/apps/notebooks/amrclaw/advection_2d_square/_output
Currently using plotpages.plotclaw_driver below screws up where output is printed. We need to fix this, but for now save these for resetting later...
import sys
sys_stdout = sys.stdout
sys_stderr = sys.stderr
#plotdata.setplot = False # to keep changes made above to plotdata
plotdata.setplot = setplot.setplot # to use original setplot function
plotdata.plotdir = plotdir
from clawpack.visclaw import plotpages
plotpages.plotclaw_driver(plotdata, verbose=False)
sys.stdout = sys_stdout; sys.stderr = sys_stderr
Executed setplot successfully Will plot 11 frames numbered: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Will make 1 figure(s) for each frame, numbered: [0] ----------------------------------- Creating html pages for figures... Directory '/Users/rjl/git/clawpack/apps/notebooks/amrclaw/advection_2d_square/_plots' already exists, files may be overwritten Now making png files for all figures... Frame 0 at time t = 0.0 Frame 1 at time t = 0.2 Frame 2 at time t = 0.4 Frame 3 at time t = 0.6 Reading Frame 4 at t = 0.8 from outdir = /Users/rjl/git/clawpack/apps/notebooks/amrclaw/advection_2d_square/_output Frame 4 at time t = 0.8 Reading Frame 5 at t = 1 from outdir = /Users/rjl/git/clawpack/apps/notebooks/amrclaw/advection_2d_square/_output Frame 5 at time t = 1.0 Reading Frame 6 at t = 1.2 from outdir = /Users/rjl/git/clawpack/apps/notebooks/amrclaw/advection_2d_square/_output Frame 6 at time t = 1.2 Reading Frame 7 at t = 1.4 from outdir = /Users/rjl/git/clawpack/apps/notebooks/amrclaw/advection_2d_square/_output Frame 7 at time t = 1.4 Reading Frame 8 at t = 1.6 from outdir = /Users/rjl/git/clawpack/apps/notebooks/amrclaw/advection_2d_square/_output Frame 8 at time t = 1.6 Reading Frame 9 at t = 1.8 from outdir = /Users/rjl/git/clawpack/apps/notebooks/amrclaw/advection_2d_square/_output Frame 9 at time t = 1.8 Reading Frame 10 at t = 2 from outdir = /Users/rjl/git/clawpack/apps/notebooks/amrclaw/advection_2d_square/_output Frame 10 at time t = 2.0 Reading gauge data from /Users/rjl/git/clawpack/apps/notebooks/amrclaw/advection_2d_square/_output/fort.gauge Read in gauges [1 2] Gauge 1 Gauge 2 ----------------------------------- Creating latex file... Directory '/Users/rjl/git/clawpack/apps/notebooks/amrclaw/advection_2d_square/_plots' already exists, files may be overwritten Latex file created: /Users/rjl/git/clawpack/apps/notebooks/amrclaw/advection_2d_square/_plots/plots.tex Use pdflatex to create pdf file Created JSAnimation for figure 0 -------------------------------------------------------- Point your browser to: file:///Users/rjl/git/clawpack/apps/notebooks/amrclaw/advection_2d_square/_plots/_PlotIndex.html
Note the figures displayed above are the final plots produced by plotpages (e.g. last frame)
After creating all png files in the _plots directory...
import glob
from matplotlib import image
from clawpack.visclaw.JSAnimation import IPython_display
from matplotlib import animation
figno = 0
fname = '_plots/*fig' + str(figno) + '.png'
filenames=sorted(glob.glob(fname))
fig = plt.figure()
im = plt.imshow(image.imread(filenames[0]))
def init():
im.set_data(image.imread(filenames[0]))
return im,
def animate(i):
image_i=image.imread(filenames[i])
im.set_data(image_i)
return im,
animation.FuncAnimation(fig, animate, init_func=init,
frames=len(filenames), interval=20, blit=True)
for gaugeno in [1,2]:
gauge = plotdata.getgauge(gaugeno)
q = gauge.q[0,:]
t = gauge.t
qmax = q.max()
tmax = t[q.argmax()]
print "Gauge %s at %s has a maximum value of q = %7.5f at t = %7.5f" % (gaugeno, gauge.location, qmax, tmax)
plot(t,q,label="Gauge %s" % gaugeno)
legend()
ylim(-0.1,1.1)
xlabel('time')
ylabel('q at gauge')
title("Gauge output")
Gauge 1 at [0.6, 0.4] has a maximum value of q = 1.00000 at t = 0.08912 Gauge 2 at [0.6, 0.8] has a maximum value of q = 1.00000 at t = 0.24500
<matplotlib.text.Text at 0x109c056d0>