Skip to content

Fix ion_sound_speed ignoring provided average ionization (Z) parameter#3217

Open
yaochengchen wants to merge 3 commits intoPlasmaPy:mainfrom
yaochengchen:fix_bug_formulary_speeds
Open

Fix ion_sound_speed ignoring provided average ionization (Z) parameter#3217
yaochengchen wants to merge 3 commits intoPlasmaPy:mainfrom
yaochengchen:fix_bug_formulary_speeds

Conversation

@yaochengchen
Copy link
Contributor

Summary

This PR fixes a logic bug in plasmapy.formulary.speeds.ion_sound_speed
where the optional Z parameter (intended to represent the average
ionization of a plasma) was silently ignored in the sound speed
calculation.

Previously, the implementation always used ion.charge_number
in the formula:

gamma_e * ion.charge_number * k_B * T_e

@github-actions
Copy link

Thank you for submitting this pull request (PR)! ✨

Below are checks that are being run for this PR. Click on the name of a check to learn why it didn't pass. ✅ Don't worry if something broke! We break stuff all the time. 😅

PlasmaPy's contributor guide has pages on:

Tip

📚 For a documentation preview, click on docs/readthedocs.org:plasmapy below. For cryptic documentation errors, see the documentation troubleshooting guide.

Tip

🧹 Automatically fix most pre-commit.ci failures by commenting pre-commit.ci autofix below. For other failures, please see the pre-commit troubleshooting guide.

We thank you once again! 🌌

@github-actions github-actions bot added plasmapy.formulary Related to the plasmapy.formulary subpackage testing bugfix Pull requests that fix a bug. Remember: a bug is a sign of a missing test! python Pull requests that update Python code labels Feb 20, 2026
@codecov
Copy link

codecov bot commented Feb 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.59%. Comparing base (d3bca28) to head (6b2ea23).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3217   +/-   ##
=======================================
  Coverage   94.59%   94.59%           
=======================================
  Files         108      108           
  Lines        9872     9873    +1     
  Branches     1501     1501           
=======================================
+ Hits         9338     9339    +1     
  Misses        332      332           
  Partials      202      202           

☔ View full report in Codecov by Sentry.
📢 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.

@namurphy
Copy link
Member

Thank you for submitting this!

In this case, Z was being used by a decorator to ion_sound_speed, so this change isn't needed.

In ion_sound_speed and many other formulary functions, the Z parameter is used by @particle_input to create the Particle object. An example of how it's used is:

>>> from plasmapy.particles import particle_input, ParticleLike, Particle
>>>
>>> @particle_input
... def transform_into_particle(particle: ParticleLike, Z=None) -> Particle:
...     return particle
...     
>>> transform_into_particle("He", Z=2)
Particle("He 2+")

This pattern is less explicit, but decorators help us avoid having to repeat code that used in a lot of different functions.

@yaochengchen
Copy link
Contributor Author

Thank you for submitting this!

In this case, Z was being used by a decorator to ion_sound_speed, so this change isn't needed.

In ion_sound_speed and many other formulary functions, the Z parameter is used by @particle_input to create the Particle object. An example of how it's used is:

>>> from plasmapy.particles import particle_input, ParticleLike, Particle
>>>
>>> @particle_input
... def transform_into_particle(particle: ParticleLike, Z=None) -> Particle:
...     return particle
...     
>>> transform_into_particle("He", Z=2)
Particle("He 2+")

This pattern is less explicit, but decorators help us avoid having to repeat code that used in a lot of different functions.

Thanks @namurphy for the clarification — that makes sense now.

I really appreciate this design pattern; it's a clean way to avoid repeating logic across functions. Thanks for the review, and sorry for the unnecessary trouble!

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

Labels

bugfix Pull requests that fix a bug. Remember: a bug is a sign of a missing test! plasmapy.formulary Related to the plasmapy.formulary subpackage python Pull requests that update Python code testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants