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 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.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 = 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 darcy_dualVEM_example1(**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) def funP_ex(pt): return np.sin(2 * np.pi * pt[0]) * np.sin(2 * np.pi * pt[1]) def funU_ex(pt): return [ -2 * np.pi * np.cos(2 * np.pi * pt[0]) * np.sin(2 * np.pi * pt[1]), -2 * np.pi * np.sin(2 * np.pi * pt[0]) * np.cos(2 * np.pi * pt[1]), 0, ] def fun(pt): return 8 * 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 = 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.0210718223032, 0.00526933885613]) assert np.allclose(errors, errors_known)
def darcy_dualVEM_example2(**kwargs): ####################### # Simple 2d Darcy problem on a surface with known exact solution ####################### Nx = Ny = 25 g = simplex.StructuredTriangleGrid([Nx, Ny], [1, 1]) R = cg.rot(np.pi / 6., [0, 1, 1]) g.nodes = np.dot(R, g.nodes) g.compute_geometry() T = cg.tangent_matrix(g.nodes) kxx = np.ones(g.num_cells) perm = tensor.SecondOrderTensor(g.dim, kxx) def funP_ex(pt): return np.pi * pt[0] - 6 * pt[1] + np.exp(1) * pt[2] - 4 def funU_ex(pt): return np.dot(T, [-np.pi, 6, -np.exp(1)]) def fun(pt): return 0 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 = 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, 0]) assert np.allclose(errors, errors_known)
def gb_error(gb, v1, v2, norm='L2'): gb.add_node_props(['error_1', 'error_2']) has_splitter.split(gb, 'error_1', v1) has_splitter.split(gb, 'error_2', v2) e = gb.apply_function_to_nodes( lambda g, d: error_L2(g, d['error_1'], d['error_2'], relative=False)) return e
def gb_error(gb, v1, v2, norm="L2"): gb.add_node_props(["error_1", "error_2"]) has_splitter.split(gb, "error_1", v1) has_splitter.split(gb, "error_2", v2) e = gb.apply_function_to_nodes( lambda g, d: error_L2(g, d["error_1"], d["error_2"], relative=False)) return e
def test_0d_elimination_2d_1d_cross(self): """ Simplest case possible: 2d case with two fractures intersecting in a single 0d grid at the center of the domain. """ f1 = np.array([[0, 1], [.5, .5]]) f2 = np.array([[.5, .5], [0, 1]]) gb = meshing.cart_grid([f1, f2], [2, 2], **{'physdims': [1, 1]}) gb.compute_geometry() gb.assign_node_ordering() tol = 1e-3 solver = tpfa.Tpfa() gb.add_node_props(['param']) a = 1e-2 for g, d in gb: param = Parameters(g) a_dim = np.power(a, gb.dim_max() - g.dim) aperture = np.ones(g.num_cells) * a_dim param.set_aperture(aperture) kxx = np.ones(g.num_cells) * np.power(1e3, g.dim < gb.dim_max()) p = tensor.SecondOrderTensor(3, kxx, kyy=kxx, kzz=kxx) param.set_tensor('flow', p) bound_faces = g.tags['domain_boundary_faces'].nonzero()[0] if bound_faces.size != 0: bound_face_centers = g.face_centers[:, bound_faces] right = bound_face_centers[0, :] > 1 - tol left = bound_face_centers[0, :] < tol labels = np.array(['neu'] * bound_faces.size) labels[right] = ['dir'] bc_val = np.zeros(g.num_faces) bc_dir = bound_faces[right] bc_neu = bound_faces[left] bc_val[bc_dir] = g.face_centers[0, bc_dir] bc_val[bc_neu] = -g.face_areas[bc_neu] * a_dim param.set_bc(solver, bc.BoundaryCondition(g, bound_faces, labels)) param.set_bc_val(solver, bc_val) else: param.set_bc("flow", bc.BoundaryCondition(g, np.empty(0), np.empty(0))) d['param'] = param coupling_conditions = tpfa.TpfaCoupling(solver) solver_coupler = coupler.Coupler(solver, coupling_conditions) A, rhs = solver_coupler.matrix_rhs(gb) p = sps.linalg.spsolve(A, rhs) p_cond, _, _, _ = condensation.solve_static_condensation(A, rhs, gb, dim=0) solver_coupler.split(gb, 'pressure', p) solver_coupler.split(gb, "p_cond", p_cond) tol = 1e-10 assert ((np.amax(np.absolute(p - p_cond))) < tol) assert (np.sum( error.error_L2(g, d['pressure'], d['p_cond']) for g, d in gb) < tol)
def test_0d_elimination_3d_2d_1d_0d(self): """ 3d case with a single 0d grid. """ f1 = np.array([[0, 1, 1, 0], [0, 0, 1, 1], [.5, .5, .5, .5]]) f2 = np.array([[.5, .5, .5, .5], [0, 1, 1, 0], [0, 0, 1, 1]]) f3 = np.array([[0, 1, 1, 0], [.5, .5, .5, .5], [0, 0, 1, 1]]) gb = meshing.cart_grid([f1, f2, f3], [2, 2, 2], **{'physdims': [1, 1, 1]}) gb.compute_geometry() gb.assign_node_ordering() cell_centers1 = np.array([[0.25, 0.75, 0.25, 0.75], [0.25, 0.25, 0.75, 0.75], [0.5, 0.5, 0.5, 0.5]]) cell_centers2 = np.array([[0.5, 0.5, 0.5, 0.5], [0.25, 0.25, 0.75, 0.75], [0.75, 0.25, 0.75, 0.25]]) cell_centers3 = np.array([[0.25, 0.75, 0.25, 0.75], [0.5, 0.5, 0.5, 0.5], [0.25, 0.25, 0.75, 0.75]]) cell_centers4 = np.array([[0.5], [0.25], [0.5]]) cell_centers5 = np.array([[0.5], [0.75], [0.5]]) cell_centers6 = np.array([[0.75], [0.5], [0.5]]) cell_centers7 = np.array([[0.25], [0.5], [0.5]]) cell_centers8 = np.array([[0.5], [0.5], [0.25]]) cell_centers9 = np.array([[0.5], [0.5], [0.75]]) for g, d in gb: if np.allclose(g.cell_centers[:, 0], cell_centers1[:, 0]): d['node_number'] = 1 elif np.allclose(g.cell_centers[:, 0], cell_centers2[:, 0]): d['node_number'] = 2 elif np.allclose(g.cell_centers[:, 0], cell_centers3[:, 0]): d['node_number'] = 3 elif np.allclose(g.cell_centers[:, 0], cell_centers4[:, 0]): d['node_number'] = 4 elif np.allclose(g.cell_centers[:, 0], cell_centers5[:, 0]): d['node_number'] = 5 elif np.allclose(g.cell_centers[:, 0], cell_centers6[:, 0]): d['node_number'] = 6 elif np.allclose(g.cell_centers[:, 0], cell_centers7[:, 0]): d['node_number'] = 7 elif np.allclose(g.cell_centers[:, 0], cell_centers8[:, 0]): d['node_number'] = 8 elif np.allclose(g.cell_centers[:, 0], cell_centers9[:, 0]): d['node_number'] = 9 else: pass tol = 1e-3 solver = tpfa.Tpfa() gb.add_node_props(['param']) a = 1e-2 for g, d in gb: param = Parameters(g) aperture = np.ones(g.num_cells) * np.power(a, gb.dim_max() - g.dim) param.set_aperture(aperture) p = tensor.SecondOrderTensor( 3, np.ones(g.num_cells) * np.power(1e3, g.dim < gb.dim_max())) param.set_tensor('flow', p) bound_faces = g.tags['domain_boundary_faces'].nonzero()[0] if bound_faces.size != 0: bound_face_centers = g.face_centers[:, bound_faces] left = bound_face_centers[0, :] > 1 - tol right = bound_face_centers[0, :] < tol labels = np.array(['neu'] * bound_faces.size) labels[np.logical_or(left, right)] = ['dir'] bc_val = np.zeros(g.num_faces) bc_dir = bound_faces[np.logical_or(left, right)] bc_val[bc_dir] = g.face_centers[0, bc_dir] param.set_bc(solver, bc.BoundaryCondition(g, bound_faces, labels)) param.set_bc_val(solver, bc_val) else: param.set_bc("flow", bc.BoundaryCondition(g, np.empty(0), np.empty(0))) d['param'] = param coupling_conditions = tpfa.TpfaCoupling(solver) solver_coupler = coupler.Coupler(solver, coupling_conditions) A, rhs = solver_coupler.matrix_rhs(gb) p = sps.linalg.spsolve(A, rhs) p_cond, _, _, _ = condensation.solve_static_condensation(A, rhs, gb, dim=0) solver_coupler.split(gb, 'pressure', p) solver_coupler.split(gb, "p_cond", p_cond) tol = 1e-10 assert ((np.amax(np.absolute(p - p_cond))) < tol) assert (np.sum( error.error_L2(g, d['pressure'], d['p_cond']) for g, d in gb) < tol)
def atest_1d_elimination_3d_2d_1d(self): """ 3d case with a single 1d grid. """ f1 = np.array([[0, 1, 1, 0], [0, 0, 1, 1], [.5, .5, .5, .5]]) f2 = np.array([[.5, .5, .5, .5], [0, 1, 1, 0], [0, 0, 1, 1]]) gb = meshing.cart_grid([f1, f2], [2, 2, 2], **{"physdims": [1, 1, 1]}) gb.compute_geometry() gb.assign_node_ordering() tol = 1e-3 solver = tpfa.Tpfa() gb.add_node_props(["param"]) a = 1e-2 for g, d in gb: param = Parameters(g) aperture = np.ones(g.num_cells) * np.power(a, gb.dim_max() - g.dim) param.set_aperture(aperture) p = tensor.SecondOrderTensor( 3, np.ones(g.num_cells) * np.power(1e3, g.dim < gb.dim_max())) param.set_tensor("flow", p) bound_faces = g.tags["domain_boundary_faces"].nonzero()[0] bound_face_centers = g.face_centers[:, bound_faces] left = bound_face_centers[0, :] > 1 - tol right = bound_face_centers[0, :] < tol labels = np.array(["neu"] * bound_faces.size) labels[np.logical_or(left, right)] = ["dir"] bc_val = np.zeros(g.num_faces) bc_dir = bound_faces[np.logical_or(left, right)] bc_val[bc_dir] = g.face_centers[0, bc_dir] param.set_bc(solver, bc.BoundaryCondition(g, bound_faces, labels)) param.set_bc_val(solver, bc_val) d["param"] = param coupling_conditions = tpfa.TpfaCoupling(solver) solver_coupler = coupler.Coupler(solver, coupling_conditions) A, rhs = solver_coupler.matrix_rhs(gb) p = sps.linalg.spsolve(A, rhs) p_cond, _, _, _ = condensation.solve_static_condensation(A, rhs, gb, dim=1) solver_coupler.split(gb, "pressure", p) solver_coupler.split(gb, "p_cond", p_cond) tol = 1e-5 self.assertTrue((np.amax(np.absolute(p - p_cond))) < tol) self.assertTrue( np.sum( error.error_L2(g, d["pressure"], d["p_cond"]) for g, d in gb) < tol)
def atest_0d_elimination_two_0d_grids(self): """ 2d case involving two 0d grids. """ f1 = np.array([[0, 1], [.5, .5]]) f2 = np.array([[.5, .5], [0, 1]]) f3 = np.array([[.25, .25], [0, 1]]) gb = meshing.cart_grid([f1, f2, f3], [4, 2], **{"physdims": [1, 1]}) gb.compute_geometry() gb.assign_node_ordering() tol = 1e-3 solver = tpfa.Tpfa() gb.add_node_props(["param"]) a = 1e-2 for g, d in gb: param = Parameters(g) a_dim = np.power(a, gb.dim_max() - g.dim) aperture = np.ones(g.num_cells) * a_dim param.set_aperture(aperture) kxx = np.ones(g.num_cells) * np.power(1e3, g.dim < gb.dim_max()) p = tensor.SecondOrderTensor(3, kxx, kyy=kxx, kzz=kxx) param.set_tensor("flow", p) bound_faces = g.tags["domain_boundary_faces"].nonzero()[0] if bound_faces.size != 0: bound_face_centers = g.face_centers[:, bound_faces] right = bound_face_centers[0, :] > 1 - tol left = bound_face_centers[0, :] < tol labels = np.array(["neu"] * bound_faces.size) labels[right] = ["dir"] bc_val = np.zeros(g.num_faces) bc_dir = bound_faces[right] bc_neu = bound_faces[left] bc_val[bc_dir] = g.face_centers[0, bc_dir] bc_val[bc_neu] = -g.face_areas[bc_neu] * a_dim param.set_bc(solver, bc.BoundaryCondition(g, bound_faces, labels)) param.set_bc_val(solver, bc_val) else: param.set_bc("flow", bc.BoundaryCondition(g, np.empty(0), np.empty(0))) d["param"] = param coupling_conditions = tpfa.TpfaCoupling(solver) solver_coupler = coupler.Coupler(solver, coupling_conditions) A, rhs = solver_coupler.matrix_rhs(gb) p = sps.linalg.spsolve(A, rhs) p_cond, _, _, _ = condensation.solve_static_condensation(A, rhs, gb, dim=0) solver_coupler.split(gb, "pressure", p) solver_coupler.split(gb, "p_cond", p_cond) tol = 1e-10 self.assertTrue((np.amax(np.absolute(p - p_cond))) < tol) self.assertTrue( np.sum(error.error_L2(g, d["pressure"], d["p_cond"]) for g, d in gb) < tol )