PeTar
N-body code for collisional gravitational systems
hard_assert.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cassert>
4 #include <string>
5 #include "hard_ptcl.hpp"
6 #include "Hermite/hermite_particle.h"
7 #include "soft_ptcl.hpp"
8 
9 // Hard debug dump for one cluster
10 class HardDump{
11 public:
12  typedef H4::ParticleH4<PtclHard> PtclH4;
18  PS::ReallocatableArray<PS::S32> n_member_in_group;
19  PS::ReallocatableArray<FPSoft> ptcl_arti_bk;
20  PS::ReallocatableArray<PtclH4> ptcl_bk;
22 
24 
26 
35  void backup(PtclH4 * _ptcl_local,
36  const PS::S32 _n_ptcl,
37  FPSoft* _ptcl_artifical,
38  const PS::S32 _n_group,
39  const PS::S32* _n_member_in_group,
40  const PS::F64 _time_offset,
41  const PS::F64 _time_end,
42  const PS::S32 _n_artificial) {
43  ptcl_bk.resizeNoInitialize(_n_ptcl);
44  for (int i=0; i<_n_ptcl; i++) ptcl_bk[i] = _ptcl_local[i];
45  time_offset = _time_offset;
46  time_end = _time_end;
47  n_ptcl =_n_ptcl;
48  n_member_in_group.resizeNoInitialize(_n_group);
49  for (int i=0; i<_n_group; i++) n_member_in_group[i] = _n_member_in_group[i];
50  if (_ptcl_artifical!=NULL) {
51  n_arti = _n_group*_n_artificial;
52  ptcl_arti_bk.resizeNoInitialize(n_arti);
53  for (int i=0; i<n_arti; i++) ptcl_arti_bk[i] = _ptcl_artifical[i];
54  }
55  else n_arti = 0;
56  n_group = _n_group;
57  backup_flag = true;
58  }
59 
61  /*
62  @param[in] _fname: file name to write
63  */
64  void dumpOneCluster(const char* _fname) {
65  std::FILE* fp = std::fopen(_fname,"w");
66  if (fp==NULL) {
67  std::cerr<<"Error: filename "<<_fname<<" cannot be open!\n";
68  abort();
69  }
70  fwrite(&time_offset, sizeof(PS::F64),1,fp);
71  fwrite(&time_end, sizeof(PS::F64),1,fp);
72  // hard particles
73  fwrite(&n_ptcl, sizeof(PS::S32), 1, fp);
74  for(int i=0; i<n_ptcl; i++) ptcl_bk[i].writeBinary(fp);
75  // static member
76  PS::F64 ptcl_st_dat[4];
77  ptcl_st_dat[0] = Ptcl::search_factor;
78  ptcl_st_dat[1] = Ptcl::r_search_min;
79  ptcl_st_dat[2] = Ptcl::mean_mass_inv;
80  ptcl_st_dat[3] = Ptcl::r_group_crit_ratio;
81  fwrite(ptcl_st_dat, sizeof(PS::F64),4, fp);
82  // artificial
83  fwrite(&n_arti, sizeof(PS::S32),1,fp);
84  fwrite(&n_group, sizeof(PS::S32), 1, fp);
85  fwrite(n_member_in_group.getPointer(), sizeof(PS::S32), n_group, fp);
86  for (int i=0; i<n_arti; i++) ptcl_arti_bk[i].writeBinary(fp);
87  fclose(fp);
88  backup_flag = false;
89  }
90 
92  /*
93  @param[in] _fname: file name to read
94  */
95  void readOneCluster(const char* _fname) {
96  std::FILE* fp = std::fopen(_fname,"r");
97  if (fp==NULL) {
98  std::cerr<<"Error: filename "<<_fname<<" cannot be open!\n";
99  abort();
100  }
101  // read time
102  size_t rcount = fread(&time_offset, sizeof(PS::F64),1,fp);
103  rcount += fread(&time_end, sizeof(PS::F64),1,fp);
104  if (rcount<2) {
105  std::cerr<<"Error: Data reading fails! requiring data number is 2, only obtain "<<rcount<<".\n";
106  abort();
107  }
108  // read hard particles
109  rcount = fread(&n_ptcl, sizeof(PS::S32),1, fp);
110  if (rcount<1) {
111  std::cerr<<"Error: Data reading fails! requiring data number is 1, only obtain "<<rcount<<".\n";
112  abort();
113  }
114  if (n_ptcl<=0) {
115  std::cerr<<"Error: particle number "<<n_ptcl<<" <=0 !\n";
116  abort();
117  }
118  ptcl_bk.resizeNoInitialize(n_ptcl);
119  for(int i=0; i<n_ptcl; i++) ptcl_bk[i].readBinary(fp);
120  // static members
121  PS::F64 ptcl_st_dat[4];
122  rcount = fread(ptcl_st_dat, sizeof(PS::F64),4, fp);
123  if (rcount<4) {
124  std::cerr<<"Error: Data reading fails! requiring data number is 3, only obtain "<<rcount<<".\n";
125  abort();
126  }
127  Ptcl::search_factor = ptcl_st_dat[0];
128  Ptcl::r_search_min = ptcl_st_dat[1];
129  Ptcl::mean_mass_inv = ptcl_st_dat[2];
130  Ptcl::r_group_crit_ratio = ptcl_st_dat[3];
131  // artifical particles
132  rcount = fread(&n_arti, sizeof(PS::S32),1,fp);
133  // number of groups
134  rcount += fread(&n_group, sizeof(PS::S32), 1, fp);
135  if (rcount<2) {
136  std::cerr<<"Error: Data reading fails! requiring data number is 2, only obtain "<<rcount<<".\n";
137  abort();
138  }
139  n_member_in_group.resizeNoInitialize(n_group);
140  rcount = fread(n_member_in_group.getPointer(), sizeof(PS::S32), n_group, fp);
141  if (rcount<(size_t)n_group) {
142  std::cerr<<"Error: Data reading fails! requiring data number is "<<n_group<<", only obtain "<<rcount<<".\n";
143  abort();
144  }
145  if (n_arti<0) {
146  std::cerr<<"Error: artificial particle number "<<n_arti<<" <0 !\n";
147  abort();
148  }
149  if (n_group<0) {
150  std::cerr<<"Error: group number "<<n_group<<" <0 !\n";
151  abort();
152  }
153  // read artifical particles
154  if (n_arti>0) {
155  ptcl_arti_bk.resizeNoInitialize(n_arti);
156  for (int i=0; i<n_arti; i++) ptcl_arti_bk[i].readBinary(fp);
157  }
158  fclose(fp);
159  }
160 
161 };
162 
163 // a list of hard dump for multi threads
165 public:
166  int size;
167  int mpi_rank;
170 
172 
173  void initial(const int _nthread, const int _rank=0) {
174  size = _nthread;
175  mpi_rank = _rank;
176  hard_dump = new HardDump[_nthread];
177  }
178 
179  void clear() {
180  size = 0;
181  if (hard_dump!=NULL) {
182  delete[] hard_dump;
183  hard_dump=NULL;
184  }
185  }
186 
188  clear();
189  }
190 
191  void dumpAll(const char *filename) {
192  std::string point(".");
193  std::string fname_prefix = filename + point + std::to_string(mpi_rank) + point;
194  for (int i=0; i<size; i++) {
195  std::time_t tnow = std::time(nullptr);
196  std::string fname = fname_prefix + std::to_string(i) + point + std::to_string(dump_number++) + point + std::to_string(tnow);
197  if (hard_dump[i].backup_flag) {
198  hard_dump[i].dumpOneCluster(fname.c_str());
199  std::cerr<<"Dump file: "<<fname.c_str()<<std::endl;
200  }
201  }
202  }
203 
204  void dumpThread(const char *filename){
205  const PS::S32 ith = PS::Comm::getThreadNum();
206  std::string point(".");
207  std::time_t tnow = std::time(nullptr);
208  //std::tm *local_time = localtime(&tnow);
209  std::string fname = filename + point + std::to_string(mpi_rank) + point + std::to_string(ith) + point + std::to_string(dump_number++) + point + std::to_string(tnow);
210  if (hard_dump[ith].backup_flag) {
211  hard_dump[ith].dumpOneCluster(fname.c_str());
212  std::cerr<<"Thread: "<<ith<<" Dump file: "<<fname.c_str()<<std::endl;
213  }
214  }
215 
216  HardDump& operator [] (const int i) {
217  assert(i<size);
218  return hard_dump[i];
219  }
220 };
221 
222 static HardDumpList hard_dump;
223 
224 #ifdef HARD_DUMP
225 #define DATADUMP(expr) hard_dump.dumpThread(expr)
226 #else
227 #define DATADUMP(expr)
228 #endif
229 
230 #ifdef HARD_DEBUG
231 #define ASSERT(expr) \
232  if(!(expr)) { \
233  std::cerr<<"Assertion! "<<__FILE__<<":"<<__LINE__<<": ("<<#expr<<") fail!"<<std::endl; \
234  DATADUMP("hard_dump"); \
235  abort(); \
236  }
237 #else
238 #define ASSERT(expr)
239 #endif
HardDumpList::dump_number
int dump_number
Definition: hard_assert.hpp:168
FPSoft
Definition: soft_ptcl.hpp:26
HardDump::ptcl_bk
PS::ReallocatableArray< PtclH4 > ptcl_bk
Definition: hard_assert.hpp:20
HardDump
Definition: hard_assert.hpp:10
PIKG::S32
int32_t S32
Definition: pikg_vector.hpp:24
HardDumpList::dumpThread
void dumpThread(const char *filename)
Definition: hard_assert.hpp:204
galpy_pot_movie.fp
fp
Definition: galpy_pot_movie.py:131
soft_ptcl.hpp
HardDumpList::dumpAll
void dumpAll(const char *filename)
Definition: hard_assert.hpp:191
Ptcl::mean_mass_inv
static PS::F64 mean_mass_inv
Definition: ptcl.hpp:45
HardDumpList
Definition: hard_assert.hpp:164
HardDumpList::~HardDumpList
~HardDumpList()
Definition: hard_assert.hpp:187
PIKG::F64
double F64
Definition: pikg_vector.hpp:17
HardDump::n_arti
PS::S32 n_arti
Definition: hard_assert.hpp:16
HardDump::dumpOneCluster
void dumpOneCluster(const char *_fname)
Dumping one cluster data for debuging.
Definition: hard_assert.hpp:64
HardDumpList::operator[]
HardDump & operator[](const int i)
Definition: hard_assert.hpp:216
HardDump::readOneCluster
void readOneCluster(const char *_fname)
reading one cluster data for debuging
Definition: hard_assert.hpp:95
Ptcl::r_search_min
static PS::F64 r_search_min
Definition: ptcl.hpp:43
HardDumpList::mpi_rank
int mpi_rank
Definition: hard_assert.hpp:167
HardDump::backup_flag
bool backup_flag
Definition: hard_assert.hpp:21
HardDumpList::initial
void initial(const int _nthread, const int _rank=0)
Definition: hard_assert.hpp:173
HardDumpList::hard_dump
HardDump * hard_dump
Definition: hard_assert.hpp:169
HardDumpList::size
int size
Definition: hard_assert.hpp:166
HardDump::backup
void backup(PtclH4 *_ptcl_local, const PS::S32 _n_ptcl, FPSoft *_ptcl_artifical, const PS::S32 _n_group, const PS::S32 *_n_member_in_group, const PS::F64 _time_offset, const PS::F64 _time_end, const PS::S32 _n_artificial)
backup one hard cluster data
Definition: hard_assert.hpp:35
HardDump::time_end
PS::F64 time_end
Definition: hard_assert.hpp:14
HardDumpList::clear
void clear()
Definition: hard_assert.hpp:179
HardDump::HardDump
HardDump()
Definition: hard_assert.hpp:23
HardDump::n_group
PS::S32 n_group
Definition: hard_assert.hpp:17
HardDump::PtclH4
H4::ParticleH4< PtclHard > PtclH4
Definition: hard_assert.hpp:12
HardDump::ptcl_arti_bk
PS::ReallocatableArray< FPSoft > ptcl_arti_bk
Definition: hard_assert.hpp:19
hard_ptcl.hpp
HardDumpList::HardDumpList
HardDumpList()
Definition: hard_assert.hpp:171
Ptcl::search_factor
static PS::F64 search_factor
Definition: ptcl.hpp:42
Ptcl::r_group_crit_ratio
static PS::F64 r_group_crit_ratio
Definition: ptcl.hpp:44
HardDump::n_member_in_group
PS::ReallocatableArray< PS::S32 > n_member_in_group
Definition: hard_assert.hpp:18
HardDump::time_offset
PS::F64 time_offset
Definition: hard_assert.hpp:13
HardDump::n_ptcl
PS::S32 n_ptcl
Definition: hard_assert.hpp:15