| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | ! | ||
| 2 | ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| 3 | ! SLEPc - Scalable Library for Eigenvalue Problem Computations | ||
| 4 | ! Copyright (c) 2002-, 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> ./test7f [-help] [-n <n>] [-verbose] [-inplace] | ||
| 11 | ! | ||
| 12 | ! Description: Simple example that tests the matrix square root. | ||
| 13 | ! | ||
| 14 | ! ---------------------------------------------------------------------- | ||
| 15 | ! | ||
| 16 | #include <slepc/finclude/slepcfn.h> | ||
| 17 | 32 | program test7f | |
| 18 | 24 | use slepcfn | |
| 19 | implicit none | ||
| 20 | |||
| 21 | ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| 22 | ! Declarations | ||
| 23 | ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| 24 | |||
| 25 | Mat :: A, S, R | ||
| 26 | FN :: fn | ||
| 27 | PetscInt :: n | ||
| 28 | PetscMPIInt :: rank | ||
| 29 | PetscErrorCode :: ierr | ||
| 30 | PetscBool :: flg, verbose, inplace | ||
| 31 | PetscReal :: re, im, nrm | ||
| 32 | PetscScalar :: tau, eta, alpha, x, y, yp | ||
| 33 | 20 | PetscScalar, pointer :: aa(:, :) | |
| 34 | |||
| 35 | ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| 36 | ! Beginning of program | ||
| 37 | ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| 38 | |||
| 39 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(SlepcInitialize(PETSC_NULL_CHARACTER, ierr)) |
| 40 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallMPIA(MPI_Comm_rank(PETSC_COMM_WORLD, rank, ierr)) |
| 41 | 24 | n = 10 | |
| 42 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(PetscOptionsGetInt(PETSC_NULL_OPTIONS, PETSC_NULL_CHARACTER, '-n', n, flg, ierr)) |
| 43 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(PetscOptionsHasName(PETSC_NULL_OPTIONS, PETSC_NULL_CHARACTER, '-verbose', verbose, ierr)) |
| 44 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(PetscOptionsHasName(PETSC_NULL_OPTIONS, PETSC_NULL_CHARACTER, '-inplace', inplace, ierr)) |
| 45 | |||
| 46 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
24 | if (rank == 0) then |
| 47 | 24 | write (*, '(/a,i3,a)') 'Matrix square root, n =', n, ' (Fortran)' | |
| 48 | end if | ||
| 49 | |||
| 50 | ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| 51 | ! Create FN object and matrix | ||
| 52 | ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| 53 | |||
| 54 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(FNCreate(PETSC_COMM_WORLD, fn, ierr)) |
| 55 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(FNSetType(fn, FNSQRT, ierr)) |
| 56 | 24 | tau = 0.15 | |
| 57 | 24 | eta = 1.0 | |
| 58 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(FNSetScale(fn, tau, eta, ierr)) |
| 59 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(FNSetFromOptions(fn, ierr)) |
| 60 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(FNGetScale(fn, tau, eta, ierr)) |
| 61 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(FNView(fn, PETSC_NULL_VIEWER, ierr)) |
| 62 | |||
| 63 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(MatCreateSeqDense(PETSC_COMM_SELF, n, n, PETSC_NULL_SCALAR_ARRAY, A, ierr)) |
| 64 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(PetscObjectSetName(A, 'A', ierr)) |
| 65 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(MatDenseGetArray(A, aa, ierr)) |
| 66 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | call FillUpMatrix(n, aa) |
| 67 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(MatDenseRestoreArray(A, aa, ierr)) |
| 68 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(MatSetOption(A, MAT_HERMITIAN, PETSC_TRUE, ierr)) |
| 69 | |||
| 70 | ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| 71 | ! Scalar evaluation | ||
| 72 | ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| 73 | |||
| 74 | 24 | x = 2.2 | |
| 75 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(FNEvaluateFunction(fn, x, y, ierr)) |
| 76 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(FNEvaluateDerivative(fn, x, yp, ierr)) |
| 77 | |||
| 78 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
24 | if (rank == 0) then |
| 79 | 24 | re = PetscRealPart(y) | |
| 80 | 24 | im = PetscImaginaryPart(y) | |
| 81 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
16 | if (abs(im) < 1.d-10) then |
| 82 | 24 | write (*, '(a3,f3.1,a,f8.5)') 'f(', PetscRealPart(x), ') = ', re | |
| 83 | else | ||
| 84 | ✗ | write (*, '(a3,f3.1,a,f10.5,sp,f9.5,a)') 'f(', PetscRealPart(x), ') = ', re, im, 'i' | |
| 85 | end if | ||
| 86 | 24 | re = PetscRealPart(yp) | |
| 87 | 16 | im = PetscImaginaryPart(yp) | |
| 88 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
16 | if (abs(im) < 1.d-10) then |
| 89 | 24 | write (*, '(a3,f3.1,a,f8.5)') 'f''(', PetscRealPart(x), ') = ', re | |
| 90 | else | ||
| 91 | ✗ | write (*, '(a3,f3.1,a,f8.5,sp,f8.5,a)') 'f''(', PetscRealPart(x), ') = ', re, im, 'i' | |
| 92 | end if | ||
| 93 | end if | ||
| 94 | |||
| 95 | ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| 96 | ! Compute matrix square root | ||
| 97 | ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| 98 | |||
| 99 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(MatCreateSeqDense(PETSC_COMM_SELF, n, n, PETSC_NULL_SCALAR_ARRAY, S, ierr)) |
| 100 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(PetscObjectSetName(S, 'S', ierr)) |
| 101 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | if (inplace) then |
| 102 | ✗ | PetscCallA(MatCopy(A, S, SAME_NONZERO_PATTERN, ierr)) | |
| 103 | ✗ | PetscCallA(MatSetOption(S, MAT_HERMITIAN, PETSC_TRUE, ierr)) | |
| 104 | ✗ | PetscCallA(FNEvaluateFunctionMat(fn, S, PETSC_NULL_MAT, ierr)) | |
| 105 | else | ||
| 106 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(FNEvaluateFunctionMat(fn, A, S, ierr)) |
| 107 | end if | ||
| 108 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | if (verbose) then |
| 109 | ✗ | if (rank == 0) write (*, *) 'Matrix A - - - - - - - -' | |
| 110 | ✗ | PetscCallA(MatView(A, PETSC_NULL_VIEWER, ierr)) | |
| 111 | ✗ | if (rank == 0) write (*, *) 'Computed sqrtm(A) - - - - - - - -' | |
| 112 | ✗ | PetscCallA(MatView(S, PETSC_NULL_VIEWER, ierr)) | |
| 113 | end if | ||
| 114 | |||
| 115 | ! *** check error ||S*S-A||_F | ||
| 116 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(MatMatMult(S, S, MAT_INITIAL_MATRIX, PETSC_DEFAULT_REAL, R, ierr)) |
| 117 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
24 | if (eta /= 1.0) then |
| 118 | 24 | alpha = 1.0/(eta*eta) | |
| 119 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(MatScale(R, alpha, ierr)) |
| 120 | end if | ||
| 121 | 24 | alpha = -tau | |
| 122 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(MatAXPY(R, alpha, A, SAME_NONZERO_PATTERN, ierr)) |
| 123 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(MatNorm(R, NORM_FROBENIUS, nrm, ierr)) |
| 124 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
24 | if (nrm < 100*PETSC_MACHINE_EPSILON) then |
| 125 | 24 | write (*, *) '||S*S-A||_F < 100*eps' | |
| 126 | else | ||
| 127 | ✗ | write (*, '(a,f8.5)') '||S*S-A||_F = ', nrm | |
| 128 | end if | ||
| 129 | |||
| 130 | ! *** Clean up | ||
| 131 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(MatDestroy(S, ierr)) |
| 132 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(MatDestroy(R, ierr)) |
| 133 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(MatDestroy(A, ierr)) |
| 134 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
24 | PetscCallA(FNDestroy(fn, ierr)) |
| 135 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
48 | PetscCallA(SlepcFinalize(ierr)) |
| 136 | |||
| 137 | contains | ||
| 138 | |||
| 139 | 24 | subroutine FillUpMatrix(n, X) | |
| 140 | PetscInt :: n, i, j | ||
| 141 | PetscScalar :: X(n, n) | ||
| 142 | |||
| 143 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
480 | do i = 1, n |
| 144 | 480 | X(i, i) = 2.5 | |
| 145 | end do | ||
| 146 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
72 | do j = 1, 2 |
| 147 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
912 | do i = 1, n - j |
| 148 | 840 | X(i, i + j) = 1.d0 | |
| 149 | 888 | X(i + j, i) = 1.d0 | |
| 150 | end do | ||
| 151 | end do | ||
| 152 | |||
| 153 | 24 | end | |
| 154 | |||
| 155 | end program test7f | ||
| 156 | |||
| 157 | !/*TEST | ||
| 158 | ! | ||
| 159 | ! test: | ||
| 160 | ! suffix: 1 | ||
| 161 | ! nsize: 1 | ||
| 162 | ! args: -fn_scale .13,2 -n 19 -fn_method {{0 1 2 3}shared output} | ||
| 163 | ! filter: grep -v "computing matrix functions" | ||
| 164 | ! output_file: output/test7f_1.out | ||
| 165 | ! | ||
| 166 | !TEST*/ | ||
| 167 |