Simbody

State.h

Go to the documentation of this file.
00001 #ifndef SimTK_SimTKCOMMON_STATE_H_
00002 #define SimTK_SimTKCOMMON_STATE_H_
00003 
00004 /* -------------------------------------------------------------------------- *
00005  *                      SimTK Core: SimTKcommon                               *
00006  * -------------------------------------------------------------------------- *
00007  * This is part of the SimTK Core biosimulation toolkit originating from      *
00008  * Simbios, the NIH National Center for Physics-Based Simulation of           *
00009  * Biological Structures at Stanford, funded under the NIH Roadmap for        *
00010  * Medical Research, grant U54 GM072970. See https://simtk.org.               *
00011  *                                                                            *
00012  * Portions copyright (c) 2005-10 Stanford University and the Authors.        *
00013  * Authors: Michael Sherman                                                   *
00014  * Contributors: Peter Eastman                                                *
00015  *                                                                            *
00016  * Permission is hereby granted, free of charge, to any person obtaining a    *
00017  * copy of this software and associated documentation files (the "Software"), *
00018  * to deal in the Software without restriction, including without limitation  *
00019  * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
00020  * and/or sell copies of the Software, and to permit persons to whom the      *
00021  * Software is furnished to do so, subject to the following conditions:       *
00022  *                                                                            *
00023  * The above copyright notice and this permission notice shall be included in *
00024  * all copies or substantial portions of the Software.                        *
00025  *                                                                            *
00026  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
00027  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
00028  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
00029  * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
00030  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
00031  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
00032  * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
00033  * -------------------------------------------------------------------------- */
00034 
00035 #include "SimTKcommon/basics.h"
00036 #include "SimTKcommon/Simmatrix.h"
00037 #include "SimTKcommon/internal/Event.h"
00038 
00039 #include <ostream>
00040 #include <cassert>
00041 #include <set>
00042 
00043 namespace SimTK {
00044 
00045 
00048 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SubsystemIndex);
00049 
00056 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemYIndex);
00057 
00065 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemQIndex);
00069 SimTK_DEFINE_UNIQUE_INDEX_TYPE(QIndex);
00070 
00077 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemUIndex);
00081 SimTK_DEFINE_UNIQUE_INDEX_TYPE(UIndex);
00082 
00089 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemZIndex);
00093 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ZIndex);
00094 
00099 SimTK_DEFINE_UNIQUE_INDEX_TYPE(DiscreteVariableIndex);
00100 
00105 SimTK_DEFINE_UNIQUE_INDEX_TYPE(CacheEntryIndex);
00106 
00112 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemYErrIndex);
00113 
00119 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemQErrIndex);
00123 SimTK_DEFINE_UNIQUE_INDEX_TYPE(QErrIndex);
00124 
00130 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemUErrIndex);
00134 SimTK_DEFINE_UNIQUE_INDEX_TYPE(UErrIndex);
00135 
00141 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemUDotErrIndex);
00145 SimTK_DEFINE_UNIQUE_INDEX_TYPE(UDotErrIndex);
00146 
00152 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemMultiplierIndex);
00156 SimTK_DEFINE_UNIQUE_INDEX_TYPE(MultiplierIndex);
00157 
00162 typedef int StageVersion;
00163 
00164 
00279 class SimTK_SimTKCOMMON_EXPORT State {
00280 public:
00282     State();
00283     ~State();
00284 
00286     void clear();
00287 
00291     void setNumSubsystems(int i);
00292 
00295     void initializeSubsystem(SubsystemIndex, const String& name, const String& version);
00296 
00301     State(const State&);
00302 
00307     State& operator=(const State&);
00308 
00315     SubsystemIndex addSubsystem(const String& name, const String& version);
00316 
00317     int getNumSubsystems() const;
00318     const String& getSubsystemName   (SubsystemIndex) const;
00319     const String& getSubsystemVersion(SubsystemIndex) const;
00320     const Stage&  getSubsystemStage  (SubsystemIndex) const;
00321 
00323     const Stage& getSystemStage() const;
00324 
00331     void invalidateAll(Stage);
00332 
00340     void invalidateAllCacheAtOrAbove(Stage) const;
00341 
00348     void advanceSubsystemToStage(SubsystemIndex, Stage) const;
00353     void advanceSystemToStage(Stage) const;
00354 
00363 
00364     QIndex allocateQ(SubsystemIndex, const Vector& qInit); // qdot, qdotdot also allocated in cache
00365     UIndex allocateU(SubsystemIndex, const Vector& uInit); // udot                    "
00366     ZIndex allocateZ(SubsystemIndex, const Vector& zInit); // zdot                    "
00367 
00378 
00379     QErrIndex    allocateQErr   (SubsystemIndex, int nqerr) const;    // these are cache entries
00380     UErrIndex    allocateUErr   (SubsystemIndex, int nuerr) const;
00381     UDotErrIndex allocateUDotErr(SubsystemIndex, int nudoterr) const;
00382 
00392     EventIndex allocateEventIndex(SubsystemIndex, int nevent=1) const;
00393 
00405     EventTriggerByStageIndex allocateEventTrigger(SubsystemIndex, Stage, EventIndex, int nevent=1) const;
00406 
00407     //OBSOLETE
00408     EventTriggerByStageIndex allocateEventTrigger(SubsystemIndex, Stage, int nevent) const;
00409 
00410 
00420 
00433     DiscreteVariableIndex allocateDiscreteVariable(SubsystemIndex, Stage invalidates, AbstractValue*);
00434 
00483     DiscreteVariableIndex
00484         allocateAutoUpdateDiscreteVariable(SubsystemIndex, Stage invalidates, AbstractValue*,
00485                                            Stage updateDependsOn); 
00489     CacheEntryIndex getDiscreteVarUpdateIndex(SubsystemIndex, DiscreteVariableIndex) const;
00492     Stage getDiscreteVarAllocationStage(SubsystemIndex, DiscreteVariableIndex) const;
00497     Stage getDiscreteVarInvalidatesStage(SubsystemIndex, DiscreteVariableIndex) const;
00498 
00499 
00502     const AbstractValue& getDiscreteVariable(SubsystemIndex, DiscreteVariableIndex) const;
00504     Real getDiscreteVarLastUpdateTime(SubsystemIndex, DiscreteVariableIndex) const;
00509     const AbstractValue& getDiscreteVarUpdateValue(SubsystemIndex, DiscreteVariableIndex) const;
00515     AbstractValue& updDiscreteVarUpdateValue(SubsystemIndex, DiscreteVariableIndex) const;
00518     bool isDiscreteVarUpdateValueRealized(SubsystemIndex, DiscreteVariableIndex) const;
00521     void markDiscreteVarUpdateValueRealized(SubsystemIndex, DiscreteVariableIndex) const;
00522 
00526     AbstractValue& updDiscreteVariable(SubsystemIndex, DiscreteVariableIndex);
00528     void setDiscreteVariable(SubsystemIndex, DiscreteVariableIndex, const AbstractValue&);
00530 
00541 
00610     CacheEntryIndex allocateCacheEntry(SubsystemIndex, Stage earliest, Stage latest,
00611                                        AbstractValue*) const;
00612 
00617     CacheEntryIndex allocateCacheEntry(SubsystemIndex sx, Stage g, AbstractValue* v) const
00618     {   return allocateCacheEntry(sx, g, g, v); }
00619 
00628     CacheEntryIndex allocateLazyCacheEntry(SubsystemIndex sx, Stage earliest, AbstractValue* v) const
00629     {   return allocateCacheEntry(sx, earliest, Stage::Infinity, v); }
00630 
00633     Stage getCacheEntryAllocationStage(SubsystemIndex, CacheEntryIndex) const;
00634 
00641     const AbstractValue& getCacheEntry(SubsystemIndex, CacheEntryIndex) const;
00642 
00649     AbstractValue& updCacheEntry(SubsystemIndex, CacheEntryIndex) const; // mutable
00650 
00659     bool isCacheValueRealized(SubsystemIndex, CacheEntryIndex) const;
00660 
00673     void markCacheValueRealized(SubsystemIndex, CacheEntryIndex) const;
00674 
00680     void markCacheValueNotRealized(SubsystemIndex, CacheEntryIndex) const;
00682 
00699 
00703     int getNY() const;
00708     int getNQ() const;
00710     SystemYIndex getQStart() const;
00714     int getNU() const;
00716     SystemYIndex getUStart() const;
00720     int getNZ() const;
00722     SystemYIndex getZStart() const;
00726     int getNYErr() const;
00729     int getNQErr() const;
00731     SystemYErrIndex getQErrStart() const; 
00735     int getNUErr() const;
00737     SystemYErrIndex getUErrStart() const; 
00742     int getNUDotErr() const;
00747     int getNMultipliers() const; // =mp+mv+ma, necessarily the same as NUDotErr
00750     int getNEventTriggers() const;
00753     int getNEventTriggersByStage(Stage) const;
00757     SystemEventTriggerIndex getEventTriggerStartByStage(Stage) const; // per-stage
00758 
00760 
00774 
00775     SystemQIndex getQStart(SubsystemIndex)       const; int getNQ(SubsystemIndex)       const;
00776     SystemUIndex getUStart(SubsystemIndex)       const; int getNU(SubsystemIndex)       const;
00777     SystemZIndex getZStart(SubsystemIndex)       const; int getNZ(SubsystemIndex)       const;
00778 
00779     SystemQErrIndex       getQErrStart(SubsystemIndex)    const; int getNQErr(SubsystemIndex)    const;
00780     SystemUErrIndex       getUErrStart(SubsystemIndex)    const; int getNUErr(SubsystemIndex)    const;
00781     SystemUDotErrIndex    getUDotErrStart(SubsystemIndex) const; int getNUDotErr(SubsystemIndex) const;
00782     SystemMultiplierIndex getMultipliersStart(SubsystemIndex) const;
00783     int getNMultipliers(SubsystemIndex)     const;
00784 
00785     SystemEventTriggerByStageIndex getEventTriggerStartByStage(SubsystemIndex, Stage) const;
00786     int getNEventTriggersByStage(SubsystemIndex, Stage) const;
00787 
00789 
00800 
00803     void mapQToSubsystem(SystemQIndex, SubsystemIndex&, QIndex&) const;
00806     void mapUToSubsystem(SystemUIndex, SubsystemIndex&, UIndex&) const;
00809     void mapZToSubsystem(SystemZIndex, SubsystemIndex&, ZIndex&) const;
00812     void mapQErrToSubsystem(SystemQErrIndex, SubsystemIndex&, QErrIndex&) const;
00815     void mapUErrToSubsystem(SystemUErrIndex, SubsystemIndex&, UErrIndex&) const;
00818     void mapUDotErrToSubsystem(SystemUDotErrIndex, SubsystemIndex&, UDotErrIndex&) const;
00822     void mapMultiplierToSubsystem(SystemMultiplierIndex, SubsystemIndex&, MultiplierIndex&) const;
00825     void mapEventTriggerToSubsystem(SystemEventTriggerIndex, SubsystemIndex&, EventTriggerIndex&) const;
00828     void mapEventTriggerToStage(SystemEventTriggerIndex, Stage&, SystemEventTriggerByStageIndex&) const;
00831     void mapSubsystemEventTriggerToStage(EventTriggerIndex, Stage&, EventTriggerByStageIndex&) const;
00832 
00834 
00835     const Vector& getEventTriggers() const;
00836     const Vector& getEventTriggersByStage(Stage) const;
00837     const Vector& getEventTriggersByStage(SubsystemIndex, Stage) const;
00838 
00839     Vector& updEventTriggers() const; // mutable
00840     Vector& updEventTriggersByStage(Stage) const;
00841     Vector& updEventTriggersByStage(SubsystemIndex, Stage) const;
00842 
00844     const Vector& getQ(SubsystemIndex) const;
00845     const Vector& getU(SubsystemIndex) const;
00846     const Vector& getZ(SubsystemIndex) const;
00847 
00848     Vector& updQ(SubsystemIndex);
00849     Vector& updU(SubsystemIndex);
00850     Vector& updZ(SubsystemIndex);
00851 
00853     const Vector& getQDot(SubsystemIndex) const;
00854     const Vector& getUDot(SubsystemIndex) const;
00855     const Vector& getZDot(SubsystemIndex) const;
00856     const Vector& getQDotDot(SubsystemIndex) const;
00857 
00858     Vector& updQDot(SubsystemIndex) const;    // these are mutable
00859     Vector& updUDot(SubsystemIndex) const;
00860     Vector& updZDot(SubsystemIndex) const;
00861     Vector& updQDotDot(SubsystemIndex) const;
00862 
00863     const Vector& getQErr(SubsystemIndex) const;
00864     const Vector& getUErr(SubsystemIndex) const;
00865     const Vector& getUDotErr(SubsystemIndex) const;
00866     const Vector& getMultipliers(SubsystemIndex) const;
00867     Vector& updQErr(SubsystemIndex) const;    // these are mutable
00868     Vector& updUErr(SubsystemIndex) const;
00869     Vector& updUDotErr(SubsystemIndex) const;
00870     Vector& updMultipliers(SubsystemIndex) const;
00871 
00873     const Real&   getTime() const;
00874     const Vector& getY() const; // {Q,U,Z} packed and in that order
00875 
00877     const Vector& getQ() const;
00878     const Vector& getU() const;
00879     const Vector& getZ() const;
00880 
00883     Real&   updTime();  // Back up to Stage::Time-1
00884     Vector& updY();     // Back up to Stage::Dynamics-1
00885 
00887     void setTime(Real t);
00888     void setY(const Vector& y);
00889 
00891     Vector& updQ();     // Back up to Stage::Position-1
00892     Vector& updU();     // Back up to Stage::Velocity-1
00893     Vector& updZ();     // Back up to Stage::Dynamics-1
00894 
00896     void setQ(const Vector& q);
00897     void setU(const Vector& u);
00898     void setZ(const Vector& z);
00899 
00900     const Vector& getYDot()    const; // Stage::Acceleration
00901 
00903     const Vector& getQDot()    const; // Stage::Velocity
00904     const Vector& getZDot()    const; // Stage::Dynamics
00905     const Vector& getUDot()    const; // Stage::Acceleration
00906 
00908     const Vector& getQDotDot() const; // Stage::Acceleration
00909 
00911     Vector& updYDot() const;    // Stage::Acceleration-1
00912     Vector& updQDot() const;    // Stage::Velocity-1     (view into YDot)
00913     Vector& updZDot() const;    // Stage::Dynamics-1            "
00914     Vector& updUDot() const;    // Stage::Acceleration-1        "
00915 
00919     Vector& updQDotDot() const; // Stage::Acceleration-1
00920 
00923     const Vector& getYErr() const;  // Stage::Velocity
00924 
00926     const Vector& getQErr() const;  // Stage::Position (index 3 constraints)
00927     const Vector& getUErr() const;  // Stage::Velocity (index 2 constraints)
00928 
00930     const Vector& getUDotErr()     const; // Stage::Acceleration (index 1 constraints)
00931     const Vector& getMultipliers() const; // Stage::Acceleration
00932 
00934     Vector& updYErr() const; // Stage::Velocity-1
00935     Vector& updQErr() const; // Stage::Position-1 (view into YErr)
00936     Vector& updUErr() const; // Stage::Velocity-1        "
00937 
00938     Vector& updUDotErr()     const; // Stage::Acceleration-1 (not a view)
00939     Vector& updMultipliers() const; // Stage::Acceleration-1 (not a view)
00940 
00944     const Stage& getLowestStageModified() const;
00945 
00948     void resetLowestStageModified() const;
00949 
00953     void autoUpdateDiscreteVariables();
00954 
00955     String toString() const;
00956     String cacheToString() const;
00957 
00958 private:
00959     // OBSOLETE
00960     
00961     // This method was misnamed in SimTK 2.0; it has been changed to 
00962     // isCacheValueRealized() to match markCacheValueRealized(). The old name is 
00963     // here as an uncallable private method in the hope of getting a helpful
00964     // error message out of the compiler that will lead you to the correctly-
00965     // named method. I guess if you're reading this, it worked!
00966     bool isCacheValueCurrent(SubsystemIndex sx, CacheEntryIndex cx) const
00967     {   return isCacheValueRealized(sx,cx); }
00968     // Part of our ongoing crusade to turn getN's into getNums for API consistency.
00969     int getNSubsystems() const {return getNumSubsystems();}
00970     void setNSubsystems(int i) {setNumSubsystems(i);}
00971 
00972 private:
00973     class StateImpl* impl;
00974     const StateImpl& getImpl() const {assert(impl); return *impl;}
00975     StateImpl&       updImpl()       {assert(impl); return *impl;}
00976 };
00977 
00978 SimTK_SimTKCOMMON_EXPORT std::ostream& 
00979 operator<<(std::ostream& o, const State& s);
00980 
00981 } // namespace SimTK
00982 
00983 #endif // SimTK_SimTKCOMMON_STATE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines