|
| 1 | +from firedrake import * |
| 2 | +from firedrake.petsc import PETSc |
| 3 | +import numpy |
| 4 | + |
| 5 | + |
| 6 | +def test_zero_initial_guess(): |
| 7 | + mesh = UnitIntervalMesh(10) |
| 8 | + space = FunctionSpace(mesh, "CG", 1) |
| 9 | + test = TestFunction(space) |
| 10 | + trial = TrialFunction(space) |
| 11 | + |
| 12 | + solver = LinearSolver(assemble(inner(trial, test) * dx), |
| 13 | + solver_parameters={"ksp_type": "preonly", |
| 14 | + "pc_type": "jacobi", |
| 15 | + "ksp_max_it": 1, |
| 16 | + "ksp_initial_guess_nonzero": False}) |
| 17 | + b = assemble(inner(Constant(1), test) * dx) |
| 18 | + |
| 19 | + u1 = Function(space, name="u1") |
| 20 | + u1.assign(0) |
| 21 | + solver.solve(u1, b) |
| 22 | + |
| 23 | + u2 = Function(space, name="u2") |
| 24 | + u2.assign(1) |
| 25 | + solver.solve(u2, b) |
| 26 | + assert numpy.allclose(u1.dat.data_ro, u2.dat.data_ro) |
| 27 | + |
| 28 | + |
| 29 | +def test_convergence_error_update(): |
| 30 | + mesh = UnitIntervalMesh(10) |
| 31 | + space = FunctionSpace(mesh, "CG", 1) |
| 32 | + test = TestFunction(space) |
| 33 | + trial = TrialFunction(space) |
| 34 | + |
| 35 | + solver = LinearSolver(assemble(inner(trial, test) * dx), |
| 36 | + solver_parameters={"ksp_type": "cg", |
| 37 | + "pc_type": "none", |
| 38 | + "ksp_max_it": 1, |
| 39 | + "ksp_atol": 1.0e-2}) |
| 40 | + b = assemble(inner(Constant(1), test) * dx) |
| 41 | + |
| 42 | + u = Function(space, name="u") |
| 43 | + u.assign(-1) |
| 44 | + uinit = Function(u, name="uinit") |
| 45 | + try: |
| 46 | + solver.solve(u, b) |
| 47 | + except firedrake.exceptions.ConvergenceError: |
| 48 | + assert solver.ksp.getConvergedReason() == PETSc.KSP.ConvergedReason.DIVERGED_MAX_IT |
| 49 | + |
| 50 | + assert not numpy.allclose(u.dat.data_ro, uinit.dat.data_ro) |
0 commit comments