def test_preconditioner(self): A, rhs = make_problem(100) for rtype in ('spai0', 'ilu0'): for P in (amg.amg(A, prm={'relax.type': rtype}), amg.relaxation(A, prm={'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'): for P in ( amg.amg(A, prm={'relax.type': rtype}), amg.relaxation(A, prm={'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'): for P in ( amg.amg(A, prm={'relax.type': rtype}), amg.relaxation(A, prm={'type': rtype}) ): # Setup solver solve = amg.solver(P, prm=dict(type=stype, tol=1e-3, maxiter=1000)) # Solve x = solve(rhs) # Check residual self.assertTrue(norm(rhs - A * x) / norm(rhs) < 1e-3) # Solve again, now provide system matrix explicitly x = solve(A, rhs) # 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'): for P in (amg.amg(A, prm={'relax.type': rtype}), amg.relaxation(A, prm={'type': rtype})): # Setup solver solve = amg.solver(P, prm=dict(type=stype, tol=1e-3, maxiter=1000)) # Solve x = solve(rhs) # Check residual self.assertTrue(norm(rhs - A * x) / norm(rhs) < 1e-3) # Solve again, now provide system matrix explicitly x = solve(A, rhs) # Check residual self.assertTrue(norm(rhs - A * x) / norm(rhs) < 1e-3)
#---------------------------------------------------------------------------- if args.A: with timeit('Read problem'): A = mmread(args.A) f = mmread(args.f).flatten() if args.f else ones(A.rows()) else: with timeit('Generate problem'): A,f = make_poisson(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.relaxation(A, p_prm) if args.single else amg.amg(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()
parser.add_argument('-p,--prm', dest='p', help='AMGCL parameters: key1=val1 key2=val2', nargs='+', default=[]) parser.add_argument('-1,--single-level', dest='single', help='Use single level relaxation as preconditioner', action='store_true', default=False) 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.rows()) else: A,f = make_poisson(args.n) # Parse parameters prm = {p[0]: p[1] for p in map(lambda s: s.split('='), args.p)} # Create preconditioner P = amg.relaxation(A, prm) if args.single else amg.amg(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))) # Save the solution if args.x: mmwrite(args.x, x.reshape((-1,1)))
default=False) 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.rows()) else: A, f = make_poisson(args.n) # Parse parameters prm = {p[0]: p[1] for p in map(lambda s: s.split('='), args.p)} # Create preconditioner P = amg.relaxation(A, prm) if args.single else amg.amg(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)))