Actual source code: ex3.c

petsc-3.7.5 2017-01-01
Report Typos and Errors
  2: static char help[] = "Demonstrates creating a blocked index set.\n\n";

  4: /*T
  5:     Concepts: index sets^creating a block index set;
  6:     Concepts: IS^creating a block index set;

  8:     Description:  Creates an index set based on blocks of integers. Views that index set
  9:     and then destroys it.
 10: T*/

 12: #include <petscis.h>
 13: #include <petscviewer.h>

 17: int main(int argc,char **argv)
 18: {
 20:   PetscInt       i,n = 4, inputindices[] = {0,1,3,4},bs = 3,issize;
 21:   const PetscInt *indices;
 22:   IS             set;
 23:   PetscBool      isblock;

 25:   PetscInitialize(&argc,&argv,(char*)0,help);

 27:   /*
 28:     Create a block index set. The index set has 4 blocks each of size 3.
 29:     The indices are {0,1,2,3,4,5,9,10,11,12,13,14}
 30:     Note each processor is generating its own index set
 31:     (in this case they are all identical)
 32:   */
 33:   ISCreateBlock(PETSC_COMM_SELF,bs,n,inputindices,PETSC_COPY_VALUES,&set);
 34:   ISView(set,PETSC_VIEWER_STDOUT_SELF);

 36:   /*
 37:     Extract indices from set.
 38:   */
 39:   ISGetLocalSize(set,&issize);
 40:   ISGetIndices(set,&indices);
 41:   PetscPrintf(PETSC_COMM_SELF,"Printing indices directly\n");
 42:   for (i=0; i<issize; i++) {
 43:     PetscPrintf(PETSC_COMM_SELF,"%D\n",indices[i]);
 44:   }
 45:   ISRestoreIndices(set,&indices);

 47:   /*
 48:     Extract the block indices. This returns one index per block.
 49:   */
 50:   ISBlockGetIndices(set,&indices);
 51:   PetscPrintf(PETSC_COMM_SELF,"Printing block indices directly\n");
 52:   for (i=0; i<n; i++) {
 53:     PetscPrintf(PETSC_COMM_SELF,"%D\n",indices[i]);
 54:   }
 55:   ISBlockRestoreIndices(set,&indices);

 57:   /*
 58:     Check if this is really a block index set
 59:   */
 60:   PetscObjectTypeCompare((PetscObject)set,ISBLOCK,&isblock);
 61:   if (!isblock) SETERRQ(PETSC_COMM_SELF,1,"Index set is not blocked!");

 63:   /*
 64:     Determine the block size of the index set
 65:   */
 66:   ISGetBlockSize(set,&bs);
 67:   if (bs != 3) SETERRQ(PETSC_COMM_SELF,1,"Block size is not 3!");

 69:   /*
 70:     Get the number of blocks
 71:   */
 72:   ISBlockGetLocalSize(set,&n);
 73:   if (n != 4) SETERRQ(PETSC_COMM_SELF,1,"Number of blocks not 4!");

 75:   ISDestroy(&set);
 76:   PetscFinalize();
 77:   return 0;
 78: }