# Set matrix operator solver.setOperators(A) # Compute solution solver.setMonitor(lambda ksp, its, rnorm: print( "Iteration: {}, rel. residual: {}".format(its, rnorm))) solver.solve(b, u.vector) solver.view() # Save solution to XDMF format file = XDMFFile(MPI.comm_world, "elasticity.xdmf") file.write(u) unorm = u.vector.norm() if MPI.rank(mesh.mpi_comm()) == 0: print("Solution vector norm:", unorm) # Save colored mesh partitions in VTK format if running in parallel # if MPI.size(mesh.mpi_comm()) > 1: # File("partitions.pvd") << MeshFunction("size_t", mesh, mesh.topology.dim, \ # MPI.rank(mesh.mpi_comm())) # Project and write stress field to post-processing file # W = TensorFunctionSpace(mesh, "Discontinuous Lagrange", 0) # stress = project(sigma(u), V=W) # File("stress.pvd") << stress # Plot solution # import matplotlib.pyplot as plt # import dolfinx.plotting
# Use Chebyshev smoothing for multigrid opts["mg_levels_ksp_type"] = "chebyshev" opts["mg_levels_pc_type"] = "jacobi" # Improve estimate of eigenvalues for Chebyshev smoothing opts["mg_levels_esteig_ksp_type"] = "cg" opts["mg_levels_ksp_chebyshev_esteig_steps"] = 20 # Create CG Krylov solver and turn convergence monitoring on solver = PETSc.KSP().create(MPI.COMM_WORLD) solver.setFromOptions() # Set matrix operator solver.setOperators(A) # Compute solution solver.setMonitor(lambda ksp, its, rnorm: print( "Iteration: {}, rel. residual: {}".format(its, rnorm))) solver.solve(b, u.vector) solver.view() # Save solution to XDMF format with XDMFFile(MPI.COMM_WORLD, "elasticity.xdmf", "w") as file: file.write_mesh(mesh) file.write_function(u) unorm = u.vector.norm() if mesh.mpi_comm().rank == 0: print("Solution vector norm:", unorm)