diff --git a/src/Solution/NumericalSolution.f90 b/src/Solution/NumericalSolution.f90 index 66dfc6a28c1..f9fb303dbe7 100644 --- a/src/Solution/NumericalSolution.f90 +++ b/src/Solution/NumericalSolution.f90 @@ -1222,6 +1222,10 @@ subroutine sln_da(this) call this%cnvg_summary%destroy() deallocate (this%cnvg_summary) ! + ! -- linear solver + call this%linear_solver%destroy() + deallocate (this%linear_solver) + ! ! -- linear solver settings call this%linear_settings%destroy() deallocate (this%linear_settings) diff --git a/src/Solution/PETSc/PetscConvergence.F90 b/src/Solution/PETSc/PetscConvergence.F90 index ad04f2a3dc3..7c4dcd2cf81 100644 --- a/src/Solution/PETSc/PetscConvergence.F90 +++ b/src/Solution/PETSc/PetscConvergence.F90 @@ -18,6 +18,8 @@ module PetscConvergenceModule real(DP) :: dvclose integer(I4B) :: max_its type(ConvergenceSummaryType), pointer :: cnvg_summary => null() + contains + procedure :: destroy end type PetscContextType ! passing our context into PETSc requires an explicit interface @@ -73,6 +75,8 @@ subroutine petsc_check_convergence(ksp, n, rnorm, flag, context, ierr) summary => context%cnvg_summary + ! NB: KSPBuildResidual needs to have its vector destroyed + ! to avoid a memory leak, KSPBuildSolution doesn't... call KSPBuildSolution(ksp, PETSC_NULL_VEC, x, ierr) CHKERRQ(ierr) call KSPBuildResidual(ksp, PETSC_NULL_VEC, PETSC_NULL_VEC, res, ierr) @@ -88,6 +92,8 @@ subroutine petsc_check_convergence(ksp, n, rnorm, flag, context, ierr) CHKERRQ(ierr) call VecCopy(res, context%res_old, ierr) CHKERRQ(ierr) + call VecDestroy(res, ierr) + CHKERRQ(ierr) flag = KSP_CONVERGED_ITERATING end if return @@ -122,6 +128,9 @@ subroutine petsc_check_convergence(ksp, n, rnorm, flag, context, ierr) call VecCopy(res, context%res_old, ierr) CHKERRQ(ierr) + call VecDestroy(res, ierr) + CHKERRQ(ierr) + ! get dv and dr per local model call VecGetArrayF90(context%delta_x, local_dx, ierr) CHKERRQ(ierr) @@ -171,4 +180,20 @@ subroutine petsc_check_convergence(ksp, n, rnorm, flag, context, ierr) end subroutine petsc_check_convergence + subroutine destroy(this) + class(PetscContextType) :: this + ! local + integer(I4B) :: ierr + + call VecDestroy(this%x_old, ierr) + CHKERRQ(ierr) + call VecDestroy(this%res_old, ierr) + CHKERRQ(ierr) + call VecDestroy(this%delta_x, ierr) + CHKERRQ(ierr) + call VecDestroy(this%delta_res, ierr) + CHKERRQ(ierr) + + end subroutine destroy + end module PetscConvergenceModule diff --git a/src/Solution/PETSc/PetscSolver.F90 b/src/Solution/PETSc/PetscSolver.F90 index 6c981368b09..b72f53a54bd 100644 --- a/src/Solution/PETSc/PetscSolver.F90 +++ b/src/Solution/PETSc/PetscSolver.F90 @@ -419,10 +419,7 @@ subroutine petsc_destroy(this) deallocate (this%vec_residual) ! delete context - call VecDestroy(this%petsc_ctx%delta_x, ierr) - CHKERRQ(ierr) - call VecDestroy(this%petsc_ctx%x_old, ierr) - CHKERRQ(ierr) + call this%petsc_ctx%destroy() deallocate (this%petsc_ctx) call VecDestroy(this%pc_context%diag, ierr)