Exemple #1
0
    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)
Exemple #2
0
    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)
Exemple #3
0
    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)
Exemple #4
0
    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)
Exemple #5
0
    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)
Exemple #6
0
#----------------------------------------------------------------------------
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()
Exemple #7
0
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)))
Exemple #8
0
uv0 = np.zeros(basis["u"].N)
inlet_dofs = basis["u"].get_dofs(mesh.boundaries["inlet"]).all("u^1")
inlet_y_lim = [p.x[1] for p in channel.lines[3].points[::-1]]
monic = np.polynomial.polynomial.Polynomial.fromroots(inlet_y_lim)
uv0[inlet_dofs] = -6 * monic(basis["u"].doflocs[1, inlet_dofs]) / inlet_y_lim[1] ** 2


def embed(xy: np.ndarray) -> np.ndarray:
    return np.pad(xy, ((0, 0), (0, 1)), "constant")


solvers = [
    pyamgcl.solver(
        pyamgcl.amg(
            skfem.condense(K_lhs, D=dirichlet["u"], expand=False),
            {"coarsening.type": "aggregation", "relax.type": "ilu0"},
        ),
        {"type": "cg"},
    ),
    pyamgcl.solver(
        pyamgcl.amg(skfem.condense(L["p"], D=dirichlet["p"], expand=False)),
        {"type": "cg"},
    ),
    pyamgcl.solver(
        pyamgcl.amg(
            skfem.condense(M / dt, D=dirichlet["u"], expand=False),
            {"relax.type": "gauss_seidel"},
        ),
        {"type": "cg"},
    ),
]
Exemple #9
0
                    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)))