Actual source code: ex11.c


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

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

  9: /*
 10:   Include "petscvec.h" so that we can use vectors.  Note that this file
 11:   automatically includes:
 12:      petscsys.h       - base PETSc routines   petscis.h     - index sets
 13:      petscviewer.h - viewers
 14: */

 16: #include <petscvec.h>

 18: int main(int argc,char **argv)
 19: {
 20:   Vec            x;               /* vectors */
 21:   PetscReal      norm;
 22:   PetscInt       n = 20;
 23:   PetscScalar    one = 1.0;

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

 28:   /*
 29:      Create a vector, specifying only its global dimension.
 30:      When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
 31:      the vector format (currently parallel,
 32:      shared, or sequential) is determined at runtime.  Also, the parallel
 33:      partitioning of the vector is determined by PETSc at runtime.

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

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

 47:   */
 48:   VecCreate(PETSC_COMM_WORLD,&x);
 49:   VecSetSizes(x,PETSC_DECIDE,n);
 50:   VecSetBlockSize(x,2);
 51:   VecSetFromOptions(x);

 53:   /*
 54:      Set the vectors to entries to a constant value.
 55:   */
 56:   VecSet(x,one);

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

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

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

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

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

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

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

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

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

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

 94: /*TEST

 96:      test:
 97:        nsize: 2

 99: TEST*/