cellular_raza_building_blocks/cell_building_blocks/
mechanics.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
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
use cellular_raza_concepts::{CalcError, Mechanics, RngError};

use itertools::Itertools;
use nalgebra::{SMatrix, SVector};

use serde::{Deserialize, Serialize};

#[cfg(feature = "pyo3")]
use pyo3::prelude::*;

macro_rules! implement_newton_damped_mechanics(
    (
        $struct_name:ident,
        $d:literal
    ) => {
        implement_newton_damped_mechanics!($struct_name, $d, f64);
    };
    (
        $struct_name:ident,
        $d:literal,
        $float_type:ty
    ) => {
        /// Newtonian dynamics governed by mass and damping.
        ///
        /// # Parameters
        /// | Symbol | Parameter | Description |
        /// | --- | --- | --- |
        /// | $\vec{x}$ | `pos` | Position of the particle. |
        /// | $\dot{\vec{x}}$ | `vel` | Velocity of the particle. |
        /// | $\lambda$ | `damping` | Damping constant |
        /// | $m$ | `mass` | Mass of the particle. |
        ///
        /// # Equations
        /// The equation of motion is given by
        /// \\begin{equation}
        ///     m \ddot{\vec{x}} = \vec{F} - \lambda \dot{\vec{x}}
        /// \\end{equation}
        /// where $\vec{F}$ is the force as calculated by the
        /// [Interaction](cellular_raza_concepts::Interaction) trait.
        ///
        /// # Comments
        /// If the cell is growing, we need to increase the mass $m$.
        /// By interacting with the outside world, we can adapt $\lambda$ to external values
        /// although this is rarely desirable.
        /// Both operations need to be implemented by other concepts such as
        /// [Cycle](cellular_raza_concepts::Cycle).
        #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
        #[cfg_attr(feature = "pyo3", pyclass)]
        pub struct $struct_name {
            /// Current position $\vec{x}$ given by a vector of dimension `D`.
            pub pos: SVector<$float_type, $d>,
            /// Current velocity $\dot{\vec{x}}$ given by a vector of dimension `D`.
            pub vel: SVector<$float_type, $d>,
            /// Damping constant $\lambda$.
            pub damping_constant: $float_type,
            /// Mass $m$ of the object.
            pub mass: $float_type,
        }

        impl $struct_name {
            #[doc = "Create a new "]
            #[doc = stringify!($struct_name)]
            /// from position, velocity, damping constant and mass
            pub fn new(
                pos: [$float_type; $d],
                vel: [$float_type; $d],
                damping_constant: $float_type,
                mass: $float_type,
            ) -> Self {
                Self {
                    pos: pos.into(),
                    vel: vel.into(),
                    damping_constant,
                    mass,
                }
            }

        }

        #[cfg(feature = "pyo3")]
        #[pymethods]
        #[cfg_attr(docsrs, doc(cfg(feature = "pyo3")))]
        impl $struct_name {
            #[new]
            fn _new(
                pos: [$float_type; $d],
                vel: [$float_type; $d],
                damping_constant: $float_type,
                mass: $float_type,
            ) -> Self {
                Self::new(pos, vel, damping_constant, mass)
            }


            /// [pyo3] getter for `pos`
            #[getter]
            pub fn get_pos(&self) -> [$float_type; $d] {
                self.pos.into()
            }

            /// [pyo3] getter for `vel`
            #[getter]
            pub fn get_vel(&self) -> [$float_type; $d] {
                self.vel.into()
            }

            /// [pyo3] getter for `damping_constant`
            #[getter]
            pub fn get_damping_constant(&self) -> $float_type {
                self.damping_constant
            }

            /// [pyo3] getter for `mass`
            #[getter]
            pub fn get_mass(&self) -> $float_type {
                self.mass
            }

            /// [pyo3] setter for `pos`
            #[setter]
            pub fn set_pos(&mut self, pos: [$float_type; $d]) {
                self.pos = pos.into();
            }

            /// [pyo3] setter for `vel`
            #[setter]
            pub fn set_vel(&mut self, vel: [$float_type; $d]) {
                self.vel = vel.into();
            }

            /// [pyo3] setter for `damping_constant`
            #[setter]
            pub fn set_damping_constant(&mut self, damping_constant: $float_type) {
                self.damping_constant = damping_constant;
            }

            /// [pyo3] setter for `mass`
            #[setter]
            pub fn set_mass(&mut self, mass: $float_type) {
                self.mass = mass;
            }
        }

        impl Mechanics<
            SVector<$float_type, $d>,
            SVector<$float_type, $d>,
            SVector<$float_type, $d>,
            $float_type
        > for $struct_name
        {
            fn get_random_contribution(
                &self,
                _: &mut rand_chacha::ChaCha8Rng,
                _dt: $float_type,
            ) -> Result<(SVector<$float_type, $d>, SVector<$float_type, $d>), RngError> {
                Ok((num::Zero::zero(), num::Zero::zero()))
            }

            fn calculate_increment(
                &self,
                force: SVector<$float_type, $d>,
            ) -> Result<(SVector<$float_type, $d>, SVector<$float_type, $d>), CalcError> {
                let dx = self.vel;
                let dv = force / self.mass - self.damping_constant * self.vel;
                Ok((dx, dv))
            }
        }

        impl cellular_raza_concepts::Position<SVector<$float_type, $d>> for $struct_name {
            fn pos(&self) -> SVector<$float_type, $d> {
                self.pos
            }

            fn set_pos(&mut self, pos: &SVector<$float_type, $d>) {
                self.pos = *pos;
            }
        }

        impl cellular_raza_concepts::Velocity<SVector<$float_type, $d>> for $struct_name {
            fn velocity(&self) -> SVector<$float_type, $d> {
                self.vel
            }

            fn set_velocity(&mut self, velocity: &SVector<$float_type, $d>) {
                self.vel = *velocity;
            }
        }
    }
);

