Skip to content

Add mass stopping power calculation and bindings for protons in liqui…#182

Open
witNie wants to merge 12 commits into
masterfrom
62-add-stopping-power-for-protons-in-liquid-water-according-to-one-of-the-models_2
Open

Add mass stopping power calculation and bindings for protons in liqui…#182
witNie wants to merge 12 commits into
masterfrom
62-add-stopping-power-for-protons-in-liquid-water-according-to-one-of-the-models_2

Conversation

@witNie
Copy link
Copy Markdown
Contributor

@witNie witNie commented Mar 9, 2026

…d water

@grzanka
Copy link
Copy Markdown
Contributor

grzanka commented Mar 9, 2026

Add some python examples demonstrating if it works as expected...

Try reproducing in Python plot like that:
image

add another data serie for carbon ion on same plot

- Introduced `stopping_power` function to calculate stopping power in MeV*cm²/g.
- Refactored existing mass stopping power functionality into `stopping_power.cpp` and `stopping_power.h`.
- Updated stopping bindings to include new `stopping_power` function.
- Enhanced error handling for particle and material processing.
- Added support for both scalar and array inputs for energy, particle, and material.
@witNie
Copy link
Copy Markdown
Contributor Author

witNie commented Mar 13, 2026

Screenshot from 2026-03-13 19-55-42 It looks similar

@witNie
Copy link
Copy Markdown
Contributor Author

witNie commented Mar 13, 2026

Screenshot from 2026-03-13 20-18-05 this as well checks out Screenshot from 2026-03-13 20-17-47

@grzanka grzanka assigned witNie and unassigned witNie Apr 1, 2026
@witNie witNie marked this pull request as ready for review April 1, 2026 18:43
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds new stopping power APIs to the pyamtrack.stopping nanobind module, exposing libamtrack stopping power calculations to Python (with vectorization support via the existing wrapper utilities).

Changes:

  • Add new C++ wrapper functions mass_stopping_power() and stopping_power() (nanobind-friendly, vectorized/cartesian-product capable).
  • Expose these functions in the stopping Python extension module.
  • Add a Jupyter notebook example demonstrating stopping power calculations.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/stopping/stopping_power.h Declares new stopping power API functions/constants for the stopping module.
src/stopping/stopping_power.cpp Implements stopping power wrappers, argument normalization, and Particle/Material handling.
src/stopping/stopping_bindings.cpp Registers the new functions in the nanobind module (and currently exports a debug function).
src/stopping/electron_range.cpp Makes get_id static to avoid external linkage conflicts.
examples/stopping.ipynb Adds an example notebook (currently has a unit/label mismatch vs the function called).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/stopping/stopping_power.cpp Outdated
Comment thread src/stopping/stopping_bindings.cpp Outdated
Comment on lines +68 to +76
m.def("mass_stopping_power", &mass_stopping_power,
nb::arg("E_MeV_u"),
nb::arg("particle") = 1001,
nb::arg("material") = 1,
nb::arg("source") = 2,
nb::arg("cartesian_product") = false,
R"pbdoc(
Calculate mass stopping power in MeV*cm2/g.

Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

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

New public APIs mass_stopping_power and stopping_power are added here, but there are existing pytest suites for pyamtrack.stopping (e.g., tests/test_stopping.py) and no tests are added for these functions. Please add coverage for scalar + vectorized inputs, Particle/Material object arguments, and basic sanity checks on returned shapes/positivity (and ideally an error-path test for invalid particle/material types).

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Will try to make copilot do this one for me

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@copilot apply changes based on this feedback

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Added in commit 372609f. The new tests in tests/test_stopping.py cover:

  • Scalar inputs returning a positive float
  • List and NumPy array inputs with shape and positivity checks
  • Particle.from_number() and Material object arguments (compared against equivalent integer IDs)
  • Carbon vs proton sanity check (carbon stopping power > proton at same energy/nucleon)
  • Vectorized particle and material lists
  • Error paths: TypeError for a string particle, TypeError for a Particle with A=None, and TypeError for a string material

Also updated the particle/material docstrings in stopping_bindings.cpp to document list support (addressing the resolved review comment).

Comment thread examples/stopping.ipynb
witNie and others added 3 commits April 9, 2026 21:45
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Witold Nieć <witold.niec@gmail.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/stopping/stopping_bindings.cpp
Comment thread src/stopping/stopping_bindings.cpp Outdated
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.

Add stopping power for protons in liquid water according to one of the models

4 participants