def test_add_selective_node_props(self): gb = pp.GridBucket() g1 = MockGrid() g2 = MockGrid() gb.add_nodes(g1) gb.add_nodes(g2) p1 = "a" p2 = "b" pboth = "c" # Add by single grid gb.add_node_props(p1, g1) # Add by list gb.add_node_props(p2, [g2]) # add by list with two items gb.add_node_props(pboth, [g1, g2]) for g, d in gb: self.assertTrue(pboth in d.keys()) if g == g1: self.assertTrue(p1 in d.keys()) self.assertTrue(not p2 in d.keys()) else: self.assertTrue(p2 in d.keys()) self.assertTrue(not p1 in d.keys())
def test_add_selective_node_props(self): gb = pp.GridBucket() g1 = MockGrid() g2 = MockGrid() gb.add_nodes(g1) gb.add_nodes(g2) p1 = 'a' p2 = 'b' pboth = 'c' # Add by single grid gb.add_node_props(p1, g1) # Add by list gb.add_node_props(p2, [g2]) # add by list with two items gb.add_node_props(pboth, [g1, g2]) for g, d in gb: assert pboth in d.keys() if g == g1: assert p1 in d.keys() assert not p2 in d.keys() else: assert p2 in d.keys() assert not p1 in d.keys()
def test_set_get_edge_props(self): gb = pp.GridBucket() g1 = MockGrid() g2 = MockGrid() g3 = MockGrid() gb.add_nodes(g1) gb.add_nodes(g2) gb.add_nodes(g3) gb.add_edge([g1, g2], None) gb.add_edge([g2, g3], None) d = {"a": 1, "b": 2, "c": 3} keys = d.keys() vals = d.values() pairs = [[g1, g2], [g2, g3]] for k, v in zip(keys, vals): gb.set_edge_prop(pairs[0], k, v) # Obtain all keys, check that we have them all all_keys = gb.edge_props(pairs[0]) self.assertTrue(all([k in all_keys.keys() for k in keys])) all_keys = gb.edge_props(pairs[0][::-1]) self.assertTrue(all([k in all_keys.keys() for k in keys])) # The other edge has no properties, Python should raise KeyError self.assertRaises(KeyError, gb.edge_props, edge=pairs[1], key="a") # Try a non-existing edge, the method itself should raise KeyError self.assertRaises(KeyError, gb.edge_props, edge=[g1, g3], key="a")
def test_node_edge_iterators(self): gb = pp.GridBucket() g1 = MockGrid(1) g2 = MockGrid(2) g3 = MockGrid(3) gb.add_nodes(g1) gb.add_nodes(g2) gb.add_nodes(g3) gb.add_edge([g1, g2], None) gb.add_edge([g2, g3], None) # First test traversal by gb.__iter__ found = {g1: False, g2: False, g3: False} for g, _ in gb: found[g] = True self.assertTrue(all([v for v in list(found.values())])) # Next, use the node() function found = {g1: False, g2: False, g3: False} for g, _ in gb.nodes(): found[g] = True self.assertTrue(all([v for v in list(found.values())])) # Finally, check the edges found = {(g2, g1): False, (g3, g2): False} for e, _ in gb.edges(): found[e] = True self.assertTrue(all([v for v in list(found.values())]))
def test_update_nodes(self): gb = pp.GridBucket() g1 = MockGrid() g2 = MockGrid() gb.add_nodes(g1) gb.add_nodes(g2) gb.add_edge([g1, g2], None) d = {"a": 1, "b": 2} keys = d.keys() vals = d.values() for k, v in zip(keys, vals): gb.set_edge_prop([g1, g2], k, v) gb.set_node_prop(g1, k, v) g3 = MockGrid() gb.update_nodes({g1: g3}) # Check that the new grid and edge inherited data for k, v in zip(keys, vals): v2 = gb.node_props(g3, k) self.assertTrue(v == v2) v2 = gb.edge_props([g2, g3], k) self.assertTrue(v == v2) # g1 is no longer associated with gb self.assertRaises(KeyError, gb.node_props, g1, "a")
def multilayer_grid_bucket(gb): # we assume conforming grids and no 0d grids gb_new = pp.GridBucket() gb_new.add_nodes([g for g, _ in gb]) for e, d in gb.edges(): mg = d["mortar_grid"] gs, gm = gb.nodes_of_edge(e) # we construct the grid for the new layer g_new = unite_grids([g for g in mg.side_grids.values()]) # update the names gs.name += ["fracture"] g_new.name += ["layer"] # add the new grid to the grid bucket gb_new.add_nodes(g_new) # we store the master-grid with the new layer grid with the # mortar mapping from the original edge edge = [gm, g_new] gb_new.add_edge(edge, None) mg1 = copy.deepcopy(mg) # the slave to mortar needs to be changed mg1._slave_to_mortar_int = sps.identity(g_new.num_cells, format="csc") gb_new.set_edge_prop(edge, "mortar_grid", mg1) # we store the slave-grid with the new layer grid with the # mortar mapping from the original edge edge = [g_new, gs] gb_new.add_edge(edge, None) mg2 = copy.deepcopy(mg) # the master to mortar needs to be changed mg2._master_to_mortar_int = sps.identity(g_new.num_cells, format="csc") gb_new.set_edge_prop(edge, "mortar_grid", mg2) gb_new.assign_node_ordering() # identification of layer and fracture for g, d in gb_new: d.update({pp.STATE: {}}) # save the identification of the fracture if "fracture" in g.name: d[pp.STATE]["fracture"] = np.ones(g.num_cells) d[pp.STATE]["layer"] = np.zeros(g.num_cells) # save the identification of the layer elif "layer" in g.name: d[pp.STATE]["fracture"] = np.zeros(g.num_cells) half_cells = int(g.num_cells / 2) d[pp.STATE]["layer"] = np.hstack((np.ones(half_cells), 2 * np.ones(half_cells))) # save zero for the other cases else: d[pp.STATE]["fracture"] = np.zeros(g.num_cells) d[pp.STATE]["layer"] = np.zeros(g.num_cells) return gb_new
def test_node_neighbor_with_dim(self): # Test node neighbors, using dim keywords gb = pp.GridBucket() g1 = MockGrid(1) g2 = MockGrid(2) g3 = MockGrid(3) gb.add_nodes(g1) gb.add_nodes(g2) gb.add_nodes(g3) gb.add_edge([g1, g2], None) gb.add_edge([g2, g3], None) neigh_1 = gb.node_neighbors(g1, only_higher=True) self.assertTrue(neigh_1.size == 1) self.assertTrue(neigh_1[0] == g2) neigh_1 = gb.node_neighbors(g1, only_lower=True) self.assertTrue(neigh_1.size == 0) neigh_2 = gb.node_neighbors(g2, only_higher=True) self.assertTrue(neigh_2.size == 1) self.assertTrue(neigh_2[0] == g3) neigh_2 = gb.node_neighbors(g2, only_lower=True) self.assertTrue(neigh_2.size == 1) self.assertTrue(neigh_2[0] == g1) self.assertRaises(ValueError, gb.node_neighbors, g1, True, True)
def test_add_selective_edge_props(self): gb = pp.GridBucket() g1 = MockGrid() g2 = MockGrid() g3 = MockGrid() gb.add_nodes(g1) gb.add_nodes(g2) gb.add_nodes(g3) gb.add_edge([g1, g2], None) gb.add_edge([g2, g3], None) p1 = "a" p2 = "b" pboth = "c" # Add by single grid gb.add_edge_props(p1, [[g1, g2]]) # Add by list gb.add_edge_props(p2, [[g2, g3]]) # add by list with two items gb.add_edge_props(pboth, [[g1, g2], [g2, g3]]) # Try to add test to non-existing edge. Should give error self.assertRaises(KeyError, gb.add_edge_props, pboth, [[g1, g3]]) for g, d in gb.edges(): self.assertTrue(pboth in d.keys()) if g1 in g and g2 in g: self.assertTrue(p1 in d.keys()) self.assertTrue(not p2 in d.keys()) else: self.assertTrue(p2 in d.keys()) self.assertTrue(not p1 in d.keys())
def generate_2d_grid(self, n, xmax, ymax): g1 = pp.CartGrid([xmax * n, ymax * n], physdims=[xmax, ymax]) g1.compute_geometry() gb = pp.GridBucket() gb.add_nodes(g1) tol = 1e-6 left_faces = np.argwhere(g1.face_centers[1] > ymax - tol).ravel() right_faces = np.argwhere(g1.face_centers[1] < 0 + tol).ravel() val = np.ones(left_faces.size, dtype=np.bool) shape = [g1.num_faces, g1.num_faces] face_faces = sps.coo_matrix((val, (right_faces, left_faces)), shape=shape) gb.add_edge((g1, g1), face_faces) mg = pp.TensorGrid(np.linspace(0, xmax, n + 1)) mg.nodes[1] = ymax mg.compute_geometry() d_e = gb.edge_props((g1, g1)) d_e["mortar_grid"] = pp.BoundaryMortar(g1.dim - 1, mg, face_faces) gb.assign_node_ordering() return gb
def test_str_repr(self): g1 = MockGrid(dim=1, num_cells=1, num_faces=3, num_nodes=3) g2 = MockGrid(dim=2, num_cells=3, num_faces=7, num_nodes=3) gb = pp.GridBucket() gb.add_nodes([g1, g2]) gb.__str__() gb.__repr__()
def test_diameter(self): g1 = MockGrid(1, 2) g2 = MockGrid(2, 3) gb = pp.GridBucket() gb.add_nodes(g1) gb.add_nodes(g2) self.assertTrue(gb.diameter() == 3) self.assertTrue(gb.diameter(lambda g: g.dim == 1) == 2)
def test_dimension_ordering_edges(self): gb = pp.GridBucket() g1 = MockGrid(1) g2 = MockGrid(2) gb.add_nodes(g1) gb.add_nodes(g2) gb.add_edge([g1, g2], None) gb.add_edge([g1, g1], None) for e, _ in gb.edges(): self.assertTrue(e[0].dim >= e[1].dim) gb = pp.GridBucket() gb.add_nodes(g1) gb.add_nodes(g2) gb.add_edge([g2, g1], None) gb.add_edge([g1, g1], None) for e, _ in gb.edges(): self.assertTrue(e[0].dim >= e[1].dim)
def test_num_graph_nodes_edges(self): gb = pp.GridBucket() g1 = MockGrid(1) g2 = MockGrid(2) g3 = MockGrid(3) gb.add_nodes(g1) gb.add_nodes(g2) gb.add_nodes(g3) gb.add_edge([g1, g2], None) gb.add_edge([g2, g3], None) self.assertTrue(gb.num_graph_edges() == 2) self.assertTrue(gb.num_graph_nodes() == 3)
def test_add_single_edge_prop(self): gb = pp.GridBucket() g1 = MockGrid() g2 = MockGrid() gb.add_nodes(g1) gb.add_nodes(g2) gb.add_edge([g1, g2], None) gb.add_edge_props("a") for _, d in gb.edges(): self.assertTrue("a" in d.keys())
def test_overwrite_node_props(self): gb = pp.GridBucket() g1 = MockGrid() gb.add_nodes(g1) key = "foo" val = 42 gb.set_node_prop(g1, key, val) gb.add_node_props(key, overwrite=False) self.assertTrue(gb.node_props(g1, key) == val) gb.add_node_props(key) self.assertTrue(gb.node_props(g1, key) is None)
def test_add_edge(self): gb = pp.GridBucket() g1 = MockGrid(1) g2 = MockGrid(2) g3 = MockGrid(3) gb.add_nodes(g1) gb.add_nodes(g2) gb.add_nodes(g3) gb.add_edge([g1, g2], None) # Should not be able to add existing edge self.assertRaises(ValueError, gb.add_edge, [g1, g2], None) # Should not be able to add couplings two dimensions appart self.assertRaises(ValueError, gb.add_edge, [g1, g3], None)
def test_add_single_edge_prop_reverse_order(self): # Add property when reverting the order of the grid_pair gb = pp.GridBucket() g1 = MockGrid() g2 = MockGrid() gb.add_nodes(g1) gb.add_nodes(g2) gb.add_edge([g1, g2], None) # Add property, with reverse order of grid pair gb.add_edge_props("a", edges=[[g2, g1]]) for _, d in gb.edges(): self.assertTrue("a" in d.keys())
def test_add_multiple_edge_props(self): gb = pp.GridBucket() g1 = MockGrid() g2 = MockGrid() gb.add_nodes(g1) gb.add_nodes(g2) gb.add_edge([g1, g2], None) props = ["a", "b"] gb.add_edge_props(props) for _, d in gb.edges(): for p in props: self.assertTrue(p in d.keys())
def test_num_cells_faces_nodes(self): g1 = MockGrid(dim=1, num_cells=1, num_faces=3, num_nodes=3) g2 = MockGrid(dim=2, num_cells=3, num_faces=7, num_nodes=3) gb = pp.GridBucket() gb.add_nodes([g1, g2]) self.assertTrue(gb.num_cells() == (g1.num_cells + g2.num_cells)) self.assertTrue(gb.num_faces() == (g1.num_faces + g2.num_faces)) self.assertTrue(gb.num_nodes() == (g1.num_nodes + g2.num_nodes)) l = lambda g: g.dim == 1 self.assertTrue(gb.num_cells(l) == g1.num_cells) self.assertTrue(gb.num_faces(l) == g1.num_faces) self.assertTrue(gb.num_nodes(l) == g1.num_nodes)
def test_cell_centers(self): gb = pp.GridBucket() g1 = pp.CartGrid([1, 1, 1]) g1.nodes += 0.1 * np.random.random((g1.dim, g1.num_nodes)) g1.compute_geometry() g2 = pp.CartGrid([1, 1, 1]) g2.nodes += 0.1 * np.random.random((g2.dim, g2.num_nodes)) g2.compute_geometry() gb.add_nodes([g1, g2]) cond = lambda g: g == g1 cell_centers = np.hstack((g1.cell_centers, g2.cell_centers)) self.assertTrue(np.all(cell_centers == gb.cell_centers())) self.assertTrue(np.all(g1.cell_centers == gb.cell_centers(cond)))
def generate_grids(self, n, xmax, ymax, split): g1 = pp.CartGrid([split * n, ymax * n], physdims=[split, ymax]) g2 = pp.CartGrid([(xmax - split) * n, ymax * n], physdims=[xmax - split, ymax]) g2.nodes[0] += split g1.compute_geometry() g2.compute_geometry() grids = [g2, g1] gb = pp.GridBucket() [gb.add_nodes(g) for g in grids] [g2, g1] = gb.grids_of_dimension(2) tol = 1e-6 if np.any(g2.cell_centers[0] > split): right_grid = g2 left_grid = g1 else: right_grid = g1 left_grid = g2 gb.set_node_prop(left_grid, "node_number", 1) gb.set_node_prop(right_grid, "node_number", 0) left_faces = np.argwhere( left_grid.face_centers[0] > split - tol).ravel() right_faces = np.argwhere( right_grid.face_centers[0] < split + tol).ravel() val = np.ones(left_faces.size, dtype=np.bool) shape = [right_grid.num_faces, left_grid.num_faces] face_faces = sps.coo_matrix((val, (right_faces, left_faces)), shape=shape) gb.add_edge((right_grid, left_grid), face_faces) mg = pp.TensorGrid(np.array([split] * ((n + 1) * ymax))) mg.nodes[1] = np.linspace(0, ymax, (n + 1) * ymax) mg.compute_geometry() side_g = {pp.grids.mortar_grid.LEFT_SIDE: mg} d_e = gb.edge_props((g1, g2)) d_e["mortar_grid"] = pp.BoundaryMortar(g1.dim - 1, side_g, face_faces) d_e["edge_number"] = 0 return gb
def test_contains_edge(self): gb = pp.GridBucket() g1 = MockGrid(1) g2 = MockGrid(2) g3 = MockGrid(3) gb.add_nodes(g1) gb.add_nodes(g2) gb.add_nodes(g3) gb.add_edge([g1, g2], None) # This edge is defined self.assertTrue((g1, g2) in gb) # this is not self.assertFalse((g1, g3) in gb) # This is a list, and thus not an edge in the networkx sense self.assertFalse([g1, g2] in gb)
def test_overwrite_edge_props(self): gb = pp.GridBucket() g1 = MockGrid() g2 = MockGrid() gb.add_nodes(g1) gb.add_nodes(g2) e = (g1, g2) gb.add_edge(e, None) key = "foo" val = 42 gb.set_edge_prop(e, key, val) gb.add_edge_props(key, overwrite=False) self.assertTrue(gb.edge_props(e, key) == val) gb.add_edge_props(key) self.assertTrue(gb.edge_props(e, key) is None)
def multilayer_grid_bucket(gb): # we assume conforming grids and no 0d grids gb_new = pp.GridBucket() gb_new.add_nodes([g for g, _ in gb]) for e, d in gb.edges(): mg = d["mortar_grid"] gs, gm = gb.nodes_of_edge(e) # we construct the grid for the new layer g_new = unite_grids([g for g in mg.side_grids.values()]) # update the names gs.name += ["fault"] g_new.name += ["layer"] # add the new grid to the grid bucket gb_new.add_nodes(g_new) # we store the master-grid with the new layer grid with the # mortar mapping from the original edge edge = [gm, g_new] gb_new.add_edge(edge, None) mg1 = copy.deepcopy(mg) # the slave to mortar needs to be changed mg1.slave_to_mortar_int = sps.identity(g_new.num_cells, format="csc") gb_new.set_edge_prop(edge, "mortar_grid", mg1) # we store the slave-grid with the new layer grid with the # mortar mapping from the original edge edge = [g_new, gs] gb_new.add_edge(edge, None) mg2 = copy.deepcopy(mg) # the master to mortar needs to be changed mg2.master_to_mortar_int = sps.identity(g_new.num_cells, format="csc") gb_new.set_edge_prop(edge, "mortar_grid", mg2) gb_new.assign_node_ordering() return gb_new
def test_node_neighbor_no_dim(self): # Test node neighbors, not caring about dimensions gb = pp.GridBucket() g1 = MockGrid() g2 = MockGrid() g3 = MockGrid() gb.add_nodes(g1) gb.add_nodes(g2) gb.add_nodes(g3) gb.add_edge([g1, g2], None) gb.add_edge([g2, g3], None) neigh_1 = gb.node_neighbors(g1) self.assertTrue(neigh_1.size == 1) self.assertTrue(neigh_1[0] == g2) neigh_2 = gb.node_neighbors(g2) self.assertTrue(neigh_2.size == 2) self.assertTrue(neigh_2[0] == g1 or neigh_2[0] == g3) self.assertTrue(neigh_2[1] == g1 or neigh_2[1] == g3)
def test_set_get_node_props_single_grid(self): gb = pp.GridBucket() g1 = MockGrid() gb.add_nodes(g1) d = {'a': 1, 'b': 2, 'c': 3} keys = d.keys() vals = d.values() for k, v in zip(keys, vals): gb.set_node_prop(g1, k, v) # Obtain all keys, check that we have them all all_keys = gb.node_props(g1) assert all([k in keys for k in all_keys.keys()]) assert all([k in all_keys.keys() for k in keys]) # Next obtain values by keyword for k, v in zip(keys, vals): v2 = gb.node_props(g1, k) assert v == v2
def test_remove_selective_node_props(self): gb = pp.GridBucket() g1 = MockGrid() g2 = MockGrid() gb.add_nodes(g1) gb.add_nodes(g2) props = ['a', 'b', 'c'] gb.add_node_props(props) gb.remove_node_props('a', g1) gb.remove_node_props('b', g2) gb.remove_node_props('c', [g1, g2]) for g, d in gb: assert not 'c' in d.keys() if g == g1: assert not 'a' in d.keys() assert 'b' in d.keys() else: assert not 'b' in d.keys() assert 'a' in d.keys()
def test_set_get_node_props_single_grid(self): gb = pp.GridBucket() g1 = MockGrid() gb.add_nodes(g1) d = {"a": 1, "b": 2, "c": 3} keys = d.keys() vals = d.values() for k, v in zip(keys, vals): gb.set_node_prop(g1, k, v) # Obtain all keys, check that we have them all all_keys = gb.node_props(g1) self.assertTrue(all([k in keys for k in all_keys.keys()])) self.assertTrue(all([k in all_keys.keys() for k in keys])) # Next obtain values by keyword for k, v in zip(keys, vals): v2 = gb.node_props(g1, k) self.assertTrue(v == v2)
def test_remove_selective_node_props(self): gb = pp.GridBucket() g1 = MockGrid() g2 = MockGrid() gb.add_nodes(g1) gb.add_nodes(g2) props = ["a", "b", "c"] gb.add_node_props(props) gb.remove_node_props("a", g1) gb.remove_node_props("b", g2) gb.remove_node_props("c", [g1, g2]) for g, d in gb: self.assertTrue(not "c" in d.keys()) if g == g1: self.assertTrue(not "a" in d.keys()) self.assertTrue("b" in d.keys()) else: self.assertTrue(not "b" in d.keys()) self.assertTrue("a" in d.keys())
def test_bounding_box(self): gb = pp.GridBucket() g1 = pp.CartGrid([1, 1, 1]) g1.nodes = np.random.random((g1.dim, g1.num_nodes)) g2 = pp.CartGrid([1, 1, 1]) # Shift g2 with 1 g2.nodes = 1 + np.random.random((g2.dim, g2.num_nodes)) gb.add_nodes([g1, g2]) bmin, bmax = gb.bounding_box() # Since g2 is shifted, minimum should be at g1, maximum in g2 self.assertTrue(np.allclose(bmin, g1.nodes.min(axis=1))) self.assertTrue(np.allclose(bmax, g2.nodes.max(axis=1))) d = gb.bounding_box(as_dict=True) self.assertTrue(d["xmin"] == np.min(g1.nodes[0])) self.assertTrue(d["ymin"] == np.min(g1.nodes[1])) self.assertTrue(d["zmin"] == np.min(g1.nodes[2])) self.assertTrue(d["xmax"] == np.max(g2.nodes[0])) self.assertTrue(d["ymax"] == np.max(g2.nodes[1])) self.assertTrue(d["zmax"] == np.max(g2.nodes[2]))