#!/usr/bin/env python # coding: utf-8 # You can actually do a lot of interesting things -- not related to hyperbolic PDEs -- with the building blocks of PyClaw. Let's see how. # # To run this notebook, you'll first need to [install PyClaw](http://www.clawpack.org/pyclaw/started.html) if you haven't already. # In[1]: get_ipython().run_line_magic('matplotlib', 'inline') from clawpack import pyclaw import numpy as np import matplotlib.pyplot as plt # The `pyclaw.Geometry` module contains things of general use when dealing with structured grids in 1, 2, or 3 dimensions. If you place the cursor at the end of the next cell and hit ``, you'll see what is included: # # - Dimension # - Patch # - Grid # - Domain # In[ ]: pyclaw.geometry. # # `pyclaw.Dimension` # The building block for all PyClaw geometry is the Dimension object. The docstring explains most of what it can do. # In[2]: print pyclaw.Dimension.__doc__ # Let's instantiate a `Dimension` object and see what we can do with it. # In[3]: x = pyclaw.Dimension(0.,1.,10) # Dimension with 10 intervals in [0,1] # A Dimension object is essentially an equipartitioning of an interval. The four arguments used to initialize it are, in order: # # - A name # - The left end of the interval (`lower`) # - The right end of the interval (`upper`) # - The number of partitions, or cells (`n`) # # Printing the Dimension object gives us essentially this information back: # In[4]: print x x.centers # Notice that the printed statement also includes `delta`, the width of a single partition. The Dimension knows a lot more. For instance, it can tell us the locations of the cell centers: # In[5]: print x.centers # and of the cell edges (often referred to as *interfaces* in finite volume terminology): # In[6]: print x.nodes # ## Modifying a Dimension # The Dimension is an interactive object: if we change one of its properties, the others are updated automatically. Here, we change the number of cells from 10 to 8 and the cell centers are automatically respaced. # In[7]: x.num_cells=8 print x x.centers # Similarly, we can change the boundary locations: # In[8]: x.lower=-0.5 print x x.centers # Using the cell and edge coordinates, we can easily plot a function that is defined piecewise over these intervals: # In[9]: q = np.exp(x.centers) print q for i in range(x.num_cells): plt.plot([x.nodes[i],x.edges[i+1]],[q[i],q[i]],'b',lw=2) if i