Skip to content

Commit 5423946

Browse files
authored
Merge pull request #1425 from opensim-org/manager-cleanup
Remove CMC usage from Manager and other Manager clean up
2 parents 126319d + 8246c8f commit 5423946

File tree

5 files changed

+64
-158
lines changed

5 files changed

+64
-158
lines changed

OpenSim/Simulation/Manager/Manager.cpp

Lines changed: 14 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ Manager::Manager(Model& model, bool dummyVar) :
7171
{
7272
setNull();
7373

74-
// STATES
75-
constructStates();
76-
7774
// STORAGE
7875
constructStorage();
7976

@@ -99,31 +96,15 @@ setNull()
9996
_sessionName = "";
10097
_ti = 0.0;
10198
_tf = 1.0;
102-
_firstDT = 1.0e-8;
103-
_steps = 0;
104-
_trys = 0;
105-
_maxSteps = 10000;
10699
_halt = false;
107-
_dtMax = 1.0;
108-
_dtMin = 1.0e-8;
109100
_specifiedDT = false;
110101
_constantDT = false;
111102
_dt = 1.0e-4;
112103
_performAnalyses=true;
113104
_writeToStorage=true;
114105
_tArray.setSize(0);
115-
_system = 0;
116106
_dtArray.setSize(0);
117107
}
118-
//_____________________________________________________________________________
119-
/**
120-
* Construct the states.
121-
*/
122-
bool Manager::
123-
constructStates()
124-
{
125-
return(true);
126-
}
127108

128109
//_____________________________________________________________________________
129110
/**
@@ -510,9 +491,6 @@ setModel(Model& aModel)
510491
}
511492

512493
_model = &aModel;
513-
514-
// STATES
515-
constructStates();
516494

517495
// STORAGE
518496
constructStorage();
@@ -599,33 +577,6 @@ getFinalTime() const
599577
return(_tf);
600578
}
601579

602-
//-----------------------------------------------------------------------------
603-
// FIRST DT
604-
//-----------------------------------------------------------------------------
605-
//_____________________________________________________________________________
606-
/**
607-
* Set the first time step taken in an integration.
608-
*
609-
* @param aDT First integration time step.
610-
*/
611-
void Manager::
612-
setFirstDT(double aDT)
613-
{
614-
_firstDT = aDT;
615-
if(_firstDT<1.0e-8) _firstDT = 1.0e-8;
616-
}
617-
//_____________________________________________________________________________
618-
/**
619-
* Get the first time step taken in an integration.
620-
*
621-
* @return First integration time step.
622-
*/
623-
double Manager::
624-
getFirstDT() const
625-
{
626-
return(_firstDT);
627-
}
628-
629580

630581
//=============================================================================
631582
// EXECUTION
@@ -680,19 +631,19 @@ hasStateStorage() const
680631
* the model.
681632
*/
682633
bool Manager::
683-
integrate( SimTK::State& s, double dtFirst )
634+
integrate(SimTK::State& s)
684635
{
685636

686637
int step = 0;
687638

688639
s.setTime( _ti );
689640

690641
// INTEGRATE
691-
return(doIntegration(s, step, dtFirst));
642+
return doIntegration(s, step);
692643

693644
}
694645

