Skip to content

[SPH] 1Pn disc-sink interactions - #1923

Open
AugustinDart wants to merge 4 commits into
Shamrock-code:mainfrom
AugustinDart:interaction_PN_sink_disk
Open

[SPH] 1Pn disc-sink interactions#1923
AugustinDart wants to merge 4 commits into
Shamrock-code:mainfrom
AugustinDart:interaction_PN_sink_disk

Conversation

@AugustinDart

Copy link
Copy Markdown
Contributor

-1PN for Sink-disk interaction added
-Lense thirring for Sink-Disk interaction modified

@github-actions

Copy link
Copy Markdown
Contributor

Thanks @AugustinDart for opening this PR!

You can do multiple things directly here:
1 - Comment pre-commit.ci run to run pre-commit checks.
2 - Comment pre-commit.ci autofix to apply fixes.
3 - Add label autofix.ci to fix authorship & pre-commit for every commit made.
4 - Add label light-ci to only trigger a reduced & faster version of the CI (need the full one before merge).
5 - Add label trigger-ci to create an empty commit to trigger the CI.

Once the workflow completes a message will appear displaying informations related to the run.

Also the PR gets automatically reviewed by gemini, you can:
1 - Comment /gemini review to trigger a review
2 - Comment /gemini summary for a summary
3 - Tag it using @gemini-code-assist either in the PR or in review comments on files

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a new 1PN external force model (AddForce1PN) and updates the Lense-Thirring force model to support configurable central positions and velocities. Feedback focuses on preventing potential division-by-zero errors in both force modules when a particle is exactly at the central position. Additionally, it is recommended to update the particle accretion logic to respect the new Lense-Thirring central position, and to fix a minor variable alignment issue.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread src/shammodels/common/include/shammodels/common/modules/AddForce1PN.hpp Outdated
Comment on lines +81 to 85
Tvec r_a = xyz[gid] - cpos;
Tvec v_a = vxyz[gid] - cvel;
Tscal abs_ra = sycl::length(r_a);
Tscal abs_ra_2 = abs_ra * abs_ra;
Tscal abs_ra_3 = abs_ra_2 * abs_ra;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Similar to the 1PN force, if a particle is exactly at the central position cpos, abs_ra will be 0, leading to a division by zero when calculating omega_a. Adding a guard to skip calculation when abs_ra is extremely small prevents numerical instability. Additionally, since this relative position and distance calculation logic is duplicated across multiple force modules, consider refactoring it into a shared helper function or lambda to improve readability and maintainability.

Suggested change
Tvec r_a = xyz[gid] - cpos;
Tvec v_a = vxyz[gid] - cvel;
Tscal abs_ra = sycl::length(r_a);
Tscal abs_ra_2 = abs_ra * abs_ra;
Tscal abs_ra_3 = abs_ra_2 * abs_ra;
Tvec r_a = xyz[gid] - cpos;
Tscal abs_ra = sycl::length(r_a);
if (abs_ra < 1e-12) {
return;
}
Tvec v_a = vxyz[gid] - cvel;
Tscal abs_ra_2 = abs_ra * abs_ra;
Tscal abs_ra_3 = abs_ra_2 * abs_ra;
References
  1. Refactor duplicated logic into a helper function or lambda to improve readability and maintainability.

