Skip to content

Commit 2799327

Browse files
committed
Add ability to change preconditioner and add AMG
1 parent 83c6bfe commit 2799327

1 file changed

Lines changed: 37 additions & 4 deletions

File tree

src/heatflow/mod_petsc_solver.f90

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ module petsc_solver
77
private
88
public :: petsc_init, petsc_finalize, solve_petsc_csr, petsc_cleanup
99

10+
! ===== PRECONDITIONER SELECTION =====
11+
! Change this to switch between preconditioners:
12+
! 'GAMG' = Algebraic Multigrid (best for elliptic PDEs, 10-20x faster)
13+
! 'ILU' = Incomplete LU (good general purpose, robust)
14+
! 'LU' = Direct solver (most robust, uses more memory)
15+
character(len=10), parameter :: PRECONDITIONER = 'GAMG' ! <-- Change here!
16+
! ====================================
17+
1018
! Persistent PETSc objects (reused across timesteps for memory efficiency)
1119
Mat, save :: A_saved = PETSC_NULL_MAT
1220
Vec, save :: bb_saved = PETSC_NULL_VEC
@@ -116,10 +124,35 @@ subroutine solve_petsc_csr(n, ia, ja, aval, b, x, rtol, maxit)
116124
call KSPCreate(PETSC_COMM_SELF, ksp_saved, ierr)
117125
call KSPSetOperators(ksp_saved, A_saved, A_saved, ierr)
118126
call KSPGetPC(ksp_saved, pc, ierr)
119-
! Use ILU preconditioner for better conditioning (especially for high conductivity materials)
120-
call PCSetType(pc, PCILU, ierr) ! ILU preconditioner (better than Jacobi)
121-
! call PCSetType(pc, PCJACOBI, ierr) ! Jacobi preconditioner (simple)
122-
call KSPSetType(ksp_saved, KSPBCGS, ierr) ! BiCGSTAB solver
127+
128+
! Select preconditioner based on parameter at top of module
129+
select case (trim(PRECONDITIONER))
130+
case ('GAMG')
131+
! Algebraic Multigrid - Best for elliptic PDEs with varying coefficients
132+
! Optimal O(1) iterations, 10-20x faster than ILU for large problems
133+
call PCSetType(pc, PCGAMG, ierr)
134+
call KSPSetType(ksp_saved, KSPGMRES, ierr) ! GMRES works well with AMG
135+
write(*,'(A)') ' [Solver] Using GAMG (Algebraic Multigrid) preconditioner with GMRES'
136+
137+
case ('ILU')
138+
! Incomplete LU - Good general purpose, robust
139+
call PCSetType(pc, PCILU, ierr)
140+
call KSPSetType(ksp_saved, KSPBCGS, ierr) ! BiCGSTAB works well with ILU
141+
write(*,'(A)') ' [Solver] Using ILU preconditioner with BiCGSTAB'
142+
143+
case ('LU')
144+
! Direct LU - Most robust, more memory intensive
145+
call PCSetType(pc, PCLU, ierr)
146+
call KSPSetType(ksp_saved, KSPPREONLY, ierr) ! Direct solve
147+
write(*,'(A)') ' [Solver] Using direct LU solver'
148+
149+
case default
150+
write(*,'(A,A)') ' [Warning] Unknown preconditioner: ', trim(PRECONDITIONER)
151+
write(*,'(A)') ' Defaulting to ILU'
152+
call PCSetType(pc, PCILU, ierr)
153+
call KSPSetType(ksp_saved, KSPBCGS, ierr)
154+
end select
155+
123156
call KSPSetTolerances(ksp_saved, rtol, PETSC_DEFAULT_REAL, &
124157
PETSC_DEFAULT_REAL, maxit, ierr)
125158
call KSPSetNormType(ksp_saved, KSP_NORM_UNPRECONDITIONED, ierr)

0 commit comments

Comments
 (0)