krabmaga 0.6.0

A modern developing art for reliable and efficient Agent-based Model (ABM) simulation with the Rust language.
Documentation
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
use crate::engine::fields::field::Field;
use crate::engine::location::{Int2D, Real2D};
use crate::mpi::point_to_point::Destination;
use crate::mpi::request::WaitGuard;
use crate::mpi::topology::Communicator;
use crate::UNIVERSE;
use hashbrown::HashMap;
use mpi::point_to_point::Source;
use mpi::traits::*;
use std::cell::RefCell;
use std::cmp;

pub trait Location2D<Real2D> {
    fn get_location(self) -> Real2D;
    fn set_location(&mut self, loc: Real2D);
}

#[derive(Clone, Equivalence)]
pub struct Block {
    /// A Block is equivalent to a subtree. It has an ID, an origin point coordinates, a width and a height
    id: u32,
    x: f32,
    y: f32,
    width: f32,
    height: f32,
}

#[derive(Debug, PartialEq)]
enum Axis {
    Vertical,
    Horizontal,
}

#[derive(Clone)]
pub struct Kdtree<O: Location2D<Real2D> + Clone + Copy + PartialEq + std::fmt::Display + 'static> {
    /// ID of the field
    pub id: u32,
    /// X coordinate of the origin point of the field
    pub pos_x: f32,
    /// Y coordinate of the origin point of the field
    pub pos_y: f32,
    /// Width of the field
    width: f32,
    /// Height of the field
    height: f32,
    /// Width of the field before subdivision
    original_width: f32,
    /// Height of the field before subdivision
    original_height: f32,
    /// Matrix to write data. Vector of vectors that have a generic Object O inside
    pub locs: Vec<RefCell<Vec<Vec<O>>>>,
    /// Number of agents inside the field
    pub nagents: RefCell<usize>,
    /// Read index of the matrix
    read: usize,
    /// Write index of the matrix
    write: usize,
    /// Discretized height of the field
    pub dh: i32,
    /// Discretized width of the field
    pub dw: i32,
    /// Value to discretize `Real2D` positions to our Matrix
    discretization: f32,
    /// Vector that contains all subtrees created, as Blocks
    subtrees: Vec<Block>,
    /// Vector that contains all IDs of the neighbors of the current subtree
    pub neighbor_trees: Vec<i32>,
    /// Vector that contains all agents that have been found as neighbors of the agents from other subtrees in the previous step
    pub prec_neighbors: Vec<Vec<O>>,
    /// Vector that contains all agents that have been found as neighbors of the agents from other subtrees in the current step
    neighbors: Vec<Vec<O>>,
    /// Vector that contains all agents that have been received from neighbor subtrees
    pub received_neighbors: Vec<O>,
    /// Vector that every Halo Region of the current subtree, as Blocks
    halo_regions: Vec<Block>,
    /// Vector that contains the neighbor subtree(s) of each Halo Region
    neighbors_halo_regions: Vec<Vec<(i32, i32)>>,
    /// Vector that contains all the agents that need to be sent to the neighbors subtrees
    pub agents_to_send: Vec<Vec<O>>,
    /// HashMap where the keys are the id of an agent and the values are the id assgined to that agent in the scheduler
    pub scheduled_agent: HashMap<u32, u32>,
    /// The max distance from the edges of the field in which to find the neighbors
    distance: f32,
    /// Number of processors assigned to the simulation
    processors: u32,
    /// Field density
    pub density_estimation: usize,
    /// `true` if you want calculate field density, `false` otherwise
    pub density_estimation_check: bool,
}

