def upwind_example2(**kwargs): ####################### # Simple 2d upwind problem with explicit Euler scheme in time coupled with # a Darcy problem ####################### T = 2 Nx, Ny = 10, 10 g = structured.CartGrid([Nx, Ny], [1, 1]) g.compute_geometry() kxx = np.ones(g.num_cells) perm = second_order_tensor.SecondOrderTensor(g.dim, kxx) def funp_ex(pt): return -np.sin(pt[0]) * np.sin(pt[1]) - pt[0] f = np.zeros(g.num_cells) b_faces = g.get_boundary_faces() bnd = bc.BoundaryCondition(g, b_faces, ['dir'] * b_faces.size) bnd_val = {'dir': funp_ex(g.face_centers[:, b_faces])} solver = dual.DualVEM() data = {'k': perm, 'f': f, 'bc': bnd, 'bc_val': bnd_val} D, rhs = solver.matrix_rhs(g, data) up = sps.linalg.spsolve(D, rhs) beta_n = solver.extractU(g, up) u, p = solver.extractU(g, up), solver.extractP(g, up) P0u = solver.projectU(g, u, data) export_vtk(g, "darcy", {"p": p, "P0u": P0u}) advect = upwind.Upwind() bnd_val = {'dir': np.hstack(([1], np.zeros(b_faces.size - 1)))} data = {'beta_n': beta_n, 'bc': bnd, 'bc_val': bnd_val} U, rhs = advect.matrix_rhs(g, data) data = {'deltaT': advect.cfl(g, data)} M, _ = mass_matrix.Mass().matrix_rhs(g, data) conc = np.zeros(g.num_cells) M_minus_U = M - U invM, _ = mass_matrix.InvMass().matrix_rhs(g, data) # Loop over the time Nt = int(T / data['deltaT']) time = np.empty(Nt) for i in np.arange(Nt): # Update the solution conc = invM.dot((M_minus_U).dot(conc) + rhs) time[i] = data['deltaT'] * i export_vtk(g, "conc_darcy", {"conc": conc}, time_step=i) export_pvd(g, "conc_darcy", time)
def test_upwind_1d_beta_negative(self): g = structured.CartGrid(3, 1) g.compute_geometry() solver = upwind.Upwind() data = {'beta_n': solver.beta_n(g, [-1, 0, 0])} M = solver.matrix_rhs(g, data)[0].todense() deltaT = solver.cfl(g, data) M_known = np.array([[1, -1, 0], [0, 1, -1], [0, 0, 0]]) deltaT_known = 1 / 3 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_simplex_beta_positive(self): g = simplex.StructuredTriangleGrid([2, 1], [1, 1]) g.compute_geometry() solver = upwind.Upwind() data = {'beta_n': solver.beta_n(g, [1, 0, 0])} M = solver.matrix_rhs(g, data)[0].todense() deltaT = solver.cfl(g, data) M_known = np.array([[1, -1, 0, 0], [0, 0, 0, 0], [0, 0, 1, -1], [-1, 0, 0, 1]]) deltaT_known = 1 / 8 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_beta_positive(self): g = structured.CartGrid([3, 2], [1, 1]) g.compute_geometry() solver = upwind.Upwind() data = {'beta_n': solver.beta_n(g, [1, 0, 0])} M = solver.matrix_rhs(g, data)[0].todense() deltaT = solver.cfl(g, data) M_known = 0.5 * np.array([[0, 0, 0, 0, 0, 0], [-1, 1, 0, 0, 0, 0], [0, -1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, -1, 1, 0], [0, 0, 0, 0, -1, 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_1d_surf_beta_negative(self): g = structured.CartGrid(3, 1) R = cg.rot(-np.pi / 8., [-1, 1, -1]) g.nodes = np.dot(R, g.nodes) g.compute_geometry(is_embedded=True) solver = upwind.Upwind() data = {'beta_n': solver.beta_n(g, np.dot(R, [-1, 0, 0]))} M = solver.matrix_rhs(g, data)[0].todense() deltaT = solver.cfl(g, data) M_known = np.array([[1, -1, 0], [0, 1, -1], [0, 0, 0]]) deltaT_known = 1 / 3 rtol = 1e-15 atol = rtol assert np.allclose(M, M_known, rtol, atol) assert np.allclose(deltaT, deltaT_known, rtol, atol)
def upwind_example1(**kwargs): ####################### # 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() beta_n = advect.beta_n(g, [1, 0, 0]) b_faces = g.get_boundary_faces() bnd = bc.BoundaryCondition(g, b_faces, ['dir'] * b_faces.size) bnd_val = {'dir': np.hstack(([1], np.zeros(b_faces.size - 1)))} data = {'beta_n': beta_n, 'bc': bnd, 'bc_val': bnd_val} U, rhs = advect.matrix_rhs(g, data) data = {'deltaT': 2 * advect.cfl(g, data)} M, _ = mass_matrix.Mass().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) 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 export_vtk(g, "conc_IE", {"conc": conc}, time_step=i) export_pvd(g, "conc_IE", time)
def upwind_example0(**kwargs): ####################### # Simple 2d upwind problem with explicit Euler scheme in time ####################### T = 1 Nx, Ny = 10, 1 g = structured.CartGrid([Nx, Ny], [1, 1]) g.compute_geometry() advect = upwind.Upwind() beta_n = advect.beta_n(g, [1, 0, 0]) b_faces = g.get_boundary_faces() bnd = bc.BoundaryCondition(g, b_faces, ['dir'] * b_faces.size) bnd_val = {'dir': np.hstack(([1], np.zeros(b_faces.size - 1)))} data = {'beta_n': beta_n, 'bc': bnd, 'bc_val': bnd_val} U, rhs = advect.matrix_rhs(g, data) data = {'deltaT': advect.cfl(g, data)} M, _ = mass_matrix.Mass().matrix_rhs(g, data) conc = np.zeros(g.num_cells) M_minus_U = M - U invM, _ = mass_matrix.InvMass().matrix_rhs(g, data) # Loop over the time Nt = int(T / data['deltaT']) time = np.empty(Nt) for i in np.arange(Nt): # Update the solution conc = invM.dot((M_minus_U).dot(conc) + rhs) time[i] = data['deltaT'] * i export_vtk(g, "conc_EE", {"conc": conc}, time_step=i) export_pvd(g, "conc_EE", time)