module petsc_mod #include "petsc/finclude/petscsys.h" #include "petsc/finclude/petsc.h" implicit none private public :: init_petsc, stop_petsc public :: petsc_initialized public :: petsc_mpi_comm logical, save, protected :: petsc_initialized = .false. MPI_Comm, allocatable, save, protected :: petsc_mpi_comm contains subroutine init_petsc use petscsys, only: PETSC_COMM_SELF, PETSC_NULL_CHARACTER PetscErrorCode :: ierr if (petsc_initialized) error stop 'init_petsc: module already initialized' call PetscInitialize(PETSC_NULL_CHARACTER,ierr) if (ierr /= 0) error stop 'init_petsc: Cannot initialize PETSc' allocate(petsc_mpi_comm,source=PETSC_COMM_SELF) petsc_initialized = .true. end subroutine init_petsc subroutine stop_petsc PetscErrorCode :: ierr if (.not. petsc_initialized) error stop 'stop_petsc: uninitialized module' call PetscFinalize(ierr) if (ierr /= 0) error stop 'stop_petsc: Cannot finalize PETSc' petsc_initialized = .false. end subroutine stop_petsc end module petsc_mod