Comment on lines 534 to +537
auto set_central_pos
= register_constant_set<Tvec>(solver_graph, prefix_central_pos, [&]() {
return Tvec{0, 0, 0}; // no support for offset yet
return ext_force->central_pos;
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Since LenseThirring now supports a non-zero central_pos, please make sure to also update point_mass_accrete_particles() (around line 678 in this file) to use ext_force->central_pos instead of the hardcoded {0, 0, 0}. Otherwise, accretion will still occur at the origin instead of the actual central position.

Tscal c = edges.constant_c.data;
Tscal cmass = edges.central_mass.data;
Tvec cpos = edges.central_pos.data;
Tvec cvel = edges.central_vel.data;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The variable cvel is not aligned with the other variable declarations in this block. Aligning the assignment operator improves readability.

Suggested change
Tvec cvel = edges.central_vel.data;
Tvec cvel = edges.central_vel.data;

@tdavidcl tdavidcl changed the title Interaction 1Pn added [SPH] 1Pn disc-sink interactions Jul 16, 2026
@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Adds the PN_1PN external force model, JSON support, distributed acceleration computation, SPH solver-graph integration, and Python configuration bindings. Lense–Thirring forces now accept central position and velocity, using relative velocity during acceleration evaluation.

Changes

External force models

Layer / File(s) Summary
Force contracts and JSON representation
src/shammodels/common/include/shammodels/common/ExtForceConfig.hpp
Defines PN_1PN, extends Lense–Thirring state, adds registration helpers, and updates JSON serialization and deserialization.
External-force computation
src/shammodels/common/include/shammodels/common/modules/AddForce1PN.hpp, src/shammodels/common/include/shammodels/common/modules/AddForceLenseThirring.hpp
Adds the distributed 1PN acceleration node and computes Lense–Thirring acceleration from velocity relative to the central body.
SPH solver-graph integration
src/shammodels/sph/src/modules/ExternalForces.cpp
Registers 1PN constants and solver-graph operations, enables required constants, and wires central velocity into Lense–Thirring evaluation.
Solver and Python configuration APIs
src/shammodels/sph/include/shammodels/sph/SolverConfig.hpp, src/shammodels/sph/src/pySPHModel.cpp
Adds 1PN configuration methods and exposes the expanded Lense–Thirring parameters through the solver and Python APIs.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant PythonConfig
  participant SolverConfig
  participant ExtForceConfig
  participant ExternalForces
  participant AddForce1PN
  participant AccelerationField

  PythonConfig->>SolverConfig: add_ext_force_1pn(central_mass, central_pos, central_vel)
  SolverConfig->>ExtForceConfig: add_1pn(...)
  ExtForceConfig-->>ExternalForces: PN_1PN variant
  ExternalForces->>AddForce1PN: wire constants and field edges
  AddForce1PN->>AccelerationField: accumulate 1PN acceleration
Loading

Suggested reviewers: y-lapeyre

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and matches the main change: adding 1PN and Lense-Thirring sink-disk interactions.
Description check ✅ Passed The description is directly related to the changes, mentioning 1PN and Lense-Thirring sink-disk interaction updates.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@tdavidcl tdavidcl added the draft label Jul 16, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/shammodels/common/include/shammodels/common/modules/AddForce1PN.hpp`:
- Around line 65-110: Use a single dimensionally consistent radius clamp in the
1PN kernel around the acceleration calculation in AddForce1PN: clamp r once into
r_safe, then derive and consistently use r_safe² and r_safe³ for all
radius-dependent denominators, preserving the existing acceleration behavior. In
AddForceLenseThirring, clamp abs_ra before computing its powers so the
central-position case cannot produce NaN acceleration; apply the change in
src/shammodels/common/include/shammodels/common/modules/AddForce1PN.hpp lines
65-110 and
src/shammodels/common/include/shammodels/common/modules/AddForceLenseThirring.hpp
lines 80-89.

In `@src/shammodels/sph/src/modules/ExternalForces.cpp`:
- Around line 488-500: Update the deferred getters registered in
ExternalForces.cpp at lines 488-500 to capture the external-force data by value
rather than retaining the block-local ext_force reference; apply this to the 1PN
mass, position, and velocity getters. Also update lines 536-541 to value-capture
the Lense–Thirring position and velocity getters, preserving their existing
returned fields.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 24f44f40-919e-4158-ace0-8304d1519182

📥 Commits

Reviewing files that changed from the base of the PR and between c48c1bb and 8f92590.

📒 Files selected for processing (6)
  • src/shammodels/common/include/shammodels/common/ExtForceConfig.hpp
  • src/shammodels/common/include/shammodels/common/modules/AddForce1PN.hpp
  • src/shammodels/common/include/shammodels/common/modules/AddForceLenseThirring.hpp
  • src/shammodels/sph/include/shammodels/sph/SolverConfig.hpp
  • src/shammodels/sph/src/modules/ExternalForces.cpp
  • src/shammodels/sph/src/pySPHModel.cpp

Comment on lines +65 to +110
Tscal eps_gr = 1e-10; // small value to avoid division by zero
Tscal cmass = edges.central_mass.data;
Tvec cpos = edges.central_pos.data;
Tvec cvel = edges.central_vel.data;
Tscal GM = cmass * G;


sham::distributed_data_kernel_call(
shamsys::instance::get_compute_scheduler_ptr(),

sham::DDMultiRef{
edges.spans_positions.get_spans(),
edges.spans_velocities.get_spans()
},

sham::DDMultiRef{
edges.spans_accel_ext.get_spans()
},

edges.sizes.indexes,

[cpos, cvel, GM, c, eps_gr](u32 gid,
const Tvec *xyz,
const Tvec *vxyz,
Tvec *axyz_ext) {

Tvec r_a = xyz[gid] - cpos;
Tvec v_a = vxyz[gid] - cvel;

Tscal r = sycl::length(r_a);

Tvec r_hat = r_a / (r+eps_gr);

Tscal v2 = sham::dot(v_a, v_a);

Tscal vr = sham::dot(v_a, r_hat);


Tvec acc_1PN =
-GM / (r * r + eps_gr)
*
(
(
v2 / (c * c)
-
4 * GM / ((r + eps_gr) * c * c)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Use one dimensionally consistent radius-clamping strategy. The current kernels either mix length and squared-length epsilon terms or permit division by zero.

  • src/shammodels/common/include/shammodels/common/modules/AddForce1PN.hpp#L65-L110: clamp r once and use r_safe, r_safe², and r_safe³ consistently.
  • src/shammodels/common/include/shammodels/common/modules/AddForceLenseThirring.hpp#L80-L89: clamp abs_ra before calculating its powers to prevent NaN acceleration at the central position.
📍 Affects 2 files
  • src/shammodels/common/include/shammodels/common/modules/AddForce1PN.hpp#L65-L110 (this comment)
  • src/shammodels/common/include/shammodels/common/modules/AddForceLenseThirring.hpp#L80-L89
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/shammodels/common/include/shammodels/common/modules/AddForce1PN.hpp`
around lines 65 - 110, Use a single dimensionally consistent radius clamp in the
1PN kernel around the acceleration calculation in AddForce1PN: clamp r once into
r_safe, then derive and consistently use r_safe² and r_safe³ for all
radius-dependent denominators, preserving the existing acceleration behavior. In
AddForceLenseThirring, clamp abs_ra before computing its powers so the
central-position case cannot produce NaN acceleration; apply the change in
src/shammodels/common/include/shammodels/common/modules/AddForce1PN.hpp lines
65-110 and
src/shammodels/common/include/shammodels/common/modules/AddForceLenseThirring.hpp
lines 80-89.

Comment on lines +488 to +500
auto set_cmass = register_constant_set<Tscal>(solver_graph, prefix_cmass, [&]() {
return ext_force->central_mass;
});

auto set_central_pos
= register_constant_set<Tvec>(solver_graph, prefix_central_pos, [&]() {
return ext_force->central_pos;
});

auto set_central_vel
= register_constant_set<Tvec>(solver_graph, prefix_central_vel, [&]() {
return ext_force->central_vel;
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🔴 Critical | ⚡ Quick win

Capture external-force constants by value. register_constant_set stores each getter for deferred execution, while [&] retains a reference to the block-local ext_force pointer variable after its lifetime ends.

  • src/shammodels/sph/src/modules/ExternalForces.cpp#L488-L500: value-capture the 1PN mass, position, and velocity.
  • src/shammodels/sph/src/modules/ExternalForces.cpp#L536-L541: value-capture the Lense–Thirring position and velocity.
📍 Affects 1 file
  • src/shammodels/sph/src/modules/ExternalForces.cpp#L488-L500 (this comment)
  • src/shammodels/sph/src/modules/ExternalForces.cpp#L536-L541
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/shammodels/sph/src/modules/ExternalForces.cpp` around lines 488 - 500,
Update the deferred getters registered in ExternalForces.cpp at lines 488-500 to
capture the external-force data by value rather than retaining the block-local
ext_force reference; apply this to the 1PN mass, position, and velocity getters.
Also update lines 536-541 to value-capture the Lense–Thirring position and
velocity getters, preserving their existing returned fields.

@y-lapeyre
y-lapeyre self-requested a review July 16, 2026 14:20

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
src/shammodels/sph/include/shammodels/sph/SolverConfig.hpp (2)

122-123: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use lower_case names for the new TVI C++ identifiers.

The new internal identifiers use C_1_fluid and C_delta_v, violating the repository naming rule. Rename them to c_1_fluid and c_delta_v; preserve the JSON/Python names if they are external compatibility keys.

  • src/shammodels/sph/include/shammodels/sph/SolverConfig.hpp#L122-L123: rename the new MonofluidTVI members.
  • src/shammodels/sph/include/shammodels/sph/SolverConfig.hpp#L142-L143: rename the setter parameters and references.
  • src/shammodels/sph/src/pySPHModel.cpp#L273-L274: rename the binding-lambda parameters while retaining public py::arg names if required.

As per coding guidelines, C++ variables, parameters, and members must use lower_case.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/shammodels/sph/include/shammodels/sph/SolverConfig.hpp` around lines 122
- 123, Rename the new MonofluidTVI C++ members in
src/shammodels/sph/include/shammodels/sph/SolverConfig.hpp at lines 122-123 to
c_1_fluid and c_delta_v, and update the corresponding setter parameters and
references at lines 142-143. In src/shammodels/sph/src/pySPHModel.cpp lines
273-274, rename the binding-lambda parameters consistently while preserving any
required public py::arg names for JSON/Python compatibility.

Source: Coding guidelines


192-197: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Keep TVI defaults optional when loading JSON

mode_from_json() still makes the new monofluid_tvi fields mandatory, so older configs without C_1_fluid, C_delta_v, cfl_density_threshold, or ensure_s_j_positivity will fail to load. Read them with the same defaults as set_monofluid_tvi(), or version the format.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/shammodels/sph/include/shammodels/sph/SolverConfig.hpp` around lines 192
- 197, The mode_from_json() loader currently requires the new monofluid_tvi
fields; make C_1_fluid, C_delta_v, cfl_density_threshold, and
ensure_s_j_positivity optional by applying the same defaults defined by
set_monofluid_tvi(). Preserve loading of older JSON configurations while
retaining explicitly provided values.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/shammodels/sph/include/shammodels/sph/SolverConfig.hpp`:
- Around line 122-123: Rename the new MonofluidTVI C++ members in
src/shammodels/sph/include/shammodels/sph/SolverConfig.hpp at lines 122-123 to
c_1_fluid and c_delta_v, and update the corresponding setter parameters and
references at lines 142-143. In src/shammodels/sph/src/pySPHModel.cpp lines
273-274, rename the binding-lambda parameters consistently while preserving any
required public py::arg names for JSON/Python compatibility.
- Around line 192-197: The mode_from_json() loader currently requires the new
monofluid_tvi fields; make C_1_fluid, C_delta_v, cfl_density_threshold, and
ensure_s_j_positivity optional by applying the same defaults defined by
set_monofluid_tvi(). Preserve loading of older JSON configurations while
retaining explicitly provided values.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 8f53b1d0-0ca9-4250-ae76-4ba99b67ef28

📥 Commits

Reviewing files that changed from the base of the PR and between 8f92590 and 00d63c7.

📒 Files selected for processing (2)
  • src/shammodels/sph/include/shammodels/sph/SolverConfig.hpp
  • src/shammodels/sph/src/pySPHModel.cpp

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/shammodels/common/include/shammodels/common/modules/AddForce1PN.hpp (1)

102-117: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Include the Newtonian term in this standalone 1PN force.

PN_1PN wiring registers only this node, but this expression adds only the (O(c^{-2})) correction. It therefore omits the leading (-GM\hat r/r^2) acceleration entirely.

Proposed fix
                             (
-                                v2 / (c * c)
+                                Tscal{1} + v2 / (c * c)
                                 -
                                 4 * GM * inv_r / (c * c)
                             )
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/shammodels/common/include/shammodels/common/modules/AddForce1PN.hpp`
around lines 102 - 117, Update the standalone 1PN acceleration expression in
PN_1PN to include the leading Newtonian -GM * inv_r2 * r_hat term in addition to
the existing O(c^-2) correction, so the registered node returns the complete
acceleration.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/shammodels/common/include/shammodels/common/modules/AddForce1PN.hpp`:
- Around line 102-117: Update the standalone 1PN acceleration expression in
PN_1PN to include the leading Newtonian -GM * inv_r2 * r_hat term in addition to
the existing O(c^-2) correction, so the registered node returns the complete
acceleration.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 737fdea0-31ba-470c-a8f6-d237e1760a45

📥 Commits

Reviewing files that changed from the base of the PR and between 00d63c7 and d00e694.

📒 Files selected for processing (1)
  • src/shammodels/common/include/shammodels/common/modules/AddForce1PN.hpp

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants