Skip to content

Commit

Permalink
integrators: add python bindings for dense integration
Browse files Browse the repository at this point in the history
  • Loading branch information
RussTedrake committed Apr 11, 2020
1 parent d5019f2 commit bf5c7d4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions bindings/pydrake/systems/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ drake_py_unittest(
":analysis_py",
":framework_py",
":primitives_py",
"//bindings/pydrake:trajectories_py",
],
)

Expand Down
10 changes: 9 additions & 1 deletion bindings/pydrake/systems/analysis_py.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,15 @@ PYBIND11_MODULE(analysis, m) {
doc.IntegratorBase.set_throw_on_minimum_step_size_violation.doc)
.def("get_throw_on_minimum_step_size_violation",
&IntegratorBase<T>::get_throw_on_minimum_step_size_violation,
doc.IntegratorBase.get_throw_on_minimum_step_size_violation.doc);
doc.IntegratorBase.get_throw_on_minimum_step_size_violation.doc)
.def("StartDenseIntegration", &IntegratorBase<T>::StartDenseIntegration,
doc.IntegratorBase.StartDenseIntegration.doc)
.def("get_dense_output", &IntegratorBase<T>::get_dense_output,
py_reference_internal, doc.IntegratorBase.get_dense_output.doc)
.def("StopDenseIntegration", &IntegratorBase<T>::StopDenseIntegration,
// Keep alive, ownership (tr.): `return` keeps `self` alive.
py::keep_alive<1, 0>(),
doc.IntegratorBase.StopDenseIntegration.doc);

DefineTemplateClassWithDefault<RungeKutta2Integrator<T>, IntegratorBase<T>>(
m, "RungeKutta2Integrator", GetPyParam<T>(),
Expand Down
22 changes: 21 additions & 1 deletion bindings/pydrake/systems/test/analysis_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
from pydrake.systems.analysis import (
RungeKutta2Integrator_,
RegionOfAttraction,
RegionOfAttractionOptions
RegionOfAttractionOptions,
Simulator
)
from pydrake.trajectories import PiecewisePolynomial


class AnalysisTest(unittest.TestCase):
Expand All @@ -29,3 +31,21 @@ def test_symbolic_integrators(self):

max_h = 0.1
RungeKutta2Integrator_[Expression](sys, max_h, context)

def test_dense_integration(self):
x = Variable("x")
sys = SymbolicVectorSystem(state=[x], dynamics=[-x+x**3])
simulator = Simulator(sys)
integrator = simulator.get_mutable_integrator()
self.assertIsNone(integrator.get_dense_output())
integrator.StartDenseIntegration()
# If I include the following line, then I get a RuntimeError
# on StopDenseIntegration()
# a = integrator.get_dense_output()
self.assertIsInstance(integrator.get_dense_output(),
PiecewisePolynomial)
simulator.AdvanceTo(1.0)
pp = integrator.StopDenseIntegration()
self.assertEqual(pp.start_time(), 0.0)
self.assertEqual(pp.end_time(), 1.0)
self.assertIsNone(integrator.get_dense_output())

0 comments on commit bf5c7d4

Please sign in to comment.