Skip to content

Weighted adjoint (Hᵀw) for the JutulDarcy wrapper - #20

Merged
KriFos1 merged 1 commit into
Python-Ensemble-Toolbox:mainfrom
MathiasMNilsen:pr/weighted-adjoint
Nov 27, 2026
Merged

KriFos1 merged 1 commit into
Python-Ensemble-Toolbox:mainfrom
MathiasMNilsen:pr/weighted-adjoint

Conversation

@MathiasMNilsen

@MathiasMNilsen MathiasMNilsen commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What this adds

The wrapper's adjoint mode returns the sensitivity of every requested datum to the parameters, at the cost of one
adjoint solve per datum. A method that only needs the gradient of a scalar, such as the data misfit, does not need
that Jacobian.

With the new option weighted_adjoint = {parameters = [...]} the wrapper does one adjoint solve per member and
returns the gradient of sum_i w_i d_i for given weights w, as a pandas.Series with one full-grid gradient per
parameter. The weights are set as adjoint_weights: a DataFrame laid out like the forward output, or a callable
adjoint_weights(forward_df, member) that is called after each forward run, so that the weights can depend on the
simulated data. With w = C_d⁻¹ (d_obs − d) the result is the gradient of the log-likelihood.

weighted_adjoint cannot be combined with adjoints, and it requires adjoint_mode = "sensitivities".

Also in this commit

  • Cumulative summary vectors change value. In METRIC and FIELD units the cumulative totals (WOPT, FOPT, …)
    came back 86,400 times too large, because JutulDarcy integrates per-day rates over seconds. They are now divided
    by that factor. Anyone who reads cumulative data types from this wrapper gets different numbers after this PR.
  • LAB units are detected.
  • The rate objective matches report times with isapprox, since ministep times are floating-point sums.
  • Both adjoint modes share the set-up of the permeability gradient and of the adjoint storage.

Tests

tests/test_jutul_darcy.py gains tests that the weighted adjoint

  • equals the weighted sum of the per-datum sensitivities,
  • is linear in the weights a callable returns,
  • satisfies a Stein's-lemma check,
  • refuses weights for data that are not reported, and option combinations that cannot work,

and a test that cumulative output is in volume units.

Run locally on the bundled TINY model: all 16 tests of tests/test_jutul_darcy.py pass when the six new ones are run in a process of their own (-k "weighted or cumulative"). Run in one process after the other tests, five of the new ones error at the set-up of their forward-run fixture, inside Julia ("an error occurred while setting an error"); the cause is not known.

Relation to #18 and #19

#18 adds a weighted adjoint to this wrapper through a new method, run_vjp(state, weights), and #19 does the same
for OPM Flow. #18 and this PR give the same quantity, Hᵀw, through different interfaces, and both change
jutul_darcy.py, so they conflict as they stand.

  • Here the gradient comes back from run_fwd_sim together with the forward output, and the weights can be a
    callable of that member's forward output. A method whose weights depend on the member's own prediction, such as
    the misfit weights C_d⁻¹ (d_obs − d), then costs one forward and one adjoint run per member.
  • In Add weighted JutulDarcy adjoints for PET #18 the weights are given before the call, several weight columns are solved against one forward simulation
    that the call runs itself, and the supported data are metric producer rates.

This is opened as a draft so that the two can be reconciled before either is merged.

Related

The PET side, which reads this adjoint and supplies the weights of the data misfit:
Python-Ensemble-Toolbox/PET#205.

🤖 Generated with Claude Code

New `weighted_adjoint` option: one adjoint solve per member returns the
gradient of sum_i w_i d_i for given data weights (DataFrame or callable
(forward_df, member)). With w = C_d^-1 (d_obs - d) this is minus the
gradient of the data misfit, as needed by SVGD. The objective is
cross-checked against sum(w d) before solving.

Also:
- Fix cumulative summary vectors (WOPT, ...) being 86400x too large in
  METRIC/FIELD (JutulDarcy integrates per-day rates over seconds). This
  changes the values returned for cumulative data types.
- Detect LAB units.
- Use isapprox for the report-time match in the rate objective.
- Share perm-gradient and adjoint-storage setup between both adjoint modes.
- Tests: agreement with per-datum sensitivities, linearity in callable
  weights, Stein's lemma check, and option validation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

2 participants