impl<
        O: Location2D<Real2D>
            + Clone
            + Copy
            + PartialEq
            + std::fmt::Display
            + mpi::datatype::Equivalence,
    > Kdtree<O>
{
    /// Creates a new `KdTree`. WARNING: Use the create_tree(...) function instead of this.
    ///
    /// # Arguments
    /// * `id` - ID of the process it will be assigned to - Starts from 0
    /// * `pos_x` - The X coordinate of the origin point of the field
    /// * `pos_y` - The Y coordinate of the origin point of the field
    /// * `width` - The width of the field
    /// * `height` - The height of the field
    /// * `discretization` - The value to discretize `Real2D` positions to our Matrix
    /// * `distance` - The max distance from the edges of the field in which to find the neighbors
    pub fn new(
        id: u32,
        pos_x: f32,
        pos_y: f32,
        width: f32,
        height: f32,
        discretization: f32,
        distance: f32,
    ) -> Self {
        Kdtree {
            id,
            pos_x,
            pos_y,
            locs: vec![
                RefCell::new(
                    std::iter::repeat_with(Vec::new)
                        .take(
                            (((width / discretization).ceil() + 1.0)
                                * ((height / discretization).ceil() + 1.0))
                                as usize,
                        )
                        .collect(),
                ),
                RefCell::new(
                    std::iter::repeat_with(Vec::new)
                        .take(
                            (((width / discretization).ceil() + 1.0)
                                * ((height / discretization).ceil() + 1.0))
                                as usize,
                        )
                        .collect(),
                ),
            ],
            subtrees: Vec::new(),
            neighbor_trees: Vec::new(),
            nagents: RefCell::new(0),
            original_width: 0.,
            original_height: 0.,
            read: 0,
            write: 1,
            dh: ((height / discretization).ceil() as i32 + 1),
            dw: ((width / discretization).ceil() as i32 + 1),
            discretization,
            width,
            height,
            processors: 0,
            prec_neighbors: Vec::new(),
            neighbors: vec![vec![]; 4],
            received_neighbors: Vec::new(),
            halo_regions: Vec::new(),
            neighbors_halo_regions: Vec::new(),
            agents_to_send: Vec::new(),
            scheduled_agent: HashMap::new(),
            distance,
            density_estimation: 0,
            density_estimation_check: false,
        }
    }

    /// Creates the KdTree, splits it into subtrees and returns the root
    pub fn create_tree(
        id: u32,
        x: f32,
        y: f32,
        width: f32,
        height: f32,
        discretization: f32,
        distance: f32,
    ) -> Self {
        let mut tree = Kdtree::new(id, x, y, width, height, discretization, distance);
        tree.original_height = height;
        tree.original_width = width;
        tree.first_subdivision();
        tree
    }

    /// Splits the tree into subtrees based on the number of processors assigned to the simulation
    pub fn first_subdivision(&mut self) {
        let world = UNIVERSE.world();
        let mut temp_subtrees: Vec<Kdtree<O>> = vec![];

        if self.processors == 0 {
            self.processors = world.size() as u32;
        }

        if world.size() == 1 {
            println!("Running distributed (MPI) Kdtree with a single processor");
            println!(
                "Generated id{} for {};{} w:{} h:{}",
                self.id, self.pos_x, self.pos_y, self.width, self.height
            );
        }

        if world.size() != 1 {
            if world.rank() == 0 {
                println!(
                    "Running distributed (MPI) Kdtree with {} processors",
                    self.processors
                );

                if self.processors != 1
                //Root subdivision
                {
                    //First split is a vertical split and generates 2 subtrees
                    let nodes = self.split(&Axis::Vertical);
                    temp_subtrees.push(nodes.0);
                    temp_subtrees.push(nodes.1);

                    let mut count = 2;
                    let mut axis = Axis::Horizontal;

                    //Splits every subtree generated into as many subtrees as the number of processors
                    while count < self.processors {
                        for n in 0..temp_subtrees.len() {
                            if count >= self.processors {
                                break;
                            }
                            let nodes = temp_subtrees[n * 2].split(&axis);
                            temp_subtrees[n * 2] = nodes.0;
                            temp_subtrees.insert((n * 2) + 1, nodes.1);
                            count += 1;
                        }
                        if axis == Axis::Vertical {
                            axis = Axis::Horizontal;
                        } else {
                            axis = Axis::Vertical;
                        }
                    }
                }

                //Creates the equivalent Block of every subtree created, then stores them into 'subtrees'
                let mut subtree_id = self.id;
                for subtree in temp_subtrees.iter_mut() {
                    let block = Block {
                        id: subtree_id,
                        x: subtree.pos_x,
                        y: subtree.pos_y,
                        width: subtree.width,
                        height: subtree.height,
                    };
                    self.subtrees.push(block);
                    println!(
                        "Generated id {} for {};{} w:{} h:{}",
                        subtree_id, subtree.pos_x, subtree.pos_y, subtree.width, subtree.height
                    );
                    subtree_id += 1;
                }

                //If the subtree ID is equal to the processor rank, change its width and height values to the new ones
                for sub in self.subtrees.iter() {
                    if sub.id as i32 == world.rank() {
                        self.width = sub.width;
                        self.height = sub.height;
                    }
                }

                //Calculates neighbor subtrees IDs
                self.agents_to_send = vec![vec![]; UNIVERSE.world().size() as usize];
                for sub in self.subtrees.iter() {
                    if sub.id as i32 != world.rank() {
                        let other_points = self.get_block_boundary_points(sub, true);
                        if self.get_boundary_points(true).iter().any(|&self_point| {
                            other_points
                                .iter()
                                .any(|&other_point| self_point == other_point)
                        }) {
                            self.neighbor_trees.push(sub.id as i32);
                        }
                    }
                }

                //Send the generated subtrees to all processors
                for i in 1..world.size() {
                    world.process_at_rank(i).send(&self.subtrees);
                }
            } else {
                //Receives all subtrees and calculates their neighbors
                let (subtrees, _) = world.process_at_rank(0).receive_vec::<Block>();
                self.agents_to_send = vec![vec![]; UNIVERSE.world().size() as usize];
                self.subtrees = subtrees;
                for subtree in self.subtrees.iter() {
                    if subtree.id as i32 == world.rank() {
                        self.width = subtree.width;
                        self.height = subtree.height;
                        self.pos_x = subtree.x;
                        self.pos_y = subtree.y;
                        for sub in self.subtrees.iter() {
                            if sub.id as i32 != world.rank() {
                                let other_points = self.get_block_boundary_points(sub, true);
                                if self.get_boundary_points(true).iter().any(|&self_point| {
                                    other_points
                                        .iter()
                                        .any(|&other_point| self_point == other_point)
                                }) {
                                    self.neighbor_trees.push(sub.id as i32);
                                }
                            }
                        }
                    }
                }
            }
        }

        //Calculate Halo Regions for the current subtree
        self.calculate_regions();

        //Calculate the neighbor subtrees of all the Halo Regions of the current subtree
        self.calculate_neighbor_regions();
    }

    /// Calculates the 4 vertices of the current KdTree
    ///
    /// # Arguments
    /// * `toroidal` - 'true' if the points have to be toroidal, 'false' otherwise
    fn get_boundary_points(&self, toroidal: bool) -> Vec<(f32, f32)> {
        let (x, y) = (self.pos_x, self.pos_y);
        let (width, height) = (self.width, self.height);
        let o_w = self.original_width;
        let o_h = self.original_height;

        let mut points = vec![
            (x, y),
            (x + width, y),
            (x, y + height),
            (x + width, y + height),
        ];
        if toroidal {
            let toroidal_points = vec![
                (x % o_w, y % o_h),
                ((x + width) % o_w, y % o_h),
                (x % o_w, (y + height) % o_h),
                ((x + width) % o_w, (y + height) % o_h),
            ];

            points.extend(toroidal_points.iter().cloned());
        }

        points
    }

    /// Calculates the 4 vertices of the Block
    ///
    /// # Arguments
    /// * `toroidal` - 'true' if the points have to be toroidal, 'false' otherwise
    fn get_block_boundary_points(&self, block: &Block, toroidal: bool) -> Vec<(f32, f32)> {
        let (x, y) = (block.x, block.y);
        let (width, height) = (block.width, block.height);
        let o_w = self.original_width;
        let o_h = self.original_height;

        let mut points = vec![
            (x, y),
            (x + width, y),
            (x, y + height),
            (x + width, y + height),
        ];
        if toroidal {
            let toroidal_points = vec![
                (x % o_w, y % o_h),
                ((x + width) % o_w, y % o_h),
                (x % o_w, (y + height) % o_h),
                ((x + width) % o_w, (y + height) % o_h),
            ];
            points.extend(toroidal_points.iter().cloned());
        }
        points
    }

    /// Calculates the 4 halo regions of the subtree
    fn calculate_regions(&mut self) {
        let h = self.distance;
        let w = self.distance;

        let north_y = self.pos_y + self.height - self.distance;
        let east_x = self.pos_x + self.width - self.distance;
        let south_y = self.pos_y;
        let west_x = self.pos_x;

        let north = Block {
            id: 0,
            x: self.pos_x,
            y: north_y,
            width: self.width,
            height: h,
        };
        let east = Block {
            id: 1,
            x: east_x,
            y: self.pos_y,
            width: w,
            height: self.height,
        };
        let south = Block {
            id: 2,
            x: self.pos_x,
            y: south_y,
            width: self.width,
            height: h,
        };
        let west = Block {
            id: 3,
            x: west_x,
            y: self.pos_y,
            width: w,
            height: self.height,
        };

        self.halo_regions.push(north);
        self.halo_regions.push(east);
        self.halo_regions.push(south);
        self.halo_regions.push(west);
    }

    /// Calculates the neighbor subtrees of each halo region
    fn calculate_neighbor_regions(&mut self) {
        let world = UNIVERSE.world();

        for region in self.halo_regions.iter() {
            let region_points = self.get_block_boundary_points(region, true);
            let mut neighbor_blocks_region = Vec::new();
            for block in self.subtrees.iter() {
                if block.id as i32 != world.rank() {
                    let block_points = self.get_block_boundary_points(block, true);
                    if region_points.iter().any(|&region_points| {
                        block_points
                            .iter()
                            .any(|&block_points| region_points == block_points)
                    }) {
                        neighbor_blocks_region.push((region.id as i32, block.id as i32))
                    }
                }
            }
            if neighbor_blocks_region.len() > 0 {
                self.neighbors_halo_regions.push(neighbor_blocks_region);
            }
        }
    }

    /// Splits the tree into two subtrees
    ///
    /// # Arguments
    /// * `direction` - 'Axis::Vertical' if the split must be done on the vertical axis, 'Axis::Horizontal' otherwise
    fn split(&mut self, direction: &Axis) -> (Kdtree<O>, Kdtree<O>) {
        let id = self.id.clone();
        let mut node_x = self.pos_x;
        let mut node_y = self.pos_y;
        let mut node_w = self.width;
        let mut node_h = self.height;

        let mut n1 = self.clone();
        n1.locs.clear();

        match direction {
            Axis::Vertical => {
                n1.width = n1.width / 2.0;
                node_x = self.pos_x + self.width / 2.0;
                node_w = self.width / 2.0;
            }
            Axis::Horizontal => {
                self.height = self.height / 2.0;
                n1.height = self.height;
                node_y = self.pos_y + self.height;
                node_h = self.height;
            }
        }
        let n2 = Kdtree::new(
            id,
            node_x,
            node_y,
            node_w,
            node_h,
            self.discretization,
            self.distance,
        );

        return (n1, n2);
    }

    /// Gets the ID of the subtree that 'contains' a given coordinate
    ///
    /// # Arguments
    /// * `x` - The X coordinate
    /// * `y` - The Y coordinate
    pub fn get_block_by_location(&self, x: f32, y: f32) -> i32 {
        let world = UNIVERSE.world();
        if world.size() == 1 {
            return 0;
        }
        for block in self.subtrees.iter() {
            if block.x <= x
                && x <= block.x + block.width
                && block.y <= y
                && y <= block.y + block.height
            {
                return block.id as i32;
            }
        }
        panic!(
            "Block for location {};{} not found! This should not happen!",
            x, y
        );
    }

    /// Inserts an agent into the field based on its location
    ///
    /// # Arguments
    /// * `agent` - The agent to insert
    /// * `loc` - The location of the agent
    pub fn insert(&mut self, agent: O, loc: Real2D) {
        let bag = self.discretize(&loc);
        let index = ((bag.x * self.dh) + bag.y) as usize;
        let mut bags = self.locs[self.write].borrow_mut();
        bags[index].push(agent);

        for region in &self.halo_regions {
            if region.x <= loc.x
                && loc.x <= region.x + region.width
                && region.y <= loc.y
                && loc.y <= region.y + region.height
            {
                self.neighbors[region.id as usize].push(agent);
                break;
            }
        }
        if !self.density_estimation_check {
            *self.nagents.borrow_mut() += 1;
        }
        drop(bags);
    }

    /// Inserts an agent into the field (in read mode) based on its location
    ///
    /// # Arguments
    /// * `agent` - The agent to insert
    /// * `loc` - The location of the agent
    pub fn insert_read(&mut self, agent: O, loc: Real2D) {
        let bag = self.discretize(&loc);
        let index = ((bag.x * self.dh) + bag.y) as usize;
        let mut bags = self.locs[self.read].borrow_mut();
        bags[index].push(agent);

        if !self.density_estimation_check {
            *self.nagents.borrow_mut() += 1;
        }
    }

    /// Removes an agent from the field based on its location
    ///
    /// # Arguments
    /// * `object` - The agent to remove
    /// * `loc` - The location of the agent
    pub fn remove_object_location(&self, object: O, loc: Real2D) {
        let bag = self.discretize(&loc);
        let index = ((bag.x * self.dh) + bag.y) as usize;
        let mut bags = self.locs[self.write].borrow_mut();
        if !bags[index].is_empty() {
            let before = bags[index].len();
            bags[index].retain(|&x| x != object);
            let after = bags[index].len();

            if !self.density_estimation_check {
                *self.nagents.borrow_mut() -= before - after;
            }
        }
    }

    /* fn contains(&self, x: f32, y: f32) -> bool {
        self.pos_x <= x
            && x < self.pos_x + self.width
            && self.pos_y <= y
            && y < self.pos_y + self.height
    } */

    /* fn calculate_median(&self, agents: &Vec<(O, i32, i32)>) -> i32 {
        let len = agents.len();

        if len >= 1 && len % 2 == 1 {
            return agents[len / 2].1;
        } else if len >= 1 {
            return (agents[len / 2 - 1].1 + agents[(len / 2)].1) / 2;
        } else {
            return 0;
        }
    } */

    /* fn calculate_median_on_y(&self, agents: &Vec<(O, i32, i32)>) -> i32 {
        let len = agents.len();

        if len > 1 && len % 2 == 1 {
            return agents[len / 2].2;
        } else if len > 1 {
            return (agents[len / 2 - 1].2 + agents[(len / 2)].2) / 2;
        } else {
            return 0;
        }
    } */

    /// Map coordinates of an object into matrix indexes
    ///
    /// # Arguments
    /// * `loc` - `Real2D` coordinates of the object
    fn discretize(&self, loc: &Real2D) -> Int2D {
        let x_floor = (loc.x / self.discretization).floor();
        let x_floor = x_floor as i32;

        let y_floor = (loc.y / self.discretization).floor();
        let y_floor = y_floor as i32;

        Int2D {
            x: x_floor,
            y: y_floor,
        }
    }

    /// Returns the set of objects within a certain distance.
    ///
    /// # Arguments
    /// * `loc` - `Real2D` coordinates of the object
    /// * `dist` - Distance to look for objects
    pub fn get_neighbors_within_distance(&self, loc: Real2D, dist: f32) -> Vec<O> {
        let mut neighbors: Vec<O>;

        neighbors = Vec::new();

        if dist <= 0.0 {
            return neighbors;
        }

        let disc_dist = (dist / self.discretization).floor() as i32;
        let disc_loc = self.discretize(&loc);
        let max_x = (self.original_width / self.discretization).ceil() as i32;
        let max_y = (self.original_height / self.discretization).ceil() as i32;

        let mut min_i = disc_loc.x - disc_dist;
        let mut max_i = disc_loc.x + disc_dist;
        let mut min_j = disc_loc.y - disc_dist;
        let mut max_j = disc_loc.y + disc_dist;

        min_i = cmp::max(0, min_i);
        max_i = cmp::min(max_i, max_x - 1);
        min_j = cmp::max(0, min_j);
        max_j = cmp::min(max_j, max_y - 1);

        for i in min_i..max_i + 1 {
            for j in min_j..max_j + 1 {
                let bag_id = Int2D {
                    x: t_transform(i, max_x),
                    y: t_transform(j, max_y),
                };

                let check = check_circle(
                    &bag_id,
                    self.discretization,
                    self.original_width,
                    self.original_height,
                    &loc,
                    dist,
                    true,
                );

                let index = ((bag_id.x * self.dh) + bag_id.y) as usize;
                // let bags = self.rbags.borrow();
                let bags = self.locs[self.read].borrow();

                for elem in &bags[index] {
                    if ((check == 0
                        && distance(
                            &loc,
                            &(elem.get_location()),
                            self.original_width,
                            self.original_height,
                            true,
                        ) <= dist)
                        || check == 1)
                        && elem.get_location() != loc
                    {
                        neighbors.push(*elem);
                    }
                }
            }
        }
        neighbors
    }

    /// Function that starts the message exchange phase.
    /// Step 1: The process sends to its neighbors the number of agents it will send to them
    /// Step 2: The process allocates memory for the upcoming agents
    /// Step 3: The agents will be sent to its neighbors and received from its neighbors
    /// Step 4: Return the received agents
    ///
    /// # Arguments
    /// * `agents_to_send` - The agents that must be sent to the other processes
    /// * `dummy` - A dummy agent that will be used for mmemory allocation
    /// * `with_regions` - 'true' if the agents must be sent only to the neighbors of its Halo Regions, 'false' otherwise
    pub fn message_exchange(
        &self,
        agents_to_send: &Vec<Vec<O>>,
        dummy: O,
        with_regions: bool,
    ) -> Vec<Vec<O>> {
        let world = UNIVERSE.world();
        let mut received_messages = vec![0; world.size() as usize];
        let mut send_vec = vec![0; world.size() as usize];
        let mut send_agent_vec: Vec<Vec<O>> = vec![vec![]; world.size() as usize];

        if with_regions {
            for region in self.neighbors_halo_regions.iter() {
                for neighbor in region.iter() {
                    send_vec[neighbor.1 as usize] += agents_to_send[neighbor.0 as usize].len();
                    send_agent_vec[neighbor.1 as usize]
                        .extend(agents_to_send[neighbor.0 as usize].iter())
                }
            }
        } else {
            for neighbor in &self.neighbor_trees {
                send_vec[*neighbor as usize] += self.agents_to_send[*neighbor as usize].len();
                send_agent_vec[*neighbor as usize]
                    .extend(self.agents_to_send[*neighbor as usize].iter())
            }
        }

        //I make a receive of messages from all my neighbors and send to all my neighbors. A message contains the number of agents i will receive.
        for neighbor in &self.neighbor_trees {
            mpi::request::scope(|scope| {
                let ln = &send_vec[*neighbor as usize];
                let _rreq = WaitGuard::from(
                    world
                        .process_at_rank(*neighbor)
                        .immediate_receive_into_with_tag(
                            scope,
                            &mut received_messages[*neighbor as usize],
                            *neighbor,
                        ),
                );
                let _sreq = WaitGuard::from(
                    world
                        .process_at_rank(*neighbor)
                        .immediate_ready_send_with_tag(scope, ln, world.rank()),
                );
            });
        }

        //Allocate memory based on the number of agents i will receive
        let mut vec: Vec<Vec<O>> = vec![vec![]; world.size() as usize];
        if received_messages.len() > 0 {
            for i in &self.neighbor_trees {
                if received_messages[*i as usize] != 0 {
                    vec[*i as usize].append(&mut vec![dummy; received_messages[*i as usize]]);
                } else {
                    vec[*i as usize].append(&mut vec![]);
                }
            }
        }

        // I receive the agents from my neighbors and send my agents to them.
        mpi::request::multiple_scope(world.size() as usize, |scope, coll| {
            for (id, buffer) in vec.iter_mut().enumerate() {
                if received_messages[id as usize] != 0 {
                    let rreq = world
                        .process_at_rank(id as i32)
                        .immediate_receive_into_with_tag(scope, &mut buffer[..], world.rank());
                    coll.add(rreq);
                }
            }

            for id in self.neighbor_trees.iter() {
                if send_agent_vec[*id as usize].len() != 0 {
                    let sreq = world.process_at_rank(*id).immediate_send_with_tag(
                        scope,
                        &send_agent_vec[*id as usize][..],
                        *id,
                    );
                    coll.add(sreq);
                }
            }
            let mut out = vec![];
            coll.wait_all(&mut out);
        });
        return vec;
    }

    /// Returns the set of objects within a certain relaxed distance.
    ///
    /// # Arguments
    /// * `loc` - `Real2D` coordinates of the object
    /// * `dist` - Distance to look for objects
    pub fn get_distributed_neighbors_within_relax_distance(
        &mut self,
        loc: Real2D,
        distance: f32,
    ) -> Vec<O> {
        let mut dist = distance;

        if dist > self.distance {
            dist = self.distance;
        }

        let mut neighbors: Vec<O>;

        neighbors = Vec::new();

        if dist <= 0.0 {
            return neighbors;
        }

        let disc_dist = (dist / self.discretization).floor() as i32;
        let disc_loc = self.discretize(&loc);
        let max_x = (self.original_width / self.discretization).ceil() as i32;
        let max_y = (self.original_height / self.discretization).ceil() as i32;

        let mut min_i = disc_loc.x - disc_dist;
        let mut max_i = disc_loc.x + disc_dist;
        let mut min_j = disc_loc.y - disc_dist;
        let mut max_j = disc_loc.y + disc_dist;

        min_i = cmp::max(0, min_i);
        max_i = cmp::min(max_i, max_x - 1);
        min_j = cmp::max(0, min_j);
        max_j = cmp::min(max_j, max_y - 1);

        for i in min_i..max_i + 1 {
            for j in min_j..max_j + 1 {
                let bag_id = Int2D {
                    x: t_transform(i, max_x),
                    y: t_transform(j, max_y),
                };

                let index = ((bag_id.x * self.dh) + bag_id.y) as usize;
                let bags = self.locs[self.read].borrow();

                for elem in &bags[index] {
                    if elem.get_location() != loc {
                        neighbors.push(*elem);
                    }
                }
            }
        }
        neighbors
    }
}

