Skip to content

Add option to update DensityDiffusionAntuono between time steps - #1301

Open
efaulhaber wants to merge 9 commits into
trixi-framework:mainfrom
efaulhaber:frozen-diffusion
Open

efaulhaber wants to merge 9 commits into
trixi-framework:mainfrom
efaulhaber:frozen-diffusion

Conversation

@efaulhaber

@efaulhaber efaulhaber commented Aug 24, 2026 •

Copy link
Copy Markdown
Member

This strategy is explained in Equation (4.29) of Antuono et al., "Numerical diffusive terms in weakly-compressible SPH schemes" (2012).

@efaulhaber efaulhaber self-assigned this Aug 24, 2026
@codecov

codecov Bot commented Aug 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.77419% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 90.51%. Comparing base (02fc220) to head (19e416d).
⚠️ Report is 10 commits behind head on main.

Files with missing lines Patch % Lines
...fluid/weakly_compressible_sph/density_diffusion.jl 96.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1301      +/-   ##
==========================================
+ Coverage   90.50%   90.51%   +0.01%     
==========================================
  Files         141      142       +1     
  Lines       11409    11433      +24     
==========================================
+ Hits        10326    10349      +23     
- Misses       1083     1084       +1     
Flag Coverage Δ
total 90.52% <96.77%> (+0.01%) ⬆️
unit 77.69% <48.38%> (-0.07%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@efaulhaber efaulhaber left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codex Review

The patch adds an option to update Antuono density-diffusion data between time steps through UpdateCallback, including documentation, tests, and initialized caches.

This is an AI-generated code review. Please verify the findings and summary before acting on them.
Review generated by codex-pr-review

Comment thread src/callbacks/update.jl Resolved
Comment thread test/examples/examples_fluid.jl Resolved
@efaulhaber
efaulhaber marked this pull request as ready for review August 24, 2026 10:19
@efaulhaber
efaulhaber requested a review from svchb August 24, 2026 10:25
@svchb svchb added the enhancement New feature or request label Oct 1, 2026
@svchb

svchb commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

PR #1301 review: callback-updated Antuono density diffusion

Recommendation: Request changes. The new update_everystage=false mode can silently apply one particle’s cached density gradient to another after sorting. The mode also needs a direct regression test and clearer documentation of its update schedule, callback ordering, and accuracy tradeoff.

Reviewed PR #1301 at 19e416d6f. The review covers behavior introduced by the new option.

P1 — Sorting can assign cached gradients to the wrong particles

SortingCallback permutes coordinates, velocities, densities, and pressures. It does not permute the renormalized density-gradient cache, which the Antuono diffusion term reads by particle index. Stage updates rebuild the cache before using it; frozen mode keeps the old ordering until the next refresh.

I reproduced this with CallbackSet(UpdateCallback(), SortingCallback(interval=1)) on a 20-particle WCSPH case with nonuniform density. At the same particle state, RHS components differed by up to 7.69 before and after refreshing the cache. Putting sorting first avoided the mismatch in that probe, but ordering alone cannot fix a sort that fires between refreshes.

Requested change: Apply the particle permutation to the frozen gradient cache, or reject the combination with a clear error. If supported, test a sort that occurs between refreshes and verify that the density RHS is unchanged after undoing the permutation.

P2 — Test that the gradient is held and refreshed on schedule

The vortex-street regression is a useful integration smoke test, but its final nonzero-gradient assertion does not establish when the gradient was updated or whether it is correct. A valid gradient can be zero, and !iszero(NaN) passes.

Requested change: Use a small fixture with a changing, nonuniform density field and UpdateCallback(interval=2). Verify that initialization produces a finite gradient, that a step without a scheduled refresh leaves the cache unchanged, and that the scheduled refresh matches a reconstruction from the current state. If retaining the activation assertion, identify the newly activated slot and check its gradient rather than requiring every active particle’s gradient to be nonzero.

P2 — Document the numerical tradeoff and callback order

What is held fixed. In frozen mode, the diffusion term still uses the current stage’s densities and positions, but it uses the renormalized gradients from the latest refresh. In a fixed-step, 20-particle WCSPH probe with RDPK3SpFSAL35, timestep halving against a fine-step stage-updated reference gave observed orders of 3.01–3.05 with stage updates and 0.94–0.99 with frozen gradients. This is one case, but it shows that the approximation can reduce temporal order even with a higher-order integrator.

Initialization order. Callbacks that evaluate the RHS at initialization can read the cache before UpdateCallback fills it. I reproduced this with PostprocessCallback: when it ran first, the RHS read by a custom quantity differed from the initialized RHS by up to 18.75. Putting UpdateCallback first eliminated the difference. The vortex example currently puts solution saving first, while the new test disables saving. Document the required order and show it in a configuration that uses frozen gradients and saves the initial solution.

Suggested docstring wording:

With update_everystage=false, reconstruct the renormalized density gradient at initialization and whenever UpdateCallback runs. By default, the callback refreshes it after each accepted step. The diffusion term uses the latest cached gradient between refreshes. This reduces the number of gradient reconstructions but can reduce temporal accuracy. Place UpdateCallback before callbacks that evaluate the initial RHS, such as SolutionSavingCallback and PostprocessCallback.

The changelog should name update_everystage=false and say that it freezes the renormalized density gradient between callback refreshes.

@svchb svchb left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See AI review

@efaulhaber

Copy link
Copy Markdown
Member Author

See AI review

  1. Sorting. SortingCallback now also permutes the frozen renormalized density gradient. The same problem already existed for shifting, so delta_v for PST with update_everystage=false is now also permuted. It also permutes the particle masses, which were never sorted before (had a TODO comment). I added a test to check that the RHS after sorting equals the permuted RHS from before sorting.
  2. Update tests. There's a new unit test on a small patch with a changing, non-uniform density and UpdateCallback(interval=2). It compares the cache against an independent reconstruction of the gradient that is hardcoded in the tests without NHS. It checks that initialization gives the correct gradient, that a step without a scheduled refresh leaves the cache unchanged, and that the update matches the current state. The vortex-street test now finds the particles activated in the last update and checks that their gradient matches a recomputation from the final state, instead of only checking for nonzero values.
  3. Initialization order. "Document the required order" is not a nice API. Instead, I fixed callbacks before the UpdateCallback: SolutionSavingCallback and PostprocessCallback initialize the frozen caches (DD gradient, PST delta_v, TLSPH averaged velocity) before they evaluate the RHS at initialization. I already implemented this for TLSPH averaged velocity and it was already broken for PST. A new test puts a PostprocessCallback first and checks that it sees the same RHS as after full initialization.
  4. Docs. The docstring now explains that the gradient is computed at initialization and on each UpdateCallback and frozen in between. It also says this reduces the order of the time integration scheme, so a slightly smaller time step might be necessary. I don't think the accuracy discussion makes sense here because the temporal error is negligible in practice. We don't use high order time integration for accuracy but for stability.

@efaulhaber
efaulhaber requested a review from svchb November 25, 2026 09:41

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants