1 module petscmatrixmodule
 
    2 #include <petsc/finclude/petscksp.h> 
   20     integer(I4B) :: nnz_local
 
   21     integer(I4B) :: offset
 
   22     integer(I4B), 
dimension(:), 
pointer, 
contiguous, 
private :: ia_local
 
   23     integer(I4B), 
dimension(:), 
pointer, 
contiguous, 
private :: ja_local
 
   24     real(DP), 
dimension(:), 
pointer, 
contiguous, 
private :: amat_local
 
   25     integer(I4B), 
dimension(:), 
pointer, 
contiguous :: ia_petsc
 
   26     integer(I4B), 
dimension(:), 
pointer, 
contiguous :: ja_petsc
 
   27     real(DP), 
dimension(:), 
pointer, 
contiguous :: amat_petsc
 
   28     logical(LGP) :: is_parallel
 
   31     procedure :: init => pm_init
 
   32     procedure :: destroy => pm_destroy
 
   33     procedure :: create_vec_mm => pm_create_vec_mm
 
   34     procedure :: create_vec => pm_create_vec
 
   35     procedure :: get_value_pos => pm_get_value_pos
 
   36     procedure :: get_diag_value => pm_get_diag_value
 
   37     procedure :: set_diag_value => pm_set_diag_value
 
   38     procedure :: set_value_pos => pm_set_value_pos
 
   39     procedure :: add_value_pos => pm_add_value_pos
 
   40     procedure :: add_diag_value => pm_add_diag_value
 
   41     procedure :: zero_entries => pm_zero_entries
 
   42     procedure :: zero_row_offdiag => pm_zero_row_offdiag
 
   43     procedure :: get_first_col_pos => pm_get_first_col_pos
 
   44     procedure :: get_last_col_pos => pm_get_last_col_pos
 
   45     procedure :: get_column => pm_get_column
 
   46     procedure :: get_position => pm_get_position
 
   47     procedure :: get_position_diag => pm_get_position_diag
 
   48     procedure :: get_aij => pm_get_aij
 
   49     procedure :: get_row_offset => pm_get_row_offset
 
   51     procedure :: multiply => pm_multiply
 
   54     procedure :: update => pm_update
 
   55     procedure :: get_aij_local => pm_get_aij_local
 
   58     procedure, 
private :: pm_get_position
 
   60   end type petscmatrixtype
 
   64   subroutine pm_init(this, sparse, mem_path)
 
   66     class(PetscMatrixType) :: this
 
   67     type(sparsematrix) :: sparse
 
   68     character(len=*) :: mem_path
 
   70     petscerrorcode :: ierr
 
   71     integer(I4B) :: i, ierror
 
   73     integer(I4B), 
