Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose observation uncertainty to users via YAML configuration file #132

Merged
merged 16 commits into from
Aug 26, 2020
Merged
44 changes: 14 additions & 30 deletions src/flownet/ahm/_assisted_history_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import pathlib
import re
import shutil
from typing import List, Dict, Optional, Tuple
from typing import List, Dict, Tuple

from configsuite import ConfigSuite
wouterjdb marked this conversation as resolved.
Show resolved Hide resolved
import jinja2
import numpy as np
import pandas as pd
Expand Down Expand Up @@ -37,11 +38,7 @@ def __init__(
network: NetworkModel,
schedule: Schedule,
parameters: List[Parameter],
case_name: str,
perforation_strategy: str,
reference_simulation: str,
ert_config: Dict,
random_seed: Optional[int] = None,
config: ConfigSuite.snapshot,
):
"""
Initialize an Assisted History Matching Class
Expand All @@ -50,22 +47,12 @@ def __init__(
network: NetworkModel instance
schedule: Schedule instance
parameters: List of Parameter objects
case_name: Name of simulation case
perforation_strategy: String indicating perforation handling strategy
reference_simulation: String indicating path to reference simulation case
ert_config: Dictionary containing information about queue (system, name, server and max_running)
and realizations (num_realizations, required_success_percent and max_runtime)
random_seed: Random seed to control reproducibility of FlowNet

config: Information from the FlowNet config YAML
"""
self._network: NetworkModel = network
self._schedule: Schedule = schedule
self._parameters: List[Parameter] = parameters
self._perforation_strategy: str = perforation_strategy
self._reference_simulation: str = reference_simulation
self._ert_config: dict = ert_config
self._case_name: str = case_name
self._random_seed: Optional[int] = random_seed
self._config: ConfigSuite.snapshot = config

def create_ert_setup(self, args: argparse.Namespace, training_set_fraction: float):
# pylint: disable=attribute-defined-outside-init
Expand All @@ -87,11 +74,8 @@ def create_ert_setup(self, args: argparse.Namespace, training_set_fraction: floa
args,
self._network,
self._schedule,
perforation_strategy=self._perforation_strategy,
reference_simulation=self._reference_simulation,
ert_config=self._ert_config,
config=self._config,
parameters=self._parameters,
random_seed=self._random_seed,
training_set_fraction=training_set_fraction,
)

Expand Down Expand Up @@ -122,15 +106,15 @@ def run_ert(self, weights: List[float]):
{
"output_folder": self.output_folder,
"iterations": range(len(weights) + 1),
"runpath": self._ert_config["runpath"],
"runpath": self._config.ert.runpath,
}
)
)

run_ert_subprocess(
f"ert es_mda --weights {','.join(map(str, weights))!r} ahm_config.ert",
cwd=self.output_folder,
runpath=self._ert_config["runpath"],
runpath=self._config.ert.runpath,
)

def report(self):
Expand All @@ -150,7 +134,7 @@ def report(self):
f"Number of observations: {self._schedule.get_nr_observations(self._training_set_fraction):>20}"
)
print(
f"Number of realizations: {self._ert_config['realizations'].num_realizations:>20}"
f"Number of realizations: {self._config.ert.realizations.num_realizations:>20}"
)

distributions = {
Expand All @@ -166,14 +150,14 @@ def report(self):
for distribution in distributions:
if distribution[0] == LogUniformDistribution:
print(
"Loguniform".ljust((15)),
"Loguniform".ljust(15),
f"{distribution[1]:16.8f}",
f"{(distribution[2] - distribution[1]) / np.log(distribution[2] / distribution[1]):16.8f}",
f"{distribution[2]:16.8f}",
)
else:
print(
"Uniform".ljust((15)),
"Uniform".ljust(15),
f"{distribution[1]:16.8f}",
f"{(distribution[2] + distribution[1]) / 2.0:16.8f}",
f"{distribution[2]:16.8f}",
Expand All @@ -190,7 +174,7 @@ def delete_simulation_output():
Nothing

"""
parser = argparse.ArgumentParser(prog=("Delete simulation output."))
parser = argparse.ArgumentParser(prog="Delete simulation output.")

parser.add_argument(
"ecl_base", type=str, help="Base name of the simulation DATA file"
Expand Down Expand Up @@ -218,7 +202,7 @@ def _load_parameters(runpath: str) -> Tuple[int, Dict]:
realization = int(re.findall(r"[0-9]+", runpath)[-2])
parameters = json.loads((pathlib.Path(runpath) / "parameters.json").read_text())

return (realization, parameters["FLOWNET_PARAMETERS"])
return realization, parameters["FLOWNET_PARAMETERS"]


def save_iteration_parameters():
Expand All @@ -243,7 +227,7 @@ def save_iteration_parameters():
Nothing

"""
parser = argparse.ArgumentParser(prog=("Save iteration parameters to a file."))
parser = argparse.ArgumentParser(prog="Save iteration parameters to a file.")
parser.add_argument("runpath", type=str, help="Path to the ERT runpath.")
args = parser.parse_args()
args.runpath = args.runpath.replace("%d", "*")
Expand Down
13 changes: 2 additions & 11 deletions src/flownet/ahm/_run_ahm.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def run_flownet_history_matching(

# Load production and well coordinate data
field_data = FlowData(
config.flownet.data_source.input_case,
config.flownet.data_source.simulation.input_case,
perforation_handling_strategy=config.flownet.perforation_handling_strategy,
)
df_production_data: pd.DataFrame = field_data.production
Expand Down Expand Up @@ -393,16 +393,7 @@ def run_flownet_history_matching(
if isinstance(network.faults, dict):
parameters.append(FaultTransmissibility(fault_mult_dist_values, network))

ahm = AssistedHistoryMatching(
network,
schedule,
parameters,
case_name=config.name,
perforation_strategy=config.flownet.perforation_handling_strategy,
reference_simulation=config.flownet.data_source.input_case,
ert_config=config.ert._asdict(),
random_seed=config.flownet.random_seed,
)
ahm = AssistedHistoryMatching(network, schedule, parameters, config,)

ahm.create_ert_setup(
args=args, training_set_fraction=_find_training_set_fraction(schedule, config),
Expand Down
Loading