VecGetValues() uses 0 based indexing in both Fortran and C. You don't want to use VecGetValues() and VecSetValues() usually since they result in two copies of the arrays and copying entire arrays back and forth.
You can avoid copying between PETSc vectors and your arrays by using VecGetArray(), VecGetArrayWrite(), and VecGetArrayRead(). You can also use VecCreateMPIWithArray() to create a PETSc vector using your array; for example for input to the right hand side of a KSP. These arrays start their indexing with the Fortran default of 1. Barry > On Jan 2, 2026, at 2:42 PM, Michael Whitten via petsc-users > <[email protected]> wrote: > > Hi PETSc mailing list users, > > I have managed to install PETSc and run some PETSc examples and little test > codes of my own in Fortran. I am now trying to make PETSc work with my > existing Fortran code. I have tried to build little test examples of the > functionality that I can then incorporate into my larger code base. However, > I am having trouble just passing vectors back and forth between PETSc and > Fortran. > > I have attached a minimum semi-working example that can be compiled with the > standard 'Makefile.user'. It throws an error when I try to copy the PETSc > vector back to a Fortran vector using VecGetValues(). I get that it can only > access values of the array on the local process but how do I fix this? Is > this even the right approach? > > In the final implementation I want to be able to assemble my matrix and > vector, convert them to PETSc data structures, use PETSc to solve, and then > convert the solution vector back to Fortran and return. I want to be able to > do this with both the linear and nonlinear solvers. It seems like this is > what PETSc is, in part, built to do. Is this a reasonable expectation to > achieve? Is this a reasonable use case for PETSc? > > Thanks in advance for any help you can offer. > > best, > Michael > <test.F90>
