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
00098 DiscreteVariableIndex allocateDiscreteVariable
00099 (State&, Stage invalidates, AbstractValue* v) const;
00100 DiscreteVariableIndex allocateAutoUpdateDiscreteVariable
00101 (State&, Stage invalidates, AbstractValue* v, Stage updateDependsOn) const;
00102
00103 CacheEntryIndex allocateCacheEntry
00104 (const State&, Stage dependsOn, Stage computedBy, AbstractValue* v) const;
00105 CacheEntryIndex allocateCacheEntry
00106 (const State& state, Stage g, AbstractValue* v) const
00107 { return allocateCacheEntry(state, g, g, v); }
00108 CacheEntryIndex allocateLazyCacheEntry
00109 (const State& state, Stage earliest, AbstractValue* v) const
00110 { return allocateCacheEntry(state, earliest, Stage::Infinity, v); }
00111
00112 QErrIndex allocateQErr (const State&, int nqerr) const;
00113 UErrIndex allocateUErr (const State&, int nuerr) const;
00114 UDotErrIndex allocateUDotErr (const State&, int nudoterr) const;
00115 EventTriggerByStageIndex allocateEventTriggersByStage
00116 (const State&, Stage, int ntriggers) const;
00117
00118
00119
00120
00121
00122
00123
00124
00125 const Vector& getQ(const State&) const;
00126 const Vector& getU(const State&) const;
00127 const Vector& getZ(const State&) const;
00128 const Vector& getQDot(const State&) const;
00129 const Vector& getUDot(const State&) const;
00130 const Vector& getZDot(const State&) const;
00131 const Vector& getQDotDot(const State&) const;
00132 const Vector& getQErr(const State&) const;
00133 const Vector& getUErr(const State&) const;
00134 const Vector& getUDotErr(const State&) const;
00135 const Vector& getMultipliers(const State&) const;
00136 const Vector& getEventTriggersByStage(const State&, Stage) const;
00137
00138
00139
00140
00141
00142 Vector& updQ(State&) const;
00143 Vector& updU(State&) const;
00144 Vector& updZ(State&) const;
00145
00146
00147 void setQ(State& s, const Vector& q) const {
00148 assert(q.size() == getNQ(s));
00149 updQ(s) = q;
00150 }
00151 void setU(State& s, const Vector& u) const {
00152 assert(u.size() == getNU(s));
00153 updU(s) = u;
00154 }
00155 void setZ(State& s, const Vector& z) const {
00156 assert(z.size() == getNZ(s));
00157 updZ(s) = z;
00158 }
00159
00160
00161
00162
00163
00164 Vector& updQDot(const State&) const;
00165 Vector& updUDot(const State&) const;
00166 Vector& updZDot(const State&) const;
00167 Vector& updQDotDot(const State&) const;
00168 Vector& updQErr(const State&) const;
00169 Vector& updUErr(const State&) const;
00170 Vector& updUDotErr(const State&) const;
00171 Vector& updMultipliers(const State&) const;
00172 Vector& updEventTriggersByStage(const State&, Stage) const;
00173
00174
00175
00176
00177 Stage getStage(const State&) const;
00178 const AbstractValue& getDiscreteVariable(const State& s, DiscreteVariableIndex dx) const;
00179
00180 Real getDiscreteVarLastUpdateTime(const State& s, DiscreteVariableIndex dx) const
00181 { return s.getDiscreteVarLastUpdateTime(getMySubsystemIndex(),dx); }
00182 CacheEntryIndex getDiscreteVarUpdateIndex(const State& s, DiscreteVariableIndex dx) const
00183 { return s.getDiscreteVarUpdateIndex(getMySubsystemIndex(),dx); }
00184 const AbstractValue& getDiscreteVarUpdateValue(const State& s, DiscreteVariableIndex dx) const
00185 { return s.getDiscreteVarUpdateValue(getMySubsystemIndex(),dx); }
00186 AbstractValue& updDiscreteVarUpdateValue(const State& s, DiscreteVariableIndex dx) const
00187 { return s.updDiscreteVarUpdateValue(getMySubsystemIndex(),dx); }
00188 bool isDiscreteVarUpdateValueRealized(const State& s, DiscreteVariableIndex dx) const
00189 { return s.isDiscreteVarUpdateValueRealized(getMySubsystemIndex(),dx); }
00190 void markDiscreteVarUpdateValueRealized(const State& s, DiscreteVariableIndex dx) const
00191 { return s.markDiscreteVarUpdateValueRealized(getMySubsystemIndex(),dx); }
00192
00193
00194 AbstractValue& updDiscreteVariable(State&, DiscreteVariableIndex) const;
00195
00196 const AbstractValue& getCacheEntry(const State&, CacheEntryIndex) const;
00197
00198 AbstractValue& updCacheEntry(const State&, CacheEntryIndex) const;
00199
00200 bool isCacheValueRealized(const State&, CacheEntryIndex) const;
00201 void markCacheValueRealized(const State&, CacheEntryIndex) const;
00202
00203
00204
00205
00206
00207
00208 SystemQIndex getQStart (const State&) const;
00209 int getNQ (const State&) const;
00210 SystemUIndex getUStart (const State&) const;
00211 int getNU (const State&) const;
00212 SystemZIndex getZStart (const State&) const;
00213 int getNZ (const State&) const;
00214 SystemQErrIndex getQErrStart (const State&) const;
00215 int getNQErr (const State&) const;
00216 SystemUErrIndex getUErrStart (const State&) const;
00217 int getNUErr (const State&) const;
00218 SystemUDotErrIndex getUDotErrStart(const State&) const;
00219 int getNUDotErr (const State&) const;
00220 SystemMultiplierIndex getMultipliersStart (const State&) const;
00221 int getNMultipliers (const State&) const;
00222 SystemEventTriggerByStageIndex getEventTriggerStartByStage(const State&, Stage) const;
00223 int getNEventTriggersByStage (const State&, Stage) const;
00224
00225 bool isInSystem() const;
00226 bool isInSameSystem(const Subsystem& otherSubsystem) const;
00227
00228 const System& getSystem() const;
00229 System& updSystem();
00230
00231 SubsystemIndex getMySubsystemIndex() const;
00232
00233
00234
00235 bool isOwnerHandle() const;
00236 bool isEmptyHandle() const;
00237
00238
00239 bool isSameSubsystem(const Subsystem& otherSubsystem) const;
00240
00241 bool subsystemTopologyHasBeenRealized() const;
00242 void invalidateSubsystemTopologyCache() const;
00243
00244
00245
00246 MeasureIndex adoptMeasure(AbstractMeasure&);
00247
00248 AbstractMeasure getMeasure(MeasureIndex) const;
00249 template <class T> Measure_<T> getMeasure_(MeasureIndex mx) const
00250 { return Measure_<T>::getAs(getMeasure(mx));}
00251
00252
00253
00254 const Subsystem::Guts& getSubsystemGuts() const {assert(guts); return *guts;}
00255 Subsystem::Guts& updSubsystemGuts() {assert(guts); return *guts;}
00256
00257
00258
00259
00260 void adoptSubsystemGuts(Subsystem::Guts* g);
00261 void setSystem(System&, SubsystemIndex);
00262
00263 explicit Subsystem(Subsystem::Guts* g) : guts(g) { }
00264 bool hasGuts() const {return guts!=0;}
00265 };
00266
00267
00268
00274 class SimTK_SimTKCOMMON_EXPORT DefaultSystemSubsystem : public Subsystem {
00275 public:
00276 explicit DefaultSystemSubsystem(System& sys);
00277 void addEventHandler(ScheduledEventHandler* handler);
00278 void addEventHandler(TriggeredEventHandler* handler);
00279 void addEventReporter(ScheduledEventReporter* handler) const;
00280 void addEventReporter(TriggeredEventReporter* handler) const;
00281 EventId createEventId(SubsystemIndex subsys, const State& state) const;
00282 void findSubsystemEventIds
00283 (SubsystemIndex subsys, const State& state,
00284 const Array_<EventId>& allEvents,
00285 Array_<EventId>& eventsForSubsystem) const;
00286 private:
00287 const DefaultSystemSubsystemGuts& getGuts() const;
00288 DefaultSystemSubsystemGuts& updGuts();
00289 };
00290
00291
00292 }
00293
00294 #endif // SimTK_SimTKCOMMON_SUBSYSTEM_H_