-
Notifications
You must be signed in to change notification settings - Fork 13
/
PhysicalModel.cpp
1889 lines (1723 loc) · 67.5 KB
/
PhysicalModel.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//$Id$
//------------------------------------------------------------------------------
// PhysicalModel
//------------------------------------------------------------------------------
// GMAT: General Mission Analysis Tool.
//
// Copyright (c) 2002 - 2015 United States Government as represented by the
// Administrator of the National Aeronautics and Space Administration.
// All Other Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0.
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language
// governing permissions and limitations under the License.
//
// *** File Name : PhysicalModel.cpp
// *** Created : October 1, 2002
// ***************************************************************************
// *** Developed By : Thinking Systems, Inc. (www.thinksysinc.com) ***
// *** For: Flight Dynamics Analysis Branch (Code 572) ***
// *** Under Contract: P.O. GSFC S-66617-G ***
// *** ***
// *** This software is subject to the Software Usage Agreement described ***
// *** by NASA Case Number GSC-14735-1. The Software Usage Agreement ***
// *** must be included in any distribution. Removal of this header is ***
// *** strictly prohibited. ***
// *** ***
// *** ***
// *** Header Version: July 12, 2002 ***
// ***************************************************************************
// Module Type : ANSI C++ Source
// Development Environment : Visual C++ 7.0
// Modification History : 11/26/2002 - D. Conway, Thinking Systems, Inc.
// Original delivery
//
// : 1/8/2003 - D. Conway, Thinking Systems, Inc.
// Updated interfaces based on GSFC feedback
//
// : 2/5/2003 - D. Conway, Thinking Systems, Inc.
// Incorporated the Derivative class into this
// class and removed Derivative from the class
// heirarchy
//
// : 3/3/2003 - D. Conway, Thinking Systems, Inc.
// Updated parameter strings to include units;
// Added code to switch between relative and
// absolute error calculations
//
// : 09/24/2003 - W. Waktola, Missions Applications Branch
// Changes:
// - Updated style using GMAT cpp style guide//
//
// : 10/15/2003 - W. Waktola, Missions Applications Branch
// Changes:
// - All double types to Real types
// - All primitive int types to Integer types
// - virtual char* GetParameterName(const int parm) const to
// virtual std::string GetParameterName(const int parm) const
// - Changed GetParameterName() from if statements to switch statements
// Removals:
// - static Real parameterUndefined
// - SetUndefinedValue()
// - ParameterCount()
// - GetParameter()
// - SetParameter()
// Additions:
// - PARAMTER_TEXT[]
// - PARAMETER_TYPE[]
// - GetParameterText()
// - GetParameterID()
// - GetParameterType()
// - GetParameterTypeString()
// - GetRealParameter()
// - SetRealParameter()
//
// : 10/20/2003 - W. Waktola, Missions Applications Branch
// Changes:
// - Fixed format.
// - parameterCount to PhysicalModelParamCount.
// Removals:
// - GetParameterName()
//
// : 10/23/2003 - D. Conway, Thinking Systems, Inc. &
// W. Waktola, Missions Applications Branch
// Changes:
// - Changed constructor from PhysicalModel::PhysicalModel(void) to
// PhysicalModel(Gmat::ObjectType typeId, const std::string &typeStr,
// const std::string &nomme = "")
// - Added parameterCount = 1 in constructors
// - In SetErrorThreshold(), changed statement from relativeErrorThreshold = fabs(thold);
// to relativeErrorThreshold = (thold >= 0.0 ? thold : -thold);
//
// **************************************************************************
#include "PhysicalModel.hpp"
#include "gmatdefs.hpp"
#include "GmatBase.hpp"
#include "CelestialBody.hpp"
#include "MessageInterface.hpp"
#include "TimeTypes.hpp"
#include "PropagationStateManager.hpp"
//#define PHYSICAL_MODEL_DEBUG_INIT
//#define DEBUG_INITIALIZATION
//#define DEBUG_PM_CONSTRUCT
//#define DEBUG_STATE_ALLOCATION
//#define DEBUG_MEMORY
//#ifndef DEBUG_MEMORY
//#define DEBUG_MEMORY
//#endif
#ifdef DEBUG_MEMORY
#include "MemoryTracker.hpp"
#endif
//---------------------------------
// static data
//---------------------------------
const std::string
PhysicalModel::PARAMETER_TEXT[PhysicalModelParamCount - GmatBaseParamCount] =
{
"Epoch",
"ElapsedSeconds",
"BodyName",
"DerivativeID",
};
const Gmat::ParameterType
PhysicalModel::PARAMETER_TYPE[PhysicalModelParamCount - GmatBaseParamCount] =
{
Gmat::REAL_TYPE,
Gmat::REAL_TYPE,
Gmat::OBJECT_TYPE,
Gmat::INTEGER_TYPE,
};
//---------------------------------
// public
//---------------------------------
//------------------------------------------------------------------------------
// PhysicalModel(Gmat::ObjectType typeId, const std::string &typeStr,
// const std::string &nomme = "")
//------------------------------------------------------------------------------
/**
* Constructor for the Physical Model base class
*
* This constructor sets the size of the physical model to one variable, and
* NULLs the state pointer. Derived classes should set the dimension parameter
* to a more appropriate value; the Initialize() method is used to allocate
* state data array.
*/
//------------------------------------------------------------------------------
PhysicalModel::PhysicalModel(Gmat::ObjectType id, const std::string &typeStr,
const std::string &nomme) :
GmatBase (id, typeStr, nomme),
body (NULL),
forceOrigin (NULL),
bodyName ("Earth"),
dimension (1),
stateChanged (false),
psm (NULL),
theState (NULL),
modelState (NULL),
rawState (NULL),
epoch (21545.0),
elapsedTime (0.0),
prevElapsedTime (0.0),
deriv (NULL),
relativeErrorThreshold (0.10),
solarSystem (NULL),
fillCartesian (true),
cartesianStart (0),
cartesianCount (0),
fillSTM (false),
stmStart (-1),
stmCount (0),
stmRowCount (6),
fillAMatrix (false),
aMatrixStart (-1),
aMatrixCount (0)
{
objectTypes.push_back(Gmat::PHYSICAL_MODEL);
objectTypeNames.push_back("PhysicalModel");
parameterCount = PhysicalModelParamCount;
// Do not allow ODE model changes in command mode
blockCommandModeAssignment = true;
}
//------------------------------------------------------------------------------
// PhysicalModel::~PhysicalModel()
//------------------------------------------------------------------------------
/**
* Destructor for the model
* If the state array has been allocated, this destructor destroys it.
*/
//------------------------------------------------------------------------------
PhysicalModel::~PhysicalModel()
{
#ifdef DEBUG_STATE_ALLOCATION
MessageInterface::ShowMessage("Deleting PhysicalModel (type %s) at %p\n",
typeName.c_str(), this);
#endif
if (rawState != modelState)
if (rawState)
{
#ifdef DEBUG_MEMORY
MemoryTracker::Instance()->Remove
(rawState, "rawState", "PhysicalModel::~PhysicalModel()",
"deleting rawState", this);
#endif
#ifdef DEBUG_STATE_ALLOCATION
MessageInterface::ShowMessage("Deleting rawState at %p\n",
rawState);
#endif
delete [] rawState;
}
if (modelState)
{
#ifdef DEBUG_MEMORY
MemoryTracker::Instance()->Remove
(modelState, "modelState", "PhysicalModel::~PhysicalModel()",
"deleting modelState", this);
#endif
#ifdef DEBUG_STATE_ALLOCATION
MessageInterface::ShowMessage("Deleting modelState at %p\n",
modelState);
#endif
delete [] modelState;
}
if (deriv)
{
#ifdef DEBUG_MEMORY
MemoryTracker::Instance()->Remove
(deriv, "deriv", "PhysicalModel::~PhysicalModel()",
"deleting deriv", this);
#endif
delete [] deriv;
}
}
//------------------------------------------------------------------------------
// PhysicalModel::PhysicalModel(const PhysicalModel& pm)
//------------------------------------------------------------------------------
/**
* The copy constructor for the physical model.
*
* @param pm The model copied here
*/
//------------------------------------------------------------------------------
PhysicalModel::PhysicalModel(const PhysicalModel& pm) :
GmatBase (pm),
/// @note: Since the next two are global objects, assignment works
body (pm.body),
forceOrigin (pm.forceOrigin),
bodyName (pm.bodyName),
dimension (pm.dimension),
stateChanged (pm.stateChanged),
psm (NULL),
theState (NULL),
modelState (NULL),
rawState (NULL),
epoch (pm.epoch),
elapsedTime (pm.elapsedTime),
prevElapsedTime (pm.prevElapsedTime),
deriv (NULL),
derivativeIds (pm.derivativeIds),
derivativeNames (pm.derivativeNames),
relativeErrorThreshold (pm.relativeErrorThreshold),
solarSystem (pm.solarSystem),
fillCartesian (pm.fillCartesian),
cartesianStart (pm.cartesianStart),
cartesianCount (pm.cartesianCount),
fillSTM (pm.fillSTM),
stmStart (pm.stmStart),
stmCount (pm.stmCount),
stmRowCount (pm.stmRowCount),
fillAMatrix (pm.fillAMatrix),
aMatrixStart (pm.aMatrixStart),
aMatrixCount (pm.aMatrixCount)
{
if (pm.modelState != NULL)
{
if (modelState != NULL)
{
#ifdef DEBUG_STATE_ALLOCATION
MessageInterface::ShowMessage("Deleting modelState at %p\n",
modelState);
#endif
delete [] modelState;
modelState = NULL;
}
modelState = new Real[dimension];
#ifdef DEBUG_MEMORY
MemoryTracker::Instance()->Add
(modelState, "modelState", "PhysicalModel::PhysicalModel(copy)",
"modelState = new Real[dimension]", this);
#endif
if (modelState != NULL)
memcpy(modelState, pm.modelState, dimension * sizeof(Real));
else
isInitialized = false;
}
rawState = modelState;
if (pm.deriv != NULL)
{
deriv = new Real[dimension];
#ifdef DEBUG_MEMORY
MemoryTracker::Instance()->Add
(deriv, "deriv", "PhysicalModel::PhysicalModel(copy)",
"deriv = new Real[dimension]", this);
#endif
if (deriv != NULL)
memcpy(deriv, pm.deriv, dimension * sizeof(Real));
else
isInitialized = false;
}
}
//------------------------------------------------------------------------------
// PhysicalModel& PhysicalModel::operator=(const PhysicalModel& pm)
//------------------------------------------------------------------------------
/**
* The assignment operator for the physical model
*
* @param pm The model copied into this one.
*
* @retval This PhysicalModel. configured to match pm.
*/
//------------------------------------------------------------------------------
PhysicalModel& PhysicalModel::operator=(const PhysicalModel& pm)
{
if (&pm == this)
return *this;
GmatBase::operator=(pm);
#ifdef DEBUG_PM_CONSTRUCT
MessageInterface::ShowMessage("Entering copy constructor for PM: object name is: %s\n",
pm.GetName().c_str());
MessageInterface::ShowMessage(" epoch = %le\n", pm.epoch);
MessageInterface::ShowMessage(" epoch from state = %le\n", (pm.theState)->GetEpoch());
#endif
/// @note: Since the next two are global objects, assignment works
body = pm.body;
forceOrigin = pm.forceOrigin;
bodyName = pm.bodyName;
dimension = pm.dimension;
isInitialized = false; //pm.initialized;
epoch = pm.epoch;
elapsedTime = pm.elapsedTime;
prevElapsedTime = pm.prevElapsedTime;
relativeErrorThreshold = pm.relativeErrorThreshold;
solarSystem = pm.solarSystem;
fillCartesian = pm.fillCartesian;
cartesianStart = pm.cartesianStart;
cartesianCount = pm.cartesianCount;
fillSTM = pm.fillSTM;
stmStart = pm.stmStart;
stmCount = pm.stmCount;
stmRowCount = pm.stmRowCount;
fillAMatrix = pm.fillAMatrix;
aMatrixStart = pm.aMatrixStart;
aMatrixCount = pm.aMatrixCount;
theState = pm.theState;
if (pm.modelState)
{
if (modelState)
{
#ifdef DEBUG_MEMORY
MemoryTracker::Instance()->Remove
(modelState, "modelState", "PhysicalModel::operator=()",
"deleting modelState", this);
#endif
#ifdef DEBUG_STATE_ALLOCATION
MessageInterface::ShowMessage("Deleting modelState at %p\n",
modelState);
#endif
delete [] modelState;
modelState = NULL;
}
modelState = new Real[dimension];
#ifdef DEBUG_MEMORY
MemoryTracker::Instance()->Add
(modelState, "modelState", "ODEModel::operator=()",
"modelState = new Real[dimension]", this);
#endif
if (modelState != NULL)
memcpy(modelState, pm.modelState, dimension * sizeof(Real));
else
isInitialized = false;
stateChanged = pm.stateChanged;
}
else
{
if (modelState != NULL)
{
#ifdef DEBUG_STATE_ALLOCATION
MessageInterface::ShowMessage("Deleting modelState at %p\n",
modelState);
#endif
delete [] modelState;
modelState = NULL;
}
}
rawState = modelState;
if (pm.deriv)
{
if (deriv)
{
#ifdef DEBUG_MEMORY
MemoryTracker::Instance()->Remove
(deriv, "deriv", "PhysicalModel::operator=()",
"deleting deriv", this);
#endif
delete [] deriv;
deriv = NULL;
}
deriv = new Real[dimension];
#ifdef DEBUG_MEMORY
MemoryTracker::Instance()->Add
(deriv, "deriv", "ODEModel::operator=()",
"deriv = new Real[dimension]", this);
#endif
if (deriv != NULL)
memcpy(deriv, pm.deriv, dimension * sizeof(Real));
else
isInitialized = false;
}
else
{
if (deriv)
{
delete [] deriv;
deriv = NULL;
}
}
return *this;
}
//------------------------------------------------------------------------------
// CelestialBody* GetBody()
//------------------------------------------------------------------------------
/**
*
*/
//------------------------------------------------------------------------------
CelestialBody* PhysicalModel::GetBody()
{
return body;
}
//------------------------------------------------------------------------------
// CelestialBody* GetBodyName()
//------------------------------------------------------------------------------
/**
*
*/
//------------------------------------------------------------------------------
std::string PhysicalModel::GetBodyName()
{
return bodyName;
}
//------------------------------------------------------------------------------
// void SetBody(CelestialBody *body)
//------------------------------------------------------------------------------
/**
*
*/
//------------------------------------------------------------------------------
void PhysicalModel::SetBody(CelestialBody *theBody)
{
// Comments: delete body may cause crash when body is a celestial body in solar system.
// For example, if Earth object is deleted, GMAT will crash due to other part of
// GMAT code is used Earth object for calculation.
//if (theBody != NULL)
//{
// if (body != NULL)
// {
// #ifdef DEBUG_MEMORY
// MemoryTracker::Instance()->Remove
// (deriv, "deriv", "PhysicalModel::SetBody()",
// "deleting deriv", this);
// #endif
// delete body;
// }
//}
body = theBody;
bodyName = body->GetName();
// mu = theBody->GetGravitationalConstant();
}
void PhysicalModel::SetForceOrigin(CelestialBody* toBody)
{
forceOrigin = toBody;
}
CelestialBody* PhysicalModel::GetForceOrigin()
{
return forceOrigin;
}
//------------------------------------------------------------------------------
// bool PhysicalModel::Initialize()
//------------------------------------------------------------------------------
/**
* Prepare the physical model for use
*
* This method allocates the state and deriv arrays, and can be overridden to
* perform other actions for the system setup.
*
* Note that deriv is allocated even if it is not used. This feature may be
* PhysicalModelremoved in a later release.
*/
//------------------------------------------------------------------------------
bool PhysicalModel::Initialize()
{
#ifdef DEBUG_INITIALIZATION
MessageInterface::ShowMessage(
"PhysicalModel::Initialize() entered for %s; dimension = %d\n",
typeName.c_str(), dimension);
#endif
if ((rawState != NULL) && (rawState != modelState))
{
#ifdef DEBUG_MEMORY
MemoryTracker::Instance()->Remove
(rawState, "rawState", "PhysicalModel::Initialize()",
"deleting rawState", this);
#endif
#ifdef DEBUG_STATE_ALLOCATION
MessageInterface::ShowMessage("Deleting rawState at %p\n", rawState);
#endif
delete [] rawState;
rawState = NULL;
}
if (modelState)
{
#ifdef DEBUG_MEMORY
MemoryTracker::Instance()->Remove
(modelState, "modelState", "PhysicalModel::Initialize()",
"deleting modelState", this);
#endif
#ifdef DEBUG_STATE_ALLOCATION
MessageInterface::ShowMessage("Deleting modelState (for %s) at %p\n",
typeName.c_str(), modelState);
#endif
delete [] modelState;
modelState = NULL;
rawState = NULL;
isInitialized = false;
}
if (deriv)
{
#ifdef DEBUG_MEMORY
MemoryTracker::Instance()->Remove
(deriv, "deriv", "PhysicalModel::Initialize()",
"deleting deriv", this);
#endif
delete [] deriv;
deriv = NULL;
}
// MessageInterface::ShowMessage("PMInitialize setting dim = %d\n", dimension);
#ifdef DEBUG_STATE_ALLOCATION
MessageInterface::ShowMessage("Allocating modelState (for %s) at ",
typeName.c_str());
#endif
modelState = new Real[dimension];
#ifdef DEBUG_STATE_ALLOCATION
MessageInterface::ShowMessage("%p\n", modelState);
#endif
#ifdef DEBUG_MEMORY
MemoryTracker::Instance()->Add
(modelState, "modelState", "PhysicalModel::Initialize()",
"modelState = new Real[dimension]", this);
#endif
if (modelState != NULL)
{
deriv = new Real[dimension];
for (Integer i = 0; i < dimension; ++i)
deriv[i] = 0.0;
#ifdef DEBUG_MEMORY
MemoryTracker::Instance()->Add
(deriv, "deriv", "PhysicalModel::Initialize()",
"deriv = new Real[dimension]", this);
#endif
if (deriv)
isInitialized = true;
else
isInitialized = false;
}
else
isInitialized = false;
rawState = modelState;
return isInitialized;
}
//------------------------------------------------------------------------------
// bool SetBody(const std::string &theBody)
//------------------------------------------------------------------------------
/**
* This method sets the body for this PhysicalModel object.
*
* @param <theBody> body name to use.
*
* @return flag indicating success of the operation.
*/
//------------------------------------------------------------------------------
bool PhysicalModel::SetBody(const std::string &theBody)
{
bodyName = theBody;
// initialize the body
if (solarSystem == NULL) throw ODEModelException( // or just return false?
"Solar System undefined for Harmonic Field.");
body = solarSystem->GetBody(bodyName); // catch errors here?
return true;
}
//------------------------------------------------------------------------------
// void SetBodyName(const std::string &name)
//------------------------------------------------------------------------------
/**
*
*/
//------------------------------------------------------------------------------
void PhysicalModel::SetBodyName(const std::string &theBody)
{
bodyName = theBody;
}
//------------------------------------------------------------------------------
// Real PhysicalModel::GetErrorThreshold(void) const
//------------------------------------------------------------------------------
/**
* Returns the threshold for switching between relative and absolute error
*/
//------------------------------------------------------------------------------
Real PhysicalModel::GetErrorThreshold(void) const
{
return relativeErrorThreshold;
}
//------------------------------------------------------------------------------
// bool PhysicalModel::SetErrorThreshold(const Real thold)
//------------------------------------------------------------------------------
/**
* Sets the threshold for switching between relative and absolute error
*
* @param thold The new threshold value
*/
//------------------------------------------------------------------------------
bool PhysicalModel::SetErrorThreshold(const Real thold)
{
relativeErrorThreshold = (thold >= 0.0 ? thold : -thold);
return true;
}
//------------------------------------------------------------------------------
// Integer PhysicalModel::GetDimension()
//------------------------------------------------------------------------------
/**
* Accessor method used by Propagator class to determine # of vars
* The Propagator class evolves the system being modeled by advancing some
* number of variables. The count of the variables must be coordinated
* between the propagator and the physical model of the system;
* GetDimension() is called by the Propagator class to obtain this
* information from the PhysicalModel class.
*/
//------------------------------------------------------------------------------
Integer PhysicalModel::GetDimension()
{
return dimension;
}
//------------------------------------------------------------------------------
// void PhysicalModel::SetDimension(Integer n)
//------------------------------------------------------------------------------
/**
* Accessor method used to set # of vars
* Use this method to reset the count of the variables modeled by the physical
* model of the system for models that allow for changes in this value.
*/
//------------------------------------------------------------------------------
void PhysicalModel::SetDimension(Integer n)
{
dimension = n;
isInitialized = false;
}
//------------------------------------------------------------------------------
// Real * PhysicalModel::GetState()
//------------------------------------------------------------------------------
/**
* Accessor method used to access the state array
* Use this method with care -- it exposes the internal array of state data to
* external users. The Propagator and Integrator classes can use this access to
* make system evelotion more efficient, but at the cost of loss of
* encapsulation of the state data.
*/
//------------------------------------------------------------------------------
Real * PhysicalModel::GetState()
{
return modelState;
// return rawState;
}
//------------------------------------------------------------------------------
// Real * PhysicalModel::GetJ2KState()
//------------------------------------------------------------------------------
/**
* Accessor method used to access the J2000 body based state array
* Use this method with care -- it exposes the internal array of state data to
* external users. The Propagator and Integrator classes can use this access to
* make system evolution more efficient, but at the cost of loss of
* encapsulation of the state data.
*/
//------------------------------------------------------------------------------
Real * PhysicalModel::GetJ2KState()
{
return rawState;
}
//------------------------------------------------------------------------------
// void PhysicalModel::SetState(const Real * st)
//------------------------------------------------------------------------------
/**
* Used to set the elements of the state array
*
* @param st Array of data containing the desired values for the state elements
*/
//------------------------------------------------------------------------------
void PhysicalModel::SetState(const Real * st)
{
#ifdef PHYSICAL_MODEL_DEBUG_INIT
MessageInterface::ShowMessage(
"PhysicalModel::SetState() called for %s<%s>\n",
typeName.c_str(), instanceName.c_str());
#endif
if (modelState != NULL)
{
for (Integer i = 0; i < dimension; i++)
modelState[i] = st[i];
stateChanged = true;
}
}
void PhysicalModel::SetState(GmatState * st)
{
#ifdef PHYSICAL_MODEL_DEBUG_INIT
MessageInterface::ShowMessage(
"PhysicalModel::SetState(GmatState) called for %s<%s>\n",
typeName.c_str(), instanceName.c_str());
MessageInterface::ShowMessage(" and epoch = %le\n", st->GetEpoch());
#endif
theState = st;
epoch = st->GetEpoch(); // ***
if (dimension != st->GetSize())
MessageInterface::ShowMessage("Dimension mismatch!!!\n");
if (modelState != NULL)
SetState(st->GetState());
}
//------------------------------------------------------------------------------
// const Real* PhysicalModel::GetDerivativeArray(void)
//------------------------------------------------------------------------------
/**
* Accessor for the derivative array
* This method returns a pointer to the derivative array. The Predictor-
* Correctors need this access in order to extrapolate the next state.
*/
//------------------------------------------------------------------------------
const Real* PhysicalModel::GetDerivativeArray()
{
return deriv;
}
//------------------------------------------------------------------------------
// void PhysicalModel::IncrementTime(Real dt)
//------------------------------------------------------------------------------
/**
* Used to increment the internal time counter
*
* @param dt Amount of time to increment by (usually in seconds)
*/
//------------------------------------------------------------------------------
void PhysicalModel::IncrementTime(Real dt)
{
#ifdef DEBUG_TIME_INCREMENT
MessageInterface::ShowMessage("Increment time called; prevElapsedTime = "
"%.12lf, elapsed time = %.12lf ==> ", prevElapsedTime,
elapsedTime);
#endif
prevElapsedTime = elapsedTime;
elapsedTime += dt;
stateChanged = true;
#ifdef DEBUG_TIME_INCREMENT
MessageInterface::ShowMessage("prevElapsedTime = %.12lf, elapsed time = "
"%.12lf\n", prevElapsedTime, elapsedTime);
#endif
}
//------------------------------------------------------------------------------
// Real PhysicalModel::GetTime()
//------------------------------------------------------------------------------
/**
* Read accessor for the time elapsed
* Use this method to track the elapsed time for the model. You can set the
* system to start from a non-zero time by setting the value for the elapsedTime
* parameter to the desired start value. See the SetTime parameter for the
* write accessor.
*/
//------------------------------------------------------------------------------
Real PhysicalModel::GetTime()
{
return elapsedTime;
}
//------------------------------------------------------------------------------
// void PhysicalModel::SetTime(Real t)
//------------------------------------------------------------------------------
/**
* Write accessor for the total time elapsed
* Use this method to set time for the model
*/
//------------------------------------------------------------------------------
void PhysicalModel::SetTime(Real t)
{
elapsedTime = t;
}
//------------------------------------------------------------------------------
// bool PhysicalModel::GetDerivatives(Real * state, Real dt, Integer order,
// const Integer id)
//------------------------------------------------------------------------------
/**
* Method invoked to calculate derivatives
* This method is invoked to fill the deriv array with derivative information
* for the system that is being integrated. It uses the state and elapsedTime
* parameters, along with the time interval dt passed in as a parameter, to
* calculate the derivative information at time \f$t=t_0+t_{elapsed}+dt\f$.
*
* @param dt Additional time increment for the derivatitive
* calculation; defaults to 0.
* @param state Pointer to the current state data. This can differ
* from the PhysicalModel state if the subscribing
* integrator samples other state values during
* propagation. (For example, the Runge-Kutta integrators
* do this during the stage calculations.)
* @param order The order of the derivative to be taken (first
* derivative, second derivative, etc)
* @param id ID for the type of derivative requested for models that
* support more than one type. Default value of -1
* indicates that the default derivative model is
* requested. This number should be a StateElementId.
*
* @return true if the call succeeds, false on failure. This
* default implementation always returns false.
*/
//------------------------------------------------------------------------------
bool PhysicalModel::GetDerivatives(Real * state, Real dt, Integer order,
const Integer id)
{
return false;
}
//------------------------------------------------------------------------------
// Rvector6 GetDerivativesForSpacecraft(Spacecraft *sc)
//------------------------------------------------------------------------------
/**
* Retrieves the derivative vector for a Spacecraft acted on by the model.
*
* @param sc The Spacecraft that is located at the time and place of the
* derivative calculation
*
* @return The derivative vector for the spacecraft's state
*/
//------------------------------------------------------------------------------
Rvector6 PhysicalModel::GetDerivativesForSpacecraft(Spacecraft *sc)
{
Rvector6 retval;
throw ODEModelException("GetDerivativesForSpacecraft not implemented "
"for the " + typeName + " physical model.");
return retval;
}
//------------------------------------------------------------------------------
// Real PhysicalModel::EstimateError(Real * diffs, Real * answer) const
//------------------------------------------------------------------------------
/**
* Interface used to estimate the error in the current step
*
* The method calculates the largest local estimate of the error from the
* integration given the components of the differences calculated by the
* integrator. It returns the largest error estimate found.
*
* The default implementation returns the largest single relative component
* found based on the input arrays. In other words, the implementation provided
* here returns the largest component of the following vector:
*
* \f[\vec \epsilon = |{{{EE}_n}\over{x_n^f - x_n^i}}|\f]
*
* subject to the discussion of the relativeErrorThreshold parameter, below.
*
* There are several alternatives that users of this class can implement: the
* error could be calculated based on the largest error in the individual
* components of the state vector, as the magnitude of the state vector (that
* is, the L2 (rss) norm of the error estimate vector). The estimated error
* should never be negative, so a return value less than 0.0 can be used to
* indicate an error condition.
*
* One item to note in this implementation is the relativeErrorThreshold local
* variable. This parameter looks at the difference between the initial state
* of the variables and the state after the integration. If that difference is
* smaller in magnitude than the value of relativeErrorThreshold, then the error
* value calculated is the absolute error; if it is larger, the calculated value
* is scaled by the difference. In other words, given
*
* \f[\Delta^i = |r^i(t + \delta t) - r^i(t)|\f]
*
* this method will return the largest error in the final states if each
* component of \f$\Delta^i\f$ is smaller than relativeErrorThreshold, and will
* return the largest value of the error divided by the corresponding
* \f$\Delta^i\f$ if each component of \f$\Delta^i\f$ is larger than
* relativeErrorThreshold. This property lets the integrators step over small
* discontinuities (for example, shadow crossings for spacecraft orbital models)
* without hanging.
*
* @param diffs Array of differences calculated by the integrator. This array
* must be the same size as the state vector
* @param answer Candidate new state from the integrator
*
* @returns The maximum calculated error
*/
//------------------------------------------------------------------------------
Real PhysicalModel::EstimateError(Real * diffs, Real * answer) const
{
Real retval = 0.0, err, delta;
for (Integer i = 0; i < dimension; ++i)
{
delta = answer[i] - modelState[i];
if (delta > relativeErrorThreshold)
err = fabs(diffs[i] / delta);
else
err = fabs(diffs[i]);
if (err > retval)
retval = err;
}