def test_dof_index(self): s = System() j1 = FreeJoint('j1') j2 = FreeJoint('j2') s.add_leaf(j1) j1.add_leaf(j2) s.setup() # Prescribe some of the strains: # _____j1____ _____j2____ # Strain: 0 1 2 3 4 5 0 1 2 3 4 5 # Prescr: * * * # Dofs: 0 1 2 3 4 5 6 7 8 s.prescribe(j1, 0, part=[1, 4]) s.prescribe(j2, 0, part=[3]) self.assertEqual(s.dof_index('j1', 0), 0) self.assertEqual(s.dof_index('j1', 2), 1) self.assertEqual(s.dof_index('j1', 5), 3) self.assertEqual(s.dof_index('j2', 5), 8) # Strain index out of range should give IndexError with self.assertRaises(IndexError): s.dof_index('j1', 6) # Asking for index of prescribed strain should give ValueError with self.assertRaises(ValueError): s.dof_index('j1', 1)
def test_solve_accelerations(self): # solve_accelerations() should find: # (a) response of system to forces (here, gravity) # (b) include any prescribed accelerations in qdd vector g = 9.81 s = System(gravity=g) j = FreeJoint('joint') b = RigidBody('body', mass=23.54, inertia=52.1 * np.eye(3)) s.add_leaf(j) j.add_leaf(b) s.setup() # Prescribe horizontal acceleration. Vertical acceleration # should result from gravity. s.prescribe(j, 2.3, part=[0]) # x acceleration # Initially accelerations are zero assert_aae(j.ap, 0) assert_aae(j.ad, 0) assert_aae(j.astrain, 0) # Solve accelerations & check s.solve_accelerations() s.update_kinematics() assert_aae(j.ap, 0) # ground assert_aae(j.ad, [2.3, 0, -g, 0, 0, 0]) assert_aae(j.astrain, j.ad) # not always true, but works for FreeJoint
def test_solve_accelerations_coupling(self): # Further to test above, check that coupling between prescribed # accelerations and other dofs is correct. For example, if there # is a rigid body vertically offset from the joint, then a # prescribed horizontal acceleration should cause an angular # acceleration as well as the translational acceleration. s = System() j = FreeJoint('joint') c = RigidConnection('conn', [0, 0, 1.7]) b = RigidBody('body', mass=23.54, inertia=74.1 * np.eye(3)) s.add_leaf(j) j.add_leaf(c) c.add_leaf(b) s.setup() # Prescribe horizontal acceleration, solve other accelerations s.prescribe(j, 2.3, part=[0]) # x acceleration s.update_kinematics() # update system to show prescribed acc s.solve_accelerations() # solve free accelerations s.update_kinematics() # update system to show solution # Ground shouldn't move assert_aae(j.ap, 0) # Need angular acceleration = (m a_x L) / I0 I0 = 74.1 + (23.54 * 1.7**2) expected_angular_acc = -(23.54 * 2.3 * 1.7) / I0 assert_aae(j.ad, [2.3, 0, 0, 0, expected_angular_acc, 0]) assert_aae(j.astrain, j.ad) # not always true, but works for FreeJoint
def test_single_rigid_body(self): mass = 36.2 inertia = np.diag([75.4, 653, 234]) s = System() j = FreeJoint('joint') b = RigidBody('body', mass, inertia) s.add_leaf(j) j.add_leaf(b) s.setup() # Calculate reduced system to get rigid body matrices rsys = ReducedSystem(s) self.assertEqual(rsys.M.shape, (6, 6)) self.assertEqual(rsys.Q.shape, (6, )) assert_aae(rsys.M[:3, :3], mass * np.eye(3)) assert_aae(rsys.M[3:, 3:], inertia) assert_aae(rsys.M[3:, :3], 0) assert_aae(rsys.M[:3, 3:], 0) assert_aae(rsys.Q, 0) # Now if some freedoms are prescribed, don't appear in reduced system s.prescribe(j, part=[1, 2, 3, 4, 5]) # only x-translation is free now rsys = ReducedSystem(s) self.assertEqual(rsys.M.shape, (1, 1)) self.assertEqual(rsys.Q.shape, (1, )) assert_aae(rsys.M[0, 0], mass) assert_aae(rsys.Q, 0)
def test_single_rigid_body(self): mass = 36.2 inertia = np.diag([75.4, 653, 234]) s = System() j = FreeJoint('joint') b = RigidBody('body', mass, inertia) s.add_leaf(j) j.add_leaf(b) s.setup() # Calculate reduced system to get rigid body matrices rsys = ReducedSystem(s) self.assertEqual(rsys.M.shape, (6, 6)) self.assertEqual(rsys.Q.shape, (6,)) assert_aae(rsys.M[:3, :3], mass * np.eye(3)) assert_aae(rsys.M[3:, 3:], inertia) assert_aae(rsys.M[3:, :3], 0) assert_aae(rsys.M[:3, 3:], 0) assert_aae(rsys.Q, 0) # Now if some freedoms are prescribed, don't appear in reduced system s.prescribe(j, part=[1, 2, 3, 4, 5]) # only x-translation is free now rsys = ReducedSystem(s) self.assertEqual(rsys.M.shape, (1, 1)) self.assertEqual(rsys.Q.shape, (1,)) assert_aae(rsys.M[0, 0], mass) assert_aae(rsys.Q, 0)
def test_print_functions(self): # Not very good tests, but at least check they run without errors joint = FreeJoint('joint') body = RigidBody('body', mass=1.235) s = System() s.add_leaf(joint) joint.add_leaf(body) s.setup() s.print_states() s.print_info()
def test_prescribe_free(self): s = System() j = FreeJoint('joint') s.add_leaf(j) s.setup() s.time = 3.54 # Initially all 6 joint motions are free self.assertEqual(len(s.q.dofs), 6) # Prescribing joint to be fixed results in no dofs s.prescribe(j) self.assertEqual(len(s.q.dofs), 0) # Freeing joint results in 6 dofs again s.free(j) self.assertEqual(len(s.q.dofs), 6) # Prescribing 2 of joint motions leaves 4 dofs s.prescribe(j, lambda t: t, [0, 2]) self.assertEqual(len(s.q.dofs), 4) # Prescribing another joint motions leaves 3 dofs s.prescribe(j, 2.0, part=3) self.assertEqual(len(s.q.dofs), 3) # Check accelerations are applied to qdd assert_aae(s.qdd[j.istrain], 0) s.apply_prescribed_accelerations() assert_aae(s.qdd[j.istrain], [3.54, 0, 3.54, 2.0, 0, 0]) # Freeing joint results in 6 dofs again s.free(j) self.assertEqual(len(s.q.dofs), 6)
def test_dofs_subset(self): s = System() j = FreeJoint('joint') s.add_leaf(j) s.setup() # 2 nodes, 6 constraints, 6 dofs self.assertEqual(len(s.q), 2 * 12 + 6 + 6) self.assertEqual(len(s.qd), 2 * 6 + 6 + 6) self.assertEqual(len(s.qdd), 2 * 6 + 6 + 6) self.assertEqual(len(s.q.dofs), 6) self.assertEqual(len(s.qd.dofs), 6) self.assertEqual(len(s.qdd.dofs), 6)
def test_get_set_state(self): s = System() j = FreeJoint('joint') s.add_leaf(j) s.setup() # State is [q_dofs, qd_dofs]. # Here we have 6 dofs: s.set_state([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) self.assertEqual(list(s.q.dofs), [1, 2, 3, 4, 5, 6]) self.assertEqual(list(s.qd.dofs), [7, 8, 9, 10, 11, 12]) s.q.dofs[2] = 100 s.qd.dofs[0] = -1 self.assertEqual(list(s.get_state()), [1, 2, 100, 4, 5, 6, -1, 8, 9, 10, 11, 12])