Skip to content

Commit 73103d2

Browse files
Added support for custom stopping criteria for the PETSc SNES object
1 parent b8e9a0f commit 73103d2

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/PETSC.jl

+29
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,32 @@ const SNESASPIN = "aspin"
685685
const SNESCOMPOSITE = "composite"
686686
const SNESPATCH = "patch"
687687

688+
"""
689+
Julia alias to `SNESConvergedReason` C enum.
690+
691+
See [PETSc manual](https://petsc.org/release/manualpages/SNES/SNESConvergedReason/).
692+
"""
693+
@enum SNESConvergedReason begin
694+
SNES_CONVERGED_FNORM_ABS = 2 # ||F|| < atol
695+
SNES_CONVERGED_FNORM_RELATIVE = 3 # ||F|| < rtol*||F_initial||
696+
SNES_CONVERGED_SNORM_RELATIVE = 4 # Newton computed step size small; || delta x || < stol || x ||
697+
SNES_CONVERGED_ITS = 5 # maximum iterations reached
698+
SNES_BREAKOUT_INNER_ITER = 6 # Flag to break out of inner loop after checking custom convergence.
699+
# it is used in multi-phase flow when state changes diverged
700+
SNES_DIVERGED_FUNCTION_DOMAIN = -1 # the new x location passed the function is not in the domain of F
701+
SNES_DIVERGED_FUNCTION_COUNT = -2
702+
SNES_DIVERGED_LINEAR_SOLVE = -3 # the linear solve failed
703+
SNES_DIVERGED_FNORM_NAN = -4
704+
SNES_DIVERGED_MAX_IT = -5
705+
SNES_DIVERGED_LINE_SEARCH = -6 # the line search failed
706+
SNES_DIVERGED_INNER = -7 # inner solve failed
707+
SNES_DIVERGED_LOCAL_MIN = -8 # || J^T b || is small, implies converged to local minimum of F()
708+
SNES_DIVERGED_DTOL = -9 # || F || > divtol*||F_initial||
709+
SNES_DIVERGED_JACOBIAN_DOMAIN = -10 # Jacobian calculation does not make sense
710+
SNES_DIVERGED_TR_DELTA = -11
711+
712+
SNES_CONVERGED_ITERATING = 0
713+
end
688714

689715
@wrapper(:SNESCreate,PetscErrorCode,(MPI.Comm,Ptr{SNES}),(comm,snes),"https://petsc.org/release/docs/manualpages/SNES/SNESCreate.html")
690716
@wrapper(:SNESSetFunction,PetscErrorCode,(SNES,Vec,Ptr{Cvoid},Ptr{Cvoid}),(snes,vec,fptr,ctx),"https://petsc.org/release/docs/manualpages/SNES/SNESSetFunction.html")
@@ -700,6 +726,9 @@ const SNESPATCH = "patch"
700726
@wrapper(:SNESSetCountersReset,PetscErrorCode,(SNES,PetscBool),(snes,reset),"https://petsc.org/release/docs/manualpages/SNES/SNESSetCountersReset.html")
701727
@wrapper(:SNESGetNumberFunctionEvals,PetscErrorCode,(SNES,Ptr{PetscInt}),(snes,nfuncs),"https://petsc.org/release/docs/manualpages/SNES/SNESGetNumberFunctionEvals.html")
702728
@wrapper(:SNESGetLinearSolveFailures,PetscErrorCode,(SNES,Ptr{PetscInt}),(snes,nfails),"https://petsc.org/release/docs/manualpages/SNES/SNESGetLinearSolveFailures.html")
729+
@wrapper(:SNESSetConvergenceTest,PetscErrorCode,(SNES,Ptr{Cvoid},Ptr{Cvoid},Ptr{Cvoid}),(snes,convtest,cctx,destroy),"https://petsc.org/release/manualpages/SNES/SNESSetConvergenceTest/")
730+
@wrapper(:SNESConvergedDefault,PetscErrorCode,(SNES,PetscInt,PetscReal,PetscReal,PetscReal,Ptr{SNESConvergedReason},Ptr{Cvoid}),(snes,it,xnorm,gnorm,f,reason,user),"https://petsc.org/release/manualpages/SNES/SNESConvergedDefault/")
731+
703732

704733
# Garbage collection of PETSc objects
705734
@wrapper(:PetscObjectRegisterDestroy,PetscErrorCode,(Ptr{Cvoid},),(obj,),"https://petsc.org/release/docs/manualpages/Sys/PetscObjectRegisterDestroy.html")

test/PLaplacianTests.jl

+16
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,27 @@ using Test
88
using SparseMatricesCSR
99

1010

11+
function snes_convergence_test(snes::SNES,
12+
it::PetscInt,
13+
xnorm::PetscReal,
14+
gnorm::PetscReal,
15+
f::PetscReal,
16+
reason::Ptr{PETSC.SNESConvergedReason},
17+
user::Ptr{Cvoid})::PetscInt
18+
PETSC.SNESConvergedDefault(snes, it, xnorm, gnorm, f, reason, user)
19+
end
20+
1121
function mysnessetup(snes)
1222
ksp = Ref{GridapPETSc.PETSC.KSP}()
1323
pc = Ref{GridapPETSc.PETSC.PC}()
1424
mumpsmat = Ref{GridapPETSc.PETSC.Mat}()
25+
26+
fconvptr = @cfunction($snes_convergence_test,
27+
PetscInt,
28+
(SNES, PetscInt, PetscReal, PetscReal, PetscReal, Ptr{PETSC.SNESConvergedReason}, Ptr{Cvoid}))
29+
1530
@check_error_code GridapPETSc.PETSC.SNESSetFromOptions(snes[])
31+
@check_error_code GridapPETSc.PETSC.SNESSetConvergenceTest(snes[],fconvptr,C_NULL,C_NULL)
1632
@check_error_code GridapPETSc.PETSC.SNESGetKSP(snes[],ksp)
1733
#@check_error_code GridapPETSc.PETSC.KSPView(ksp[],C_NULL)
1834
@check_error_code GridapPETSc.PETSC.KSPSetType(ksp[],GridapPETSc.PETSC.KSPPREONLY)

0 commit comments

Comments
 (0)