Namelists

Contents
Define a namelist
Read one
Write one
Config-file pattern

Define a namelist

A namelist groups several variables under one name so they can be read and written together. Declare the variables first, then list them in a namelist statement. The group name sits between slashes, as in /cfg/.

program cfg_demo implicit none integer :: nx = 100 real :: tol = 1.0e-6 character(len=32) :: title = "run1" namelist /cfg/ nx, tol, title end program cfg_demo

Read one

Open the text file, read it with the nml selector, then close the file. Every name found in the file is stored into the matching variable. Names that are missing simply keep their current values.

program cfg_read implicit none integer :: nx = 100 real :: tol = 1.0e-6 character(len=32) :: title = "run1" namelist /cfg/ nx, tol, title integer :: u open(newunit=u, file="cfg.nml", status="old", action="read") read(u, nml=cfg) close(u) end program cfg_read

Write one

Write the whole group with the nml selector to print every name and value. This is handy for logging the exact settings that a run has used.

program cfg_write implicit none integer :: nx = 100 real :: tol = 1.0e-6 character(len=32) :: title = "run1" namelist /cfg/ nx, tol, title write(*, nml=cfg) end program cfg_write

Config-file pattern

A common pattern is to set safe defaults in the code and let a short config file override them. Ship a default cfg.nml file with the program and read it at startup. Unknown names stop the program with an error, so keep the file and the namelist in sync.

&cfg nx = 200 tol = 1.0e-8 title = "big" /

Next: IEEE arithmetic

Article author: Arthur Isaev

Related articles
Fortran
Fortran course: zero to hero in 50 lessons
What is Fortran and where it runs
Install gfortran
Your first program
Program structure
Types: integer real complex logical character
Kind parameters
Operators
Conditions
DO loops
Arrays
Array sections
Character strings
Formatted IO
Files
Subroutines and functions
Modules
USE with ONLY
INTENT
OPTIONAL and PRESENT
Explicit interfaces
Derived types
Type-bound procedures
Object orientation
Operator overloading
Finalization
Allocatable arrays
Pointers in Fortran
DO CONCURRENT
Coarrays
OpenMP basics
Namelists
IEEE arithmetic
Intrinsic matrix math
Random numbers
String tools
Command-line arguments
Environment variables
error stop
Testing
Build with gfortran and make
Debugging
C interoperability
From fixed form to free form
Numeric models
Performance
Modern Fortran tour: 2008, 2018, 2023
Capstone: stats tool
Capstone: 1D heat equation
Style guide
Hero roadmap: the whole course on one page
Install gfortran on Windows 11
Install gfortran on Ubuntu
Install gfortran on Rocky Linux
Install gfortran on macOS
Install gfortran on FreeBSD
Fortran in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: