Cursor rules for Fortran

By default some LLMs will produce Fortran code with default reals. To avoid this, I often instruct them to

Declare real variables as

real(kind=dp) with

dp a kind constant imported from module kind_mod, which I will provide

I tell them to put procedures in modules, and in my Fortran agents I tell them to iterate until the code compiles with the gfortran options

-O0 -fmax-errors=1 -Wall -Werror=unused-parameter -Werror=unused-variable -Werror=unused-function -Wno-maybe-uninitialized -Wno-surprising -fbounds-check -static -g

Common mistakes that LLM make are to

  1. Assume pi is a built-in constant
  2. Declare variables after executable code, without using block
  3. print or write in pure procedures or use random_number
  4. Think that random_number is a function rather than a subroutine
  5. Declare superfluous variables such as i and j.
  6. Declare variables more than once
  7. Not to explicitly declare as public module entities that need to be public. This is allowed by the standard, but I want code that compiles with gfortran -fmodule-private.
  8. Have two variables that differ only by case in the same scope. In other languages those variables are distinct.

You can instruct them not to do this.

3 Likes