cellular_raza-building-blocks 0.0.6

cellular_raza Building Blocks
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
use cellular_raza_concepts::{CalcError, Mechanics, RngError};

use itertools::Itertools;
use nalgebra::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)]
        #[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,
        }

        #[cfg(feature = "pyo3")]
        #[pymethods]
        impl $struct_name {
            #[doc = "Create a new "]
            #[doc = stringify!($struct_name)]
            /// from position, velocity, damping constant and mass
            #[new]
            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,
                }
            }

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

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

            #[getter]
            fn get_damping_constant(&self) -> $float_type {
                self.damping_constant
            }

            #[getter]
            fn get_mass(&self) -> $float_type {
                self.mass
            }

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

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

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

            #[setter]
            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 pos(&self) -> SVector<$float_type, $d> {
                self.pos
            }

            fn velocity(&self) -> SVector<$float_type, $d> {
                self.vel
            }

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

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

            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))
            }
        }
    }
);

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);

/// An empty struct to signalize that no velocity needs to be updated.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(feature = "pyo3", pyclass)]
pub struct NoVelocity;

impl core::ops::Add for NoVelocity {
    type Output = Self;

    fn add(self, _: Self) -> Self::Output {
        NoVelocity
    }
}

impl<F> core::ops::Mul<F> for NoVelocity {
    type Output = Self;

    fn mul(self, _: F) -> Self::Output {
        NoVelocity
    }
}

fn generate_random_vector<const D: usize>(
    rng: &mut rand_chacha::ChaCha8Rng,
    distribution_width: f64,
) -> Result<SVector<f64, D>, RngError> {
    let distr = match rand_distr::Normal::new(0.0, distribution_width) {
        Ok(e) => Ok(e),
        Err(e) => Err(RngError(format!("{e}"))),
    }?;
    let random_dir = SVector::<f64, D>::from_distribution(&distr, rng);
    Ok(random_dir)
}

