Simbody
3.5
|
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 void getFreeQIndex(const State&, Array_<SystemQIndex>& freeQs) const; 00154 void getFreeUIndex(const State&, Array_<SystemUIndex>& freeUs) const; 00155 00156 void projectQ(State&, Vector& qErrEst, 00157 const ProjectOptions& options, ProjectResults& results) const; 00158 void projectU(State&, Vector& uErrEst, 00159 const ProjectOptions& options, ProjectResults& results) const; 00160 00161 void handleEvents 00162 (State&, Event::Cause, const Array_<EventId>& eventIds, 00163 const HandleEventsOptions& options, 00164 HandleEventsResults& results) const; 00165 void reportEvents(const State&, Event::Cause, const Array_<EventId>& eventIds) const; 00166 void calcEventTriggerInfo(const State&, Array_<EventTriggerInfo>&) const; 00167 void calcTimeOfNextScheduledEvent(const State&, Real& tNextEvent, Array_<EventId>& eventIds, bool includeCurrentTime) const; 00168 void calcTimeOfNextScheduledReport(const State&, Real& tNextEvent, Array_<EventId>& eventIds, bool includeCurrentTime) const; 00169 00170 void calcDecorativeGeometryAndAppend(const State&, Stage, 00171 Array_<DecorativeGeometry>&) const; 00172 00173 00174 protected: 00175 Guts(const Guts&); // copies the base class; for use from derived class copy constructors 00176 00177 // The destructor is already virtual; see above. 00178 00179 virtual System::Guts* cloneImpl() const = 0; 00180 00181 // Override these to change the evaluation order of the Subsystems. 00182 // The default is to evaluate them in increasing order of SubsystemIndex. 00183 // These methods should not be called directly; they are invoked by the 00184 // above wrapper methods. Note: the wrappers *will not* call these 00185 // routines if the system stage has already met the indicated stage level. 00186 // If fact these routines will be called only when the system stage 00187 // is at the level just prior to the one indicated here. For example, 00188 // realizeVelocityImpl() will be called only if the passed-in State 00189 // has been determined to have its system stage exactly Stage::Position. 00190 virtual int realizeTopologyImpl(State& state) const {return 0;} 00191 virtual int realizeModelImpl (State& state) const {return 0;} 00192 virtual int realizeInstanceImpl(const State& state) const {return 0;} 00193 virtual int realizeTimeImpl (const State& state) const {return 0;} 00194 virtual int realizePositionImpl(const State& state) const {return 0;} 00195 virtual int realizeVelocityImpl(const State& state) const {return 0;} 00196 virtual int realizeDynamicsImpl(const State& state) const {return 0;} 00197 virtual int realizeAccelerationImpl(const State& state) const {return 0;} 00198 virtual int realizeReportImpl (const State& state) const {return 0;} 00199 00200 virtual void multiplyByNImpl(const State& state, const Vector& u, 00201 Vector& dq) const; 00202 virtual void multiplyByNTransposeImpl(const State& state, const Vector& fq, 00203 Vector& fu) const; 00204 virtual void multiplyByNPInvImpl(const State& state, const Vector& dq, 00205 Vector& u) const; 00206 virtual void multiplyByNPInvTransposeImpl(const State& state, const Vector& fu, 00207 Vector& fq) const; 00208 00209 // Defaults assume no prescribed motion; hence, no change made. 00210 virtual bool prescribeQImpl(State&) const {return false;} 00211 virtual bool prescribeUImpl(State&) const {return false;} 00212 00213 00214 // Defaults assume no constraints and return success meaning "all 00215 // constraints satisfied". 00216 virtual void projectQImpl(State& state, Vector& qErrEst, 00217 const ProjectOptions& options, ProjectResults& results) const 00218 { results.clear(); results.setExitStatus(ProjectResults::Succeeded); } 00219 virtual void projectUImpl(State& state, Vector& uErrEst, 00220 const ProjectOptions& options, ProjectResults& results) const 00221 { results.clear(); results.setExitStatus(ProjectResults::Succeeded); } 00222 00223 virtual void handleEventsImpl 00224 (State& state, Event::Cause cause, const Array_<EventId>& eventIds, 00225 const HandleEventsOptions& options, HandleEventsResults& results) const; 00226 00227 virtual int reportEventsImpl(const State& state, Event::Cause cause, 00228 const Array_<EventId>& eventIds) const; 00229 00230 virtual int calcEventTriggerInfoImpl(const State& state, 00231 Array_<EventTriggerInfo>& info) const; 00232 00233 virtual int calcTimeOfNextScheduledEventImpl 00234 (const State& state, Real& tNextEvent, Array_<EventId>& eventIds, 00235 bool includeCurrentTime) const; 00236 virtual int calcTimeOfNextScheduledReportImpl 00237 (const State& state, Real& tNextEvent, Array_<EventId>& eventIds, 00238 bool includeCurrentTime) const; 00239 00240 00241 // Default is that all the state variables are free. 00242 virtual void getFreeQIndexImpl 00243 (const State& s, Array_<SystemQIndex>& freeQs) const { 00244 const unsigned nq = (unsigned)s.getNQ(); 00245 freeQs.resize(nq); 00246 for (unsigned i=0; i<nq; ++i) 00247 freeQs[i] = SystemQIndex(i); 00248 } 00249 virtual void getFreeUIndexImpl 00250 (const State& s, Array_<SystemUIndex>& freeUs) const { 00251 const unsigned nu = (unsigned)s.getNU(); 00252 freeUs.resize(nu); 00253 for (unsigned i=0; i<nu; ++i) 00254 freeUs[i] = SystemUIndex(i); 00255 } 00256 00257 private: 00258 Guts& operator=(const Guts&); // suppress default copy assignment operator 00259 00260 class EventTriggerInfoRep; 00261 00262 }; 00263 00264 00265 } // namespace SimTK 00266 00267 #endif // SimTK_SimTKCOMMON_SYSTEM_GUTS_H_