1 #ifndef SimTK_SimTKCOMMON_STATE_IMPL_H_     2 #define SimTK_SimTKCOMMON_STATE_IMPL_H_    61 class DiscreteVarInfo {
    64     :   allocationStage(Stage::Empty), invalidatedStage(Stage::Empty),
    65         value(0), timeLastUpdated(
NaN) {}
    67     DiscreteVarInfo(Stage allocation, Stage invalidated, AbstractValue* v)
    68     :   allocationStage(allocation), invalidatedStage(invalidated),
    69         autoUpdateEntry(), value(v), timeLastUpdated(
NaN) 
    70     {   assert(isReasonable()); }
    77     DiscreteVarInfo& deepAssign(
const DiscreteVarInfo& src) {
    78         assert(src.isReasonable());
    80         allocationStage   = src.allocationStage;
    81         invalidatedStage  = src.invalidatedStage;
    82         autoUpdateEntry   = src.autoUpdateEntry;
    83         if (value) *value = *src.value;
    84         else        value = src.value->clone();
    85         timeLastUpdated   = src.timeLastUpdated;
    90     void deepDestruct() {
delete value; value=0;}
    91     const Stage& getAllocationStage()
  const {
return allocationStage;}
    94     void swapValue(
Real updTime, AbstractValue*& other) 
    95     {   std::swap(value, other); timeLastUpdated=updTime; }
    97     const AbstractValue& getValue()
 const {assert(value); 
return *value;}
    98     Real                 getTimeLastUpdated()
 const     99     {   assert(value); 
return timeLastUpdated; }
   100     AbstractValue&       updValue(
Real updTime)
   101     {   assert(value); timeLastUpdated=updTime; 
return *value; }
   103     const Stage&    getInvalidatedStage()
 const {
return invalidatedStage;}
   104     CacheEntryIndex getAutoUpdateEntry()
  const {
return autoUpdateEntry;}
   105     void setAutoUpdateEntry(CacheEntryIndex cx) {autoUpdateEntry = cx;}
   109     Stage           allocationStage;
   110     Stage           invalidatedStage;
   111     CacheEntryIndex autoUpdateEntry;
   114     AbstractValue*  value;
   115     Real            timeLastUpdated;
   117     bool isReasonable()
 const   120              && (invalidatedStage > allocationStage)
   129 class CacheEntryInfo {
   132     :   allocationStage(Stage::Empty), dependsOnStage(Stage::Empty), 
   133         computedByStage(Stage::Empty), value(0), versionWhenLastComputed(-1) {}
   136        (Stage allocation, Stage dependsOn, Stage computedBy, AbstractValue* v)
   137     :   allocationStage(allocation), dependsOnStage(dependsOn), 
   138         computedByStage(computedBy), value(v), versionWhenLastComputed(0) 
   139     {   assert(isReasonable()); }
   141     bool isCurrent(
const Stage& current, 
const StageVersion versions[])
 const    142     {   
if (current >= computedByStage) 
return true;
   143         if (current <  dependsOnStage)  
return false;
   144         return versions[dependsOnStage] == versionWhenLastComputed;}
   147     {   
return versionWhenLastComputed; }
   151     void invalidate() {versionWhenLastComputed = 0;}
   153     {   versionWhenLastComputed = versions[dependsOnStage];}
   158     CacheEntryInfo& deepAssign(
const CacheEntryInfo& src) {
   159         assert(src.isReasonable());
   161         allocationStage   = src.allocationStage;
   162         dependsOnStage    = src.dependsOnStage;
   163         computedByStage   = src.computedByStage;
   164         associatedVar     = src.associatedVar;
   165         if (value) *value = *src.value;
   166         else        value = src.value->clone();
   167         versionWhenLastComputed = src.versionWhenLastComputed;
   172     void deepDestruct() {
delete value; value=0;}
   173     const Stage& getAllocationStage()
 const {
return allocationStage;}
   178     void swapValue(
Real updTime, DiscreteVarInfo& dv) 
   179     {   dv.swapValue(updTime, value); }
   180     const AbstractValue& getValue()
 const {assert(value); 
return *value;}
   181     AbstractValue&       updValue()       {assert(value); 
return *value;}
   183     const Stage&          getDependsOnStage()
  const {
return dependsOnStage;}
   184     const Stage&          getComputedByStage()
 const {
return computedByStage;}
   185     DiscreteVariableIndex getAssociatedVar()
   const {
return associatedVar;}
   186     void setAssociatedVar(DiscreteVariableIndex dx)  {associatedVar=dx;}
   189     Stage                   allocationStage;
   190     Stage                   dependsOnStage;
   191     Stage                   computedByStage;
   192     DiscreteVariableIndex   associatedVar;  
   195     AbstractValue*          value;
   198     bool isReasonable()
 const   202              && (computedByStage >= dependsOnStage)
   204              && (versionWhenLastComputed >= 0); }
   215     :   allocationStage(Stage::Empty), firstIndex(-1), nslots(0) {}
   217     TriggerInfo(Stage allocation, 
int index, 
int n)
   218     :   allocationStage(allocation), firstIndex(index), nslots(n)
   219     {   assert(isReasonable()); assert(n>0);}
   224     int getFirstIndex()
 const {
return firstIndex;}
   225     int getNumSlots()
 const {
return nslots;}
   228     TriggerInfo& deepAssign(
const TriggerInfo& src) 
   229     {   
return operator=(src); }
   230     void         deepDestruct() {}
   231     const Stage& getAllocationStage()
 const {
return allocationStage;}
   234     Stage                   allocationStage;
   238     bool isReasonable()
 const {
   258 class ContinuousVarInfo {
   260     ContinuousVarInfo() : allocationStage(Stage::Empty), firstIndex(-1) {}
   262     ContinuousVarInfo(Stage         allocation, 
   266     :   allocationStage(allocation), firstIndex(index), initialValues(initVals)
   267     {   assert(isReasonable());
   268         assert(varWeights.size()==0 || varWeights.size()==initVals.size());
   269         assert(weightsAreOK(varWeights));
   270         if (varWeights.size()) weights=varWeights;
   271         else weights=
Vector(initVals.size(), 
Real(1));
   274     int getFirstIndex()
 const {
return firstIndex;}
   275     int getNumVars()
 const {
return initialValues.
size();}
   276     const Vector& getInitialValues()
 const {
return initialValues;}
   277     const Vector& getWeights()
 const {
return weights;}
   283     ContinuousVarInfo& deepAssign(
const ContinuousVarInfo& src) 
   284     {   
return operator=(src); }
   285     void               deepDestruct() {}
   286     const Stage&       getAllocationStage()
 const {
return allocationStage;}
   289     Stage     allocationStage;
   294     static bool weightsAreOK(
const Vector& wts) {
   295         for (
int i=0; i<wts.size(); ++i)
   296             if (wts[i] <= 0) 
return false;
   300     bool isReasonable()
 const {
   310 class ConstraintErrInfo {
   312     ConstraintErrInfo() : allocationStage(Stage::Empty), firstIndex(-1) {}
   314     ConstraintErrInfo(Stage         allocation,
   318     :   allocationStage(allocation), firstIndex(index)
   319     {   assert(isReasonable());
   320         assert(varWeights.size()==0 || varWeights.size()==nerr);
   321         assert(weightsAreOK(varWeights));
   322         if (varWeights.size()) weights=varWeights;
   326     int getFirstIndex()
 const {
return firstIndex;}
   327     int getNumErrs()
 const {
return weights.size();}
   328     const Vector& getWeights()
 const {
return weights;}
   334     ConstraintErrInfo& deepAssign(
const ConstraintErrInfo& src) 
   335     {   
return operator=(src); }
   336     void               deepDestruct() {}
   337     const Stage&       getAllocationStage()
 const {
return allocationStage;}
   340     Stage     allocationStage;
   344     static bool weightsAreOK(
const Vector& wts) {
   345         for (
int i=0; i<wts.size(); ++i)
   346             if (wts[i] <= 0) 
return false;
   350     bool isReasonable()
 const {
   364 class PerSubsystemInfo {
   366     PerSubsystemInfo() : currentStage(Stage::Empty)     {initialize();}
   367     PerSubsystemInfo(
const String& n, 
const String& v) 
   368       : name(n), version(v), currentStage(Stage::Empty) {initialize();}
   373     ~PerSubsystemInfo() {
   381     PerSubsystemInfo(
const PerSubsystemInfo& src) : currentStage(Stage::Empty) {
   386     PerSubsystemInfo& operator=(
const PerSubsystemInfo& src) {
   400     void invalidateStageJustThisSubsystem(Stage g) {
   402         restoreToStage(g.prev());
   408     void advanceToStage(Stage g)
 const {
   410         assert(currentStage == g.prev());
   417     void clearReferencesToModelStageGlobals() {
   418         qstart.invalidate(); ustart.invalidate(); 
   420         q.clear(); u.clear(); z.clear();
   421         uWeights.clear(); zWeights.clear();
   422         qdot.clear(); udot.clear(); zdot.clear(); qdotdot.clear();
   425     void clearReferencesToInstanceStageGlobals() {
   427         qerrWeights.clear(); uerrWeights.clear();
   430         qerrstart.invalidate();uerrstart.invalidate();udoterrstart.invalidate();
   431         qerr.clear();uerr.clear();udoterr.clear();multipliers.clear();
   434             triggerstart[j].invalidate();
   439     QIndex getNextQIndex()
 const {
   440         if (qInfo.empty()) 
return QIndex(0);
   441         const ContinuousVarInfo& last = qInfo.back();
   442         return QIndex(last.getFirstIndex()+last.getNumVars());
   444     UIndex getNextUIndex()
 const {
   445         if (uInfo.empty()) 
return UIndex(0);
   446         const ContinuousVarInfo& last = uInfo.back();
   447         return UIndex(last.getFirstIndex()+last.getNumVars());
   449     ZIndex getNextZIndex()
 const {
   450         if (zInfo.empty()) 
return ZIndex(0);
   451         const ContinuousVarInfo& last = zInfo.back();
   452         return ZIndex(last.getFirstIndex()+last.getNumVars());
   455     QErrIndex getNextQErrIndex()
 const {
   456         if (qerrInfo.empty()) 
return QErrIndex(0);
   457         const ConstraintErrInfo& last = qerrInfo.back();
   458         return QErrIndex(last.getFirstIndex()+last.getNumErrs());
   460     UErrIndex getNextUErrIndex()
 const {
   461         if (uerrInfo.empty()) 
return UErrIndex(0);
   462         const ConstraintErrInfo& last = uerrInfo.back();
   463         return UErrIndex(last.getFirstIndex()+last.getNumErrs());
   465     UDotErrIndex getNextUDotErrIndex()
 const {
   466         if (udoterrInfo.empty()) 
return UDotErrIndex(0);
   467         const ConstraintErrInfo& last = udoterrInfo.back();
   468         return UDotErrIndex(last.getFirstIndex()+last.getNumErrs());
   470     DiscreteVariableIndex getNextDiscreteVariableIndex()
 const {
   471         return DiscreteVariableIndex(discreteInfo.size());
   473     CacheEntryIndex getNextCacheEntryIndex()
 const {
   474         return CacheEntryIndex(cacheInfo.size());
   476     EventTriggerByStageIndex getNextEventTriggerByStageIndex(Stage g)
 const {
   477         if (triggerInfo[g].empty()) 
return EventTriggerByStageIndex(0);
   478         const TriggerInfo& last = triggerInfo[g].back();
   479         return EventTriggerByStageIndex
   480             (last.getFirstIndex()+last.getNumSlots());
   502     Array_<ContinuousVarInfo>      qInfo, uInfo, zInfo;
   503     Array_<DiscreteVarInfo>        discreteInfo;
   506     mutable Array_<ConstraintErrInfo>   qerrInfo, uerrInfo, udoterrInfo;
   508     mutable Array_<CacheEntryInfo>      cacheInfo;
   524     Vector          uWeights, zWeights;
   526     mutable Vector  qdot, udot, zdot, qdotdot;
   529     Vector          qerrWeights, uerrWeights;
   532     mutable SystemQErrIndex                 qerrstart;
   533     mutable SystemUErrIndex                 uerrstart;
   534     mutable SystemUDotErrIndex              udoterrstart;
   535     mutable SystemEventTriggerByStageIndex  triggerstart[
Stage::NValid];
   537     mutable Vector  qerr, uerr;
   539     mutable Vector  udoterr, multipliers; 
   552     mutable Stage        currentStage;
   560         qstart.invalidate();ustart.invalidate();zstart.invalidate();
   561         qerrstart.invalidate();uerrstart.invalidate();udoterrstart.invalidate();
   563             triggerstart[j].invalidate();
   564             stageVersions[j] = 1; 
   571     void clearContinuousVars();
   572     void clearConstraintErrs();
   573     void clearDiscreteVars();
   574     void clearEventTriggers(
int g);
   577     void clearAllStacks();
   579     void popContinuousVarsBackToStage(
const Stage& g);
   580     void popDiscreteVarsBackToStage(
const Stage& g);
   581     void popConstraintErrsBackToStage(
const Stage& g);
   582     void popCacheBackToStage(
const Stage& g);
   583     void popEventTriggersBackToStage(
const Stage& g);
   585     void popAllStacksBackToStage(
const Stage& g);
   588     void copyContinuousVarInfoThroughStage
   589        (
const Array_<ContinuousVarInfo>& src, 
const Stage& g,
   590         Array_<ContinuousVarInfo>& dest);
   592     void copyDiscreteVarsThroughStage
   593        (
const Array_<DiscreteVarInfo>& src, 
const Stage& g);
   596     void copyConstraintErrInfoThroughStage
   597        (
const Array_<ConstraintErrInfo>& src, 
const Stage& g,
   598         Array_<ConstraintErrInfo>& dest);
   600     void copyCacheThroughStage
   601        (
const Array_<CacheEntryInfo>& src, 
const Stage& g);
   603     void copyEventsThroughStage
   604        (
const Array_<TriggerInfo>& src, 
const Stage& g,
   605         Array_<TriggerInfo>& dest);
   607     void copyAllStacksThroughStage(
const PerSubsystemInfo& src, 
const Stage& g);
   612     void restoreToStage(Stage g);
   620     void copyFrom(
const PerSubsystemInfo& src, Stage maxStage);
   631     :   t(
NaN), currentSystemStage(Stage::Empty) {initializeStageVersions();} 
   635     StateImpl(
const StateImpl& src);
   637     StateImpl& operator=(
const StateImpl& src);
   642     StateImpl* clone()
 const {
return new StateImpl(*
this);}
   644     const Stage& getSystemStage()
 const {
return currentSystemStage;}
   645     Stage&       updSystemStage()
 const {
return currentSystemStage;} 
   648     const PerSubsystemInfo& getSubsystem(
int subsystem)
 const {
   650                          "StateImpl::getSubsystem()");
   651         return subsystems[subsystem];
   654     PerSubsystemInfo& updSubsystem(
int subsystem) {
   656                          "StateImpl::updSubsystem()");
   657         return subsystems[subsystem];
   660     const Stage& getSubsystemStage(
int subsystem)
 const {
   661         return subsystems[subsystem].currentStage;
   663     Stage& updSubsystemStage(
int subsystem)
 const {
   664         return subsystems[subsystem].currentStage; 
   667     const StageVersion* getSubsystemStageVersions(
int subsystem)
 const {
   668         return subsystems[subsystem].stageVersions;
   678     void invalidateJustSystemStage(Stage stg);
   684     void advanceSystemToStage(Stage stg) 
const;
   686     void setNumSubsystems(
int i) {
   689         subsystems.resize(i);
   692     void initializeSubsystem
   693        (SubsystemIndex i, 
const String& name, 
const String& version) {
   694         updSubsystem(i).name = name;
   695         updSubsystem(i).version = version;
   698     SubsystemIndex addSubsystem(
const String& name, 
const String& version) {
   699         const SubsystemIndex sx(subsystems.size());
   700         subsystems.push_back(PerSubsystemInfo(name,version));
   704     int getNumSubsystems()
 const {
return (
int)subsystems.size();}
   706     const String& getSubsystemName(SubsystemIndex subsys)
 const {
   707         return subsystems[subsys].name;
   709     const String& getSubsystemVersion(SubsystemIndex subsys)
 const {
   710         return subsystems[subsys].version;
   715     void invalidateAll(Stage g) {
   716         invalidateJustSystemStage(g);
   717         for (SubsystemIndex i(0); i<(int)subsystems.size(); ++i)
   718             subsystems[i].invalidateStageJustThisSubsystem(g);
   724     void invalidateAllCacheAtOrAbove(Stage g)
 const {
   726             "StateImpl::invalidateAllCacheAtOrAbove()");
   730         StateImpl* mthis = 
const_cast<StateImpl*
>(
this);
   731         mthis->invalidateJustSystemStage(g);
   732         for (SubsystemIndex i(0); i<(int)subsystems.size(); ++i)
   733             mthis->subsystems[i].invalidateStageJustThisSubsystem(g);
   738     void advanceSubsystemToStage(SubsystemIndex subsys, Stage g)
 const {
   739         subsystems[subsys].advanceToStage(g);
   747     QIndex allocateQ(SubsystemIndex subsys, 
const Vector& qInit) {
   749                                    "StateImpl::allocateQ()");
   751         const Stage allocStage = getSubsystemStage(subsys).next();
   752         PerSubsystemInfo& ss = subsystems[subsys];
   753         const QIndex nxt(ss.getNextQIndex());
   754         ss.qInfo.push_back(ContinuousVarInfo(allocStage,nxt,qInit,
Vector())); 
   758     UIndex allocateU(SubsystemIndex subsys, 
const Vector& uInit) {
   760                                    "StateImpl::allocateU()");
   761         const Stage allocStage = getSubsystemStage(subsys).next();
   762         PerSubsystemInfo& ss = subsystems[subsys];
   763         const UIndex nxt(ss.getNextUIndex());
   764         ss.uInfo.push_back(ContinuousVarInfo(allocStage,nxt,uInit,
Vector())); 
   767     ZIndex allocateZ(SubsystemIndex subsys, 
const Vector& zInit) {
   769                                    "StateImpl::allocateZ()");
   770         const Stage allocStage = getSubsystemStage(subsys).next();
   771         PerSubsystemInfo& ss = subsystems[subsys];
   772         const ZIndex nxt(ss.getNextZIndex());
   773         ss.zInfo.push_back(ContinuousVarInfo(allocStage,nxt,zInit,
Vector())); 
   777     QErrIndex allocateQErr(SubsystemIndex subsys, 
int nqerr)
 const {
   779                                    "StateImpl::allocateQErr()");
   780         const Stage allocStage = getSubsystemStage(subsys).next();
   781         const PerSubsystemInfo& ss = subsystems[subsys];
   782         const QErrIndex nxt(ss.getNextQErrIndex());
   783         ss.qerrInfo.push_back(ConstraintErrInfo(allocStage,nxt,nqerr,
Vector())); 
   786     UErrIndex allocateUErr(SubsystemIndex subsys, 
int nuerr)
 const {
   789         const Stage allocStage = getSubsystemStage(subsys).next();
   790         const PerSubsystemInfo& ss = subsystems[subsys];
   791         const UErrIndex nxt(ss.getNextUErrIndex());
   792         ss.uerrInfo.push_back(ConstraintErrInfo(allocStage,nxt,nuerr,
Vector())); 
   795     UDotErrIndex allocateUDotErr(SubsystemIndex subsys, 
int nudoterr)
 const {
   797                                    "StateImpl::allocateUDotErr()");
   798         const Stage allocStage = getSubsystemStage(subsys).next();
   799         const PerSubsystemInfo& ss = subsystems[subsys];
   800         const UDotErrIndex nxt(ss.getNextUDotErrIndex());
   801         ss.udoterrInfo.push_back
   802            (ConstraintErrInfo(allocStage,nxt,nudoterr,
Vector())); 
   805     EventTriggerByStageIndex allocateEventTrigger
   806        (SubsystemIndex subsys, Stage g, 
int nt)
 const {
   808                                    "StateImpl::allocateEventTrigger()");
   809         const Stage allocStage = getSubsystemStage(subsys).next();
   810         const PerSubsystemInfo& ss = subsystems[subsys];
   811         const EventTriggerByStageIndex 
   812             nxt(ss.getNextEventTriggerByStageIndex(g));
   813         ss.triggerInfo[g].push_back(TriggerInfo(allocStage,nxt,nt)); 
   820     DiscreteVariableIndex allocateDiscreteVariable
   821        (SubsystemIndex subsys, Stage invalidates, AbstractValue* vp) 
   825             "StateImpl::allocateDiscreteVariable()");
   827         const Stage maxAcceptable = (invalidates <= 
Stage::Model    830             maxAcceptable.next(), 
"StateImpl::allocateDiscreteVariable()");
   832         const Stage allocStage = getSubsystemStage(subsys).next();    
   833         PerSubsystemInfo& ss = subsystems[subsys];
   834         const DiscreteVariableIndex nxt(ss.getNextDiscreteVariableIndex());
   835         ss.discreteInfo.push_back
   836            (DiscreteVarInfo(allocStage,invalidates,vp));
   841     CacheEntryIndex allocateCacheEntry
   842        (SubsystemIndex subsys, Stage dependsOn, Stage computedBy,
   843         AbstractValue* vp)
 const    847             "StateImpl::allocateCacheEntry()");
   849             "StateImpl::allocateCacheEntry()");
   853         const Stage allocStage = getSubsystemStage(subsys).next();    
   854         const PerSubsystemInfo& ss = subsystems[subsys];
   855         const CacheEntryIndex nxt(ss.getNextCacheEntryIndex());
   856         ss.cacheInfo.push_back
   857            (CacheEntryInfo(allocStage, dependsOn, computedBy, vp)); 
   863     DiscreteVariableIndex allocateAutoUpdateDiscreteVariable
   864        (SubsystemIndex subsys, Stage invalidates, AbstractValue* vp,
   865         Stage updateDependsOn)
   867         const DiscreteVariableIndex dx = 
   868             allocateDiscreteVariable(subsys,invalidates,vp->clone());
   869         const CacheEntryIndex       cx = 
   872         PerSubsystemInfo& ss = subsystems[subsys];
   873         DiscreteVarInfo& dvinfo = ss.discreteInfo[dx];
   874         CacheEntryInfo&  ceinfo = ss.cacheInfo[cx];
   875         dvinfo.setAutoUpdateEntry(cx);
   876         ceinfo.setAssociatedVar(dx);
   884                             "StateImpl::getNY()");
   888     SystemYIndex getQStart()
 const {
   890                             "StateImpl::getQStart()");
   891         return SystemYIndex(0); 
   895                             "StateImpl::getNQ()");
   899     SystemYIndex getUStart()
 const {
   901                             "StateImpl::getUStart()");
   902         return SystemYIndex(q.size()); 
   906                             "StateImpl::getNU()");
   910     SystemYIndex getZStart()
 const {
   912                             "StateImpl::getZStart()");
   913         return SystemYIndex(q.size() + u.size()); 
   917                             "StateImpl::getNZ()");
   921     int getNYErr()
 const {
   923                             "StateImpl::getNYErr()");
   927     SystemYErrIndex getQErrStart()
 const {
   929                             "StateImpl::getQErrStart()");
   930         return SystemYErrIndex(0); 
   932     int getNQErr()
 const {
   934                             "StateImpl::getNQErr()");
   938     SystemYErrIndex getUErrStart()
 const {
   940                             "StateImpl::getUErrStart()");
   941         return SystemYErrIndex(qerr.size()); 
   943     int getNUErr()
 const {
   945                             "StateImpl::getNUErr()");
   951     int getNUDotErr()
 const {
   953                             "StateImpl::getNUDotErr()");
   954         return udoterr.size();
   957     int getNEventTriggers()
 const {
   959                             "StateImpl::getNEventTriggers()");
   960         return allTriggers.size();
   963     SystemEventTriggerIndex getEventTriggerStartByStage(Stage g)
 const {
   965                             "StateImpl::getEventTriggerStartByStage()");
   967         for (
int j=0; j<g; ++j)
   968             nxt += triggers[j].size();
   969         return SystemEventTriggerIndex(nxt); 
   972     int getNEventTriggersByStage(Stage g)
 const {
   974                             "StateImpl::getNEventTriggersByStage()");
   975         return triggers[g].size();
   980     SystemQIndex getQStart(SubsystemIndex subsys)
 const {
   982                             "StateImpl::getQStart(subsys)");
   983         return getSubsystem(subsys).qstart;
   985     int getNQ(SubsystemIndex subsys)
 const {
   987                             "StateImpl::getNQ(subsys)");
   988         return getSubsystem(subsys).q.size();
   991     SystemUIndex getUStart(SubsystemIndex subsys)
 const {
   993                             "StateImpl::getUStart(subsys)");
   994         return getSubsystem(subsys).ustart;
   996     int getNU(SubsystemIndex subsys)
 const {
   998                             "StateImpl::getNU(subsys)");
   999         return getSubsystem(subsys).u.size();
  1002     SystemZIndex getZStart(SubsystemIndex subsys)
 const {
  1004                             "StateImpl::getZStart(subsys)");
  1005         return getSubsystem(subsys).zstart;
  1007     int getNZ(SubsystemIndex subsys)
 const {
  1009                             "StateImpl::getNZ(subsys)");
  1010         return getSubsystem(subsys).z.size();
  1013     SystemQErrIndex getQErrStart(SubsystemIndex subsys)
 const {
  1015                             "StateImpl::getQErrStart(subsys)");
  1016         return getSubsystem(subsys).qerrstart;
  1018     int getNQErr(SubsystemIndex subsys)
 const {
  1020                             "StateImpl::getNQErr(subsys)");
  1021         return getSubsystem(subsys).qerr.size();
  1024     SystemUErrIndex getUErrStart(SubsystemIndex subsys)
 const {
  1026                             "StateImpl::getUErrStart(subsys)");
  1027         return getSubsystem(subsys).uerrstart;
  1029     int getNUErr(SubsystemIndex subsys)
 const {
  1031                             "StateImpl::getNUErr(subsys)");
  1032         return getSubsystem(subsys).uerr.size();
  1036     SystemUDotErrIndex getUDotErrStart(SubsystemIndex subsys)
 const {
  1038                             "StateImpl::getUDotErrStart(subsys)");
  1039         return getSubsystem(subsys).udoterrstart;
  1041     int getNUDotErr(SubsystemIndex subsys)
 const {
  1043                             "StateImpl::getNUDotErr(subsys)");
  1044         return getSubsystem(subsys).udoterr.size();
  1047     SystemEventTriggerByStageIndex getEventTriggerStartByStage
  1048        (SubsystemIndex subsys, Stage g)
 const {
  1050                             "StateImpl::getEventTriggerStartByStage(subsys)");
  1051         return getSubsystem(subsys).triggerstart[g];
  1054     int getNEventTriggersByStage(SubsystemIndex subsys, Stage g)
 const {
  1056                             "StateImpl::getNEventTriggersByStage(subsys)");
  1057         return getSubsystem(subsys).triggers[g].size();
  1062     const Vector& getQ(SubsystemIndex subsys)
 const {
  1064                             "StateImpl::getQ(subsys)");
  1065         return getSubsystem(subsys).q;
  1067     const Vector& getU(SubsystemIndex subsys)
 const {
  1069                             "StateImpl::getU(subsys)");
  1070         return getSubsystem(subsys).u;
  1072     const Vector& getZ(SubsystemIndex subsys)
 const {
  1074                             "StateImpl::getZ(subsys)");
  1075         return getSubsystem(subsys).z;
  1078     const Vector& getUWeights(SubsystemIndex subsys)
 const {
  1080             "StateImpl::getUWeights(subsys)");
  1081         return getSubsystem(subsys).uWeights;
  1083     const Vector& getZWeights(SubsystemIndex subsys)
 const {
  1085             "StateImpl::getZWeights(subsys)");
  1086         return getSubsystem(subsys).zWeights;
  1089     const Vector& getQDot(SubsystemIndex subsys)
 const {
  1091                             "StateImpl::getQDot(subsys)");
  1093                             "StateImpl::getQDot(subsys)");
  1094         return getSubsystem(subsys).qdot;
  1096     const Vector& getUDot(SubsystemIndex subsys)
 const {
  1098                             "StateImpl::getUDot(subsys)");
  1100                             "StateImpl::getUDot(subsys)");
  1101         return getSubsystem(subsys).udot;
  1103     const Vector& getZDot(SubsystemIndex subsys)
 const {
  1105                             "StateImpl::getZDot(subsys)");
  1107                             "StateImpl::getZDot(subsys)");
  1108         return getSubsystem(subsys).zdot;
  1110     const Vector& getQDotDot(SubsystemIndex subsys)
 const {
  1112                             "StateImpl::getQDotDot(subsys)");
  1114                             "StateImpl::getQDotDot(subsys)");
  1115         return getSubsystem(subsys).qdotdot;
  1118     Vector& updQ(SubsystemIndex subsys) {
  1120                             "StateImpl::updQ(subsys)");
  1122         return updSubsystem(subsys).q;
  1124     Vector& updU(SubsystemIndex subsys) {
  1126                             "StateImpl::updU(subsys)");
  1128         return updSubsystem(subsys).u;
  1130     Vector& updZ(SubsystemIndex subsys) {
  1132                             "StateImpl::updZ(subsys)");
  1134         return updSubsystem(subsys).z;
  1137     Vector& updUWeights(SubsystemIndex subsys) {
  1139             "StateImpl::updUWeights(subsys)");
  1141         return updSubsystem(subsys).uWeights;
  1143     Vector& updZWeights(SubsystemIndex subsys) {
  1145             "StateImpl::updZWeights(subsys)");
  1147         return updSubsystem(subsys).zWeights;
  1152     Vector& updQDot(SubsystemIndex subsys)
 const {
  1154                             "StateImpl::updQDot(subsys)");
  1155         return getSubsystem(subsys).qdot;
  1157     Vector& updUDot(SubsystemIndex subsys)
 const {
  1159                             "StateImpl::updUDot(subsys)");
  1160         return getSubsystem(subsys).udot;
  1162     Vector& updZDot(SubsystemIndex subsys)
 const {
  1164                             "StateImpl::updZDot(subsys)");
  1165         return getSubsystem(subsys).zdot;
  1167     Vector& updQDotDot(SubsystemIndex subsys)
 const {
  1169                             "StateImpl::updQDotDot(subsys)");
  1170         return getSubsystem(subsys).qdotdot;
  1174     const Vector& getQErr(SubsystemIndex subsys)
 const {
  1176                             "StateImpl::getQErr(subsys)");
  1178                             "StateImpl::getQErr(subsys)");
  1179         return getSubsystem(subsys).qerr;
  1181     const Vector& getUErr(SubsystemIndex subsys)
 const {
  1183                             "StateImpl::getUErr(subsys)");
  1185                             "StateImpl::getUErr(subsys)");
  1186         return getSubsystem(subsys).uerr;
  1189     const Vector& getQErrWeights(SubsystemIndex subsys)
 const {
  1191             "StateImpl::getQErrWeights(subsys)");
  1192         return getSubsystem(subsys).qerrWeights;
  1194     const Vector& getUErrWeights(SubsystemIndex subsys)
 const {
  1196             "StateImpl::getUErrWeights(subsys)");
  1197         return getSubsystem(subsys).uerrWeights;
  1200     const Vector& getUDotErr(SubsystemIndex subsys)
 const {
  1202                             "StateImpl::getUDotErr(subsys)");
  1204                             "StateImpl::getUDotErr(subsys)");
  1205         return getSubsystem(subsys).udoterr;
  1207     const Vector& getMultipliers(SubsystemIndex subsys)
 const {
  1209                             "StateImpl::getMultipliers(subsys)");
  1211                             "StateImpl::getMultipliers(subsys)");
  1212         return getSubsystem(subsys).multipliers;
  1215     const Vector& getEventTriggersByStage(SubsystemIndex subsys, Stage g)
 const {
  1217                             "StateImpl::getEventTriggersByStage(subsys)");
  1219                             "StateImpl::getEventTriggersByStage(subsys)");
  1220         return getSubsystem(subsys).triggers[g];
  1223     Vector& updQErr(SubsystemIndex subsys)
 const {
  1225                             "StateImpl::updQErr(subsys)");
  1226         return getSubsystem(subsys).qerr;
  1228     Vector& updUErr(SubsystemIndex subsys)
 const {
  1230                             "StateImpl::updUErr(subsys)");
  1231         return getSubsystem(subsys).uerr;
  1234     Vector& updQErrWeights(SubsystemIndex subsys) {
  1236             "StateImpl::updQErrWeights(subsys)");
  1238         return updSubsystem(subsys).qerrWeights;
  1240     Vector& updUErrWeights(SubsystemIndex subsys) {
  1242             "StateImpl::updUErrWeights(subsys)");
  1244         return updSubsystem(subsys).uerrWeights;
  1247     Vector& updUDotErr(SubsystemIndex subsys)
 const {
  1249                             "StateImpl::updUDotErr(subsys)");
  1250         return getSubsystem(subsys).udoterr;
  1252     Vector& updMultipliers(SubsystemIndex subsys)
 const {
  1254                             "StateImpl::updMultipliers(subsys)");
  1255         return getSubsystem(subsys).multipliers;
  1257     Vector& updEventTriggersByStage(SubsystemIndex subsys, Stage g)
 const {
  1259                             "StateImpl::updEventTriggersByStage(subsys)");
  1260         return getSubsystem(subsys).triggers[g];
  1267     const Real& getTime()
 const {
  1269                             "StateImpl::getTime()");
  1273     const Vector& getY()
 const {
  1275                             "StateImpl::getY()");
  1279     const Vector& getQ()
 const {
  1281                             "StateImpl::getQ()");
  1285     const Vector& getU()
 const {
  1287                             "StateImpl::getU()");
  1291     const Vector& getZ()
 const {
  1293                             "StateImpl::getZ()");
  1297     const Vector& getUWeights()
 const {
  1299                             "StateImpl::getUWeights()");
  1303     const Vector& getZWeights()
 const {
  1305                             "StateImpl::getZWeights()");
  1313                             "StateImpl::updTime()");
  1320                             "StateImpl::updY()");
  1327                             "StateImpl::updQ()");
  1334                             "StateImpl::updU()");
  1341                             "StateImpl::updZ()");
  1348             "StateImpl::updUWeights()");
  1355             "StateImpl::updZWeights()");
  1361     const Vector& getYDot()
 const {
  1363                             "StateImpl::getYDot()");
  1367     const Vector& getQDot()
 const {
  1369                             "StateImpl::getQDot()");
  1373     const Vector& getZDot()
 const {
  1375                             "StateImpl::getZDot()");
  1379     const Vector& getUDot()
 const {
  1381                             "StateImpl::getUDot()");
  1385     const Vector& getQDotDot()
 const {
  1387                             "StateImpl::getQDotDot()");
  1392     Vector& updYDot()
 const {
  1394                             "StateImpl::updYDot()");
  1398     Vector& updQDot()
 const {
  1400                             "StateImpl::updQDot()");
  1404     Vector& updUDot()
 const {
  1406                             "StateImpl::updUDot()");
  1410     Vector& updZDot()
 const {
  1412                             "StateImpl::updZDot()");
  1416     Vector& updQDotDot()
 const {
  1418                             "StateImpl::updQDotDot()");
  1423     const Vector& getYErr()
 const {
  1425                             "StateImpl::getYErr()");
  1429     const Vector& getQErr()
 const {
  1431                             "StateImpl::getQErr()");
  1434     const Vector& getUErr()
 const {
  1436                             "StateImpl::getUErr()");
  1440     const Vector& getQErrWeights()
 const {
  1442             "StateImpl::getQErrWeights()");
  1445     const Vector& getUErrWeights()
 const {
  1447             "StateImpl::getUErrWeights()");
  1451     const Vector& getUDotErr()
 const {
  1453                             "StateImpl::getUDotErr()");
  1456     const Vector& getMultipliers()
 const {
  1458                             "StateImpl::getMultipliers()");
  1462     Vector& updYErr()
 const {
  1464                             "StateImpl::updYErr()");
  1469                             "StateImpl::updQErr()");
  1474                             "StateImpl::updUErr()");
  1478     Vector& updQErrWeights() {
  1480             "StateImpl::updQErrWeights()");
  1484     Vector& updUErrWeights() {
  1486             "StateImpl::updUErrWeights()");
  1491     Vector& updUDotErr()
 const{
  1493                             "StateImpl::updUDotErr()");
  1496     Vector& updMultipliers()
 const{
  1498                             "StateImpl::updMultipliers()");
  1502     const Vector& getEventTriggers()
 const {
  1504                             "StateImpl::getEventTriggers()");
  1507     const Vector& getEventTriggersByStage(Stage g)
 const {
  1509                             "StateImpl::getEventTriggersByStage()");
  1514     Vector& updEventTriggers()
 const {
  1516                             "StateImpl::updEventTriggers()");
  1519     Vector& updEventTriggersByStage(Stage g)
 const {
  1521                             "StateImpl::updEventTriggersByStage()");
  1525     CacheEntryIndex getDiscreteVarUpdateIndex
  1526        (SubsystemIndex subsys, DiscreteVariableIndex index)
 const {
  1527         const PerSubsystemInfo& ss = subsystems[subsys];
  1529             "StateImpl::getDiscreteVarUpdateIndex()");
  1530         const DiscreteVarInfo& dv = ss.discreteInfo[index];
  1531         return dv.getAutoUpdateEntry();
  1534     Stage getDiscreteVarAllocationStage
  1535        (SubsystemIndex subsys, DiscreteVariableIndex index)
 const {
  1536         const PerSubsystemInfo& ss = subsystems[subsys];
  1538             "StateImpl::getDiscreteVarAllocationStage()");
  1539         const DiscreteVarInfo& dv = ss.discreteInfo[index];
  1540         return dv.getAllocationStage();
  1543     Stage getDiscreteVarInvalidatesStage
  1544        (SubsystemIndex subsys, DiscreteVariableIndex index)
 const {
  1545         const PerSubsystemInfo& ss = subsystems[subsys];
  1547             "StateImpl::getDiscreteVarInvalidatesStage()");
  1548         const DiscreteVarInfo& dv = ss.discreteInfo[index];
  1549         return dv.getInvalidatedStage();
  1555     const AbstractValue& 
  1556     getDiscreteVariable(SubsystemIndex subsys, DiscreteVariableIndex index)
 const {
  1557         const PerSubsystemInfo& ss = subsystems[subsys];
  1560                          "StateImpl::getDiscreteVariable()");
  1561         const DiscreteVarInfo& dv = ss.discreteInfo[index];
  1565                                 "StateImpl::getDiscreteVariable()");
  1568         return dv.getValue();
  1570     Real getDiscreteVarLastUpdateTime(SubsystemIndex subsys, DiscreteVariableIndex index)
 const {
  1571         const PerSubsystemInfo& ss = subsystems[subsys];
  1573                          "StateImpl::getDiscreteVarLastUpdateTime()");
  1574         const DiscreteVarInfo& dv = ss.discreteInfo[index];
  1575         return dv.getTimeLastUpdated();
  1578     const AbstractValue& getDiscreteVarUpdateValue
  1579        (SubsystemIndex subsys, DiscreteVariableIndex index)
 const {
  1580         const CacheEntryIndex cx = getDiscreteVarUpdateIndex(subsys,index);
  1581         SimTK_ERRCHK2(cx.isValid(), 
"StateImpl::getDiscreteVarUpdateValue()", 
  1582             "Subsystem %d has a discrete variable %d but it does not have an"  1583             " associated update cache variable.", (int)subsys, (
int)index);
  1584         return getCacheEntry(subsys, cx);
  1586     AbstractValue& updDiscreteVarUpdateValue
  1587        (SubsystemIndex subsys, DiscreteVariableIndex index)
 const {
  1588         const CacheEntryIndex cx = getDiscreteVarUpdateIndex(subsys,index);
  1589         SimTK_ERRCHK2(cx.isValid(), 
"StateImpl::updDiscreteVarUpdateValue()", 
  1590             "Subsystem %d has a discrete variable %d but it does not have an"  1591             " associated update cache variable.", (int)subsys, (
int)index);
  1592         return updCacheEntry(subsys, cx);
  1594     bool isDiscreteVarUpdateValueRealized
  1595        (SubsystemIndex subsys, DiscreteVariableIndex index)
 const {
  1596         const CacheEntryIndex cx = getDiscreteVarUpdateIndex(subsys,index);
  1597         SimTK_ERRCHK2(cx.isValid(), 
"StateImpl::isDiscreteVarUpdateValueRealized()", 
  1598             "Subsystem %d has a discrete variable %d but it does not have an"  1599             " associated update cache variable.", (int)subsys, (
int)index);
  1600         return isCacheValueRealized(subsys, cx);
  1602     void markDiscreteVarUpdateValueRealized
  1603        (SubsystemIndex subsys, DiscreteVariableIndex index)
 const {
  1604         const CacheEntryIndex cx = getDiscreteVarUpdateIndex(subsys,index);
  1606             "StateImpl::markDiscreteVarUpdateValueRealized()", 
  1607             "Subsystem %d has a discrete variable %d but it does not have an"  1608             " associated update cache variable.", (int)subsys, (
int)index);
  1609         markCacheValueRealized(subsys, cx);
  1618     updDiscreteVariable(SubsystemIndex subsys, DiscreteVariableIndex index) {
  1619         PerSubsystemInfo& ss = subsystems[subsys];
  1622                          "StateImpl::updDiscreteVariable()");
  1623         DiscreteVarInfo& dv = ss.discreteInfo[index];
  1630         invalidateAll(dv.getInvalidatedStage());
  1633         const CacheEntryIndex cx = dv.getAutoUpdateEntry();
  1635             CacheEntryInfo& ce = ss.cacheInfo[cx];
  1641         return dv.updValue(t);
  1644     Stage getCacheEntryAllocationStage
  1645        (SubsystemIndex subsys, CacheEntryIndex index)
 const {
  1646         const PerSubsystemInfo& ss = subsystems[subsys];
  1648             "StateImpl::getCacheEntryAllocationStage()");
  1649         const CacheEntryInfo& ce = ss.cacheInfo[index];
  1650         return ce.getAllocationStage();
  1655     const AbstractValue& 
  1656     getCacheEntry(SubsystemIndex subsys, CacheEntryIndex index)
 const {
  1657         const PerSubsystemInfo& ss = subsystems[subsys];
  1660                          "StateImpl::getCacheEntry()");
  1661         const CacheEntryInfo& ce = ss.cacheInfo[index];
  1667             ce.getDependsOnStage(), 
"StateImpl::getCacheEntry()");
  1669         if (ss.currentStage < ce.getComputedByStage()) {
  1671                 ss.stageVersions[ce.getDependsOnStage()];
  1673                 ce.getVersionWhenLastComputed();
  1675             if (lastCacheVersion != currentDependsOnVersion) {
  1677                     ss.currentStage, ce.getDependsOnStage(), 
  1678                     currentDependsOnVersion, lastCacheVersion);
  1686         return ce.getValue();
  1692     updCacheEntry(SubsystemIndex subsys, CacheEntryIndex index)
 const {
  1693         const PerSubsystemInfo& ss = subsystems[subsys];
  1696                          "StateImpl::updCacheEntry()");
  1697         CacheEntryInfo& ce = ss.cacheInfo[index];
  1699         return ce.updValue();
  1702     bool isCacheValueRealized(SubsystemIndex subx, CacheEntryIndex cx)
 const {
  1703         const PerSubsystemInfo& ss = subsystems[subx];
  1705                          "StateImpl::isCacheValueRealized()");
  1706         const CacheEntryInfo& ce = ss.cacheInfo[cx];
  1707         return ce.isCurrent(getSubsystemStage(subx), 
  1708                             getSubsystemStageVersions(subx));
  1710     void markCacheValueRealized(SubsystemIndex subx, CacheEntryIndex cx)
 const {
  1711         const PerSubsystemInfo& ss = subsystems[subx];
  1713                          "StateImpl::markCacheValueRealized()");
  1714         CacheEntryInfo& ce = ss.cacheInfo[cx];
  1721                             ce.getDependsOnStage().prev(), 
  1722                             "StateImpl::markCacheValueRealized()");
  1724         ce.markAsComputed(getSubsystemStageVersions(subx));
  1727     void markCacheValueNotRealized
  1728        (SubsystemIndex subx, CacheEntryIndex cx)
 const {
  1729         const PerSubsystemInfo& ss = subsystems[subx];
  1731             "StateImpl::markCacheValueNotRealized()");
  1732         CacheEntryInfo& ce = ss.cacheInfo[cx];
  1740     void setSystemTopologyStageVersion(
StageVersion topoVersion)
  1741     {   assert(topoVersion>0); 
  1745     void getSystemStageVersions(Array_<StageVersion>& versions)
 const {
  1746         versions.resize(currentSystemStage+1);
  1747         for (
int i=0; i <= currentSystemStage; ++i)
  1748             versions[i] = systemStageVersions[i];
  1755     Stage getLowestSystemStageDifference
  1756        (
const Array_<StageVersion>& prevVersions)
 const {
  1757         const int nRealizedBefore = (int)prevVersions.size();
  1758         const int nRealizedNow    = (int)currentSystemStage+1; 
  1759         const int nRealizedBoth   = 
std::min(nRealizedBefore,nRealizedNow);
  1763         for (; g < nRealizedBoth; ++g)
  1764             if (systemStageVersions[g] != prevVersions[g])
  1773     void autoUpdateDiscreteVariables();
  1775     String toString() 
const;    
  1776     String cacheToString() 
const;
  1779     void initializeStageVersions() {
  1781             systemStageVersions[i] = 1; 
  1814     mutable Stage        currentSystemStage;
  1838     mutable Vector  multipliers; 
  1847     mutable Vector  allTriggers;
  1857     Array_<PerSubsystemInfo> subsystems;
  1860     bool allSubsystemsAtLeastAtStage(Stage g)
 const {
  1861         for (SubsystemIndex i(0); i < (int)subsystems.size(); ++i)
  1862             if (subsystems[i].currentStage < g)
  1876     updImpl().setNumSubsystems(i);
  1879    (SubsystemIndex subsys, 
const String& name, 
const String& version) {
  1880     updImpl().initializeSubsystem(subsys, name, version);
  1884    (
const String& name, 
const String& version) {
  1885     return updImpl().addSubsystem(name, version);
  1888     return getImpl().getNumSubsystems();
  1891     return getImpl().getSubsystemName(subsys);
  1894     return getImpl().getSubsystemVersion(subsys);
  1897     return getImpl().getSubsystemStage(subsys);
  1900     return getImpl().getSystemStage();
  1903     updImpl().invalidateAll(stage);
  1906     getImpl().invalidateAllCacheAtOrAbove(stage);
  1909     getImpl().advanceSubsystemToStage(subsys, stage);
  1912     getImpl().advanceSystemToStage(stage);
  1916 {   
return getImpl().getSystemTopologyStageVersion(); }
  1920     return updImpl().allocateQ(subsys, qInit);
  1923     return updImpl().allocateU(subsys, uInit);
  1926     return updImpl().allocateZ(subsys, zInit);
  1931     return getImpl().allocateQErr(subsys, nqerr);
  1934     return getImpl().allocateUErr(subsys, nuerr);
  1938     return getImpl().allocateUDotErr(subsys, nudoterr);
  1942 inline EventTriggerByStageIndex 
State::  1944     return getImpl().allocateEventTrigger(subsys, stage, nevent);
  1948 inline DiscreteVariableIndex 
State::  1950     return updImpl().allocateDiscreteVariable(subsys, stage, v);
  1952 inline DiscreteVariableIndex 
State::  1954    (SubsystemIndex subsys, Stage invalidates, AbstractValue* v,
  1955     Stage updateDependsOn) {
  1956     return updImpl().allocateAutoUpdateDiscreteVariable
  1957        (subsys, invalidates, v, updateDependsOn); 
  1960 inline CacheEntryIndex 
State::  1962    (SubsystemIndex subsys, DiscreteVariableIndex index)
 const {
  1963     return getImpl().getDiscreteVarUpdateIndex(subsys, index);
  1967    (SubsystemIndex subsys, DiscreteVariableIndex index)
 const {
  1968     return getImpl().getDiscreteVarAllocationStage(subsys, index);
  1972    (SubsystemIndex subsys, DiscreteVariableIndex index)
 const {
  1973     return getImpl().getDiscreteVarInvalidatesStage(subsys, index);
  1976 inline const AbstractValue& 
State::  1978     return getImpl().getDiscreteVariable(subsys, index);
  1982    (SubsystemIndex subsys, DiscreteVariableIndex index)
 const {
  1983     return getImpl().getDiscreteVarLastUpdateTime(subsys, index);
  1985 inline const AbstractValue& 
State::  1987    (SubsystemIndex subsys, DiscreteVariableIndex index)
 const {
  1988     return getImpl().getDiscreteVarUpdateValue(subsys, index);
  1992    (SubsystemIndex subsys, DiscreteVariableIndex index)
 const {
  1993     return getImpl().updDiscreteVarUpdateValue(subsys, index);
  1997    (SubsystemIndex subsys, DiscreteVariableIndex index)
 const {
  1998     return getImpl().isDiscreteVarUpdateValueRealized(subsys, index);
  2002    (SubsystemIndex subsys, DiscreteVariableIndex index)
 const {
  2003     getImpl().markDiscreteVarUpdateValueRealized(subsys, index);
  2008    (SubsystemIndex subsys, DiscreteVariableIndex index) {
  2009     return updImpl().updDiscreteVariable(subsys, index);
  2013    (SubsystemIndex i, DiscreteVariableIndex index, 
const AbstractValue& v) {
  2014     updDiscreteVariable(i,index) = v;
  2018 inline CacheEntryIndex 
State::  2020                    AbstractValue* v)
 const {
  2021     return getImpl().allocateCacheEntry(subsys, dependsOn, computedBy, v);
  2026     return getImpl().getCacheEntryAllocationStage(subsys, index);
  2028 inline const AbstractValue& 
State::  2029 getCacheEntry(SubsystemIndex subsys, CacheEntryIndex index)
 const {
  2030     return getImpl().getCacheEntry(subsys, index);
  2033 updCacheEntry(SubsystemIndex subsys, CacheEntryIndex index)
 const {
  2034     return getImpl().updCacheEntry(subsys, index);
  2039     return getImpl().isCacheValueRealized(subx, cx); 
  2043     getImpl().markCacheValueRealized(subx, cx); 
  2047     getImpl().markCacheValueNotRealized(subx, cx); 
  2053     return getImpl().getNY();
  2056     return getImpl().getNQ();
  2059     return getImpl().getQStart();
  2062     return getImpl().getNU();
  2065     return getImpl().getUStart();
  2068     return getImpl().getNZ();
  2071     return getImpl().getZStart();
  2074     return getImpl().getNYErr();
  2077     return getImpl().getNQErr();
  2080     return getImpl().getQErrStart();
  2083     return getImpl().getNUErr();
  2086     return getImpl().getUErrStart();
  2089     return getImpl().getNUDotErr();
  2092     return getNUDotErr();
  2095     return getImpl().getNEventTriggers();
  2098     return getImpl().getNEventTriggersByStage(stage);
  2105     return getImpl().getQStart(subsys);
  2108     return getImpl().getNQ(subsys);
  2111     return getImpl().getUStart(subsys);
  2114     return getImpl().getNU(subsys);
  2117     return getImpl().getZStart(subsys);
  2120     return getImpl().getNZ(subsys);
  2123     return getImpl().getQErrStart(subsys);
  2126     return getImpl().getNQErr(subsys);
  2129     return getImpl().getUErrStart(subsys);
  2132     return getImpl().getNUErr(subsys);
  2135     return getImpl().getUDotErrStart(subsys);
  2138     return getImpl().getNUDotErr(subsys);
  2141     return SystemMultiplierIndex(getUDotErrStart(subsys));
  2144     return getNUDotErr(subsys);
  2146 inline SystemEventTriggerByStageIndex 
State::  2148     return getImpl().getEventTriggerStartByStage(subsys, stage);
  2152     return getImpl().getNEventTriggersByStage(subsys, stage);
  2159     return getImpl().getEventTriggersByStage(subsys, stage);
  2163     return getImpl().updEventTriggersByStage(subsys, stage);
  2166     return getImpl().getQ(subsys);
  2169     return getImpl().getU(subsys);
  2172     return getImpl().getZ(subsys);
  2175     return getImpl().getUWeights(subsys);
  2178     return getImpl().getZWeights(subsys);
  2181     return updImpl().updQ(subsys);
  2184     return updImpl().updU(subsys);
  2187     return updImpl().updZ(subsys);
  2190     return updImpl().updUWeights(subsys);
  2193     return updImpl().updZWeights(subsys);
  2196     return getImpl().getQDot(subsys);
  2199     return getImpl().getUDot(subsys);
  2202     return getImpl().getZDot(subsys);
  2205     return getImpl().getQDotDot(subsys);
  2208     return getImpl().updQDot(subsys);
  2211     return getImpl().updUDot(subsys);
  2214     return getImpl().updZDot(subsys);
  2217     return getImpl().updQDotDot(subsys);
  2220     return getImpl().getQErr(subsys);
  2223     return getImpl().getUErr(subsys);
  2226     return getImpl().getQErrWeights(subsys);
  2229     return getImpl().getUErrWeights(subsys);
  2232     return getImpl().getUDotErr(subsys);
  2235     return getImpl().getMultipliers(subsys);
  2238     return getImpl().updQErr(subsys);
  2241     return getImpl().updUErr(subsys);
  2244     return updImpl().updQErrWeights(subsys);
  2247     return updImpl().updUErrWeights(subsys);
  2250     return getImpl().updUDotErr(subsys);
  2253     return getImpl().updMultipliers(subsys);
  2256 inline SystemEventTriggerIndex 
State::  2258     return getImpl().getEventTriggerStartByStage(stage);
  2262     return getImpl().getEventTriggers();
  2265     return getImpl().getEventTriggersByStage(stage);
  2269     return getImpl().updEventTriggers();
  2272     return getImpl().updEventTriggersByStage(stage);
  2276     return getImpl().getTime();
  2279     return getImpl().getY();
  2282     return getImpl().getQ();
  2285     return getImpl().getU();
  2288     return getImpl().getZ();
  2291     return getImpl().getUWeights();
  2294     return getImpl().getZWeights();
  2297     return updImpl().updTime();
  2300     return updImpl().updY();
  2309     return updImpl().updQ();
  2312     return updImpl().updU();
  2315     return updImpl().updZ();
  2318     return updImpl().updUWeights();
  2321     return updImpl().updZWeights();
  2333     return getImpl().getYDot();
  2336     return getImpl().getQDot();
  2339     return getImpl().getZDot();
  2342     return getImpl().getUDot();
  2345     return getImpl().getQDotDot();
  2348     return getImpl().updYDot();
  2351     return getImpl().updQDot();
  2354     return getImpl().updZDot();
  2357     return getImpl().updUDot();
  2360     return getImpl().updQDotDot();
  2363     return getImpl().getYErr();
  2366     return getImpl().getQErr();
  2369     return getImpl().getUErr();
  2372     return getImpl().getQErrWeights();
  2375     return getImpl().getUErrWeights();
  2378     return getImpl().getUDotErr();
  2381     return getImpl().getMultipliers();
  2384     return getImpl().updYErr();
  2387     return getImpl().updQErr();
  2390     return getImpl().updUErr();
  2393     return updImpl().updQErrWeights();
  2396     return updImpl().updUErrWeights();
  2399     return getImpl().updUDotErr();
  2402     return getImpl().updMultipliers();
  2407 {   
return updImpl().setSystemTopologyStageVersion(topoVersion); }
  2411     return getImpl().getSystemStageVersions(versions); 
  2415     return getImpl().getLowestSystemStageDifference(prev); 
  2418     updImpl().autoUpdateDiscreteVariables(); 
  2422     return getImpl().toString();
  2425     return getImpl().cacheToString();
  2433 #endif // SimTK_SimTKCOMMON_STATE_IMPL_H_ AbstractValue & updDiscreteVarUpdateValue(SubsystemIndex, DiscreteVariableIndex) const 
For an auto-updating discrete variable, return a writable reference to the value of its associated up...
 
Vector & updUErrWeights()
Set the unit weighting (1/unit error) for each of the mp+mv velocity-level inline constraints...
 
void getSystemStageVersions(Array_< StageVersion > &versions) const 
(Advanced) Record the current version numbers of each valid System-level stage. 
 
const Vector & getUDot() const 
 
SystemYErrIndex getUErrStart() const 
Returns the yErr index at which the uErr's begin. Callable at Instance stage. 
 
const String & getSubsystemVersion(SubsystemIndex) const 
 
Stage getCacheEntryAllocationStage(SubsystemIndex, CacheEntryIndex) const 
At what stage was this State when this cache entry was allocated? The answer must be Stage::Empty...
 
Physical parameters set. 
Definition: Stage.h:56
 
int getNYErr() const 
Get the total number nyerr=nqerr+nuerr of shared cache entries for position-level and velocity-level ...
 
void setZ(const Vector &z)
 
Vector & updQErrWeights()
Set the unit weighting (1/unit error) for each of the mp+mquat position inline constraint equations...
 
bool isDiscreteVarUpdateValueRealized(SubsystemIndex, DiscreteVariableIndex) const 
Check whether the update value for this auto-update discrete variable has already been computed since...
 
#define SimTK_STAGECHECK_LT_ALWAYS(currentStage, targetStage, methodNm)
Definition: ExceptionMacros.h:183
 
Modeling choices made. 
Definition: Stage.h:55
 
void setTime(Real t)
An alternate syntax equivalent to updTime() and updY(). 
 
const Stage & getSubsystemStage(SubsystemIndex) const 
 
int getNU() const 
Get total number of shared u's (generalized speeds; mobilities). 
 
AbstractValue & updCacheEntry(SubsystemIndex, CacheEntryIndex) const 
Retrieve a writable reference to the value contained in a particular cache entry. ...
 
void markCacheValueNotRealized(SubsystemIndex, CacheEntryIndex) const 
Normally cache entries are invalidated automatically, however this method allows manual invalidation ...
 
void advanceSubsystemToStage(SubsystemIndex, Stage) const 
Advance a particular Subsystem's current stage by one to the indicated stage. 
 
QErrIndex allocateQErr(SubsystemIndex, int nqerr) const 
Allocate nqerr cache slots to hold the current error for position-level (holonomic) constraint equati...
 
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
 
#define SimTK_STAGECHECK_GE_ALWAYS(currentStage, targetStage, methodNm)
Definition: ExceptionMacros.h:180
 
CacheEntryIndex allocateCacheEntry(SubsystemIndex, Stage earliest, Stage latest, AbstractValue *) const 
There are two Stages supplied explicitly as arguments to this method: earliest and latest...
 
const Stage & getSystemStage() const 
This returns the *global* stage for this State. 
 
const Vector & getYErr() const 
Return the current constraint errors for all constraints. 
 
Spatial velocities available. 
Definition: Stage.h:59
 
const Vector & getQErrWeights() const 
Get the unit weighting (1/unit error) for each of the mp+mquat position inline constraints equations...
 
Lower than any legitimate Stage. 
Definition: Stage.h:53
 
Real getDiscreteVarLastUpdateTime(SubsystemIndex, DiscreteVariableIndex) const 
Return the time of last update for this discrete variable. 
 
Vector & updYErr() const 
These are mutable. 
 
void setY(const Vector &y)
 
SystemEventTriggerIndex getEventTriggerStartByStage(Stage) const 
Return the index within the global event trigger array at which the first of the event triggers assoc...
 
int getNQErr() const 
Return the total number nqerr=mp+nQuaternions of cache entries for position-level constraint errors...
 
int size() const 
Definition: VectorBase.h:396
 
const AbstractValue & getDiscreteVariable(SubsystemIndex, DiscreteVariableIndex) const 
Get the current value of the indicated discrete variable. 
 
ELEM min(const VectorBase< ELEM > &v)
Definition: VectorMath.h:178
 
UDotErrIndex allocateUDotErr(SubsystemIndex, int nudoterr) const 
Allocate nudoterr cache slots to hold the current error for acceleration-level (acceleration-only, nonholonomic first derivative, and holonomic second derivative) constraint equations. 
 
SystemYIndex getUStart() const 
Returns the y index at which the u's begin. Callable at Model stage. 
 
const Vector & getUErr() const 
 
CacheEntryIndex getDiscreteVarUpdateIndex(SubsystemIndex, DiscreteVariableIndex) const 
For an auto-updating discrete variable, return the CacheEntryIndex for its associated update cache en...
 
SystemUDotErrIndex getUDotErrStart(SubsystemIndex) const 
 
void autoUpdateDiscreteVariables()
(Advanced) This is called at the beginning of every integration step to set the values of auto-update...
 
Vector & updUDotErr() const 
 
bool isCacheValueRealized(SubsystemIndex, CacheEntryIndex) const 
Check whether the value in a particular cache entry has been recalculated since the last change to th...
 
int getNMultipliers() const 
Return the total number of constraint multipliers; necessarily the same as the number of acceleration...
 
SimTK_Real Real
This is the default compiled-in floating point type for SimTK, either float or double. 
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:593
 
#define SimTK_ERRCHK2(cond, whereChecked, fmt, a1, a2)                            
Definition: ExceptionMacros.h:328
 
int getNUErr() const 
Return the total number nuerr=mp+mv of cache entries for velocity-level constraint errors (including ...
 
Vector & updQ()
These are just views into Y. 
 
int getNQ() const 
Get total number of shared q's (generalized coordinates; second order state variables). 
 
const Real NaN
This is the IEEE "not a number" constant for this implementation of the default-precision Real type; ...
 
#define SimTK_STAGECHECK_RANGE_ALWAYS(lower, current, upper, methodNm)
Definition: ExceptionMacros.h:186
 
void setDiscreteVariable(SubsystemIndex, DiscreteVariableIndex, const AbstractValue &)
Alternate interface to updDiscreteVariable. 
 
AbstractValue & updDiscreteVariable(SubsystemIndex, DiscreteVariableIndex)
Get a writable reference to the value stored in the indicated discrete state variable dv...
 
void setU(const Vector &u)
 
int getNumSubsystems() const 
Return the number of Subsystems known to this State. 
 
DiscreteVariableIndex allocateAutoUpdateDiscreteVariable(SubsystemIndex, Stage invalidates, AbstractValue *, Stage updateDependsOn)
This method allocates a DiscreteVariable whose value should be updated automatically after each time ...
 
const Vector & getEventTriggers() const 
 
const Vector & getQDotDot() const 
This has its own space, not a view. 
 
Vector & updQDotDot() const 
This is a separate shared cache entry, not part of YDot. 
 
Report-only quantities evaluated. 
Definition: Stage.h:62
 
void setSystemTopologyStageVersion(StageVersion topoVersion)
(Advanced) This explicitly modifies the Topology stage version; don't use this method unless you know...
 
SubsystemIndex addSubsystem(const String &name, const String &version)
Register a new subsystem as a client of this State. 
 
const Vector & getZWeights() const 
Get a unit weighting (1/unit change) for each z that can be used to weight a vector dz so that the di...
 
System topology realized. 
Definition: Stage.h:54
 
Forces calculated. 
Definition: Stage.h:60
 
Stage getLowestSystemStageDifference(const Array_< StageVersion > &prevVersions) const 
(Advanced) Given a list of per-stage version numbers extracted by an earlier call to getSystemStageVe...
 
Vector & updYDot() const 
These are mutable. 
 
Spatial configuration available. 
Definition: Stage.h:58
 
Stage getDiscreteVarAllocationStage(SubsystemIndex, DiscreteVariableIndex) const 
At what stage was this State when this discrete variable was allocated? The answer must be Stage::Emp...
 
const Vector & getZ() const 
 
Real & updTime()
You can call these as long as System stage >= Model, but the stage will be backed up if necessary to ...
 
SystemYIndex getQStart() const 
Returns the y index at which the q's begin. Callable at Model stage. 
 
DiscreteVariableIndex allocateDiscreteVariable(SubsystemIndex, Stage invalidates, AbstractValue *)
The Stage supplied here in the call is the earliest subsystem stage which is invalidated by a change ...
 
const Vector & getUErrWeights() const 
Get the unit weighting (1/unit error) for each of the mp+mv velocity-level inline constraint equation...
 
const Vector & getUDotErr() const 
These have their own space, they are not views. 
 
const Vector & getQDot() const 
These are just views into YDot. 
 
Higher than any legitimate Stage. 
Definition: Stage.h:63
 
const Vector & getU() const 
 
const Vector & getQ() const 
These are just views into Y. 
 
void setQ(const Vector &q)
Alternate interface. 
 
const Vector & getEventTriggersByStage(Stage) const 
 
int getNUDotErr() const 
Return the total number nudotErr=mp+mv+ma of cache entries for acceleration-level constraint errors (...
 
const AbstractValue & getCacheEntry(SubsystemIndex, CacheEntryIndex) const 
Retrieve a const reference to the value contained in a particular cache entry. 
 
UErrIndex allocateUErr(SubsystemIndex, int nuerr) const 
Allocate nuerr cache slots to hold the current error for velocity-level (nonholonomic and holonomic f...
 
SystemYIndex getZStart() const 
Returns the y index at which the z's begin. Callable at Model stage. 
 
const Vector & getQErr() const 
These are just views into YErr. 
 
void setNumSubsystems(int i)
Set the number of subsystems in this state. 
 
int getNEventTriggers() const 
Return the total number of event trigger function slots in the cache. 
 
Vector_< Real > Vector
Variable-size column vector of Real elements; abbreviation for Vector_<Real>. 
Definition: BigMatrix.h:1473
 
StageVersion getSystemTopologyStageVersion() const 
The Topology stage version number (an integer) stored in this State must match the topology cache ver...
 
#define SimTK_INDEXCHECK(ix, ub, where)
Definition: ExceptionMacros.h:145
 
QIndex allocateQ(SubsystemIndex, const Vector &qInit)
Allocate generalized coordinates q, which are second order continuous state variables. 
 
const AbstractValue & getDiscreteVarUpdateValue(SubsystemIndex, DiscreteVariableIndex) const 
For an auto-updating discrete variable, return the current value of its associated update cache entry...
 
SystemMultiplierIndex getMultipliersStart(SubsystemIndex) const 
 
const Vector & getYDot() const 
 
Vector & updUWeights()
Set u weights (and q weights indirectly). 
 
UIndex allocateU(SubsystemIndex, const Vector &uInit)
Allocate generalized speeds u, which are first order continuous state variables related to the deriva...
 
int getNY() const 
Get the total number ny=nq+nu+nz of shared continuous state variables. 
 
void initializeSubsystem(SubsystemIndex, const String &name, const String &version)
Set the name and version for a given subsystem, which must already have a slot allocated. 
 
Vector & updZWeights()
Set z weights. 
 
void advanceSystemToStage(Stage) const 
Advance the System-level current stage by one to the indicated stage. 
 
For iterating over meaningful stage values. 
Definition: Stage.h:67
 
const Vector & getMultipliers() const 
 
ZIndex allocateZ(SubsystemIndex, const Vector &zInit)
Allocate auxiliary first order continuous state variables z. 
 
const Real & getTime() const 
You can call these as long as *system* stage >= Model. 
 
const String & getSubsystemName(SubsystemIndex) const 
 
const Vector & getZDot() const 
 
const Vector & getY() const 
 
A new time has been realized. 
Definition: Stage.h:57
 
Stage getDiscreteVarInvalidatesStage(SubsystemIndex, DiscreteVariableIndex) const 
What is the earliest stage that is invalidated when this discrete variable is modified? All later stages are also invalidated. 
 
const Vector & getUWeights() const 
Get a unit weighting (1/unit change) for each u that can be used to weight a vector du so that the di...
 
String cacheToString() const 
 
Vector & updEventTriggers() const 
 
void markDiscreteVarUpdateValueRealized(SubsystemIndex, DiscreteVariableIndex) const 
Mark the update value for this auto-update discrete variable as up-to-date with respect to the state ...
 
Accelerations and multipliers calculated. 
Definition: Stage.h:61
 
void invalidateAll(Stage)
If any subsystem or the system stage is currently at or higher than the passed-in one...
 
#define SimTK_STAGECHECK_GE(currentStage, targetStage, methodNm)
Definition: ExceptionMacros.h:208
 
SystemYErrIndex getQErrStart() const 
Returns the yErr index at which the qErr's begin. Callable at Instance stage. 
 
EventTriggerByStageIndex allocateEventTrigger(SubsystemIndex, Stage stage, int nevent) const 
Allocate room for nevent witness function values that will be available at the indicated stage...
 
#define SimTK_THROW4(exc, a1, a2, a3, a4)
Definition: Exception.h:317
 
void invalidateAllCacheAtOrAbove(Stage) const 
If any subsystem or the system stage is currently at or higher than the passed-in one...
 
int StageVersion
This is the type to use for Stage version numbers. 
Definition: State.h:160
 
Vector & updEventTriggersByStage(Stage) const 
 
Vector & updMultipliers() const 
 
int getNZ() const 
Get total number of shared z's (auxiliary state variables). 
 
int getNEventTriggersByStage(Stage) const 
Return the size of the partition of event trigger functions which are evaluated at a given Stage...
 
void markCacheValueRealized(SubsystemIndex, CacheEntryIndex) const 
Mark the value of a particular cache entry as up to date after it has been recalculated.