Date: 2023-01-26T12:17:57+00:00

Actual source code: test3.c
slepc-3.18.2 2023-01-26
Report Typos and Errors

      1: /*
      2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
      4:    Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain

      6:    This file is part of SLEPc.
      7:    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
      8:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      9: */

     11: static char help[] = "Test matrix exponential.\n\n";

     13: #include <slepcfn.h>

     15: /*
     16:    Compute matrix exponential B = expm(A)
     17:  */
     18: PetscErrorCode TestMatExp(FN fn,Mat A,PetscViewer viewer,PetscBool verbose,PetscBool inplace,PetscBool checkerror)
     19: {
     20:   PetscScalar    tau,eta;
     21:   PetscBool      set,flg;
     22:   PetscInt       n;
     23:   Mat            F,R,Finv,Acopy;
     24:   Vec            v,f0;
     25:   FN             finv;
     26:   PetscReal      nrm,nrmf;

     29:   MatGetSize(A,&n,NULL);
     30:   MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&F);
     31:   PetscObjectSetName((PetscObject)F,"F");
     32:   /* compute matrix exponential */
     33:   if (inplace) {
     34:     MatCopy(A,F,SAME_NONZERO_PATTERN);
     35:     MatIsHermitianKnown(A,&set,&flg);
     36:     if (set && flg) MatSetOption(F,MAT_HERMITIAN,PETSC_TRUE);
     37:     FNEvaluateFunctionMat(fn,F,NULL);
     38:   } else {
     39:     MatDuplicate(A,MAT_COPY_VALUES,&Acopy);
     40:     FNEvaluateFunctionMat(fn,A,F);
     41:     /* check that A has not been modified */
     42:     MatAXPY(Acopy,-1.0,A,SAME_NONZERO_PATTERN);
     43:     MatNorm(Acopy,NORM_1,&nrm);
     44:     if (nrm>100*PETSC_MACHINE_EPSILON) PetscPrintf(PETSC_COMM_WORLD,"Warning: the input matrix has changed by %g\n",(double)nrm);
     45:     MatDestroy(&Acopy);
     46:   }
     47:   if (verbose) {
     48:     PetscPrintf(PETSC_COMM_WORLD,"Matrix A - - - - - - - -\n");
     49:     MatView(A,viewer);
     50:     PetscPrintf(PETSC_COMM_WORLD,"Computed expm(A) - - - - - - -\n");
     51:     MatView(F,viewer);
     52:   }
     53:   /* print matrix norm for checking */
     54:   MatNorm(F,NORM_1,&nrmf);
     55:   PetscPrintf(PETSC_COMM_WORLD,"The 1-norm of f(A) is %g\n",(double)nrmf);
     56:   if (checkerror) {
     57:     MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&Finv);
     58:     PetscObjectSetName((PetscObject)Finv,"Finv");
     59:     FNGetScale(fn,&tau,&eta);
     60:     /* compute inverse exp(-tau*A)/eta */
     61:     FNCreate(PETSC_COMM_WORLD,&finv);
     62:     FNSetType(finv,FNEXP);
     63:     FNSetFromOptions(finv);
     64:     FNSetScale(finv,-tau,1.0/eta);
     65:     if (inplace) {
     66:       MatCopy(A,Finv,SAME_NONZERO_PATTERN);
     67:       MatIsHermitianKnown(A,&set,&flg);
     68:       if (set && flg) MatSetOption(Finv,MAT_HERMITIAN,PETSC_TRUE);
     69:       FNEvaluateFunctionMat(finv,Finv,NULL);
     70:     } else FNEvaluateFunctionMat(finv,A,Finv);
     71:     FNDestroy(&finv);
     72:     /* check error ||F*Finv-I||_F */
     73:     MatMatMult(F,Finv,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&R);
     74:     MatShift(R,-1.0);
     75:     MatNorm(R,NORM_FROBENIUS,&nrm);
     76:     if (nrm<100*PETSC_MACHINE_EPSILON) PetscPrintf(PETSC_COMM_WORLD,"||exp(A)*exp(-A)-I||_F < 100*eps\n");
     77:     else PetscPrintf(PETSC_COMM_WORLD,"||exp(A)*exp(-A)-I||_F = %g\n",(double)nrm);
     78:     MatDestroy(&R);
     79:     MatDestroy(&Finv);
     80:   }
     81:   /* check FNEvaluateFunctionMatVec() */
     82:   MatCreateVecs(A,&v,&f0);
     83:   MatGetColumnVector(F,f0,0);
     84:   FNEvaluateFunctionMatVec(fn,A,v);
     85:   VecAXPY(v,-1.0,f0);
     86:   VecNorm(v,NORM_2,&nrm);
     87:   if (nrm/nrmf>100*PETSC_MACHINE_EPSILON) PetscPrintf(PETSC_COMM_WORLD,"Warning: the norm of f(A)*e_1-v is %g\n",(double)nrm);
     88:   MatDestroy(&F);
     89:   VecDestroy(&v);
     90:   VecDestroy(&f0);
     91:   return 0;
     92: }

     94: int main(int argc,char **argv)
     95: {
     96:   FN             fn;
     97:   Mat            A=NULL;
     98:   PetscInt       i,j,n=10;
     99:   PetscScalar    *As;
    100:   PetscViewer    viewer;
    101:   PetscBool      verbose,inplace,checkerror,matcuda;

    104:   SlepcInitialize(&argc,&argv,(char*)0,help);
    105:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
    106:   PetscOptionsHasName(NULL,NULL,"-verbose",&verbose);
    107:   PetscOptionsHasName(NULL,NULL,"-inplace",&inplace);
    108:   PetscOptionsHasName(NULL,NULL,"-checkerror",&checkerror);
    109:   PetscOptionsHasName(NULL,NULL,"-matcuda",&matcuda);
    110:   PetscPrintf(PETSC_COMM_WORLD,"Matrix exponential, n=%" PetscInt_FMT ".\n",n);

    112:   /* Create exponential function object */
    113:   FNCreate(PETSC_COMM_WORLD,&fn);
    114:   FNSetType(fn,FNEXP);
    115:   FNSetFromOptions(fn);

    117:   /* Set up viewer */
    118:   PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
    119:   FNView(fn,viewer);
    120:   if (verbose) PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);

    122:   /* Create matrices */
    123:   if (matcuda) {
    124: #if defined(PETSC_HAVE_CUDA)
    125:     MatCreateSeqDenseCUDA(PETSC_COMM_SELF,n,n,NULL,&A);
    126: #endif
    127:   } else MatCreateSeqDense(PETSC_COMM_SELF,n,n,NULL,&A);
    128:   PetscObjectSetName((PetscObject)A,"A");

    130:   /* Fill A with a symmetric Toeplitz matrix */
    131:   MatDenseGetArray(A,&As);
    132:   for (i=0;i<n;i++) As[i+i*n]=2.0;
    133:   for (j=1;j<3;j++) {
    134:     for (i=0;i<n-j;i++) { As[i+(i+j)*n]=1.0; As[(i+j)+i*n]=1.0; }
    135:   }
    136:   MatDenseRestoreArray(A,&As);
    137:   MatSetOption(A,MAT_HERMITIAN,PETSC_TRUE);
    138:   TestMatExp(fn,A,viewer,verbose,inplace,checkerror);

    140:   /* Repeat with non-symmetric A */
    141:   MatDenseGetArray(A,&As);
    142:   for (j=1;j<3;j++) {
    143:     for (i=0;i<n-j;i++) { As[(i+j)+i*n]=-1.0; }
    144:   }
    145:   MatDenseRestoreArray(A,&As);
    146:   MatSetOption(A,MAT_HERMITIAN,PETSC_FALSE);
    147:   TestMatExp(fn,A,viewer,verbose,inplace,checkerror);

    149:   MatDestroy(&A);
    150:   FNDestroy(&fn);
    151:   SlepcFinalize();
    152:   return 0;
    153: }

    155: /*TEST

    157:    testset:
    158:       filter: grep -v "computing matrix functions"
    159:       output_file: output/test3_1.out
    160:       test:
    161:          suffix: 1
    162:          args: -fn_method {{0 1}}
    163:       test:
    164:          suffix: 1_subdiagonalpade
    165:          args: -fn_method {{2 3}}
    166:          requires: c99_complex !single
    167:       test:
    168:          suffix: 1_cuda
    169:          args: -fn_method 1 -matcuda
    170:          requires: cuda !magma
    171:       test:
    172:          suffix: 1_magma
    173:          args: -fn_method {{0 1 2 3}} -matcuda
    174:          requires: cuda magma
    175:       test:
    176:          suffix: 2
    177:          args: -inplace -fn_method{{0 1}}
    178:       test:
    179:          suffix: 2_subdiagonalpade
    180:          args: -inplace -fn_method{{2 3}}
    181:          requires: c99_complex !single
    182:       test:
    183:          suffix: 2_cuda
    184:          args: -inplace -fn_method 1 -matcuda
    185:          requires: cuda !magma
    186:       test:
    187:          suffix: 2_magma
    188:          args: -inplace -fn_method {{0 1 2 3}} -matcuda
    189:          requires: cuda magma

    191:    testset:
    192:       args: -fn_scale 0.1
    193:       filter: grep -v "computing matrix functions"
    194:       output_file: output/test3_3.out
    195:       test:
    196:          suffix: 3
    197:          args: -fn_method {{0 1}}
    198:       test:
    199:         suffix: 3_subdiagonalpade
    200:         args: -fn_method {{2 3}}
    201:         requires: c99_complex !single

    203:    testset:
    204:       args: -n 120 -fn_scale 0.6,1.5
    205:       filter: grep -v "computing matrix functions"
    206:       output_file: output/test3_4.out
    207:       test:
    208:          suffix: 4
    209:          args: -fn_method {{0 1}}
    210:          requires: !single
    211:       test:
    212:         suffix: 4_subdiagonalpade
    213:         args: -fn_method {{2 3}}
    214:         requires: c99_complex !single

    216:    test:
    217:       suffix: 5
    218:       args: -fn_scale 30 -fn_method {{2 3}}
    219:       filter: grep -v "computing matrix functions"
    220:       requires: c99_complex !single
    221:       output_file: output/test3_5.out

    223:    test:
    224:       suffix: 6
    225:       args: -fn_scale 1e-9 -fn_method {{2 3}}
    226:       filter: grep -v "computing matrix functions"
    227:       requires: c99_complex !single
    228:       output_file: output/test3_6.out

    230: TEST*/