Subsystem.h

Go to the documentation of this file.
00001 #ifndef SimTK_SimTKCOMMON_SUBSYSTEM_H_
00002 #define SimTK_SimTKCOMMON_SUBSYSTEM_H_
00003 
00004 /* -------------------------------------------------------------------------- *
00005  *                      SimTK Core: SimTKcommon                               *
00006  * -------------------------------------------------------------------------- *
00007  * This is part of the SimTK Core 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) 2006-7 Stanford University and the Authors.         *
00013  * Authors: Michael Sherman                                                   *
00014  * Contributors:                                                              *
00015  *                                                                            *
00016  * Permission is hereby granted, free of charge, to any person obtaining a    *
00017  * copy of this software and associated documentation files (the "Software"), *
00018  * to deal in the Software without restriction, including without limitation  *
00019  * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
00020  * and/or sell copies of the Software, and to permit persons to whom the      *
00021  * Software is furnished to do so, subject to the following conditions:       *
00022  *                                                                            *
00023  * The above copyright notice and this permission notice shall be included in *
00024  * all copies or substantial portions of the Software.                        *
00025  *                                                                            *
00026  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
00027  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
00028  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
00029  * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
00030  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
00031  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
00032  * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
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 class DefaultSystemSubsystemGuts;
00047 class ScheduledEventHandler;
00048 class ScheduledEventReporter;
00049 class TriggeredEventHandler;
00050 class TriggeredEventReporter;
00051 
00074 class SimTK_SimTKCOMMON_EXPORT Subsystem {
00075 public:
00076     class Guts; // local; name is Subsystem::Guts
00077     friend class Guts;
00078 private:
00079     // This is the only data member in this class. Also, any class derived from
00080     // Subsystem must have *NO* data members at all (data goes in the Guts class).
00081     Guts* guts;
00082 public:
00083     Subsystem() : guts(0) { } // an empty handle
00084     Subsystem(const Subsystem&);
00085     Subsystem& operator=(const Subsystem&);
00086     ~Subsystem();
00087 
00088     const String& getName()    const;
00089     const String& getVersion() const;
00090 
00091     // These return views on State shared global resources. The views
00092     // are private to this subsystem, but the global resources themselves
00093     // are not allocated until the *System* advances to stage Model.
00094     // Note that there is no subsystem equivalent of the State's "y"
00095     // vector because in general a subsystem's state variables will
00096     // not be contiguous. However, a subsystem's q's, u's, and z's
00097     // will all be contiguous within those arrays.
00098     const Vector& getQ(const State&) const;
00099     const Vector& getU(const State&) const;
00100     const Vector& getZ(const State&) const;
00101     const Vector& getQDot(const State&) const;
00102     const Vector& getUDot(const State&) const;
00103     const Vector& getZDot(const State&) const;
00104     const Vector& getQDotDot(const State&) const;
00105     const Vector& getQErr(const State&) const;
00106     const Vector& getUErr(const State&) const;
00107     const Vector& getUDotErr(const State&) const;
00108     const Vector& getMultipliers(const State&) const;
00109 
00110     // These return writable access to this subsystem's partition in the
00111     // State pool of continuous variables. These can be called at Stage::Model
00112     // or higher, and if necesary they invalidate the Position (q), Velocity (u),
00113     // or Dynamics (z) stage respectively.
00114     Vector& updQ(State&) const; // invalidates Stage::Position
00115     Vector& updU(State&) const; // invalidates Stage::Velocity
00116     Vector& updZ(State&) const; // invalidates Stage::Dynamics
00117 
00118     // For convenience.
00119     void setQ(State& s, const Vector& q) const {
00120         assert(q.size() == getNQ(s));
00121         updQ(s) = q;
00122     }
00123     void setU(State& s, const Vector& u) const {
00124         assert(u.size() == getNU(s));
00125         updU(s) = u;
00126     }
00127     void setZ(State& s, const Vector& z) const {
00128         assert(z.size() == getNZ(s));
00129         updZ(s) = z;
00130     }
00131 
00132     // These update the State cache which is mutable; hence, const State. They
00133     // can be called only if the previous stage has already been realized, e.g.,
00134     // updQDot() is allowed only while realizing the Velocity stage, requiring
00135     // that Position stage has already been realized.
00136     Vector& updQDot(const State&) const;
00137     Vector& updUDot(const State&) const;
00138     Vector& updZDot(const State&) const;
00139     Vector& updQDotDot(const State&) const;
00140     Vector& updQErr(const State&) const;
00141     Vector& updUErr(const State&) const;
00142     Vector& updUDotErr(const State&) const;
00143     Vector& updMultipliers(const State&) const;
00144 
00145     // These pull out the State entries which belong exclusively to
00146     // this Subsystem. These variables and cache entries are available
00147     // as soon as this subsystem is at stage Model.
00148     Stage getStage(const State&) const;
00149     const AbstractValue& getDiscreteVariable(const State&, int index) const;
00150     // State is *not* mutable here -- must have write access to change state variables.
00151     AbstractValue& updDiscreteVariable(State&, int index) const;
00152     const AbstractValue& getCacheEntry(const State&, int index) const;
00153     // State is mutable here.
00154     AbstractValue& updCacheEntry(const State&, int index) const;
00155 
00156     // Dimensions. These are valid at System Stage::Model while access to the various
00157     // arrays may have stricter requirements. Hence it is better to use these
00158     // routines than to get a reference to a Vector above and ask for its size().
00159 
00160     int getQStart      (const State&) const;
00161     int getNQ          (const State&) const;
00162     int getUStart      (const State&) const;
00163     int getNU          (const State&) const;
00164     int getZStart      (const State&) const;
00165     int getNZ          (const State&) const;
00166     int getQErrStart   (const State&) const;
00167     int getNQErr       (const State&) const;
00168     int getUErrStart   (const State&) const;
00169     int getNUErr       (const State&) const;
00170     int getUDotErrStart(const State&) const;
00171     int getNUDotErr    (const State&) const;
00172     int getMultipliersStart(const State&) const;
00173     int getNMultipliers    (const State&) const;
00174 
00175     bool isInSystem() const;
00176     bool isInSameSystem(const Subsystem& otherSubsystem) const;
00177 
00178     const System& getSystem() const;
00179     System&       updSystem();
00180 
00181     SubsystemIndex getMySubsystemIndex() const;
00182 
00183     // Is this handle the owner of this rep? This is true if the
00184     // handle is empty or if its rep points back here.
00185     bool isOwnerHandle() const;
00186     bool isEmptyHandle() const;
00187 
00188     // There can be multiple handles on the same Subsystem.
00189     bool isSameSubsystem(const Subsystem& otherSubsystem) const;
00190 
00191     bool subsystemTopologyHasBeenRealized() const;
00192     void invalidateSubsystemTopologyCache() const;
00193 
00194     // dynamic_cast the returned reference to a reference to your concrete Guts
00195     // class.
00196     const Subsystem::Guts& getSubsystemGuts() const {assert(guts); return *guts;}
00197     Subsystem::Guts&       updSubsystemGuts()       {assert(guts); return *guts;}
00198 
00199     // Put new Guts into this *empty* handle and take over ownership.
00200     // If this handle is already in use, this routine will throw
00201     // an exception.
00202     void adoptSubsystemGuts(Subsystem::Guts* g);
00203     void setSystem(System&, SubsystemIndex);
00204 
00205     explicit Subsystem(Subsystem::Guts* g) : guts(g) { }
00206     bool hasGuts() const {return guts!=0;}
00207 };
00208 
00209 
00216 class SimTK_SimTKCOMMON_EXPORT DefaultSystemSubsystem : public Subsystem {
00217 public:
00218     DefaultSystemSubsystem(System& sys);
00219     void addEventHandler(ScheduledEventHandler* handler);
00220     void addEventHandler(TriggeredEventHandler* handler);
00221     void addEventReporter(ScheduledEventReporter* handler) const;
00222     void addEventReporter(TriggeredEventReporter* handler) const;
00223     EventId createEventId(SubsystemIndex subsys, State& state) const;
00224     void findSubsystemEventIds(SubsystemIndex subsys, const State& state, const std::vector<EventId>& allEvents, std::vector<EventId>& eventsForSubsystem) const;
00225 private:
00226     const DefaultSystemSubsystemGuts& getGuts() const;
00227     DefaultSystemSubsystemGuts& updGuts();
00228 };
00229 
00230 
00231 } // namespace SimTK
00232 
00233 #endif // SimTK_SimTKCOMMON_SUBSYSTEM_H_

Generated on Thu Feb 28 01:34:32 2008 for SimTKcommon by  doxygen 1.4.7