Skip to content

Commit

Permalink
Move fully to new Dimension constructor syntax. Resolves clawpack#480.
Browse files Browse the repository at this point in the history
Also, fix some potential issues in which io read routines
created num_cells as an array of floats.
  • Loading branch information
ketch committed May 19, 2015
1 parent 38767b3 commit 03443e6
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 39 deletions.
4 changes: 2 additions & 2 deletions examples/psystem_2d/psystem_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ def setup(kernel_language='Fortran',
x_lower=0.25; x_upper=20.25
y_lower=0.25; y_upper=20.25
# cells per layer
mx=(x_upper-x_lower)*cells_per_layer;
my=(y_upper-y_lower)*cells_per_layer
mx=int((x_upper-x_lower)*cells_per_layer)
my=int((y_upper-y_lower)*cells_per_layer)
# Initial condition parameters
initial_amplitude=10.
x0=0.25 # Center of initial perturbation
Expand Down
50 changes: 17 additions & 33 deletions src/pyclaw/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ class Dimension(object):
:Initialization:
Required arguments, order:
Required arguments, in order:
- *lower* - (float) Lower extent of dimension
- *upper* - (float) Upper extent of dimension
- *num_cells* - (int) Number of cells
Expand Down Expand Up @@ -578,52 +578,36 @@ def edges_with_ghost(self,num_ghost):
return np.hstack((pre,edges,post))


def __init__(self, *args, **kargs):
def __init__(self, lower, upper, num_cells, name='x',
on_lower_boundary=None,on_upper_boundary=None, units=None):
r"""
Create a Dimension object.
See :class:`Dimension` for full documentation
"""

# ========== Class Data Attributes ===================================
self.name = 'x'
r"""(string) Name of this coordinate dimension (e.g. 'x')"""
self.on_lower_boundary = None
r"""(bool) - Whether the dimension is touching a lower boundary."""
self.on_upper_boundary = None
r"""(bool) - Whether the dimension is touching an upper boundary."""
self.units = None
r"""(string) Corresponding physical units of this dimension (e.g.
'm/s'), ``default = None``"""
if isinstance(lower,basestring):
raise Exception('Passing dimension name as first argument is deprecated. \
Pass it as a keyword argument instead.')

self._edges = None
self._centers = None
self._centers_with_ghost = None
self._edges_with_ghost = None

# Parse args
if isinstance(args[0],basestring):
import warnings
warnings.warn('Passing dimension name as first argument is deprecated. \
Pass it as a keyword argument instead.')
self.name = args[0]
self._lower = float(args[1])
self._upper = float(args[2])
self._num_cells = int(args[3])
else:
self._lower = float(args[0])
self._upper = float(args[1])
self._num_cells = int(args[2])

for (k,v) in kargs.iteritems():
setattr(self,k,v)
self._lower = float(lower)
self._upper = float(upper)
self._num_cells = int(num_cells)
self.name = name
self.on_lower_boundary = on_lower_boundary
self.on_upper_boundary = on_upper_boundary
self.units = units

self._check_validity()

def _check_validity(self):
assert type(self.num_cells) is int, 'Dimension.num_cells must be an integer'
assert type(self.lower) is float, 'Dimension.lower must be a float'
assert type(self.upper) is float, 'Dimension.upper must be a float'
assert isinstance(self.num_cells,int), 'Dimension.num_cells must be an integer; got %s' % type(self.num_cells)
assert isinstance(self.lower,float), 'Dimension.lower must be a float'
assert isinstance(self.upper,float), 'Dimension.upper must be a float'
assert self.num_cells>0, 'Dimension.num_cells must be positive'
assert self.upper > self.lower, 'Dimension.upper must be greater than lower'

Expand Down
2 changes: 1 addition & 1 deletion src/pyclaw/io/ascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def read(solution,frame,path='./',file_prefix='fort',read_aux=False,
[t,num_eqn,nstates,num_aux,num_dim] = read_t(frame,path,file_prefix)

patches = []
n = np.zeros((num_dim))
n = np.zeros((num_dim),dtype=int)
d = np.zeros((num_dim))
lower = np.zeros((num_dim))
# Since we do not have names here, we will construct each patch with
Expand Down
2 changes: 1 addition & 1 deletion src/pyclaw/io/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def read(solution,frame,path='./',file_prefix='fort',read_aux=False,
qdata = np.fromfile(file=b_file, dtype=np.float64)

i_start_patch = 0 # index into qdata for start of next patch
n = np.zeros((num_dim))
n = np.zeros((num_dim),dtype=int)
lower = np.zeros((num_dim))
d = np.zeros((num_dim))
# Since we do not have dimension names here, we will construct
Expand Down
4 changes: 2 additions & 2 deletions src/pyclaw/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,12 +536,12 @@ def read_data_line(inputfile,num_entries=1,data_type=float):
while l==[]: # skip over blank lines
line = inputfile.readline()
l = line.split()
if num_entries == 1: # This is a convenience for calling functions
return data_type(l[0])
val = np.empty(num_entries,data_type)
if num_entries > len(l):
print 'Error in read_data_line: num_entries = ', num_entries
print ' is larger than length of l = ',l
if num_entries == 1: # This is a convenience for calling functions
return data_type(l[0])
val = [data_type(entry) for entry in l[:num_entries]]
return val

Expand Down

0 comments on commit 03443e6

Please sign in to comment.