abstract over hdf5::File

This commit is contained in:
Magnus Ulimoen 2020-04-06 23:10:08 +02:00
parent 13abdefe57
commit d3465d5e1e
1 changed files with 76 additions and 70 deletions

View File

@ -422,10 +422,10 @@ fn main() {
let output = if opt.legacy { let output = if opt.legacy {
None None
} else { } else {
Some(create_hdf(&opt.output, sys.grids.as_slice()).unwrap()) Some(File::create(&opt.output, sys.grids.as_slice()).unwrap())
}; };
if let Some(file) = output.as_ref() { if let Some(file) = output.as_ref() {
add_timestep_to_file(&file, 0, sys.fnow.as_slice()).unwrap(); file.add_timestep(0, sys.fnow.as_slice()).unwrap();
} }
let bar = progressbar(opt.no_progressbar, ntime); let bar = progressbar(opt.no_progressbar, ntime);
@ -440,7 +440,7 @@ fn main() {
bar.finish(); bar.finish();
if let Some(file) = output.as_ref() { if let Some(file) = output.as_ref() {
add_timestep_to_file(&file, ntime, sys.fnow.as_slice()).unwrap(); file.add_timestep(ntime, sys.fnow.as_slice()).unwrap();
} else { } else {
legacy_output(&opt.output, &sys); legacy_output(&opt.output, &sys);
} }
@ -491,84 +491,90 @@ fn legacy_output<T: sbp::operators::UpwindOperator, P: AsRef<std::path::Path>>(
} }
} }
fn create_hdf<P: AsRef<std::path::Path>>( #[derive(Debug, Clone)]
path: P, struct File(hdf5::File);
grids: &[sbp::grid::Grid],
) -> Result<hdf5::File, Box<dyn std::error::Error>> {
let gzip = 7;
let file = hdf5::File::create(path.as_ref())?; impl File {
let _tds = file fn create<P: AsRef<std::path::Path>>(
.new_dataset::<u64>() path: P,
.resizable(true) grids: &[sbp::grid::Grid],
.chunk((1,)) ) -> Result<Self, Box<dyn std::error::Error>> {
.create("t", (0,))?; let file = hdf5::File::create(path.as_ref())?;
let _tds = file
.new_dataset::<u64>()
.resizable(true)
.chunk((1,))
.create("t", (0,))?;
for (i, grid) in grids.iter().enumerate() { for (i, grid) in grids.iter().enumerate() {
let g = file.create_group(&i.to_string())?; let g = file.create_group(&i.to_string())?;
g.link_soft("/t", "t").unwrap(); g.link_soft("/t", "t").unwrap();
let add_dim = |name| { let add_dim = |name| {
g.new_dataset::<Float>() g.new_dataset::<Float>()
.gzip(gzip) .chunk((grid.ny(), grid.nx()))
.create(name, (grid.ny(), grid.nx())) .gzip(9)
}; .create(name, (grid.ny(), grid.nx()))
let xds = add_dim("x")?; };
xds.write(grid.x())?; let xds = add_dim("x")?;
let yds = add_dim("y")?; xds.write(grid.x())?;
yds.write(grid.y())?; let yds = add_dim("y")?;
yds.write(grid.y())?;
let add_var = |name| { let add_var = |name| {
g.new_dataset::<Float>() g.new_dataset::<Float>()
.gzip(gzip) .gzip(3)
.chunk((1, grid.ny(), grid.nx())) .shuffle(true)
.resizable_idx(&[true, false, false]) .chunk((1, grid.ny(), grid.nx()))
.create(name, (0, grid.ny(), grid.nx())) .resizable_idx(&[true, false, false])
}; .create(name, (0, grid.ny(), grid.nx()))
add_var("rho")?; };
add_var("rhou")?; add_var("rho")?;
add_var("rhov")?; add_var("rhou")?;
add_var("e")?; add_var("rhov")?;
add_var("e")?;
}
Ok(Self(file))
} }
Ok(file) fn add_timestep(
} &self,
t: u64,
fields: &[euler::Field],
) -> Result<(), Box<dyn std::error::Error>> {
let file = &self.0;
let tds = file.dataset("t")?;
let tpos = tds.size();
tds.resize((tpos + 1,))?;
tds.write_slice(&[t], ndarray::s![tpos..tpos + 1])?;
fn add_timestep_to_file( for (i, fnow) in fields.iter().enumerate() {
file: &hdf5::File, let g = file.group(&i.to_string())?;
t: u64, let (tpos, ny, nx) = {
fields: &[euler::Field], let ds = g.dataset("rho")?;
) -> Result<(), Box<dyn std::error::Error>> { let shape = ds.shape();
let tds = file.dataset("t")?; (shape[0], shape[1], shape[2])
let tpos = tds.size(); };
tds.resize((tpos + 1,))?;
tds.write_slice(&[t], ndarray::s![tpos..tpos + 1])?;
for (i, fnow) in fields.iter().enumerate() { let rhods = g.dataset("rho")?;
let g = file.group(&i.to_string())?; let rhouds = g.dataset("rhou")?;
let (tpos, ny, nx) = { let rhovds = g.dataset("rhov")?;
let ds = g.dataset("rho")?; let eds = g.dataset("e")?;
let shape = ds.shape();
(shape[0], shape[1], shape[2])
};
let rhods = g.dataset("rho")?; let (rho, rhou, rhov, e) = fnow.components();
let rhouds = g.dataset("rhou")?; rhods.resize((tpos + 1, ny, nx))?;
let rhovds = g.dataset("rhov")?; rhods.write_slice(rho, ndarray::s![tpos, .., ..])?;
let eds = g.dataset("e")?;
let (rho, rhou, rhov, e) = fnow.components(); rhouds.resize((tpos + 1, ny, nx))?;
rhods.resize((tpos + 1, ny, nx))?; rhouds.write_slice(rhou, ndarray::s![tpos, .., ..])?;
rhods.write_slice(rho, ndarray::s![tpos, .., ..])?;
rhouds.resize((tpos + 1, ny, nx))?; rhovds.resize((tpos + 1, ny, nx))?;
rhouds.write_slice(rhou, ndarray::s![tpos, .., ..])?; rhovds.write_slice(rhov, ndarray::s![tpos, .., ..])?;
rhovds.resize((tpos + 1, ny, nx))?; eds.resize((tpos + 1, ny, nx))?;
rhovds.write_slice(rhov, ndarray::s![tpos, .., ..])?; eds.write_slice(e, ndarray::s![tpos, .., ..])?;
}
eds.resize((tpos + 1, ny, nx))?; Ok(())
eds.write_slice(e, ndarray::s![tpos, .., ..])?;
} }
Ok(())
} }