implement_newton_damped_mechanics!(NewtonDamped1D, 1);
implement_newton_damped_mechanics!(NewtonDamped2D, 2);
implement_newton_damped_mechanics!(NewtonDamped3D, 3);

implement_newton_damped_mechanics!(NewtonDamped1DF32, 1, f32);
implement_newton_damped_mechanics!(NewtonDamped2DF32, 2, f32);
implement_newton_damped_mechanics!(NewtonDamped3DF32, 3, f32);

/// Generate a vector corresponding to a wiener process.
///
/// This function calculates a statically sized random vector with dimension `D`.
/// It uses a [rand_distr::StandardNormal] distribution and divides the result by `dt` such that
/// the correct incremental wiener process is obtained.
pub fn wiener_process<F, const D: usize>(
    rng: &mut rand_chacha::ChaCha8Rng,
    dt: F,
) -> Result<SVector<F, D>, RngError>
where
    F: core::ops::DivAssign + nalgebra::Scalar + num::Float,
    rand_distr::StandardNormal: rand_distr::Distribution<F>,
{
    let std_dev = dt.sqrt();
    let distr = match rand_distr::Normal::new(F::zero(), std_dev) {
        Ok(e) => Ok(e),
        Err(e) => Err(RngError(format!("{e}"))),
    }?;
    let random_dir = SVector::<F, D>::from_distribution(&distr, rng);
    Ok(random_dir / dt)
}

