Add VolumeConservationLoss to physicsnemo.experimental.losses - #1809
Closed
wdyab wants to merge 2 commits into
Closed
Add VolumeConservationLoss to physicsnemo.experimental.losses#1809wdyab wants to merge 2 commits into
wdyab wants to merge 2 commits into
Conversation
wdyab
force-pushed
the
nof/pr3c-volume-conservation-loss
branch
from
July 8, 2026 18:49
fbebf28 to
6ee25cd
Compare
wdyab
force-pushed
the
nof/pr3c-volume-conservation-loss
branch
4 times, most recently
from
July 14, 2026 09:10
5ba6c23 to
c9e38cc
Compare
wdyab
marked this pull request as ready for review
July 14, 2026 09:11
Contributor
Greptile SummaryThis PR adds experimental weak-physics losses for conservation and derivative regularization. The main changes are:
Important Files Changed
Reviews (2): Last reviewed commit: "Add VolumeConservationLoss to physicsnem..." | Re-trigger Greptile |
Adds a generic, dimension-agnostic spatial-derivative regularization loss that compares the interior (non-periodic) central-difference gradients of a prediction and a target on a non-uniform grid (per-axis cell widths), using an injectable data-fitting metric from physicsnemo.metrics.general and optional inactive-cell masking for sparse grids. The reusable central_difference and cell_centre_distance helpers are also exported. The existing physicsnemo.nn.functional.derivatives operators are periodic, coordinate-based, and single-field, so they do not cover this batched, non-periodic, masked regularization regime; this is documented in the module. Pre-commit: markdownlint and import-linter were skipped (SKIP=...) as they fail only on local environment artifacts (markdownlint's Node/npm cannot write to the sandbox tmp cache; import-linter reports pre-existing external-import violations with 0 file violations, which pass in upstream CI). ruff-check, ruff-format, interrogate, license header, and large-file hooks ran and passed. Signed-off-by: wdyab <wdyab@nvidia.com>
Adds a generic, dimension-agnostic weak-conservation loss that compares the (optionally volume-weighted) spatial integral of a prediction and a target at each step of a trailing feature/time axis, using an injectable data-fitting metric from physicsnemo.metrics.general and optional inactive-cell masking for sparse grids. Named "volume conservation" (not "mass conservation") because it enforces conservation of a volume-weighted spatial integral; no material density or porosity is applied, so it is meaningful only for quantities whose volume integral is approximately conserved. Pre-commit: markdownlint and import-linter were skipped (SKIP=...) as they fail only on local environment artifacts (markdownlint's Node/npm cannot write to the sandbox tmp cache; import-linter reports pre-existing external-import violations with 0 file violations, which pass in upstream CI). ruff-check, ruff-format, interrogate, license header, and large-file hooks ran and passed. Signed-off-by: wdyab <wdyab@nvidia.com>
wdyab
force-pushed
the
nof/pr3c-volume-conservation-loss
branch
from
July 14, 2026 09:26
c9e38cc to
c34cc5d
Compare
Contributor
Author
|
@peterdsharpe @mnabian @melo-gonzo for your review |
Contributor
Author
|
Closing per the offline discussion with the maintainers: these regularization losses will live in the reservoir-simulation example recipe downstream, and the generic finite-difference need will be addressed by extending physicsnemo.nn.functional.derivatives (non-periodic + batched) rather than adding a parallel implementation. Thanks for the reviews! |
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.
Summary
Adds
VolumeConservationLosstophysicsnemo.experimental.losses: a generic,dimension-agnostic weak-conservation loss. It compares the (optionally
volume-weighted) spatial integral of
predandtargetat each step of atrailing feature/time axis, using a dependency-injected data-fitting
metriccallable and an optional inactive-cell mask.
The default
metricismse(always available); anyphysicsnemo.metrics.generalfunction — or a relative-error metric such as the one proposed in #1746 once it
lands — can be dependency-injected. Inputs are validated (pred/target shape,
cell_volumesshape, and static/per-samplemaskshape) under atorch.compiler.is_compiling()guard; the masking path uses static-shapeweighting (no boolean indexing), so it is
torch.compile-compatible.Named volume conservation (not "mass conservation") on purpose: it conserves
a volume-weighted spatial integral, not true mass — no material density or
porosity is applied.
Closes #1806
Test plan
test/experimental/losses/test_volume_conservation.py: analytic 2D/3Dreservoir integrals (known totals), dependency-injected metric sweep,
volume-weighted vs uniform, multi-timestep, masking (static + per-sample),
gradient flow, scale invariance, uniform-default, and error paths
(pred/target, cell_volumes, and mask shape mismatches).
interrogate100%,ruffcheck+format clean, doctest, license headers, CHANGELOG.