I have kinda spooky condition with Enzyme and maybe you can give insights to locate the error.
Background: I'm using Enzyme to calculate Jacobian in my CFD code. I need to set the neighbor cell value based on face type (interior, Neumann, or Dirichlet). If the face is interior, the neighbor cell value is the actual neighboring cell; if Neumann, it's the center cell value. These are working well. However, when it comes to Dirichlet, where I get the cell value from an array, I encounter an issue. After running the Jacobian, my array holding boundary values is changed when using Enzyme. It's worth noting that when I use PETSc's Jacobian via graph coloring, there is no problem, so looks like my residual calculation function is not mutating boundary array.
The most interesting part (for me) is that in my CFD code, I separately solve momentum and pressure equations. And this issue only arises in the pressure equation. In momentum equation there is no problem. The problematic code snippet is:
if (user->PFaceType[face][0] == DRICHLET) { pN = user->PBoundaryVal[face][0]; }
If I include this part, my code fails with Enzyme. However, if I set pN = 0 hardcoded, there is no problem.
I am using latest Enzyme version with LLVM 16. Actually I wanted to include my code but it has many LOCs but if you tell me where to look specifically I can provide you something.