Skip to content

Commit

Permalink
Merge 440c692 into b4308b6
Browse files Browse the repository at this point in the history
  • Loading branch information
ketch committed Nov 5, 2014
2 parents b4308b6 + 440c692 commit 163ad20
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 42 deletions.
34 changes: 13 additions & 21 deletions src/petclaw/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ def num_eqn(self):
raise Exception('state.num_eqn has not been set.')
else: return self.q_da.dof

@property
def num_aux(self):
r"""(int) - Number of auxiliary fields"""
if self.aux_da is None: return 0
else: return self.aux_da.dof

@property
def mp(self):
r"""(int) - Number of derived quantities (components of p)"""
Expand Down Expand Up @@ -40,29 +46,16 @@ def mF(self,mF):
self._F_da = self._create_DA(mF)
self.gFVec = self._F_da.createGlobalVector()

@property
def num_aux(self):
r"""(int) - Number of auxiliary fields"""
if self.aux_da is None: return 0
else: return self.aux_da.dof

@property
def q(self):
r"""
Array to store solution (q) values.
Settting state.num_eqn automatically allocates space for q, as does
setting q itself.
Array of solution values.
"""
if self.q_da is None: return 0
shape = self.grid.num_cells
shape.insert(0,self.num_eqn)
q=self.gqVec.getArray().reshape(shape, order = 'F')
return q
return self.gqVec.getArray().reshape(shape, order = 'F')
@q.setter
def q(self,val):
num_eqn = val.shape[0]
if self.gqVec is None: self._init_q_da(num_eqn)
self.gqVec.setArray(val.reshape([-1], order = 'F'))

@property
Expand Down Expand Up @@ -139,7 +132,7 @@ def __init__(self,geom,num_eqn,num_aux=0):
self.patch = geom.patches[0]
else:
raise Exception("""A PetClaw State object must be initialized with
a PyClaw Patch or Domain object.""")
a PetClaw Patch or Domain object.""")

self.aux_da = None
self.q_da = None
Expand Down Expand Up @@ -174,21 +167,21 @@ def _init_aux_da(self,num_aux,num_ghost=0):
Initializes PETSc DA and global & local Vectors for handling the
auxiliary array, aux.
Initializes aux_da, gauxVec and lauxVec.
Initializes aux_da, gauxVec and _aux_local_vector.
"""
self.aux_da = self._create_DA(num_aux,num_ghost)
self.gauxVec = self.aux_da.createGlobalVector()
self.lauxVec = self.aux_da.createLocalVector()
self._aux_local_vector = self.aux_da.createLocalVector()

def _init_q_da(self,num_eqn,num_ghost=0):
r"""
Initializes PETSc DA and Vecs for handling the solution, q.
Initializes q_da, gqVec and lqVec.
Initializes q_da, gqVec and _q_local_vector.
"""
self.q_da = self._create_DA(num_eqn,num_ghost)
self.gqVec = self.q_da.createGlobalVector()
self.lqVec = self.q_da.createLocalVector()
self._q_local_vector = self.q_da.createLocalVector()

