Actual source code: ex15f.F

slepc-3.11.2 2019-07-30
Report Typos and Errors
  1: !
  2: !  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3: !  SLEPc - Scalable Library for Eigenvalue Problem Computations
  4: !  Copyright (c) 2002-2019, Universitat Politecnica de Valencia, Spain
  5: !
  6: !  This file is part of SLEPc.
  7: !  SLEPc is distributed under a 2-clause BSD license (see LICENSE).
  8: !  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  9: !
 10: !  Program usage: mpiexec -n <np> ./ex15f [-help] [-n <n>] [-mu <mu>] [all SLEPc options]
 11: !
 12: !  Description: Singular value decomposition of the Lauchli matrix.
 13: !
 14: !  The command line options are:
 15: !    -n <n>, where <n> = matrix dimension.
 16: !    -mu <mu>, where <mu> = subdiagonal value.
 17: !
 18: ! ----------------------------------------------------------------------
 19: !
 20:       program main
 21: #include <slepc/finclude/slepcsvd.h>
 22:       use slepcsvd
 23:       implicit none

 25: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 26: !     Declarations
 27: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 28: !
 29: !  Variables:
 30: !     A     operator matrix
 31: !     svd   singular value solver context

 33:       Mat            A
 34:       SVD            svd
 35:       SVDType        tname
 36:       PetscReal      tol, error, sigma, mu
 37:       PetscInt       n, i, j, Istart, Iend
 38:       PetscInt       nsv, maxit, its, nconv
 39:       PetscMPIInt    rank
 40:       PetscErrorCode ierr
 41:       PetscBool      flg
 42:       PetscScalar    one, alpha

 44: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 45: !     Beginning of program
 46: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 48:       call SlepcInitialize(PETSC_NULL_CHARACTER,ierr)
 49:       call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
 50:       n = 100
 51:       call PetscOptionsGetInt(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,  &
 52:      &                        '-n',n,flg,ierr)
 53:       mu = PETSC_SQRT_MACHINE_EPSILON
 54:       call PetscOptionsGetReal(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER, &
 55:      &                        '-mu',mu,flg,ierr)

 57:       if (rank .eq. 0) then
 58:         write(*,100) n, mu
 59:       endif
 60:  100  format (/'Lauchli SVD, n =',I3,', mu=',E12.4,' (Fortran)')

 62: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 63: !     Build the Lauchli matrix
 64: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 66:       call MatCreate(PETSC_COMM_WORLD,A,ierr)
 67:       call MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n+1,n,ierr)
 68:       call MatSetFromOptions(A,ierr)
 69:       call MatSetUp(A,ierr)

 71:       call MatGetOwnershipRange(A,Istart,Iend,ierr)
 72:       one = 1.0
 73:       do i=Istart,Iend-1
 74:         if (i .eq. 0) then
 75:           do j=0,n-1
 76:             call MatSetValue(A,i,j,one,INSERT_VALUES,ierr)
 77:           end do
 78:         else
 79:           alpha = mu
 80:           call MatSetValue(A,i,i-1,alpha,INSERT_VALUES,ierr)
 81:         end if
 82:       enddo

 84:       call MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY,ierr)
 85:       call MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY,ierr)

 87: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 88: !     Create the singular value solver and display info
 89: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 91: !     ** Create singular value solver context
 92:       call SVDCreate(PETSC_COMM_WORLD,svd,ierr)

 94: !     ** Set operator
 95:       call SVDSetOperator(svd,A,ierr)

 97: !     ** Use thick-restart Lanczos as default solver
 98:       call SVDSetType(svd,SVDTRLANCZOS,ierr)

100: !     ** Set solver parameters at runtime
101:       call SVDSetFromOptions(svd,ierr)

103: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
104: !     Solve the singular value system
105: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

107:       call SVDSolve(svd,ierr)
108:       call SVDGetIterationNumber(svd,its,ierr)
109:       if (rank .eq. 0) then
110:         write(*,110) its
111:       endif
112:  110  format (/' Number of iterations of the method:',I4)

114: !     ** Optional: Get some information from the solver and display it
115:       call SVDGetType(svd,tname,ierr)
116:       if (rank .eq. 0) then
117:         write(*,120) tname
118:       endif
119:  120  format (' Solution method: ',A)
120:       call SVDGetDimensions(svd,nsv,PETSC_NULL_INTEGER,                 &
121:      &                      PETSC_NULL_INTEGER,ierr)
122:       if (rank .eq. 0) then
123:         write(*,130) nsv
124:       endif
125:  130  format (' Number of requested singular values:',I2)
126:       call SVDGetTolerances(svd,tol,maxit,ierr)
127:       if (rank .eq. 0) then
128:         write(*,140) tol, maxit
129:       endif
130:  140  format (' Stopping condition: tol=',1P,E11.4,', maxit=',I4)

132: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
133: !     Display solution and clean up
134: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

136: !     ** Get number of converged singular triplets
137:       call SVDGetConverged(svd,nconv,ierr)
138:       if (rank .eq. 0) then
139:         write(*,150) nconv
140:       endif
141:  150  format (' Number of converged approximate singular triplets:',I2/)

143: !     ** Display singular values and relative errors
144:       if (nconv.gt.0) then
145:         if (rank .eq. 0) then
146:           write(*,*) '       sigma          relative error'
147:           write(*,*) ' ----------------- ------------------'
148:         endif
149:         do i=0,nconv-1
150: !         ** Get i-th singular value
151:           call SVDGetSingularTriplet(svd,i,sigma,PETSC_NULL_VEC,        &
152:      &         PETSC_NULL_VEC,ierr)

154: !         ** Compute the relative error for each singular triplet
155:           call SVDComputeError(svd,i,SVD_ERROR_RELATIVE,error,ierr)
156:           if (rank .eq. 0) then
157:             write(*,160) sigma, error
158:           endif
159:  160      format (1P,'   ',E12.4,'       ',E12.4)

161:         enddo
162:         if (rank .eq. 0) then
163:           write(*,*)
164:         endif
165:       endif

167: !     ** Free work space
168:       call SVDDestroy(svd,ierr)
169:       call MatDestroy(A,ierr)

171:       call SlepcFinalize(ierr)
172:       end

174: !/*TEST
175: !
176: !   test:
177: !      suffix: 1
178: !      filter: sed -e "s/[0-9]\.[0-9]*E[+-]\([0-9]*\)/removed/g"
179: !
180: !TEST*/