Actual source code: ex15.c


  2: static char help[] = "Tests VecSetValuesBlocked() on sequential vectors.\n\n";

  4: #include <petscvec.h>

  6: int main(int argc, char **argv)
  7: {
  8:   PetscMPIInt size;
  9:   PetscInt    n = 9, bs = 3, indices[2], i;
 10:   PetscScalar values[6];
 11:   Vec         x;

 13:   PetscFunctionBeginUser;
 14:   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
 15:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));

 17:   PetscCheck(size == 1, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "Must be run with one processor");

 19:   /* create vector */
 20:   PetscCall(VecCreate(PETSC_COMM_SELF, &x));
 21:   PetscCall(VecSetSizes(x, n, n));
 22:   PetscCall(VecSetBlockSize(x, bs));
 23:   PetscCall(VecSetType(x, VECSEQ));

 25:   for (i = 0; i < 6; i++) values[i] = 4.0 * i;
 26:   indices[0] = 0;
 27:   indices[1] = 2;

 29:   PetscCall(VecSetValuesBlocked(x, 2, indices, values, INSERT_VALUES));
 30:   PetscCall(VecAssemblyBegin(x));
 31:   PetscCall(VecAssemblyEnd(x));

 33:   /*
 34:       Resulting vector should be 0 4 8  0 0 0 12 16 20
 35:   */
 36:   PetscCall(VecView(x, PETSC_VIEWER_STDOUT_WORLD));

 38:   /* test insertion with negative indices */
 39:   PetscCall(VecSetOption(x, VEC_IGNORE_NEGATIVE_INDICES, PETSC_TRUE));
 40:   for (i = 0; i < 6; i++) values[i] = -4.0 * i;
 41:   indices[0] = -1;
 42:   indices[1] = 2;

 44:   PetscCall(VecSetValuesBlocked(x, 2, indices, values, ADD_VALUES));
 45:   PetscCall(VecAssemblyBegin(x));
 46:   PetscCall(VecAssemblyEnd(x));

 48:   /*
 49:       Resulting vector should be 0 4 8  0 0 0 0 0 0
 50:   */
 51:   PetscCall(VecView(x, PETSC_VIEWER_STDOUT_WORLD));

 53:   PetscCall(VecDestroy(&x));

 55:   PetscCall(PetscFinalize());
 56:   return 0;
 57: }

 59: /*TEST

 61:    test:

 63: TEST*/