xref: /petsc/src/vec/vec/tests/ex16.c (revision ebead697dbf761eb322f829370bbe90b3bd93fa3)
1 static char help[] = "Tests VecSetValuesBlocked() on MPI vectors.\n\n";
2 
3 #include <petscvec.h>
4 
5 int main(int argc,char **argv)
6 {
7   PetscMPIInt    size,rank;
8   PetscInt       i,n = 8,bs = 2,indices[2];
9   PetscScalar    values[4];
10   Vec            x;
11 
12   PetscFunctionBeginUser;
13   PetscCall(PetscInitialize(&argc,&argv,(char*)0,help));
14   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size));
15   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
16 
17   PetscCheck(size == 2,PETSC_COMM_WORLD,PETSC_ERR_WRONG_MPI_SIZE,"Must be run with two processors");
18 
19   /* create vector */
20   PetscCall(VecCreate(PETSC_COMM_WORLD,&x));
21   PetscCall(VecSetSizes(x,PETSC_DECIDE,n));
22   PetscCall(VecSetBlockSize(x,bs));
23   PetscCall(VecSetFromOptions(x));
24 
25   if (rank == 0) {
26     for (i=0; i<4; i++) values[i] = i+1;
27     indices[0] = 0;
28     indices[1] = 2;
29     PetscCall(VecSetValuesBlocked(x,2,indices,values,INSERT_VALUES));
30   }
31   PetscCall(VecAssemblyBegin(x));
32   PetscCall(VecAssemblyEnd(x));
33 
34   /*
35       Resulting vector should be 1 2 0 0 3 4 0 0
36   */
37   PetscCall(VecView(x,PETSC_VIEWER_STDOUT_WORLD));
38 
39   /* test insertion with negative indices */
40   PetscCall(VecSetOption(x,VEC_IGNORE_NEGATIVE_INDICES,PETSC_TRUE));
41   if (rank == 0) {
42     for (i=0; i<4; i++) values[i] = -(i+1);
43     indices[0] = -1;
44     indices[1] = 3;
45     PetscCall(VecSetValuesBlocked(x,2,indices,values,INSERT_VALUES));
46   }
47   PetscCall(VecAssemblyBegin(x));
48   PetscCall(VecAssemblyEnd(x));
49 
50   /*
51       Resulting vector should be 1 2 0 0 3 4 -3 -4
52   */
53   PetscCall(VecView(x,PETSC_VIEWER_STDOUT_WORLD));
54 
55   PetscCall(VecDestroy(&x));
56 
57   PetscCall(PetscFinalize());
58   return 0;
59 }
60 
61 /*TEST
62 
63    test:
64       nsize: 2
65 
66 TEST*/
67