Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28479 Discussions

Allowing for Deferred-length Character Variables in Namelists

__c
Beginner
291 Views

Dear All,

I was wondering if the issue of allocatable,
deferred-length character variables in namelist has
been finally solved in most recent versions of
ifort.

I think the issue was brought up back in 2016
(there might be some post on some Intel forum)
and Steve Lionel promptly replied and confessed
such request was lukewarmly received by the standard
committed and later dismissed.

It would be nice to have this feature
(character(len=:),allocatable allowed in namelist)
thoroughly supported by the compiler.

Enclosed at the bottom of this note you can find a
code snippet with its associated log.
The code would compile with the following
activated:

character(len=:) , allocatable :: AllIsStoredHere,TempStuff

but would error immediately.

This is the ifort version I have been running:

[cabbonda@descartes TestNamelist]$ ifort --version
ifort (IFORT) 18.0.0 20170811
Copyright (C) 1985-2017 Intel Corporation.  All rights reserved.

This is the log of the code when the declaration

character(len=:) , allocatable :: AllIsStoredHere,TempStuff

is turned off.

[cabbonda@descartes TestNamelist]$ TestNamelist.e

 ~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~
 Namelist iostat =   0
 ~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~

 &DEFINEDIRS
 ALLISSTOREDHERE = EccoRespiroAppena                                 ,
 TEMPSTUFF       = TuCheLeVanitaConoscestiDelMondo
 /

 ~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~
 Deferred-length Character  =  MiaMadreAveaUnaPoveraAncella
 ... and its length ...          28
 ~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~

I would appreciate if you could provide some insight into the matter.

Thanks in advance for your help,

-claudio 

And here you can find the code snippet:

program TestNamelist

      implicit none

      integer :: inml, iErr
      logical :: lErr
      character(len=50) :: AllIsStoredHere,TempStuff
      !character(len=:) , allocatable :: AllIsStoredHere,TempStuff
      character(len=:) , allocatable :: DoesThisWork
      namelist /DefineDirs/ AllIsStoredHere,TempStuff

      open(newunit=inml,&
           file='TestThisNamelist.nml',&
           status='unknown',&
           iostat=iErr)

      write(unit=*,fmt='(/,a)') ' ~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~'
      write(unit=*,fmt='(a,1x,i2)') ' Namelist iostat = ' , iErr
      write(unit=*,fmt='(a,/)') ' ~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~'

      ! Read nml
      read(unit=inml,nml=DefineDirs)
      ! Print nml to stout
      write(unit=*,nml=DefineDirs)

      !Let's automatically allocate this string
      DoesThisWork= 'MiaMadreAveaUnaPoveraAncella'
      write(unit=*,fmt='(/,a)') ' ~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~'
      write(unit=*,fmt='(a,1x,a)') ' Deferred-length Character  = ',DoesThisWork
      write(unit=*,fmt='(a,1x,i5)') ' ... and its length ...      ',len(DoesThisWork)
      write(unit=*,fmt='(a,/)') ' ~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~'

end program TestNamelist

!Content of the File TestThisNamelist.nml
!$DefineDirs
 !AllIsStoredHere = 'EccoRespiroAppena'
 !TempStuff = 'TuCheLeVanitaConoscestiDelMondo'
!$end

 

 

0 Kudos
1 Reply
DataScientist
Valued Contributor I
291 Views

Assuming that I have understood your question correctly: I am using Intel ifort 18.0.2.185 Build 20180210, and the allocatable characters seems to work inside namelists. All I needed to add was to allocate the two character variables prior to reading them from the namelist. Once read, one can trim and reallocate them properly to contain only the relevant characters (This trimming approach could be problematic for charatcer variables that contain trailing blanks, though). Here is the modified code:

program TestNamelist

      implicit none

      integer :: inml, iErr
      logical :: lErr
      !character(len=50) :: AllIsStoredHere,TempStuff
      character(len=:) , allocatable :: AllIsStoredHere,TempStuff
      character(len=:) , allocatable :: DoesThisWork
      namelist /DefineDirs/ AllIsStoredHere,TempStuff

      open(newunit=inml,&
           file='TestThisNamelist.nml',&
           status='unknown',&
           iostat=iErr)

      write(unit=*,fmt='(/,a)') ' ~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~'
      write(unit=*,fmt='(a,1x,i2)') ' Namelist iostat = ' , iErr
      write(unit=*,fmt='(a,/)') ' ~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~'

      ! allocate character variables:
      allocate( character(1023) :: AllIsStoredHere )
      allocate( character(1023) :: TempStuff )
      ! Read nml
      read(unit=inml,nml=DefineDirs)
      AllIsStoredHere = trim(adjustl(AllIsStoredHere))
      TempStuff = trim(adjustl(TempStuff))
      ! Print nml to stout
      write(unit=*,nml=DefineDirs)

      !Let's automatically allocate this string
      DoesThisWork= 'MiaMadreAveaUnaPoveraAncella'
      write(unit=*,fmt='(/,a)') ' ~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~'
      write(unit=*,fmt='(a,1x,a)') ' Deferred-length Character  = ',DoesThisWork
      write(unit=*,fmt='(a,1x,i5)') ' ... and its length ...      ',len(DoesThisWork)
      write(unit=*,fmt='(a,/)') ' ~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~'

end program TestNamelist

!Content of the File TestThisNamelist.nml
!$DefineDirs
 !AllIsStoredHere = 'EccoRespiroAppena'
 !TempStuff = 'TuCheLeVanitaConoscestiDelMondo'
!$end

and here is the output:

>a.out

 ~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~
 Namelist iostat =   0
 ~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~

 &DEFINEDIRS
 ALLISSTOREDHERE = EccoRespiroAppena,
 TEMPSTUFF       = TuCheLeVanitaConoscestiDelMondo
 /

 ~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~
 Deferred-length Character  =  MiaMadreAveaUnaPoveraAncella
 ... and its length ...          28
 ~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~~o^o~

 

0 Kudos
Reply