def build_pc_amgcl(A: spmatrix, **kwargs) -> LinearOperator: """AMG preconditioner""" if hasattr(pyamgcl, 'amgcl'): # v. 1.3.99+ return pyamgcl.amgcl(A, **kwargs) else: return pyamgcl.amg(A, **kwargs)
def test_preconditioner(self): A, rhs = make_problem(100) for rtype in ('spai0', 'ilu0'): P = amg.amgcl(A, prm={'relax.type': rtype}) # Solve x,info = bicgstab(A, rhs, M=P, tol=1e-3) self.assertTrue(info == 0) # Check residual self.assertTrue(norm(rhs - A * x) / norm(rhs) < 1e-3)
def test_preconditioner(self): A, rhs = make_problem(100) for rtype in ('spai0', 'ilu0'): P = amg.amgcl(A, prm={'relax.type': rtype}) # Solve x, info = bicgstab(A, rhs, M=P, tol=1e-3) self.assertTrue(info == 0) # Check residual self.assertTrue(norm(rhs - A * x) / norm(rhs) < 1e-3)
def test_solver(self): A, rhs = make_problem(100) for stype in ('bicgstab', 'lgmres'): for rtype in ('spai0', 'ilu0'): P = amg.amgcl(A, prm={'relax.type': rtype}) # Setup solver solve = amg.solver(P, prm=dict(type=stype, tol=1e-3, maxiter=1000)) # Solve x = solve(rhs)
#---------------------------------------------------------------------------- if args.A: with timeit('Read problem'): A = mmread(args.A) f = mmread(args.f).flatten() if args.f else np.ones(A.shape[0]) else: with timeit('Generate problem'): A,f = make_poisson_3d(args.n) # Parse parameters prm = {p[0]: p[1] for p in map(lambda s: s.split('='), args.p)} # Create preconditioner with timeit('Setup solver'): P = amg.amgcl(A, prm) print(P) iters = [0] def count_iters(x): iters[0] += 1 # Solve the system for the RHS with timeit('Solve the problem'): x,info = lgmres(A, f, M=P, maxiter=100, tol=1e-8, atol=1e-8, callback=count_iters) print('{0}: {1:.6e}'.format(iters[0], np.linalg.norm(f - A * x) / np.linalg.norm(f))) # Save the solution if args.x: with timeit('Save the result'):
def build_pc_amg(A: spmatrix, **kwargs) -> LinearOperator: """AMG preconditioner""" return aslinearoperator(amgcl(A, **kwargs))
#---------------------------------------------------------------------------- if args.A: with timeit('Read problem'): A = mmread(args.A) f = mmread(args.f).flatten() if args.f else np.ones(A.shape[0]) else: with timeit('Generate problem'): A,f = make_poisson_3d(args.n) # Parse parameters p_prm = {p[0]: p[1] for p in map(lambda s: s.split('='), args.p)} s_prm = {p[0]: p[1] for p in map(lambda s: s.split('='), args.s)} # Create solver/preconditioner pair with timeit('Setup solver'): S = amg.solver(amg.amgcl(A, p_prm), s_prm) print(S) # Solve the system for the RHS with timeit('Solve the problem'): x = S(f) error = np.linalg.norm(f - A * x) / np.linalg.norm(f) print("{0.iters}: {0.error:.6e} / {1:.6e}".format(S, error)) # Save the solution if args.x: with timeit('Save the result'): mmwrite(args.x, x.reshape((-1,1))) timeit.report()
default=[]) args = parser.parse_args(sys.argv[1:]) #---------------------------------------------------------------------------- if args.A: A = mmread(args.A) f = mmread(args.f).flatten() if args.f else np.ones(A.shape[0]) else: A, f = make_poisson_3d(args.n) # Parse parameters prm = {p[0]: p[1] for p in map(lambda s: s.split('='), args.p)} # Create preconditioner P = amg.amgcl(A, prm) print(P) iters = [0] def count_iters(x): iters[0] += 1 # Solve the system for the RHS x, info = lgmres(A, f, M=P, maxiter=100, tol=1e-8, callback=count_iters) print('{0}: {1:.6e}'.format(iters[0], np.linalg.norm(f - A * x) / np.linalg.norm(f)))
#---------------------------------------------------------------------------- if args.A: with timeit('Read problem'): A = mmread(args.A) f = mmread(args.f).flatten() if args.f else np.ones(A.shape[0]) else: with timeit('Generate problem'): A, f = make_poisson_3d(args.n) # Parse parameters p_prm = {p[0]: p[1] for p in map(lambda s: s.split('='), args.p)} s_prm = {p[0]: p[1] for p in map(lambda s: s.split('='), args.s)} # Create solver/preconditioner pair with timeit('Setup solver'): S = amg.solver(amg.amgcl(A, p_prm), s_prm) print(S) # Solve the system for the RHS with timeit('Solve the problem'): x = S(f) error = np.linalg.norm(f - A * x) / np.linalg.norm(f) print("{0.iters}: {0.error:.6e} / {1:.6e}".format(S, error)) # Save the solution if args.x: with timeit('Save the result'): mmwrite(args.x, x.reshape((-1, 1))) timeit.report()