impl<O: Location2D<Real2D> + Clone + Copy + PartialEq + std::fmt::Display> Drop for Kdtree<O> {
    fn drop(&mut self) {}
}

impl<O: Location2D<Real2D> + Eq + Clone + Copy + std::fmt::Display> Field for Kdtree<O> {
    /// Swap read and write buffer, puts current neighbors into prec_neighbors and clears all vectors
    fn lazy_update(&mut self) {
        self.prec_neighbors = Vec::new();
        self.prec_neighbors.append(&mut self.neighbors);
        self.neighbors = vec![vec![]; 4];
        self.agents_to_send = vec![vec![]; UNIVERSE.world().size() as usize];
        self.received_neighbors.clear();
        std::mem::swap(&mut self.read, &mut self.write);

        if !self.density_estimation_check {
            self.density_estimation = (*self.nagents.borrow_mut()) / ((self.dw * self.dh) as usize);
            self.density_estimation_check = true;
            self.locs[self.write] = RefCell::new(
                std::iter::repeat_with(|| Vec::with_capacity(self.density_estimation))
                    .take((self.dw * self.dh) as usize)
                    .collect(),
            );
        } else {
            let mut bags = self.locs[self.write].borrow_mut();
            for b in 0..bags.len() {
                bags[b].clear();
            }
        }
    }

