Add serde deserialize to Grid

This commit is contained in:
Magnus Ulimoen 2020-09-16 21:44:11 +02:00
parent ac83a2cbca
commit 5b7f407af9
3 changed files with 17 additions and 4 deletions

View File

@ -6,7 +6,7 @@ edition = "2018"
[dependencies]
sbp = { path = "../sbp", features = ["rayon", "serde"] }
sbp = { path = "../sbp", features = ["rayon", "serde1"] }
euler = { path = "../euler", features = ["serde1"] }
hdf5 = "0.7.0"
rayon = "1.3.0"

View File

@ -17,6 +17,7 @@ serde = { version = "1.0.115", optional = true, default-features = false, featur
# Use f32 as precision, default is f64
f32 = []
sparse = ["sprs"]
serde1 = ["serde", "ndarray/serde"]
[dev-dependencies]
criterion = "0.3.2"

View File

@ -1,8 +1,11 @@
use super::operators::SbpOperator2d;
use crate::Float;
use ndarray::{Array2, ArrayView2};
use ndarray::{Array2, ArrayView2, ArrayViewMut2};
#[cfg(feature = "serde1")]
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct Grid {
pub(crate) x: Array2<Float>,
pub(crate) y: Array2<Float>,
@ -30,12 +33,21 @@ impl Grid {
self.x.shape()[0]
}
pub fn x(&self) -> ndarray::ArrayView2<Float> {
pub fn x(&self) -> ArrayView2<Float> {
self.x.view()
}
pub fn y(&self) -> ndarray::ArrayView2<Float> {
pub fn x_mut(&mut self) -> ArrayViewMut2<Float> {
self.x.view_mut()
}
pub fn y(&self) -> ArrayView2<Float> {
self.y.view()
}
pub fn y_mut(&mut self) -> ArrayViewMut2<Float> {
self.y.view_mut()
}
pub fn components(&mut self) -> (ArrayViewMut2<Float>, ArrayViewMut2<Float>) {
(self.x.view_mut(), self.y.view_mut())
}
pub fn metrics(&self, op: &dyn SbpOperator2d) -> Result<Metrics, ndarray::ShapeError> {
Metrics::new(self, op)