Skip to content

Commit

Permalink
Add test case for non-adaptive localisation
Browse files Browse the repository at this point in the history
Add option to specify alternative settings for the test case using a yml file
Replaced gaussianfft as python module for simulation of gaussian fields with gstools.
write scaling factor in same file format as input fields
Add config file for test example
Add example case with  ROFF and GRDECL file
Add cases with RMS grid index origin
Add case with actnum
  • Loading branch information
oddvarlia committed Nov 8, 2023
1 parent d49714d commit 496aee1
Show file tree
Hide file tree
Showing 35 changed files with 2,643 additions and 26 deletions.
84 changes: 58 additions & 26 deletions semeio/workflows/localisation/local_script_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from ecl.util.geometry import Surface
from ert.analysis.row_scaling import RowScaling
from ert.config import Field, GenDataConfig, GenKwConfig, SurfaceConfig
from ert.field_utils import save_field
from ert.field_utils.field_file_format import FieldFileFormat
from numpy import ma

from semeio.workflows.localisation.localisation_debug_settings import (
Expand Down Expand Up @@ -632,7 +634,9 @@ def add_ministeps(
tapering_range = (
corr_spec.field_scale.normalised_tapering_range
)
check_if_ref_point_in_grid(ref_pos, grid_for_field)
check_if_ref_point_in_grid(
ref_pos, grid_for_field, log_level=user_config.log_level
)
param_for_field = apply_decay(
corr_spec.field_scale.method,
row_scaling,
Expand Down Expand Up @@ -695,6 +699,7 @@ def add_ministeps(
grid_for_field,
param_for_field,
user_config.log_level,
node.file_format,
)
update_step["row_scaling_parameters"].append(
[node_name, row_scaling]
Expand Down Expand Up @@ -810,15 +815,21 @@ def add_ministeps(
return update_steps


def check_if_ref_point_in_grid(ref_point, grid):
def check_if_ref_point_in_grid(ref_point, grid, log_level):
try:
grid.find_cell_xy(ref_point[0], ref_point[1], 0)
(i_indx, j_indx) = grid.find_cell_xy(ref_point[0], ref_point[1], 0)
except ValueError as err:
raise ValueError(
f"Reference point {ref_point} corresponds to undefined grid cell "
f"or is outside the area defined by the grid {grid.get_name()}\n"
"Check specification of reference point."
"Check specification of reference point ",
"and grid index origin of grid with field parameters.",
) from err
debug_print(
f"Reference point {ref_point} has grid indices: ({i_indx}, {j_indx})",
LogLevel.LEVEL3,
log_level,
)


@dataclass
Expand Down Expand Up @@ -947,35 +958,56 @@ def write_qc_parameter(
grid,
param_for_field,
log_level=LogLevel.OFF,
file_format=FieldFileFormat.GRDECL,
):
# pylint: disable=too-many-arguments

if param_for_field is None or field_scale is None:
return

scaling_values = np.reshape(
param_for_field, (grid.getNX(), grid.getNY(), grid.getNZ()), "F"
# Write scaling parameter once per corr_name
if corr_name == cls.corr_name:
return

cls.corr_name = corr_name

# Need a parameter name <= 8 character long for GRDECL format
scaling_kw_name = "S_" + str(cls.scaling_param_number)
file_name_without_suffix = (
cls.corr_name + "_" + node_name + "_" + scaling_kw_name
)

# Write scaling parameter once per corr_name
if corr_name != cls.corr_name:
cls.corr_name = corr_name
# Need a parameter name <= 8 character long
scaling_kw_name = "S_" + str(cls.scaling_param_number)
scaling_kw = grid.create_kw(scaling_values, scaling_kw_name, False)
filename = (
cls.corr_name + "_" + node_name + "_" + scaling_kw_name + ".GRDECL"
)
print(
"Write calculated scaling factor with name: "
f"{scaling_kw_name} to file: {filename}"
)
debug_print(
f"Write calculated scaling factor with name: "
f"{scaling_kw_name} to file: {filename}",
LogLevel.LEVEL3,
log_level,
if file_format == FieldFileFormat.GRDECL:
scaling_values = np.reshape(
param_for_field, (grid.getNX(), grid.getNY(), grid.getNZ()), "F"
)

scaling_kw = grid.create_kw(scaling_values, scaling_kw_name, False)
filename = file_name_without_suffix + ".GRDECL"

with cwrap.open(filename, "w") as file:
grid.write_grdecl(scaling_kw, file)
# Increase parameter number to define unique parameter name
cls.scaling_param_number = cls.scaling_param_number + 1
# For testing
name = scaling_kw_name + "_use_save_field"
save_field(scaling_values, name, filename, FieldFileFormat.GRDECL)

elif file_format == FieldFileFormat.ROFF:
scaling_values = np.reshape(
param_for_field, (grid.getNX(), grid.getNY(), grid.getNZ()), "C"
)
filename = file_name_without_suffix + ".roff"
save_field(scaling_values, scaling_kw_name, filename, FieldFileFormat.ROFF)

print(
"Write calculated scaling factor with name: "
f"{scaling_kw_name} to file: {filename}"
)
debug_print(
f"Write calculated scaling factor with name: "
f"{scaling_kw_name} to file: {filename}",
LogLevel.LEVEL3,
log_level,
)

# Increase parameter number to define unique parameter name
cls.scaling_param_number = cls.scaling_param_number + 1
1 change: 1 addition & 0 deletions test_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ pytest-httpserver
pytest-snapshot
rstcheck-core<=1.0.3
xlwt
gstools
43 changes: 43 additions & 0 deletions tests/jobs/localisation/example_case/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
## Directory for Test Case for Non-Adaptive Localisation (`LOCALISATION_JOB`)
### Main Components:
- **ERT config file:** `sim_field.ert`
The ERT model depends on:
- `scripts/sim_fields.py`
- `scripts/common_functions.py`
- **ERT Keyword Input:**
- **GRID:** Input grid is made by `scripts/init_test_case.py`
- **OBS_CONFIG:** Observations are extracted from an upscaled realization made by `scripts/init_test_case.py`
- **GEN_DATA:** Prediction of observables made by forward model `SIM_FIELD` which runs `scripts/sim_fields.py`
- **FIELD:** Prior realizations of field made by forward model `SIM_FIELD` which runs `scripts/sim_fields.py`
- **Other ERT Input Files:**
- `time_map.txt`
- `localisation.wf`
- **Other Files:**
- `randomseeds.txt`: Not used by ERT, but by `sim_fields.py`. This file can be generated by `scripts/init_test_case.py`
- `UpscaledGrid.EGRID`: Not used by ERT, but by `sim_fields.py`. This file can be generated by `scripts/init_test_case.py`
### Typical Workflow:
1. **Preparation:** Prepare ERT config input by running `scripts/init_test_case.py`.
2. Directories for observations are created automatically according to the default settings.
3. Activate or deactivate localisation in ERT config file (`HOOK_WORKFLOW LOAD_WORKFLOW` for localisation).
4. Now ready to run ERT.
### What the Script `sim_fields.py` Does:
1. Get iteration and realisation number from ERT using ERT environment variables `_ERT_ITERATION_NUMBER` and `_ERT_REALIZATION_NUMBER`. If running with an old Komodo version, the script will require iteration and realisation_number as command line input.
2. If iteration = 0, then:
- Simulate field and export to file to be used in `FIELD` keyword in ERT config file.
- Upscale field and optionally export to file for QC purpose.
Else:
- Import updated field from ERT.
- Upscale field and optionally export file for QC purpose.
3. Extract predicted values of observables from upscaled field (values for selected grid cells related to the upscaled grid) and write `GEN_DATA` files.
4. Optionally write some files for QC purpose.Directory for test case for non-adaptive localisation (LOCALISATION_JOB)
53 changes: 53 additions & 0 deletions tests/jobs/localisation/example_case/example_test_config_A.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# ROFF format standard Eclipse origo
settings:
grid_size:
xsize: 7500.0
ysize: 12500.0
zsize: 50.0
use_eclipse_grid_index_origo: True

field:
name: "FIELDPAR"
algorithm: "gstools"
file_format: "ROFF"
initial_file_name: "init_files/FieldParam"
updated_file_name: "FieldParam"
seed_file: "randomseeds.txt"
variogram: "gaussian"
correlation_range: [3000.0, 2000.0, 2.0]
correlation_azimuth: 45.0
correlation_dip: 0.0
correlation_exponent: 1.9
trend_use: False
trend_params: [ 1.0, -1.0 ]
trend_relstd: 0.15
grid_dimension: [150, 250, 1]
grid_file_name: "GRID_ECLIPSE_STANDARD_GRID_INDEX_ORIGO.EGRID"

response:
name: "UPSCALED"
grid_dimension: [15, 25, 1]
upscaled_file_name: "Upscaled"
grid_file_name: "UpscaleGrid.EGRID"
file_format: "ROFF"
write_upscaled_field: True
response_function: "average"
gen_data_file_name: "UpscaledField_0.txt"

observation:
directory: "observations"
file_name: "observations.obs"
data_dir: "obs_data"
reference_param_file: "init_files/ObsField"
reference_field_name: "ObsField"
rel_error: 0.10
min_abs_error: 0.01
# selected_grid_cells:
# - [5, 10, 1]
# - [10, 5, 1]

localisation:
method: "gaussian"

optional:
write_obs_pred_diff_field_file: True
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# ROFF format standard Eclipse origo
settings:
grid_size:
xsize: 7500.0
ysize: 12500.0
zsize: 50.0
use_eclipse_grid_index_origo: True
polygon_file: "Polygons.txt"

field:
name: "FIELDPAR"
algorithm: "gstools"
file_format: "ROFF"
initial_file_name: "init_files/FieldParam"
updated_file_name: "FieldParam"
seed_file: "randomseeds.txt"
variogram: "gaussian"
correlation_range: [3000.0, 2000.0, 2.0]
correlation_azimuth: 45.0
correlation_dip: 0.0
correlation_exponent: 1.9
trend_use: False
trend_params: [ 1.0, -1.0 ]
trend_relstd: 0.15
grid_dimension: [150, 250, 1]
grid_file_name: "GRID_WITH_ACTNUM.EGRID"

response:
name: "UPSCALED"
grid_dimension: [15, 25, 1]
upscaled_file_name: "Upscaled"
grid_file_name: "UpscaleGrid.EGRID"
file_format: "ROFF"
write_upscaled_field: True
response_function: "average"
gen_data_file_name: "UpscaledField_0.txt"

observation:
directory: "observations"
file_name: "observations.obs"
data_dir: "obs_data"
reference_param_file: "init_files/ObsField"
reference_field_name: "ObsField"
rel_error: 0.10
min_abs_error: 0.01
# selected_grid_cells:
# - [5, 10, 1]
# - [10, 5, 1]

localisation:
method: "gaussian"

optional:
write_obs_pred_diff_field_file: True
53 changes: 53 additions & 0 deletions tests/jobs/localisation/example_case/example_test_config_B.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# GRDECL format standard Eclipse origo
settings:
grid_size:
xsize: 7500.0
ysize: 12500.0
zsize: 50.0
use_eclipse_grid_index_origo: True

field:
name: "FIELDPAR"
algorithm: "gstools"
file_format: "GRDECL"
initial_file_name: "init_files/FieldParam"
updated_file_name: "FieldParam"
seed_file: "randomseeds.txt"
variogram: "gaussian"
correlation_range: [3000.0, 2000.0, 2.0]
correlation_azimuth: 45.0
correlation_dip: 0.0
correlation_exponent: 1.9
trend_use: False
trend_params: [ 1.0, -1.0 ]
trend_relstd: 0.15
grid_dimension: [150, 250, 1]
grid_file_name: "GRID_ECLIPSE_STANDARD_GRID_INDEX_ORIGO.EGRID"

response:
name: "UPSCALED"
grid_dimension: [15, 25, 1]
upscaled_file_name: "Upscaled"
grid_file_name: "UpscaleGrid.EGRID"
file_format: "GRDECL"
write_upscaled_field: True
response_function: "average"
gen_data_file_name: "UpscaledField_0.txt"

observation:
directory: "observations"
file_name: "observations.obs"
data_dir: "obs_data"
reference_param_file: "init_files/ObsField"
reference_field_name: "ObsField"
rel_error: 0.10
min_abs_error: 0.01
# selected_grid_cells:
# - [5, 10, 1]
# - [10, 5, 1]

localisation:
method: "gaussian"

optional:
write_obs_pred_diff_field_file: True
53 changes: 53 additions & 0 deletions tests/jobs/localisation/example_case/example_test_config_C.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# ROFF format standard RMS origo
settings:
grid_size:
xsize: 7500.0
ysize: 12500.0
zsize: 50.0
use_eclipse_grid_index_origo: False

field:
name: "FIELDPAR"
algorithm: "gstools"
file_format: "ROFF"
initial_file_name: "init_files/FieldParam"
updated_file_name: "FieldParam"
seed_file: "randomseeds.txt"
variogram: "gaussian"
correlation_range: [3000.0, 2000.0, 2.0]
correlation_azimuth: 45.0
correlation_dip: 0.0
correlation_exponent: 1.9
trend_use: False
trend_params: [ 1.0, -1.0 ]
trend_relstd: 0.15
grid_dimension: [150, 250, 1]
grid_file_name: "GRID_RMS_STANDARD_GRID_INDEX_ORIGO.EGRID"

response:
name: "UPSCALED"
grid_dimension: [15, 25, 1]
upscaled_file_name: "Upscaled"
grid_file_name: "UpscaleGrid.EGRID"
file_format: "ROFF"
write_upscaled_field: True
response_function: "average"
gen_data_file_name: "UpscaledField_0.txt"

observation:
directory: "observations"
file_name: "observations.obs"
data_dir: "obs_data"
reference_param_file: "init_files/ObsField"
reference_field_name: "ObsField"
rel_error: 0.10
min_abs_error: 0.01
# selected_grid_cells:
# - [5, 10, 1]
# - [10, 5, 1]

localisation:
method: "gaussian"

optional:
write_obs_pred_diff_field_file: True
Loading

0 comments on commit 496aee1

Please sign in to comment.