From d7b4e9c5e57f27082d481ef175b3c4338a60f62f Mon Sep 17 00:00:00 2001 From: tehforsch Date: Mon, 18 Sep 2023 15:12:24 +0200 Subject: [PATCH] add output of total luminosity --- src/io/time_series.rs | 14 ++++++++------ src/main.rs | 3 +++ src/source_systems.rs | 9 +++++++++ src/sweep/time_series.rs | 2 -- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/io/time_series.rs b/src/io/time_series.rs index d9ed5fed..a1aca649 100644 --- a/src/io/time_series.rs +++ b/src/io/time_series.rs @@ -117,12 +117,14 @@ pub fn output_time_series_system( val: ev.clone(), }) .collect(); - let f = OpenOptions::new() - .append(true) - .open(&path) - .unwrap_or_else(|e| panic!("Failed to open time series file. {}", e)); - serde_yaml::to_writer(&f, &entries) - .unwrap_or_else(|e| panic!("Failed to write to time series file: {}", e)); + if entries.len() > 0 { + let f = OpenOptions::new() + .append(true) + .open(&path) + .unwrap_or_else(|e| panic!("Failed to open time series file. {}", e)); + serde_yaml::to_writer(&f, &entries) + .unwrap_or_else(|e| panic!("Failed to write to time series file: {}", e)); + } } fn get_time_series_filename( diff --git a/src/main.rs b/src/main.rs index f036ee42..a2f2bfc5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,6 +24,7 @@ use subsweep::components::IonizedHydrogenFraction; use subsweep::components::Position; use subsweep::cosmology::Cosmology; use subsweep::io::input::DatasetInputPlugin; +use subsweep::io::time_series::TimeSeriesPlugin; use subsweep::io::DatasetDescriptor; use subsweep::io::DatasetShape; use subsweep::io::InputDatasetDescriptor; @@ -31,6 +32,7 @@ use subsweep::prelude::*; use subsweep::simulation_plugin::remove_components_system; use subsweep::source_systems::set_source_terms_system; use subsweep::source_systems::Sources; +use subsweep::source_systems::TotalLuminosity; use subsweep::units::Dimensionless; use subsweep::units::PhotonRate; use subsweep::units::SourceRate; @@ -104,6 +106,7 @@ fn main() { StartupStages::InsertGrid, remove_components_system::, ) + .add_plugin(TimeSeriesPlugin::::default()) .add_plugin(DatasetInputPlugin::::from_descriptor( InputDatasetDescriptor::::new( DatasetDescriptor { diff --git a/src/source_systems.rs b/src/source_systems.rs index 1d80225d..d4319581 100644 --- a/src/source_systems.rs +++ b/src/source_systems.rs @@ -1,8 +1,11 @@ +use bevy_ecs::event::EventWriter; use bevy_ecs::prelude::Res; use bevy_ecs::prelude::Resource; +use derive_custom::Named; use log::debug; use mpi::traits::Equivalence; use ordered_float::OrderedFloat; +use serde::Serialize; use crate::communication::MpiWorld; use crate::components; @@ -16,6 +19,10 @@ use crate::units::Length; use crate::units::SourceRate; use crate::units::VecLength; +#[derive(Debug, Clone, Equivalence, Named, Serialize)] +#[name = "total_luminosity"] +pub struct TotalLuminosity(pub SourceRate); + #[derive(Debug, Equivalence, Clone, PartialOrd, PartialEq)] pub struct DistanceToSourceData(Length); @@ -36,6 +43,7 @@ pub fn set_source_terms_system( decomposition: Res, box_: Res, world_rank: Res, + mut writer: EventWriter, ) { let mut source_comm = MpiWorld::::new(); let all_sources = source_comm.all_gather_varcount(&sources.sources); @@ -55,5 +63,6 @@ pub fn set_source_terms_system( } } let total: SourceRate = all_sources.iter().map(|source| source.rate).sum(); + writer.send(TotalLuminosity(total)); debug!("Total luminosity: {:+.2e}", total.in_photons_per_second()); } diff --git a/src/sweep/time_series.rs b/src/sweep/time_series.rs index d863e27d..5b87aaf6 100644 --- a/src/sweep/time_series.rs +++ b/src/sweep/time_series.rs @@ -25,12 +25,10 @@ use crate::units::Volume; #[derive(Component, Debug, Clone, Equivalence, Deref, DerefMut, From, Named, Serialize)] #[name = "hydrogen_ionization_mass_average"] -#[repr(transparent)] pub struct HydrogenIonizationMassAverage(pub Dimensionless); #[derive(Component, Debug, Clone, Equivalence, Deref, DerefMut, From, Named, Serialize)] #[name = "hydrogen_ionization_volume_average"] -#[repr(transparent)] pub struct HydrogenIonizationVolumeAverage(pub Dimensionless); #[derive(Serialize, Clone, Named)]