Actual source code: ex3.c

petsc-3.3-p7 2013-05-11
  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>

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

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

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

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

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

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

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

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