def test_add_get_remove_single(self): """ Add an object to the SV database. """ # Reset the SV database and instantiate a Leonard. leo = getLeonard() # Create an object ID for the test. id_1 = '1' # The number of SV entries must now be zero. assert len(leo.allBodies) == 0 # Query an object. Since none exists yet this must fail. assert id_1 not in leo.allBodies # Create an object and serialise it. body = getRigidBody(cshapes={'cssphere': getCSSphere()}) # Add the object to Leonard and verify it worked. assert leoAPI.addCmdSpawn([(id_1, body)]) leo.processCommandsAndSync() assert leo.allBodies[id_1] == body # Remove object id_1. assert leoAPI.addCmdRemoveObject(id_1).ok leo.processCommandsAndSync() # Object must not exist anymore in the simulation. assert id_1 not in leo.allBodies assert len(leo.allBodies) == 0
def test_add_get_remove_single(self): """ Add an object to the SV database. """ # Reset the SV database and instantiate a Leonard. leo = getLeonard() # Create an object ID for the test. id_1 = 1 # The number of SV entries must now be zero. assert len(leo.allBodies) == 0 # Query an object. Since none exists yet this must fail. assert id_1 not in leo.allBodies # Create an object and serialise it. body = getRigidBody(cshapes={'cssphere': getCSSphere()}) # Add the object to Leonard and verify it worked. assert leoAPI.addCmdSpawn([(id_1, body)]) leo.processCommandsAndSync() assert leo.allBodies[id_1] == body # Remove object id_1. assert leoAPI.addCmdRemoveObject(id_1).ok leo.processCommandsAndSync() # Object must not exist anymore in the simulation. assert id_1 not in leo.allBodies assert len(leo.allBodies) == 0
def test_processCommandQueue(self): """ Create commands to spawn-, delete, and modify objects or their booster values. Then verify that ``processCommandQueue`` corrently updates Leonard's object cache. """ # Get a Leonard instance. leo = getLeonard(azrael.leonard.LeonardDistributedZeroMQ) # Convenience. body_1 = getRigidBody(imass=1) body_2 = getRigidBody(imass=2) id_1, id_2 = 1, 2 # Cache must be empty. assert len(leo.allBodies) == len(leo.allForces) == 0 # Spawn two objects. tmp = [(id_1, body_1), (id_2, body_2)] assert leoAPI.addCmdSpawn(tmp).ok leo.processCommandsAndSync() # Verify the local cache (forces and torques must default to zero). assert getRigidBody(*leo.allBodies[id_1]) == body_1 assert getRigidBody(*leo.allBodies[id_2]) == body_2 tmp = leo.allForces[id_1] assert tmp.forceDirect == tmp.torqueDirect == [0, 0, 0] assert tmp.forceBoost == tmp.torqueBoost == [0, 0, 0] del tmp # Remove first object. assert leoAPI.addCmdRemoveObject(id_1).ok leo.processCommandsAndSync() assert id_1 not in leo.allBodies assert id_1 not in leo.allForces # Change the State Vector of id_2. pos = (10, 11.5, 12) body_3 = {'position': pos} assert leo.allBodies[id_2].position == (0, 0, 0) assert leoAPI.addCmdModifyBodyState(id_2, body_3).ok leo.processCommandsAndSync() assert leo.allBodies[id_2].position == pos # Apply a direct force and torque to id_2. force, torque = [1, 2, 3], [4, 5, 6] assert leoAPI.addCmdDirectForce(id_2, force, torque).ok leo.processCommandsAndSync() assert leo.allForces[id_2].forceDirect == force assert leo.allForces[id_2].torqueDirect == torque # Specify a new force- and torque value due to booster activity. force, torque = [1, 2, 3], [4, 5, 6] assert leoAPI.addCmdBoosterForce(id_2, force, torque).ok leo.processCommandsAndSync() assert leo.allForces[id_2].forceBoost == force assert leo.allForces[id_2].torqueBoost == torque
def test_commandQueue(self): """ Add-, query, and remove commands from the command queue. """ # Convenience. body_1 = getRigidBody() body_2 = {'imass': 2, 'scale': 3} id_1, id_2 = '0', '1' # The command queue must be empty for every category. ret = leoAPI.dequeueCommands() assert ret.ok assert ret.data['spawn'] == [] assert ret.data['remove'] == [] assert ret.data['modify'] == [] assert ret.data['direct_force'] == [] assert ret.data['booster_force'] == [] # Spawn two objects with id_1 and id_2. tmp = [(id_1, body_1), (id_2, body_1)] assert leoAPI.addCmdSpawn(tmp).ok # Verify that the spawn commands were added. ret = leoAPI.dequeueCommands() assert ret.ok spawn = ret.data['spawn'] assert {spawn[0]['objID'], spawn[1]['objID']} == {id_1, id_2} assert ret.data['remove'] == [] assert ret.data['modify'] == [] assert ret.data['direct_force'] == [] assert ret.data['booster_force'] == [] # De-queuing the commands once more must not return any results because # they have already been removed. ret = leoAPI.dequeueCommands() assert ret.ok assert ret.data['spawn'] == [] assert ret.data['remove'] == [] assert ret.data['modify'] == [] assert ret.data['direct_force'] == [] assert ret.data['booster_force'] == [] # Modify state variable for body with id_1. newSV = {'imass': 10, 'position': [3, 4, 5]} assert leoAPI.addCmdModifyBodyState(id_1, newSV).ok ret = leoAPI.dequeueCommands() modify = ret.data['modify'] assert ret.ok and len(modify) == 1 assert modify[0]['objID'] == id_1 assert modify[0]['rbs'] == newSV del newSV # Set the direct force and torque for id_2. force, torque = [1, 2, 3], [4, 5, 6] assert leoAPI.addCmdDirectForce(id_2, force, torque).ok ret = leoAPI.dequeueCommands() fat = ret.data['direct_force'] assert ret.ok assert len(fat) == 1 assert fat[0]['objID'] == id_2 assert fat[0]['force'] == force assert fat[0]['torque'] == torque # Set the booster force and torque for id_1. force, torque = [1, 2, 3], [4, 5, 6] assert leoAPI.addCmdBoosterForce(id_1, force, torque).ok ret = leoAPI.dequeueCommands() fat = ret.data['booster_force'] assert ret.ok assert len(fat) == 1 assert fat[0]['objID'] == id_1 assert fat[0]['force'] == force assert fat[0]['torque'] == torque # Remove an object. assert leoAPI.addCmdRemoveObject(id_1).ok ret = leoAPI.dequeueCommands() assert ret.ok and ret.data['remove'][0]['objID'] == id_1 # Add commands for two objects (it is perfectly ok to add commands for # non-existing body IDs since this is just a command queue - Leonard # will skip commands for non-existing IDs automatically). force, torque = [7, 8, 9], [10, 11.5, 12.5] for objID in (id_1, id_2): assert leoAPI.addCmdSpawn([(objID, body_1)]).ok assert leoAPI.addCmdModifyBodyState(objID, body_2).ok assert leoAPI.addCmdRemoveObject(objID).ok assert leoAPI.addCmdDirectForce(objID, force, torque).ok assert leoAPI.addCmdBoosterForce(objID, force, torque).ok # De-queue all commands. ret = leoAPI.dequeueCommands() assert ret.ok assert len(ret.data['spawn']) == 2 assert len(ret.data['remove']) == 2 assert len(ret.data['modify']) == 2 assert len(ret.data['direct_force']) == 2 assert len(ret.data['booster_force']) == 2
def test_commandQueue(self): """ Add-, query, and remove commands from the command queue. """ # Convenience. body_1 = getRigidBody() body_2 = {'imass': 2, 'scale': 3} id_1, id_2 = 0, 1 # The command queue must be empty for every category. ret = leoAPI.dequeueCommands() assert ret.ok assert ret.data['spawn'] == [] assert ret.data['remove'] == [] assert ret.data['modify'] == [] assert ret.data['direct_force'] == [] assert ret.data['booster_force'] == [] # Spawn two objects with id_1 and id_2. tmp = [(id_1, body_1), (id_2, body_1)] assert leoAPI.addCmdSpawn(tmp).ok # Verify that the spawn commands were added. ret = leoAPI.dequeueCommands() assert ret.ok assert ret.data['spawn'][0]['objID'] == id_1 assert ret.data['spawn'][1]['objID'] == id_2 assert ret.data['remove'] == [] assert ret.data['modify'] == [] assert ret.data['direct_force'] == [] assert ret.data['booster_force'] == [] # De-queuing the commands once more must not return any results because # they have already been removed. ret = leoAPI.dequeueCommands() assert ret.ok assert ret.data['spawn'] == [] assert ret.data['remove'] == [] assert ret.data['modify'] == [] assert ret.data['direct_force'] == [] assert ret.data['booster_force'] == [] # Modify state variable for body with id_1. newSV = {'imass': 10, 'position': [3, 4, 5]} assert leoAPI.addCmdModifyBodyState(id_1, newSV).ok ret = leoAPI.dequeueCommands() modify = ret.data['modify'] assert ret.ok and len(modify) == 1 assert modify[0]['objID'] == id_1 assert modify[0]['rbs'] == newSV del newSV # Set the direct force and torque for id_2. force, torque = [1, 2, 3], [4, 5, 6] assert leoAPI.addCmdDirectForce(id_2, force, torque).ok ret = leoAPI.dequeueCommands() fat = ret.data['direct_force'] assert ret.ok assert len(fat) == 1 assert fat[0]['objID'] == id_2 assert fat[0]['force'] == force assert fat[0]['torque'] == torque # Set the booster force and torque for id_1. force, torque = [1, 2, 3], [4, 5, 6] assert leoAPI.addCmdBoosterForce(id_1, force, torque).ok ret = leoAPI.dequeueCommands() fat = ret.data['booster_force'] assert ret.ok assert len(fat) == 1 assert fat[0]['objID'] == id_1 assert fat[0]['force'] == force assert fat[0]['torque'] == torque # Remove an object. assert leoAPI.addCmdRemoveObject(id_1).ok ret = leoAPI.dequeueCommands() assert ret.ok and ret.data['remove'][0]['objID'] == id_1 # Add commands for two objects (it is perfectly ok to add commands for # non-existing body IDs since this is just a command queue - Leonard # will skip commands for non-existing IDs automatically). force, torque = [7, 8, 9], [10, 11.5, 12.5] for objID in (id_1, id_2): assert leoAPI.addCmdSpawn([(objID, body_1)]).ok assert leoAPI.addCmdModifyBodyState(objID, body_2).ok assert leoAPI.addCmdRemoveObject(objID).ok assert leoAPI.addCmdDirectForce(objID, force, torque).ok assert leoAPI.addCmdBoosterForce(objID, force, torque).ok # De-queue all commands. ret = leoAPI.dequeueCommands() assert ret.ok assert len(ret.data['spawn']) == 2 assert len(ret.data['remove']) == 2 assert len(ret.data['modify']) == 2 assert len(ret.data['direct_force']) == 2 assert len(ret.data['booster_force']) == 2