Actual source code: ex45.c

petsc-3.7.2 2016-06-05
Report Typos and Errors
  2: static char help[] = "Demonstrates VecStrideSubSetScatter() and VecStrideSubSetGather().\n\n";

  4: /*T
  5:    Concepts: vectors^sub-vectors;
  6:    Processors: n

  8:    Allows one to easily pull out some components of a multi-component vector and put them in another vector.

 10:    Note that these are special cases of VecScatter
 11: T*/

 13: /*
 14:   Include "petscvec.h" so that we can use vectors.  Note that this file
 15:   automatically includes:
 16:      petscsys.h       - base PETSc routines   petscis.h     - index sets
 17:      petscviewer.h - viewers
 18: */

 20: #include <petscvec.h>

 24: int main(int argc,char **argv)
 25: {
 26:   Vec            v,s;
 27:   PetscInt       i,start,end,n = 8;
 29:   PetscScalar    value;
 30:   const PetscInt vidx[] = {1,2},sidx[] = {1,0};

 32:   PetscInitialize(&argc,&argv,(char*)0,help);
 33:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);

 35:   /*
 36:       Create multi-component vector with 4 components
 37:   */
 38:   VecCreate(PETSC_COMM_WORLD,&v);
 39:   VecSetSizes(v,PETSC_DECIDE,n);
 40:   VecSetBlockSize(v,4);
 41:   VecSetFromOptions(v);

 43:   /*
 44:       Create double-component vectors
 45:   */
 46:   VecCreate(PETSC_COMM_WORLD,&s);
 47:   VecSetSizes(s,PETSC_DECIDE,n/2);
 48:   VecSetBlockSize(s,2);
 49:   VecSetFromOptions(s);

 51:   /*
 52:      Set the vector values
 53:   */
 54:   VecGetOwnershipRange(v,&start,&end);
 55:   for (i=start; i<end; i++) {
 56:     value = i;
 57:     VecSetValues(v,1,&i,&value,INSERT_VALUES);
 58:   }

 60:   /*
 61:      Get the components from the large multi-component vector to the small multi-component vector,
 62:      scale the smaller vector and then move values back to the large vector
 63:   */
 64:   VecStrideSubSetGather(v,PETSC_DETERMINE,vidx,NULL,s,INSERT_VALUES);
 65:   VecView(s,PETSC_VIEWER_STDOUT_WORLD);
 66:   VecScale(s,100.0);

 68:   VecStrideSubSetScatter(s,PETSC_DETERMINE,NULL,vidx,v,ADD_VALUES);
 69:   VecView(v,PETSC_VIEWER_STDOUT_WORLD);

 71:   /*
 72:      Get the components from the large multi-component vector to the small multi-component vector,
 73:      scale the smaller vector and then move values back to the large vector
 74:   */
 75:   VecStrideSubSetGather(v,2,vidx,sidx,s,INSERT_VALUES);
 76:   VecView(s,PETSC_VIEWER_STDOUT_WORLD);
 77:   VecScale(s,100.0);

 79:   VecStrideSubSetScatter(s,2,sidx,vidx,v,ADD_VALUES);
 80:   VecView(v,PETSC_VIEWER_STDOUT_WORLD);

 82:   /*
 83:      Free work space.  All PETSc objects should be destroyed when they
 84:      are no longer needed.
 85:   */
 86:   VecDestroy(&v);
 87:   VecDestroy(&s);
 88:   PetscFinalize();
 89:   return 0;
 90: }