From f5714e198711043638962e69368fc821f30f23ba Mon Sep 17 00:00:00 2001 From: Michael Krayer Date: Fri, 6 Aug 2021 22:02:35 +0200 Subject: [PATCH] clipping and thresholding for particles; improvements for field --- field.py | 20 ++++++++++++++++---- helper.py | 9 ++++++++- particle.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 73 insertions(+), 8 deletions(-) diff --git a/field.py b/field.py index 2331ebc..fcb6f1b 100644 --- a/field.py +++ b/field.py @@ -214,7 +214,11 @@ class Field3d: data = self.data[sl] return Field3d(data,origin,spacing,deep=deep) - def clip(self,position,axis,invert=False,deep=False): + def clip(self,position,axis,invert=False,deep=False,is_relative=False): + '''Extracts a subfield by clipping with a plane with normal pointing + direction specified by 'axis'.''' + if is_relative: + coord = self.origin[axis] + coord*self.dim(axis=axis)*self.spacing[axis] idx_clip = self.nearest_gridpoint(coord,axis=axis,lower=True) sl = 3*[slice(None)] origin_ = self.origin @@ -228,21 +232,29 @@ class Field3d: return Field3d(data_,origin_,spacing_,deep=deep) def clip_box(self,bounds,deep=False,is_relative=False): + '''Extracts a subfield by clipping with a box.''' if is_relative: bounds = tuple(self.origin[ii//2] + bounds[ii]*self.dim(ii//2)*self.spacing[ii//2] for ii in range(6)) - print(bounds) idx_lo = self.nearest_gridpoint((bounds[0],bounds[2],bounds[4]),lower=True) idx_hi = self.nearest_gridpoint((bounds[1],bounds[3],bounds[5]),lower=True) - origin_ = self.origin - spacing_ = self.spacing idx_lo = tuple(0 if idx_lo[axis]<0 else idx_lo[axis] for axis in range(3)) idx_hi = tuple(0 if idx_hi[axis]<0 else idx_hi[axis] for axis in range(3)) sl_ = tuple([slice(idx_lo[0],idx_hi[0]+1), slice(idx_lo[1],idx_hi[1]+1), slice(idx_lo[2],idx_hi[2]+1)]) + origin_ = tuple(self.origin[axis]+idx_lo[axis]*self.spacing[axis] for axis in range(3)) + spacing_ = self.spacing data_ = self.data[sl_] return Field3d(data_,origin_,spacing_,deep=deep) + def threshold(self,val,invert=False): + '''Returns a binary array indicating which grid points are above, + or in case of invert=True below, a given threshold.''' + if invert: + return self.data>=val + else: + return self.data=bounds[0],self.attr['x']<=bounds[1]), + np.logical_and(self.attr['y']>=bounds[2],self.attr['y']<=bounds[3])), + np.logical_and(self.attr['z']>=bounds[4],self.attr['z']<=bounds[5])) + if invert: + np.logical_not(li,li) + return self._slice(li) + + def threshold(self,key,val,invert=False): + '''Returns particles for which specified attribute is above, + or in case invert=True below, the specified threshold.''' + assert key in self.attr, "'key' not found in attr." + if invert: + li = self.attr[key]=val + return self._slice(li) + def to_vtk(self,deep=False): import pyvista as pv position = np.vstack([self.attr[key] for key in ('x','y','z')]).transpose() @@ -169,6 +214,7 @@ class Particles: for key in self.attr: mesh[key] = self.attr[key] return mesh + def glyph(self,theta_resolution=30,phi_resolution=30,deep=False): import pyvista as pv assert self.has_attribute('r'), "Attribute 'r' required."