cellular_raza_core/backend/chili/
aux_storage.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
use cellular_raza_concepts::CycleEvent;
pub use circ_buffer::*;
use serde::{Deserialize, Serialize};

use std::ops::{Deref, DerefMut};

pub use cellular_raza_concepts::Xapy;

use super::{CellIdentifier, VoxelPlainIndex};

/// Wrapper around the user-defined CellAgent
///
/// This wrapper serves to provide a unique identifier and the option to specify
/// the parent of the current cell.
#[derive(Clone, Deserialize, Serialize)]
pub struct CellBox<C> {
    /// The identifier is composed of two values, one for the voxel index in which the
    /// object was created and another one which counts how many elements have already
    /// been created there.
    pub identifier: CellIdentifier,
    /// Identifier of the parent cell if this cell was created by cell-division
    pub parent: Option<CellIdentifier>,
    /// The cell which is encapsulated by this box.
    pub cell: C,
}

impl<C> Deref for CellBox<C> {
    type Target = C;

    fn deref(&self) -> &Self::Target {
        &self.cell
    }
}

impl<C> DerefMut for CellBox<C> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.cell
    }
}

impl<C> cellular_raza_concepts::Id for CellBox<C> {
    type Identifier = CellIdentifier;

    fn get_id(&self) -> CellIdentifier {
        self.identifier
    }

    fn ref_id(&self) -> &Self::Identifier {
        &self.identifier
    }
}

impl<C> CellBox<C> {
    /// Create a new [CellBox] at a specific voxel with a voxel-unique number
    /// of cells that has already been created at this position.
    pub fn new(
        voxel_index: VoxelPlainIndex,
        n_cell: u64,
        cell: C,
        parent: Option<CellIdentifier>,
    ) -> CellBox<C> {
        CellBox::<C> {
            identifier: CellIdentifier(voxel_index, n_cell),
            parent,
            cell,
        }
    }
}

// --------------------------------- UPDATE-MECHANICS --------------------------------
/// Used to store intermediate information about last positions and velocities.
/// Can store up to `N` values.
pub trait UpdateMechanics<Pos, Vel, For, const N: usize> {
    /// Stores the last position of the cell. May overwrite old results depending on
    /// how many old results are being stored.
    fn set_last_position(&mut self, pos: Pos);

    /// Get all previous positions. This number maybe smaller than the maximum number of stored
    /// positions but never exceeds it.
    fn previous_positions<'a>(&'a self) -> RingBufferIterRef<'a, Pos, N>;

    /// Stores the last velocity of the cell. Overwrites old results when stored amount
    /// exceeds number of maximum stored values.
    fn set_last_velocity(&mut self, vel: Vel);

    /// Get all previous velocities. This number may be smaller than the maximum number of stored
    /// velocities but never exceeds it.
    fn previous_velocities<'a>(&'a self) -> RingBufferIterRef<'a, Vel, N>;

    /// Get the number of previous values currently stored
    ///
    /// This number is by definition between 0 and `N`.
    fn n_previous_values(&self) -> usize;

    /// Add force to currently stored forces
    fn add_force(&mut self, force: For);

    /// Obtain current force on cell
    fn get_current_force_and_reset(&mut self) -> For;
}

/// Stores intermediate information about the mechanics of a cell.
#[derive(Clone, Deserialize, Serialize)]
pub struct AuxStorageMechanics<Pos, Vel, For, const N: usize> {
    positions: RingBuffer<Pos, N>,
    velocities: RingBuffer<Vel, N>,
    current_force: For,
    zero_force: For,
}

// It is necessary to implement this trait by hand since with the current version of the Mechanics
// concept, we need to specify next_random_mechanics_update: Some(0.0) in order for any updates to
// be done at all.
impl<Pos, Vel, For, const N: usize> Default for AuxStorageMechanics<Pos, Vel, For, N>
where
    For: num::Zero,
{
    fn default() -> Self {
        Self {
            positions: RingBuffer::<Pos, N>::default(),
            velocities: RingBuffer::<Vel, N>::default(),
            current_force: num::Zero::zero(),
            zero_force: num::Zero::zero(),
        }
    }
}

