Simbody
3.5
|
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-14 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 00031 // Note: the StateImpl.h header is included at the end of this file to 00032 // complete the inline part of the State definition. 00033 #include "SimTKcommon/basics.h" 00034 #include "SimTKcommon/Simmatrix.h" 00035 #include "SimTKcommon/internal/Event.h" 00036 00037 #include <ostream> 00038 #include <cassert> 00039 #include <algorithm> 00040 00041 namespace SimTK { 00042 00043 00046 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SubsystemIndex); 00047 00054 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemYIndex); 00055 00063 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemQIndex); 00067 SimTK_DEFINE_UNIQUE_INDEX_TYPE(QIndex); 00068 00075 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemUIndex); 00079 SimTK_DEFINE_UNIQUE_INDEX_TYPE(UIndex); 00080 00087 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemZIndex); 00091 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ZIndex); 00092 00097 SimTK_DEFINE_UNIQUE_INDEX_TYPE(DiscreteVariableIndex); 00098 00103 SimTK_DEFINE_UNIQUE_INDEX_TYPE(CacheEntryIndex); 00104 00110 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemYErrIndex); 00111 00117 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemQErrIndex); 00121 SimTK_DEFINE_UNIQUE_INDEX_TYPE(QErrIndex); 00122 00128 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemUErrIndex); 00132 SimTK_DEFINE_UNIQUE_INDEX_TYPE(UErrIndex); 00133 00139 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemUDotErrIndex); 00143 SimTK_DEFINE_UNIQUE_INDEX_TYPE(UDotErrIndex); 00144 00150 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemMultiplierIndex); 00154 SimTK_DEFINE_UNIQUE_INDEX_TYPE(MultiplierIndex); 00155 00160 typedef int StageVersion; 00161 00162 00276 class SimTK_SimTKCOMMON_EXPORT State { 00277 public: 00279 State(); 00280 00287 State(const State&); 00288 00295 State& operator=(const State&); 00296 00298 ~State(); 00299 00301 void clear(); 00302 00306 inline void setNumSubsystems(int i); 00307 00310 inline void initializeSubsystem(SubsystemIndex, const String& name, 00311 const String& version); 00312 00319 inline SubsystemIndex addSubsystem(const String& name, const String& version); 00320 00322 inline int getNumSubsystems() const; 00323 inline const String& getSubsystemName (SubsystemIndex) const; 00324 inline const String& getSubsystemVersion(SubsystemIndex) const; 00325 inline const Stage& getSubsystemStage (SubsystemIndex) const; 00326 00328 inline const Stage& getSystemStage() const; 00329 00336 inline void invalidateAll(Stage); 00337 00345 inline void invalidateAllCacheAtOrAbove(Stage) const; 00346 00353 inline void advanceSubsystemToStage(SubsystemIndex, Stage) const; 00358 inline void advanceSystemToStage(Stage) const; 00359 00364 inline StageVersion getSystemTopologyStageVersion() const; 00365 00382 inline QIndex allocateQ(SubsystemIndex, const Vector& qInit); 00390 inline UIndex allocateU(SubsystemIndex, const Vector& uInit); 00396 inline ZIndex allocateZ(SubsystemIndex, const Vector& zInit); 00413 inline QErrIndex allocateQErr (SubsystemIndex, int nqerr) const; 00416 inline UErrIndex allocateUErr (SubsystemIndex, int nuerr) const; 00421 inline UDotErrIndex allocateUDotErr(SubsystemIndex, int nudoterr) const; 00440 inline EventTriggerByStageIndex 00441 allocateEventTrigger(SubsystemIndex, Stage stage, int nevent) const; 00468 inline DiscreteVariableIndex 00469 allocateDiscreteVariable(SubsystemIndex, Stage invalidates, AbstractValue*); 00470 00521 inline DiscreteVariableIndex 00522 allocateAutoUpdateDiscreteVariable(SubsystemIndex, Stage invalidates, 00523 AbstractValue*, Stage updateDependsOn); 00526 inline CacheEntryIndex 00527 getDiscreteVarUpdateIndex(SubsystemIndex, DiscreteVariableIndex) const; 00530 inline Stage 00531 getDiscreteVarAllocationStage(SubsystemIndex, DiscreteVariableIndex) const; 00536 inline Stage 00537 getDiscreteVarInvalidatesStage(SubsystemIndex, DiscreteVariableIndex) const; 00538 00539 00542 inline const AbstractValue& 00543 getDiscreteVariable(SubsystemIndex, DiscreteVariableIndex) const; 00545 inline Real 00546 getDiscreteVarLastUpdateTime(SubsystemIndex, DiscreteVariableIndex) const; 00551 inline const AbstractValue& 00552 getDiscreteVarUpdateValue(SubsystemIndex, DiscreteVariableIndex) const; 00558 inline AbstractValue& 00559 updDiscreteVarUpdateValue(SubsystemIndex, DiscreteVariableIndex) const; 00563 inline bool 00564 isDiscreteVarUpdateValueRealized(SubsystemIndex, DiscreteVariableIndex) const; 00567 inline void 00568 markDiscreteVarUpdateValueRealized(SubsystemIndex, DiscreteVariableIndex) const; 00569 00573 inline AbstractValue& 00574 updDiscreteVariable(SubsystemIndex, DiscreteVariableIndex); 00576 inline void 00577 setDiscreteVariable 00578 (SubsystemIndex, DiscreteVariableIndex, const AbstractValue&); 00660 inline CacheEntryIndex 00661 allocateCacheEntry(SubsystemIndex, Stage earliest, Stage latest, 00662 AbstractValue*) const; 00663 00668 inline CacheEntryIndex 00669 allocateCacheEntry(SubsystemIndex sx, Stage g, AbstractValue* v) const 00670 { return allocateCacheEntry(sx, g, g, v); } 00671 00680 inline CacheEntryIndex 00681 allocateLazyCacheEntry 00682 (SubsystemIndex sx, Stage earliest, AbstractValue* v) const 00683 { return allocateCacheEntry(sx, earliest, Stage::Infinity, v); } 00684 00687 inline Stage 00688 getCacheEntryAllocationStage(SubsystemIndex, CacheEntryIndex) const; 00689 00696 inline const AbstractValue& 00697 getCacheEntry(SubsystemIndex, CacheEntryIndex) const; 00698 00705 inline AbstractValue& 00706 updCacheEntry(SubsystemIndex, CacheEntryIndex) const; // mutable 00707 00716 inline bool isCacheValueRealized(SubsystemIndex, CacheEntryIndex) const; 00717 00730 inline void markCacheValueRealized(SubsystemIndex, CacheEntryIndex) const; 00731 00737 inline void markCacheValueNotRealized(SubsystemIndex, CacheEntryIndex) const; 00740 00741 00742 00743 00744 00745 00746 00747 00748 00749 00750 00751 00752 00753 00754 00755 00756 00760 inline int getNY() const; 00765 inline int getNQ() const; 00767 inline SystemYIndex getQStart() const; 00771 inline int getNU() const; 00773 inline SystemYIndex getUStart() const; 00777 inline int getNZ() const; 00779 inline SystemYIndex getZStart() const; 00783 inline int getNYErr() const; 00786 inline int getNQErr() const; 00788 inline SystemYErrIndex getQErrStart() const; 00792 inline int getNUErr() const; 00794 inline SystemYErrIndex getUErrStart() const; 00799 inline int getNUDotErr() const; 00804 inline int getNMultipliers() const; // =mp+mv+ma, necessarily the same as NUDotErr 00807 inline int getNEventTriggers() const; 00810 inline int getNEventTriggersByStage(Stage) const; 00814 inline SystemEventTriggerIndex getEventTriggerStartByStage(Stage) const; 00815 00817 00831 00832 inline SystemQIndex getQStart(SubsystemIndex) const; 00833 inline int getNQ(SubsystemIndex) const; 00834 inline SystemUIndex getUStart(SubsystemIndex) const; 00835 inline int getNU(SubsystemIndex) const; 00836 inline SystemZIndex getZStart(SubsystemIndex) const; 00837 inline int getNZ(SubsystemIndex) const; 00838 00839 inline SystemQErrIndex getQErrStart(SubsystemIndex) const; 00840 inline int getNQErr(SubsystemIndex) const; 00841 inline SystemUErrIndex getUErrStart(SubsystemIndex) const; 00842 inline int getNUErr(SubsystemIndex) const; 00843 inline SystemUDotErrIndex getUDotErrStart(SubsystemIndex) const; 00844 inline int getNUDotErr(SubsystemIndex) const; 00845 inline SystemMultiplierIndex getMultipliersStart(SubsystemIndex) const; 00846 inline int getNMultipliers(SubsystemIndex) const; 00847 00848 inline SystemEventTriggerByStageIndex 00849 getEventTriggerStartByStage(SubsystemIndex, Stage) const; 00850 inline int getNEventTriggersByStage(SubsystemIndex, Stage) const; 00851 00853 00854 inline const Vector& getEventTriggers() const; 00855 inline const Vector& getEventTriggersByStage(Stage) const; 00856 inline const Vector& getEventTriggersByStage(SubsystemIndex, Stage) const; 00857 00858 inline Vector& updEventTriggers() const; // mutable 00859 inline Vector& updEventTriggersByStage(Stage) const; 00860 inline Vector& updEventTriggersByStage(SubsystemIndex, Stage) const; 00861 00863 inline const Vector& getQ(SubsystemIndex) const; 00864 inline const Vector& getU(SubsystemIndex) const; 00865 inline const Vector& getZ(SubsystemIndex) const; 00866 00867 inline const Vector& getUWeights(SubsystemIndex) const; 00868 inline const Vector& getZWeights(SubsystemIndex) const; 00869 00870 inline Vector& updQ(SubsystemIndex); 00871 inline Vector& updU(SubsystemIndex); 00872 inline Vector& updZ(SubsystemIndex); 00873 00874 inline Vector& updUWeights(SubsystemIndex); 00875 inline Vector& updZWeights(SubsystemIndex); 00876 00878 inline const Vector& getQDot(SubsystemIndex) const; 00879 inline const Vector& getUDot(SubsystemIndex) const; 00880 inline const Vector& getZDot(SubsystemIndex) const; 00881 inline const Vector& getQDotDot(SubsystemIndex) const; 00882 00883 inline Vector& updQDot(SubsystemIndex) const; // these are mutable 00884 inline Vector& updUDot(SubsystemIndex) const; 00885 inline Vector& updZDot(SubsystemIndex) const; 00886 inline Vector& updQDotDot(SubsystemIndex) const; 00887 00888 inline const Vector& getQErr(SubsystemIndex) const; 00889 inline const Vector& getUErr(SubsystemIndex) const; 00890 inline const Vector& getUDotErr(SubsystemIndex) const; 00891 inline const Vector& getMultipliers(SubsystemIndex) const; 00892 00893 inline const Vector& getQErrWeights(SubsystemIndex) const; 00894 inline const Vector& getUErrWeights(SubsystemIndex) const; 00895 00896 inline Vector& updQErr(SubsystemIndex) const; // these are mutable 00897 inline Vector& updUErr(SubsystemIndex) const; 00898 inline Vector& updUDotErr(SubsystemIndex) const; 00899 inline Vector& updMultipliers(SubsystemIndex) const; 00900 00901 inline Vector& updQErrWeights(SubsystemIndex); 00902 inline Vector& updUErrWeights(SubsystemIndex); 00903 00905 inline const Real& getTime() const; 00906 inline const Vector& getY() const; // {Q,U,Z} packed and in that order 00907 00909 inline const Vector& getQ() const; 00910 inline const Vector& getU() const; 00911 inline const Vector& getZ() const; 00912 00913 00948 inline const Vector& getUWeights() const; // diag(Wu) 00949 00956 inline const Vector& getZWeights() const; 00957 00961 inline Vector& updUWeights(); 00962 00966 inline Vector& updZWeights(); 00967 00970 inline Real& updTime(); // Back up to Stage::Time-1 00971 inline Vector& updY(); // Back up to Stage::Dynamics-1 00972 00974 inline void setTime(Real t); 00975 inline void setY(const Vector& y); 00976 00978 inline Vector& updQ(); // Back up to Stage::Position-1 00979 inline Vector& updU(); // Back up to Stage::Velocity-1 00980 inline Vector& updZ(); // Back up to Stage::Dynamics-1 00981 00983 inline void setQ(const Vector& q); 00984 inline void setU(const Vector& u); 00985 inline void setZ(const Vector& z); 00986 00987 inline const Vector& getYDot() const; // Stage::Acceleration 00988 00990 inline const Vector& getQDot() const; // Stage::Velocity 00991 inline const Vector& getZDot() const; // Stage::Dynamics 00992 inline const Vector& getUDot() const; // Stage::Acceleration 00993 00995 inline const Vector& getQDotDot() const; // Stage::Acceleration 00996 00998 inline Vector& updYDot() const; // Stage::Acceleration-1 00999 inline Vector& updQDot() const; // Stage::Velocity-1 (view into YDot) 01000 inline Vector& updZDot() const; // Stage::Dynamics-1 " 01001 inline Vector& updUDot() const; // Stage::Acceleration-1 " 01002 01006 inline Vector& updQDotDot() const; // Stage::Acceleration-1 01007 01010 inline const Vector& getYErr() const; // Stage::Velocity 01011 01013 inline const Vector& getQErr() const; // Stage::Position (index 3 constraints) 01014 inline const Vector& getUErr() const; // Stage::Velocity (index 2 constraints) 01015 01017 inline const Vector& getUDotErr() const; // Stage::Acceleration (index 1 constraints) 01018 inline const Vector& getMultipliers() const; // Stage::Acceleration 01019 01022 inline const Vector& getQErrWeights() const; 01023 01034 inline const Vector& getUErrWeights() const; 01035 01040 inline Vector& updQErrWeights(); 01041 01046 inline Vector& updUErrWeights(); 01047 01049 inline Vector& updYErr() const; // Stage::Velocity-1 01050 inline Vector& updQErr() const; // Stage::Position-1 (view into YErr) 01051 inline Vector& updUErr() const; // Stage::Velocity-1 " 01052 01053 inline Vector& updUDotErr() const; // Stage::Acceleration-1 (not a view) 01054 inline Vector& updMultipliers() const; // Stage::Acceleration-1 (not a view) 01055 01063 inline void getSystemStageVersions(Array_<StageVersion>& versions) const; 01064 01074 inline Stage getLowestSystemStageDifference 01075 (const Array_<StageVersion>& prevVersions) const; 01076 01083 inline void setSystemTopologyStageVersion(StageVersion topoVersion); 01084 01088 inline void autoUpdateDiscreteVariables(); 01089 01090 inline String toString() const; 01091 inline String cacheToString() const; 01092 01093 //------------------------------------------------------------------------------ 01094 // The implementation class and associated inline methods are defined in a 01095 // separate header file included below. 01096 private: 01097 class StateImpl* impl; 01098 const StateImpl& getImpl() const {assert(impl); return *impl;} 01099 StateImpl& updImpl() {assert(impl); return *impl;} 01100 }; 01101 01102 // Dump state and cache to a stream for debugging; this is not serialization. 01103 SimTK_SimTKCOMMON_EXPORT std::ostream& 01104 operator<<(std::ostream& o, const State& s); 01105 01106 } // namespace SimTK 01107 01108 // This completes the inline definition of State. 01109 #include "SimTKcommon/internal/StateImpl.h" 01110 01111 #endif // SimTK_SimTKCOMMON_STATE_H_