1 #ifndef SimTK_SimTKCOMMON_MEASURE_IMPLEMENTATION_H_ 2 #define SimTK_SimTKCOMMON_MEASURE_IMPLEMENTATION_H_ 58 : copyNumber(src.copyNumber+1), mySubsystem(0), refCount(0) {}
65 { copyNumber=src.copyNumber+1;
66 refCount=0; mySubsystem=0; }
106 SimTK_ERRCHK2(0 <= derivOrder && derivOrder <= getNumTimeDerivatives(),
107 "Measure::getDependsOnStage()",
108 "derivOrder %d was out of range; this Measure allows 0-%d.",
109 derivOrder, getNumTimeDerivatives());
110 return getDependsOnStageVirtual(derivOrder);
115 { assert(!mySubsystem && mx.isValid());
116 mySubsystem = ⊂ myIndex = mx; }
123 {
return getSubsystem().getMySubsystemIndex(); }
126 {
if (isInSubsystem()) getSubsystem().invalidateSubsystemTopologyCache(); }
135 virtual void realizeTopology(
State&)
const = 0;
148 virtual Stage getDependsOnStageVirtual(
int order)
const = 0;
155 MeasureIndex myIndex;
159 mutable int refCount;
179 "An empty Measure handle can't be put in a Subsystem.");
196 if (impl != src.impl) {
212 impl = src.impl->
clone();
256 template <
class T>
class Measure_Num {
259 template <>
class Measure_Num<float> {
261 typedef float Element;
262 static int size(
const float&) {
return 1;}
263 static const float&
get(
const float& v,
int i) {assert(i==0);
return v;}
264 static float& upd(
float& v,
int i) {assert(i==0);
return v;}
265 static void makeNaNLike(
const float&,
float& nanValue)
267 static void makeZeroLike(
const float&,
float& zeroValue) {zeroValue=0.f;}
270 template <>
class Measure_Num<double> {
272 typedef double Element;
273 static int size(
const double&) {
return 1;}
274 static const double&
get(
const double& v,
int i) {assert(i==0);
return v;}
275 static double& upd(
double& v,
int i) {assert(i==0);
return v;}
276 static void makeNaNLike(
const double&,
double& nanValue)
278 static void makeZeroLike(
const double&,
double& zeroValue) {zeroValue=0.;}
282 template <
int M,
class E>
283 class Measure_Num< Vec<M,
E,1> > {
284 typedef Vec<M,E,1> T;
287 static int size(
const T&) {
return M;}
288 static const E&
get(
const T& v,
int i) {
return v[i];}
289 static E& upd(T& v,
int i) {
return v[i];}
290 static void makeNaNLike (
const T&, T& nanValue) {nanValue.setToNaN();}
291 static void makeZeroLike(
const T&, T& zeroValue) {zeroValue.setToZero();}
295 template <
int M,
int N,
class E>
296 class Measure_Num< Mat<M,N,
E> > {
297 typedef Mat<M,N,E> T;
300 static int size(
const T&) {
return N;}
301 static const typename T::TCol&
get(
const T& m,
int j) {
return m.col(j);}
302 static typename T::TCol& upd(T& m,
int j) {
return m.col(j);}
303 static void makeNaNLike (
const T&, T& nanValue) {nanValue.setToNaN();}
304 static void makeZeroLike(
const T&, T& zeroValue) {zeroValue.setToZero();}
309 class Measure_Num< Vector_<
E> > {
310 typedef Vector_<E> T;
313 static int size(
const T& v) {
return v.size();}
314 static const E&
get(
const T& v,
int i) {
return v[i];}
315 static E& upd(T& v,
int i) {
return v[i];}
316 static void makeNaNLike(
const T& v, T& nanValue)
317 { nanValue.resize(v.size()); nanValue.setToNaN(); }
318 static void makeZeroLike(
const T& v, T& zeroValue)
319 { zeroValue.resize(v.size()); zeroValue.setToZero(); }
324 class Measure_Num< Rotation_<
E> > {
325 typedef Rotation_<E> T;
328 static int size(
const T&) {
return 1;}
329 static const T&
get(
const T& v,
int i) {assert(i==0);
return v;}
330 static T& upd(T& v,
int i) {assert(i==0);
return v;}
331 static void makeNaNLike(
const T&, T& nanValue)
332 { nanValue.setRotationToNaN(); }
333 static void makeZeroLike(
const T&, T& zeroValue)
334 { zeroValue.setRotationToIdentityMatrix(); }
338 class Measure_Num< Transform_<
E> > {
339 typedef Transform_<E> T;
342 static int size(
const T&) {
return 1;}
343 static const T&
get(
const T& v,
int i) {assert(i==0);
return v;}
344 static T& upd(T& v,
int i) {assert(i==0);
return v;}
345 static void makeNaNLike(
const T&, T& nanValue)
346 { nanValue.setToNaN(); }
347 static void makeZeroLike(
const T&, T& zeroValue)
348 { zeroValue.setToZero(); }
367 "Measure_<T>::getValue()",
368 "derivOrder %d was out of range; this Measure allows 0-%d.",
382 "Measure_<T>::getValue()",
383 "Expected State to have been realized to at least stage " 384 "%s but stage was %s.",
390 if (derivOrder < getNumCacheEntries()) {
391 if (!isCacheValueRealized(s,derivOrder)) {
392 T& value = updCacheEntry(s,derivOrder);
393 calcCachedValueVirtual(s, derivOrder, value);
394 markCacheValueRealized(s,derivOrder);
397 return getCacheEntry(s,derivOrder);
402 return getUncachedValueVirtual(s,derivOrder);
408 this->defaultValue = defaultValue;
409 Measure_Num<T>::makeZeroLike(defaultValue, zeroValue);
410 this->invalidateTopologyCache();
419 { presumeValidAtDependsOnStage = presume;
420 this->invalidateTopologyCache(); }
423 {
return presumeValidAtDependsOnStage; }
427 : presumeValidAtDependsOnStage(false),
428 defaultValue(defaultValue),
429 derivIx(numCacheEntries)
431 Measure_Num<T>::makeZeroLike(defaultValue, zeroValue);
438 : presumeValidAtDependsOnStage(false),
440 derivIx(numCacheEntries)
442 Measure_Num<T>::makeZeroLike(defaultValue, zeroValue);
449 : presumeValidAtDependsOnStage(source.presumeValidAtDependsOnStage),
450 defaultValue(source.defaultValue),
451 derivIx(source.derivIx.size())
453 Measure_Num<T>::makeZeroLike(defaultValue, zeroValue);
459 int size()
const {
return Measure_Num<T>::size(defaultValue);}
469 SimTK_ERRCHK2(0 <= derivOrder && derivOrder < getNumCacheEntries(),
470 "Measure_<T>::Implementation::getCacheEntry()",
471 "Derivative order %d is out of range; only %d cache entries" 472 " were allocated.", derivOrder, getNumCacheEntries());
475 this->
getSubsystem().getCacheEntry(s, derivIx[derivOrder]));
482 SimTK_ERRCHK2(0 <= derivOrder && derivOrder < getNumCacheEntries(),
483 "Measure_<T>::Implementation::updCacheEntry()",
484 "Derivative order %d is out of range; only %d cache entries" 485 " were allocated.", derivOrder, getNumCacheEntries());
488 this->
getSubsystem().updCacheEntry(s, derivIx[derivOrder]));
494 SimTK_ERRCHK2(0 <= derivOrder && derivOrder < getNumCacheEntries(),
495 "Measure_<T>::Implementation::isCacheValueRealized()",
496 "Derivative order %d is out of range; only %d cache entries" 497 " were allocated.", derivOrder, getNumCacheEntries());
506 SimTK_ERRCHK2(0 <= derivOrder && derivOrder < getNumCacheEntries(),
507 "Measure_<T>::Implementation::markCacheValueRealized()",
508 "Derivative order %d is out of range; only %d cache entries" 509 " were allocated.", derivOrder, getNumCacheEntries());
519 SimTK_ERRCHK2(0 <= derivOrder && derivOrder < getNumCacheEntries(),
520 "Measure_<T>::Implementation::markCacheValueNotRealized()",
521 "Derivative order %d is out of range; only %d cache entries" 522 " were allocated.", derivOrder, getNumCacheEntries());
538 "Measure_<T>::Implementation::calcCachedValueVirtual()",
539 "This method should have been overridden by the derived" 540 " Measure but was not. It is needed to calculate the" 541 " cached value for derivOrder=%d.", derivOrder); }
551 "Measure_<T>::Implementation::getUncachedValueVirtual()",
552 "This method should have been overridden by the derived" 553 " Measure but was not. It is needed to return the uncached" 554 " value at derivOrder=%d.", derivOrder);
555 return *
reinterpret_cast<T*
>(0);
566 void realizeTopology(
State& s)
const override final {
570 if (getNumCacheEntries()) {
571 derivIx[0] = presumeValidAtDependsOnStage
577 if (getNumCacheEntries() > 1) {
578 T nanValue; Measure_Num<T>::makeNaNLike(defaultValue, nanValue);
579 for (
int i=1; i < getNumCacheEntries(); ++i) {
580 derivIx[i] = presumeValidAtDependsOnStage
590 realizeMeasureTopologyVirtual(s);
596 bool presumeValidAtDependsOnStage;
601 mutable Array_<CacheEntryIndex> derivIx;
629 {
return derivOrder>0 ? this->getValueZero() : this->
getDefaultValue(); }
647 template <
class T>
inline 649 template <
class T>
inline 657 template <
class T>
inline 659 template <
class T>
inline 689 {
return derivOrder==0 ? s.
getTime()
719 invalidatedStage(
Stage::Empty) {}
723 invalidatedStage(invalidated) {}
728 invalidatedStage(source.invalidatedStage) {}
731 invalidatedStage = invalidates;
732 this->invalidateTopologyCache();
741 { updVarValue(state) = value; }
757 {
return derivOrder>0 ? this->getValueZero() : getVarValue(s); }
766 const T& getVarValue(
const State& s)
const {
767 assert(discreteVarIndex.isValid());
769 this->
getSubsystem().getDiscreteVariable(s, discreteVarIndex));
771 T& updVarValue(
State& s)
const {
772 assert(discreteVarIndex.isValid());
774 this->
getSubsystem().updDiscreteVariable(s, discreteVarIndex));
778 Stage invalidatedStage;
801 dependsOnStage(dependsOn==
Stage::Empty ?
Stage::Topology : dependsOn),
802 invalidatedStage(invalidated)
804 "Got invalidated stage %s and dependsOn stage %s which is illegal " 805 "because the invalidated stage must be later than dependsOn.",
812 dependsOnStage(source.dependsOnStage),
813 invalidatedStage(source.invalidatedStage) {}
818 "Measure::Result::setDependsOnStage()",
819 "The provided dependsOn stage %s is illegal because it is not " 820 "less than the current invalidated stage %s. Change the " 821 "invalidated stage first with setInvalidatedStage().",
823 getInvalidatedStage().getName().c_str());
825 dependsOnStage = dependsOn;
826 this->invalidateTopologyCache();
831 "Measure::Result::setInvalidatedStage()",
832 "The provided invalidated stage %s is illegal because it is not " 833 "greater than the current dependsOn stage %s. Change the " 834 "dependsOn stage first with setDependsOnStage().",
838 invalidatedStage = invalidated;
839 this->invalidateTopologyCache();
850 "Measure::Result::markAsValid()",
851 "This Result Measure cannot be marked valid in a State where this " 852 "measure's Subsystem has been realized only to stage %s, because " 853 "its value was declared to depend on stage %s. To mark it valid, " 854 "we require that the State have been realized at least to the " 855 "previous stage (%s in this case); that is, you must at least be " 856 "*working on* the dependsOn stage in order to claim this result is " 858 subsystemStage.
getName().c_str(),
861 this->markCacheValueRealized(state, 0); }
864 {
return this->isCacheValueRealized(state, 0); }
867 { this->markCacheValueNotRealized(state, 0);
871 { markAsNotValid(state);
return this->updCacheEntry(state, 0); }
888 "Measure_<T>::Result::getValue()",
889 "Measure_<T>::Result::getValue() was called when the value was not " 890 "yet valid. For most Measure types, this would have initiated " 891 "computation of the value, but Result measures must have their values " 892 "calculated and set externally, and then marked valid."); }
896 Stage dependsOnStage;
897 Stage invalidatedStage;
909 static const int NumDerivs = 3;
913 a(
CNT<T>::getNaN()), w(
CNT<T>::getNaN()), p(
CNT<T>::getNaN()) {}
919 a(amplitude), w(frequency), p(phase) {}
937 using std::sin;
using std::cos;
939 assert(NumDerivs == 3);
941 const T arg = w*t + p;
943 switch (derivOrder) {
944 case 0: value = a*sin(arg);
break;
945 case 1: value = w*a*cos(arg);
break;
946 case 2: value = -w*w*a*sin(arg);
break;
947 case 3: value = -w*w*w*a*cos(arg);
break;
949 "Measure::Sinusoid::Implementation::calcCachedValueVirtual():" 950 " derivOrder %d is out of range 0-3.", derivOrder);
978 : left(left), right(right) {}
997 right.getDependsOnStage(order))); }
1003 value = left.getValue(s,derivOrder) + right.getValue(s,derivOrder);
1031 : left(left), right(right) {}
1050 right.getDependsOnStage(order))); }
1056 value = left.getValue(s,derivOrder) - right.getValue(s,derivOrder);
1085 : factor(factor), operand(operand) {}
1092 this->invalidateTopologyCache();
1113 {
return operand.getDependsOnStage(order); }
1119 value = factor * operand.getValue(s,derivOrder);
1155 const T& defaultValue)
1157 derivMeasure(deriv), icMeasure(ic) {}
1163 derivMeasure(source.derivMeasure), icMeasure(source.icMeasure) {}
1169 { assert(zIndex >= 0);
1170 for (
int i=0; i < this->size(); ++i)
1172 Measure_Num<T>::get(value, i); }
1176 "Measure_<T>::Integrate::getDerivativeMeasure()",
1177 "No derivative measure is available for this integrated measure.");
1178 return derivMeasure; }
1182 "Measure_<T>::Integrate::getInitialConditionMeasure()",
1183 "No initial condition measure is available for this " 1184 "integrated measure.");
1188 { derivMeasure = d; this->invalidateTopologyCache(); }
1190 { icMeasure = ic; this->invalidateTopologyCache(); }
1200 {
int integralDerivs = getDerivativeMeasure().getNumTimeDerivatives();
1204 return integralDerivs; }
1208 { assert(derivOrder == 0);
1209 assert(Measure_Num<T>::size(value) == this->size());
1210 assert(zIndex.isValid());
1212 for (
int i=0; i < this->size(); ++i)
1213 Measure_Num<T>::upd(value,i) = allZ[zIndex+i];
1218 { assert(derivOrder > 0);
1219 return getDerivativeMeasure().getValue(s, derivOrder-1);
1223 {
return derivOrder>0
1224 ? getDerivativeMeasure().getDependsOnStage(derivOrder-1)
1230 assert(zIndex.isValid());
1232 if (!icMeasure.isEmptyHandle()) {
1234 .
realize(s, icMeasure.getDependsOnStage());
1235 const T& ic = icMeasure.getValue(s);
1236 for (
int i=0; i < this->size(); ++i)
1237 allZ[zIndex+i] = Measure_Num<T>::get(ic,i);
1239 for (
int i=0; i < this->size(); ++i)
1249 Vector init(this->size());
1250 for (
int i=0; i < this->size(); ++i)
1258 assert(zIndex.isValid());
1260 if (!derivMeasure.isEmptyHandle()) {
1261 const T& deriv = derivMeasure.getValue(s);
1262 for (
int i=0; i < this->size(); ++i)
1263 allZDot[zIndex+i] = Measure_Num<T>::get(deriv,i);
1265 allZDot(zIndex,this->size()) = 0;
1293 class Measure_Differentiate_Result {
1295 Measure_Differentiate_Result() : derivIsGood(false) {}
1306 typedef Measure_Differentiate_Result<T> Result;
1313 operand(operand), forceUseApprox(false), isApproxInUse(false) {}
1319 forceUseApprox = mustApproximate;
1320 this->invalidateTopologyCache();
1324 this->operand = operand;
1325 this->invalidateTopologyCache();
1340 {
if (!isApproxInUse)
return operand.getNumTimeDerivatives()-1;
1344 {
if (!isApproxInUse)
return operand.getDependsOnStage(order+1);
1345 else return operand.getDependsOnStage(order); }
1353 {
if (!isApproxInUse)
1354 return operand.getValue(s, derivOrder+1);
1356 ensureDerivativeIsRealized(s);
1360 return result.operandDot;
1364 if (!isApproxInUse)
return;
1366 assert(resultIx.isValid());
1369 (subsys.updDiscreteVariable(s,resultIx));
1371 result.operand = operand.getValue(s);
1372 result.operandDot = this->getValueZero();
1373 result.derivIsGood =
false;
1377 isApproxInUse = (forceUseApprox || operand.getNumTimeDerivatives()==0);
1389 ensureDerivativeIsRealized(s);
1393 assert(resultIx.isValid());
1395 if (subsys.isDiscreteVarUpdateValueRealized(s,resultIx))
1398 const Real t0 = subsys.getDiscreteVarLastUpdateTime(s,resultIx);
1400 (subsys.getDiscreteVariable(s,resultIx));
1401 const T& f0 = prevResult.operand;
1402 const T& fdot0 = prevResult.operandDot;
1403 const bool good0 = prevResult.derivIsGood;
1407 (subsys.updDiscreteVarUpdateValue(s,resultIx));
1408 T& f = result.operand;
1409 T& fdot = result.operandDot;
1410 bool& good = result.derivIsGood;
1415 fdot = this->getValueZero();
1420 fdot = (f-f0)/(t-t0);
1422 fdot =
Real(2)*fdot - fdot0;
1425 subsys.markDiscreteVarUpdateValueRealized(s,resultIx);
1430 bool forceUseApprox;
1433 mutable bool isApproxInUse;
1465 this->operand = operand;
1466 this->invalidateTopologyCache();
1473 this->operation = op;
1474 this->invalidateTopologyCache();
1487 assert(extremeIx.isValid());
1490 (subsys.updDiscreteVariable(s,extremeIx));
1499 const bool hasNewExtreme = ensureExtremeHasBeenUpdated(s);
1517 {
return operand.getNumTimeDerivatives(); }
1522 {
return operand.getDependsOnStage(order); }
1532 const bool hasNewExtreme = ensureExtremeHasBeenUpdated(s);
1533 if (derivOrder > 0) {
1537 return hasNewExtreme ? operand.getValue(s, derivOrder)
1538 : this->getValueZero();
1540 if (hasNewExtreme) {
1555 setValue(s, operand.getValue(s));
1572 case MaxAbs: initVal = 0;
break;
1577 new Value<T>(initVal), operand.getDependsOnStage(0));
1581 new Value<bool>(
false), operand.getDependsOnStage(0));
1587 ensureExtremeHasBeenUpdated(s);
1596 assert(extremeIx.isValid() && isNewExtremeIx.isValid());
1601 if (subsys.isDiscreteVarUpdateValueRealized(s, isNewExtremeIx))
1603 (subsys.getDiscreteVarUpdateValue(s,isNewExtremeIx));
1611 (subsys.getDiscreteVariable(s,extremeIx));
1612 const T& currentVal = operand.
getValue(s);
1615 bool foundNewExt =
false;
1616 for (
int i=0; i < this->size() && !foundNewExt; ++i)
1617 foundNewExt = isNewExtreme(Measure_Num<T>::get(currentVal,i),
1618 Measure_Num<T>::get(prevExtreme,i));
1625 (subsys.updDiscreteVarUpdateValue(s,isNewExtremeIx)) = foundNewExt;
1626 subsys.markDiscreteVarUpdateValueRealized(s,isNewExtremeIx);
1640 (subsys.updDiscreteVarUpdateValue(s,extremeIx));
1642 for (
int i=0; i < this->size(); ++i)
1643 Measure_Num<T>::upd(newExtreme,i) =
1644 extremeOf(Measure_Num<T>::get(currentVal,i),
1645 Measure_Num<T>::get(prevExtreme,i));
1648 subsys.markDiscreteVarUpdateValueRealized(s,extremeIx);
1654 bool isNewExtreme(
const typename Measure_Num<T>::Element& newVal,
1655 const typename Measure_Num<T>::Element& oldExtreme)
const 1657 switch (operation) {
1664 "Measure::Extreme::Implementation::isNewExtreme(): " 1665 "unrecognized operation %d", (
int)operation);
1672 bool isExtremeDir(
const typename Measure_Num<T>::Element& value,
1673 const typename Measure_Num<T>::Element& deriv)
const 1675 const int sv =
sign(value), sd =
sign(deriv);
1676 if (sd == 0)
return false;
1677 switch (operation) {
1684 "Measure::Extreme::Implementation::isExtremeDir(): " 1685 "unrecognized operation %d", (
int)operation);
1689 typename Measure_Num<T>::Element
1690 extremeOf(
const typename Measure_Num<T>::Element& newVal,
1691 const typename Measure_Num<T>::Element& oldExtreme)
const 1693 return isNewExtreme(newVal,oldExtreme) ? newVal : oldExtreme;
1697 Measure_<T> operand;
1701 mutable DiscreteVariableIndex extremeIx;
1707 mutable DiscreteVariableIndex isNewExtremeIx;
1746 class Measure_Delay_Buffer {
1748 explicit Measure_Delay_Buffer() {initDataMembers();}
1749 void clear() {initDataMembers();}
1750 int size()
const {
return m_size;}
1751 int capacity()
const {
return m_times.size();}
1752 bool empty()
const {
return size()==0;}
1753 bool full()
const {
return size()==capacity();}
1755 double getEntryTime(
int i)
const 1756 { assert(i < size());
return m_times[getArrayIndex(i)];}
1757 const T& getEntryValue(
int i)
const 1758 { assert(i < size());
return m_values[getArrayIndex(i)];}
1761 InitialAllocation = 8,
1763 MaxShrinkProofSize = 16,
1769 void append(
double tEarliest,
double tNow,
const T& valueNow) {
1770 forgetEntriesMuchOlderThan(tEarliest);
1771 removeEntriesLaterOrEq(tNow);
1774 else if (capacity() >
std::max((
int)MaxShrinkProofSize,
1775 (
int)TooBigFactor * (size()+1)))
1777 const int nextFree = getArrayIndex(m_size++);
1778 m_times[nextFree] = tNow;
1779 m_values[nextFree] = valueNow;
1780 m_maxSize =
std::max(m_maxSize, size());
1784 void prepend(
double tNewOldest,
const T& value) {
1785 assert(empty() || tNewOldest < m_times[m_oldest]);
1786 if (full()) makeMoreRoom();
1787 m_oldest = empty() ? 0 : getArrayIndex(-1);
1788 m_times[m_oldest] = tNewOldest;
1789 m_values[m_oldest] = value;
1791 m_maxSize =
std::max(m_maxSize, size());
1800 void copyInAndUpdate(
const Measure_Delay_Buffer& oldBuf,
double tEarliest,
1801 double tNow,
const T& valueNow) {
1803 m_oldest = m_size = 0;
1806 int firstNeeded = oldBuf.countNumUnneededOldEntries(tEarliest);
1807 int lastNeeded = oldBuf.findLastEarlier(tNow);
1808 int numOldEntriesToKeep = lastNeeded-firstNeeded+1;
1809 int newSize = numOldEntriesToKeep+1;
1811 int newSizeRequest = -1;
1812 if (capacity() < newSize) {
1813 newSizeRequest =
std::max((
int)InitialAllocation,
1814 (
int)GrowthFactor * newSize);
1816 }
else if (capacity() >
std::max((
int)MaxShrinkProofSize,
1817 (
int)TooBigFactor * newSize)) {
1818 newSizeRequest =
std::max((
int)MaxShrinkProofSize,
1819 (
int)GrowthFactor * newSize);
1824 if (newSizeRequest != -1) {
1825 const double dNaN = NTraits<double>::getNaN();
1826 m_values.resize(newSizeRequest);
1827 if (m_values.capacity() > m_values.size())
1828 m_values.resize(m_values.capacity());
1829 m_times.resize(m_values.size(), dNaN);
1832 m_maxCapacity =
std::max(m_maxCapacity, capacity());
1836 for (
int i=firstNeeded; i<=lastNeeded; ++i, ++nxt) {
1837 m_times[nxt] = oldBuf.getEntryTime(i);
1838 m_values[nxt] = oldBuf.getEntryValue(i);
1841 m_times[nxt] = tNow;
1842 m_values[nxt] = valueNow;
1843 assert(nxt+1==newSize);
1845 m_maxSize =
std::max(m_maxSize, size());
1851 T calcValueAtTime(
double tDelay,
double tNow,
const T& valueNow)
const;
1856 void calcValueAtTimeLinearOnly(
double tDelay, T& delayedValue)
const {
1860 Measure_Num<T>::makeNaNLike(T(), delayedValue);
1864 int firstLater = findFirstLaterOrEq(tDelay);
1866 if (firstLater > 0) {
1868 int firstEarlier = firstLater-1;
1869 double t0=getEntryTime(firstEarlier), t1=getEntryTime(firstLater);
1870 const T& v0=getEntryValue(firstEarlier);
1871 const T& v1=getEntryValue(firstLater);
1872 Real fraction =
Real((tDelay-t0)/(t1-t0));
1873 delayedValue = T(v0 + fraction*(v1-v0));
1877 if (firstLater==0) {
1880 delayedValue = getEntryValue(firstLater);
1889 delayedValue = getEntryValue(0);
1894 double t0=getEntryTime(size()-2), t1=getEntryTime(size()-1);
1895 const T& v0=getEntryValue(size()-2);
1896 const T& v1=getEntryValue(size()-1);
1897 Real fraction =
Real((tDelay-t0)/(t1-t0));
1898 assert(fraction > 1.0);
1899 delayedValue = T(v0 + fraction*(v1-v0));
1903 int getNumGrows()
const {
return m_nGrows;}
1906 int getNumShrinks()
const {
return m_nShrinks;}
1908 int getMaxSize()
const {
return m_maxSize;}
1910 int getMaxCapacity()
const {
return m_maxCapacity;}
1915 int getArrayIndex(
int i)
const 1916 { assert(-1<=i && i<=size());
1917 const int rawIndex = m_oldest + i;
1918 if (rawIndex < 0)
return rawIndex + capacity();
1919 else return rawIndex % capacity(); }
1922 void forgetEntriesMuchOlderThan(
double tEarliest) {
1923 const int numToRemove = countNumUnneededOldEntries(tEarliest);
1925 m_oldest = getArrayIndex(numToRemove);
1926 m_size -= numToRemove;
1933 int countNumUnneededOldEntries(
double tEarliest)
const {
1934 const int firstLater = findFirstLaterOrEq(tEarliest);
1940 void removeEntriesLaterOrEq(
double t) {
1941 int lastEarlier = findLastEarlier(t);
1942 m_size = lastEarlier+1;
1943 if (m_size==0) m_oldest=0;
1948 int findFirstLaterOrEq(
double tDelay)
const {
1949 for (
int i=0; i < size(); ++i)
1950 if (getEntryTime(i) >= tDelay)
1957 int findLastEarlier(
double t)
const {
1958 for (
int i=size()-1; i>=0; --i)
1959 if (getEntryTime(i) < t)
1966 void makeMoreRoom() {
1967 const int newSizeRequest =
std::max((
int)InitialAllocation,
1968 (
int)GrowthFactor * size());
1969 resize(newSizeRequest);
1971 m_maxCapacity =
std::max(m_maxCapacity, capacity());
1976 void makeLessRoom() {
1977 const int targetMaxSize =
std::max((
int)MaxShrinkProofSize,
1978 (
int)GrowthFactor * size());
1979 if (capacity() > targetMaxSize) {
1980 resize(targetMaxSize);
1990 void resize(
int newSizeRequest) {
1991 assert(newSizeRequest >= size());
1992 const double dNaN = NTraits<double>::getNaN();
1993 Array_<T,int> newValues(newSizeRequest);
1994 if (newValues.capacity() > newValues.size())
1995 newValues.resize(newValues.capacity());
1996 Array_<double,int> newTimes(newValues.size(), dNaN);
1999 for (
int i=0; i < size(); ++i) {
2000 const int ix = getArrayIndex(i);
2001 newTimes[i] = m_times[ix];
2002 newValues[i] = m_values[ix];
2004 m_times.swap(newTimes);
2005 m_values.swap(newValues);
2010 void initDataMembers() {
2011 m_times.clear(); m_values.clear();
2013 m_nGrows=m_nShrinks=m_maxSize=m_maxCapacity=0;
2017 Array_<double,int> m_times;
2018 Array_<T,int> m_values;
2023 int m_nGrows, m_nShrinks, m_maxSize, m_maxCapacity;
2029 typedef Measure_Delay_Buffer<T> Buffer;
2035 m_canUseCurrentValue(false), m_useLinearInterpolationOnly(false) {}
2039 m_canUseCurrentValue(false), m_useLinearInterpolationOnly(false) {}
2046 this->m_source = source;
2047 this->invalidateTopologyCache();
2052 if (delay != this->m_delay) {
2053 this->m_delay = delay;
2054 this->invalidateTopologyCache();
2059 if (linearOnly != this->m_useLinearInterpolationOnly) {
2060 this->m_useLinearInterpolationOnly = linearOnly;
2061 this->invalidateTopologyCache();
2066 if (canUseCurrentValue != this->m_canUseCurrentValue) {
2067 this->m_canUseCurrentValue = canUseCurrentValue;
2068 this->invalidateTopologyCache();
2075 {
return this->m_useLinearInterpolationOnly; }
2077 {
return this->m_canUseCurrentValue; }
2094 {
return this->m_canUseCurrentValue ? m_source.getDependsOnStage(order)
2105 buffer.calcValueAtTimeLinearOnly(s.
getTime()-m_delay, value);
2109 assert(m_bufferIx.isValid());
2112 (subsys.updDiscreteVariable(s,m_bufferIx));
2115 buffer.append(s.
getTime()-m_delay, s.
getTime(), m_source.getValue(s));
2134 assert(m_bufferIx.isValid());
2138 (subsys.getDiscreteVariable(s,m_bufferIx));
2141 (subsys.updDiscreteVarUpdateValue(s,m_bufferIx));
2144 nextBuffer.copyInAndUpdate(prevBuffer, t-m_delay,
2147 subsys.markDiscreteVarUpdateValueRealized(s,m_bufferIx);
2153 bool m_canUseCurrentValue;
2154 bool m_useLinearInterpolationOnly;
2166 #endif // SimTK_SimTKCOMMON_MEASURE_IMPLEMENTATION_H_ Definition: MeasureImplementation.h:2028
void setIsPresumedValidAtDependsOnStage(bool presume)
Definition: MeasureImplementation.h:418
Definition: MeasureImplementation.h:610
Implementation()
Definition: MeasureImplementation.h:911
Stage getDependsOnStageVirtual(int order) const override
Definition: MeasureImplementation.h:929
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:1339
int getNumTimeDerivativesVirtual() const override
Extreme(f(t)) has the same number of derivatives as f except that they are all zero unless f(t) is a ...
Definition: MeasureImplementation.h:1516
Operation getOperation() const
Return the particular operation being performed by this Extreme measure.
Definition: MeasureImplementation.h:1482
A Subsystem is expected to be part of a larger System and to have interdependencies with other subsys...
Definition: Subsystem.h:55
void setOperandMeasure(const Measure_< T > &operand)
Set the operand measure for this Extreme measure; this is a Topology stage change so you'll have to c...
Definition: MeasureImplementation.h:1464
const T & getUncachedValueVirtual(const State &s, int derivOrder) const override
This is only called when derivOrder >= the number of cache entries we have, but still <= the number o...
Definition: MeasureImplementation.h:755
virtual void realizeMeasureModelVirtual(State &) const
Definition: MeasureImplementation.h:137
virtual void realizeMeasureTimeVirtual(const State &) const
Definition: MeasureImplementation.h:139
#define SimTK_SimTKCOMMON_EXPORT
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:224
bool isInSubsystem() const
Definition: MeasureImplementation.h:118
Stage getStage(const State &s) const
Definition: MeasureImplementation.h:128
#define SimTK_ERRCHK2_ALWAYS(cond, whereChecked, fmt, a1, a2)
Definition: ExceptionMacros.h:289
int getRefCount() const
Definition: MeasureImplementation.h:249
int size() const
Return the number of elements in the data type of this Measure; for Vector measures this is determine...
Definition: MeasureImplementation.h:459
const Measure_< T > & getOperandMeasure() const
Definition: MeasureImplementation.h:1330
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:747
void initializeVirtual(State &s) const override
At start of a time stepping study, this should be called to set the current extreme value to the curr...
Definition: MeasureImplementation.h:1553
#define SimTK_ASSERT1_ALWAYS(cond, msg, a1)
Definition: ExceptionMacros.h:351
virtual void calcCachedValueVirtual(const State &, int derivOrder, T &value) const
Concrete measures must override this if the state cache is used for precalculated values or derivativ...
Definition: MeasureImplementation.h:536
Implementation(Stage invalidated, const T &defaultValue)
Definition: MeasureImplementation.h:721
Definition: MeasureImplementation.h:673
virtual void realizeMeasureInstanceVirtual(const State &) const
Definition: MeasureImplementation.h:138
static const Value & downcast(const AbstractValue &value)
Downcast a const reference to an AbstractValue to a const reference to this type Value<T>.
Definition: Value.h:219
Stage getDependsOnStageVirtual(int order) const override
Definition: MeasureImplementation.h:995
Modeling choices made.
Definition: Stage.h:71
static Value & updDowncast(AbstractValue &value)
Downcast a writable reference to an AbstractValue to a writable reference to this type Value<T>...
Definition: Value.h:225
int incrRefCount() const
Definition: MeasureImplementation.h:73
Vector & updZ(State &s) const
Definition: Subsystem.h:137
#define SimTK_ERRCHK1_ALWAYS(cond, whereChecked, fmt, a1)
Definition: ExceptionMacros.h:285
MeasureIndex getSubsystemMeasureIndex() const
Definition: MeasureImplementation.h:121
Implementation(const Implementation &src)
Base class copy constructor removes the Subsystem and sets the reference count to zero...
Definition: MeasureImplementation.h:57
Real getDelay() const
Definition: MeasureImplementation.h:2073
const Real & getTime() const
You can call these as long as system stage >= Model.
Implementation(const Implementation &source)
Copy constructor copies the number of cache entries from the source, but not the cache indices themse...
Definition: MeasureImplementation.h:448
friend class Implementation
Definition: Measure.h:253
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:991
Definition: Measure.h:845
Stage getDependsOnStageVirtual(int derivOrder) const override
Definition: MeasureImplementation.h:752
MeasureIndex adoptMeasure(AbstractMeasure &)
Obtain the Subsystem name if one was given on construction of the concrete Subsystem.
Definition: SubsystemGuts.h:509
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:2083
DiscreteVariableIndex allocateDiscreteVariable(State &s, Stage g, AbstractValue *v) const
Definition: Subsystem.h:96
Implementation(const Measure_< T > &operand)
Definition: MeasureImplementation.h:1311
String getName() const
Return a printable name corresponding to the stage level currently stored in this Stage...
Definition: Stage.h:144
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
Implementation(const T &defaultValue, int numCacheEntries=1)
Definition: MeasureImplementation.h:426
void ensureDerivativeIsRealized(const State &s) const
Definition: MeasureImplementation.h:1392
Definition: Measure.h:842
void calcCachedValueVirtual(const State &s, int derivOrder, T &value) const override
Concrete measures must override this if the state cache is used for precalculated values or derivativ...
Definition: MeasureImplementation.h:1000
void realizeMeasureAccelerationVirtual(const State &s) const override
Set the zdots to the integrand (derivative measure) value.
Definition: MeasureImplementation.h:1257
const T & getUncachedValueVirtual(const State &s, int derivOrder) const override
We're not using the Measure_<T> base class cache services, but we do have one of our own...
Definition: MeasureImplementation.h:1528
const T & getCacheEntry(const State &s, int derivOrder) const
Get a const reference to the value stored in one of this Measure's cache entries, indexed by the deri...
Definition: MeasureImplementation.h:468
bool isUsingApproximation() const
Definition: MeasureImplementation.h:1329
void setInvalidatedStage(Stage invalidated)
Definition: MeasureImplementation.h:829
Stage getDependsOnStageVirtual(int order) const override
Definition: MeasureImplementation.h:1048
const Subsystem & getSubsystem() const
Return a reference to the Subsystem that owns this Measure.
Definition: MeasureImplementation.h:229
virtual ~Implementation()
Definition: MeasureImplementation.h:132
const AbstractValue & getDiscreteVariable(const State &s, DiscreteVariableIndex index) const
Definition: Subsystem.h:245
Lower than any legitimate Stage.
Definition: Stage.h:69
const T & getDefaultValue() const
Obtain a reference to the default value associated with this Measure.
Definition: Measure.h:290
void setOperandMeasure(const Measure_< T > &operand)
Definition: MeasureImplementation.h:1323
void setCanUseCurrentValue(bool canUseCurrentValue)
Definition: MeasureImplementation.h:2065
void setValue(State &s, const T &value) const
Set the value of the state variables(s) that hold the integral.
Definition: MeasureImplementation.h:1168
void setInitialConditionMeasure(const Measure_< T > &ic)
Definition: MeasureImplementation.h:1189
Implementation(const Measure_< T > &source, Real delay)
Definition: MeasureImplementation.h:2037
This class is basically a glorified enumerated type, type-safe and range checked but permitting conve...
Definition: Stage.h:66
void realizeMeasureAccelerationVirtual(const State &s) const override
In case no one has updated the value of this measure yet, we have to make sure it gets updated before...
Definition: MeasureImplementation.h:1388
void calcCachedValueVirtual(const State &s, int derivOrder, T &value) const override
Concrete measures must override this if the state cache is used for precalculated values or derivativ...
Definition: MeasureImplementation.h:1053
bool isInSubsystem() const
Test whether this Measure object has been adopted by a Subsystem.
Definition: MeasureImplementation.h:225
int getNumTimeDerivativesVirtual() const override
This measure has one more time derivative than the integrand.
Definition: MeasureImplementation.h:1199
Stage getDependsOnStageVirtual(int order) const override
Definition: MeasureImplementation.h:2093
DiscreteVariableIndex allocateAutoUpdateDiscreteVariable(State &s, Stage invalidates, AbstractValue *v, Stage updateDependsOn) const
Definition: Subsystem.h:99
void calcCachedValueVirtual(const State &s, int derivOrder, T &value) const override
Concrete measures must override this if the state cache is used for precalculated values or derivativ...
Definition: MeasureImplementation.h:1206
Track the value of the operand that is of maximum absolute value.
Definition: Measure.h:905
void setValue(State &s, const T &value) const
Set the current extreme value stored in this Extreme measure's state variable.
Definition: MeasureImplementation.h:1486
virtual void realizeMeasureTopologyVirtual(State &) const
Concrete measures can override this to allocate Topology-stage resources.
Definition: MeasureImplementation.h:531
The abstract parent of all Measure Implementation classes.
Definition: MeasureImplementation.h:48
This is the base Implementation class for all Measures whose value type is known. ...
Definition: MeasureImplementation.h:363
Measure_ & setDefaultValue(const T &defaultValue)
Change the default value associated with this Measure.
Definition: Measure.h:285
void updateBuffer(const State &s) const
Definition: MeasureImplementation.h:2133
Definition: MeasureImplementation.h:790
void calcCachedValueVirtual(const State &s, int derivOrder, T &value) const override
Concrete measures must override this if the state cache is used for precalculated values or derivativ...
Definition: MeasureImplementation.h:1116
Real getDiscreteVarLastUpdateTime(const State &s, DiscreteVariableIndex dx) const
Definition: Subsystem.h:253
void markCacheValueNotRealized(const State &s, CacheEntryIndex cx) const
Definition: Subsystem.h:275
bool isValid(const State &state) const
Definition: MeasureImplementation.h:863
Implementation()
The derivative and initialConditions Measures will be empty handles if this is default constructed...
Definition: MeasureImplementation.h:1150
void setDelay(Real delay)
Definition: MeasureImplementation.h:2051
void realizeMeasureTopologyVirtual(State &s) const override
Concrete measures can override this to allocate Topology-stage resources.
Definition: MeasureImplementation.h:1376
void realizeTime(const State &s) const
Definition: MeasureImplementation.h:91
This is the base class for all Measure handle classes.
Definition: Measure.h:151
Stage getDependsOnStage(int derivOrder) const
Definition: MeasureImplementation.h:105
Implementation()
This default constructor is for use by concrete measure implementation classes.
Definition: MeasureImplementation.h:52
Implementation * clone() const
This is a deep copy of the concrete Implementation object, except the Subsystem will have been remove...
Definition: MeasureImplementation.h:86
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
ZIndex allocateZ(State &s, const Vector &zInit) const
Definition: Subsystem.h:92
This Measure tracks extreme values attained by the elements of its source operand since the last init...
Definition: Measure.h:837
void calcCachedValueVirtual(const State &s, int derivOrder, T &value) const override
Concrete measures must override this if the state cache is used for precalculated values or derivativ...
Definition: MeasureImplementation.h:932
void realizeReport(const State &s) const
Definition: MeasureImplementation.h:96
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:700
Stage getDependsOnStageVirtual(int order) const override
The depends-on stage for this measure is the same as for its operand.
Definition: MeasureImplementation.h:1521
void setValue(State &state, const T &value) const
Change the value of this Measure in the given state.
Definition: MeasureImplementation.h:740
bool ensureExtremeHasBeenUpdated(const State &s) const
Here we make sure that the cache entry is updated if the current value of the operand is more extreme...
Definition: MeasureImplementation.h:1595
int getRefCount() const
Definition: MeasureImplementation.h:79
Track the minimum value of the operand (signed).
Definition: Measure.h:885
Stage getDependsOnStageVirtual(int order) const override
Definition: MeasureImplementation.h:1112
bool isSameSubsystem(const Subsystem &otherSubsystem) const
Determine if this Subsystem handle refers to the same Subsystem::Guts object as handle otherSubsystem...
Definition: Subsystem.h:322
This object is intended to contain all state information for a SimTK::System, except topological info...
Definition: State.h:280
This file declares the base class AbstractMeasure for all derived Measure handle classes, and the handle classes for built-in Measures.
const T & getUncachedValueVirtual(const State &s, int derivOrder) const override
This is only called when derivOrder >= the number of cache entries we have, but still <= the number o...
Definition: MeasureImplementation.h:1351
const Real NaN
This is the IEEE "not a number" constant for this implementation of the default-precision Real type; ...
Definition: MeasureImplementation.h:1023
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:924
bool getCanUseCurrentValue() const
Definition: MeasureImplementation.h:2076
int getCopyNumber() const
Definition: MeasureImplementation.h:81
void initializeVirtual(State &s) const override
Definition: MeasureImplementation.h:2108
Implementation(const T &litude, const T &frequency, const T &phase=T(0))
Definition: MeasureImplementation.h:915
Implementation()
Definition: MeasureImplementation.h:1309
Stage getDependsOnStage() const
Definition: MeasureImplementation.h:843
const Stage & getSystemStage() const
This returns the global stage for this State.
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:986
bool isFinite(const negator< float > &x)
Definition: negator.h:285
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:1195
void calcCachedValueVirtual(const State &, int derivOrder, T &value) const override
Concrete measures must override this if the state cache is used for precalculated values or derivativ...
Definition: MeasureImplementation.h:885
const AbstractValue & getDiscreteVarUpdateValue(const State &s, DiscreteVariableIndex dx) const
Definition: Subsystem.h:259
Stage getInvalidatedStage() const
Definition: MeasureImplementation.h:735
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:1335
void calcCachedValueVirtual(const State &s, int derivOrder, T &value) const override
Concrete measures must override this if the state cache is used for precalculated values or derivativ...
Definition: MeasureImplementation.h:2099
const Measure_< T > & getOperandMeasure() const
Definition: MeasureImplementation.h:1095
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:1044
void setValue(const T &v)
Changing the value of a Constant measure is a topological change; if this is a Vector measure you can...
Definition: MeasureImplementation.h:621
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:632
bool getIsPresumedValidAtDependsOnStage() const
Definition: MeasureImplementation.h:422
void setSourceMeasure(const Measure_< T > &source)
Definition: MeasureImplementation.h:2044
MeasureIndex getSubsystemMeasureIndex() const
Return the MeasureIndex by which this Measure is known to the Subsystem that owns it...
Definition: MeasureImplementation.h:237
Implementation(const Implementation &source)
Definition: MeasureImplementation.h:726
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:744
Stage getDependsOnStageVirtual(int derivOrder) const override
Definition: MeasureImplementation.h:634
void initializeVirtual(State &s) const override
Definition: MeasureImplementation.h:1363
Report-only quantities evaluated.
Definition: Stage.h:78
const Real & getUncachedValueVirtual(const State &s, int derivOrder) const override
This is only called when derivOrder >= the number of cache entries we have, but still <= the number o...
Definition: MeasureImplementation.h:687
Definition: MeasureImplementation.h:970
Definition: Measure.h:844
Definition: MeasureImplementation.h:1076
System topology realized.
Definition: Stage.h:70
void initialize(State &s) const
This should be called at the start of a time stepping study to cause this Measure to set its state va...
Definition: MeasureImplementation.h:101
Stage getStage(const State &s) const
Definition: Subsystem.h:239
Implementation(Stage dependsOn, Stage invalidated)
Definition: MeasureImplementation.h:799
Implementation()
Definition: MeasureImplementation.h:717
Forces calculated.
Definition: Stage.h:76
Implementation()
Definition: MeasureImplementation.h:2033
bool hasImpl() const
Definition: Measure.h:244
Implementation()
Definition: MeasureImplementation.h:1027
#define SimTK_ERRCHK_ALWAYS(cond, whereChecked, msg)
Definition: ExceptionMacros.h:281
void realizeMeasureTopologyVirtual(State &s) const override
Concrete measures can override this to allocate Topology-stage resources.
Definition: MeasureImplementation.h:2118
void realize(const State &state, Stage stage=Stage::HighestRuntime) const
Realize the given state to the indicated stage.
void setOperation(Operation op)
Set the particular operation to be performed by this Extreme measure; this is a Topology stage change...
Definition: MeasureImplementation.h:1472
const T & getUncachedValueVirtual(const State &, int derivOrder) const override
This is only called when derivOrder >= the number of cache entries we have, but still <= the number o...
Definition: MeasureImplementation.h:627
Unique integer type for Subsystem-local z indexing.
void realizeMeasureTopologyVirtual(State &s) const override
Allocate the auto-updated state variable that holds the extreme seen so far.
Definition: MeasureImplementation.h:1565
Implementation(const Measure_< T > &left, const Measure_< T > &right)
Definition: MeasureImplementation.h:1029
T & updValue(const State &state) const
Definition: MeasureImplementation.h:870
const T & getValue() const
Retrieve the original (type-erased)thing as read-only.
Definition: Value.h:79
Implementation & operator=(const Implementation &src)
Base class copy assignment operator removes the Subsystem, and sets the reference count to zero...
Definition: MeasureImplementation.h:63
Implementation()
Definition: MeasureImplementation.h:681
#define SimTK_ERRCHK3_ALWAYS(cond, whereChecked, fmt, a1, a2, a3)
Definition: ExceptionMacros.h:293
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:875
bool isCacheValueRealized(const State &s, CacheEntryIndex cx) const
Definition: Subsystem.h:271
CacheEntryIndex allocateCacheEntry(const State &s, Stage dependsOn, Stage computedBy, AbstractValue *v) const
Definition: Subsystem.h:103
ELEM max(const VectorBase< ELEM > &v)
Definition: VectorMath.h:251
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:1039
Implementation(const Measure_< T > &left, const Measure_< T > &right)
Definition: MeasureImplementation.h:976
This creates a Measure whose value is a Topology-stage constant of any type T.
Definition: Measure.h:341
virtual void realizeMeasureReportVirtual(const State &) const
Definition: MeasureImplementation.h:144
void setForceUseApproximation(bool mustApproximate)
Definition: MeasureImplementation.h:1318
Subsystem & updSubsystem()
Definition: MeasureImplementation.h:120
The abstract parent of all Subsystem implementation classes.
Definition: SubsystemGuts.h:47
void realizeMeasureAccelerationVirtual(const State &s) const override
In case no one has updated the value of this measure yet, we have to make sure it gets updated before...
Definition: MeasureImplementation.h:1586
SubsystemIndex getSubsystemIndex() const
Definition: MeasureImplementation.h:122
bool isCacheValueRealized(const State &s, int derivOrder) const
Determine whether a particular one of this Measure's cache entries has already been realized since th...
Definition: MeasureImplementation.h:493
bool getForceUseApproximation() const
Definition: MeasureImplementation.h:1328
Zero()
Definition: MeasureImplementation.h:648
int getNumCacheEntries() const
Return the number of cache entries allocated for the value and derivatives of this Measure...
Definition: MeasureImplementation.h:463
virtual void realizeMeasurePositionVirtual(const State &) const
Definition: MeasureImplementation.h:140
Stage prev() const
Return the Stage before this one, with Stage::Empty returned if this Stage is already at its lowest v...
Definition: Stage.h:139
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:878
Implementation()
Definition: MeasureImplementation.h:974
int getNumTimeDerivatives() const
Every Measure can produce a value, and some can provide one or more total derivatives with respect to...
Definition: MeasureImplementation.h:241
~AbstractMeasure()
Destructor decrements the Implementation's reference count and deletes the object if the count goes t...
Definition: MeasureImplementation.h:221
const Implementation & getImpl() const
Definition: Measure.h:242
Stage getDependsOnStageVirtual(int derivOrder) const override
Definition: MeasureImplementation.h:1222
RowVectorBase< typename CNT< ELEM >::TAbs > abs(const RowVectorBase< ELEM > &v)
Definition: VectorMath.h:120
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:694
#define SimTK_ERRCHK(cond, whereChecked, msg)
Definition: ExceptionMacros.h:324
const T & getUncachedValueVirtual(const State &s, int derivOrder) const override
This is only called when derivOrder >= the number of cache entries we have, but still <= the number o...
Definition: MeasureImplementation.h:1216
const Real Infinity
This is the IEEE positive infinity constant for this implementation of the default-precision Real typ...
#define SimTK_ERRCHK2(cond, whereChecked, fmt, a1, a2)
Definition: ExceptionMacros.h:328
Specialized information about Composite Numerical Types which allows us to define appropriate templat...
Definition: CompositeNumericalTypes.h:136
Definition: MeasureImplementation.h:906
const System & getSystem() const
Return a const reference to the System that contains this Subsystem.
Definition: SubsystemGuts.h:520
const Vector & getZ(const State &s) const
Definition: Subsystem.h:128
One()
Definition: MeasureImplementation.h:658
void realizeMeasureTopologyVirtual(State &s) const override
Allocate one Real continuous state variable z per element of this Measure's data type T...
Definition: MeasureImplementation.h:1248
void realizeModel(State &s) const
Definition: MeasureImplementation.h:89
bool isSameSubsystem(const Subsystem &) const
Is getSubsystem() the same as the passed-in Subsystem?
Definition: MeasureImplementation.h:233
void realizeVelocity(const State &s) const
Definition: MeasureImplementation.h:93
void markCacheValueRealized(const State &s, CacheEntryIndex cx) const
Definition: Subsystem.h:273
virtual void realizeMeasureAccelerationVirtual(const State &) const
Definition: MeasureImplementation.h:143
Declares the user-visible part of a SimTK::State, the implementation is done in a separate internal c...
Definition: MeasureImplementation.h:1443
const Real E
e = Real(exp(1))
Implementation(const Measure_< T > &deriv, const Measure_< T > &ic, const T &defaultValue)
Here we're shallow-copying the Measure handles so we'll be referring to the original Measures...
Definition: MeasureImplementation.h:1154
const Measure_< T > & getSourceMeasure() const
Definition: MeasureImplementation.h:2072
void realizeMeasureAccelerationVirtual(const State &s) const override
In case no one has updated the value of this measure yet, we have to make sure it gets updated before...
Definition: MeasureImplementation.h:2126
void invalidateAllCacheAtOrAbove(Stage) const
If any subsystem or the system stage is currently at or higher than the passed-in one...
Stage getDependsOnStageVirtual(int derivOrder) const override
Cache value is available after its "depends on" stage has been realized; but all its derivatives are ...
Definition: MeasureImplementation.h:882
This unique integer type is for selecting discrete variables.
void realizeAcceleration(const State &s) const
Definition: MeasureImplementation.h:95
Definition: Measure.h:843
Stage getInvalidatedStage() const
Definition: MeasureImplementation.h:844
The implementation for Integrate measures allocates a continuous state variable or variables from the...
Definition: MeasureImplementation.h:1145
Implementation(const T &value)
Definition: MeasureImplementation.h:616
Vector & updZDot(const State &s) const
Definition: Subsystem.h:152
unsigned int sign(unsigned char u)
Definition: Scalar.h:311
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:1103
Implementation(const Implementation &source)
Definition: MeasureImplementation.h:810
const T & getValueZero() const
Return a reference to a zero of the same type and size as this Measure's value.
Definition: MeasureImplementation.h:560
Implementation()
Default constructor leaves the operand measure unspecified; no base class cache entries are allocated...
Definition: MeasureImplementation.h:1450
void invalidateTopologyCache() const
Definition: MeasureImplementation.h:125
void realizeDynamics(const State &s) const
Definition: MeasureImplementation.h:94
void markAsNotValid(const State &state) const
Definition: MeasureImplementation.h:866
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:1511
void setDerivativeMeasure(const Measure_< T > &d)
Definition: MeasureImplementation.h:1187
int getNumTimeDerivatives() const
Definition: MeasureImplementation.h:103
Track the maximum value of the operand (signed).
Definition: Measure.h:895
bool getUseLinearInterpolationOnly() const
Definition: MeasureImplementation.h:2074
AbstractMeasure & deepAssign(const AbstractMeasure &source)
Deep assignment clones the Implementation object pointed to by the source handle, so that this handle...
Definition: MeasureImplementation.h:208
Implementation(int numCacheEntries=1)
Argument numCacheEntries should be one greater than the number of derivatives; that is...
Definition: MeasureImplementation.h:437
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:2087
Stage getDependsOnStageVirtual(int order) const override
Definition: MeasureImplementation.h:1343
Stage getDependsOnStage(int derivOrder=0) const
At what Stage can we expect the value of this AbstractMeasure or one of its time derivatives to be av...
Definition: MeasureImplementation.h:245
void realizeMeasureTopologyVirtual(State &s) const override
Concrete measures can override this to allocate Topology-stage resources.
Definition: MeasureImplementation.h:761
Provide a unique integer type for identifying Subsystems.
void setUseLinearInterpolationOnly(bool linearOnly)
Definition: MeasureImplementation.h:2058
CacheEntryIndex allocateLazyCacheEntry(const State &state, Stage earliest, AbstractValue *v) const
Definition: Subsystem.h:111
Implementation(const Measure_< T > &operand, Operation op)
Construct a measure that returns the extreme value taken on by the operand measure during a time step...
Definition: MeasureImplementation.h:1455
void markCacheValueNotRealized(const State &s, int derivOrder) const
Invalidate one of this Measure's cache entries.
Definition: MeasureImplementation.h:518
Operation
Definition: Measure.h:841
Track the value of the operand that is of minimum absolute value (not very useful).
Definition: Measure.h:916
Implementation()
Definition: MeasureImplementation.h:1082
An object of this type is used as a dummy argument to make sure the automatically-generated handle co...
Definition: Measure.h:156
void setScaleFactor(Real sf)
Definition: MeasureImplementation.h:1090
void setDependsOnStage(Stage dependsOn)
Definition: MeasureImplementation.h:815
Implementation(Real factor, const Measure_< T > &operand)
Definition: MeasureImplementation.h:1084
void setSubsystem(Subsystem &sub, MeasureIndex mx)
Definition: MeasureImplementation.h:114
AbstractMeasure(Implementation *g=0)
Provide an Implementation for this AbstractMeasure and bump its reference count.
Definition: MeasureImplementation.h:171
const Measure_< T > & getOperandMeasure() const
Return a reference to the operand measure for this Extreme measure.
Definition: MeasureImplementation.h:1478
void setDefaultValue(const T &defaultValue)
Set a new default value for this Measure.
Definition: MeasureImplementation.h:407
const T & getValue(const State &s, int derivOrder) const
Definition: MeasureImplementation.h:365
const T & getDefaultValue() const
Return a reference to the value that this Measure will use to initialize its value-level state resour...
Definition: MeasureImplementation.h:416
AbstractMeasure & shallowAssign(const AbstractMeasure &)
Shallow assignment operator destructs the current Implementation object (meaning its reference count ...
Definition: MeasureImplementation.h:195
Definition: MeasureImplementation.h:710
A new time has been realized.
Definition: Stage.h:73
void realizeInstance(const State &s) const
Definition: MeasureImplementation.h:90
This is the header which should be included in user programs that would like to make use of all the S...
static K getNaN()
Definition: CompositeNumericalTypes.h:246
Real getTimeOfExtremeValue(const State &s) const
Return the time at which the extreme was last updated.
Definition: MeasureImplementation.h:1497
void markCacheValueRealized(const State &s, int derivOrder) const
Mark one of this Measure's cache entries up to date; call this after you have calculated a value or d...
Definition: MeasureImplementation.h:505
const Measure_< T > & getDerivativeMeasure() const
Definition: MeasureImplementation.h:1174
Stage getDependsOnStageVirtual(int derivOrder) const override
Definition: MeasureImplementation.h:696
const Subsystem & getSubsystem() const
Definition: MeasureImplementation.h:119
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:636
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:1108
Implementation(const Implementation &source)
Copy constructor shallow-copies the referenced measures, but we don't want to share our state variabl...
Definition: MeasureImplementation.h:1161
void realizePosition(const State &s) const
Definition: MeasureImplementation.h:92
virtual int getNumTimeDerivativesVirtual() const
Definition: MeasureImplementation.h:147
Includes internal headers providing declarations for the basic SimTK Core classes.
void setInvalidatedStage(Stage invalidates)
Definition: MeasureImplementation.h:730
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:927
virtual void realizeMeasureVelocityVirtual(const State &) const
Definition: MeasureImplementation.h:141
virtual void initializeVirtual(State &) const
Definition: MeasureImplementation.h:146
bool isSameMeasure(const AbstractMeasure &other) const
There can be multiple handles on the same Measure.
Definition: Measure.h:220
void initializeVirtual(State &s) const override
Initialize the state to the current value of the initial condition measure, if there is one...
Definition: MeasureImplementation.h:1229
virtual const T & getUncachedValueVirtual(const State &, int derivOrder) const
This is only called when derivOrder >= the number of cache entries we have, but still <= the number o...
Definition: MeasureImplementation.h:549
Definition: MeasureImplementation.h:1303
Implementation()
Definition: MeasureImplementation.h:795
T & updCacheEntry(const State &s, int derivOrder) const
Get a writable reference to the value stored in one of this Measure's cache entries, indexed by the derivative order (with the value treated as the 0th derivative).
Definition: MeasureImplementation.h:481
virtual void realizeMeasureDynamicsVirtual(const State &) const
Definition: MeasureImplementation.h:142
int decrRefCount() const
Definition: MeasureImplementation.h:76
const Measure_< T > & getInitialConditionMeasure() const
Definition: MeasureImplementation.h:1180
This is the base handle class for all Measures whose value type is known, including all the Simbody b...
Definition: Measure.h:263
void markAsValid(const State &state) const
Definition: MeasureImplementation.h:847
Implementation()
Definition: MeasureImplementation.h:615
Concrete templatized class derived from AbstractValue, adding generic value type-specific functionali...
Definition: Value.h:39