Skip to content

Commit

Permalink
Merge pull request #493 from ketch/geom_doctests
Browse files Browse the repository at this point in the history
Tweaks to geometry modifications
  • Loading branch information
ketch committed Dec 30, 2014
2 parents 5f54fe1 + 8308666 commit 41650ba
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/pyclaw/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class Grid(object):
A PyClaw grid is usually constructed from a tuple of PyClaw Dimension objects:
>>> from clawpack.pyclaw.geometry import Dimension, Grid
>>> x = Dimension('x',0.,1.,10)
>>> y = Dimension('y',-1.,1.,25)
>>> x = Dimension(0.,1.,10,name='x')
>>> y = Dimension(-1.,1.,25,name='y')
>>> grid = Grid((x,y))
>>> print grid
2-dimensional domain (x,y)
Expand All @@ -71,7 +71,7 @@ class Grid(object):
A grid can be extended to higher dimensions using the add_dimension() method:
>>> z=Dimension('z',-2.0,2.0,21)
>>> z=Dimension(-2.0,2.0,21,name='z')
>>> grid.add_dimension(z)
>>> grid.num_dim
3
Expand All @@ -83,11 +83,13 @@ class Grid(object):
We can get the x, y, and z-coordinate arrays of cell edges and centers from the grid.
Properties beginning with 'c' refer to the computational (unmapped) domain, while
properties beginning with 'p' refer to the physical (mapped) domain. For grids with
no mapping, the two are identical. Notice also the difference between 'center' and
'centers':
no mapping, the two are identical. Also note the difference between 'center' and
'centers'.
>>> grid.p_center([1,2,3])
[0.15000000000000002, -0.80000000000000004, -1.3333333333333335]
>>> import numpy as np
>>> np.set_printoptions(precision=2) # avoid doctest issues with roundoff
>>> grid.c_center([1,2,3])
array([ 0.15, -0.8 , -1.33])
>>> grid.p_edges[0][0,0,0]
0.0
>>> grid.p_edges[1][0,0,0]
Expand All @@ -97,7 +99,7 @@ class Grid(object):
It's also possible to get coordinates for ghost cell arrays:
>>> x = Dimension('x',0.,1.,5)
>>> x = Dimension(0.,1.,5,name='x')
>>> grid1d = Grid([x])
>>> grid1d.c_centers
[array([ 0.1, 0.3, 0.5, 0.7, 0.9])]
Expand All @@ -110,7 +112,7 @@ class Grid(object):
or to adjust the local spacing of grid cells. For instance, we can
use smaller cells on the left and larger cells on the right by doing:
>>> double = lambda x : x[0]**2
>>> double = lambda xarr : np.array([x**2 for x in xarr])
>>> grid1d.mapc2p = double
>>> grid1d.p_centers
array([ 0.01, 0.09, 0.25, 0.49, 0.81])
Expand Down Expand Up @@ -222,8 +224,6 @@ def __init__(self,dimensions):
for dim in dimensions:
self.add_dimension(dim)

self.mapc2p = identity_map[str(self.num_dim)]

super(Grid,self).__init__()

def _clear_cached_values(self):
Expand All @@ -250,6 +250,8 @@ def add_dimension(self,dimension):
self._dimensions.append(dimension.name)
setattr(self,dimension.name,dimension)
self._clear_cached_values()
# Reset mapping as it presumably makes no sense now
self.mapc2p = identity_map[str(self.num_dim)]


def get_dim_attribute(self,attr):
Expand All @@ -264,7 +266,7 @@ def __copy__(self):
def __str__(self):
output = "%s-dimensional domain " % str(self.num_dim)
output += "("+",".join([dim.name for dim in self.dimensions])+")\n"
if self.mapc2p == identity_map:
if self.mapc2p in identity_map.values():
output += "No mapping\n"
output += "Extent: "
else:
Expand Down Expand Up @@ -331,7 +333,7 @@ def c_center(self,ind):
r"""Compute center of computational cell with index ind."""

index = [np.array(i) for i in ind]
return [self.c_centers[i][index] for i in range(self.num_dim)]
return np.array([self.c_centers[i][index] for i in range(self.num_dim)])

def p_center(self,ind):
r"""Compute center of physical cell with index ind."""
Expand Down

0 comments on commit 41650ba

Please sign in to comment.