-
Notifications
You must be signed in to change notification settings - Fork 0
/
taylorSeriesIntegrator.cpp
290 lines (218 loc) · 12.2 KB
/
taylorSeriesIntegrator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "algorithm"
#include "constants.h"
#include "taylorSeriesIntegrator.h"
#include "spacecraft.h"
#include "integrationSettings.h"
#include "lagrangeInterpolator.h"
#include "stepSizeControlTSI.h"
#include "problemRecurrenceRelations.h"
#include "discreteForceModel.h"
#include "Tudat/Mathematics/Interpolators/interpolator.h"
#include "Tudat/Mathematics/BasicMathematics/nearestNeighbourSearch.h"
#include "boost/make_shared.hpp"
//typedef Eigen::Matrix< double, 8, 1> Vector8d;
TaylorSeriesIntegrator::TaylorSeriesIntegrator(
ProblemRecurrenceRelationsPointer problemRecurrenceRelationsPointer,
SpacecraftPointer spacecraftpointer,
IntegrationSettingsPointer integrationSettingsPointer,
ConstantsPointer constantsPointer,
StepSizeControlTSIPointer stepSizeControlTSIPointer):
problemRecurrenceRelationsPointer_( problemRecurrenceRelationsPointer ),
spacecraftPointer_( spacecraftpointer ),
integrationSettingsPointer_( integrationSettingsPointer ),
constantsPointer_( constantsPointer ),
stepSizeControlTSIPointer_( stepSizeControlTSIPointer){}
TaylorSeriesIntegrator::~TaylorSeriesIntegrator()
{
}
void TaylorSeriesIntegrator::integrate()
{
// Setting the first time and state outputs
Eigen::MatrixXd Time( 1, 1 );
Time( 0, 0 ) = integrationSettingsPointer_->getInitialTime();
int numberOfStateVariables = spacecraftPointer_->getInitialState().rows();
Eigen::MatrixXd initialState( numberOfStateVariables, 1 );
initialState = spacecraftPointer_->getInitialState();
Eigen::MatrixXd stateMatrix( 1, numberOfStateVariables );
stateMatrix.row( 0 ) = initialState.adjoint();
// Declare computedThrustAccelerationMatrix
Eigen::MatrixXd computedThrustAccelerationMatrix( 1, 3 );
// Initiate values for the while loop
double currentTime = integrationSettingsPointer_->getInitialTime();
Eigen::MatrixXd currentState( numberOfStateVariables, 1 );
currentState = initialState;
double currentStepSize = integrationSettingsPointer_->getInitialStepSize();
// Define thrustForceMatrix for in loop use
Eigen::Matrix< double, Eigen::Dynamic, 4 > thrustForceMatrix;
thrustForceMatrix = spacecraftPointer_->getThrustForceMatrix();
// Define variables for in loop use
double finalTime = integrationSettingsPointer_->getFinalTime();
int order = integrationSettingsPointer_->getOrderOfTaylorSeries();
int numberOfVariables = spacecraftPointer_->getInitialReducedState().size();
double minimumStepSize = integrationSettingsPointer_->getMinimumStepSize();
double maximumStepSize = integrationSettingsPointer_->getMaximumStepSize();
int refineFactor = integrationSettingsPointer_->getRefineFactor();
Eigen::MatrixXd dummy_variable;
// Declarations for variables used in time loop
Eigen::MatrixXd currentThrustAccelerationVector( 1, 3 );
double previousTime;
Eigen::MatrixXd nextStateVector( numberOfStateVariables, 1 );
Eigen::MatrixXd coefficientMatrix( order + 1, numberOfStateVariables );
int numberOfRowsInStateMatrix;
Eigen::MatrixXd interTime( order + 1, 1 );
Eigen::MatrixXd coefficientMatrixInLoop( order + 1, 1 );
// Advance the state
while( currentTime < finalTime ) // while the current time is smaller than the final time
{
/// Use discrete force model to update the current force,
/// force derivatives, acceleration and acceleration derivatives in the spacecraft body
// Construct discreteForceModel
DiscreteForceModelPointer discreteForceModelPointer =
boost::make_shared< DiscreteForceModel >(
spacecraftPointer_,
integrationSettingsPointer_,
constantsPointer_);
// Update variables in spacecraft body
discreteForceModelPointer->updateCurrentForcesAndAccelerationsForTSI( currentTime ); // Writes output in spacecraftPointer_
// is not done for the finalTime. So at finalTime the current forces and accelerations are not calculated
// Get current thrust accelerationvector from spacecraft pointer. Must be computed before next step size is calculated
currentThrustAccelerationVector = spacecraftPointer_->getCurrentAcceleration();
/// The general algorithm starts here (problem independent)
// Store currentTime for the refinement
previousTime = currentTime;
// Advance the time
currentTime = currentTime + currentStepSize;
if( currentTime > finalTime ) // if the next time is bigger than the final time
{
currentStepSize = currentStepSize - ( currentTime - finalTime ); // get the correct step size
currentTime = finalTime; // get the correct time
}
// Calculate next state and next time and matrix of derivatives. The output will be saved in
// the spacecaftPointer_
problemRecurrenceRelationsPointer_->computeNextState( currentStepSize);
//! Get output of problemRecurrenceRelations
// Get current state vector from spacecraft pointer
nextStateVector = spacecraftPointer_->getCurrentState();
// Get current coefficients of derivative matrix from spacecraft pointer.
coefficientMatrix = spacecraftPointer_->getCurrentCoefficientMatrix();
// Store the next state in new row of the state matrix
numberOfRowsInStateMatrix = stateMatrix.rows();
stateMatrix.conservativeResize( numberOfRowsInStateMatrix + 1, Eigen::NoChange );
stateMatrix.row( numberOfRowsInStateMatrix ) = nextStateVector.adjoint();
// Store the next time in new row of the time vector
Time.conservativeResize( numberOfRowsInStateMatrix + 1, Eigen::NoChange );
Time( numberOfRowsInStateMatrix, 0 ) = currentTime;
// Store the next computed thrust acceleration vector in a new row of the computed thrust acceleration matrix
computedThrustAccelerationMatrix.conservativeResize( numberOfRowsInStateMatrix, Eigen::NoChange );
computedThrustAccelerationMatrix.row( numberOfRowsInStateMatrix - 1 ) = currentThrustAccelerationVector;
// Define new matrix with the coefficients of the Taylor series
Eigen::MatrixXd coefficientMatrixReduced( coefficientMatrix.rows(), numberOfVariables );
coefficientMatrixReduced = coefficientMatrix.leftCols( numberOfVariables );
/// Next time step
// Define next step size
double temp_currentStepSize = currentStepSize;
currentStepSize = stepSizeControlTSIPointer_->determineNextStepSize( coefficientMatrixReduced, temp_currentStepSize );
// Correct step size in case step size is outside limits
if ( currentStepSize < minimumStepSize )
{
currentStepSize = minimumStepSize;
}
else if ( currentStepSize > maximumStepSize )
{
currentStepSize = maximumStepSize;
}
// Refinement
if ( refineFactor > 0 ) // refineFactor is defined as an int and defines amount of points in between
// previousTime and currentTime
{
double referenceStepSize = ( currentTime - previousTime ) / ( refineFactor + 1 );
// Compute the (refineFactor) additional steps in between previous and current time
Eigen::VectorXd referenceTime( refineFactor );
referenceTime.setLinSpaced( refineFactor, previousTime + referenceStepSize, currentTime - referenceStepSize );
Eigen::MatrixXd interStatesMatrix( refineFactor, numberOfVariables );
//interStatesMatrix.fill( 0.0 );
for ( int n = 0; n < refineFactor; n++ )
{
for ( int orderInLoop = 0; orderInLoop <= order; orderInLoop++ )
{
interTime( orderInLoop, 0 ) = std::pow( referenceTime( n ) - previousTime , orderInLoop );
}
for ( int n2 = 0; n2 < numberOfVariables; n2++ )
{
coefficientMatrixInLoop = coefficientMatrix.col( n2 );
dummy_variable = coefficientMatrixInLoop.adjoint() * interTime;
interStatesMatrix( n, n2 ) = dummy_variable( 0 );
}
}
Eigen::MatrixXd newInterStateMatrix( refineFactor, numberOfStateVariables );
newInterStateMatrix << interStatesMatrix, Eigen::MatrixXd::Zero( refineFactor, ( numberOfStateVariables - numberOfVariables ) );
// Add refined states to state matrix
Eigen::MatrixXd temp_stateMatrix( stateMatrix.rows() + refineFactor, stateMatrix.cols() );
temp_stateMatrix << stateMatrix.topRows( stateMatrix.rows() - 1 ),
newInterStateMatrix,
stateMatrix.bottomRows( 1 );
stateMatrix = temp_stateMatrix;
// Add refined times to state matrix
Eigen::MatrixXd temp_Time( Time.rows() + refineFactor, Time.cols() );
temp_Time << Time.topRows( Time.rows() - 1 ),
referenceTime,
Time.bottomRows( 1 );
Time = temp_Time;
}
}
// Store output
setStateMatrix( stateMatrix );
setTimeVector( Time );
setComputedThrustAccelerationMatrix( computedThrustAccelerationMatrix );
}
//void TaylorSeriesIntegrator::integrate()
//{
// // Set loop counter
// int i = 0; // Starting from the first element in a vector
// Vector8d errorRelative = settings_.getErrorRelative();
// Vector8d errorAbsolute = settings_.getErrorAbsolute();
// Eigen::VectorXd Time;
// Time( 0 ) = settings_.getInitialTime();
// // Get thrust force matrix
// // Put thrustForceMatrix_ in an std vector of std vector
// std::vector< std::vector > thrustForceMatrix_;
// std::vector thrustForceMatrix_; // vector of vectors, so it forms a matrix
// thrustForceMatrix_= spacecraft_.getThrustForce();
// // Extract thrust force vectors and time vector out of
// // the thrust force matrix
// Eigen::VectorXd thrustForceValue_time = thrustForceMatrix_.col(0);
// Eigen::VectorXd thrustForceVector_e1 = thrustForceMatrix_.col(1);
// Eigen::VectorXd thrustForceVector_e2 = thrustForceMatrix_.col(2);
// Eigen::VectorXd thrustForceVector_e3 = thrustForceMatrix_.col(3);
// while ( Time( i ) < ( settings_.getFinalTime() - settings_.getStepSize() ) )
// {
// // Calculation of limitingError
// Vector8d reducedCurrentState = spacecraft_.getState().block(0,0,8,1); // Extract from element (1,1) to the eighth row and first column
// Vector8d reducedCurrentStateAbs = reducedCurrentState.cwiseAbs();
// Vector8d product = errorRelative.cwiseProduct( reducedCurrentStateAbs );
// Vector8d limitingError = product.cwiseMax( errorAbsolute );
// //! Interpolator
// // Extract thrust force vectors and time vector out of
// // the thrust force matrix has been done outside while loop already
// const double thrustForceValue_e1 = tudat::interpolators::computeLinearInterpolation(
// thrustForceValue_time, thrustForceVector_e1,
// Time( i ) );
// const double thrustForceValue_e2 = tudat::interpolators::computeLinearInterpolation(
// thrustForceValue_time, thrustForceVector_e2,
// Time( i ) );
// const double thrustForceValue_e3 = tudat::interpolators::computeLinearInterpolation(
// thrustForceValue_time, thrustForceVector_e3,
// Time( i ) );
// // Store the three thrust force values in a 1x3 vector
// Eigen::Vector3d thrustForceVector_interpolated;
// thrustForceVector_interpolated( thrustForceValue_e1, thrustForceValue_e2, thrustForceValue_e3 );
//// Shouldn't there also be a second value of the thrust force vector be provided to the integratorStep function??
// integratorStep( settings_.getStepSize(), thrustForceVector_interpolated, limitingError );
// ///! Store next time and increase loop counter
// Time( i + 1 ) = Time( i ) + settings_.getStepSize();
// i++;
// }
//}
void TaylorSeriesIntegrator::integratorStep(double& stepSize,const Eigen::Vector3d& thrustForceVector,const double limitingError)
{
}