Skip to content

Commit

Permalink
Modified test case for non-adaptive localisation
Browse files Browse the repository at this point in the history
  • Loading branch information
oddvarlia committed Oct 29, 2023
1 parent d5d2e7e commit aa7ab86
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 34 deletions.
61 changes: 53 additions & 8 deletions tests/jobs/localisation/example_case/scripts/common_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ class GridSize:

xsize: float = 7500.0
ysize: float = 12500.0
# xsize: float = 7500.0
# ysize: float = 7500.0
zsize: float = 50.0
use_eclipse_grid_index_origo: bool = True
use_eclipse_grid_index_origo: bool = False


@dataclass
Expand All @@ -48,7 +50,7 @@ class Field:
updated_file_name: str = "FieldParam.roff"
seed_file: str = "randomseeds.txt"
variogram: str = "exponential"
correlation_range: Tuple[float] = (5000.0, 3000.0, 2.0)
correlation_range: Tuple[float] = (3000.0, 1000.0, 2.0)
correlation_azimuth: float = 45.0
correlation_dip: float = 0.0
correlation_exponent: float = 1.9
Expand Down Expand Up @@ -91,7 +93,18 @@ class Observation:
param_file_name: str = "init_files/UpscaledObsField.roff"
rel_error: float = 0.10
min_abs_error: float = 0.01
selected_grid_cells: Tuple[Tuple[int]] = ((5, 10, 1), (10, 20, 1))
# selected_grid_cells: Tuple[Tuple[int]] = ((5, 10, 1), (10, 5, 1))
selected_grid_cells: Tuple[Tuple[int]] = (8, 12, 1)


@dataclass
class Localisation:
"""
Specify settings for the localisation config file.
"""

method: str = "gaussian"
# method: str = "constant"


@dataclass
Expand All @@ -115,6 +128,7 @@ class Settings:
field: Field = Field()
response: Response = Response()
observation: Observation = Observation()
localisation: Localisation = Localisation()
optional: Optional = Optional()


Expand Down Expand Up @@ -214,10 +228,11 @@ def write_upscaled_field(
if selected_cell_index_list is not None:
selected_upscaled_values = np.zeros((nx, ny, nz), dtype=np.float32, order="F")
selected_upscaled_values[:, :, :] = -1
for indices in selected_cell_index_list:
Iindx = indices[0] - 1
Jindx = indices[1] - 1
Kindx = indices[2] - 1
nobs = get_nobs()
for obs_number in range(nobs):
(Iindx, Jindx, Kindx) = get_cell_indices(
obs_number, nobs, selected_cell_index_list
)
selected_upscaled_values[Iindx, Jindx, Kindx] = upscaled_values[
Iindx, Jindx, Kindx
]
Expand Down Expand Up @@ -434,7 +449,7 @@ def simulate_field_using_gstools(start_seed):
# print(f"Field type name: {srf.name} ")
# print(f"Field nugget: {srf.nugget} ")
# print(f"Field opt arg: {srf.opt_arg}")
field = field_srf.reshape((nx, ny, nz), order="F")
field = field_srf.reshape((nx, ny, nz), order="C")
if settings.grid_size.use_eclipse_grid_index_origo:
field_result = np.zeros((nx, ny, nz), dtype=np.float32)
j_indices = -np.arange(ny) + ny - 1
Expand Down Expand Up @@ -524,3 +539,33 @@ def write_obs_pred_diff_field(upscaled_field_object, observation_field_object):
f"Write field with difference between observation and prediction: {filename} "
)
diff_object.to_file(filename, fformat="roff")


def get_cell_indices(obs_number, nobs, cell_indx_list):
if nobs == 1:
Iindx = cell_indx_list[0] - 1
Jindx = cell_indx_list[1] - 1
Kindx = cell_indx_list[2] - 1
else:
Iindx = cell_indx_list[obs_number][0] - 1
Jindx = cell_indx_list[obs_number][1] - 1
Kindx = cell_indx_list[obs_number][2] - 1

return (Iindx, Jindx, Kindx)


def get_nobs():
"""
Check if cell_indx_list is a single tuple (i,j,k) or
a list of tuples of type (i,j,k).
Return number of cell indices found in list
"""
cell_indx_list = settings.observation.selected_grid_cells
is_list_of_ints = all(isinstance(indx, int) for indx in cell_indx_list)
if is_list_of_ints:
nobs = 1
else:
# list of tuples (i,j,k)
nobs = len(cell_indx_list)
print(f"nobs: {nobs} ")
return nobs
67 changes: 45 additions & 22 deletions tests/jobs/localisation/example_case/scripts/init_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@

# pylint: disable=import-error
import xtgeo
from common_functions import generate_field_and_upscale, settings, write_upscaled_field
from common_functions import (
generate_field_and_upscale,
get_cell_indices,
get_nobs,
settings,
write_upscaled_field,
)

# pylint: disable=too-many-arguments,invalid-name,missing-function-docstring
# pylint: disable=too-many-locals,redefined-outer-name
Expand Down Expand Up @@ -48,9 +54,9 @@ def obs_positions():
)

