Fortran Lab Examples
Foundations
Hello world, the five intrinsic types with KIND, operator precedence, and the declaration discipline. These four files introduce every value and habit the later demos rely on.
| # | Description | Link |
|---|---|---|
| 1 | Hello, name — program unit, implicit none, trimmed output | View |
| 2 | Intrinsic types and the KIND parameter | View |
| 3 | Operator precedence and the integer-division traps | View |
| 4 | Declaration syntax and named constants | View |
Control Flow
Decisions (IF ladder and SELECT CASE), the three loop families, and the parallel-ready do concurrent reduction with Fortran 2018 locality.
| # | Description | Link |
|---|---|---|
| 5 | IF ladder and SELECT CASE decision patterns | View |
| 6 | Counted DO, named EXIT scan, DO WHILE convergence | View |
| 7 | DO CONCURRENT with REDUCE — race-free summation | View |
Programming
Functions and subroutines with INTENT, elemental procedures, array slicing and WHERE masks, deferred-length strings with internal files, derived types, and the module + USE pattern.
| # | Description | Link |
|---|---|---|
| 8 | Function vs subroutine, INTENT contracts | View |
| 9 | Elemental function: one body, array and scalar calls | View |
| 10 | Array constructors, slicing, strides, WHERE | View |
| 11 | Deferred-length strings and internal files | View |
| 12 | Derived types: definition, constructor, value copy | View |
| 13 | Module with private API and USE ONLY imports | View |
Files & I/O
Formatted output, robust record-based reading with iostat/iomsg, and NAMELIST configuration files that write and read themselves.
| # | Description | Link |
|---|---|---|
| 14 | FORMAT descriptors and robust file reading | View |
| 15 | NAMELIST: config files without a parser | View |
Parallel & HPC
The three big parallel models, each in one compilable file. OpenMP needs -fopenmp; coarrays run single-image out of the box; the MPI ring needs OpenMPI/MPICH.
| # | Description | Link |
|---|---|---|
| 16 | OpenMP fork-join with REDUCTION — pi integration | View |
| 17 | Coarrays: images and the CO_SUM collective | View |
| 18 | MPI ring: point-to-point send/recv around a circle | View |
Interoperability
Crossing the language boundary in both directions: a bind(c) library with its C caller, and the F2PY bridge whose Python driver calls the Fortran kernel every time step.
| # | Description | Link |
|---|---|---|
| 19 | iso_c_binding library export (Fortran) | View |
| 20 | C caller of the same library (C) | View |
| 21 | F2PY kernel — one Euler step (Fortran) | View |
| 22 | F2PY driver — Python orchestration | View |
gfortran -Wall -o demo demo/foundations/01_hello.f90. Demos 5–15 need only gfortran; 16 needs -fopenmp; 17 works with plain gfortran (single image) or -fcoarray=lib; 18 needs mpifort + mpiexec -n 4; the interop pair compile with the commands in their own comments. Larger multi-file study projects follow in Study Projects.