def as_proto(self) -> protos.EngineeringState: """Returns a deep copy of this EngineeringState as a protobuf.""" constructed_protobuf = protos.EngineeringState() constructed_protobuf.CopyFrom(self._proto_state) for component_data, component in zip(self.components, constructed_protobuf.components): (component.connected, component.temperature, component.resistance, component.voltage, component.current, component.coolant_hab_one, component.coolant_hab_two, component.coolant_ayse) = (component_data.connected, component_data.temperature, component_data.resistance, component_data.voltage, component_data.current, component_data.coolant_hab_one, component_data.coolant_hab_two, component_data.coolant_ayse) for coolant_data, coolant in zip(self.coolant_loops, constructed_protobuf.coolant_loops): (coolant.coolant_temp, coolant.primary_pump_on, coolant.secondary_pump_on) = (coolant_data.coolant_temp, coolant_data.primary_pump_on, coolant_data.secondary_pump_on) for radiator_data, radiator in zip(self.radiators, constructed_protobuf.radiators): ( radiator.attached_to_coolant_loop, radiator.functioning, ) = ( radiator_data.attached_to_coolant_loop, radiator_data.functioning, ) return constructed_protobuf
class PhysicsStateTestCase(unittest.TestCase): """Tests state.PhysicsState accessors and setters.""" proto_state = protos.PhysicalState( timestamp=5, entities=[ protos.Entity(name='First', mass=100, r=200, x=10, y=20, vx=30, vy=40, heading=7, spin=50, fuel=60, throttle=70), protos.Entity(name='Second', mass=101, r=201, artificial=True, x=11, y=21, vx=31, vy=41, heading=2, spin=51, fuel=61, throttle=71, landed_on='First', broken=True) ], engineering=protos.EngineeringState( components=[protos.EngineeringState.Component()] * _N_COMPONENTS, coolant_loops=[protos.EngineeringState.CoolantLoop()] * _N_COOLANT_LOOPS, radiators=[protos.EngineeringState.Radiator()] * _N_RADIATORS)) def test_landed_on(self): """Test that the special .landed_on field is properly set.""" ps = PhysicsState(None, self.proto_state) self.assertEqual(ps['First'].landed_on, '') self.assertEqual(ps['Second'].landed_on, 'First') def test_y_vector_init(self): """Test that initializing with a y-vector uses y-vector values.""" y0 = np.concatenate(( np.array([ 10, 20, # x 30, 40, # y 50, 60, # vx 0, 0, # vy 0, 0, # heading 70, 80, # spin 90, 100, # fuel 0, 0, # throttle 1, -1, # only First is landed on Second 0, 1, # Second is broken common.SRB_EMPTY, 1 # time_acc ]), np.zeros(EngineeringState.N_ENGINEERING_FIELDS))) ps = PhysicsState(y0, self.proto_state) self.assertTrue(np.array_equal(ps.y0(), y0.astype(ps.y0().dtype))) self.assertEqual(ps['First'].landed_on, 'Second') proto_state = ps.as_proto() proto_state.timestamp = 50 self.assertEqual(proto_state.timestamp, 50) self.assertEqual(proto_state.entities[0].fuel, 90) self.assertTrue(proto_state.entities[1].broken) def test_get_set(self): """Test __getitem__ and __setitem__.""" ps = PhysicsState(None, self.proto_state) entity = ps[0] entity.landed_on = 'Second' ps[0] = entity self.assertEqual(ps[0].landed_on, 'Second') def test_entity_view(self): """Test that setting and getting _EntityView attrs propagate.""" ps = PhysicsState(None, self.proto_state) self.assertEqual(ps[0].name, 'First') entity = ps[0] self.assertTrue(isinstance(entity, _EntityView)) self.assertEqual(entity.x, 10) self.assertEqual(entity.y, 20) self.assertEqual(entity.vx, 30) self.assertEqual(entity.vy, 40) self.assertEqual(entity.spin, 50) self.assertEqual(entity.fuel, 60) self.assertEqual(entity.landed_on, '') self.assertEqual(entity.throttle, 70) ps.y0() self.assertEqual(entity.heading, 7 % (2 * np.pi)) ps[0].landed_on = 'Second' self.assertEqual(entity.landed_on, 'Second') entity.x = 500 self.assertEqual(ps[0].x, 500) entity.pos = np.array([55, 66]) self.assertEqual(ps['First'].x, 55) self.assertEqual(ps['First'].y, 66)