<p>In Astropy, <a href="https://docs.astropy.org/en/stable/units/#module-astropy.units.cgs" rel="noreferrer"><code>u.G</code> represents a Gauss</a>, not the gravitational constant. That's why you get the &quot;A&quot; in one of the error messages; it represents an ampere. To use the gravitational constant in your code, you need to <a href="https://docs.astropy.org/en/stable/constants/index.html#reference-api" rel="noreferrer">use <code>astropy.constants</code></a> and replace <code>u.G</code> in your code with <code>constants.G</code> (or just add <code>import astropy.constants as c</code> and use <code>c.G</code>, if you prefer).</p>

CC BY-SA 4.0
2021-10-12T15:18:39.223Z
2021-10-12T17:30:34.350Z
2021-10-12T17:30:34.350Z
HDE 226868
<p>hope you're all doing well.</p>
<p>I'm not very familiarized with <code>AstroPy</code> and I'm having a problem computing the conversion between angular momentum <strong>units</strong>.</p>
<p>If I define the abs. value angular momentum</p>
<p><span class="math-container">$$ \|\vec{J_2}\| \propto \|\vec{r}\times \vec{p}\| \overset{!}{\propto} a_2 m_2 \left(\frac{G M_s}{a_2}\right)^{1/2} = m_2 (G M_s a_2)^{1/2}$$</span></p>
<p>from the movement of a body <span class="math-container">$m_2$</span> interacting with <span class="math-container">$M_s$</span>, assuming a Keplerian motion, I'll write:</p>
<p>then, a generic abs. ang. mom. <span class="math-container">$\|J_1\|$</span>, let's say <span class="math-container">$$\|J_1\| \equiv \mathrm{\|((1m) \times (1 \frac{kg \, m}{s}))\| = 1 \frac{kg\, m}{s}}$$</span> <code>J1 = u.m * u.kg * u.m * (1/u.s)</code> should have equivalent dimensions compared to <span class="math-container">$J_2$</span>. So I should be able to verify using</p>
<p><code>J2.decompose()</code> or <code>J2.to(J1)</code>, but all I get is <code>UnitConversionError: 'AU(1/2) earthMass G(1/2) solMass(1/2)' and 'kg m2 / s' (angular momentum) are not convertible</code></p>
<pre><code>J1 = angular_momentum(1* u.Msun, 3*u.Mearth, 0.1*u.au)

J2 = u.m * u.kg * u.m * (1/u.s)

J1.to(J2)
---&gt; UnitConversionError
</code></pre>
<p>or</p>
<pre><code>angular_momentum(1*u.Msun, 3*u.Mearth, 0.1*u.au).decompose() #why?
</code></pre>
<p><span class="math-container">$$3.0900743 \times 10^{43} \mathrm{\frac{kg^2 \,m^{1/2}}{A^{1/2}\,s}} \qquad \mathrm{(output)}$$</span></p>
<p>this looks like <code>* u.au</code> is not working as expected. Anyways, passing units, e.g., in <code>u.m</code> does not fix the issue.</p>
<p><strong>Question:</strong> anyone knows what is happening here?</p>
<p><strong>Edit:</strong> this is the function I'm using to compute <span class="math-container">$J_2$</span>
def angular_momentum(ms, mp, a):</p>
<pre><code>    #from Lp = r x p ~~ a * (mp (G ms / a)^1/2) keplerian rot ~~ mp (g ms a)^1/2
    
    Lp = mp * (u.G  *  ms  *  a)**(1./2.)   #This MUST! have L units
    return Lp 
</code></pre>

CC BY-SA 4.0
2021-10-12T22:25:11.997Z
Screenshots of text are discouraged in Stack Exchange for several reasons, including that they can not be searched and that users using screen readers (e.g. text to speech) can't read them. The standard way is to copy paste the text from your computer output to your post, then for code/script add four spaces in front of each line to display as a code block. I've [done that for you here](https://astronomy.stackexchange.com/q/47049/7982) (by just retyping it) and it should probably be done here as well. *Thanks!*
uhoh
<python><units><astropy>
Astropy units issue
nuwe
# Astropy units issue

(Asked by: username_0 on 2021-10-12)

hope you're all doing well.
I'm not very familiarized with AstroPy and I'm having a problem computing the conversion between angular momentum units.
If I define the abs. value angular momentum
$$ \|\vec{J_2}\| \propto \|\vec{r}\times \vec{p}\| \overset{!}{\propto} a_2 m_2 \left(\frac{G M_s}{a_2}\right)^{1/2} = m_2 (G M_s a_2)^{1/2}$$
from the movement of a body $m_2$ interacting with $M_s$, assuming a Keplerian motion, I'll write:
then, a generic abs. ang. mom. $\|J_1\|$, let's say $$\|J_1\| \equiv \mathrm{\|((1m) \times (1 \frac{kg \, m}{s}))\| = 1 \frac{kg\, m}{s}}$$ J1 = u.m * u.kg * u.m * (1/u.s) should have equivalent dimensions compared to $J_2$. So I should be able to verify using
J2.decompose() or J2.to(J1), but all I get is UnitConversionError: 'AU(1/2) earthMass G(1/2) solMass(1/2)' and 'kg m2 / s' (angular momentum) are not convertible
J1 = angular_momentum(1* u.Msun, 3*u.Mearth, 0.1*u.au)

J2 = u.m * u.kg * u.m * (1/u.s)

J1.to(J2)
---> UnitConversionError

or
angular_momentum(1*u.Msun, 3*u.Mearth, 0.1*u.au).decompose() #why?

$$3.0900743 \times 10^{43} \mathrm{\frac{kg^2 \,m^{1/2}}{A^{1/2}\,s}} \qquad \mathrm{(output)}$$
this looks like * u.au is not working as expected. Anyways, passing units, e.g., in u.m does not fix the issue.
Question: anyone knows what is happening here?
Edit: this is the function I'm using to compute $J_2$
def angular_momentum(ms, mp, a):
    #from Lp = r x p ~~ a * (mp (G ms / a)^1/2) keplerian rot ~~ mp (g ms a)^1/2
    
    Lp = mp * (u.G  *  ms  *  a)**(1./2.)   #This MUST! have L units
    return Lp

**Tags:** python, units, astropy
> **Comments:**
> **username_1**: Screenshots of text are discouraged in Stack Exchange for several reasons, including that they can not be searched and that users using screen readers (e.g. text to speech) can't read them. The standard way is to copy paste the text from your computer output to your post, then for code/script add four spaces in front of each line to display as a code block. I've [done that for you here](https://astronomy.stackexchange.com/q/47049/7982) (by just retyping it) and it should probably be done here as well. *Thanks!*  

---

## 1 answers

### Answer by username_2 on 2021-10-12. Score: 11 | Accepted answer

In Astropy, [`u.G` represents a Gauss](https://docs.astropy.org/en/stable/units/#module-astropy.units.cgs), not the gravitational constant. That's why you get the "A" in one of the error messages; it represents an ampere. To use the gravitational constant in your code, you need to [use `astropy.constants`](https://docs.astropy.org/en/stable/constants/index.html#reference-api) and replace `u.G` in your code with `constants.G` (or just add `import astropy.constants as c` and use `c.G`, if you prefer).


---

