def isInside(self, pts, locType='N'): """ Determines if a set of points are inside a mesh. :param numpy.ndarray pts: Location of points to test :rtype: numpy.ndarray :return: inside, numpy array of booleans """ pts = utils.asArray_N_x_Dim(pts, self.dim) tensors = self.getTensor(locType) if locType == 'N' and self._meshType == 'CYL': # NOTE: for a CYL mesh we add a node to check if we are inside in # the radial direction! tensors[0] = np.r_[0., tensors[0]] tensors[1] = np.r_[tensors[1], 2.0*np.pi] inside = np.ones(pts.shape[0], dtype=bool) for i, tensor in enumerate(tensors): TOL = np.diff(tensor).min() * 1.0e-10 inside = ( inside & (pts[:, i] >= tensor.min()-TOL) & (pts[:, i] <= tensor.max()+TOL) ) return inside
def point2index(self, locs): locs = utils.asArray_N_x_Dim(locs, self.dim) inds = np.empty(locs.shape[0], dtype=np.int64) for ind, loc in enumerate(locs): inds[ind] = self._get_containing_cell_index(loc) return inds
def test_asArray_N_x_Dim(self): true = np.array([[1, 2, 3]]) listArray = asArray_N_x_Dim([1, 2, 3], 3) self.assertTrue(np.all(true == listArray)) self.assertTrue(true.shape == listArray.shape) listArray = asArray_N_x_Dim(np.r_[1, 2, 3], 3) self.assertTrue(np.all(true == listArray)) self.assertTrue(true.shape == listArray.shape) listArray = asArray_N_x_Dim(np.array([[1, 2, 3.0]]), 3) self.assertTrue(np.all(true == listArray)) self.assertTrue(true.shape == listArray.shape) true = np.array([[1, 2], [4, 5]]) listArray = asArray_N_x_Dim([[1, 2], [4, 5]], 2) self.assertTrue(np.all(true == listArray)) self.assertTrue(true.shape == listArray.shape)
def _getInterpolationMat(self, loc, locType='CC', zerosOutside=False): """ Produces interpolation matrix :param numpy.ndarray loc: Location of points to interpolate to :param str locType: What to interpolate (see below) :rtype: scipy.sparse.csr_matrix :return: M, the interpolation matrix locType can be:: 'Ex' -> x-component of field defined on edges 'Ey' -> y-component of field defined on edges 'Ez' -> z-component of field defined on edges 'Fx' -> x-component of field defined on faces 'Fy' -> y-component of field defined on faces 'Fz' -> z-component of field defined on faces 'N' -> scalar field defined on nodes 'CC' -> scalar field defined on cell centers 'CCVx' -> x-component of vector field defined on cell centers 'CCVy' -> y-component of vector field defined on cell centers 'CCVz' -> z-component of vector field defined on cell centers """ loc = utils.asArray_N_x_Dim(loc, self.dim) if zerosOutside is False: assert np.all(self.isInside(loc)), "Points outside of mesh" else: indZeros = np.logical_not(self.isInside(loc)) loc[indZeros, :] = np.array([ v.mean() for v in self.getTensor('CC') ]) if locType in ['Fx', 'Fy', 'Fz', 'Ex', 'Ey', 'Ez']: ind = {'x': 0, 'y': 1, 'z': 2}[locType[1]] assert self.dim >= ind, 'mesh is not high enough dimension.' nF_nE = self.vnF if 'F' in locType else self.vnE components = [utils.spzeros(loc.shape[0], n) for n in nF_nE] components[ind] = utils.interpmat(loc, *self.getTensor(locType)) # remove any zero blocks (hstack complains) components = [comp for comp in components if comp.shape[1] > 0] Q = sp.hstack(components) elif locType in ['CC', 'N']: Q = utils.interpmat(loc, *self.getTensor(locType)) elif locType in ['CCVx', 'CCVy', 'CCVz']: Q = utils.interpmat(loc, *self.getTensor('CC')) Z = utils.spzeros(loc.shape[0], self.nC) if locType == 'CCVx': Q = sp.hstack([Q, Z, Z]) elif locType == 'CCVy': Q = sp.hstack([Z, Q, Z]) elif locType == 'CCVz': Q = sp.hstack([Z, Z, Q]) else: raise NotImplementedError( 'getInterpolationMat: locType==' + locType + ' and mesh.dim==' + str(self.dim) ) if zerosOutside: Q[indZeros, :] = 0 return Q.tocsr()