def test_simulator_ctor(self): # Create simple system. system = ConstantVectorSource([1]) def check_output(context): # Check number of output ports and value for a given context. output = system.AllocateOutput(context) self.assertEquals(output.get_num_ports(), 1) system.CalcOutput(context, output) value = output.get_vector_data(0).get_value() self.assertTrue(np.allclose([1], value)) # Create simulator with basic constructor. simulator = Simulator(system) simulator.Initialize() simulator.set_target_realtime_rate(0) simulator.set_publish_every_time_step(True) self.assertTrue( simulator.get_context() is simulator.get_mutable_context()) check_output(simulator.get_context()) simulator.StepTo(1) # Create simulator specifying context. context = system.CreateDefaultContext() # @note `simulator` now owns `context`. simulator = Simulator(system, context) self.assertTrue(simulator.get_context() is context) check_output(context) simulator.StepTo(1)
def test_integrator_constructors(self): """Test all constructors for all integrator types.""" sys = ConstantVectorSource([1]) con = sys.CreateDefaultContext() RungeKutta2Integrator(system=sys, max_step_size=0.01) RungeKutta2Integrator(system=sys, max_step_size=0.01, context=con) RungeKutta3Integrator(system=sys) RungeKutta3Integrator(system=sys, context=con)
def test_copy(self): # Copy a context using `copy` or `clone`. system = ConstantVectorSource([1]) context = system.CreateDefaultContext() context_2 = copy.copy(context) self.assertNotEquals(context, context_2) context_3 = context.Clone() self.assertNotEquals(context, context_3)
def test_copy(self): # Copy a context using `deepcopy` or `clone`. system = ConstantVectorSource([1]) context = system.CreateDefaultContext() context_copies = [ copy.copy(context), copy.deepcopy(context), context.Clone(), ] # TODO(eric.cousineau): Compare copies. for context_copy in context_copies: self.assertTrue(context_copy is not context)
def test_simulator_context_manipulation(self): system = ConstantVectorSource([1]) # Use default-constructed context. simulator = Simulator(system) self.assertTrue(simulator.has_context()) context_default = simulator.get_mutable_context() # WARNING: Once we call `simulator.reset_context()`, it will delete the # context it currently owns, which is `context_default` in this case. # BE CAREFUL IN SITUATIONS LIKE THIS! # TODO(eric.cousineau): Bind `release_context()`, or migrate context # usage to use `shared_ptr`. context = system.CreateDefaultContext() simulator.reset_context(context) self.assertIs(context, simulator.get_mutable_context()) # WARNING: This will also invalidate `context`. Be careful! simulator.reset_context(None) self.assertFalse(simulator.has_context())
def test_vector_output_port_eval(self): np_value = np.array([1., 2., 3.]) model_value = AbstractValue.Make(BasicVector(np_value)) source = ConstantVectorSource(np_value) context = source.CreateDefaultContext() output_port = source.get_output_port(0) value = output_port.Eval(context) self.assertEqual(type(value), np.ndarray) np.testing.assert_equal(value, np_value) value_abs = output_port.EvalAbstract(context) self.assertEqual(type(value_abs), type(model_value)) self.assertEqual(type(value_abs.get_value().get_value()), np.ndarray) np.testing.assert_equal(value_abs.get_value().get_value(), np_value) basic = output_port.EvalBasicVector(context) self.assertEqual(type(basic), BasicVector) self.assertEqual(type(basic.get_value()), np.ndarray) np.testing.assert_equal(basic.get_value(), np_value)
def test_constant_vector_source(self): source = ConstantVectorSource(source_value=[1., 2.]) context = source.CreateDefaultContext() source.get_source_value(context) source.get_mutable_source_value(context)