/// Used to construct initial (empty) AuxStorage variants.
pub trait DefaultFrom<T> {
    /// Constructs the Type in question from a given value. This is typically a zero value.
    /// If it can be constructed from the [num::Zero] trait, this method is not required and
    /// `cellular_raza` will determine the initial zero-value correctly.
    /// For other types (ie. dynamically-sized ones) additional entries may be necessary.
    fn default_from(value: &T) -> Self;
}

impl<Pos, Vel, For, const N: usize> DefaultFrom<For> for AuxStorageMechanics<Pos, Vel, For, N>
where
    For: Clone,
{
    fn default_from(value: &For) -> Self {
        let force: For = value.clone().into();
        Self {
            positions: RingBuffer::default(),
            velocities: RingBuffer::default(),
            current_force: force.clone(),
            zero_force: force.clone(),
        }
    }
}

impl<Pos, Vel, For, const N: usize> UpdateMechanics<Pos, Vel, For, N>
    for AuxStorageMechanics<Pos, Vel, For, N>
where
    For: Clone + core::ops::AddAssign<For>,
{
    #[inline]
    fn previous_positions<'a>(&'a self) -> RingBufferIterRef<'a, Pos, N> {
        self.positions.iter()
    }

    #[inline]
    fn previous_velocities<'a>(&'a self) -> RingBufferIterRef<'a, Vel, N> {
        self.velocities.iter()
    }

    #[inline]
    fn n_previous_values(&self) -> usize {
        self.positions.get_size()
    }

    #[inline]
    fn set_last_position(&mut self, pos: Pos) {
        self.positions.push(pos);
    }

    #[inline]
    fn set_last_velocity(&mut self, vel: Vel) {
        self.velocities.push(vel);
    }

    #[inline]
    fn add_force(&mut self, force: For) {
        self.current_force += force;
    }

    #[inline]
    fn get_current_force_and_reset(&mut self) -> For {
        let f = self.current_force.clone();
        self.current_force = self.zero_force.clone();
        f
    }
}

// ----------------------------------- UPDATE-CYCLE ----------------------------------
/// Trait which describes how to store intermediate
/// information on the cell cycle.
pub trait UpdateCycle {
    /// Set all cycle events. This function is currently the
    /// only way to change the contents of the stored events.
    fn set_cycle_events(&mut self, events: Vec<CycleEvent>);

    /// Get all cycle events currently stored.
    fn get_cycle_events(&self) -> &Vec<CycleEvent>;

    /// Drain all cycle events
    fn drain_cycle_events(&mut self) -> std::vec::Drain<CycleEvent>;

    /// Add another cycle event to the storage.
    fn add_cycle_event(&mut self, event: CycleEvent);
}

/// Stores intermediate information about the cell cycle.
///
/// This struct is used in the [build_aux_storage](crate::backend::chili::build_aux_storage) macro.
/// It can in principle also be re-used on its own since it implements the [UpdateCycle] trait.
///
/// ```
/// use cellular_raza_core::backend::chili::{AuxStorageCycle,UpdateCycle};
/// use cellular_raza_concepts::CycleEvent;
///
/// // Construct a new empty AuxStorageCycle
/// let mut aux_storage_cycle = AuxStorageCycle::default();
///
/// // Add one element
/// aux_storage_cycle.add_cycle_event(CycleEvent::Division);
/// assert_eq!(aux_storage_cycle.get_cycle_events().len(), 1);
///
/// // Drain all elements currently present
/// let events = aux_storage_cycle.drain_cycle_events();
/// assert_eq!(events.len(), 1);
/// ```
#[derive(Clone, Default, Deserialize, Serialize)]
pub struct AuxStorageCycle {
    cycle_events: Vec<CycleEvent>,
}

