ndgrid/io/
ron.rs

1//! RON I/O
2use crate::traits::{ConvertToSerializable, Grid, RONExport, RONImport};
3#[cfg(feature = "mpi")]
4use crate::traits::{ParallelGrid, RONExportParallel};
5#[cfg(feature = "mpi")]
6use mpi::traits::Communicator;
7
8impl<G: Grid + ConvertToSerializable> RONExport for G {
9    fn to_ron_string(&self) -> String {
10        ron::to_string(&self.to_serializable()).unwrap()
11    }
12}
13
14impl<G: Grid + ConvertToSerializable> RONImport for G
15where
16    for<'a> G::SerializableType: serde::Deserialize<'a>,
17{
18    fn from_ron_string(s: String) -> Self {
19        Self::from_serializable(ron::from_str(&s).unwrap())
20    }
21}
22
23#[cfg(feature = "mpi")]
24impl<'a, C: Communicator + 'a, G: ParallelGrid<C = C>> RONExportParallel<'a, C> for G
25where
26    Self::LocalGrid: RONExport,
27    Self: 'a,
28{
29}
30
31#[cfg(test)]
32mod test {
33    use super::*;
34    use crate::{
35        MixedGrid, MixedGridBuilder, SingleElementGrid,
36        shapes::regular_sphere,
37        traits::{Builder, Grid},
38    };
39    use ndelement::{ciarlet::CiarletElement, map::IdentityMap, types::ReferenceCellType};
40
41    #[test]
42    fn test_ron_export_and_import() {
43        let g = regular_sphere::<f64>(1);
44        let n = g.entity_count(ReferenceCellType::Interval);
45        g.export_as_ron("_test_export.ron");
46
47        let g2 = SingleElementGrid::<f64, CiarletElement<f64, IdentityMap>>::import_from_ron(
48            "_test_export.ron",
49        );
50
51        assert_eq!(g2.entity_count(ReferenceCellType::Interval), n);
52    }
53
54    #[test]
55    fn test_ron_export_and_import_mixed() {
56        let mut b = MixedGridBuilder::<f64>::new(2);
57        b.add_point(0, &[0.0, 0.0]);
58        b.add_point(1, &[1.0, 0.0]);
59        b.add_point(2, &[0.0, 1.0]);
60        b.add_point(3, &[1.0, 1.0]);
61        b.add_point(4, &[2.0, 0.5]);
62        b.add_point(5, &[1.6, 0.9]);
63        b.add_point(6, &[1.0, 0.5]);
64        b.add_point(7, &[1.6, 0.1]);
65
66        b.add_cell(0, (ReferenceCellType::Quadrilateral, 1, &[0, 1, 2, 3]));
67        b.add_cell(1, (ReferenceCellType::Triangle, 2, &[1, 4, 3, 5, 6, 7]));
68        let g = b.create_grid();
69
70        let n = g.entity_count(ReferenceCellType::Interval);
71        g.export_as_ron("_test_export_mixed.ron");
72
73        let g2 = MixedGrid::<f64, CiarletElement<f64, IdentityMap>>::import_from_ron(
74            "_test_export_mixed.ron",
75        );
76
77        assert_eq!(g2.entity_count(ReferenceCellType::Interval), n);
78    }
79}