dimension(:), 
pointer :: ia_tmp, ja_tmp
 
   74     integer(I4B) :: nrows_local
 
   77     this%memory_path = mem_path
 
   78     this%nrow = sparse%nrow
 
   79     this%ncol = sparse%ncol
 
   81     this%nnz_local = sparse%nnz - sparse%nnz_od
 
   82     this%offset = sparse%offset
 
   85     call mem_allocate(this%ia_local, this%nrow + 1, 
'IA_LOCAL', this%memory_path)
 
   86     call mem_allocate(this%ja_local, this%nnz_local, 
'JA_LOCAL', this%memory_path)
 
   87     call mem_allocate(this%amat_local, this%nnz_local, 
'AMAT_LOCAL', &
 
   89     call mem_allocate(this%ia_petsc, this%nrow + 1, 
'IA_PETSC', this%memory_path)
 
   90     call mem_allocate(this%ja_petsc, this%nnz, 
'JA_PETSC', this%memory_path)
 
   91     call mem_allocate(this%amat_petsc, this%nnz, 
'AMAT_PETSC', this%memory_path)
 
   93     call sparse%sort(with_csr=.true.) 
 
   94     call sparse%filliaja(this%ia_petsc, this%ja_petsc, ierror)
 
   97     do i = 1, this%nrow + 1
 
   98       this%ia_petsc(i) = this%ia_petsc(i) - 1
 
  101       this%ja_petsc(i) = this%ja_petsc(i) - 1
 
  102       this%amat_petsc(i) = 
dzero 
  107       this%is_parallel = .true.
 
  108       call matcreatempiaijwitharrays(petsc_comm_world, &
 
  109                                      sparse%nrow, sparse%nrow, &
 
  110                                      sparse%ncol, sparse%ncol, &
 
  111                                      this%ia_petsc, this%ja_petsc, &
 
  112                                      this%amat_petsc, this%mat, &
 
  115       this%is_parallel = .false.
 
  116       call matcreateseqaijwitharrays(petsc_comm_world, &
 
  117                                      sparse%nrow, sparse%ncol, &
 
  118                                      this%ia_petsc, this%ja_petsc, &
 
  119                                      this%amat_petsc, this%mat, &
 
  126     call matgetdiagonalblock(this%mat, local_mat, ierr)
 
  129     call matgetrowijf90(local_mat, 1, petsc_false, petsc_false, &
 
  130                         nrows_local, ia_tmp, ja_tmp, &
 
  134     do i = 1, 
size(ia_tmp)
 
  135       this%ia_local(i) = ia_tmp(i)
 
  137     do i = 1, 
size(ja_tmp) - 1
 
  138       this%ja_local(i) = ja_tmp(i)
 
  141     call matrestorerowijf90(local_mat, 1, petsc_false, petsc_false, &
 
  142                             nrows_local, ia_tmp, ja_tmp, done, ierr)
 
  145   end subroutine pm_init
 
  149   subroutine pm_update(this)
 
  150     class(PetscMatrixType) :: this
 
  152     petscerrorcode :: ierr
 
  154     if (this%is_parallel) 
then 
  155       call matupdatempiaijwitharrays(this%mat, this%nrow, this%nrow, &
 
  156                                      this%ncol, this%ncol, &
 
  157                                      this%ia_petsc, this%ja_petsc, &
 
  158                                      this%amat_petsc, ierr)
 
  162   end subroutine pm_update
 
  164   subroutine pm_destroy(this)
 
  165     class(PetscMatrixType) :: this
 
  167     petscerrorcode :: ierr
 
  169     call matdestroy(this%mat, ierr)
 
  179   end subroutine pm_destroy
 
  181   function pm_create_vec_mm(this, n, name, mem_path) 
result(vec)
 
  182     class(PetscMatrixType) :: this
 
  184     character(len=*) :: name
 
  185     character(len=*) :: mem_path
 
  186     class(VectorBaseType), 
pointer :: vec
 
  188     class(PetscVectorType), 
pointer :: petsc_vec
 
  191     call petsc_vec%create_mm(n, name, mem_path)
 
  194   end function pm_create_vec_mm
 
  196   function pm_create_vec(this, n) 
result(vec)
 
  197     class(PetscMatrixType) :: this
 
  199     class(VectorBaseType), 
pointer :: vec
 
  201     class(PetscVectorType), 
pointer :: petsc_vec
 
  204     call petsc_vec%create(n)
 
  207   end function pm_create_vec
 
  209   function pm_get_value_pos(this, ipos) 
result(value)
 
  210     class(PetscMatrixType) :: this
 
  214     value = this%amat_petsc(ipos)
 
  216   end function pm_get_value_pos
 
  218   function pm_get_diag_value(this, irow) 
result(diag_value)
 
  219     class(PetscMatrixType) :: this
 
  221     real(DP) :: diag_value
 
  223     integer(I4B) :: idiag
 
  225     idiag = this%get_position_diag(irow)
 
  226     diag_value = this%amat_petsc(idiag)
 
  228   end function pm_get_diag_value
 
  230   subroutine pm_set_diag_value(this, irow, diag_value)
 
  231     class(PetscMatrixType) :: this
 
  233     real(DP) :: diag_value
 
  235     integer(I4B) :: idiag
 
  237     idiag = this%get_position_diag(irow)
 
  238     this%amat_petsc(idiag) = diag_value
 
  240   end subroutine pm_set_diag_value
 
  242   subroutine pm_set_value_pos(this, ipos, value)
 
  243     class(PetscMatrixType) :: this
 
  247     this%amat_petsc(ipos) = 
value 
  249   end subroutine pm_set_value_pos
 
  251   subroutine pm_add_value_pos(this, ipos, value)
 
  252     class(PetscMatrixType) :: this
 
  256     this%amat_petsc(ipos) = this%amat_petsc(ipos) + 
value 
  258   end subroutine pm_add_value_pos
 
  260   subroutine pm_add_diag_value(this, irow, value)
 
  261     class(PetscMatrixType) :: this
 
  265     integer(I4B) :: idiag
 
  267     idiag = this%get_position_diag(irow)
 
  268     this%amat_petsc(idiag) = this%amat_petsc(idiag) + 
value 
  270   end subroutine pm_add_diag_value
 
  272   function pm_get_first_col_pos(this, irow) 
result(first_col_pos)
 
  273     class(PetscMatrixType) :: this
 
  275     integer(I4B) :: first_col_pos
 
  277     integer(I4B) :: irow_local
 
  280     irow_local = irow - this%offset
 
  283     first_col_pos = this%ia_petsc(irow_local) + 1
 
  285   end function pm_get_first_col_pos
 
  287   function pm_get_last_col_pos(this, irow) 
result(last_col_pos)
 
  288     class(PetscMatrixType) :: this
 
  290     integer(I4B) :: last_col_pos
 
  292     integer(I4B) :: irow_local
 
  295     irow_local = irow - this%offset
 
  298     last_col_pos = this%ia_petsc(irow_local + 1)
 
  300   end function pm_get_last_col_pos
 
  302   function pm_get_column(this, ipos) 
result(icol)
 
  303     class(PetscMatrixType) :: this
 
  308     icol = this%ja_petsc(ipos) + 1
 
  310   end function pm_get_column
 
  316   function pm_get_position(this, irow, icol) 
result(ipos)
 
  317     class(PetscMatrixType) :: this
 
  322     integer(I4B) :: ipos_f
 
  323     integer(I4B) :: irow_local
 
  328     irow_local = irow - this%offset
 
  331     do ipos_f = this%ia_petsc(irow_local) + 1, this%ia_petsc(irow_local + 1)
 
  332       if (this%ja_petsc(ipos_f) + 1 == icol) 
then 
  338   end function pm_get_position
 
  340   function pm_get_position_diag(this, irow) 
result(ipos_diag)
 
  341     class(PetscMatrixType) :: this
 
  343     integer(I4B) :: ipos_diag
 
  345     ipos_diag = this%pm_get_position(irow, irow)
 
  347   end function pm_get_position_diag
 
  351   subroutine pm_zero_entries(this)
 
  352     class(PetscMatrixType) :: this
 
  357       this%amat_petsc(i) = 
dzero 
  360   end subroutine pm_zero_entries
 
  364   subroutine pm_zero_row_offdiag(this, irow)
 
  365     class(PetscMatrixType) :: this
 
  368     integer(I4B) :: ipos, idiag
 
  370     idiag = this%get_position_diag(irow)
 
  371     do ipos = this%get_first_col_pos(irow), this%get_last_col_pos(irow)
 
  372       if (ipos == idiag) cycle
 
  373       this%amat_petsc(ipos) = 
dzero 
  376   end subroutine pm_zero_row_offdiag
 
  378   subroutine pm_get_aij(this, ia, ja, amat)
 
  380     class(PetscMatrixType) :: this
 
  381     integer(I4B), 
dimension(:), 
pointer, 
contiguous :: ia
 
  382     integer(I4B), 
dimension(:), 
pointer, 
contiguous :: ja
 
  383     real(DP), 
dimension(:), 
pointer, 
contiguous :: amat
 
  385     call ustop(
"pm_get_aij not implemented")
 
  387   end subroutine pm_get_aij
 
  391   subroutine pm_get_aij_local(this, ia, ja, amat)
 
  392     class(PetscMatrixType) :: this
 
  393     integer(I4B), 
dimension(:), 
pointer, 
contiguous :: ia
 
  394     integer(I4B), 
dimension(:), 
pointer, 
contiguous :: ja
 
  395     real(DP), 
dimension(:), 
pointer, 
contiguous :: amat
 
  397     petscerrorcode :: ierr
 
  398     integer(I4B) :: irow, icol, ipos
 
  399     integer(I4B), 
dimension(1) :: irow_idxs, icol_idxs
 
  400     real(DP), 
dimension(1) :: values
 
  403     call matgetdiagonalblock(this%mat, local_mat, ierr)
 
  406     do irow = 1, this%nrow
 
  407       do ipos = this%ia_local(irow), this%ia_local(irow + 1) - 1
 
  408         icol = this%ja_local(ipos)
 
  409         irow_idxs(1) = irow - 1
 
  410         icol_idxs(1) = icol - 1
 
  411         call matgetvalues(local_mat, 1, irow_idxs, 1, icol_idxs, values, ierr)
 
  413         this%amat_local(ipos) = values(1)
 
  419     amat => this%amat_local
 
  421   end subroutine pm_get_aij_local
 
  423   function pm_get_row_offset(this) 
result(offset)
 
  424     class(PetscMatrixType) :: this
 
  425     integer(I4B) :: offset
 
  429   end function pm_get_row_offset
 
  433   subroutine pm_multiply(this, vec_x, vec_y)
 
  434     class(PetscMatrixType) :: this
 
  435     class(VectorBaseType), 
pointer :: vec_x
 
  436     class(VectorBaseType), 
pointer :: vec_y
 
  438     petscerrorcode :: ierr
 
  439     class(PetscVectorType), 
pointer :: x, y
 
  455     call matmult(this%mat, x%vec_impl, y%vec_impl, ierr)
 
  458   end subroutine pm_multiply
 
  460 end module petscmatrixmodule
 
This module contains simulation constants.
real(dp), parameter dzero
real constant zero
This module defines variable data types.
This module contains simulation methods.
subroutine, public ustop(stopmess, ioutlocal)
Stop the simulation.
This module contains simulation variables.
character(len=linelength) simulation_mode