|
| 1 | + |
| 2 | +struct _HPDDMLinearSolver <: LinearSolver |
| 3 | + ranks :: MPIArray |
| 4 | + mat :: PETScMatrix |
| 5 | + is :: PETScIndexSet |
| 6 | + ksp_setup :: Function |
| 7 | + pc_setup :: Function |
| 8 | + tols :: SolverTolerances{Float64} |
| 9 | +end |
| 10 | + |
| 11 | +SolverInterfaces.get_solver_tolerances(s::_HPDDMLinearSolver) = s.tols |
| 12 | + |
| 13 | +""" |
| 14 | + HPDDMLinearSolver( |
| 15 | + ranks::MPIArray,mat::PETScMatrix,is::PETScIndexSet[,ksp_setup[,pc_setup]]; |
| 16 | + maxiter=500, atol=1.e-12, rtol=1.e-8 |
| 17 | + ) |
| 18 | +
|
| 19 | +Wrapper for a `PETScLinearSolver` preconditioned with the HPDDM library. |
| 20 | +
|
| 21 | +# Arguments |
| 22 | +
|
| 23 | +- `indices::MPIArray`: For each rank, the local-to-global index map for the matrix rows/cols. |
| 24 | +- `mats::MPIArray`: For each rank, the matrix for the local overlapping Neumann problem. |
| 25 | +- `ksp_setup::Function`: PETSc setup options for the KSP solver. |
| 26 | +- `pc_setup::Function`: Extra setup options for the PCHPDDM preconditioner. |
| 27 | +
|
| 28 | +The defaults for `ksp_setup` and `pc_setup` set options from the command line, using the |
| 29 | + following functions: |
| 30 | +
|
| 31 | +```julia |
| 32 | +function hpddm_default_setup_ksp(ksp::Ref{PETSC.KSP}) |
| 33 | + @check_error_code GridapPETSc.PETSC.KSPSetFromOptions(ksp[]) |
| 34 | +end |
| 35 | +
|
| 36 | +function hpddm_default_setup_pc(pc::Ref{PETSC.PC}) |
| 37 | + @check_error_code PETSC.PCSetFromOptions(pc[]) |
| 38 | +end |
| 39 | +``` |
| 40 | +
|
| 41 | +To modify the default setup, you can pass your own functions (with the same signatures) |
| 42 | +as arguments to the constructor. |
| 43 | +""" |
| 44 | +function GridapSolvers.HPDDMLinearSolver( |
| 45 | + indices::MPIArray,mats::MPIArray, |
| 46 | + ksp_setup::Function = hpddm_default_setup_ksp, |
| 47 | + pc_setup::Function = hpddm_default_setup_pc; |
| 48 | + maxiter=500, atol=1.e-12, rtol=1.e-8 |
| 49 | +) |
| 50 | + ranks = linear_indices(mats) |
| 51 | + is = PETScIndexSet(PartitionedArrays.getany(indices)) |
| 52 | + mat = PETScMatrix(PartitionedArrays.getany(mats)) |
| 53 | + tols = SolverTolerances{Float64}(;maxiter=maxiter,atol=atol,rtol=rtol) |
| 54 | + _HPDDMLinearSolver(ranks,mat,is,ksp_setup,pc_setup,tols) |
| 55 | +end |
| 56 | + |
| 57 | +""" |
| 58 | + HPDDMLinearSolver(space::FESpace,biform::Function[,args...];kwargs...) |
| 59 | +
|
| 60 | +Creates a `HPDDMLinearSolver` from a finite element space and a bilinear form for the local overlapping |
| 61 | +Neumann problems. The extra arguments are the same as for the low-level constructor. |
| 62 | +
|
| 63 | +To have overlapping Neumann problems, the `Measure` has to be modified to include ghost cells. |
| 64 | +For instance, for a Poisson problem we would have: |
| 65 | +
|
| 66 | +```julia |
| 67 | +Ωg = Triangulation(with_ghost,model) |
| 68 | +dΩg = Measure(Ωg,qdegree) |
| 69 | +a(u,v) = ∫(∇(u)⋅∇(v))dΩg |
| 70 | +``` |
| 71 | +""" |
| 72 | +function GridapSolvers.HPDDMLinearSolver( |
| 73 | + space::FESpace,biform::Function, |
| 74 | + ksp_setup::Function = hpddm_default_setup_ksp, |
| 75 | + pc_setup::Function = hpddm_default_setup_pc; |
| 76 | + kwargs... |
| 77 | +) |
| 78 | + assems = map(local_views(space)) do space |
| 79 | + SparseMatrixAssembler( |
| 80 | + SparseMatrixCSR{0,PetscScalar,PetscInt},Vector{PetscScalar},space,space |
| 81 | + ) |
| 82 | + end |
| 83 | + indices, mats = subassemble_matrices(space,biform,assems) |
| 84 | + HPDDMLinearSolver(indices,mats,ksp_setup,pc_setup;kwargs...) |
| 85 | +end |
| 86 | + |
| 87 | +function subassemble_matrices(space,biform,assems) |
| 88 | + |
| 89 | + u, v = get_trial_fe_basis(space), get_fe_basis(space) |
| 90 | + data = collect_cell_matrix(space,space,biform(u,v)) |
| 91 | + |
| 92 | + indices = local_to_global(get_free_dof_ids(space)) |
| 93 | + mats = map(assemble_matrix, assems, data) |
| 94 | + |
| 95 | + return indices, mats |
| 96 | +end |
| 97 | + |
| 98 | +struct HPDDMLinearSolverSS <: SymbolicSetup |
| 99 | + solver::_HPDDMLinearSolver |
| 100 | +end |
| 101 | + |
| 102 | +function Algebra.symbolic_setup(solver::_HPDDMLinearSolver,mat::AbstractMatrix) |
| 103 | + HPDDMLinearSolverSS(solver) |
| 104 | +end |
| 105 | + |
| 106 | +function Algebra.numerical_setup(ss::HPDDMLinearSolverSS,A::AbstractMatrix) |
| 107 | + B = convert(PETScMatrix,A) |
| 108 | + ns = PETScLinearSolverNS(A,B) |
| 109 | + @check_error_code PETSC.KSPCreate(B.comm,ns.ksp) |
| 110 | + @check_error_code PETSC.KSPSetOperators(ns.ksp[],ns.B.mat[],ns.B.mat[]) |
| 111 | + hpddm_setup(ss.solver,ns.ksp) |
| 112 | + @check_error_code PETSC.KSPSetUp(ns.ksp[]) |
| 113 | + GridapPETSc.Init(ns) |
| 114 | +end |
| 115 | + |
| 116 | +function hpddm_default_setup_ksp(ksp) |
| 117 | + @check_error_code PETSC.KSPSetFromOptions(ksp[]) |
| 118 | +end |
| 119 | + |
| 120 | +function hpddm_default_setup_pc(pc) |
| 121 | + @check_error_code PETSC.PCSetFromOptions(pc[]) |
| 122 | +end |
| 123 | + |
| 124 | +function hpddm_setup(solver::_HPDDMLinearSolver,ksp) |
| 125 | + solver.ksp_setup(ksp) |
| 126 | + |
| 127 | + tols = solver.tols |
| 128 | + rtol = PetscScalar(tols.rtol) |
| 129 | + atol = PetscScalar(tols.atol) |
| 130 | + dtol = PetscScalar(tols.dtol) |
| 131 | + maxits = PetscInt(tols.maxiter) |
| 132 | + @check_error_code PETSC.KSPSetTolerances(ksp[], rtol, atol, dtol, maxits) |
| 133 | + |
| 134 | + pc = Ref{PETSC.PC}() |
| 135 | + mat, is = solver.mat.mat, solver.is.is |
| 136 | + @check_error_code PETSC.KSPGetPC(ksp[],pc) |
| 137 | + @check_error_code PETSC.PCSetType(pc[],PETSC.PCHPDDM) |
| 138 | + @check_error_code PETSC.PCHPDDMSetAuxiliaryMat(pc[],is[],mat[],C_NULL,C_NULL) |
| 139 | + @check_error_code PETSC.PCHPDDMHasNeumannMat(pc[],PETSC.PETSC_TRUE) |
| 140 | + @check_error_code PETSC.PCHPDDMSetSTShareSubKSP(pc[],PETSC.PETSC_TRUE) |
| 141 | + |
| 142 | + solver.pc_setup(pc) |
| 143 | +end |
0 commit comments