xref: /petsc/src/vec/vec/tests/ex40.c (revision d71ae5a4db6382e7f06317b8d368875286fe9008)
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 {
8   PetscMPIInt size;
9   PetscInt    n = 10, i;
10   PetscScalar array[10];
11   Vec         x;
12 
13   PetscFunctionBeginUser;
14   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
15   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
16   PetscCheck(size == 1, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "This is a uniprocessor example only!");
17 
18   /* create vector */
19   for (i = 0; i < n; i++) array[i] = i;
20   n = n - 1;
21 
22   PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF, 1, n, array + 1, &x));
23   PetscCall(VecView(x, PETSC_VIEWER_STDOUT_SELF));
24   PetscCall(VecDestroy(&x));
25   PetscCall(PetscFinalize());
26   return 0;
27 }
28