00001
00002
00008
00009
00011 #include "Matrix.h"
00012 #include "Rotation.h"
00013 #include "Attitude.h"
00014 #include "Orbit.h"
00015 #include "CombinedNumericPropagator.h"
00016 #include "RungeKuttaIntegrator.h"
00017 #include "orbitmodels/TwoBodyDynamics.h"
00018 #include "EarthCentralBody.h"
00019 #include "OrbitState.h"
00020 #include "AttitudeState.h"
00021 #include "orbitstaterep/PositionVelocity.h"
00022 #include "orbitframes/OrbitFrameIJK.h"
00023 #include "Plot.h"
00024 using namespace O_SESSAME;
00025
00026 NumericPropagator* SetupPropagator();
00027 Environment* SetupEnvironment();
00028 Orbit* SetupOrbit();
00029 Attitude* SetupAttitude();
00030 void myOrbitStateConvFunc(const Matrix &_meshPoint, OrbitState &_convertedOrbitState);
00031 void myAttitudeStateConvFunc(const Matrix &_meshPoint, AttitudeState &_convertedAttitudeState);
00032
00033 int main()
00034 {
00035 cout << "Calling SetupOrbit()" << endl;
00036 Orbit* myOrbit = SetupOrbit();
00037 cout << "Calling SetupAttitude()" << endl;
00038 Attitude* myAttitude = SetupAttitude();
00039
00040
00041 cout << "Calling SetupPropagator()" << endl;
00042 NumericPropagator* myPropagator = SetupPropagator();
00043 myOrbit->SetPropagator(myPropagator);
00044 myAttitude->SetPropagator(myPropagator);
00045
00046
00047 cout << "Calling SetupEnvironment()" << endl;
00048 Environment* myEnvironment = SetupEnvironment();
00049 myOrbit->SetEnvironment(myEnvironment);
00050 myAttitude->SetEnvironment(myEnvironment);
00051
00052
00053 double numOrbits = 1/10.0;
00054 cout << "Number of Orbits: " << flush;
00055
00056 vector<ssfTime> integrationTimes;
00057 ssfTime begin(0);
00058 ssfTime end(begin + 92*60*numOrbits);
00059 integrationTimes.push_back(begin);
00060 integrationTimes.push_back(end);
00061 cout << "PropTime = " << begin.GetSeconds() << " s -> " << end.GetSeconds() << " s" << endl;
00062 cout << "Orbit State: " << ~myOrbit->GetStateObject().GetState() << endl;
00063 cout << "Attitude State: " << ~myAttitude->GetStateObject().GetState() << endl;
00064
00065
00066 tick();
00067 myPropagator->Propagate(integrationTimes, myOrbit->GetStateObject().GetState(), myAttitude->GetStateObject().GetState());
00068 ssfSeconds calcTime = tock();
00069 cout << "finished propagating in " << calcTime << " seconds." << endl;
00070
00071 for (int ii = 0; ii < 2 ; ++ii)
00072 {
00073
00074 integrationTimes[0] = integrationTimes[1];
00075 integrationTimes[1] = integrationTimes[0] + 92*60*numOrbits;
00076 cout << integrationTimes[0] << " -> " << integrationTimes[1] << endl;
00077 myPropagator->Propagate(integrationTimes, myOrbit->GetStateObject().GetState(), myAttitude->GetStateObject().GetState());
00078 }
00079
00080 Matrix orbitHistory = myOrbit->GetHistory().GetHistory();
00081 Matrix orbitPlotting = orbitHistory(_,_(MatrixIndexBase+1,MatrixIndexBase+3));
00082 Matrix attitudeHistory = myAttitude->GetHistory().GetHistory();
00083 Matrix attitudePlotting = attitudeHistory(_,_(MatrixIndexBase,MatrixIndexBase+4));
00084
00085
00086 Plot3D(orbitPlotting);
00087
00088 Plot2D(attitudePlotting);
00089
00090
00091 ofstream ofile;
00092 ofile.open("OrbitHistory.dat");
00093 ofile << myOrbit->GetHistory().GetHistory();
00094 ofile.close();
00095
00096 ofile.open("AttitudeHistory.dat");
00097 ofile << myAttitude->GetHistory().GetHistory();
00098 ofile.close();
00099
00100 return 0;
00101
00102 }
00103
00104
00105
00106
00107
00108 Vector GravityForceFunction(const ssfTime &_currentTime, const OrbitState &_currentOrbitState, const AttitudeState &_currentAttitudeState, const EnvFuncParamaterType &_parameterList)
00109 {
00110
00111 static Vector Forces(3);
00112
00113 static Vector Position(3); Position(_) = _currentOrbitState.GetState()(_(VectorIndexBase,VectorIndexBase+2));
00114 Forces = -398600.441 / pow(norm2(Position),3) * Position;
00115 return Forces;
00116 }
00117
00118 Vector DragForceFunction(const ssfTime &_currentTime, const OrbitState &_currentOrbitState, const AttitudeState &_currentAttitudeState, const EnvFuncParamaterType &_parameterList)
00119 {
00120 static Vector Forces(3);
00121 double BC = *(reinterpret_cast<double*>(_parameterList[0]));
00122 static Vector Vrel(3); Vrel(_) = _currentOrbitState.GetState()(_(VectorIndexBase+3,VectorIndexBase+5));
00123 double Vrel_mag = norm2(Vrel);
00124 double rho = *(reinterpret_cast<double*>(_parameterList[1]));
00125 Forces = -1/2 * rho / BC * pow(Vrel_mag,2) * Vrel / Vrel_mag;
00126 return Forces;
00127 }
00128
00129 Environment* SetupEnvironment()
00130 {
00131
00132 Environment* pEarthEnv = new Environment;
00133 EarthCentralBody *pCBEarth = new EarthCentralBody;
00134 pEarthEnv->SetCentralBody(pCBEarth);
00135
00136
00137 cout << "Filling Parameters" << endl;
00138 EnvFunction TwoBodyGravity(&GravityForceFunction);
00139 pEarthEnv->AddForceFunction(TwoBodyGravity);
00140
00141
00142 EnvFunction DragForce(&DragForceFunction);
00143 double *BC = new double(200);
00144 DragForce.AddParameter(reinterpret_cast<void*>(BC), 1);
00145 double *rho = new double(1.13 * pow(static_cast<double>(10), static_cast<double>(-12)));
00146 DragForce.AddParameter(reinterpret_cast<void*>(rho), 2);
00147 pEarthEnv->AddForceFunction(DragForce);
00148
00149 cout << "Finished Setting up Earth Environment" << endl;
00150 return pEarthEnv;
00151 }
00152
00153
00154
00155
00156 NumericPropagator* SetupPropagator()
00157 {
00158 CombinedNumericPropagator* myProp = new CombinedNumericPropagator;
00159
00160
00161
00162 RungeKuttaIntegrator* orbitIntegrator = new RungeKuttaIntegrator;
00163 RungeKuttaIntegrator* attitudeIntegrator = new RungeKuttaIntegrator;
00164
00165 orbitIntegrator->SetNumSteps(100);
00166 myProp->SetOrbitIntegrator(orbitIntegrator);
00167 attitudeIntegrator->SetNumSteps(1000);
00168 myProp->SetAttitudeIntegrator(attitudeIntegrator);
00169
00170 myProp->SetOrbitStateConversion(&myOrbitStateConvFunc);
00171 myProp->SetAttitudeStateConversion(&myAttitudeStateConvFunc);
00172 return myProp;
00173 }
00174
00175
00176
00177
00178 void myOrbitStateConvFunc(const Matrix &_meshPoint, OrbitState &_convertedOrbitState)
00179 {
00180 static Vector tempVector(_meshPoint[MatrixColsIndex].getIndexBound() - 1);
00181 tempVector(_) = ~_meshPoint(_,_(MatrixIndexBase+1, _meshPoint[MatrixColsIndex].getIndexBound()));
00182 _convertedOrbitState.SetState(tempVector);
00183 return;
00184 }
00185
00186 Orbit* SetupOrbit()
00187 {
00188 Orbit* myOrbit = new Orbit;
00189
00190
00191 OrbitState myOrbitState;
00192 myOrbitState.SetStateRepresentation(new PositionVelocity);
00193 myOrbitState.SetOrbitFrame(new OrbitFrameIJK);
00194 Vector initPV(6);
00195
00196 initPV(VectorIndexBase+0) = -5776.6;
00197 initPV(VectorIndexBase+1) = -157;
00198 initPV(VectorIndexBase+2) = 3496.9;
00199 initPV(VectorIndexBase+3) = -2.595;
00200 initPV(VectorIndexBase+4) = -5.651;
00201 initPV(VectorIndexBase+5) = -4.513;
00202 myOrbitState.SetState(initPV);
00203 myOrbit->SetStateObject(myOrbitState);
00204
00205 myOrbit->SetDynamicsEq(&TwoBodyDynamics);
00206
00207
00208 Matrix Parameters(1,1);
00209 Parameters(MatrixIndexBase,MatrixIndexBase) = 398600.441;
00210 myOrbit->SetParameters(Parameters);
00211 return myOrbit;
00212 }
00213
00214
00215
00216
00217
00218 void myAttitudeStateConvFunc(const Matrix &_meshPoint, AttitudeState &_convertedAttitudeState)
00219 {
00220 static Vector tempQ(4); tempQ(_) = ~_meshPoint(_,_(2, 5));
00221 static Vector tempVector(3); tempVector(_) = ~_meshPoint(1, _(6, 8));
00222 _convertedAttitudeState.SetState(Rotation(Quaternion(tempQ)), tempVector);
00223 return;
00224 }
00225 static Vector AttituteDynamics(const ssfTime &_time, const Vector& _integratingState, Orbit *_Orbit, Attitude *_Attitude, const Matrix &_parameters, const Functor &_forceFunctorPtr)
00226 {
00227 static Vector stateDot(7);
00228 static Matrix bMOI(3,3);
00229 static Matrix qtemp(4,3);
00230 static Matrix Tmoment(3,1);
00231 static Vector qIn(4);
00232 static Vector wIn(3);
00233 qIn = _integratingState(_(VectorIndexBase,VectorIndexBase + 3));
00234 wIn = _integratingState(_(VectorIndexBase + 4,VectorIndexBase + 6));
00235 qIn /= norm2(qIn);
00236
00237
00238 qtemp(_(VectorIndexBase+0,VectorIndexBase+2),_(VectorIndexBase+0,VectorIndexBase+2)) = skew(qIn(_(VectorIndexBase+0,VectorIndexBase+2))) + qIn(VectorIndexBase+3) * eye(3);
00239 qtemp(VectorIndexBase+3, _(VectorIndexBase+0,VectorIndexBase+2)) = -(~qIn(_(VectorIndexBase+0,VectorIndexBase+2)));
00240 qtemp(_,MatrixIndexBase) = 0.5 * qtemp * wIn;
00241
00242 bMOI = _parameters(_(MatrixIndexBase+0,MatrixIndexBase+2),_(MatrixIndexBase+0,MatrixIndexBase+2));
00243 Tmoment(_,_) = (_forceFunctorPtr.Call(_time, _Orbit->GetStateObject(), _Attitude->GetStateObject()))(_);
00244 Tmoment = (bMOI.inverse() * (Tmoment - skew(wIn) * (bMOI * wIn)));
00245
00246 stateDot(_(VectorIndexBase,VectorIndexBase + 3)) = qtemp(_,MatrixIndexBase);
00247 stateDot(_(VectorIndexBase + 4,VectorIndexBase + 6)) = Tmoment(_,MatrixIndexBase);
00248
00249 return stateDot;
00250 }
00251
00252 Attitude* SetupAttitude()
00253 {
00254 Attitude* myAttitude = new Attitude;
00255
00256 AttitudeState myAttitudeState;
00257 myAttitudeState.SetRotation(Rotation(Quaternion(0,0,0,1)));
00258 Vector initAngVelVector(3);
00259 initAngVelVector(1) = 0.1;
00260 myAttitudeState.SetAngularVelocity(initAngVelVector);
00261
00262 myAttitude->SetStateObject(myAttitudeState);
00263 myAttitude->SetDynamicsEq(&AttituteDynamics);
00264 myAttitude->SetParameters(eye(3));
00265
00266 return myAttitude;
00267
00268 }
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288