1use crate::value::Value;
6use std::collections::BTreeMap;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10pub enum AtmosphericLayer {
11 Exosphere,
12 Thermosphere,
13 Mesosphere,
14 Stratosphere,
15 Troposphere,
16}
17
18impl AtmosphericLayer {
19 pub fn as_str(&self) -> &'static str {
21 match self {
22 Self::Exosphere => "exosphere",
23 Self::Thermosphere => "thermosphere",
24 Self::Mesosphere => "mesosphere",
25 Self::Stratosphere => "stratosphere",
26 Self::Troposphere => "troposphere",
27 }
28 }
29
30 pub fn base_altitude_km(&self) -> f64 {
32 match self {
33 Self::Exosphere => 600.0,
34 Self::Thermosphere => 85.0,
35 Self::Mesosphere => 50.0,
36 Self::Stratosphere => 12.0,
37 Self::Troposphere => 0.0,
38 }
39 }
40}
41
42#[derive(Debug, Clone, PartialEq)]
44pub struct AtmosphericProfile {
45 pub body_name: String,
46 pub surface_pressure_pa: f64,
48 pub surface_temperature_k: f64,
50 pub molar_mass_kg_mol: f64,
52 pub scale_height_m: f64,
54 pub composition: Vec<(&'static str, f64)>,
56}
57
58impl AtmosphericProfile {
59 pub fn earth() -> Self {
61 Self {
62 body_name: "Earth".into(),
63 surface_pressure_pa: 101_325.0,
64 surface_temperature_k: 288.15,
65 molar_mass_kg_mol: 0.0289644,
66 scale_height_m: 8_500.0,
67 composition: vec![
68 ("N2", 0.78084),
69 ("O2", 0.20946),
70 ("Ar", 0.00934),
71 ("CO2", 0.00042),
72 ],
73 }
74 }
75
76 pub fn mars() -> Self {
78 Self {
79 body_name: "Mars".into(),
80 surface_pressure_pa: 636.0,
81 surface_temperature_k: 210.0,
82 molar_mass_kg_mol: 0.04334,
83 scale_height_m: 11_100.0,
84 composition: vec![("CO2", 0.960), ("N2", 0.019), ("Ar", 0.019)],
85 }
86 }
87
88 pub fn venus() -> Self {
90 Self {
91 body_name: "Venus".into(),
92 surface_pressure_pa: 9_200_000.0, surface_temperature_k: 737.0,
94 molar_mass_kg_mol: 0.04345,
95 scale_height_m: 15_900.0,
96 composition: vec![("CO2", 0.965), ("N2", 0.035)],
97 }
98 }
99
100 pub fn pressure_at_altitude(&self, altitude_m: f64) -> f64 {
103 self.surface_pressure_pa * (-altitude_m / self.scale_height_m).exp()
104 }
105
106 pub fn temperature_at_altitude(&self, altitude_m: f64) -> f64 {
109 let lapse_rate = 0.0065; let troposphere_height = 12_000.0; if altitude_m < troposphere_height {
112 self.surface_temperature_k - lapse_rate * altitude_m
113 } else {
114 self.surface_temperature_k - lapse_rate * troposphere_height
116 }
117 }
118
119 pub fn layer_at_altitude(&self, altitude_m: f64) -> AtmosphericLayer {
121 let alt_km = altitude_m / 1000.0;
122 if alt_km >= 600.0 {
123 AtmosphericLayer::Exosphere
124 } else if alt_km >= 85.0 {
125 AtmosphericLayer::Thermosphere
126 } else if alt_km >= 50.0 {
127 AtmosphericLayer::Mesosphere
128 } else if alt_km >= 12.0 {
129 AtmosphericLayer::Stratosphere
130 } else {
131 AtmosphericLayer::Troposphere
132 }
133 }
134
135 pub fn to_value(&self) -> Value {
137 let mut rec = BTreeMap::new();
138 rec.insert("body_name".into(), Value::String(self.body_name.clone()));
139 rec.insert(
140 "surface_pressure_pa".into(),
141 Value::F64(self.surface_pressure_pa),
142 );
143 rec.insert(
144 "surface_temperature_k".into(),
145 Value::F64(self.surface_temperature_k),
146 );
147 rec.insert("scale_height_m".into(), Value::F64(self.scale_height_m));
148 rec.insert(
149 "composition".into(),
150 Value::List(
151 self.composition
152 .iter()
153 .map(|(gas, frac)| {
154 let mut r = BTreeMap::new();
155 r.insert("gas".into(), Value::String((*gas).into()));
156 r.insert("fraction".into(), Value::F64(*frac));
157 Value::Record(r)
158 })
159 .collect(),
160 ),
161 );
162 Value::Record(rec)
163 }
164}
165
166#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
168pub enum MagnetosphereType {
169 None,
171 Induced,
173 Dipole,
175 Multipole,
177}
178
179impl MagnetosphereType {
180 pub fn as_str(&self) -> &'static str {
182 match self {
183 Self::None => "none",
184 Self::Induced => "induced",
185 Self::Dipole => "dipole",
186 Self::Multipole => "multipole",
187 }
188 }
189}
190
191#[derive(Debug, Clone, PartialEq)]
193pub struct MagnetosphereProfile {
194 pub body_name: String,
195 pub mag_type: MagnetosphereType,
196 pub surface_field_t: f64,
198 pub dipole_tilt_deg: f64,
200 pub magnetopause_m: f64,
202}
203
204impl MagnetosphereProfile {
205 pub fn earth() -> Self {
207 Self {
208 body_name: "Earth".into(),
209 mag_type: MagnetosphereType::Dipole,
210 surface_field_t: 3.12e-5, dipole_tilt_deg: 11.0,
212 magnetopause_m: 1.0e8, }
214 }
215
216 pub fn jupiter() -> Self {
218 Self {
219 body_name: "Jupiter".into(),
220 mag_type: MagnetosphereType::Dipole,
221 surface_field_t: 4.28e-4, dipole_tilt_deg: 9.6,
223 magnetopause_m: 7.5e9, }
225 }
226
227 pub fn mars() -> Self {
229 Self {
230 body_name: "Mars".into(),
231 mag_type: MagnetosphereType::None,
232 surface_field_t: 0.0,
233 dipole_tilt_deg: 0.0,
234 magnetopause_m: 0.0,
235 }
236 }
237
238 pub fn field_at_distance(&self, r_m: f64, body_radius_m: f64) -> f64 {
241 if self.surface_field_t == 0.0 {
242 return 0.0;
243 }
244 self.surface_field_t * (body_radius_m / r_m).powi(3)
245 }
246
247 pub fn to_value(&self) -> Value {
249 let mut rec = BTreeMap::new();
250 rec.insert("body_name".into(), Value::String(self.body_name.clone()));
251 rec.insert(
252 "mag_type".into(),
253 Value::String(self.mag_type.as_str().into()),
254 );
255 rec.insert("surface_field_t".into(), Value::F64(self.surface_field_t));
256 rec.insert("dipole_tilt_deg".into(), Value::F64(self.dipole_tilt_deg));
257 rec.insert("magnetopause_m".into(), Value::F64(self.magnetopause_m));
258 Value::Record(rec)
259 }
260}
261
262#[cfg(test)]
263mod tests {
264 use super::*;
265
266 #[test]
267 fn earth_atmosphere_surface_pressure() {
268 let atm = AtmosphericProfile::earth();
269 let p = atm.pressure_at_altitude(0.0);
270 assert!((p - 101_325.0).abs() < 1.0);
271 }
272
273 #[test]
274 fn earth_atmosphere_altitude_pressure() {
275 let atm = AtmosphericProfile::earth();
276 let p = atm.pressure_at_altitude(8_500.0);
278 let ratio = p / 101_325.0;
279 assert!(
280 (ratio - 0.368).abs() < 0.01,
281 "got {} expected ~0.368",
282 ratio
283 );
284 }
285
286 #[test]
287 fn earth_atmosphere_temperature_lapse() {
288 let atm = AtmosphericProfile::earth();
289 let t = atm.temperature_at_altitude(1_000.0);
290 assert!((t - 281.65).abs() < 0.1, "got {} expected ~281.65", t);
292 }
293
294 #[test]
295 fn atmospheric_layer_classification() {
296 let atm = AtmosphericProfile::earth();
297 assert_eq!(atm.layer_at_altitude(0.0), AtmosphericLayer::Troposphere);
298 assert_eq!(
299 atm.layer_at_altitude(5_000.0),
300 AtmosphericLayer::Troposphere
301 );
302 assert_eq!(
303 atm.layer_at_altitude(20_000.0),
304 AtmosphericLayer::Stratosphere
305 );
306 assert_eq!(
307 atm.layer_at_altitude(60_000.0),
308 AtmosphericLayer::Mesosphere
309 );
310 assert_eq!(
311 atm.layer_at_altitude(100_000.0),
312 AtmosphericLayer::Thermosphere
313 );
314 assert_eq!(
315 atm.layer_at_altitude(700_000.0),
316 AtmosphericLayer::Exosphere
317 );
318 }
319
320 #[test]
321 fn mars_atmosphere_thin() {
322 let mars = AtmosphericProfile::mars();
323 assert!(mars.surface_pressure_pa < 1_000.0); }
325
326 #[test]
327 fn venus_atmosphere_dense() {
328 let venus = AtmosphericProfile::venus();
329 assert!(venus.surface_pressure_pa > 1_000_000.0); }
331
332 #[test]
333 fn earth_magnetosphere_dipole() {
334 let mag = MagnetosphereProfile::earth();
335 assert_eq!(mag.mag_type, MagnetosphereType::Dipole);
336 assert!(mag.surface_field_t > 0.0);
337 }
338
339 #[test]
340 fn jupiter_magnetosphere_largest() {
341 let jup = MagnetosphereProfile::jupiter();
342 let earth = MagnetosphereProfile::earth();
343 assert!(jup.magnetopause_m > earth.magnetopause_m);
344 assert!(jup.surface_field_t > earth.surface_field_t);
345 }
346
347 #[test]
348 fn mars_no_magnetosphere() {
349 let mars = MagnetosphereProfile::mars();
350 assert_eq!(mars.mag_type, MagnetosphereType::None);
351 assert_eq!(mars.surface_field_t, 0.0);
352 }
353
354 #[test]
355 fn dipole_field_falloff() {
356 let mag = MagnetosphereProfile::earth();
357 let earth_r = 6_371_000.0;
358 let b = mag.field_at_distance(2.0 * earth_r, earth_r);
360 let ratio = b / mag.surface_field_t;
361 assert!(
362 (ratio - 0.125).abs() < 0.001,
363 "got {} expected 0.125",
364 ratio
365 );
366 }
367
368 #[test]
369 fn atmosphere_to_value() {
370 let atm = AtmosphericProfile::earth();
371 let v = atm.to_value();
372 match v {
373 Value::Record(r) => {
374 assert!(r.contains_key("surface_pressure_pa"));
375 assert!(r.contains_key("composition"));
376 }
377 _ => panic!("expected Record"),
378 }
379 }
380
381 #[test]
382 fn magnetosphere_to_value() {
383 let mag = MagnetosphereProfile::earth();
384 let v = mag.to_value();
385 match v {
386 Value::Record(r) => {
387 assert!(r.contains_key("surface_field_t"));
388 }
389 _ => panic!("expected Record"),
390 }
391 }
392
393 #[test]
394 fn atmospheric_layer_base_altitudes() {
395 assert_eq!(AtmosphericLayer::Troposphere.base_altitude_km(), 0.0);
396 assert!(AtmosphericLayer::Stratosphere.base_altitude_km() > 0.0);
397 assert!(AtmosphericLayer::Exosphere.base_altitude_km() > 500.0);
398 }
399}