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 
00047 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SubsystemIndex);
00048 
00054 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemYIndex);
00055 
00062 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemQIndex);
00065 SimTK_DEFINE_UNIQUE_INDEX_TYPE(QIndex);
00066 
00072 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemUIndex);
00075 SimTK_DEFINE_UNIQUE_INDEX_TYPE(UIndex);
00076 
00082 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemZIndex);
00085 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ZIndex);
00086 
00090 SimTK_DEFINE_UNIQUE_INDEX_TYPE(DiscreteVariableIndex);
00091 
00095 SimTK_DEFINE_UNIQUE_INDEX_TYPE(CacheEntryIndex);
00096 
00101 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemYErrIndex);
00102 
00107 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemQErrIndex);
00110 SimTK_DEFINE_UNIQUE_INDEX_TYPE(QErrIndex);
00111 
00116 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemUErrIndex);
00119 SimTK_DEFINE_UNIQUE_INDEX_TYPE(UErrIndex);
00120 
00125 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemUDotErrIndex);
00128 SimTK_DEFINE_UNIQUE_INDEX_TYPE(UDotErrIndex);
00129 
00134 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemMultiplierIndex);
00137 SimTK_DEFINE_UNIQUE_INDEX_TYPE(MultiplierIndex);
00138 
00143 typedef int StageVersion;
00144 
00145 
00260 class SimTK_SimTKCOMMON_EXPORT State {
00261 public:
00263     State();
00264     ~State();
00265 
00267     void clear();
00268 
00272     void setNSubsystems(int i);
00273 
00276     void initializeSubsystem(SubsystemIndex, const String& name, const String& version);
00277 
00282     State(const State&);
00283 
00288     State& operator=(const State&);
00289 
00296     SubsystemIndex addSubsystem(const String& name, const String& version);
00297 
00298     int getNSubsystems() const;
00299     const String& getSubsystemName   (SubsystemIndex) const;
00300     const String& getSubsystemVersion(SubsystemIndex) const;
00301     const Stage&  getSubsystemStage  (SubsystemIndex) const;
00302 
00304     const Stage& getSystemStage() const;
00305 
00309     void invalidateAll(Stage) const;  // cache is mutable
00310 
00317     void advanceSubsystemToStage(SubsystemIndex, Stage) const;
00322     void advanceSystemToStage(Stage) const;
00323 
00332 
00333     QIndex allocateQ(SubsystemIndex, const Vector& qInit); // qdot, qdotdot also allocated in cache
00334     UIndex allocateU(SubsystemIndex, const Vector& uInit); // udot                    "
00335     ZIndex allocateZ(SubsystemIndex, const Vector& zInit); // zdot                    "
00336 
00347 
00348     QErrIndex    allocateQErr   (SubsystemIndex, int nqerr) const;    // these are cache entries
00349     UErrIndex    allocateUErr   (SubsystemIndex, int nuerr) const;
00350     UDotErrIndex allocateUDotErr(SubsystemIndex, int nudoterr) const;
00351 
00361     EventIndex allocateEventIndex(SubsystemIndex, int nevent=1) const;
00362 
00374     EventTriggerByStageIndex allocateEventTrigger(SubsystemIndex, Stage, EventIndex, int nevent=1) const;
00375 
00376     //OBSOLETE
00377     EventTriggerByStageIndex allocateEventTrigger(SubsystemIndex, Stage, int nevent) const;
00378 
00379 
00389 
00402     DiscreteVariableIndex allocateDiscreteVariable(SubsystemIndex, Stage invalidates, AbstractValue*);
00403 
00452     DiscreteVariableIndex
00453         allocateAutoUpdateDiscreteVariable(SubsystemIndex, Stage invalidates, AbstractValue*,
00454                                            Stage updateDependsOn); 
00458     CacheEntryIndex getDiscreteVarUpdateIndex(SubsystemIndex, DiscreteVariableIndex) const;
00461     Stage getDiscreteVarAllocationStage(SubsystemIndex, DiscreteVariableIndex) const;
00466     Stage getDiscreteVarInvalidatesStage(SubsystemIndex, DiscreteVariableIndex) const;
00467 
00468 
00471     const AbstractValue& getDiscreteVariable(SubsystemIndex, DiscreteVariableIndex) const;
00473     Real getDiscreteVarLastUpdateTime(SubsystemIndex, DiscreteVariableIndex) const;
00478     const AbstractValue& getDiscreteVarUpdateValue(SubsystemIndex, DiscreteVariableIndex) const;
00484     AbstractValue& updDiscreteVarUpdateValue(SubsystemIndex, DiscreteVariableIndex) const;
00487     bool isDiscreteVarUpdateValueRealized(SubsystemIndex, DiscreteVariableIndex) const;
00490     void markDiscreteVarUpdateValueRealized(SubsystemIndex, DiscreteVariableIndex) const;
00491 
00495     AbstractValue& updDiscreteVariable(SubsystemIndex, DiscreteVariableIndex);
00497     void setDiscreteVariable(SubsystemIndex, DiscreteVariableIndex, const AbstractValue&);
00499 
00510 
00579     CacheEntryIndex allocateCacheEntry(SubsystemIndex, Stage earliest, Stage latest,
00580                                        AbstractValue*) const;
00581 
00586     CacheEntryIndex allocateCacheEntry(SubsystemIndex sx, Stage g, AbstractValue* v) const
00587     {   return allocateCacheEntry(sx, g, g, v); }
00588 
00597     CacheEntryIndex allocateLazyCacheEntry(SubsystemIndex sx, Stage earliest, AbstractValue* v) const
00598     {   return allocateCacheEntry(sx, earliest, Stage::Infinity, v); }
00599 
00602     Stage getCacheEntryAllocationStage(SubsystemIndex, CacheEntryIndex) const;
00603 
00610     const AbstractValue& getCacheEntry(SubsystemIndex, CacheEntryIndex) const;
00611 
00618     AbstractValue& updCacheEntry(SubsystemIndex, CacheEntryIndex) const; // mutable
00619 
00628     bool isCacheValueRealized(SubsystemIndex, CacheEntryIndex) const;
00629 
00642     void markCacheValueRealized(SubsystemIndex, CacheEntryIndex) const;
00644 
00661 
00665     int getNY() const;
00670     int getNQ() const;
00672     SystemYIndex getQStart() const;
00676     int getNU() const;
00678     SystemYIndex getUStart() const;
00682     int getNZ() const;
00684     SystemYIndex getZStart() const;
00688     int getNYErr() const;
00691     int getNQErr() const;
00693     SystemYErrIndex getQErrStart() const; 
00697     int getNUErr() const;
00699     SystemYErrIndex getUErrStart() const; 
00704     int getNUDotErr() const;
00709     int getNMultipliers() const; // =mp+mv+ma, necessarily the same as NUDotErr
00712     int getNEventTriggers() const;
00715     int getNEventTriggersByStage(Stage) const;
00719     SystemEventTriggerIndex getEventTriggerStartByStage(Stage) const; // per-stage
00720 
00722 
00736 
00737     SystemQIndex getQStart(SubsystemIndex)       const; int getNQ(SubsystemIndex)       const;
00738     SystemUIndex getUStart(SubsystemIndex)       const; int getNU(SubsystemIndex)       const;
00739     SystemZIndex getZStart(SubsystemIndex)       const; int getNZ(SubsystemIndex)       const;
00740 
00741     SystemQErrIndex       getQErrStart(SubsystemIndex)    const; int getNQErr(SubsystemIndex)    const;
00742     SystemUErrIndex       getUErrStart(SubsystemIndex)    const; int getNUErr(SubsystemIndex)    const;
00743     SystemUDotErrIndex    getUDotErrStart(SubsystemIndex) const; int getNUDotErr(SubsystemIndex) const;
00744     SystemMultiplierIndex getMultipliersStart(SubsystemIndex) const;
00745     int getNMultipliers(SubsystemIndex)     const;
00746 
00747     SystemEventTriggerByStageIndex getEventTriggerStartByStage(SubsystemIndex, Stage) const;
00748     int getNEventTriggersByStage(SubsystemIndex, Stage) const;
00749 
00751 
00762 
00765     void mapQToSubsystem(SystemQIndex, SubsystemIndex&, QIndex&) const;
00768     void mapUToSubsystem(SystemUIndex, SubsystemIndex&, UIndex&) const;
00771     void mapZToSubsystem(SystemZIndex, SubsystemIndex&, ZIndex&) const;
00774     void mapQErrToSubsystem(SystemQErrIndex, SubsystemIndex&, QErrIndex&) const;
00777     void mapUErrToSubsystem(SystemUErrIndex, SubsystemIndex&, UErrIndex&) const;
00780     void mapUDotErrToSubsystem(SystemUDotErrIndex, SubsystemIndex&, UDotErrIndex&) const;
00784     void mapMultiplierToSubsystem(SystemMultiplierIndex, SubsystemIndex&, MultiplierIndex&) const;
00787     void mapEventTriggerToSubsystem(SystemEventTriggerIndex, SubsystemIndex&, EventTriggerIndex&) const;
00790     void mapEventTriggerToStage(SystemEventTriggerIndex, Stage&, SystemEventTriggerByStageIndex&) const;
00793     void mapSubsystemEventTriggerToStage(EventTriggerIndex, Stage&, EventTriggerByStageIndex&) const;
00794 
00796 
00797     const Vector& getEventTriggers() const;
00798     const Vector& getEventTriggersByStage(Stage) const;
00799     const Vector& getEventTriggersByStage(SubsystemIndex, Stage) const;
00800 
00801     Vector& updEventTriggers() const; // mutable
00802     Vector& updEventTriggersByStage(Stage) const;
00803     Vector& updEventTriggersByStage(SubsystemIndex, Stage) const;
00804 
00806     const Vector& getQ(SubsystemIndex) const;
00807     const Vector& getU(SubsystemIndex) const;
00808     const Vector& getZ(SubsystemIndex) const;
00809 
00810     Vector& updQ(SubsystemIndex);
00811     Vector& updU(SubsystemIndex);
00812     Vector& updZ(SubsystemIndex);
00813 
00815     const Vector& getQDot(SubsystemIndex) const;
00816     const Vector& getUDot(SubsystemIndex) const;
00817     const Vector& getZDot(SubsystemIndex) const;
00818     const Vector& getQDotDot(SubsystemIndex) const;
00819 
00820     Vector& updQDot(SubsystemIndex) const;    // these are mutable
00821     Vector& updUDot(SubsystemIndex) const;
00822     Vector& updZDot(SubsystemIndex) const;
00823     Vector& updQDotDot(SubsystemIndex) const;
00824 
00825     const Vector& getQErr(SubsystemIndex) const;
00826     const Vector& getUErr(SubsystemIndex) const;
00827     const Vector& getUDotErr(SubsystemIndex) const;
00828     const Vector& getMultipliers(SubsystemIndex) const;
00829     Vector& updQErr(SubsystemIndex) const;    // these are mutable
00830     Vector& updUErr(SubsystemIndex) const;
00831     Vector& updUDotErr(SubsystemIndex) const;
00832     Vector& updMultipliers(SubsystemIndex) const;
00833 
00835     const Real&   getTime() const;
00836     const Vector& getY() const; // {Q,U,Z} packed and in that order
00837 
00839     const Vector& getQ() const;
00840     const Vector& getU() const;
00841     const Vector& getZ() const;
00842 
00845     Real&   updTime();  // Back up to Stage::Time-1
00846     Vector& updY();     // Back up to Stage::Dynamics-1
00847 
00849     void setTime(Real t);
00850     void setY(const Vector& y);
00851 
00853     Vector& updQ();     // Back up to Stage::Position-1
00854     Vector& updU();     // Back up to Stage::Velocity-1
00855     Vector& updZ();     // Back up to Stage::Dynamics-1
00856 
00858     void setQ(const Vector& q);
00859     void setU(const Vector& u);
00860     void setZ(const Vector& z);
00861 
00862     const Vector& getYDot()    const; // Stage::Acceleration
00863 
00865     const Vector& getQDot()    const; // Stage::Velocity
00866     const Vector& getZDot()    const; // Stage::Dynamics
00867     const Vector& getUDot()    const; // Stage::Acceleration
00868 
00870     const Vector& getQDotDot() const; // Stage::Acceleration
00871 
00873     Vector& updYDot() const;    // Stage::Acceleration-1
00874     Vector& updQDot() const;    // Stage::Velocity-1     (view into YDot)
00875     Vector& updZDot() const;    // Stage::Dynamics-1            "
00876     Vector& updUDot() const;    // Stage::Acceleration-1        "
00877 
00881     Vector& updQDotDot() const; // Stage::Acceleration-1
00882 
00885     const Vector& getYErr() const;  // Stage::Velocity
00886 
00888     const Vector& getQErr() const;  // Stage::Position (index 3 constraints)
00889     const Vector& getUErr() const;  // Stage::Velocity (index 2 constraints)
00890 
00892     const Vector& getUDotErr()     const; // Stage::Acceleration (index 1 constraints)
00893     const Vector& getMultipliers() const; // Stage::Acceleration
00894 
00896     Vector& updYErr() const; // Stage::Velocity-1
00897     Vector& updQErr() const; // Stage::Position-1 (view into YErr)
00898     Vector& updUErr() const; // Stage::Velocity-1        "
00899 
00900     Vector& updUDotErr()     const; // Stage::Acceleration-1 (not a view)
00901     Vector& updMultipliers() const; // Stage::Acceleration-1 (not a view)
00902 
00906     const Stage& getLowestStageModified() const;
00907 
00910     void resetLowestStageModified() const;
00911 
00915     void autoUpdateDiscreteVariables();
00916     
00927     void createRestrictedState(State&                       restrictedState, 
00928                                EnumerationSet<Stage>        restrictedStages,
00929                                std::set<SubsystemIndex>     restrictedSubsystems);
00930 
00933     const EnumerationSet<Stage>& getRestrictedStages() const;
00934 
00937     const std::set<SubsystemIndex>& getRestrictedSubsystems() const;
00938 
00939     String toString() const;
00940     String cacheToString() const;
00941 
00942 private:
00943     // OBSOLETE: This method was misnamed in SimTK 2.0; it has been changed to 
00944     // isCacheValueRealized() to match markCacheValueRealized(). The old name is 
00945     // here as an uncallable private method in the hope of getting a helpful
00946     // error message out of the compiler that will lead you to the correctly-
00947     // named method. I guess if you're reading this, it worked!
00948     bool isCacheValueCurrent(SubsystemIndex sx, CacheEntryIndex cx) const
00949     {   return isCacheValueRealized(sx,cx); }
00950 
00951 private:
00952     class StateRep* rep;
00953     const StateRep& getRep() const {assert(rep); return *rep;}
00954     StateRep&       updRep()       {assert(rep); return *rep;}
00955 };
00956 
00957 SimTK_SimTKCOMMON_EXPORT std::ostream& 
00958 operator<<(std::ostream& o, const State& s);
00959 
00960 } // namespace SimTK
00961 
00962 #endif // SimTK_SimTKCOMMON_STATE_H_

Generated on Thu Aug 12 16:37:27 2010 for SimTKcore by  doxygen 1.6.1