Commit 30623565 authored by theos's avatar theos
Browse files

Reimplemented Field class.

parent 4424bdee
This diff is collapsed.
......@@ -13,7 +13,18 @@ class FieldType(object):
self._dtype = np.dtype(dtype)
self._dof = self._get_dof()
def __hash__(self):
# Extract the identifying parts from the vars(self) dict.
result_hash = 0
for (key, item) in vars(self).items():
result_hash ^= item.__hash__() ^ int(hash(key)/117)
return result_hash
def __eq__(self, x):
if isinstance(x, type(self)):
return hash(self) == hash(x)
else:
return False
@property
def shape(self):
......@@ -24,17 +35,8 @@ class FieldType(object):
return self._dtype
@property
def dof(self):
return self._dof
def _get_dof(self):
if issubclass(self.dtype.type, np.complexfloating):
multiplicator = 2
else:
multiplicator = 1
dof = multiplicator*reduce(lambda x, y: x*y, self.shape)
return dof
def dim(self):
raise NotImplementedError
def process(self, method_name, array, inplace=True, **kwargs):
try:
......@@ -49,8 +51,5 @@ class FieldType(object):
return result_array
def complement_cast(self, x, axis=None):
def complement_cast(self, x, axes=None):
return x
def dot_contraction(self, x, axes):
raise NotImplementedError
......@@ -5,5 +5,6 @@ from base_field_type import FieldType
class FieldArray(FieldType):
def dot_contraction(self, x, axes):
return x.sum(axis=axes)
@property
def dim(self):
return reduce(lambda x, y: x*y, self.shape)
......@@ -177,27 +177,6 @@ class LMSpace(Space):
mmax = self.paradict['mmax']
return (np.int((mmax + 1) * (lmax + 1) - ((mmax + 1) * mmax) // 2),)
@property
def dof(self):
"""
Computes the number of degrees of freedom of the space, taking into
account symmetry constraints and complex-valuedness.
Returns
-------
dof : int
Number of degrees of freedom of the space.
Notes
-----
The number of degrees of freedom is reduced due to the hermitian
symmetry, which is assumed for the spherical harmonics components.
"""
# dof = 2*dim-(lmax+1) = (lmax+1)*(2*mmax+1)*(mmax+1)*mmax
lmax = self.paradict['lmax']
mmax = self.paradict['mmax']
dof = np.int((lmax + 1) * (2 * mmax + 1) - (mmax + 1) * mmax)
return dof
@property
def meta_volume(self):
......
......@@ -63,7 +63,9 @@ class PowerSpace(Space):
"ERROR: There is no k_array implementation for PowerSpace."))
def calculate_power_spectrum(self, x, axes=None):
fieldabs = abs(x)**2
fieldabs = abs(x)
fieldabs **= 2
pindex = self.paradict['pindex']
if axes is not None:
pindex = self._shape_up_pindex(
......@@ -81,6 +83,7 @@ class PowerSpace(Space):
rho = rho.reshape(new_rho_shape)
power_spectrum /= rho
power_spectrum **= 0.5
return power_spectrum
def _shape_up_pindex(self, pindex, target_shape, target_strategy, axes):
......
......@@ -237,23 +237,6 @@ class Space(object):
raise NotImplementedError(about._errors.cstring(
"ERROR: There is no generic dim for the Space base class."))
@property
def dof(self):
"""
Computes the number of degrees of freedom of the space, i.e./ the
number of points for real-valued fields and twice that number for
complex-valued fields.
Returns
-------
dof : int
Number of degrees of freedom of the space.
"""
dof = self.dim
if issubclass(self.dtype.type, np.complexfloating):
dof = dof * 2
return dof
@property
def total_volume(self):
raise NotImplementedError(about._errors.cstring(
......@@ -281,25 +264,6 @@ class Space(object):
"""
raise NotImplementedError
def dot_contraction(self, x, axes):
"""
Computes the discrete inner product of two given arrays of field
values.
Parameters
----------
x : numpy.ndarray
First array
y : numpy.ndarray
Second array
Returns
-------
dot : scalar
Inner product of the two arrays.
"""
return x.sum(axis=axes)
def compute_k_array(self, distribution_strategy):
raise NotImplementedError(about._errors.cstring(
"ERROR: There is no generic k_array for Space base class."))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment