Simbody  3.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SystemGuts.h
Go to the documentation of this file.
1 #ifndef SimTK_SimTKCOMMON_SYSTEM_GUTS_H_
2 #define SimTK_SimTKCOMMON_SYSTEM_GUTS_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * Simbody(tm): SimTKcommon *
6  * -------------------------------------------------------------------------- *
7  * This is part of the SimTK biosimulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
11  * *
12  * Portions copyright (c) 2006-12 Stanford University and the Authors. *
13  * Authors: Michael Sherman *
14  * Contributors: *
15  * *
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may *
17  * not use this file except in compliance with the License. You may obtain a *
18  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
19  * *
20  * Unless required by applicable law or agreed to in writing, software *
21  * distributed under the License is distributed on an "AS IS" BASIS, *
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
23  * See the License for the specific language governing permissions and *
24  * limitations under the License. *
25  * -------------------------------------------------------------------------- */
26 
27 #include "SimTKcommon/basics.h"
28 #include "SimTKcommon/Simmatrix.h"
31 
32 namespace SimTK {
33 
34 class Subsystem;
35 class DecorativeGeometry;
36 
72  class GutsRep;
73  friend class GutsRep;
74 
75  // This is the only data member in this class.
76  GutsRep* rep; // opaque implementation of System::Guts base class.
77 public:
78  // Note that this serves as a default constructor since both arguments have defaults.
79  explicit Guts(const String& name="<NONAME>",
80  const String& version="0.0.0");
81  virtual ~Guts();
82 
83  const String& getName() const;
84  const String& getVersion() const;
85 
86  void setHasTimeAdvancedEvents(bool hasEm);
87  bool hasTimeAdvancedEvents() const;
88 
90  // EVALUATION (REALIZATION) //
92 
93  // These are the routines to which the System class forwards requests.
94 
95  const State& getDefaultState() const;
96  State& updDefaultState();
97 
98  void realize(const State& s, Stage g = Stage::HighestRuntime) const;
99 
100  SubsystemIndex adoptSubsystem(Subsystem& child);
101 
102  int getNumSubsystems() const;
103  const Subsystem& getSubsystem(SubsystemIndex) const;
104  Subsystem& updSubsystem(SubsystemIndex);
105 
106  // Obtain the owner handle for this System::Guts object.
107  const System& getSystem() const;
108  System& updSystem();
109 
110  void setOwnerHandle(System&);
111  bool hasOwnerHandle() const;
112 
113  explicit Guts(class GutsRep* r) : rep(r) { }
114  bool hasRep() const {return rep!=0;}
115  const GutsRep& getRep() const {assert(rep); return *rep;}
116  GutsRep& updRep() const {assert(rep); return *rep;}
117 
118  bool systemTopologyHasBeenRealized() const;
119  StageVersion getSystemTopologyCacheVersion() const;
120  void setSystemTopologyCacheVersion(StageVersion topoVersion) const;
121  void invalidateSystemTopologyCache() const;
122 
123  // Wrap the cloneImpl virtual method.
124  System::Guts* clone() const;
125 
126  // These routines wrap the virtual realize...Impl() methods to ensure
127  // good behavior such as checking that stage requirements are met and
128  // updating the stage at the end. Note that these will do nothing if
129  // the System stage is already at or greater than the indicated stage.
130 
131  const State& realizeTopology() const;
132  void realizeModel(State&) const;
133  void realizeInstance (const State& s) const;
134  void realizeTime (const State& s) const;
135  void realizePosition (const State& s) const;
136  void realizeVelocity (const State& s) const;
137  void realizeDynamics (const State& s) const;
138  void realizeAcceleration(const State& s) const;
139  void realizeReport (const State& s) const;
140 
141  // These wrap the other virtual methods.
142  void multiplyByN(const State& state, const Vector& u,
143  Vector& dq) const;
144  void multiplyByNTranspose(const State& state, const Vector& fq,
145  Vector& fu) const;
146  void multiplyByNPInv(const State& state, const Vector& dq,
147  Vector& u) const;
148  void multiplyByNPInvTranspose(const State& state, const Vector& fu,
149  Vector& fq) const;
150 
151  bool prescribeQ(State&) const;
152  bool prescribeU(State&) const;
153 
154  void projectQ(State&, Vector& qErrEst,
155  const ProjectOptions& options, ProjectResults& results) const;
156  void projectU(State&, Vector& uErrEst,
157  const ProjectOptions& options, ProjectResults& results) const;
158 
159  void handleEvents
160  (State&, Event::Cause, const Array_<EventId>& eventIds,
161  const HandleEventsOptions& options,
162  HandleEventsResults& results) const;
163  void reportEvents(const State&, Event::Cause, const Array_<EventId>& eventIds) const;
164  void calcEventTriggerInfo(const State&, Array_<EventTriggerInfo>&) const;
165  void calcTimeOfNextScheduledEvent(const State&, Real& tNextEvent, Array_<EventId>& eventIds, bool includeCurrentTime) const;
166  void calcTimeOfNextScheduledReport(const State&, Real& tNextEvent, Array_<EventId>& eventIds, bool includeCurrentTime) const;
167 
168  void calcDecorativeGeometryAndAppend(const State&, Stage,
170 
171 
172 protected:
173  Guts(const Guts&); // copies the base class; for use from derived class copy constructors
174 
175  // The destructor is already virtual; see above.
176 
177  virtual System::Guts* cloneImpl() const = 0;
178 
179  // Override these to change the evaluation order of the Subsystems.
180  // The default is to evaluate them in increasing order of SubsystemIndex.
181  // These methods should not be called directly; they are invoked by the
182  // above wrapper methods. Note: the wrappers *will not* call these
183  // routines if the system stage has already met the indicated stage level.
184  // If fact these routines will be called only when the system stage
185  // is at the level just prior to the one indicated here. For example,
186  // realizeVelocityImpl() will be called only if the passed-in State
187  // has been determined to have its system stage exactly Stage::Position.
188  virtual int realizeTopologyImpl(State& state) const {return 0;}
189  virtual int realizeModelImpl (State& state) const {return 0;}
190  virtual int realizeInstanceImpl(const State& state) const {return 0;}
191  virtual int realizeTimeImpl (const State& state) const {return 0;}
192  virtual int realizePositionImpl(const State& state) const {return 0;}
193  virtual int realizeVelocityImpl(const State& state) const {return 0;}
194  virtual int realizeDynamicsImpl(const State& state) const {return 0;}
195  virtual int realizeAccelerationImpl(const State& state) const {return 0;}
196  virtual int realizeReportImpl (const State& state) const {return 0;}
197 
198  virtual void multiplyByNImpl(const State& state, const Vector& u,
199  Vector& dq) const;
200  virtual void multiplyByNTransposeImpl(const State& state, const Vector& fq,
201  Vector& fu) const;
202  virtual void multiplyByNPInvImpl(const State& state, const Vector& dq,
203  Vector& u) const;
204  virtual void multiplyByNPInvTransposeImpl(const State& state, const Vector& fu,
205  Vector& fq) const;
206 
207  // Defaults assume no prescribed motion; hence, no change made.
208  virtual bool prescribeQImpl(State&) const {return false;}
209  virtual bool prescribeUImpl(State&) const {return false;}
210 
211  // Defaults assume no constraints and return success meaning "all
212  // constraints satisfied".
213  virtual void projectQImpl(State& state, Vector& qErrEst,
214  const ProjectOptions& options, ProjectResults& results) const
215  { results.clear(); results.setExitStatus(ProjectResults::Succeeded); }
216  virtual void projectUImpl(State& state, Vector& uErrEst,
217  const ProjectOptions& options, ProjectResults& results) const
218  { results.clear(); results.setExitStatus(ProjectResults::Succeeded); }
219 
220  virtual void handleEventsImpl
221  (State& state, Event::Cause cause, const Array_<EventId>& eventIds,
222  const HandleEventsOptions& options, HandleEventsResults& results) const;
223 
224  virtual int reportEventsImpl(const State& state, Event::Cause cause,
225  const Array_<EventId>& eventIds) const;
226 
227  virtual int calcEventTriggerInfoImpl(const State& state,
228  Array_<EventTriggerInfo>& info) const;
229 
230  virtual int calcTimeOfNextScheduledEventImpl
231  (const State& state, Real& tNextEvent, Array_<EventId>& eventIds,
232  bool includeCurrentTime) const;
233  virtual int calcTimeOfNextScheduledReportImpl
234  (const State& state, Real& tNextEvent, Array_<EventId>& eventIds,
235  bool includeCurrentTime) const;
236 
237 private:
238  Guts& operator=(const Guts&); // suppress default copy assignment operator
239 
240  class EventTriggerInfoRep;
241 
242 };
243 
244 
245 } // namespace SimTK
246 
247 #endif // SimTK_SimTKCOMMON_SYSTEM_GUTS_H_
The abstract parent of all Subsystems.
Definition: Subsystem.h:61
virtual int realizeTimeImpl(const State &state) const
Definition: SystemGuts.h:191
#define SimTK_SimTKCOMMON_EXPORT
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:202
virtual int realizeTopologyImpl(State &state) const
Definition: SystemGuts.h:188
virtual void projectUImpl(State &state, Vector &uErrEst, const ProjectOptions &options, ProjectResults &results) const
Definition: SystemGuts.h:216
Definition: Stage.h:68
This class is basically a glorified enumerated type, type-safe and range checked but permitting conve...
Definition: Stage.h:50
virtual int realizeDynamicsImpl(const State &state) const
Definition: SystemGuts.h:194
virtual int realizeReportImpl(const State &state) const
Definition: SystemGuts.h:196
ProjectResults & clear()
Restore this object to its default-constructed state, with the return status set to Invalid...
Definition: System.h:1074
This is the handle class for the hidden State implementation.
Definition: State.h:264
virtual int realizeAccelerationImpl(const State &state) const
Definition: SystemGuts.h:195
virtual bool prescribeQImpl(State &) const
Definition: SystemGuts.h:208
Results for advanced users of project() methods.
Definition: System.h:1052
These are all the possible causes for events.
Definition: Event.h:123
Options for the advanced project() methods.
Definition: System.h:935
virtual bool prescribeUImpl(State &) const
Definition: SystemGuts.h:209
virtual int realizeVelocityImpl(const State &state) const
Definition: SystemGuts.h:193
The project() was successful either because no projection was necessary or projection was able to ach...
Definition: System.h:1061
The SimTK::Array_<T> container class is a plug-compatible replacement for the C++ standard template l...
Definition: Array.h:50
virtual int realizePositionImpl(const State &state) const
Definition: SystemGuts.h:192
Guts(class GutsRep *r)
Definition: SystemGuts.h:113
GutsRep & updRep() const
Definition: SystemGuts.h:116
ProjectResults & setExitStatus(Status status)
Definition: System.h:1094
SimTK::String is a plug-compatible std::string replacement (plus some additional functionality) inten...
Definition: String.h:62
const GutsRep & getRep() const
Definition: SystemGuts.h:115
This is the declaration for the System::Guts class, the abstract object to which a System handle poin...
Definition: SystemGuts.h:71
bool hasRep() const
Definition: SystemGuts.h:114
This is the base class that serves as the parent of all SimTK System objects; most commonly Simbody's...
Definition: System.h:96
virtual int realizeInstanceImpl(const State &state) const
Definition: SystemGuts.h:190
Provide a unique integer type for identifying Subsystems.
Results returned by the handleEvent() method.
Definition: Event.h:341
This is the header which should be included in user programs that would like to make use of all the S...
Includes internal headers providing declarations for the basic SimTK Core classes.
virtual int realizeModelImpl(State &state) const
Definition: SystemGuts.h:189
virtual void projectQImpl(State &state, Vector &qErrEst, const ProjectOptions &options, ProjectResults &results) const
Definition: SystemGuts.h:213
int StageVersion
This is the type to use for Stage version numbers.
Definition: State.h:154
Options for the handleEvent() method.
Definition: Event.h:265