Alternative unit systems with Unitful.jl

The docs for Unitful.jl describe how the package can be extended by defining new units and new unphysical dimensions. But there is no information about the correct procedure when one needs to alter the base dimensions and derived dimensions themselves.

The problem is the following: in Gauss CGS unit system there are less base dimensions than in SI. To be precise, in Gauss system, Amper is not independent, but is represented in terms of “cm”, “g” and “s”.
The other electromagnetic quantities are expressed in terms of mechanical dimensions as well.
In order to make the consistent calculations with Gauss unit system, one should properly redefine the base dimensions and all the other units.

So, what is the correct procedure to make a completely alternative unit system (with altered base dimentions) in Unitful.jl?

3 Likes

Look at pkgdefault.jl, you can do the same with your system of units

You can define your own units even if it’s not coherents with Unitful’s default units, as long as you don’t import them

for example:

julia> using Unitful; import Unitful.m

julia> @dimension 𝐁  "𝐁"  MyDimension
𝐁

julia> @refunit B "B" MyUnit 𝐁  true
B

julia> @unit A "A" Ampere 1m/B true
A

julia> A == Unitful.A
false

julia> 1A == 1m/B
true
1 Like

I tried suggesting to make Unitful more modular in https://github.com/PainterQubits/Unitful.jl/issues/359, but this didn’t get any support back then.
Many of the short, especially one-letter, unit abbreviations are different for different areas. This causes confusion from time to time.
It doesn’t help that the namespace of units (as in 1u"m") is global.

2 Likes

When you say “as long as I don’t import them”, do you mean importing Unitful units, or importing my own custom defined units?

Let’s say I want to make a package with my customary unit system. According to the docs, I need to add to my module __init__ function which will register this library with Unitful. Now, let us suppose that I bring both Unitful and my package into scope with

using Unitful, MyCustomUnitPackage

Now, if I do something like

x = 1"A"

will it use Ampers from Unitful or Ampers from my cystom package?

I really like this idea. Why did it not have any support back then?

According to @MarcMush, I can create a package that will provide Gauss unit system, however, it will also load into memory the original “pkgdefault” file of Unitful. Seems like a lot of unnecessary stuff.

Although SI unit system is default in Engineering, I believe Gauss unit system is widely used in Physics.
As a result, it is a good thing to have. At the same time, implementation of the Gauss unit system could certainly benefit from modularity. As a result, this seems like a strong point in favour of modularity.

2 Likes

I don’t know why, it’s just an observation that there are no comments in that issue.

The bottom of this page describes the intended usage for unit system incompatible with the SI: Extending Unitful · Unitful.jl

Basically, make up new dimensions that display differently but similarly to the SI base dimensions, eg L* instead of L.

The problem is, the Gauss unit system is not completely incompatible with SI unit system. All the base dimensions are the same in both unit systems with the exception of Amper, which is derived. This change only affects electromagnetic quantities, not mechanical ones.

Basically, everything boils down to redefinition of all electromagnetic units, both SI and Gauss, so that they have correct dimensions corresponding to Gauss system.

From the point of users, there should be no difference in the names of the units whether they use SI or Gauss system. The thing that bothers me is that @u_str names in Unitful and extension package would clash then.

Let me emphasize that we can separate units and dimension systems.

We can convert SI units into corresponding Gauss units and vice versa.
However, depending on the dimension system, electromagnetic units, both SI and Gauss, have different dimensions.

By the way, we can trim down the set of base dimensions even further. For example, we can put Boltzmann constant to unity and measure the temperature in the units of energy. However, we can still have units of Kelvin. It is just that the dimension of these units would be that of energy.

It’s not possible to write a ‘gauss2si’ function that takes a single argument of a quantity in Gaussian units and returns an si value is it? Since for example both electric and magnetic field have the same units in Gaussian units but different in si. Isn’t the Gaussian system fundamentally carrying less information? I personally hate that property , but that’s not reason to stop you from making a good package to work with it.

Anyway, I think the main problem is that as you say the u_str macro space seems shared across the whole Unitful package and right now you can only extend it, not have an alternate world. Then the solution in issue #395 seems good. Maybe you should just make UnitfulBase and UnitfulCGS or UnitfulGaussian to start. Then a pr to make Unitful depend on UnitfulBase might be pretty easy.

To be clear I’m not a Unitful maintainer.

One to one correspondence is not crucial.

Actually, technically speaking, the units of electric and magnetic fields have the same dimensions, but are named differently. Still, there is no loss of information: we can express Gauss units in terms of SI units. But we can also express all the SI units in terms of Gauss units.

I disagree that there is no loss of information. Consider that I’m working on a problem that involves the variables x the distance between two objects and C the capacitance of some circuit element. If I try to add x+C it has no physical meaning, and in the SI unit system I will catch the error because they have different units. In the gaussian unit system I will not because they have the same representation in base units. Therefore a quantity represented in SI units contains more information about what things can be added and have physical meaning. There are similar examples in SI, but strictly fewer of them: eg both energy and Torque have units of N m in SI and could be added without error, but the sum has no physical meaning.

Adding together units that are different in SI sometimes does make sense. A canonical example is a natural system of units with c=1, and adding mass to energy - which is totally reasonable.

please as unrelated questions in a separate thread.

Thank you for the email. I removed the post and created a new topic.