macro_rules! implement_brownian_mechanis(
    ($struct_name:ident, $d:literal) => {
        /// Brownian motion of particles
        ///
        /// # Parameters
        /// | Symbol | Parameter | Description |
        /// | --- | --- | --- |
        /// | $\vec{x}$ | `pos` | Position of the particle. |
        /// | $D$ | `diffusion_constant` | Dampening constant of each particle. |
        /// | $k_BT$ | `kb_temperature` | Product of temperature and boltzmann constant $k_B T$. |
        /// | $\Delta t$ | `update_interval` | A multiple of the integration constant `dt` which determines how often a new random direction for movement is chosen. |
        ///
        /// # 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 variable `update_interval` $n_t$ determines how often a new random direction for travel
        /// is generated.
        /// 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)]
        #[cfg_attr(feature = "pyo3", pyclass)]
        pub struct $struct_name {
            /// Current position of the particle $\vec{x}$.
            pub pos: SVector<f64, $d>,
            /// Diffusion constant $D$.
            pub diffusion_constant: f64,
            /// The product of temperature and boltzmann constant $k_B T$.
            pub kb_temperature: f64,
            /// The steps it takes for the particle to update its random vector
            pub update_interval: usize,
            random_vector: SVector<f64, $d>,
        }

        #[cfg(feature = "pyo3")]
        #[cfg_attr(feature = "pyo3", pymethods)]
        impl $struct_name {
            /// Constructs a new [Brownian] mechanics model for the specified dimension.
            #[new]
            pub fn new(
                pos: [f64; $d],
                diffusion_constant: f64,
                kb_temperature: f64,
                update_interval: usize,
            ) -> Self {
                use num::Zero;
                Self {
                    pos: pos.into(),
                    diffusion_constant,
                    kb_temperature,
                    update_interval,
                    random_vector: SVector::<f64, $d>::zero(),
                }
            }
        }

        // TODO use NoVelocity struct
        impl Mechanics<SVector<f64, $d>, SVector<f64, $d>, SVector<f64, $d>> for $struct_name {
            fn pos(&self) -> SVector<f64, $d> {
                self.pos
            }

            fn velocity(&self) -> SVector<f64, $d> {
                use num::Zero;
                SVector::<f64, $d>::zero()
            }

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

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

            fn set_random_variable(
                &mut self,
                rng: &mut rand_chacha::ChaCha8Rng,
                dt: f64,
            ) -> Result<Option<f64>, RngError> {
                self.random_vector = generate_random_vector(rng, self.update_interval as f64 * dt)?;
                Ok(Some(self.update_interval as f64 * dt))
            }

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

implement_brownian_mechanis!(Brownian1D, 1);
implement_brownian_mechanis!(Brownian2D, 2);
implement_brownian_mechanis!(Brownian3D, 3);

macro_rules! define_langevin_nd(
    ($struct_name:ident, $d:literal) => {
        /// Langevin dynamics
        ///
        /// # Parameters
        /// | Symbol | Parameter | Description |
        /// | --- | --- | --- |
        /// | $\vec{X}$ | `pos` | Position of the particle. |
        /// | $\dot{\vec{X}}$ | `vel` | Velocity of the particle. |
        /// | $M$ | `mass` | Mass of the particle. |
        /// | $\gamma$ | `damping` | Damping constant |
        /// | $k_BT$ | `kb_temperature` | Product of temperature and boltzmann constant $k_B T$. |
        /// | $\Delta t$ | `update_interval` | A multiple of the integration constant `dt` which determines how often a new random direction for movement is chosen. |
        ///
        /// # 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)]
        pub struct $struct_name {
            /// Current position
            pub pos: SVector<f64, $d>,
            /// Current velocity
            pub vel: SVector<f64, $d>,
            /// Mass of the object
            pub mass: f64,
            /// Damping constant
            pub damping: f64,
            /// Product of Boltzmann constant and temperature
            pub kb_temperature: f64,
            /// Number of steps to do before updating the internal random vector again
            pub update_interval: usize,
            random_vector: SVector<f64, $d>,
        }

        impl Mechanics<SVector<f64, $d>, SVector<f64, $d>, SVector<f64, $d>> for $struct_name {
            fn pos(&self) -> SVector<f64, $d> {
                self.pos
            }

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

            fn velocity(&self) -> SVector<f64, $d> {
                self.vel
            }

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

            fn set_random_variable(
                &mut self,
                rng: &mut rand_chacha::ChaCha8Rng,
                dt: f64,
            ) -> Result<Option<f64>, RngError> {
                self.random_vector = generate_random_vector(rng, 2.0 * self.kb_temperature)?;// TODO * self.update_interval as f64 * dt)?;
                Ok(Some(self.update_interval as f64 * dt))
            }

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

        #[cfg(feature = "pyo3")]
        #[pymethods]
        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]
            pub fn new(
                pos: [f64; $d],
                vel: [f64; $d],
                mass: f64,
                damping: f64,
                kb_temperature: f64,
                update_interval: usize,
            ) -> Self {
                Self {
                    pos: pos.into(),
                    vel: vel.into(),
                    mass,
                    damping,
                    kb_temperature,
                    update_interval,
                    random_vector: [0.0; $d].into(),
                }
            }

            #[getter(pos)]
            /// Get position of object
            pub fn get_position(&self) -> [f64; $d] {
                self.pos.into()
            }

            #[setter(pos)]
            /// Set position of object
            pub fn set_position(&mut self, pos: [f64; $d]) {
                self.pos = pos.into();
            }

            #[getter(damping)]
            /// Get damping constant of object
            pub fn get_damping(&self) -> f64 {
                self.damping
            }

            #[setter(damping)]
            /// Set the damping constant of the object
            pub fn set_damping(&mut self, damping: f64) {
                self.damping = damping;
            }

            #[getter(mass)]
            /// Get mass of the object
            pub fn get_mass(&self) -> f64 {
                self.mass
            }

            #[setter(mass)]
            /// Set mass of the object
            pub fn set_mass(&mut self, mass: f64) {
                self.mass = mass;
            }

            #[getter(kb_temperature)]
            /// Get the product of Boltzmann constant and temperature
            pub fn get_kb_temperature(&self) -> f64 {
                self.kb_temperature
            }

            #[setter(kb_temperature)]
            /// Define product of Boltzmann constant and temperature
            pub fn set_kb_temperature(&mut self, kb_temperature: f64) {
                self.kb_temperature = kb_temperature;
            }

            #[getter(update_interval)]
            /// Get the update interval after which a new random vector is chosen
            pub fn get_update_interval(&self) -> usize {
                self.update_interval
            }

            #[setter(update_interval)]
            /// Sets the update interval
            pub fn set_update_interval(&mut self, update_interval: usize) {
                self.update_interval = update_interval;
            }

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

define_langevin_nd!(Langevin1D, 1);
define_langevin_nd!(Langevin2D, 2);
define_langevin_nd!(Langevin3D, 3);

/// 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)]
pub struct VertexMechanics2D<const D: usize> {
    points: nalgebra::SMatrix<f64, D, 2>,
    velocity: nalgebra::SMatrix<f64, D, 2>,
    cell_boundary_lengths: nalgebra::SVector<f64, D>,
    spring_tensions: nalgebra::SVector<f64, D>,
    cell_area: f64,
    central_pressure: f64,
    dampening_constant: f64,
}

/// Alias for the spatial representation of a cell
pub type VertexVector2<const D: usize> = nalgebra::SMatrix<f64, D, 2>;
/// Alias for a connection quantity between two individual vertices
pub type VertexConnections2<const D: usize> = nalgebra::SVector<f64, D>;

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, dampening 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,
        dampening_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.gen_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 = VertexVector2::<D>::from_row_iterator(
            (0..D)
                .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()
                })
                .flatten(),
        );
        // Randomize the boundary lengths
        let cell_boundary_lengths = VertexConnections2::<D>::from_iterator(
            points
                .row_iter()
                .circular_tuple_windows()
                .map(|(p1, p2)| (p1 - p2).norm()),
        );
        VertexMechanics2D {
            points,
            velocity: VertexVector2::<D>::zeros(),
            cell_boundary_lengths,
            spring_tensions: VertexConnections2::<D>::from_element(spring_tensions),
            cell_area,
            central_pressure,
            dampening_constant,
        }
    }

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

    /// Set the current cell area
    pub fn set_cell_area(&mut self, cell_area: f64) {
        // Calculate the relative difference to current area
        match self.cell_area {
            a if a == 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.dampening_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;
            }
        };
    }
}

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,
        dampening_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;
        let filled_rectangle = 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 = VertexVector2::<4>::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 =
                    VertexConnections2::<4>::from_iterator((0..2 * 4).map(|_| cell_side_length));

                VertexMechanics2D {
                    points,
                    velocity: VertexVector2::<4>::zeros(),
                    cell_boundary_lengths,
                    spring_tensions: VertexConnections2::<4>::from_element(spring_tensions),
                    cell_area,
                    central_pressure,
                    dampening_constant,
                }
            })
            .collect::<Vec<_>>();

        filled_rectangle
    }
}

impl<const D: usize> Mechanics<VertexVector2<D>, VertexVector2<D>, VertexVector2<D>>
    for VertexMechanics2D<D>
{
    fn pos(&self) -> VertexVector2<D> {
        self.points.clone()
    }

    fn velocity(&self) -> VertexVector2<D> {
        self.velocity.clone()
    }

    fn set_pos(&mut self, pos: &VertexVector2<D>) {
        self.points = pos.clone();
    }

    fn set_velocity(&mut self, velocity: &VertexVector2<D>) {
        self.velocity = velocity.clone();
    }

    fn calculate_increment(
        &self,
        force: VertexVector2<D>,
    ) -> Result<(VertexVector2<D>, VertexVector2<D>), 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.clone() * 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.clone();
        let dv = force + internal_force - self.dampening_constant * self.velocity.clone();
        Ok((dx, dv))
    }
}