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
// fisica::units::length
//
//!
//
use crate::units::{Force, Moment, Speed, Time};
use crate::Magnitude;
/// [*Length*][0] is the measure of one spatial dimension of an object, in `m` (metres).
///
/// [0]: https://en.wikipedia.org/wiki/Length
#[derive(Clone, Copy, Debug)]
pub struct Length {
pub m: Magnitude,
}
impl Length {
/// New Length.
#[inline]
pub const fn new(m: Magnitude) -> Self {
Self { m }
}
/// Returns the magnitude.
#[inline]
pub const fn m(&self) -> Magnitude {
self.m
}
}
/// (== [`Length`]) How far apart objects are.
pub type Distance = Length;
/// (== [`Length`]) Vertical length.
pub type Height = Length;
/// # [`Distance`] formulas
impl Distance {
/// Derives the [`Distance`] from the given [`Time`] and [`Speed`] (`d = s × t`).
#[inline]
pub fn from_time_speed(t: Time, s: Speed) -> Self {
Length::new(t.m() * s.m())
}
/// (Alias of [from_time_speed][Length::from_time_speed]).
#[inline]
pub fn from_speed_time(s: Speed, t: Time) -> Self {
Self::from_time_speed(t, s)
}
/// Calculates the `Speed` given the [`Time`] (`s = d / t`).
pub fn calc_speed(&self, t: Time) -> Speed {
Speed::new(self.m() / t.m())
}
/// Calculates the [`Time`] given the [`Speed`] (`t = d / s`).
pub fn calc_time(&self, s: Speed) -> Time {
Time::new(self.m() / s.m())
}
/// Derives the `Distance` from the given [`Moment`] and [`Force`] (`d = M / F`).
pub fn from_moment_force(m: Moment, f: Force) -> Self {
Self::new(m.m() / f.m())
}
/// (Alias of [from_moment_force][Length::from_moment_force]).
#[inline]
pub fn from_force_moment(f: Force, m: Moment) -> Self {
Self::from_moment_force(m, f)
}
/// Calculates the [`Moment`] given the [`Force`] (`M = F × d`).
#[inline]
pub fn calc_moment(&self, f: Force) -> Moment {
Moment::new(f.d * self.m())
}
/// Calculates the [`Force`] given the [`Moment`] (`F = M / d`).
#[inline]
pub fn calc_force(&self, m: Moment) -> Force {
Force::new(m.d / self.m())
}
}
/// # `Length` constants by order of magnitude
///
/// <https://en.wikipedia.org/wiki/Orders_of_magnitude_(length)>
impl Length {
// subatomic scale
/// (10e-35) The [*Planck length*][0] constant.
///
/// [0]:https://en.wikipedia.org/wiki/Planck_length
pub const PLANCK: Self = Length::new(1.616255e-35);
/// (10e-17) Range of the [*weak force*][0](`10 am`).
///
/// [0]: https://en.wikipedia.org/wiki/Weak_interaction
pub const WEAK_FORCE_RANGE: Self = Length::new(1e-17);
/// (10e-16) Approximate [*proton*][0] radius (`0.833 fm`).
///
/// [0]:https://en.wikipedia.org/wiki/Proton
pub const PROTON_RADIUS: Self = Length::new(8.33e-16);
// atomic to cellular scale
/// (10e-15) [*Classical electron radius*][0] (`2.8179403227 fm`).
///
/// [0]:https://en.wikipedia.org/wiki/Classical_electron_radius
pub const ELECTRON_RADIUS: Self = Length::new(2.8179403227e-15);
/// (10e-15) Minimum diameter of the atomic nucleus (`3 fm`).
pub const ATOMIC_NUCLEUS_DIAMETER_MIN: Self = Length::new(3e-15);
/// (10e-14) Maximum diameter of the atomic nucleus (`15 fm`).
pub const ATOMIC_NUCLEUS_DIAMETER_MAX: Self = Length::new(1.5e-14);
/// (10e-12) Wavelength of shortest [*X-rays*][0] (`5 pm`).
///
/// [0]:https://en.wikipedia.org/wiki/X-ray
pub const XRAY_SHORTEST_WAVELENGTH: Self = Length::new(5e-12);
/// (10e-11) Covalent radius of [*helium*][0] atom (`28 pm`).
///
/// [0]:https://en.wikipedia.org/wiki/Helium
pub const HELIUM_RADIUS: Self = Length::new(2.8e-11);
/// (10e-11) [*Bohr radius*][0] (`52.9177210903 pm`).
///
/// [0]:https://en.wikipedia.org/wiki/Bohr_radius
pub const BOHR_RADIUS: Self = Length::new(5.29177210903e-11);
/// (10e-10) 1 [*Ångström*][0] (`100 pm`)
///
/// [0]:https://en.wikipedia.org/wiki/Angstrom
pub const ANGSTROM: Self = Length::new(1e-10);
/// (10e-10) Length of a carbon-carbon [*covalent bond*][0] in diamond (`154 pm`).
///
/// [0]:https://en.wikipedia.org/wiki/Bond_length
pub const COVALENT_BOND_DIAMOND: Self = Length::new(1.54e-10);
/// (10e11) Roughly the [*distance from the Earth to the Sun*][0] (`149.5978707 Tm`).
///
/// [0]:https://en.wikipedia.org/wiki/Astronomical_unit
pub const ASTRONOMICAL_UNIT: Self = Length::new(1.495978707e11);
}
/// # Non SI units conversions
impl Length {
scalar_methods![
Length,
qa = au,
Qa = astronomical_units,
qu = "au",
Qu = "[astronomical units][Length::ASTRONOMICAL_UNIT]",
f = Self::ASTRONOMICAL_UNIT.m(),
fu = "149.5978707",
bu = "Tm"
];
scalar_methods![
Length,
qa = A,
Qa = angstroms,
qu = "Å",
Qu = "ångströms",
f = 1.0e10,
fu = "10⁻¹⁰",
bu = "metres"
];
}
impl_scalar_methods![Length, m, metres];
#[cfg(test)]
mod tests {
use crate::Direction;
use {super::*, float_eq::assert_float_eq};
/// Checks the constants are defined as expected.
#[test]
fn length_constants() {
assert_float_eq!(
0.00000000001616255,
Length::PLANCK.as_ym(),
r2nd <= Magnitude::EPSILON
);
assert_float_eq!(
10.,
Length::WEAK_FORCE_RANGE.as_am(),
r2nd <= Magnitude::EPSILON
);
assert_float_eq!(
0.833,
Length::PROTON_RADIUS.as_fm(),
r2nd <= Magnitude::EPSILON
);
assert_float_eq!(
2.8179403227,
Length::ELECTRON_RADIUS.as_fm(),
r2nd <= Magnitude::EPSILON
);
assert_float_eq!(
3.,
Length::ATOMIC_NUCLEUS_DIAMETER_MIN.as_fm(),
r2nd <= Magnitude::EPSILON
);
assert_float_eq!(
15.,
Length::ATOMIC_NUCLEUS_DIAMETER_MAX.as_fm(),
r2nd <= Magnitude::EPSILON
);
assert_float_eq!(
5.,
Length::XRAY_SHORTEST_WAVELENGTH.as_pm(),
r2nd <= Magnitude::EPSILON
);
assert_float_eq!(
28.,
Length::HELIUM_RADIUS.as_pm(),
r2nd <= Magnitude::EPSILON
);
assert_float_eq!(
52.9177210903,
Length::BOHR_RADIUS.as_pm(),
r2nd <= Magnitude::EPSILON
);
assert_float_eq!(
154.,
Length::COVALENT_BOND_DIAMOND.as_pm(),
r2nd <= Magnitude::EPSILON
);
}
/// Checks the formulas behave as expected.
#[test]
fn length_formulas() {
// Distance, Speed & Time
let distance = Distance::from_time_speed(Time::new(25.), Speed::new(12.));
assert_float_eq!(300., distance.m(), r2nd <= Magnitude::EPSILON);
assert_float_eq!(
25.,
distance.calc_time(Speed::new(12.)).m(),
r2nd <= Magnitude::EPSILON
);
assert_float_eq!(
12.,
distance.calc_speed(Time::new(25.)).m(),
r2nd <= Magnitude::EPSILON
);
// Distance, Moment & Force
let distance = Distance::from_moment_force(
Moment::new(Direction::new(6., 0., 0.)),
Force::new(Direction::new(30., 0., 0.)),
);
assert_float_eq!(0.2, distance.m, r2nd <= Magnitude::EPSILON);
assert_float_eq!(
6.,
distance
.calc_moment(Force::new(Direction::new(30., 0., 0.)))
.m(),
r2nd <= Magnitude::EPSILON
);
assert_float_eq!(
30.,
distance
.calc_force(Moment::new(Direction::new(6., 0., 0.)))
.m(),
r2nd <= Magnitude::EPSILON
);
}
}