Skip to content

Commit

Permalink
Updated test code
Browse files Browse the repository at this point in the history
  • Loading branch information
oddvarlia committed Nov 24, 2023
1 parent 921482c commit 865c092
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 43 deletions.
2 changes: 1 addition & 1 deletion tests/jobs/localisation/example_case/scripts/FM_SIM_FIELD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
EXECUTABLE ./sim_fields.py

ARGLIST <ITER> <IENS> <TEST_CONFIG>
ARGLIST <ITER> <IENS> <TEST_CONFIG> <CONFIG_PATH>

STDERR sim_fields.stderr
STDOUT sim_fields.stdout
50 changes: 34 additions & 16 deletions tests/jobs/localisation/example_case/scripts/common_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
Common functions used by the scripts: init_test_case.py and sim_field.py
"""
import copy
import dataclasses
import math
import os
from dataclasses import dataclass
from typing import Tuple
from pathlib import Path
from typing import Any, Dict, Tuple

import gstools as gs
import numpy as np
Expand Down Expand Up @@ -50,7 +51,7 @@ class Field:
initial_file_name: str = "init_files/FieldParam"
updated_file_name: str = "FieldParam"
seed_file: str = "randomseeds.txt"
variogram: str = "exponential"
variogram: str = "gaussian"
correlation_range: Tuple[float] = (250.0, 500.0, 2.0)
correlation_azimuth: float = 0.0
correlation_dip: float = 0.0
Expand Down Expand Up @@ -134,18 +135,35 @@ class Settings:
localisation: Localisation = Localisation()
optional: Optional = Optional()


def read_config_file(config_file_name):
# Modify default settings if config_file exists
settings = Settings()
if os.path.exists(config_file_name):
with open(config_file_name, "r", encoding="utf-8") as yml_file:
settings_yml = yaml.safe_load(yml_file)

updated_settings = update_settings(settings, settings_yml)
return updated_settings

raise IOError("Missing config file ")
def update(self, updates):
for key, value in updates.items():
if hasattr(self, key):
attr = getattr(self, key)
if dataclasses.is_dataclass(attr):
for attr_key, attr_value in value.items():
if hasattr(attr, attr_key):
setattr(attr, attr_key, attr_value)
else:
setattr(self, key, value)


def read_config_file(config_file_name: Path) -> Dict[str, Any]:
with open(config_file_name, "r", encoding="utf-8") as f:
settings = yaml.safe_load(f)
model_size = ModelSize(**settings["settings"]["model_size"])
field = Field(**settings["settings"]["field"])
response = Response(**settings["settings"]["response"])
observation = Observation(**settings["settings"]["observation"])
localisation = Localisation(**settings["settings"]["localisation"])
optional = Optional(**settings["settings"]["optional"])
return Settings(
model_size=model_size,
field=field,
response=response,
observation=observation,
localisation=localisation,
optional=optional,
)


def update_key(key, default_value, settings_dict, parent_key=None):
Expand Down Expand Up @@ -493,7 +511,6 @@ def generate_field_and_upscale(


def get_seed(seed_file_name, r_number):
# Realization number counted from 0
with open(seed_file_name, "r", encoding="utf8") as file:
lines = file.readlines()
try:
Expand All @@ -504,6 +521,7 @@ def get_seed(seed_file_name, r_number):
raise IOError(
"Invalid seed value in file for realization{r_number}"
) from exc
print(f"Seed value from get_seed: {seed_value} Realisation number: {r_number} ")
return seed_value


Expand Down
22 changes: 14 additions & 8 deletions tests/jobs/localisation/example_case/scripts/sim_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
Script used as forward model in ERT to test localisation.
"""
import os
import sys

# pylint: disable=import-error, redefined-outer-name
Expand Down Expand Up @@ -56,7 +57,9 @@ def get_iteration_real_number_config_file(argv):
print(f"ERT realization: {real_number}")

config_file_name = argv[3]
return iteration, real_number, config_file_name

config_path = argv[4]
return iteration, real_number, config_file_name, config_path


def main(args):
Expand All @@ -75,18 +78,21 @@ def main(args):
# and the coarse grid with upscaled values must have Eclipse grid index origin

# Read config_file if it exists. Use default settings for everything not specified.
iteration, real_number, config_file_name = get_iteration_real_number_config_file(
args
)
(
iteration,
real_number,
config_file_name,
config_path,
) = get_iteration_real_number_config_file(args)
settings = read_config_file(config_file_name)

