def test_is_planar_3d(self): pts = np.array( [ [0., 1., 0., 4. / 7.], [0., 1., 1., 0.], [5. / 8., 7. / 8., 7. / 4., 1. / 8.], ] ) self.assertTrue(cg.is_planar(pts))
def _create_embedded_2d_grid(loc_coord, glob_id): """ Create a 2d grid that is embedded in a 3d grid. """ loc_center = np.mean(loc_coord, axis=1).reshape((-1, 1)) loc_coord -= loc_center # Check that the points indeed form a line assert cg.is_planar(loc_coord) # Find the tangent of the line # Projection matrix rot = cg.project_plane_matrix(loc_coord) loc_coord_2d = rot.dot(loc_coord) # The points are now 2d along two of the coordinate axis, but we # don't know which yet. Find this. sum_coord = np.sum(np.abs(loc_coord_2d), axis=1) active_dimension = np.logical_not(np.isclose(sum_coord, 0)) # Check that we are indeed in 2d assert np.sum(active_dimension) == 2 # Sort nodes, and create grid coord_2d = loc_coord_2d[active_dimension] sort_ind = np.lexsort((coord_2d[0], coord_2d[1])) sorted_coord = coord_2d[:, sort_ind] sorted_coord = np.round(sorted_coord * 1e10) / 1e10 unique_x = np.unique(sorted_coord[0]) unique_y = np.unique(sorted_coord[1]) # assert unique_x.size == unique_y.size g = structured.TensorGrid(unique_x, unique_y) assert np.all(g.nodes[0:2] - sorted_coord == 0) # Project back to active dimension nodes = np.zeros(g.nodes.shape) nodes[active_dimension] = g.nodes[0:2] g.nodes = nodes # Project back again to 3d coordinates irot = rot.transpose() g.nodes = irot.dot(g.nodes) g.nodes += loc_center # Add mapping to global point numbers g.global_point_ind = glob_id[sort_ind] return g
def grid(g): """ Sanity check for the grid. General method which apply the following: - check if the face normals are actually normal to the faces - check if a bidimensional grid is planar Args: g (grid): Grid, or a subclass, with geometry fields computed. How to use: import core.grids.check as check check.grid(g) """ if g.dim == 1: assert cg.is_collinear(g.nodes) face_normals_1d(g) if g.dim == 2: assert cg.is_planar(g.nodes) if g.dim != 1: face_normals(g)
def plot_over_line(gb, pts, name, tol): values = np.zeros(pts.shape[1]) is_found = np.zeros(pts.shape[1], dtype=np.bool) for g, d in gb: if g.dim < gb.dim_max(): continue if not cg.is_planar(np.hstack((g.nodes, pts)), tol=1e-4): continue faces_cells, _, _ = sps.find(g.cell_faces) nodes_faces, _, _ = sps.find(g.face_nodes) normal = cg.compute_normal(g.nodes) for c in np.arange(g.num_cells): loc = slice(g.cell_faces.indptr[c], g.cell_faces.indptr[c + 1]) pts_id_c = np.array([ nodes_faces[g.face_nodes.indptr[f]:g.face_nodes.indptr[f + 1]] for f in faces_cells[loc] ]).T pts_id_c = sort_points.sort_point_pairs(pts_id_c)[0, :] pts_c = g.nodes[:, pts_id_c] mask = np.where(np.logical_not(is_found))[0] if mask.size == 0: break check = np.zeros(mask.size, dtype=np.bool) last = False for i, pt in enumerate(pts[:, mask].T): check[i] = cg.is_point_in_cell(pts_c, pt) if last and not check[i]: break is_found[mask] = check values[mask[check]] = d[name][c] return values
def test_is_planar_3d( self ): pts = np.array( [ [ 0., 1., 0., 4./7. ], [ 0., 1., 1., 0. ], [ 5./8., 7./8., 7./4., 1./8. ] ] ) assert cg.is_planar( pts )
def test_is_planar_2d( self ): pts = np.array( [ [ 0., 2., -1. ], [ 0., 4., 2. ], [ 2., 2., 2. ] ] ) assert cg.is_planar( pts )