cellular_raza_core/backend/chili/
update_cycle.rsuse super::{CellBox, CellIdentifier, SimulationError, SubDomainBox, UpdateCycle, Voxel};
use cellular_raza_concepts::SubDomain;
pub use cellular_raza_concepts::CycleEvent;
#[cfg(feature = "tracing")]
use tracing::instrument;
impl<C, A> Voxel<C, A> {
#[cfg_attr(feature = "tracing", instrument(skip(self, default_from)))]
pub(crate) fn update_cell_cycle_4<
#[cfg(feature = "tracing")] Float: core::fmt::Debug,
#[cfg(not(feature = "tracing"))] Float,
Func,
>(
&mut self,
default_from: &Func,
) -> Result<(), SimulationError>
where
C: cellular_raza_concepts::Cycle<C, Float>,
A: UpdateCycle,
Func: Fn(&C) -> A,
{
self.cells
.iter_mut()
.map(|(cbox, aux_storage)| {
let mut remaining_events = Vec::new();
for event in aux_storage.drain_cycle_events() {
match event {
CycleEvent::Division => {
let new_cell = C::divide(&mut self.rng, &mut cbox.cell)?;
let parent_ident = cbox.identifier;
self.id_counter += 1;
cbox.identifier = CellIdentifier(self.plain_index, self.id_counter);
cbox.parent = Some(parent_ident);
self.new_cells.push((new_cell, Some(parent_ident)));
}
CycleEvent::Remove => remaining_events.push(event),
CycleEvent::PhasedDeath => {
remaining_events.push(event);
}
};
}
aux_storage.set_cycle_events(remaining_events);
Ok(())
})
.collect::<Result<(), SimulationError>>()?;
self.cells.retain(|(_, aux_storage)| {
!aux_storage.get_cycle_events().contains(&CycleEvent::Remove)
});
self.cells
.extend(self.new_cells.drain(..).map(|(cell, parent_id)| {
let aux_storage = default_from(&cell);
self.id_counter += 1;
(
CellBox::new(self.plain_index, self.id_counter, cell, parent_id),
aux_storage,
)
}));
Ok(())
}
}
impl<I, S, C, A, Com, Sy> SubDomainBox<I, S, C, A, Com, Sy>
where
S: SubDomain,
{
#[cfg_attr(feature = "tracing", instrument(skip(self, default_from)))]
pub fn update_cell_cycle_4<
#[cfg(feature = "tracing")] F: core::fmt::Debug,
#[cfg(not(feature = "tracing"))] F,
Func,
>(
&mut self,
default_from: &Func,
) -> Result<(), SimulationError>
where
C: cellular_raza_concepts::Cycle<C, F>,
A: UpdateCycle,
Func: Fn(&C) -> A,
{
self.voxels
.iter_mut()
.map(|(_, vox)| vox.update_cell_cycle_4(default_from))
.collect::<Result<(), SimulationError>>()?;
Ok(())
}
}
pub fn local_cycle_update<C, A, Float>(
cell: &mut C,
aux_storage: &mut A,
dt: Float,
rng: &mut rand_chacha::ChaCha8Rng,
) -> Result<(), cellular_raza_concepts::DeathError>
where
C: cellular_raza_concepts::Cycle<C, Float>,
A: UpdateCycle,
{
if aux_storage
.get_cycle_events()
.contains(&CycleEvent::PhasedDeath)
{
if C::update_conditional_phased_death(rng, &dt, cell)? {
aux_storage.add_cycle_event(CycleEvent::Remove);
}
} else {
if let Some(event) = C::update_cycle(rng, &dt, cell) {
aux_storage.add_cycle_event(event);
}
}
Ok(())
}