Demo of running the Classic Clawpack Fortran code and producing an animation of results

If you’re viewing this notebook online and wish to run it, you can download it from here.

The environment variable CLAW should be set before starting the notebook:

In [1]:
import os
CLAW = os.environ['CLAW']
print CLAW
/Users/rjl/git/clawpack

Move to the desired directory:

In [2]:
os.chdir(CLAW + '/classic/examples/acoustics_1d_example1/')
print os.getcwd()

/Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1
In [3]:
%%system
make output
Out[3]:
['rm -f .output',
 'python /Users/rjl/git/clawpack/clawutil/src/python/clawutil/runclaw.py xclaw                          _output                     \\',
 '\tTrue                    False                     . False',
 'Reading data file: claw.data   ',
 '         first 5 lines are comments and will be skipped',
 ' running...',
 '  ',
 'Reading data file: setprob.data             ',
 '         first 5 lines are comments and will be skipped',
 'CLAW1EZ: Frame    1 output files done at time t =  0.5000D-01',
 '',
 'CLAW1EZ: Frame    2 output files done at time t =  0.1000D+00',
 '',
 'CLAW1EZ: Frame    3 output files done at time t =  0.1500D+00',
 '',
 'CLAW1EZ: Frame    4 output files done at time t =  0.2000D+00',
 '',
 'CLAW1EZ: Frame    5 output files done at time t =  0.2500D+00',
 '',
 'CLAW1EZ: Frame    6 output files done at time t =  0.3000D+00',
 '',
 'CLAW1EZ: Frame    7 output files done at time t =  0.3500D+00',
 '',
 'CLAW1EZ: Frame    8 output files done at time t =  0.4000D+00',
 '',
 'CLAW1EZ: Frame    9 output files done at time t =  0.4500D+00',
 '',
 'CLAW1EZ: Frame   10 output files done at time t =  0.5000D+00',
 '',
 'CLAW1EZ: Frame   11 output files done at time t =  0.5500D+00',
 '',
 'CLAW1EZ: Frame   12 output files done at time t =  0.6000D+00',
 '',
 'CLAW1EZ: Frame   13 output files done at time t =  0.6500D+00',
 '',
 'CLAW1EZ: Frame   14 output files done at time t =  0.7000D+00',
 '',
 'CLAW1EZ: Frame   15 output files done at time t =  0.7500D+00',
 '',
 'CLAW1EZ: Frame   16 output files done at time t =  0.8000D+00',
 '',
 '==> runclaw: Will take data from  /Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1',
 '==> runclaw: Will write output to  /Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1/_output',
 '==> runclaw: Removing all old fort files in  /Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1/_output',
 '',
 '==> runclaw: Finished executing',
 '',
 '==> runclaw: Done executing /Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1/xclaw via clawutil.runclaw.py',
 '==> runclaw: Output is in  /Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1/_output']
