Skip to content

stopping power and mass stopping power implemetations - #182

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

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

Conversation

@witNie

@witNie witNie commented Mar 9, 2026 •

Copy link
Copy Markdown
Contributor

No description provided.

@grzanka

grzanka commented Mar 9, 2026

Copy link
Copy Markdown
Contributor

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

witNie commented Mar 13, 2026

Copy link
Copy Markdown
Contributor Author
Screenshot from 2026-03-13 19-55-42 It looks similar

@witNie

witNie commented Mar 13, 2026

Copy link
Copy Markdown
Contributor Author
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

Copilot AI 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.

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

Copilot AI 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.

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 Outdated
Comment thread src/stopping/stopping_bindings.cpp Outdated
witNie added 3 commits April 29, 2026 14:43
…ding-to-one-of-the-models_2' of https://github.com/libamtrack/pyamtrack into 62-add-stopping-power-for-protons-in-liquid-water-according-to-one-of-the-models_2
@witNie witNie changed the title Add mass stopping power calculation and bindings for protons in liqui… stopping power and mass stopping power implemetations Sep 20, 2026
@grzanka
grzanka requested a balanced review from Copilot September 21, 2026 06:45

Copilot AI 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.

Copilot review overview

🔵 Needs a closer look

It adds a substantial native binding module with intricate numeric/memory logic and introduces a functional regression (empty models submodule breaks pyamtrack.stopping.models.* used by an existing example), so human review is warranted.

Review effort: Balanced
Findings: 1 High severity · 2 Low severity

Open (3)

Comment thread src/stopping/stopping_bindings.cpp Outdated
Comment thread src/stopping/stopping_bindings.cpp
Comment thread tests/test_stopping.py Outdated

@grzanka grzanka 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.

Good feature overall (vectorised API, return_source, thorough mass_stopping_power docstring). Findings below, most important first.

🔴 P1 – blockers

  1. Tests are disabled. In tests/test_stopping.py ~177 of 397 lines are commented out: all 7 pre-existing electron_range tests plus 9 earlier new tests (invalid-argument, Ion/Material argument, string/enum source, …). Green CI no longer covers electron_range (touched here) or the type-validation paths. Restore the old tests; revive or delete (not comment out) the new ones.
  2. Unrelated breaking change: pyamtrack.stopping.models.* constants are gone. The loop populating models in stopping_bindings.cpp is commented out, so stopping.models.tabata / .edmund raise AttributeError. examples/example.ipynb (l. 222, 289, 321, 327) still uses them, and (1) hides it. Restore, or deprecate explicitly in a separate PR. No commented-out code, please.

🟠 P2 – should fix before merge

  1. Inconsistent error contract. Docstring promises TypeError for a bad source type, but parse_stopping_power_source throws invalid_argument (→ ValueError) and silently accepts ints 0–3 (undocumented). For list/array input the shared wrapper rewraps everything as RuntimeError("Error processing 1-D NumPy array: …"), so scalar → ValueError, vector → RuntimeError (the new tests assert both). Pick one contract and document it.
  2. No physics anchors in tests. Active tests compare the function to itself (scalar vs vector vs cartesian) or check > 0. Add 2–3 NIST PSTAR reference values with a tolerance (e.g. 100 MeV p in liquid water ≈ 7.289 MeV·cm²/g).
  3. Re-implements libamtrack internals. stopping_power.cpp hand-declares extern AT_stopping_power_ICRU_table[2] / PSTAR_data (not in public headers) and hard-codes material_no - 1, Z > 18, He ≤ 250 MeV/u, materials Water_Liquid..Copper. This silently rots if upstream changes. Prefer upstream status codes / a public API; at minimum name and comment the constants and pin them with a test.
  4. source="default" silently switches physics model per element (PSTAR→Bethe outside the table), and allow_multiple_sources=False then raises a generic "Inconsistent stopping power source selection" with no element/energy and no hint about the flag. Consider dropping the flag (and its stateful mutable lambda) in favour of return_source / a warning, or at least make the message actionable.
  5. API shape. return_source=True changes the return type (value → tuple); prefer a separate function or result object. help() shows source=0 while docs say "default". .export_values() leaks DEFAULT/BETHE/PSTAR/ICRU into the pyamtrack.stopping namespace.

🟡 P3 – maintainability

  1. Formatting churn mixed with logic (~100 lines of clang-format reflow in particles.cpp/.h, ion.h). Split it out so the functional diff is reviewable.
  2. Dead code: Particle::get_id() is unused; Particle::get_particle_no() (throwing, non-virtual, hidden by Ion::) is unreachable; select_stopping_power_source(…, particle_no) ignores its parameter; parse_/select_stopping_power_source are internal but exported in stopping_power.h.
  3. Duplicate passes: validate_particle_argument + parse_particle_argument (same for material) walk the same structure twice (tolist() twice for arrays), then the wrapper walks it again. One normalize_* returning validated IDs would do.
  4. Portability: source_ids use long (int32 on Windows, int64 elsewhere) → use int64_t. windows.yml doesn't run on PRs, so this is unexercised.

🟢 P4 – docs / hygiene

  1. docs/naming_convention.md lists every public pyamtrack.stopping function and argument, but was not updated (mass_stopping_power, stopping_power, particle, source, allow_multiple_sources, return_source, StoppingPowerSource). stopping_power has no Parameters/Returns/Raises docstring.
  2. examples/stopping_power.ipynb is a scratch notebook (one ad-hoc call, leftover outputs, empty last cell) that duplicates stopping.ipynb; drop it. stopping.ipynb again ends with an empty code cell.
  3. Empty PR description, typo in title ("implemetations"), 21 commits incl. "refactor wip" and 4 merges. Describe scope and API, squash on merge.

Comment thread src/particles/ions/ion.h Outdated
Comment thread src/particles/ions/ion.h Outdated
Comment thread src/particles/ions/ion.h Outdated
Comment thread src/stopping/stopping_bindings.cpp Outdated

@grzanka grzanka 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.

see comemnts

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

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