Skip to content

Support PETSc 3.23+ - #408

Open
sabakhshi wants to merge 18 commits into
mainfrom
petsc325support
Open

Support PETSc 3.23+#408
sabakhshi wants to merge 18 commits into
mainfrom
petsc325support

Conversation

@sabakhshi

Copy link
Copy Markdown
Contributor

Purpose

PetSC 3.23 introduced major breaking changes to the Fortran interface breaking a lot of code in ADflow.
This PR introduces compatibility with the changes in PetSC 3.23+.
Confirmed also working to PetSC 3.25.4.
Note this PR breaks compatibility with PetSC versions below 3.23

Expected time until merged

1 week

Type of change

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (non-backwards-compatible fix or feature)
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no API changes)
  • Documentation update
  • Maintenance update
  • Other (please describe)

Testing

All regression and units tests pass.

Checklist

  • I have run ruff check and ruff format to make sure the Python code adheres to PEP-8 and is consistently formatted
  • [] I have formatted the Fortran code with fprettify or C/C++ code with clang-format as applicable
  • I have run unit and regression tests which pass locally with my changes
  • I have added new tests that prove my fix is effective or that my feature works
  • I have added necessary documentation

@sabakhshi
sabakhshi requested a review from a team as a code owner August 28, 2026 00:13
@sabakhshi
sabakhshi requested review from A-CGray, ArshSaja, anilyil and mpdutta and removed request for ArshSaja August 28, 2026 00:13
@sabakhshi sabakhshi changed the title Support PetSC 3.23+ Support PetSc 3.23+ Aug 28, 2026
@A-CGray A-CGray changed the title Support PetSc 3.23+ Support PETSc 3.23+ Aug 31, 2026
@A-CGray

A-CGray commented Aug 31, 2026

Copy link
Copy Markdown
Member

I'm getting this compiler error building with PETSc 3.25.4

../NKSolver/NKSolvers.F90:176:49:

 176 |                                 PETSC_DETERMINE, ctx, dRdwPseudo, ierr)
     |                                                 1
Error: Rank mismatch in argument ‘ctx’ at (1) (scalar and rank-1)
../NKSolver/NKSolvers.F90:205:66:

 205 |                 call KSPMonitorSet(NK_KSP, linearResidualMonitor, PETSC_NULL_FUNCTION, &
     |                                                                  1
Error: Invalid procedure argument at (1)
../NKSolver/NKSolvers.F90:1727:13:

1727 |         use NKSolver, only: destroyNKSolver, linearResidualMonitor
     |             1
Fatal Error: Cannot open module file ‘nksolver.mod’ for reading at (1): No such file or directory
compilation terminated.
make[3]: *** [rules:9: NKSolvers.o] Error 1
make[3]: *** Waiting for unfinished jobs....

       --- Compiled adjointDebug.F90 successfully ---


       --- Compiled fortranPC.F90 successfully ---

../adjoint/adjointAPI.F90:1202:49:

1202 |                                 PETSC_DETERMINE, matfreectx, dRdwT, ierr)
     |                                                 1
Error: Rank mismatch in argument ‘ctx’ at (1) (scalar and rank-1)
../adjoint/adjointAPI.F90:929:57:

 929 |             call KSPMonitorSet(adjointKSP, MyKSPMonitor, PETSC_NULL_FUNCTION, &
     |                                                         1
Error: Invalid procedure argument at (1)

sabakhshi and others added 12 commits August 31, 2026 17:30
`module utils` imported petscvec at module scope, so every file that
imports `utils` transitively depended on `petscvec.mod`. The module has
no default `private`, so it also re-exported all of petscvec to its two
unqualified importers. Only `deallocDerivativeValues` needs it, for the
`VecDestroy` calls at the end.

Shrinks `utils.mod` from 272K to 7.0K.

The regenerated Tapenade output drops the inherited import from
`utils_d`, `utils_b` and `utils_fast_b`, none of which reference PETSc
at all.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Same change as the previous commit. Here the module-scope `use petscvec`
also sat outside the `#ifndef USE_TAPENADE` guard, so it leaked into the
generated AD sources; the four procedures that touch PETSc are all
inside that guard.

No finclude is required, because the file declares no PETSc types of its
own and PETSC_COPY_VALUES / PETSC_DETERMINE arrive through petscvec's
re-export of petscis.

Shrinks `walldistance.mod` from 266K to 19K.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
`ADjointPETSc` does not declare PETSC_COMM_WORLD, KSP_NORM_NONE or
PETSC_DEFAULT_REAL. It only passed them on from petscksp. Two files
relied on that, which hides where the names come from and breaks as
soon as the import in `ADjointPETSc` is narrowed.

`module NKSolver` already imports petscksp at module scope, so those two
names are host-associated and the borrowed copies can simply go.
`initializePETSc` now takes PETSC_COMM_WORLD from petscsys, which is
also the variable it assigns to.

This follows the cleanup already started at preprocessingAPI.F90:1689
and adjointUtils.F90:733, where the same borrowed PETSc constants were
commented out beside a direct `use petscvec`.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@sabakhshi

Copy link
Copy Markdown
Contributor Author

What was wrong with the Tapenade? All test were passing locally for both Meghna and I

@A-CGray

A-CGray commented Sep 5, 2026

Copy link
Copy Markdown
Member

What was wrong with the Tapenade? All test were passing locally for both Meghna and I

The changes to the petsc imports changed the code generated by Tapenade, so the Tapenade CI check was failing, it passes now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants