Skip to content

Edit a loaded grid

examples/grid_edit.py
"""Edit a loaded grid: cut the permeability tenfold in a box of cells, and check it in Python.
``m.apply`` appends statements to the model's grid program (the deck's own edits are kept), and
``m.array`` reads any array after the program has run, so the effect is checked in Python before
the deck is written. ``ps.Box`` is one-based and inclusive, as the deck's own boxes are, and
``ps.Box(...).index`` turns it into a NumPy index.
Both models are run, so the field's oil rate at the end shows what the tighter box costs.
Flow keywords it exercises: MULTIPLY with a box (one card per statement, after the deck's own),
on SPE1's grid.
Run it with ``python examples/grid_edit.py [DECK]``. DECK defaults to SPE1 in the opm-tests corpus
at ``opm-tests/``; it writes ``out/grid_edit/``.
"""
import sys
import petroscript as ps
path = sys.argv[1] if len(sys.argv) > 1 else "opm-tests/spe1/SPE1CASE1.DATA"
m = ps.load(path)
box = ps.Box(i=(4, 7), j=(4, 7), k=(1, 3))
tighter = m.apply(*(ps.grid.Scale(direction, by=0.1, cells=box)
for direction in ("permeability_x", "permeability_y", "permeability_z")))
before = m.array("permeability_x")[box.index] # cells (nx, ny, nz), in the model's units
after = tighter.array("permeability_x")[box.index]
print("permeability_x in the box, before:", sorted(set(before.ravel().tolist())))
print("permeability_x in the box, after: ", sorted(set(after.ravel().tolist())))
assert (after == before * 0.1).all()
for name, model in (("as loaded", m), ("tighter", tighter)):
folder = "out/grid_edit/" + name.replace(" ", "_")
deck = ps.compile(model)
deck.write(folder)
if ps.simulators():
oil = deck.run(folder=folder + "/run").field("oil_production_rate")
rate, unit = oil["value"][-1], oil["unit"][-1]
print(f"{name}: the field's oil rate at the end is {rate:.0f} {unit}")
if not ps.simulators():
print("no simulator found (set PETROSCRIPT_FLOW, or put flow on PATH); the decks are in "
"out/grid_edit")

PetroScript is licensed under the Apache License 2.0. © 2026 Blauweiss EDV LLC. PetroScript by Blauweiss AI.For AI agents: llms.txt