SlowDown Algorithmic Regularization (SDAR)
Algorithmic Regularization with slowdown method for integrating few-body motions
particle_group.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iomanip>
4 #include "Float.h"
5 #include "list.h"
6 
7 namespace COMM {
8 
10 
12  template <class Tparticle, class Tpcm>
13  class ParticleGroup: public List<Tparticle>{
14  private:
15  typedef List<Tparticle> TList;
16  bool origin_frame_flag;
17 
18  public:
19  Tpcm cm;
20 
22 
24  ParticleGroup(): TList(), origin_frame_flag(true) {};
25 
27 
29  void clear() {
30  TList::clear();
31  origin_frame_flag = true;
32  }
33 
35  /* Copy function will remove the local data and also copy the particle data or the link
36  */
37  ParticleGroup& operator = (const ParticleGroup& _particle_group) {
38  TList::clear();
39  *(List<Tparticle>*)this = *(List<Tparticle>*)&_particle_group;
40  origin_frame_flag = _particle_group.origin_frame_flag;
41  cm = _particle_group.cm;
42 
43  return *this;
44  }
45 
46 
49  clear();
50  }
51 
53 
55  int getBackupDataSize() const {
56  return 6*TList::num_;
57  }
58 
60 
64  for (int i=0; i<TList::num_; i++) {
65  const int k=6*i;
66  Float* pos = TList::data_[i].getPos();
67  Float* vel = TList::data_[i].getVel();
68  _bk[k ] = pos[0];
69  _bk[k+1] = pos[1];
70  _bk[k+2] = pos[2];
71  _bk[k+3] = vel[0];
72  _bk[k+4] = vel[1];
73  _bk[k+5] = vel[2];
74  }
75  return 6*TList::num_;
76  }
77 
79 
83  for (int i=0; i<TList::num_; i++) {
84  const int k=6*i;
85  Float* pos = TList::data_[i].getPos();
86  Float* vel = TList::data_[i].getVel();
87  pos[0] = _bk[k ];
88  pos[1] = _bk[k+1];
89  pos[2] = _bk[k+2];
90  vel[0] = _bk[k+3];
91  vel[1] = _bk[k+4];
92  vel[2] = _bk[k+5];
93  }
94  return 6*TList::num_;
95  }
96 
98 
102  void printColumnTitle(std::ostream & _fout, const int _width=20) {
103  _fout<<std::setw(_width)<<"N";
104  for (int i=0; i<TList::num_; i++) TList::data_[i].printColumnTitle(_fout, _width);
105  }
106 
108 
112  void printColumn(std::ostream & _fout, const int _width=20){
113  _fout<<std::setw(_width)<<TList::num_;
114  for (int i=0; i<TList::num_; i++) TList::data_[i].printColumn(_fout, _width);
115  }
116 
118 
121  void writeBinary(FILE* _fout) {
122  int num = TList::num_;
123  fwrite(&num,sizeof(int), 1, _fout);
124  for (int i=0; i<TList::num_; i++) TList::data_[i].writeBinary(_fout);
125  fwrite(&origin_frame_flag, sizeof(bool), 1, _fout);
126  cm.writeBinary(_fout);
127  }
128 
131  // @param [in] _fout: FILE IO for writing
132  //*/
133  //void writeAscii(FILE* _fout) const{
134  // fprintf(_fout, "%d ", TList::num_);
135  // for (int i=0; i<TList::num_; i++) TList::data_[i].writeAscii(_fout);
136  //}
137 
139 
142  void writeMemberAscii(std::ostream& _fout) const{
143  _fout<<TList::num_<<" ";
144  for (int i=0; i<TList::num_; i++) TList::data_[i].writeAscii(_fout);
145  }
146 
148 
152  void readBinary(FILE* _fin) {
153  ASSERT(TList::mode_==ListMode::local);
154  ASSERT(TList::num_==0);
155  ASSERT(TList::nmax_==0);
156  int n_new;
157  int rn = fread(&n_new, sizeof(int),1, _fin);
158  if(rn<1) {
159  std::cerr<<"Error: cannot read particle number!\n";
160  abort();
161  }
162  if(n_new<=0) {
163  std::cerr<<"Error: reading particle number "<<n_new<<"<=0!\n";
164  abort();
165  }
166  TList::reserveMem(n_new);
167  for (int i=0; i<n_new; i++) TList::data_[i].readBinary(_fin);
168  TList::num_ = n_new;
169  rn = fread(&origin_frame_flag, sizeof(bool), 1, _fin);
170  if(rn<1) {
171  std::cerr<<"Error: cannot read origin_frame_flag!\n";
172  abort();
173  }
174  cm.readBinary(_fin);
175  }
176 
178 
182  void readMemberAscii(std::istream& _fin) {
183  ASSERT(TList::mode_==ListMode::local);
184  ASSERT(TList::num_==0);
185  ASSERT(TList::nmax_==0);
186  int n_new;
187  _fin>>n_new;
188  ASSERT(!_fin.eof());
189  if(n_new<=0) {
190  std::cerr<<"Error: reading particle number "<<n_new<<"<=0!\n";
191  abort();
192  }
193  TList::reserveMem(n_new);
194  for (int i=0; i<n_new; i++) {
195  TList::data_[i].readAscii(_fin);
196  ASSERT(!_fin.eof());
197  }
198  TList::num_ = n_new;
199  }
200 
202 
206  if (origin_frame_flag) {
207  const Float *rc = cm.getPos();
208  const Float *vc = cm.getVel();
209  for (int i=0;i<TList::num_;i++) {
210  Float *ri = TList::data_[i].getPos();
211  Float *vi = TList::data_[i].getVel();
212  ri[0] -= rc[0];
213  ri[1] -= rc[1];
214  ri[2] -= rc[2];
215  vi[0] -= vc[0];
216  vi[1] -= vc[1];
217  vi[2] -= vc[2];
218  }
219  origin_frame_flag = false;
220  }
221  else {
222  std::cerr<<"Warning: particles are already in the center-of-mass frame!\n";
223  }
224  }
225 
227 
231  if (origin_frame_flag) {
232  std::cerr<<"Warning: particles are already in original frame!\n";
233  }
234  else {
235  const Float *rc = cm.getPos();
236  const Float *vc = cm.getVel();
237  for (int i=0;i<TList::num_;i++) {
238  Float *ri = TList::data_[i].getPos();
239  Float *vi = TList::data_[i].getVel();
240  ri[0] += rc[0];
241  ri[1] += rc[1];
242  ri[2] += rc[2];
243  vi[0] += vc[0];
244  vi[1] += vc[1];
245  vi[2] += vc[2];
246  }
247  origin_frame_flag = true;
248  }
249  }
250 
253  Float *rc = cm.getPos();
254  Float *vc = cm.getVel();
255  rc[0] = rc[1] = rc[2] = 0.0;
256  vc[0] = vc[1] = vc[2] = 0.0;
257  cm.mass = 0.0;
258  for (int i=0;i<TList::num_;i++) {
259  const Float *ri = TList::data_[i].getPos();
260  const Float *vi = TList::data_[i].getVel();
261  const Float mi = TList::data_[i].mass;
262  rc[0] += ri[0] * mi;
263  rc[1] += ri[1] * mi;
264  rc[2] += ri[2] * mi;
265 
266  vc[0] += vi[0] * mi;
267  vc[1] += vi[1] * mi;
268  vc[2] += vi[2] * mi;
269 
270  cm.mass += mi;
271  }
272  rc[0] /= cm.mass;
273  rc[1] /= cm.mass;
274  rc[2] /= cm.mass;
275  vc[0] /= cm.mass;
276  vc[1] /= cm.mass;
277  vc[2] /= cm.mass;
278  }
279 
281  bool isOriginFrame() const {
282  return origin_frame_flag;
283  }
284  };
285 
286 }
COMM::ParticleGroup::shiftToCenterOfMassFrame
void shiftToCenterOfMassFrame()
shift particle to their c.m. frame
Definition: particle_group.h:205
COMM::ParticleGroup::ParticleGroup
ParticleGroup()
center of mass particle for the group
Definition: particle_group.h:24
COMM
Definition: binary_tree.h:8
COMM::ParticleGroup::printColumnTitle
void printColumnTitle(std::ostream &_fout, const int _width=20)
print titles of class members using column style
Definition: particle_group.h:102
COMM::ParticleGroup::writeBinary
void writeBinary(FILE *_fout)
write particle data to files (notice original address is lost)
Definition: particle_group.h:121
COMM::List< Tparticle >::data_
Tparticle * data_
member array to store the data, or a link to existed member array (not allocated)
Definition: list.h:23
COMM::ParticleGroup::operator=
ParticleGroup & operator=(const ParticleGroup &_particle_group)
operator = is copy
Definition: particle_group.h:37
COMM::ParticleGroup::backupParticlePosVel
int backupParticlePosVel(Float *_bk)
Backup member particle position and velocity.
Definition: particle_group.h:63
Float.h
COMM::ParticleGroup::isOriginFrame
bool isOriginFrame() const
return true if the system is the in their origin frame
Definition: particle_group.h:281
COMM::List< Tparticle >::nmax_
int nmax_
maximum number of members allocated in memory
Definition: list.h:22
COMM::List< Tparticle >::clear
void clear()
Clear function.
Definition: list.h:76
COMM::ParticleGroup::clear
void clear()
Clear function.
Definition: particle_group.h:29
COMM::ParticleGroup::getBackupDataSize
int getBackupDataSize() const
get backup data size
Definition: particle_group.h:55
COMM::List< Tparticle >::mode_
ListMode mode_
mode indicator
Definition: list.h:25
COMM::ParticleGroup::readMemberAscii
void readMemberAscii(std::istream &_fin)
Read particle data from file.
Definition: particle_group.h:182
COMM::ParticleGroup::writeMemberAscii
void writeMemberAscii(std::ostream &_fout) const
write particle data to files (notice original address is lost)
Definition: particle_group.h:142
COMM::ParticleGroup::calcCenterOfMass
void calcCenterOfMass()
calculate center-of-mass
Definition: particle_group.h:252
COMM::ParticleGroup
Particle group class to store and manage a group of particle.
Definition: particle_group.h:13
COMM::ParticleGroup::cm
Tpcm cm
Definition: particle_group.h:19
COMM::ParticleGroup::shiftToOriginFrame
void shiftToOriginFrame()
shift particle to their original frame
Definition: particle_group.h:230
Float
double Float
Definition: Float.h:25
COMM::List< Tparticle >::num_
int num_
number of current members in the list
Definition: list.h:21
COMM::ParticleGroup::~ParticleGroup
~ParticleGroup()
destructor
Definition: particle_group.h:48
COMM::ParticleGroup::restoreParticlePosVel
int restoreParticlePosVel(Float *_bk)
restore member particle position and velocity
Definition: particle_group.h:82
COMM::List
list class to store and manage a group of member
Definition: list.h:19
COMM::ListMode::local
@ local
list.h
COMM::ParticleGroup::printColumn
void printColumn(std::ostream &_fout, const int _width=20)
print data of class members using column style
Definition: particle_group.h:112
COMM::List< Tparticle >::reserveMem
void reserveMem(const int _nmax)
Memory allocation for storing members.
Definition: list.h:48
COMM::ParticleGroup::readBinary
void readBinary(FILE *_fin)
Read particle data from file.
Definition: particle_group.h:152