pydrake.systems.analysis¶
Bindings for the analysis portion of the Systems framework.
-
pydrake.systems.analysis.
GetIntegrationSchemes
() → List[str]¶ Returns the allowed string values for the
scheme
parameter in ResetIntegratorFromFlags().
-
pydrake.systems.analysis.
IntegratorBase
¶
-
template
pydrake.systems.analysis.
IntegratorBase_
¶ Instantiations:
IntegratorBase_[float]
,IntegratorBase_[AutoDiffXd]
,IntegratorBase_[Expression]
-
class
IntegratorBase_[float]
¶ An abstract class for an integrator for ODEs and DAEs as represented by a Drake System. Integrators solve initial value problems of the form:
ẋ(t) = f(t, x(t)) with f : ℝ × ℝⁿ → ℝⁿ
(i.e.,
f()
is an ordinary differential equation) given initial conditions (t₀, x₀). Thus, integrators advance the continuous state of a dynamical system forward in time.Drake’s subclasses of IntegratorBase<T> should follow the naming pattern
FooIntegrator<T>
by convention.-
__init__
¶ Initialize self. See help(type(self)) for accurate signature.
-
get_dense_output
(self: pydrake.systems.analysis.IntegratorBase_[float]) → pydrake.trajectories.PiecewisePolynomial¶ Returns a const pointer to the integrator’s current PiecewisePolynomial instance, holding a representation of the continuous state trajectory since the last StartDenseIntegration() call. This is suitable to query the integrator’s current dense output, if any (may be nullptr).
-
get_fixed_step_mode
(self: pydrake.systems.analysis.IntegratorBase_[float]) → bool¶ Gets whether an integrator is running in fixed step mode. If the integrator does not support error estimation, this function will always return
True
.See also
set_fixed_step_mode()
-
get_maximum_step_size
(self: pydrake.systems.analysis.IntegratorBase_[float]) → float¶ Gets the maximum step size that may be taken by this integrator. This is a soft maximum: the integrator may stretch it by as much as 1% to hit a discrete event.
See also
set_requested_minimum_step_size()
-
get_requested_minimum_step_size
(self: pydrake.systems.analysis.IntegratorBase_[float]) → float¶ Gets the requested minimum step size
h_min
for this integrator.See also
set_requested_minimum_step_size()
See also
get_working_minimum_step_size(T)
-
get_target_accuracy
(self: pydrake.systems.analysis.IntegratorBase_[float]) → float¶ Gets the target accuracy.
See also
get_accuracy_in_use()
-
get_throw_on_minimum_step_size_violation
(self: pydrake.systems.analysis.IntegratorBase_[float]) → bool¶ Reports the current setting of the throw_on_minimum_step_size_violation flag.
See also
set_throw_on_minimum_step_size_violation().
-
set_fixed_step_mode
(self: pydrake.systems.analysis.IntegratorBase_[float], arg0: bool) → None¶ Sets an integrator with error control to fixed step mode. If the integrator runs in fixed step mode, it will always take the maximum step size directed (which may be that determined by get_maximum_step_size(), or may be smaller, as directed by, e.g., Simulator for event handling purposes).
Warning
The error estimation process will still be active (so get_error_estimate() will still return a correct result), meaning that the additional (typically, but not necessarily small) computation required for error estimation will still be performed.
Raises: - RuntimeError if integrator does not support error estimation and
flag
is set toFalse
.
-
set_maximum_step_size
(self: pydrake.systems.analysis.IntegratorBase_[float], arg0: float) → None¶ @anchor integrator-maxstep @name Methods related to maximum integration step size
Sets the nominal maximum step size- the actual maximum step size taken may be slightly larger (see set_maximum_step_size() and get_stretch_factor())- that an integrator will take. Each integrator has a default maximum step size, which might be infinite. Sets the maximum step size that may be taken by this integrator. This setting should be used if you know the maximum time scale of your problem. The integrator may stretch the maximum step size by as much as 1% to reach a discrete event. For fixed step integrators, all steps will be taken at the maximum step size unless an event would be missed.
Warning
See integrator-initial-step-size “Initial step size selection”
-
set_requested_minimum_step_size
(self: pydrake.systems.analysis.IntegratorBase_[float], arg0: float) → None¶ Sets the requested minimum step size
h_min
that may be taken by this integrator. No step smaller than this will be taken except under circumstances as described integrator-minstep “above”. This setting will be ignored if it is smaller than the absolute minimumh_floor
also described above. Default value is zero.- Parameter
min_step_size
: - a non-negative value. Setting this value to zero will cause the integrator to use a reasonable value instead (see get_working_minimum_step_size()).
See also
get_requested_minimum_step_size()
See also
get_working_minimum_step_size()
- Parameter
-
set_target_accuracy
(self: pydrake.systems.analysis.IntegratorBase_[float], arg0: float) → None¶ @anchor integrator-accuracy @name Methods for getting and setting integrator accuracy The precise meaning of accuracy is a complicated discussion, but it translates roughly to the number of significant digits you want in the results. By convention it is supplied as
10^-digits
, meaning that an accuracy of 1e-3 provides about three significant digits. For more discussion of accuracy, see accuracy_and_tolerance and ref. [1].Integrators vary in the range of accuracy (loosest to tightest) that they can support, and each integrator will choose a default accuracy to be used that lies somewhere within this range and attempts to balance computation and accuracy. If you request accuracy outside the supported range for the chosen integrator it will be quietly adjusted to be in range. You can find out the accuracy setting actually being used using
get_accuracy_in_use()
.Implicit integrators additionally use the accuracy setting for determining when the underlying Newton-Raphson root finding process has converged. For those integrators, the accuracy setting also limits the allowable iteration error in the Newton-Raphson process. Looser accuracy in that process certainly implies greater error in the ODE solution and might impact the stability of the solution negatively as well.
- [1] M. Sherman, A. Seth, S. Delp. Procedia IUTAM 2:241-261 (2011),
Section 3.3. https://dx.doi.org/10.1016/j.piutam.2011.04.023
Request that the integrator attempt to achieve a particular accuracy for the continuous portions of the simulation. Otherwise a default accuracy is chosen for you. This may be ignored for fixed-step integration since accuracy control requires variable step sizes. You should call supports_error_estimation() to ensure that the integrator supports this capability before calling this function; if the integrator does not support it, this method will throw an exception.
Raises: RuntimeError if integrator does not support error estimation.
-
set_throw_on_minimum_step_size_violation
(self: pydrake.systems.analysis.IntegratorBase_[float], arg0: bool) → None¶ Sets whether the integrator should throw a RuntimeError exception when the integrator’s step size selection algorithm determines that it must take a step smaller than the minimum step size (for, e.g., purposes of error control). Default is
True
. IfFalse
, the integrator will advance time and state using the minimum specified step size in such situations. See integrator-minstep “this section” for more detail.
-
StartDenseIntegration
(self: pydrake.systems.analysis.IntegratorBase_[float]) → None¶ Starts dense integration, allocating a new dense output for this integrator to use.
- Precondition:
- The integrator has been initialized.
- Precondition:
- The system being integrated has continuous state.
- Precondition:
- No dense integration is in progress (no dense output is held by the integrator)
Raises: RuntimeError if any of the preconditions is not met. Warning
Dense integration may incur significant overhead.
-
StopDenseIntegration
(self: pydrake.systems.analysis.IntegratorBase_[float]) → pydrake.trajectories.PiecewisePolynomial¶ Stops dense integration, yielding ownership of the current dense output to the caller.
- Remark:
- This process is irreversible.
Returns: A PiecewisePolynomial instance, i.e. a representation of the continuous state trajectory of the system being integrated that can be evaluated at any time within its extension. This representation is defined starting at the context time of the last StartDenseIntegration() call and finishing at the current context time. - Precondition:
- Dense integration is in progress (a dense output is held by this integrator, after a call to StartDenseIntegration()).
- Postcondition:
- Previously held dense output is not updated nor referenced by the integrator anymore.
Raises: RuntimeError if any of the preconditions is not met.
-
-
class
-
class
pydrake.systems.analysis.
IntegratorBase_[AutoDiffXd]
¶ An abstract class for an integrator for ODEs and DAEs as represented by a Drake System. Integrators solve initial value problems of the form:
ẋ(t) = f(t, x(t)) with f : ℝ × ℝⁿ → ℝⁿ
(i.e.,
f()
is an ordinary differential equation) given initial conditions (t₀, x₀). Thus, integrators advance the continuous state of a dynamical system forward in time.Drake’s subclasses of IntegratorBase<T> should follow the naming pattern
FooIntegrator<T>
by convention.-
__init__
¶ Initialize self. See help(type(self)) for accurate signature.
-
get_dense_output
(self: pydrake.systems.analysis.IntegratorBase_[AutoDiffXd]) → drake::trajectories::PiecewisePolynomial<Eigen::AutoDiffScalar<Eigen::Matrix<double, -1, 1, 0, -1, 1> > >¶ Returns a const pointer to the integrator’s current PiecewisePolynomial instance, holding a representation of the continuous state trajectory since the last StartDenseIntegration() call. This is suitable to query the integrator’s current dense output, if any (may be nullptr).
-
get_fixed_step_mode
(self: pydrake.systems.analysis.IntegratorBase_[AutoDiffXd]) → bool¶ Gets whether an integrator is running in fixed step mode. If the integrator does not support error estimation, this function will always return
True
.See also
set_fixed_step_mode()
-
get_maximum_step_size
(self: pydrake.systems.analysis.IntegratorBase_[AutoDiffXd]) → pydrake.autodiffutils.AutoDiffXd¶ Gets the maximum step size that may be taken by this integrator. This is a soft maximum: the integrator may stretch it by as much as 1% to hit a discrete event.
See also
set_requested_minimum_step_size()
-
get_requested_minimum_step_size
(self: pydrake.systems.analysis.IntegratorBase_[AutoDiffXd]) → pydrake.autodiffutils.AutoDiffXd¶ Gets the requested minimum step size
h_min
for this integrator.See also
set_requested_minimum_step_size()
See also
get_working_minimum_step_size(T)
-
get_target_accuracy
(self: pydrake.systems.analysis.IntegratorBase_[AutoDiffXd]) → float¶ Gets the target accuracy.
See also
get_accuracy_in_use()
-
get_throw_on_minimum_step_size_violation
(self: pydrake.systems.analysis.IntegratorBase_[AutoDiffXd]) → bool¶ Reports the current setting of the throw_on_minimum_step_size_violation flag.
See also
set_throw_on_minimum_step_size_violation().
-
set_fixed_step_mode
(self: pydrake.systems.analysis.IntegratorBase_[AutoDiffXd], arg0: bool) → None¶ Sets an integrator with error control to fixed step mode. If the integrator runs in fixed step mode, it will always take the maximum step size directed (which may be that determined by get_maximum_step_size(), or may be smaller, as directed by, e.g., Simulator for event handling purposes).
Warning
The error estimation process will still be active (so get_error_estimate() will still return a correct result), meaning that the additional (typically, but not necessarily small) computation required for error estimation will still be performed.
Raises: - RuntimeError if integrator does not support error estimation and
flag
is set toFalse
.
-
set_maximum_step_size
(self: pydrake.systems.analysis.IntegratorBase_[AutoDiffXd], arg0: pydrake.autodiffutils.AutoDiffXd) → None¶ @anchor integrator-maxstep @name Methods related to maximum integration step size
Sets the nominal maximum step size- the actual maximum step size taken may be slightly larger (see set_maximum_step_size() and get_stretch_factor())- that an integrator will take. Each integrator has a default maximum step size, which might be infinite. Sets the maximum step size that may be taken by this integrator. This setting should be used if you know the maximum time scale of your problem. The integrator may stretch the maximum step size by as much as 1% to reach a discrete event. For fixed step integrators, all steps will be taken at the maximum step size unless an event would be missed.
Warning
See integrator-initial-step-size “Initial step size selection”
-
set_requested_minimum_step_size
(self: pydrake.systems.analysis.IntegratorBase_[AutoDiffXd], arg0: pydrake.autodiffutils.AutoDiffXd) → None¶ Sets the requested minimum step size
h_min
that may be taken by this integrator. No step smaller than this will be taken except under circumstances as described integrator-minstep “above”. This setting will be ignored if it is smaller than the absolute minimumh_floor
also described above. Default value is zero.- Parameter
min_step_size
: - a non-negative value. Setting this value to zero will cause the integrator to use a reasonable value instead (see get_working_minimum_step_size()).
See also
get_requested_minimum_step_size()
See also
get_working_minimum_step_size()
- Parameter
-
set_target_accuracy
(self: pydrake.systems.analysis.IntegratorBase_[AutoDiffXd], arg0: float) → None¶ @anchor integrator-accuracy @name Methods for getting and setting integrator accuracy The precise meaning of accuracy is a complicated discussion, but it translates roughly to the number of significant digits you want in the results. By convention it is supplied as
10^-digits
, meaning that an accuracy of 1e-3 provides about three significant digits. For more discussion of accuracy, see accuracy_and_tolerance and ref. [1].Integrators vary in the range of accuracy (loosest to tightest) that they can support, and each integrator will choose a default accuracy to be used that lies somewhere within this range and attempts to balance computation and accuracy. If you request accuracy outside the supported range for the chosen integrator it will be quietly adjusted to be in range. You can find out the accuracy setting actually being used using
get_accuracy_in_use()
.Implicit integrators additionally use the accuracy setting for determining when the underlying Newton-Raphson root finding process has converged. For those integrators, the accuracy setting also limits the allowable iteration error in the Newton-Raphson process. Looser accuracy in that process certainly implies greater error in the ODE solution and might impact the stability of the solution negatively as well.
- [1] M. Sherman, A. Seth, S. Delp. Procedia IUTAM 2:241-261 (2011),
Section 3.3. https://dx.doi.org/10.1016/j.piutam.2011.04.023
Request that the integrator attempt to achieve a particular accuracy for the continuous portions of the simulation. Otherwise a default accuracy is chosen for you. This may be ignored for fixed-step integration since accuracy control requires variable step sizes. You should call supports_error_estimation() to ensure that the integrator supports this capability before calling this function; if the integrator does not support it, this method will throw an exception.
Raises: RuntimeError if integrator does not support error estimation.
-
set_throw_on_minimum_step_size_violation
(self: pydrake.systems.analysis.IntegratorBase_[AutoDiffXd], arg0: bool) → None¶ Sets whether the integrator should throw a RuntimeError exception when the integrator’s step size selection algorithm determines that it must take a step smaller than the minimum step size (for, e.g., purposes of error control). Default is
True
. IfFalse
, the integrator will advance time and state using the minimum specified step size in such situations. See integrator-minstep “this section” for more detail.
-
StartDenseIntegration
(self: pydrake.systems.analysis.IntegratorBase_[AutoDiffXd]) → None¶ Starts dense integration, allocating a new dense output for this integrator to use.
- Precondition:
- The integrator has been initialized.
- Precondition:
- The system being integrated has continuous state.
- Precondition:
- No dense integration is in progress (no dense output is held by the integrator)
Raises: RuntimeError if any of the preconditions is not met. Warning
Dense integration may incur significant overhead.
-
StopDenseIntegration
(self: pydrake.systems.analysis.IntegratorBase_[AutoDiffXd]) → drake::trajectories::PiecewisePolynomial<Eigen::AutoDiffScalar<Eigen::Matrix<double, -1, 1, 0, -1, 1> > >¶ Stops dense integration, yielding ownership of the current dense output to the caller.
- Remark:
- This process is irreversible.
Returns: A PiecewisePolynomial instance, i.e. a representation of the continuous state trajectory of the system being integrated that can be evaluated at any time within its extension. This representation is defined starting at the context time of the last StartDenseIntegration() call and finishing at the current context time. - Precondition:
- Dense integration is in progress (a dense output is held by this integrator, after a call to StartDenseIntegration()).
- Postcondition:
- Previously held dense output is not updated nor referenced by the integrator anymore.
Raises: RuntimeError if any of the preconditions is not met.
-
-
class
pydrake.systems.analysis.
IntegratorBase_[Expression]
¶ An abstract class for an integrator for ODEs and DAEs as represented by a Drake System. Integrators solve initial value problems of the form:
ẋ(t) = f(t, x(t)) with f : ℝ × ℝⁿ → ℝⁿ
(i.e.,
f()
is an ordinary differential equation) given initial conditions (t₀, x₀). Thus, integrators advance the continuous state of a dynamical system forward in time.Drake’s subclasses of IntegratorBase<T> should follow the naming pattern
FooIntegrator<T>
by convention.-
__init__
¶ Initialize self. See help(type(self)) for accurate signature.
-
get_dense_output
(self: pydrake.systems.analysis.IntegratorBase_[Expression]) → drake::trajectories::PiecewisePolynomial<drake::symbolic::Expression>¶ Returns a const pointer to the integrator’s current PiecewisePolynomial instance, holding a representation of the continuous state trajectory since the last StartDenseIntegration() call. This is suitable to query the integrator’s current dense output, if any (may be nullptr).
-
get_fixed_step_mode
(self: pydrake.systems.analysis.IntegratorBase_[Expression]) → bool¶ Gets whether an integrator is running in fixed step mode. If the integrator does not support error estimation, this function will always return
True
.See also
set_fixed_step_mode()
-
get_maximum_step_size
(self: pydrake.systems.analysis.IntegratorBase_[Expression]) → pydrake.symbolic.Expression¶ Gets the maximum step size that may be taken by this integrator. This is a soft maximum: the integrator may stretch it by as much as 1% to hit a discrete event.
See also
set_requested_minimum_step_size()
-
get_requested_minimum_step_size
(self: pydrake.systems.analysis.IntegratorBase_[Expression]) → pydrake.symbolic.Expression¶ Gets the requested minimum step size
h_min
for this integrator.See also
set_requested_minimum_step_size()
See also
get_working_minimum_step_size(T)
-
get_target_accuracy
(self: pydrake.systems.analysis.IntegratorBase_[Expression]) → float¶ Gets the target accuracy.
See also
get_accuracy_in_use()
-
get_throw_on_minimum_step_size_violation
(self: pydrake.systems.analysis.IntegratorBase_[Expression]) → bool¶ Reports the current setting of the throw_on_minimum_step_size_violation flag.
See also
set_throw_on_minimum_step_size_violation().
-
set_fixed_step_mode
(self: pydrake.systems.analysis.IntegratorBase_[Expression], arg0: bool) → None¶ Sets an integrator with error control to fixed step mode. If the integrator runs in fixed step mode, it will always take the maximum step size directed (which may be that determined by get_maximum_step_size(), or may be smaller, as directed by, e.g., Simulator for event handling purposes).
Warning
The error estimation process will still be active (so get_error_estimate() will still return a correct result), meaning that the additional (typically, but not necessarily small) computation required for error estimation will still be performed.
Raises: - RuntimeError if integrator does not support error estimation and
flag
is set toFalse
.
-
set_maximum_step_size
(self: pydrake.systems.analysis.IntegratorBase_[Expression], arg0: pydrake.symbolic.Expression) → None¶ @anchor integrator-maxstep @name Methods related to maximum integration step size
Sets the nominal maximum step size- the actual maximum step size taken may be slightly larger (see set_maximum_step_size() and get_stretch_factor())- that an integrator will take. Each integrator has a default maximum step size, which might be infinite. Sets the maximum step size that may be taken by this integrator. This setting should be used if you know the maximum time scale of your problem. The integrator may stretch the maximum step size by as much as 1% to reach a discrete event. For fixed step integrators, all steps will be taken at the maximum step size unless an event would be missed.
Warning
See integrator-initial-step-size “Initial step size selection”
-
set_requested_minimum_step_size
(self: pydrake.systems.analysis.IntegratorBase_[Expression], arg0: pydrake.symbolic.Expression) → None¶ Sets the requested minimum step size
h_min
that may be taken by this integrator. No step smaller than this will be taken except under circumstances as described integrator-minstep “above”. This setting will be ignored if it is smaller than the absolute minimumh_floor
also described above. Default value is zero.- Parameter
min_step_size
: - a non-negative value. Setting this value to zero will cause the integrator to use a reasonable value instead (see get_working_minimum_step_size()).
See also
get_requested_minimum_step_size()
See also
get_working_minimum_step_size()
- Parameter
-
set_target_accuracy
(self: pydrake.systems.analysis.IntegratorBase_[Expression], arg0: float) → None¶ @anchor integrator-accuracy @name Methods for getting and setting integrator accuracy The precise meaning of accuracy is a complicated discussion, but it translates roughly to the number of significant digits you want in the results. By convention it is supplied as
10^-digits
, meaning that an accuracy of 1e-3 provides about three significant digits. For more discussion of accuracy, see accuracy_and_tolerance and ref. [1].Integrators vary in the range of accuracy (loosest to tightest) that they can support, and each integrator will choose a default accuracy to be used that lies somewhere within this range and attempts to balance computation and accuracy. If you request accuracy outside the supported range for the chosen integrator it will be quietly adjusted to be in range. You can find out the accuracy setting actually being used using
get_accuracy_in_use()
.Implicit integrators additionally use the accuracy setting for determining when the underlying Newton-Raphson root finding process has converged. For those integrators, the accuracy setting also limits the allowable iteration error in the Newton-Raphson process. Looser accuracy in that process certainly implies greater error in the ODE solution and might impact the stability of the solution negatively as well.
- [1] M. Sherman, A. Seth, S. Delp. Procedia IUTAM 2:241-261 (2011),
Section 3.3. https://dx.doi.org/10.1016/j.piutam.2011.04.023
Request that the integrator attempt to achieve a particular accuracy for the continuous portions of the simulation. Otherwise a default accuracy is chosen for you. This may be ignored for fixed-step integration since accuracy control requires variable step sizes. You should call supports_error_estimation() to ensure that the integrator supports this capability before calling this function; if the integrator does not support it, this method will throw an exception.
Raises: RuntimeError if integrator does not support error estimation.
-
set_throw_on_minimum_step_size_violation
(self: pydrake.systems.analysis.IntegratorBase_[Expression], arg0: bool) → None¶ Sets whether the integrator should throw a RuntimeError exception when the integrator’s step size selection algorithm determines that it must take a step smaller than the minimum step size (for, e.g., purposes of error control). Default is
True
. IfFalse
, the integrator will advance time and state using the minimum specified step size in such situations. See integrator-minstep “this section” for more detail.
-
StartDenseIntegration
(self: pydrake.systems.analysis.IntegratorBase_[Expression]) → None¶ Starts dense integration, allocating a new dense output for this integrator to use.
- Precondition:
- The integrator has been initialized.
- Precondition:
- The system being integrated has continuous state.
- Precondition:
- No dense integration is in progress (no dense output is held by the integrator)
Raises: RuntimeError if any of the preconditions is not met. Warning
Dense integration may incur significant overhead.
-
StopDenseIntegration
(self: pydrake.systems.analysis.IntegratorBase_[Expression]) → drake::trajectories::PiecewisePolynomial<drake::symbolic::Expression>¶ Stops dense integration, yielding ownership of the current dense output to the caller.
- Remark:
- This process is irreversible.
Returns: A PiecewisePolynomial instance, i.e. a representation of the continuous state trajectory of the system being integrated that can be evaluated at any time within its extension. This representation is defined starting at the context time of the last StartDenseIntegration() call and finishing at the current context time. - Precondition:
- Dense integration is in progress (a dense output is held by this integrator, after a call to StartDenseIntegration()).
- Postcondition:
- Previously held dense output is not updated nor referenced by the integrator anymore.
Raises: RuntimeError if any of the preconditions is not met.
-
-
class
pydrake.systems.analysis.
IntegratorBase_[float]
¶ An abstract class for an integrator for ODEs and DAEs as represented by a Drake System. Integrators solve initial value problems of the form:
ẋ(t) = f(t, x(t)) with f : ℝ × ℝⁿ → ℝⁿ
(i.e.,
f()
is an ordinary differential equation) given initial conditions (t₀, x₀). Thus, integrators advance the continuous state of a dynamical system forward in time.Drake’s subclasses of IntegratorBase<T> should follow the naming pattern
FooIntegrator<T>
by convention.-
__init__
¶ Initialize self. See help(type(self)) for accurate signature.
-
get_dense_output
(self: pydrake.systems.analysis.IntegratorBase_[float]) → pydrake.trajectories.PiecewisePolynomial¶ Returns a const pointer to the integrator’s current PiecewisePolynomial instance, holding a representation of the continuous state trajectory since the last StartDenseIntegration() call. This is suitable to query the integrator’s current dense output, if any (may be nullptr).
-
get_fixed_step_mode
(self: pydrake.systems.analysis.IntegratorBase_[float]) → bool¶ Gets whether an integrator is running in fixed step mode. If the integrator does not support error estimation, this function will always return
True
.See also
set_fixed_step_mode()
-
get_maximum_step_size
(self: pydrake.systems.analysis.IntegratorBase_[float]) → float¶ Gets the maximum step size that may be taken by this integrator. This is a soft maximum: the integrator may stretch it by as much as 1% to hit a discrete event.
See also
set_requested_minimum_step_size()
-
get_requested_minimum_step_size
(self: pydrake.systems.analysis.IntegratorBase_[float]) → float¶ Gets the requested minimum step size
h_min
for this integrator.See also
set_requested_minimum_step_size()
See also
get_working_minimum_step_size(T)
-
get_target_accuracy
(self: pydrake.systems.analysis.IntegratorBase_[float]) → float¶ Gets the target accuracy.
See also
get_accuracy_in_use()
-
get_throw_on_minimum_step_size_violation
(self: pydrake.systems.analysis.IntegratorBase_[float]) → bool¶ Reports the current setting of the throw_on_minimum_step_size_violation flag.
See also
set_throw_on_minimum_step_size_violation().
-
set_fixed_step_mode
(self: pydrake.systems.analysis.IntegratorBase_[float], arg0: bool) → None¶ Sets an integrator with error control to fixed step mode. If the integrator runs in fixed step mode, it will always take the maximum step size directed (which may be that determined by get_maximum_step_size(), or may be smaller, as directed by, e.g., Simulator for event handling purposes).
Warning
The error estimation process will still be active (so get_error_estimate() will still return a correct result), meaning that the additional (typically, but not necessarily small) computation required for error estimation will still be performed.
Raises: - RuntimeError if integrator does not support error estimation and
flag
is set toFalse
.
-
set_maximum_step_size
(self: pydrake.systems.analysis.IntegratorBase_[float], arg0: float) → None¶ @anchor integrator-maxstep @name Methods related to maximum integration step size
Sets the nominal maximum step size- the actual maximum step size taken may be slightly larger (see set_maximum_step_size() and get_stretch_factor())- that an integrator will take. Each integrator has a default maximum step size, which might be infinite. Sets the maximum step size that may be taken by this integrator. This setting should be used if you know the maximum time scale of your problem. The integrator may stretch the maximum step size by as much as 1% to reach a discrete event. For fixed step integrators, all steps will be taken at the maximum step size unless an event would be missed.
Warning
See integrator-initial-step-size “Initial step size selection”
-
set_requested_minimum_step_size
(self: pydrake.systems.analysis.IntegratorBase_[float], arg0: float) → None¶ Sets the requested minimum step size
h_min
that may be taken by this integrator. No step smaller than this will be taken except under circumstances as described integrator-minstep “above”. This setting will be ignored if it is smaller than the absolute minimumh_floor
also described above. Default value is zero.- Parameter
min_step_size
: - a non-negative value. Setting this value to zero will cause the integrator to use a reasonable value instead (see get_working_minimum_step_size()).
See also
get_requested_minimum_step_size()
See also
get_working_minimum_step_size()
- Parameter
-
set_target_accuracy
(self: pydrake.systems.analysis.IntegratorBase_[float], arg0: float) → None¶ @anchor integrator-accuracy @name Methods for getting and setting integrator accuracy The precise meaning of accuracy is a complicated discussion, but it translates roughly to the number of significant digits you want in the results. By convention it is supplied as
10^-digits
, meaning that an accuracy of 1e-3 provides about three significant digits. For more discussion of accuracy, see accuracy_and_tolerance and ref. [1].Integrators vary in the range of accuracy (loosest to tightest) that they can support, and each integrator will choose a default accuracy to be used that lies somewhere within this range and attempts to balance computation and accuracy. If you request accuracy outside the supported range for the chosen integrator it will be quietly adjusted to be in range. You can find out the accuracy setting actually being used using
get_accuracy_in_use()
.Implicit integrators additionally use the accuracy setting for determining when the underlying Newton-Raphson root finding process has converged. For those integrators, the accuracy setting also limits the allowable iteration error in the Newton-Raphson process. Looser accuracy in that process certainly implies greater error in the ODE solution and might impact the stability of the solution negatively as well.
- [1] M. Sherman, A. Seth, S. Delp. Procedia IUTAM 2:241-261 (2011),
Section 3.3. https://dx.doi.org/10.1016/j.piutam.2011.04.023
Request that the integrator attempt to achieve a particular accuracy for the continuous portions of the simulation. Otherwise a default accuracy is chosen for you. This may be ignored for fixed-step integration since accuracy control requires variable step sizes. You should call supports_error_estimation() to ensure that the integrator supports this capability before calling this function; if the integrator does not support it, this method will throw an exception.
Raises: RuntimeError if integrator does not support error estimation.
-
set_throw_on_minimum_step_size_violation
(self: pydrake.systems.analysis.IntegratorBase_[float], arg0: bool) → None¶ Sets whether the integrator should throw a RuntimeError exception when the integrator’s step size selection algorithm determines that it must take a step smaller than the minimum step size (for, e.g., purposes of error control). Default is
True
. IfFalse
, the integrator will advance time and state using the minimum specified step size in such situations. See integrator-minstep “this section” for more detail.
-
StartDenseIntegration
(self: pydrake.systems.analysis.IntegratorBase_[float]) → None¶ Starts dense integration, allocating a new dense output for this integrator to use.
- Precondition:
- The integrator has been initialized.
- Precondition:
- The system being integrated has continuous state.
- Precondition:
- No dense integration is in progress (no dense output is held by the integrator)
Raises: RuntimeError if any of the preconditions is not met. Warning
Dense integration may incur significant overhead.
-
StopDenseIntegration
(self: pydrake.systems.analysis.IntegratorBase_[float]) → pydrake.trajectories.PiecewisePolynomial¶ Stops dense integration, yielding ownership of the current dense output to the caller.
- Remark:
- This process is irreversible.
Returns: A PiecewisePolynomial instance, i.e. a representation of the continuous state trajectory of the system being integrated that can be evaluated at any time within its extension. This representation is defined starting at the context time of the last StartDenseIntegration() call and finishing at the current context time. - Precondition:
- Dense integration is in progress (a dense output is held by this integrator, after a call to StartDenseIntegration()).
- Postcondition:
- Previously held dense output is not updated nor referenced by the integrator anymore.
Raises: RuntimeError if any of the preconditions is not met.
-
-
pydrake.systems.analysis.
MonteCarloSimulation
(make_simulator: Callable[[pydrake.common._module_py.RandomGenerator], pydrake.systems.analysis.Simulator_[float]], output: Callable[[pydrake.systems.framework.System_[float], pydrake.systems.framework.Context_[float]], float], final_time: float, num_samples: int, generator: pydrake.common._module_py.RandomGenerator) → List[pydrake.systems.analysis.RandomSimulationResult]¶ Generate samples of a scalar random variable output by running many random simulations drawn from independent samples of the distributions governing the stochastic simulation.
In pseudo-code, this algorithm implements:
for i=1:num_samples const generator_snapshot = deepcopy(generator) output = RandomSimulation(..., generator) data(i) = std::pair(generator_snapshot, output) return data
See also
RandomSimulation() for details about
make_simulator
,output
, andfinal_time
.- Parameter
num_samples
: - Number of independent samples to draw from the distribution (and equivalently, the number of simulations to run).
- Parameter
generator
: - Random number generator to be used to generate the random samples. If null, then a new RandomGenerator will be allocated and used internally (and repeated calls to this method will return identical results). To produce statistically “independent” samples on a future call to MonteCarloSimulation, you should make repeated uses of the same RandomGenerator object.
Returns: a list of RandomSimulationResult’s. - Parameter
-
pydrake.systems.analysis.
PrintSimulatorStatistics
(arg0: pydrake.systems.analysis.Simulator_[float]) → None¶ This method outputs to stdout relevant simulation statistics for a simulator that advanced the state of a system forward in time.
- Parameter
simulator
: - The simulator to output statistics for.
- Parameter
-
pydrake.systems.analysis.
RandomSimulation
(make_simulator: Callable[[pydrake.common._module_py.RandomGenerator], pydrake.systems.analysis.Simulator_[float]], output: Callable[[pydrake.systems.framework.System_[float], pydrake.systems.framework.Context_[float]], float], final_time: float, generator: pydrake.common._module_py.RandomGenerator) → float¶ Run a deterministic simulation of a (stochastic) System using the
generator
to instantiate all “random” quantities.In pseudo-code, this algorithm implements:
simulator = make_simulator(generator) simulator.get_system().SetRandomContext(generator) simulator.AdvanceTo(final_time) return output(simulator.get_context())
- Parameter
make_simulator
: - Callers to this method define a stochastic simulation by providing
the
make_simulator
factory method to return a Simulator using the supplied RandomGenerator as the only source of randomness. This interface was designed to support cases where the System/Diagram is random (not only the Context), e.g. in the case where are variable number of objects are added to a multibody simulation. - Parameter
output
: - The scalar random variable output, denoted
output
, is defined as a function of the Simulator’s System’s Context, evaluated at thefinal_time
. Monte-Carlo investigations that studying the details of an entire trajectory can still use this interface, e.g. by including a “runtime monitor” System that latches the worst-case deviation of a specification into it’s Context to be queried at the final time. - Parameter
final_time
: - The time that each instance of the Simulator is stepped to. In many cases, this will be equivalent to the duration of the simulation, but it need not be because SetRandomContext() could initialize the time to a non-zero value, or an event could trigger premature termination of the simulation (see #4447).
- Parameter
generator
: - Random number generator to be used to generate the random samples.
Returns: the output
evaluated from the Context atfinal_time
.- Parameter
-
class
pydrake.systems.analysis.
RandomSimulationResult
¶ A snapshot of the generator used to produce the random simulation. Use, e.g.,
RandomGenerator generator(result.generator_snapshot) RandomSimulation(make_simulator, output, final_time, &generator)
for a deterministic playback of the sampled simulation. Use the operator<<() operator>>() methods provided for the generator, which implements the Standard Template Library <a href= “https://en.cppreference.com/w/cpp/named_req/RandomNumberEngine”> RandomNumberEngine concept</a>, if you wish to serialize the results. Note that the generator “snapshots” are returned as const, because any non-const operations on that object may advance the state of the generator (making it no-longer valuable in reproducing the simulation).
-
__init__
¶ Initialize self. See help(type(self)) for accurate signature.
-
generator_snapshot
¶
-
output
¶
-
-
pydrake.systems.analysis.
RegionOfAttraction
(system: pydrake.systems.framework.System_[float], context: pydrake.systems.framework.Context_[float], options: pydrake.systems.analysis.RegionOfAttractionOptions = <pydrake.systems.analysis.RegionOfAttractionOptions object at 0x7fa8ff3afcf0>) → pydrake.symbolic.Expression¶ Estimates the region of attraction of the time-invariant
system
at the fixed point defined bycontext
.This implementation only searches for the largest level set of the
lyapunov_candidate
function fromoptions
(or a candidate obtained from solving the Lyapunov equation on the linearization).- Parameter
system
: - a time-invariant continuous-time System that supports scalar-type conversion to symbolic::Expression. The dynamics of the system must be polynomial.
- Parameter
context
: - a Context that defines the parameters of the system and the fixed-point about which we are analyzing the regional stability.
- Parameter
options
: - provides a variety of configuration options.
See also
RegionOfAttractionOptions.
Returns: a symbolic::Expression representing a Lyapunov function using the symbolic Variables named x0, x1…, where the order matches the continuous state vector in the context
, or the vector state_variables passed in through the options structure (if it is non-empty). The level set {x | V(x)<=1} containing the fixed-point incontext
represents the region of attraction.Note: There are more numerical techniques that we know how to apply here. Do report an issue if you discover a system for which this code does not perform well.
- Parameter
-
class
pydrake.systems.analysis.
RegionOfAttractionOptions
¶ Consolidates the many possible options to be passed to the region of attraction algorithm.
-
__init__
(self: pydrake.systems.analysis.RegionOfAttractionOptions) → None¶
-
lyapunov_candidate
¶ A candidate Lyapunov function using the symbolic Variables named x0, x1, …, where the order matches the continuous state vector of the system being evaluated (or the vector state_variables).
-
state_variables
¶ If non-empty, a list of Variable that associates the variable name with the elements of the System’s continuous state vector. Must be empty or have size equal to the number of continuous state variables in the system.
-
-
pydrake.systems.analysis.
ResetIntegratorFromFlags
(simulator: pydrake.systems.analysis.Simulator_[float], scheme: str, max_step_size: float) → pydrake.systems.analysis.IntegratorBase_[float]¶ Resets the integrator used to advanced the continuous time dynamics of the system associated with
simulator
according to the given arguments.- Parameter
simulator
: - On input, a valid pointer to a Simulator. On output the integrator
for
simulator
is reset according to the given arguments. - Parameter
scheme
: - Integration scheme to be used, e.g., “runge_kutta2”. See GetIntegrationSchemes() for a the list of valid options.
- Parameter
max_step_size
: - The IntegratorBase::set_maximum_step_size() value.
Returns: A reference to the the newly created integrator owned by simulator
.@tparam_default_nonsymbolic_scalar
- Parameter
-
pydrake.systems.analysis.
RungeKutta2Integrator
¶ alias of
pydrake.systems.analysis.RungeKutta2Integrator_[float]
-
template
pydrake.systems.analysis.
RungeKutta2Integrator_
¶ Instantiations:
RungeKutta2Integrator_[float]
,RungeKutta2Integrator_[AutoDiffXd]
,RungeKutta2Integrator_[Expression]
-
class
RungeKutta2Integrator_[float]
¶ Bases:
pydrake.systems.analysis.IntegratorBase_[float]
A second-order, explicit Runge Kutta integrator.
-
__init__
(self: pydrake.systems.analysis.RungeKutta2Integrator_[float], system: pydrake.systems.framework.System_[float], max_step_size: float, context: pydrake.systems.framework.Context_[float] = None) → None¶ Constructs fixed-step integrator for a given system using the given context for initial conditions.
- Parameter
system
: - A reference to the system to be simulated
- Parameter
max_step_size
: - The maximum (fixed) step size; the integrator will not take larger step sizes than this.
- Parameter
context
: - pointer to the context (nullptr is ok, but the caller must set a non-null context before Initialize()-ing the integrator).
See also
Initialize()
- Parameter
-
-
class
-
class
pydrake.systems.analysis.
RungeKutta2Integrator_[AutoDiffXd]
¶ Bases:
pydrake.systems.analysis.IntegratorBase_[AutoDiffXd]
A second-order, explicit Runge Kutta integrator.
-
__init__
(self: pydrake.systems.analysis.RungeKutta2Integrator_[AutoDiffXd], system: pydrake.systems.framework.System_[AutoDiffXd], max_step_size: pydrake.autodiffutils.AutoDiffXd, context: pydrake.systems.framework.Context_[AutoDiffXd] = None) → None¶ Constructs fixed-step integrator for a given system using the given context for initial conditions.
- Parameter
system
: - A reference to the system to be simulated
- Parameter
max_step_size
: - The maximum (fixed) step size; the integrator will not take larger step sizes than this.
- Parameter
context
: - pointer to the context (nullptr is ok, but the caller must set a non-null context before Initialize()-ing the integrator).
See also
Initialize()
- Parameter
-
-
class
pydrake.systems.analysis.
RungeKutta2Integrator_[Expression]
¶ Bases:
pydrake.systems.analysis.IntegratorBase_[Expression]
A second-order, explicit Runge Kutta integrator.
-
__init__
(self: pydrake.systems.analysis.RungeKutta2Integrator_[Expression], system: pydrake.systems.framework.System_[Expression], max_step_size: pydrake.symbolic.Expression, context: pydrake.systems.framework.Context_[Expression] = None) → None¶ Constructs fixed-step integrator for a given system using the given context for initial conditions.
- Parameter
system
: - A reference to the system to be simulated
- Parameter
max_step_size
: - The maximum (fixed) step size; the integrator will not take larger step sizes than this.
- Parameter
context
: - pointer to the context (nullptr is ok, but the caller must set a non-null context before Initialize()-ing the integrator).
See also
Initialize()
- Parameter
-
-
class
pydrake.systems.analysis.
RungeKutta2Integrator_[float]
¶ Bases:
pydrake.systems.analysis.IntegratorBase_[float]
A second-order, explicit Runge Kutta integrator.
-
__init__
(self: pydrake.systems.analysis.RungeKutta2Integrator_[float], system: pydrake.systems.framework.System_[float], max_step_size: float, context: pydrake.systems.framework.Context_[float] = None) → None¶ Constructs fixed-step integrator for a given system using the given context for initial conditions.
- Parameter
system
: - A reference to the system to be simulated
- Parameter
max_step_size
: - The maximum (fixed) step size; the integrator will not take larger step sizes than this.
- Parameter
context
: - pointer to the context (nullptr is ok, but the caller must set a non-null context before Initialize()-ing the integrator).
See also
Initialize()
- Parameter
-
-
pydrake.systems.analysis.
RungeKutta3Integrator
¶ alias of
pydrake.systems.analysis.RungeKutta3Integrator_[float]
-
template
pydrake.systems.analysis.
RungeKutta3Integrator_
¶ Instantiations:
RungeKutta3Integrator_[float]
,RungeKutta3Integrator_[AutoDiffXd]
-
class
RungeKutta3Integrator_[float]
¶ Bases:
pydrake.systems.analysis.IntegratorBase_[float]
A third-order Runge Kutta integrator with a third order error estimate.
For a discussion of this Runge-Kutta method, see [Butcher, 1987]. The embedded error estimate was derived using the method mentioned in [Hairer, 1993].
The Butcher tableau for this integrator follows:
| 0 | 1/2 | 1/2 1 | -1 2 --------------------------------------------------------------------------- 1/6 2/3 1/6 0 1 0
where the second to last row is the 3rd-order propagated solution and the last row is the 2nd-order midpoint used for the error estimate.
The following documentation is pulled from Simbody’s implementation of this integrator: “This is a 3-stage, first-same-as-last (FSAL) 3rd order method which gives us an embedded 2nd order method as well, so we can extract a 3rd-order error estimate for the 2nd-order result, which error estimate can then be used for step size control, since it will behave as h^3. We then propagate the 3rd order result (whose error is unknown), which Hairer calls ‘local extrapolation’. We call the initial state (t0,y0) and want (t0+h,y1). We are given the initial derivative f0=f(t0,y0), which most likely is left over from an evaluation at the end of the last step.”
- [Butcher, 1987] J. C. Butcher. The Numerical Analysis of Ordinary Differential Equations. John Wiley & Sons, 1987. p. 325.
- [Hairer, 1993] E. Hairer, S. Noersett, and G. Wanner. Solving ODEs I. 2nd rev. ed. Springer, 1993. p. 166.
-
__init__
(self: pydrake.systems.analysis.RungeKutta3Integrator_[float], system: pydrake.systems.framework.System_[float], context: pydrake.systems.framework.Context_[float] = None) → None¶
-
class
-
class
pydrake.systems.analysis.
RungeKutta3Integrator_[AutoDiffXd]
¶ Bases:
pydrake.systems.analysis.IntegratorBase_[AutoDiffXd]
A third-order Runge Kutta integrator with a third order error estimate.
For a discussion of this Runge-Kutta method, see [Butcher, 1987]. The embedded error estimate was derived using the method mentioned in [Hairer, 1993].
The Butcher tableau for this integrator follows:
| 0 | 1/2 | 1/2 1 | -1 2 --------------------------------------------------------------------------- 1/6 2/3 1/6 0 1 0
where the second to last row is the 3rd-order propagated solution and the last row is the 2nd-order midpoint used for the error estimate.
The following documentation is pulled from Simbody’s implementation of this integrator: “This is a 3-stage, first-same-as-last (FSAL) 3rd order method which gives us an embedded 2nd order method as well, so we can extract a 3rd-order error estimate for the 2nd-order result, which error estimate can then be used for step size control, since it will behave as h^3. We then propagate the 3rd order result (whose error is unknown), which Hairer calls ‘local extrapolation’. We call the initial state (t0,y0) and want (t0+h,y1). We are given the initial derivative f0=f(t0,y0), which most likely is left over from an evaluation at the end of the last step.”
- [Butcher, 1987] J. C. Butcher. The Numerical Analysis of Ordinary Differential Equations. John Wiley & Sons, 1987. p. 325.
- [Hairer, 1993] E. Hairer, S. Noersett, and G. Wanner. Solving ODEs I. 2nd rev. ed. Springer, 1993. p. 166.
-
__init__
(self: pydrake.systems.analysis.RungeKutta3Integrator_[AutoDiffXd], system: pydrake.systems.framework.System_[AutoDiffXd], context: pydrake.systems.framework.Context_[AutoDiffXd] = None) → None¶
-
class
pydrake.systems.analysis.
RungeKutta3Integrator_[float]
¶ Bases:
pydrake.systems.analysis.IntegratorBase_[float]
A third-order Runge Kutta integrator with a third order error estimate.
For a discussion of this Runge-Kutta method, see [Butcher, 1987]. The embedded error estimate was derived using the method mentioned in [Hairer, 1993].
The Butcher tableau for this integrator follows:
| 0 | 1/2 | 1/2 1 | -1 2 --------------------------------------------------------------------------- 1/6 2/3 1/6 0 1 0
where the second to last row is the 3rd-order propagated solution and the last row is the 2nd-order midpoint used for the error estimate.
The following documentation is pulled from Simbody’s implementation of this integrator: “This is a 3-stage, first-same-as-last (FSAL) 3rd order method which gives us an embedded 2nd order method as well, so we can extract a 3rd-order error estimate for the 2nd-order result, which error estimate can then be used for step size control, since it will behave as h^3. We then propagate the 3rd order result (whose error is unknown), which Hairer calls ‘local extrapolation’. We call the initial state (t0,y0) and want (t0+h,y1). We are given the initial derivative f0=f(t0,y0), which most likely is left over from an evaluation at the end of the last step.”
- [Butcher, 1987] J. C. Butcher. The Numerical Analysis of Ordinary Differential Equations. John Wiley & Sons, 1987. p. 325.
- [Hairer, 1993] E. Hairer, S. Noersett, and G. Wanner. Solving ODEs I. 2nd rev. ed. Springer, 1993. p. 166.
-
__init__
(self: pydrake.systems.analysis.RungeKutta3Integrator_[float], system: pydrake.systems.framework.System_[float], context: pydrake.systems.framework.Context_[float] = None) → None¶
-
pydrake.systems.analysis.
Simulator
¶
-
template
pydrake.systems.analysis.
Simulator_
¶ Instantiations:
Simulator_[float]
,Simulator_[AutoDiffXd]
-
class
Simulator_[float]
¶ A class for advancing the state of hybrid dynamic systems, represented by
System<T>
objects, forward in time. Starting with an initial Context for a given System, Simulator advances time and produces a series of Context values that forms a trajectory satisfying the system’s dynamic equations to a specified accuracy. Only the Context is modified by a Simulator; the System is const.A Drake System is a continuous/discrete/hybrid dynamic system where the continuous part is a DAE, that is, it is expected to consist of a set of differential equations and bilateral algebraic constraints. The set of active constraints may change as a result of particular events, such as contact.
Given a current Context, we expect a System to provide us with - derivatives for the continuous differential equations that already satisfy the differentiated form of the constraints (typically, acceleration constraints), - a projection method for least-squares correction of violated higher-level constraints (position and velocity level), - a time-of-next-update method that can be used to adjust the integrator step size in preparation for a discrete update, - methods that can update discrete variables when their update time is reached, - witness (guard) functions for event isolation, - event handlers (reset functions) for making appropriate changes to state and mode variables when an event has been isolated.
The continuous parts of the trajectory are advanced using a numerical integrator. Different integrators have different properties; you can choose the one that is most appropriate for your application or use the default which is adequate for most systems.
<h3>How the simulation is stepped: simulation mechanics for authors of discrete and hybrid systems</h3>
This section is targeted toward users who have created a LeafSystem implementing a discrete or hybrid system. For authors of such systems, it can be useful to understand the simulation details in order to attain the desired state behavior over time. This behavior is dependent on the ordering in which discrete events and continuous updates are processed. (By “discrete events” we mean to include any of Drake’s event handlers.) The basic issues and terminology are introduced in the discrete_systems module; please look there first before proceeding.
As pictured in discrete_systems, when a continuous-time system has discrete events, the state x can have two significant values at the event time t. These are - x⁻(t), the value of x before the discrete update occurs (○ markers), and - x⁺(t), the value of x after the discrete update occurs (● markers).
Thus the value of the Context, which contains both time and state, advances from {t, x⁻(t)} to {t, x⁺(t)} as a result of the update. While those Context values are user-visible, the details of stepping here require an intermediate value which we’ll denote {t, x*(t)}.
Recall that Drake’s state x is partitioned into continuous, discrete, and abstract partitions xc, xd, and xa, so
x = { xc, xd, xa }
. Within a single step, these are updated in three stages: -Unrestricted update (can change x) -
Discrete update (can change only xd) -
Continuous update (changes t and xc)
Where needed, we extend the above notation to xc⁻, xa⁺, etc. to indicate the value of an individual partition at a particular stage of the stepping algorithm.
The following pseudocode uses the above notation to describe the algorithm “Step()” that the Simulator uses to incrementally advance the system trajectory (time t and state x). The Simulator’s AdvanceTo() method will be defined in terms of Step below. In general, the length of a step is not known a priori and is determined by the Step() algorithm. Each step consists of zero or more unrestricted updates, followed by zero or more discrete updates, followed by (possibly zero-length) continuous time and state advancement, followed by zero or more publishes, and then a call to the monitor() function if one has been defined. Updates, publishes, and the monitor can report errors or detect a termination condition; that is not shown in the pseudocode below.
The pseudocode will clarify the effects on time and state of each of the update stages above. This algorithm is given a starting Context value
{tₛ, x⁻(tₛ)}
and returns an end Context value{tₑ, x⁻(tₑ)}
, where tₑ is no later than a given tₘₐₓ.// Advance the trajectory (time and state) from start value {tₛ, x⁻(tₛ)} to an // end value {tₑ, x⁻(tₑ)}, where tₛ ≤ tₑ ≤ tₘₐₓ. procedure Step(tₛ, x⁻(tₛ), tₘₐₓ) // Update any variables (no restrictions). x*(tₛ) ← DoAnyUnrestrictedUpdates(tₛ, x⁻(tₛ)) // ---------------------------------- // Time and state are at {tₛ, x*(tₛ)} // ---------------------------------- // Update discrete variables. xd⁺(tₛ) ← DoAnyDiscreteUpdates(tₛ, x*(tₛ)) xc⁺(tₛ) ← xc*(tₛ) // These values carry over from x*(tₛ). xa⁺(tₛ) ← xa*(tₛ) // ---------------------------------- // Time and state are at {tₛ, x⁺(tₛ)} // ---------------------------------- // See how far it is safe to integrate without missing any events. tₑᵥₑₙₜ ← CalcNextEventTime(tₛ, x⁺(tₛ)) // Integrate continuous variables forward in time. Integration may terminate // before reaching tₛₜₒₚ due to witnessed events. tₛₜₒₚ ← min(tₑᵥₑₙₜ, tₘₐₓ) tₑ, xc⁻(tₑ) ← Integrate(tₛ, x⁺(tₛ), tₛₜₒₚ) xd⁻(tₑ) ← xd⁺(tₛ) // Discrete values are held from x⁺(tₛ). xa⁻(tₑ) ← xa⁺(tₛ) // ---------------------------------- // Time and state are at {tₑ, x⁻(tₑ)} // ---------------------------------- DoAnyPublishes(tₑ, x⁻(tₑ)) CallMonitor(tₑ, x⁻(tₑ)) return {tₑ, x⁻(tₑ)}
We can use the notation and pseudocode to flesh out the AdvanceTo(), AdvancePendingEvents(), and Initialize() functions. Termination and error conditions detected by event handlers or the monitor are reported as status returns from these methods.
// Advance the simulation until time tₘₐₓ. procedure AdvanceTo(tₘₐₓ) → status t ← current_time while t < tₘₐₓ {tₑ, x⁻(tₑ)} ← Step(t, x⁻(t), tₘₐₓ) {t, x⁻(t)} ← {tₑ, x⁻(tₑ)} endwhile // AdvancePendingEvents() is an advanced method, not commonly used. // Perform just the start-of-step update to advance from x⁻(t) to x⁺(t). procedure AdvancePendingEvents() → status t ≜ current_time, x⁻(t) ≜ current_state x⁺(t) ← DoAnyPendingUpdates(t, x⁻(t)) as in Step() x(t) ← x⁺(t) // No continuous update needed. DoAnyPublishes(t, x(t)) CallMonitor(t, x(t)) // Update time and state to {t₀, x⁻(t₀)}, which is the starting value of the // trajectory, and thus the value the Context should contain at the start of the // first simulation step. procedure Initialize(t₀, x₀) → status x⁺(t₀) ← DoAnyInitializationUpdates as in Step() x⁻(t₀) ← x⁺(t₀) // No continuous update needed. // ---------------------------------- // Time and state are at {t₀, x⁻(t₀)} // ---------------------------------- DoAnyPublishes(t₀, x⁻(t₀)) CallMonitor(t₀, x⁻(t₀))
Initialize() can be viewed as a “0ᵗʰ step” that occurs before the first Step() call as described above. Like Step(), Initialize() first performs pending updates (in this case only initialization events can be “pending”). Time doesn’t advance so there is no continuous update phase and witnesses cannot trigger. Finally, again like Step(), the initial trajectory point
{t₀, x⁻(t₀)}
is provided to the handlers for any triggered publish events. That includes initialization publish events, per-step publish events, and periodic or timed publish events that trigger at t₀, followed by a call to the monitor() function if one has been defined (a monitor is semantically identical to a per-step publish).-
__init__
(self: pydrake.systems.analysis.Simulator_[float], system: pydrake.systems.framework.System_[float], context: pydrake.systems.framework.Context_[float] = None) → None¶ Create a Simulator that can advance a given System through time to produce a trajectory consisting of a sequence of Context values. The System must not have unresolved input ports if the values of those ports are necessary for computations performed during simulation (see class documentation).
The Simulator holds an internal, non-owned reference to the System object so you must ensure that
system
has a longer lifetime than the Simulator. It also owns a compatible Context internally that takes on each of the trajectory values. You may optionally provide a Context that will be used as the initial condition for the simulation; otherwise the Simulator will obtain a default Context fromsystem
.
-
AdvancePendingEvents
(self: pydrake.systems.analysis.Simulator_[float]) → pydrake.systems.analysis.SimulatorStatus¶ (Advanced) Handles discrete and abstract state update events that are pending from the previous AdvanceTo() call, without advancing time. See the Simulator class description for details about how Simulator advances time and handles events. In the terminology used there, this method advances the internal Context from
{t, x⁻(t)}
to{t, x⁺(t)}
.Normally, these update events would be handled at the start of the next AdvanceTo() call, so this method is rarely needed. It can be useful at the end of a simulation or to get intermediate results when you are specifically interested in the
x⁺(t)
result.This method is equivalent to
AdvanceTo(current_time)
, wherecurrent_time=simulator.get_context().get_time())
. If there are no pending events, nothing happens except possibly a final per-step publish call (if enabled) followed by a call to the monitor() function (if one has been provided).- Returns
status
: - A SimulatorStatus object indicating success, termination, or an error condition as reported by event handlers or the monitor function.
See also
AdvanceTo(), Initialize(), SimulatorStatus
- Returns
-
AdvanceTo
(self: pydrake.systems.analysis.Simulator_[float], boundary_time: float) → pydrake.systems.analysis.SimulatorStatus¶ Advances the System’s trajectory until
boundary_time
is reached in the context or some other termination condition occurs. A variety ofRuntimeError
conditions are possible here, as well as error conditions that may be thrown by the System when it is asked to perform computations. Be sure to enclose your simulation in atry-catch
block and display thewhat()
message.We recommend that you call Initialize() prior to making the first call to AdvanceTo(). However, if you don’t it will be called for you the first time that you attempt a step, possibly resulting in unexpected error conditions. See documentation for
Initialize()
for the error conditions it might produce.Warning
You should consider calling Initialize() if you alter the the Context or Simulator options between successive AdvanceTo() calls. See Initialize() for more information.
- Parameter
boundary_time
: - The maximum time to which the trajectory will be advanced by this call to AdvanceTo(). The method may return earlier if an event or the monitor function requests termination or reports an error condition.
- Returns
status
: - A SimulatorStatus object indicating success, termination, or an error condition as reported by event handlers or the monitor function. The time in the context will be set either to the boundary_time or the time a termination or error was first detected.
- Precondition:
- The internal Context satisfies all System constraints or will after pending Context updates are performed.
See also
Initialize(), AdvancePendingEvents(), SimulatorStatus
- Parameter
-
clear_monitor
(self: pydrake.systems.analysis.Simulator_[float]) → None¶ Removes the monitoring function if there is one.
See also
set_monitor()
-
get_actual_realtime_rate
(self: pydrake.systems.analysis.Simulator_[float]) → float¶ Return the rate that simulated time has progressed relative to real time. A return of 1 means the simulation just matched real time, 2 means the simulation was twice as fast as real time, 0.5 means it was running in 2X slow motion, etc.
The value returned here is calculated as follows:
simulated_time_now - initial_simulated_time rate = ------------------------------------------- realtime_now - initial_realtime
The
initial
times are recorded when Initialize() or ResetStatistics() is called. The returned rate is undefined if Initialize() has not yet been called.Returns: The rate achieved since the last Initialize() or ResetStatistics() call. See also
set_target_realtime_rate()
-
get_context
(self: pydrake.systems.analysis.Simulator_[float]) → pydrake.systems.framework.Context_[float]¶ Returns a const reference to the internally-maintained Context holding the most recent step in the trajectory. This is suitable for publishing or extracting information about this trajectory step. Do not call this method if there is no Context.
-
get_integrator
(self: pydrake.systems.analysis.Simulator_[float]) → pydrake.systems.analysis.IntegratorBase_[float]¶ Gets a reference to the integrator used to advance the continuous aspects of the system.
-
get_monitor
(self: pydrake.systems.analysis.Simulator_[float]) → Callable[[pydrake.systems.framework.Context_[float]], pydrake.systems.framework.EventStatus]¶ Obtains a reference to the monitoring function, which may be empty.
See also
set_monitor()
-
get_mutable_context
(self: pydrake.systems.analysis.Simulator_[float]) → pydrake.systems.framework.Context_[float]¶ Returns a mutable reference to the internally-maintained Context holding the most recent step in the trajectory. This is suitable for use in updates, sampling operations, event handlers, and constraint projection. You can also modify this prior to calling Initialize() to set initial conditions. Do not call this method if there is no Context.
-
get_mutable_integrator
(self: pydrake.systems.analysis.Simulator_[float]) → pydrake.systems.analysis.IntegratorBase_[float]¶ Gets a reference to the mutable integrator used to advance the continuous state of the system.
-
get_system
(self: pydrake.systems.analysis.Simulator_[float]) → pydrake.systems.framework.System_[float]¶ Gets a constant reference to the system.
Note
a mutable reference is not available.
-
get_target_realtime_rate
(self: pydrake.systems.analysis.Simulator_[float]) → float¶ Return the real time rate target currently in effect. The default is zero, meaning the Simulator runs as fast as possible. You can change the target with set_target_realtime_rate().
-
has_context
(self: pydrake.systems.analysis.Simulator_[float]) → bool¶ Returns
True
if this Simulator has an internally-maintained Context. This is always true unlessreset_context()
has been called.
-
Initialize
(self: pydrake.systems.analysis.Simulator_[float]) → pydrake.systems.analysis.SimulatorStatus¶ Prepares the Simulator for a simulation. In order, the sequence of actions taken here are: - The active integrator’s Initialize() method is invoked. - Statistics are reset. - Initialization update events are triggered and handled to produce the initial trajectory value
{t₀, x(t₀)}
. - Then that initial value is provided to the handlers for any publish events that have triggered, including initialization and per-step publish events, periodic or other time-triggered publish events that are scheduled for the initial time t₀, and finally a call to the monitor() function if one has been defined.See the class documentation for more information. We recommend calling Initialize() explicitly prior to beginning a simulation so that error conditions will be discovered early. However, Initialize() will be called automatically by the first AdvanceTo() call if it hasn’t already been called.
Note
If you make a change to the Context or to Simulator options between AdvanceTo() calls you should consider whether to call Initialize() before resuming; AdvanceTo() will not do that automatically for you. Whether to do so depends on whether you want the above initialization operations performed.
Note
In particular, if you changed the time you must call Initialize(). The time-triggered events must be recalculated in case one is due at the new starting time. Currently, the AdvanceTo() call will print a warning for the first violation; after 2020-12-01 the warning will become a hard error.
Warning
Initialize() does not automatically attempt to satisfy System constraints – it is up to you to make sure that constraints are satisfied by the initial conditions.
This method will throw
RuntimeError
if the combination of options doesn’t make sense. Other failures are possible from the System and integrator in use.- Returns
status
: - A SimulatorStatus object indicating success, termination, or an error condition as reported by event handlers or the monitor function.
See also
AdvanceTo(), AdvancePendingEvents(), SimulatorStatus
- Returns
-
reset_context
(self: pydrake.systems.analysis.Simulator_[float], context: pydrake.systems.framework.Context_[float]) → None¶ Replace the internally-maintained Context with a different one. The current Context is deleted. This is useful for supplying a new set of initial conditions. You should invoke Initialize() after replacing the Context.
- Parameter
context
: - The new context, which may be null. If the context is null, a new context must be set before attempting to step the system forward.
- Parameter
-
ResetStatistics
(self: pydrake.systems.analysis.Simulator_[float]) → None¶ Forget accumulated statistics. Statistics are reset to the values they have post construction or immediately after
Initialize()
.
-
set_monitor
(self: pydrake.systems.analysis.Simulator_[float], monitor: Callable[[pydrake.systems.framework.Context_[float]], pydrake.systems.framework.EventStatus]) → None¶ Provides a monitoring function that will be invoked at the end of every step. (See the Simulator class documentation for a precise definition of “step”.) A monitor() function can be used to capture the trajectory, to terminate the simulation, or to detect error conditions. The monitor() function is invoked by the Simulator with a Context whose value is a point along the simulated trajectory. The monitor can be any functor and should capture any System references it needs to operate correctly.
A monitor() function behaves the same as would a per-step Publish event handler included in the top-level System or Diagram being simulated. As in the case of Publish(), the monitor is called at the end of every step taken internally by AdvanceTo(), and also at the end of Initialize() and AdvancePendingEvents(). (See the Simulator class documentation for more detail about what happens when in these methods.) The monitor receives the top-level (root) Context, from which any sub-Context can be obtained using
subsystem.GetMyContextFromRoot()
, provided the necessary subsystem reference has been captured for use in the monitor.** Examples Output time and continuous states whenever the trajectory is advanced:
simulator.set_monitor([](const Context<T>& root_context) { std::cout << root_context.get_time() << " " << root_context.get_continuous_state_vector() << std::endl; return EventStatus::Succeeded(); });
Terminate early but successfully on a condition in a subsystem of the System diagram being simulated:
simulator.set_monitor([&my_subsystem](const Context<T>& root_context) { const Context<T>& subcontext = my_subsystem.GetMyContextFromRoot(root_context); if (my_subsystem.GoalReached(subcontext)) { return EventStatus::ReachedTermination(my_subsystem, "Simulation achieved the desired goal."); } return EventStatus::Succeeded(); });
In the above case, the Simulator’s AdvanceTo() method will return early when the subsystem reports that it has reached its goal. The returned status will indicate the termination reason, and a human-readable termination message containing the message provided by the monitor can be obtained with status.FormatMessage().
Failure due to plant center of mass falling below a threshold:
simulator.set_monitor([&plant](const Context<T>& root_context) { const Context<T>& plant_context = plant.GetMyContextFromRoot(root_context); const Vector3<T> com = plant.CalcCenterOfMassPosition(plant_context); if (com[2] < 0.1) { // Check z height of com. return EventStatus::Failed(plant, "System fell over."); } return EventStatus::Succeeded(); });
In the above case the Simulator’s AdvanceTo() method will throw an RuntimeError containing a human-readable message including the text provided in the monitor.
Note
monitor() is called every time the trajectory is advanced by a step, which can mean it is called many times during a single AdvanceTo() call.
Note
The presence of a monitor has no effect on the step sizes taken, so a termination or error condition will be discovered only when first observed after a step is complete; it will not be further localized. Use witness-triggered events instead if you need precise isolation.
-
set_publish_every_time_step
(self: pydrake.systems.analysis.Simulator_[float], arg0: bool) → None¶ Sets whether the simulation should trigger a forced-Publish event on the System under simulation at the end of every trajectory-advancing step. Specifically, that means the System::Publish() event dispatcher will be invoked on each subsystem of the System and passed the current Context and a forced-publish Event. If a subsystem has declared a forced-publish event handler, that will be called. Otherwise, nothing will happen unless the DoPublish() dispatcher has been overridden.
Enabling this option does not cause a forced-publish to be triggered at initialization; if you want that you should also call
set_publish_at_initialization(true)
. If you want a forced-publish at the end of every step, you will usually also want one at the end of initialization, requiring both options to be enabled.See also
LeafSystem::DeclareForcedPublishEvent()
-
set_target_realtime_rate
(self: pydrake.systems.analysis.Simulator_[float], arg0: float) → None¶ Slow the simulation down to approximately synchronize with real time when it would otherwise run too fast. Normally the Simulator takes steps as quickly as it can. You can request that it slow down to synchronize with real time by providing a realtime rate greater than zero here.
Warning
No guarantees can be made about how accurately the simulation can be made to track real time, even if computation is fast enough. That’s because the system utilities used to implement this do not themselves provide such guarantees. So this is likely to work nicely for visualization purposes where human perception is the only concern. For any other uses you should consider whether approximate real time is adequate for your purposes.
Note
If the full-speed simulation is already slower than real time you can’t speed it up with this call! Instead consider requesting less integration accuracy, using a faster integration method or fixed time step, or using a simpler model.
- Parameter
realtime_rate
: - Desired rate relative to real time. Set to 1 to track real time, 2 to run twice as fast as real time, 0.5 for half speed, etc. Zero or negative restores the rate to its default of 0, meaning the simulation will proceed as fast as possible.
- Parameter
-
-
class
-
class
pydrake.systems.analysis.
Simulator_[AutoDiffXd]
¶ A class for advancing the state of hybrid dynamic systems, represented by
System<T>
objects, forward in time. Starting with an initial Context for a given System, Simulator advances time and produces a series of Context values that forms a trajectory satisfying the system’s dynamic equations to a specified accuracy. Only the Context is modified by a Simulator; the System is const.A Drake System is a continuous/discrete/hybrid dynamic system where the continuous part is a DAE, that is, it is expected to consist of a set of differential equations and bilateral algebraic constraints. The set of active constraints may change as a result of particular events, such as contact.
Given a current Context, we expect a System to provide us with - derivatives for the continuous differential equations that already satisfy the differentiated form of the constraints (typically, acceleration constraints), - a projection method for least-squares correction of violated higher-level constraints (position and velocity level), - a time-of-next-update method that can be used to adjust the integrator step size in preparation for a discrete update, - methods that can update discrete variables when their update time is reached, - witness (guard) functions for event isolation, - event handlers (reset functions) for making appropriate changes to state and mode variables when an event has been isolated.
The continuous parts of the trajectory are advanced using a numerical integrator. Different integrators have different properties; you can choose the one that is most appropriate for your application or use the default which is adequate for most systems.
<h3>How the simulation is stepped: simulation mechanics for authors of discrete and hybrid systems</h3>
This section is targeted toward users who have created a LeafSystem implementing a discrete or hybrid system. For authors of such systems, it can be useful to understand the simulation details in order to attain the desired state behavior over time. This behavior is dependent on the ordering in which discrete events and continuous updates are processed. (By “discrete events” we mean to include any of Drake’s event handlers.) The basic issues and terminology are introduced in the discrete_systems module; please look there first before proceeding.
As pictured in discrete_systems, when a continuous-time system has discrete events, the state x can have two significant values at the event time t. These are - x⁻(t), the value of x before the discrete update occurs (○ markers), and - x⁺(t), the value of x after the discrete update occurs (● markers).
Thus the value of the Context, which contains both time and state, advances from {t, x⁻(t)} to {t, x⁺(t)} as a result of the update. While those Context values are user-visible, the details of stepping here require an intermediate value which we’ll denote {t, x*(t)}.
Recall that Drake’s state x is partitioned into continuous, discrete, and abstract partitions xc, xd, and xa, so
x = { xc, xd, xa }
. Within a single step, these are updated in three stages: -Unrestricted update (can change x) -
Discrete update (can change only xd) -
Continuous update (changes t and xc)
Where needed, we extend the above notation to xc⁻, xa⁺, etc. to indicate the value of an individual partition at a particular stage of the stepping algorithm.
The following pseudocode uses the above notation to describe the algorithm “Step()” that the Simulator uses to incrementally advance the system trajectory (time t and state x). The Simulator’s AdvanceTo() method will be defined in terms of Step below. In general, the length of a step is not known a priori and is determined by the Step() algorithm. Each step consists of zero or more unrestricted updates, followed by zero or more discrete updates, followed by (possibly zero-length) continuous time and state advancement, followed by zero or more publishes, and then a call to the monitor() function if one has been defined. Updates, publishes, and the monitor can report errors or detect a termination condition; that is not shown in the pseudocode below.
The pseudocode will clarify the effects on time and state of each of the update stages above. This algorithm is given a starting Context value
{tₛ, x⁻(tₛ)}
and returns an end Context value{tₑ, x⁻(tₑ)}
, where tₑ is no later than a given tₘₐₓ.// Advance the trajectory (time and state) from start value {tₛ, x⁻(tₛ)} to an // end value {tₑ, x⁻(tₑ)}, where tₛ ≤ tₑ ≤ tₘₐₓ. procedure Step(tₛ, x⁻(tₛ), tₘₐₓ) // Update any variables (no restrictions). x*(tₛ) ← DoAnyUnrestrictedUpdates(tₛ, x⁻(tₛ)) // ---------------------------------- // Time and state are at {tₛ, x*(tₛ)} // ---------------------------------- // Update discrete variables. xd⁺(tₛ) ← DoAnyDiscreteUpdates(tₛ, x*(tₛ)) xc⁺(tₛ) ← xc*(tₛ) // These values carry over from x*(tₛ). xa⁺(tₛ) ← xa*(tₛ) // ---------------------------------- // Time and state are at {tₛ, x⁺(tₛ)} // ---------------------------------- // See how far it is safe to integrate without missing any events. tₑᵥₑₙₜ ← CalcNextEventTime(tₛ, x⁺(tₛ)) // Integrate continuous variables forward in time. Integration may terminate // before reaching tₛₜₒₚ due to witnessed events. tₛₜₒₚ ← min(tₑᵥₑₙₜ, tₘₐₓ) tₑ, xc⁻(tₑ) ← Integrate(tₛ, x⁺(tₛ), tₛₜₒₚ) xd⁻(tₑ) ← xd⁺(tₛ) // Discrete values are held from x⁺(tₛ). xa⁻(tₑ) ← xa⁺(tₛ) // ---------------------------------- // Time and state are at {tₑ, x⁻(tₑ)} // ---------------------------------- DoAnyPublishes(tₑ, x⁻(tₑ)) CallMonitor(tₑ, x⁻(tₑ)) return {tₑ, x⁻(tₑ)}
We can use the notation and pseudocode to flesh out the AdvanceTo(), AdvancePendingEvents(), and Initialize() functions. Termination and error conditions detected by event handlers or the monitor are reported as status returns from these methods.
// Advance the simulation until time tₘₐₓ. procedure AdvanceTo(tₘₐₓ) → status t ← current_time while t < tₘₐₓ {tₑ, x⁻(tₑ)} ← Step(t, x⁻(t), tₘₐₓ) {t, x⁻(t)} ← {tₑ, x⁻(tₑ)} endwhile // AdvancePendingEvents() is an advanced method, not commonly used. // Perform just the start-of-step update to advance from x⁻(t) to x⁺(t). procedure AdvancePendingEvents() → status t ≜ current_time, x⁻(t) ≜ current_state x⁺(t) ← DoAnyPendingUpdates(t, x⁻(t)) as in Step() x(t) ← x⁺(t) // No continuous update needed. DoAnyPublishes(t, x(t)) CallMonitor(t, x(t)) // Update time and state to {t₀, x⁻(t₀)}, which is the starting value of the // trajectory, and thus the value the Context should contain at the start of the // first simulation step. procedure Initialize(t₀, x₀) → status x⁺(t₀) ← DoAnyInitializationUpdates as in Step() x⁻(t₀) ← x⁺(t₀) // No continuous update needed. // ---------------------------------- // Time and state are at {t₀, x⁻(t₀)} // ---------------------------------- DoAnyPublishes(t₀, x⁻(t₀)) CallMonitor(t₀, x⁻(t₀))
Initialize() can be viewed as a “0ᵗʰ step” that occurs before the first Step() call as described above. Like Step(), Initialize() first performs pending updates (in this case only initialization events can be “pending”). Time doesn’t advance so there is no continuous update phase and witnesses cannot trigger. Finally, again like Step(), the initial trajectory point
{t₀, x⁻(t₀)}
is provided to the handlers for any triggered publish events. That includes initialization publish events, per-step publish events, and periodic or timed publish events that trigger at t₀, followed by a call to the monitor() function if one has been defined (a monitor is semantically identical to a per-step publish).-
__init__
(self: pydrake.systems.analysis.Simulator_[AutoDiffXd], system: pydrake.systems.framework.System_[AutoDiffXd], context: pydrake.systems.framework.Context_[AutoDiffXd] = None) → None¶ Create a Simulator that can advance a given System through time to produce a trajectory consisting of a sequence of Context values. The System must not have unresolved input ports if the values of those ports are necessary for computations performed during simulation (see class documentation).
The Simulator holds an internal, non-owned reference to the System object so you must ensure that
system
has a longer lifetime than the Simulator. It also owns a compatible Context internally that takes on each of the trajectory values. You may optionally provide a Context that will be used as the initial condition for the simulation; otherwise the Simulator will obtain a default Context fromsystem
.
-
AdvancePendingEvents
(self: pydrake.systems.analysis.Simulator_[AutoDiffXd]) → pydrake.systems.analysis.SimulatorStatus¶ (Advanced) Handles discrete and abstract state update events that are pending from the previous AdvanceTo() call, without advancing time. See the Simulator class description for details about how Simulator advances time and handles events. In the terminology used there, this method advances the internal Context from
{t, x⁻(t)}
to{t, x⁺(t)}
.Normally, these update events would be handled at the start of the next AdvanceTo() call, so this method is rarely needed. It can be useful at the end of a simulation or to get intermediate results when you are specifically interested in the
x⁺(t)
result.This method is equivalent to
AdvanceTo(current_time)
, wherecurrent_time=simulator.get_context().get_time())
. If there are no pending events, nothing happens except possibly a final per-step publish call (if enabled) followed by a call to the monitor() function (if one has been provided).- Returns
status
: - A SimulatorStatus object indicating success, termination, or an error condition as reported by event handlers or the monitor function.
See also
AdvanceTo(), Initialize(), SimulatorStatus
- Returns
-
AdvanceTo
(self: pydrake.systems.analysis.Simulator_[AutoDiffXd], boundary_time: pydrake.autodiffutils.AutoDiffXd) → pydrake.systems.analysis.SimulatorStatus¶ Advances the System’s trajectory until
boundary_time
is reached in the context or some other termination condition occurs. A variety ofRuntimeError
conditions are possible here, as well as error conditions that may be thrown by the System when it is asked to perform computations. Be sure to enclose your simulation in atry-catch
block and display thewhat()
message.We recommend that you call Initialize() prior to making the first call to AdvanceTo(). However, if you don’t it will be called for you the first time that you attempt a step, possibly resulting in unexpected error conditions. See documentation for
Initialize()
for the error conditions it might produce.Warning
You should consider calling Initialize() if you alter the the Context or Simulator options between successive AdvanceTo() calls. See Initialize() for more information.
- Parameter
boundary_time
: - The maximum time to which the trajectory will be advanced by this call to AdvanceTo(). The method may return earlier if an event or the monitor function requests termination or reports an error condition.
- Returns
status
: - A SimulatorStatus object indicating success, termination, or an error condition as reported by event handlers or the monitor function. The time in the context will be set either to the boundary_time or the time a termination or error was first detected.
- Precondition:
- The internal Context satisfies all System constraints or will after pending Context updates are performed.
See also
Initialize(), AdvancePendingEvents(), SimulatorStatus
- Parameter
-
clear_monitor
(self: pydrake.systems.analysis.Simulator_[AutoDiffXd]) → None¶ Removes the monitoring function if there is one.
See also
set_monitor()
-
get_actual_realtime_rate
(self: pydrake.systems.analysis.Simulator_[AutoDiffXd]) → float¶ Return the rate that simulated time has progressed relative to real time. A return of 1 means the simulation just matched real time, 2 means the simulation was twice as fast as real time, 0.5 means it was running in 2X slow motion, etc.
The value returned here is calculated as follows:
simulated_time_now - initial_simulated_time rate = ------------------------------------------- realtime_now - initial_realtime
The
initial
times are recorded when Initialize() or ResetStatistics() is called. The returned rate is undefined if Initialize() has not yet been called.Returns: The rate achieved since the last Initialize() or ResetStatistics() call. See also
set_target_realtime_rate()
-
get_context
(self: pydrake.systems.analysis.Simulator_[AutoDiffXd]) → pydrake.systems.framework.Context_[AutoDiffXd]¶ Returns a const reference to the internally-maintained Context holding the most recent step in the trajectory. This is suitable for publishing or extracting information about this trajectory step. Do not call this method if there is no Context.
-
get_integrator
(self: pydrake.systems.analysis.Simulator_[AutoDiffXd]) → pydrake.systems.analysis.IntegratorBase_[AutoDiffXd]¶ Gets a reference to the integrator used to advance the continuous aspects of the system.
-
get_monitor
(self: pydrake.systems.analysis.Simulator_[AutoDiffXd]) → Callable[[pydrake.systems.framework.Context_[AutoDiffXd]], pydrake.systems.framework.EventStatus]¶ Obtains a reference to the monitoring function, which may be empty.
See also
set_monitor()
-
get_mutable_context
(self: pydrake.systems.analysis.Simulator_[AutoDiffXd]) → pydrake.systems.framework.Context_[AutoDiffXd]¶ Returns a mutable reference to the internally-maintained Context holding the most recent step in the trajectory. This is suitable for use in updates, sampling operations, event handlers, and constraint projection. You can also modify this prior to calling Initialize() to set initial conditions. Do not call this method if there is no Context.
-
get_mutable_integrator
(self: pydrake.systems.analysis.Simulator_[AutoDiffXd]) → pydrake.systems.analysis.IntegratorBase_[AutoDiffXd]¶ Gets a reference to the mutable integrator used to advance the continuous state of the system.
-
get_system
(self: pydrake.systems.analysis.Simulator_[AutoDiffXd]) → pydrake.systems.framework.System_[AutoDiffXd]¶ Gets a constant reference to the system.
Note
a mutable reference is not available.
-
get_target_realtime_rate
(self: pydrake.systems.analysis.Simulator_[AutoDiffXd]) → float¶ Return the real time rate target currently in effect. The default is zero, meaning the Simulator runs as fast as possible. You can change the target with set_target_realtime_rate().
-
has_context
(self: pydrake.systems.analysis.Simulator_[AutoDiffXd]) → bool¶ Returns
True
if this Simulator has an internally-maintained Context. This is always true unlessreset_context()
has been called.
-
Initialize
(self: pydrake.systems.analysis.Simulator_[AutoDiffXd]) → pydrake.systems.analysis.SimulatorStatus¶ Prepares the Simulator for a simulation. In order, the sequence of actions taken here are: - The active integrator’s Initialize() method is invoked. - Statistics are reset. - Initialization update events are triggered and handled to produce the initial trajectory value
{t₀, x(t₀)}
. - Then that initial value is provided to the handlers for any publish events that have triggered, including initialization and per-step publish events, periodic or other time-triggered publish events that are scheduled for the initial time t₀, and finally a call to the monitor() function if one has been defined.See the class documentation for more information. We recommend calling Initialize() explicitly prior to beginning a simulation so that error conditions will be discovered early. However, Initialize() will be called automatically by the first AdvanceTo() call if it hasn’t already been called.
Note
If you make a change to the Context or to Simulator options between AdvanceTo() calls you should consider whether to call Initialize() before resuming; AdvanceTo() will not do that automatically for you. Whether to do so depends on whether you want the above initialization operations performed.
Note
In particular, if you changed the time you must call Initialize(). The time-triggered events must be recalculated in case one is due at the new starting time. Currently, the AdvanceTo() call will print a warning for the first violation; after 2020-12-01 the warning will become a hard error.
Warning
Initialize() does not automatically attempt to satisfy System constraints – it is up to you to make sure that constraints are satisfied by the initial conditions.
This method will throw
RuntimeError
if the combination of options doesn’t make sense. Other failures are possible from the System and integrator in use.- Returns
status
: - A SimulatorStatus object indicating success, termination, or an error condition as reported by event handlers or the monitor function.
See also
AdvanceTo(), AdvancePendingEvents(), SimulatorStatus
- Returns
-
reset_context
(self: pydrake.systems.analysis.Simulator_[AutoDiffXd], context: pydrake.systems.framework.Context_[AutoDiffXd]) → None¶ Replace the internally-maintained Context with a different one. The current Context is deleted. This is useful for supplying a new set of initial conditions. You should invoke Initialize() after replacing the Context.
- Parameter
context
: - The new context, which may be null. If the context is null, a new context must be set before attempting to step the system forward.
- Parameter
-
ResetStatistics
(self: pydrake.systems.analysis.Simulator_[AutoDiffXd]) → None¶ Forget accumulated statistics. Statistics are reset to the values they have post construction or immediately after
Initialize()
.
-
set_monitor
(self: pydrake.systems.analysis.Simulator_[AutoDiffXd], monitor: Callable[[pydrake.systems.framework.Context_[AutoDiffXd]], pydrake.systems.framework.EventStatus]) → None¶ Provides a monitoring function that will be invoked at the end of every step. (See the Simulator class documentation for a precise definition of “step”.) A monitor() function can be used to capture the trajectory, to terminate the simulation, or to detect error conditions. The monitor() function is invoked by the Simulator with a Context whose value is a point along the simulated trajectory. The monitor can be any functor and should capture any System references it needs to operate correctly.
A monitor() function behaves the same as would a per-step Publish event handler included in the top-level System or Diagram being simulated. As in the case of Publish(), the monitor is called at the end of every step taken internally by AdvanceTo(), and also at the end of Initialize() and AdvancePendingEvents(). (See the Simulator class documentation for more detail about what happens when in these methods.) The monitor receives the top-level (root) Context, from which any sub-Context can be obtained using
subsystem.GetMyContextFromRoot()
, provided the necessary subsystem reference has been captured for use in the monitor.** Examples Output time and continuous states whenever the trajectory is advanced:
simulator.set_monitor([](const Context<T>& root_context) { std::cout << root_context.get_time() << " " << root_context.get_continuous_state_vector() << std::endl; return EventStatus::Succeeded(); });
Terminate early but successfully on a condition in a subsystem of the System diagram being simulated:
simulator.set_monitor([&my_subsystem](const Context<T>& root_context) { const Context<T>& subcontext = my_subsystem.GetMyContextFromRoot(root_context); if (my_subsystem.GoalReached(subcontext)) { return EventStatus::ReachedTermination(my_subsystem, "Simulation achieved the desired goal."); } return EventStatus::Succeeded(); });
In the above case, the Simulator’s AdvanceTo() method will return early when the subsystem reports that it has reached its goal. The returned status will indicate the termination reason, and a human-readable termination message containing the message provided by the monitor can be obtained with status.FormatMessage().
Failure due to plant center of mass falling below a threshold:
simulator.set_monitor([&plant](const Context<T>& root_context) { const Context<T>& plant_context = plant.GetMyContextFromRoot(root_context); const Vector3<T> com = plant.CalcCenterOfMassPosition(plant_context); if (com[2] < 0.1) { // Check z height of com. return EventStatus::Failed(plant, "System fell over."); } return EventStatus::Succeeded(); });
In the above case the Simulator’s AdvanceTo() method will throw an RuntimeError containing a human-readable message including the text provided in the monitor.
Note
monitor() is called every time the trajectory is advanced by a step, which can mean it is called many times during a single AdvanceTo() call.
Note
The presence of a monitor has no effect on the step sizes taken, so a termination or error condition will be discovered only when first observed after a step is complete; it will not be further localized. Use witness-triggered events instead if you need precise isolation.
-
set_publish_every_time_step
(self: pydrake.systems.analysis.Simulator_[AutoDiffXd], arg0: bool) → None¶ Sets whether the simulation should trigger a forced-Publish event on the System under simulation at the end of every trajectory-advancing step. Specifically, that means the System::Publish() event dispatcher will be invoked on each subsystem of the System and passed the current Context and a forced-publish Event. If a subsystem has declared a forced-publish event handler, that will be called. Otherwise, nothing will happen unless the DoPublish() dispatcher has been overridden.
Enabling this option does not cause a forced-publish to be triggered at initialization; if you want that you should also call
set_publish_at_initialization(true)
. If you want a forced-publish at the end of every step, you will usually also want one at the end of initialization, requiring both options to be enabled.See also
LeafSystem::DeclareForcedPublishEvent()
-
set_target_realtime_rate
(self: pydrake.systems.analysis.Simulator_[AutoDiffXd], arg0: float) → None¶ Slow the simulation down to approximately synchronize with real time when it would otherwise run too fast. Normally the Simulator takes steps as quickly as it can. You can request that it slow down to synchronize with real time by providing a realtime rate greater than zero here.
Warning
No guarantees can be made about how accurately the simulation can be made to track real time, even if computation is fast enough. That’s because the system utilities used to implement this do not themselves provide such guarantees. So this is likely to work nicely for visualization purposes where human perception is the only concern. For any other uses you should consider whether approximate real time is adequate for your purposes.
Note
If the full-speed simulation is already slower than real time you can’t speed it up with this call! Instead consider requesting less integration accuracy, using a faster integration method or fixed time step, or using a simpler model.
- Parameter
realtime_rate
: - Desired rate relative to real time. Set to 1 to track real time, 2 to run twice as fast as real time, 0.5 for half speed, etc. Zero or negative restores the rate to its default of 0, meaning the simulation will proceed as fast as possible.
- Parameter
-
-
class
pydrake.systems.analysis.
Simulator_[float]
¶ A class for advancing the state of hybrid dynamic systems, represented by
System<T>
objects, forward in time. Starting with an initial Context for a given System, Simulator advances time and produces a series of Context values that forms a trajectory satisfying the system’s dynamic equations to a specified accuracy. Only the Context is modified by a Simulator; the System is const.A Drake System is a continuous/discrete/hybrid dynamic system where the continuous part is a DAE, that is, it is expected to consist of a set of differential equations and bilateral algebraic constraints. The set of active constraints may change as a result of particular events, such as contact.
Given a current Context, we expect a System to provide us with - derivatives for the continuous differential equations that already satisfy the differentiated form of the constraints (typically, acceleration constraints), - a projection method for least-squares correction of violated higher-level constraints (position and velocity level), - a time-of-next-update method that can be used to adjust the integrator step size in preparation for a discrete update, - methods that can update discrete variables when their update time is reached, - witness (guard) functions for event isolation, - event handlers (reset functions) for making appropriate changes to state and mode variables when an event has been isolated.
The continuous parts of the trajectory are advanced using a numerical integrator. Different integrators have different properties; you can choose the one that is most appropriate for your application or use the default which is adequate for most systems.
<h3>How the simulation is stepped: simulation mechanics for authors of discrete and hybrid systems</h3>
This section is targeted toward users who have created a LeafSystem implementing a discrete or hybrid system. For authors of such systems, it can be useful to understand the simulation details in order to attain the desired state behavior over time. This behavior is dependent on the ordering in which discrete events and continuous updates are processed. (By “discrete events” we mean to include any of Drake’s event handlers.) The basic issues and terminology are introduced in the discrete_systems module; please look there first before proceeding.
As pictured in discrete_systems, when a continuous-time system has discrete events, the state x can have two significant values at the event time t. These are - x⁻(t), the value of x before the discrete update occurs (○ markers), and - x⁺(t), the value of x after the discrete update occurs (● markers).
Thus the value of the Context, which contains both time and state, advances from {t, x⁻(t)} to {t, x⁺(t)} as a result of the update. While those Context values are user-visible, the details of stepping here require an intermediate value which we’ll denote {t, x*(t)}.
Recall that Drake’s state x is partitioned into continuous, discrete, and abstract partitions xc, xd, and xa, so
x = { xc, xd, xa }
. Within a single step, these are updated in three stages: -Unrestricted update (can change x) -
Discrete update (can change only xd) -
Continuous update (changes t and xc)
Where needed, we extend the above notation to xc⁻, xa⁺, etc. to indicate the value of an individual partition at a particular stage of the stepping algorithm.
The following pseudocode uses the above notation to describe the algorithm “Step()” that the Simulator uses to incrementally advance the system trajectory (time t and state x). The Simulator’s AdvanceTo() method will be defined in terms of Step below. In general, the length of a step is not known a priori and is determined by the Step() algorithm. Each step consists of zero or more unrestricted updates, followed by zero or more discrete updates, followed by (possibly zero-length) continuous time and state advancement, followed by zero or more publishes, and then a call to the monitor() function if one has been defined. Updates, publishes, and the monitor can report errors or detect a termination condition; that is not shown in the pseudocode below.
The pseudocode will clarify the effects on time and state of each of the update stages above. This algorithm is given a starting Context value
{tₛ, x⁻(tₛ)}
and returns an end Context value{tₑ, x⁻(tₑ)}
, where tₑ is no later than a given tₘₐₓ.// Advance the trajectory (time and state) from start value {tₛ, x⁻(tₛ)} to an // end value {tₑ, x⁻(tₑ)}, where tₛ ≤ tₑ ≤ tₘₐₓ. procedure Step(tₛ, x⁻(tₛ), tₘₐₓ) // Update any variables (no restrictions). x*(tₛ) ← DoAnyUnrestrictedUpdates(tₛ, x⁻(tₛ)) // ---------------------------------- // Time and state are at {tₛ, x*(tₛ)} // ---------------------------------- // Update discrete variables. xd⁺(tₛ) ← DoAnyDiscreteUpdates(tₛ, x*(tₛ)) xc⁺(tₛ) ← xc*(tₛ) // These values carry over from x*(tₛ). xa⁺(tₛ) ← xa*(tₛ) // ---------------------------------- // Time and state are at {tₛ, x⁺(tₛ)} // ---------------------------------- // See how far it is safe to integrate without missing any events. tₑᵥₑₙₜ ← CalcNextEventTime(tₛ, x⁺(tₛ)) // Integrate continuous variables forward in time. Integration may terminate // before reaching tₛₜₒₚ due to witnessed events. tₛₜₒₚ ← min(tₑᵥₑₙₜ, tₘₐₓ) tₑ, xc⁻(tₑ) ← Integrate(tₛ, x⁺(tₛ), tₛₜₒₚ) xd⁻(tₑ) ← xd⁺(tₛ) // Discrete values are held from x⁺(tₛ). xa⁻(tₑ) ← xa⁺(tₛ) // ---------------------------------- // Time and state are at {tₑ, x⁻(tₑ)} // ---------------------------------- DoAnyPublishes(tₑ, x⁻(tₑ)) CallMonitor(tₑ, x⁻(tₑ)) return {tₑ, x⁻(tₑ)}
We can use the notation and pseudocode to flesh out the AdvanceTo(), AdvancePendingEvents(), and Initialize() functions. Termination and error conditions detected by event handlers or the monitor are reported as status returns from these methods.
// Advance the simulation until time tₘₐₓ. procedure AdvanceTo(tₘₐₓ) → status t ← current_time while t < tₘₐₓ {tₑ, x⁻(tₑ)} ← Step(t, x⁻(t), tₘₐₓ) {t, x⁻(t)} ← {tₑ, x⁻(tₑ)} endwhile // AdvancePendingEvents() is an advanced method, not commonly used. // Perform just the start-of-step update to advance from x⁻(t) to x⁺(t). procedure AdvancePendingEvents() → status t ≜ current_time, x⁻(t) ≜ current_state x⁺(t) ← DoAnyPendingUpdates(t, x⁻(t)) as in Step() x(t) ← x⁺(t) // No continuous update needed. DoAnyPublishes(t, x(t)) CallMonitor(t, x(t)) // Update time and state to {t₀, x⁻(t₀)}, which is the starting value of the // trajectory, and thus the value the Context should contain at the start of the // first simulation step. procedure Initialize(t₀, x₀) → status x⁺(t₀) ← DoAnyInitializationUpdates as in Step() x⁻(t₀) ← x⁺(t₀) // No continuous update needed. // ---------------------------------- // Time and state are at {t₀, x⁻(t₀)} // ---------------------------------- DoAnyPublishes(t₀, x⁻(t₀)) CallMonitor(t₀, x⁻(t₀))
Initialize() can be viewed as a “0ᵗʰ step” that occurs before the first Step() call as described above. Like Step(), Initialize() first performs pending updates (in this case only initialization events can be “pending”). Time doesn’t advance so there is no continuous update phase and witnesses cannot trigger. Finally, again like Step(), the initial trajectory point
{t₀, x⁻(t₀)}
is provided to the handlers for any triggered publish events. That includes initialization publish events, per-step publish events, and periodic or timed publish events that trigger at t₀, followed by a call to the monitor() function if one has been defined (a monitor is semantically identical to a per-step publish).-
__init__
(self: pydrake.systems.analysis.Simulator_[float], system: pydrake.systems.framework.System_[float], context: pydrake.systems.framework.Context_[float] = None) → None¶ Create a Simulator that can advance a given System through time to produce a trajectory consisting of a sequence of Context values. The System must not have unresolved input ports if the values of those ports are necessary for computations performed during simulation (see class documentation).
The Simulator holds an internal, non-owned reference to the System object so you must ensure that
system
has a longer lifetime than the Simulator. It also owns a compatible Context internally that takes on each of the trajectory values. You may optionally provide a Context that will be used as the initial condition for the simulation; otherwise the Simulator will obtain a default Context fromsystem
.
-
AdvancePendingEvents
(self: pydrake.systems.analysis.Simulator_[float]) → pydrake.systems.analysis.SimulatorStatus¶ (Advanced) Handles discrete and abstract state update events that are pending from the previous AdvanceTo() call, without advancing time. See the Simulator class description for details about how Simulator advances time and handles events. In the terminology used there, this method advances the internal Context from
{t, x⁻(t)}
to{t, x⁺(t)}
.Normally, these update events would be handled at the start of the next AdvanceTo() call, so this method is rarely needed. It can be useful at the end of a simulation or to get intermediate results when you are specifically interested in the
x⁺(t)
result.This method is equivalent to
AdvanceTo(current_time)
, wherecurrent_time=simulator.get_context().get_time())
. If there are no pending events, nothing happens except possibly a final per-step publish call (if enabled) followed by a call to the monitor() function (if one has been provided).- Returns
status
: - A SimulatorStatus object indicating success, termination, or an error condition as reported by event handlers or the monitor function.
See also
AdvanceTo(), Initialize(), SimulatorStatus
- Returns
-
AdvanceTo
(self: pydrake.systems.analysis.Simulator_[float], boundary_time: float) → pydrake.systems.analysis.SimulatorStatus¶ Advances the System’s trajectory until
boundary_time
is reached in the context or some other termination condition occurs. A variety ofRuntimeError
conditions are possible here, as well as error conditions that may be thrown by the System when it is asked to perform computations. Be sure to enclose your simulation in atry-catch
block and display thewhat()
message.We recommend that you call Initialize() prior to making the first call to AdvanceTo(). However, if you don’t it will be called for you the first time that you attempt a step, possibly resulting in unexpected error conditions. See documentation for
Initialize()
for the error conditions it might produce.Warning
You should consider calling Initialize() if you alter the the Context or Simulator options between successive AdvanceTo() calls. See Initialize() for more information.
- Parameter
boundary_time
: - The maximum time to which the trajectory will be advanced by this call to AdvanceTo(). The method may return earlier if an event or the monitor function requests termination or reports an error condition.
- Returns
status
: - A SimulatorStatus object indicating success, termination, or an error condition as reported by event handlers or the monitor function. The time in the context will be set either to the boundary_time or the time a termination or error was first detected.
- Precondition:
- The internal Context satisfies all System constraints or will after pending Context updates are performed.
See also
Initialize(), AdvancePendingEvents(), SimulatorStatus
- Parameter
-
clear_monitor
(self: pydrake.systems.analysis.Simulator_[float]) → None¶ Removes the monitoring function if there is one.
See also
set_monitor()
-
get_actual_realtime_rate
(self: pydrake.systems.analysis.Simulator_[float]) → float¶ Return the rate that simulated time has progressed relative to real time. A return of 1 means the simulation just matched real time, 2 means the simulation was twice as fast as real time, 0.5 means it was running in 2X slow motion, etc.
The value returned here is calculated as follows:
simulated_time_now - initial_simulated_time rate = ------------------------------------------- realtime_now - initial_realtime
The
initial
times are recorded when Initialize() or ResetStatistics() is called. The returned rate is undefined if Initialize() has not yet been called.Returns: The rate achieved since the last Initialize() or ResetStatistics() call. See also
set_target_realtime_rate()
-
get_context
(self: pydrake.systems.analysis.Simulator_[float]) → pydrake.systems.framework.Context_[float]¶ Returns a const reference to the internally-maintained Context holding the most recent step in the trajectory. This is suitable for publishing or extracting information about this trajectory step. Do not call this method if there is no Context.
-
get_integrator
(self: pydrake.systems.analysis.Simulator_[float]) → pydrake.systems.analysis.IntegratorBase_[float]¶ Gets a reference to the integrator used to advance the continuous aspects of the system.
-
get_monitor
(self: pydrake.systems.analysis.Simulator_[float]) → Callable[[pydrake.systems.framework.Context_[float]], pydrake.systems.framework.EventStatus]¶ Obtains a reference to the monitoring function, which may be empty.
See also
set_monitor()
-
get_mutable_context
(self: pydrake.systems.analysis.Simulator_[float]) → pydrake.systems.framework.Context_[float]¶ Returns a mutable reference to the internally-maintained Context holding the most recent step in the trajectory. This is suitable for use in updates, sampling operations, event handlers, and constraint projection. You can also modify this prior to calling Initialize() to set initial conditions. Do not call this method if there is no Context.
-
get_mutable_integrator
(self: pydrake.systems.analysis.Simulator_[float]) → pydrake.systems.analysis.IntegratorBase_[float]¶ Gets a reference to the mutable integrator used to advance the continuous state of the system.
-
get_system
(self: pydrake.systems.analysis.Simulator_[float]) → pydrake.systems.framework.System_[float]¶ Gets a constant reference to the system.
Note
a mutable reference is not available.
-
get_target_realtime_rate
(self: pydrake.systems.analysis.Simulator_[float]) → float¶ Return the real time rate target currently in effect. The default is zero, meaning the Simulator runs as fast as possible. You can change the target with set_target_realtime_rate().
-
has_context
(self: pydrake.systems.analysis.Simulator_[float]) → bool¶ Returns
True
if this Simulator has an internally-maintained Context. This is always true unlessreset_context()
has been called.
-
Initialize
(self: pydrake.systems.analysis.Simulator_[float]) → pydrake.systems.analysis.SimulatorStatus¶ Prepares the Simulator for a simulation. In order, the sequence of actions taken here are: - The active integrator’s Initialize() method is invoked. - Statistics are reset. - Initialization update events are triggered and handled to produce the initial trajectory value
{t₀, x(t₀)}
. - Then that initial value is provided to the handlers for any publish events that have triggered, including initialization and per-step publish events, periodic or other time-triggered publish events that are scheduled for the initial time t₀, and finally a call to the monitor() function if one has been defined.See the class documentation for more information. We recommend calling Initialize() explicitly prior to beginning a simulation so that error conditions will be discovered early. However, Initialize() will be called automatically by the first AdvanceTo() call if it hasn’t already been called.
Note
If you make a change to the Context or to Simulator options between AdvanceTo() calls you should consider whether to call Initialize() before resuming; AdvanceTo() will not do that automatically for you. Whether to do so depends on whether you want the above initialization operations performed.
Note
In particular, if you changed the time you must call Initialize(). The time-triggered events must be recalculated in case one is due at the new starting time. Currently, the AdvanceTo() call will print a warning for the first violation; after 2020-12-01 the warning will become a hard error.
Warning
Initialize() does not automatically attempt to satisfy System constraints – it is up to you to make sure that constraints are satisfied by the initial conditions.
This method will throw
RuntimeError
if the combination of options doesn’t make sense. Other failures are possible from the System and integrator in use.- Returns
status
: - A SimulatorStatus object indicating success, termination, or an error condition as reported by event handlers or the monitor function.
See also
AdvanceTo(), AdvancePendingEvents(), SimulatorStatus
- Returns
-
reset_context
(self: pydrake.systems.analysis.Simulator_[float], context: pydrake.systems.framework.Context_[float]) → None¶ Replace the internally-maintained Context with a different one. The current Context is deleted. This is useful for supplying a new set of initial conditions. You should invoke Initialize() after replacing the Context.
- Parameter
context
: - The new context, which may be null. If the context is null, a new context must be set before attempting to step the system forward.
- Parameter
-
ResetStatistics
(self: pydrake.systems.analysis.Simulator_[float]) → None¶ Forget accumulated statistics. Statistics are reset to the values they have post construction or immediately after
Initialize()
.
-
set_monitor
(self: pydrake.systems.analysis.Simulator_[float], monitor: Callable[[pydrake.systems.framework.Context_[float]], pydrake.systems.framework.EventStatus]) → None¶ Provides a monitoring function that will be invoked at the end of every step. (See the Simulator class documentation for a precise definition of “step”.) A monitor() function can be used to capture the trajectory, to terminate the simulation, or to detect error conditions. The monitor() function is invoked by the Simulator with a Context whose value is a point along the simulated trajectory. The monitor can be any functor and should capture any System references it needs to operate correctly.
A monitor() function behaves the same as would a per-step Publish event handler included in the top-level System or Diagram being simulated. As in the case of Publish(), the monitor is called at the end of every step taken internally by AdvanceTo(), and also at the end of Initialize() and AdvancePendingEvents(). (See the Simulator class documentation for more detail about what happens when in these methods.) The monitor receives the top-level (root) Context, from which any sub-Context can be obtained using
subsystem.GetMyContextFromRoot()
, provided the necessary subsystem reference has been captured for use in the monitor.** Examples Output time and continuous states whenever the trajectory is advanced:
simulator.set_monitor([](const Context<T>& root_context) { std::cout << root_context.get_time() << " " << root_context.get_continuous_state_vector() << std::endl; return EventStatus::Succeeded(); });
Terminate early but successfully on a condition in a subsystem of the System diagram being simulated:
simulator.set_monitor([&my_subsystem](const Context<T>& root_context) { const Context<T>& subcontext = my_subsystem.GetMyContextFromRoot(root_context); if (my_subsystem.GoalReached(subcontext)) { return EventStatus::ReachedTermination(my_subsystem, "Simulation achieved the desired goal."); } return EventStatus::Succeeded(); });
In the above case, the Simulator’s AdvanceTo() method will return early when the subsystem reports that it has reached its goal. The returned status will indicate the termination reason, and a human-readable termination message containing the message provided by the monitor can be obtained with status.FormatMessage().
Failure due to plant center of mass falling below a threshold:
simulator.set_monitor([&plant](const Context<T>& root_context) { const Context<T>& plant_context = plant.GetMyContextFromRoot(root_context); const Vector3<T> com = plant.CalcCenterOfMassPosition(plant_context); if (com[2] < 0.1) { // Check z height of com. return EventStatus::Failed(plant, "System fell over."); } return EventStatus::Succeeded(); });
In the above case the Simulator’s AdvanceTo() method will throw an RuntimeError containing a human-readable message including the text provided in the monitor.
Note
monitor() is called every time the trajectory is advanced by a step, which can mean it is called many times during a single AdvanceTo() call.
Note
The presence of a monitor has no effect on the step sizes taken, so a termination or error condition will be discovered only when first observed after a step is complete; it will not be further localized. Use witness-triggered events instead if you need precise isolation.
-
set_publish_every_time_step
(self: pydrake.systems.analysis.Simulator_[float], arg0: bool) → None¶ Sets whether the simulation should trigger a forced-Publish event on the System under simulation at the end of every trajectory-advancing step. Specifically, that means the System::Publish() event dispatcher will be invoked on each subsystem of the System and passed the current Context and a forced-publish Event. If a subsystem has declared a forced-publish event handler, that will be called. Otherwise, nothing will happen unless the DoPublish() dispatcher has been overridden.
Enabling this option does not cause a forced-publish to be triggered at initialization; if you want that you should also call
set_publish_at_initialization(true)
. If you want a forced-publish at the end of every step, you will usually also want one at the end of initialization, requiring both options to be enabled.See also
LeafSystem::DeclareForcedPublishEvent()
-
set_target_realtime_rate
(self: pydrake.systems.analysis.Simulator_[float], arg0: float) → None¶ Slow the simulation down to approximately synchronize with real time when it would otherwise run too fast. Normally the Simulator takes steps as quickly as it can. You can request that it slow down to synchronize with real time by providing a realtime rate greater than zero here.
Warning
No guarantees can be made about how accurately the simulation can be made to track real time, even if computation is fast enough. That’s because the system utilities used to implement this do not themselves provide such guarantees. So this is likely to work nicely for visualization purposes where human perception is the only concern. For any other uses you should consider whether approximate real time is adequate for your purposes.
Note
If the full-speed simulation is already slower than real time you can’t speed it up with this call! Instead consider requesting less integration accuracy, using a faster integration method or fixed time step, or using a simpler model.
- Parameter
realtime_rate
: - Desired rate relative to real time. Set to 1 to track real time, 2 to run twice as fast as real time, 0.5 for half speed, etc. Zero or negative restores the rate to its default of 0, meaning the simulation will proceed as fast as possible.
- Parameter
-
-
class
pydrake.systems.analysis.
SimulatorStatus
¶ Holds the status return value from a call to Simulator::AdvanceTo() and related methods. The argument t to AdvanceTo(t) is called the boundary time, and represents the maximum time to which the simulation trajectory will be advanced by a call to AdvanceTo(). (For methods that don’t advance time, the current time is considered to be the boundary time.) A normal, successful return means that simulated time advanced successfully to the boundary time, without encountering a termination condition or error condition. AdvanceTo() may return earlier than the boundary time if one of those conditions is encountered. In that case the return object holds a reference to the subsystem that detected the condition and a human-friendly message from that subsystem that hopefully explains what happened.
-
__init__
¶ Initialize self. See help(type(self)) for accurate signature.
-
boundary_time
(self: pydrake.systems.analysis.SimulatorStatus) → float¶ Returns the maximum time we could have reached with this call; whether we actually got there depends on the status. This is the time supplied in an AdvanceTo() call or the current time for methods that don’t advance time, that is, Initialize() and AdvancePendingEvents().
-
FormatMessage
(self: pydrake.systems.analysis.SimulatorStatus) → str¶ Returns a human-readable message explaining the return result.
-
IsIdenticalStatus
(self: pydrake.systems.analysis.SimulatorStatus, other: pydrake.systems.analysis.SimulatorStatus) → bool¶ Returns true if the
other
status contains exactly the same information asthis
status. This is likely only useful for unit testing of SimulatorStatus.
-
message
(self: pydrake.systems.analysis.SimulatorStatus) → str¶ For termination or error conditions, returns a human-readable message explaining what happened. This is the message from the subsystem that detected the condition. FormatMessage() returns additional information and also includes this message.
-
reason
(self: pydrake.systems.analysis.SimulatorStatus) → pydrake.systems.analysis.SimulatorStatus.ReturnReason¶ Returns the reason that a Simulator call returned.
-
return_time
(self: pydrake.systems.analysis.SimulatorStatus) → float¶ Returns the time that was actually reached. This will be boundary_time() if succeeded() returns true. Otherwise it is the time at which a termination or error condition was detected and may be earlier than boundary_time().
-
class
ReturnReason
¶ Members:
kReachedBoundaryTime : This is the normal return: no termination or error condition wasencountered before reaching the boundary time. There is no message and no saved System.
kReachedTerminationCondition : An event handler or monitor function returned with a “reachedtermination condition” EventStatus (has message with details). For AdvanceTo() the return time may be earlier than the boundary time.
kEventHandlerFailed : An event handler or monitor function returned with a “failed”EventStatus (has message with details). For AdvanceTo() the return time may be earlier than the boundary time.
-
__init__
(self: pydrake.systems.analysis.SimulatorStatus.ReturnReason, arg0: int) → None¶
-
kEventHandlerFailed
= ReturnReason.kEventHandlerFailed¶
-
kReachedBoundaryTime
= ReturnReason.kReachedBoundaryTime¶
-
kReachedTerminationCondition
= ReturnReason.kReachedTerminationCondition¶
-
name
¶
-
-
succeeded
(self: pydrake.systems.analysis.SimulatorStatus) → bool¶ Returns true if we reached the boundary time with no surprises.
-
system
(self: pydrake.systems.analysis.SimulatorStatus) → pydrake.systems.framework.SystemBase¶ Optionally, returns the subsystem to which the status and contained message should be attributed. May be nullptr in which case the status should be attributed to the System as a whole.
-