-
Notifications
You must be signed in to change notification settings - Fork 308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert from energy to velocity in relativistic formulary #1056
Comments
If this function doesn't exist somewhere I've overlooked I'm happy to help @phyzicist add this. This function would be a convenient refactor of these lines in
|
I propose adding a function: relativistic_velocity(m, E, kinetic=False) | (default) Calculate the velocity (in meters/second) of an object with rest mass m and total relativistic energy E. And then the contents of this function would be basically what @pheuer has already written. |
I could also see this being articulated as an equivalency between velocity and energy |
Relativistic_velocity with an energy input sounds great and definitely more obvious than than any name with "equivalency" :) One possible variant: speed rather than velocity? With an energy input we can't get the direction, and the way it's taught here in Poland speed implies the scalar magnitude of velocity. I don't actually know whether that distinction is common elsewhere. |
Agreed, |
My issue with just using def relativistic_speed(value: [u.J, u.kg * u.m / u.s], mass):
# if value.unit equivalent to u.J, convert from energy
...
# if value.unit equivalent to u.kg * u.m / u.s, convert from momentum
...
return speed I got to admit, I kind of like the idea of incorporating this into an astropy equivalency. However, it would be in addition to the functions defined here. The "old school" astropy equivalencies is a function that returns a list of 2-4 element tuples. A 4-element tuple is structure like How would I see this incorporated... from functools import partial
def relativistic(mass):
equiv = [
(
u.m / u.s,
u.J,
partial(relativistic_energy, mass=mass),
partial(relativistic_speed, mass=mass),
),
]
return equiv Then one could use it like... >>> E = 0.7 * u.MeV
>>> speed = E.to(u.m/u.s, equivalencies=relativistic(mass=Particle("e").mass)
<Quantity 2.0489278e+08 m / s> I did procrastinate a little and tried to construct something like this and it did work relatively well. The only issue I ran into is that the equivalencies mechanisms seemed to to strip units so I had to manually put them back in the converting functions. ... |
Just jumping in before looking at that code sample to point out a potential flaw in that premise: don't most modern editors (jupyter, pycharm, ipython shell, spyder, vscode, any sufficiently advanced vim configuration) show the signature when suggesting function calls? If this works as I think it does - and I'll try to get you some screenshots later - I think we can comfortably assume the signature to be part of the minimal information anyone will get while using the function. |
I see your point about about IDE's having a display of parameters info, but ...
I'll point out my suggestion above about queuing off off of units would not work with natural units |
Sure, but if you're aware that a function exists, you've either checked the docs, in which case you've seen either the signature or an example of usage, or you've used autocomplete, in which case you'll most likely get the pop-up anyway, no? |
Sure, but that does not address my second point. If we're going to add functionality that calculates speed from relativistic energy, then we should also add functionality to calculates speed from relativistic momentum. We should do this to make the pkg as complete as possible. Now, we have one of two approaches to accomplish this...
Now, there's pros and cons to both approaches. I think my preference would be for option 1, but, if @phyzicist wants to take on this contribution, then I'd let him decide how he'd like to approach it. |
Tbh I'm fine starting as is, from energy, and adding a dispatch-on-unit mechanism later on. It'd come in handy in a bunch of places but a general implementation is likely beyond the scope of this here issue. But a simple implementation for both momentum -> speed and energy -> speed here would also be cool to see, of course :) Sorry, I'm a little cranky today :D |
What was the problem with just accepting velocity, momentum, or energy as the input and then calculating appropriately based on units? Like @rocco8773 described at the very beginning? It might not be the most pythonic choice, but it gets the function to fill the roles we want with the right name. Then we can worry about refactoring it using fancy python later? The only ambiguity would be total energy vs. kinetic energy. I'd suggest assuming kinetic energy and making that clear in the function call. |
I'm fine with this too. My only concern is the first part gets contributed and then the rest gets abandoned/forgotten. The first PR contributes what was actually needed, so then there would be little to no motivation to complete the overall functionality.
No worries. It's a Monday so it's allowed. 😆
@StanczakDominik Correct me if I'm wrong, but I think this issue was mainly center with my suggestion involving some trickery that could be beyond the scope of the functionality originally proposed in this PR. The In the final functionality I would incorporate the unit interpretation and a def relativistic_speed(value: [u.J, u.kg * u.m / u.s], particle: ParticleLike, from: str = None) -> u.m/u.s:
...
return speed Here the |
But isn't the def relativistic_speed(value: [u.J, u.kg * u.m / u.s, particle: ParticleLike) -> u.m/u.s:
if energy units:
momentum = expression
return velocity(momenutm) Works if you make clear in documentation that this takes the KINETIC energy? |
Ok, I see you're point. I think this route would be a little more concise then... def relativistic_speed(value: [u.J, u.kg * u.m / u.s], particle: ParticleLike, is_kinetic_energy: bool = False) -> u.m/u.s:
if value.units.is_equivalent(u.J):
if is_kinetic_energy:
# convert KE to momentum
value = ...
else:
# convert Etotal to momentum
value = ...
# convert mometum to speed
speed = ...
return speed |
Yes, that seems like the way to go (at least for now) to me! |
I thought functools.singledispatch could help us write that, but units are not types, so that's out of the question. |
To come back to this after a while, in #1539 I'm creating the |
Feature request
I can't find a good function in either plasmapy or astropy to convert from kinetic energy into relativistic velocity for a single particle. If it doesn't exist, I'm happy to add it to the relativistic formulary.
The text was updated successfully, but these errors were encountered: