program main

#include <petsc/finclude/petscksp.h>
    use petscvec
    implicit none

    PetscErrorCode ierr
    PetscInt i
    Vec v1p

    integer :: n
    real(8), allocatable :: v1(:), v2(:)

    n = 10

    allocate(v1(n),v2(n),source=0.0d0)

    v1 = 1.0d0

    PetscCallA(PetscInitialize(ierr))

    !Create PETSc vector
    PetscCallA(VecCreate(PETSC_COMM_WORLD,v1p,ierr))
    PetscCallA(VecSetSizes(v1p,PETSC_DECIDE,n,ierr))
    PetscCallA(VecSetFromOptions(v1p,ierr))

    !Pass Fortran Vector to PETSc
    PetscCallA(VecSetValues(v1p,n,[(i,i=0,n-1)],v1,INSERT_VALUES,ierr))

    !Send PETSc vector to each processor
    PetscCallA(VecAssemblyBegin(v1p,ierr))
    PetscCallA(VecAssemblyEnd(v1p,ierr))

    !Print PETSc vector
    PetscCallA(VecView(v1p,PETSC_VIEWER_STDOUT_WORLD,ierr))

    !***ERROR HERE***
    !Pass PETSc vector to Fortran
    PetscCallA(VecGetValues(v1p,n,[(i,i=1,n)],v2,ierr))

    !Print Fortran vector
    print *, "v2: ", v2

    !PETSc clean up
    PetscCallA(VecDestroy(v1p,ierr))
    PetscCallA(PetscFinalize(ierr))


end program main
