fastiron_stats/structures/
raw.rs1use std::{fmt::Display, fs::File, iter::zip, ops::Index};
6
7pub const N_TALLIED_DATA: usize = 17;
14
15#[derive(Debug, Clone, Copy)]
17pub enum TalliedData {
18 Cycle = 0,
20 Start = 1,
22 Source = 2,
24 Rr = 3,
26 Split = 4,
28 Absorb = 5,
30 Scatter = 6,
32 Fission = 7,
34 Produce = 8,
36 Collision = 9,
38 Escape = 10,
40 Census = 11,
42 NumSeg = 12,
44 ScalarFlux = 13,
46 PopulationControl = 14,
48 CycleTracking = 15,
50 CycleSync = 16,
52}
53
54impl Display for TalliedData {
56 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57 write!(
58 f,
59 "{}",
60 match *self {
61 TalliedData::Cycle => "Cycle",
62 TalliedData::Start => "Start",
63 TalliedData::Source => "Source",
64 TalliedData::Rr => "Rr",
65 TalliedData::Split => "Split",
66 TalliedData::Absorb => "Absorb",
67 TalliedData::Scatter => "Scatter",
68 TalliedData::Fission => "Fission",
69 TalliedData::Produce => "Produce",
70 TalliedData::Collision => "Collision",
71 TalliedData::Escape => "Escape",
72 TalliedData::Census => "Census",
73 TalliedData::NumSeg => "NumSeg",
74 TalliedData::ScalarFlux => "ScalarFlux",
75 TalliedData::PopulationControl => "PopulationControl",
76 TalliedData::CycleTracking => "CycleTracking",
77 TalliedData::CycleSync => "CycleSync",
78 }
79 )
80 }
81}
82
83#[derive(Debug)]
89pub struct TalliedVariable {
90 pub values: Vec<f64>,
92 pub mean: f64,
94 pub variance: f64,
96}
97
98impl TalliedVariable {
99 pub fn new(values: &[f64]) -> Self {
102 let n_val = values.len() as f64;
103 let val = values.to_vec();
104 let mut mean = val.iter().sum();
105 mean /= n_val;
106 let mut var = val.iter().map(|xi| (xi - mean) * (xi - mean)).sum();
107 var /= n_val;
108
109 Self {
110 values: val,
111 mean,
112 variance: var,
113 }
114 }
115
116 pub fn n_val(&self) -> usize {
118 self.values.len()
119 }
120}
121
122pub fn covariance(x: &TalliedVariable, y: &TalliedVariable) -> f64 {
124 assert_eq!(x.n_val(), y.n_val());
125 let iter = zip(x.values.iter(), y.values.iter());
126 let mut cov = iter.map(|(xi, yi)| (xi - x.mean) * (yi - y.mean)).sum();
127 cov /= x.n_val() as f64;
128 cov
129}
130
131pub fn correlation(x: &TalliedVariable, y: &TalliedVariable) -> f64 {
137 if (x.variance == 0.0) | (y.variance == 0.0) {
138 return 0.0;
140 }
141 let cov = covariance(x, y);
142 cov / (x.variance * y.variance).sqrt()
143}
144
145pub struct TalliesReport {
147 pub tallies_data: [TalliedVariable; N_TALLIED_DATA],
149}
150
151impl From<File> for TalliesReport {
153 fn from(file: File) -> Self {
154 let mut reader = csv::ReaderBuilder::new().delimiter(b';').from_reader(file);
155 let mut values: [Vec<f64>; N_TALLIED_DATA] = Default::default();
156 values.iter_mut().for_each(|v| v.reserve(100));
157 for result in reader.records() {
159 let mut record = result.unwrap();
160 record.trim();
161 (0..N_TALLIED_DATA).for_each(|idx| {
163 let val = record.get(idx).unwrap();
164 values[idx].push(val.parse().unwrap())
165 })
166 }
167 Self {
169 tallies_data: values.map(|val| TalliedVariable::new(&val)),
170 }
171 }
172}
173
174impl Index<TalliedData> for TalliesReport {
175 type Output = TalliedVariable;
176
177 fn index(&self, tallied_data: TalliedData) -> &Self::Output {
178 &self.tallies_data[tallied_data as usize]
179 }
180}
181
182pub const N_TIMERS: usize = 6;
188
189pub const TIMERS_ARR: [TimerSV; N_TIMERS] = [
191 TimerSV::Main,
192 TimerSV::PopulationControl,
193 TimerSV::CycleTracking,
194 TimerSV::CycleTrackingProcess,
195 TimerSV::CycleTrackingSort,
196 TimerSV::CycleSync,
197];
198
199#[derive(Debug, Clone, Copy)]
201pub enum TimerSV {
202 Main = 0,
203 PopulationControl = 1,
204 CycleTracking = 2,
205 CycleTrackingProcess = 3,
206 CycleTrackingSort = 4,
207 CycleSync = 5,
208}
209
210impl Display for TimerSV {
212 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
213 write!(
214 f,
215 "{}",
216 match *self {
217 TimerSV::Main => "Section::Main",
218 TimerSV::PopulationControl => "Section::PopulationControl",
219 TimerSV::CycleTracking => "Section::CycleTracking",
220 TimerSV::CycleTrackingProcess => "Section::CycleTrackingProcess",
221 TimerSV::CycleTrackingSort => "Section::CycleTrackingSort",
222 TimerSV::CycleSync => "Section::CycleSync",
223 }
224 )
225 }
226}
227
228#[derive(Default, Clone, Copy, Debug)]
230pub struct SummarizedVariable {
231 pub mean: f64,
233 pub lowest: f64,
235 pub highest: f64,
237 pub total: f64,
239}
240
241pub struct TimerReport {
243 pub timers_data: [SummarizedVariable; N_TIMERS],
245}
246
247impl From<File> for TimerReport {
249 fn from(file: File) -> Self {
250 let mut res = [SummarizedVariable::default(); N_TIMERS];
251 let mut reader = csv::ReaderBuilder::new().delimiter(b';').from_reader(file);
252
253 for (timer_idx, result) in reader.records().enumerate() {
255 let mut record = result.unwrap();
256 record.trim();
257 res[timer_idx].lowest = record.get(2).unwrap().parse().unwrap();
259 res[timer_idx].mean = record.get(3).unwrap().parse().unwrap();
260 res[timer_idx].highest = record.get(4).unwrap().parse().unwrap();
261 res[timer_idx].total = record.get(5).unwrap().parse().unwrap();
262 }
263
264 Self { timers_data: res }
265 }
266}
267
268impl Index<TimerSV> for TimerReport {
269 type Output = SummarizedVariable;
270
271 fn index(&self, timer: TimerSV) -> &Self::Output {
272 &self.timers_data[timer as usize]
273 }
274}