Actual source code: ex141.c

petsc-3.7.3 2016-08-01
Report Typos and Errors
  2: static char help[] = "Tests converting a SBAIJ matrix to BAIJ format with MatCovert. Modified from ex55.c\n\n";

  4: #include <petscmat.h>
  5: /* Usage: ./ex141 -mat_view */

  9: int main(int argc,char **args)
 10: {
 11:   Mat            C,B;
 13:   PetscInt       i,bs=2,mbs,m,block,d_nz=6,col[3];
 14:   PetscMPIInt    size;
 15:   char           file[PETSC_MAX_PATH_LEN];
 16:   PetscViewer    fd;
 17:   PetscBool      equal,loadmat;
 18:   PetscScalar    value[4];
 19:   PetscInt       ridx[2],cidx[2];

 21:   PetscInitialize(&argc,&args,(char*)0,help);
 22:   PetscOptionsGetString(NULL,NULL,"-f",file,PETSC_MAX_PATH_LEN,&loadmat);
 23:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 24:   if (size != 1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"This is a uniprocessor example only!");

 26:   /* input matrix C */
 27:   if (loadmat) {
 28:     /* Open binary file. Load a sbaij matrix, then destroy the viewer. */
 29:     PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);
 30:     MatCreate(PETSC_COMM_WORLD,&C);
 31:     MatSetType(C,MATSEQSBAIJ);
 32:     MatLoad(C,fd);
 33:     PetscViewerDestroy(&fd);
 34:   } else { /* Create a sbaij mat with bs>1  */
 35:     mbs  =8;
 36:     PetscOptionsGetInt(NULL,NULL,"-mbs",&mbs,NULL);
 37:     m    = mbs*bs;
 38:     MatCreate(PETSC_COMM_WORLD,&C);
 39:     MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m,m);
 40:     MatSetType(C,MATSBAIJ);
 41:     MatSetFromOptions(C);
 42:     MatSeqSBAIJSetPreallocation(C,bs,d_nz,NULL);
 43:     MatSetUp(C);
 44:     MatSetOption(C,MAT_IGNORE_LOWER_TRIANGULAR,PETSC_TRUE);

 46:     for (block=0; block<mbs; block++) {
 47:       /* diagonal blocks */
 48:       value[0] = -1.0; value[1] = 4.0; value[2] = -1.0;
 49:       for (i=1+block*bs; i<bs-1+block*bs; i++) {
 50:         col[0] = i-1; col[1] = i; col[2] = i+1;
 51:         MatSetValues(C,1,&i,3,col,value,INSERT_VALUES);
 52:       }
 53:       i = bs - 1+block*bs; col[0] = bs - 2+block*bs; col[1] = bs - 1+block*bs;

 55:       value[0]=-1.0; value[1]=4.0;
 56:       MatSetValues(C,1,&i,2,col,value,INSERT_VALUES);

 58:       i = 0+block*bs; col[0] = 0+block*bs; col[1] = 1+block*bs;

 60:       value[0]=4.0; value[1] = -1.0;
 61:       MatSetValues(C,1,&i,2,col,value,INSERT_VALUES);
 62:     }
 63:     /* off-diagonal blocks */
 64:     value[0]=-1.0; value[1] = -0.1; value[2] = 0.0; value[3] = -1.0; /* row-oriented */
 65:     for (block=0; block<mbs-1; block++) {
 66:       for (i=0; i<bs; i++) {
 67:         ridx[i] = block*bs+i; cidx[i] = (block+1)*bs+i;
 68:       }
 69:       MatSetValues(C,bs,ridx,bs,cidx,value,INSERT_VALUES);
 70:     }
 71:     MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 72:     MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
 73:   }

 75:   /* convert C to BAIJ format */
 76:   MatConvert(C,MATSEQBAIJ,MAT_INITIAL_MATRIX,&B);
 77:   MatMultEqual(B,C,10,&equal);
 78:   if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"MatConvert fails!");

 80:   MatDestroy(&B);
 81:   MatDestroy(&C);
 82:   PetscFinalize();
 83:   return 0;
 84: }