695-
bool Manager::doIntegration(SimTK::State& s, int step, double dtFirst ) {
646+
bool Manager::doIntegration(SimTK::State& s, int step) {
696647

697648
if(!_integ) {
698649
throw Exception("Manager::doIntegration(): "
@@ -704,11 +655,8 @@ bool Manager::doIntegration(SimTK::State& s, int step, double dtFirst ) {
704655
// Halts must arrive during an integration.
705656
clearHalt();
706657

707-
double dt/*,dtPrev*/,tReal;
658+
double tReal;
708659
double time =_ti;
709-
dt=dtFirst;
710-
if(dt>_dtMax) dt = _dtMax;
711-
//dtPrev=dt;
712660

713661
// CHECK SPECIFIED DT STEPPING
714662

@@ -739,34 +687,21 @@ bool Manager::doIntegration(SimTK::State& s, int step, double dtFirst ) {
739687
double fixedStepSize;
740688
if( _constantDT || _specifiedDT) fixedStep = true;
741689

742-
// If _system is has been set we should be integrating a CMC system
743-
// not the model's system.
744-
const SimTK::System& sys = _system ? *_system
745-
: _model->getMultibodySystem();
746-
747690
// Only initialize a TimeStepper if it hasn't been done yet
748-
if (_timeStepper == NULL) initializeTimeStepper(sys, s);
691+
if (_timeStepper == NULL) initializeTimeStepper(s);
749692

750693
SimTK::Integrator::SuccessfulStepStatus status;
751694

752-
if( fixedStep ) {
753-
dt = getFixedStepSize(getTimeArrayStep(_ti));
754-
} else {
695+
if( !fixedStep ) {
755696
_integ->setReturnEveryInternalStep(true);
756697
}
757698

758-
if( s.getTime()+dt >= _tf ) dt = _tf - s.getTime();
759-
760-
// We need to be at a valid stage to initialize the controls, but only when
761-
// we are integrating the complete model system, not the CMC system. This
762-
// is very ugly and a cleaner solution is required- aseth
763-
if(_system == NULL)
764-
sys.realize(s, SimTK::Stage::Velocity); // this is multibody system
765-
initialize(s, dt);
699+
_model->realizeVelocity(s);
700+
initializeStorageAndAnalyses(s);
766701

767702
if( fixedStep){
768703
s.updTime() = time;
769-
sys.realize(s, SimTK::Stage::Acceleration);
704+
_model->realizeAcceleration(s);
770705

771706
if(_performAnalyses)_model->updAnalysisSet().step(s, step);
772707
tReal = s.getTime();
@@ -849,9 +784,8 @@ double Manager::getFixedStepSize(int tArrayStep) const {
849784
*
850785
* @param s system state before integration
851786
*/
852-
void Manager::initialize(SimTK::State& s, double dt )
787+
void Manager::initializeStorageAndAnalyses(SimTK::State& s)
853788
{
854-
// skip initializations for CMC's actuator system
855789
if( _writeToStorage && _performAnalyses ) {
856790

857791
double tReal = s.getTime();
@@ -882,11 +816,11 @@ void Manager::initialize(SimTK::State& s, double dt )
882816
/**
883817
* set and initialize a SimTK::TimeStepper
884818
*/
885-
void Manager::initializeTimeStepper(
886-
const SimTK::System& sys, const SimTK::State& state)
819+
void Manager::initializeTimeStepper(const SimTK::State& s)
887820
{
888-
_timeStepper.reset(new SimTK::TimeStepper(sys, *_integ));
889-
_timeStepper->initialize(state);
821+
_timeStepper.reset(
822+
new SimTK::TimeStepper(_model->getMultibodySystem(), *_integ));
823+
_timeStepper->initialize(s);
890824
_timeStepper->setReportAllSignificantStates(true);
891825
}
892826

OpenSim/Simulation/Manager/Manager.h

Lines changed: 45 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -95,38 +95,28 @@ class OSIMSIMULATION_API Manager
9595
double _ti;
9696
/** Final time of the simulation. */
9797
double _tf;
98-
/** First dt in an integration. */
99-
double _firstDT;
10098

10199
/** Storage for the states. */
102100
std::unique_ptr<Storage> _stateStore;
103101

104-
int _steps;
105-
/** Number of integration step tries. */
106-
int _trys;
107-
/** Maximum number of steps in an integration. */
108-
int _maxSteps;
109-
/** Flag for signaling a desired halt. */
110-
bool _halt;
111-
/** Minimum step size. */
112-
double _dtMin;
113-
/** Maximum step size. */
114-
double _dtMax;
115-
/** Flag to indicate whether or not specified integration time steps
116-
should be used. The specified integration time steps are held in _tVec.
117-
If _tVec does not contain time steps appropriate for the integration,
118-
an exception is thrown. */
119-
bool _specifiedDT;
120-
/** Flag to indicate whether or not constant (fixed) integration time
121-
steps should be used. The constant integration time step is set using
122-
setDT(). */
123-
bool _constantDT;
124-
/** Constant integration time step. */
125-
double _dt;
126-
/** Vector of integration time steps. */
127-
Array<double> _tArray;
128-
/** Vector of integration time step deltas. */
129-
Array<double> _dtArray;
102+
/** Flag for signaling a desired halt. */
103+
bool _halt;
104+
105+
/** Flag to indicate whether or not specified integration time steps
106+
should be used. The specified integration time steps are held in _tVec.
107+
If _tVec does not contain time steps appropriate for the integration,
108+
an exception is thrown. */
109+
bool _specifiedDT;
110+
/** Flag to indicate whether or not constant (fixed) integration time
111+
steps should be used. The constant integration time step is set using
112+
setDT(). */
113+
bool _constantDT;
114+
/** Constant integration time step. */
115+
double _dt;
116+
/** Vector of integration time steps. */
117+
Array<double> _tArray;
118+
/** Vector of integration time step deltas. */
119+
Array<double> _dtArray;
130120

131121
/** Name to be shown by the UI */
132122
static std::string _displayName;
@@ -140,9 +130,6 @@ class OSIMSIMULATION_API Manager
140130
/** controllerSet used for the integration */
141131
ControllerSet* _controllerSet;
142132

143-
/** system of equations to be integrated */
144-
const SimTK::System* _system;
145-
146133

147134
//=============================================================================
148135
// METHODS
@@ -170,7 +157,6 @@ class OSIMSIMULATION_API Manager
170157

171158
private:
172159
void setNull();
173-
bool constructStates();
174160
bool constructStorage();
175161
//--------------------------------------------------------------------------
176162
// GET AND SET
@@ -198,41 +184,33 @@ class OSIMSIMULATION_API Manager
198184
double getInitialTime() const;
199185
void setFinalTime(double aTF);
200186
double getFinalTime() const;
201-
void setFirstDT(double aDT);
202-
double getFirstDT() const;
203-
// SPECIFIED TIME STEP
204-
void setUseSpecifiedDT(bool aTrueFalse);
205-
bool getUseSpecifiedDT() const;
206-
// CONSTANT TIME STEP
207-
void setUseConstantDT(bool aTrueFalse);
208-
bool getUseConstantDT() const;
209-
// DT VECTOR
210-
const Array<double>& getDTArray();
211-
void setDTArray(const SimTK::Vector_<double>& aDT, double aTI = 0.0);
212-
double getDTArrayDT(int aStep);
213-
void printDTArray(const char *aFileName=NULL);
214-
// TIME VECTOR
215-
const Array<double>& getTimeArray();
216-
double getTimeArrayTime(int aStep);
217-
int getTimeArrayStep(double aTime);
218-
void printTimeArray(const char *aFileName=NULL);
219-
void resetTimeAndDTArrays(double aTime);
220-
221-
double getNextTimeArrayTime(double aTime);
222-
223-
224-
// SYSTEM
225-
// only called when need to integrate a different set of equations
226-
// then what is defined by the model
227-
void setSystem(SimTK::System* system) { _system = system; }
187+
// SPECIFIED TIME STEP
188+
void setUseSpecifiedDT(bool aTrueFalse);
189+
bool getUseSpecifiedDT() const;
190+
// CONSTANT TIME STEP
191+
void setUseConstantDT(bool aTrueFalse);
192+
bool getUseConstantDT() const;
193+
// DT VECTOR
194+
const Array<double>& getDTArray();
195+
void setDTArray(const SimTK::Vector_<double>& aDT, double aTI = 0.0);
196+
double getDTArrayDT(int aStep);
197+
void printDTArray(const char *aFileName=NULL);
198+
// TIME VECTOR
199+
const Array<double>& getTimeArray();
200+
double getTimeArrayTime(int aStep);
201+
int getTimeArrayStep(double aTime);
202+
void printTimeArray(const char *aFileName=NULL);
203+
void resetTimeAndDTArrays(double aTime);
204+
double getNextTimeArrayTime(double aTime);
205+
206+
228207

229208
//--------------------------------------------------------------------------
230209
// EXECUTION
231210
//--------------------------------------------------------------------------
232-
bool integrate( SimTK::State& s, double dtFirst=1.0e-6 );
233-
bool doIntegration( SimTK::State& s, int step, double dtFirst );
234-
void initialize(SimTK::State& s, double dt);
235-
void finalize( SimTK::State& s);
211+
bool integrate(SimTK::State& s);
212+
bool doIntegration(SimTK::State& s, int step);
213+
void finalize(SimTK::State& s);
236214
double getFixedStepSize(int tArrayStep) const;
237215

238216
// STATE STORAGE
@@ -254,7 +232,10 @@ class OSIMSIMULATION_API Manager
254232

255233
// Handles common tasks of some of the other constructors.
256234
Manager(Model& aModel, bool dummyVar);
257-
void initializeTimeStepper(const SimTK::System& sys, const SimTK::State& state);
235+
236+
// Helper functions during initialization of integration
237+
void initializeStorageAndAnalyses(SimTK::State& s);
238+
void initializeTimeStepper(const SimTK::State& s);
258239

259240
//=============================================================================
260241
}; // END of class Manager

OpenSim/Simulation/Test/testMuscleMetabolicsProbes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ Storage simulateModel(Model& model, double t0, double t1)
644644
// Simulate.
645645
const clock_t tStart = clock();
646646
cout << "- integrating from " << t0 << " to " << t1 << "s" << endl;
647-
manager.integrate(state, 1.0e-3);
647+
manager.integrate(state);
648648
cout << "- simulation complete (" << (double)(clock()-tStart)/CLOCKS_PER_SEC
649649
<< " seconds elapsed)" << endl;
650650

OpenSim/Tools/ForwardTool.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ bool ForwardTool::run()
275275
// Initialize integrator
276276
integrator.setInternalStepLimit(_maxSteps);
277277
integrator.setMaximumStepSize(_maxDT);
278+
integrator.setMinimumStepSize(_minDT);
278279
integrator.setAccuracy(_errorTolerance);
279280

280281

OpenSim/Tools/VectorFunctionForActuators.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include "VectorFunctionForActuators.h"
2929
#include <OpenSim/Simulation/Model/CMCActuatorSubsystem.h>
3030
#include <OpenSim/Simulation/Model/Model.h>
31-
#include <OpenSim/Simulation/Manager/Manager.h>
3231
#include "CMC.h"
3332

3433

@@ -258,23 +257,14 @@ evaluate( const SimTK::State& s, double *aX, double *rF)
258257

259258
// create a Manager that will integrate just the actuator subsystem and use only the
260259
// CMC controller
261-
262-
Manager manager(*_model, *_integrator);
263-
manager.setInitialTime(_ti);
264-
manager.setFinalTime(_tf);
265-
manager.setSystem( _CMCActuatorSystem );
266-
// tell the manager to not call the analyses or write to storage
267-
// while the CMCSubsystem is being integrated.
268-
manager.setPerformAnalyses(false);
269-
manager.setWriteToStorage(false);
270260
SimTK::State& actSysState = _CMCActuatorSystem->updDefaultState();
271261
getCMCActSubsys()->updZ(actSysState) = _model->getMultibodySystem()
272262
.getDefaultSubsystem().getZ(s);
273-
274263
actSysState.setTime(_ti);
275264

276-
// Integration
277-
manager.integrate(actSysState, 0.000001);
265+
SimTK::TimeStepper ts(*_CMCActuatorSystem, *_integrator);
266+
ts.initialize(actSysState);
267+
ts.stepTo(_tf);
278268

279269
const Set<const Actuator>& forceSet = controller.getActuatorSet();
280270
// Vector function values

0 commit comments

Comments
 (0)