Having trouble with basic Fortran-PETSc interoperability
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
Hi Michael, If you are trying to get all the values on the 0th processor, you can find that here: https://urldefense.us/v3/__https://petsc.org/release/faq/*how-do-i-collect-t... Randy
On Jan 2, 2026, at 11:42 AM, 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>
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>
On Fri, Jan 2, 2026 at 2:48 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?
No. You should just call VecGetArray(), to get back an F90 pointer to the values. This is much more convenient.
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?
Yes, that should work getting the array directly. Thanks, Matt
Thanks in advance for any help you can offer.
best, Michael
-- What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead. -- Norbert Wiener https://urldefense.us/v3/__https://www.cse.buffalo.edu/*knepley/__;fg!!G_uCf... <https://urldefense.us/v3/__http://www.cse.buffalo.edu/*knepley/__;fg!!G_uCfs... >
participants (4)
-
Barry Smith -
Matthew Knepley -
Michael Whitten -
Randall Mackie