Date: 2021-02-01T14:41:49+00:00

Actual source code: test18.c
slepc-3.14.2 2021-02-01
Report Typos and Errors

      1: /*
      2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
      4:    Copyright (c) 2002-2020, 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 BVNormalize().\n\n";

     13: #include <slepcbv.h>

     15: int main(int argc,char **argv)
     16: {
     18:   BV             X,Y,Z;
     19:   Mat            B;
     20:   Vec            v,t;
     21:   PetscInt       i,j,n=20,k=8,l=3,Istart,Iend;
     22:   PetscViewer    view;
     23:   PetscBool      verbose;
     24:   PetscReal      norm,error;
     25:   PetscScalar    alpha;
     26: #if !defined(PETSC_USE_COMPLEX)
     27:   PetscScalar    *eigi;
     28:   PetscRandom    rand;
     29:   PetscReal      normr,normi;
     30:   Vec            vi;
     31: #endif

     33:   SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
     34:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
     35:   PetscOptionsGetInt(NULL,NULL,"-k",&k,NULL);
     36:   PetscOptionsGetInt(NULL,NULL,"-l",&l,NULL);
     37:   PetscOptionsHasName(NULL,NULL,"-verbose",&verbose);
     38:   PetscPrintf(PETSC_COMM_WORLD,"Test BV normalization with %D columns of length %D.\n",k,n);

     40:   /* Create template vector */
     41:   VecCreate(PETSC_COMM_WORLD,&t);
     42:   VecSetSizes(t,PETSC_DECIDE,n);
     43:   VecSetFromOptions(t);

     45:   /* Create BV object X */
     46:   BVCreate(PETSC_COMM_WORLD,&X);
     47:   PetscObjectSetName((PetscObject)X,"X");
     48:   BVSetSizesFromVec(X,t,k);
     49:   BVSetFromOptions(X);

     51:   /* Set up viewer */
     52:   PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&view);
     53:   if (verbose) {
     54:     PetscViewerPushFormat(view,PETSC_VIEWER_ASCII_MATLAB);
     55:   }

     57:   /* Fill X entries */
     58:   for (j=0;j<k;j++) {
     59:     BVGetColumn(X,j,&v);
     60:     VecSet(v,0.0);
     61:     for (i=0;i<=n/2;i++) {
     62:       if (i+j<n) {
     63:         alpha = (3.0*i+j-2)/(2*(i+j+1));
     64:         VecSetValue(v,i+j,alpha,INSERT_VALUES);
     65:       }
     66:     }
     67:     VecAssemblyBegin(v);
     68:     VecAssemblyEnd(v);
     69:     BVRestoreColumn(X,j,&v);
     70:   }
     71:   if (verbose) {
     72:     BVView(X,view);
     73:   }

     75:   /* Create copies on Y and Z */
     76:   BVDuplicate(X,&Y);
     77:   PetscObjectSetName((PetscObject)Y,"Y");
     78:   BVCopy(X,Y);
     79:   BVDuplicate(X,&Z);
     80:   PetscObjectSetName((PetscObject)Z,"Z");
     81:   BVCopy(X,Z);
     82:   BVSetActiveColumns(X,l,k);
     83:   BVSetActiveColumns(Y,l,k);
     84:   BVSetActiveColumns(Z,l,k);

     86:   /* Test BVNormalize */
     87:   BVNormalize(X,NULL);
     88:   if (verbose) {
     89:     BVView(X,view);
     90:   }

     92:   /* Check unit norm of columns */
     93:   error = 0.0;
     94:   for (j=l;j<k;j++) {
     95:     BVNormColumn(X,j,NORM_2,&norm);
     96:     error = PetscMax(error,PetscAbsReal(norm-PetscRealConstant(1.0)));
     97:   }
     98:   if (error<100*PETSC_MACHINE_EPSILON) {
     99:     PetscPrintf(PETSC_COMM_WORLD,"Deviation from normalized vectors < 100*eps\n");
    100:   } else {
    101:     PetscPrintf(PETSC_COMM_WORLD,"Deviation from normalized vectors: %g\n",(double)norm);
    102:   }

    104:   /* Create inner product matrix */
    105:   MatCreate(PETSC_COMM_WORLD,&B);
    106:   MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,n,n);
    107:   MatSetFromOptions(B);
    108:   MatSetUp(B);
    109:   PetscObjectSetName((PetscObject)B,"B");

    111:   MatGetOwnershipRange(B,&Istart,&Iend);
    112:   for (i=Istart;i<Iend;i++) {
    113:     if (i>0) { MatSetValue(B,i,i-1,-1.0,INSERT_VALUES); }
    114:     if (i<n-1) { MatSetValue(B,i,i+1,-1.0,INSERT_VALUES); }
    115:     MatSetValue(B,i,i,2.0,INSERT_VALUES);
    116:   }
    117:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
    118:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
    119:   if (verbose) {
    120:     MatView(B,view);
    121:   }

    123:   /* Test BVNormalize with B-norm */
    124:   BVSetMatrix(Y,B,PETSC_FALSE);
    125:   BVNormalize(Y,NULL);
    126:   if (verbose) {
    127:     BVView(Y,view);
    128:   }

    130:   /* Check unit B-norm of columns */
    131:   error = 0.0;
    132:   for (j=l;j<k;j++) {
    133:     BVNormColumn(Y,j,NORM_2,&norm);
    134:     error = PetscMax(error,PetscAbsReal(norm-PetscRealConstant(1.0)));
    135:   }
    136:   if (error<100*PETSC_MACHINE_EPSILON) {
    137:     PetscPrintf(PETSC_COMM_WORLD,"Deviation from B-normalized vectors < 100*eps\n");
    138:   } else {
    139:     PetscPrintf(PETSC_COMM_WORLD,"Deviation from B-normalized vectors: %g\n",(double)norm);
    140:   }

    142: #if !defined(PETSC_USE_COMPLEX)
    143:   /* fill imaginary parts */
    144:   PetscCalloc1(k,&eigi);
    145:   BVGetRandomContext(Z,&rand);
    146:   for (j=l+1;j<k-1;j+=5) {
    147:     PetscRandomGetValue(rand,&alpha);
    148:     eigi[j]   =  alpha;
    149:     eigi[j+1] = -alpha;
    150:   }
    151:   if (verbose) {
    152:     VecCreateSeqWithArray(PETSC_COMM_SELF,1,k,eigi,&v);
    153:     VecView(v,view);
    154:     VecDestroy(&v);
    155:   }

    157:   /* Test BVNormalize with complex conjugate columns */
    158:   BVNormalize(Z,eigi);
    159:   if (verbose) {
    160:     BVView(Z,view);
    161:   }

    163:   /* Check unit norm of (complex conjugate) columns */
    164:   error = 0.0;
    165:   for (j=l;j<k;j++) {
    166:     if (eigi[j]) {
    167:       BVGetColumn(Z,j,&v);
    168:       BVGetColumn(Z,j+1,&vi);
    169:       VecNormBegin(v,NORM_2,&normr);
    170:       VecNormBegin(vi,NORM_2,&normi);
    171:       VecNormEnd(v,NORM_2,&normr);
    172:       VecNormEnd(vi,NORM_2,&normi);
    173:       BVRestoreColumn(Z,j+1,&vi);
    174:       BVRestoreColumn(Z,j,&v);
    175:       norm = SlepcAbsEigenvalue(normr,normi);
    176:       j++;
    177:     } else {
    178:       BVGetColumn(Z,j,&v);
    179:       VecNorm(v,NORM_2,&norm);
    180:       BVRestoreColumn(Z,j,&v);
    181:     }
    182:     error = PetscMax(error,PetscAbsReal(norm-PetscRealConstant(1.0)));
    183:   }
    184:   if (error<100*PETSC_MACHINE_EPSILON) {
    185:     PetscPrintf(PETSC_COMM_WORLD,"Deviation from normalized conjugate vectors < 100*eps\n");
    186:   } else {
    187:     PetscPrintf(PETSC_COMM_WORLD,"Deviation from normalized conjugate vectors: %g\n",(double)norm);
    188:   }
    189:   PetscFree(eigi);
    190: #endif

    192:   BVDestroy(&X);
    193:   BVDestroy(&Y);
    194:   BVDestroy(&Z);
    195:   MatDestroy(&B);
    196:   VecDestroy(&t);
    197:   SlepcFinalize();
    198:   return ierr;
    199: }

    201: /*TEST

    203:    testset:
    204:       args: -n 250 -l 6 -k 15
    205:       nsize: {{1 2}}
    206:       requires: !complex
    207:       output_file: output/test18_1.out
    208:       test:
    209:          suffix: 1
    210:          args: -bv_type {{vecs contiguous svec mat}}
    211:       test:
    212:          suffix: 1_cuda
    213:          args: -bv_type svec -vec_type cuda
    214:          requires: cuda

    216:    testset:
    217:       args: -n 250 -l 6 -k 15
    218:       nsize: {{1 2}}
    219:       requires: complex
    220:       output_file: output/test18_1_complex.out
    221:       test:
    222:          suffix: 1_complex
    223:          args: -bv_type {{vecs contiguous svec mat}}
    224:       test:
    225:          suffix: 1_cuda_complex
    226:          args: -bv_type svec -vec_type cuda
    227:          requires: cuda

    229: TEST*/