00001 #ifndef SimTK_SimTKCOMMON_SUBSYSTEM_GUTS_H_
00002 #define SimTK_SimTKCOMMON_SUBSYSTEM_GUTS_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/System.h"
00039
00040 #include <cassert>
00041
00042 namespace SimTK {
00043
00044 class System;
00045 class DecorativeGeometry;
00046
00050 class SimTK_SimTKCOMMON_EXPORT Subsystem::Guts {
00051 public:
00052 Guts(const Guts&);
00053 Guts& operator=(const Guts&);
00054
00055
00056
00057 explicit Guts(const String& name="<NONAME>",
00058 const String& version="0.0.0");
00059 virtual ~Guts();
00060
00061 const String& getName() const;
00062 const String& getVersion() const;
00063
00064
00065
00066
00067
00068 QIndex allocateQ(State& s, const Vector& qInit) const;
00069
00070 UIndex allocateU(State& s, const Vector& uInit) const;
00071
00072 ZIndex allocateZ(State& s, const Vector& zInit) const;
00073 DiscreteVariableIndex allocateDiscreteVariable(State& s, Stage g, AbstractValue* v) const;
00074
00075
00076 CacheEntryIndex allocateCacheEntry (const State&, Stage dependsOn, Stage computedBy, AbstractValue* v) const;
00077 CacheEntryIndex allocateCacheEntry (const State& state, Stage g, AbstractValue* v) const
00078 { return allocateCacheEntry(state, g, g, v); }
00079
00080
00081 QErrIndex allocateQErr(const State& s, int nqerr) const;
00082 UErrIndex allocateUErr(const State& s, int nuerr) const;
00083 UDotErrIndex allocateUDotErr(const State& s, int nudoterr) const;
00084 EventTriggerByStageIndex allocateEventTriggersByStage(const State&, Stage, int ntriggers) const;
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 const Vector& getQ(const State&) const;
00095 const Vector& getU(const State&) const;
00096 const Vector& getZ(const State&) const;
00097 const Vector& getQDot(const State&) const;
00098 const Vector& getUDot(const State&) const;
00099 const Vector& getZDot(const State&) const;
00100 const Vector& getQDotDot(const State&) const;
00101 const Vector& getQErr(const State&) const;
00102 const Vector& getUErr(const State&) const;
00103 const Vector& getUDotErr(const State&) const;
00104 const Vector& getMultipliers(const State&) const;
00105 const Vector& getEventTriggersByStage(const State&, Stage) const;
00106
00107
00108
00109
00110
00111 Vector& updQ(State&) const;
00112 Vector& updU(State&) const;
00113 Vector& updZ(State&) const;
00114
00115
00116 void setQ(State& s, const Vector& q) const {
00117 assert(q.size() == getNQ(s));
00118 updQ(s) = q;
00119 }
00120 void setU(State& s, const Vector& u) const {
00121 assert(u.size() == getNU(s));
00122 updU(s) = u;
00123 }
00124 void setZ(State& s, const Vector& z) const {
00125 assert(z.size() == getNZ(s));
00126 updZ(s) = z;
00127 }
00128
00129
00130
00131
00132
00133 Vector& updQDot(const State&) const;
00134 Vector& updUDot(const State&) const;
00135 Vector& updZDot(const State&) const;
00136 Vector& updQDotDot(const State&) const;
00137 Vector& updQErr(const State&) const;
00138 Vector& updUErr(const State&) const;
00139 Vector& updUDotErr(const State&) const;
00140 Vector& updMultipliers(const State&) const;
00141 Vector& updEventTriggersByStage(const State&, Stage) const;
00142
00143
00144
00145
00146 Stage getStage(const State&) const;
00147 const AbstractValue& getDiscreteVariable(const State&, DiscreteVariableIndex) const;
00148
00149 AbstractValue& updDiscreteVariable(State&, DiscreteVariableIndex) const;
00150 const AbstractValue& getCacheEntry(const State&, CacheEntryIndex) const;
00151
00152 AbstractValue& updCacheEntry(const State&, CacheEntryIndex) const;
00153
00154 bool isCacheValueCurrent(const State&, CacheEntryIndex) const;
00155 void markCacheValueRealized(const State&, CacheEntryIndex) const;
00156
00157
00158
00159
00160
00161 SystemQIndex getQStart (const State&) const;
00162 int getNQ (const State&) const;
00163 SystemUIndex getUStart (const State&) const;
00164 int getNU (const State&) const;
00165 SystemZIndex getZStart (const State&) const;
00166 int getNZ (const State&) const;
00167 SystemQErrIndex getQErrStart (const State&) const;
00168 int getNQErr (const State&) const;
00169 SystemUErrIndex getUErrStart (const State&) const;
00170 int getNUErr (const State&) const;
00171 SystemUDotErrIndex getUDotErrStart(const State&) const;
00172 int getNUDotErr (const State&) const;
00173 SystemMultiplierIndex getMultipliersStart(const State&) const;
00174 int getNMultipliers(const State&) const;
00175 SystemEventTriggerByStageIndex getEventTriggerStartByStage(const State&, Stage) const;
00176 int getNEventTriggersByStage(const State&, Stage) const;
00177
00178 MeasureIndex adoptMeasure(Measure& m);
00179 Measure getMeasure(MeasureIndex) const;
00180 template <class T> Measure_<T> getMeasure_(MeasureIndex mx) const
00181 { return Measure_<T>::getAs(getMeasure(mx));}
00182
00183 bool isInSystem() const;
00184 bool isInSameSystem(const Subsystem& otherSubsystem) const;
00185
00186 const System& getSystem() const;
00187 System& updSystem();
00188
00189 SubsystemIndex getMySubsystemIndex() const;
00190
00191
00192 const Subsystem& getOwnerSubsystemHandle() const;
00193 Subsystem& updOwnerSubsystemHandle();
00194 void setOwnerSubsystemHandle(Subsystem&);
00195 bool hasOwnerSubsystemHandle() const;
00196
00197 void setSystem(System&, SubsystemIndex);
00198
00199 class GutsRep;
00200 explicit Guts(GutsRep* r) : rep(r) { }
00201 bool hasRep() const {return rep!=0;}
00202 const GutsRep& getRep() const {assert(rep); return *rep;}
00203 GutsRep& updRep() const {assert(rep); return *rep;}
00204 void setRep(GutsRep& r) {assert(!rep); rep = &r;}
00205
00206 bool subsystemTopologyHasBeenRealized() const;
00207 void invalidateSubsystemTopologyCache() const;
00208
00209
00210
00211
00212 Subsystem::Guts* clone() const;
00213
00214
00215
00216
00217
00218
00219
00220 void realizeSubsystemTopology (State&) const;
00221 void realizeSubsystemModel (State&) const;
00222 void realizeSubsystemInstance (const State&) const;
00223 void realizeSubsystemTime (const State&) const;
00224 void realizeSubsystemPosition (const State&) const;
00225 void realizeSubsystemVelocity (const State&) const;
00226 void realizeSubsystemDynamics (const State&) const;
00227 void realizeSubsystemAcceleration(const State&) const;
00228 void realizeSubsystemReport (const State&) const;
00229
00230
00231
00232
00233
00234
00235
00236
00237 void calcQUnitWeights(const State&, Vector& weights) const;
00238
00239
00240
00241
00242
00243
00244 void calcUUnitWeights(const State&, Vector& weights) const;
00245
00246
00247
00248
00249
00250
00251 void calcZUnitWeights(const State&, Vector& weights) const;
00252
00253
00254
00255
00256
00257
00258
00259
00260 void calcQErrUnitTolerances(const State&, Vector& tolerances) const;
00261
00262
00263
00264
00265
00266
00267
00268
00269 void calcUErrUnitTolerances(const State&, Vector& tolerances) const;
00270
00271
00272
00273
00274
00275
00276 void calcDecorativeGeometryAndAppend(const State&, Stage, std::vector<DecorativeGeometry>&) const;
00277
00278 void createScheduledEvent(const State& state, EventId& eventId) const;
00279 void createTriggeredEvent(const State& state, EventId& eventId, EventTriggerByStageIndex& triggerFunctionIndex, Stage stage) const;
00280
00281
00282
00283
00284
00285 virtual void calcEventTriggerInfo(const State&, std::vector<System::EventTriggerInfo>&) const;
00286 virtual void calcTimeOfNextScheduledEvent(const State&, Real& tNextEvent, std::vector<EventId>& eventIds, bool includeCurrentTime) const;
00287 virtual void calcTimeOfNextScheduledReport(const State&, Real& tNextEvent, std::vector<EventId>& eventIds, bool includeCurrentTime) const;
00288 virtual void handleEvents(State&, Event::Cause, const std::vector<EventId>& eventIds,
00289 Real accuracy, const Vector& yWeights, const Vector& ooConstraintTols,
00290 Stage& lowestModified, bool& shouldTerminate) const;
00291 virtual void reportEvents(const State&, Event::Cause, const std::vector<EventId>& eventIds) const;
00292
00293 protected:
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311 virtual Subsystem::Guts* cloneImpl() const = 0;
00312
00313 virtual int realizeSubsystemTopologyImpl(State& s) const;
00314 virtual int realizeSubsystemModelImpl(State& s) const;
00315 virtual int realizeSubsystemInstanceImpl(const State& s) const;
00316 virtual int realizeSubsystemTimeImpl(const State& s) const;
00317 virtual int realizeSubsystemPositionImpl(const State& s) const;
00318 virtual int realizeSubsystemVelocityImpl(const State& s) const;
00319 virtual int realizeSubsystemDynamicsImpl(const State& s) const;
00320 virtual int realizeSubsystemAccelerationImpl(const State& s) const;
00321 virtual int realizeSubsystemReportImpl(const State& s) const;
00322
00323 virtual int calcQUnitWeightsImpl(const State& s, Vector& weights) const;
00324 virtual int calcUUnitWeightsImpl(const State& s, Vector& weights) const;
00325 virtual int calcZUnitWeightsImpl(const State& s, Vector& weights) const;
00326 virtual int calcQErrUnitTolerancesImpl(const State& s, Vector& tolerances) const;
00327 virtual int calcUErrUnitTolerancesImpl(const State& s, Vector& tolerances) const;
00328 virtual int calcDecorativeGeometryAndAppendImpl
00329 (const State&, Stage, std::vector<DecorativeGeometry>&) const;
00330
00331 void advanceToStage(const State& s, Stage g) const;
00332
00333 private:
00334
00335 GutsRep* rep;
00336
00337 friend class GutsRep;
00338 };
00339
00340 }
00341
00342 #endif // SimTK_SimTKCOMMON_SUBSYSTEM_GUTS_H_