def is_inside(self, pts, location_type="nodes", **kwargs): """ 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 """ if "locType" in kwargs: warnings.warn( "The locType keyword argument has been deprecated, please use location_type. " "This will be removed in discretize 1.0.0", DeprecationWarning, ) location_type = kwargs["locType"] pts = as_array_n_by_dim(pts, self.dim) tensors = self.get_tensor(location_type) if location_type[0].lower() == "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.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 is_inside(self, pts, location_type="nodes", **kwargs): """Determine which points lie within the mesh For an arbitrary set of points, **is_indside** returns a boolean array identifying which points lie within the mesh. Parameters ---------- pts : (n_pts, dim) numpy.ndarray Locations of input points. Must have same dimension as the mesh. location_type : str, optional Use *N* to determine points lying within the cluster of mesh nodes. Use *CC* to determine points lying within the cluster of mesh cell centers. Returns ------- (n_pts) numpy.ndarray of bool Boolean array identifying points which lie within the mesh """ if "locType" in kwargs: warnings.warn( "The locType keyword argument has been deprecated, please use location_type. " "This will be removed in discretize 1.0.0", DeprecationWarning, ) location_type = kwargs["locType"] pts = as_array_n_by_dim(pts, self.dim) tensors = self.get_tensor(location_type) if location_type[0].lower() == "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.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): """Finds cells that contain the given points. Returns an array of index values of the cells that contain the given points Parameters ---------- locs: array_like of shape (N, dim) points to search for the location of Returns ------- numpy.array of integers of length(N) Cell indices that contain the points """ locs = as_array_n_by_dim(locs, self.dim) inds = self._get_containing_cell_indexes(locs) return inds
def get_interpolation_matrix( self, locs, location_type="CC", zeros_outside=False, **kwargs ): if "locType" in kwargs: warnings.warn( "The locType keyword argument has been deprecated, please use location_type. " "This will be removed in discretize 1.0.0", FutureWarning, ) location_type = kwargs["locType"] if "zerosOutside" in kwargs: warnings.warn( "The zerosOutside keyword argument has been deprecated, please use zeros_outside. " "This will be removed in discretize 1.0.0", FutureWarning, ) zeros_outside = kwargs["zerosOutside"] locs = as_array_n_by_dim(locs, self.dim) if location_type not in ["N", "CC", "Ex", "Ey", "Ez", "Fx", "Fy", "Fz"]: raise Exception( "location_type must be one of N, CC, Ex, Ey, Ez, Fx, Fy, or Fz" ) if self.dim == 2 and location_type in ["Ez", "Fz"]: raise Exception("Unable to interpolate from Z edges/face in 2D") locs = np.require(np.atleast_2d(locs), dtype=np.float64, requirements="C") if location_type == "N": Av = self._getNodeIntMat(locs, zeros_outside) elif location_type in ["Ex", "Ey", "Ez"]: Av = self._getEdgeIntMat(locs, zeros_outside, location_type[1]) elif location_type in ["Fx", "Fy", "Fz"]: Av = self._getFaceIntMat(locs, zeros_outside, location_type[1]) elif location_type in ["CC"]: Av = self._getCellIntMat(locs, zeros_outside) return Av
def get_interpolation_matrix( self, locs, location_type="CC", zeros_outside=False, **kwargs ): """Produces interpolation matrix Parameters ---------- loc : numpy.ndarray Location of points to interpolate to location_type: str What to interpolate location_type 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 Returns ------- scipy.sparse.csr_matrix M, the interpolation matrix """ if "locType" in kwargs: warnings.warn( "The locType keyword argument has been deprecated, please use location_type. " "This will be removed in discretize 1.0.0", DeprecationWarning, ) location_type = kwargs["locType"] if "zerosOutside" in kwargs: warnings.warn( "The zerosOutside keyword argument has been deprecated, please use zeros_outside. " "This will be removed in discretize 1.0.0", DeprecationWarning, ) zeros_outside = kwargs["zerosOutside"] locs = as_array_n_by_dim(locs, self.dim) if location_type not in ["N", "CC", "Ex", "Ey", "Ez", "Fx", "Fy", "Fz"]: raise Exception("location_type must be one of N, CC, Ex, Ey, Ez, Fx, Fy, or Fz") if self.dim == 2 and location_type in ["Ez", "Fz"]: raise Exception("Unable to interpolate from Z edges/face in 2D") locs = np.require(np.atleast_2d(locs), dtype=np.float64, requirements="C") if location_type == "N": Av = self._getNodeIntMat(locs, zeros_outside) elif location_type in ["Ex", "Ey", "Ez"]: Av = self._getEdgeIntMat(locs, zeros_outside, location_type[1]) elif location_type in ["Fx", "Fy", "Fz"]: Av = self._getFaceIntMat(locs, zeros_outside, location_type[1]) elif location_type in ["CC"]: Av = self._getCellIntMat(locs, zeros_outside) return Av
def _getInterpolationMat( self, loc, location_type="cell_centers", zeros_outside=False ): """Produces interpolation matrix Parameters ---------- loc : numpy.ndarray Location of points to interpolate to location_type: str, optional What to interpolate location_type can be:: 'Ex', 'edges_x' -> x-component of field defined on x edges 'Ey', 'edges_y' -> y-component of field defined on y edges 'Ez', 'edges_z' -> z-component of field defined on z edges 'Fx', 'faces_x' -> x-component of field defined on x faces 'Fy', 'faces_y' -> y-component of field defined on y faces 'Fz', 'faces_z' -> z-component of field defined on z faces 'N', 'nodes' -> scalar field defined on nodes 'CC', 'cell_centers' -> scalar field defined on cell centers 'CCVx', 'cell_centers_x' -> x-component of vector field defined on cell centers 'CCVy', 'cell_centers_y' -> y-component of vector field defined on cell centers 'CCVz', 'cell_centers_z' -> z-component of vector field defined on cell centers Returns ------- scipy.sparse.csr_matrix M, the interpolation matrix """ loc = as_array_n_by_dim(loc, self.dim) if not zeros_outside: if not np.all(self.is_inside(loc)): raise ValueError("Points outside of mesh") else: indZeros = np.logical_not(self.is_inside(loc)) loc[indZeros, :] = np.array([v.mean() for v in self.get_tensor("CC")]) location_type = self._parse_location_type(location_type) if location_type in [ "faces_x", "faces_y", "faces_z", "edges_x", "edges_y", "edges_z", ]: ind = {"x": 0, "y": 1, "z": 2}[location_type[-1]] if self.dim < ind: raise ValueError("mesh is not high enough dimension.") if "f" in location_type.lower(): items = (self.nFx, self.nFy, self.nFz)[: self.dim] else: items = (self.nEx, self.nEy, self.nEz)[: self.dim] components = [spzeros(loc.shape[0], n) for n in items] components[ind] = interpolation_matrix(loc, *self.get_tensor(location_type)) # remove any zero blocks (hstack complains) components = [comp for comp in components if comp.shape[1] > 0] Q = sp.hstack(components) elif location_type in ["cell_centers", "nodes"]: Q = interpolation_matrix(loc, *self.get_tensor(location_type)) elif location_type in ["cell_centers_x", "cell_centers_y", "cell_centers_z"]: Q = interpolation_matrix(loc, *self.get_tensor("CC")) Z = spzeros(loc.shape[0], self.nC) if location_type[-1] == "x": Q = sp.hstack([Q, Z, Z]) elif location_type[-1] == "y": Q = sp.hstack([Z, Q, Z]) elif location_type[-1] == "z": Q = sp.hstack([Z, Z, Q]) else: raise NotImplementedError( "getInterpolationMat: location_type==" + location_type + " and mesh.dim==" + str(self.dim) ) if zeros_outside: Q[indZeros, :] = 0 return Q.tocsr()
def point2index(self, locs): locs = as_array_n_by_dim(locs, self.dim) inds = self._get_containing_cell_indexes(locs) return inds