Skip to content

Add weighted OPM Flow adjoints for PET - #19

Merged
KriFos1 merged 2 commits into
Python-Ensemble-Toolbox:mainfrom
KriFos1:feature/opm-adjoint
Nov 27, 2026
Merged

KriFos1 merged 2 commits into
Python-Ensemble-Toolbox:mainfrom
KriFos1:feature/opm-adjoint

Conversation

@KriFos1

@KriFos1 KriFos1 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Public API

Adds subsurface.multphaseflow.opm_adjoint.FlowAdjoint(options), an opt-in Flow wrapper for PET LowRankGN (PET #207). adjoint_executable selects the flow_adjoint binary of the official OPM/opm-adjoint, used for forecasts and derivatives. run_vjp(state, weights) records the centre once, replays one weighted objective per nonzero column and returns one (k, n) gradient block per state variable. Each replay uses its own output directory to protect the forward summary. The existing flow wrapper is unchanged.

How it talks to opm-adjoint

  • Each weight column is written as a JSON objective (--adjoint-config): one rate term per observed value, active on the substep that ends on its report. No fork or custom objective is needed.
  • Gradients are read from ADJOINT_GRADIENTS_{PORO,PERM}.csv and the JSON sidecar.
  • A rate term carries the substep length as a factor, and the summary stores time in single precision. Each batch therefore adds one objective-only replay with unit weights, which gives the lengths in double precision; the summary time differences check units, sign and timing.

Scope

Serial METRIC decks; producer oil, water and gas surface rates on any report a simulator step ends on; direct porosity or independent permeability fields (mD or natural log of mD), per active cell or per grid cell. Connection factors that follow the permeability and multisegment wells are differentiated by opm-adjoint. Two cases need changes that are not in opm-adjoint master yet: OPM/opm-adjoint#2 for exact gradients with producers on RESV control, and OPM/opm-adjoint#3 for wells that the schedule defines after the first report step.

Verification

Against unmodified OPM/opm-adjoint master (a02f844, OPM modules of 17-18 November 2026) on its three-cell MODEL_1D_DEBUG tutorial deck:

  • tests/test_flow_vjp.py passes with fixed and with adaptive time steps (several substeps per report): identity and mixed seeds built with PET's DataLayout match central directional differences for porosity and permeability.
  • PET LowRankGN ran end to end through the wrapper on a twin experiment (data misfit 3.07 to 0.13 in six iterations).

Not exercised: state vectors per grid cell on a deck with inactive cells (the tutorial deck has none). The wrapper reads per-term objective values from the flow_adjoint log in the extra replay and raises if that format changes. The README documents the executable and fixture needed to repeat the test.

FlowAdjoint.run_vjp now targets the official OPM/opm-adjoint
flow_adjoint instead of a fork with the linw objective. Each weight
column is written as a JSON objective (--adjoint-config) with one
rate term per observed value, active on the substep that ends on its
report, and the gradients are read from the
ADJOINT_GRADIENTS_{PORO,PERM}.csv files and their JSON sidecar.

A rate term carries the substep length as a factor. The summary
stores time in single precision, so each batch adds one objective-
only replay with unit weights that gives the lengths in double
precision; the summary time differences then check units, sign and
timing.

Reports no longer have to be at midnight, and state vectors may be
given per active cell or per grid cell. Connection factors that
follow the permeability and multisegment wells are left to
opm-adjoint. The run_vjp(state, weights) contract is unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant