Actual source code: ex11.c


  2: static char help[] = "Demonstrates VecStrideNorm().\n\n";

  4: /*
  5:   Include "petscvec.h" so that we can use vectors.  Note that this file
  6:   automatically includes:
  7:      petscsys.h       - base PETSc routines   petscis.h     - index sets
  8:      petscviewer.h - viewers
  9: */

 11: #include <petscvec.h>

 13: int main(int argc,char **argv)
 14: {
 15:   Vec            x;               /* vectors */
 16:   PetscReal      norm;
 17:   PetscInt       n = 20;
 18:   PetscScalar    one = 1.0;

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

 23:   /*
 24:      Create a vector, specifying only its global dimension.
 25:      When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
 26:      the vector format (currently parallel,
 27:      shared, or sequential) is determined at runtime.  Also, the parallel
 28:      partitioning of the vector is determined by PETSc at runtime.

 30:      Routines for creating particular vector types directly are:
 31:         VecCreateSeq() - uniprocessor vector
 32:         VecCreateMPI() - distributed vector, where the user can
 33:                          determine the parallel partitioning
 34:         VecCreateShared() - parallel vector that uses shared memory
 35:                             (available only on the SGI); otherwise,
 36:                             is the same as VecCreateMPI()

 38:      With VecCreate(), VecSetSizes() and VecSetFromOptions() the option
 39:      -vec_type mpi or -vec_type shared causes the
 40:      particular type of vector to be formed.

 42:   */
 43:   VecCreate(PETSC_COMM_WORLD,&x);
 44:   VecSetSizes(x,PETSC_DECIDE,n);
 45:   VecSetBlockSize(x,2);
 46:   VecSetFromOptions(x);

 48:   /*
 49:      Set the vectors to entries to a constant value.
 50:   */
 51:   VecSet(x,one);

 53:   VecNorm(x,NORM_2,&norm);
 54:   PetscPrintf(PETSC_COMM_WORLD,"L_2 Norm of entire vector: %g\n",(double)norm);

 56:   VecNorm(x,NORM_1,&norm);
 57:   PetscPrintf(PETSC_COMM_WORLD,"L_1 Norm of entire vector: %g\n",(double)norm);

 59:   VecNorm(x,NORM_INFINITY,&norm);
 60:   PetscPrintf(PETSC_COMM_WORLD,"L_inf Norm of entire vector: %g\n",(double)norm);

 62:   VecStrideNorm(x,0,NORM_2,&norm);
 63:   PetscPrintf(PETSC_COMM_WORLD,"L_2 Norm of sub-vector 0: %g\n",(double)norm);

 65:   VecStrideNorm(x,0,NORM_1,&norm);
 66:   PetscPrintf(PETSC_COMM_WORLD,"L_1 Norm of sub-vector 0: %g\n",(double)norm);

 68:   VecStrideNorm(x,0,NORM_INFINITY,&norm);
 69:   PetscPrintf(PETSC_COMM_WORLD,"L_inf Norm of sub-vector 0: %g\n",(double)norm);

 71:   VecStrideNorm(x,1,NORM_2,&norm);
 72:   PetscPrintf(PETSC_COMM_WORLD,"L_2 Norm of sub-vector 1: %g\n",(double)norm);

 74:   VecStrideNorm(x,1,NORM_1,&norm);
 75:   PetscPrintf(PETSC_COMM_WORLD,"L_1 Norm of sub-vector 1: %g\n",(double)norm);

 77:   VecStrideNorm(x,1,NORM_INFINITY,&norm);
 78:   PetscPrintf(PETSC_COMM_WORLD,"L_inf Norm of sub-vector 1: %g\n",(double)norm);

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

 89: /*TEST

 91:      test:
 92:        nsize: 2

 94: TEST*/