macro_rules! implement_brownian_mechanics(
    ($struct_name:ident, $d:literal, $float_type:ty) => {
        /// Brownian motion of particles
        ///
        /// # Parameters & Variables
        /// | Symbol | Struct Field | Description |
        /// | --- | --- | --- |
        /// | $D$ | `diffusion_constant` | Damping constant of each particle. |
        /// | $k_BT$ | `kb_temperature` | Product of temperature $T$ and Boltzmann constant $k_B$. |
        /// | | | |
        /// | $\vec{x}$ | `pos` | Position of the particle. |
        /// | $R(t)$ | (automatically generated) | Gaussian process |
        ///
        /// # Equations
        /// We integrate the standard brownian motion stochastic differential equation.
        /// \\begin{equation}
        ///     \dot{\vec{x}} = -\frac{D}{k_B T}\nabla V(x) + \sqrt{2D}R(t)
        /// \\end{equation}
        /// The new random vector is then also sampled by a distribution with greater width.
        /// If we choose this value larger than one, we can
        /// resolve smaller timesteps to more accurately solve the equations.
        #[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
        #[cfg_attr(feature = "pyo3", pyclass)]
        pub struct $struct_name {
            /// Current position of the particle $\vec{x}$.
            pub pos: SVector<$float_type, $d>,
            /// Diffusion constant $D$.
            pub diffusion_constant: $float_type,
            /// The product of temperature and boltzmann constant $k_B T$.
            pub kb_temperature: $float_type,
        }

        impl $struct_name {
            /// Constructs a new
            #[doc = concat!("[", stringify!($struct_name), "]")]
            pub fn new(
                pos: [$float_type; $d],
                diffusion_constant: $float_type,
                kb_temperature: $float_type,
            ) -> Self {
                Self {
                    pos: pos.into(),
                    diffusion_constant,
                    kb_temperature,
                }
            }
        }

        #[cfg(feature = "pyo3")]
        #[pymethods]
        #[cfg_attr(docsrs, doc(cfg(feature = "pyo3")))]
        impl $struct_name {
            #[new]
            fn _new(
                pos: [$float_type; $d],
                diffusion_constant: $float_type,
                kb_temperature: $float_type,
            ) -> Self {
                Self::new(pos, diffusion_constant, kb_temperature)
            }


            /// [pyo3] setter for `pos`
            #[setter]
            pub fn set_pos(&mut self, pos: [$float_type; $d]) {
                self.pos = pos.into();
            }

            /// [pyo3] setter for `diffusion_constant`
            #[setter]
            pub fn set_diffusion_constant(&mut self, diffusion_constant: $float_type) {
                self.diffusion_constant = diffusion_constant;
            }

            /// [pyo3] setter for `kb_temperature`
            #[setter]
            pub fn set_kb_temperature(&mut self, kb_temperature: $float_type) {
                self.kb_temperature = kb_temperature;
            }

            /// [pyo3] getter for `pos`
            #[getter]
            pub fn get_pos(&self) -> [$float_type; $d] {
                self.pos.into()
            }

            /// [pyo3] getter for `diffusion_constant`
            #[getter]
            pub fn get_diffusion_constant(&self) -> $float_type {
                self.diffusion_constant
            }

            /// [pyo3] getter for `kb_temperature`
            #[getter]
            pub fn get_kb_temperature(&self) -> $float_type {
                self.kb_temperature
            }
        }

        impl Mechanics<
            SVector<$float_type, $d>,
            SVector<$float_type, $d>,
            SVector<$float_type, $d>,
            $float_type
        > for $struct_name {
            fn get_random_contribution(
                &self,
                rng: &mut rand_chacha::ChaCha8Rng,
                dt: $float_type,
            ) -> Result<(SVector<$float_type, $d>, SVector<$float_type, $d>), RngError> {
                let dpos = (2.0 as $float_type * self.diffusion_constant).sqrt()
                    * wiener_process(
                    rng,
                    dt
                )?;
                let dvel = SVector::<$float_type, $d>::zeros();
                Ok((dpos, dvel))
            }

            fn calculate_increment(
                &self,
                force: SVector<$float_type, $d>,
            ) -> Result<(SVector<$float_type, $d>, SVector<$float_type, $d>), CalcError> {
                use num::Zero;
                let dx = self.diffusion_constant / self.kb_temperature * force;
                Ok((dx, SVector::<$float_type, $d>::zero()))
            }
        }

        impl cellular_raza_concepts::Position<SVector<$float_type, $d>> for $struct_name {
            fn pos(&self) -> SVector<$float_type, $d> {
                self.pos
            }

            fn set_pos(&mut self, pos: &SVector<$float_type, $d>) {
                self.pos = *pos;
            }

        }

        impl cellular_raza_concepts::Velocity<SVector<$float_type, $d>> for $struct_name {
            fn velocity(&self) -> SVector<$float_type, $d> {
                use num::Zero;
                SVector::<$float_type, $d>::zero()
            }

            fn set_velocity(&mut self, _velocity: &SVector<$float_type, $d>) {}
        }
    }
);

implement_brownian_mechanics!(Brownian1D, 1, f64);
implement_brownian_mechanics!(Brownian2D, 2, f64);
implement_brownian_mechanics!(Brownian3D, 3, f64);
implement_brownian_mechanics!(Brownian1DF32, 1, f32);
implement_brownian_mechanics!(Brownian2DF32, 2, f32);
implement_brownian_mechanics!(Brownian3DF32, 3, f32);