impl UpdateCycle for AuxStorageCycle {
    #[inline]
    fn set_cycle_events(&mut self, events: Vec<CycleEvent>) {
        self.cycle_events = events;
    }

    #[inline]
    fn get_cycle_events(&self) -> &Vec<CycleEvent> {
        &self.cycle_events
    }

    #[inline]
    fn drain_cycle_events(&mut self) -> std::vec::Drain<CycleEvent> {
        self.cycle_events.drain(..)
    }

    #[inline]
    fn add_cycle_event(&mut self, event: CycleEvent) {
        self.cycle_events.push(event);
    }
}

// --------------------------------- UPDATE-REACTIONS --------------------------------
/// Interface to store intermediate information about cellular reactions.
pub trait UpdateReactions<Ri> {
    /// Set the value of intracellular concentrations
    fn set_conc(&mut self, conc: Ri);
    /// Obtain the current value of intracellular concentrations
    fn get_conc(&self) -> Ri;
    /// Add concentrations to the current value
    fn incr_conc(&mut self, incr: Ri);
}

/// Helper storage for values regarding intracellular concentrations for the
/// [Reactions](cellular_raza_concepts::Reactions) trait.
#[derive(Clone, Default, Deserialize, Serialize)]
pub struct AuxStorageReactions<Ri> {
    concentration: Ri,
}

impl<Ri> DefaultFrom<Ri> for AuxStorageReactions<Ri>
where
    Ri: Clone,
{
    fn default_from(value: &Ri) -> Self {
        AuxStorageReactions {
            concentration: value.clone(),
        }
    }
}

impl<R> UpdateReactions<R> for AuxStorageReactions<R>
where
    R: Clone + core::ops::Add<R, Output = R>,
{
    #[inline]
    fn get_conc(&self) -> R {
        self.concentration.clone()
    }

    #[inline]
    fn incr_conc(&mut self, incr: R) {
        self.concentration = self.concentration.clone() + incr;
    }

    #[inline]
    fn set_conc(&mut self, conc: R) {
        self.concentration = conc;
    }
}

/// Used to update properties of the cell related to the
/// [ReactionsContact](cellular_raza_concepts::ReactionsContact) trait.
pub trait UpdateReactionsContact<Ri, const N: usize> {
    /// Sets the current contact reactions increment
    fn set_current_increment(&mut self, new_increment: Ri);
    /// Adds to the current increment
    fn incr_current_increment(&mut self, increment: Ri);
    /// Obtains the current increment
    fn get_current_increment(&self) -> Ri;
    /// Obtain previous increments used for adams_bashforth integrators
    fn previous_increments<'a>(&'a self) -> RingBufferIterRef<'a, Ri, N>;
    /// Set the last increment in the ring buffer
    fn set_last_increment(&mut self, increment: Ri);
    /// Get the number of previous values to match against [circ_buffer::RingBufferIterRef]
    fn n_previous_values(&self) -> usize;
}

/// Implementor of the [UpdateReactionsContact] trait.
#[derive(Clone, Default, Deserialize, Serialize)]
pub struct AuxStorageReactionsContact<Ri, const N: usize> {
    current_increment: Ri,
    increments: RingBuffer<Ri, N>,
}

impl<Ri, const N: usize> DefaultFrom<Ri> for AuxStorageReactionsContact<Ri, N>
where
    Ri: Clone,
{
    fn default_from(value: &Ri) -> Self {
        AuxStorageReactionsContact {
            current_increment: value.clone(),
            increments: Default::default(),
        }
    }
}

impl<Ri, const N: usize> UpdateReactionsContact<Ri, N> for AuxStorageReactionsContact<Ri, N>
where
    Ri: Clone + core::ops::Add<Ri, Output = Ri>,
{
    #[inline]
    fn get_current_increment(&self) -> Ri {
        self.current_increment.clone()
    }

    #[inline]
    fn incr_current_increment(&mut self, increment: Ri) {
        self.current_increment = self.current_increment.clone() + increment;
    }

    #[inline]
    fn set_current_increment(&mut self, new_increment: Ri) {
        self.current_increment = new_increment;
    }

    #[inline]
    fn previous_increments<'a>(&'a self) -> RingBufferIterRef<'a, Ri, N> {
        self.increments.iter()
    }

    #[inline]
    fn set_last_increment(&mut self, increment: Ri) {
        self.increments.push(increment)
    }

    #[inline]
    fn n_previous_values(&self) -> usize {
        self.increments.get_size()
    }
}

