PeTar
N-body code for collisional gravitational systems
changeover.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <iostream>
3 #include <iomanip>
4 #include "Common/Float.h"
5 
7 class ChangeOver{
8 private:
9  Float r_in_;
10  Float r_out_;
11  Float norm_;
12  Float coff_;
13  Float pot_off_;
14 #ifdef INTEGRATED_CUTOFF_FUNCTION
15  Float q_;
16 #endif
17 
18 public:
19  Float r_scale_next;
20 
21  ChangeOver(): r_in_(-1.0), r_out_(-1.0), norm_(0.0), coff_(0.0), pot_off_(0.0), r_scale_next(1.0) {}
22 
24 
26  bool checkParams() {
27  assert(r_in_>0.0);
28  assert(r_out_>0.0);
29  return true;
30  }
31 
33  template <class Tpars>
34  ChangeOver(const Tpars& _par) {
35  dataCopy(_par);
36  }
37 
39 
44  void setR(const Float& _m_fac, const Float& _r_in, const Float& _r_out) {
45 #ifdef FIX_CHANGEOVER
46  Float m_fac3 = 1.0;
47 #else
48  Float m_fac3 = std::max(std::pow(_m_fac,(1.0/3.0)),1.0);
49 #endif
50  r_in_ = m_fac3*_r_in;
51  r_out_ = m_fac3*_r_out;
52  norm_ = 1.0/(r_out_-r_in_);
53  coff_ = (r_out_-r_in_)/(r_out_+r_in_);
54  pot_off_ = (1.0+coff_)/r_out_;
55 #ifdef CHANGEOVER_DEBUG
56  assert(r_in_>0.0);
57  assert(r_out_>r_in_);
58 #endif
59  }
60 
62 
67  void setR(const Float& _r_in, const Float& _r_out) {
68  r_in_ = _r_in;
69  r_out_ = _r_out;
70  norm_ = 1.0/(_r_out-_r_in);
71  coff_ = (_r_out-_r_in)/(_r_out+_r_in);
72  pot_off_ = (1.0+coff_)/_r_out;
73 #ifdef CHANGEOVER_DEBUG
74  assert(_r_in>0.0);
75  assert(_r_out>_r_in);
76 #endif
77  }
80  setR(r_in_*r_scale_next, r_out_*r_scale_next);
81  r_scale_next = 1.0;
82  }
83 
85 
87  const Float &getRin() const {
88  return r_in_;
89  }
90 
92 
94  const Float &getRout() const {
95  return r_out_;
96  }
97 
98  void print(std::ostream & _fout) const{
99  _fout<<" r_in="<<r_in_
100  <<" r_out="<<r_out_;
101  }
102 
104 
108  static void printColumnTitle(std::ostream & _fout, const int _width=20) {
109  _fout<<std::setw(_width)<<"r_in"
110  <<std::setw(_width)<<"r_out";
111  }
112 
114 
119  static int printTitleWithMeaning(std::ostream & _fout, const int _counter=0, const int _offset=0) {
120  int counter = _counter;
121  counter++;
122  _fout<<std::setw(_offset)<<" "<<counter<<". r_in: inner changeover radius (0.0)\n";
123  counter++;
124  _fout<<std::setw(_offset)<<" "<<counter<<". r_out: outer changeover radius (0.0)\n";
125  return counter;
126  }
127 
129 
133  void printColumn(std::ostream & _fout, const int _width=20){
134  _fout<<std::setw(_width)<<r_in_
135  <<std::setw(_width)<<r_out_;
136  }
137 
139 
141  void writeBinary(FILE *_fp) const {
142  fwrite(this, sizeof(Float),2,_fp);
143  }
144 
146 
148  void readBinary(FILE *_fin) {
149  size_t rcount = fread(this, sizeof(Float),2,_fin);
150  if (rcount<1) {
151  std::cerr<<"Error: Data reading fails! requiring data number is 1, only obtain "<<rcount<<".\n";
152  abort();
153  }
154  setR(1.0, r_in_, r_out_);
155  }
156 
158 
160  void writeAscii(FILE *_fp) const {
161  fprintf(_fp, "%26.17e %26.17e ",
162  this->r_in_, this->r_out_);
163  }
164 
166 
168  void readAscii(FILE *_fin) {
169  PS::S64 rcount=fscanf(_fin, "%lf %lf ",
170  &this->r_in_, &this->r_out_);
171  if (rcount<2) {
172  std::cerr<<"Error: Data reading fails! requiring data number is 2, only obtain "<<rcount<<".\n";
173  abort();
174  }
175  setR(1.0, r_in_, r_out_);
176  }
177 
179 
182  template <class Tpars>
183  void dataCopy(const Tpars& _par) {
184  r_in_ = _par.r_in_ ;
185  r_out_ = _par.r_out_ ;
186  norm_ = _par.norm_ ;
187  coff_ = _par.coff_ ;
188  pot_off_= _par.pot_off_;
189  r_scale_next= _par.r_scale_next;
190 #ifdef INTEGRATED_CUTOFF_FUNCTION
191  q_ = _par.q_;
192 #endif
193  }
194 
195 
196 // Integrated potential changeover function
197 #ifdef INTEGRATED_CUTOFF_FUNCTION
198 
202  inline Float calcPotW(const Float& _dr) const {
203 #ifdef CHANGEOVER_DEBUG
204  assert(r_in_>0.0);
205  assert(r_out_>r_in_);
206 #endif
207  Float dr_rout = _dr/r_out_;
208  Float q = q_;
209  Float q2 = q*q;
210  Float q3 = q2*q;
211  Float q4 = q2*q2;
212  Float q5 = q3*q2;
213  Float q6 = q3*q3;
214  Float q7 = q4*q3;
215  Float denominator = (q-1.0)*(q-1.0)*(q-1.0)*(q-1.0)*(q-1.0)*(q-1.0)*(q-1.0);
216  Float A7 = 20.0/denominator/-6;
217  Float A6 = (-70.0*q - 70.0)/denominator/-5;
218  Float A5 = (84.0*q2 + 252.0*q + 84.0)/denominator/-4;
219  Float A4 = (-35.0*q3 - 315.0*q2 - 315.0*q - 35.0)/denominator/-3;
220  Float A3 = (140.0*q3 + 420.0*q2 + 140.0*q)/denominator/-2;
221  Float A2 = (-210*q3 - 210.0*q2)/denominator/-1;
222  Float A1 = (140*q3)/denominator*-1;
223  Float A0 = (-35.0*q4 + 21.0*q5 - 7.0*q6 + q7)/denominator;
224  Float x = 1.0; // x=rout/rout
225  Float B1 = 1.0 - ( (((((((A7*x + A6)*x + A5)*x + A4)*x + A3)*x + A2)*x + A1*log(x))*x) + A0 ); // to W(r>rout) = 1.0
226  Float A1_dash = -7*(60*q3*log(q) - q6 + 9.0*q5 - 45.0*q4 + 45.0*q2 - 9.0*q + 1.0)/(3.0*denominator);
227  if(dr_rout <= q) return 1.0 - A1_dash*dr_rout;
228  else if(dr_rout >= 1.0) return 0.0;
229  else return 1.0 - (((((((A7*dr_rout + A6)*dr_rout + A5)*dr_rout + A4)*dr_rout + A3)*dr_rout + A2)*dr_rout + A1*log(dr_rout) + B1)*dr_rout) - A0;
230  }
231 
233 
238  inline Float calcAcc0W(const Float& _dr) const {
239 #ifdef CHANGEOVER_DEBUG
240  assert(r_in_>0.0);
241  assert(r_out_>r_in_);
242 #endif
243  Float x = (_dr - r_in_)*norm_;
244  x = (x < 1.0) ? x : 1.0;
245  x = (x > 0.0) ? x : 0.0;
246  Float x2 = x*x;
247  Float x4 = x2*x2;
248  Float k = 1-(((-20.0*x+70.0)*x-84.0)*x+35.0)*x4;
249  return k;
250  }
251 
253 
261  inline Float calcAcc1W(const Float& _dr, const Float& _drdot) const {
262 #ifdef CHANGEOVER_DEBUG
263  assert(r_in_>0.0);
264  assert(r_out_>r_in_);
265 #endif
266  Float x = (_dr - r_in_)*norm_;
267  Float xdot = norm_*_drdot;
268  Float kdot = 0.0;
269  if(x <= 0.0)
270  kdot = 0.0;
271  else if(1.0 <= x)
272  kdot = 0.0;
273  else{
274  Float x2 = x*x;
275  Float x3 = x2*x;
276  Float x4 = x2*x2;
277  Float x5 = x4*x;
278  Float x6 = x4*x2;
279  kdot = -(-140.0*x6 + 420.0*x5 - 420.0*x4 + 140.0*x3) * xdot;
280  }
281  return kdot;
282  }
283 
284 #else
285  // changeover function start from potential
286 
288 
294  inline Float calcPotW(const Float& _dr) const {
295 #ifdef CHANGEOVER_DEBUG
296  assert(r_in_>0.0);
297  assert(r_out_>r_in_);
298 #endif
299  Float x = (_dr - r_in_)*norm_;
300  Float k = 1.0; //- pot_off_*_dr;
301  if(x >= 1.0 ) k = pot_off_*_dr; //0.0;
302  else if(x > 0.0) {
303  Float x2 = x*x;
304  Float x3 = x2*x;
305  Float x5 = x2*x3;
306  k -= coff_*x5*(5.0*x3 - 20.0*x2 + 28.0*x - 14.0);
307  }
308  return k;
309  }
310 
312 
318  inline Float calcAcc0W(const Float& _dr) const {
319 #ifdef CHANGEOVER_DEBUG
320  assert(r_in_>0.0);
321  assert(r_out_>r_in_);
322 #endif
323  Float x = (_dr - r_in_)*norm_;
324  x = (x < 1.0) ? x : 1.0;
325  x = (x > 0.0) ? x : 0.0;
326  Float x_1 = x - 1;
327  Float x_2 = x_1*x_1;
328  Float x_4 = x_2*x_2;
329  Float x2 = x*x;
330  Float x3 = x2*x;
331  Float x4 = x2*x2;
332  Float k = x_4*(1.0 + 4.0*x + 10.0*x2 + 20.0*x3 + 35.0*coff_*x4);
333  return k;
334  }
335 
337 
346  inline Float calcAcc1W(const Float& _dr, const Float& _drdot) const {
347 #ifdef CHANGEOVER_DEBUG
348  assert(r_in_>0.0);
349  assert(r_out_>r_in_);
350 #endif
351  Float x = (_dr - r_in_)*norm_;
352  Float xdot = norm_*_drdot;
353  Float kdot = 0.0;
354  if(x > 0.0 && x < 1.0) {
355  Float x3 = x*x*x;
356  Float x_1 = x - 1;
357  Float x_3 = x_1*x_1*x_1;
358  kdot = coff_*280.0*x3*(r_in_*norm_ + x)*x_3*xdot;
359  }
360  return kdot;
361  }
362 
363 #endif
364 
366  static Float calcPotWTwo(const ChangeOver& _ch1, const ChangeOver& _ch2, const Float& _dr) {
367  if (_ch1.getRout()> _ch2.getRout()) return _ch1.calcPotW(_dr);
368  else return _ch2.calcPotW(_dr);
369  }
370 
372  static Float calcAcc0WTwo(const ChangeOver& _ch1, const ChangeOver& _ch2, const Float& _dr) {
373  if (_ch1.getRout()> _ch2.getRout()) return _ch1.calcAcc0W(_dr);
374  else return _ch2.calcAcc0W(_dr);
375  }
376 
378  static Float calcAcc1WTwo(const ChangeOver& _ch1, const ChangeOver& _ch2, const Float& _dr, const Float& _drdot) {
379  if (_ch1.getRout()> _ch2.getRout()) return _ch1.calcAcc1W(_dr, _drdot);
380  else return _ch2.calcAcc1W(_dr, _drdot);
381  }
382 
383 };
384 
BinaryBase::kw1
int kw1
Definition: bse_test.cxx:15
ChangeOver::calcAcc0W
Float calcAcc0W(const Float &_dr) const
changeover function for force
Definition: changeover.hpp:318
BSEManager::mscale
double mscale
radius scaling factor from NB to Rsun
Definition: bse_interface.h:1061
BSEManager::printReference
static void printReference(std::ostream &fout, const int offset=4)
print reference to cite
Definition: bse_interface.h:1146
BSEManager::merge
void merge(StarParameter &_star1, StarParameter &_star2, StarParameterOut &_out1, StarParameterOut &_out2, double &_semi, double &_ecc)
merge two star using mix function, star 2 will becomes zero mass
Definition: bse_interface.h:1612
BSEManager::getBSEOutputFilenameSuffix
static std::string getBSEOutputFilenameSuffix()
Definition: bse_interface.h:1178
IOParamsBSE
IO parameters manager for BSE based code.
Definition: bse_interface.h:619
ChangeOver::r_scale_next
Float r_scale_next
(1 + coff_)/r_out = 2/(r_out+r_in)
Definition: changeover.hpp:19
BinaryBase::ecc0
double ecc0
Definition: bse_test.cxx:14
BinaryBase::semi
double semi
Definition: bse_test.cxx:14
galpy_pot_movie.fp
fp
Definition: galpy_pot_movie.py:131
BSEManager::evolveBinary
int evolveBinary(StarParameter &_star1, StarParameter &_star2, StarParameterOut &_out1, StarParameterOut &_out2, double &_semi, double &_period, double &_ecc, BinaryEvent &_bse_event, const int &_binary_init_type, const double _dt_nb)
call evolv2 for a binary
Definition: bse_interface.h:1450
BSEManager::printBinaryEventColumnOne
void printBinaryEventColumnOne(std::ostream &_fout, const BinaryEvent &_bin_event, const int k, const int _width=20, const bool print_type_name=true)
print binary event one in column
Definition: bse_interface.h:1384
ChangeOver::calcAcc1W
Float calcAcc1W(const Float &_dr, const Float &_drdot) const
changeover function for force derivative
Definition: changeover.hpp:346
ChangeOver::calcPotW
Float calcPotW(const Float &_dr) const
changeover function for potential
Definition: changeover.hpp:294
bse_interface.h
ChangeOver::printTitleWithMeaning
static int printTitleWithMeaning(std::ostream &_fout, const int _counter=0, const int _offset=0)
print column title with meaning (each line for one column)
Definition: changeover.hpp:119
galpy_pot_movie.dt
dt
Definition: galpy_pot_movie.py:134
StarParameter::printColumn
void printColumn(std::ostream &_fout, const int _width=20) const
print data of class members using column style
Definition: bse_interface.h:345
ChangeOver::updateWithRScale
void updateWithRScale()
update radius based on r_scale_next, reset r_scale_next to 1
Definition: changeover.hpp:79
StarParameterOut::print
void print(std::ostream &fout) const
for print in one line
Definition: bse_interface.h:439
ChangeOver::setR
void setR(const Float &_r_in, const Float &_r_out)
set r_in and r_out for changeover function
Definition: changeover.hpp:67
BinaryEvent
BSE based code event recorder class.
Definition: bse_interface.h:493
BinaryBase::star
StarParameter star[2]
Definition: bse_test.cxx:17
BSEManager::getBSEName
static std::string getBSEName()
Definition: bse_interface.h:1198
BinaryBase::bse_event
BinaryEvent bse_event
Definition: bse_test.cxx:19
ChangeOver::writeBinary
void writeBinary(FILE *_fp) const
write class data to file with binary format
Definition: changeover.hpp:141
PIKG::S64
int64_t S64
Definition: pikg_vector.hpp:23
ChangeOver::printColumnTitle
static void printColumnTitle(std::ostream &_fout, const int _width=20)
print titles of class members using column style
Definition: changeover.hpp:108
ChangeOver::dataCopy
void dataCopy(const Tpars &_par)
copy data from inherited class object
Definition: changeover.hpp:183
BinaryBase::ecc
double ecc
Definition: bse_test.cxx:14
BSEManager::printBinaryEventOne
void printBinaryEventOne(std::ostream &_fout, const BinaryEvent &_bin_event, const int k)
print binary event one
Definition: bse_interface.h:1373
BinaryBase::m1
double m1
Definition: bse_test.cxx:14
main
int main(int argc, char **argv)
Definition: bse_test.cxx:31
PIKG::min
T min(const Vector3< T > &v)
Definition: pikg_vector.hpp:150
BinaryBase::out
StarParameterOut out[2]
Definition: bse_test.cxx:18
BSEManager::getTime
double getTime(StarParameter &_star)
get evolved Time in NB unit
Definition: bse_interface.h:1342
BSEManager::checkParams
bool checkParams()
Definition: bse_interface.h:1090
ChangeOver::printColumn
void printColumn(std::ostream &_fout, const int _width=20)
print data of class members using column style
Definition: changeover.hpp:133
BSEManager::getDTMiss
double getDTMiss(StarParameterOut &_out)
get the difference of required finishing time and actually evolved time in NB unit
Definition: bse_interface.h:1347
StarParameterOut
SSE/BSE based code star parameter for output.
Definition: bse_interface.h:393
ChangeOver
Changeover function class.
Definition: changeover.hpp:7
BSEManager::initial
void initial(const IOParamsBSE &_input, const bool _print_flag=false)
initial SSE/BSE based code global parameters
Definition: bse_interface.h:1226
ChangeOver::writeAscii
void writeAscii(FILE *_fp) const
write class data to file with binary format
Definition: changeover.hpp:160
BinaryBase::tphys
double tphys
Definition: bse_test.cxx:16
ChangeOver::calcPotWTwo
static Float calcPotWTwo(const ChangeOver &_ch1, const ChangeOver &_ch2, const Float &_dr)
calculate changeover function Pot by selecting maximum rout
Definition: changeover.hpp:366
ChangeOver::calcAcc0WTwo
static Float calcAcc0WTwo(const ChangeOver &_ch1, const ChangeOver &_ch2, const Float &_dr)
calculate changeover function Acc0 by selecting maximum rout
Definition: changeover.hpp:372
StarParameter::initial
void initial(double _mass, int _kw=1, double _ospin=0.0, double _epoch=0.0)
Landmark luminosities
Definition: bse_interface.h:276
ChangeOver::readBinary
void readBinary(FILE *_fin)
read class data to file with binary format
Definition: changeover.hpp:148
WRITE_PRECISION
const int WRITE_PRECISION
Definition: bse_test.cxx:11
StarParameter::printColumnTitle
static void printColumnTitle(std::ostream &_fout, const int _width=20)
print titles of class members using column style
Definition: bse_interface.h:327
ChangeOver::checkParams
bool checkParams()
check whether parameters values are correct
Definition: changeover.hpp:26
ChangeOver::getRin
const Float & getRin() const
get r_in
Definition: changeover.hpp:87
BSEManager::evolveStar
int evolveStar(StarParameter &_star, StarParameterOut &_out, const double _dt, bool _unit_in_myr=false)
call SSE evolv1 for single star
Definition: bse_interface.h:1412
BSEManager::tscale
double tscale
metallicity parameters
Definition: bse_interface.h:1059
ChangeOver::getRout
const Float & getRout() const
get r_out
Definition: changeover.hpp:94
StarParameter::kw
long long int kw
Definition: bse_interface.h:258
WRITE_WIDTH
const int WRITE_WIDTH
Definition: bse_test.cxx:10
BSEManager
SSE/BSE interface manager.
Definition: bse_interface.h:1053
ChangeOver::calcAcc1WTwo
static Float calcAcc1WTwo(const ChangeOver &_ch1, const ChangeOver &_ch2, const Float &_dr, const Float &_drdot)
calculate changeover function Acc1 by selecting maximum rout
Definition: changeover.hpp:378
BinaryBase::readAscii
void readAscii(FILE *fp)
Definition: bse_test.cxx:21
PIKG::max
T max(const Vector3< T > &v)
Definition: pikg_vector.hpp:143
BinaryBase::m2
double m2
Definition: bse_test.cxx:14
BSEManager::getVelocityChange
double getVelocityChange(double *dv, StarParameterOut &_out)
get velocity change in NB unit
Definition: bse_interface.h:1399
ChangeOver::ChangeOver
ChangeOver()
scaling for changeover factor (for next step)
Definition: changeover.hpp:21
BinaryBase::period0
double period0
Definition: bse_test.cxx:14
ChangeOver::print
void print(std::ostream &_fout) const
Definition: changeover.hpp:98
ChangeOver::readAscii
void readAscii(FILE *_fin)
read class data to file with binary format
Definition: changeover.hpp:168
BSEManager::vscale
double vscale
mass scaling factor from NB to Msun
Definition: bse_interface.h:1062
StarParameter::tphys
double tphys
starting time of one evolution phase, age = tphys - epoch
Definition: bse_interface.h:266
BSEManager::getSSEOutputFilenameSuffix
static std::string getSSEOutputFilenameSuffix()
Definition: bse_interface.h:1168
ChangeOver::setR
void setR(const Float &_m_fac, const Float &_r_in, const Float &_r_out)
set r_in and r_out for changeover function
Definition: changeover.hpp:44
StarParameter::print
void print(std::ostream &fout) const
for print in one line
Definition: bse_interface.h:309
BSEManager::getTimeStepBinary
double getTimeStepBinary(StarParameter &_star1, StarParameter &_star2, double &_semi, double &_ecc, int &_binary_type)
call BSE evolv2 for a binary
Definition: bse_interface.h:1719
BinaryBase
Definition: bse_test.cxx:13
IOParamsBSE::read
int read(int argc, char *argv[], const int opt_used_pre=0)
reading parameters from GNU option API
Definition: bse_interface.h:754
BinaryBase::period
double period
Definition: bse_test.cxx:14
StarParameterOut::printColumnTitle
static void printColumnTitle(std::ostream &_fout, const int _width=20)
print titles of class members using column style
Definition: bse_interface.h:409
BSEManager::getTimeStepStar
double getTimeStepStar(StarParameter &_star)
get next time step to check in Myr
Definition: bse_interface.h:1689
BinaryBase::kw2
int kw2
Definition: bse_test.cxx:15
ChangeOver::ChangeOver
ChangeOver(const Tpars &_par)
constructor based on inherited class
Definition: changeover.hpp:34
StarParameter
SSE/BSE based code star parameter for saving.
Definition: bse_interface.h:257
IOParamsBSE::print_flag
bool print_flag
Definition: bse_interface.h:666