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;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

 90: /*TEST

 92:      test:
 93:        nsize: 2

 95: TEST*/