// -------------------------------- UPDATE-Interaction -------------------------------
/// Interface to store intermediate information about interactions.
pub trait UpdateInteraction {
    /// Obtain current number of neighbors
    fn get_current_neighbors(&self) -> usize;
    /// Set the number of neighbors
    fn set_current_neighbors(&mut self, neighbors: usize);
    /// Increment the number of current neighbors by the provided value
    fn incr_current_neighbors(&mut self, neighbors: usize);
}

/// Helper storage for number of neighbors of
/// [Interaction](cellular_raza_concepts::Interaction) trait.
#[derive(Clone, Default, Deserialize, Serialize)]
pub struct AuxStorageInteraction {
    neighbor_count: usize,
}

impl UpdateInteraction for AuxStorageInteraction {
    #[inline]
    fn get_current_neighbors(&self) -> usize {
        self.neighbor_count
    }

    #[inline]
    fn incr_current_neighbors(&mut self, neighbors: usize) {
        self.neighbor_count += neighbors;
    }

    #[inline]
    fn set_current_neighbors(&mut self, neighbors: usize) {
        self.neighbor_count = neighbors;
    }
}

#[allow(unused)]
#[doc(hidden)]
mod test_derive_aux_storage_compile {
    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    ///
    /// #[derive(AuxStorage)]
    /// struct TestStructMechanics<Pos, Vel, For, const N: usize> {
    ///     #[UpdateMechanics(Pos, Vel, For, N)]
    ///     aux_mechanics: AuxStorageMechanics<Pos, Vel, For, N>,
    /// }
    /// ```
    fn mechanics_default() {}

    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    ///
    /// #[derive(AuxStorage)]
    /// pub struct TestStructMechanics<Pos, Vel, For, const N: usize> {
    ///     #[UpdateMechanics(Pos, Vel, For, N)]
    ///     aux_mechanics: AuxStorageMechanics<Pos, Vel, For, N>,
    /// }
    /// ```
    fn mechanics_visibility_1() {}

    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    ///
    /// #[derive(AuxStorage)]
    /// pub(crate) struct TestStructMechanics<Pos, Vel, For, const N: usize> {
    ///     #[UpdateMechanics(Pos, Vel, For, N)]
    ///     aux_mechanics: AuxStorageMechanics<Pos, Vel, For, N>,
    /// }
    /// ```
    fn mechanics_visibility_2() {}

