xref: /petsc/src/vec/vec/tests/ex40.c (revision 5520554388890bd89a1c1cf7870aedf4e71d512f)
1 
2 static char help[] = "Tests taking part of existing array to create a new vector.\n\n";
3 
4 #include <petscvec.h>
5 
6 int main(int argc, char **argv) {
7   PetscMPIInt size;
8   PetscInt    n = 10, i;
9   PetscScalar array[10];
10   Vec         x;
11 
12   PetscFunctionBeginUser;
13   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
14   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
15   PetscCheck(size == 1, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "This is a uniprocessor example only!");
16 
17   /* create vector */
18   for (i = 0; i < n; i++) array[i] = i;
19   n = n - 1;
20 
21   PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF, 1, n, array + 1, &x));
22   PetscCall(VecView(x, PETSC_VIEWER_STDOUT_SELF));
23   PetscCall(VecDestroy(&x));
24   PetscCall(PetscFinalize());
25   return 0;
26 }
27