def coarsening_example0(**kwargs): ####################### # Simple 2d coarsening based on tpfa for Cartesian grids # isotropic permeability ####################### Nx = Ny = 7 g = structured.CartGrid([Nx, Ny], [1, 1]) g.compute_geometry() if kwargs["visualize"]: plot_grid(g, info="all", alpha=0) part = create_partition(tpfa_matrix(g)) g = generate_coarse_grid(g, part) g.compute_geometry(is_starshaped=True) if kwargs["visualize"]: plot_grid(g, info="all", alpha=0) g = structured.CartGrid([Nx, Ny], [1, 1]) g.compute_geometry() part = create_partition(tpfa_matrix(g), cdepth=3) g = generate_coarse_grid(g, part) g.compute_geometry(is_starshaped=True) if kwargs["visualize"]: plot_grid(g, info="all", alpha=0)
def setup_2d(): grid_list = [] # Unstructured perturbation nx = np.array([2, 2]) g_cart_unpert = structured.CartGrid(nx) g_cart_unpert.compute_geometry() grid_list.append(g_cart_unpert) # Structured perturbation g_cart_spert = structured.CartGrid(nx) g_cart_spert.nodes[0, 4] = 1.5 g_cart_spert.compute_geometry() grid_list.append(g_cart_spert) # Larger grid, random perturbations nx = np.array([3, 3]) g_cart_rpert = structured.CartGrid(nx) dx = 1 pert = .4 rand = np.vstack( ( np.random.rand(g_cart_rpert.dim, g_cart_rpert.num_nodes), np.repeat(0., g_cart_rpert.num_nodes), ) ) g_cart_rpert.nodes = g_cart_rpert.nodes + dx * pert * (0.5 - rand) g_cart_rpert.compute_geometry() grid_list.append(g_cart_rpert) return grid_list
def setup_2d(): grid_list = [] # Unstructured perturbation nx = np.array([2, 2]) g_cart_unpert = structured.CartGrid(nx) g_cart_unpert.compute_geometry() grid_list.append(g_cart_unpert) # Structured perturbation g_cart_spert = structured.CartGrid(nx) g_cart_spert.nodes[0, 4] = 1.5 g_cart_spert.compute_geometry() grid_list.append(g_cart_spert) # Larger grid, random perturbations nx = np.array([3, 3]) g_cart_rpert = structured.CartGrid(nx) dx = 1 pert = 0.4 rand = np.vstack( ( np.random.rand(g_cart_rpert.dim, g_cart_rpert.num_nodes), np.repeat(0.0, g_cart_rpert.num_nodes), ) ) g_cart_rpert.nodes = g_cart_rpert.nodes + dx * pert * (0.5 - rand) # No perturbations of the z-coordinate (which is not active in this case) g_cart_rpert.nodes[2, :] = 0 g_cart_rpert.compute_geometry() grid_list.append(g_cart_rpert) return grid_list
def test_upwind_example1(self, if_export=False): ####################### # Simple 2d upwind problem with implicit Euler scheme in time ####################### T = 1 Nx, Ny = 10, 1 g = structured.CartGrid([Nx, Ny], [1, 1]) g.compute_geometry() advect = upwind.Upwind("transport") param = Parameters(g) dis = advect.discharge(g, [1, 0, 0]) b_faces = g.get_all_boundary_faces() bc = BoundaryCondition(g, b_faces, ["dir"] * b_faces.size) bc_val = np.hstack(([1], np.zeros(g.num_faces - 1))) param.set_bc("transport", bc) param.set_bc_val("transport", bc_val) data = {"param": param, "discharge": dis} data["deltaT"] = advect.cfl(g, data) U, rhs = advect.matrix_rhs(g, data) M, _ = mass_matrix.MassMatrix().matrix_rhs(g, data) conc = np.zeros(g.num_cells) # Perform an LU factorization to speedup the solver IE_solver = sps.linalg.factorized((M + U).tocsc()) # Loop over the time Nt = int(T / data["deltaT"]) time = np.empty(Nt) folder = "example1" save = Exporter(g, "conc_IE", folder) for i in np.arange(Nt): # Update the solution # Backward and forward substitution to solve the system conc = IE_solver(M.dot(conc) + rhs) time[i] = data["deltaT"] * i if if_export: save.write_vtk({"conc": conc}, time_step=i) if if_export: save.write_pvd(time) known = np.array([ 0.99969927, 0.99769441, 0.99067741, 0.97352474, 0.94064879, 0.88804726, 0.81498958, 0.72453722, 0.62277832, 0.51725056, ]) assert np.allclose(conc, known)
def test_uniform_displacement_neumann(self): physdims = [1, 1] g_size = [4, 8] g_list = [structured.CartGrid([n, n], physdims=physdims) for n in g_size] [g.compute_geometry() for g in g_list] error = [] for g in g_list: bot = np.ravel(np.argwhere(g.face_centers[1, :] < 1e-10)) left = np.ravel(np.argwhere(g.face_centers[0, :] < 1e-10)) dir_faces = np.hstack((left, bot)) bound = bc.BoundaryCondition( g, dir_faces.ravel("F"), ["dir"] * dir_faces.size ) constit = setup_stiffness(g) # Python inverter is most efficient for small problems stress, bound_stress = mpsa.mpsa(g, constit, bound, inverter="python") div = fvutils.vector_divergence(g) a = div * stress d_x = np.random.rand(1) d_y = np.random.rand(1) d_bound = np.zeros((g.dim, g.num_faces)) d_bound[0, bound.is_dir] = d_x d_bound[1, bound.is_dir] = d_y rhs = div * bound_stress * d_bound.ravel("F") d = np.linalg.solve(a.todense(), -rhs) traction = stress * d + bound_stress * d_bound.ravel("F") self.assertTrue(np.max(np.abs(d[::2] - d_x)) < 1e-8) self.assertTrue(np.max(np.abs(d[1::2] - d_y)) < 1e-8) self.assertTrue(np.max(np.abs(traction)) < 1e-8)
def test_upwind_1d_discharge_negative(self): g = structured.CartGrid(3, 1) g.compute_geometry() solver = upwind.Upwind() param = Parameters(g) dis = solver.discharge(g, [-2, 0, 0]) bf = g.get_boundary_faces() bc = BoundaryCondition(g, bf, bf.size * ['neu']) param.set_bc(solver, bc) data = {'param': param, 'discharge': dis} M = solver.matrix_rhs(g, data)[0].todense() deltaT = solver.cfl(g, data) M_known = np.array([[0,-2, 0], [0, 2,-2], [0, 0, 2]]) deltaT_known = 1/12 rtol = 1e-15 atol = rtol assert np.allclose(M, M_known, rtol, atol) assert np.allclose(deltaT, deltaT_known, rtol, atol)
def test_coarse_grid_2d(self): g = structured.CartGrid([3, 2]) g.compute_geometry() co.generate_coarse_grid(g, [5, 2, 2, 5, 2, 2]) assert g.num_cells == 2 assert g.num_faces == 12 assert g.num_nodes == 11 pt = np.tile(np.array([2, 1, 0]), (g.nodes.shape[1], 1)).T find = np.isclose(pt, g.nodes).all(axis=0) assert find.any() == False faces_cell0, _, orient_cell0 = sps.find(g.cell_faces[:, 0]) assert np.array_equal(faces_cell0, [1, 2, 4, 5, 7, 8, 10, 11]) assert np.array_equal(orient_cell0, [-1, 1, -1, 1, -1, -1, 1, 1]) faces_cell1, _, orient_cell1 = sps.find(g.cell_faces[:, 1]) assert np.array_equal(faces_cell1, [0, 1, 3, 4, 6, 9]) assert np.array_equal(orient_cell1, [-1, 1, -1, 1, -1, 1]) known = np.array([[0, 4], [1, 5], [3, 6], [4, 7], [5, 8], [6, 10], [0, 1], [1, 2], [2, 3], [7, 8], [8, 9], [9, 10]]) for f in np.arange(g.num_faces): assert np.array_equal(sps.find(g.face_nodes[:, f])[0], known[f, :])
def test_cart_3d(self): g = structured.CartGrid([4, 3, 3]) g.nodes = g.nodes + 0.2 * np.random.random((g.dim, g.nodes.shape[1])) g.compute_geometry() # Pick out cells (0, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 2), (1, 0, 2) c = np.array([0, 1, 4, 12, 24, 25]) h, sub_f, sub_n = partition.extract_subgrid(g, c) # There are 20 nodes in each layer nodes_0 = np.array([0, 1, 2, 5, 6, 7, 10, 11]) nodes_1 = nodes_0 + 20 nodes_2 = np.array([0, 1, 2, 5, 6, 7]) + 40 nodes_3 = nodes_2 + 20 true_nodes = np.hstack((nodes_0, nodes_1, nodes_2, nodes_3)) faces_x = np.array([0, 1, 2, 5, 6, 15, 16, 30, 31, 32]) # y-faces start on 45 faces_y = np.array([45, 46, 49, 50, 53, 61, 65, 77, 78, 81, 82]) # z-faces start on 93 faces_z = np.array([93, 94, 97, 105, 106, 109, 117, 118, 129, 130]) true_faces = np.hstack((faces_x, faces_y, faces_z)) assert np.array_equal(true_nodes, sub_n) assert np.array_equal(true_faces, sub_f) self.compare_grid_geometries(g, h, c, true_faces, true_nodes)
def test_upwind_2d_cart_surf_discharge_negative(self): g = structured.CartGrid([3, 2], [1, 1]) R = cg.rot(np.pi / 6., [1, 1, 0]) g.nodes = np.dot(R, g.nodes) g.compute_geometry(is_embedded=True) solver = upwind.Upwind() param = Parameters(g) dis = solver.discharge(g, np.dot(R, [-1, 0, 0])) bf = g.tags['domain_boundary_faces'].nonzero()[0] bc = BoundaryCondition(g, bf, bf.size * ['neu']) param.set_bc(solver, bc) data = {'param': param, 'discharge': dis} M = solver.matrix_rhs(g, data)[0].todense() deltaT = solver.cfl(g, data) M_known = 0.5 * np.array([[0, -1, 0, 0, 0, 0], [0, 1, -1, 0, 0, 0], [0, 0, 1, 0, 0, 0], [0, 0, 0, 0, -1, 0], [0, 0, 0, 0, 1, -1], [0, 0, 0, 0, 0, 1]]) deltaT_known = 1 / 6 rtol = 1e-15 atol = rtol assert np.allclose(M, M_known, rtol, atol) assert np.allclose(deltaT, deltaT_known, rtol, atol)
def test_upwind_2d_cart_discharge_positive(self): g = structured.CartGrid([3, 2], [1, 1]) g.compute_geometry() solver = upwind.Upwind() param = Parameters(g) dis = solver.discharge(g, [2, 0, 0]) bf = g.tags['domain_boundary_faces'].nonzero()[0] bc = BoundaryCondition(g, bf, bf.size * ['neu']) param.set_bc(solver, bc) data = {'param': param, 'discharge': dis} M = solver.matrix_rhs(g, data)[0].todense() deltaT = solver.cfl(g, data) M_known = np.array([[1, 0, 0, 0, 0, 0], [-1, 1, 0, 0, 0, 0], [0, -1, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0], [0, 0, 0, -1, 1, 0], [0, 0, 0, 0, -1, 0]]) deltaT_known = 1 / 12 rtol = 1e-15 atol = rtol assert np.allclose(M, M_known, rtol, atol) assert np.allclose(deltaT, deltaT_known, rtol, atol)
def test_upwind_1d_discharge_negative_bc_neu(self): g = structured.CartGrid(3, 1) g.compute_geometry() solver = upwind.Upwind() param = Parameters(g) dis = solver.discharge(g, [-2, 0, 0]) bf = g.tags['domain_boundary_faces'].nonzero()[0] bc = BoundaryCondition(g, bf, bf.size * ['neu']) bc_val = np.array([2, 0, 0, -2]).ravel('F') param.set_bc(solver, bc) param.set_bc_val(solver, bc_val) data = {'param': param, 'discharge': dis} M, rhs = solver.matrix_rhs(g, data) deltaT = solver.cfl(g, data) M_known = np.array([[0, -2, 0], [0, 2, -2], [0, 0, 2]]) rhs_known = np.array([-2, 0, 2]) deltaT_known = 1 / 12 rtol = 1e-15 atol = rtol assert np.allclose(M.todense(), M_known, rtol, atol) assert np.allclose(rhs, rhs_known, rtol, atol) assert np.allclose(deltaT, deltaT_known, rtol, atol)
def test_subcell_topology_2d_cart_1(self): x = np.ones(2, dtype=int) g = structured.CartGrid(x) subcell_topology = fvutils.SubcellTopology(g) self.assertTrue(np.all(subcell_topology.cno == 0)) ncum = np.bincount( subcell_topology.nno, weights=np.ones(subcell_topology.nno.size) ) self.assertTrue(np.all(ncum == 2)) fcum = np.bincount( subcell_topology.fno, weights=np.ones(subcell_topology.fno.size) ) self.assertTrue(np.all(fcum == 2)) # There is only one cell, thus only unique subfno usubfno = np.unique(subcell_topology.subfno) self.assertTrue(usubfno.size == subcell_topology.subfno.size) self.assertTrue( np.all(np.in1d(subcell_topology.subfno, subcell_topology.subhfno)) )
def setUp(self): self.tol = set_tol() nc = np.array([2, 2]) g = structured.CartGrid(nc) g.nodes[0, 4] = 1.5 g.compute_geometry() self.g = g
def darcy_dualVEM_example0(**kwargs): ####################### # Simple 2d Darcy problem with known exact solution ####################### Nx = Ny = 25 g = structured.CartGrid([Nx, Ny], [1, 1]) g.compute_geometry() kxx = np.ones(g.num_cells) perm = tensor.SecondOrderTensor(g.dim, kxx) f = np.ones(g.num_cells) b_faces = g.get_all_boundary_faces() bnd = bc.BoundaryCondition(g, b_faces, ["dir"] * b_faces.size) bnd_val = np.zeros(g.num_faces) solver = dual.DualVEM() data = {"k": perm, "source": f, "bc": bnd, "bc_val": bnd_val} D, rhs = solver.matrix_rhs(g, data) up = sps.linalg.spsolve(D, rhs) u, p = solver.extract_u(g, up), solver.extract_p(g, up) P0u = solver.project_u(g, u) if kwargs["visualize"]: plot_grid(g, p, P0u) norms = np.array([error.norm_L2(g, p), error.norm_L2(g, P0u)]) norms_known = np.array([0.041554943620853595, 0.18738227880674516]) assert np.allclose(norms, norms_known)
def test_upwind_1d_discharge_negative_bc_dir(self): g = structured.CartGrid(3, 1) g.compute_geometry() solver = upwind.Upwind() param = Parameters(g) dis = solver.discharge(g, [-2, 0, 0]) bf = g.tags["domain_boundary_faces"].nonzero()[0] bc = BoundaryCondition(g, bf, bf.size * ["dir"]) bc_val = 3 * np.ones(g.num_faces).ravel("F") param.set_bc(solver, bc) param.set_bc_val(solver, bc_val) data = {"param": param, "discharge": dis} M, rhs = solver.matrix_rhs(g, data) deltaT = solver.cfl(g, data) M_known = np.array([[2, -2, 0], [0, 2, -2], [0, 0, 2]]) rhs_known = np.array([0, 0, 6]) deltaT_known = 1 / 12 rtol = 1e-15 atol = rtol self.assertTrue(np.allclose(M.todense(), M_known, rtol, atol)) self.assertTrue(np.allclose(rhs, rhs_known, rtol, atol)) self.assertTrue(np.allclose(deltaT, deltaT_known, rtol, atol))
def test_upwind_2d_cart_surf_discharge_positive(self): g = structured.CartGrid([3, 2], [1, 1]) R = cg.rot(np.pi / 4., [0, 1, 0]) g.nodes = np.dot(R, g.nodes) g.compute_geometry() solver = upwind.Upwind() param = Parameters(g) dis = solver.discharge(g, np.dot(R, [1, 0, 0])) bf = g.tags["domain_boundary_faces"].nonzero()[0] bc = BoundaryCondition(g, bf, bf.size * ["neu"]) param.set_bc(solver, bc) data = {"param": param, "discharge": dis} M = solver.matrix_rhs(g, data)[0].todense() deltaT = solver.cfl(g, data) M_known = 0.5 * np.array([ [1, 0, 0, 0, 0, 0], [-1, 1, 0, 0, 0, 0], [0, -1, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0], [0, 0, 0, -1, 1, 0], [0, 0, 0, 0, -1, 0], ]) deltaT_known = 1 / 6 rtol = 1e-15 atol = rtol self.assertTrue(np.allclose(M, M_known, rtol, atol)) self.assertTrue(np.allclose(deltaT, deltaT_known, rtol, atol))
def darcy_dual_hybridVEM_example0(**kwargs): ####################### # Simple 2d Darcy problem with known exact solution ####################### Nx = Ny = 25 g = structured.CartGrid([Nx, Ny], [1, 1]) g.compute_geometry() kxx = np.ones(g.num_cells) perm = tensor.SecondOrderTensor(g.dim, kxx) f = np.ones(g.num_cells) b_faces = g.get_all_boundary_faces() bnd = bc.BoundaryCondition(g, b_faces, ['dir'] * b_faces.size) bnd_val = np.zeros(g.num_faces) solver = hybrid.HybridDualVEM() data = {'perm': perm, 'source': f, 'bc': bnd, 'bc_val': bnd_val} H, rhs = solver.matrix_rhs(g, data) l = sps.linalg.spsolve(H, rhs) u, p = solver.compute_up(g, l, data) P0u = dual.DualVEM().project_u(g, u) if kwargs['visualize']: plot_grid(g, p, P0u) norms = np.array([error.norm_L2(g, p), error.norm_L2(g, P0u)]) norms_known = np.array([0.041554943620853595, 0.18738227880674516]) assert np.allclose(norms, norms_known)
def test_create_partition_2d_cart(self): g = structured.CartGrid([5, 5]) g.compute_geometry() part = co.create_partition(co.tpfa_matrix(g)) known = np.array([0, 0, 0, 1, 1, 0, 0, 2, 1, 1, 3, 2, 2, 2, 1, 3, 3, 2,\ 4, 4, 3, 3, 4, 4, 4]) assert np.array_equal(part, known)
def make_grid(grid, grid_dims, domain, dim): if grid.lower() == "cart" or grid.lower() == "cartesian": return structured.CartGrid(grid_dims, domain) elif (grid.lower() == "simplex" and dim == 2) or grid.lower() == "triangular": return simplex.StructuredTriangleGrid(grid_dims, domain) elif grid.lower() == "tetrahedral": g = simplex.StructuredTetrahedralGrid(grid_dims, domain) return g
def setup_cart_2d(nx): g = structured.CartGrid(nx) g.compute_geometry() kxx = np.ones(g.num_cells) perm = tensor.SecondOrderTensor(g.dim, kxx) return g, perm
def darcy_dual_hybridVEM_example3(**kwargs): ####################### # Simple 3d Darcy problem with known exact solution ####################### Nx = Ny = Nz = 7 g = structured.CartGrid([Nx, Ny, Nz], [1, 1, 1]) g.compute_geometry() kxx = np.ones(g.num_cells) perm = tensor.SecondOrderTensor(g.dim, kxx) def funP_ex(pt): return (np.sin(2 * np.pi * pt[0]) * np.sin(2 * np.pi * pt[1]) * np.sin(2 * np.pi * pt[2])) def funU_ex(pt): return [ -2 * np.pi * np.cos(2 * np.pi * pt[0]) * np.sin(2 * np.pi * pt[1]) * np.sin(2 * np.pi * pt[2]), -2 * np.pi * np.sin(2 * np.pi * pt[0]) * np.cos(2 * np.pi * pt[1]) * np.sin(2 * np.pi * pt[2]), -2 * np.pi * np.sin(2 * np.pi * pt[0]) * np.sin(2 * np.pi * pt[1]) * np.cos(2 * np.pi * pt[2]), ] def fun(pt): return 12 * np.pi**2 * funP_ex(pt) f = np.array([fun(pt) for pt in g.cell_centers.T]) b_faces = g.get_all_boundary_faces() bnd = bc.BoundaryCondition(g, b_faces, ["dir"] * b_faces.size) bnd_val = np.zeros(g.num_faces) bnd_val[b_faces] = funP_ex(g.face_centers[:, b_faces]) solver = hybrid.HybridDualVEM() data = {"perm": perm, "source": f, "bc": bnd, "bc_val": bnd_val} H, rhs = solver.matrix_rhs(g, data) l = sps.linalg.spsolve(H, rhs) u, p = solver.compute_up(g, l, data) P0u = dual.DualVEM().project_u(g, u) if kwargs["visualize"]: plot_grid(g, p, P0u) p_ex = error.interpolate(g, funP_ex) u_ex = error.interpolate(g, funU_ex) np.set_printoptions(linewidth=999999) np.set_printoptions(precision=16) errors = np.array( [error.error_L2(g, p, p_ex), error.error_L2(g, P0u, u_ex)]) errors_known = np.array([0.1010936831876412, 0.0680593765009036]) assert np.allclose(errors, errors_known)
def test_tpfa_cart_2d(): """ Apply TPFA on Cartesian grid, should obtain Laplacian stencil. """ # Set up 3 X 3 Cartesian grid nx = np.array([3, 3]) g = structured.CartGrid(nx) g.compute_geometry() kxx = np.ones(g.num_cells) perm = tensor.SecondOrder(g.dim, kxx) bound_faces = np.array([0, 3, 12]) bound = bc.BoundaryCondition(g, bound_faces, ['dir'] * bound_faces.size) discr = tpfa.Tpfa() d = _assign_params(g, perm, bound) discr.discretize(g, d) trm, bound_flux = d['flux'], d['bound_flux'] div = g.cell_faces.T a = div * trm b = -(div * bound_flux).A # Checks on interior cell mid = 4 assert a[mid, mid] == 4 assert a[mid - 1, mid] == -1 assert a[mid + 1, mid] == -1 assert a[mid - 3, mid] == -1 assert a[mid + 3, mid] == -1 assert np.all(b[mid, :] == 0) # The first cell should have two Dirichlet bnds assert a[0, 0] == 6 assert a[0, 1] == -1 assert a[0, 3] == -1 assert b[0, 0] == 2 assert b[0, 12] == 2 # Cell 3 has one Dirichlet, one Neumann face print(a) assert a[2, 2] == 4 assert a[2, 1] == -1 assert a[2, 5] == -1 assert b[2, 3] == 2 assert b[2, 14] == -1 # Cell 2 has one Neumann face assert a[1, 1] == 3 assert a[1, 0] == -1 assert a[1, 2] == -1 assert a[1, 4] == -1 assert b[1, 13] == -1 return a
def make_grid(grid, grid_dims, domain, dim): if grid.lower() == 'cart' or grid.lower() == 'cartesian': return structured.CartGrid(grid_dims, domain) elif (grid.lower() == 'simplex' and dim == 2) \ or grid.lower() == 'triangular': return simplex.StructuredTriangleGrid(grid_dims, domain) elif grid.lower() == 'tetrahedral': g =simplex.StructuredTetrahedralGrid(grid_dims, domain) return g
def test_tag_1d(self): g = structured.CartGrid(3, 1) self.assertTrue(np.array_equal(g.tags["fracture_faces"], [False] * g.num_faces)) self.assertTrue(np.array_equal(g.tags["fracture_nodes"], [False] * g.num_nodes)) self.assertTrue(np.array_equal(g.tags["tip_faces"], [False] * g.num_faces)) self.assertTrue(np.array_equal(g.tags["tip_nodes"], [False] * g.num_nodes)) known = [True, False, False, True] self.assertTrue(np.array_equal(g.tags["domain_boundary_faces"], known)) self.assertTrue(np.array_equal(g.tags["domain_boundary_nodes"], known))
def test_create_partition_3d_cart(self): g = structured.CartGrid([4, 4, 4]) g.compute_geometry() part = co.create_partition(co.tpfa_matrix(g)) known = np.array([1, 1, 1, 1, 2, 4, 1, 3, 2, 2, 3, 3, 2, 2, 3, 3, 5, 4,\ 1, 6, 4, 4, 4, 3, 2, 4, 7, 3, 8, 8, 3, 3, 5, 5, 6, 6, 5, 4, 7, 6, 8, 7, 7, 7, 8,\ 8, 7, 9, 5, 5, 6, 6, 5, 5, 6, 6, 8, 8, 7, 9, 8, 8, 9, 9]) - 1 assert np.array_equal(part, known)
def coarsening_example2(**kwargs): ####################### # Simple 2d coarsening based on tpfa for Cartesian grids # anisotropic permeability ####################### Nx = Ny = 7 g = structured.CartGrid([Nx, Ny], [1, 1]) g.compute_geometry() if kwargs["visualize"]: plot_grid(g, info="all", alpha=0) kxx = 3 * np.ones(g.num_cells) kyy = np.ones(g.num_cells) perm = tensor.SecondOrderTensor(g.dim, kxx=kxx, kyy=kyy) part = create_partition(tpfa_matrix(g, perm=perm)) g = generate_coarse_grid(g, part) g.compute_geometry(is_starshaped=True) if kwargs["visualize"]: plot_grid(g, info="all", alpha=0) g = structured.CartGrid([Nx, Ny], [1, 1]) g.compute_geometry() part = create_partition(tpfa_matrix(g, perm=perm), cdepth=3) g = generate_coarse_grid(g, part) g.compute_geometry(is_starshaped=True) if kwargs["visualize"]: plot_grid(g, info="all", alpha=0) g = structured.CartGrid([Nx, Ny], [1, 1]) g.compute_geometry() part = create_partition(tpfa_matrix(g, perm=perm), cdepth=2, epsilon=1e-2) g = generate_coarse_grid(g, part) g.compute_geometry(is_starshaped=True) if kwargs["visualize"]: plot_grid(g, info="all", alpha=0)
def test_3d_coarse_dims_specified(self): g = structured.CartGrid([4, 4, 4]) coarse_dims = np.array([2, 2, 2]) p = partition.partition_structured(g, coarse_dims) p_known = np.array([[0, 0, 1, 1], [0, 0, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3], [0, 0, 1, 1], [0, 0, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3], [4, 4, 5, 5], [4, 4, 5, 5], [6, 6, 7, 7], [6, 6, 7, 7], [4, 4, 5, 5], [4, 4, 5, 5], [6, 6, 7, 7], [6, 6, 7, 7]]).ravel('C') assert np.allclose(p, p_known)
def test_2d_coarse_dims_specified(self): g = structured.CartGrid([4, 10]) coarse_dims = np.array([2, 3]) p = partition.partition_structured(g, coarse_dims) p_known = np.array( [[0., 0., 1., 1.], [0., 0., 1., 1.], [0., 0., 1., 1.], [2., 2., 3., 3.], [2., 2., 3., 3.], [2., 2., 3., 3.], [4., 4., 5., 5.], [4., 4., 5., 5.], [4., 4., 5., 5.], [4., 4., 5., 5.]], dtype='int').ravel('C') assert np.allclose(p, p_known)
def darcy_dualVEM_example3(**kwargs): ####################### # Simple 3d Darcy problem with known exact solution ####################### Nx = Ny = Nz = 7 g = structured.CartGrid([Nx, Ny, Nz], [1, 1, 1]) g.compute_geometry() kxx = np.ones(g.num_cells) perm = tensor.SecondOrder(g.dim, kxx) def funP_ex(pt): return np.sin(2*np.pi*pt[0])*np.sin(2*np.pi*pt[1])\ * np.sin(2*np.pi*pt[2]) def funU_ex(pt): return [-2*np.pi*np.cos(2*np.pi*pt[0])\ * np.sin(2*np.pi*pt[1])*np.sin(2*np.pi*pt[2]), -2*np.pi*np.sin(2*np.pi*pt[0])\ * np.cos(2*np.pi*pt[1])*np.sin(2*np.pi*pt[2]), -2*np.pi*np.sin(2*np.pi*pt[0])\ * np.sin(2*np.pi*pt[1])*np.cos(2*np.pi*pt[2])] def fun(pt): return 12 * np.pi**2 * funP_ex(pt) f = np.array([fun(pt) for pt in g.cell_centers.T]) b_faces = g.get_boundary_faces() bnd = bc.BoundaryCondition(g, b_faces, ['dir'] * b_faces.size) bnd_val = np.zeros(g.num_faces) bnd_val[b_faces] = funP_ex(g.face_centers[:, b_faces]) solver = dual.DualVEM() data = {'perm': perm, 'source': f, 'bc': bnd, 'bc_val': bnd_val} D, rhs = solver.matrix_rhs(g, data) up = sps.linalg.spsolve(D, rhs) u, p = solver.extract_u(g, up), solver.extract_p(g, up) P0u = solver.project_u(g, u) if kwargs['visualize']: plot_grid(g, p, P0u) p_ex = error.interpolate(g, funP_ex) u_ex = error.interpolate(g, funU_ex) errors = np.array( [error.error_L2(g, p, p_ex), error.error_L2(g, P0u, u_ex)]) errors_known = np.array([0.1010936831876412, 0.0680593765009036]) assert np.allclose(errors, errors_known)
def test_create_partition_2d_cart_cdepth4(self): g = structured.CartGrid([10, 10]) g.compute_geometry() part = co.create_partition(co.tpfa_matrix(g), cdepth=4) known = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 1, 1, 1, 1,\ 1, 1, 1, 2, 2, 3, 3, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 1, 1, 1, 2, 2, 2, 3, 3,\ 3, 3, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 2, 2, 2, 2,\ 2]) - 1 assert np.array_equal(part, known)