space_units/quantities/
thermal.rs1use super::DisplayWithUnit;
2
3#[must_use]
18#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
19pub struct Temperature(pub(crate) f64);
20
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub enum TemperatureUnit {
24 Kelvin,
26 Celsius,
28 Fahrenheit,
30 Rankine,
32}
33
34impl TemperatureUnit {
35 const fn symbol(self) -> &'static str {
36 match self {
37 Self::Kelvin => "K",
38 Self::Celsius => "°C",
39 Self::Fahrenheit => "°F",
40 Self::Rankine => "R",
41 }
42 }
43}
44
45impl Temperature {
46 pub const fn from_k(val: f64) -> Self {
48 Self(val)
49 }
50
51 pub const fn from_c(val: f64) -> Self {
53 Self(val + 273.15)
54 }
55
56 pub const fn from_f(val: f64) -> Self {
58 Self((val - 32.0) * 5.0 / 9.0 + 273.15)
59 }
60
61 pub const fn from_r(val: f64) -> Self {
63 Self(val * 5.0 / 9.0)
64 }
65
66 pub const fn in_k(self) -> f64 {
68 self.0
69 }
70
71 pub const fn in_c(self) -> f64 {
73 self.0 - 273.15
74 }
75
76 pub const fn in_f(self) -> f64 {
78 (self.0 - 273.15) * 9.0 / 5.0 + 32.0
79 }
80
81 pub const fn in_r(self) -> f64 {
83 self.0 * 9.0 / 5.0
84 }
85
86 pub const fn display_as(self, unit: TemperatureUnit) -> DisplayWithUnit {
88 let value = match unit {
89 TemperatureUnit::Kelvin => self.0,
90 TemperatureUnit::Celsius => self.in_c(),
91 TemperatureUnit::Fahrenheit => self.in_f(),
92 TemperatureUnit::Rankine => self.in_r(),
93 };
94 DisplayWithUnit {
95 value,
96 symbol: unit.symbol(),
97 }
98 }
99
100 pub fn abs(self) -> Self {
102 Self(self.0.abs())
103 }
104}
105
106impl_quantity_display!(Temperature, "K");
107
108impl_common_ops!(Temperature);
109
110#[must_use]
126#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
127pub struct HeatFlux(pub(crate) f64);
128
129#[derive(Debug, Clone, Copy, PartialEq, Eq)]
131pub enum HeatFluxUnit {
132 WattPerM2,
134 KiloWattPerM2,
136 SolarFlux,
138}
139
140impl HeatFluxUnit {
141 const fn symbol(self) -> &'static str {
142 match self {
143 Self::WattPerM2 => "W/m^2",
144 Self::KiloWattPerM2 => "kW/m^2",
145 Self::SolarFlux => "solar_flux",
146 }
147 }
148
149 const fn wpm2_per_unit(self) -> f64 {
150 match self {
151 Self::WattPerM2 => 1.0,
152 Self::KiloWattPerM2 => 1e3,
153 Self::SolarFlux => 1_361.0,
154 }
155 }
156}
157
158impl HeatFlux {
159 pub const fn from_wpm2(val: f64) -> Self {
161 Self(val)
162 }
163
164 pub const fn from_kwpm2(val: f64) -> Self {
166 Self(val * 1e3)
167 }
168
169 pub const fn from_solar_flux(val: f64) -> Self {
171 Self(val * 1_361.0)
172 }
173
174 pub const fn in_wpm2(self) -> f64 {
176 self.0
177 }
178
179 pub const fn in_kwpm2(self) -> f64 {
181 self.0 / 1e3
182 }
183
184 pub const fn in_solar_flux(self) -> f64 {
186 self.0 / 1_361.0
187 }
188
189 pub fn in_unit(self, unit: HeatFluxUnit) -> f64 {
191 self.0 / unit.wpm2_per_unit()
192 }
193
194 pub fn display_as(self, unit: HeatFluxUnit) -> DisplayWithUnit {
196 DisplayWithUnit {
197 value: self.in_unit(unit),
198 symbol: unit.symbol(),
199 }
200 }
201
202 pub fn abs(self) -> Self {
204 Self(self.0.abs())
205 }
206}
207
208impl_quantity_display!(HeatFlux, "W/m²");
209
210impl_common_ops!(HeatFlux);