Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions cmlm/utils/input_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
import tomlkit


def scalar_to_list(val):
"""Leave lists as is, convert scalars to 1 element lists."""
if not isinstance(val, (list, tuple)):
return [val]
else:
return val


def recursively_update_dict(base, new):
"""
Update items and subitems in one nested dict-like object based on another.
Expand Down Expand Up @@ -143,6 +151,9 @@ def __init__(
self.base = base
self.output = kwargs.get("output", None)
if self.output is not None:
# if output ends with .toml, its a file, otherwise its a directory
if not self.output.endswith(".toml"):
self.output = os.path.join(self.output, "config.toml")
self.output_dir = os.path.split(self.output)[0]
if self.base is None:
if len(self.output_dir) > 0 and not os.path.exists(self.output_dir):
Expand Down Expand Up @@ -202,7 +213,7 @@ def parse_file(cls, file_name=None, additional_args=None, **kwargs):
return cls(data, name=name, base=None, **kwargs)

@classmethod
def parse_args(cls, description=None, infile=None):
def parse_args(cls, description=None, infile=None, require_output=False):
"""
Parse command line arguments specifying file and arguments to create a TPP.

Expand All @@ -212,6 +223,8 @@ def parse_args(cls, description=None, infile=None):
Short description of program for which config is being loaded
infile: str, optional
Default TOML input file to use
require_output: bool, optional
if true, the `-o` command line argument is required. Default False.

Returns
-------
Expand All @@ -235,7 +248,8 @@ def parse_args(cls, description=None, infile=None):
"-o",
"--output",
default=None,
help="File in which to write used inputs/outputs",
required=require_output,
help="File (.toml) or directory in which to write used inputs/outputs",
)
parser.add_argument(
"-t",
Expand Down Expand Up @@ -346,7 +360,7 @@ def __setitem__(self, item_name, value):
self.data[prefix] = tomlkit.document()
self[prefix][suffix] = value

def get(self, item_name, default=None, doc=None):
def get(self, item_name, default=None, doc=None, choices=None):
"""
Retrieve a leaf or subtable form the TomlParmParse table.

Expand All @@ -363,6 +377,9 @@ def get(self, item_name, default=None, doc=None):
value to use if item_name is not found in table
doc: optional
string to add as a comment in the TOML file
choices: optional
list or tuple of allowable options for input parameter. If specified,
an error will be raised if the specified value is not in the list.

Returns
-------
Expand All @@ -381,9 +398,19 @@ def get(self, item_name, default=None, doc=None):
)
self[item_name] = retval

if choices is not None:
if retval not in choices:
raise ValueError(
f"In TomlParmParse object {self.name}:\n"
f" Invalid value specified for item <{item_name}> (doc: {doc})\n"
f" Choices are: {choices}"
)

if doc is not None and self.output_type == "doc":
if default is not None:
doc += f" | optional, default: {default}"
if choices is not None:
doc += f" | choices are: {choices}"
self[item_name].comment(doc)

return retval
Expand Down
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys

sys.path.insert(0, os.path.abspath("../run_scripts/ctable"))
sys.path.insert(0, os.path.abspath("../run_scripts/cantera"))

# -- Project information -----------------------------------------------------

Expand Down
52 changes: 50 additions & 2 deletions docs/run-scripts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,68 @@
Run Scripts
===========

These scripts all use the TomlParmParse utility to manage configuration/inputs. Sample
inputs with descriptions are included in ``.toml`` files with the same base file name
as the script. All scripts can be run using::

python script_name.py script_name.toml

Adding the ``-h`` flag will give the additional command line inputs that are associated
with the TomlParmParse utility::

positional arguments:
infile Input file to parse inputs from

options:
-h, --help show this help message and exit
-o OUTPUT, --output OUTPUT
File (.toml) or directory in which to write used inputs/outputs
-t OUTPUT_TYPE, --output_type OUTPUT_TYPE
Type of output file: *doc*: include comments documenting used inputs *clean*: no comments, only used inputs *original*: input file in original formatting
-a ARGS, --args ARGS Override arguments, as a toml string
-w, --no_overwrite Disallow overwriting entries once they have been used or set
-e, --error_unused Raise error if there are unused inputs
-l, --live_update Update output file continuously as code runs

Notably, specifying ``-o`` will result in a new toml file with the configuration that
was used, including documentation for each input by default, and ``-a`` allows inputs
that are usually specified in the input file to be specified on the command line.

----

cantera
-------

These python scripts use Cantera to generate 1D flame solutions that will then be
compiled into tabulated chemistry tables.

----

compute_premixed_flamelets.py
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. automodule::
compute_premixed_flamelets

----

ctable
------

These python scripts use the CMLM package to generate tabulated chemistry data.

----

create_dummy_table_nd.py
------------------------
~~~~~~~~~~~~~~~~~~~~~~~~

.. automodule::
create_dummy_table_nd

----

create_spray_table_nd.py
------------------------
~~~~~~~~~~~~~~~~~~~~~~~~

.. automodule::
create_spray_table_nd
Loading