    /// ```
    /// mod some_module {
    ///     use cellular_raza_core::backend::chili::AuxStorage;
    ///     use cellular_raza_core::backend::chili::*;
    ///
    ///     #[derive(AuxStorage)]
    ///     pub(super) struct TestStructMechanics<Pos, Vel, For, const N: usize> {
    ///         #[UpdateMechanics(Pos, Vel, For, N)]
    ///         aux_mechanics: AuxStorageMechanics<Pos, Vel, For, N>,
    ///     }
    /// }
    /// fn use_impl<T, Pos, Vel, For, const N: usize>(
    ///     mut aux_storage: T
    /// ) -> For
    /// where
    ///     T: cellular_raza_core::backend::chili::UpdateMechanics<Pos, Vel, For, N>,
    /// {
    ///     aux_storage.get_current_force_and_reset()
    /// }
    /// ```
    fn mechanics_visibility_3() {}

    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    ///
    /// #[derive(AuxStorage)]
    /// struct TestStructMechanics<Pos, Vel, For, T, const N: usize> {
    ///     #[UpdateMechanics(Pos, Vel, For, N)]
    ///     aux_mechanics: AuxStorageMechanics<Pos, Vel, For, N>,
    ///     other: T,
    /// }
    /// ```
    fn mechanics_more_struct_generics() {}

    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    ///
    /// #[derive(AuxStorage)]
    /// struct TestStructMechanics<Pos, Vel, For, const N: usize, const M: usize> {
    ///     #[UpdateMechanics(Pos, Vel, For, N)]
    ///     aux_mechanics: AuxStorageMechanics<Pos, Vel, For, N>,
    ///     count: [i64; M],
    /// }
    /// ```
    fn mechanics_more_struct_const_generics() {}

    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    ///
    /// #[derive(AuxStorage)]
    /// struct TestStructMechanics<Pos, Vel, For, const N: usize>
    /// where
    ///     Pos: Clone,
    /// {
    ///     #[UpdateMechanics(Pos, Vel, For, N)]
    ///     aux_mechanics: AuxStorageMechanics<Pos, Vel, For, N>,
    /// }
    /// ```
    fn mechanics_where_clause() {}

    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    ///
    /// #[derive(AuxStorage)]
    /// struct TestStructMechanics<Pos, Vel, For, const N: usize> {
    ///     #[UpdateMechanics(Pos, Vel, For, N)]
    ///     #[cfg(not(test))]
    ///     aux_mechanics: AuxStorageMechanics<Pos, Vel, For, N>,
    /// }
    /// ```
    fn mechanics_other_attributes() {}

    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    /// use cellular_raza_concepts::CycleEvent;
    ///
    /// #[derive(AuxStorage)]
    /// struct TestStructCycle {
    ///     #[UpdateCycle]
    ///     aux_cycle: AuxStorageCycle,
    /// }
    /// ```
    fn cycle_default() {}

    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    /// use cellular_raza_concepts::CycleEvent;
    ///
    /// #[derive(AuxStorage)]
    /// pub struct TestStructCycle {
    ///     #[UpdateCycle]
    ///     aux_cycle: AuxStorageCycle,
    /// }
    /// ```
    fn cycle_visibility_1() {}

    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    /// use cellular_raza_concepts::CycleEvent;
    ///
    /// #[derive(AuxStorage)]
    /// pub(crate) struct TestStructCycle {
    ///     #[UpdateCycle]
    ///     aux_cycle: AuxStorageCycle,
    /// }
    /// ```
    fn cycle_visibility_2() {}

    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    /// use cellular_raza_concepts::CycleEvent;
    ///
    /// #[derive(AuxStorage)]
    /// pub struct TestStructCycle<T> {
    ///     #[UpdateCycle]
    ///     aux_cycle: AuxStorageCycle,
    ///     _generic: T,
    /// }
    /// ```
    fn cycle_generic_param() {}

    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    /// use cellular_raza_concepts::CycleEvent;
    ///
    /// #[derive(AuxStorage)]
    /// pub struct TestStructCycle<const N: usize> {
    ///     #[UpdateCycle]
    ///     aux_cycle: AuxStorageCycle,
    ///     _generic: [f64; N],
    /// }
    /// ```
    fn cycle_const_generic_param() {}

    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    /// use cellular_raza_concepts::CycleEvent;
    ///
    /// #[derive(AuxStorage)]
    /// pub struct TestStructCycle<T>
    /// where
    ///     T: Clone,
    /// {
    ///     #[UpdateCycle]
    ///     aux_cycle: AuxStorageCycle,
    ///     _generic: T,
    /// }
    /// ```
    fn cycle_where_clause() {}

    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    /// use cellular_raza_concepts::CycleEvent;
    ///
    /// #[derive(AuxStorage)]
    /// struct TestStructCycle {
    ///     #[UpdateCycle]
    ///     #[cfg(not(test))]
    ///     aux_cycle: AuxStorageCycle,
    /// }
    /// ```
    fn cycle_other_attributes() {}

    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    ///
    /// #[derive(AuxStorage)]
    /// struct TestStructReactions<R> {
    ///     #[UpdateReactions(R)]
    ///     aux_cycle: AuxStorageReactions<R>,
    /// }
    /// ```
    fn reactions_default() {}

    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    ///
    /// #[derive(AuxStorage)]
    /// pub struct TestStructReactions<R> {
    ///     #[UpdateReactions(R)]
    ///     aux_cycle: AuxStorageReactions<R>,
    /// }
    /// ```
    fn reactions_visibility_1() {}

    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    ///
    /// #[derive(AuxStorage)]
    /// pub(crate) struct TestStructReactions<R> {
    ///     #[UpdateReactions(R)]
    ///     aux_cycle: AuxStorageReactions<R>,
    /// }
    /// ```
    fn reactions_visibility_2() {}

    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    ///
    /// #[derive(AuxStorage)]
    /// struct TestStructReactions<T, R> {
    ///     #[UpdateReactions(R)]
    ///     aux_cycle: AuxStorageReactions<R>,
    ///     generic: T,
    /// }
    /// ```
    fn reactions_generic_param() {}

    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    ///
    /// #[derive(AuxStorage)]
    /// struct TestStructReactions<R, const N: usize> {
    ///     #[UpdateReactions(R)]
    ///     aux_cycle: AuxStorageReactions<R>,
    ///     generic_array: [usize; N],
    /// }
    /// ```
    fn reactions_const_generic_param() {}

    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    ///
    /// #[derive(AuxStorage)]
    /// struct TestStructReactions<T, R>
    /// where
    ///     T: Clone,
    /// {
    ///     #[UpdateReactions(R)]
    ///     aux_cycle: AuxStorageReactions<R>,
    ///     generic: T,
    /// }
    /// ```
    fn reactions_where_clause() {}

    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    ///
    /// #[derive(AuxStorage)]
    /// struct TestStructReactions<R> {
    ///     #[cfg(not(test))]
    ///     #[UpdateReactions(R)]
    ///     aux_cycle: AuxStorageReactions<R>,
    /// }
    /// ```
    fn reactions_other_attributes() {}

    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    ///
    /// #[derive(AuxStorage)]
    /// struct TestStructInteraction {
    ///     #[UpdateInteraction]
    ///     aux_interaction: AuxStorageInteraction,
    /// }
    /// ```
    fn interactions_default() {}

    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    ///
    /// #[derive(AuxStorage)]
    /// pub struct TestStructInteraction {
    ///     #[UpdateInteraction]
    ///     aux_interaction: AuxStorageInteraction,
    /// }
    /// ```
    fn interactions_visibility_1() {}

    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    ///
    /// #[derive(AuxStorage)]
    /// pub(crate) struct TestStructInteraction {
    ///     #[UpdateInteraction]
    ///     aux_interaction: AuxStorageInteraction,
    /// }
    /// ```
    fn interactions_visibility_2() {}

    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    ///
    /// #[derive(AuxStorage)]
    /// struct TestStructInteraction<T> {
    ///     #[UpdateInteraction]
    ///     aux_interaction: AuxStorageInteraction,
    ///     generic: T,
    /// }
    /// ```
    fn interactions_generic_param() {}

    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    ///
    /// #[derive(AuxStorage)]
    /// struct TestStructInteraction<const N: usize> {
    ///     #[UpdateInteraction]
    ///     aux_interaction: AuxStorageInteraction,
    ///     generic: [f64; N],
    /// }
    /// ```
    fn interactions_const_generic_param() {}

    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    ///
    /// #[derive(AuxStorage)]
    /// struct TestStructInteraction<T>
    /// where
    ///     T: Clone,
    /// {
    ///     #[UpdateInteraction]
    ///     aux_interaction: AuxStorageInteraction,
    ///     generic: T,
    /// }
    /// ```
    fn interactions_where_clause() {}

