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(); }
137 virtual void realizeTopology(
State&)
const = 0;
152 getDependsOnStageVirtual(
int order)
const = 0;
159 MeasureIndex myIndex;
163 mutable int refCount;
184 "An empty Measure handle can't be put in a Subsystem.");
201 if (impl != src.impl) {
217 impl = src.impl->
clone();
257 template <
class T>
class Measure_Num {
260 template <>
class Measure_Num<float> {
262 typedef float Element;
263 static int size(
const float&) {
return 1;}
264 static const float&
get(
const float& v,
int i) {assert(i==0);
return v;}
265 static float& upd(
float& v,
int i) {assert(i==0);
return v;}
266 static void makeNaNLike(
const float&,
float& nanValue)
268 static void makeZeroLike(
const float&,
float& zeroValue) {zeroValue=0.f;}
271 template <>
class Measure_Num<double> {
273 typedef double Element;
274 static int size(
const double&) {
return 1;}
275 static const double&
get(
const double& v,
int i) {assert(i==0);
return v;}
276 static double& upd(
double& v,
int i) {assert(i==0);
return v;}
277 static void makeNaNLike(
const double&,
double& nanValue)
279 static void makeZeroLike(
const double&,
double& zeroValue) {zeroValue=0.;}
283 template <
int M,
class E>
284 class Measure_Num< Vec<M,
E,1> > {
285 typedef Vec<M,E,1> T;
288 static int size(
const T&) {
return M;}
289 static const E&
get(
const T& v,
int i) {
return v[i];}
290 static E& upd(T& v,
int i) {
return v[i];}
291 static void makeNaNLike (
const T&, T& nanValue) {nanValue.setToNaN();}
292 static void makeZeroLike(
const T&, T& zeroValue) {zeroValue.setToZero();}
296 template <
int M,
int N,
class E>
297 class Measure_Num< Mat<M,N,
E> > {
298 typedef Mat<M,N,E> T;
301 static int size(
const T&) {
return N;}
302 static const typename T::TCol&
get(
const T& m,
int j) {
return m.col(j);}
303 static typename T::TCol& upd(T& m,
int j) {
return m.col(j);}
304 static void makeNaNLike (
const T&, T& nanValue) {nanValue.setToNaN();}
305 static void makeZeroLike(
const T&, T& zeroValue) {zeroValue.setToZero();}
310 class Measure_Num< Vector_<
E> > {
311 typedef Vector_<E> T;
314 static int size(
const T& v) {
return v.size();}
315 static const E&
get(
const T& v,
int i) {
return v[i];}
316 static E& upd(T& v,
int i) {
return v[i];}
317 static void makeNaNLike(
const T& v, T& nanValue)
318 { nanValue.resize(v.size()); nanValue.setToNaN(); }
319 static void makeZeroLike(
const T& v, T& zeroValue)
320 { zeroValue.resize(v.size()); zeroValue.setToZero(); }
340 "Measure_<T>::getValue()",
341 "derivOrder %d was out of range; this Measure allows 0-%d.",
353 "Measure_<T>::getValue()",
354 "Expected State to have been realized to at least stage "
355 "%s but stage was %s.",
361 if (derivOrder < getNumCacheEntries()) {
362 if (!isCacheValueRealized(s,derivOrder)) {
363 T& value = updCacheEntry(s,derivOrder);
364 calcCachedValueVirtual(s, derivOrder, value);
365 markCacheValueRealized(s,derivOrder);
368 return getCacheEntry(s,derivOrder);
373 return getUncachedValueVirtual(s,derivOrder);
379 this->defaultValue = defaultValue;
380 Measure_Num<T>::makeZeroLike(defaultValue, zeroValue);
381 this->invalidateTopologyCache();
390 { presumeValidAtDependsOnStage = presume;
391 this->invalidateTopologyCache(); }
394 {
return presumeValidAtDependsOnStage; }
398 : presumeValidAtDependsOnStage(false),
399 defaultValue(defaultValue),
400 derivIx(numCacheEntries)
402 Measure_Num<T>::makeZeroLike(defaultValue, zeroValue);
409 : presumeValidAtDependsOnStage(false),
411 derivIx(numCacheEntries)
413 Measure_Num<T>::makeZeroLike(defaultValue, zeroValue);
420 : presumeValidAtDependsOnStage(source.presumeValidAtDependsOnStage),
421 defaultValue(source.defaultValue),
422 derivIx(source.derivIx.size())
424 Measure_Num<T>::makeZeroLike(defaultValue, zeroValue);
430 int size()
const {
return Measure_Num<T>::size(defaultValue);}
440 SimTK_ERRCHK2(0 <= derivOrder && derivOrder < getNumCacheEntries(),
441 "Measure_<T>::Implementation::getCacheEntry()",
442 "Derivative order %d is out of range; only %d cache entries"
443 " were allocated.", derivOrder, getNumCacheEntries());
446 this->
getSubsystem().getCacheEntry(s, derivIx[derivOrder]));
453 SimTK_ERRCHK2(0 <= derivOrder && derivOrder < getNumCacheEntries(),
454 "Measure_<T>::Implementation::updCacheEntry()",
455 "Derivative order %d is out of range; only %d cache entries"
456 " were allocated.", derivOrder, getNumCacheEntries());
459 this->
getSubsystem().updCacheEntry(s, derivIx[derivOrder]));
465 SimTK_ERRCHK2(0 <= derivOrder && derivOrder < getNumCacheEntries(),
466 "Measure_<T>::Implementation::isCacheValueRealized()",
467 "Derivative order %d is out of range; only %d cache entries"
468 " were allocated.", derivOrder, getNumCacheEntries());
477 SimTK_ERRCHK2(0 <= derivOrder && derivOrder < getNumCacheEntries(),
478 "Measure_<T>::Implementation::markCacheValueRealized()",
479 "Derivative order %d is out of range; only %d cache entries"
480 " were allocated.", derivOrder, getNumCacheEntries());
490 SimTK_ERRCHK2(0 <= derivOrder && derivOrder < getNumCacheEntries(),
491 "Measure_<T>::Implementation::markCacheValueNotRealized()",
492 "Derivative order %d is out of range; only %d cache entries"
493 " were allocated.", derivOrder, getNumCacheEntries());
512 "Measure_<T>::Implementation::calcCachedValueVirtual()",
513 "This method should have been overridden by the derived"
514 " Measure but was not. It is needed to calculate the"
515 " cached value for derivOrder=%d.", derivOrder); }
525 "Measure_<T>::Implementation::getUncachedValueVirtual()",
526 "This method should have been overridden by the derived"
527 " Measure but was not. It is needed to return the uncached"
528 " value at derivOrder=%d.", derivOrder);
529 return *
reinterpret_cast<T*
>(0);
540 void realizeTopology(
State& s)
const FINAL_11 {
544 if (getNumCacheEntries()) {
545 derivIx[0] = presumeValidAtDependsOnStage
551 if (getNumCacheEntries() > 1) {
552 T nanValue; Measure_Num<T>::makeNaNLike(defaultValue, nanValue);
553 for (
int i=1; i < getNumCacheEntries(); ++i) {
554 derivIx[i] = presumeValidAtDependsOnStage
564 realizeMeasureTopologyVirtual(s);
570 bool presumeValidAtDependsOnStage;
575 mutable Array_<CacheEntryIndex> derivIx;
603 {
return derivOrder>0 ? this->getValueZero() : this->
getDefaultValue(); }
621 template <
class T>
inline
623 template <
class T>
inline
631 template <
class T>
inline
633 template <
class T>
inline
663 {
return derivOrder==0 ? s.getTime()
693 invalidatedStage(
Stage::Empty) {}
697 invalidatedStage(invalidated) {}
702 invalidatedStage(source.invalidatedStage) {}
705 invalidatedStage = invalidates;
706 this->invalidateTopologyCache();
715 { updVarValue(state) = value; }
731 {
return derivOrder>0 ? this->getValueZero() : getVarValue(s); }
740 const T& getVarValue(
const State& s)
const {
741 assert(discreteVarIndex.isValid());
743 this->
getSubsystem().getDiscreteVariable(s, discreteVarIndex));
745 T& updVarValue(
State& s)
const {
746 assert(discreteVarIndex.isValid());
748 this->
getSubsystem().updDiscreteVariable(s, discreteVarIndex));
752 Stage invalidatedStage;
775 dependsOnStage(dependsOn==
Stage::Empty ?
Stage::Topology : dependsOn),
776 invalidatedStage(invalidated)
778 "Got invalidated stage %s and dependsOn stage %s which is illegal "
779 "because the invalidated stage must be later than dependsOn.",
786 dependsOnStage(source.dependsOnStage),
787 invalidatedStage(source.invalidatedStage) {}
792 "Measure::Result::setDependsOnStage()",
793 "The provided dependsOn stage %s is illegal because it is not "
794 "less than the current invalidated stage %s. Change the "
795 "invalidated stage first with setInvalidatedStage().",
797 getInvalidatedStage().getName().c_str());
799 dependsOnStage = dependsOn;
800 this->invalidateTopologyCache();
805 "Measure::Result::setInvalidatedStage()",
806 "The provided invalidated stage %s is illegal because it is not "
807 "greater than the current dependsOn stage %s. Change the "
808 "dependsOn stage first with setDependsOnStage().",
812 invalidatedStage = invalidated;
813 this->invalidateTopologyCache();
824 "Measure::Result::markAsValid()",
825 "This Result Measure cannot be marked valid in a State where this "
826 "measure's Subsystem has been realized only to stage %s, because "
827 "its value was declared to depend on stage %s. To mark it valid, "
828 "we require that the State have been realized at least to the "
829 "previous stage (%s in this case); that is, you must at least be "
830 "*working on* the dependsOn stage in order to claim this result is "
832 subsystemStage.
getName().c_str(),
835 this->markCacheValueRealized(state, 0); }
838 {
return this->isCacheValueRealized(state, 0); }
841 { this->markCacheValueNotRealized(state, 0);
845 { markAsNotValid(state);
return this->updCacheEntry(state, 0); }
862 "Measure_<T>::Result::getValue()",
863 "Measure_<T>::Result::getValue() was called when the value was not "
864 "yet valid. For most Measure types, this would have initiated "
865 "computation of the value, but Result measures must have their values "
866 "calculated and set externally, and then marked valid."); }
870 Stage dependsOnStage;
871 Stage invalidatedStage;
883 static const int NumDerivs = 3;
887 a(
CNT<T>::getNaN()), w(
CNT<T>::getNaN()), p(
CNT<T>::getNaN()) {}
893 a(amplitude), w(frequency), p(phase) {}
911 using std::sin;
using std::cos;
913 assert(NumDerivs == 3);
915 const T arg = w*t + p;
917 switch (derivOrder) {
918 case 0: value = a*sin(arg);
break;
919 case 1: value = w*a*cos(arg);
break;
920 case 2: value = -w*w*a*sin(arg);
break;
921 case 3: value = -w*w*w*a*cos(arg);
break;
923 "Measure::Sinusoid::Implementation::calcCachedValueVirtual():"
924 " derivOrder %d is out of range 0-3.", derivOrder);
952 : left(left), right(right) {}
971 right.getDependsOnStage(order))); }
977 value = left.getValue(s,derivOrder) + right.getValue(s,derivOrder);
1005 : left(left), right(right) {}
1024 right.getDependsOnStage(order))); }
1030 value = left.getValue(s,derivOrder) - right.getValue(s,derivOrder);
1059 : factor(factor), operand(operand) {}
1066 this->invalidateTopologyCache();
1087 {
return operand.getDependsOnStage(order); }
1093 value = factor * operand.getValue(s,derivOrder);
1129 const T& defaultValue)
1131 derivMeasure(deriv), icMeasure(ic) {}
1137 derivMeasure(source.derivMeasure), icMeasure(source.icMeasure) {}
1143 { assert(zIndex >= 0);
1144 for (
int i=0; i < this->size(); ++i)
1146 Measure_Num<T>::get(value, i); }
1150 "Measure_<T>::Integrate::getDerivativeMeasure()",
1151 "No derivative measure is available for this integrated measure.");
1152 return derivMeasure; }
1156 "Measure_<T>::Integrate::getInitialConditionMeasure()",
1157 "No initial condition measure is available for this "
1158 "integrated measure.");
1162 { derivMeasure = d; this->invalidateTopologyCache(); }
1164 { icMeasure = ic; this->invalidateTopologyCache(); }
1174 {
int integralDerivs = getDerivativeMeasure().getNumTimeDerivatives();
1178 return integralDerivs; }
1182 { assert(derivOrder == 0);
1183 assert(Measure_Num<T>::size(value) == this->size());
1184 assert(zIndex.isValid());
1186 for (
int i=0; i < this->size(); ++i)
1187 Measure_Num<T>::upd(value,i) = allZ[zIndex+i];
1192 { assert(derivOrder > 0);
1193 return getDerivativeMeasure().getValue(s, derivOrder-1);
1197 {
return derivOrder>0
1198 ? getDerivativeMeasure().getDependsOnStage(derivOrder-1)
1204 assert(zIndex.isValid());
1206 if (!icMeasure.isEmptyHandle()) {
1208 .
realize(s, icMeasure.getDependsOnStage());
1209 const T& ic = icMeasure.getValue(s);
1210 for (
int i=0; i < this->size(); ++i)
1211 allZ[zIndex+i] = Measure_Num<T>::get(ic,i);
1213 for (
int i=0; i < this->size(); ++i)
1223 Vector init(this->size());
1224 for (
int i=0; i < this->size(); ++i)
1232 assert(zIndex.isValid());
1234 if (!derivMeasure.isEmptyHandle()) {
1235 const T& deriv = derivMeasure.getValue(s);
1236 for (
int i=0; i < this->size(); ++i)
1237 allZDot[zIndex+i] = Measure_Num<T>::get(deriv,i);
1239 allZDot(zIndex,this->size()) = 0;
1267 class Measure_Differentiate_Result {
1269 Measure_Differentiate_Result() : derivIsGood(false) {}
1280 typedef Measure_Differentiate_Result<T> Result;
1287 operand(operand), forceUseApprox(false), isApproxInUse(false) {}
1293 forceUseApprox = mustApproximate;
1294 this->invalidateTopologyCache();
1298 this->operand = operand;
1299 this->invalidateTopologyCache();
1314 {
if (!isApproxInUse)
return operand.getNumTimeDerivatives()-1;
1318 {
if (!isApproxInUse)
return operand.getDependsOnStage(order+1);
1319 else return operand.getDependsOnStage(order); }
1327 {
if (!isApproxInUse)
1328 return operand.getValue(s, derivOrder+1);
1330 ensureDerivativeIsRealized(s);
1334 return result.operandDot;
1338 if (!isApproxInUse)
return;
1340 assert(resultIx.isValid());
1343 (subsys.updDiscreteVariable(s,resultIx));
1345 result.operand = operand.getValue(s);
1346 result.operandDot = this->getValueZero();
1347 result.derivIsGood =
false;
1351 isApproxInUse = (forceUseApprox || operand.getNumTimeDerivatives()==0);
1363 ensureDerivativeIsRealized(s);
1367 assert(resultIx.isValid());
1369 if (subsys.isDiscreteVarUpdateValueRealized(s,resultIx))
1372 const Real t0 = subsys.getDiscreteVarLastUpdateTime(s,resultIx);
1374 (subsys.getDiscreteVariable(s,resultIx));
1375 const T& f0 = prevResult.operand;
1376 const T& fdot0 = prevResult.operandDot;
1377 const bool good0 = prevResult.derivIsGood;
1381 (subsys.updDiscreteVarUpdateValue(s,resultIx));
1382 T& f = result.operand;
1383 T& fdot = result.operandDot;
1384 bool& good = result.derivIsGood;
1386 f = operand.getValue(s);
1389 fdot = this->getValueZero();
1394 fdot = (f-f0)/(t-t0);
1396 fdot = Real(2)*fdot - fdot0;
1399 subsys.markDiscreteVarUpdateValueRealized(s,resultIx);
1404 bool forceUseApprox;
1407 mutable bool isApproxInUse;
1439 this->operand = operand;
1440 this->invalidateTopologyCache();
1447 this->operation = op;
1448 this->invalidateTopologyCache();
1461 assert(extremeIx.isValid());
1464 (subsys.updDiscreteVariable(s,extremeIx));
1473 const bool hasNewExtreme = ensureExtremeHasBeenUpdated(s);
1491 {
return operand.getNumTimeDerivatives(); }
1496 {
return operand.getDependsOnStage(order); }
1506 const bool hasNewExtreme = ensureExtremeHasBeenUpdated(s);
1507 if (derivOrder > 0) {
1511 return hasNewExtreme ? operand.getValue(s, derivOrder)
1512 : this->getValueZero();
1514 if (hasNewExtreme) {
1529 setValue(s, operand.getValue(s));
1546 case MaxAbs: initVal = 0;
break;
1551 new Value<T>(initVal), operand.getDependsOnStage(0));
1555 new Value<bool>(
false), operand.getDependsOnStage(0));
1561 ensureExtremeHasBeenUpdated(s);
1570 assert(extremeIx.isValid() && isNewExtremeIx.isValid());
1575 if (subsys.isDiscreteVarUpdateValueRealized(s, isNewExtremeIx))
1577 (subsys.getDiscreteVarUpdateValue(s,isNewExtremeIx));
1585 (subsys.getDiscreteVariable(s,extremeIx));
1586 const T& currentVal = operand.getValue(s);
1589 bool foundNewExt =
false;
1590 for (
int i=0; i < this->size() && !foundNewExt; ++i)
1591 foundNewExt = isNewExtreme(Measure_Num<T>::get(currentVal,i),
1592 Measure_Num<T>::get(prevExtreme,i));
1599 (subsys.updDiscreteVarUpdateValue(s,isNewExtremeIx)) = foundNewExt;
1600 subsys.markDiscreteVarUpdateValueRealized(s,isNewExtremeIx);
1614 (subsys.updDiscreteVarUpdateValue(s,extremeIx));
1616 for (
int i=0; i < this->size(); ++i)
1617 Measure_Num<T>::upd(newExtreme,i) =
1618 extremeOf(Measure_Num<T>::get(currentVal,i),
1619 Measure_Num<T>::get(prevExtreme,i));
1622 subsys.markDiscreteVarUpdateValueRealized(s,extremeIx);
1628 bool isNewExtreme(
const typename Measure_Num<T>::Element& newVal,
1629 const typename Measure_Num<T>::Element& oldExtreme)
const
1631 switch (operation) {
1638 "Measure::Extreme::Implementation::isNewExtreme(): "
1639 "unrecognized operation %d", (
int)operation);
1646 bool isExtremeDir(
const typename Measure_Num<T>::Element& value,
1647 const typename Measure_Num<T>::Element& deriv)
const
1649 const int sv =
sign(value), sd =
sign(deriv);
1650 if (sd == 0)
return false;
1651 switch (operation) {
1658 "Measure::Extreme::Implementation::isExtremeDir(): "
1659 "unrecognized operation %d", (
int)operation);
1663 typename Measure_Num<T>::Element
1664 extremeOf(
const typename Measure_Num<T>::Element& newVal,
1665 const typename Measure_Num<T>::Element& oldExtreme)
const
1667 return isNewExtreme(newVal,oldExtreme) ? newVal : oldExtreme;
1671 Measure_<T> operand;
1675 mutable DiscreteVariableIndex extremeIx;
1681 mutable DiscreteVariableIndex isNewExtremeIx;
1720 class Measure_Delay_Buffer {
1722 explicit Measure_Delay_Buffer() {initDataMembers();}
1723 void clear() {initDataMembers();}
1724 int size()
const {
return m_size;}
1725 int capacity()
const {
return m_times.size();}
1726 bool empty()
const {
return size()==0;}
1727 bool full()
const {
return size()==capacity();}
1729 double getEntryTime(
int i)
const
1730 { assert(i < size());
return m_times[getArrayIndex(i)];}
1731 const T& getEntryValue(
int i)
const
1732 { assert(i < size());
return m_values[getArrayIndex(i)];}
1735 InitialAllocation = 8,
1737 MaxShrinkProofSize = 16,
1743 void append(
double tEarliest,
double tNow,
const T& valueNow) {
1744 forgetEntriesMuchOlderThan(tEarliest);
1745 removeEntriesLaterOrEq(tNow);
1748 else if (capacity() >
std::max((
int)MaxShrinkProofSize,
1749 (
int)TooBigFactor * (size()+1)))
1751 const int nextFree = getArrayIndex(m_size++);
1752 m_times[nextFree] = tNow;
1753 m_values[nextFree] = valueNow;
1754 m_maxSize =
std::max(m_maxSize, size());
1758 void prepend(
double tNewOldest,
const T& value) {
1759 assert(empty() || tNewOldest < m_times[m_oldest]);
1760 if (full()) makeMoreRoom();
1761 m_oldest = empty() ? 0 : getArrayIndex(-1);
1762 m_times[m_oldest] = tNewOldest;
1763 m_values[m_oldest] = value;
1765 m_maxSize =
std::max(m_maxSize, size());
1774 void copyInAndUpdate(
const Measure_Delay_Buffer& oldBuf,
double tEarliest,
1775 double tNow,
const T& valueNow) {
1777 m_oldest = m_size = 0;
1780 int firstNeeded = oldBuf.countNumUnneededOldEntries(tEarliest);
1781 int lastNeeded = oldBuf.findLastEarlier(tNow);
1782 int numOldEntriesToKeep = lastNeeded-firstNeeded+1;
1783 int newSize = numOldEntriesToKeep+1;
1785 int newSizeRequest = -1;
1786 if (capacity() < newSize) {
1787 newSizeRequest =
std::max((
int)InitialAllocation,
1788 (
int)GrowthFactor * newSize);
1790 }
else if (capacity() >
std::max((
int)MaxShrinkProofSize,
1791 (
int)TooBigFactor * newSize)) {
1792 newSizeRequest =
std::max((
int)MaxShrinkProofSize,
1793 (
int)GrowthFactor * newSize);
1798 if (newSizeRequest != -1) {
1799 const double dNaN = NTraits<double>::getNaN();
1800 m_values.resize(newSizeRequest);
1801 if (m_values.capacity() > m_values.size())
1802 m_values.resize(m_values.capacity());
1803 m_times.resize(m_values.size(), dNaN);
1806 m_maxCapacity =
std::max(m_maxCapacity, capacity());
1810 for (
int i=firstNeeded; i<=lastNeeded; ++i, ++nxt) {
1811 m_times[nxt] = oldBuf.getEntryTime(i);
1812 m_values[nxt] = oldBuf.getEntryValue(i);
1815 m_times[nxt] = tNow;
1816 m_values[nxt] = valueNow;
1817 assert(nxt+1==newSize);
1819 m_maxSize =
std::max(m_maxSize, size());
1825 T calcValueAtTime(
double tDelay,
double tNow,
const T& valueNow)
const;
1830 void calcValueAtTimeLinearOnly(
double tDelay, T& delayedValue)
const {
1834 Measure_Num<T>::makeNaNLike(T(), delayedValue);
1838 int firstLater = findFirstLaterOrEq(tDelay);
1840 if (firstLater > 0) {
1842 int firstEarlier = firstLater-1;
1843 double t0=getEntryTime(firstEarlier), t1=getEntryTime(firstLater);
1844 const T& v0=getEntryValue(firstEarlier);
1845 const T& v1=getEntryValue(firstLater);
1846 Real fraction = Real((tDelay-t0)/(t1-t0));
1847 delayedValue = T(v0 + fraction*(v1-v0));
1851 if (firstLater==0) {
1854 delayedValue = getEntryValue(firstLater);
1863 delayedValue = getEntryValue(0);
1868 double t0=getEntryTime(size()-2), t1=getEntryTime(size()-1);
1869 const T& v0=getEntryValue(size()-2);
1870 const T& v1=getEntryValue(size()-1);
1871 Real fraction = Real((tDelay-t0)/(t1-t0));
1872 assert(fraction > 1.0);
1873 delayedValue = T(v0 + fraction*(v1-v0));
1877 int getNumGrows()
const {
return m_nGrows;}
1880 int getNumShrinks()
const {
return m_nShrinks;}
1882 int getMaxSize()
const {
return m_maxSize;}
1884 int getMaxCapacity()
const {
return m_maxCapacity;}
1889 int getArrayIndex(
int i)
const
1890 { assert(-1<=i && i<=size());
1891 const int rawIndex = m_oldest + i;
1892 if (rawIndex < 0)
return rawIndex + capacity();
1893 else return rawIndex % capacity(); }
1896 void forgetEntriesMuchOlderThan(
double tEarliest) {
1897 const int numToRemove = countNumUnneededOldEntries(tEarliest);
1899 m_oldest = getArrayIndex(numToRemove);
1900 m_size -= numToRemove;
1907 int countNumUnneededOldEntries(
double tEarliest)
const {
1908 const int firstLater = findFirstLaterOrEq(tEarliest);
1914 void removeEntriesLaterOrEq(
double t) {
1915 int lastEarlier = findLastEarlier(t);
1916 m_size = lastEarlier+1;
1917 if (m_size==0) m_oldest=0;
1922 int findFirstLaterOrEq(
double tDelay)
const {
1923 for (
int i=0; i < size(); ++i)
1924 if (getEntryTime(i) >= tDelay)
1931 int findLastEarlier(
double t)
const {
1932 for (
int i=size()-1; i>=0; --i)
1933 if (getEntryTime(i) < t)
1940 void makeMoreRoom() {
1941 const int newSizeRequest =
std::max((
int)InitialAllocation,
1942 (
int)GrowthFactor * size());
1943 resize(newSizeRequest);
1945 m_maxCapacity =
std::max(m_maxCapacity, capacity());
1950 void makeLessRoom() {
1951 const int targetMaxSize =
std::max((
int)MaxShrinkProofSize,
1952 (
int)GrowthFactor * size());
1953 if (capacity() > targetMaxSize) {
1954 resize(targetMaxSize);
1964 void resize(
int newSizeRequest) {
1965 assert(newSizeRequest >= size());
1966 const double dNaN = NTraits<double>::getNaN();
1967 Array_<T,int> newValues(newSizeRequest);
1968 if (newValues.capacity() > newValues.size())
1969 newValues.resize(newValues.capacity());
1970 Array_<double,int> newTimes(newValues.size(), dNaN);
1973 for (
int i=0; i < size(); ++i) {
1974 const int ix = getArrayIndex(i);
1975 newTimes[i] = m_times[ix];
1976 newValues[i] = m_values[ix];
1978 m_times.swap(newTimes);
1979 m_values.swap(newValues);
1984 void initDataMembers() {
1985 m_times.clear(); m_values.clear();
1987 m_nGrows=m_nShrinks=m_maxSize=m_maxCapacity=0;
1991 Array_<double,int> m_times;
1992 Array_<T,int> m_values;
1997 int m_nGrows, m_nShrinks, m_maxSize, m_maxCapacity;
2003 typedef Measure_Delay_Buffer<T> Buffer;
2009 m_canUseCurrentValue(false), m_useLinearInterpolationOnly(false) {}
2013 m_canUseCurrentValue(false), m_useLinearInterpolationOnly(false) {}
2020 this->m_source = source;
2021 this->invalidateTopologyCache();
2026 if (delay != this->m_delay) {
2027 this->m_delay = delay;
2028 this->invalidateTopologyCache();
2033 if (linearOnly != this->m_useLinearInterpolationOnly) {
2034 this->m_useLinearInterpolationOnly = linearOnly;
2035 this->invalidateTopologyCache();
2040 if (canUseCurrentValue != this->m_canUseCurrentValue) {
2041 this->m_canUseCurrentValue = canUseCurrentValue;
2042 this->invalidateTopologyCache();
2049 {
return this->m_useLinearInterpolationOnly; }
2051 {
return this->m_canUseCurrentValue; }
2068 {
return this->m_canUseCurrentValue ? m_source.getDependsOnStage(order)
2079 buffer.calcValueAtTimeLinearOnly(s.
getTime()-m_delay, value);
2083 assert(m_bufferIx.isValid());
2086 (subsys.updDiscreteVariable(s,m_bufferIx));
2089 buffer.append(s.
getTime()-m_delay, s.
getTime(), m_source.getValue(s));
2108 assert(m_bufferIx.isValid());
2112 (subsys.getDiscreteVariable(s,m_bufferIx));
2115 (subsys.updDiscreteVarUpdateValue(s,m_bufferIx));
2118 nextBuffer.copyInAndUpdate(prevBuffer, t-m_delay,
2119 t, m_source.getValue(s));
2121 subsys.markDiscreteVarUpdateValueRealized(s,m_bufferIx);
2127 bool m_canUseCurrentValue;
2128 bool m_useLinearInterpolationOnly;
2140 #endif // SimTK_SimTKCOMMON_MEASURE_IMPLEMENTATION_H_
virtual void realizeMeasureModelVirtual(State &) const
Definition: MeasureImplementation.h:139
Definition: MeasureImplementation.h:2002
virtual void realizeMeasureReportVirtual(const State &) const
Definition: MeasureImplementation.h:146
void setIsPresumedValidAtDependsOnStage(bool presume)
Definition: MeasureImplementation.h:389
Definition: MeasureImplementation.h:584
Implementation()
Definition: MeasureImplementation.h:885
Stage getDependsOnStageVirtual(int order) const override
Definition: MeasureImplementation.h:903
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:1313
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:1490
const Measure_< T > & getOperandMeasure() const
Definition: MeasureImplementation.h:1069
The abstract parent of all Subsystems.
Definition: Subsystem.h:61
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:1438
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:729
#define SimTK_SimTKCOMMON_EXPORT
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:202
#define SimTK_ERRCHK2_ALWAYS(cond, whereChecked, fmt, a1, a2)
Definition: ExceptionMacros.h:289
bool isUsingApproximation() const
Definition: MeasureImplementation.h:1303
virtual void realizeMeasureTimeVirtual(const State &) const
Definition: MeasureImplementation.h:141
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:721
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:1527
CacheEntryIndex allocateCacheEntry(const State &, Stage dependsOn, Stage computedBy, AbstractValue *v) const
#define SimTK_ASSERT1_ALWAYS(cond, msg, a1)
Definition: ExceptionMacros.h:351
Implementation(Stage invalidated, const T &defaultValue)
Definition: MeasureImplementation.h:695
Definition: MeasureImplementation.h:647
Stage getDependsOnStageVirtual(int order) const override
Definition: MeasureImplementation.h:969
Modeling choices made.
Definition: Stage.h:55
#define SimTK_ERRCHK1_ALWAYS(cond, whereChecked, fmt, a1)
Definition: ExceptionMacros.h:285
Implementation(const Implementation &src)
Base class copy constructor removes the Subsystem and sets the reference count to zero...
Definition: MeasureImplementation.h:57
Implementation(const Implementation &source)
Copy constructor copies the number of cache entries from the source, but not the cache indices themse...
Definition: MeasureImplementation.h:419
friend class Implementation
Definition: Measure.h:251
bool getIsPresumedValidAtDependsOnStage() const
Definition: MeasureImplementation.h:393
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:965
Definition: Measure.h:843
Stage getDependsOnStageVirtual(int derivOrder) const override
Definition: MeasureImplementation.h:726
bool isInSubsystem() const
Definition: MeasureImplementation.h:118
MeasureIndex adoptMeasure(AbstractMeasure &)
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:2057
Implementation(const Measure_< T > &operand)
Definition: MeasureImplementation.h:1285
int decrRefCount() const
Definition: MeasureImplementation.h:76
Implementation * clone() const
This is a deep copy of the concrete Implementation object, except the Subsystem will have been remove...
Definition: MeasureImplementation.h:86
const System & getSystem() const
void setValue(State &s, const T &value) const
Set the value of the state variables(s) that hold the integral.
Definition: MeasureImplementation.h:1142
Implementation(const T &defaultValue, int numCacheEntries=1)
Definition: MeasureImplementation.h:397
Definition: Measure.h:840
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:123
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:974
void realizeMeasureAccelerationVirtual(const State &s) const override
Set the zdots to the integrand (derivative measure) value.
Definition: MeasureImplementation.h:1231
const Vector & getZ(const State &) const
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:1502
const Stage & getSystemStage() const
This returns the *global* stage for this State.
int size() const
Return the number of elements in the data type of this Measure; for Vector measures this is determine...
Definition: MeasureImplementation.h:430
void setInvalidatedStage(Stage invalidated)
Definition: MeasureImplementation.h:803
const Subsystem & getSubsystem() const
Definition: MeasureImplementation.h:119
Stage getDependsOnStageVirtual(int order) const override
Definition: MeasureImplementation.h:1022
virtual ~Implementation()
Definition: MeasureImplementation.h:134
Real getDiscreteVarLastUpdateTime(const State &s, DiscreteVariableIndex dx) const
Definition: Subsystem.h:166
void realize(const State &state, Stage stage=Stage::HighestRuntime) const
Realize the given state to the indicated stage.
Lower than any legitimate Stage.
Definition: Stage.h:53
virtual void realizeMeasureVelocityVirtual(const State &) const
Definition: MeasureImplementation.h:143
void setOperandMeasure(const Measure_< T > &operand)
Definition: MeasureImplementation.h:1297
void setCanUseCurrentValue(bool canUseCurrentValue)
Definition: MeasureImplementation.h:2039
virtual void realizeMeasurePositionVirtual(const State &) const
Definition: MeasureImplementation.h:142
void setInitialConditionMeasure(const Measure_< T > &ic)
Definition: MeasureImplementation.h:1163
DiscreteVariableIndex allocateAutoUpdateDiscreteVariable(State &, Stage invalidates, AbstractValue *v, Stage updateDependsOn) const
void realizeDynamics(const State &s) const
Definition: MeasureImplementation.h:94
Implementation(const Measure_< T > &source, Real delay)
Definition: MeasureImplementation.h:2011
This class is basically a glorified enumerated type, type-safe and range checked but permitting conve...
Definition: Stage.h:50
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:1362
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:1027
int getNumTimeDerivativesVirtual() const override
This measure has one more time derivative than the integrand.
Definition: MeasureImplementation.h:1173
void realizeInstance(const State &s) const
Definition: MeasureImplementation.h:90
Stage getDependsOnStageVirtual(int order) const override
Definition: MeasureImplementation.h:2067
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:1180
Track the value of the operand that is of maximum absolute value.
Definition: Measure.h:903
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:523
The abstract parent of all Measure Implementation classes.
Definition: MeasureImplementation.h:48
MeasureIndex getSubsystemMeasureIndex() const
Return the MeasureIndex by which this Measure is known to the Subsystem that owns it...
Definition: MeasureImplementation.h:238
void ensureDerivativeIsRealized(const State &s) const
Definition: MeasureImplementation.h:1366
This is the base Implementation class for all Measures whose value type is known. ...
Definition: MeasureImplementation.h:336
Measure_ & setDefaultValue(const T &defaultValue)
Change the default value associated with this Measure.
Definition: Measure.h:283
Definition: MeasureImplementation.h:764
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:1090
String getName() const
Return a printable name corresponding to the stage level currently stored in this Stage...
Definition: Stage.h:128
void setValue(State &state, const T &value) const
Change the value of this Measure in the given state.
Definition: MeasureImplementation.h:714
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:464
const T & getDefaultValue() const
Obtain a reference to the default value associated with this Measure.
Definition: Measure.h:288
Implementation()
The derivative and initialConditions Measures will be empty handles if this is default constructed...
Definition: MeasureImplementation.h:1124
void setDelay(Real delay)
Definition: MeasureImplementation.h:2025
friend class GutsRep
Definition: SubsystemGuts.h:343
int incrRefCount() const
Definition: MeasureImplementation.h:73
void realizeMeasureTopologyVirtual(State &s) const override
Concrete measures can override this to allocate Topology-stage resources.
Definition: MeasureImplementation.h:1350
This is the base class for all Measure handle classes.
Definition: Measure.h:151
Implementation()
This default constructor is for use by concrete measure implementation classes.
Definition: MeasureImplementation.h:52
virtual void realizeMeasureTopologyVirtual(State &) const
Concrete measures can override this to allocate Topology-stage resources.
Definition: MeasureImplementation.h:504
bool isInSubsystem() const
Test whether this Measure object has been adopted by a Subsystem.
Definition: MeasureImplementation.h:230
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:906
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:674
Stage getDependsOnStageVirtual(int order) const override
The depends-on stage for this measure is the same as for its operand.
Definition: MeasureImplementation.h:1495
Track the minimum value of the operand (signed).
Definition: Measure.h:883
Stage getDependsOnStageVirtual(int order) const override
Definition: MeasureImplementation.h:1086
This is the handle class for the hidden State implementation.
Definition: State.h:264
bool getCanUseCurrentValue() const
Definition: MeasureImplementation.h:2050
This file declares the base class AbstractMeasure for all derived Measure handle classes, and the handle classes for built-in Measures.
void markCacheValueNotRealized(const State &, CacheEntryIndex) const
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:1325
int getRefCount() const
Definition: MeasureImplementation.h:79
const Real NaN
This is the IEEE "not a number" constant for this implementation of the default-precision Real type; ...
Definition: MeasureImplementation.h:997
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:898
const Measure_< T > & getDerivativeMeasure() const
Definition: MeasureImplementation.h:1148
Real getTimeOfExtremeValue(const State &s) const
Return the time at which the extreme was last updated.
Definition: MeasureImplementation.h:1471
void markAsNotValid(const State &state) const
Definition: MeasureImplementation.h:840
void initializeVirtual(State &s) const override
Definition: MeasureImplementation.h:2082
Implementation(const T &litude, const T &frequency, const T &phase=T(0))
Definition: MeasureImplementation.h:889
Implementation()
Definition: MeasureImplementation.h:1283
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:960
bool isFinite(const negator< float > &x)
Definition: negator.h:287
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:1169
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:859
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:1309
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:2073
Stage getDependsOnStage(int derivOrder) const
Definition: MeasureImplementation.h:105
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:1018
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:595
Vector & updZDot(const State &) const
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:606
void setSourceMeasure(const Measure_< T > &source)
Definition: MeasureImplementation.h:2018
Implementation(const Implementation &source)
Definition: MeasureImplementation.h:700
const Measure_< T > & getOperandMeasure() const
Return a reference to the operand measure for this Extreme measure.
Definition: MeasureImplementation.h:1452
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:718
Stage getDependsOnStageVirtual(int derivOrder) const override
Definition: MeasureImplementation.h:608
void initializeVirtual(State &s) const override
Definition: MeasureImplementation.h:1337
Report-only quantities evaluated.
Definition: Stage.h:62
int getCopyNumber() const
Definition: MeasureImplementation.h:81
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:661
bool isSameMeasure(const AbstractMeasure &other) const
There can be multiple handles on the same Measure.
Definition: Measure.h:220
const Measure_< T > & getOperandMeasure() const
Definition: MeasureImplementation.h:1304
Definition: MeasureImplementation.h:944
void markAsValid(const State &state) const
Definition: MeasureImplementation.h:821
Definition: Measure.h:842
const AbstractValue & getDiscreteVarUpdateValue(const State &s, DiscreteVariableIndex dx) const
Definition: Subsystem.h:170
Definition: MeasureImplementation.h:1050
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:439
System topology realized.
Definition: Stage.h:54
SubsystemIndex getSubsystemIndex() const
Definition: MeasureImplementation.h:122
bool hasImpl() const
Definition: Measure.h:242
bool getForceUseApproximation() const
Definition: MeasureImplementation.h:1302
Implementation(Stage dependsOn, Stage invalidated)
Definition: MeasureImplementation.h:773
Implementation()
Definition: MeasureImplementation.h:691
Forces calculated.
Definition: Stage.h:60
Implementation()
Definition: MeasureImplementation.h:2007
Implementation()
Definition: MeasureImplementation.h:1001
#define SimTK_ERRCHK_ALWAYS(cond, whereChecked, msg)
Definition: ExceptionMacros.h:281
void realizeAcceleration(const State &s) const
Definition: MeasureImplementation.h:95
void realizeMeasureTopologyVirtual(State &s) const override
Concrete measures can override this to allocate Topology-stage resources.
Definition: MeasureImplementation.h:2092
void setOperation(Operation op)
Set the particular operation to be performed by this Extreme measure; this is a Topology stage change...
Definition: MeasureImplementation.h:1446
bool isCacheValueRealized(const State &, CacheEntryIndex) const
int getNumTimeDerivatives() const
Every Measure can produce a value, and some can provide one or more total derivatives with respect to...
Definition: MeasureImplementation.h:242
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:601
Unique integer type for Subsystem-local z indexing.
CacheEntryIndex allocateLazyCacheEntry(const State &state, Stage earliest, AbstractValue *v) const
Definition: Subsystem.h:95
void realizeMeasureTopologyVirtual(State &s) const override
Allocate the auto-updated state variable that holds the extreme seen so far.
Definition: MeasureImplementation.h:1539
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
Implementation(const Measure_< T > &left, const Measure_< T > &right)
Definition: MeasureImplementation.h:1003
Implementation & operator=(const Implementation &src)
Base class copy assignment operator removes the Subsystem, and sets the reference count to zero...
Definition: MeasureImplementation.h:63
Stage getInvalidatedStage() const
Definition: MeasureImplementation.h:818
Operation getOperation() const
Return the particular operation being performed by this Extreme measure.
Definition: MeasureImplementation.h:1456
Implementation()
Definition: MeasureImplementation.h:655
int getRefCount() const
Definition: MeasureImplementation.h:250
#define SimTK_ERRCHK3_ALWAYS(cond, whereChecked, fmt, a1, a2, a3)
Definition: ExceptionMacros.h:293
virtual void realizeMeasureDynamicsVirtual(const State &) const
Definition: MeasureImplementation.h:144
const Subsystem & getSubsystem() const
Return a reference to the Subsystem that owns this Measure.
Definition: MeasureImplementation.h:234
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:849
ELEM max(const VectorBase< ELEM > &v)
Definition: VectorMath.h:251
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:1013
void invalidateTopologyCache() const
Definition: MeasureImplementation.h:125
Implementation(const Measure_< T > &left, const Measure_< T > &right)
Definition: MeasureImplementation.h:950
This creates a Measure whose value is a Topology-stage constant of any type T.
Definition: Measure.h:339
void updateBuffer(const State &s) const
Definition: MeasureImplementation.h:2107
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:510
void realizeReport(const State &s) const
Definition: MeasureImplementation.h:96
void setForceUseApproximation(bool mustApproximate)
Definition: MeasureImplementation.h:1292
Subsystem & updSubsystem()
Definition: MeasureImplementation.h:120
The abstract parent of all Subsystem "Guts" implementation classes.
Definition: SubsystemGuts.h:42
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:1560
Zero()
Definition: MeasureImplementation.h:622
virtual void realizeMeasureAccelerationVirtual(const State &) const
Definition: MeasureImplementation.h:145
ZIndex allocateZ(State &, const Vector &zInit) const
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:852
MeasureIndex getSubsystemMeasureIndex() const
Definition: MeasureImplementation.h:121
Implementation()
Definition: MeasureImplementation.h:948
~AbstractMeasure()
Destructor decrements the Implementation's reference count and deletes the object if the count goes t...
Definition: MeasureImplementation.h:226
T & updValue(const State &state) const
Definition: MeasureImplementation.h:844
Stage getDependsOnStageVirtual(int derivOrder) const override
Definition: MeasureImplementation.h:1196
RowVectorBase< typename CNT< ELEM >::TAbs > abs(const RowVectorBase< ELEM > &v)
Definition: VectorMath.h:120
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:668
#define SimTK_ERRCHK(cond, whereChecked, msg)
Definition: ExceptionMacros.h:324
Stage getStage(const State &s) const
Definition: MeasureImplementation.h:128
const T & getValue(const State &s, int derivOrder) const
Definition: MeasureImplementation.h:338
const Measure_< T > & getSourceMeasure() const
Definition: MeasureImplementation.h:2046
const Measure_< T > & getInitialConditionMeasure() const
Definition: MeasureImplementation.h:1154
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:1190
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:880
int getNumCacheEntries() const
Return the number of cache entries allocated for the value and derivatives of this Measure...
Definition: MeasureImplementation.h:434
One()
Definition: MeasureImplementation.h:632
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:1222
DiscreteVariableIndex allocateDiscreteVariable(State &, Stage invalidates, AbstractValue *v) const
Definition: MeasureImplementation.h:1417
const Real E
e = Real(exp(1))
bool isValid(const State &state) const
Definition: MeasureImplementation.h:837
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:387
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:1128
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:2100
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:856
This unique integer type is for selecting discrete variables.
Definition: Measure.h:841
The implementation for Integrate measures allocates a continuous state variable or variables from the...
Definition: MeasureImplementation.h:1119
Implementation(const T &value)
Definition: MeasureImplementation.h:590
void realizeVelocity(const State &s) const
Definition: MeasureImplementation.h:93
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:452
unsigned int sign(unsigned char u)
Definition: Scalar.h:311
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:1077
Implementation(const Implementation &source)
Definition: MeasureImplementation.h:784
Implementation()
Default constructor leaves the operand measure unspecified; no base class cache entries are allocated...
Definition: MeasureImplementation.h:1424
Implementation * cloneVirtual() const override
Definition: MeasureImplementation.h:1485
void setDerivativeMeasure(const Measure_< T > &d)
Definition: MeasureImplementation.h:1161
Track the maximum value of the operand (signed).
Definition: Measure.h:893
AbstractMeasure & deepAssign(const AbstractMeasure &source)
Deep assignment clones the Implementation object pointed to by the source handle, so that this handle...
Definition: MeasureImplementation.h:213
Implementation(int numCacheEntries=1)
Argument numCacheEntries should be one greater than the number of derivatives; that is...
Definition: MeasureImplementation.h:408
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:2061
Stage getDependsOnStageVirtual(int order) const override
Definition: MeasureImplementation.h:1317
void realizeMeasureTopologyVirtual(State &s) const override
Concrete measures can override this to allocate Topology-stage resources.
Definition: MeasureImplementation.h:735
Provide a unique integer type for identifying Subsystems.
const Implementation & getImpl() const
Definition: Measure.h:240
void setUseLinearInterpolationOnly(bool linearOnly)
Definition: MeasureImplementation.h:2032
int getNumTimeDerivatives() const
Definition: MeasureImplementation.h:103
const AbstractValue & getDiscreteVariable(const State &s, DiscreteVariableIndex dx) const
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:1429
Operation
Definition: Measure.h:839
Track the value of the operand that is of minimum absolute value (not very useful).
Definition: Measure.h:914
Implementation()
Definition: MeasureImplementation.h:1056
An object of this type is used as a dummy argument to make sure the automatically-generated handle co...
Definition: Measure.h:156
const Real & getTime() const
You can call these as long as *system* stage >= Model.
void setScaleFactor(Real sf)
Definition: MeasureImplementation.h:1064
void setDependsOnStage(Stage dependsOn)
Definition: MeasureImplementation.h:789
Implementation(Real factor, const Measure_< T > &operand)
Definition: MeasureImplementation.h:1058
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:176
void setDefaultValue(const T &defaultValue)
Set a new default value for this Measure.
Definition: MeasureImplementation.h:378
AbstractMeasure & shallowAssign(const AbstractMeasure &)
Shallow assignment operator destructs the current Implementation object (meaning its reference count ...
Definition: MeasureImplementation.h:200
Definition: MeasureImplementation.h:684
Vector & updZ(State &) const
Stage getInvalidatedStage() const
Definition: MeasureImplementation.h:709
void realizeTime(const State &s) const
Definition: MeasureImplementation.h:91
A new time has been realized.
Definition: Stage.h:57
const T & getValueZero() const
Return a reference to a zero of the same type and size as this Measure's value.
Definition: MeasureImplementation.h:534
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
Stage getDependsOnStageVirtual(int derivOrder) const override
Definition: MeasureImplementation.h:670
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:610
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:1082
Implementation(const Implementation &source)
Copy constructor shallow-copies the referenced measures, but we don't want to share our state variabl...
Definition: MeasureImplementation.h:1135
virtual void realizeMeasureInstanceVirtual(const State &) const
Definition: MeasureImplementation.h:140
bool getUseLinearInterpolationOnly() const
Definition: MeasureImplementation.h:2048
void markCacheValueRealized(const State &, CacheEntryIndex) const
Includes internal headers providing declarations for the basic SimTK Core classes.
void setValue(State &s, const T &value) const
Set the current extreme value stored in this Extreme measure's state variable.
Definition: MeasureImplementation.h:1460
Stage getStage(const State &) const
void setInvalidatedStage(Stage invalidates)
Definition: MeasureImplementation.h:704
int getNumTimeDerivativesVirtual() const override
Definition: MeasureImplementation.h:901
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:1569
void invalidateAllCacheAtOrAbove(Stage) const
If any subsystem or the system stage is currently at or higher than the passed-in one...
Real getDelay() const
Definition: MeasureImplementation.h:2047
Stage getDependsOnStage() const
Definition: MeasureImplementation.h:817
virtual void initializeVirtual(State &) const
Definition: MeasureImplementation.h:148
void realizePosition(const State &s) const
Definition: MeasureImplementation.h:92
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:1203
void realizeModel(State &s) const
Definition: MeasureImplementation.h:89
Definition: MeasureImplementation.h:1277
Implementation()
Definition: MeasureImplementation.h:769
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:246
void markCacheValueNotRealized(const State &s, int derivOrder) const
Invalidate one of this Measure's cache entries.
Definition: MeasureImplementation.h:489
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:476
This is the base handle class for all Measures whose value type is known, including all the Simbody b...
Definition: Measure.h:261
virtual int getNumTimeDerivativesVirtual() const
Definition: MeasureImplementation.h:150
Implementation()
Definition: MeasureImplementation.h:589
Templatized version of the abstract class, providing generic type-specific functionality that does no...
Definition: Value.h:67