From: Bernhard Reinhardt on
Hi!

I try to add namelist-functionality to some Fortran-code.

At first I only want to read in a yes/no switch. use_GII=true/false.

The program consists of several subroutines. As far as I see I have to
make the logical variable use_GII known to the subroutines. There's
already a source-file "RTsettings.f90" with contains a module with some
constants.

So I thought Id just add another module to this source-file:

MODULE RTNamelist

IMPLICIT NONE
LOGICAL :: use_GII
namelist /RTnmlst/ use_GII
use_GII = .true.
open(8,file="src/RTnamelist.txt", status='OLD')
read(unit=8,nml=RTnmlst)
close(8)

END MODULE RTNamelist

Unfortunately I get the following errors when compiling with g95:

In file ./RTSettings.f90:98

use_GII = .true.
1
Error: Unexpected assignment statement in MODULE at (1)
In file ./RTSettings.f90:99

open(8,file="src/RTnamelist.txt", status='OLD')
1
Error: Unexpected OPEN statement in MODULE at (1)
In file ./RTSettings.f90:100

read(unit=8,nml=RTnmlst)
1
Error: Unexpected READ statement in MODULE at (1)
In file ./RTSettings.f90:101

close(8)
1
Error: Unexpected CLOSE statement in MODULE at (1)


Can anyone tell me what I'm doing wrong or post a link to a HOWTO use
namelists in global context?


> g95 --version
G95 (GCC 4.0.3 (g95 0.91!) Feb 26 2008)

Regards

Bernhard
From: Alois Steindl on
Hello,
why do you think it would work in the way you tried?
A module doesn't contain executable statements; you would have to write
some subroutine, which is called from where you want.
Alois
--
Alois Steindl, Tel.: +43 (1) 58801 / 32558
Inst. for Mechanics and Mechatronics Fax.: +43 (1) 58801 / 32598
Vienna University of Technology, A-1040 Wiedner Hauptstr. 8-10
From: dpb on
Bernhard Reinhardt wrote:
....
> The program consists of several subroutines. As far as I see I have to
> make the logical variable use_GII known to the subroutines. There's
> already a source-file "RTsettings.f90" with contains a module with some
> constants.
>
> So I thought Id just add another module to this source-file:
>
> MODULE RTNamelist
>
> IMPLICIT NONE
> LOGICAL :: use_GII
> namelist /RTnmlst/ use_GII
> use_GII = .true.
> open(8,file="src/RTnamelist.txt", status='OLD')
> read(unit=8,nml=RTnmlst)
> close(8)
>
> END MODULE RTNamelist
>
> Unfortunately I get the following errors when compiling with g95:
>
> In file ./RTSettings.f90:98
>
> use_GII = .true.
> 1
> Error: Unexpected assignment statement in MODULE at (1)
> In file ./RTSettings.f90:99
>
....similar errors elided...
>
>
> Can anyone tell me what I'm doing wrong or post a link to a HOWTO use
> namelists in global context?
....

The errors are that you tried to include executable statements outside a
program unit (program, subroutine or function).

I believe the namelist identifier and variables can be host-associated
thru the module; the initialization, etc., will have to be in a program
unit that is either a unit in the module or elsewhere.

--
From: Richard Maine on
dpb <none(a)non.net> wrote:

> Bernhard Reinhardt wrote:

> > Can anyone tell me what I'm doing wrong or post a link to a HOWTO use
> > namelists in global context?
>
> The errors are that you tried to include executable statements outside a
> program unit (program, subroutine or function).

And notethat this has zero to do with namelist. The namlist statement
in the module is fine.

I'm curious what you expected this code to do. In particular, when do
you think that the executable statemenst would be executed? One does not
invoke a module at any identified time during program execution. One
USEs it to make its identifiers accessable. The USE statement does not
imply any execution or happen at any particular time during program
execution; it is a compile-time thing. If you want to run executable
code (such as your assignment statement and I/O statements), you have to
say when to execute it... which is to say, you have to call a
subroutine, reference a function, or do something equivalent.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
From: Jared Ahern on
Bernhard,

As others have noted, you need to put your executable statements
(everything after namelist /.../ ...) in a program or procedure. If
you can do it, I've found that some variation of the following to be a
good pattern for specifying preferences through a namelist file:

module setupmod
implicit none
type preflist
logical :: use_GII = .false.
integer :: length = 8
end type preflist
type(preflist), protected, save :: prefs=preflist()
namelist /allprefs/ prefs
contains
subroutine setupprefs(filename)
implicit none
character(*), intent(in) :: filename
open(5000,file=filename,action='read',status='old')
read(5000,nml=allprefs)
close(5000)
end subroutine setupprefs
end module setupmod
program test
use setupmod
implicit none
call setupprefs("prefs.nml")
if (prefs%use_GII) ...
end program test

This allows you to set default prefs, groups your preferences
together, protects them from changing, and allows you to initially set
only those required. For example, reading a file containing
"&allprefs prefs%use_GII = T /" would change only that pref from the
default, leaving prefs%length==8 . You can of course make this much
more elaborate.

Jared
 | 
Pages: 1
Prev: Renaming module procedures
Next: string stuff