PROGRAM ReadConfig
IMPLICIT NONE
INTEGER, PARAMETER :: MAX_INNER_SECTIONS = 100 ! Adjust as needed
! Declare variables for outer and inner sections
REAL :: NCONPTS, NXMESH, DNORMK, DNORMC, EXPFACTR, BEGSM, SMPARAM
! Declare an array of inner section data
REAL, ALLOCATABLE :: INNER_NCONPTS(:), INNER_NXMESH(:), INNER_DNORMK(:)
REAL, ALLOCATABLE :: INNER_DNORMC(:), INNER_EXPFACTR(:), INNER_BEGSM(:)
REAL, ALLOCATABLE :: INNER_SMPARAM(:)
! Number of inner sections
INTEGER :: NUM_INNER_SECTIONS
! Namelist declarations
NAMELIST /OUTER/ NCONPTS, NXMESH, DNORMK, DNORMC, EXPFACTR, BEGSM, SMPARAM
NAMELIST /INNER/ INNER_NCONPTS, INNER_NXMESH, INNER_DNORMK, &
INNER_DNORMC, INNER_EXPFACTR, INNER_BEGSM, INNER_SMPARAM
! File and I/O variables
INTEGER :: input_unit, ios, i
! Allocate arrays with maximum possible size
ALLOCATE(INNER_NCONPTS(MAX_INNER_SECTIONS), &
INNER_NXMESH(MAX_INNER_SECTIONS), &
INNER_DNORMK(MAX_INNER_SECTIONS), &
INNER_DNORMC(MAX_INNER_SECTIONS), &
INNER_EXPFACTR(MAX_INNER_SECTIONS), &
INNER_BEGSM(MAX_INNER_SECTIONS), &
INNER_SMPARAM(MAX_INNER_SECTIONS))
! Open input file
input_unit = 10
OPEN(UNIT=input_unit, FILE='config.nml', STATUS='OLD', ACTION='READ', IOSTAT=ios)
IF (ios /= 0) THEN
PRINT *, 'Error opening input file'
STOP
END IF
! Read outer section
READ(input_unit, NML=OUTER, IOSTAT=ios)
IF (ios /= 0) THEN
PRINT *, 'Error reading OUTER section'
STOP
END IF
! Read inner sections
NUM_INNER_SECTIONS = 0
DO
! Reset inner section variables before reading
INNER_NCONPTS = 0.0
INNER_NXMESH = 0.0
INNER_DNORMK = 0.0
INNER_DNORMC = 0.0
INNER_EXPFACTR = 0.0
INNER_BEGSM = 0.0
INNER_SMPARAM = 0.0
! Try to read next inner section
READ(input_unit, NML=INNER, IOSTAT=ios)
! Check for end of file or read error
IF (ios < 0) THEN ! End of file
EXIT
ELSE IF (ios > 0) THEN ! Read error
PRINT *, 'Error reading INNER section'
STOP
END IF
! Increment inner section count
NUM_INNER_SECTIONS = NUM_INNER_SECTIONS + 1
! Optional: Break if maximum sections reached
IF (NUM_INNER_SECTIONS >= MAX_INNER_SECTIONS) THEN
PRINT *, 'Maximum number of inner sections reached'
EXIT
END IF
END DO
! Close file
CLOSE(input_unit)
! Print out the read data
PRINT *, 'OUTER Section:'
PRINT *, 'NCONPTS:', NCONPTS
PRINT *, 'NXMESH:', NXMESH
PRINT *, 'DNORMK:', DNORMK
PRINT *, 'Number of INNER Sections:', NUM_INNER_SECTIONS
DO i = 1, NUM_INNER_SECTIONS
PRINT *, 'INNER Section', i
PRINT *, ' NCONPTS:', INNER_NCONPTS(i)
PRINT *, ' NXMESH:', INNER_NXMESH(i)
PRINT *, ' DNORMK:', INNER_DNORMK(i)
END DO
! Deallocate arrays
DEALLOCATE(INNER_NCONPTS, INNER_NXMESH, INNER_DNORMK, &
INNER_DNORMC, INNER_EXPFACTR, INNER_BEGSM, INNER_SMPARAM)
END PROGRAM ReadConfig