Silverfrost Forums

Welcome to our forums

Namelist and user defined types

11 Nov 2024 5:29 #31673

Has anyone had an issue with placing a TYPE in a namelist? I can write it out, and all the data are there, but I get an error when I try to read it.

I'll gen up an example if no one has ever had this problem.

11 Nov 2024 12:26 #31674

Bill,

This works for me with the 32 bit compiler:

module my_mod
use ISO_FORTRAN_ENV
implicit none
type :: something
      real :: a
      real :: b
      integer :: i
end type something

type(something) :: thing1
NAMELIST /group/ thing1 

contains
  integer function run()
  print*, Compiler_version()
  thing1   = something(1.0,2.0,999)
  print*, 'Thing1 before file write'
  print*, thing1%a, thing1%b, thing1%i
  ! Write thing1 to scratch
  open(unit=10,status='scratch')
  rewind 10
  write(10,group) 
  ! Change thing1
  thing1   = something(10.0,20.0,-999)
  print*, 'New Thing1'
  print*, thing1%a, thing1%b, thing1%i
  ! Recover original value from scratch
  rewind 10
  read(10,group)
  print*, 'Old thing1 read from file via namelist'
  print*, thing1%a, thing1%b, thing1%i
  rewind 10
  close(10)
  run = 1
  end function run
end module my_mod

program main
use my_mod
i = run()
end program main


 FTN95 v9.03.0
 Thing1 before file write
      1.00000         2.00000             999
 New Thing1
      10.0000         20.0000            -999
 Old thing1 read from file via namelist
      1.00000         2.00000             999

Press RETURN to close window...

There does appear to be a problem with the 64 bit compiler:

FreeFormat1.F95(14) : error 1205 - amd_fill_namelist_block is not available in FTN95/64

Note I am a little behind the times using FTN95 v9.03.0.

Also see https://forums.silverfrost.com/Forum/Topic/3893&highlight=namelist

11 Nov 2024 1:36 #31675

Thank you for the feedback. I have noted this omission.

11 Nov 2024 1:39 #31676

Ken, thanks for this.

I was not having trouble until I introduced a TYPE that also contained a character variable. Looking at the file, it appears that the numeric variables are adjacent to the character, with no intervening space or new_line. For all my character variables (arrays), there is a space between elements. Apparently, not so for TYPE.

I'll try moving the character variable to the 'middle' of the numeric types and see what happens in the actual file.

11 Nov 2024 2:12 #31677

The TYPE is defined as:

	type ::rules
	sequence
	integer::	from_acct
	character*16::	trans_name
	integer::	to_acct
	end type

The variable is declared as:

type (rules):: pledges(20)

In the created file, PLEDGES, after initialization and writing the file, the file looks like this:

PLEDGES =         4025'Invoice         '           0        4031
'Invoice         '           0        4082'Invoice         '
           0        4025'Credit Memo     '           0
        4031'Credit Memo     '           0        4082
'Credit Memo     '           0           0'                '
           0           0'                '           0
           0'                '           0           0
'                '           0           0'                '
           0           0'                '           0
           0'                '           0           0
'                '           0           0'                '
           0           0'                '           0
           0'                '           0           0
'                '           0           0'                '
           0           0'                '           0

There is no intervening space between the numeric and character elements of RULES. This yields the run-time error of:

*** Error 204, Invalid identifier in a namelist name group

Thinking there needs to be a space between the numeric value and the first quote for the character variable, I manually placed them.

PLEDGES =         4025 'Invoice         '           0        4031
'Invoice         '           0        4082 'Invoice         '
           0        4025 'Credit Memo     '           0
        4031 'Credit Memo     '           0        4082
'Credit Memo     '           0           0 '                '
           0           0 '                '           0
           0 '                '           0           0
'                '           0           0 '                '
           0           0 '                '           0
           0 '                '           0           0
'                '           0           0 '                '
           0           0 '                '           0
           0 '                '           0           0
'                '           0           0 '                '
           0           0 '                '           0

Now, the file works to READ.

Moving the variables in the TYPE simply moves the data in the file, but the space that must be between the first quote on the character data and other data is missing. If I move the character variable to the end of the type, I'll get (shorter display):

PLEDGES =         4025           0'Invoice         '        4031
           0'Invoice         '        4082           0
'Invoice         '        4025           0'Credit Memo     '
        4031           0'Credit Memo     '        4082
           0'Credit Memo     '           0           0
11 Nov 2024 11:22 #31679

Bill, This is a very interesting problem.

Of the three compilers I have on my lap top, only one runs and gives sensible output for the following code.

program test
use ISO_FORTRAN_ENV
type pets 
  character(20) :: animal
  character(20) :: name
  integer       :: age
  character(30) :: breed
end type pets

type(pets) :: mypets(2)

NAMELIST /PETLIST/ mypets

print*, COMPILER_VERSION()
mypets(1)%animal = 'dog'
mypets(1)%name   = 'Casper'
mypets(1)%age    = 4
mypets(1)%breed  = 'Jack Russell Terrier'

mypets(2)%animal = 'dog'
mypets(2)%name   = 'Patch'
mypets(2)%age    = 12
mypets(2)%breed  = 'Parson Russell Terrier'

print*, 'Before write to NAMELIST'
print*, mypets(1)
print*, mypets(2)

open(unit=10,file='list.txt',status='unknown')
write(10,PETLIST)
close(unit=10,status='keep')

open(unit=10,file='list.txt',status='unknown')
read(10,PETLIST)  
print*, 'After read from NAMELIST'
print*, mypets(1)
print*, mypets(2)

end program test


 GCC version 11.3.0
 Before write to NAMELIST
 dog                 Casper                         4 Jack Russell Terrier
 dog                 Patch                         12 Parson Russell Terrier
 After read from NAMELIST
 dog                 Casper                         4 Jack Russell Terrier
 dog                 Patch                         12 Parson Russell Terrier

Press RETURN to close window...
12 Nov 2024 3:13 #31681

The missing code in FTN95 for x64 and NAMELIST has now been added for the next release. There was also an /64 internal compiler error for NAMELISTs and user TYPEs containing character variables.

There remains a fault in the external IO library (when reading NAMELISTs with a user TYPE and CHARACTER members). This causes the data to be read from the wrong point leading to a false runtime error 'Invalid character in field'.

This remaining fault is non-trivial and may take a while to fix.

13 Nov 2024 3:43 #31683

In the interim, I have gone with a different (more pedestrian) solution, which will work just fine.

That said, my ultimate goal would be to use NAMELIST. And I am in no hurry.

13 Nov 2024 8:50 #31687

The remaining fault (when reading a NAMELIST with CHARACTER variables in a user TYPE) has now been fixed for the next release of the DLLs.

Please login to reply.