00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "space/keplerian_element_propagator.hpp"
00035 #include "data/list.hpp"
00036 #include "math/units.hpp"
00037
00038 #include <cmath>
00039
00040 using namespace gsgl;
00041 using namespace gsgl::data;
00042 using namespace gsgl::math;
00043
00044
00045 namespace periapsis
00046 {
00047
00048 namespace space
00049 {
00050
00051 BROKER_DEFINE_CREATOR(periapsis::space::keplerian_element_propagator);
00052
00053
00054 keplerian_element_propagator::keplerian_element_propagator(const config_record & obj_config)
00055 : propagator(obj_config), has_data(false), has_aux(false)
00056 {
00057
00058 for (int i = 0; i < 6; ++i)
00059 {
00060 elements[i] = 0;
00061 rates[i] = 0;
00062 }
00063
00064
00065 bool has_elements = get_array(obj_config[L"keplerian_elements"], elements, 6);
00066 bool has_rates = get_array(obj_config[L"keplerian_rates"], rates, 6);
00067
00068 if ((has_data = has_elements && has_rates))
00069 {
00070 for (int i = 2; i < 6; ++i)
00071 {
00072 elements[i] *= math::DEG2RAD;
00073 rates[i] *= math::DEG2RAD;
00074 }
00075 }
00076
00077 if ((has_aux = get_array(obj_config[L"keplerian_elements_aux"], aux, 4)))
00078 {
00079 for (int i = 0; i < 4; ++i)
00080 aux[i] *= math::DEG2RAD;
00081 }
00082 }
00083
00084
00085 keplerian_element_propagator::~keplerian_element_propagator()
00086 {
00087 }
00088
00089
00090 static double calc_E(const double M, const double e_star, const double e)
00091 {
00092 double E_zero = M + e_star * ::sin(M);
00093
00094 double delta_M;
00095 double delta_E;
00096 double E_old;
00097 double E_new = E_zero;
00098 double tol = 1.0e-6;
00099
00100 do
00101 {
00102 E_old = E_new;
00103 delta_M = M - (E_old - e_star * ::sin(E_old));
00104 delta_E = delta_M / (1.0 - e * ::cos(E_old));
00105 E_new = E_old + delta_E;
00106 }
00107 while (delta_E > tol);
00108
00109 return E_new;
00110 }
00111
00112
00113 void keplerian_element_propagator::update(const double jdn, vector & position, vector & velocity)
00114 {
00115 if (!has_data)
00116 return;
00117
00118
00119 double cur_elements[6];
00120 double T = (jdn - 2451545.0) / 36525.0;
00121
00122 for (int i = 0; i < 6; ++i)
00123 cur_elements[i] = elements[i] + rates[i] * T;
00124
00125
00126 double omega = cur_elements[4] - cur_elements[5];
00127 double M = cur_elements[3] - cur_elements[4];
00128
00129 if (has_aux)
00130 M += aux[0]*T*T + aux[1]*::cos(aux[3]*T) + aux[2]*::sin(aux[3]*T);
00131
00132 double M_plus = M + math::PI;
00133
00134 while (M_plus < 0.0)
00135 M_plus += math::PI * 2.0;
00136 while (M_plus > math::PI * 2.0)
00137 M_plus -= math::PI * 2.0;
00138
00139 M = M_plus - math::PI;
00140
00141
00142 double n = (cur_elements[3] - elements[3]) / T;
00143
00144
00145 double E = calc_E(M, cur_elements[1] * math::DEG2RAD, cur_elements[1]);
00146
00147
00148 double E_dot = n / (1.0f - cur_elements[1] * ::cos(E));
00149
00150
00151 double x_prime = cur_elements[0] * (::cos(E) - cur_elements[1]);
00152 double y_prime = cur_elements[0] * (::sqrt(1.0f - cur_elements[1]*cur_elements[1]) * ::sin(E));
00153 double z_prime = 0.0f;
00154
00155
00156 double x_dot_prime = cur_elements[0] * ( -::sin(E) * E_dot );
00157 double y_dot_prime = cur_elements[0] * (::sqrt(1.0f - cur_elements[1]*cur_elements[1]) * ::cos(E) * E_dot);
00158
00159
00160 double cos_o = ::cos(omega);
00161 double cos_O = ::cos(cur_elements[5]);
00162 double cos_I = ::cos(cur_elements[2]);
00163 double sin_o = ::sin(omega);
00164 double sin_O = ::sin(cur_elements[5]);
00165 double sin_I = ::sin(cur_elements[2]);
00166
00167 double x = (cos_o*cos_O - sin_o*sin_O*cos_I) * x_prime + (-sin_o*cos_O - cos_o*sin_O*cos_I) * y_prime;
00168 double y = (cos_o*sin_O + sin_o*cos_O*cos_I) * x_prime + (-sin_o*sin_O + cos_o*cos_O*cos_I) * y_prime;
00169 double z = (sin_o*sin_I)*x_prime + (cos_o*sin_I)*y_prime;
00170
00171
00172 double x_dot = (cos_o*cos_O - sin_o*sin_O*cos_I) * x_dot_prime + (-sin_o*cos_O - cos_o*sin_O*cos_I) * y_dot_prime;
00173 double y_dot = (cos_o*sin_O + sin_o*cos_O*cos_I) * x_dot_prime + (-sin_o*sin_O + cos_o*cos_O*cos_I) * y_dot_prime;
00174 double z_dot = (sin_o*sin_I)*x_dot_prime + (cos_o*sin_I)*y_dot_prime;
00175
00176
00177 x_dot /= units::SECONDS_PER_YEAR * 100.0;
00178 y_dot /= units::SECONDS_PER_YEAR * 100.0;
00179 z_dot /= units::SECONDS_PER_YEAR * 100.0;
00180
00181
00182 position.get_x() = static_cast<gsgl::real_t>(x);
00183 position.get_y() = static_cast<gsgl::real_t>(y);
00184 position.get_z() = static_cast<gsgl::real_t>(z);
00185 position.get_w() = 1;
00186
00187 velocity.get_x() = static_cast<gsgl::real_t>(x_dot);
00188 velocity.get_y() = static_cast<gsgl::real_t>(y_dot);
00189 velocity.get_z() = static_cast<gsgl::real_t>(z_dot);
00190 velocity.get_w() = 1;
00191 }
00192
00193
00194 bool keplerian_element_propagator::get_array(const string & str, double *a, const int num)
00195 {
00196
00197 list<string> tokens = str.split(L" \t");
00198
00199 int index = 0;
00200 for (list<string>::iterator i = tokens.iter(); i.is_valid(); ++i)
00201 {
00202 if (!i->is_empty())
00203 {
00204 if (index < num)
00205 {
00206 a[index++] = i->to_double();
00207 }
00208 else
00209 throw runtime_exception(L"Invalid keplerian elements %ls", str.w_string());
00210 }
00211 }
00212
00213 return index == num;
00214 }
00215
00216 }
00217
00218 }