Planetary Orbital Evolution due to Tides
Orbital evolution of two objects experiencing tides
Common.h
Go to the documentation of this file.
1 
8 #ifndef __COMMON_DEFINITIONS_H
9 #define __COMMON_DEFINITIONS_H
10 
11 #include <list>
12 #include <valarray>
13 #include <limits>
14 #include <sstream>
15 #include <iostream>
16 #include <iterator>
17 #include <gsl/gsl_vector.h>
18 #include <gsl/gsl_matrix.h>
19 #include <gsl/gsl_linalg.h>
20 #include <gsl/gsl_blas.h>
21 #include "gsl/gsl_poly.h"
22 #include <cassert>
23 
24 #include "../Core/SharedLibraryExportMacros.h"
25 #include "Error.h"
26 #include "AstronomicalConstants.h"
27 #include "IncludeEigen.h"
28 
29 
30 namespace Core {
31 
33  const double NaN = std::numeric_limits<double>::quiet_NaN();
34 
36  const double Inf = std::numeric_limits<double>::infinity();
37 
42  enum EvolModeType {
46 
49 
52 
56  };
57 
59  template<typename ITERATOR_TYPE>
60  std::valarray<double> valarray_from_iterator(ITERATOR_TYPE values,
61  unsigned size)
62  {
63  std::valarray<double> result(size);
64  double *dest=&result[0];
65  for(; size>0; --size) {
66  *dest = *values;
67  ++dest;
68  ++values;
69  }
70  return result;
71  }
72 
74  LIB_LOCAL inline std::valarray<double> list_to_valarray(
75  const std::list<double> &inlist
76  ) {return valarray_from_iterator(inlist.begin(), inlist.size());}
77 
79  LIB_LOCAL std::valarray<double> solve_cubic(double c0,
80  double c1,
81  double c2,
82  double c3);
83 
95  LIB_LOCAL double estimate_zerocrossing(
98  double x0,
99 
102  double y0,
103 
106  double x1,
107 
110  double y1,
111 
114  double dy0=NaN,
115 
118  double dy1=NaN);
119 
125  LIB_LOCAL double quadratic_zerocrossing(double x0, double y0,
126  double x1, double y1,
127  double x2, double y2,
128  double require_range_low = NaN,
129  double require_range_high = NaN);
130 
135  LIB_LOCAL double cubic_zerocrossing(double x0, double y0,
136  double x1, double y1,
137  double x2, double y2,
138  double x3, double y3,
139  double require_range_low = NaN,
140  double require_range_high = NaN);
141 
142 
151  LIB_LOCAL double estimate_extremum(
154  double x0,
155 
158  double y0,
159 
162  double x1,
163 
166  double y1,
167 
169  double dy0,
170 
172  double dy1,
173 
176  double *extremum_y = NULL);
177 
178 
186  LIB_LOCAL double quadratic_extremum(double x0, double y0,
187  double x1, double y1,
188  double x2, double y2,
189  double *extremum_y = NULL);
190 
201  LIB_LOCAL double cubic_extremum(double x0, double y0,
202  double x1, double y1,
203  double x2, double y2,
204  double x3, double y3,
205  double *extremum_y = NULL,
206  double require_range_low = NaN,
207  double require_range_high = NaN);
208 
219  LIB_LOCAL gsl_vector *cubic_coefficients(double x0, double y0,
220  double x1, double y1,
221  double x2, double y2,
222  double x3, double y3);
223 
232  template<class ITERATOR>
233  gsl_vector *polynomial_coefficients(ITERATOR x_i,
234  ITERATOR y_i,
235  size_t num_points)
236  {
237  Eigen::VectorXd y_vec(num_points);
238  Eigen::MatrixXd xpowers(num_points, num_points);
239  for(size_t i=0; i<num_points; i++) {
240  double x=*x_i, xpow=1.0;
241  y_vec(i)=*y_i;
242  for(size_t pow=0; pow<num_points; pow++) {
243  xpowers(i,pow)=xpow;
244  xpow*=x;
245  }
246  x_i++; y_i++;
247  }
248  static Eigen::JacobiSVD<Eigen::MatrixXd,
249  Eigen::FullPivHouseholderQRPreconditioner>
250  svd(num_points, num_points,
251  Eigen::ComputeFullU | Eigen::ComputeFullV);
252  svd.compute(xpowers);
253  Eigen::VectorXd solution=svd.solve(y_vec);
254  gsl_vector *result=gsl_vector_alloc(num_points);
255  for(size_t i=0; i<num_points; ++i)
256  gsl_vector_set(result, i, solution(i));
257  return result;
258 
259  /* gsl_vector *y_vec=gsl_vector_alloc(num_points);
260  gsl_matrix *xpowers=gsl_matrix_alloc(num_points, num_points);
261  for(size_t i=0; i<num_points; i++) {
262  double x=*x_i, xpow=1.0;
263  gsl_vector_set(y_vec, i, *y_i);
264  for(size_t pow=0; pow<num_points; pow++) {
265  gsl_matrix_set(xpowers, i, pow, xpow);
266  xpow*=x;
267  }
268  x_i++; y_i++;
269  }
270  gsl_matrix *xpowers_LU=gsl_matrix_alloc(num_points, num_points);
271  gsl_matrix_memcpy(xpowers_LU, xpowers);
272 
273  gsl_permutation *permutation=gsl_permutation_alloc(num_points);
274  gsl_vector *coefficients=gsl_vector_alloc(num_points);
275  int permutation_sign;
276  gsl_linalg_LU_decomp(xpowers_LU, permutation, &permutation_sign);
277  gsl_linalg_LU_solve(xpowers_LU, permutation, y_vec, coefficients);
278  gsl_vector *residuals=gsl_vector_alloc(num_points);
279  gsl_linalg_LU_refine(xpowers, xpowers_LU, permutation, y_vec,
280  coefficients, residuals);
281  gsl_permutation_free(permutation);
282  gsl_vector_free(y_vec);
283  gsl_vector_free(residuals);
284  gsl_matrix_free(xpowers);
285  gsl_matrix_free(xpowers_LU);
286  return coefficients;*/
287  }
288 
290  LIB_LOCAL std::ostream &operator<<(std::ostream &os,
291  const Core::EvolModeType &evol_mode);
292 
293 } //End Core namespace.
294 
295 namespace std {
297  LIB_LOCAL std::ostream &operator<<(std::ostream &os,
298  const std::valarray<double> &arr);
299 
301  LIB_LOCAL std::ostream &operator<<(std::ostream &os,
302  const std::list<double> &l);
303 }
304 
305 #endif
Defines various astronomical constants.
std::valarray< double > valarray_from_iterator(ITERATOR_TYPE values, unsigned size)
Creates a valarray from an iterator.
Definition: Common.h:60
const double Inf
Infinity.
Definition: Common.h:36
LIB_LOCAL std::valarray< double > list_to_valarray(const std::list< double > &inlist)
Creates a valarray containing the values in the given list.
Definition: Common.h:74
gsl_vector * cubic_coefficients(double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3)
Returns the polynomial coefficients of the cubic going through four points.
Definition: Common.cpp:493
double estimate_zerocrossing(double x0, double y0, double x1, double y1, double dy0, double dy1)
Finds a zero of a smooth curve defined by the given points and derivatives.
Definition: Common.cpp:68
Definition: Common.cpp:10
std::valarray< double > solve_cubic(double c0, double c1, double c2, double c3)
Solves the cubic equation .
Definition: Common.cpp:49
double cubic_zerocrossing(double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3, double require_range_low, double require_range_high)
Finds the abscissa of a zero of a cubic defined by four points.
Definition: Common.cpp:189
Defines the exception hierarchy used by this code.
double estimate_extremum(double x0, double y0, double x1, double y1, double dy0, double dy1, double *extremum_y)
Finds an extremum of a cubic defined by two points and derivatives.
Definition: Common.cpp:251
The surface rotation of one of the bodies is locked to a prescribed value.
Definition: Common.h:45
gsl_vector * polynomial_coefficients(ITERATOR x_i, ITERATOR y_i, size_t num_points)
Finds the polynomial that goes through the points iterated over by x_i and y_i.
Definition: Common.h:233
There is only one body in the system (only its rotation evolves).
Definition: Common.h:51
double quadratic_extremum(double x0, double y0, double x1, double y1, double x2, double y2, double *extremum_y)
Finds the abscissa of the extremum of the quadratic function passing through three points...
Definition: Common.cpp:321
double cubic_extremum(double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3, double *extremum_y, double require_range_low, double require_range_high)
Finds the abscissa of an extremum of the cubic function passing through four points.
Definition: Common.cpp:354
EvolModeType
The various evolution modes.
Definition: Common.h:42
Two bodies orbiting each other.
Definition: Common.h:48
double quadratic_zerocrossing(double x0, double y0, double x1, double y1, double x2, double y2, double require_range_low, double require_range_high)
Finds the abscissa of a zero of a quadratic defined by three points.
Definition: Common.cpp:138
std::ostream & operator<<(std::ostream &os, const std::valarray< std::valarray< double > > &poly_coef)
Create a string with a description of the given stop info.
Definition: Common.cpp:71