pos_list = []
for indices in cell_indx_list:
Iindx = indices[0] - 1
Jindx = indices[1] - 1
nobs = get_nobs()
for obs_number in range(nobs):
(Iindx, Jindx, _) = get_cell_indices(obs_number, nobs, cell_indx_list)
x = (Iindx + 0.5) * dx
if use_eclipse_origo:
y = ysize - (Jindx + 0.5) * dy
Expand All @@ -65,6 +71,7 @@ def obs_positions():
def write_localisation_config(
config_file_name="local_config.yml",
write_scaling=True,
localisation_method="gaussian",
):
obs_index_list = settings.observation.selected_grid_cells
field_name = settings.field.name
Expand All @@ -79,21 +86,26 @@ def write_localisation_config(
file.write("log_level: 3\n")
file.write(f"write_scaling_factors: {write_scaling}\n")
file.write("correlations:\n")
for i, indx in enumerate(obs_index_list):
Iindx, Jindx, Kindx = indx
obs_name = f"OBS_{Iindx}_{Jindx}_{Kindx}"
pos = positions[i]
file.write(f"{space}- name: CORR_{i}\n")
nobs = get_nobs()
for obs_number in range(nobs):
(Iindx, Jindx, Kindx) = get_cell_indices(obs_number, nobs, obs_index_list)
obs_name = f"OBS_{Iindx+1}_{Jindx+1}_{Kindx+1}"
pos = positions[obs_number]
file.write(f"{space}- name: CORR_{obs_number}\n")
file.write(f"{space2}obs_group:\n")
file.write(f'{space3}add: ["{obs_name}"]\n')
file.write(f"{space2}param_group:\n")
file.write(f'{space3}add: ["{field_name}"]\n')
file.write(f"{space2}field_scale:\n")
file.write(f"{space3}method: gaussian_decay\n")
file.write(f"{space3}main_range: {corr_ranges[0]}\n")
file.write(f"{space3}perp_range: {corr_ranges[1]}\n")
file.write(f"{space3}azimuth: {azimuth}\n")
file.write(f"{space3}ref_point: [ {pos[0]}, {pos[1]} ]\n")
if localisation_method == "gaussian":
file.write(f"{space3}method: gaussian_decay\n")
file.write(f"{space3}main_range: {corr_ranges[0]}\n")
file.write(f"{space3}perp_range: {corr_ranges[1]}\n")
file.write(f"{space3}azimuth: {azimuth}\n")
file.write(f"{space3}ref_point: [ {pos[0]}, {pos[1]} ]\n")
elif localisation_method == "constant":
file.write(f"{space3}method: constant\n")
file.write(f"{space3}value: 1.0\n")


def write_gen_obs(upscaled_values):
Expand All @@ -114,12 +126,11 @@ def write_gen_obs(upscaled_values):
print(f"Write observation file: {obs_file_name} ")
filename = observation_dir + "/" + obs_file_name
with open(filename, "w", encoding="utf8") as obs_file:
number = 0
for indices in cell_indx_list:
Iindx = indices[0] - 1
Jindx = indices[1] - 1
Kindx = indices[2] - 1

# Check if cell_indx_list is a single tuple (i,j,k)
# or a list of tuples of type (i,j,k)
nobs = get_nobs()
for obs_number in range(nobs):
(Iindx, Jindx, Kindx) = get_cell_indices(obs_number, nobs, cell_indx_list)
value = upscaled_values[Iindx, Jindx, Kindx]
value_err = math.fabs(value) * rel_err
value_err = max(value_err, min_err)
Expand All @@ -138,11 +149,12 @@ def write_gen_obs(upscaled_values):
obs_file.write(f"GENERAL_OBSERVATION OBS_{Iindx+1}_{Jindx+1}_{Kindx+1} ")
obs_file.write("{ ")
obs_file.write(
f"DATA = RESULT_UPSCALED_FIELD ; INDEX_LIST = {number} ; RESTART = 0; "
f"DATA = RESULT_UPSCALED_FIELD ; INDEX_LIST = {obs_number} ; "
)
obs_file.write("RESTART = 0; ")
obs_file.write(f"OBS_FILE = ./{obs_data_relative_file_name} ; ")
obs_file.write(" };\n")
number += 1

data_file_name = observation_dir + "/" + obs_data_relative_file_name
print(f"Write file: {data_file_name} ")
with open(data_file_name, "w", encoding="utf8") as data_file:
Expand Down Expand Up @@ -252,9 +264,20 @@ def main():
)

# Write file for non-adaptive localisation using distance based localisation
write_localisation_config(
config_file_name="local_config_gaussian_decay.yml",
write_scaling=True,
localisation_method="gaussian",
)
write_localisation_config(
config_file_name="local_config_constant.yml",
write_scaling=True,
localisation_method="constant",
)
write_localisation_config(
config_file_name="local_config.yml",
write_scaling=True,
localisation_method=settings.localisation.method,
)


Expand Down
9 changes: 5 additions & 4 deletions tests/jobs/localisation/example_case/scripts/sim_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
# pylint: disable=missing-function-docstring,invalid-name
from common_functions import (
generate_field_and_upscale,
get_cell_indices,
get_nobs,
read_field_from_file,
read_obs_field_from_file,
read_upscaled_field_from_file,
Expand All @@ -27,10 +29,9 @@ def write_prediction_gen_data(upscaled_values):
print(f"Write GEN_DATA file with prediction of observations: {response_file_name}")
with open(response_file_name, "w", encoding="utf8") as file:
# NOTE: The sequence of values must be the same as for the observations
for indices in cell_indx_list:
Iindx = indices[0] - 1
Jindx = indices[1] - 1
Kindx = indices[2] - 1
nobs = get_nobs()
for obs_number in range(nobs):
(Iindx, Jindx, Kindx) = get_cell_indices(obs_number, nobs, cell_indx_list)
value = upscaled_values[Iindx, Jindx, Kindx]
print(f"Prediction of obs for {Iindx+1},{Jindx+1},{Kindx+1}: {value}")
file.write(f"{value}\n")
Expand Down

0 comments on commit aa7ab86

Please sign in to comment.