To convert your input into Fortran namelist format, we need to define multiple namelist groups, one for each section (OUTER, INNER #1, etc.). Since Fortran namelists don't support repeated group names (like multiple INNER sections), a common workaround is to use unique namelist group names such as INNER_1, INNER_2, and INNER_3.
Here's how you can structure the data in namelist format:
In your Fortran program, declare variables grouped under these namelist blocks:
fortran
PROGRAM ReadNamelist
IMPLICIT NONE
REAL :: NCONPTS, NXMESH, DNORMK, DNORMC, EXPFACTR, BEGSM, SMPARAM
NAMELIST /OUTER/ NCONPTS, NXMESH, DNORMK, DNORMC, EXPFACTR, BEGSM, SMPARAM
NAMELIST /INNER_1/ NCONPTS, NXMESH, DNORMK, DNORMC, EXPFACTR, BEGSM, SMPARAM
NAMELIST /INNER_2/ NCONPTS, NXMESH, DNORMK, DNORMC, EXPFACTR, BEGSM, SMPARAM
NAMELIST /INNER_3/ NCONPTS, NXMESH, DNORMK, DNORMC, EXPFACTR, BEGSM, SMPARAM
! Initialize or read values
CALL ReadData()
CONTAINS
SUBROUTINE ReadData()
INTEGER :: unit=10, ios
OPEN(UNIT=unit, FILE='input.nml', ACTION='READ', IOSTAT=ios)
IF (ios /= 0) THEN
PRINT *, "Error opening file"
STOP
END IF
READ(unit, NML=OUTER, IOSTAT=ios)
IF (ios /= 0) PRINT *, "Error reading OUTER"
READ(unit, NML=INNER_1, IOSTAT=ios)
IF (ios /= 0) PRINT *, "Error reading INNER_1"
READ(unit, NML=INNER_2, IOSTAT=ios)
IF (ios /= 0) PRINT *, "Error reading INNER_2"
READ(unit, NML=INNER_3, IOSTAT=ios)
IF (ios /= 0) PRINT *, "Error reading INNER_3"
CLOSE(unit)
END SUBROUTINE ReadData
END PROGRAM ReadNamelist
📝 Notes
All values are written as floating-point numbers (e.g., 0. instead of 0) to match the original.
You could also use arrays if you had multiple inner regions with similar structures — but since each inner block appears structurally identical and countable here, separate namelists are appropriate.
If you expect variable numbers of inner sections, consider using derived types + arrays and custom parsing logic.
Let me know if you'd like to see an example that reads N number of inner sections dynamically!
User
number of inner section may change
Assistant
If the number of inner sections may change, and you don't know it at compile time, using a namelist becomes more complex because Fortran namelists:
Do not support variable-length arrays directly in namelists unless they're ALLOCATABLE (for simple 1D arrays).
Cannot have repeated namelist group names — each /NAMELIST/ group must be unique.
So to handle a variable number of inner sections, you need a workaround. A good solution is to:
✅ Approach: Use a Derived Type + Array
You can define a derived type that represents one "inner section", then use an allocatable array of that type. The key is to not include this array in a namelist directly, but instead read the namelist manually line-by-line and parse the data dynamically.
This gives you full control over how many inner sections are read.
🧱 Step-by-step Plan
Define a derived type for one inner section.
Read the namelist file line-by-line.
Detect each section (OUTER, INNER #N) by checking for the header lines.
Parse the values into appropriate variables or array elements.
Then, in code, dynamically generate INNER_1, INNER_2, etc., using preprocessor macros or runtime logic — though this is less flexible than manual parsing.
Let me know if you'd prefer the version using Fortran namelist syntax and how many inner sections you expect maximum!