cape.filecntl.namelist: Fortran namelists module¶
This is a module built off of the cape.filecntl.FileCntl module
customized for manipulating Fortran namelists. Such files are split
into sections which are called “name lists.” Each name list has syntax
similar to the following.
&project project_rootname = "pyfun" case_title = "Test case" /
and this module is designed to recognize such sections. The main feature of this module is methods to set specific properties of a namelist file, for example the Mach number or CFL number.
This function provides a class Namelist that can both read and
set values in the namelist. The key functions are
The conversion from namelist text to Python is handled by
Namelist.ConvertToText(), and the reverse is handled by
Namelist.ConvertToVal(). Conversions cannot quite be performed
just by the Python functions print() and eval() because
delimiters are not used in the same fashion. Some of the conversions
are tabulated below.
Namelist
Python
val = "text"
val = "text"
val = 'text'
val = 'text'
val = 3
val = 3
val = 3.1
val = 3.1
val = .false.
val = False
val = .true.
val = True
val = .f.
val = False
val = .t.
val = True
val = 10.0 20.0
val = [10.0, 20.0]
val = 1, 100
val = [1, 100]
val(1) = 1.2
val = [1.2, 1.5]
val(2) = 1.5
val = _mach_
val = "_mach_"
In most cases, the Namelist will try to interpret invalid
values for any namelist entry as a string with missing quotes. The
reason for this is that users often create template namelist with
entries like _mach_ that can be safely replaced with appropriate
values using sed commands or something similar.
There is also a function Namelist.ReturnDict() to access the
entire namelist as a dict. Similarly,
Namelist.ApplyDict() can be used to apply multiple settings using
a dict as input.
See also:
- class cape.filecntl.namelist.Namelist(fname='fun3d.nml')¶
File control class for Fortran namelists
This class is derived from the
cape.filecntl.FileCntlclass, so all methods applicable to that class can also be used for instances of this class.- Call:
>>> nml = cape.Namelist() >>> nml = cape.Namelist(fname)
- Inputs:
- fname:
str Name of namelist file to read, defaults to
'fun3d.nml'
- fname:
- Outputs:
- nml:
Namelist Namelist file control instance
- nml.Sections:
dict[list[str]] Dictionary of sections containing contents of each namelist
- nml.SectionNames:
list[str] List of section names
- nml:
- Version:
2015-10-15
@ddalle: v0.1; started2015-10-20
@ddalle: v1.0
- AddSection(sec)¶
Add a section to the namelist interface
- Call:
>>> nml.AddSection(sec)
- Inputs:
- sec:
str Name of section
- sec:
- Versions:
2016-04-22
@ddalle: v1.0
- ApplyDict(opts)¶
Apply a whole dictionary of settings to the namelist
- Call:
>>> nml.ApplyDict(opts)
- Inputs:
- nml:
Namelist Namelist file control instance
- opts:
dict Dictionary of namelist options
- nml:
- Versions:
2015-10-16
@ddalle: v1.0
- ConvertToText(v)¶
Convert a value to text to write in the namelist file
- Call:
>>> val = nml.ConvertToText(v)
- Inputs:
- nml:
Namelist Namelist file control instance
- v: any
Evaluated value of the text
- nml:
- Outputs:
- val:
str Text of the value from file
- val:
- Versions:
2015-10-16
@ddalle: v1.0
- ConvertToVal(val)¶
Convert text to Python based on a series of rules
- Call:
>>> v = nml.ConvertToVal(val)
- Inputs:
- nml:
Namelist Namelist file control instance
- val:
str|unicode Text of the value from file
- nml:
- Outputs:
- v:
str|int|float|bool|list Evaluated value of the text
- v:
- Versions:
2015-10-16
@ddalle: v1.02016-01-29
@ddalle: v1.1; boolean shortcut .T.2022-07-11
@ddalle: v1.2; parse ‘12 * 3.7’
- Copy(fname)¶
Copy a file interface
- GetVar(sec, name, k=None)¶
Get value of a variable
- Call:
>>> val = nml.GetVar(sec, name) >>> val = nml.GetVar(sec, name, k)
- Inputs:
- nml:
Namelist Namelist file control instance
- sec:
str Name of section in which to set variable
- name:
str Name of variable as identified in ‘aero.csh’
- k:
int Namelist index
- nml:
- Outputs:
- val: any
Value to which variable is set in final script
- Versions:
2015-10-15
@ddalle: v1.02015-10-20
@ddalle: v1.1; add Fortran index
- ReturnDict()¶
Return a dictionary of options that mirrors the namelist
- Call:
>>> opts = nml.ReturnDict()
- Inputs:
- nml:
Namelist Namelist file control instance
- nml:
- Outputs:
- opts:
dict Dictionary of namelist options
- opts:
- Versions:
2015-10-16
@ddalle: v1.0
- SetVar(sec, name, val, k=None, **kw)¶
Set generic
fun3d.nmlvariable value- Call:
>>> nml.SetVar(sec, name, val) >>> nml.SetVar(sec, name, val, k)
- Inputs:
- nml:
Namelist Namelist file control instance
- sec:
str Name of section in which to set variable
- name:
str Name of variable as identified in ‘aero.csh’
- val: any
Value to which variable is set in final script
- k:
int Namelist index
- indent: {
4} |int>= 0 Number of spaces for indent
- tab: {
" " * indent} |str Specific indent string
- nml:
- Versions:
2014-06-10
@ddalle: v1.02015-10-20
@ddalle: v1.1; add Fortran index2019-06-04
@ddalle: v1.2; add indentation