add a to_generic method to the AbstractSim trait

This commit is contained in:
2022-07-29 13:11:16 -07:00
parent 02920f9bd3
commit 95ffb73fe3
3 changed files with 27 additions and 5 deletions

View File

@@ -4,7 +4,7 @@ use crate::geom::{Coord, Index, Meters};
use crate::cross::real::{R32, Real};
use crate::cross::step::SimMeta;
use crate::cross::vec::{Vec3, Vec3u};
use crate::sim::{CellStateWithM, AbstractSim, Sample, StaticSim};
use crate::sim::{AbstractSim, CellStateWithM, GenericSim, Sample, StaticSim};
use crate::stim::AbstractStimulus;
use mat::{GenericMaterial, Material, MaterialExt as _};
@@ -315,6 +315,7 @@ impl<R: Real, M: Material<R> + Send + Sync> SimState<R, M> {
}
impl<R: Real, M: Material<R> + Send + Sync> AbstractSim for SimState<R, M> {
type Real = R;
type Material = M;
fn meta(&self) -> SimMeta<f32> {
let dim = Vec3u::new(
@@ -368,6 +369,9 @@ impl<R: Real, M: Material<R> + Send + Sync> AbstractSim for SimState<R, M> {
fn to_static(&self) -> StaticSim {
unimplemented!()
}
fn to_generic(&self) -> GenericSim<Self::Real> {
unimplemented!()
}
}
#[allow(unused)]

View File

@@ -1,5 +1,5 @@
use crate::geom::{Coord, Cube, Index, InvertedRegion, Meters, Region};
use crate::cross::mat::Vacuum;
use crate::cross::mat::{FullyGenericMaterial, Vacuum};
use crate::cross::real::Real;
use crate::cross::step::SimMeta;
use crate::cross::vec::{Vec3, Vec3u};
@@ -15,6 +15,7 @@ pub mod units;
use spirv::{CpuBackend, SpirvSim};
pub type StaticSim = SpirvSim<f32, Vacuum, CpuBackend>;
pub type GenericSim<R> = SpirvSim<R, FullyGenericMaterial<R>, CpuBackend>;
/// Conceptually, one cell looks like this (in 2d):
@@ -109,6 +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 Material;
fn meta(&self) -> SimMeta<f32>;
fn step_no(&self) -> u64;
@@ -117,6 +119,8 @@ pub trait AbstractSim: Sync {
/// Take a "snapshot" of the simulation, dropping all material-specific information.
fn to_static(&self) -> StaticSim;
fn to_generic(&self) -> GenericSim<Self::Real>;
fn put_material<C: Coord, M: Into<Self::Material>>(&mut self, pos: C, mat: M);
fn get_material<C: Coord>(&self, pos: C) -> &Self::Material;

View File

@@ -4,7 +4,7 @@ use log::{info, trace, warn};
use crate::geom::{Coord, Index, Meters};
use crate::real::Real;
use crate::sim::{CellStateWithM, AbstractSim, Sample, StaticSim};
use crate::sim::{AbstractSim, CellStateWithM, GenericSim, Sample, StaticSim};
use crate::stim::AbstractStimulus;
use crate::cross::vec::Vec3;
use coremem_cross::mat::{FullyGenericMaterial, Material};
@@ -72,9 +72,10 @@ impl<R: Clone, M: Clone, B: Default> Clone for SpirvSim<R, M, B> {
impl<R, M, B> AbstractSim for SpirvSim<R, M, B>
where
R: Real,
M: Send + Sync + Material<R>,
M: Send + Sync + Material<R> + Clone + Into<FullyGenericMaterial<R>>,
B: Send + Sync + SimBackend<R, M>,
{
type Real = R;
type Material = M;
fn meta(&self) -> SimMeta<f32> {
@@ -141,6 +142,19 @@ where
backend: Default::default(),
}
}
fn to_generic(&self) -> GenericSim<Self::Real> {
let mat = self.mat.iter().map(|m| m.clone().into()).collect();
GenericSim {
meta: self.meta,
e: self.e.clone(),
h: self.h.clone(),
m: self.m.clone(),
mat,
step_no: self.step_no,
backend: Default::default(),
}
}
}
impl<R: Real, M: Default, B> SpirvSim<R, M, B>
@@ -194,7 +208,7 @@ impl<R, M, B> SpirvSim<R, M, B> {
impl<R, M, B> SpirvSim<R, M, B>
where
R: Real,
M: Send + Sync + Material<R>,
M: Send + Sync + Material<R> + Clone + Into<FullyGenericMaterial<R>>,
B: Send + Sync + SimBackend<R, M>,
{
#[allow(unused)] // used for test