Add mass stopping power calculation and bindings for protons in liqui…#182
Conversation
- 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.
…d-water-according-to-one-of-the-models_2
There was a problem hiding this comment.
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()andstopping_power()(nanobind-friendly, vectorized/cartesian-product capable). - Expose these functions in the
stoppingPython 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.
| 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. | ||
|
|
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Will try to make copilot do this one for me
There was a problem hiding this comment.
@copilot apply changes based on this feedback
There was a problem hiding this comment.
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()andMaterialobject arguments (compared against equivalent integer IDs)- Carbon vs proton sanity check (carbon stopping power > proton at same energy/nucleon)
- Vectorized
particleandmateriallists - Error paths:
TypeErrorfor a string particle,TypeErrorfor a Particle withA=None, andTypeErrorfor a string material
Also updated the particle/material docstrings in stopping_bindings.cpp to document list support (addressing the resolved review comment).
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Witold Nieć <witold.niec@gmail.com>
There was a problem hiding this comment.
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.
…d-water-according-to-one-of-the-models_2
Agent-Logs-Url: https://github.com/libamtrack/pyamtrack/sessions/822ea03a-8fdf-4b4f-82f0-927cb5daedbe Co-authored-by: witNie <180410451+witNie@users.noreply.github.com>




…d water