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
00047
00048 static void subsystemDestructImplLocator(Subsystem::Guts*);
00049 static Subsystem::Guts* subsystemCloneImplLocator(const Subsystem::Guts&);
00050 static int subsystemRealizeTopologyImplLocator(const Subsystem::Guts&, State&);
00051 static int subsystemRealizeModelImplLocator(const Subsystem::Guts&, State&);
00052 static int subsystemRealizeInstanceImplLocator(const Subsystem::Guts&, const State&);
00053 static int subsystemRealizeTimeImplLocator(const Subsystem::Guts&, const State&);
00054 static int subsystemRealizePositionImplLocator(const Subsystem::Guts&, const State&);
00055 static int subsystemRealizeVelocityImplLocator(const Subsystem::Guts&, const State&);
00056 static int subsystemRealizeDynamicsImplLocator(const Subsystem::Guts&, const State&);
00057 static int subsystemRealizeAccelerationImplLocator(const Subsystem::Guts&, const State&);
00058 static int subsystemRealizeReportImplLocator(const Subsystem::Guts&, const State&);
00059 static int subsystemCalcQUnitWeightsImplLocator(const Subsystem::Guts&, const State&, Vector&);
00060 static int subsystemCalcUUnitWeightsImplLocator(const Subsystem::Guts&, const State&, Vector&);
00061 static int subsystemCalcZUnitWeightsImplLocator(const Subsystem::Guts&, const State&, Vector&);
00062 static int subsystemCalcQErrUnitTolerancesImplLocator(const Subsystem::Guts&, const State&, Vector&);
00063 static int subsystemCalcUErrUnitTolerancesImplLocator(const Subsystem::Guts&, const State&, Vector&);
00064 static int subsystemCalcDecorativeGeometryAndAppendImplLocator
00065 (const Subsystem::Guts&, const State&, Stage, std::vector<DecorativeGeometry>&);
00066
00088 class SimTK_SimTKCOMMON_EXPORT Subsystem::Guts {
00089 class GutsRep;
00090 friend class GutsRep;
00091
00092
00093 GutsRep* rep;
00094 public:
00095 Guts(const Guts&);
00096 Guts& operator=(const Guts&);
00097
00098
00099
00100
00101 inline explicit Guts(const String& name="<NONAME>",
00102 const String& version="0.0.0");
00103
00104
00105
00106
00107 virtual ~Guts() {librarySideDestruction();}
00108
00109 const String& getName() const;
00110 const String& getVersion() const;
00111
00112
00113
00114
00115
00116
00117
00118
00119 const Vector& getQ(const State&) const;
00120 const Vector& getU(const State&) const;
00121 const Vector& getZ(const State&) const;
00122 const Vector& getQDot(const State&) const;
00123 const Vector& getUDot(const State&) const;
00124 const Vector& getZDot(const State&) const;
00125 const Vector& getQDotDot(const State&) const;
00126 const Vector& getQErr(const State&) const;
00127 const Vector& getUErr(const State&) const;
00128 const Vector& getUDotErr(const State&) const;
00129 const Vector& getMultipliers(const State&) const;
00130
00131
00132
00133
00134
00135 Vector& updQ(State&) const;
00136 Vector& updU(State&) const;
00137 Vector& updZ(State&) const;
00138
00139
00140 void setQ(State& s, const Vector& q) const {
00141 assert(q.size() == getNQ(s));
00142 updQ(s) = q;
00143 }
00144 void setU(State& s, const Vector& u) const {
00145 assert(u.size() == getNU(s));
00146 updU(s) = u;
00147 }
00148 void setZ(State& s, const Vector& z) const {
00149 assert(z.size() == getNZ(s));
00150 updZ(s) = z;
00151 }
00152
00153
00154
00155
00156
00157 Vector& updQDot(const State&) const;
00158 Vector& updUDot(const State&) const;
00159 Vector& updZDot(const State&) const;
00160 Vector& updQDotDot(const State&) const;
00161 Vector& updQErr(const State&) const;
00162 Vector& updUErr(const State&) const;
00163 Vector& updUDotErr(const State&) const;
00164 Vector& updMultipliers(const State&) const;
00165
00166
00167
00168
00169 Stage getStage(const State&) const;
00170 const AbstractValue& getDiscreteVariable(const State&, int index) const;
00171
00172 AbstractValue& updDiscreteVariable(State&, int index) const;
00173 const AbstractValue& getCacheEntry(const State&, int index) const;
00174
00175 AbstractValue& updCacheEntry(const State&, int index) const;
00176
00177
00178
00179
00180
00181 int getQStart (const State&) const;
00182 int getNQ (const State&) const;
00183 int getUStart (const State&) const;
00184 int getNU (const State&) const;
00185 int getZStart (const State&) const;
00186 int getNZ (const State&) const;
00187 int getQErrStart (const State&) const;
00188 int getNQErr (const State&) const;
00189 int getUErrStart (const State&) const;
00190 int getNUErr (const State&) const;
00191 int getUDotErrStart(const State&) const;
00192 int getNUDotErr (const State&) const;
00193 int getMultipliersStart(const State&) const;
00194 int getNMultipliers (const State&) const;
00195
00196 bool isInSystem() const;
00197 bool isInSameSystem(const Subsystem& otherSubsystem) const;
00198
00199 const System& getSystem() const;
00200 System& updSystem();
00201
00202 SubsystemIndex getMySubsystemIndex() const;
00203
00204
00205 const Subsystem& getOwnerSubsystemHandle() const;
00206 Subsystem& updOwnerSubsystemHandle();
00207 void setOwnerSubsystemHandle(Subsystem&);
00208 bool hasOwnerSubsystemHandle() const;
00209
00210 void setSystem(System&, SubsystemIndex);
00211
00212 explicit Guts(class GutsRep* r) : rep(r) { }
00213 bool hasRep() const {return rep!=0;}
00214 const GutsRep& getRep() const {assert(rep); return *rep;}
00215 GutsRep& updRep() const {assert(rep); return *rep;}
00216 void setRep(GutsRep& r) {assert(!rep); rep = &r;}
00217
00218 bool subsystemTopologyHasBeenRealized() const;
00219 void invalidateSubsystemTopologyCache() const;
00220
00221
00222
00223 static void destruct(Subsystem::Guts*);
00224
00225
00226
00227
00228
00229
00230 Subsystem::Guts* clone() const;
00231
00232
00233
00234
00235
00236
00237
00238 void realizeSubsystemTopology (State&) const;
00239 void realizeSubsystemModel (State&) const;
00240 void realizeSubsystemInstance (const State&) const;
00241 void realizeSubsystemTime (const State&) const;
00242 void realizeSubsystemPosition (const State&) const;
00243 void realizeSubsystemVelocity (const State&) const;
00244 void realizeSubsystemDynamics (const State&) const;
00245 void realizeSubsystemAcceleration(const State&) const;
00246 void realizeSubsystemReport (const State&) const;
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257 void calcQUnitWeights(const State&, Vector& weights) const;
00258
00259
00260
00261
00262
00263
00264 void calcUUnitWeights(const State&, Vector& weights) const;
00265
00266
00267
00268
00269
00270
00271 void calcZUnitWeights(const State&, Vector& weights) const;
00272
00273
00274
00275
00276
00277
00278
00279
00280 void calcQErrUnitTolerances(const State&, Vector& tolerances) const;
00281
00282
00283
00284
00285
00286
00287
00288
00289 void calcUErrUnitTolerances(const State&, Vector& tolerances) const;
00290
00291
00292
00293
00294
00295
00296 void calcDecorativeGeometryAndAppend(const State&, Stage, std::vector<DecorativeGeometry>&) const;
00297
00298 void createScheduledEvent(State& state, EventId& eventId) const;
00299 void createTriggeredEvent(State& state, EventId& eventId, int& triggerFunctionIndex, Stage stage) const;
00300
00301
00302
00303
00304
00305 virtual void calcEventTriggerInfo(const State&, std::vector<System::EventTriggerInfo>&) const;
00306 virtual void calcTimeOfNextScheduledEvent(const State&, Real& tNextEvent, std::vector<EventId>& eventIds, bool includeCurrentTime) const;
00307 virtual void calcTimeOfNextScheduledReport(const State&, Real& tNextEvent, std::vector<EventId>& eventIds, bool includeCurrentTime) const;
00308 virtual void handleEvents(State&, System::EventCause, const std::vector<EventId>& eventIds,
00309 Real accuracy, const Vector& yWeights, const Vector& ooConstraintTols,
00310 Stage& lowestModified, bool& shouldTerminate) const;
00311 virtual void reportEvents(const State&, System::EventCause, const std::vector<EventId>& eventIds) const;
00312 protected:
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330 virtual Subsystem::Guts* cloneImpl() const = 0;
00331
00332 virtual int realizeSubsystemTopologyImpl(State& s) const;
00333 virtual int realizeSubsystemModelImpl(State& s) const;
00334 virtual int realizeSubsystemInstanceImpl(const State& s) const;
00335 virtual int realizeSubsystemTimeImpl(const State& s) const;
00336 virtual int realizeSubsystemPositionImpl(const State& s) const;
00337 virtual int realizeSubsystemVelocityImpl(const State& s) const;
00338 virtual int realizeSubsystemDynamicsImpl(const State& s) const;
00339 virtual int realizeSubsystemAccelerationImpl(const State& s) const;
00340 virtual int realizeSubsystemReportImpl(const State& s) const;
00341
00342 virtual int calcQUnitWeightsImpl(const State& s, Vector& weights) const;
00343 virtual int calcUUnitWeightsImpl(const State& s, Vector& weights) const;
00344 virtual int calcZUnitWeightsImpl(const State& s, Vector& weights) const;
00345 virtual int calcQErrUnitTolerancesImpl(const State& s, Vector& tolerances) const;
00346 virtual int calcUErrUnitTolerancesImpl(const State& s, Vector& tolerances) const;
00347 virtual int calcDecorativeGeometryAndAppendImpl
00348 (const State&, Stage, std::vector<DecorativeGeometry>&) const;
00349
00350
00351
00352
00353
00354 int allocateQ(State& s, const Vector& qInit) const;
00355
00356 int allocateU(State& s, const Vector& uInit) const;
00357
00358 int allocateZ(State& s, const Vector& zInit) const;
00359
00360
00361 int allocateQErr(State& s, int nqerr) const;
00362 int allocateUErr(State& s, int nuerr) const;
00363 int allocateUDotErr(State& s, int nudoterr) const;
00364 int allocateDiscreteVariable(State& s, Stage g, AbstractValue* v) const;
00365 int allocateCacheEntry(State& s, Stage g, AbstractValue* v) const;
00366 void advanceToStage(const State& s, Stage g) const;
00367
00368
00369 private:
00370
00371
00372
00373
00374 typedef void (*DestructImplLocator)(Subsystem::Guts*);
00375 typedef Subsystem::Guts* (*CloneImplLocator)(const Subsystem::Guts&);
00376 typedef int (*RealizeWritableStateImplLocator)(const Subsystem::Guts&, State&);
00377 typedef int (*RealizeConstStateImplLocator)(const Subsystem::Guts&, const State&);
00378 typedef int (*CalcUnitWeightsImplLocator)(const Subsystem::Guts&, const State&, Vector& weights);
00379 typedef int (*CalcDecorativeGeometryAndAppendImplLocator)
00380 (const Subsystem::Guts&, const State&, Stage, std::vector<DecorativeGeometry>&);
00381
00382 void librarySideConstruction(const String& name, const String& version);
00383 void librarySideDestruction();
00384
00385 void registerDestructImpl(DestructImplLocator);
00386 void registerCloneImpl(CloneImplLocator);
00387
00388 void registerRealizeTopologyImpl (RealizeWritableStateImplLocator);
00389 void registerRealizeModelImpl (RealizeWritableStateImplLocator);
00390 void registerRealizeInstanceImpl (RealizeConstStateImplLocator);
00391 void registerRealizeTimeImpl (RealizeConstStateImplLocator);
00392 void registerRealizePositionImpl (RealizeConstStateImplLocator);
00393 void registerRealizeVelocityImpl (RealizeConstStateImplLocator);
00394 void registerRealizeDynamicsImpl (RealizeConstStateImplLocator);
00395 void registerRealizeAccelerationImpl(RealizeConstStateImplLocator);
00396 void registerRealizeReportImpl (RealizeConstStateImplLocator);
00397
00398 void registerCalcQUnitWeightsImpl(CalcUnitWeightsImplLocator);
00399 void registerCalcUUnitWeightsImpl(CalcUnitWeightsImplLocator);
00400 void registerCalcZUnitWeightsImpl(CalcUnitWeightsImplLocator);
00401 void registerCalcQErrUnitTolerancesImpl(CalcUnitWeightsImplLocator);
00402 void registerCalcUErrUnitTolerancesImpl(CalcUnitWeightsImplLocator);
00403 void registerCalcDecorativeGeometryAndAppendImpl(CalcDecorativeGeometryAndAppendImplLocator);
00404
00405
00406
00407
00408 friend void subsystemDestructImplLocator(Subsystem::Guts*);
00409 friend Subsystem::Guts* subsystemCloneImplLocator(const Subsystem::Guts&);
00410
00411 friend int subsystemRealizeTopologyImplLocator(const Subsystem::Guts&, State&);
00412 friend int subsystemRealizeModelImplLocator(const Subsystem::Guts&, State&);
00413 friend int subsystemRealizeInstanceImplLocator(const Subsystem::Guts&, const State&);
00414 friend int subsystemRealizeTimeImplLocator(const Subsystem::Guts&, const State&);
00415 friend int subsystemRealizePositionImplLocator(const Subsystem::Guts&, const State&);
00416 friend int subsystemRealizeVelocityImplLocator(const Subsystem::Guts&, const State&);
00417 friend int subsystemRealizeDynamicsImplLocator(const Subsystem::Guts&, const State&);
00418 friend int subsystemRealizeAccelerationImplLocator(const Subsystem::Guts&, const State&);
00419 friend int subsystemRealizeReportImplLocator(const Subsystem::Guts&, const State&);
00420
00421 friend int subsystemCalcQUnitWeightsImplLocator(const Subsystem::Guts&, const State&, Vector&);
00422 friend int subsystemCalcUUnitWeightsImplLocator(const Subsystem::Guts&, const State&, Vector&);
00423 friend int subsystemCalcZUnitWeightsImplLocator(const Subsystem::Guts&, const State&, Vector&);
00424 friend int subsystemCalcQErrUnitTolerancesImplLocator(const Subsystem::Guts&, const State&, Vector&);
00425 friend int subsystemCalcUErrUnitTolerancesImplLocator(const Subsystem::Guts&, const State&, Vector&);
00426 friend int subsystemCalcDecorativeGeometryAndAppendImplLocator
00427 (const Subsystem::Guts&, const State&, Stage, std::vector<DecorativeGeometry>&);
00428 };
00429
00430
00431
00432
00433
00434 static void subsystemDestructImplLocator(Subsystem::Guts* sysp)
00435 { delete sysp; }
00436 static Subsystem::Guts* subsystemCloneImplLocator(const Subsystem::Guts& sys)
00437 { return sys.cloneImpl(); }
00438
00439 static int subsystemRealizeTopologyImplLocator(const Subsystem::Guts& sys, State& state)
00440 { return sys.realizeSubsystemTopologyImpl(state); }
00441 static int subsystemRealizeModelImplLocator(const Subsystem::Guts& sys, State& state)
00442 { return sys.realizeSubsystemModelImpl(state); }
00443 static int subsystemRealizeInstanceImplLocator(const Subsystem::Guts& sys, const State& state)
00444 { return sys.realizeSubsystemInstanceImpl(state); }
00445 static int subsystemRealizeTimeImplLocator(const Subsystem::Guts& sys, const State& state)
00446 { return sys.realizeSubsystemTimeImpl(state); }
00447 static int subsystemRealizePositionImplLocator(const Subsystem::Guts& sys, const State& state)
00448 { return sys.realizeSubsystemPositionImpl(state); }
00449 static int subsystemRealizeVelocityImplLocator(const Subsystem::Guts& sys, const State& state)
00450 { return sys.realizeSubsystemVelocityImpl(state); }
00451 static int subsystemRealizeDynamicsImplLocator(const Subsystem::Guts& sys, const State& state)
00452 { return sys.realizeSubsystemDynamicsImpl(state); }
00453 static int subsystemRealizeAccelerationImplLocator(const Subsystem::Guts& sys, const State& state)
00454 { return sys.realizeSubsystemAccelerationImpl(state); }
00455 static int subsystemRealizeReportImplLocator(const Subsystem::Guts& sys, const State& state)
00456 { return sys.realizeSubsystemReportImpl(state); }
00457
00458 static int subsystemCalcQUnitWeightsImplLocator(const Subsystem::Guts& sys, const State& s, Vector& weights)
00459 { return sys.calcQUnitWeightsImpl(s,weights); }
00460 static int subsystemCalcUUnitWeightsImplLocator(const Subsystem::Guts& sys, const State& s, Vector& weights)
00461 { return sys.calcUUnitWeightsImpl(s,weights); }
00462 static int subsystemCalcZUnitWeightsImplLocator(const Subsystem::Guts& sys, const State& s, Vector& weights)
00463 { return sys.calcZUnitWeightsImpl(s,weights); }
00464 static int subsystemCalcQErrUnitTolerancesImplLocator(const Subsystem::Guts& sys, const State& s, Vector& tolerances)
00465 { return sys.calcQErrUnitTolerancesImpl(s,tolerances); }
00466 static int subsystemCalcUErrUnitTolerancesImplLocator(const Subsystem::Guts& sys, const State& s, Vector& tolerances)
00467 { return sys.calcUErrUnitTolerancesImpl(s,tolerances); }
00468 static int subsystemCalcDecorativeGeometryAndAppendImplLocator
00469 (const Subsystem::Guts& sys, const State& s, Stage g, std::vector<DecorativeGeometry>& geom)
00470 { return sys.calcDecorativeGeometryAndAppendImpl(s,g,geom); }
00471
00472
00473
00474
00475
00476 inline Subsystem::Guts::Guts(const String& name, const String& version) : rep(0)
00477 {
00478 librarySideConstruction(name, version);
00479
00480
00481
00482
00483 registerDestructImpl(subsystemDestructImplLocator);
00484 registerCloneImpl(subsystemCloneImplLocator);
00485
00486 registerRealizeTopologyImpl (subsystemRealizeTopologyImplLocator);
00487 registerRealizeModelImpl (subsystemRealizeModelImplLocator);
00488 registerRealizeInstanceImpl (subsystemRealizeInstanceImplLocator);
00489 registerRealizeTimeImpl (subsystemRealizeTimeImplLocator);
00490 registerRealizePositionImpl (subsystemRealizePositionImplLocator);
00491 registerRealizeVelocityImpl (subsystemRealizeVelocityImplLocator);
00492 registerRealizeDynamicsImpl (subsystemRealizeDynamicsImplLocator);
00493 registerRealizeAccelerationImpl(subsystemRealizeAccelerationImplLocator);
00494 registerRealizeReportImpl (subsystemRealizeReportImplLocator);
00495
00496 registerCalcQUnitWeightsImpl(subsystemCalcQUnitWeightsImplLocator);
00497 registerCalcUUnitWeightsImpl(subsystemCalcUUnitWeightsImplLocator);
00498 registerCalcZUnitWeightsImpl(subsystemCalcZUnitWeightsImplLocator);
00499 registerCalcQErrUnitTolerancesImpl(subsystemCalcQErrUnitTolerancesImplLocator);
00500 registerCalcUErrUnitTolerancesImpl(subsystemCalcUErrUnitTolerancesImplLocator);
00501 registerCalcDecorativeGeometryAndAppendImpl(subsystemCalcDecorativeGeometryAndAppendImplLocator);
00502 }
00503
00504 }
00505
00506 #endif // SimTK_SimTKCOMMON_SUBSYSTEM_GUTS_H_