def test_upwind_example_1(self, if_export=False): ####################### # Simple 2d upwind problem with explicit Euler scheme in time ####################### T = 1 Nx, Ny = 4, 1 g = pp.CartGrid([Nx, Ny], [1, 1]) g.compute_geometry() advect = pp.Upwind("transport") dis = advect.darcy_flux(g, [1, 0, 0]) b_faces = g.get_all_boundary_faces() bc = pp.BoundaryCondition(g, b_faces, ["dir"] * b_faces.size) bc_val = np.hstack(([1], np.zeros(g.num_faces - 1))) specified_parameters = { "bc": bc, "bc_values": bc_val, "darcy_flux": dis } data = pp.initialize_default_data(g, {}, "transport", specified_parameters) time_step = advect.cfl(g, data) data[pp.PARAMETERS]["transport"]["time_step"] = time_step advect.discretize(g, data) U, rhs = advect.assemble_matrix_rhs(g, data) rhs = time_step * rhs U = time_step * U OF = advect.outflow(g, data) mass = pp.MassMatrix("transport") mass.discretize(g, data) M, _ = mass.assemble_matrix_rhs(g, data) conc = np.zeros(g.num_cells) M_minus_U = M - U inv_mass = pp.InvMassMatrix("transport") inv_mass.discretize(g, data) invM, _ = inv_mass.assemble_matrix_rhs(g, data) # Loop over the time Nt = int(T / time_step) time = np.empty(Nt) production = np.zeros(Nt) for i in np.arange(Nt): # Update the solution production[i] = np.sum(OF.dot(conc)) conc = invM.dot((M_minus_U).dot(conc) + rhs) time[i] = time_step * i known = 1.09375 assert np.sum(production) == known
def test_inv_mass_matrix(self): g = pp.CartGrid([3, 3, 3]) g.compute_geometry() phi = np.random.rand(g.num_cells) dt = 0.2 specified_parameters = {"time_step": dt, "mass_weight": phi} data = pp.initialize_default_data(g, {}, "flow", specified_parameters) time_discr = pp.InvMassMatrix() time_discr.discretize(g, data) lhs, rhs = time_discr.assemble_matrix_rhs(g, data) self.assertTrue(np.allclose(rhs, 0)) self.assertTrue(np.allclose(lhs.diagonal(), 1 / (g.cell_volumes * phi))) off_diag = np.where(~np.eye(lhs.shape[0], dtype=bool)) self.assertTrue(np.allclose(lhs.A[off_diag], 0))
def test_upwind_example_3(self, if_export=False): ####################### # Simple 2d upwind problem with explicit Euler scheme in time coupled with # a Darcy problem ####################### T = 2 Nx, Ny = 10, 10 def funp_ex(pt): return -np.sin(pt[0]) * np.sin(pt[1]) - pt[0] g = pp.CartGrid([Nx, Ny], [1, 1]) g.compute_geometry() # Permeability perm = pp.SecondOrderTensor(kxx=np.ones(g.num_cells)) # Boundaries b_faces = g.get_all_boundary_faces() bc = pp.BoundaryCondition(g, b_faces, ["dir"] * b_faces.size) bc_val = np.zeros(g.num_faces) bc_val[b_faces] = funp_ex(g.face_centers[:, b_faces]) specified_parameters = { "bc": bc, "bc_values": bc_val, "second_order_tensor": perm, } data = pp.initialize_default_data(g, {}, "flow", specified_parameters) solver = pp.MVEM("flow") solver.discretize(g, data) D_flow, b_flow = solver.assemble_matrix_rhs(g, data) solver_source = pp.DualScalarSource("flow") solver_source.discretize(g, data) D_source, b_source = solver_source.assemble_matrix_rhs(g, data) up = sps.linalg.spsolve(D_flow + D_source, b_flow + b_source) _, u = solver.extract_pressure(g, up, data), solver.extract_flux(g, up, data) # Darcy_Flux dis = u # Boundaries bc = pp.BoundaryCondition(g, b_faces, ["dir"] * b_faces.size) bc_val = np.hstack(([1], np.zeros(g.num_faces - 1))) specified_parameters = {"bc": bc, "bc_values": bc_val, "darcy_flux": dis} data = pp.initialize_default_data(g, {}, "transport", specified_parameters) # Advect solver advect = pp.Upwind("transport") advect.discretize(g, data) U, rhs = advect.assemble_matrix_rhs(g, data) time_step = advect.cfl(g, data) rhs = time_step * rhs U = time_step * U data[pp.PARAMETERS]["transport"]["time_step"] = time_step mass = pp.MassMatrix("transport") mass.discretize(g, data) M, _ = mass.assemble_matrix_rhs(g, data) conc = np.zeros(g.num_cells) M_minus_U = M - U inv_mass = pp.InvMassMatrix("transport") inv_mass.discretize(g, data) invM, _ = inv_mass.assemble_matrix_rhs(g, data) # Loop over the time Nt = int(T / time_step) time = np.empty(Nt) for i in np.arange(Nt): # Update the solution conc = invM.dot((M_minus_U).dot(conc) + rhs) time[i] = time_step * i known = np.array( [ 9.63168200e-01, 8.64054875e-01, 7.25390695e-01, 5.72228235e-01, 4.25640080e-01, 2.99387331e-01, 1.99574336e-01, 1.26276876e-01, 7.59011550e-02, 4.33431230e-02, 3.30416807e-02, 1.13058617e-01, 2.05372538e-01, 2.78382057e-01, 3.14035373e-01, 3.09920132e-01, 2.75024694e-01, 2.23163145e-01, 1.67386939e-01, 1.16897527e-01, 1.06111312e-03, 1.11951850e-02, 3.87907727e-02, 8.38516119e-02, 1.36617802e-01, 1.82773271e-01, 2.10446545e-01, 2.14651936e-01, 1.97681518e-01, 1.66549151e-01, 3.20751341e-05, 9.85780113e-04, 6.07062715e-03, 1.99393042e-02, 4.53237556e-02, 8.00799828e-02, 1.17199623e-01, 1.47761481e-01, 1.64729339e-01, 1.65390555e-01, 9.18585872e-07, 8.08267622e-05, 8.47227168e-04, 4.08879583e-03, 1.26336029e-02, 2.88705048e-02, 5.27841497e-02, 8.10459333e-02, 1.07956484e-01, 1.27665318e-01, 2.51295298e-08, 6.29844122e-06, 1.09361990e-04, 7.56743783e-04, 3.11384414e-03, 9.04446601e-03, 2.03443897e-02, 3.75208816e-02, 5.89595194e-02, 8.11457277e-02, 6.63498510e-10, 4.73075468e-07, 1.33728945e-05, 1.30243418e-04, 7.01905707e-04, 2.55272292e-03, 6.96686157e-03, 1.52290448e-02, 2.78607282e-02, 4.40402650e-02, 1.71197497e-11, 3.47118057e-08, 1.57974045e-06, 2.13489614e-05, 1.48634295e-04, 6.68104990e-04, 2.18444135e-03, 5.58646819e-03, 1.17334966e-02, 2.09744728e-02, 4.37822313e-13, 2.52373622e-09, 1.83589660e-07, 3.40553325e-06, 3.02948532e-05, 1.66504215e-04, 6.45119867e-04, 1.90731440e-03, 4.53436628e-03, 8.99977737e-03, 1.12627412e-14, 1.84486857e-10, 2.13562387e-08, 5.39492977e-07, 6.08223906e-06, 4.05535296e-05, 1.84731221e-04, 6.25871542e-04, 1.66459389e-03, 3.59980231e-03, ] ) assert np.allclose(conc, known)