Demo of running Fortran code and plotting results from an IPython notebook, and illustration of the behavior of various methods and limiters on the same problem.
This notebook can be found in $CLAW/apps/notebooks/classic/advection_1d
The 1-dimensional advection equation $q_t + uq_x = 0$ is solved in an interval.
The constant advection velocity $u$ is specified in setrun.py but can be changed below.
Have plots appear inline in notebook:
%pylab inline
Populating the interactive namespace from numpy and matplotlib
Check that the CLAW environment variable is set. (It must be set in the Unix shell before starting the notebook server).
import os
try:
CLAW = os.environ['CLAW']
print "Using Clawpack from ", CLAW
except:
print "*** Environment variable CLAW must be set to run code"
Using Clawpack from /Users/rjl/git/clawpack
from clawpack.clawutil import nbtools
nbtools.make_exe(new=True) # new=True ==> force recompilation of all code
Executing shell command: make new Done... Check this file to see output:
nbtools.make_htmls()
See the README.html file for links to input files...
First create data files needed for the Fortran code, using parameters specified in setrun.py:
nbtools.make_data(verbose=False)
Now run the code and produce plots. Specifying a label insures the resulting plot directory will persist after later runs are done below.
outdir,plotdir = nbtools.make_output_and_plots(label='1')
Executing shell command: make output OUTDIR=_output_1 Done... Check this file to see output:
Executing shell command: make plots OUTDIR=_output_1 PLOTDIR=_plots_1 Done... Check this file to see output:
View plots created at this link:
Clicking on the _PlotIndex link above, you can view an animation of the results.
After creating all png files in the _plots directory, these can also be combined in an animation that is displayed inline:
import clawpack.visclaw.JSAnimation.JSAnimation_frametools as J
anim = J.make_anim(plotdir, figno=1, figsize=(6,4))
anim
The animation above was computed using the default parameter values specified in setrun.py
, which specified using the high-resolution method with the MC limiter.
See the README.html file for a link to setrun.py
.
We can adjust the parameters by reading in the default values, changing one or more, and then writing the data out for the Fortran code to use:
import setrun
rundata = setrun.setrun()
print "The order is currently set to ",rundata.clawdata.order
print "The limiter is currently set to ",rundata.clawdata.limiter
The order is currently set to 2 The limiter is currently set to ['mc']
Switch to the first order method and write out the data. Then rerun the code. Note that the results are much more diffusive.
rundata.clawdata.order = 1
rundata.write()
outdir, plotdir = nbtools.make_output_and_plots(verbose=False)
anim = J.make_anim(plotdir, figno=1, figsize=(6,4))
anim
Switch back to the second-order method and use no limiter so the high-resolution method reduces to Lax-Wendroff.
A list of 1 limiter is required since this is a scalar problem with only 1 wave in the Riemann solution, so we set the limiter to ['none']
.
Note that the Lax-Wendroff method is dispersive and introduces spurious oscillations, particularly near the discontinuities in the solution.
rundata.clawdata.order = 2
rundata.clawdata.limiter = ['none']
rundata.write()
outdir, plotdir = nbtools.make_output_and_plots(verbose=False)
anim = J.make_anim(plotdir, figno=1, figsize=(6,4))
anim
Try the Lax-Wendroff method with 400 grid cells rather than the 200 used by default. Do the oscillations decrease in amplitude? The parameter to change is rundata.clawdata.num_cells
, which is a list of the number of cells in each direction (only a single entry for this 1-dimensional problem).
print "The number of grid cells is currently set to ", rundata.clawdata.num_cells
The number of grid cells is currently set to [200]