Date: 2025-03-29T05:58:55+00:00

Actual source code: ex11.c

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

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

     10: #include <petscvec.h>

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

     19:   PetscFunctionBeginUser;
     20:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
     21:   PetscCall(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.
     29:   */
     30:   PetscCall(VecCreate(PETSC_COMM_WORLD, &x));
     31:   PetscCall(VecSetSizes(x, PETSC_DECIDE, n));
     32:   PetscCall(VecSetBlockSize(x, 2));
     33:   PetscCall(VecSetFromOptions(x));

     35:   /*
     36:      Set the vectors to entries to a constant value.
     37:   */
     38:   PetscCall(VecSet(x, one));

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

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

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

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

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

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

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

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

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

     67:   /*
     68:      Free work space.  All PETSc objects should be destroyed when they
     69:      are no longer needed.
     70:   */
     71:   PetscCall(VecDestroy(&x));
     72:   PetscCall(PetscFinalize());
     73:   return 0;
     74: }

     76: /*TEST

     78:      test:
     79:        nsize: 2

     81: TEST*/