Actual source code: ex15.c
1: static char help[] = "Tests VecSetValuesBlocked() on sequential vectors.\n\n";
3: #include <petscvec.h>
5: int main(int argc, char **argv)
6: {
7: PetscMPIInt size;
8: PetscInt n = 9, bs = 3, indices[2], i;
9: PetscScalar values[6];
10: Vec x;
12: PetscFunctionBeginUser;
13: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
14: PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
16: PetscCheck(size == 1, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "Must be run with one processor");
18: /* create vector */
19: PetscCall(VecCreate(PETSC_COMM_SELF, &x));
20: PetscCall(VecSetSizes(x, n, n));
21: PetscCall(VecSetBlockSize(x, bs));
22: PetscCall(VecSetType(x, VECSEQ));
24: for (i = 0; i < 6; i++) values[i] = 4.0 * i;
25: indices[0] = 0;
26: indices[1] = 2;
28: PetscCall(VecSetValuesBlocked(x, 2, indices, values, INSERT_VALUES));
29: PetscCall(VecAssemblyBegin(x));
30: PetscCall(VecAssemblyEnd(x));
32: /*
33: Resulting vector should be 0 4 8 0 0 0 12 16 20
34: */
35: PetscCall(VecView(x, PETSC_VIEWER_STDOUT_WORLD));
37: /* test insertion with negative indices */
38: PetscCall(VecSetOption(x, VEC_IGNORE_NEGATIVE_INDICES, PETSC_TRUE));
39: for (i = 0; i < 6; i++) values[i] = -4.0 * i;
40: indices[0] = -1;
41: indices[1] = 2;
43: PetscCall(VecSetValuesBlocked(x, 2, indices, values, ADD_VALUES));
44: PetscCall(VecAssemblyBegin(x));
45: PetscCall(VecAssemblyEnd(x));
47: /*
48: Resulting vector should be 0 4 8 0 0 0 0 0 0
49: */
50: PetscCall(VecView(x, PETSC_VIEWER_STDOUT_WORLD));
52: PetscCall(VecDestroy(&x));
54: PetscCall(PetscFinalize());
55: return 0;
56: }
58: /*TEST
60: test:
62: TEST*/