def test_petsc4py_matrix(pushpop_parameters): "Test PETScMatrix <-> petsc4py.PETSc.Mat conversions" parameters["linear_algebra_backend"] = "PETSc" # Assemble a test matrix mesh = UnitSquareMesh(4, 4) V = FunctionSpace(mesh, "Lagrange", 1) u, v = TrialFunction(V), TestFunction(V) a = u * v * dx A1 = assemble(a) # Test conversion dolfin.PETScMatrix -> petsc4py.PETSc.Mat A1 = as_backend_type(A1) M1 = A1.mat() # Copy and scale matrix with petsc4py M2 = M1.copy() M2.scale(2.0) # Test conversion petsc4py.PETSc.Mat -> PETScMatrix A2 = PETScMatrix(M2) assert (A1.array() * 2.0 == A2.array()).all()
def test_petsc4py_matrix(pushpop_parameters): "Test PETScMatrix <-> petsc4py.PETSc.Mat conversions" parameters["linear_algebra_backend"] = "PETSc" # Assemble a test matrix mesh = UnitSquareMesh(4, 4) V = FunctionSpace(mesh, "Lagrange", 1) u, v = TrialFunction(V), TestFunction(V) a = u*v*dx A1 = assemble(a) # Test conversion dolfin.PETScMatrix -> petsc4py.PETSc.Mat A1 = as_backend_type(A1) M1 = A1.mat() # Copy and scale matrix with petsc4py M2 = M1.copy() M2.scale(2.0) # Test conversion petsc4py.PETSc.Mat -> PETScMatrix A2 = PETScMatrix(M2) assert (A1.array()*2.0 == A2.array()).all()
if __name__ == '__main__': from dolfin import * from weak_bcs.la_solve import my_eigvalsh mesh_c = UnitSquareMesh(1, 1) mesh_f = adapt(mesh_c) Vc = FunctionSpace(mesh_c, 'CG', 1) Vf = FunctionSpace(mesh_f, 'CG', 1) Jmat = injection_matrix(Vc, Vf) J = PETScMatrix(Jmat) f = Expression('x[0]+x[1]', degree=1) x = interpolate(f, Vc).vector().get_local() y = J.array().dot(x) # Inject coarse into fine y0 = interpolate(f, Vf).vector().get_local() print(np.linalg.norm(y0 - y)) # fc = sum(ff_i*c_i # c_i = dof_fi(phic_j) out = None for n in (2, 4, 8, 16, 32): mesh_c = UnitSquareMesh(n, n) A, B, W = stokes_iir(mesh_c) A = ii_convert(A).array() B = ii_convert(B).array() lmin, lmax = np.sort(np.abs(my_eigvalsh(A, B)))[[0, -1]]