Planetary Orbital Evolution due to Tides
Orbital evolution of two objects experiencing tides
Common.cpp
1 #include "Common.h"
2 
3 bool check_diff(double x, double y, double frac_tolerance,
4  double abs_tolerance)
5 {
6  if(std::isnan(x)) return std::isnan(y);
7  return std::abs(x-y)<=(abs_tolerance+
8  frac_tolerance*std::min(std::abs(x), std::abs(y)));
9 }
10 
11 bool check_diff(std::valarray<double> x, std::valarray<double> y,
12  std::valarray<double> frac_tolerance,
13  std::valarray<double> abs_tolerance)
14 {
15  bool result=true;
16  for(size_t i=0; i<x.size(); i++)
17  result=result && (std::abs(x[i]-y[i])<=
18  (abs_tolerance[i]+
19  frac_tolerance[i]*std::min(std::abs(x[i]), std::abs(y[i]))));
20  return result;
21 }
22 
23 bool isEqual(double a, double b) {
24  return std::abs(a-b) < 1e-20;
25 }
26 
27 double getError(double predicted, double actual) {
28  return (std::abs(actual - predicted)
29  /
30  std::max(std::abs(actual), std::abs(predicted)));
31 }
32 
33 bool approxEqual(double predicted, double actual, double thres)
34 {
35  if (isEqual(predicted, actual)) return true;
36  return getError(predicted, actual) < thres;
37 }
38 
39 double orbital_angmom_from_freq(double m1, double m2, double freq, double e)
40 {
41  using namespace Core;
42  return m1*m2*AstroConst::day
43  /std::pow(AstroConst::solar_radius, 2)
44  *std::pow(std::pow(AstroConst::G*AstroConst::solar_mass, 2)
45  *AstroConst::day/((m1+m2)*freq), 1.0/3.0)
46  *std::sqrt(1.0-e*e);
47 }
48 
49 double uniform_rand(double min, double max)
50 {
51  return (max-min)*static_cast<double>(std::rand())/RAND_MAX+min;
52 }
53 
54 /*std::string stop_info_to_str(const Evolve::StopInformation &stop)
55 {
56  std::ostringstream result;
57  result << stop << std::endl;
58  return result.str();
59 }*/
60 
61 double rand_value(double min, double max)
62 {
63  return (max-min)*rand()/RAND_MAX+min;
64 }
65 
66 int rand_value(int min, int max)
67 {
68  return rand()%(max-min+1)+min;
69 }
70 
71 std::ostream &operator<<(
72  std::ostream &os,
73  const std::valarray< std::valarray<double> > &poly_coef
74 )
75 {
76  for(size_t age_i=0; age_i<poly_coef.size(); age_i++) {
77  for(size_t mass_i=0; mass_i<poly_coef[age_i].size(); mass_i++) {
78  os << poly_coef[age_i][mass_i];
79  if(age_i) os << "*age";
80  if(age_i>1) os << "**" << age_i;
81  if(mass_i) os << "*m";
82  if(mass_i>1) os << "**" << mass_i;
83  if(age_i<poly_coef.size()-1 || mass_i<poly_coef.size()-1)
84  os << " + ";
85  }
86  }
87  return os;
88 }
89 
90 void rand_poly_coef(std::valarray< std::valarray<double> > &poly_coef,
91  double max_mass)
92 {
93  if (max_mass < 0) max_mass = MAX_STELLAR_MASS;
94  poly_coef.resize(3, std::valarray<double>(3));
95  double age_fac=1;
96  for (unsigned age_i=0; age_i < poly_coef.size(); age_i++) {
97  if (age_i > 1) age_fac *= age_i * MAX_AGE;
98  double mass_fac = 1;
99  for(unsigned m_i = 0; m_i < poly_coef[age_i].size(); ++m_i) {
100  if(m_i>1) mass_fac *= m_i * max_mass;
101  poly_coef[age_i][m_i] = (static_cast<double>(rand()) / RAND_MAX
102  /
103  mass_fac / age_fac);
104  }
105  }
106 }
107 
108 std::valarray< std::valarray<double> > rand_poly_coef(double max_mass)
109 {
110  std::valarray< std::valarray<double> > poly_coef(std::valarray<double>(3), 3);
111  rand_poly_coef(poly_coef, max_mass);
112  return poly_coef;
113 }
114 
115 std::valarray< std::valarray<double> > offset_age(
116  const std::valarray< std::valarray<double> > &poly_coef,
117  double age_offset)
118 {
119  std::valarray< std::valarray<double> > result(
120  std::valarray<double>(poly_coef[0].size()), poly_coef.size());
121  for(size_t i=0; i<poly_coef.size(); i++)
122  for(size_t j=0; j<poly_coef[0].size(); j++) {
123  double c_ij=poly_coef[i][j], offset_k=1.0;
124  size_t binom_coef=1;
125  for(size_t k=0; k<=i; k++) {
126  result[i-k][j]+=c_ij*offset_k*binom_coef;
127  offset_k*=age_offset;
128  if(k<i) binom_coef = next_binom_coef(i, k, binom_coef);
129  }
130  }
131  return result;
132 }
133 
134 unsigned next_binom_coef(unsigned n, unsigned m, unsigned nCm)
135 {
136  assert(n>=m+1);
137  return (nCm*(n-m))/(m+1);
138 }
139 
140 double lag_from_lgQ(double lgQ)
141 {
142  return 15.0 / (16.0 * M_PI * std::pow(10.0, lgQ));
143 }
144 
145 double lag_from_lgQ(double lgQ, double mass_ratio)
146 {
147  return (
148  15.0
149  /
150  (8.0 * M_PI * std::sqrt(1.0 + mass_ratio) * std::pow(10.0, lgQ))
151  );
152 }
bool approxEqual(double predicted, double actual, double thres=0.02)
Definition: Common.cpp:33
double getError(double predicted, double actual)
Definition: Common.cpp:27
Functions and classes of general use for all unit tests.
double rand_value(double min, double max)
A uniform random real value in the given range.
Definition: Common.cpp:61
const double MAX_AGE
Most tests end at this age in Gyr.
Definition: Common.h:57
double lag_from_lgQ(double lgQ)
Converts lg(Q) to a tidal phase lag.
Definition: CInterface.cpp:17
const double solar_radius
Radius of the sun [m].
void rand_poly_coef(std::valarray< std::valarray< double > > &poly_coef, double max_mass=-1)
Fills the given valarray with a random set of polynomial coefficients.
Definition: Common.cpp:90
double orbital_angmom_from_freq(double m1, double m2, double freq, double e)
The orbital angular momentum corresponding to the given frequency.
Definition: Common.cpp:39
bool check_diff(double x, double y, double frac_tolerance, double abs_tolerance)
Returns true iff .
Definition: Common.cpp:3
double uniform_rand(double min, double max)
Generates a uniformly distributed random number.
Definition: Common.cpp:49
bool isEqual(double a, double b)
Definition: Common.cpp:23
const double day
day [s]
const double solar_mass
Mass of the sun [kg].
const double MAX_STELLAR_MASS
The highest stellar mass to use in tests in .
Definition: Common.h:51
unsigned next_binom_coef(unsigned n, unsigned m, unsigned nCm)
Given n, m and (n)C(m) returns (n)C(m+1)
Definition: Common.cpp:134
std::valarray< std::valarray< double > > offset_age(const std::valarray< std::valarray< double > > &poly_coef, double age_offset)
Returns new polynomial coefficienst such that output polynomial(mass, age+age_offset)=input polynomia...
Definition: Common.cpp:115
const double G
Gravitational constant in SI.