# Alternative unit systems with Unitful.jl

**URL:** https://discourse.julialang.org/t/alternative-unit-systems-with-unitful-jl/55359
**Category:** General Usage
**Tags:** unitful
**Created:** [February 15, 2021, 11:05pm UTC](https://discourse.julialang.org/t/alternative-unit-systems-with-unitful-jl/55359 "2021-02-15T23:05:32Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)
#### Post date: [February 15, 2021, 11:05pm UTC](https://discourse.julialang.org/t/alternative-unit-systems-with-unitful-jl/55359/1 "2021-02-15T23:05:32Z")

</div>

The docs for [Unitful.jl](https://github.com/PainterQubits/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](https://github.com/PainterQubits/Unitful.jl)?**

---

<div class="post-metadata">

### Author: ![MarcMush](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcmush/32/18006_2.png) [@MarcMush](https://discourse.julialang.org/u/MarcMush)
#### Post date: [February 16, 2021, 2:13pm UTC](https://discourse.julialang.org/t/alternative-unit-systems-with-unitful-jl/55359/2 "2021-02-16T14:13:12Z")

</div>

Look at [pkgdefault.jl](https://github.com/PainterQubits/Unitful.jl/blob/master/src/pkgdefaults.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
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

```

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [February 16, 2021, 2:47pm UTC](https://discourse.julialang.org/t/alternative-unit-systems-with-unitful-jl/55359/3 "2021-02-16T14:47:13Z")

</div>

I tried suggesting to make Unitful more modular in [https://github.com/PainterQubits/Unitful.jl/issues/359](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.

---

<div class="post-metadata">

### Author: ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)
#### Post date: [February 16, 2021, 3:01pm UTC](https://discourse.julialang.org/t/alternative-unit-systems-with-unitful-jl/55359/4 "2021-02-16T15:01:06Z")

</div>

> [@MarcMush](#):
>
> 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

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

```julia
using Unitful, MyCustomUnitPackage

```

Now, if I do something like

```julia
x = 1"A"

```

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

---

<div class="post-metadata">

### Author: ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)
#### Post date: [February 16, 2021, 3:11pm UTC](https://discourse.julialang.org/t/alternative-unit-systems-with-unitful-jl/55359/5 "2021-02-16T15:11:41Z")

</div>

> [@aplavin](#):
>
> I tried suggesting to make Unitful more modular in [More modular structure · Issue #359 · PainterQubits/Unitful.jl · GitHub](https://github.com/PainterQubits/Unitful.jl/issues/359), but this didn’t get any support back then.

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.

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [February 16, 2021, 3:43pm UTC](https://discourse.julialang.org/t/alternative-unit-systems-with-unitful-jl/55359/6 "2021-02-16T15:43:45Z")

</div>

> [@Gregstrq](#):
>
> I really like this idea. Why did it not have any support back then?

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

---

<div class="post-metadata">

### Author: ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)
#### Post date: [February 16, 2021, 4:44pm UTC](https://discourse.julialang.org/t/alternative-unit-systems-with-unitful-jl/55359/7 "2021-02-16T16:44:07Z")

</div>

The bottom of this page describes the intended usage for unit system incompatible with the SI: [Extending Unitful · Unitful.jl](http://painterqubits.github.io/Unitful.jl/stable/extending/#Making-your-own-units-package-1)

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

---

<div class="post-metadata">

### Author: ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)
#### Post date: [February 17, 2021, 1:26am UTC](https://discourse.julialang.org/t/alternative-unit-systems-with-unitful-jl/55359/8 "2021-02-17T01:26:56Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)
#### Post date: [February 17, 2021, 1:33am UTC](https://discourse.julialang.org/t/alternative-unit-systems-with-unitful-jl/55359/9 "2021-02-17T01:33:54Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)
#### Post date: [February 17, 2021, 3:14am UTC](https://discourse.julialang.org/t/alternative-unit-systems-with-unitful-jl/55359/10 "2021-02-17T03:14:03Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)
#### Post date: [February 17, 2021, 12:49pm UTC](https://discourse.julialang.org/t/alternative-unit-systems-with-unitful-jl/55359/11 "2021-02-17T12:49:42Z")

</div>

> [@ggggggggg](#):
>
> 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?

One to one correspondence is not crucial.

> [@ggggggggg](#):
>
> 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?

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.

---

<div class="post-metadata">

### Author: ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)
#### Post date: [February 17, 2021, 5:27pm UTC](https://discourse.julialang.org/t/alternative-unit-systems-with-unitful-jl/55359/12 "2021-02-17T17:27:59Z")

</div>

> [@Gregstrq](#):
>
> 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.

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [February 17, 2021, 9:15pm UTC](https://discourse.julialang.org/t/alternative-unit-systems-with-unitful-jl/55359/13 "2021-02-17T21:15:28Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [March 14, 2021, 12:01am UTC](https://discourse.julialang.org/t/alternative-unit-systems-with-unitful-jl/55359/15 "2021-03-14T00:01:49Z")

</div>

please as unrelated questions in a separate thread.

---

<div class="post-metadata">

### Author: ![Victor\_Villavicencio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/victor_villavicencio/32/10774_2.png) [@Victor\_Villavicencio](https://discourse.julialang.org/u/Victor_Villavicencio)
#### Post date: [March 14, 2021, 12:52am UTC](https://discourse.julialang.org/t/alternative-unit-systems-with-unitful-jl/55359/16 "2021-03-14T00:52:36Z")

</div>

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