macro_rules! define_langevin_nd(
    ($struct_name:ident, $d:literal, $float_type:ident) => {
        /// Langevin dynamics
        ///
        /// # Parameters & Variables
        /// | Symbol | Struct Field | Description |
        /// |:---:| --- | --- |
        /// | $M$ | `mass` | Mass of the particle. |
        /// | $\gamma$ | `damping` | Damping constant |
        /// | $k_BT$ | `kb_temperature` | Product of temperature $T$ and Boltzmann constant $k_B$. |
        /// | | | |
        /// | $\vec{X}$ | `pos` | Position of the particle. |
        /// | $\dot{\vec{X}}$ | `vel` | Velocity of the particle. |
        /// | $R(t)$ | (automatically generated) | Gaussian process |
        ///
        /// # Equations
        ///
        /// \\begin{equation}
        ///     M \ddot{\mathbf{X}} = - \mathbf{\nabla} U(\mathbf{X}) - \gamma M\dot{\mathbf{X}} + \sqrt{2 M \gamma k_{\rm B} T}\mathbf{R}(t)
        /// \\end{equation}
        #[cfg_attr(feature = "pyo3", pyclass)]
        #[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
        pub struct $struct_name {
            /// Current position
            pub pos: SVector<$float_type, $d>,
            /// Current velocity
            pub vel: SVector<$float_type, $d>,
            /// Mass of the object
            pub mass: $float_type,
            /// Damping constant
            pub damping: $float_type,
            /// Product of Boltzmann constant and temperature
            pub kb_temperature: $float_type,
        }

        impl Mechanics<
            SVector<$float_type, $d>,
            SVector<$float_type, $d>,
            SVector<$float_type, $d>,
            $float_type
        > for $struct_name {
            fn get_random_contribution(
                &self,
                rng: &mut rand_chacha::ChaCha8Rng,
                dt: $float_type,
            ) -> Result<(SVector<$float_type, $d>, SVector<$float_type, $d>), RngError> {
                let dvel = (
                    2.0 as $float_type
                    * self.damping
                    * self.kb_temperature
                    / self.mass
                ).sqrt() * wiener_process(
                    rng,
                    dt
                )?;
                let dpos = SVector::<$float_type, $d>::zeros();
                Ok((dpos, dvel))
            }

            fn calculate_increment(
                &self,
                force: SVector<$float_type, $d>,
            ) -> Result<(SVector<$float_type, $d>, SVector<$float_type, $d>), CalcError> {
                let dx = self.vel;
                let dv1 =
                    1.0 as $float_type / self.mass * force;
                let dv2 =
                    - self.damping * self.vel;
                let dv = dv1 + dv2;
                Ok((dx, dv))
            }
        }

        impl cellular_raza_concepts::Position<SVector<$float_type, $d>> for $struct_name {
            fn pos(&self) -> SVector<$float_type, $d> {
                self.pos
            }

            fn set_pos(&mut self, pos: &SVector<$float_type, $d>) {
                self.pos = *pos;
            }
        }

        impl cellular_raza_concepts::Velocity<SVector<$float_type, $d>> for $struct_name {
            fn velocity(&self) -> SVector<$float_type, $d> {
                self.vel
            }

            fn set_velocity(&mut self, velocity: &SVector<$float_type, $d>) {
                self.vel = *velocity;
            }
        }

        #[cfg(feature = "pyo3")]
        #[pymethods]
        #[cfg_attr(docsrs, doc(cfg(feature = "pyo3")))]
        impl $struct_name {
            /// Creates a new [
            #[doc = stringify!($struct_name)]
            /// ] struct from position, velocity, mass, damping,
            /// kb_temperature and the update interval of the mechanics aspect.
            #[new]
            fn _new(
                pos: [$float_type; $d],
                vel: [$float_type; $d],
                mass: $float_type,
                damping: $float_type,
                kb_temperature: $float_type,
            ) -> Self {
                Self {
                    pos: pos.into(),
                    vel: vel.into(),
                    mass,
                    damping,
                    kb_temperature,
                }
            }

            #[getter(pos)]
            /// [pyo3] getter for `position`
            pub fn get_position(&self) -> [$float_type; $d] {
                self.pos.into()
            }

            #[setter(pos)]
            /// [pyo3] setter for `position`
            pub fn set_position(&mut self, pos: [$float_type; $d]) {
                self.pos = pos.into();
            }

            #[getter(damping)]
            /// [pyo3] getter for `damping`
            pub fn get_damping(&self) -> $float_type {
                self.damping
            }

            #[setter(damping)]
            /// [pyo3] setter for `damping`
            pub fn set_damping(&mut self, damping: $float_type) {
                self.damping = damping;
            }

            #[getter(mass)]
            /// [pyo3] getter for `mass`
            pub fn get_mass(&self) -> $float_type {
                self.mass
            }

            #[setter(mass)]
            /// [pyo3] setter for `mass`
            pub fn set_mass(&mut self, mass: $float_type) {
                self.mass = mass;
            }

            #[getter(kb_temperature)]
            /// [pyo3] getter for `kb_temperature`
            pub fn get_kb_temperature(&self) -> $float_type {
                self.kb_temperature
            }

            #[setter(kb_temperature)]
            /// [pyo3] setter for `kb_temperature`
            pub fn set_kb_temperature(&mut self, kb_temperature: $float_type) {
                self.kb_temperature = kb_temperature;
            }

            fn __repr__(&self) -> String {
                format!("{self:#?}")
            }
        }
    }
);

define_langevin_nd!(Langevin1D, 1, f64);
define_langevin_nd!(Langevin2D, 2, f64);
define_langevin_nd!(Langevin3D, 3, f64);
define_langevin_nd!(Langevin1DF32, 1, f32);
define_langevin_nd!(Langevin2DF32, 2, f32);
define_langevin_nd!(Langevin3DF32, 3, f32);

/// Mechanics model which represents cells as vertices with edges between them.
///
/// The vertices are attached to each other with springs and a given length between each
/// vertex.
/// Furthermore, we define a central pressure that acts when the total cell area is greater
/// or smaller than the desired one.
/// Each vertex is damped individually by the same constant.
// TODO include more formulas for this model
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct VertexMechanics2D<const D: usize> {
    points: nalgebra::SMatrix<f64, D, 2>,
    velocity: nalgebra::SMatrix<f64, D, 2>,
    /// Boundary lengths of individual edges
    pub cell_boundary_lengths: nalgebra::SVector<f64, D>,
    /// Spring tensions of individual edges
    pub spring_tensions: nalgebra::SVector<f64, D>,
    /// Total cell area
    pub cell_area: f64,
    /// Central pressure going from middle of the cell outwards
    pub central_pressure: f64,
    /// Damping constant
    pub damping_constant: f64,
    /// Controls the random motion of the entire cell
    pub diffusion_constant: f64,
}

