Skip to content

Commit ac8835c

Browse files
committed
Working petsc, not fully reproducing old yet
1 parent 763ec50 commit ac8835c

3 files changed

Lines changed: 47 additions & 11 deletions

File tree

src/.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"python-envs.defaultEnvManager": "ms-python.python:conda",
3+
"python-envs.defaultPackageManager": "ms-python.python:conda",
4+
"python-envs.pythonProjects": []
5+
}

src/heatflow/mod_evolve.f90

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,30 @@ subroutine simulate(itime)
153153
err=E
154154

155155
! call bicgstab(acsr, ia, ja, S, itmax, Temp_p, x, iter)
156-
if (.not. allocated(ia32)) then
157-
allocate(ia32(size(ia)), ja32(size(ja)))
158-
ia32 = int(ia, kind=kind(ia32))
159-
ja32 = int(ja, kind=kind(ja32))
160-
end if
161-
NA32 = int(NA, kind=kind(NA32))
156+
157+
! Allocate and initialize x with a good initial guess
162158
allocate(x(NA))
159+
x = Temp_p + (Temp_p - Temp_pp)
160+
if (any(x - Temp_p .lt. TINY)) x = x + TINY ! avoid nan solver issue
161+
162+
! Convert to 32-bit integers for PETSc (only on first call)
163+
if (.not. allocated(ia32)) then
164+
allocate(ia32(size(ia)), ja32(size(ja)))
165+
ia32 = int(ia, kind=kind(ia32))
166+
ja32 = int(ja, kind=kind(ja32))
167+
end if
168+
NA32 = int(NA, kind=kind(NA32))
169+
163170
call solve_petsc_csr(NA32, ia32, ja32, acsr, S, x, tol, itmax)
164-
if (allocated(ia32)) deallocate(ia32, ja32)
171+
172+
! Note: Don't deallocate ia32, ja32 - keep them for next time step
165173

166174

167175
! CALL solve_pardiso(acsr, S, ia, ja, x)
168176
! CALL linbcg(S,x,itol=int(itol,I4B),tol=tol, itmax=int(itmax,I4B), iter=iter, &
169177
! err=E)
170178

171-
!
179+
!
172180
if (any(isnan(x(:)))) then
173181
write(0,*) "fatal error: NAN in x tempurature vector"
174182
write(0,*) 'time step ', itime, " T ", sum(Temp_p)/size(Temp_p), E ,iter

src/heatflow/mod_petsc_solver.f90

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ subroutine solve_petsc_csr(n, ia, ja, aval, b, x, rtol, maxit)
2929
Vec :: bb, xx
3030
KSP :: ksp
3131
PC :: pc
32-
integer :: ierr, i, row_nz, start_k
32+
integer :: ierr, i, row_nz, start_k, its
3333
integer, allocatable :: cols0(:), idx(:)
3434
real(8), allocatable :: vals(:)
3535
real(8), pointer :: xptr(:)
36+
KSPConvergedReason :: reason
37+
real(8) :: rnorm
3638

3739
if (size(ia) /= n+1) stop 'solve_petsc_csr: ia size mismatch'
3840
if (size(b) /= n .or. size(x) /= n) stop 'solve_petsc_csr: vector size mismatch'
@@ -71,12 +73,33 @@ subroutine solve_petsc_csr(n, ia, ja, aval, b, x, rtol, maxit)
7173
call KSPCreate(PETSC_COMM_SELF, ksp, ierr)
7274
call KSPSetOperators(ksp, A, A, ierr) ! 3-arg form (reuse automatically)
7375
call KSPGetPC(ksp, pc, ierr)
74-
call PCSetType(pc, PCILU, ierr) ! Override at runtime with -pc_type
75-
call KSPSetType(ksp, KSPCG, ierr) ! Use -ksp_type bcgs if not SPD
76+
call PCSetType(pc, PCJACOBI, ierr) ! Use Jacobi (diagonal) preconditioner to match linbcg
77+
call KSPSetType(ksp, KSPBCGS, ierr) ! Use BCGS to match linbcg behavior
78+
79+
! Set convergence tolerances
80+
! rtol = relative tolerance, atol = absolute tolerance (use default), dtol = divergence tolerance, maxits = max iterations
7681
call KSPSetTolerances(ksp, rtol, PETSC_DEFAULT_REAL, PETSC_DEFAULT_REAL, maxit, ierr)
82+
83+
! Use unpreconditioned norm (matching linbcg with itol=1)
84+
call KSPSetNormType(ksp, KSP_NORM_UNPRECONDITIONED, ierr)
85+
86+
! Allow command line override of solver options
7787
call KSPSetFromOptions(ksp, ierr)
7888

7989
call KSPSolve(ksp, bb, xx, ierr)
90+
91+
! Check convergence
92+
call KSPGetConvergedReason(ksp, reason, ierr)
93+
call KSPGetIterationNumber(ksp, its, ierr)
94+
call KSPGetResidualNorm(ksp, rnorm, ierr)
95+
96+
if (reason < 0) then
97+
write(0,*) "WARNING: PETSc solver diverged or failed!"
98+
write(0,*) " Reason code:", reason
99+
write(0,*) " Iterations:", its
100+
write(0,*) " Residual norm:", rnorm
101+
! Don't stop - let the main code detect NaNs if needed
102+
end if
80103

81104
! Extract solution
82105
call VecGetArrayF90(xx, xptr, ierr)

0 commit comments

Comments
 (0)