Repository navigation
Check that the OPM adjoint resolves every chain-rule term - #22
Merged
KriFos1 merged 5 commits intoNov 27, 2026
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Builds on #19 (
feature/opm-adjoint); until that is merged the diff here also shows its two commits. The three commits of this PR areabbc2ba,e9c0a87and1b2b4ff.Why
FlowAdjoint.run_vjpreturned the sensitivity to the field a state variable is written to, and nothing told whether the deck moved anything else with it. A deck withCOPY PERMX PERMYlost the y part of the gradient without an error: the replayed objective still matched the forward summary.Public API
FlowAdjoint.check_chain_rule(state, weights=None, step=1e-2, rtol=0.05, seed=0). Returns a report withties,derived,approximate,unresolvedandfinite_difference; raisesValueErroron a termrun_vjpwould miss or a gradient that differs from finite differences.--enable-dry-run=true. The rendered text may differ only in the keyword of the variable's own field, and Flow'sINITarrays only in that field, in what Flow derives from it (PORV,TRAN*), and in permeability axes tied to it by a constant factor.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 getok = Noneand a warning.FlowAdjoint.run_vjp(state, weights). Same signature and return value. It nowCOPY,MULTIPLY) to its gradient;RPTONLYin the deck of its recorded run, so decks with that keyword and several substeps per report are no longer refused. Forecasts are unchanged.FlowAdjoint(options).sim_flagmay 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.FlowAdjoint.chain_rule, the last report (Nonebefore the first check).subsurface.multphaseflow.vjp:weighted_sums(prediction, reports, cells, seeds)andfinite_difference_check(forward, gradients, state, sums, step, rtol, seed), the simulator-independent part of the check.FlowAdjointsection describes the check, the tied axes,RPTONLYand the solver flags.flowand the JutulDarcy wrapper are untouched.What the check does not see
The structural part compares the rendered deck and Flow's
INITarrays, plus a scan ofCOMPDAT/COMPDATLfor 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_DEBUGtutorial deck,tests/test_flow_vjp.py: 7 passed, and each of the three commits passes on its own (3, 6, 7).COPY PERMY PERMXandMULTIPLY PERMX 0.5: the tie is found with factor 0.5 and the gradient matches finite differences.POROfrompermxis refused.RPTONLYand adaptive steps the gradient equals the one without the keyword; without the fix the run is refused.INCLUDE.On a 2,500-cell five-spot deck with
COPY PERMX PERMY,COPY PERMX PERMZandMULTIPLY PERMZ 0.001, 60 rate data and onelog_permxfield: 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;umfpackdid 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.