print(f"Config path: {config_path}")
if iteration == 0:
print(f"Generate new field parameter realization:{real_number} ")
# Simulate field (with trend)
upscaled_values = generate_field_and_upscale(
real_number,
iteration,
settings.field.seed_file,
os.path.join(config_path, settings.field.seed_file),
settings.field.algorithm,
settings.field.name,
settings.field.initial_file_name,
Expand Down Expand Up @@ -142,9 +148,9 @@ def main(args):
if settings.optional.write_obs_pred_diff_field_file:
obs_field_object = read_obs_field_from_file(
settings.response.file_format,
settings.observation.reference_param_file,
os.path.join(config_path, settings.observation.reference_param_file),
settings.response.grid_file_name,
settings.observation.reference_field_name,
os.path.join(config_path, settings.observation.reference_field_name),
)
upscaled_field_object = read_upscaled_field_from_file(
iteration,
Expand Down
16 changes: 7 additions & 9 deletions tests/jobs/localisation/example_case/sim_field_case_A.ert
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
DEFINE <USER> $USER
DEFINE <SCRATCH> /scratch/fmu
DEFINE <CASE_DIR> sim_field_A
DEFINE <ENSEMBLE_SEED_FILE> randomseeds.txt
DEFINE <TEST_CONFIG> <CONFIG_PATH>/example_test_config_A.yml
Expand All @@ -10,7 +9,6 @@ INSTALL_JOB SIM_FIELD scripts/FM_SIM_FIELD

DEFINE <OBS_FILE> <CONFIG_PATH>/observations/observations.obs
OBS_CONFIG <OBS_FILE>
TIME_MAP time_map.txt

JOBNAME sim_fields_<IENS>

Expand All @@ -19,12 +17,13 @@ NUM_REALIZATIONS 10 -- Set number of realizations to run
MAX_RUNTIME 18000 -- Set the maximum allowed run time (in seconds)
MIN_REALIZATIONS 1 -- Success criteria
MAX_SUBMIT 1 -- How many times should the queue system retry a simulation.
QUEUE_OPTION LSF MAX_RUNNING 100 -- Choke the number of simultaneous run
QUEUE_OPTION LSF LSF_QUEUE mr -- Assign LSF cluster queue to use

RUNPATH <SCRATCH>/<USER>/<CASE_DIR>/realization-<IENS>/iter-<ITER>
-- QUEUE_OPTION LSF MAX_RUNNING 100 -- Choke the number of simultaneous run
-- QUEUE_OPTION LSF LSF_QUEUE mr -- Assign LSF cluster queue to use
QUEUE_SYSTEM LOCAL
QUEUE_OPTION LOCAL MAX_RUNNING 10
RANDOM_SEED 123456 -- ERT seed value

RUNPATH simulations/<CASE_DIR>/realization-<IENS>/iter-<ITER>
ENSPATH output/<CASE_DIR>/storage -- Storage of internal ert data
UPDATE_LOG_PATH output/<CASE_DIR>/update_log -- Info of active and inactive data points
RUNPATH_FILE output/<CASE_DIR>/runpath_file -- List of runpaths
Expand All @@ -47,11 +46,10 @@ FORWARD_MODEL COPY_FILE(<FROM>=<CONFIG_PATH>/<ENSEMBLE_SEED_FILE>, <TO>=<RUNPAT
-- For QC purpose only
FORWARD_MODEL COPY_FILE(<FROM>=<CONFIG_PATH>/init_files/ObsField.roff, <TO>=<RUNPATH>/init_files/ObsField.roff)
FORWARD_MODEL MAKE_SYMLINK(<TARGET>=<CONFIG_PATH>/GRID_STANDARD.EGRID, <LINKNAME>=<RUNPATH>/GRID_STANDARD.EGRID)
FORWARD_MODEL MAKE_SYMLINK(<TARGET>=<CONFIG_PATH>/UpscaleGrid.EGRID, <LINKNAME>=<RUNPATH>/UpscaleGrid.EGRID)
FORWARD_MODEL MAKE_SYMLINK(<TARGET>=<CONFIG_PATH>/GRID_STANDARD_UPSCALED.EGRID, <LINKNAME>=<RUNPATH>/GRID_STANDARD_UPSCALED.EGRID)

-- The main forward model simulating gaussian field with trend, and upscale
FORWARD_MODEL SIM_FIELD(<ITERATION>=<ITER>, <REALNUMBER>=<IENS>, <TEST_CONFIG>=<TEST_CONFIG>)

FORWARD_MODEL SIM_FIELD(<ITERATION>=<ITER>, <REALNUMBER>=<IENS>, <TEST_CONFIG>=<TEST_CONFIG>, <CONFIG_PATH>=<CONFIG_PATH>)

GRID <CONFIG_PATH>/GRID_STANDARD.EGRID -- Necessary for AHM using field parameters

Expand Down
17 changes: 8 additions & 9 deletions tests/jobs/localisation/example_case/sim_field_local_case_A.ert
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
DEFINE <USER> $USER
DEFINE <SCRATCH> /scratch/fmu
DEFINE <CASE_DIR> sim_field_local_A
DEFINE <ENSEMBLE_SEED_FILE> randomseeds.txt
DEFINE <TEST_CONFIG> <CONFIG_PATH>/example_test_config_A.yml
Expand All @@ -10,21 +9,21 @@ INSTALL_JOB SIM_FIELD scripts/FM_SIM_FIELD

DEFINE <OBS_FILE> <CONFIG_PATH>/observations/observations.obs
OBS_CONFIG <OBS_FILE>
TIME_MAP time_map.txt

JOBNAME sim_fields_<IENS>


NUM_REALIZATIONS 10 -- Set number of realizations to run
NUM_REALIZATIONS 10 -- Set number of realizations to run
MAX_RUNTIME 18000 -- Set the maximum allowed run time (in seconds)
MIN_REALIZATIONS 1 -- Success criteria
MAX_SUBMIT 1 -- How many times should the queue system retry a simulation.
QUEUE_OPTION LSF MAX_RUNNING 100 -- Choke the number of simultaneous run
QUEUE_OPTION LSF LSF_QUEUE mr -- Assign LSF cluster queue to use

RUNPATH <SCRATCH>/<USER>/<CASE_DIR>/realization-<IENS>/iter-<ITER>
-- QUEUE_OPTION LSF MAX_RUNNING 100 -- Choke the number of simultaneous run
-- QUEUE_OPTION LSF LSF_QUEUE mr -- Assign LSF cluster queue to use
QUEUE_SYSTEM LOCAL
QUEUE_OPTION LOCAL MAX_RUNNING 10
RANDOM_SEED 123456 -- ERT seed value

RUNPATH simulations/<CASE_DIR>/realization-<IENS>/iter-<ITER>
ENSPATH output/<CASE_DIR>/storage -- Storage of internal ert data
UPDATE_LOG_PATH output/<CASE_DIR>/update_log -- Info of active and inactive data points
RUNPATH_FILE output/<CASE_DIR>/runpath_file -- List of runpaths
Expand All @@ -47,10 +46,10 @@ FORWARD_MODEL COPY_FILE(<FROM>=<CONFIG_PATH>/<ENSEMBLE_SEED_FILE>, <TO>=<RUNPAT
-- For QC purpose only
FORWARD_MODEL COPY_FILE(<FROM>=<CONFIG_PATH>/init_files/ObsField.roff, <TO>=<RUNPATH>/init_files/ObsField.roff)
FORWARD_MODEL MAKE_SYMLINK(<TARGET>=<CONFIG_PATH>/GRID_STANDARD.EGRID, <LINKNAME>=<RUNPATH>/GRID_STANDARD.EGRID)
FORWARD_MODEL MAKE_SYMLINK(<TARGET>=<CONFIG_PATH>/UpscaleGrid.EGRID, <LINKNAME>=<RUNPATH>/UpscaleGrid.EGRID)
FORWARD_MODEL MAKE_SYMLINK(<TARGET>=<CONFIG_PATH>/GRID_STANDARD_UPSCALED.EGRID, <LINKNAME>=<RUNPATH>/GRID_STANDARD_UPSCALED.EGRID)

-- The main forward model simulating gaussian field with trend, and upscale
FORWARD_MODEL SIM_FIELD(<ITERATION>=<ITER>, <REALNUMBER>=<IENS>, <TEST_CONFIG>=<TEST_CONFIG>)
FORWARD_MODEL SIM_FIELD(<ITERATION>=<ITER>, <REALNUMBER>=<IENS>, <TEST_CONFIG>=<TEST_CONFIG>, <CONFIG_PATH>=<CONFIG_PATH>)

GRID <CONFIG_PATH>/GRID_STANDARD.EGRID -- Necessary for AHM using field parameters

Expand Down

0 comments on commit 865c092

Please sign in to comment.