Skip to content

Commit

Permalink
add support for fixed particle weight in SingleSpeciesAnalyzer
Browse files Browse the repository at this point in the history
hope the conversion fix in __getitem__ doesnt break anything
  • Loading branch information
skuschel committed Dec 7, 2014
1 parent 39d95ab commit 3884ee2
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions postpic/analyzer/particles.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ def __getitem__(self, key):
else:
ret = self._dumpreader.getSpecies(self.species, key)
if ret is not None:
ret = np.asfarray(ret, dtype='float64')
if self._compressboollist is not None:
ret = np.float64(ret)
if not isinstance(ret, float) and self._compressboollist is not None:
ret = ret[self._compressboollist] # avoid executing this line too often.
self._cache[key] = ret
# if memomry is low, caching could be skipped entirely.
Expand Down Expand Up @@ -122,17 +122,30 @@ def uncompress(self):
# --- Only very basic functions

def __len__(self): # = number of particles
w = self['weight']
if w is None:
ret = 0
else:
ret = len(w)
# find a valid dataset to count number of paricles
if self._compressboollist is not None:
return len(self._compressboollist)
for key in ['weight', 'x', 'px', 'y', 'py', 'z', 'pz']:
data = self[key]
try:
# len(3) will yield a TypeError, len([3]) returns 1
ret = len(data)
break
except(TypeError):
pass
return ret

# --- These functions are for practical use. Return None if not dumped.

def weight(self): # np.float64(np.array([4.3])) == 4.3 may cause error
return self['weight']
w = self['weight']
# on constant weight, self['weight'] may return a single scalar,
# which is converted to float by __getitem__
if isinstance(w, float):
ret = np.repeat(w, len(self))
else:
ret = w
return ret

def mass(self): # SI
return np.repeat(self._mass, len(self))
Expand Down

0 comments on commit 3884ee2

Please sign in to comment.