Date: 2022-03-04T17:37:29+00:00

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;
     24:   PetscScalar    one = 1.0;

     26:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
     27:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

     95: /*TEST

     97:      test:
     98:        nsize: 2

    100: TEST*/