forked from def-/nim-units
-
Notifications
You must be signed in to change notification settings - Fork 0
/
units.nim
287 lines (236 loc) · 7.85 KB
/
units.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
{.hint[XDeclaredButNotUsed]: off.}
import strutils, math, macros
template additive(quant: typedesc): typed =
proc `+` *(x, y: quant): quant {.borrow.}
proc `-` *(x, y: quant): quant {.borrow.}
proc `+=` *(x: var quant, y: quant) {.borrow.}
proc `-=` *(x: var quant, y: quant) {.borrow.}
template multiplicative(quant: typedesc): typed =
proc `*` *(x: quant, y: float): quant {.borrow.}
proc `*` *(x: float, y: quant): quant {.borrow.}
proc `/` *(x: quant, y: float): quant {.borrow.}
template `*=` *(x: var quant, y: float) = x = quant(float(x) * float(y))
template `/=` *(x: var quant, y: float) = x = quant(float(x) / float(y))
template comparable(quant: typedesc): typed =
proc `<` *(x, y: quant): bool {.borrow.}
proc `<=` *(x, y: quant): bool {.borrow.}
proc `==` *(x, y: quant): bool {.borrow.}
template metricPrefix(unit: untyped, prefix: untyped, factor: untyped, addS = true): typed =
template `prefix unit`*(x = 1.0): auto = (x * factor).unit
when addS:
template `prefix unit s`*(x = 1.0): auto = (x * factor).unit
template metricPrefixes(unit: untyped, addS = true): typed =
metricPrefix(unit, yocto, 1e-24, addS)
metricPrefix(unit, zepto, 1e-21, addS)
metricPrefix(unit, atto, 1e-18, addS)
metricPrefix(unit, femto, 1e-15, addS)
metricPrefix(unit, pico, 1e-12, addS)
metricPrefix(unit, nano, 1e-9, addS)
metricPrefix(unit, micro, 1e-6, addS)
metricPrefix(unit, milli, 1e-3, addS)
metricPrefix(unit, centi, 1e-2, addS)
metricPrefix(unit, deci, 1e-1, addS)
metricPrefix(unit, deka, 1e1, addS)
metricPrefix(unit, hecto, 1e2, addS)
metricPrefix(unit, kilo, 1e3, addS)
metricPrefix(unit, mega, 1e6, addS)
metricPrefix(unit, giga, 1e9, addS)
metricPrefix(unit, tera, 1e12, addS)
metricPrefix(unit, peta, 1e15, addS)
metricPrefix(unit, exa, 1e18, addS)
metricPrefix(unit, zetta, 1e21, addS)
metricPrefix(unit, yotta, 1e24, addS)
template quantity(quant: untyped, unit: untyped, output: untyped, addS = true): typed =
type quant* = distinct float
additive(quant)
multiplicative(quant)
comparable(quant)
template unit*(x = 1.0): quant = quant(x)
metricPrefixes(unit, addS)
when addS:
template `unit s`*(x = 1.0): quant = quant(x)
when output == "kg":
proc `$`*(x: quant): string = $ float(x / 1000).formatFloat(precision = 0) & " " & output
else:
proc `$`*(x: quant): string = $ float(x).formatFloat(precision = 0) & " " & output
template isMult(to: untyped, from1: untyped, from2: untyped): typed =
proc `*` *(x: from1, y: from2): to {.borrow.}
when not (from1 is from2):
proc `*` *(x: from2, y: from1): to {.borrow.}
proc `/` *(x: to, y: from1): from2 {.borrow.}
when not (from1 is from2):
proc `/` *(x: to, y: from2): from1 {.borrow.}
template isInverse(a: untyped, b: untyped): typed =
proc `/` *(x: float, y: a): b {.borrow.}
proc `/` *(x: float, y: b): a {.borrow.}
template isAlso(a: untyped, b: untyped): typed =
template `as a` *(x: b): a = a(x)
macro `:=`(abc, data): typed =
assert(abc.kind == nnkIdent)
let to = $abc.ident
case data.len
of 0:
let from1 = $data.ident
result = parseStmt("isAlso(" & to & ", " & from1 & ")")
of 3:
assert(data.kind == nnkInfix)
let m1 = data[0]
assert(m1.kind == nnkIdent)
let m2 = data[1]
let from1 = case m2.kind
of nnkIdent:
$m2.ident
else:
$m2.intVal
let m3 = data[2]
assert(m3.kind == nnkIdent)
let from2 = $m3.ident
case $m1.ident
of "*":
result = parseStmt("isMult(" & to & ", " & from1 & ", " & from2 & ")")
of "/":
if from1 == "1":
result = parseStmt("isInverse(" & to & ", " & from2 & ")")
else:
result = parseStmt("isMult(" & from1 & ", " & to & ", " & from2 & ")")
else:
assert false
else:
discard
template altName(x: untyped, y: untyped, addS = false): typed =
const y* = x
metricPrefixes(y, addS)
template inUnit(x: untyped, y: untyped): untyped =
float(float(x).y)
# SI base units
quantity(Length, meter, "m")
meter.altName(metre, true)
quantity(Mass, gram, "kg")
quantity(Time, second, "s")
quantity(ElectricCurrent, ampere, "A")
quantity(Temperature, kelvin, "K")
quantity(AmountOfSubstance, mole, "mol")
quantity(LuminousIntensity, candela, "cd")
# Simple helpers
quantity(Area, squareMeter, "m²")
quantity(Volume, cubicMeter, "m³")
# SI named derived units
quantity(Angle, radian, "rad")
quantity(SolidAngle, steradian, "sr")
quantity(Frequency, hertz, "Hz")
quantity(Force, newton, "N")
quantity(Pressure, pascal, "Pa")
quantity(Energy, joule, "J")
quantity(Power, watt, "W")
quantity(ElectricCharge, coulomb, "C")
quantity(Potential, volt, "V")
quantity(Capacitance, farad, "F")
quantity(Resistance, ohm, "Ω")
quantity(Conductance, siemens, "S", false)
quantity(MagneticFlux, weber, "Wb")
quantity(MagneticFieldStrength, tesla, "T")
quantity(Inductance, henry, "H", false)
henry.altName(henries)
quantity(LuminousFlux, lumen, "lm")
quantity(Illuminance, lux, "lx", false)
quantity(Radioactivity, becquerel, "Bq")
quantity(AbsorbedDose, gray, "Gy")
quantity(EquivalentDose, sievert, "Sv")
quantity(CatalyticActivity, katal, "kat")
# Other derived units
quantity(Velocity, meterPerSecond, "m/s", false)
meterPerSecond.altName(metersPerSecond)
meterPerSecond.altName(metrePerSecond)
meterPerSecond.altName(metresPerSecond)
quantity(Acceleration, meterPerSecondSquared, "m/s²", false)
meterPerSecondSquared.altName(metersPerSecondSquared)
meterPerSecondSquared.altName(metrePerSecondSquared)
meterPerSecondSquared.altName(metresPerSecondSquared)
# Non-SI units
template degreeCelsius*(x: untyped): untyped = (x + 273.15).kelvin
template foot*(x: untyped): untyped = (x * 0.3048).meter
#template mile*(x: untyped): untyped = (x * 1_609.344).meter
#template liter*(x: untyped): untyped = (x / 1000).cubicmeter
#template gallon*(x: untyped): untyped = (x * 3.79).liter
# Relations between quantities
Area := Length * Length
Volume := Area * Length
Frequency := 1 / Time
Velocity := Length / Time
Acceleration := Velocity / Time
Angle := Length / Length
SolidAngle := Area / Area
Force := Mass * Acceleration
Pressure := Force / Area
Energy := Force * Length
Power := Energy / Time
ElectricCharge := Time * ElectricCurrent
Potential := Power / ElectricCurrent
Capacitance := ElectricCharge / Potential
Resistance := Potential / ElectricCurrent
Conductance := ElectricCurrent / Potential
MagneticFlux := Potential * Time
MagneticFieldStrength := MagneticFlux / Area
Inductance := MagneticFlux / ElectricCurrent
LuminousFlux := LuminousIntensity * SolidAngle
Illuminance := LuminousFlux / Area
Radioactivity := Frequency
EquivalentDose := Energy / Mass
AbsorbedDose := EquivalentDose
CatalyticActivity := Frequency * AmountOfSubstance
when isMainModule:
# Tests
var f = 2.newton
var x = 2.meter
var e = f * x
echo e
var v = 12.5.volt
var i = 3.0.ampere
var r = v / i
var i2 = v / r
echo r
echo i2
echo i2 == i
var a = 2.meter * 5.meter
echo a
echo 2.meter * 5.meter * 3.meter
echo 1.meter + 3.foot
echo 3.yoctometer
var s = 2.second
echo s
var vel = 2.meters / 5.second
echo vel / millisecond
var gew: Mass = 2.gram
echo gew
#var sum1 = 0.0
#for i in 1..1000000000:
# sum1 += 1.0
#echo sum1
# Just as fast as simple floats
#var sum2 = 0.0.kilogram
#for i in 1..1000000000:
# sum2 += 1.0.kilogram
#echo sum2
var temp = 10.degreeCelsius
echo temp + 10.kelvin
echo x.inUnit(millimeters)
echo x
var ti: Time = 4.seconds
echo ti
var ve = 2.meters / ti
echo ve
var ac: Acceleration = ve / millisecond
echo ac
ac *= 3.0
echo ac
var eqv = 2.joule / 10.kilogram
echo eqv
echo asAbsorbedDose eqv
echo 1 / 1.second
echo asRadioactivity 1 / 1.second
echo 20.kilogram
echo 20.kilometer
#echo 20.gallon
#quantity(Efficiency, metersPe, "rad")
#let fuelEfficiency = 40.mile / 1.gallon
# TODO: Some more complicated examples for actual calculations