Fortran

From G1Joshi/Agent-Skills · View interactive version

Fortran is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert modern Fortran (Fortran 90/2008/2018) assistance covering array operations, OpenMP, and numerical libraries. Use when writing scientific simulations, physics calculations, or high-performance linear algebra.

How to use this skill

Skill file

Fortran

Modern Fortran is a high-performance compiled language engineered for numerical analysis, computational fluid dynamics, climate modeling, and large-scale parallel scientific computing (MPI/OpenMP).

When to Use

Quick Start

program main
    implicit none
    integer, parameter :: dp = kind(1.0d0)
    real(dp) :: x = 2.0_dp, y = 3.0_dp, z

    z = x ** y
    print *, "Result of 2^3 is:", z
end program main

Core Concepts

Array Slicing & Pure Mathematical Syntax

Fortran treats multi-dimensional arrays as first-class primitives with native vector slicing:

program matrix_ops
  implicit none
  real, dimension(3, 3) :: A, B, C

  ! Initialize array
  A = 2.0
  B = 3.0

  ! Element-wise matrix multiplication in single line
  C = A * B + 1.0

  print *, "Result Matrix (1,1):", C(1, 1)
end program matrix_ops

Modern Fortran Modules (Fortran 2018/2023)

Encapsulates data, interfaces, and subroutines cleanly:

module physics_engine
  implicit none
  private
  public :: calculate_kinetic_energy

contains

  pure function calculate_kinetic_energy(mass, velocity) result(energy)
    real, intent(in) :: mass, velocity
    real :: energy
    energy = 0.5 * mass * (velocity ** 2)
  end function calculate_kinetic_energy

end module physics_engine

Coarray Parallelism for High-Performance Computing (HPC)

Built-in SPMD (Single Program, Multiple Data) parallel syntax without external MPI library calls:

program coarray_demo
  implicit none
  integer :: val[*]

  val = this_image() ! Each parallel CPU core assigns its own image ID
  sync all

  if (this_image() == 1) then
    print *, "Image 1 read from Image 2:", val[2]
  end if
end program coarray_demo

Common Patterns

Vectorized Array Operations and Slicing

Problem: Writing nested DO loops for large matrix calculations is slow and error-prone.

Solution:

Use Fortran's native array syntax for automatic vectorization:

program matrix_math
    implicit none
    real, dimension(100, 100) :: A, B, C

    call random_number(A)
    call random_number(B)

    ! Array-level element-wise operation (vectorized)
    C = A * B + sin(A)

    print *, "Sum of C matrix:", sum(C)
end program matrix_math

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :------------------------------------------------- | :------------------------------------------------------------------------ | :--------------------------------------------------------------------------------------- |

| Segmentation fault in array operation | Out of bounds array indexing or stack exhaustion from large local arrays. | Allocate large arrays on the heap using allocatable and compile with -fcheck=bounds. |

| Type mismatch in argument '...' | Passing default real/integer to function expecting double precision. | Specify explicit precision kind (e.g. 1.0_dp) on literals. |

| implicit none error: symbol has no IMPLICIT type | Undeclared variable used in program unit. | Declare all variables explicitly with type and kind parameters. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.