Simbody
|
00001 #ifndef SimTK_SimTKCOMMON_SYSTEM_GUTS_H_ 00002 #define SimTK_SimTKCOMMON_SYSTEM_GUTS_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 namespace SimTK { 00041 00042 class Subsystem; 00043 class DecorativeGeometry; 00044 00079 class SimTK_SimTKCOMMON_EXPORT System::Guts { 00080 class GutsRep; 00081 friend class GutsRep; 00082 00083 // This is the only data member in this class. 00084 GutsRep* rep; // opaque implementation of System::Guts base class. 00085 public: 00086 // Note that this serves as a default constructor since both arguments have defaults. 00087 explicit Guts(const String& name="<NONAME>", 00088 const String& version="0.0.0"); 00089 virtual ~Guts(); 00090 00091 const String& getName() const; 00092 const String& getVersion() const; 00093 00094 void setHasTimeAdvancedEvents(bool hasEm); 00095 bool hasTimeAdvancedEvents() const; 00096 00098 // EVALUATION (REALIZATION) // 00100 00101 // These are the routines to which the System class forwards requests. 00102 00103 const State& getDefaultState() const; 00104 State& updDefaultState(); 00105 00106 void realize(const State& s, Stage g = Stage::HighestRuntime) const; 00107 00108 SubsystemIndex adoptSubsystem(Subsystem& child); 00109 00110 int getNumSubsystems() const; 00111 const Subsystem& getSubsystem(SubsystemIndex) const; 00112 Subsystem& updSubsystem(SubsystemIndex); 00113 00114 // Obtain the owner handle for this System::Guts object. 00115 const System& getSystem() const; 00116 System& updSystem(); 00117 00118 void setOwnerHandle(System&); 00119 bool hasOwnerHandle() const; 00120 00121 explicit Guts(class GutsRep* r) : rep(r) { } 00122 bool hasRep() const {return rep!=0;} 00123 const GutsRep& getRep() const {assert(rep); return *rep;} 00124 GutsRep& updRep() const {assert(rep); return *rep;} 00125 00126 bool systemTopologyHasBeenRealized() const; 00127 void invalidateSystemTopologyCache() const; 00128 00129 // Wrap the cloneImpl virtual method. 00130 System::Guts* clone() const; 00131 00132 // These routines wrap the virtual realize...Impl() methods to ensure 00133 // good behavior such as checking that stage requirements are met and 00134 // updating the stage at the end. Note that these will do nothing if 00135 // the System stage is already at or greater than the indicated stage. 00136 00137 const State& realizeTopology() const; 00138 void realizeModel(State&) const; 00139 void realizeInstance (const State& s) const; 00140 void realizeTime (const State& s) const; 00141 void realizePosition (const State& s) const; 00142 void realizeVelocity (const State& s) const; 00143 void realizeDynamics (const State& s) const; 00144 void realizeAcceleration(const State& s) const; 00145 void realizeReport (const State& s) const; 00146 00147 // These wrap the other virtual methods. 00148 Real calcTimescale(const State&) const; 00149 void calcYUnitWeights(const State&, Vector& weights) const; 00150 void prescribe(State&, Stage) const; 00151 void project(State&, Real consAccuracy, const Vector& yweights, 00152 const Vector& ootols, Vector& yerrest, System::ProjectOptions) const; 00153 void calcYErrUnitTolerances(const State&, Vector& tolerances) const; 00154 void handleEvents 00155 (State&, Event::Cause, const Array_<EventId>& eventIds, 00156 Real accuracy, const Vector& yWeights, const Vector& ooConstraintTols, 00157 Stage& lowestModified, bool& shouldTerminate) const; 00158 void reportEvents(const State&, Event::Cause, const Array_<EventId>& eventIds) const; 00159 void calcEventTriggerInfo(const State&, Array_<EventTriggerInfo>&) const; 00160 void calcTimeOfNextScheduledEvent(const State&, Real& tNextEvent, Array_<EventId>& eventIds, bool includeCurrentTime) const; 00161 void calcTimeOfNextScheduledReport(const State&, Real& tNextEvent, Array_<EventId>& eventIds, bool includeCurrentTime) const; 00162 00163 void calcDecorativeGeometryAndAppend(const State&, Stage, 00164 Array_<DecorativeGeometry>&) const; 00165 00166 00167 protected: 00168 Guts(const Guts&); // copies the base class; for use from derived class copy constructors 00169 00170 // The destructor is already virtual; see above. 00171 00172 virtual System::Guts* cloneImpl() const = 0; 00173 00174 // Override these to change the evaluation order of the Subsystems. 00175 // The default is to evaluate them in increasing order of SubsystemIndex. 00176 // These methods should not be called directly; they are invoked by the 00177 // above wrapper methods. Note: the wrappers *will not* call these 00178 // routines if the system stage has already met the indicated stage level. 00179 // If fact these routines will be called only when the system stage 00180 // is at the level just prior to the one indicated here. For example, 00181 // realizeVelocityImpl() will be called only if the passed-in State 00182 // has been determined to have its system stage exactly Stage::Position. 00183 virtual int realizeTopologyImpl(State&) const; 00184 virtual int realizeModelImpl(State&) const; 00185 virtual int realizeInstanceImpl(const State&) const; 00186 virtual int realizeTimeImpl(const State&) const; 00187 virtual int realizePositionImpl(const State&) const; 00188 virtual int realizeVelocityImpl(const State&) const; 00189 virtual int realizeDynamicsImpl(const State&) const; 00190 virtual int realizeAccelerationImpl(const State&) const; 00191 virtual int realizeReportImpl(const State&) const; 00192 00193 virtual Real calcTimescaleImpl(const State&) const; 00194 00195 virtual int calcYUnitWeightsImpl(const State&, Vector& weights) const; 00196 00197 virtual int prescribeImpl(State&, Stage) const; 00198 virtual int projectImpl(State&, Real consAccuracy, const Vector& yweights, 00199 const Vector& ootols, Vector& yerrest, System::ProjectOptions) const; 00200 virtual int calcYErrUnitTolerancesImpl(const State&, Vector& tolerances) const; 00201 00202 virtual int handleEventsImpl 00203 (State&, Event::Cause, const Array_<EventId>& eventIds, 00204 Real accuracy, const Vector& yWeights, const Vector& ooConstraintTols, 00205 Stage& lowestModified, bool& shouldTerminate) const; 00206 00207 virtual int reportEventsImpl(const State&, Event::Cause, const Array_<EventId>& eventIds) const; 00208 00209 virtual int calcEventTriggerInfoImpl(const State&, Array_<EventTriggerInfo>&) const; 00210 00211 virtual int calcTimeOfNextScheduledEventImpl(const State&, Real& tNextEvent, Array_<EventId>& eventIds, bool includeCurrentTime) const; 00212 virtual int calcTimeOfNextScheduledReportImpl(const State&, Real& tNextEvent, Array_<EventId>& eventIds, bool includeCurrentTime) const; 00213 00214 private: 00215 // OBSOLETE 00216 // Part of our ongoing crusade to turn getN's into getNums for API consistency. 00217 int getNSubsystems() const {return getNumSubsystems();} 00218 00219 private: 00220 Guts& operator=(const Guts&); // suppress default copy assignment operator 00221 00222 class EventTriggerInfoRep; 00223 00224 }; 00225 00226 00227 } // namespace SimTK 00228 00229 #endif // SimTK_SimTKCOMMON_SYSTEM_GUTS_H_