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 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 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.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 darcy_dual_hybridVEM_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.SecondOrder(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_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) 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)
# ------------------------------------------------------------------------------# Nx = Ny = 20 # g = structured.CartGrid([Nx, Ny], [1, 1]) g = simplex.StructuredTriangleGrid([Nx, Ny], [1, 1]) R = cg.rot(np.pi / 4.0, [1, 0, 0]) g.nodes = np.dot(R, g.nodes) g.compute_geometry() # co.coarsen(g, 'by_volume') # Assign parameters data = add_data(g) # Choose and define the solvers solver = dual.DualVEM("flow") A, b = solver.matrix_rhs(g, data) up = sps.linalg.spsolve(A, b) u = solver.extract_u(g, up) p = solver.extract_p(g, up) P0u = solver.project_u(g, u, data) diam = np.amax(g.cell_diameters()) print("h=", diam, "- err(p)=", error_p(g, p)) exporter.export_vtk(g, "vem", {"p": p, "P0u": P0u}, folder="vem") # ------------------------------------------------------------------------------#
#------------------------------------------------------------------------------# Nx = Ny = 20 #g = structured.CartGrid([Nx, Ny], [1, 1]) g = simplex.StructuredTriangleGrid([Nx, Ny], [1, 1]) R = cg.rot(np.pi / 4., [1, 0, 0]) g.nodes = np.dot(R, g.nodes) g.compute_geometry(is_embedded=True) #co.coarsen(g, 'by_volume') # Assign parameters data = add_data(g) # Choose and define the solvers solver = dual.DualVEM('flow') A, b = solver.matrix_rhs(g, data) up = sps.linalg.spsolve(A, b) u = solver.extract_u(g, up) p = solver.extract_p(g, up) P0u = solver.project_u(g, u, data) diam = np.amax(g.cell_diameters()) print("h=", diam, "- err(p)=", error_p(g, p)) exporter.export_vtk(g, 'vem', {"p": p, "P0u": P0u}, folder='vem') #------------------------------------------------------------------------------#
def darcy_dualVEM_coupling_example2(**kwargs): ####################### # Simple 2d Darcy problem with known exact solution ####################### np.set_printoptions(linewidth=999999, threshold=np.nan, precision=16) f_1 = np.array([[-1, 1, 1, -1], [0, 0, 0, 0], [-1, -1, 1, 1]]) f_2 = np.array([[0, 0, 0, 0], [-1, 1, 1, -1], [-1.5, -1.5, .8, .8]]) domain = {'xmin': -2, 'xmax': 2, 'ymin': -2, 'ymax': 2, 'zmin': -2, 'zmax': 2} # mesh_size = {'mode': 'constant', 'value': 0.5, 'bound_value': 1} mesh_size = {'mode': 'constant', 'value': 5, 'bound_value': 10} mesh_size = {'mode': 'constant', 'value': 0.25, 'bound_value': 10} kwargs['mesh_size'] = mesh_size kwargs['gmsh_path'] = '~/gmsh/bin/gmsh' gb = meshing.simplex_grid([f_1, f_2], domain, **kwargs) gb.remove_nodes(lambda g: g.dim == gb.dim_max()) # It's not really working now the coarsening part with the gb # for g, _ in gb: # if g.dim != 1: # part = create_partition(tpfa_matrix(g)) # gb.change_nodes({g: generate_coarse_grid(g, part)}) # # gb.compute_geometry(is_starshaped=True) print([g.num_faces for g, _ in gb]) gb.assign_node_ordering() # Need to remove the boundary flag explicity from the fracture face, # because of the mix formulation internal_flag = FaceTag.FRACTURE [g.remove_face_tag_if_tag(FaceTag.BOUNDARY, internal_flag) for g, _ in gb if g.dim == gb.dim_max()] if kwargs['visualize']: plot_grid(gb, info="f", alpha=0) gb.add_node_props(['perm', 'source', 'bc', 'bc_val']) for g, d in gb: kxx = np.ones(g.num_cells) d['perm'] = tensor.SecondOrder(g.dim, kxx) d['source'] = np.zeros(g.num_cells) b_faces = g.get_boundary_faces() b_faces_dir = b_faces[np.bitwise_or(g.face_centers[1, b_faces] == -1, g.face_centers[0, b_faces] == -1)] b_faces_dir_cond = g.face_centers[0, b_faces_dir] b_faces_neu = np.setdiff1d(b_faces, b_faces_dir, assume_unique=True) b_faces = np.hstack((b_faces_dir, b_faces_neu)) b_faces_label = np.hstack((['dir'] * b_faces_dir.size, ['neu'] * b_faces_neu.size)) d['bc'] = bc.BoundaryCondition(g, b_faces, b_faces_label) d['bc_val'] = {'dir': b_faces_dir_cond, 'neu': np.zeros(b_faces_neu.size)} gb.add_edge_prop('kn') for e, d in gb.edges_props(): g_l = gb.sorted_nodes_of_edge(e)[0] c = g_l.cell_centers[2, :] > -0.5 d['kn'] = (np.ones(g_l.num_cells) + c.astype('float') * (-1 + 1e-2)) solver = dual.DualVEM() coupling_conditions = dual_coupling.DualCoupling(solver) solver_coupler = coupler.Coupler(solver, coupling_conditions) A, b = solver_coupler.matrix_rhs(gb) up = sps.linalg.spsolve(A, b) solver_coupler.split(gb, "up", up) gb.add_node_props(["u", 'pressure', "P0u"]) for g, d in gb: d["u"] = solver.extractU(g, d["up"]) d['pressure'] = solver.extractP(g, d["up"]) d["P0u"] = solver.projectU(g, d["u"], d) if kwargs['visualize']: plot_grid(gb, 'pressure', "P0u") export_vtk(gb, "grid", ['pressure', "P0u"])
def darcy_dualVEM_coupling_example2(**kwargs): ####################### # Simple 2d Darcy problem with known exact solution ####################### np.set_printoptions(linewidth=999999, threshold=np.nan, precision=16) f_1 = np.array([[-1, 1, 1, -1], [0, 0, 0, 0], [-1, -1, 1, 1]]) f_2 = np.array([[0, 0, 0, 0], [-1, 1, 1, -1], [-1.5, -1.5, .8, .8]]) domain = { "xmin": -2, "xmax": 2, "ymin": -2, "ymax": 2, "zmin": -2, "zmax": 2 } kwargs = { "mesh_size_frac": .25, "mesh_size_bound": 10, "mesh_size_min": .02, "gmsh_path": "~/gmsh/bin/gmsh", } gb = meshing.simplex_grid([f_1, f_2], domain, **kwargs) gb.remove_nodes(lambda g: g.dim == gb.dim_max()) # It's not really working now the coarsening part with the gb # for g, _ in gb: # if g.dim != 1: # part = create_partition(tpfa_matrix(g)) # gb.change_nodes({g: generate_coarse_grid(g, part)}) # # gb.compute_geometry(is_starshaped=True) print([g.num_faces for g, _ in gb]) gb.assign_node_ordering() if kwargs["visualize"]: plot_grid(gb, info="f", alpha=0) gb.add_node_props(["perm", "source", "bc", "bc_val"]) for g, d in gb: kxx = np.ones(g.num_cells) d["perm"] = tensor.SecondOrderTensor(g.dim, kxx) d["source"] = np.zeros(g.num_cells) b_faces = g.tags["domain_boundary_faces"].nonzero()[0] b_faces_dir = b_faces[np.bitwise_or(g.face_centers[1, b_faces] == -1, g.face_centers[0, b_faces] == -1)] b_faces_dir_cond = g.face_centers[0, b_faces_dir] b_faces_neu = np.setdiff1d(b_faces, b_faces_dir, assume_unique=True) b_faces = np.hstack((b_faces_dir, b_faces_neu)) b_faces_label = np.hstack( (["dir"] * b_faces_dir.size, ["neu"] * b_faces_neu.size)) d["bc"] = bc.BoundaryCondition(g, b_faces, b_faces_label) d["bc_val"] = { "dir": b_faces_dir_cond, "neu": np.zeros(b_faces_neu.size) } gb.add_edge_prop("kn") for e, d in gb.edges_props(): g_l = gb.sorted_nodes_of_edge(e)[0] c = g_l.cell_centers[2, :] > -0.5 d["kn"] = np.ones(g_l.num_cells) + c.astype("float") * (-1 + 1e-2) solver = dual.DualVEM() coupling_conditions = dual_coupling.DualCoupling(solver) solver_coupler = coupler.Coupler(solver, coupling_conditions) A, b = solver_coupler.matrix_rhs(gb) up = sps.linalg.spsolve(A, b) solver_coupler.split(gb, "up", up) gb.add_node_props(["u", "pressure", "P0u"]) for g, d in gb: d["u"] = solver.extractU(g, d["up"]) d["pressure"] = solver.extractP(g, d["up"]) d["P0u"] = solver.projectU(g, d["u"], d) if kwargs["visualize"]: plot_grid(gb, "pressure", "P0u") export_vtk(gb, "grid", ["pressure", "P0u"])