Simbody  3.4 (development)
SystemGuts.h
Go to the documentation of this file.
00001 #ifndef SimTK_SimTKCOMMON_SYSTEM_GUTS_H_
00002 #define SimTK_SimTKCOMMON_SYSTEM_GUTS_H_
00003 
00004 /* -------------------------------------------------------------------------- *
00005  *                       Simbody(tm): SimTKcommon                             *
00006  * -------------------------------------------------------------------------- *
00007  * This is part of the SimTK 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/home/simbody.  *
00011  *                                                                            *
00012  * Portions copyright (c) 2006-12 Stanford University and the Authors.        *
00013  * Authors: Michael Sherman                                                   *
00014  * Contributors:                                                              *
00015  *                                                                            *
00016  * Licensed under the Apache License, Version 2.0 (the "License"); you may    *
00017  * not use this file except in compliance with the License. You may obtain a  *
00018  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0.         *
00019  *                                                                            *
00020  * Unless required by applicable law or agreed to in writing, software        *
00021  * distributed under the License is distributed on an "AS IS" BASIS,          *
00022  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
00023  * See the License for the specific language governing permissions and        *
00024  * limitations under the License.                                             *
00025  * -------------------------------------------------------------------------- */
00026 
00027 #include "SimTKcommon/basics.h"
00028 #include "SimTKcommon/Simmatrix.h"
00029 #include "SimTKcommon/internal/State.h"
00030 #include "SimTKcommon/internal/System.h"
00031 
00032 namespace SimTK {
00033 
00034 class Subsystem;
00035 class DecorativeGeometry;
00036 
00071 class SimTK_SimTKCOMMON_EXPORT System::Guts {
00072     class GutsRep;
00073     friend class GutsRep;
00074 
00075     // This is the only data member in this class.
00076     GutsRep* rep; // opaque implementation of System::Guts base class.
00077 public:
00078     // Note that this serves as a default constructor since both arguments have defaults.
00079     explicit Guts(const String& name="<NONAME>", 
00080                   const String& version="0.0.0");
00081     virtual ~Guts();
00082 
00083     const String& getName()    const;
00084     const String& getVersion() const;
00085 
00086     void setHasTimeAdvancedEvents(bool hasEm);
00087     bool hasTimeAdvancedEvents() const;
00088 
00090         // EVALUATION (REALIZATION) //
00092 
00093     // These are the routines to which the System class forwards requests.
00094 
00095     const State& getDefaultState() const;
00096     State&       updDefaultState();
00097 
00098     void realize(const State& s, Stage g = Stage::HighestRuntime) const;
00099 
00100     SubsystemIndex adoptSubsystem(Subsystem& child);
00101 
00102     int getNumSubsystems() const;
00103     const Subsystem& getSubsystem(SubsystemIndex)   const;
00104     Subsystem&       updSubsystem(SubsystemIndex);
00105 
00106     // Obtain the owner handle for this System::Guts object.
00107     const System& getSystem() const;
00108     System& updSystem();
00109 
00110     void setOwnerHandle(System&);
00111     bool hasOwnerHandle() const;
00112 
00113     explicit Guts(class GutsRep* r) : rep(r) { }
00114     bool           hasRep() const {return rep!=0;}
00115     const GutsRep& getRep() const {assert(rep); return *rep;}
00116     GutsRep&       updRep() const {assert(rep); return *rep;}
00117 
00118     bool systemTopologyHasBeenRealized() const;
00119     StageVersion getSystemTopologyCacheVersion() const;
00120     void setSystemTopologyCacheVersion(StageVersion topoVersion) const;
00121     void invalidateSystemTopologyCache() const;
00122 
00123     // Wrap the cloneImpl virtual method.
00124     System::Guts* clone() const;
00125 
00126     // These routines wrap the virtual realize...Impl() methods to ensure
00127     // good behavior such as checking that stage requirements are met and
00128     // updating the stage at the end. Note that these will do nothing if
00129     // the System stage is already at or greater than the indicated stage.
00130 
00131     const State& realizeTopology() const;
00132     void realizeModel(State&) const;
00133     void realizeInstance    (const State& s) const;
00134     void realizeTime        (const State& s) const;
00135     void realizePosition    (const State& s) const;
00136     void realizeVelocity    (const State& s) const;
00137     void realizeDynamics    (const State& s) const;
00138     void realizeAcceleration(const State& s) const;
00139     void realizeReport      (const State& s) const;
00140 
00141     // These wrap the other virtual methods.
00142     void multiplyByN(const State& state, const Vector& u, 
00143                      Vector& dq) const;
00144     void multiplyByNTranspose(const State& state, const Vector& fq, 
00145                               Vector& fu) const;
00146     void multiplyByNPInv(const State& state, const Vector& dq, 
00147                          Vector& u) const;
00148     void multiplyByNPInvTranspose(const State& state, const Vector& fu, 
00149                                   Vector& fq) const;
00150 
00151     bool prescribeQ(State&) const;
00152     bool prescribeU(State&) const;
00153 
00154     void projectQ(State&, Vector& qErrEst, 
00155                   const ProjectOptions& options, ProjectResults& results) const;
00156     void projectU(State&, Vector& uErrEst, 
00157                   const ProjectOptions& options, ProjectResults& results) const;
00158 
00159     void handleEvents
00160         (State&, Event::Cause, const Array_<EventId>& eventIds,
00161          const HandleEventsOptions& options,
00162          HandleEventsResults& results) const;
00163     void reportEvents(const State&, Event::Cause, const Array_<EventId>& eventIds) const;
00164     void calcEventTriggerInfo(const State&, Array_<EventTriggerInfo>&) const;
00165     void calcTimeOfNextScheduledEvent(const State&, Real& tNextEvent, Array_<EventId>& eventIds, bool includeCurrentTime) const;
00166     void calcTimeOfNextScheduledReport(const State&, Real& tNextEvent, Array_<EventId>& eventIds, bool includeCurrentTime) const;
00167 
00168     void calcDecorativeGeometryAndAppend(const State&, Stage, 
00169                                          Array_<DecorativeGeometry>&) const;
00170 
00171 
00172 protected:
00173     Guts(const Guts&);  // copies the base class; for use from derived class copy constructors
00174     
00175     // The destructor is already virtual; see above.
00176 
00177     virtual System::Guts* cloneImpl() const = 0;
00178 
00179     // Override these to change the evaluation order of the Subsystems.
00180     // The default is to evaluate them in increasing order of SubsystemIndex.
00181     // These methods should not be called directly; they are invoked by the
00182     // above wrapper methods. Note: the wrappers *will not* call these
00183     // routines if the system stage has already met the indicated stage level.
00184     // If fact these routines will be called only when the system stage
00185     // is at the level just prior to the one indicated here. For example,
00186     // realizeVelocityImpl() will be called only if the passed-in State
00187     // has been determined to have its system stage exactly Stage::Position.
00188     virtual int realizeTopologyImpl(State& state)       const {return 0;}
00189     virtual int realizeModelImpl   (State& state)       const {return 0;}
00190     virtual int realizeInstanceImpl(const State& state) const {return 0;}
00191     virtual int realizeTimeImpl    (const State& state) const {return 0;}
00192     virtual int realizePositionImpl(const State& state) const {return 0;}
00193     virtual int realizeVelocityImpl(const State& state) const {return 0;}
00194     virtual int realizeDynamicsImpl(const State& state) const {return 0;}
00195     virtual int realizeAccelerationImpl(const State& state) const {return 0;}
00196     virtual int realizeReportImpl  (const State& state) const {return 0;}
00197 
00198     virtual void multiplyByNImpl(const State& state, const Vector& u, 
00199                                  Vector& dq) const;
00200     virtual void multiplyByNTransposeImpl(const State& state, const Vector& fq, 
00201                                           Vector& fu) const;
00202     virtual void multiplyByNPInvImpl(const State& state, const Vector& dq, 
00203                                      Vector& u) const;
00204     virtual void multiplyByNPInvTransposeImpl(const State& state, const Vector& fu, 
00205                                               Vector& fq) const;
00206 
00207     // Defaults assume no prescribed motion; hence, no change made.
00208     virtual bool prescribeQImpl(State&) const {return false;}
00209     virtual bool prescribeUImpl(State&) const {return false;}
00210 
00211     // Defaults assume no constraints and return success meaning "all 
00212     // constraints satisfied".
00213     virtual void projectQImpl(State& state, Vector& qErrEst, 
00214              const ProjectOptions& options, ProjectResults& results) const
00215     {   results.clear(); results.setExitStatus(ProjectResults::Succeeded); }
00216     virtual void projectUImpl(State& state, Vector& uErrEst, 
00217              const ProjectOptions& options, ProjectResults& results) const
00218     {   results.clear(); results.setExitStatus(ProjectResults::Succeeded); }
00219 
00220     virtual void handleEventsImpl
00221        (State& state, Event::Cause cause, const Array_<EventId>& eventIds,
00222         const HandleEventsOptions& options, HandleEventsResults& results) const;
00223 
00224     virtual int reportEventsImpl(const State& state, Event::Cause cause, 
00225                                  const Array_<EventId>& eventIds) const;
00226 
00227     virtual int calcEventTriggerInfoImpl(const State& state, 
00228                                          Array_<EventTriggerInfo>& info) const;
00229 
00230     virtual int calcTimeOfNextScheduledEventImpl
00231        (const State& state, Real& tNextEvent, Array_<EventId>& eventIds, 
00232         bool includeCurrentTime) const;
00233     virtual int calcTimeOfNextScheduledReportImpl
00234        (const State& state, Real& tNextEvent, Array_<EventId>& eventIds, 
00235         bool includeCurrentTime) const;
00236 
00237 private:
00238     Guts& operator=(const Guts&); // suppress default copy assignment operator
00239 
00240     class EventTriggerInfoRep;
00241 
00242 };
00243 
00244 
00245 } // namespace SimTK
00246 
00247 #endif // SimTK_SimTKCOMMON_SYSTEM_GUTS_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines