Skip to content

Check that the OPM adjoint resolves every chain-rule term - #22

Merged
KriFos1 merged 5 commits into
Python-Ensemble-Toolbox:mainfrom
KriFos1:feature/opm-chain-rule-check
Nov 27, 2026
Merged

KriFos1 merged 5 commits into
Python-Ensemble-Toolbox:mainfrom
KriFos1:feature/opm-chain-rule-check

Conversation

@KriFos1

@KriFos1 KriFos1 commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Builds on #19 (feature/opm-adjoint); until that is merged the diff here also shows its two commits. The three commits of this PR are abbc2ba, e9c0a87 and 1b2b4ff.

Why

FlowAdjoint.run_vjp returned the sensitivity to the field a state variable is written to, and nothing told whether the deck moved anything else with it. A deck with COPY PERMX PERMY lost the y part of the gradient without an error: the replayed objective still matched the forward summary.

Public API

  • New: FlowAdjoint.check_chain_rule(state, weights=None, step=1e-2, rtol=0.05, seed=0). Returns a report with ties, derived, approximate, unresolved and finite_difference; raises ValueError on a term run_vjp would miss or a gradient that differs from finite differences.
    • Structural part: each variable is perturbed, the deck rendered again and read by Flow with --enable-dry-run=true. The rendered text may differ only in the keyword of the variable's own field, and Flow's INIT arrays only in that field, in what Flow derives from it (PORV, TRAN*), and in permeability axes tied to it by a constant factor.
    • With weights: the gradients are compared with central differences of the weighted sums along an alternating and a uniform direction at two step sizes, through template and simulator (four simulations per variable and direction). Rows the two step sizes disagree on get ok = None and a warning.
  • Changed: FlowAdjoint.run_vjp(state, weights). Same signature and return value. It now
    • runs the structural check before its first batch (two dry runs per variable) and raises on an unresolved term;
    • adds permeability axes the deck ties to the variable by a constant factor (COPY, MULTIPLY) to its gradient;
    • comments out RPTONLY in the deck of its recorded run, so decks with that keyword and several substeps per report are no longer refused. Forecasts are unchanged.
  • Changed: FlowAdjoint(options). sim_flag may carry opm-adjoint's solver flags, such as --adjoint-linear-solver=umfpack. Only the flags the wrapper sets itself are refused: --adjoint-file, --adjoint-save, --adjoint-mode, --adjoint-config, --adjoint-objective, --output-dir, --enable-storage-cache.
  • New attribute: FlowAdjoint.chain_rule, the last report (None before the first check).
  • New in subsurface.multphaseflow.vjp: weighted_sums(prediction, reports, cells, seeds) and finite_difference_check(forward, gradients, state, sums, step, rtol, seed), the simulator-independent part of the check.
  • README: the FlowAdjoint section describes the check, the tied axes, RPTONLY and the solver flags.

flow and the JutulDarcy wrapper are untouched.

What the check does not see

The structural part compares the rendered deck and Flow's INIT arrays, plus a scan of COMPDAT/COMPDATL for connection factors left to Flow. Dependencies inside Flow that show in neither (non-neighbour connections, aquifers, threshold pressures) are caught only by the finite-difference part, which costs simulations and is therefore not run automatically.

Verification

Against unmodified OPM/opm-adjoint master (a02f844) on its MODEL_1D_DEBUG tutorial deck, tests/test_flow_vjp.py: 7 passed, and each of the three commits passes on its own (3, 6, 7).

  • A deck where the flow direction is reached only through COPY PERMY PERMX and MULTIPLY PERMX 0.5: the tie is found with factor 0.5 and the gradient matches finite differences.
  • A template that writes PORO from permx is refused.
  • With RPTONLY and adaptive steps the gradient equals the one without the keyword; without the fix the run is refused.
  • Defaulted connection factors are found through an INCLUDE.

On a 2,500-cell five-spot deck with COPY PERMX PERMY, COPY PERMX PERMZ and MULTIPLY PERMZ 0.001, 60 rate data and one log_permx field: the check finds both ties, and gradient and finite differences agree within 0.03 % (alternating direction) and 0.12 % (uniform direction) at a homogeneous point, and within 0.2–1.3 % at a heterogeneous one with random weights. With adaptive time stepping the uniform direction was inconclusive there; with fixed report steps and tight tolerances it is not. The default iterative adjoint solver aborted on one single-datum objective of that deck; umfpack did not, which is what the solver-flag change is for.

Not exercised: state vectors per grid cell on a deck with inactive cells, ties of a porosity field, and COMPDATL. The repository has no CI, so nothing ran beyond the local tests above.

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.
FlowAdjoint refused every --adjoint- flag in sim_flag, so the solver
of the transposed systems could not be chosen. On a 2,500-cell deck
the default iterative solver stopped short of its tolerance for a
single-datum objective and aborted the sweep; the direct solver
(--adjoint-linear-solver=umfpack) did not.

Only the flags the wrapper sets itself are refused now: the archive,
mode, objective, output and storage-cache flags.
run_vjp returned the sensitivity to the field a state variable is
written to, and nothing told whether the deck moved anything else
with it. A deck with COPY PERMX PERMY lost the y part of the gradient
without an error, and the replayed objective still matched.

FlowAdjoint.check_chain_rule(state, weights=None) perturbs each
variable, renders the deck again and lets Flow read it without
simulating. The rendered text may differ only in the keyword of the
variable's own field, and Flow's INIT arrays only in that field, in
what Flow derives from it, and in permeability axes tied to it by a
constant factor. run_vjp adds the tied axes to the gradient, runs the
check before its first batch and raises on anything else. Connection
factors left for Flow to compute are reported as approximate.

With weights, the gradients are also compared with central
differences of the weighted sums along an alternating and a uniform
direction at two step sizes, through template and simulator. A
mismatch raises; rows the step sizes disagree on are marked
inconclusive with a warning.

vjp.weighted_sums and vjp.finite_difference_check hold the part that
does not depend on the simulator.
run_vjp takes the substep lengths from the summary of its recorded
run. A deck with RPTONLY writes the summary at report steps only, so
with several substeps per report the run was refused. The keyword is
commented out in the deck of the recorded run; forecasts are
unchanged.
@KriFos1
KriFos1 marked this pull request as ready for review November 27, 2026 09:12
@KriFos1
KriFos1 merged commit 6f17a56 into Python-Ensemble-Toolbox:main Nov 27, 2026
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