Date: 2022-11-02T11:46:27+00:00

Actual source code: test8.c
slepc-3.18.1 2022-11-02
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 BV orthogonalization with selected columns.\n\n";

     13: #include <slepcbv.h>

     15: int main(int argc,char **argv)
     16: {
     17:   BV             X;
     18:   Vec            v,t,z;
     19:   PetscInt       i,j,n=20,k=8;
     20:   PetscViewer    view;
     21:   PetscBool      verbose,*which;
     22:   PetscReal      norm;
     23:   PetscScalar    alpha,*pz;

     26:   SlepcInitialize(&argc,&argv,(char*)0,help);
     27:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
     28:   PetscOptionsGetInt(NULL,NULL,"-k",&k,NULL);
     29:   PetscOptionsHasName(NULL,NULL,"-verbose",&verbose);
     30:   PetscPrintf(PETSC_COMM_WORLD,"Test BV orthogonalization with selected columns of length %" PetscInt_FMT ".\n",n);

     32:   /* Create template vector */
     33:   VecCreate(PETSC_COMM_WORLD,&t);
     34:   VecSetSizes(t,PETSC_DECIDE,n);
     35:   VecSetFromOptions(t);

     37:   /* Create BV object X */
     38:   BVCreate(PETSC_COMM_WORLD,&X);
     39:   PetscObjectSetName((PetscObject)X,"X");
     40:   BVSetSizesFromVec(X,t,k);
     41:   BVSetOrthogonalization(X,BV_ORTHOG_MGS,BV_ORTHOG_REFINE_IFNEEDED,PETSC_DEFAULT,BV_ORTHOG_BLOCK_GS);
     42:   BVSetFromOptions(X);

     44:   /* Set up viewer */
     45:   PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&view);
     46:   if (verbose) PetscViewerPushFormat(view,PETSC_VIEWER_ASCII_MATLAB);

     48:   /* Fill X entries */
     49:   for (j=0;j<k;j++) {
     50:     BVGetColumn(X,j,&v);
     51:     VecSet(v,0.0);
     52:     for (i=0;i<=n/2;i++) {
     53:       if (i+j<n) {
     54:         alpha = (3.0*i+j-2)/(2*(i+j+1));
     55:         VecSetValue(v,i+j,alpha,INSERT_VALUES);
     56:       }
     57:     }
     58:     VecAssemblyBegin(v);
     59:     VecAssemblyEnd(v);
     60:     BVRestoreColumn(X,j,&v);
     61:   }
     62:   if (verbose) BVView(X,view);

     64:   /* Orthonormalize first k-1 columns */
     65:   for (j=0;j<k-1;j++) {
     66:     BVOrthogonalizeColumn(X,j,NULL,&norm,NULL);
     67:     alpha = 1.0/norm;
     68:     BVScaleColumn(X,j,alpha);
     69:   }
     70:   if (verbose) BVView(X,view);

     72:   /* Select odd columns and orthogonalize last column against those only */
     73:   PetscMalloc1(k,&which);
     74:   for (i=0;i<k;i++) which[i] = (i%2)? PETSC_TRUE: PETSC_FALSE;
     75:   BVOrthogonalizeSomeColumn(X,k-1,which,NULL,NULL,NULL);
     76:   PetscFree(which);
     77:   if (verbose) BVView(X,view);

     79:   /* Check orthogonality */
     80:   PetscPrintf(PETSC_COMM_WORLD,"Orthogonalization coefficients:\n");
     81:   VecCreateSeq(PETSC_COMM_SELF,k-1,&z);
     82:   PetscObjectSetName((PetscObject)z,"z");
     83:   VecGetArray(z,&pz);
     84:   BVDotColumn(X,k-1,pz);
     85:   for (i=0;i<k-1;i++) {
     86:     if (PetscAbsScalar(pz[i])<5.0*PETSC_MACHINE_EPSILON) pz[i]=0.0;
     87:   }
     88:   VecRestoreArray(z,&pz);
     89:   VecView(z,view);
     90:   VecDestroy(&z);

     92:   BVDestroy(&X);
     93:   VecDestroy(&t);
     94:   SlepcFinalize();
     95:   return 0;
     96: }

     98: /*TEST

    100:    testset:
    101:       output_file: output/test8_1.out
    102:       test:
    103:          suffix: 1
    104:          args: -bv_type {{vecs contiguous svec mat}shared output}
    105:       test:
    106:          suffix: 1_cuda
    107:          args: -bv_type svec -vec_type cuda
    108:          requires: cuda
    109:       test:
    110:          suffix: 2
    111:          args: -bv_type {{vecs contiguous svec mat}shared output} -bv_orthog_refine never
    112:          requires: !single
    113:       test:
    114:          suffix: 3
    115:          args: -bv_type {{vecs contiguous svec mat}shared output} -bv_orthog_refine always
    116:       test:
    117:          suffix: 4
    118:          args: -bv_type {{vecs contiguous svec mat}shared output} -bv_orthog_type mgs
    119:       test:
    120:          suffix: 4_cuda
    121:          args: -bv_type svec -vec_type cuda -bv_orthog_type mgs
    122:          requires: cuda

    124: TEST*/