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
//! Write the results of integration to the specified file
use byteorder::{LittleEndian, WriteBytesExt};
use std::{
fs::{self, OpenOptions},
path::Path,
};
use crate::orbit::Orbit;
impl Orbit {
/// Write the results of integration to the specified file
pub fn write(&self, folder: &Path, fields: &[&str], append: bool) {
// Define the path to the object directory
let object_dir = folder.join(&self.id);
// Prepare a result buffer
let mut r: Result<_, _> = Ok(());
// If the object directory doesn't exist, create it
if !object_dir.exists() {
r = fs::create_dir(&object_dir);
}
// If the object directory is ready
if r.is_ok() {
/// Do a write call for this field
macro_rules! write_field {
($field:ident) => {
if fields.contains(&stringify!($field)) {
if let Ok(mut f) = OpenOptions::new()
.write(true)
.truncate(!append)
.append(append)
.create(true)
.open(object_dir.join(format!("{}.bin", stringify!($field))))
{
for v in &self.results.$field {
f.write_f64::<LittleEndian>(*v).unwrap()
}
}
}
};
}
// Write the results
// Solutions of the Lagrangian equations
write_field!(r);
write_field!(psi);
write_field!(z);
write_field!(p_r);
write_field!(p_psi);
write_field!(p_z);
// Galactic Cartesian system
write_field!(x);
write_field!(y);
// Total energy
write_field!(e);
// Apocentric and pericentric distances
write_field!(apo);
write_field!(peri);
}
}
}