Skip to content

Commit

Permalink
Merge pull request #3 from uwoseis/modeldependent
Browse files Browse the repository at this point in the history
This should be safe to merge.
  • Loading branch information
bsmithyman committed Nov 12, 2015
2 parents 772d3ed + 01453dc commit 91370b0
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 23 deletions.
25 changes: 3 additions & 22 deletions anemoi/discretization.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@

from .meta import AttributeMapper
from .meta import AttributeMapper, BaseModelDependent
from .solver import DirectSolver
import numpy as np

class BaseDiscretization(AttributeMapper):
class BaseDiscretization(BaseModelDependent):

initMap = {
# Argument Required Rename as ... Store as type
'c': (True, '_c', np.complex128),
'rho': (False, '_rho', np.float64),
'freq': (True, None, np.complex128),
'dx': (False, '_dx', np.float64),
'dz': (False, '_dz', np.float64),
'nx': (True, None, np.int64),
'nz': (True, None, np.int64),
'freeSurf': (False, '_freeSurf', tuple),
'Solver': (False, '_Solver', None),
}

Expand All @@ -28,26 +23,12 @@ def c(self):
@property
def rho(self):
if getattr(self, '_rho', None) is None:
self._rho = 310. * self.c**0.25
self._rho = 310. * self.c**0.25

if isinstance(self._rho, np.ndarray):
return self._rho
else:
return self._rho * np.ones((self.nz, self.nx), dtype=np.float64)

@property
def dx(self):
return getattr(self, '_dx', 1.)

@property
def dz(self):
return getattr(self, '_dz', self.dx)

@property
def freeSurf(self):
if getattr(self, '_freeSurf', None) is None:
self._freeSurf = (False, False, False, False)
return self._freeSurf

@property
def Ainv(self):
Expand Down
78 changes: 77 additions & 1 deletion anemoi/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
import warnings
import numpy as np

class ClassProperty(property):
def __get__(self, cls, owner):
return self.fget.__get__(None, owner)()

class AMMetaClass(type):

def __new__(mcs, name, bases, attrs):
Expand All @@ -12,6 +16,9 @@ def __new__(mcs, name, bases, attrs):
initMap = {}
for baseMap in baseMaps:
initMap.update(baseMap)
for key in initMap:
if initMap[key] is None:
del(initMap[key])

attrs['initMap'] = initMap

Expand All @@ -29,7 +36,7 @@ def __call__(cls, *args, **kwargs):
warnings.simplefilter('ignore')
for key in obj.initMap.keys():
if (key not in systemConfig) and obj.initMap[key][0]:
raise Exception('Class %s requires parameter \'%s\'!'%(cls.__name__, key))
raise ValueError('Class %s requires parameter \'%s\'!'%(cls.__name__, key))
if key in systemConfig:
if obj.initMap[key][2] is None:
typer = lambda x: x
Expand Down Expand Up @@ -96,3 +103,72 @@ class AttributeMapper(object):

def __init__(self, systemConfig):
pass

@ClassProperty
@classmethod
def required(cls):
return set([key for key in cls.initMap if cls.initMap[key][0]])

@ClassProperty
@classmethod
def optional(cls):
return set([key for key in cls.initMap if not cls.initMap[key][0]])


class BaseModelDependent(AttributeMapper):

initMap = {
# Argument Required Rename as ... Store as type
'nx': (True, None, np.int64),
'ny': (False, None, np.int64),
'nz': (True, None, np.int64),
'xorig': (False, '_xorig', np.float64),
'yorig': (False, '_xorig', np.float64),
'zorig': (False, '_zorig', np.float64),
'dx': (False, '_dx', np.float64),
'dy': (False, '_dx', np.float64),
'dz': (False, '_dz', np.float64),
'freeSurf': (False, '_freeSurf', tuple),
}

@property
def xorig(self):
return getattr(self, '_xorig', 0.)

@property
def yorig(self):
if hasattr(self, 'ny'):
return getattr(self, '_yorig', 0.)
else:
raise AttributeError('%s object is not 3D'%(self.__class__.__name__,))

@property
def zorig(self):
return getattr(self, '_zorig', 0.)

@property
def dx(self):
return getattr(self, '_dx', 1.)

@property
def dy(self):
if hasattr(self, 'ny'):
return getattr(self, '_dy', self.dx)
else:
raise AttributeError('%s object is not 3D'%(self.__class__.__name__,))

@property
def dz(self):
return getattr(self, '_dz', self.dx)

@property
def freeSurf(self):
if getattr(self, '_freeSurf', None) is None:
self._freeSurf = (False, False, False, False)
return self._freeSurf

@property
def modelDims(self):
if hasattr(self, 'ny'):
return (self.nz, self.ny, self.nx)
return (self.nz, self.nx)

0 comments on commit 91370b0

Please sign in to comment.