NDDEM
Octree.h
Go to the documentation of this file.
1 
2 #ifndef OCTREE_H
3 #define OCTREE_H
4 
5 
6 
7 
8 
9 
10 template <int d>
11 class Octree {
12 public:
13  int init_cells (std::vector<Boundary<d>> &boundaries, std::vector<double> & r)
14  {
15  lvl_tree.resize(r.size(), -1) ;
16 
17  auto [it_min, it_max] = std::minmax_element(r.begin(), r.end()) ;
18  double rmax = *it_max ;
19  double ds = 2.1*rmax ;
20 
21  char lvl = 0;
22  double smallestdim ;
23 
24  do {
25  octree.push_back(Cells<d>()) ;
26 
27  if (lvl==0) octree[lvl].init_cells(boundaries, ds) ;
28  else octree[lvl].init_subcells(boundaries, octree[lvl-1].delta, octree[lvl-1].n_cell) ;
29 
30  smallestdim = octree[lvl].delta[0] ;
31  for (int dd=1 ; dd<d ; dd++)
32  if (octree[lvl].delta[dd]<smallestdim)
33  smallestdim = octree[lvl].delta[dd] ;
34 
35  lvl++ ;
36  }
37  while (fit_in (r, smallestdim, lvl-1) > 0) ;
38  max_lvl = lvl ;
39  return 0;
40  }
41  //----------------------------------------------------------
43  {
44  return octree.back().cum_n_cell.back() ;
45  }
46  //----------------------------------------------------------
47  int fit_in (std::vector<double> &r , double smallestdim, char lvl)
48  {
49  int leftovers=0 ;
50  for (size_t i=0 ; i<lvl_tree.size() ; i++)
51  {
52  if (2*r[i]<smallestdim/2.) leftovers ++ ;
53  if (2*r[i]<smallestdim) lvl_tree[i] = lvl ;
54  }
55  return leftovers ;
56  }
57  //------------------------------------------------------------
58  int allocate_to_cells (std::vector<std::vector<double>> & X)
59  {
60  for (int i=0 ; i<max_lvl ; i++)
61  octree[i].clear_all() ;
62 
63  //printf("A ") ;
64  for (size_t i=0 ; i<X.size() ; i++)
65  {
66  octree[lvl_tree[i]].allocate_single(X[i], i) ;
67  /*if (i==101 || i==103)
68  {
69  auto v = octree[lvl_tree[i]].id2x(res) ;
70  printf("%d, %d, %d %d %d ", lvl_tree[i], res, v[0], v[1], v[2]) ;
71  if (lvl_tree[i]==1) printf("->/2 %d ", octree[0].x2id({v[0]/2, v[1]/2, v[2]/2})) ;
72  }*/
73  }
74  //printf("\n") ;
75  return 0;
76  }
77  //------------------------------------------------------------
78  int contacts(int ID, std::pair<int,int> bounds, CLp_it_t<d> & CLp_it, ContactList<d> & CLnew, std::vector<std::vector<double>> const & X, std::vector<double> const &r, double LE_displacement)
79  {
80 
81  for (char lvl = max_lvl - 1 ; lvl>=0 ; lvl--)
82  {
83  octree[lvl].contacts(ID, bounds, CLp_it, CLnew, X, r, LE_displacement) ;
84 
85  for (char super_lvl=lvl-1 ; super_lvl>=0 ; super_lvl--)
86  octree[lvl].contacts_external (ID, octree[super_lvl], lvl-super_lvl, bounds, CLp_it, CLnew, X, r, LE_displacement) ;
87 
88  std::get<0>(bounds)/= pow(2,d) ;
89  if (lvl>0 && std::get<1>(bounds) == octree[lvl].cum_n_cell[d]) std::get<1>(bounds)=octree[lvl-1].cum_n_cell[d] ;
90  else std::get<1>(bounds) /= pow(2,d) ;
91  }
92 
93  return 0 ;
94  }
95 
96 
97  std::vector<Cells<d>> octree ;
98  std::vector<char> lvl_tree ;
99  char max_lvl=-1 ;
100 
101  template <class Archive> void serialize( Archive & ar ) { ar(octree, lvl_tree, max_lvl ); }
102 
103 
104 } ;
105 
106 #endif
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
Definition: Boundaries.h:8
All the cells making the space, with related function for creating the cell array,...
Definition: Cells.h:44
Handles lists of contacts.
Definition: ContactList.h:208
Definition: Octree.h:11
void serialize(Archive &ar)
Definition: Octree.h:101
int init_cells(std::vector< Boundary< d >> &boundaries, std::vector< double > &r)
Definition: Octree.h:13
std::vector< char > lvl_tree
Definition: Octree.h:98
int fit_in(std::vector< double > &r, double smallestdim, char lvl)
Definition: Octree.h:47
char max_lvl
Definition: Octree.h:99
int contacts(int ID, std::pair< int, int > bounds, CLp_it_t< d > &CLp_it, ContactList< d > &CLnew, std::vector< std::vector< double >> const &X, std::vector< double > const &r, double LE_displacement)
Definition: Octree.h:78
int cells_to_split()
Definition: Octree.h:42
std::vector< Cells< d > > octree
Definition: Octree.h:97
int allocate_to_cells(std::vector< std::vector< double >> &X)
Definition: Octree.h:58
uint d
Simple packing structure for the iterators to the contact list regions per particle.
Definition: Multiproc.h:23