Skip to content
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

@register_unit not global #151

Open
bodokaiser opened this issue Sep 5, 2024 · 5 comments
Open

@register_unit not global #151

bodokaiser opened this issue Sep 5, 2024 · 5 comments

Comments

@bodokaiser
Copy link
Contributor

Hi all,

I am in the process of upgrading the Coils.jl package from Unitful to DynamicQuantities and register Gauss as units:

using DynamicQuantities: @register_unit, @u_str

@register_unit Gauss 1e-4u"T"

However, when I run my package tests, I need to re-register the unit; otherwise, I get "LoadError: ArgumentError: Symbol Gauss not found in Units or Constants."

I think this is not what one would expect and its a bit annoying for users to have to re-register the units themselves.

@MilesCranmer
Copy link
Member

Do you want to add Gauss to the units? I didn’t add it to emphasise that DQ is SI based (so nobody mistakenly uses the CGS version of Maxwells equations) but we could explain this in the docs.

The @register_unit acting locally rather than globally is a feature rather than a bug. Though it can sometimes be convenient, the global behavior of Unitful in registering units is very dangerous and opens up all sorts of footguns and “spooky action at a distance”.

@bodokaiser
Copy link
Contributor Author

Hi Miles, we should allow people to register their own defined units globally or support the common units. If you prefer to add more units to the package, let's do that!

In the atom physics community, "Gauss" is the standard unit for magnetic fields, and with Coils.jl, we can use Julia to design coil geometries to create magnetic fields in atom physics experiments.

What do Maxwell's equations have to do with DynamicQuantities, or is that another package of yours that strongly uses DynamicQuantities?

@MilesCranmer
Copy link
Member

I’m happy to add Gauss though we should emphasise in the docs that it is the SI version. See https://en.m.wikipedia.org/wiki/Gaussian_units for differences. It’s just about preventing footguns.

Another way you can “export” units to users is to provide a macro for it, like

Coils.@register_units

Which would execute all of the @register_unit in the user’s module. Global registries and spooky action at a distance is very dangerous and should be avoided.

@MilesCranmer
Copy link
Member

They key part of the wiki article is:

Conversions between quantities in Gaussian and SI units are not direct unit conversions, because the quantities themselves are defined differently in each system. This means that the equations expressing physical laws of electromagnetism—such as Maxwell's equations—will change depending on the system of units employed. As an example, quantities that are dimensionless in one system may have dimension in the other.

@bodokaiser
Copy link
Contributor Author

Thanks for sharing the details ☺️. Can I prepare a PR to add Gauss?

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

No branches or pull requests

2 participants