1
1
module Units
2
2
3
+ export uparse, @u_str
4
+
3
5
import .. DEFAULT_DIM_TYPE
4
6
import .. DEFAULT_VALUE_TYPE
5
7
import .. Quantity
@@ -27,12 +29,19 @@ function _add_prefixes(base_unit::Symbol, prefixes)
27
29
end
28
30
29
31
# SI base units
32
+ " Length in meters. Available variants: `fm`, `pm`, `nm`, `μm` (/`um`), `cm`, `dm`, `mm`, `km`, `Mm`, `Gm`."
30
33
const m = Quantity (1.0 , length= 1 )
34
+ " Mass in grams. Available variants: `μg` (/`ug`), `mg`, `kg`."
31
35
const g = Quantity (1e-3 , mass= 1 )
36
+ " Time in seconds. Available variants: fs, ps, ns, μs (/us), ms, min, h (/hr), day, yr, kyr, Myr, Gyr."
32
37
const s = Quantity (1.0 , time= 1 )
38
+ " Current in Amperes. Available variants: nA, μA (/uA), mA, kA."
33
39
const A = Quantity (1.0 , current= 1 )
40
+ " Temperature in Kelvin. Available variant: mK."
34
41
const K = Quantity (1.0 , temperature= 1 )
42
+ " Luminosity in candela. Available variant: mcd."
35
43
const cd = Quantity (1.0 , luminosity= 1 )
44
+ " Amount in moles. Available variant: mmol."
36
45
const mol = Quantity (1.0 , amount= 1 )
37
46
38
47
@add_prefixes m (f, p, n, μ, u, c, d, m, k, M, G)
@@ -44,15 +53,26 @@ const mol = Quantity(1.0, amount=1)
44
53
@add_prefixes mol (m,)
45
54
46
55
# SI derived units
56
+ " Frequency in Hertz. Available variants: kHz, MHz, GHz."
47
57
const Hz = inv (s)
58
+ " Force in Newtons."
48
59
const N = kg * m / s^ 2
60
+ " Pressure in Pascals. Available variant: kPa."
49
61
const Pa = N / m^ 2
62
+ " Energy in Joules. Available variant: kJ."
50
63
const J = N * m
64
+ " Power in Watts. Available variants: kW, MW, GW."
51
65
const W = J / s
66
+ " Charge in Coulombs."
52
67
const C = A * s
68
+ " Voltage in Volts. Available variants: kV, MV, GV."
53
69
const V = W / A
70
+ " Capacitance in Farads."
54
71
const F = C / V
72
+ " Resistance in Ohms. Available variant: mΩ. Also available is ASCII `ohm` (with variant `mohm`)."
55
73
const Ω = V / A
74
+ const ohm = Ω
75
+ " Magnetic flux density in Teslas."
56
76
const T = N / (A * m)
57
77
58
78
@add_prefixes Hz (k, M, G)
@@ -64,6 +84,7 @@ const T = N / (A * m)
64
84
@add_prefixes V (m, k, M, G)
65
85
@add_prefixes F ()
66
86
@add_prefixes Ω (m,)
87
+ @add_prefixes ohm (m,)
67
88
@add_prefixes T ()
68
89
69
90
# Common assorted units
@@ -81,11 +102,13 @@ const yr = 365.25 * day
81
102
@add_prefixes yr (k, M, G)
82
103
83
104
# # Volume
105
+ " Volume in liters. Available variants: mL, dL."
84
106
const L = dm^ 3
85
107
86
108
@add_prefixes L (m, d)
87
109
88
110
# # Pressure
111
+ " Pressure in bars."
89
112
const bar = 100 * kPa
90
113
91
114
@add_prefixes bar ()
@@ -112,7 +135,7 @@ as_quantity(x::Number) = Quantity(convert(DEFAULT_VALUE_TYPE, x), DEFAULT_DIM_TY
112
135
as_quantity (x) = error (" Unexpected type evaluated: $(typeof (x)) " )
113
136
114
137
"""
115
- @u_str(s::AbstractString)
138
+ u"[unit expression]"
116
139
117
140
Parse a string containing an expression of units and return the
118
141
corresponding `Quantity` object. For example, `u"km/s^2"`
@@ -122,4 +145,4 @@ macro u_str(s)
122
145
return esc (uparse (s))
123
146
end
124
147
125
- end
148
+ end
0 commit comments