DynamicQuantities.jl icon indicating copy to clipboard operation
DynamicQuantities.jl copied to clipboard

@register_unit not global

Open bodokaiser opened this issue 1 year ago • 5 comments

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.

bodokaiser avatar Sep 05 '24 08:09 bodokaiser

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”.

MilesCranmer avatar Sep 05 '24 08:09 MilesCranmer

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?

bodokaiser avatar Sep 05 '24 09:09 bodokaiser

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 avatar Sep 05 '24 09:09 MilesCranmer

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.

MilesCranmer avatar Sep 05 '24 10:09 MilesCranmer

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

bodokaiser avatar Sep 05 '24 11:09 bodokaiser