In [4]:
%%system
make plots
Out[4]:
['rm -f .plots',
 'python /Users/rjl/git/clawpack/visclaw/src/python/visclaw/plotclaw.py _output                     _plots                     setplot.py           ',
 'Importing setplot.setplot from /Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1.',
 'Executed setplot successfully',
 'Will plot 17 frames numbered: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]',
 'Will make 1 figure(s) for each frame, numbered:  [1]',
 '',
 '-----------------------------------',
 '',
 '',
 'Creating html pages for figures...',
 '',
 "Directory '/Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1/_plots' ",
 '    already exists, files may be overwritten ',
 'Now making png files for all figures...',
 '    Reading  Frame 0 at t = 0  from outdir = /Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1/_output',
 'Frame 0 at time t = 0.0',
 '    Reading  Frame 1 at t = 0.05  from outdir = /Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1/_output',
 'Frame 1 at time t = 0.05',
 '    Reading  Frame 2 at t = 0.1  from outdir = /Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1/_output',
 'Frame 2 at time t = 0.1',
 '    Reading  Frame 3 at t = 0.15  from outdir = /Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1/_output',
 'Frame 3 at time t = 0.15',
 '    Reading  Frame 4 at t = 0.2  from outdir = /Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1/_output',
 'Frame 4 at time t = 0.2',
 '    Reading  Frame 5 at t = 0.25  from outdir = /Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1/_output',
 'Frame 5 at time t = 0.25',
 '    Reading  Frame 6 at t = 0.3  from outdir = /Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1/_output',
 'Frame 6 at time t = 0.3',
 '    Reading  Frame 7 at t = 0.35  from outdir = /Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1/_output',
 'Frame 7 at time t = 0.35',
 '    Reading  Frame 8 at t = 0.4  from outdir = /Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1/_output',
 'Frame 8 at time t = 0.4',
 '    Reading  Frame 9 at t = 0.45  from outdir = /Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1/_output',
 'Frame 9 at time t = 0.45',
 '    Reading  Frame 10 at t = 0.5  from outdir = /Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1/_output',
 'Frame 10 at time t = 0.5',
 '    Reading  Frame 11 at t = 0.55  from outdir = /Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1/_output',
 'Frame 11 at time t = 0.55',
 '    Reading  Frame 12 at t = 0.6  from outdir = /Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1/_output',
 'Frame 12 at time t = 0.6',
 '    Reading  Frame 13 at t = 0.65  from outdir = /Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1/_output',
 'Frame 13 at time t = 0.65',
 '    Reading  Frame 14 at t = 0.7  from outdir = /Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1/_output',
 'Frame 14 at time t = 0.7',
 '    Reading  Frame 15 at t = 0.75  from outdir = /Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1/_output',
 'Frame 15 at time t = 0.75',
 '    Reading  Frame 16 at t = 0.8  from outdir = /Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1/_output',
 'Frame 16 at time t = 0.8',
 '',
 '-----------------------------------',
 '',
 'Creating latex file...',
 "Directory '/Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1/_plots' ",
 '    already exists, files may be overwritten ',
 '',
 'Latex file created:  ',
 '  /Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1/_plots/plots.tex',
 '',
 'Use pdflatex to create pdf file',
 'Created JSAnimation for figure 1',
 '',
 '--------------------------------------------------------',
 '',
 'Point your browser to:',
 '    file:///Users/rjl/git/clawpack/classic/examples/acoustics_1d_example1/_plots/_PlotIndex.html',
 'python(45105,0x7fff76101180) malloc: *** error for object 0x10413f410: pointer being freed was not allocated',
 '*** set a breakpoint in malloc_error_break to debug',
 'make: *** [plots] Abort trap: 6']

To display a single frame:

In [6]:
from IPython.display import Image
frameno = 2
fname = '_plots/frame00%sfig1.png' % str(frameno).zfill(2)
print "showing ",fname
i = Image(fname)
display(i)
showing  _plots/frame0002fig1.png
../../../../../../../../Users/rjl/git/clawpack/doc/doc/_build/html/sphinx-versioning/.doctrees/nbsphinx/classic_demo_9_1.png

One way to display a sequence of plot images as an animation:

In [8]:
import glob
from matplotlib import image
from clawpack.visclaw.JSAnimation import IPython_display
from matplotlib import animation

figno = 1
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)
Out[8]:


Once Loop Reflect

Another approach to animation:

Based on the pyclaw example at https://www.wakari.io/usermgmt/nb/pyclaw/Figure_7_2

This only works if the notebook is running, whereas the above animation should play from nbviewer.

In [9]:
import matplotlib.pyplot as plt
import time
from IPython.display import clear_output

def solution_plot(n,setplot=None,outdir='./_output'):
    from clawpack.visclaw import data, frametools
    plotdata = data.ClawPlotData()
    plotdata.outdir = outdir
    if setplot is None:
        from setplot import setplot
    plotdata.setplot = setplot
    plotdata = frametools.call_setplot(setplot,plotdata)
    frametools.plotframe(n,plotdata)

for n in range(5):
    solution_plot(n)
    time.sleep(0.2)
    clear_output()
    f=plt.gcf()
    display(f)
    plt.clf()
../../../../../../../../Users/rjl/git/clawpack/doc/doc/_build/html/sphinx-versioning/.doctrees/nbsphinx/classic_demo_14_0.png
<matplotlib.figure.Figure at 0x106b9bad0>
In [ ]: