Marine systems simulation
Z_Tree.h
1#pragma once
2
3#include <stdint.h>
4#include <iostream>
5#include <string>
6#include <vector>
7#include <unordered_map>
8#include <map>
9#include "eigen_matrix_defs.h"
10#include "compact_data_representations.h"
11#include <malloc.h>
12#include <random>
13#include <thread>
14#include <mutex>
15#include <condition_variable>
16
17namespace Fish{
18class IndividualFish;
19}
20
21namespace Grid
22{
23 typedef uint64_t bit64;
24 typedef uint32_t bit32;
25 typedef uint16_t bit16;
26 typedef uint8_t bit8;
27
28 typedef int64_t int64;
29 typedef int32_t int32;
30 typedef int16_t int16;
31 typedef int8_t int8;
32
33 typedef std::mt19937 rng_type;
34
35/*
36\class FastNeighbourLookup
37 "Close-neighbour" lookup structure, based on the 3-Dimensional Z-curve (or Morton index).
38 Object are sorted according to their Z index value.
39 This keeps spatially close elements close or "often close" in a statistical and cache friendly way
40*/
42public:
43
44 static const int max_num_neigbours = 5; // BS: 5 set by Martin Føre
47 void Init(int num_individuals);
48
49 /*
50 gives indices of a set of all or some of the 'close' neighbors to to given my_index
51 */
52 int SelectedNeigbours(int my_index, int neigbour_indices[max_num_neigbours], Fish::vec3& density_gradient);
53
54 /*
55 spawns a new thread to update neighbour lookup structure
56 */
57 void AsyncUpdate(std::vector<Fish::IndividualFish>& individuals, const double* const X);
58
59 /*
60 has the previous AsyncUpdate completed?
61 */
62 bool AsyncUpdateIsComplete();
63
64 /*
65 returns the new Z-based ordering for the index lookup
66 elements must be sorted according to this ordering for the lookup results to be correct
67 the value "array[i]" denotes the new position of the previous element "i"
68 */
69 std::vector<int>& SortedZcurveOrdering();
70
71
72private:
73 void Update();
74
75 std::vector<int> m_sorted_Z_curve_ordering;
76 std::vector<int> m_inverse_sorted_Z_curve_ordering;
77
78
79 struct Particle {
80 Particle(){}
81 Particle(const Fish::vec3& p, const Fish::vec3& v, int n);
82
83 static bit32 expand(bit32 a);
84 static bit32 make_Z_index(bit8 X, bit8 Y, bit8 Z);
85 static bit32 make_Z_index(const Eigen::Vector3d & P);
86 void make_Z_index() {
87 Z_index = make_Z_index(P);//bit8(P.x()),bit8(P.y()),bit8(P.z()));
88 }
89
90 Eigen::Matrix<bit8, 3, 1> decompress_Z_index();
91
92 //CompactData::Position P;
93 //bit8 x;
94 //bit8 y;
95 //bit8 z;
96 Eigen::Vector3d P;
97 Eigen::Vector3d V;
98 bit32 Z_index;
99 int N_index;
100 };
101
102 std::vector<Particle> m_particle_temp_buffer; // temporary sorting buffer
103 std::vector<Particle> m_particle; // position particles, sorted by Z_index
104
105 std::vector<bit32> m_Z_map; // direct lookup map from Z-index to end index of cell
106 std::vector<bit32> m_Cell; // compressed Z-map, with Zero cells omitted. index of first particle in cell
107 std::vector<int> m_neighbour_fast_map; // working buffer
108
109 struct neighbour_set {
110 neighbour_set() {for(int i = 0; i < max_num_neigbours; ++i){ix[i]=-1;}num = 0;density_gradient_unit.setZero();}
111 int ix[max_num_neigbours];
112 int num;
113 Eigen::Vector3d density_gradient_unit;
114 };
115
116 std::vector<neighbour_set> m_neighbours_buffer1;
117 std::vector<neighbour_set> m_neighbours_buffer2;
118
119 neighbour_set* read_buffer;
120 neighbour_set* write_buffer;
121
122 void UpdateWorkingLoop();
123 enum async_state {IDLE, WORKING, TERMINATING};
124 async_state m_async_state;
125
126 std::mutex m_async_mutex;
127 std::thread m_async_thread;
128 std::condition_variable m_async_condition_variable;
129
130 rng_type rng;
131};
132
133}
134
Definition: Z_Tree.h:41