cellular_raza_core/backend/chili/
update_cycle.rs

1use super::{CellBox, CellIdentifier, SimulationError, SubDomainBox, UpdateCycle, Voxel};
2use cellular_raza_concepts::SubDomain;
3
4pub use cellular_raza_concepts::CycleEvent;
5
6#[cfg(feature = "tracing")]
7use tracing::instrument;
8
9impl<C, A> Voxel<C, A> {
10    #[cfg_attr(feature = "tracing", instrument(skip(self, default_from)))]
11    pub(crate) fn update_cell_cycle_4<
12        #[cfg(feature = "tracing")] Float: core::fmt::Debug,
13        #[cfg(not(feature = "tracing"))] Float,
14        Func,
15    >(
16        &mut self,
17        default_from: &Func,
18    ) -> Result<(), SimulationError>
19    where
20        C: cellular_raza_concepts::Cycle<C, Float>,
21        A: UpdateCycle,
22        Func: Fn(&C) -> A,
23    {
24        // Update the cell individual cells
25        self.cells
26            .iter_mut()
27            .map(|(cbox, aux_storage)| {
28                // Check for cycle events and take action if necessary
29                let mut remaining_events = Vec::new();
30                for event in aux_storage.drain_cycle_events() {
31                    match event {
32                        CycleEvent::Division => {
33                            let new_cell = C::divide(&mut self.rng, &mut cbox.cell)?;
34                            let parent_ident = cbox.identifier;
35                            self.id_counter += 1;
36                            cbox.identifier =
37                                CellIdentifier::Division(self.plain_index, self.id_counter);
38                            cbox.parent = Some(parent_ident);
39                            self.new_cells.push((new_cell, Some(parent_ident)));
40                        }
41                        CycleEvent::Remove => remaining_events.push(event),
42                        CycleEvent::PhasedDeath => {
43                            remaining_events.push(event);
44                        }
45                    };
46                }
47                aux_storage.set_cycle_events(remaining_events);
48                Ok(())
49            })
50            .collect::<Result<(), SimulationError>>()?;
51
52        // Remove cells which are flagged for death
53        self.cells.retain(|(_, aux_storage)| {
54            !aux_storage.get_cycle_events().contains(&CycleEvent::Remove)
55        });
56
57        // Include new cells
58        self.cells
59            .extend(self.new_cells.drain(..).map(|(cell, parent_id)| {
60                let aux_storage = default_from(&cell);
61                self.id_counter += 1;
62                (
63                    CellBox::new(self.plain_index, self.id_counter, cell, parent_id),
64                    aux_storage,
65                )
66            }));
67        Ok(())
68    }
69}
70
71impl<I, S, C, A, Com, Sy> SubDomainBox<I, S, C, A, Com, Sy>
72where
73    S: SubDomain,
74{
75    /// Separate function to update the cell cycle
76    ///
77    /// Instead of running one big update function for all local rules, we have to treat this cell
78    /// cycle differently since new cells could be generated and thus have consequences for other
79    /// update steps as well.
80    #[cfg_attr(feature = "tracing", instrument(skip(self, default_from)))]
81    pub fn update_cell_cycle_4<
82        #[cfg(feature = "tracing")] F: core::fmt::Debug,
83        #[cfg(not(feature = "tracing"))] F,
84        Func,
85    >(
86        &mut self,
87        default_from: &Func,
88    ) -> Result<(), SimulationError>
89    where
90        C: cellular_raza_concepts::Cycle<C, F>,
91        A: UpdateCycle,
92        Func: Fn(&C) -> A,
93    {
94        self.voxels
95            .iter_mut()
96            .map(|(_, vox)| vox.update_cell_cycle_4(default_from))
97            .collect::<Result<(), SimulationError>>()?;
98        Ok(())
99    }
100}
101
102/// Advances the cycle of a cell by a small time increment `dt`.
103pub fn local_cycle_update<C, A, Float>(
104    cell: &mut C,
105    aux_storage: &mut A,
106    dt: Float,
107    rng: &mut rand_chacha::ChaCha8Rng,
108) -> Result<(), cellular_raza_concepts::DeathError>
109where
110    C: cellular_raza_concepts::Cycle<C, Float>,
111    A: UpdateCycle,
112{
113    // Update the cell cycle
114    if aux_storage
115        .get_cycle_events()
116        .contains(&CycleEvent::PhasedDeath)
117    {
118        if C::update_conditional_phased_death(rng, &dt, cell)? {
119            aux_storage.add_cycle_event(CycleEvent::Remove);
120        }
121    } else {
122        if let Some(event) = C::update_cycle(rng, &dt, cell) {
123            aux_storage.add_cycle_event(event);
124        }
125    }
126    Ok(())
127}