Differentiate Fortran90 code that implements a Boundary element method

I have a fortran90 BEM code that I need to calculate the adjoints of. I am very new to this ecosystem but saw some advice on using Enzyme to do so. Has anyone done similar for the BEM code before?
Any help/pointers is appreciated.

Automatic differentiation can only be used if the code is pure Julia. Also even in this case some precautions must be taken to ensure that the code is differentiable.

If the F90 code is not too complex you could think of porting it to Julia.

If the variables are not too many you can rely on Numerical Differentiation packages like GitHub - JuliaDiff/FiniteDifferences.jl: High accuracy derivatives, estimated via numerical finite differences (formerly FDM.jl).


By virtue of working on the LLVM level, Enzyme indeed can differentiate fortran code (given you use a relevant fortran compiler). The setup will vary slightly depending on whether you use classic LLVM flang, the new MLIR flang, or something else. Regardless you will essentially follow the same workflow as described for C++ and add an -Xclang -load -Xclang /path/to/ClangEnzyme-XX.so to your compile args if clang-like plugins are supported, or a opt /path/to/LLVMEnzyme-XX.so -enzyme inp.ll -o out.ll if you need to apply at the LLVM level itself.

However, if the code is pure Fortran and not julia-related, perhaps this would be better to discuss on the Enzyme mailing list (https://groups.google.com/g/enzyme-dev), or the Enzyme subcategory LLVM discourse (Enzyme - LLVM Discussion Forums).

If you want to AD through a fortran library, and call that from Julia (with Enzyme for AD), you’ll need to ensure the library you’re loading has embedded bitcode, or similar.