    /// ```
    /// use cellular_raza_core::backend::chili::AuxStorage;
    /// use cellular_raza_core::backend::chili::*;
    ///
    /// #[derive(AuxStorage)]
    /// struct TestStructInteraction {
    ///     #[UpdateInteraction]
    ///     #[cfg(not(test))]
    ///     aux_interaction: AuxStorageInteraction,
    /// }
    /// ```
    fn interactions_other_attributes() {}
}

#[cfg(test)]
mod test_derive_aux_storage {
    use super::*;
    use cellular_raza_core_proc_macro::AuxStorage;

    #[derive(AuxStorage)]
    struct TestStructDouble<Pos, Vel, For, const N: usize> {
        #[UpdateCycle]
        aux_cycle: AuxStorageCycle,
        #[UpdateMechanics(Pos, Vel, For, N)]
        aux_mechanics: AuxStorageMechanics<Pos, Vel, For, N>,
    }

    #[derive(AuxStorage)]
    struct TestStructCycle {
        #[UpdateCycle]
        aux_cycle: AuxStorageCycle,
    }

    #[derive(AuxStorage)]
    struct TestStructMechanics<Pos, Vel, For, const N: usize> {
        #[UpdateMechanics(Pos, Vel, For, N)]
        aux_mechanics: AuxStorageMechanics<Pos, Vel, For, N>,
    }

