Subsystem.h
Go to the documentation of this file.00001 #ifndef SimTK_SimTKCOMMON_SUBSYSTEM_H_
00002 #define SimTK_SimTKCOMMON_SUBSYSTEM_H_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "SimTKcommon/basics.h"
00036 #include "SimTKcommon/Simmatrix.h"
00037 #include "SimTKcommon/internal/State.h"
00038 #include "SimTKcommon/internal/Measure.h"
00039 #include "SimTKcommon/internal/System.h"
00040
00041 #include <cassert>
00042
00043 namespace SimTK {
00044
00045 class System;
00046 class DecorativeGeometry;
00047 class DefaultSystemSubsystemGuts;
00048 class ScheduledEventHandler;
00049 class ScheduledEventReporter;
00050 class TriggeredEventHandler;
00051 class TriggeredEventReporter;
00052
00075 class SimTK_SimTKCOMMON_EXPORT Subsystem {
00076 public:
00077 class Guts;
00078 friend class Guts;
00079 private:
00080
00081
00082 Guts* guts;
00083 public:
00084 Subsystem() : guts(0) { }
00085 Subsystem(const Subsystem&);
00086 Subsystem& operator=(const Subsystem&);
00087 ~Subsystem();
00088
00089 const String& getName() const;
00090 const String& getVersion() const;
00091
00092
00093
00094 QIndex allocateQ(State&, const Vector& qInit) const;
00095 UIndex allocateU(State&, const Vector& uInit) const;
00096 ZIndex allocateZ(State&, const Vector& zInit) const;
00097 DiscreteVariableIndex allocateDiscreteVariable(State&, Stage, AbstractValue* v) const;
00098
00099 CacheEntryIndex allocateCacheEntry (const State&, Stage dependsOn, Stage computedBy, AbstractValue* v) const;
00100 CacheEntryIndex allocateCacheEntry (const State& state, Stage g, AbstractValue* v) const
00101 { return allocateCacheEntry(state, g, g, v); }
00102 QErrIndex allocateQErr (const State&, int nqerr) const;
00103 UErrIndex allocateUErr (const State&, int nuerr) const;
00104 UDotErrIndex allocateUDotErr (const State&, int nudoterr) const;
00105 EventTriggerByStageIndex allocateEventTriggersByStage(const State&, Stage, int ntriggers) const;
00106
00107
00108
00109
00110
00111
00112
00113
00114 const Vector& getQ(const State&) const;
00115 const Vector& getU(const State&) const;
00116 const Vector& getZ(const State&) const;
00117 const Vector& getQDot(const State&) const;
00118 const Vector& getUDot(const State&) const;
00119 const Vector& getZDot(const State&) const;
00120 const Vector& getQDotDot(const State&) const;
00121 const Vector& getQErr(const State&) const;
00122 const Vector& getUErr(const State&) const;
00123 const Vector& getUDotErr(const State&) const;
00124 const Vector& getMultipliers(const State&) const;
00125 const Vector& getEventTriggersByStage(const State&, Stage) const;
00126
00127
00128
00129
00130
00131 Vector& updQ(State&) const;
00132 Vector& updU(State&) const;
00133 Vector& updZ(State&) const;
00134
00135
00136 void setQ(State& s, const Vector& q) const {
00137 assert(q.size() == getNQ(s));
00138 updQ(s) = q;
00139 }
00140 void setU(State& s, const Vector& u) const {
00141 assert(u.size() == getNU(s));
00142 updU(s) = u;
00143 }
00144 void setZ(State& s, const Vector& z) const {
00145 assert(z.size() == getNZ(s));
00146 updZ(s) = z;
00147 }
00148
00149
00150
00151
00152
00153 Vector& updQDot(const State&) const;
00154 Vector& updUDot(const State&) const;
00155 Vector& updZDot(const State&) const;
00156 Vector& updQDotDot(const State&) const;
00157 Vector& updQErr(const State&) const;
00158 Vector& updUErr(const State&) const;
00159 Vector& updUDotErr(const State&) const;
00160 Vector& updMultipliers(const State&) const;
00161 Vector& updEventTriggersByStage(const State&, Stage) const;
00162
00163
00164
00165
00166 Stage getStage(const State&) const;
00167 const AbstractValue& getDiscreteVariable(const State&, DiscreteVariableIndex) const;
00168
00169 AbstractValue& updDiscreteVariable(State&, DiscreteVariableIndex) const;
00170 const AbstractValue& getCacheEntry(const State&, CacheEntryIndex) const;
00171
00172 AbstractValue& updCacheEntry(const State&, CacheEntryIndex) const;
00173
00174 bool isCacheValueCurrent(const State&, CacheEntryIndex) const;
00175 void markCacheValueRealized(const State&, CacheEntryIndex) const;
00176
00177
00178
00179
00180
00181 SystemQIndex getQStart (const State&) const;
00182 int getNQ (const State&) const;
00183 SystemUIndex getUStart (const State&) const;
00184 int getNU (const State&) const;
00185 SystemZIndex getZStart (const State&) const;
00186 int getNZ (const State&) const;
00187 SystemQErrIndex getQErrStart (const State&) const;
00188 int getNQErr (const State&) const;
00189 SystemUErrIndex getUErrStart (const State&) const;
00190 int getNUErr (const State&) const;
00191 SystemUDotErrIndex getUDotErrStart(const State&) const;
00192 int getNUDotErr (const State&) const;
00193 SystemMultiplierIndex getMultipliersStart (const State&) const;
00194 int getNMultipliers (const State&) const;
00195 SystemEventTriggerByStageIndex getEventTriggerStartByStage(const State&, Stage) const;
00196 int getNEventTriggersByStage (const State&, Stage) const;
00197
00198 bool isInSystem() const;
00199 bool isInSameSystem(const Subsystem& otherSubsystem) const;
00200
00201 const System& getSystem() const;
00202 System& updSystem();
00203
00204 SubsystemIndex getMySubsystemIndex() const;
00205
00206
00207
00208 bool isOwnerHandle() const;
00209 bool isEmptyHandle() const;
00210
00211
00212 bool isSameSubsystem(const Subsystem& otherSubsystem) const;
00213
00214 bool subsystemTopologyHasBeenRealized() const;
00215 void invalidateSubsystemTopologyCache() const;
00216
00217
00218
00219 MeasureIndex adoptMeasure(Measure&);
00220
00221 Measure getMeasure(MeasureIndex) const;
00222 template <class T> Measure_<T> getMeasure_(MeasureIndex mx) const
00223 { return Measure_<T>::getAs(getMeasure(mx));}
00224
00225
00226
00227 const Subsystem::Guts& getSubsystemGuts() const {assert(guts); return *guts;}
00228 Subsystem::Guts& updSubsystemGuts() {assert(guts); return *guts;}
00229
00230
00231
00232
00233 void adoptSubsystemGuts(Subsystem::Guts* g);
00234 void setSystem(System&, SubsystemIndex);
00235
00236 explicit Subsystem(Subsystem::Guts* g) : guts(g) { }
00237 bool hasGuts() const {return guts!=0;}
00238 };
00239
00240
00247 class SimTK_SimTKCOMMON_EXPORT DefaultSystemSubsystem : public Subsystem {
00248 public:
00249 DefaultSystemSubsystem(System& sys);
00250 void addEventHandler(ScheduledEventHandler* handler);
00251 void addEventHandler(TriggeredEventHandler* handler);
00252 void addEventReporter(ScheduledEventReporter* handler) const;
00253 void addEventReporter(TriggeredEventReporter* handler) const;
00254 EventId createEventId(SubsystemIndex subsys, const State& state) const;
00255 void findSubsystemEventIds(SubsystemIndex subsys, const State& state, const std::vector<EventId>& allEvents, std::vector<EventId>& eventsForSubsystem) const;
00256 private:
00257 const DefaultSystemSubsystemGuts& getGuts() const;
00258 DefaultSystemSubsystemGuts& updGuts();
00259 };
00260
00261
00262 }
00263
00264 #endif // SimTK_SimTKCOMMON_SUBSYSTEM_H_