impl<const D: usize> VertexMechanics2D<D> {
    /// Creates a new vertex model in equilibrium conditions.
    ///
    /// The specified parameters are then used to carefully calculate relating properties of the model.
    /// We outline the formulas used.
    /// Given the number of vertices \\(N\\) in our model (specified by the const generic argument
    /// of the [VertexMechanics2D] struct),
    /// the resulting average angle when all nodes are equally distributed is
    /// \\[
    ///     \Delta\varphi = \frac{2\pi}{N}
    /// \\]
    /// Given the total area of the cell ([regular polygon](https://en.wikipedia.org/wiki/Regular_polygon)) \\(A\\), we can calculate the distance from the center point to the individual vertices by inverting
    /// \\[
    ///     A = r^2 \sin\left(\frac{\pi}{N}\right)\cos\left(\frac{\pi}{N}\right).
    /// \\]
    /// This formula can be derived by elementary geometric arguments.
    /// We can then generate the points \\(\vec{p}\_i\\) which make up the cell-boundary by using
    /// \\[
    ///     \vec{p}\_i = \vec{p}\_{mid} + r(\cos(i \Delta\varphi), \sin(i\Delta\varphi))^T.
    /// \\]
    /// From these points, their distance is calculated and passed as the individual boundary lengths.
    /// When randomization is turned on, these points will be slightly randomized in their radius and angle which might lead to non-equilibrium configurations.
    /// Pressure, damping and spring tensions are not impacted by randomization.
    pub fn new(
        middle: SVector<f64, 2>,
        cell_area: f64,
        rotation_angle: f64,
        spring_tensions: f64,
        central_pressure: f64,
        damping_constant: f64,
        diffusion_constant: f64,
        randomize: Option<(f64, rand_chacha::ChaCha8Rng)>,
    ) -> Self {
        use rand::Rng;
        // Restrict the randomize variable between 0 and 1
        let r = match randomize {
            Some((rand, _)) => rand.clamp(0.0, 1.0),
            _ => 0.0,
        };
        let rng = || -> f64 {
            match randomize {
                Some((_, mut rng)) => rng.random_range(0.0..1.0),
                None => 0.0,
            }
        };
        // Randomize the overall rotation angle
        let rotation_angle = (1.0 - r * rng.clone()()) * rotation_angle;
        // Calculate the angle fraction used to determine the points of the polygon
        let angle_fraction = std::f64::consts::PI / D as f64;
        // Calculate the radius from cell area
        let radius = (cell_area / D as f64 / angle_fraction.sin() / angle_fraction.cos()).sqrt();
        // TODO this needs to be calculated again
        let points = nalgebra::SMatrix::<f64, D, 2>::from_row_iterator((0..D).flat_map(|n| {
            let angle =
                rotation_angle + 2.0 * angle_fraction * n as f64 * (1.0 - r * rng.clone()());
            let radius_modified = radius * (1.0 + 0.5 * r * (1.0 - rng.clone()()));
            [
                middle.x + radius_modified * angle.cos(),
                middle.y + radius_modified * angle.sin(),
            ]
            .into_iter()
        }));
        // Randomize the boundary lengths
        let cell_boundary_lengths = SVector::<f64, D>::from_iterator(
            points
                .row_iter()
                .circular_tuple_windows()
                .map(|(p1, p2)| (p1 - p2).norm()),
        );
        VertexMechanics2D {
            points,
            velocity: nalgebra::SMatrix::<f64, D, 2>::zeros(),
            cell_boundary_lengths,
            spring_tensions: SVector::<f64, D>::from_element(spring_tensions),
            cell_area,
            central_pressure,
            damping_constant,
            diffusion_constant,
        }
    }

    /// Calculates the boundary length of the regular polygon given the total area in equilibrium.
    ///
    /// The formula used is
    /// $$\\begin{align}
    ///     A &= \frac{L^2}{4n\tan\left(\frac{\pi}{n}\right)}\\\\
    ///     L &= \sqrt{4An\tan\left(\frac{\pi}{n}\right)}
    /// \\end{align}$$
    /// where $A$ is the total area, $n$ is the number of vertices and $L$ is the total boundary
    /// length.
    pub fn calculate_boundary_length(cell_area: f64) -> f64 {
        (4.0 * cell_area * (std::f64::consts::PI / D as f64).tan() * D as f64).sqrt()
    }

    /// Calculates the cell area of the regular polygon in equilibrium.
    ///
    /// The formula used is identical the the one of [Self::calculate_boundary_length].
    pub fn calculate_cell_area(boundary_length: f64) -> f64 {
        D as f64 * boundary_length.powf(2.0) / (4.0 * (std::f64::consts::PI / D as f64).tan())
    }