    fn add_get_events<A>(aux_storage: &mut A)
    where
        A: UpdateCycle,
    {
        aux_storage.add_cycle_event(CycleEvent::Division);
        let events = aux_storage.get_cycle_events();
        assert_eq!(events, &vec![CycleEvent::Division]);
    }

    fn set_get_events<A>(aux_storage: &mut A)
    where
        A: UpdateCycle,
    {
        let initial_events = vec![
            CycleEvent::Division,
            CycleEvent::Division,
            CycleEvent::PhasedDeath,
        ];
        aux_storage.set_cycle_events(initial_events.clone());
        let events = aux_storage.get_cycle_events();
        assert_eq!(events.len(), 3);
        assert_eq!(events, &initial_events);
    }

    #[test]
    fn cycle_add_get_events() {
        let mut aux_storage = TestStructCycle {
            aux_cycle: AuxStorageCycle::default(),
        };
        add_get_events(&mut aux_storage);
    }

    #[test]
    fn cycle_set_get_events() {
        let mut aux_storage = TestStructCycle {
            aux_cycle: AuxStorageCycle::default(),
        };
        set_get_events(&mut aux_storage);
    }

    #[test]
    fn mechanics() {
        let mut aux_storage = TestStructMechanics::<_, _, _, 4> {
            aux_mechanics: AuxStorageMechanics::default(),
        };
        aux_storage.set_last_position(3_f64);
        aux_storage.set_last_velocity(5_f32);
        aux_storage.add_force(1_f32);
    }

    #[test]
    fn cycle_mechanics_add_get_events() {
        let mut aux_storage = TestStructDouble::<_, _, _, 4> {
            aux_cycle: AuxStorageCycle::default(),
            aux_mechanics: AuxStorageMechanics::default(),
        };
        aux_storage.set_last_position(3_f64);
        aux_storage.set_last_velocity(5_f32);
        aux_storage.add_force(-5_f32);
        add_get_events(&mut aux_storage);
    }

    #[test]
    fn cycle_mechanics_set_get_events() {
        let mut aux_storage = TestStructDouble::<_, _, _, 4> {
            aux_cycle: AuxStorageCycle::default(),
            aux_mechanics: AuxStorageMechanics::default(),
        };
        aux_storage.set_last_position(3_f64);
        aux_storage.set_last_velocity(5_f32);
        aux_storage.add_force(111_i64);
        set_get_events(&mut aux_storage);
    }
}

