Skip to content

Commit 0234a02

Browse files
Update rips module and Python examples (#36)
Co-authored-by: magnesj <1793152+magnesj@users.noreply.github.com>
1 parent 474933f commit 0234a02

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+2215
-744
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
###########################################################################################
2+
# This example will calculate the average value of the porosity property for a specified
3+
# region in the reservoir model. The region is identified by its EQLNUM value.
4+
###########################################################################################
5+
6+
import rips
7+
8+
resinsight = rips.Instance.find()
9+
10+
cases = resinsight.project.cases()
11+
12+
time_step = 0
13+
region_number = 2
14+
15+
for case in cases:
16+
sum_poro = 0.0
17+
cell_cout = 0
18+
19+
eqlnum = case.active_cell_property("STATIC_NATIVE", "EQLNUM", time_step)
20+
poro = case.active_cell_property("STATIC_NATIVE", "PORO", time_step)
21+
if len(eqlnum) != len(poro):
22+
print("Size of eqlnum and poro is not identical.")
23+
break
24+
25+
for i in range(len(eqlnum)):
26+
if eqlnum[i] == region_number:
27+
sum_poro += poro[i]
28+
cell_cout += 1
29+
30+
# Calculate the average porosity for the specified region
31+
if cell_cout > 0:
32+
average_poro = sum_poro / cell_cout
33+
print(
34+
f"Case {case.id}: Cell count {cell_cout} Average PORO for region {region_number} = {average_poro}"
35+
)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
######################################################################
2+
# This script creates a corner point grid from a Eclipse coord, zcorn
3+
# and actnum configuration.
4+
######################################################################
5+
import rips
6+
from xtgeo.grid3d._egrid import EGrid
7+
from xtgeo.io._file import FileFormat
8+
import numpy as np
9+
10+
grid_filepath = "/home/resinsight/testdata/01_drogon_ahm/realization-0/iter-0/eclipse/model/DROGON-0.EGRID"
11+
12+
name = "DROGON-0 from python"
13+
14+
grid = EGrid.from_file(grid_filepath, fileformat=FileFormat.EGRID)
15+
print("Grid: ", grid)
16+
print("Grid type: ", type(grid))
17+
print("coord: ", grid.coord.shape, grid.coord.dtype)
18+
print("zcorn:", grid.zcorn.shape, grid.zcorn.dtype)
19+
print("actnum: ", grid.actnum.shape, grid.actnum.dtype)
20+
21+
resinsight = rips.Instance.find()
22+
23+
project = resinsight.project
24+
25+
coord = np.ascontiguousarray(grid.coord, dtype=np.float32)
26+
zcorn = np.ascontiguousarray(grid.zcorn, dtype=np.float32)
27+
actnum = np.ascontiguousarray(grid.actnum, dtype=np.int32)
28+
29+
30+
print("coordsv: ", len(coord), type(coord[0]))
31+
print("zcornsv: ", len(zcorn), type(zcorn[0]))
32+
print("actnumsv: ", len(actnum), type(actnum[0]))
33+
34+
35+
print("Grid dimensions: ", grid.dimensions)
36+
nx = grid.dimensions.ncol
37+
ny = grid.dimensions.nrow
38+
nz = grid.dimensions.nlay
39+
40+
project.create_corner_point_grid(name, nx, ny, nz, coord, zcorn, actnum)

docs/rips/PythonExamples/create_intersection.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Load ResInsight Processing Server Client Library
2-
import math, time
32
import rips
43

54
resinsight = rips.Instance.find()
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
###################################################################################
2+
# This example will connect to ResInsight, find bounding box and add a polygon
3+
#
4+
###################################################################################
5+
6+
# Import the ResInsight Processing Server Module
7+
import rips
8+
9+
# Connect to ResInsight
10+
resinsight = rips.Instance.find()
11+
if resinsight is not None:
12+
# Get a list of all cases
13+
cases = resinsight.project.cases()
14+
15+
for c in cases:
16+
print("Case name: " + c.name)
17+
18+
# create a polygon which is same a the bounding box in x and y.
19+
# depth is set to middle of the bounding box
20+
bbox = c.reservoir_boundingbox()
21+
depth = bbox.max_z - ((bbox.max_z - bbox.min_z) / 2.0)
22+
23+
coordinates = []
24+
coordinates.append([bbox.min_x, bbox.min_y, depth])
25+
coordinates.append([bbox.max_x, bbox.min_y, depth])
26+
coordinates.append([bbox.max_x, bbox.max_y, depth])
27+
coordinates.append([bbox.min_x, bbox.max_y, depth])
28+
29+
polygon_collection = resinsight.project.descendants(rips.PolygonCollection)[0]
30+
p = polygon_collection.create_polygon(
31+
name="{} bounding box".format(c.name), coordinates=coordinates
32+
)
33+
print("Coordinates for {}:".format(p.name))
34+
for coord in p.coordinates:
35+
print(coord)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
######################################################################
2+
# This script creates a regular surface for each case
3+
######################################################################
4+
import rips
5+
import math
6+
7+
8+
def create_x_surface(nx, ny):
9+
surface = []
10+
for j in range(ny):
11+
for i in range(nx):
12+
surface.append(float(i))
13+
return surface
14+
15+
16+
def create_wave_surface(nx, ny):
17+
# Fill the coordinate and wave pattern arrays
18+
surface = []
19+
for j in range(ny):
20+
for i in range(nx):
21+
# Create wave pattern - combining multiple sine waves
22+
x = -5 + 10 * i / (nx - 1)
23+
y = -5 + 10 * j / (ny - 1)
24+
offset = (
25+
math.sin(x**2 + y**2)
26+
+ 0.5 * math.sin(2 * x) * math.cos(2 * y)
27+
+ 25.0 * math.cos(5 * x + 2 * y)
28+
)
29+
surface.append(-depth + offset)
30+
return surface
31+
32+
33+
resinsight = rips.Instance.find()
34+
35+
project = resinsight.project
36+
37+
38+
if resinsight is not None:
39+
# Get a list of all cases
40+
cases = resinsight.project.cases()
41+
42+
for c in cases:
43+
bbox = c.reservoir_boundingbox()
44+
depth = bbox.max_z - ((bbox.max_z - bbox.min_z) / 2.0)
45+
46+
origin_x = bbox.min_x
47+
origin_y = bbox.min_y
48+
49+
name = "{} surface".format(c.name)
50+
51+
nx = 200
52+
ny = 100
53+
54+
increment_x = (bbox.max_x - bbox.min_x) / float(nx)
55+
increment_y = (bbox.max_y - bbox.min_y) / float(ny)
56+
57+
surface_collection = resinsight.project.descendants(rips.SurfaceCollection)[0]
58+
59+
# Create a surface at a given depth
60+
s = surface_collection.new_regular_surface(
61+
name=name,
62+
origin_x=origin_x,
63+
origin_y=origin_y,
64+
depth=-depth,
65+
nx=nx,
66+
ny=ny,
67+
increment_x=increment_x,
68+
increment_y=increment_y,
69+
)
70+
71+
# Rotate the resulting surface
72+
s.rotation = 45.0
73+
s.update()
74+
75+
# Add one property
76+
s.set_property("first_property", create_x_surface(nx, ny))
77+
78+
# Add a wave surface
79+
s.set_property("wave", create_wave_surface(nx, ny))
80+
81+
# Use the wave as depth for the surface
82+
s.set_property_as_depth("wave")

docs/rips/PythonExamples/create_wbs_plot.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import grpc
32

43
# Load ResInsight Processing Server Client Library
54
import rips

docs/rips/PythonExamples/error_handling.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,26 @@
44
###################################################################
55

66
import rips
7-
import grpc
87
import tempfile
98

9+
1010
resinsight = rips.Instance.find()
1111

1212
case = None
1313

1414
# Try loading a non-existing case. We should get a grpc.RpcError exception from the server
1515
try:
1616
case = resinsight.project.load_case("Nonsense")
17-
except grpc.RpcError as e:
18-
print(
19-
"Expected Server Exception Received while loading case: ", e.code(), e.details()
20-
)
17+
except rips.RipsError as e:
18+
print("Expected Server Exception Received while loading case: ", e)
2119

22-
# Try loading well paths from a non-existing folder. We should get a grpc.RpcError exception from the server
20+
# Try loading well paths from a non-existing folder. We should get a rips.RipsError exception from the server
2321
try:
2422
well_path_files = resinsight.project.import_well_paths(
2523
well_path_folder="NONSENSE/NONSENSE"
2624
)
27-
except grpc.RpcError as e:
28-
print(
29-
"Expected Server Exception Received while loading wellpaths: ",
30-
e.code(),
31-
e.details(),
32-
)
25+
except rips.RipsError as e:
26+
print("Server Exception Received while loading wellpaths: ", e)
3327

3428
# Try loading well paths from an existing but empty folder. We should get a warning.
3529
try:
@@ -42,7 +36,7 @@
4236
print("Should get warnings below")
4337
for warning in resinsight.project.warnings():
4438
print(warning)
45-
except grpc.RpcError as e:
39+
except rips.RipsError as e:
4640
print("Unexpected Server Exception caught!!!", e)
4741

4842
case = resinsight.project.case(case_id=0)
@@ -54,18 +48,18 @@
5448
try:
5549
case.set_active_cell_property(results, "GENERATED", "POROAPPENDED", 0)
5650
print("Everything went well as expected")
57-
except: # Match any exception, but it should not happen
58-
print("Ooops!")
51+
except Exception as e: # Match any exception, but it should not happen
52+
print("Ooops!", e)
5953

6054
# Add another value, so this is outside the bounds of the active cell result storage
6155
results.append(1.0)
6256

63-
# This time we should get a grpc.RpcError exception, which is a server side error.
57+
# This time we should get a rips.RipsError exception.
6458
try:
6559
case.set_active_cell_property(results, "GENERATED", "POROAPPENDED", 0)
6660
print("Everything went well??")
67-
except grpc.RpcError as e:
68-
print("Expected Server Exception Received: ", e)
61+
except rips.RipsError as e:
62+
print("Server Exception Received: ", e)
6963
except IndexError:
7064
print("Got index out of bounds error. This shouldn't happen here")
7165

@@ -77,7 +71,7 @@
7771
try:
7872
case.set_active_cell_property(results, "GENERATED", "POROAPPENDED", 0)
7973
print("Everything went well??")
80-
except grpc.RpcError as e:
74+
except rips.RipsError as e:
8175
print("Got unexpected server exception", e, "This should not happen now")
8276
except IndexError:
8377
print("Got expected index out of bounds error on client side")

docs/rips/PythonExamples/export_well_path_completions.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#
44
############################################################################
55

6-
import os
76
import rips
87

98
# Load instance

docs/rips/PythonExamples/fishbones_completion.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Load ResInsight Processing Server Client Library
22
import rips
3-
import time
43

54
# Connect to ResInsight instance
65
resinsight = rips.Instance.find()

docs/rips/PythonExamples/headless_plot_export.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import sys
21
import os
32

43
import rips

0 commit comments

Comments
 (0)