Simbody  3.6
Event.h
Go to the documentation of this file.
1 #ifndef SimTK_SimTKCOMMON_EVENT_H_
2 #define SimTK_SimTKCOMMON_EVENT_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) 2008-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 
32 #include "SimTKcommon/basics.h"
33 
34 namespace SimTK {
35 
40 
47 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemEventTriggerIndex);
48 
57 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SystemEventTriggerByStageIndex);
58 
62 SimTK_DEFINE_UNIQUE_INDEX_TYPE(EventTriggerByStageIndex);
63 
76 class Event {
77 public:
78 
126  class Cause {
127  public:
128  enum Num {
133  Signaled = 5,
135  Invalid = -1
136  };
137 
138  Cause() : value(Invalid) {}
139  Cause(Num n) : value(n) {} // implicit conversion
140  operator Num() const {return value;} // implicit conversion
141  Cause& operator=(Num n) {value=n; return *this;}
142 
143  bool isValid() const {return Initialization<=value && value<=Termination;}
144 
145  private:
146  Num value;
147  };
148 
151  SimTK_SimTKCOMMON_EXPORT static const char* getCauseName(Cause);
152 
153 
158  enum Trigger {
159  NoEventTrigger =0x0000, // must be 0
160 
161  PositiveToNegative =0x0001, // 1
162  NegativeToPositive =0x0002, // 2
163 
167  };
168 
172 
173 
177  static Trigger classifyTransition(int before, int after) {
178  if (before==after)
179  return NoEventTrigger;
180  if (before==0)
181  return NoEventTrigger; // Do not report transitions away from zero.
182  if (before==1)
183  return PositiveToNegative;
184  // before==-1
185  return NegativeToPositive;
186  }
187 
191  static Trigger maskTransition(Trigger transition, Trigger mask) {
192  // we're depending on NoEventTrigger==0
193  return Trigger(transition & mask);
194  }
195 
196 private:
197 };
198 
199 
210 public:
212  explicit EventTriggerInfo(EventId eventId);
213  ~EventTriggerInfo();
216 
217  EventId getEventId() const; // returns -1 if not set
218  bool shouldTriggerOnRisingSignTransition() const; // default=true
219  bool shouldTriggerOnFallingSignTransition() const; // default=true
220  Real getRequiredLocalizationTimeWindow() const; // default=0.1
221 
222  // These return the modified 'this', like assignment operators.
223  EventTriggerInfo& setEventId(EventId);
224  EventTriggerInfo& setTriggerOnRisingSignTransition(bool);
225  EventTriggerInfo& setTriggerOnFallingSignTransition(bool);
226  EventTriggerInfo& setRequiredLocalizationTimeWindow(Real);
227 
229  unsigned mask = 0;
230  if (shouldTriggerOnRisingSignTransition()) {
232  }
233  if (shouldTriggerOnFallingSignTransition()) {
235  }
236  return Event::Trigger(mask);
237  }
238 
239  Event::Trigger calcTransitionToReport
240  (Event::Trigger transitionSeen) const
241  {
242  // report -1 to 1 or 1 to -1 as appropriate
243  if (transitionSeen & Event::Rising)
245  if (transitionSeen & Event::Falling)
247  assert(!"impossible event transition situation");
248  return Event::NoEventTrigger;
249  }
250 
251 private:
252  class EventTriggerInfoRep;
253 
254  // opaque implementation for binary compatibility
255  EventTriggerInfoRep* rep;
256 
257  const EventTriggerInfoRep& getRep() const {assert(rep); return *rep;}
258  EventTriggerInfoRep& updRep() {assert(rep); return *rep;}
259 };
260 
261 
262 
263 
264 //==============================================================================
265 // HANDLE EVENTS OPTIONS and HANDLE EVENTS RESULTS
266 //==============================================================================
270 public:
271  enum Option {
273  None = 0x0000,
277  DontThrow = 0x0001,
280  UseInfinityNorm = 0x0002
281  };
282 
283 
284  HandleEventsOptions() {clear();}
285  explicit HandleEventsOptions(Real accuracy)
286  { clear(); setAccuracy(accuracy); }
288  { clear(); setOption(opt); }
289 
294  { optionSet=0; setAccuracyDefaults(); return *this; }
295 
300  assert(accuracy > 0);
301  requiredAccuracy = accuracy;
302  return *this;
303  }
304 
308  { optionSet &= ~(unsigned)opt; return *this; }
312  { optionSet |= (unsigned)opt; return *this; }
313 
315  Real getAccuracy() const {return requiredAccuracy;}
316 
317  bool isOptionSet(Option opt) const {return (optionSet&(unsigned)opt) != 0;}
318 
319  static Real getDefaultAccuracy() {return Real(1e-4);}
320 
321  // Set operators: not, or, and, set difference
323  { optionSet |= opts.optionSet; return *this; }
324  HandleEventsOptions& operator&=(const HandleEventsOptions& opts)
325  { optionSet &= opts.optionSet; return *this; }
327  { optionSet &= ~opts.optionSet; return *this; }
328 
329  HandleEventsOptions& operator|=(Option opt) {setOption(opt); return *this;}
330  HandleEventsOptions& operator-=(Option opt) {clearOption(opt); return *this;}
331 
332 private:
333  Real requiredAccuracy;
334  unsigned optionSet;
335 
336  void setAccuracyDefaults() {
337  requiredAccuracy = getDefaultAccuracy();
338  }
339 };
340 
346 public:
347  HandleEventsResults() : m_lowestModifiedStage(Stage::Infinity) {clear();}
348 
349  enum Status {
351  Invalid = -1,
354  Succeeded = 0,
358  ShouldTerminate = 1,
362  Failed = 2
363  };
364 
368  m_exitStatus = Invalid;
369  m_anyChangeMade = false;
370  m_lowestModifiedStage = Stage::Infinity; // i.e., nothing modified
371  m_message.clear();
372  return *this;
373  }
374  bool isValid() const {return m_exitStatus != Invalid;}
375  Status getExitStatus() const {return m_exitStatus;}
376 
377  bool getAnyChangeMade() const
378  { assert(isValid()); return m_anyChangeMade; }
380  { assert(isValid()); return m_lowestModifiedStage; }
381  const String& getMessage() const
382  { assert(isValid()); return m_message; }
383 
385  { m_exitStatus=status; return *this; }
387  { m_anyChangeMade=changeMade; return *this; }
389  { m_lowestModifiedStage=stage; return *this; }
391  { m_message=message; return *this; }
392 private:
393  Status m_exitStatus;
394  bool m_anyChangeMade;
395  Stage m_lowestModifiedStage;
396  String m_message;
397 };
398 
399 } // namespace SimTK
400 
401 #endif // SimTK_SimTKCOMMON_EVENT_H_
#define SimTK_SimTKCOMMON_EXPORT
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:224
Trigger
Triggered Events respond to zero crossings of their associated trigger function.
Definition: Event.h:158
SimTK_DEFINE_UNIQUE_INDEX_TYPE(AssemblyConditionIndex)
Status getExitStatus() const
Definition: Event.h:375
HandleEventsOptions & operator-=(const HandleEventsOptions &opts)
Definition: Event.h:326
Definition: Event.h:165
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
static std::string eventTriggerString(Trigger)
This is useful for debugging; it translates an Event::Trigger or a mask formed by a union of Event::T...
Definition: Event.h:131
Definition: Event.h:159
Definition: Event.h:164
static Trigger classifyTransition(int before, int after)
Classify a before/after sign transition.
Definition: Event.h:177
This class is basically a glorified enumerated type, type-safe and range checked but permitting conve...
Definition: Stage.h:66
Definition: Event.h:135
HandleEventsOptions & setAccuracy(Real accuracy)
The norm of the constraint errors must be driven to below this value for a project() to be considered...
Definition: Event.h:299
Definition: Event.h:166
bool getAnyChangeMade() const
Definition: Event.h:377
HandleEventsResults & setExitStatus(Status status)
Definition: Event.h:384
SimTK_Real Real
This is the default compiled-in floating point type for SimTK, either float or double.
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:606
HandleEventsOptions & clearOption(Option opt)
Remove a given option from the set.
Definition: Event.h:307
HandleEventsResults & setMessage(const String &message)
Definition: Event.h:390
bool isValid() const
Definition: Event.h:374
HandleEventsOptions & setOption(Option opt)
Select a given option from the set.
Definition: Event.h:311
Definition: Event.h:133
An Event is "something that happens" during a Study that is advancing through time.
Definition: Event.h:76
Num
Definition: Event.h:128
Definition: Event.h:134
HandleEventsResults & setAnyChangeMade(bool changeMade)
Definition: Event.h:386
Event::Trigger calcTransitionMask() const
Definition: Event.h:228
These are all the possible causes for events.
Definition: Event.h:126
HandleEventsOptions & operator-=(Option opt)
Definition: Event.h:330
Option
Definition: Event.h:271
HandleEventsOptions & clear()
Restore this object to its default-constructed state (no options selected, default accuracy)...
Definition: Event.h:293
HandleEventsOptions & operator|=(Option opt)
Definition: Event.h:329
HandleEventsOptions & operator|=(const HandleEventsOptions &opts)
Definition: Event.h:322
Cause()
Definition: Event.h:138
HandleEventsOptions()
Definition: Event.h:284
Definition: Event.h:130
static const char * getCauseName(Cause)
This is useful for debugging; it translates an Event::Cause into a readable string.
bool isValid() const
Definition: Event.h:143
Higher than any legitimate Stage.
Definition: Stage.h:79
const Real Infinity
This is the IEEE positive infinity constant for this implementation of the default-precision Real typ...
HandleEventsResults & clear()
Restore this object to its default-constructed state, with the return status set to Invalid...
Definition: Event.h:367
HandleEventsResults()
Definition: Event.h:347
bool isOptionSet(Option opt) const
Definition: Event.h:317
SimTK::String is a plug-compatible std::string replacement (plus some additional functionality) inten...
Definition: String.h:62
static Trigger maskTransition(Trigger transition, Trigger mask)
Given an observed transition, weed out ignorable ones using the supplied mask.
Definition: Event.h:191
Cause(Num n)
Definition: Event.h:139
Definition: Event.h:162
Status
Definition: Event.h:349
This class is used to communicate between the System and an Integrator regarding the properties of a ...
Definition: Event.h:209
Real getAccuracy() const
Return the current value for the accuracy option.
Definition: Event.h:315
Definition: Event.h:132
Results returned by the handleEvent() method.
Definition: Event.h:345
HandleEventsOptions(Option opt)
Definition: Event.h:287
Includes internal headers providing declarations for the basic SimTK Core classes.
const String & getMessage() const
Definition: Event.h:381
Definition: Event.h:161
HandleEventsOptions(Real accuracy)
Definition: Event.h:285
Stage getLowestModifiedStage() const
Definition: Event.h:379
Cause & operator=(Num n)
Definition: Event.h:141
HandleEventsResults & setLowestModifiedStage(Stage stage)
Definition: Event.h:388
static Real getDefaultAccuracy()
Definition: Event.h:319
Options for the handleEvent() method.
Definition: Event.h:269
This is a class to represent unique IDs for events in a type-safe way.