    /// Calculates the current area of the cell
    pub fn get_current_cell_area(&self) -> f64 {
        0.5_f64
            * self
                .points
                .row_iter()
                .circular_tuple_windows()
                .map(|(p1, p2)| p1.transpose().perp(&p2.transpose()))
                .sum::<f64>()
    }

    /// Calculate the current polygons boundary length
    pub fn calculate_current_boundary_length(&self) -> f64 {
        self.points
            .row_iter()
            .tuple_windows::<(_, _)>()
            .map(|(p1, p2)| (p2 - p1).norm())
            .sum::<f64>()
    }

    /// Obtain current cell area
    pub fn get_cell_area(&self) -> f64 {
        self.cell_area
    }

    /// Set the current cell area and adjust the length of edges such that the cell is still in
    /// equilibrium.
    pub fn set_cell_area_and_boundary_length(&mut self, cell_area: f64) {
        // Calculate the relative difference to current area
        match self.cell_area {
            0.0 => {
                let new_interaction_parameters = Self::new(
                    self.points
                        .row_iter()
                        .map(|v| v.transpose())
                        .sum::<nalgebra::Vector2<f64>>(),
                    cell_area,
                    0.0,
                    self.spring_tensions.sum() / self.spring_tensions.len() as f64,
                    self.central_pressure,
                    self.damping_constant,
                    self.diffusion_constant,
                    None,
                );
                *self = new_interaction_parameters;
            }
            _ => {
                let relative_length_difference = (cell_area.abs() / self.cell_area.abs()).sqrt();
                // Calculate the new length of the cell boundary lengths
                self.cell_boundary_lengths
                    .iter_mut()
                    .for_each(|length| *length *= relative_length_difference);
                self.cell_area = cell_area;
            }
        };
    }

    /// Change the internal cell area
    pub fn set_cell_area(&mut self, cell_area: f64) {
        self.cell_area = cell_area;
    }
}

impl VertexMechanics2D<6> {
    /// Fills the area of a given rectangle with hexagonal cells. Their orientation is such that
    /// the top border has a flat top.
    ///
    /// The produced pattern will like similar to this.
    /// ```text
    /// __________________________________
    /// |   ___       ___          ___   |
    /// |  /   \     /   \        /   \  |
    /// | /     \___/     \_ ..._/     \ |
    /// | \     /   \     /      \     / |
    /// |  \___/     \___/        \___/  |
    /// |  /   \     /   \        /   \  |
    /// | .     .   .     .      .     . |
    /// ```
    /// The padding around the generated cells will be determined automatically.
    pub fn fill_rectangle_flat_top(
        cell_area: f64,
        spring_tensions: f64,
        central_pressure: f64,
        damping_constant: f64,
        diffusion_constant: f64,
        rectangle: [SVector<f64, 2>; 2],
    ) -> Vec<Self> {
        // If the supplied area is larger than the total area, return nothing
        let side_x = rectangle[1].x - rectangle[0].x;
        let side_y = rectangle[1].y - rectangle[0].y;
        if cell_area > side_x * side_y {
            return Vec::new();
        }
        let segment_length = Self::calculate_boundary_length(cell_area) / 6.0;
        let radius_outer = Self::outer_radius_from_cell_area(cell_area);
        let radius_inner = Self::inner_radius_from_cell_area(cell_area);

        // Check if only one single hexagon fits into the domain in any dimension
        let n_max_x = (side_x - 2.0 * radius_outer).div_euclid(3.0 / 2.0 * radius_outer) as usize;
        let n_max_y = side_y.div_euclid(2.0 * radius_inner);
        let total_width_x = 2.0 * radius_outer + (n_max_x - 1) as f64 * 3.0 / 2.0 * radius_outer;
        let total_width_y = n_max_y * 2.0 * radius_inner;

        let pad_x = (side_x - total_width_x) / 2.0;
        let pad_y = (side_y - total_width_y) / 2.0;
        let padding = nalgebra::RowVector2::from([pad_x, pad_y]);

        let mut generated_models = vec![];
        for n_x in 0..n_max_x {
            for n_y in 0..n_max_y as usize - n_x % 2 {
                let middle = rectangle[0].transpose()
                    + padding
                    + nalgebra::RowVector2::from([
                        (1.0 + 3.0 / 2.0 * n_x as f64) * radius_outer,
                        (1 + 2 * n_y + n_x % 2) as f64 * radius_inner,
                    ]);
                let mut pos = nalgebra::SMatrix::<f64, 6, 2>::zeros();
                for i in 0..6 {
                    let phi = 2.0 * std::f64::consts::PI * i as f64 / 6.0;
                    pos.set_row(
                        i,
                        &(middle
                            + radius_outer * nalgebra::RowVector2::from([phi.cos(), phi.sin()])),
                    );
                }
                generated_models.push(Self {
                    points: pos,
                    velocity: SMatrix::zeros(),
                    cell_boundary_lengths: SVector::from_element(segment_length),
                    spring_tensions: SVector::from_element(spring_tensions),
                    cell_area,
                    central_pressure,
                    damping_constant,
                    diffusion_constant,
                });
            }
        }
        generated_models
    }
}

