From 7a5d73a1be46999b0ee21dcdec92cffcd7d8b316 Mon Sep 17 00:00:00 2001 From: Jacob Williams Date: Thu, 23 Jan 2020 17:01:36 -0600 Subject: [PATCH 1/2] Added way for user to specify the sparsity partition directly. Fixes #6 Also added way to get an already computed partition. --- src/numerical_differentiation_module.f90 | 38 +++++++++++++++++++----- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/src/numerical_differentiation_module.f90 b/src/numerical_differentiation_module.f90 index 19ec0e7..4b7be18 100644 --- a/src/numerical_differentiation_module.f90 +++ b/src/numerical_differentiation_module.f90 @@ -1629,7 +1629,7 @@ end subroutine compute_indices !@note If specifying the linear pattern, all three optional arguments ! must be present. - subroutine set_sparsity_pattern(me,irow,icol,linear_irow,linear_icol,linear_vals) + subroutine set_sparsity_pattern(me,irow,icol,linear_irow,linear_icol,linear_vals,maxgrp,ngrp) implicit none @@ -1639,6 +1639,10 @@ subroutine set_sparsity_pattern(me,irow,icol,linear_irow,linear_icol,linear_vals integer,dimension(:),intent(in),optional :: linear_irow !! linear sparsity pattern nonzero elements row indices integer,dimension(:),intent(in),optional :: linear_icol !! linear sparsity pattern nonzero elements column indices real(wp),dimension(:),intent(in),optional :: linear_vals !! linear sparsity values (constant elements of the Jacobian) + integer,intent(in),optional :: maxgrp !! DSM sparsity partition + !! [only used if `me%partition_sparsity_pattern=True`] + integer,dimension(me%n),intent(in),optional :: ngrp !! DSM sparsity partition + !! [only used if `me%partition_sparsity_pattern=True`] integer :: info !! status output form [[dsm]] @@ -1659,11 +1663,23 @@ subroutine set_sparsity_pattern(me,irow,icol,linear_irow,linear_icol,linear_vals call me%sparsity%compute_indices() if (me%partition_sparsity_pattern) then - call me%sparsity%dsm_wrapper(me%n,me%m,info) - if (info/=1) then - call me%raise_exception(16,'set_sparsity_pattern',& - 'error partitioning sparsity pattern.') - return + if (present(maxgrp) .and. present(ngrp)) then + ! use the user-input partition: + if (maxgrp>0 .and. all(ngrp>=1 .and. ngrp<=maxgrp)) then + me%sparsity%maxgrp = maxgrp + me%sparsity%ngrp = ngrp + else + call me%raise_exception(28,'set_sparsity_pattern',& + 'invalid sparsity partition inputs.') + return + end if + else + call me%sparsity%dsm_wrapper(me%n,me%m,info) + if (info/=1) then + call me%raise_exception(16,'set_sparsity_pattern',& + 'error partitioning sparsity pattern.') + return + end if end if end if @@ -2133,7 +2149,9 @@ end subroutine compute_sparsity_pattern ! Returns the sparsity pattern from the class. ! If it hasn't been computed, the output arrays will not be allocated. - subroutine get_sparsity_pattern(me,irow,icol,linear_irow,linear_icol,linear_vals) + subroutine get_sparsity_pattern(me,irow,icol,& + linear_irow,linear_icol,linear_vals,& + maxgrp,ngrp) implicit none @@ -2146,6 +2164,8 @@ subroutine get_sparsity_pattern(me,irow,icol,linear_irow,linear_icol,linear_vals !! elements column indices real(wp),dimension(:),allocatable,intent(out),optional :: linear_vals !! linear sparsity values (constant !! elements of the Jacobian) + integer,intent(out),optional :: maxgrp !! DSM sparsity partition + integer,dimension(:),allocatable,intent(out),optional :: ngrp !! DSM sparsity partition if (me%exception_raised) return ! check for exceptions @@ -2165,6 +2185,10 @@ subroutine get_sparsity_pattern(me,irow,icol,linear_irow,linear_icol,linear_vals if (allocated(me%sparsity%linear_vals)) linear_vals = me%sparsity%linear_vals end if + ! optional DSM partition: + if (present(ngrp) .and. allocated(me%sparsity%ngrp)) ngrp = me%sparsity%ngrp + if (present(maxgrp)) maxgrp = me%sparsity%maxgrp + end subroutine get_sparsity_pattern !******************************************************************************* From 840fe3564632167d07efda18aafcfd63129d8111 Mon Sep 17 00:00:00 2001 From: Jacob Williams Date: Sat, 22 May 2021 22:13:58 -0500 Subject: [PATCH 2/2] some mods --- src/numerical_differentiation_module.f90 | 30 +++++++++++++----------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/numerical_differentiation_module.f90 b/src/numerical_differentiation_module.f90 index 4b7be18..c5fa4d6 100644 --- a/src/numerical_differentiation_module.f90 +++ b/src/numerical_differentiation_module.f90 @@ -1505,15 +1505,17 @@ subroutine dsm_wrapper(me,n,m,info) integer,intent(out) :: info !! status output from [[dsm]] integer :: mingrp !! for call to [[dsm]] - integer,dimension(m+1) :: ipntr !! for call to [[dsm]] - integer,dimension(n+1) :: jpntr !! for call to [[dsm]] - integer,dimension(:),allocatable :: irow !! for call to [[dsm]] - !! (temp copy since [[dsm]] - !! will modify it) - integer,dimension(:),allocatable :: icol !! for call to [[dsm]] - !! (temp copy since [[dsm]] - !! will modify it) - + integer,dimension(:),allocatable :: ipntr !! for call to [[dsm]] + integer,dimension(:),allocatable :: jpntr !! for call to [[dsm]] + integer,dimension(:),allocatable :: irow !! for call to [[dsm]] + !! (temp copy since [[dsm]] + !! will modify it) + integer,dimension(:),allocatable :: icol !! for call to [[dsm]] + !! (temp copy since [[dsm]] + !! will modify it) + + allocate(ipntr(m+1)) + allocate(jpntr(n+1)) allocate(me%ngrp(n)) irow = me%irow icol = me%icol @@ -1639,10 +1641,10 @@ subroutine set_sparsity_pattern(me,irow,icol,linear_irow,linear_icol,linear_vals integer,dimension(:),intent(in),optional :: linear_irow !! linear sparsity pattern nonzero elements row indices integer,dimension(:),intent(in),optional :: linear_icol !! linear sparsity pattern nonzero elements column indices real(wp),dimension(:),intent(in),optional :: linear_vals !! linear sparsity values (constant elements of the Jacobian) - integer,intent(in),optional :: maxgrp !! DSM sparsity partition - !! [only used if `me%partition_sparsity_pattern=True`] - integer,dimension(me%n),intent(in),optional :: ngrp !! DSM sparsity partition - !! [only used if `me%partition_sparsity_pattern=True`] + integer,intent(in),optional :: maxgrp !! DSM sparsity partition + !! [only used if `me%partition_sparsity_pattern=True`] + integer,dimension(:),intent(in),optional :: ngrp !! DSM sparsity partition (size `n`) + !! [only used if `me%partition_sparsity_pattern=True`] integer :: info !! status output form [[dsm]] @@ -1665,7 +1667,7 @@ subroutine set_sparsity_pattern(me,irow,icol,linear_irow,linear_icol,linear_vals if (me%partition_sparsity_pattern) then if (present(maxgrp) .and. present(ngrp)) then ! use the user-input partition: - if (maxgrp>0 .and. all(ngrp>=1 .and. ngrp<=maxgrp)) then + if (maxgrp>0 .and. all(ngrp>=1 .and. ngrp<=maxgrp) .and. size(ngrp)==me%n) then me%sparsity%maxgrp = maxgrp me%sparsity%ngrp = ngrp else