# Unitful.jl - how to represent "particles per cubic centimeter"

**URL:** <https://discourse.julialang.org/t/unitful-jl-how-to-represent-particles-per-cubic-centimeter/62165>\
**Category:** New to Julia\
**Tags:** pluto, unitful\
**Created:** [June 1, 2021, 12:20am UTC](https://discourse.julialang.org/t/unitful-jl-how-to-represent-particles-per-cubic-centimeter/62165 "2021-06-01T00:20:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dcgrigsby](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dcgrigsby/32/20746_2.png) [@dcgrigsby](https://discourse.julialang.org/u/dcgrigsby)\
**Post date:** [June 1, 2021, 12:20am UTC](https://discourse.julialang.org/t/unitful-jl-how-to-represent-particles-per-cubic-centimeter/62165/1 "2021-06-01T00:20:04Z")

</div>

Using [this equation](https://en.wikipedia.org/wiki/Solar_wind#Pressure), I’d like to create a function to calculate the velocity of the Solar Wind.

The equation has three inputs: `mp` (the mass of a photon), `n` (number of particles per cubic cm), and `V` (velocity in kilometers per second).

I’d like to use [Unitful.jl](https://github.com/PainterQubits/Unitful.jl) to type the arguments. For example, I’m using the following to type Velocity:

`V::typeof(1.0u"km/s")`

The mass of photon is easy to represent, in fact there’s a [Unitful constant](https://github.com/PainterQubits/Unitful.jl/blob/master/src/pkgdefaults.jl#L154) for it.

What I can’t figure out is how to represent `n`, the number of particles per cm^3.

Unitful defines an [“amount” dimension](https://github.com/PainterQubits/Unitful.jl/blob/master/src/pkgdefaults.jl#L11), 𝐍 (\bfN), but you don’t appear to be able to use it in units, so I can’t say `7u"𝐍/cm^3"`. There don’t appear to be any amount unit types other than moles, which doesn’t seem like the right choice.

I tried defining a new unit, but using the macros to do so in Pluto doesn’t work.

I’d prefer to type the variable with Unitful because, I think, you get conversions for free. So, for example, if the inputs were particles per cubic meter they’d automatically be converted.

I’d be grateful for some help! Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [June 1, 2021, 12:54am UTC](https://discourse.julialang.org/t/unitful-jl-how-to-represent-particles-per-cubic-centimeter/62165/2 "2021-06-01T00:54:45Z")

</div>

> [@dcgrigsby](#):
>
> I tried defining a new unit, but using the macros to do so in Pluto doesn’t work.

Presumably this is a flaw in Pluto and not Unitful.jl. You could just `eval` the definition in, or put it in a separate file and `include` that file.

This trouble with macros in Pluto.jl will hopefully be addressed soon once [Analyse macros by Pangoraw · Pull Request #1032 · fonsp/Pluto.jl · GitHub](https://github.com/fonsp/Pluto.jl/pull/1032) is merged.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [June 1, 2021, 1:05am UTC](https://discourse.julialang.org/t/unitful-jl-how-to-represent-particles-per-cubic-centimeter/62165/3 "2021-06-01T01:05:34Z")

</div>

Okay, I just checked in a Pluto notebook, and defining the unit in a module and then registering that module seems to work for me:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/8/f88a06d20a7ccd7ea7469a9122cfc11ddf57ddba.png)

```julia
### A Pluto.jl notebook ###
# v0.14.7

using Markdown
using InteractiveUtils

# ╔═╡ 2784b1a6-c274-11eb-1408-b1cfb6b90eec
using Unitful

# ╔═╡ ad245d53-7925-48e3-bfbd-f86c0219e3bf
module MyUnits 
using Unitful
@unit N "N" Number 1 false
end

# ╔═╡ e6cf3bc1-f1bb-4cf4-8335-a28938ebb1ac
Unitful.register(MyUnits)

# ╔═╡ 6d64e53c-fd95-4202-b313-8ee0da6a7fef
1u"N/cm^3"

# ╔═╡ Cell order:
# ╠═2784b1a6-c274-11eb-1408-b1cfb6b90eec
# ╠═ad245d53-7925-48e3-bfbd-f86c0219e3bf
# ╠═e6cf3bc1-f1bb-4cf4-8335-a28938ebb1ac
# ╠═6d64e53c-fd95-4202-b313-8ee0da6a7fef

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 1, 2021, 5:06am UTC](https://discourse.julialang.org/t/unitful-jl-how-to-represent-particles-per-cubic-centimeter/62165/4 "2021-06-01T05:06:48Z")

</div>

I’m not that familiar with Unitful.jl, but wouldn’t you just use the units `1/cm^3` or `cm^(-3)`?

---

<div class="post-metadata">

**Author:** ![dcgrigsby](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dcgrigsby/32/20746_2.png) [@dcgrigsby](https://discourse.julialang.org/u/dcgrigsby)\
**Post date:** [June 2, 2021, 1:17am UTC](https://discourse.julialang.org/t/unitful-jl-how-to-represent-particles-per-cubic-centimeter/62165/5 "2021-06-02T01:17:09Z")

</div>

@Mason - Ah, very interesting! I had tried to create a unit that way, but not in a module. Thank you!

@DNF - Very elegant and, I bet, what the authors had in mind. It seems to work. I’ll fiddle with it a bit more to confirm that there aren’t side effects. Love that your intuition took you right to the answer!

---

<div class="post-metadata">

**Author:** ![dcgrigsby](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dcgrigsby/32/20746_2.png) [@dcgrigsby](https://discourse.julialang.org/u/dcgrigsby)\
**Post date:** [June 2, 2021, 2:03am UTC](https://discourse.julialang.org/t/unitful-jl-how-to-represent-particles-per-cubic-centimeter/62165/7 "2021-06-02T02:03:50Z")

</div>

A postscript for people who find this in the future:

If you have created a variable with units like this:

`n = 7u"cm^-3"`

And, later, you want to get the 7 out, you can strip the units by using the multiplicative inverse of the units:

`n*u"cm^3" == 7 # true`

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [June 2, 2021, 3:24am UTC](https://discourse.julialang.org/t/unitful-jl-how-to-represent-particles-per-cubic-centimeter/62165/8 "2021-06-02T03:24:14Z")

</div>

Or use the [`ustrip`](https://painterqubits.github.io/Unitful.jl/stable/manipulations/#Unit-stripping-1) function.