impl VertexMechanics2D<4> {
    /// Fill a specified rectangle with cells of 4 vertices
    pub fn fill_rectangle(
        cell_area: f64,
        spring_tensions: f64,
        central_pressure: f64,
        damping_constant: f64,
        diffusion_constant: f64,
        rectangle: [SVector<f64, 2>; 2],
    ) -> Vec<Self> {
        let cell_side_length: f64 = cell_area.sqrt();
        let cell_side_length_padded: f64 = cell_side_length * 1.04;

        let number_of_cells_x: u64 =
            ((rectangle[1].x - rectangle[0].x) / cell_side_length_padded).floor() as u64;
        let number_of_cells_y: u64 =
            ((rectangle[1].y - rectangle[0].y) / cell_side_length_padded).floor() as u64;

        let start_x: f64 = rectangle[0].x
            + 0.5
                * ((rectangle[1].x - rectangle[0].x)
                    - number_of_cells_x as f64 * cell_side_length_padded);
        let start_y: f64 = rectangle[0].y
            + 0.5
                * ((rectangle[1].y - rectangle[0].y)
                    - number_of_cells_y as f64 * cell_side_length_padded);

        use itertools::iproduct;
        iproduct!(0..number_of_cells_x, 0..number_of_cells_y)
            .map(|(i, j)| {
                let corner = (
                    start_x + (i as f64) * cell_side_length_padded,
                    start_y + (j as f64) * cell_side_length_padded,
                );

                let points = nalgebra::SMatrix::<f64, 4, 2>::from_row_iterator([
                    corner.0,
                    corner.1,
                    corner.0 + cell_side_length,
                    corner.1,
                    corner.0 + cell_side_length,
                    corner.1 + cell_side_length,
                    corner.0,
                    corner.1 + cell_side_length,
                ]);
                let cell_boundary_lengths = nalgebra::SVector::<f64, 4>::from_iterator(
                    (0..2 * 4).map(|_| cell_side_length),
                );

                VertexMechanics2D {
                    points,
                    velocity: nalgebra::SMatrix::<f64, 4, 2>::zeros(),
                    cell_boundary_lengths,
                    spring_tensions: nalgebra::SVector::<f64, 4>::from_element(spring_tensions),
                    cell_area,
                    central_pressure,
                    damping_constant,
                    diffusion_constant,
                }
            })
            .collect::<Vec<_>>()
    }
}

impl<const D: usize> VertexMechanics2D<D> {
    /// Calculates the outer circle radius of the Regular Polygon given its area.
    pub fn outer_radius_from_cell_area(cell_area: f64) -> f64 {
        // let segment_length = Self::calculate_boundary_length(cell_area) / D as f64;
        // segment_length / (std::f64::consts::PI / D as f64).tan() / 2.0
        let boundary_length = Self::calculate_boundary_length(cell_area);
        Self::outer_radius_from_boundary_length(boundary_length)
    }

    /// Calculates the outer circle radius of the Regular Polygon given its boundary length.
    pub fn outer_radius_from_boundary_length(boundary_length: f64) -> f64 {
        let segment_length = boundary_length / D as f64;
        segment_length / (std::f64::consts::PI / D as f64).sin() / 2.0
    }

    /// Calculates the inner circle radius of the Regular Polygon given its area.
    pub fn inner_radius_from_cell_area(cell_area: f64) -> f64 {
        let boundary_length = Self::calculate_boundary_length(cell_area);
        Self::inner_radius_from_boundary_length(boundary_length)
    }

    /// Calculates the inner circle radius of the Regular Polygon given its boundary length.
    pub fn inner_radius_from_boundary_length(boundary_length: f64) -> f64 {
        let segment_length = boundary_length / D as f64;
        segment_length / (std::f64::consts::PI / D as f64).tan() / 2.0
    }
}

