aboutsummaryrefslogtreecommitdiffstats
path: root/fg21sim/utils/units.py
diff options
context:
space:
mode:
authorAaron LI <aaronly.me@outlook.com>2017-05-27 18:11:00 +0800
committerAaron LI <aly@aaronly.me>2017-06-01 16:33:40 +0800
commitc630afcff2a8529232dee68a3964e52cf8f080b4 (patch)
treeaf5163d51c89a03084df6decbf33d7e5847d0a24 /fg21sim/utils/units.py
parentc7e866bb8474c0b391ebb29826361c75240e5cd3 (diff)
downloadfg21sim-c630afcff2a8529232dee68a3964e52cf8f080b4.tar.bz2
clusters: Move units.py and cosmology.py to utils
Diffstat (limited to 'fg21sim/utils/units.py')
-rw-r--r--fg21sim/utils/units.py72
1 files changed, 72 insertions, 0 deletions
diff --git a/fg21sim/utils/units.py b/fg21sim/utils/units.py
new file mode 100644
index 0000000..a7e4ef5
--- /dev/null
+++ b/fg21sim/utils/units.py
@@ -0,0 +1,72 @@
+# Copyright (c) 2017 Weitian LI <liweitianux@live.com>
+# MIT license
+
+"""
+Commonly used units and their conversions relations, as well as constants.
+
+Astropy's units system is very powerful, but also very slow,
+and may even be the speed bottleneck of the program.
+
+This module provides commonly used units conversions by holding
+them directly in a class, thus avoid repeated/unnecessary calculations.
+"""
+
+import astropy.units as au
+import astropy.constants as ac
+
+
+class Units:
+ """
+ Commonly used units, especially in the CGS unit system.
+ """
+ # Unit for electron momentum (p), thus its value is the Lorentz factor
+ mec = ac.m_e.cgs.value*ac.c.cgs.value # [g cm / s]
+
+
+class UnitConversions:
+ """
+ Commonly used units conversion relations.
+
+ Hold the conversion relations directly to avoid repeated/unnecessary
+ calculations.
+ """
+ # Mass
+ Msun2g = au.solMass.to(au.g)
+ g2Msun = au.g.to(au.solMass)
+ # Time
+ Gyr2s = au.Gyr.to(au.s)
+ s2Gyr = au.s.to(au.Gyr)
+ # Length
+ kpc2cm = au.kpc.to(au.cm)
+ cm2kpc = au.cm.to(au.kpc)
+ Mpc2cm = au.Mpc.to(au.cm)
+ cm2Mpc = au.cm.to(au.Mpc)
+ Mpc2km = au.Mpc.to(au.km)
+ km2Mpc = au.km.to(au.Mpc)
+ kpc2km = au.kpc.to(au.km)
+ km2kpc = au.km.to(au.kpc)
+ km2cm = au.km.to(au.cm)
+ # Energy
+ keV2erg = au.keV.to(au.erg)
+
+
+class Constants:
+ """
+ Commonly used constants, especially in the CGS unit system.
+
+ Astropy's constants are stored in SI units by default.
+ When request a constant in CGS unit system, additional (and slow)
+ conversions required.
+ """
+ # Speed of light
+ c = ac.c.cgs.value # [cm/s]
+ # Atomic mass unit (i.e., a.m.u.)
+ u = ac.u.cgs.value # [g]
+ # Gravitational constant
+ G = ac.G.cgs.value # [cm^3/g/s^2]
+ # Electron charge
+ e = ac.e.gauss.value # [Fr] = [esu]
+
+ # Mean molecular weight
+ # Ref.: Ettori et al, 2013, Space Science Review, 177, 119-154, Eq.(6)
+ mu = 0.6