oxiphysics-io 0.1.1

File I/O and serialization for the OxiPhysics engine
Documentation
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
//! Auto-generated module
//!
//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)

#[allow(unused_imports)]
use super::functions::*;
use std::collections::{HashMap, VecDeque};

/// A single row in the simulation database, mapping column names to values.
#[derive(Debug, Clone, Default)]
pub struct DbRow {
    /// Map from column name to value.
    pub columns: HashMap<String, DbValue>,
}
impl DbRow {
    /// Create an empty row.
    pub fn new() -> Self {
        Self::default()
    }
    /// Insert a key-value pair into this row.
    pub fn set(&mut self, key: impl Into<String>, val: impl Into<DbValue>) {
        self.columns.insert(key.into(), val.into());
    }
    /// Get a value by column name.
    pub fn get(&self, key: &str) -> Option<&DbValue> {
        self.columns.get(key)
    }
}
/// Metadata for a single simulation run.
#[derive(Debug, Clone)]
pub struct SimulationMetadata {
    /// Unique simulation identifier.
    pub sim_id: String,
    /// Human-readable description.
    pub description: String,
    /// Creation timestamp (Unix seconds, as f64).
    pub created_at: f64,
    /// Arbitrary key-value parameters.
    pub parameters: HashMap<String, String>,
    /// List of artifact file paths associated with this simulation.
    pub artifacts: Vec<String>,
}
impl SimulationMetadata {
    /// Create a new metadata entry with empty parameters and artifacts.
    pub fn new(sim_id: impl Into<String>, description: impl Into<String>, created_at: f64) -> Self {
        Self {
            sim_id: sim_id.into(),
            description: description.into(),
            created_at,
            parameters: HashMap::new(),
            artifacts: Vec::new(),
        }
    }
    /// Add a simulation parameter.
    pub fn set_param(&mut self, key: impl Into<String>, val: impl Into<String>) {
        self.parameters.insert(key.into(), val.into());
    }
    /// Add an artifact path.
    pub fn add_artifact(&mut self, path: impl Into<String>) {
        self.artifacts.push(path.into());
    }
}
/// Registry of simulation metadata entries (data catalog / provenance store).
#[derive(Debug, Default)]
pub struct DataCatalog {
    pub(super) entries: HashMap<String, SimulationMetadata>,
}
impl DataCatalog {
    /// Create an empty data catalog.
    pub fn new() -> Self {
        Self::default()
    }
    /// Register a simulation entry.
    pub fn register(&mut self, meta: SimulationMetadata) {
        self.entries.insert(meta.sim_id.clone(), meta);
    }
    /// Look up a simulation by ID.
    pub fn lookup(&self, sim_id: &str) -> Option<&SimulationMetadata> {
        self.entries.get(sim_id)
    }
    /// Look up a simulation by ID (mutable).
    pub fn lookup_mut(&mut self, sim_id: &str) -> Option<&mut SimulationMetadata> {
        self.entries.get_mut(sim_id)
    }
    /// Remove a simulation by ID.
    ///
    /// Returns `true` if an entry was removed.
    pub fn remove(&mut self, sim_id: &str) -> bool {
        self.entries.remove(sim_id).is_some()
    }
    /// Search for simulations whose description contains `query` (case-insensitive).
    pub fn search_description(&self, query: &str) -> Vec<&SimulationMetadata> {
        let q = query.to_lowercase();
        self.entries
            .values()
            .filter(|m| m.description.to_lowercase().contains(&q))
            .collect()
    }
    /// Return all simulation IDs.
    pub fn all_ids(&self) -> Vec<&str> {
        self.entries.keys().map(|s| s.as_str()).collect()
    }
    /// Number of registered simulations.
    pub fn len(&self) -> usize {
        self.entries.len()
    }
    /// Returns `true` if no simulations are registered.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
}
/// Append-only time series with range query and decimation.
#[derive(Debug, Default)]
pub struct TimeSeriesStore {
    /// Internal storage (always sorted by time).
    pub(super) samples: Vec<TsSample>,
    /// Label/name of this time series.
    pub label: String,
}
impl TimeSeriesStore {
    /// Create a new, empty time series with the given label.
    pub fn new(label: impl Into<String>) -> Self {
        Self {
            samples: Vec::new(),
            label: label.into(),
        }
    }
    /// Append a sample.  Samples should be added in ascending time order.
    pub fn append(&mut self, time: f64, value: f64) {
        self.samples.push(TsSample::new(time, value));
    }
    /// Number of samples stored.
    pub fn len(&self) -> usize {
        self.samples.len()
    }
    /// Returns `true` if no samples have been recorded.
    pub fn is_empty(&self) -> bool {
        self.samples.is_empty()
    }
    /// Return all samples whose timestamp is in `[t_start, t_end]`.
    pub fn range_query(&self, t_start: f64, t_end: f64) -> Vec<&TsSample> {
        self.samples
            .iter()
            .filter(|s| s.time >= t_start && s.time <= t_end)
            .collect()
    }
    /// Decimate the series by keeping every `n`-th sample.
    ///
    /// Returns a new `TimeSeriesStore` with the decimated data.
    pub fn decimate(&self, n: usize) -> TimeSeriesStore {
        let mut out = TimeSeriesStore::new(format!("{}_dec{n}", self.label));
        if n == 0 {
            return out;
        }
        for (i, s) in self.samples.iter().enumerate() {
            if i % n == 0 {
                out.append(s.time, s.value);
            }
        }
        out
    }
    /// Compute the mean value over a time range `[t_start, t_end]`.
    pub fn mean_in_range(&self, t_start: f64, t_end: f64) -> Option<f64> {
        let vals: Vec<f64> = self
            .range_query(t_start, t_end)
            .iter()
            .map(|s| s.value)
            .collect();
        if vals.is_empty() {
            None
        } else {
            Some(vals.iter().sum::<f64>() / vals.len() as f64)
        }
    }
    /// Return the maximum value in the entire series.
    pub fn max_value(&self) -> Option<f64> {
        self.samples.iter().map(|s| s.value).reduce(f64::max)
    }
    /// Return the minimum value in the entire series.
    pub fn min_value(&self) -> Option<f64> {
        self.samples.iter().map(|s| s.value).reduce(f64::min)
    }
    /// Export the series to a CSV string `"time,value\n..."`.
    pub fn to_csv(&self) -> String {
        let mut out = String::from("time,value\n");
        for s in &self.samples {
            out.push_str(&format!("{},{}\n", s.time, s.value));
        }
        out
    }
    /// Interpolate the value at time `t` using linear interpolation.
    ///
    /// Returns `None` if the series is empty or `t` is out of range.
    pub fn interpolate(&self, t: f64) -> Option<f64> {
        if self.samples.is_empty() {
            return None;
        }
        let pos = self.samples.partition_point(|s| s.time <= t);
        if pos == 0 {
            return Some(self.samples[0].value);
        }
        if pos >= self.samples.len() {
            return Some(
                self.samples
                    .last()
                    .expect("collection should not be empty")
                    .value,
            );
        }
        let lo = &self.samples[pos - 1];
        let hi = &self.samples[pos];
        let dt = hi.time - lo.time;
        if dt < 1e-15 {
            return Some(lo.value);
        }
        let frac = (t - lo.time) / dt;
        Some(lo.value + frac * (hi.value - lo.value))
    }
}
/// Catalog of simulation snapshots supporting lazy-loading semantics.
///
/// Snapshots are stored by ID. The catalog provides range queries by
/// simulation time and tag-based filtering.
#[derive(Debug, Default)]
pub struct SnapshotCatalog {
    pub(super) snapshots: HashMap<String, SnapshotEntry>,
    /// Auto-increment counter for generated IDs.
    pub(super) next_id: u64,
}
impl SnapshotCatalog {
    /// Create an empty catalog.
    pub fn new() -> Self {
        Self::default()
    }
    /// Register a snapshot entry and return its id.
    pub fn register(&mut self, entry: SnapshotEntry) -> &str {
        let id = entry.id.clone();
        self.snapshots.insert(id.clone(), entry);
        self.snapshots[&id].id.as_str()
    }
    /// Auto-generate an id and register a snapshot.
    ///
    /// Returns the generated id as an owned `String`.
    pub fn register_auto(&mut self, sim_time: f64, path: impl Into<String>) -> String {
        let id = format!("snap_{:06}", self.next_id);
        self.next_id += 1;
        self.register(SnapshotEntry::new(id.clone(), sim_time, path));
        id
    }
    /// Look up a snapshot by id.
    pub fn get(&self, id: &str) -> Option<&SnapshotEntry> {
        self.snapshots.get(id)
    }
    /// Look up a snapshot mutably by id.
    pub fn get_mut(&mut self, id: &str) -> Option<&mut SnapshotEntry> {
        self.snapshots.get_mut(id)
    }
    /// Remove a snapshot by id.  Returns `true` if removed.
    pub fn remove(&mut self, id: &str) -> bool {
        self.snapshots.remove(id).is_some()
    }
    /// Number of snapshots in the catalog.
    pub fn len(&self) -> usize {
        self.snapshots.len()
    }
    /// Returns `true` if the catalog is empty.
    pub fn is_empty(&self) -> bool {
        self.snapshots.is_empty()
    }
    /// Return all snapshots whose `sim_time` is in `[t_start, t_end]`.
    pub fn range_query(&self, t_start: f64, t_end: f64) -> Vec<&SnapshotEntry> {
        self.snapshots
            .values()
            .filter(|s| s.sim_time >= t_start && s.sim_time <= t_end)
            .collect()
    }
    /// Return all snapshots that contain `tag`.
    pub fn query_by_tag(&self, tag: &str) -> Vec<&SnapshotEntry> {
        self.snapshots
            .values()
            .filter(|s| s.tags.iter().any(|t| t == tag))
            .collect()
    }
    /// Return ids of all unloaded snapshots.
    pub fn unloaded_ids(&self) -> Vec<&str> {
        self.snapshots
            .values()
            .filter(|s| !s.loaded)
            .map(|s| s.id.as_str())
            .collect()
    }
    /// Mark all snapshots in a time range as loaded.
    pub fn mark_range_loaded(&mut self, t_start: f64, t_end: f64) {
        for s in self.snapshots.values_mut() {
            if s.sim_time >= t_start && s.sim_time <= t_end {
                s.loaded = true;
            }
        }
    }
    /// Compute total file size across all snapshots.
    pub fn total_file_size(&self) -> usize {
        self.snapshots.values().map(|s| s.file_size).sum()
    }
    /// Return snapshots sorted by sim_time (ascending).
    pub fn sorted_by_time(&self) -> Vec<&SnapshotEntry> {
        let mut v: Vec<&SnapshotEntry> = self.snapshots.values().collect();
        v.sort_by(|a, b| {
            a.sim_time
                .partial_cmp(&b.sim_time)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        v
    }
}
/// A cached simulation result identified by a string key.
#[derive(Debug, Clone)]
pub struct CacheEntry {
    /// Cache key.
    pub key: String,
    /// Serialised or raw result data.
    pub data: Vec<f64>,
    /// Simulation time at which this result was computed.
    pub sim_time: f64,
    /// Cache version tag for invalidation.
    pub version: u64,
}
impl CacheEntry {
    /// Create a new cache entry.
    pub fn new(key: impl Into<String>, data: Vec<f64>, sim_time: f64, version: u64) -> Self {
        Self {
            key: key.into(),
            data,
            sim_time,
            version,
        }
    }
}
/// A single simulation run record with metadata.
///
/// Stores the simulation name, Unix timestamp, and arbitrary string parameters.
#[derive(Debug, Clone)]
pub struct SimulationRecord {
    /// Unique run name / identifier.
    pub name: String,
    /// Unix timestamp (seconds since epoch) as f64.
    pub timestamp: f64,
    /// Arbitrary key-value simulation parameters.
    pub params: HashMap<String, String>,
    /// Optional output file path.
    pub output_path: Option<String>,
}
impl SimulationRecord {
    /// Create a new record with the given name and timestamp.
    pub fn new(name: impl Into<String>, timestamp: f64) -> Self {
        Self {
            name: name.into(),
            timestamp,
            params: HashMap::new(),
            output_path: None,
        }
    }
    /// Set a parameter value.
    pub fn set_param(&mut self, key: impl Into<String>, val: impl Into<String>) {
        self.params.insert(key.into(), val.into());
    }
    /// Get a parameter value by key.
    pub fn get_param(&self, key: &str) -> Option<&str> {
        self.params.get(key).map(|s| s.as_str())
    }
    /// Set the output file path.
    pub fn set_output(&mut self, path: impl Into<String>) {
        self.output_path = Some(path.into());
    }
}
/// Serialize/deserialize `SimulationRecord` to a simple JSON-like text format.
///
/// The format is:
/// ```text
/// {"name":"`n`","timestamp":`t`,"params":{"k1":"v1",...},"output":"`path`"}
/// ```
#[derive(Debug, Clone, Default)]
pub struct DatabaseSerializer;
impl DatabaseSerializer {
    /// Create a new serializer.
    pub fn new() -> Self {
        Self
    }
    /// Serialize a `SimulationRecord` to a JSON-like string.
    pub fn serialize(&self, rec: &SimulationRecord) -> String {
        let params_str: Vec<String> = rec
            .params
            .iter()
            .map(|(k, v)| format!(r#""{k}":"{v}""#))
            .collect();
        let params_json = format!("{{{}}}", params_str.join(","));
        let output_str = match &rec.output_path {
            Some(p) => format!(r#""{p}""#),
            None => "null".to_string(),
        };
        format!(
            r#"{{"name":"{name}","timestamp":{ts},"params":{params},"output":{out}}}"#,
            name = rec.name,
            ts = rec.timestamp,
            params = params_json,
            out = output_str,
        )
    }
    /// Serialize a list of records to a JSON array string.
    pub fn serialize_all(&self, recs: &[&SimulationRecord]) -> String {
        let items: Vec<String> = recs.iter().map(|r| self.serialize(r)).collect();
        format!("[{}]", items.join(","))
    }
    /// Deserialize a single record from a JSON-like string.
    ///
    /// Uses minimal parsing; fields must appear in the order produced by
    /// `serialize`. Returns `None` on parse failure.
    pub fn deserialize(&self, s: &str) -> Option<SimulationRecord> {
        let name = self.extract_string_field(s, "name")?;
        let ts_str = self.extract_raw_field(s, "timestamp")?;
        let timestamp: f64 = ts_str.trim().parse().ok()?;
        let mut rec = SimulationRecord::new(name, timestamp);
        let out_raw = self.extract_raw_field(s, "output").unwrap_or_default();
        let out_raw = out_raw.trim();
        if out_raw != "null" && out_raw.starts_with('"') {
            let cleaned = out_raw.trim_matches('"').to_string();
            rec.set_output(cleaned);
        }
        Some(rec)
    }
    fn extract_string_field(&self, s: &str, field: &str) -> Option<String> {
        let key = format!(r#""{field}":""#);
        let start = s.find(key.as_str())? + key.len();
        let rest = &s[start..];
        let end = rest.find('"')?;
        Some(rest[..end].to_string())
    }
    fn extract_raw_field(&self, s: &str, field: &str) -> Option<String> {
        let key = format!(r#""{field}":"#);
        let start = s.find(key.as_str())? + key.len();
        let rest = &s[start..];
        let end = rest.find([',', '}']).unwrap_or(rest.len());
        Some(rest[..end].to_string())
    }
}
/// A single sample in a time series.
#[derive(Debug, Clone)]
pub struct TsSample {
    /// Timestamp in seconds.
    pub time: f64,
    /// Scalar value at this timestamp.
    pub value: f64,
}
impl TsSample {
    /// Create a new time series sample.
    pub fn new(time: f64, value: f64) -> Self {
        Self { time, value }
    }
}
/// A material entry in the material database.
#[derive(Debug, Clone)]
pub struct MaterialRecord {
    /// Material name/identifier.
    pub name: String,
    /// Mass density in kg/m³.
    pub density: f64,
    /// Young's modulus in Pa.
    pub youngs_modulus: f64,
    /// Poisson's ratio (dimensionless).
    pub poisson_ratio: f64,
    /// Thermal conductivity in W/(m·K).
    pub thermal_conductivity: f64,
    /// Yield strength in Pa (0 if not applicable).
    pub yield_strength: f64,
    /// Arbitrary tags for fuzzy search.
    pub tags: Vec<String>,
}
impl MaterialRecord {
    /// Create a new material record.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        name: impl Into<String>,
        density: f64,
        youngs_modulus: f64,
        poisson_ratio: f64,
        thermal_conductivity: f64,
        yield_strength: f64,
        tags: Vec<String>,
    ) -> Self {
        Self {
            name: name.into(),
            density,
            youngs_modulus,
            poisson_ratio,
            thermal_conductivity,
            yield_strength,
            tags,
        }
    }
}
/// Column filter for export: keep only these columns (if empty, keep all).
#[derive(Debug, Clone, Default)]
pub struct ExportFilter {
    /// Columns to include (empty = all columns).
    pub columns: Vec<String>,
    /// Optional minimum value for a numeric column named `filter_col`.
    pub min_value: Option<(String, f64)>,
    /// Optional maximum value for a numeric column named `filter_col`.
    pub max_value: Option<(String, f64)>,
}
impl ExportFilter {
    /// Create an empty (pass-all) filter.
    pub fn new() -> Self {
        Self::default()
    }
    /// Check whether a row passes the numeric range filters.
    pub fn row_passes(&self, row: &DbRow) -> bool {
        if let Some((col, lo)) = &self.min_value {
            let v = row
                .get(col)
                .and_then(|v| v.as_f64())
                .unwrap_or(f64::NEG_INFINITY);
            if v < *lo {
                return false;
            }
        }
        if let Some((col, hi)) = &self.max_value {
            let v = row
                .get(col)
                .and_then(|v| v.as_f64())
                .unwrap_or(f64::INFINITY);
            if v > *hi {
                return false;
            }
        }
        true
    }
}
/// Export format selector.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExportFormat {
    /// Comma-separated values.
    Csv,
    /// JSON array of objects.
    Json,
    /// Simplified HDF5-like text representation.
    Hdf5Text,
}
/// Export pipeline: converts `SimulationDatabase` rows to text output.
#[derive(Debug)]
pub struct ExportPipeline {
    /// Output format.
    pub format: ExportFormat,
    /// Row filter.
    pub filter: ExportFilter,
}
impl ExportPipeline {
    /// Create a new export pipeline with the given format and no filter.
    pub fn new(format: ExportFormat) -> Self {
        Self {
            format,
            filter: ExportFilter::new(),
        }
    }
    /// Set the export filter.
    pub fn with_filter(mut self, filter: ExportFilter) -> Self {
        self.filter = filter;
        self
    }
    /// Run the export, returning the serialised output as a `String`.
    pub fn export(&self, db: &SimulationDatabase) -> String {
        let cols = &self.filter.columns;
        let rows: Vec<&DbRow> = db
            .rows
            .iter()
            .filter(|r| self.filter.row_passes(r))
            .collect();
        match self.format {
            ExportFormat::Csv => self.to_csv(&rows, cols),
            ExportFormat::Json => self.to_json(&rows, cols),
            ExportFormat::Hdf5Text => self.to_hdf5_text(&rows, cols, &db.name),
        }
    }
    /// Determine effective column list from rows.
    fn effective_cols(&self, rows: &[&DbRow], cols: &[String]) -> Vec<String> {
        if cols.is_empty() {
            let mut seen: Vec<String> = Vec::new();
            for row in rows {
                for k in row.columns.keys() {
                    if !seen.contains(k) {
                        seen.push(k.clone());
                    }
                }
            }
            seen.sort();
            seen
        } else {
            cols.to_vec()
        }
    }
    fn value_to_str(v: &DbValue) -> String {
        match v {
            DbValue::Int(i) => i.to_string(),
            DbValue::Float(f) => format!("{f}"),
            DbValue::Text(s) => s.clone(),
            DbValue::Bool(b) => b.to_string(),
            DbValue::Null => "".to_string(),
        }
    }
    fn to_csv(&self, rows: &[&DbRow], cols: &[String]) -> String {
        let headers = self.effective_cols(rows, cols);
        let mut out = headers.join(",");
        out.push('\n');
        for row in rows {
            let line: Vec<String> = headers
                .iter()
                .map(|c| row.get(c).map(Self::value_to_str).unwrap_or_default())
                .collect();
            out.push_str(&line.join(","));
            out.push('\n');
        }
        out
    }
    fn to_json(&self, rows: &[&DbRow], cols: &[String]) -> String {
        let headers = self.effective_cols(rows, cols);
        let mut out = String::from("[\n");
        for (ri, row) in rows.iter().enumerate() {
            out.push_str("  {");
            let fields: Vec<String> = headers
                .iter()
                .map(|c| {
                    let val = row
                        .get(c)
                        .map(|v| match v {
                            DbValue::Text(s) => format!(r#""{s}""#),
                            DbValue::Null => "null".to_string(),
                            other => Self::value_to_str(other),
                        })
                        .unwrap_or_else(|| "null".to_string());
                    format!(r#""{c}":{val}"#)
                })
                .collect();
            out.push_str(&fields.join(","));
            out.push('}');
            if ri + 1 < rows.len() {
                out.push(',');
            }
            out.push('\n');
        }
        out.push(']');
        out
    }
    fn to_hdf5_text(&self, rows: &[&DbRow], cols: &[String], table_name: &str) -> String {
        let headers = self.effective_cols(rows, cols);
        let mut out = format!("# HDF5-like text dump of table '{table_name}'\n");
        out.push_str(&format!("# columns: {}\n", headers.join(",")));
        out.push_str(&format!("# rows: {}\n", rows.len()));
        for row in rows {
            let line: Vec<String> = headers
                .iter()
                .map(|c| row.get(c).map(Self::value_to_str).unwrap_or_default())
                .collect();
            out.push_str(&line.join(" "));
            out.push('\n');
        }
        out
    }
}
/// In-memory material property database with fuzzy search and interpolation.
#[derive(Debug, Default)]
pub struct MaterialDatabase {
    pub(super) records: Vec<MaterialRecord>,
}
impl MaterialDatabase {
    /// Create an empty material database.
    pub fn new() -> Self {
        Self::default()
    }
    /// Create a database pre-populated with common engineering materials.
    pub fn with_defaults() -> Self {
        let mut db = Self::new();
        db.insert(MaterialRecord::new(
            "steel_1020",
            7850.0,
            210e9,
            0.29,
            50.0,
            250e6,
            vec!["metal".into(), "steel".into(), "ferrous".into()],
        ));
        db.insert(MaterialRecord::new(
            "aluminium_6061",
            2700.0,
            69e9,
            0.33,
            167.0,
            276e6,
            vec!["metal".into(), "aluminium".into(), "light".into()],
        ));
        db.insert(MaterialRecord::new(
            "copper",
            8960.0,
            110e9,
            0.34,
            385.0,
            70e6,
            vec!["metal".into(), "copper".into(), "conductor".into()],
        ));
        db.insert(MaterialRecord::new(
            "polycarbonate",
            1200.0,
            2.4e9,
            0.37,
            0.2,
            60e6,
            vec!["polymer".into(), "plastic".into(), "transparent".into()],
        ));
        db.insert(MaterialRecord::new(
            "concrete",
            2300.0,
            30e9,
            0.20,
            1.7,
            3e6,
            vec!["composite".into(), "concrete".into(), "brittle".into()],
        ));
        db
    }
    /// Insert a material record.
    pub fn insert(&mut self, record: MaterialRecord) {
        self.records.push(record);
    }
    /// Exact lookup by name.
    pub fn lookup(&self, name: &str) -> Option<&MaterialRecord> {
        self.records.iter().find(|r| r.name == name)
    }
    /// Fuzzy search: return all records whose name or tags contain `query`
    /// (case-insensitive substring match).
    pub fn fuzzy_search(&self, query: &str) -> Vec<&MaterialRecord> {
        let q = query.to_lowercase();
        self.records
            .iter()
            .filter(|r| {
                r.name.to_lowercase().contains(&q)
                    || r.tags.iter().any(|t| t.to_lowercase().contains(&q))
            })
            .collect()
    }
    /// Interpolate material properties between two named materials.
    ///
    /// Returns a new `MaterialRecord` with blended properties at weight `t`
    /// (0 = pure first material, 1 = pure second material).
    ///
    /// Returns `None` if either material is not found.
    pub fn interpolate(&self, name_a: &str, name_b: &str, t: f64) -> Option<MaterialRecord> {
        let a = self.lookup(name_a)?;
        let b = self.lookup(name_b)?;
        let lerp = |va: f64, vb: f64| va + t * (vb - va);
        Some(MaterialRecord::new(
            format!("{name_a}_to_{name_b}_{t:.2}"),
            lerp(a.density, b.density),
            lerp(a.youngs_modulus, b.youngs_modulus),
            lerp(a.poisson_ratio, b.poisson_ratio),
            lerp(a.thermal_conductivity, b.thermal_conductivity),
            lerp(a.yield_strength, b.yield_strength),
            vec![],
        ))
    }
    /// Number of materials in the database.
    pub fn len(&self) -> usize {
        self.records.len()
    }
    /// Returns `true` if no materials are stored.
    pub fn is_empty(&self) -> bool {
        self.records.is_empty()
    }
    /// Return all material names.
    pub fn names(&self) -> Vec<&str> {
        self.records.iter().map(|r| r.name.as_str()).collect()
    }
}
/// LRU cache for expensive simulation results.
///
/// Evicts least-recently-used entries when the capacity limit is reached.
/// Invalidation is by version number: entries with a stale version are evicted.
#[derive(Debug)]
pub struct ResultCache {
    /// Maximum number of entries.
    pub capacity: usize,
    /// Current cache version; entries below this are invalid.
    pub current_version: u64,
    /// LRU queue: front = most recently used.
    pub(super) entries: VecDeque<CacheEntry>,
}
impl ResultCache {
    /// Create a new LRU cache with the given capacity.
    pub fn new(capacity: usize) -> Self {
        Self {
            capacity,
            current_version: 0,
            entries: VecDeque::new(),
        }
    }
    /// Insert or update a cache entry.  Evicts LRU entry when over capacity.
    pub fn put(&mut self, entry: CacheEntry) {
        self.entries.retain(|e| e.key != entry.key);
        self.entries.push_front(entry);
        while self.entries.len() > self.capacity {
            self.entries.pop_back();
        }
    }
    /// Retrieve a cached entry by key, moving it to the front (MRU).
    ///
    /// Returns `None` if the key is not found or the entry is stale.
    pub fn get(&mut self, key: &str) -> Option<&CacheEntry> {
        let pos = self.entries.iter().position(|e| e.key == key)?;
        let entry = self.entries.remove(pos)?;
        if entry.version < self.current_version {
            return None;
        }
        self.entries.push_front(entry);
        self.entries.front()
    }
    /// Increment the version, invalidating all current cache entries.
    pub fn invalidate_all(&mut self) {
        self.current_version += 1;
    }
    /// Remove a specific key from the cache.
    pub fn remove(&mut self, key: &str) {
        self.entries.retain(|e| e.key != key);
    }
    /// Number of entries currently in the cache.
    pub fn len(&self) -> usize {
        self.entries.len()
    }
    /// Returns `true` if the cache is empty.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
    /// Evict all stale entries (version < current_version).
    pub fn evict_stale(&mut self) {
        let ver = self.current_version;
        self.entries.retain(|e| e.version >= ver);
    }
}
/// In-memory CRUD store for `SimulationRecord` entries.
///
/// Supports insert, lookup by name, update, delete, and query filtering.
#[derive(Debug, Default)]
pub struct SimulationRecordDatabase {
    pub(super) records: HashMap<String, SimulationRecord>,
}
impl SimulationRecordDatabase {
    /// Create an empty database.
    pub fn new() -> Self {
        Self::default()
    }
    /// Insert or replace a record.
    pub fn insert(&mut self, rec: SimulationRecord) {
        self.records.insert(rec.name.clone(), rec);
    }
    /// Look up a record by name.
    pub fn get(&self, name: &str) -> Option<&SimulationRecord> {
        self.records.get(name)
    }
    /// Look up a record mutably by name.
    pub fn get_mut(&mut self, name: &str) -> Option<&mut SimulationRecord> {
        self.records.get_mut(name)
    }
    /// Delete a record by name.  Returns `true` if the record existed.
    pub fn delete(&mut self, name: &str) -> bool {
        self.records.remove(name).is_some()
    }
    /// Number of records.
    pub fn count(&self) -> usize {
        self.records.len()
    }
    /// Returns `true` if there are no records.
    pub fn is_empty(&self) -> bool {
        self.records.is_empty()
    }
    /// Query: return all records that match the given `DatabaseQuery`.
    pub fn query(&self, q: &DatabaseQuery) -> Vec<&SimulationRecord> {
        self.records.values().filter(|r| q.matches(r)).collect()
    }
    /// Return all record names.
    pub fn names(&self) -> Vec<&str> {
        self.records.keys().map(|s| s.as_str()).collect()
    }
    /// Clear all records.
    pub fn clear(&mut self) {
        self.records.clear();
    }
}
/// Metadata for a single simulation snapshot.
#[derive(Debug, Clone)]
pub struct SnapshotEntry {
    /// Snapshot identifier (e.g. `"frame_0042"`).
    pub id: String,
    /// Simulation time at which this snapshot was taken (s).
    pub sim_time: f64,
    /// File path or URI for lazy loading (empty = not yet saved).
    pub path: String,
    /// File size in bytes (0 if unknown).
    pub file_size: usize,
    /// Whether this snapshot has been loaded into memory.
    pub loaded: bool,
    /// Optional per-snapshot tags.
    pub tags: Vec<String>,
}
impl SnapshotEntry {
    /// Create a new, unloaded snapshot entry.
    pub fn new(id: impl Into<String>, sim_time: f64, path: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            sim_time,
            path: path.into(),
            file_size: 0,
            loaded: false,
            tags: Vec::new(),
        }
    }
    /// Mark this snapshot as loaded.
    pub fn mark_loaded(&mut self) {
        self.loaded = true;
    }
    /// Add a tag.
    pub fn add_tag(&mut self, tag: impl Into<String>) {
        self.tags.push(tag.into());
    }
}
/// A simple in-memory relational table for simulation data.
///
/// Supports insert, column-based equality filtering, and projection.
#[derive(Debug, Default)]
pub struct SimulationDatabase {
    /// All rows stored in the table.
    pub rows: Vec<DbRow>,
    /// Table name for metadata/export purposes.
    pub name: String,
}
impl SimulationDatabase {
    /// Create a new, named, empty table.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            rows: Vec::new(),
            name: name.into(),
        }
    }
    /// Insert a row into the table.
    pub fn insert(&mut self, row: DbRow) {
        self.rows.push(row);
    }
    /// Count the number of rows.
    pub fn row_count(&self) -> usize {
        self.rows.len()
    }
    /// Query rows where column `col` equals `val`.
    pub fn query_eq(&self, col: &str, val: &DbValue) -> Vec<&DbRow> {
        self.rows
            .iter()
            .filter(|r| r.get(col).map(|v| v == val).unwrap_or(false))
            .collect()
    }
    /// Query rows where column `col` (numeric) is in `[lo, hi]`.
    pub fn query_range(&self, col: &str, lo: f64, hi: f64) -> Vec<&DbRow> {
        self.rows
            .iter()
            .filter(|r| {
                r.get(col)
                    .and_then(|v| v.as_f64())
                    .map(|f| f >= lo && f <= hi)
                    .unwrap_or(false)
            })
            .collect()
    }
    /// Return a projected view: for each row, extract the listed columns.
    pub fn project(&self, cols: &[&str]) -> Vec<HashMap<String, DbValue>> {
        self.rows
            .iter()
            .map(|r| {
                cols.iter()
                    .filter_map(|&c| r.get(c).map(|v| (c.to_string(), v.clone())))
                    .collect()
            })
            .collect()
    }
    /// Delete all rows where column `col` equals `val`.
    ///
    /// Returns the number of rows deleted.
    pub fn delete_eq(&mut self, col: &str, val: &DbValue) -> usize {
        let before = self.rows.len();
        self.rows
            .retain(|r| r.get(col).map(|v| v != val).unwrap_or(true));
        before - self.rows.len()
    }
    /// Clear all rows.
    pub fn clear(&mut self) {
        self.rows.clear();
    }
    /// Compute the mean of a numeric column across all rows.
    ///
    /// Returns `None` if no numeric values are found.
    pub fn column_mean(&self, col: &str) -> Option<f64> {
        let vals: Vec<f64> = self
            .rows
            .iter()
            .filter_map(|r| r.get(col).and_then(|v| v.as_f64()))
            .collect();
        if vals.is_empty() {
            None
        } else {
            Some(vals.iter().sum::<f64>() / vals.len() as f64)
        }
    }
}
/// Query predicate for filtering `SimulationRecord` entries.
#[derive(Debug, Clone, Default)]
pub struct DatabaseQuery {
    /// Keep only records whose timestamp >= this value.
    pub time_start: Option<f64>,
    /// Keep only records whose timestamp <= this value.
    pub time_end: Option<f64>,
    /// Keep only records whose name starts with this prefix.
    pub name_prefix: Option<String>,
    /// Keep only records that have param `key` equal to `value`.
    pub param_filter: Option<(String, String)>,
}
impl DatabaseQuery {
    /// Create an empty (pass-all) query.
    pub fn new() -> Self {
        Self::default()
    }
    /// Filter by time range `[t_start, t_end]`.
    pub fn with_time_range(mut self, t_start: f64, t_end: f64) -> Self {
        self.time_start = Some(t_start);
        self.time_end = Some(t_end);
        self
    }
    /// Filter by name prefix.
    pub fn with_name_prefix(mut self, prefix: impl Into<String>) -> Self {
        self.name_prefix = Some(prefix.into());
        self
    }
    /// Filter by a parameter key=value match.
    pub fn with_param(mut self, key: impl Into<String>, val: impl Into<String>) -> Self {
        self.param_filter = Some((key.into(), val.into()));
        self
    }
    /// Check whether a `SimulationRecord` passes this query.
    pub fn matches(&self, rec: &SimulationRecord) -> bool {
        if let Some(t0) = self.time_start
            && rec.timestamp < t0
        {
            return false;
        }
        if let Some(t1) = self.time_end
            && rec.timestamp > t1
        {
            return false;
        }
        if let Some(ref prefix) = self.name_prefix
            && !rec.name.starts_with(prefix.as_str())
        {
            return false;
        }
        if let Some((ref k, ref v)) = self.param_filter {
            match rec.params.get(k.as_str()) {
                Some(pv) if pv == v => {}
                _ => return false,
            }
        }
        true
    }
}
/// A column value in the simulation database.
#[derive(Debug, Clone, PartialEq)]
pub enum DbValue {
    /// Integer value.
    Int(i64),
    /// Floating-point value.
    Float(f64),
    /// Text value.
    Text(String),
    /// Boolean value.
    Bool(bool),
    /// Null / missing value.
    Null,
}
impl DbValue {
    /// Return `Some(f64)` if this value is `Float` or `Int`, else `None`.
    pub fn as_f64(&self) -> Option<f64> {
        match self {
            DbValue::Float(v) => Some(*v),
            DbValue::Int(v) => Some(*v as f64),
            _ => None,
        }
    }
    /// Return `Some(&str)` if this value is `Text`, else `None`.
    pub fn as_str(&self) -> Option<&str> {
        match self {
            DbValue::Text(s) => Some(s.as_str()),
            _ => None,
        }
    }
}