Skip to content

Commit

Permalink
Make it possible to have multiline statements in config
Browse files Browse the repository at this point in the history
  • Loading branch information
eivindjahren committed Dec 2, 2024
1 parent ef9b3b8 commit 2ef40b4
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 10 deletions.
1 change: 1 addition & 0 deletions docs/ert/reference/configuration/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ This chapter sets out everything you need to know to configure ERT.
.. toctree::
:maxdepth: 2

syntax
data_types
forward_model
observations
Expand Down
31 changes: 31 additions & 0 deletions docs/ert/reference/configuration/syntax.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
The ert configuration file format
=================================

The ert config file, workflows, workflow job configuration files and forward
model configuration files all share the same syntax. These are lines starting
with one keyword followed by arguments separated by whitespace. e.g.

::

QUEUE_SYSTEM LOCAL
NUM_REALIZATIONS 12
-- A comment
FORWARD_MODEL DELETE_FILE(<FILES>="file1 file-- file3")

FORWARD_MODEL TEMPLATE_RENDER( \
<INPUT_FILES>=parameters.json, \
<TEMPLATE_FILE>=<CONFIG_PATH>/resources/SPE1.DATA.jinja2, \
<OUTPUT_FILE>=SPE1.DATA \
)

So for example :code:`QUEUE_SYSTEM` is a keyword and :code:`LOCAL` is an
argument. A comment is started with :code:`--`. :code:`FORWARD_MODEL` is a
special keyword that starts an installed forward model step (e.g.
:code:`DELETE_FILE`) with named forward model arguments inside parenthesis,
(e.g. :code:`<FILES>` is set to :code:`file1 file-- file3`. Note that arguments
can be surrounded by quotes (:code:`"`) to make spaces and :code:`--` part of
the argument.

As can be seen, you can have multi-line statements by escaping the newline with
:code:`\\` (like with bash). NB: the newline has to follow directly after
:code:`\\` without any spaces, tabs or other trailing whitespace.
5 changes: 2 additions & 3 deletions src/ert/config/parsing/lark_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
from .types import Defines, FileContextToken, Instruction, MaybeWithContext

grammar = r"""
WHITESPACE: (" "|"\t")+
%ignore WHITESPACE
%import common.CNAME
%import common.SIGNED_NUMBER -> NUMBER
%import common.NEWLINE -> NEWLINE
WHITESPACE: (" "|"\t"|"\\"NEWLINE)+
%ignore WHITESPACE
_STRING_INNER: /.+?/
_STRING_ESC_INNER: _STRING_INNER /(?<!\\)(\\\\)*?/
Expand Down
6 changes: 5 additions & 1 deletion test-data/ert/flow_example/flow.ert
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ OBS_CONFIG obs.txt

GEN_KW FIELD_PROPERTIES field_properties_priors

FORWARD_MODEL TEMPLATE_RENDER(<INPUT_FILES>=parameters.json, <TEMPLATE_FILE>=<CONFIG_PATH>/resources/SPE1.DATA.jinja2, <OUTPUT_FILE>=SPE1.DATA)
FORWARD_MODEL TEMPLATE_RENDER( \
<INPUT_FILES>=parameters.json, \
<TEMPLATE_FILE>=<CONFIG_PATH>/resources/SPE1.DATA.jinja2, \
<OUTPUT_FILE>=SPE1.DATA \
)

FORWARD_MODEL FLOW
26 changes: 21 additions & 5 deletions test-data/ert/snake_oil/snake_oil.ert
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,24 @@ FORWARD_MODEL SNAKE_OIL_NPV
FORWARD_MODEL SNAKE_OIL_DIFF

RUN_TEMPLATE templates/seed_template.txt seed.txt
GEN_KW SNAKE_OIL_PARAM templates/snake_oil_template.txt snake_oil_params.txt parameters/snake_oil_parameters.txt

GEN_DATA SNAKE_OIL_OPR_DIFF INPUT_FORMAT:ASCII RESULT_FILE:snake_oil_opr_diff_%d.txt REPORT_STEPS:199
GEN_DATA SNAKE_OIL_WPR_DIFF INPUT_FORMAT:ASCII RESULT_FILE:snake_oil_wpr_diff_%d.txt REPORT_STEPS:199
GEN_DATA SNAKE_OIL_GPR_DIFF INPUT_FORMAT:ASCII RESULT_FILE:snake_oil_gpr_diff_%d.txt REPORT_STEPS:199
GEN_KW \
SNAKE_OIL_PARAM \
templates/snake_oil_template.txt \
snake_oil_params.txt \
parameters/snake_oil_parameters.txt

GEN_DATA \
SNAKE_OIL_OPR_DIFF \
INPUT_FORMAT:ASCII \
RESULT_FILE:snake_oil_opr_diff_%d.txt \
REPORT_STEPS:199
GEN_DATA \
SNAKE_OIL_WPR_DIFF \
INPUT_FORMAT:ASCII \
RESULT_FILE:snake_oil_wpr_diff_%d.txt \
REPORT_STEPS:199
GEN_DATA \
SNAKE_OIL_GPR_DIFF \
INPUT_FORMAT:ASCII \
RESULT_FILE:snake_oil_gpr_diff_%d.txt \
REPORT_STEPS:199
24 changes: 23 additions & 1 deletion tests/ert/unit_tests/config/parsing/test_lark_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ def test_that_realisation_is_a_alias_of_realization():
assert config["NUM_REALIZATIONS"] == 1


@pytest.mark.usefixtures("use_tmpdir")
def test_that_new_line_can_be_escaped():
with open("config.ert", mode="w", encoding="utf-8") as fh:
fh.write(
dedent(
"""
NUM_REALIZATIONS \
1
"""
)
)

config = parse("config.ert", schema=init_user_config_schema())
assert config["NUM_REALIZATIONS"] == 1


@pytest.mark.filterwarnings(
"ignore:.*Using DEFINE with substitution strings that are not of the form '<KEY>'.*:ert.config.ConfigWarning"
)
Expand All @@ -79,7 +95,13 @@ def test_that_redefines_are_applied_correctly_as_forward_model_args():
NUM_REALIZATIONS 1
DEFINE <A> 2
DEFINE <B> 5
FORWARD_MODEL MAKE_SYMLINK(<U>=<A>, <F>=<B>, <E>=<O>, R=Hello, <R>=R)
FORWARD_MODEL MAKE_SYMLINK( \\
<U>=<A>, \\
<F>=<B>, \\
<E>=<O>, \\
R=Hello, \\
<R>=R \\
)
DEFINE <B> 10
DEFINE B <A>
DEFINE D <A>
Expand Down

0 comments on commit 2ef40b4

Please sign in to comment.