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

Detecting presence of logical arguments in a namelist read operation

OP1
Neuer Beitragender III
574Aufrufe
Hi,

Let's assume I have a text file containing the following namelist variable

&TEST
LOGICAL_FLAG = T
/

Let's assume that LOGICAL_FLAG was set to .TRUE. prior to reading the file. After reading the file it's value will not be changed (since it's also .TRUE.) and actually if LOGICAL_FLAG were omitted from the file the result would be the same.

I would like to be able to know if LOGICAL_FLAG was present or not. With other variables, it is easy to assign a specific value to the namelist variables (say, X = -HUGE(1.0), for instance) to detect a change. But for logicals it doesn't work of course. Is there a way to assign a special value to LOGICAL_FLAG to detect whether it was modified by the read operation?

Thanks in advance for your help,

Olivier
0 Kudos
3 Antworten
mecej4
Geehrter Beitragender III
574Aufrufe
There is an ambiguity in your objective, which needs to be resolved before attempting to suggest solutions.

You say, "After reading the file its value will not be changed (since it's also .TRUE.)" In actuality, the value .TRUE. is written into the variable upon reading the namelist input, regardless of whether the variable was initialized earlier and regardless of what value it previously held.

Along these lines, if you enabled a hardware data breakpoint in a debugger, the breakpoint would be triggered upon writing .TRUE. into the variable, again regardless of whether the previous value was .FALSE. or .TRUE.

To distinguish between .TRUE., .FALSE. and undefined, some Fortran compilers initialize logical variables to a special value such as Z'8B8B8B8B' and introduce code to check for this value.
OP1
Neuer Beitragender III
574Aufrufe

Your statement does not correspond to what IVF does. The help file indicates that "It is not necessary to assign values to all of the objects declared in the corresponding NAMELIST group. If a namelist object does not appear in the input statement, its value (if any) is unchanged".

This being said, yes, I'd like to know if I can assign a value to this logical variable which corresponds neither to a .TRUE. or a .FALSE.

Is there such a thing as a NaN for a LOGICAL?

Olivier

Steven_L_Intel1
Mitarbeiter
574Aufrufe
There isn't a NaN, but you could take advantage of the compiler's extension that LOGICALs can be treated as integers in some contexts. So you could do something like this:

[fortran]logical l
namelist /nml/ l

l = .true. 
do ! Set bits 8-15 to '44' l = ior(Z'4400',iand(l,Z'FFFF00FF')) print *, "Before read, L = ", L read (*, NML,end=99) if (iand(l,Z'FF00') == Z'4400') then print *, "L was not read, value is still", L else print *, "L was read, value is now", L end if end do 99 end[/fortran]
Antworten