Simbody  3.4 (development)
State.h
Go to the documentation of this file.
00001 #ifndef SimTK_SimTKCOMMON_STATE_H_
00002 #define SimTK_SimTKCOMMON_STATE_H_
00003 
00004 /* -------------------------------------------------------------------------- *
00005  *                       Simbody(tm): SimTKcommon                             *
00006  * -------------------------------------------------------------------------- *
00007  * This is part of the SimTK 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-12 Stanford University and the Authors.        *
00013  * Authors: Michael Sherman                                                   *
00014  * Contributors: Peter Eastman                                                *
00015  *                                                                            *
00016  * Licensed under the Apache License, Version 2.0 (the "License"); you may    *
00017  * not use this file except in compliance with the License. You may obtain a  *
00018  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0.         *
00019  *                                                                            *
00020  * Unless required by applicable law or agreed to in writing, software        *
00021  * distributed under the License is distributed on an "AS IS" BASIS,          *
00022  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
00023  * See the License for the specific language governing permissions and        *
00024  * limitations under the License.                                             *
00025  * -------------------------------------------------------------------------- */
00026 
00027 #include "SimTKcommon/basics.h"
00028 #include "SimTKcommon/Simmatrix.h"
00029 #include "SimTKcommon/internal/Event.h"
00030 
00031 #include <ostream>
00032 #include <cassert>
00033 #include <set>
00034 
00035 namespace SimTK {
00036 
00037 
00040 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SubsystemIndex);
00041 
00048 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemYIndex);
00049 
00057 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemQIndex);
00061 SimTK_DEFINE_UNIQUE_INDEX_TYPE(QIndex);
00062 
00069 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemUIndex);
00073 SimTK_DEFINE_UNIQUE_INDEX_TYPE(UIndex);
00074 
00081 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemZIndex);
00085 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ZIndex);
00086 
00091 SimTK_DEFINE_UNIQUE_INDEX_TYPE(DiscreteVariableIndex);
00092 
00097 SimTK_DEFINE_UNIQUE_INDEX_TYPE(CacheEntryIndex);
00098 
00104 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemYErrIndex);
00105 
00111 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemQErrIndex);
00115 SimTK_DEFINE_UNIQUE_INDEX_TYPE(QErrIndex);
00116 
00122 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemUErrIndex);
00126 SimTK_DEFINE_UNIQUE_INDEX_TYPE(UErrIndex);
00127 
00133 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemUDotErrIndex);
00137 SimTK_DEFINE_UNIQUE_INDEX_TYPE(UDotErrIndex);
00138 
00144 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemMultiplierIndex);
00148 SimTK_DEFINE_UNIQUE_INDEX_TYPE(MultiplierIndex);
00149 
00154 typedef int StageVersion;
00155 
00156 
00264 class SimTK_SimTKCOMMON_EXPORT State {
00265 public:
00267 State();
00268 ~State();
00269 
00271 void clear();
00272 
00276 void setNumSubsystems(int i);
00277 
00280 void initializeSubsystem(SubsystemIndex, const String& name, 
00281                                          const String& version);
00282 
00287 State(const State&);
00288 
00293 State& operator=(const State&);
00294 
00301 SubsystemIndex addSubsystem(const String& name, const String& version);
00302 
00303 int getNumSubsystems() const;
00304 const String& getSubsystemName   (SubsystemIndex) const;
00305 const String& getSubsystemVersion(SubsystemIndex) const;
00306 const Stage&  getSubsystemStage  (SubsystemIndex) const;
00307 
00309 const Stage& getSystemStage() const;
00310 
00317 void invalidateAll(Stage);
00318 
00326 void invalidateAllCacheAtOrAbove(Stage) const;
00327 
00334 void advanceSubsystemToStage(SubsystemIndex, Stage) const;
00339 void advanceSystemToStage(Stage) const;
00340 
00345 StageVersion getSystemTopologyStageVersion() const;
00346 
00363 QIndex allocateQ(SubsystemIndex, const Vector& qInit);
00371 UIndex allocateU(SubsystemIndex, const Vector& uInit); 
00377 ZIndex allocateZ(SubsystemIndex, const Vector& zInit);
00394 QErrIndex    allocateQErr   (SubsystemIndex, int nqerr) const;   
00397 UErrIndex    allocateUErr   (SubsystemIndex, int nuerr) const;
00402 UDotErrIndex allocateUDotErr(SubsystemIndex, int nudoterr) const;
00421 EventTriggerByStageIndex 
00422 allocateEventTrigger(SubsystemIndex, Stage stage, int nevent) const;
00448 DiscreteVariableIndex 
00449 allocateDiscreteVariable(SubsystemIndex, Stage invalidates, AbstractValue*);
00450 
00502 DiscreteVariableIndex
00503 allocateAutoUpdateDiscreteVariable(SubsystemIndex, Stage invalidates, 
00504                                    AbstractValue*, Stage updateDependsOn); 
00507 CacheEntryIndex 
00508 getDiscreteVarUpdateIndex(SubsystemIndex, DiscreteVariableIndex) const;
00510 Stage getDiscreteVarAllocationStage(SubsystemIndex, DiscreteVariableIndex) const;
00515 Stage getDiscreteVarInvalidatesStage(SubsystemIndex, DiscreteVariableIndex) const;
00516 
00517 
00520 const AbstractValue& 
00521 getDiscreteVariable(SubsystemIndex, DiscreteVariableIndex) const;
00523 Real getDiscreteVarLastUpdateTime(SubsystemIndex, DiscreteVariableIndex) const;
00528 const AbstractValue& 
00529 getDiscreteVarUpdateValue(SubsystemIndex, DiscreteVariableIndex) const;
00535 AbstractValue& 
00536 updDiscreteVarUpdateValue(SubsystemIndex, DiscreteVariableIndex) const;
00540 bool isDiscreteVarUpdateValueRealized(SubsystemIndex, DiscreteVariableIndex) const;
00543 void markDiscreteVarUpdateValueRealized(SubsystemIndex, DiscreteVariableIndex) const;
00544 
00548 AbstractValue& updDiscreteVariable(SubsystemIndex, DiscreteVariableIndex);
00550 void setDiscreteVariable(SubsystemIndex, DiscreteVariableIndex, 
00551                          const AbstractValue&);
00633 CacheEntryIndex allocateCacheEntry(SubsystemIndex, Stage earliest, Stage latest,
00634                                     AbstractValue*) const;
00635 
00640 CacheEntryIndex allocateCacheEntry(SubsystemIndex sx, Stage g, AbstractValue* v) const
00641 {   return allocateCacheEntry(sx, g, g, v); }
00642 
00651 CacheEntryIndex allocateLazyCacheEntry(SubsystemIndex sx, Stage earliest, AbstractValue* v) const
00652 {   return allocateCacheEntry(sx, earliest, Stage::Infinity, v); }
00653 
00656 Stage getCacheEntryAllocationStage(SubsystemIndex, CacheEntryIndex) const;
00657 
00664 const AbstractValue& getCacheEntry(SubsystemIndex, CacheEntryIndex) const;
00665 
00672 AbstractValue& updCacheEntry(SubsystemIndex, CacheEntryIndex) const; // mutable
00673 
00682 bool isCacheValueRealized(SubsystemIndex, CacheEntryIndex) const;
00683 
00696 void markCacheValueRealized(SubsystemIndex, CacheEntryIndex) const;
00697 
00703 void markCacheValueNotRealized(SubsystemIndex, CacheEntryIndex) const;
00706 
00707 
00708 
00709 
00710 
00711 
00712 
00713 
00714 
00715 
00716 
00717 
00718 
00719 
00720 
00721 
00722 
00726 int getNY() const;
00731 int getNQ() const;
00733 SystemYIndex getQStart() const;
00737 int getNU() const;
00739 SystemYIndex getUStart() const;
00743 int getNZ() const;
00745 SystemYIndex getZStart() const;
00749 int getNYErr() const;
00752 int getNQErr() const;
00754 SystemYErrIndex getQErrStart() const; 
00758 int getNUErr() const;
00760 SystemYErrIndex getUErrStart() const; 
00765 int getNUDotErr() const;
00770 int getNMultipliers() const; // =mp+mv+ma, necessarily the same as NUDotErr
00773 int getNEventTriggers() const;
00776 int getNEventTriggersByStage(Stage) const;
00780 SystemEventTriggerIndex getEventTriggerStartByStage(Stage) const; // per-stage
00781 
00783 
00797 
00798 SystemQIndex getQStart(SubsystemIndex) const; 
00799 int getNQ(SubsystemIndex) const;
00800 SystemUIndex getUStart(SubsystemIndex) const; 
00801 int getNU(SubsystemIndex) const;
00802 SystemZIndex getZStart(SubsystemIndex) const; 
00803 int getNZ(SubsystemIndex) const;
00804 
00805 
00806 SystemQErrIndex getQErrStart(SubsystemIndex) const; 
00807 int getNQErr(SubsystemIndex) const;
00808 SystemUErrIndex getUErrStart(SubsystemIndex) const; 
00809 int getNUErr(SubsystemIndex) const;
00810 SystemUDotErrIndex getUDotErrStart(SubsystemIndex) const; 
00811 int getNUDotErr(SubsystemIndex) const;
00812 SystemMultiplierIndex getMultipliersStart(SubsystemIndex) const;
00813 int getNMultipliers(SubsystemIndex) const;
00814 
00815 SystemEventTriggerByStageIndex 
00816     getEventTriggerStartByStage(SubsystemIndex, Stage) const;
00817 int getNEventTriggersByStage(SubsystemIndex, Stage) const;
00818 
00820 
00833 
00836 void mapQToSubsystem(SystemQIndex, SubsystemIndex&, QIndex&) const;
00839 void mapUToSubsystem(SystemUIndex, SubsystemIndex&, UIndex&) const;
00842 void mapZToSubsystem(SystemZIndex, SubsystemIndex&, ZIndex&) const;
00845 void mapQErrToSubsystem(SystemQErrIndex, SubsystemIndex&, QErrIndex&) const;
00848 void mapUErrToSubsystem(SystemUErrIndex, SubsystemIndex&, UErrIndex&) const;
00851 void mapUDotErrToSubsystem(SystemUDotErrIndex, SubsystemIndex&, UDotErrIndex&) const;
00855 void mapMultiplierToSubsystem(SystemMultiplierIndex, SubsystemIndex&, MultiplierIndex&) const;
00858 //void mapEventTriggerToSubsystem(SystemEventTriggerIndex, SubsystemIndex&, EventTriggerIndex&) const;
00861 void mapEventTriggerToStage(SystemEventTriggerIndex, Stage&, SystemEventTriggerByStageIndex&) const;
00864 //void mapSubsystemEventTriggerToStage(EventTriggerIndex, Stage&, EventTriggerByStageIndex&) const;
00865 
00867 
00868 const Vector& getEventTriggers() const;
00869 const Vector& getEventTriggersByStage(Stage) const;
00870 const Vector& getEventTriggersByStage(SubsystemIndex, Stage) const;
00871 
00872 Vector& updEventTriggers() const; // mutable
00873 Vector& updEventTriggersByStage(Stage) const;
00874 Vector& updEventTriggersByStage(SubsystemIndex, Stage) const;
00875 
00877 const Vector& getQ(SubsystemIndex) const;
00878 const Vector& getU(SubsystemIndex) const;
00879 const Vector& getZ(SubsystemIndex) const;
00880 
00881 const Vector& getUWeights(SubsystemIndex) const;
00882 const Vector& getZWeights(SubsystemIndex) const;
00883 
00884 Vector& updQ(SubsystemIndex);
00885 Vector& updU(SubsystemIndex);
00886 Vector& updZ(SubsystemIndex);
00887 
00888 Vector& updUWeights(SubsystemIndex);
00889 Vector& updZWeights(SubsystemIndex);
00890 
00892 const Vector& getQDot(SubsystemIndex) const;
00893 const Vector& getUDot(SubsystemIndex) const;
00894 const Vector& getZDot(SubsystemIndex) const;
00895 const Vector& getQDotDot(SubsystemIndex) const;
00896 
00897 Vector& updQDot(SubsystemIndex) const;    // these are mutable
00898 Vector& updUDot(SubsystemIndex) const;
00899 Vector& updZDot(SubsystemIndex) const;
00900 Vector& updQDotDot(SubsystemIndex) const;
00901 
00902 const Vector& getQErr(SubsystemIndex) const;
00903 const Vector& getUErr(SubsystemIndex) const;
00904 const Vector& getUDotErr(SubsystemIndex) const;
00905 const Vector& getMultipliers(SubsystemIndex) const;
00906 
00907 const Vector& getQErrWeights(SubsystemIndex) const;
00908 const Vector& getUErrWeights(SubsystemIndex) const;
00909 
00910 Vector& updQErr(SubsystemIndex) const;    // these are mutable
00911 Vector& updUErr(SubsystemIndex) const;
00912 Vector& updUDotErr(SubsystemIndex) const;
00913 Vector& updMultipliers(SubsystemIndex) const;
00914 
00915 Vector& updQErrWeights(SubsystemIndex);
00916 Vector& updUErrWeights(SubsystemIndex);
00917 
00919 const Real&   getTime() const;
00920 const Vector& getY() const; // {Q,U,Z} packed and in that order
00921 
00923 const Vector& getQ() const;
00924 const Vector& getU() const;
00925 const Vector& getZ() const;
00926 
00927 
00962 const Vector& getUWeights() const;    // diag(Wu)
00963 
00970 const Vector& getZWeights() const;
00971 
00975 Vector& updUWeights();
00976 
00980 Vector& updZWeights();
00981 
00984 Real&   updTime();  // Back up to Stage::Time-1
00985 Vector& updY();     // Back up to Stage::Dynamics-1
00986 
00988 void setTime(Real t);
00989 void setY(const Vector& y);
00990 
00992 Vector& updQ();     // Back up to Stage::Position-1
00993 Vector& updU();     // Back up to Stage::Velocity-1
00994 Vector& updZ();     // Back up to Stage::Dynamics-1
00995 
00997 void setQ(const Vector& q);
00998 void setU(const Vector& u);
00999 void setZ(const Vector& z);
01000 
01001 const Vector& getYDot()    const; // Stage::Acceleration
01002 
01004 const Vector& getQDot()    const; // Stage::Velocity
01005 const Vector& getZDot()    const; // Stage::Dynamics
01006 const Vector& getUDot()    const; // Stage::Acceleration
01007 
01009 const Vector& getQDotDot() const; // Stage::Acceleration
01010 
01012 Vector& updYDot() const;    // Stage::Acceleration-1
01013 Vector& updQDot() const;    // Stage::Velocity-1     (view into YDot)
01014 Vector& updZDot() const;    // Stage::Dynamics-1            "
01015 Vector& updUDot() const;    // Stage::Acceleration-1        "
01016 
01020 Vector& updQDotDot() const; // Stage::Acceleration-1
01021 
01024 const Vector& getYErr() const;  // Stage::Velocity
01025 
01027 const Vector& getQErr() const;  // Stage::Position (index 3 constraints)
01028 const Vector& getUErr() const;  // Stage::Velocity (index 2 constraints)
01029 
01031 const Vector& getUDotErr()     const; // Stage::Acceleration (index 1 constraints)
01032 const Vector& getMultipliers() const; // Stage::Acceleration
01033 
01036 const Vector& getQErrWeights() const;
01037 
01048 const Vector& getUErrWeights() const;
01049 
01054 Vector& updQErrWeights();
01055 
01060 Vector& updUErrWeights();
01061 
01063 Vector& updYErr() const; // Stage::Velocity-1
01064 Vector& updQErr() const; // Stage::Position-1 (view into YErr)
01065 Vector& updUErr() const; // Stage::Velocity-1        "
01066 
01067 Vector& updUDotErr()     const; // Stage::Acceleration-1 (not a view)
01068 Vector& updMultipliers() const; // Stage::Acceleration-1 (not a view)
01069 
01077 void getSystemStageVersions(Array_<StageVersion>& versions) const;
01078 
01088 Stage getLowestSystemStageDifference
01089    (const Array_<StageVersion>& prevVersions) const;
01090 
01097 void setSystemTopologyStageVersion(StageVersion topoVersion);
01098 
01102 void autoUpdateDiscreteVariables();
01103 
01104 String toString() const;
01105 String cacheToString() const;
01106 
01107 //------------------------------------------------------------------------------
01108                                 private:
01109 class StateImpl* impl;
01110 const StateImpl& getImpl() const {assert(impl); return *impl;}
01111 StateImpl&       updImpl()       {assert(impl); return *impl;}
01112 };
01113 
01114 SimTK_SimTKCOMMON_EXPORT std::ostream& 
01115 operator<<(std::ostream& o, const State& s);
01116 
01117 } // namespace SimTK
01118 
01119 #endif // SimTK_SimTKCOMMON_STATE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines