PaGMO  1.1.5
time2distance.cpp
1 /*****************************************************************************
2  * Copyright (C) 2004-2015 The PaGMO development team, *
3  * Advanced Concepts Team (ACT), European Space Agency (ESA) *
4  * *
5  * https://github.com/esa/pagmo *
6  * *
7  * act@esa.int *
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17  * GNU General Public License for more details. *
18  * *
19  * You should have received a copy of the GNU General Public License *
20  * along with this program; if not, write to the *
21  * Free Software Foundation, Inc., *
22  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
23  *****************************************************************************/
24 
25 /*
26 %Inputs:
27 % r0: column vector for the position (mu=1)
28 % v0: column vector for the velocity (mu=1)
29 % rtarget: distance to be reached
30 %
31 %Outputs:
32 % t: time taken to reach a given distance
33 %
34 %Comments: everything works in non dimensional units
35 */
36 
37 #include <cmath>
38 
39 #include "Astro_Functions.h"
40 #include "propagateKEP.h"
41 #include "time2distance.h"
42 
43 using namespace std;
44 
45 double time2distance(const double *r0, const double *v0, const double &rtarget)
46 {
47  double temp = 0.0;
48  double E[6];
49  double r0norm = norm2(r0);
50  double a, e, E0, p, ni, Et;
51  int i;
52 
53  if (r0norm < rtarget)
54  {
55  for (i=0; i<3; i++)
56  temp += r0[i]*v0[i];
57 
58  IC2par(r0,v0,1,E);
59  a = E[0];
60  e = E[1];
61  E0 = E[5];
62  p = a * (1-e*e);
63  // If the solution is an ellipse
64  if (e<1)
65  {
66  double ra = a * (1+e);
67  if (rtarget>ra)
68  return -1; // NaN;
69  else // we find the anomaly where the target distance is reached
70  {
71  ni = acos((p/rtarget-1)/e); //in 0-pi
72  Et = 2*atan(sqrt((1-e)/(1+e))*tan(ni/2)); // algebraic kepler's equation
73 
74  if (temp>0)
75  return sqrt(pow(a,3))*(Et-e*sin(Et)-E0 + e*sin(E0));
76  else
77  {
78  E0 = -E0;
79  return sqrt(pow(a,3))*(Et-e*sin(Et)+E0 - e*sin(E0));
80  }
81  }
82  }
83  else // the solution is a hyperbolae
84  {
85  ni = acos((p/rtarget-1)/e); // in 0-pi
86  Et = 2*atan(sqrt((e-1)/(e+1))*tan(ni/2)); // algebraic equivalent of kepler's equation in terms of the Gudermannian
87 
88  if (temp>0) // out==1
89  return sqrt(pow((-a),3))*(e*tan(Et)-log(tan(Et/2+M_PI/4))-e*tan(E0)+log(tan(E0/2+M_PI/4)));
90  else
91  {
92  E0 = -E0;
93  return sqrt(pow((-a),3))*(e*tan(Et)-log(tan(Et/2+M_PI/4))+e*tan(E0)-log(tan(E0/2+M_PI/4)));
94  }
95  }
96  }
97  else
98  return 12;
99 }
STL namespace.