Actual source code: ex47.c

  2: static char help[] = "Tests the various routines in MatBAIJ format.\n\
  3: Input arguments are:\n\
  4:   -f <input_file> : file to load.  For a 5X5 example of the 5-pt. stencil,\n\
  5:                     use the file petsc/src/mat/examples/matbinary.ex\n\n";

 7:  #include petscmat.h

 11: int main(int argc,char **args)
 12: {
 13:   Mat               A,B,C;
 14:   PetscViewer       va,vb,vc;
 15:   Vec               x,y;
 16:   PetscErrorCode    ierr;
 17:   PetscInt          i,j,row,m,n,ncols1,ncols2,ct,m2,n2;
 18:   const PetscInt    *cols1,*cols2;
 19:   char              file[PETSC_MAX_PATH_LEN];
 20:   PetscTruth        tflg;
 21:   PetscScalar       rval;
 22:   const PetscScalar *vals1,*vals2;
 23:   PetscReal         norm1,norm2,rnorm;
 24:   PetscRandom       r;


 27:   PetscInitialize(&argc,&args,(char *)0,help);
 28: #if defined(PETSC_USE_COMPLEX)
 29:   SETERRQ(1,"This example does not work with complex numbers");
 30: #else
 31: 
 32:   PetscOptionsGetString(PETSC_NULL,"-f",file,PETSC_MAX_PATH_LEN,PETSC_NULL);

 34:   /* Load the matrix as AIJ format */
 35:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&va);
 36:   MatLoad(va,MATSEQAIJ,&A);
 37:   PetscViewerDestroy(va);

 39:   /* Load the matrix as BAIJ format */
 40:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&vb);
 41:   MatLoad(vb,MATSEQBAIJ,&B);
 42:   PetscViewerDestroy(vb);

 44:   /* Load the matrix as BAIJ format */
 45:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&vc);
 46:   MatLoad(vc,MATSEQBAIJ,&C);
 47:   PetscViewerDestroy(vc);

 49:   MatGetSize(A,&m,&n);
 50:   MatGetSize(B,&m2,&n2);
 51:   if (m!=m2) SETERRQ(1,"Matrices are of different size. Cannot run this example");
 52: 
 53:   /* Test MatEqual() */
 54:   MatEqual(B,C,&tflg);
 55:   if (!tflg) SETERRQ(1,"MatEqual() failed");

 57:   /* Test MatGetDiagonal() */
 58:    VecCreateSeq(PETSC_COMM_SELF,m,&x);
 59:    VecCreateSeq(PETSC_COMM_SELF,m,&y);

 61:   MatGetDiagonal(A,x);
 62:   MatGetDiagonal(B,y);
 63: 
 64:   VecEqual(x,y,&tflg);
 65:   if (!tflg)  SETERRQ(1,"MatGetDiagonal() failed");

 67:   /* Test MatDiagonalScale() */
 68:   PetscRandomCreate(PETSC_COMM_SELF,&r);
 69:   PetscRandomSetFromOptions(r);
 70:   VecSetRandom(x,r);
 71:   VecSetRandom(y,r);

 73:   MatDiagonalScale(A,x,y);
 74:   MatDiagonalScale(B,x,y);
 75:   MatMult(A,x,y);
 76:   VecNorm(y,NORM_2,&norm1);
 77:   MatMult(B,x,y);
 78:   VecNorm(y,NORM_2,&norm2);
 79:   rnorm = ((norm1-norm2)*100)/norm1;
 80:   if (rnorm<-0.1 || rnorm>0.01) {
 81:     PetscPrintf(PETSC_COMM_SELF,"Norm1=%e Norm2=%e\n",norm1,norm2);
 82:     SETERRQ(1,"MatDiagonalScale() failed");
 83:   }

 85:   /* Test MatGetRow()/ MatRestoreRow() */
 86:   for (ct=0; ct<100; ct++) {
 87:     PetscRandomGetValue(r,&rval);
 88:     row  = (int)(rval*m);
 89:     MatGetRow(A,row,&ncols1,&cols1,&vals1);
 90:     MatGetRow(B,row,&ncols2,&cols2,&vals2);
 91: 
 92:     for (i=0,j=0; i<ncols1 && j<ncols2; i++) {
 93:       while (cols2[j] != cols1[i]) j++;
 94:       if (vals1[i] != vals2[j]) SETERRQ(1,"MatGetRow() failed - vals incorrect.");
 95:     }
 96:     if (i<ncols1) SETERRQ(1,"MatGetRow() failed - cols incorrect");
 97: 
 98:     MatRestoreRow(A,row,&ncols1,&cols1,&vals1);
 99:     MatRestoreRow(B,row,&ncols2,&cols2,&vals2);
100:   }
101: 
102:   MatDestroy(A);
103:   MatDestroy(B);
104:   MatDestroy(C);
105:   VecDestroy(x);
106:   VecDestroy(y);
107:   PetscRandomDestroy(r);
108:   PetscFinalize();
109: #endif
110:   return 0;
111: }