SerializeRenderer: render to GenericSim, not StaticSim

This commit is contained in:
2022-07-29 13:27:05 -07:00
parent 95213e61be
commit 604f368f0d
4 changed files with 23 additions and 28 deletions

View File

@@ -125,7 +125,7 @@ impl<S: AbstractSim + 'static> Driver<S> {
impl<S: AbstractSim + Serialize + 'static> Driver<S> {
pub fn add_serializer_renderer(&mut self, out_base: &str, step_frequency: u64, frame_limit: Option<u64>) {
let fmt_str = format!("{out_base}{{step_no}}.bc", out_base=out_base);
self.add_renderer(render::SerializerRenderer::new_static(&*fmt_str), &*fmt_str, step_frequency, frame_limit);
self.add_renderer(render::SerializerRenderer::new_generic(&*fmt_str), &*fmt_str, step_frequency, frame_limit);
}
}

View File

@@ -1,7 +1,7 @@
use crate::geom::Meters;
use crate::real::ToFloat as _;
use crate::cross::vec::{Vec2, Vec3};
use crate::sim::{AbstractSim, Sample, StaticSim};
use crate::sim::{AbstractSim, GenericSim, Sample};
use crate::meas::{self, AbstractMeasurement};
use crossterm::{cursor, QueueableCommand as _};
use crossterm::style::{style, Color, PrintStyledContent, Stylize as _};
@@ -562,7 +562,7 @@ impl<S: AbstractSim> Renderer<S> for MultiRenderer<S> {
}
#[derive(Serialize, Deserialize)]
pub struct SerializedFrame<S=StaticSim> {
pub struct SerializedFrame<S> {
pub state: S,
/// although not generally necessary to load the sim, saving the measurements is beneficial for
/// post-processing.
@@ -570,20 +570,20 @@ pub struct SerializedFrame<S=StaticSim> {
}
impl<S: AbstractSim> SerializedFrame<S> {
pub fn to_static(self) -> SerializedFrame<StaticSim> {
pub fn to_generic(self) -> SerializedFrame<GenericSim<S::Real>> {
SerializedFrame {
state: AbstractSim::to_static(&self.state),
state: AbstractSim::to_generic(&self.state),
measurements: self.measurements,
}
}
}
/// this serializes the simulation state plus measurements to disk.
/// it can either convert the state to a generic, material-agnostic format (static)
/// it can either convert the state to a generic, material-agnostic format (generic)
/// or dump it as-is.
pub struct SerializerRenderer {
fmt_str: String,
prefer_static: bool,
prefer_generic: bool,
}
impl SerializerRenderer {
@@ -592,16 +592,16 @@ impl SerializerRenderer {
pub fn new(fmt_str: &str) -> Self {
Self {
fmt_str: fmt_str.into(),
prefer_static: false,
prefer_generic: false,
}
}
/// Same as `new`, but cast to StaticSim before serializing. This yields a file that's easier
/// for post-processing, and may be smaller in size.
pub fn new_static(fmt_str: &str) -> Self {
/// Same as `new`, but cast to GenericSim before serializing. This yields a file that's easier
/// for post-processing.
pub fn new_generic(fmt_str: &str) -> Self {
Self {
fmt_str: fmt_str.into(),
prefer_static: true,
prefer_generic: true,
}
}
}
@@ -629,8 +629,8 @@ impl<S: AbstractSim + Serialize> Renderer<S> for SerializerRenderer {
default_render_z_slice(self, state, z, measurements, config)
}
fn render(&self, state: &S, measurements: &[&dyn AbstractMeasurement<S>], _config: RenderConfig) {
if self.prefer_static {
self.serialize(&state.to_static(), meas::eval_to_vec(state, measurements));
if self.prefer_generic {
self.serialize(&state.to_generic(), meas::eval_to_vec(state, measurements));
} else {
self.serialize(state, meas::eval_to_vec(state, measurements));
}

View File

@@ -110,7 +110,7 @@ impl<R: Real> CellStateWithM<R> {
// TODO: the Sync bound here could be removed with some refactoring
pub trait AbstractSim: Sync {
type Real;
type Real: Real;
type Material;
fn meta(&self) -> SimMeta<f32>;
fn step_no(&self) -> u64;

View File

@@ -1,7 +1,7 @@
//! Post-processing tools
use coremem::meas;
use coremem::render::{ColorTermRenderer, Renderer as _, RenderConfig, SerializedFrame};
use coremem::sim::{AbstractSim, StaticSim};
use coremem::sim::{AbstractSim, GenericSim};
use itertools::Itertools as _;
use lru::LruCache;
@@ -9,7 +9,6 @@ use rayon::{ThreadPool, ThreadPoolBuilder};
use std::collections::HashSet;
use std::fs::{DirEntry, File, read_dir};
use std::io::BufReader;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::mpsc::{self, Receiver, Sender};
@@ -33,25 +32,21 @@ pub type Result<T> = std::result::Result<T, Error>;
pub struct Frame {
path: PathBuf,
data: SerializedFrame<StaticSim>,
data: SerializedFrame<GenericSim<f32>>,
}
impl Frame {
pub fn measurements(&self) -> &[meas::Evaluated] {
&*self.data.measurements
}
pub fn sim(&self) -> &GenericSim<f32> {
&self.data.state
}
pub fn path(&self) -> &Path {
&*self.path
}
}
impl Deref for Frame {
type Target = StaticSim;
fn deref(&self) -> &Self::Target {
&self.data.state
}
}
#[derive(Default)]
pub struct Loader {
dir: PathBuf,
@@ -252,7 +247,7 @@ impl Viewer {
let mut cache = LoaderCache::new(loader, 6, 6);
let viewing = cache.load_first();
Self {
z: viewing.depth() / 2,
z: viewing.sim().depth() / 2,
viewing,
cache,
renderer: Default::default(),
@@ -262,7 +257,7 @@ impl Viewer {
}
pub fn navigate(&mut self, time_steps: isize, z_steps: i32) {
let new_z = (self.z as i32).saturating_add(z_steps);
let new_z = new_z.max(0).min(self.viewing.depth() as i32 - 1) as u32;
let new_z = new_z.max(0).min(self.viewing.sim().depth() as i32 - 1) as u32;
if time_steps == 0 && new_z == self.z && self.render_config == self.last_config {
return;
}
@@ -274,7 +269,7 @@ impl Viewer {
}
pub fn render(&self) {
self.renderer.render_z_slice(
&**self.viewing,
self.viewing.sim(),
self.z,
&*meas::as_dyn_measurements(self.viewing.measurements()),
self.render_config,