SlowDown Algorithmic Regularization (SDAR)
Algorithmic Regularization with slowdown method for integrating few-body motions
neighbor.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Common/Float.h"
6 #include <limits>
7 
8 namespace H4 {
10  enum class NBType {single, group, none};
11 
12  // neighbor address
13  template <class Tparticle>
14  struct NBAdr{
17  // adr;
18  void* adr;
19  int index;
20  NBType type; // -1: undefine; 0: H4particle; 1: ParticleGroup
21 
22  NBAdr(): adr(NULL), index(-1), type(NBType::none) {}
23 
24  NBAdr(Single* _adr, const int _index): adr((void*)_adr), index(_index), type(NBType::single) {}
25 
26  NBAdr(Group* _adr, const int _index): adr((void*)_adr), index(_index), type(NBType::group) {}
27 
28  NBAdr& operator = (const NBAdr& _nb) {
29  adr = _nb.adr;
30  index =_nb.index;
31  type = _nb.type;
32  return *this;
33  }
34 
35  };
36 
37 
39  template <class Tparticle>
40  class Neighbor{
41  public:
42 
43  int r_min_index; // nearest neighbor index for each ptcl
44  int mass_min_index; // mimimum mass in neighbors
45  Float r_min_sq; // nearest neighbor distance square
46  Float r_min_mass; // nearest neighbor index for each ptcl
47  Float mass_min; // mimimum mass in neighbors
48  Float r_neighbor_crit_sq; // neighbor radius criterion
49  bool need_resolve_flag; // indicate whether the members need to be resolved for outside
50  bool initial_step_flag; // indicate whether the time step need to be initialized due to the change of neighbors
51  int n_neighbor_group; // number of group neighbor
52  int n_neighbor_single; // number of single neighbor
53  COMM::List<NBAdr<Tparticle>> neighbor_address; // neighbor perturber address
54 
57 
59 
61  bool checkParams() {
62  ASSERT(r_neighbor_crit_sq>0.0);
63  return true;
64  }
65 
66 
68 
72  void printColumnTitle(std::ostream & _fout, const int _width=20) {
73  _fout<<std::setw(_width)<<"r_min_index"
74  <<std::setw(_width)<<"mass_min_index"
75  <<std::setw(_width)<<"r_min_sq"
76  <<std::setw(_width)<<"r_min_mass"
77  <<std::setw(_width)<<"mass_min"
78  <<std::setw(_width)<<"r_neighbor_crit_sq"
79  <<std::setw(_width)<<"need_resolve_flag"
80  <<std::setw(_width)<<"initial_step_flag"
81  <<std::setw(_width)<<"n_neighbor_group"
82  <<std::setw(_width)<<"n_neighbor_single";
83  }
84 
86 
90  void printColumn(std::ostream & _fout, const int _width=20){
91  _fout<<std::setw(_width)<<r_min_index
92  <<std::setw(_width)<<mass_min_index
93  <<std::setw(_width)<<r_min_sq
94  <<std::setw(_width)<<r_min_mass
95  <<std::setw(_width)<<mass_min
96  <<std::setw(_width)<<r_neighbor_crit_sq
97  <<std::setw(_width)<<need_resolve_flag
98  <<std::setw(_width)<<initial_step_flag
99  <<std::setw(_width)<<n_neighbor_group
100  <<std::setw(_width)<<n_neighbor_single;
101  }
102 
104 
107  void reserveMem(const int _nmax) {
109  neighbor_address.reserveMem(_nmax);
110  }
111 
114  r_min_index = -1;
115  mass_min_index = -1;
118  need_resolve_flag = false;
119  initial_step_flag = false;
120  }
121 
123  void resetNeighbor() {
124  r_min_index = -1;
125  mass_min_index = -1;
128  n_neighbor_group = 0;
129  n_neighbor_single = 0;
130  neighbor_address.resizeNoInitialize(0);
131  }
132 
134  void clear() {
136  r_neighbor_crit_sq = -1.0;
137  n_neighbor_group = 0;
138  n_neighbor_single = 0;
139  neighbor_address.clear();
140  }
141 
143  void clearNoFreeMem() {
145  n_neighbor_group = 0;
146  n_neighbor_single = 0;
147  neighbor_address.resizeNoInitialize(0);
148  }
149 
151  template <class Tp>
152  void checkAndAddNeighborSingle(const Float _r2, Tp& _particle, const Neighbor<Tparticle>& _nbp, const int _index) {
153  if (_r2<std::max(r_neighbor_crit_sq,_nbp.r_neighbor_crit_sq)) {
154  neighbor_address.addMember(NBAdr<Tparticle>(&_particle, _index));
156  }
157  // mass weighted nearest neigbor
158  Float mass = _particle.mass;
159  if (_r2*r_min_mass < r_min_sq*mass) {
160  r_min_sq = _r2;
161  r_min_index = _index;
162  r_min_mass = mass;
163  }
164  // minimum mass
165  if(mass_min>mass) {
166  mass_min = mass;
167  mass_min_index = _index;
168  }
169  // if neighbor step need update, also set update flag for i particle
170  if(_nbp.initial_step_flag) initial_step_flag = true;
171  }
172 
174  template <class Tgroup>
175  void checkAndAddNeighborGroup(const Float _r2, Tgroup& _group, const int _index) {
176  ASSERT(_r2>0.0);
177  if (_r2<std::max(r_neighbor_crit_sq, _group.perturber.r_neighbor_crit_sq)) {
178  neighbor_address.addMember(NBAdr<Tparticle>(&_group.particles,_index));
180  }
181  // mass weighted nearest neigbor
182  Float mass_cm = _group.particles.cm.mass;
183  if (_r2*r_min_mass < r_min_sq*mass_cm) {
184  r_min_sq = _r2;
185  r_min_index = _index;
186  r_min_mass = mass_cm;
187  }
188  // minimum mass
189  if(mass_min>mass_cm) {
190  mass_min = mass_cm;
191  mass_min_index = _index;
192  }
193  // if neighbor step need update, also set update flag for i particle
194  if(_group.perturber.initial_step_flag) initial_step_flag = true;
195  }
196 
198 
201  // always resolve
202  if (!need_resolve_flag) {
203  need_resolve_flag = true;
204  initial_step_flag = true;
205  }
206  // _kappa>3.0 seems cause oscillation
207  /* suppress unresolved flag
208  if (_kappa>11.0 && need_resolve_flag) {
209  need_resolve_flag = false;
210  initial_step_flag = true;
211  }
212  */
213  }
214 
215  };
216 }
H4::Neighbor::checkParams
bool checkParams()
check whether parameters values are correct
Definition: neighbor.h:61
H4::Neighbor::mass_min_index
int mass_min_index
Definition: neighbor.h:44
H4::NBAdr::NBAdr
NBAdr(Group *_adr, const int _index)
Definition: neighbor.h:26
H4::NBType
NBType
type of neighbor address
Definition: neighbor.h:10
H4::Neighbor::Neighbor
Neighbor()
constructor
Definition: neighbor.h:56
H4::Neighbor::checkGroupResolve
void checkGroupResolve()
check whether members should be resolved for outside
Definition: neighbor.h:200
H4::NBAdr::index
int index
Definition: neighbor.h:19
H4::Neighbor::resetNeighbor
void resetNeighbor()
reset neighbor information
Definition: neighbor.h:123
H4::Neighbor::clearNoFreeMem
void clearNoFreeMem()
clear no release memory
Definition: neighbor.h:143
Float.h
H4::Neighbor::n_neighbor_group
int n_neighbor_group
Definition: neighbor.h:51
NUMERIC_FLOAT_MAX
const Float NUMERIC_FLOAT_MAX
Definition: Float.h:29
H4::Neighbor::r_min_mass
Float r_min_mass
Definition: neighbor.h:46
H4::Neighbor::need_resolve_flag
bool need_resolve_flag
Definition: neighbor.h:49
H4::Neighbor::r_neighbor_crit_sq
Float r_neighbor_crit_sq
Definition: neighbor.h:48
H4::Neighbor::checkAndAddNeighborGroup
void checkAndAddNeighborGroup(const Float _r2, Tgroup &_group, const int _index)
check and add neighbor of group
Definition: neighbor.h:175
H4::Neighbor::reserveMem
void reserveMem(const int _nmax)
reserve memory for neighbor lists
Definition: neighbor.h:107
H4::Neighbor::checkAndAddNeighborSingle
void checkAndAddNeighborSingle(const Float _r2, Tp &_particle, const Neighbor< Tparticle > &_nbp, const int _index)
check and add neighbor of single
Definition: neighbor.h:152
H4::Neighbor::printColumn
void printColumn(std::ostream &_fout, const int _width=20)
print data of class members using column style
Definition: neighbor.h:90
H4::NBAdr::operator=
NBAdr & operator=(const NBAdr &_nb)
Definition: neighbor.h:28
H4::Neighbor::clear
void clear()
clear function
Definition: neighbor.h:134
H4::NBAdr::adr
void * adr
Definition: neighbor.h:18
H4::Neighbor::clearNoFreeMemNoResizeNeighborAdress
void clearNoFreeMemNoResizeNeighborAdress()
clear function
Definition: neighbor.h:113
particle_group.h
H4::ParticleH4
particle type for AR integrator, not necessary anymore
Definition: hermite_particle.h:6
H4::NBAdr::type
NBType type
Definition: neighbor.h:20
COMM::ParticleGroup
Particle group class to store and manage a group of particle.
Definition: particle_group.h:13
H4::Neighbor::printColumnTitle
void printColumnTitle(std::ostream &_fout, const int _width=20)
print titles of class members using column style
Definition: neighbor.h:72
Float
double Float
Definition: Float.h:25
H4::NBAdr
Definition: neighbor.h:14
hermite_particle.h
H4::Neighbor::r_min_sq
Float r_min_sq
Definition: neighbor.h:45
H4::NBType::single
@ single
H4::Neighbor::mass_min
Float mass_min
Definition: neighbor.h:47
H4::Neighbor::neighbor_address
COMM::List< NBAdr< Tparticle > > neighbor_address
Definition: neighbor.h:53
COMM::List
list class to store and manage a group of member
Definition: list.h:19
H4::NBAdr::Group
COMM::ParticleGroup< Tparticle, ParticleH4< Tparticle > > Group
Definition: neighbor.h:16
H4
Definition: ar_information.h:9
COMM::ListMode::local
@ local
H4::NBAdr::Single
ParticleH4< Tparticle > Single
Definition: neighbor.h:15
H4::Neighbor
Neighbor information collector.
Definition: neighbor.h:40
H4::Neighbor::n_neighbor_single
int n_neighbor_single
Definition: neighbor.h:52
H4::NBType::none
@ none
H4::NBAdr::NBAdr
NBAdr(Single *_adr, const int _index)
Definition: neighbor.h:24
H4::NBType::group
@ group
H4::NBAdr::NBAdr
NBAdr()
Definition: neighbor.h:22
H4::Neighbor::initial_step_flag
bool initial_step_flag
Definition: neighbor.h:50
H4::Neighbor::r_min_index
int r_min_index
Definition: neighbor.h:43