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();
252 template <
class T>
class Measure_Num {
255 template <>
class Measure_Num<float> {
257 typedef float Element;
258 static int size(
const float&) {
return 1;}
259 static const float&
get(
const float& v,
int i) {assert(i==0);
return v;}
260 static float& upd(
float& v,
int i) {assert(i==0);
return v;}
261 static void makeNaNLike(
const float&,
float& nanValue)
263 static void makeZeroLike(
const float&,
float& zeroValue) {zeroValue=0.f;}
266 template <>
class Measure_Num<double> {
268 typedef double Element;
269 static int size(
const double&) {
return 1;}
270 static const double&
get(
const double& v,
int i) {assert(i==0);
return v;}
271 static double& upd(
double& v,
int i) {assert(i==0);
return v;}
272 static void makeNaNLike(
const double&,
double& nanValue)
274 static void makeZeroLike(
const double&,
double& zeroValue) {zeroValue=0.;}
278 template <
int M,
class E>
279 class Measure_Num< Vec<M,E,1> > {
283 static int size(
const T&) {
return M;}
284 static const E&
get(
const T& v,
int i) {
return v[i];}
285 static E& upd(T& v,
int i) {
return v[i];}
286 static void makeNaNLike (
const T&, T& nanValue) {nanValue.setToNaN();}
287 static void makeZeroLike(
const T&, T& zeroValue) {zeroValue.setToZero();}
291 template <
int M,
int N,
class E>
292 class Measure_Num< Mat<M,N,E> > {
296 static int size(
const T&) {
return N;}
297 static const typename T::TCol&
get(
const T& m,
int j) {
return m.col(j);}
298 static typename T::TCol& upd(T& m,
int j) {
return m.col(j);}
299 static void makeNaNLike (
const T&, T& nanValue) {nanValue.setToNaN();}
300 static void makeZeroLike(
const T&, T& zeroValue) {zeroValue.setToZero();}
305 class Measure_Num< Vector_<E> > {
309 static int size(
const T& v) {
return v.size();}
310 static const E&
get(
const T& v,
int i) {
return v[i];}
311 static E& upd(T& v,
int i) {
return v[i];}
312 static void makeNaNLike(
const T& v, T& nanValue)
313 { nanValue.resize(v.size()); nanValue.setToNaN(); }
314 static void makeZeroLike(
const T& v, T& zeroValue)
315 { zeroValue.resize(v.size()); zeroValue.setToZero(); }
320 class Measure_Num< Rotation_<E> > {
324 static int size(
const T&) {
return 1;}
325 static const T&
get(
const T& v,
int i) {assert(i==0);
return v;}
326 static T& upd(T& v,
int i) {assert(i==0);
return v;}
327 static void makeNaNLike(
const T&, T& nanValue)
328 { nanValue.setRotationToNaN(); }
329 static void makeZeroLike(
const T&, T& zeroValue)
330 { zeroValue.setRotationToIdentityMatrix(); }
334 class Measure_Num< Transform_<E> > {
338 static int size(
const T&) {
return 1;}
339 static const T&
get(
const T& v,
int i) {assert(i==0);
return v;}
340 static T& upd(T& v,
int i) {assert(i==0);
return v;}
341 static void makeNaNLike(
const T&, T& nanValue)
342 { nanValue.setToNaN(); }
343 static void makeZeroLike(
const T&, T& zeroValue)
344 { zeroValue.setToZero(); }
363 "Measure_<T>::getValue()",
364 "derivOrder %d was out of range; this Measure allows 0-%d.",
378 "Measure_<T>::getValue()",
379 "Expected State to have been realized to at least stage " 380 "%s but stage was %s.",
386 if (derivOrder < getNumCacheEntries()) {
387 if (!isCacheValueRealized(s,derivOrder)) {
388 T& value = updCacheEntry(s,derivOrder);
389 calcCachedValueVirtual(s, derivOrder, value);
390 markCacheValueRealized(s,derivOrder);
393 return getCacheEntry(s,derivOrder);
398 return getUncachedValueVirtual(s,derivOrder);
404 this->defaultValue = defaultValue;
405 Measure_Num<T>::makeZeroLike(defaultValue, zeroValue);
406 this->invalidateTopologyCache();
415 { presumeValidAtDependsOnStage = presume;
416 this->invalidateTopologyCache(); }
419 {
return presumeValidAtDependsOnStage; }
423 : presumeValidAtDependsOnStage(false),
424 defaultValue(defaultValue),
425 derivIx(numCacheEntries)
427 Measure_Num<T>::makeZeroLike(defaultValue, zeroValue);
434 : presumeValidAtDependsOnStage(false),
436 derivIx(numCacheEntries)
438 Measure_Num<T>::makeZeroLike(defaultValue, zeroValue);
445 : presumeValidAtDependsOnStage(source.presumeValidAtDependsOnStage),
446 defaultValue(source.defaultValue),
447 derivIx(source.derivIx.size())
449 Measure_Num<T>::makeZeroLike(defaultValue, zeroValue);
455 int size()
const {
return Measure_Num<T>::size(defaultValue);}
465 SimTK_ERRCHK2(0 <= derivOrder && derivOrder < getNumCacheEntries(),
466 "Measure_<T>::Implementation::getCacheEntry()",
467 "Derivative order %d is out of range; only %d cache entries" 468 " were allocated.", derivOrder, getNumCacheEntries());
471 this->
getSubsystem().getCacheEntry(s, derivIx[derivOrder]));
478 SimTK_ERRCHK2(0 <= derivOrder && derivOrder < getNumCacheEntries(),
479 "Measure_<T>::Implementation::updCacheEntry()",
480 "Derivative order %d is out of range; only %d cache entries" 481 " were allocated.", derivOrder, getNumCacheEntries());
484 this->
getSubsystem().updCacheEntry(s, derivIx[derivOrder]));
490 SimTK_ERRCHK2(0 <= derivOrder && derivOrder < getNumCacheEntries(),
491 "Measure_<T>::Implementation::isCacheValueRealized()",
492 "Derivative order %d is out of range; only %d cache entries" 493 " were allocated.", derivOrder, getNumCacheEntries());
502 SimTK_ERRCHK2(0 <= derivOrder && derivOrder < getNumCacheEntries(),
503 "Measure_<T>::Implementation::markCacheValueRealized()",
504 "Derivative order %d is out of range; only %d cache entries" 505 " were allocated.", derivOrder, getNumCacheEntries());
515 SimTK_ERRCHK2(0 <= derivOrder && derivOrder < getNumCacheEntries(),
516 "Measure_<T>::Implementation::markCacheValueNotRealized()",
517 "Derivative order %d is out of range; only %d cache entries" 518 " were allocated.", derivOrder, getNumCacheEntries());
534 "Measure_<T>::Implementation::calcCachedValueVirtual()",
535 "This method should have been overridden by the derived" 536 " Measure but was not. It is needed to calculate the" 537 " cached value for derivOrder=%d.", derivOrder); }
547 "Measure_<T>::Implementation::getUncachedValueVirtual()",
548 "This method should have been overridden by the derived" 549 " Measure but was not. It is needed to return the uncached" 550 " value at derivOrder=%d.", derivOrder);
551 return *
reinterpret_cast<T*
>(0);
562 void realizeTopology(
State& s)
const override final {
566 if (getNumCacheEntries()) {
567 derivIx[0] = presumeValidAtDependsOnStage
573 if (getNumCacheEntries() > 1) {
574 T nanValue; Measure_Num<T>::makeNaNLike(defaultValue, nanValue);
575 for (
int i=1; i < getNumCacheEntries(); ++i) {
576 derivIx[i] = presumeValidAtDependsOnStage
586 realizeMeasureTopologyVirtual(s);
592 bool presumeValidAtDependsOnStage;
617 void setValue(
const T& v) {this->setDefaultValue(v);}
625 {
return derivOrder>0 ? this->getValueZero() : this->getDefaultValue(); }
643 template <
class T>
inline 645 template <
class T>
inline 653 template <
class T>
inline 655 template <
class T>
inline 685 {
return derivOrder==0 ? s.
getTime()
715 invalidatedStage(
Stage::Empty) {}
719 invalidatedStage(invalidated) {}
724 invalidatedStage(source.invalidatedStage) {}
727 invalidatedStage = invalidates;
728 this->invalidateTopologyCache();
737 { updVarValue(state) = value; }
753 {
return derivOrder>0 ? this->getValueZero() : getVarValue(s); }
762 const T& getVarValue(
const State& s)
const {
763 assert(discreteVarIndex.isValid());
765 this->
getSubsystem().getDiscreteVariable(s, discreteVarIndex));
767 T& updVarValue(
State& s)
const {
768 assert(discreteVarIndex.isValid());
770 this->
getSubsystem().updDiscreteVariable(s, discreteVarIndex));
774 Stage invalidatedStage;
797 dependsOnStage(dependsOn==
Stage::Empty ?
Stage::Topology : dependsOn),
798 invalidatedStage(invalidated)
800 "Got invalidated stage %s and dependsOn stage %s which is illegal " 801 "because the invalidated stage must be later than dependsOn.",
808 dependsOnStage(source.dependsOnStage),
809 invalidatedStage(source.invalidatedStage) {}
814 "Measure::Result::setDependsOnStage()",
815 "The provided dependsOn stage %s is illegal because it is not " 816 "less than the current invalidated stage %s. Change the " 817 "invalidated stage first with setInvalidatedStage().",
819 getInvalidatedStage().getName().c_str());
821 dependsOnStage = dependsOn;
822 this->invalidateTopologyCache();
827 "Measure::Result::setInvalidatedStage()",
828 "The provided invalidated stage %s is illegal because it is not " 829 "greater than the current dependsOn stage %s. Change the " 830 "dependsOn stage first with setDependsOnStage().",
834 invalidatedStage = invalidated;
835 this->invalidateTopologyCache();
846 "Measure::Result::markAsValid()",
847 "This Result Measure cannot be marked valid in a State where this " 848 "measure's Subsystem has been realized only to stage %s, because " 849 "its value was declared to depend on stage %s. To mark it valid, " 850 "we require that the State have been realized at least to the " 851 "previous stage (%s in this case); that is, you must at least be " 852 "*working on* the dependsOn stage in order to claim this result is " 854 subsystemStage.
getName().c_str(),
857 this->markCacheValueRealized(state, 0); }
860 {
return this->isCacheValueRealized(state, 0); }
863 { this->markCacheValueNotRealized(state, 0);
867 { markAsNotValid(state);
return this->updCacheEntry(state, 0); }
884 "Measure_<T>::Result::getValue()",
885 "Measure_<T>::Result::getValue() was called when the value was not " 886 "yet valid. For most Measure types, this would have initiated " 887 "computation of the value, but Result measures must have their values " 888 "calculated and set externally, and then marked valid."); }
892 Stage dependsOnStage;
893 Stage invalidatedStage;
905 static const int NumDerivs = 3;
909 a(
CNT<T>::getNaN()), w(
CNT<T>::getNaN()), p(
CNT<T>::getNaN()) {}
915 a(amplitude), w(frequency), p(phase) {}
933 using std::sin;
using std::cos;
935 assert(NumDerivs == 3);
937 const T arg = w*t + p;
939 switch (derivOrder) {
940 case 0: value = a*sin(arg);
break;
941 case 1: value = w*a*cos(arg);
break;
942 case 2: value = -w*w*a*sin(arg);
break;
943 case 3: value = -w*w*w*a*cos(arg);
break;
945 "Measure::Sinusoid::Implementation::calcCachedValueVirtual():" 946 " derivOrder %d is out of range 0-3.", derivOrder);
974 : left(left), right(right) {}
993 right.getDependsOnStage(order))); }
999 value = left.getValue(s,derivOrder) + right.getValue(s,derivOrder);
1027 : left(left), right(right) {}
1046 right.getDependsOnStage(order))); }
1052 value = left.getValue(s,derivOrder) - right.getValue(s,derivOrder);
1081 : factor(factor), operand(operand) {}
1088 this->invalidateTopologyCache();
1109 {
return operand.getDependsOnStage(order); }
1115 value = factor * operand.getValue(s,derivOrder);
1151 const T& defaultValue)
1153 derivMeasure(deriv), icMeasure(ic) {}
1159 derivMeasure(source.derivMeasure), icMeasure(source.icMeasure) {}
1165 { assert(zIndex >= 0);
1166 for (
int i=0; i < this->size(); ++i)
1168 Measure_Num<T>::get(value, i); }
1172 "Measure_<T>::Integrate::getDerivativeMeasure()",
1173 "No derivative measure is available for this integrated measure.");
1174 return derivMeasure; }
1178 "Measure_<T>::Integrate::getInitialConditionMeasure()",
1179 "No initial condition measure is available for this " 1180 "integrated measure.");
1184 { derivMeasure = d; this->invalidateTopologyCache(); }
1186 { icMeasure = ic; this->invalidateTopologyCache(); }
1196 {
int integralDerivs = getDerivativeMeasure().getNumTimeDerivatives();
1200 return integralDerivs; }
1204 { assert(derivOrder == 0);
1205 assert(Measure_Num<T>::size(value) == this->size());
1206 assert(zIndex.isValid());
1208 for (
int i=0; i < this->size(); ++i)
1209 Measure_Num<T>::upd(value,i) = allZ[zIndex+i];
1214 { assert(derivOrder > 0);
1215 return getDerivativeMeasure().getValue(s, derivOrder-1);
1219 {
return derivOrder>0
1220 ? getDerivativeMeasure().getDependsOnStage(derivOrder-1)
1226 assert(zIndex.isValid());
1228 if (!icMeasure.isEmptyHandle()) {
1230 .
realize(s, icMeasure.getDependsOnStage());
1231 const T& ic = icMeasure.
getValue(s);
1232 for (
int i=0; i < this->size(); ++i)
1233 allZ[zIndex+i] = Measure_Num<T>::get(ic,i);
1235 for (
int i=0; i < this->size(); ++i)
1245 Vector init(this->size());
1246 for (
int i=0; i < this->size(); ++i)
1254 assert(zIndex.isValid());
1256 if (!derivMeasure.isEmptyHandle()) {
1257 const T& deriv = derivMeasure.
getValue(s);
1258 for (
int i=0; i < this->size(); ++i)
1259 allZDot[zIndex+i] = Measure_Num<T>::get(deriv,i);
1261 allZDot(zIndex,this->size()) = 0;
1289 class Measure_Differentiate_Result {
1291 Measure_Differentiate_Result() : derivIsGood(
false) {}
1302 typedef Measure_Differentiate_Result<T> Result;
1309 operand(operand), forceUseApprox(false), isApproxInUse(false) {}
1315 forceUseApprox = mustApproximate;
1316 this->invalidateTopologyCache();
1320 this->operand = operand;
1321 this->invalidateTopologyCache();
1336 {
if (!isApproxInUse)
return operand.getNumTimeDerivatives()-1;
1340 {
if (!isApproxInUse)
return operand.getDependsOnStage(order+1);
1341 else return operand.getDependsOnStage(order); }
1349 {
if (!isApproxInUse)
1350 return operand.getValue(s, derivOrder+1);
1352 ensureDerivativeIsRealized(s);
1356 return result.operandDot;
1360 if (!isApproxInUse)
return;
1362 assert(resultIx.isValid());
1365 (subsys.updDiscreteVariable(s,resultIx));
1367 result.operand = operand.getValue(s);
1368 result.operandDot = this->getValueZero();
1369 result.derivIsGood =
false;
1373 isApproxInUse = (forceUseApprox || operand.getNumTimeDerivatives()==0);
1385 ensureDerivativeIsRealized(s);
1389 assert(resultIx.isValid());
1391 if (subsys.isDiscreteVarUpdateValueRealized(s,resultIx))
1394 const Real t0 = subsys.getDiscreteVarLastUpdateTime(s,resultIx);
1396 (subsys.getDiscreteVariable(s,resultIx));
1397 const T& f0 = prevResult.operand;
1398 const T& fdot0 = prevResult.operandDot;
1399 const bool good0 = prevResult.derivIsGood;
1403 (subsys.updDiscreteVarUpdateValue(s,resultIx));
1404 T& f = result.operand;
1405 T& fdot = result.operandDot;
1406 bool& good = result.derivIsGood;
1411 fdot = this->getValueZero();
1416 fdot = (f-f0)/(t-t0);
1418 fdot =
Real(2)*fdot - fdot0;
1421 subsys.markDiscreteVarUpdateValueRealized(s,resultIx);
1426 bool forceUseApprox;
1429 mutable bool isApproxInUse;
1461 this->operand = operand;
1462 this->invalidateTopologyCache();
1469 this->operation = op;
1470 this->invalidateTopologyCache();
1483 assert(extremeIx.isValid());
1486 (subsys.updDiscreteVariable(s,extremeIx));
1495 const bool hasNewExtreme = ensureExtremeHasBeenUpdated(s);
1513 {
return operand.getNumTimeDerivatives(); }
1518 {
return operand.getDependsOnStage(order); }
1528 const bool hasNewExtreme = ensureExtremeHasBeenUpdated(s);
1529 if (derivOrder > 0) {
1533 return hasNewExtreme ? operand.getValue(s, derivOrder)
1534 : this->getValueZero();
1536 if (hasNewExtreme) {
1568 case MaxAbs: initVal = 0;
break;
1573 new Value<T>(initVal), operand.getDependsOnStage(0));
1577 new Value<bool>(
false), operand.getDependsOnStage(0));
1583 ensureExtremeHasBeenUpdated(s);
1592 assert(extremeIx.isValid() && isNewExtremeIx.isValid());
1597 if (subsys.isDiscreteVarUpdateValueRealized(s, isNewExtremeIx))
1599 (subsys.getDiscreteVarUpdateValue(s,isNewExtremeIx));
1607 (subsys.getDiscreteVariable(s,extremeIx));
1608 const T& currentVal = operand.
getValue(s);
1611 bool foundNewExt =
false;
1612 for (
int i=0; i < this->size() && !foundNewExt; ++i)
1613 foundNewExt = isNewExtreme(Measure_Num<T>::get(currentVal,i),
1614 Measure_Num<T>::get(prevExtreme,i));
1621 (subsys.updDiscreteVarUpdateValue(s,isNewExtremeIx)) = foundNewExt;
1622 subsys.markDiscreteVarUpdateValueRealized(s,isNewExtremeIx);
1636 (subsys.updDiscreteVarUpdateValue(s,extremeIx));
1638 for (
int i=0; i < this->size(); ++i)
1639 Measure_Num<T>::upd(newExtreme,i) =
1640 extremeOf(Measure_Num<T>::get(currentVal,i),
1641 Measure_Num<T>::get(prevExtreme,i));
1644 subsys.markDiscreteVarUpdateValueRealized(s,extremeIx);
1650 bool isNewExtreme(
const typename Measure_Num<T>::Element& newVal,
1651 const typename Measure_Num<T>::Element& oldExtreme)
const 1653 switch (operation) {
1660 "Measure::Extreme::Implementation::isNewExtreme(): " 1661 "unrecognized operation %d", (
int)operation);
1668 bool isExtremeDir(
const typename Measure_Num<T>::Element& value,
1669 const typename Measure_Num<T>::Element& deriv)
const 1671 const int sv =
sign(value), sd =
sign(deriv);
1672 if (sd == 0)
return false;
1673 switch (operation) {
1680 "Measure::Extreme::Implementation::isExtremeDir(): " 1681 "unrecognized operation %d", (
int)operation);
1685 typename Measure_Num<T>::Element
1686 extremeOf(
const typename Measure_Num<T>::Element& newVal,
1687 const typename Measure_Num<T>::Element& oldExtreme)
const 1689 return isNewExtreme(newVal,oldExtreme) ? newVal : oldExtreme;
1694 Operation operation;
1742 class Measure_Delay_Buffer {
1744 explicit Measure_Delay_Buffer() {initDataMembers();}
1745 void clear() {initDataMembers();}
1746 int size()
const {
return m_size;}
1747 int capacity()
const {
return m_times.size();}
1748 bool empty()
const {
return size()==0;}
1749 bool full()
const {
return size()==capacity();}
1751 double getEntryTime(
int i)
const 1752 { assert(i < size());
return m_times[getArrayIndex(i)];}
1753 const T& getEntryValue(
int i)
const 1754 { assert(i < size());
return m_values[getArrayIndex(i)];}
1757 InitialAllocation = 8,
1759 MaxShrinkProofSize = 16,
1765 void append(
double tEarliest,
double tNow,
const T& valueNow) {
1766 forgetEntriesMuchOlderThan(tEarliest);
1767 removeEntriesLaterOrEq(tNow);
1770 else if (capacity() >
std::max((
int)MaxShrinkProofSize,
1771 (
int)TooBigFactor * (size()+1)))
1773 const int nextFree = getArrayIndex(m_size++);
1774 m_times[nextFree] = tNow;
1775 m_values[nextFree] = valueNow;
1776 m_maxSize =
std::max(m_maxSize, size());
1780 void prepend(
double tNewOldest,
const T& value) {
1781 assert(empty() || tNewOldest < m_times[m_oldest]);
1782 if (full()) makeMoreRoom();
1783 m_oldest = empty() ? 0 : getArrayIndex(-1);
1784 m_times[m_oldest] = tNewOldest;
1785 m_values[m_oldest] = value;
1787 m_maxSize =
std::max(m_maxSize, size());
1796 void copyInAndUpdate(
const Measure_Delay_Buffer& oldBuf,
double tEarliest,
1797 double tNow,
const T& valueNow) {
1799 m_oldest = m_size = 0;
1802 int firstNeeded = oldBuf.countNumUnneededOldEntries(tEarliest);
1803 int lastNeeded = oldBuf.findLastEarlier(tNow);
1804 int numOldEntriesToKeep = lastNeeded-firstNeeded+1;
1805 int newSize = numOldEntriesToKeep+1;
1807 int newSizeRequest = -1;
1808 if (capacity() < newSize) {
1809 newSizeRequest =
std::max((
int)InitialAllocation,
1810 (
int)GrowthFactor * newSize);
1812 }
else if (capacity() >
std::max((
int)MaxShrinkProofSize,
1813 (
int)TooBigFactor * newSize)) {
1814 newSizeRequest =
std::max((
int)MaxShrinkProofSize,
1815 (
int)GrowthFactor * newSize);
1820 if (newSizeRequest != -1) {
1822 m_values.resize(newSizeRequest);
1823 if (m_values.capacity() > m_values.size())
1824 m_values.resize(m_values.capacity());
1825 m_times.resize(m_values.size(), dNaN);
1828 m_maxCapacity =
std::max(m_maxCapacity, capacity());
1832 for (
int i=firstNeeded; i<=lastNeeded; ++i, ++nxt) {
1833 m_times[nxt] = oldBuf.getEntryTime(i);
1834 m_values[nxt] = oldBuf.getEntryValue(i);
1837 m_times[nxt] = tNow;
1838 m_values[nxt] = valueNow;
1839 assert(nxt+1==newSize);
1841 m_maxSize =
std::max(m_maxSize, size());
1847 T calcValueAtTime(
double tDelay,
double tNow,
const T& valueNow)
const;
1852 void calcValueAtTimeLinearOnly(
double tDelay, T& delayedValue)
const {
1856 Measure_Num<T>::makeNaNLike(T(), delayedValue);
1860 int firstLater = findFirstLaterOrEq(tDelay);
1862 if (firstLater > 0) {
1864 int firstEarlier = firstLater-1;
1865 double t0=getEntryTime(firstEarlier), t1=getEntryTime(firstLater);
1866 const T& v0=getEntryValue(firstEarlier);
1867 const T& v1=getEntryValue(firstLater);
1868 Real fraction =
Real((tDelay-t0)/(t1-t0));
1869 delayedValue = T(v0 + fraction*(v1-v0));
1873 if (firstLater==0) {
1876 delayedValue = getEntryValue(firstLater);
1885 delayedValue = getEntryValue(0);
1890 double t0=getEntryTime(size()-2), t1=getEntryTime(size()-1);
1891 const T& v0=getEntryValue(size()-2);
1892 const T& v1=getEntryValue(size()-1);
1893 Real fraction =
Real((tDelay-t0)/(t1-t0));
1894 assert(fraction > 1.0);
1895 delayedValue = T(v0 + fraction*(v1-v0));
1899 int getNumGrows()
const {
return m_nGrows;}
1902 int getNumShrinks()
const {
return m_nShrinks;}
1904 int getMaxSize()
const {
return m_maxSize;}
1906 int getMaxCapacity()
const {
return m_maxCapacity;}
1911 int getArrayIndex(
int i)
const 1912 { assert(-1<=i && i<=size());
1913 const int rawIndex = m_oldest + i;
1914 if (rawIndex < 0)
return rawIndex + capacity();
1915 else return rawIndex % capacity(); }
1918 void forgetEntriesMuchOlderThan(
double tEarliest) {
1919 const int numToRemove = countNumUnneededOldEntries(tEarliest);
1921 m_oldest = getArrayIndex(numToRemove);
1922 m_size -= numToRemove;
1929 int countNumUnneededOldEntries(
double tEarliest)
const {
1930 const int firstLater = findFirstLaterOrEq(tEarliest);
1936 void removeEntriesLaterOrEq(
double t) {
1937 int lastEarlier = findLastEarlier(t);
1938 m_size = lastEarlier+1;
1939 if (m_size==0) m_oldest=0;
1944 int findFirstLaterOrEq(
double tDelay)
const {
1945 for (
int i=0; i < size(); ++i)
1946 if (getEntryTime(i) >= tDelay)
1953 int findLastEarlier(
double t)
const {
1954 for (
int i=size()-1; i>=0; --i)
1955 if (getEntryTime(i) < t)
1962 void makeMoreRoom() {
1963 const int newSizeRequest =
std::max((
int)InitialAllocation,
1964 (
int)GrowthFactor * size());
1965 resize(newSizeRequest);
1967 m_maxCapacity =
std::max(m_maxCapacity, capacity());
1972 void makeLessRoom() {
1973 const int targetMaxSize =
std::max((
int)MaxShrinkProofSize,
1974 (
int)GrowthFactor * size());
1975 if (capacity() > targetMaxSize) {
1976 resize(targetMaxSize);
1986 void resize(
int newSizeRequest) {
1987 assert(newSizeRequest >= size());
1995 for (
int i=0; i < size(); ++i) {
1996 const int ix = getArrayIndex(i);
1997 newTimes[i] = m_times[ix];
1998 newValues[i] = m_values[ix];
2000 m_times.
swap(newTimes);
2001 m_values.swap(newValues);
2006 void initDataMembers() {
2007 m_times.clear(); m_values.clear();
2009 m_nGrows=m_nShrinks=m_maxSize=m_maxCapacity=0;
2019 int m_nGrows, m_nShrinks, m_maxSize, m_maxCapacity;
2025 typedef Measure_Delay_Buffer<T> Buffer;
2031 m_canUseCurrentValue(false), m_useLinearInterpolationOnly(false) {}
2035 m_canUseCurrentValue(false), m_useLinearInterpolationOnly(false) {}
2042 this->m_source = source;
2043 this->invalidateTopologyCache();
2048 if (delay != this->m_delay) {
2049 this->m_delay = delay;
2050 this->invalidateTopologyCache();
2055 if (linearOnly != this->m_useLinearInterpolationOnly) {
2056 this->m_useLinearInterpolationOnly = linearOnly;
2057 this->invalidateTopologyCache();
2062 if (canUseCurrentValue != this->m_canUseCurrentValue) {
2063 this->m_canUseCurrentValue = canUseCurrentValue;
2064 this->invalidateTopologyCache();
2071 {
return this->m_useLinearInterpolationOnly; }
2073 {
return this->m_canUseCurrentValue; }
2090 {
return this->m_canUseCurrentValue ? m_source.getDependsOnStage(order)
2101 buffer.calcValueAtTimeLinearOnly(s.
getTime()-m_delay, value);
2105 assert(m_bufferIx.isValid());
2108 (subsys.updDiscreteVariable(s,m_bufferIx));
2111 buffer.append(s.
getTime()-m_delay, s.
getTime(), m_source.getValue(s));
2130 assert(m_bufferIx.isValid());
2134 (subsys.getDiscreteVariable(s,m_bufferIx));
2137 (subsys.updDiscreteVarUpdateValue(s,m_bufferIx));
2140 nextBuffer.copyInAndUpdate(prevBuffer, t-m_delay,
2143 subsys.markDiscreteVarUpdateValueRealized(s,m_bufferIx);
2149 bool m_canUseCurrentValue;
2150 bool m_useLinearInterpolationOnly;
2162 #endif // SimTK_SimTKCOMMON_MEASURE_IMPLEMENTATION_H_ Definition: MeasureImplementation.h:2024
void setIsPresumedValidAtDependsOnStage(bool presume)
Definition: MeasureImplementation.h:414
Definition: MeasureImplementation.h:606
Implementation()
Definition: MeasureImplementation.h:907
Stage getDependsOnStageVirtual(int order) const override
Definition: MeasureImplementation.h:925
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:1335
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:1512
Operation getOperation() const
Return the particular operation being performed by this Extreme measure.
Definition: MeasureImplementation.h:1478
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:1460
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:751
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:245
int size() const
Return the number of elements in the data type of this Measure; for Vector measures this is determine...
Definition: MeasureImplementation.h:455
const Measure_< T > & getOperandMeasure() const
Definition: MeasureImplementation.h:1326
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:743
size_type size() const
Return the current number of elements stored in this array.
Definition: Array.h:2075
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:1549
#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:532
This is the vector class intended to appear in user code for large, variable size column vectors...
Definition: BigMatrix.h:171
Implementation(Stage invalidated, const T &defaultValue)
Definition: MeasureImplementation.h:717
Definition: MeasureImplementation.h:669
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:991
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:2069
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:444
friend class Implementation
Definition: Measure.h:251
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:987
Definition: Measure.h:843
Stage getDependsOnStageVirtual(int derivOrder) const override
Definition: MeasureImplementation.h:748
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:2079
DiscreteVariableIndex allocateDiscreteVariable(State &s, Stage g, AbstractValue *v) const
Definition: Subsystem.h:96
Implementation(const Measure_< T > &operand)
Definition: MeasureImplementation.h:1307
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:422
void ensureDerivativeIsRealized(const State &s) const
Definition: MeasureImplementation.h:1388
Definition: Measure.h:840
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:996
void realizeMeasureAccelerationVirtual(const State &s) const override
Set the zdots to the integrand (derivative measure) value.
Definition: MeasureImplementation.h:1253
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:1524
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:464
bool isUsingApproximation() const
Definition: MeasureImplementation.h:1325
void resize(size_type n)
Change the size of this Array, preserving all the elements that will still fit, and default construct...
Definition: Array.h:2091
void setInvalidatedStage(Stage invalidated)
Definition: MeasureImplementation.h:825
Stage getDependsOnStageVirtual(int order) const override
Definition: MeasureImplementation.h:1044
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:288
void setOperandMeasure(const Measure_< T > &operand)
Definition: MeasureImplementation.h:1319
void setCanUseCurrentValue(bool canUseCurrentValue)
Definition: MeasureImplementation.h:2061
void setValue(State &s, const T &value) const
Set the value of the state variables(s) that hold the integral.
Definition: MeasureImplementation.h:1164
void setInitialConditionMeasure(const Measure_< T > &ic)
Definition: MeasureImplementation.h:1185
The Rotation class is a Mat33 that guarantees that the matrix can be interpreted as a legitimate 3x3 ...
Definition: Quaternion.h:40
Implementation(const Measure_< T > &source, Real delay)
Definition: MeasureImplementation.h:2033
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:1384
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:1049
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:1195
Stage getDependsOnStageVirtual(int order) const override
Definition: MeasureImplementation.h:2089
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:1202
Track the value of the operand that is of maximum absolute value.
Definition: Measure.h:903
void setValue(State &s, const T &value) const
Set the current extreme value stored in this Extreme measure's state variable.
Definition: MeasureImplementation.h:1482
virtual void realizeMeasureTopologyVirtual(State &) const
Concrete measures can override this to allocate Topology-stage resources.
Definition: MeasureImplementation.h:527
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:359
void updateBuffer(const State &s) const
Definition: MeasureImplementation.h:2129
Definition: MeasureImplementation.h:786
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:1112
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:859
Implementation()
The derivative and initialConditions Measures will be empty handles if this is default constructed...
Definition: MeasureImplementation.h:1146
void setDelay(Real delay)
Definition: MeasureImplementation.h:2047
void realizeMeasureTopologyVirtual(State &s) const override
Concrete measures can override this to allocate Topology-stage resources.
Definition: MeasureImplementation.h:1372
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:835
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:928
void realizeReport(const State &s) const
Definition: MeasureImplementation.h:96
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:696
Stage getDependsOnStageVirtual(int order) const override
The depends-on stage for this measure is the same as for its operand.
Definition: MeasureImplementation.h:1517
void setValue(State &state, const T &value) const
Change the value of this Measure in the given state.
Definition: MeasureImplementation.h:736
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:1591
int getRefCount() const
Definition: MeasureImplementation.h:79
Track the minimum value of the operand (signed).
Definition: Measure.h:883
Stage getDependsOnStageVirtual(int order) const override
Definition: MeasureImplementation.h:1108
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:1347
const Real NaN
This is the IEEE "not a number" constant for this implementation of the default-precision Real type; ...
Definition: MeasureImplementation.h:1019
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:920
bool getCanUseCurrentValue() const
Definition: MeasureImplementation.h:2072
int getCopyNumber() const
Definition: MeasureImplementation.h:81
void initializeVirtual(State &s) const override
Definition: MeasureImplementation.h:2104
Implementation(const T &litude, const T &frequency, const T &phase=T(0))
Definition: MeasureImplementation.h:911
Implementation()
Definition: MeasureImplementation.h:1305
Stage getDependsOnStage() const
Definition: MeasureImplementation.h:839
const Stage & getSystemStage() const
This returns the global stage for this State.
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:982
bool isFinite(const negator< float > &x)
Definition: negator.h:285
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:1191
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:881
const AbstractValue & getDiscreteVarUpdateValue(const State &s, DiscreteVariableIndex dx) const
Definition: Subsystem.h:259
Stage getInvalidatedStage() const
Definition: MeasureImplementation.h:731
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:1331
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:2095
const Measure_< T > & getOperandMeasure() const
Definition: MeasureImplementation.h:1091
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:1040
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:617
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:628
bool getIsPresumedValidAtDependsOnStage() const
Definition: MeasureImplementation.h:418
void setSourceMeasure(const Measure_< T > &source)
Definition: MeasureImplementation.h:2040
MeasureIndex getSubsystemMeasureIndex() const
Return the MeasureIndex by which this Measure is known to the Subsystem that owns it...
Definition: MeasureImplementation.h:233
Implementation(const Implementation &source)
Definition: MeasureImplementation.h:722
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:740
Stage getDependsOnStageVirtual(int derivOrder) const override
Definition: MeasureImplementation.h:630
void initializeVirtual(State &s) const override
Definition: MeasureImplementation.h:1359
This is a fixed-length column vector designed for no-overhead inline computation. ...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:618
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:683
Definition: MeasureImplementation.h:966
Definition: Measure.h:842
Definition: MeasureImplementation.h:1072
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:795
Implementation()
Definition: MeasureImplementation.h:713
Forces calculated.
Definition: Stage.h:76
Implementation()
Definition: MeasureImplementation.h:2029
bool hasImpl() const
Definition: Measure.h:242
Implementation()
Definition: MeasureImplementation.h:1023
The Array_<T> container class is a plug-compatible replacement for the C++ standard template library ...
Definition: Array.h:53
#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:2114
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:1468
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:623
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:1561
Implementation(const Measure_< T > &left, const Measure_< T > &right)
Definition: MeasureImplementation.h:1025
T & updValue(const State &state) const
Definition: MeasureImplementation.h:866
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:677
#define SimTK_ERRCHK3_ALWAYS(cond, whereChecked, fmt, a1, a2, a3)
Definition: ExceptionMacros.h:293
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:871
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:1035
Implementation(const Measure_< T > &left, const Measure_< T > &right)
Definition: MeasureImplementation.h:972
This creates a Measure whose value is a Topology-stage constant of any type T.
Definition: Measure.h:339
virtual void realizeMeasureReportVirtual(const State &) const
Definition: MeasureImplementation.h:144
void setForceUseApproximation(bool mustApproximate)
Definition: MeasureImplementation.h:1314
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:1582
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:489
bool getForceUseApproximation() const
Definition: MeasureImplementation.h:1324
Zero()
Definition: MeasureImplementation.h:644
int getNumCacheEntries() const
Return the number of cache entries allocated for the value and derivatives of this Measure...
Definition: MeasureImplementation.h:459
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:874
Implementation()
Definition: MeasureImplementation.h:970
int getNumTimeDerivatives() const
Every Measure can produce a value, and some can provide one or more total derivatives with respect to...
Definition: MeasureImplementation.h:237
~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:240
Stage getDependsOnStageVirtual(int derivOrder) const override
Definition: MeasureImplementation.h:1218
RowVectorBase< typename CNT< ELEM >::TAbs > abs(const RowVectorBase< ELEM > &v)
Definition: VectorMath.h:120
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:690
void swap(Array_ &other)
This is a specialized algorithm providing constant time exchange of data with another array that has ...
Definition: Array.h:1983
#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:1212
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:902
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:654
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:1244
void realizeModel(State &s) const
Definition: MeasureImplementation.h:89
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:1439
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:1150
const Measure_< T > & getSourceMeasure() const
Definition: MeasureImplementation.h:2068
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:2122
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:878
This unique integer type is for selecting discrete variables.
void realizeAcceleration(const State &s) const
Definition: MeasureImplementation.h:95
Definition: Measure.h:841
Stage getInvalidatedStage() const
Definition: MeasureImplementation.h:840
The implementation for Integrate measures allocates a continuous state variable or variables from the...
Definition: MeasureImplementation.h:1141
Implementation(const T &value)
Definition: MeasureImplementation.h:612
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:1099
Implementation(const Implementation &source)
Definition: MeasureImplementation.h:806
const T & getValueZero() const
Return a reference to a zero of the same type and size as this Measure's value.
Definition: MeasureImplementation.h:556
Implementation()
Default constructor leaves the operand measure unspecified; no base class cache entries are allocated...
Definition: MeasureImplementation.h:1446
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:862
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:1507
void setDerivativeMeasure(const Measure_< T > &d)
Definition: MeasureImplementation.h:1183
int getNumTimeDerivatives() const
Definition: MeasureImplementation.h:103
Track the maximum value of the operand (signed).
Definition: Measure.h:893
bool getUseLinearInterpolationOnly() const
Definition: MeasureImplementation.h:2070
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:433
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:2083
Stage getDependsOnStageVirtual(int order) const override
Definition: MeasureImplementation.h:1339
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:241
void realizeMeasureTopologyVirtual(State &s) const override
Concrete measures can override this to allocate Topology-stage resources.
Definition: MeasureImplementation.h:757
Provide a unique integer type for identifying Subsystems.
void setUseLinearInterpolationOnly(bool linearOnly)
Definition: MeasureImplementation.h:2054
const T & getValue(const State &s, int derivOrder=0) const
Retrieve the Value of this Measure or one of its time derivatives, assuming the supplied State has be...
Definition: Measure.h:274
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:1451
void markCacheValueNotRealized(const State &s, int derivOrder) const
Invalidate one of this Measure's cache entries.
Definition: MeasureImplementation.h:514
Operation
Definition: Measure.h:839
Track the value of the operand that is of minimum absolute value (not very useful).
Definition: Measure.h:914
This class represents a small matrix whose size is known at compile time, containing elements of any ...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:620
Implementation()
Definition: MeasureImplementation.h:1078
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:1086
void setDependsOnStage(Stage dependsOn)
Definition: MeasureImplementation.h:811
Implementation(Real factor, const Measure_< T > &operand)
Definition: MeasureImplementation.h:1080
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:1474
void setDefaultValue(const T &defaultValue)
Set a new default value for this Measure.
Definition: MeasureImplementation.h:403
const T & getValue(const State &s, int derivOrder) const
Definition: MeasureImplementation.h:361
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:412
AbstractMeasure & shallowAssign(const AbstractMeasure &)
Shallow assignment operator destructs the current Implementation object (meaning its reference count ...
Definition: MeasureImplementation.h:195
Definition: MeasureImplementation.h:706
Constant & setValue(const T &value)
Change the value returned by this Measure.
Definition: Measure.h:356
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:1493
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:501
const Measure_< T > & getDerivativeMeasure() const
Definition: MeasureImplementation.h:1170
size_type capacity() const
Return the number of elements this array can currently hold without requiring reallocation.
Definition: Array.h:2085
Stage getDependsOnStageVirtual(int derivOrder) const override
Definition: MeasureImplementation.h:692
const Subsystem & getSubsystem() const
Definition: MeasureImplementation.h:119
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:632
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:1104
Implementation(const Implementation &source)
Copy constructor shallow-copies the referenced measures, but we don't want to share our state variabl...
Definition: MeasureImplementation.h:1157
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:726
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:923
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:1225
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:545
Definition: MeasureImplementation.h:1299
Implementation()
Definition: MeasureImplementation.h:791
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:477
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:1176
This is the base handle class for all Measures whose value type is known, including all the Simbody b...
Definition: Measure.h:261
void markAsValid(const State &state) const
Definition: MeasureImplementation.h:843
Implementation()
Definition: MeasureImplementation.h:611
Concrete templatized class derived from AbstractValue, adding generic value type-specific functionali...
Definition: Value.h:39