def test_scalar_p1_scaled_mesh(): # Make coarse mesh smaller than fine mesh meshc = UnitCubeMesh(MPI.comm_world, 2, 2, 2) meshc.geometry.points *= 0.9 meshf = UnitCubeMesh(MPI.comm_world, 3, 4, 5) Vc = FunctionSpace(meshc, "CG", 1) Vf = FunctionSpace(meshf, "CG", 1) u = Expression("x[0] + 2*x[1] + 3*x[2]", degree=1) uc = interpolate(u, Vc) uf = interpolate(u, Vf) mat = PETScDMCollection.create_transfer_matrix(Vc, Vf).mat() Vuc = Function(Vf) mat.mult(uc.vector().vec(), Vuc.vector().vec()) diff = Vuc.vector() diff.vec().axpy(-1, uf.vector().vec()) assert diff.norm(Norm.l2) < 1.0e-12 # Now make coarse mesh larger than fine mesh meshc.geometry.points *= 1.5 uc = interpolate(u, Vc) mat = PETScDMCollection.create_transfer_matrix(Vc, Vf).mat() mat.mult(uc.vector().vec(), Vuc.vector().vec()) diff = Vuc.vector() diff.vec().axpy(-1, uf.vector().vec()) assert diff.norm(Norm.l2) < 1.0e-12
def test_mg_solver_laplace(): # Create meshes and function spaces meshes = [UnitSquareMesh(N, N) for N in [16, 32, 64]] V = [FunctionSpace(mesh, "Lagrange", 1) for mesh in meshes] # Create variational problem on fine grid u, v = TrialFunction(V[-1]), TestFunction(V[-1]) a = dot(grad(u), grad(v)) * dx L = v * dx bc0 = Function(V[-1]) bc = DirichletBC(V[-1], bc0, "on_boundary") A, b = assemble_system(a, L, bc) # Create collection of PETSc DM objects dm_collection = PETScDMCollection(V) # Create PETSc Krylov solver and set operator solver = PETScKrylovSolver() solver.set_operator(A) # Set PETSc solver type PETScOptions.set("ksp_type", "richardson") PETScOptions.set("pc_type", "mg") # Set PETSc MG type and levels PETScOptions.set("pc_mg_levels", len(V)) PETScOptions.set("pc_mg_galerkin") # Set smoother PETScOptions.set("mg_levels_ksp_type", "chebyshev") PETScOptions.set("mg_levels_pc_type", "jacobi") # Set tolerance and monitor residual PETScOptions.set("ksp_monitor_true_residual") PETScOptions.set("ksp_atol", 1.0e-12) PETScOptions.set("ksp_rtol", 1.0e-12) solver.set_from_options() # Get fine grid DM and attach fine grid DM to solver solver.set_dm(dm_collection.get_dm(-1)) solver.set_dm_active(False) # Solve x = PETScVector() solver.solve(x, b) # Check multigrid solution against LU solver solution solver = LUSolver(A) # noqa x_lu = PETScVector() solver.solve(x_lu, b) assert round((x - x_lu).norm("l2"), 10) == 0 # Clear all PETSc options from petsc4py import PETSc opts = PETSc.Options() for key in opts.getAll(): opts.delValue(key)
def test_vector_p1_3d(): meshc = UnitCubeMesh(MPI.comm_world, 2, 3, 4) meshf = UnitCubeMesh(MPI.comm_world, 3, 4, 5) Vc = VectorFunctionSpace(meshc, ("CG", 1)) Vf = VectorFunctionSpace(meshf, ("CG", 1)) def u(x): values0 = x[:, 0] + 2.0 * x[:, 1] values1 = 4.0 * x[:, 0] values2 = 3.0 * x[:, 2] + x[:, 0] return np.stack([values0, values1, values2], axis=1) uc, uf = Function(Vc), Function(Vf) uc.interpolate(u) uf.interpolate(u) mat = PETScDMCollection.create_transfer_matrix(Vc._cpp_object, Vf._cpp_object) Vuc = Function(Vf) mat.mult(uc.vector, Vuc.vector) diff = Vuc.vector diff.axpy(-1, uf.vector) assert diff.norm() < 1.0e-12
def test_vector_p1_3d(): meshc = UnitCubeMesh(MPI.comm_world, 2, 3, 4) meshf = UnitCubeMesh(MPI.comm_world, 3, 4, 5) Vc = VectorFunctionSpace(meshc, ("CG", 1)) Vf = VectorFunctionSpace(meshf, ("CG", 1)) @function.expression.numba_eval def expr_eval(values, x, cell_idx): values[:, 0] = x[:, 0] + 2.0 * x[:, 1] values[:, 1] = 4.0 * x[:, 0] values[:, 2] = 3.0 * x[:, 2] + x[:, 0] u = Expression(expr_eval, shape=(3, )) uc = interpolate(u, Vc) uf = interpolate(u, Vf) mat = PETScDMCollection.create_transfer_matrix(Vc._cpp_object, Vf._cpp_object) Vuc = Function(Vf) mat.mult(uc.vector(), Vuc.vector()) diff = Vuc.vector() diff.axpy(-1, uf.vector()) assert diff.norm() < 1.0e-12
def test_taylor_hood_cube(): pytest.xfail("Problem with Mixed Function Spaces") meshc = UnitCubeMesh(MPI.comm_world, 2, 2, 2) meshf = UnitCubeMesh(MPI.comm_world, 3, 4, 5) Ve = VectorElement("CG", meshc.ufl_cell(), 2) Qe = FiniteElement("CG", meshc.ufl_cell(), 1) Ze = MixedElement([Ve, Qe]) Zc = FunctionSpace(meshc, Ze) Zf = FunctionSpace(meshf, Ze) def z(values, x): values[:, 0] = x[:, 0] * x[:, 1] values[:, 1] = x[:, 1] * x[:, 2] values[:, 2] = x[:, 2] * x[:, 0] values[:, 3] = x[:, 0] + 3.0 * x[:, 1] + x[:, 2] zc = interpolate(z, Zc) zf = interpolate(z, Zf) mat = PETScDMCollection.create_transfer_matrix(Zc, Zf) Zuc = Function(Zf) mat.mult(zc.vector, Zuc.vector) Zuc.vector.update_ghost_values() diff = Function(Zf) diff.assign(Zuc - zf) assert diff.vector.norm("l2") < 1.0e-12
def test_taylor_hood_cube(): pytest.xfail("Problem with Mixed Function Spaces") meshc = UnitCubeMesh(MPI.comm_world, 2, 2, 2) meshf = UnitCubeMesh(MPI.comm_world, 3, 4, 5) Ve = VectorElement("CG", meshc.ufl_cell(), 2) Qe = FiniteElement("CG", meshc.ufl_cell(), 1) Ze = MixedElement([Ve, Qe]) Zc = FunctionSpace(meshc, Ze) Zf = FunctionSpace(meshf, Ze) z = Expression( ("x[0]*x[1]", "x[1]*x[2]", "x[2]*x[0]", "x[0] + 3*x[1] + x[2]"), degree=2) zc = interpolate(z, Zc) zf = interpolate(z, Zf) mat = PETScDMCollection.create_transfer_matrix(Zc, Zf) Zuc = Function(Zf) mat.mult(zc.vector(), Zuc.vector()) Zuc.vector().update_ghost_values() diff = Function(Zf) diff.assign(Zuc - zf) assert diff.vector().norm("l2") < 1.0e-12
def test_scalar_p1_scaled_mesh(): # Make coarse mesh smaller than fine mesh meshc = UnitCubeMesh(MPI.comm_world, 2, 2, 2) meshc.geometry.points *= 0.9 meshf = UnitCubeMesh(MPI.comm_world, 3, 4, 5) Vc = FunctionSpace(meshc, ("CG", 1)) Vf = FunctionSpace(meshf, ("CG", 1)) @function.expression.numba_eval def expr_eval(values, x, cell_idx): values[:, 0] = x[:, 0] + 2.0 * x[:, 1] + 3.0 * x[:, 2] u = Expression(expr_eval) uc = interpolate(u, Vc) uf = interpolate(u, Vf) mat = PETScDMCollection.create_transfer_matrix(Vc._cpp_object, Vf._cpp_object) Vuc = Function(Vf) mat.mult(uc.vector(), Vuc.vector()) diff = Vuc.vector() diff.axpy(-1, uf.vector()) assert diff.norm() < 1.0e-12 # Now make coarse mesh larger than fine mesh meshc.geometry.points *= 1.5 uc = interpolate(u, Vc) mat = PETScDMCollection.create_transfer_matrix(Vc._cpp_object, Vf._cpp_object) mat.mult(uc.vector(), Vuc.vector()) diff = Vuc.vector() diff.axpy(-1, uf.vector()) assert diff.norm() < 1.0e-12
def test_scalar_p1_scaled_mesh(): # Make coarse mesh smaller than fine mesh meshc = UnitCubeMesh(MPI.comm_world, 2, 2, 2) meshc.geometry.points *= 0.9 meshf = UnitCubeMesh(MPI.comm_world, 3, 4, 5) Vc = FunctionSpace(meshc, ("CG", 1)) Vf = FunctionSpace(meshf, ("CG", 1)) def u(x): return x[:, 0] + 2.0 * x[:, 1] + 3.0 * x[:, 2] uc, uf = Function(Vc), Function(Vf) uc.interpolate(u) uf.interpolate(u) mat = PETScDMCollection.create_transfer_matrix(Vc._cpp_object, Vf._cpp_object) Vuc = Function(Vf) mat.mult(uc.vector, Vuc.vector) diff = Vuc.vector diff.axpy(-1, uf.vector) assert diff.norm() < 1.0e-12 # Now make coarse mesh larger than fine mesh meshc.geometry.points *= 1.5 uc.interpolate(u) mat = PETScDMCollection.create_transfer_matrix(Vc._cpp_object, Vf._cpp_object) mat.mult(uc.vector, Vuc.vector) diff = Vuc.vector diff.axpy(-1, uf.vector) assert diff.norm() < 1.0e-12
def test_vector_p1_3d(): meshc = UnitCubeMesh(MPI.comm_world, 2, 3, 4) meshf = UnitCubeMesh(MPI.comm_world, 3, 4, 5) Vc = VectorFunctionSpace(meshc, "CG", 1) Vf = VectorFunctionSpace(meshf, "CG", 1) u = Expression(("x[0] + 2*x[1]", "4*x[0]", "3*x[2] + x[0]"), degree=1) uc = interpolate(u, Vc) uf = interpolate(u, Vf) mat = PETScDMCollection.create_transfer_matrix(Vc, Vf).mat() Vuc = Function(Vf) mat.mult(uc.vector().vec(), Vuc.vector().vec()) diff = Vuc.vector() diff.vec().axpy(-1, uf.vector().vec()) assert diff.norm(Norm.l2) < 1.0e-12
def test_vector_p2_2d(): meshc = UnitSquareMesh(MPI.comm_world, 5, 4) meshf = UnitSquareMesh(MPI.comm_world, 5, 8) Vc = VectorFunctionSpace(meshc, ("CG", 2)) Vf = VectorFunctionSpace(meshf, ("CG", 2)) u = Expression(("x[0] + 2*x[1]*x[0]", "4*x[0]*x[1]"), degree=2) uc = interpolate(u, Vc) uf = interpolate(u, Vf) mat = PETScDMCollection.create_transfer_matrix(Vc._cpp_object, Vf._cpp_object).mat() Vuc = Function(Vf) mat.mult(uc.vector().vec(), Vuc.vector().vec()) diff = Vuc.vector() diff.vec().axpy(-1, uf.vector().vec()) assert diff.norm(Norm.l2) < 1.0e-12
def test_scalar_p2(): meshc = UnitCubeMesh(MPI.comm_world, 2, 2, 2) meshf = UnitCubeMesh(MPI.comm_world, 3, 4, 5) Vc = FunctionSpace(meshc, ("CG", 2)) Vf = FunctionSpace(meshf, ("CG", 2)) u = Expression("x[0]*x[2] + 2*x[1]*x[0] + 3*x[2]", degree=2) uc = interpolate(u, Vc) uf = interpolate(u, Vf) mat = PETScDMCollection.create_transfer_matrix(Vc._cpp_object, Vf._cpp_object).mat() Vuc = Function(Vf) mat.mult(uc.vector().vec(), Vuc.vector().vec()) diff = Vuc.vector() diff.vec().axpy(-1, uf.vector().vec()) assert diff.norm(Norm.l2) < 1.0e-12
def test_scalar_p2(): meshc = UnitCubeMesh(MPI.comm_world, 2, 2, 2) meshf = UnitCubeMesh(MPI.comm_world, 3, 4, 5) Vc = FunctionSpace(meshc, ("CG", 2)) Vf = FunctionSpace(meshf, ("CG", 2)) def u(values, x): values[:, 0] = x[:, 0] + 2.0 * x[:, 1] + 3.0 * x[:, 2] uc = interpolate(u, Vc) uf = interpolate(u, Vf) mat = PETScDMCollection.create_transfer_matrix(Vc._cpp_object, Vf._cpp_object) Vuc = Function(Vf) mat.mult(uc.vector, Vuc.vector) diff = Vuc.vector diff.axpy(-1, uf.vector) assert diff.norm() < 1.0e-12
def test_vector_p2_2d(): meshc = UnitSquareMesh(MPI.comm_world, 5, 4) meshf = UnitSquareMesh(MPI.comm_world, 5, 8) Vc = VectorFunctionSpace(meshc, ("CG", 2)) Vf = VectorFunctionSpace(meshf, ("CG", 2)) def u(values, x): values[:, 0] = x[:, 0] + 2.0 * x[:, 1] values[:, 1] = 4.0 * x[:, 0] * x[:, 1] uc = interpolate(u, Vc) uf = interpolate(u, Vf) mat = PETScDMCollection.create_transfer_matrix(Vc._cpp_object, Vf._cpp_object) Vuc = Function(Vf) mat.mult(uc.vector, Vuc.vector) diff = Vuc.vector diff.axpy(-1, uf.vector) assert diff.norm() < 1.0e-12
def test_vector_p2_2d(): meshc = UnitSquareMesh(MPI.comm_world, 5, 4) meshf = UnitSquareMesh(MPI.comm_world, 5, 8) Vc = VectorFunctionSpace(meshc, ("CG", 2)) Vf = VectorFunctionSpace(meshf, ("CG", 2)) def u(x): return np.stack([x[:, 0] + 2.0 * x[:, 1], 4.0 * x[:, 0] * x[:, 1]], axis=1) uc, uf = Function(Vc), Function(Vf) uc.interpolate(u) uf.interpolate(u) mat = PETScDMCollection.create_transfer_matrix(Vc._cpp_object, Vf._cpp_object) Vuc = Function(Vf) mat.mult(uc.vector, Vuc.vector) diff = Vuc.vector diff.axpy(-1, uf.vector) assert diff.norm() < 1.0e-12
def test_vector_p2_2d(): meshc = UnitSquareMesh(MPI.comm_world, 5, 4) meshf = UnitSquareMesh(MPI.comm_world, 5, 8) Vc = VectorFunctionSpace(meshc, ("CG", 2)) Vf = VectorFunctionSpace(meshf, ("CG", 2)) @function.expression.numba_eval def expr_eval(values, x, cell_idx): values[:, 0] = x[:, 0] + 2.0 * x[:, 1] values[:, 1] = 4.0 * x[:, 0] * x[:, 1] u = Expression(expr_eval, shape=(2, )) uc = interpolate(u, Vc) uf = interpolate(u, Vf) mat = PETScDMCollection.create_transfer_matrix(Vc._cpp_object, Vf._cpp_object).mat() Vuc = Function(Vf) mat.mult(uc.vector().vec(), Vuc.vector().vec()) diff = Vuc.vector() diff.vec().axpy(-1, uf.vector().vec()) assert diff.norm(Norm.l2) < 1.0e-12
def test_scalar_p2(): meshc = UnitCubeMesh(MPI.comm_world, 2, 2, 2) meshf = UnitCubeMesh(MPI.comm_world, 3, 4, 5) Vc = FunctionSpace(meshc, ("CG", 2)) Vf = FunctionSpace(meshf, ("CG", 2)) @function.expression.numba_eval def expr_eval(values, x, cell_idx): values[:, 0] = x[:, 0] + 2.0 * x[:, 1] + 3.0 * x[:, 2] u = Expression(expr_eval) uc = interpolate(u, Vc) uf = interpolate(u, Vf) mat = PETScDMCollection.create_transfer_matrix(Vc._cpp_object, Vf._cpp_object).mat() Vuc = Function(Vf) mat.mult(uc.vector().vec(), Vuc.vector().vec()) diff = Vuc.vector() diff.vec().axpy(-1, uf.vector().vec()) assert diff.norm(Norm.l2) < 1.0e-12
def xtest_mg_solver_stokes(): mesh0 = UnitCubeMesh(2, 2, 2) mesh1 = UnitCubeMesh(4, 4, 4) mesh2 = UnitCubeMesh(8, 8, 8) Ve = VectorElement("CG", mesh0.ufl_cell(), 2) Qe = FiniteElement("CG", mesh0.ufl_cell(), 1) Ze = MixedElement([Ve, Qe]) Z0 = FunctionSpace(mesh0, Ze) Z1 = FunctionSpace(mesh1, Ze) Z2 = FunctionSpace(mesh2, Ze) W = Z2 # Boundaries def right(x, on_boundary): return x[0] > (1.0 - DOLFIN_EPS) def left(x, on_boundary): return x[0] < DOLFIN_EPS def top_bottom(x, on_boundary): return x[1] > 1.0 - DOLFIN_EPS or x[1] < DOLFIN_EPS # No-slip boundary condition for velocity noslip = Constant((0.0, 0.0, 0.0)) bc0 = DirichletBC(W.sub(0), noslip, top_bottom) # Inflow boundary condition for velocity inflow = Expression(("-sin(x[1]*pi)", "0.0", "0.0"), degree=2) bc1 = DirichletBC(W.sub(0), inflow, right) # Collect boundary conditions bcs = [bc0, bc1] # Define variational problem (u, p) = TrialFunctions(W) (v, q) = TestFunctions(W) f = Constant((0.0, 0.0, 0.0)) a = inner(grad(u), grad(v)) * dx + div(v) * p * dx + q * div(u) * dx L = inner(f, v) * dx # Form for use in constructing preconditioner matrix b = inner(grad(u), grad(v)) * dx + p * q * dx # Assemble system A, bb = assemble_system(a, L, bcs) # Assemble preconditioner system P, btmp = assemble_system(b, L, bcs) spaces = [Z0, Z1, Z2] dm_collection = PETScDMCollection(spaces) solver = PETScKrylovSolver() solver.set_operators(A, P) PETScOptions.set("ksp_type", "gcr") PETScOptions.set("pc_type", "mg") PETScOptions.set("pc_mg_levels", 3) PETScOptions.set("pc_mg_galerkin") PETScOptions.set("ksp_monitor_true_residual") PETScOptions.set("ksp_atol", 1.0e-10) PETScOptions.set("ksp_rtol", 1.0e-10) solver.set_from_options() from petsc4py import PETSc ksp = solver.ksp() ksp.setDM(dm_collection.dm()) ksp.setDMActive(False) x = PETScVector() solver.solve(x, bb) # Check multigrid solution against LU solver solver = LUSolver(A) # noqa x_lu = PETScVector() solver.solve(x_lu, bb) assert round((x - x_lu).norm("l2"), 10) == 0 # Clear all PETSc options opts = PETSc.Options() for key in opts.getAll(): opts.delValue(key)