impl<const D: usize>
    Mechanics<
        nalgebra::SMatrix<f64, D, 2>,
        nalgebra::SMatrix<f64, D, 2>,
        nalgebra::SMatrix<f64, D, 2>,
    > for VertexMechanics2D<D>
{
    fn calculate_increment(
        &self,
        force: nalgebra::SMatrix<f64, D, 2>,
    ) -> Result<(nalgebra::SMatrix<f64, D, 2>, nalgebra::SMatrix<f64, D, 2>), CalcError> {
        // Calculate the total internal force
        let middle = self.points.row_sum() / self.points.shape().0 as f64;
        let current_ara: f64 = 0.5_f64
            * self
                .points
                .row_iter()
                .circular_tuple_windows()
                .map(|(p1, p2)| p1.transpose().perp(&p2.transpose()))
                .sum::<f64>();

        let mut internal_force = self.points * 0.0;
        for (index, (point_1, point_2, point_3)) in self
            .points
            .row_iter()
            .circular_tuple_windows::<(_, _, _)>()
            .enumerate()
        {
            let tension_12 = self.spring_tensions[index];
            let tension_23 = self.spring_tensions[(index + 1) % self.spring_tensions.len()];
            let mut force_2 = internal_force.row_mut((index + 1) % self.points.shape().0);

            // Calculate forces arising from springs between points
            let p_21 = point_2 - point_1;
            let p_23 = point_2 - point_3;
            let force1 =
                p_21.normalize() * (self.cell_boundary_lengths[index] - p_21.norm()) * tension_12;
            let force2 = p_23.normalize()
                * (self.cell_boundary_lengths[(index + 1) % self.cell_boundary_lengths.len()]
                    - p_23.norm())
                * tension_23;

            // Calculate force arising from internal pressure
            let middle_to_point_2 = point_2 - middle;
            let force3 = middle_to_point_2.normalize()
                * (self.cell_area - current_ara)
                * self.central_pressure;

            // Combine forces
            force_2 += force1 + force2 + force3;
        }
        let dx = self.velocity;
        let dv = force + internal_force - self.damping_constant * self.velocity;
        Ok((dx, dv))
    }

    fn get_random_contribution(
        &self,
        rng: &mut rand_chacha::ChaCha8Rng,
        dt: f64,
    ) -> Result<(nalgebra::SMatrix<f64, D, 2>, nalgebra::SMatrix<f64, D, 2>), RngError> {
        let mut dvel = nalgebra::SMatrix::<f64, D, 2>::zeros();
        let dpos = nalgebra::SMatrix::<f64, D, 2>::zeros();
        if dt != 0.0 {
            let random_vector: SVector<f64, 2> = wiener_process(rng, dt)?;
            dvel.row_iter_mut().for_each(|mut r| {
                r *= 0.0;
                r += random_vector.transpose();
            });
            Ok((dpos, self.diffusion_constant * dvel))
        } else {
            Ok((nalgebra::SMatrix::<f64, D, 2>::zeros(), dvel))
        }
    }
}

impl<const D: usize> cellular_raza_concepts::Position<nalgebra::SMatrix<f64, D, 2>>
    for VertexMechanics2D<D>
{
    fn pos(&self) -> nalgebra::SMatrix<f64, D, 2> {
        self.points
    }

    fn set_pos(&mut self, pos: &nalgebra::SMatrix<f64, D, 2>) {
        self.points = *pos;
    }
}

impl<const D: usize> cellular_raza_concepts::Velocity<nalgebra::SMatrix<f64, D, 2>>
    for VertexMechanics2D<D>
{
    fn velocity(&self) -> nalgebra::SMatrix<f64, D, 2> {
        self.velocity
    }

    fn set_velocity(&mut self, velocity: &nalgebra::SMatrix<f64, D, 2>) {
        self.velocity = *velocity;
    }
}

#[cfg(test)]
mod test_vertex_mechanics_6n {
    #[test]
    fn test_fill_too_small() {
        use crate::VertexMechanics2D;
        use nalgebra::Vector2;
        let models = VertexMechanics2D::<6>::fill_rectangle_flat_top(
            200.0,
            0.0,
            0.0,
            0.0,
            0.0,
            [Vector2::from([1.0, 1.0]), Vector2::from([2.0, 2.0])],
        );
        assert_eq!(models.len(), 0);
    }

    #[test]
    fn test_fill_multiple() {
        use crate::VertexMechanics2D;
        use cellular_raza_concepts::Position;
        use nalgebra::Vector2;
        let models = VertexMechanics2D::<6>::fill_rectangle_flat_top(
            36.0,
            0.0,
            0.0,
            0.0,
            0.0,
            [Vector2::from([0.0; 2]), Vector2::from([100.0; 2])],
        );
        use itertools::Itertools;
        for (m1, m2) in models.into_iter().circular_tuple_windows() {
            if m1.pos().row_mean().transpose().x == m2.pos().row_mean().transpose().x {
                let max = m1
                    .pos()
                    .row_iter()
                    .map(|row| row.transpose().y)
                    .max_by(|x0, x1| x0.partial_cmp(x1).unwrap())
                    .unwrap();
                let min = m2
                    .pos()
                    .row_iter()
                    .map(|row| row.transpose().y)
                    .min_by(|x0, x1| x0.partial_cmp(x1).unwrap())
                    .unwrap();
                assert!((max - min).abs() < 1e-7);
            }
        }
    }
}