    fn update(&mut self) {}
}

fn t_transform(n: i32, size: i32) -> i32 {
    if n >= 0 {
        n % size
    } else {
        (n % size) + size
    }
}

fn check_circle(
    bag: &Int2D,
    discretization: f32,
    width: f32,
    height: f32,
    loc: &Real2D,
    dis: f32,
    tor: bool,
) -> i8 {
    let nw = Real2D {
        x: (bag.x as f32) * discretization,
        y: (bag.y as f32) * discretization,
    };
    let ne = Real2D {
        x: nw.x,
        y: (nw.y + discretization).min(height),
    };
    let sw = Real2D {
        x: (nw.x + discretization).min(width),
        y: nw.y,
    };
    let se = Real2D { x: sw.x, y: ne.y };

    if distance(&nw, loc, width, height, tor) <= dis
        && distance(&ne, loc, width, height, tor) <= dis
        && distance(&sw, loc, width, height, tor) <= dis
        && distance(&se, loc, width, height, tor) <= dis
    {
        1
    } else if distance(&nw, loc, width, height, tor) > dis
        && distance(&ne, loc, width, height, tor) > dis
        && distance(&sw, loc, width, height, tor) > dis
        && distance(&se, loc, width, height, tor) > dis
    {
        -1
    } else {
        0
    }
}

fn distance(loc1: &Real2D, loc2: &Real2D, dim1: f32, dim2: f32, tor: bool) -> f32 {
    let dx;
    let dy;

    if tor {
        dx = toroidal_distance(loc1.x, loc2.x, dim1);
        dy = toroidal_distance(loc1.y, loc2.y, dim2);
    } else {
        dx = loc1.x - loc2.x;
        dy = loc1.y - loc2.y;
    }
    (dx * dx + dy * dy).sqrt()
}

pub fn toroidal_distance(val1: f32, val2: f32, dim: f32) -> f32 {
    if (val1 - val2).abs() <= dim / 2.0 {
        return val1 - val2;
    }

    let d = toroidal_transform(val1, dim) - toroidal_transform(val2, dim);

    if d * 2.0 > dim {
        d - dim
    } else if d * 2.0 < -dim {
        d + dim
    } else {
        d
    }
}

pub fn toroidal_transform(val: f32, dim: f32) -> f32 {
    if val >= 0.0 && val < dim {
        val
    } else {
        let mut val = val % dim;
        if val < 0.0 {
            val += dim;
        }
        val
    }
}