Skip to content

Commit

Permalink
Use one experiment per optimization
Browse files Browse the repository at this point in the history
(Everest) Use one experiment per optimization
  • Loading branch information
Yngve S. Kristiansen committed Sep 27, 2024
1 parent 321767c commit 904aa5b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 15 deletions.
14 changes: 5 additions & 9 deletions src/ert/simulator/batch_simulator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Union
from uuid import UUID

import numpy as np

Expand All @@ -9,7 +10,7 @@
from .batch_simulator_context import BatchContext

if TYPE_CHECKING:
from ert.storage import Ensemble, Storage
from ert.storage import Ensemble, Experiment


class BatchSimulator:
Expand Down Expand Up @@ -91,6 +92,7 @@ def callback(*args, **kwargs):
self.control_keys = set(controls.keys())
self.result_keys = set(results)
self.callback = callback
self._experiment_id: Optional[UUID] = None

ens_config = self.ert_config.ensemble_config
for control_name, variables in controls.items():
Expand Down Expand Up @@ -181,7 +183,7 @@ def start(
self,
case_name: str,
case_data: List[Tuple[int, Dict[str, Dict[str, Any]]]],
storage: Storage,
experiment: Experiment,
) -> BatchContext:
"""Start batch simulation, return a simulation context
Expand Down Expand Up @@ -240,13 +242,7 @@ def start(
time, so when you have called the 'start' method you need to let that
batch complete before you start a new batch.
"""
experiment = storage.create_experiment(
parameters=self.ert_config.ensemble_config.parameter_configuration,
responses=self.ert_config.ensemble_config.response_configuration,
name=f"experiment_{case_name}",
)
ensemble = storage.create_ensemble(
experiment.id,
ensemble = experiment.create_ensemble(
name=case_name,
ensemble_size=self.ert_config.model_config.num_realizations,
)
Expand Down
49 changes: 43 additions & 6 deletions tests/ert/unit_tests/simulator/test_batch_sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,14 @@ def batch_simulator(batch_sim_example):
def test_that_starting_with_invalid_key_raises_key_error(
batch_simulator, _input, match, storage
):
experiment = storage.create_experiment(
name="EnOptCase",
parameters=batch_simulator.ert_config.ensemble_config.parameter_configuration,
responses=batch_simulator.ert_config.ensemble_config.response_configuration,
)

with pytest.raises(KeyError, match=match):
batch_simulator.start("case", _input, storage)
batch_simulator.start("case", _input, experiment)


@pytest.mark.integration_test
Expand All @@ -166,7 +172,13 @@ def test_batch_simulation(batch_simulator, storage):
),
]

ctx = batch_simulator.start("case", case_data, storage=storage)
experiment = storage.create_experiment(
name="EnOptCase",
parameters=batch_simulator.ert_config.ensemble_config.parameter_configuration,
responses=batch_simulator.ert_config.ensemble_config.response_configuration,
)

ctx = batch_simulator.start("case", case_data, experiment)
assert len(case_data) == len(ctx.mask)

# Asking for results before it is complete.
Expand Down Expand Up @@ -280,8 +292,15 @@ def test_that_batch_simulator_handles_invalid_suffixes_at_start(
},
["ORDER"],
)

experiment = storage.create_experiment(
name="EnOptCase",
parameters=batch_sim_example.ensemble_config.parameter_configuration,
responses=batch_sim_example.ensemble_config.response_configuration,
)

with pytest.raises(KeyError, match=match):
rsim.start("case", inp, storage)
rsim.start("case", inp, experiment)


@pytest.mark.integration_test
Expand Down Expand Up @@ -328,7 +347,13 @@ def test_batch_simulation_suffixes(batch_sim_example, storage):
),
]

ctx = rsim.start("case", case_data, storage=storage)
experiment = storage.create_experiment(
name="EnOptCase",
parameters=ert_config.ensemble_config.parameter_configuration,
responses=ert_config.ensemble_config.response_configuration,
)

ctx = rsim.start("case", case_data, experiment)
assert len(case_data) == len(ctx)
_wait_for_completion(ctx)

Expand Down Expand Up @@ -395,8 +420,14 @@ def test_stop_sim(copy_case, storage):
),
]

experiment = storage.create_experiment(
name="EnOptCase",
parameters=ert_config.ensemble_config.parameter_configuration,
responses=ert_config.ensemble_config.response_configuration,
)

# Starting a simulation which should actually run through.
ctx = rsim.start(case_name, case_data, storage=storage)
ctx = rsim.start(case_name, case_data, experiment)

ctx.stop()
status = ctx.status
Expand Down Expand Up @@ -459,7 +490,13 @@ def test_batch_ctx_status_failing_jobs(setup_case, storage):
for idx in range(10)
]

batch_ctx = rsim.start("case_name", ensembles, storage=storage)
experiment = storage.create_experiment(
name="EnOptCase",
parameters=ert_config.ensemble_config.parameter_configuration,
responses=ert_config.ensemble_config.response_configuration,
)

batch_ctx = rsim.start("case_name", ensembles, experiment)
while batch_ctx.running():
assertContextStatusOddFailures(batch_ctx)
time.sleep(1)
Expand Down

0 comments on commit 904aa5b

Please sign in to comment.