#[allow(unused)]
#[doc(hidden)]
mod test_build_aux_storage {
    use crate::backend::chili::proc_macro::aux_storage_constructor;
    macro_rules! construct (
        (name:$test_name:ident,
        aspects:[$($asp:ident),*]) => {
            /// ```
            /// use serde::{Deserialize, Serialize};
            /// use cellular_raza_core::backend::chili::*;
            /// build_aux_storage!(
            #[doc = concat!("aspects: [", $(stringify!($asp,),)* "],")]
            ///     aux_storage_name: __cr_AuxStorage,
            ///     core_path: cellular_raza_core
            /// );
            // #[doc = concat!("let mut aux_storage = __cr_AuxStorage {", init!($($asp),*) "};")]
            // #[doc = init!{@start $($asp),* end}]
            #[doc = concat!(
                "let mut aux_storage = (",
                stringify!(aux_storage_constructor!(
                    aux_storage_name: __cr_AuxStorage,
                    core_path: cellular_raza_core,
                    aspects: [$($asp),*],
                )),
                ")(());",
            )]
            /// macro_rules! test_aspect (
            ///     (Mechanics) => {
            ///         {
            ///             use cellular_raza_core::backend::chili::UpdateMechanics;
            ///             aux_storage.set_last_position(1_f32);
            ///             aux_storage.set_last_position(3_f32);
            ///             let last_positions = aux_storage
            ///                 .previous_positions()
            ///                 .map(|f| *f)
            ///                 .collect::<Vec<f32>>();
            ///             assert_eq!(last_positions, vec![1_f32, 3_f32]);
            ///             aux_storage.set_last_velocity(10_f32);
            ///             let last_velocities: cellular_raza_core::backend::chili::RingBufferIterRef<_, 4>
            ///                 = aux_storage.previous_velocities();
            ///             let last_velocities = last_velocities.map(|f| *f).collect::<Vec<f32>>();
            ///             assert_eq!(last_velocities, vec![10_f32]);
            ///             aux_storage.add_force(22_f32);
            ///             assert_eq!(aux_storage.get_current_force_and_reset(), 22_f32);
            ///         }
            ///     };
            ///     (Interaction) => {
            ///         {
            ///             use cellular_raza_core::backend::chili::UpdateInteraction;
            ///             aux_storage.incr_current_neighbors(1);
            ///             aux_storage.incr_current_neighbors(2);
            ///             aux_storage.incr_current_neighbors(1);
            ///             assert_eq!(aux_storage.get_current_neighbors(), 4);
            ///         }
            ///     };
            ///     (Cycle) => {
            ///         {
            ///             use cellular_raza_core::backend::chili::UpdateCycle;
            ///             use cellular_raza_concepts::CycleEvent;
            ///             aux_storage.add_cycle_event(CycleEvent::Division);
            ///             assert_eq!(aux_storage.get_cycle_events(), &vec![CycleEvent::Division]);
            ///         }
            ///     };
            ///     (Reactions) => {
            ///         {
            ///             use cellular_raza_core::backend::chili::UpdateReactions;
            ///             aux_storage.set_conc(0_f32);
            ///             aux_storage.incr_conc(1.44_f32);
            ///             assert_eq!(aux_storage.get_conc(), 0_f32 + 1.44_f32);
            ///         }
            ///     };
            ///     (ReactionsContact) => {
            ///         {
            ///             use cellular_raza_core::backend::chili::UpdateReactionsContact;
            ///             aux_storage.set_last_increment(0f32);
            ///             aux_storage.set_last_increment(3f32);
            ///             assert_eq!(UpdateReactionsContact::n_previous_values(&aux_storage), 2);
            ///             let last_increments =
            ///                 UpdateReactionsContact::<f32, 10>::previous_increments(
            ///                 &aux_storage
            ///             );
            ///             let last_increments = last_increments.map(|f| *f).collect::<Vec<_>>();
            ///             assert_eq!(last_increments, vec![0.0, 3.0]);
            ///         }
            ///     };
            /// );
            #[doc = concat!($(
                concat!("test_aspect!(", stringify!($asp), ");")
            ,)*)]
            /// ```
            fn $test_name() {}
        }
    );

    cellular_raza_core_proc_macro::run_test_for_aspects!(
        test: construct,
        aspects: [Mechanics, Interaction, Cycle, Reactions, ReactionsContact]
    );
}