def _create_DA(self,dof,num_ghost=0):
r"""Returns a PETSc DA and associated global Vec.
Expand Down Expand Up @@ -227,7 +220,6 @@ def _create_DA(self,dof,num_ghost=0):

return DA


def get_qbc_from_q(self,num_ghost,qbc):
"""
Returns q with ghost cells attached, by accessing the local vector.
Expand Down
58 changes: 37 additions & 21 deletions src/pyclaw/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,39 @@
David I. Ketcheson -- Initial version (June 2011)
"""

import numpy as np

class State(object):
r"""
A PyClaw State object contains the current state on a particular patch,
including the unkowns q, the time t, and the auxiliary coefficients aux.
Both q and aux are initialized to None. They cannot be accessed until
num_eqn and num_aux (respectively) are set.
The variables num_eqn and num_aux determine the length of the first
dimension of the q and aux arrays.
:State Data:
The arrays :attr:`q`, and :attr:`aux` have variable
extents based on the patch dimensions and the values of
:attr:`num_eqn` and :attr:`num_aux`. Note that these are initialy set to
None and later set to appropriately sized empty numpy arrays when
:attr:`num_eqn` and :attr:`num_aux` are set.
:attr:`num_eqn` and :attr:`num_aux`.
To instantiate a State, we first need a patch:
A State object is automatically created upon instantiation of a Solution object
from a Domain object:
>>> from clawpack import pyclaw
>>> x = pyclaw.Dimension('x',0.0,1.0,100)
>>> domain = pyclaw.Domain(x)
>>> num_eqn = 1
>>> solution = pyclaw.Solution(num_eqn,domain)
>>> print solution.state
PyClaw State object
Patch dimensions: [100]
Time t=0.0
Number of conserved quantities: 1
<BLANKLINE>
A State lives on a Patch, and can be instantiated directly
by first creating a Patch:
>>> x = pyclaw.Dimension('x',0.,1.,100)
>>> patch = pyclaw.Patch((x))
Expand Down Expand Up @@ -79,6 +91,10 @@ def num_aux(self):
if self.aux is not None: return self.aux.shape[0]
else: return 0

@property
def grid(self):
return self.patch.grid

@property
def mp(self):
r"""(int) - Number of derived quantities"""
Expand All @@ -91,10 +107,6 @@ def mp(self,mp):
else:
self.p = self.new_array(mp)

@property
def grid(self):
return self.patch.grid

@property
def mF(self):
r"""(int) - Number of output functionals"""
Expand Down Expand Up @@ -159,8 +171,8 @@ def is_valid(self):
Checks to see if this state is valid
The state is declared valid based on the following criteria:
- :attr:`q` is not None
- :attr:`num_eqn` > 0
- :attr:`q` is Fortran contiguous
- :attr:`aux` is Fortran contiguous
A debug logger message will be sent documenting exactly what was not
valid.
Expand All @@ -175,6 +187,10 @@ def is_valid(self):
if not self.q.flags['F_CONTIGUOUS']:
logger.debug('q array is not Fortran contiguous.')
valid = False
if self.aux is not None:
if not self.aux.flags['F_CONTIGUOUS']:
logger.debug('q array is not Fortran contiguous.')
valid = False
return valid

def set_cparam(self,fortran_module):
Expand Down Expand Up @@ -205,19 +221,18 @@ def set_num_ghost(self,num_ghost):
"""
pass


def set_q_from_qbc(self,num_ghost,qbc):
"""
Set the value of q using the array qbc. This is called after
qbc is updated by the solver.
Set the value of q using the array qbc. Typically this is called
after qbc has been updated by the solver.
"""

patch = self.patch
if patch.num_dim == 1:
num_dim = self.patch.num_dim
if num_dim == 1:
self.q = qbc[:,num_ghost:-num_ghost]
elif patch.num_dim == 2:
elif num_dim == 2:
self.q = qbc[:,num_ghost:-num_ghost,num_ghost:-num_ghost]
elif patch.num_dim == 3:
elif num_dim == 3:
self.q = qbc[:,num_ghost:-num_ghost,num_ghost:-num_ghost,num_ghost:-num_ghost]
else:
raise Exception("Assumption (1 <= num_dim <= 3) violated.")
Expand Down Expand Up @@ -277,7 +292,6 @@ def get_auxbc_from_aux(self,num_ghost,auxbc):
def __copy__(self):
return self.__class__(self)


def __deepcopy__(self,memo={}):
import copy
result = self.__class__(copy.deepcopy(self.patch),self.num_eqn,self.num_aux)
Expand All @@ -295,9 +309,11 @@ def __deepcopy__(self,memo={}):
return result

def sum_F(self,i):
import numpy as np
return np.sum(np.abs(self.F[i,...]))

def new_array(self,dof):
import numpy as np
if dof==0: return None
shape = [dof]
shape.extend(self.grid.num_cells)
Expand Down

0 comments on commit 163ad20

Please sign in to comment.