|
| 1 | +Using fast diagonalisation solvers in Firedrake |
| 2 | +=============================================== |
| 3 | + |
| 4 | +In this demo we show how to efficiently solve the Poisson equation using |
| 5 | +high-order tensor-product elements. This is done through a special basis, |
| 6 | +obtained from the fast diagonalisation method (FDM). We first contruct an |
| 7 | +auxiliary operator that is sparse in this basis, with as many zeros as a |
| 8 | +low-order method. We then combine this with an additive Schwarz method. |
| 9 | +Finally, we show how to do static condensation using fieldsplit. |
| 10 | + |
| 11 | +Creating an Extruded mesh |
| 12 | +------------------------- |
| 13 | + |
| 14 | +The fast diagonalisation method produces a basis of discrete eigenfunctions. |
| 15 | +These are polynomials, and can be efficiently computed on tensor |
| 16 | +product-elements by solving an eigenproblem on the interval. Therefore, we will |
| 17 | +require quadrilateral or hexahedral meshes. Currently, the solver only supports |
| 18 | +extruded hexahedral meshes, so we must create an :func:`~ExtrudedMesh`. :: |
| 19 | + |
| 20 | + from firedrake import * |
| 21 | + |
| 22 | + base = UnitSquareMesh(8, 8, quadrilateral=True) |
| 23 | + mesh = ExtrudedMesh(base, 8) |
| 24 | + |
| 25 | +Defining the problem: the Poisson equation |
| 26 | +------------------------------------------ |
| 27 | + |
| 28 | +Having defined the mesh we now need to set up our problem. The crucial step |
| 29 | +for fast diagonalisation is a special choice of basis functions. We obtain them |
| 30 | +by passing `variant="fdm"` to the :func:`~FunctionSpace` constructor. |
| 31 | +The solvers in this demo work also with other element variants, but |
| 32 | +each iteration would involve an additional a basis transformation. |
| 33 | +We then define the Poisson problem in the usual way. :: |
| 34 | + |
| 35 | + degree = 5 |
| 36 | + V = FunctionSpace(mesh, "Q", degree, variant="fdm") |
| 37 | + |
| 38 | + u = TrialFunction(V) |
| 39 | + v = TestFunction(V) |
| 40 | + |
| 41 | + a = inner(grad(u), grad(v))*dx |
| 42 | + |
| 43 | + bcs = [DirichletBC(V, 0, sub) for sub in ("on_boundary", "top", "bottom")] |
| 44 | + |
| 45 | +To stress-test the solver, we prescribe a random :class:`~.Cofunction` as |
| 46 | +right-hand side :: |
| 47 | + |
| 48 | + rg = RandomGenerator(PCG64(seed=123456789)) |
| 49 | + L = rg.uniform(V.dual(), -1, 1) |
| 50 | + |
| 51 | +We'll demonstrate a few different sets of solver parameters, so let's define a |
| 52 | +function that takes in set of parameters and uses them on a :class:`~.LinearVariationalSolver`. :: |
| 53 | + |
| 54 | + def run_solve(parameters): |
| 55 | + uh = Function(V) |
| 56 | + problem = LinearVariationalProblem(a, L, uh, bcs=bcs) |
| 57 | + solver = LinearVariationalSolver(problem, solver_parameters=parameters) |
| 58 | + solver.solve() |
| 59 | + iterations = solver.snes.getLinearSolveIterations() |
| 60 | + print("Iterations", iterations) |
| 61 | + |
| 62 | +Specifying the solver |
| 63 | +~~~~~~~~~~~~~~~~~~~~~ |
| 64 | + |
| 65 | +The solver avoids the assembly of a matrix with dense elements submatrices, and |
| 66 | +insteads applies a matrix-free conjugate gradient method with a preconditioner |
| 67 | +obtained by assembling a sparse matrix. This is done through the python type |
| 68 | +preconditioner :class:`~.FDMPC`. We define a function that enables us to |
| 69 | +compose :class:`~.FDMPC` with an inner preconditioner. :: |
| 70 | + |
| 71 | + def fdm_params(relax): |
| 72 | + return { |
| 73 | + "mat_type": "matfree", |
| 74 | + "ksp_type": "cg", |
| 75 | + "ksp_monitor": None, |
| 76 | + "pc_type": "python", |
| 77 | + "pc_python_type": "firedrake.FDMPC", |
| 78 | + "fdm": relax, |
| 79 | + } |
| 80 | + |
| 81 | +Let's start with our first test. We'll confirm a working solve by |
| 82 | +using a sparse direct LU factorization. :: |
| 83 | + |
| 84 | + lu_params = { |
| 85 | + "pc_type": "lu", |
| 86 | + "pc_factor_mat_solver_type": "mumps", |
| 87 | + } |
| 88 | + |
| 89 | + print('FDM + LU') |
| 90 | + run_solve(fdm_params(lu_params)) |
| 91 | + |
| 92 | +Moving on to a more complicated solver, we'll employ a two-level with a Q1 |
| 93 | +coarse space via :class:`~.P1PC`. As the fine level relaxation we define an |
| 94 | +additive Scharz method on vertex-star patches implemented via |
| 95 | +:class:`~.ASMExtrudedStarPC` as we have an extruded mesh:: |
| 96 | + |
| 97 | + asm_params = { |
| 98 | + "pc_type": "python", |
| 99 | + "pc_python_type": "firedrake.P1PC", |
| 100 | + "pmg_mg_coarse_mat_type": "aij", |
| 101 | + "pmg_mg_coarse": lu_params, |
| 102 | + "pmg_mg_levels": { |
| 103 | + "ksp_max_it": 1, |
| 104 | + "ksp_type": "chebyshev", |
| 105 | + "ksp_chebyshev_esteig": "0.125,0.625,0.125,1.125", |
| 106 | + "ksp_convergence_test": "skip", |
| 107 | + "pc_type": "python", |
| 108 | + "pc_python_type": "firedrake.ASMExtrudedStarPC", |
| 109 | + "sub_sub_pc_type": "lu", |
| 110 | + }, |
| 111 | + } |
| 112 | + |
| 113 | + print('FDM + ASM') |
| 114 | + run_solve(fdm_params(asm_params)) |
| 115 | + |
| 116 | +Static condensation |
| 117 | +------------------- |
| 118 | + |
| 119 | +Finally, we construct :class:`~.FDMPC` solver parameters using static |
| 120 | +condensation. The fast diagonalisation basis diagonalizes the operator on cell |
| 121 | +interiors. So we define a solver that splits the interior and facet degrees of |
| 122 | +freedom via :class:`~.FacetSplitPC` and fieldsplit options. We set the option |
| 123 | +`fdm_static_condensation` to tell :class:`~.FDMPC` to assemble a 2-by-2 block |
| 124 | +preconditioner where the lower-right block is replaced by the Schur complement |
| 125 | +resulting from eliminating the interior degrees of freedom. We use |
| 126 | +point-Jacobi to invert the diagonal, and we may apply the two-level additive |
| 127 | +Schwarz method on the facets. :: |
| 128 | + |
| 129 | + def fdm_static_condensation_params(relax): |
| 130 | + return { |
| 131 | + "mat_type": "matfree", |
| 132 | + "ksp_type": "cg", |
| 133 | + "ksp_monitor": None, |
| 134 | + "pc_type": "python", |
| 135 | + "pc_python_type": "firedrake.FacetSplitPC", |
| 136 | + "facet_pc_type": "python", |
| 137 | + "facet_pc_python_type": "firedrake.FDMPC", |
| 138 | + "facet_fdm_static_condensation": True, |
| 139 | + "facet_fdm_pc_use_amat": False, |
| 140 | + "facet_fdm_pc_type": "fieldsplit", |
| 141 | + "facet_fdm_pc_fieldsplit_type": "symmetric_multiplicative", |
| 142 | + "facet_fdm_fieldsplit_ksp_type": "preonly", |
| 143 | + "facet_fdm_fieldsplit_0_pc_type": "jacobi", |
| 144 | + "facet_fdm_fieldsplit_1": relax, |
| 145 | + } |
| 146 | + |
| 147 | + print('FDM + SC + ASM') |
| 148 | + run_solve(fdm_static_condensation_params(asm_params)) |
| 149 | + |
| 150 | +A runnable python version of this demo can be found :demo:`here |
| 151 | +<fast_diagonalisation_poisson.py>`. |
0 commit comments