Пример #1
0
    def test_solve_reactions(self):
        # Check it calls the Element method in the right order: down
        # the tree from leaves to base. It must also reset reactions.
        s = System()
        c0 = RigidConnection('c0')
        c1 = RigidConnection('c1')
        c2 = RigidConnection('c2')
        b1 = RigidBody('b1', 1)
        b2 = RigidBody('b2', 1)
        s.add_leaf(c0)
        c0.add_leaf(c1)
        c0.add_leaf(c2)
        c1.add_leaf(b1)
        c2.add_leaf(b2)
        s.setup()

        # Check elements' iter_reactions() are called
        def mock_iter_reactions(element):
            calls.append(element)
        calls = []
        import types
        for el in s.elements.values():
            el.iter_reactions = types.MethodType(mock_iter_reactions, el)

        # Test
        s.joint_reactions[:] = 3
        s.solve_reactions()
        self.assertEqual(calls, [b2, c2, b1, c1, c0])
        assert_aae(s.joint_reactions, 0)
Пример #2
0
    def test_1dof_nonlinear_system(self):
        s = System()
        j = PrismaticJoint('joint', [0, 0, 1])
        k = 0.45  # quadratic stiffness coefficient
        j.internal_force = lambda el, t: -k * el.xstrain[0]**2
        b = RigidBody('body', 10)
        s.add_leaf(j)
        j.add_leaf(b)
        s.setup()

        # Linearise around z0 = 0: stiffness should be zero
        linsys = LinearisedSystem.from_system(s, z0=0)
        assert_aae(linsys.M, [[10.0]])
        assert_aae(linsys.C, [[0.0]])
        assert_aae(linsys.K, [[0.0]])

        # Linearise about z0 = 2: stiffness should be 2kx
        linsys = LinearisedSystem.from_system(s, z0=[2])
        assert_aae(linsys.M, [[10.0]])
        assert_aae(linsys.C, [[0.0]])
        assert_aae(linsys.K, [[2 * k * 2]])

        # Test setting z0 in another way
        linsys = LinearisedSystem.from_system(s, z0={'joint': [4.2]})
        assert_aae(linsys.M, [[10.0]])
        assert_aae(linsys.C, [[0.0]])
        assert_aae(linsys.K, [[2 * k * 4.2]])
Пример #3
0
    def test_adding_elements(self):
        conn = RigidConnection('conn')
        body = RigidBody('body', mass=1.235)
        s = System()
        s.add_leaf(conn)
        conn.add_leaf(body)
        s.setup()

        # Should have dict of elements
        self.assertEqual(s.elements, {'conn': conn, 'body': body})

        # Number of states:
        #   6 ground
        # + 6 constraints on conn
        # + 6 <node-0>   between conn and body
        # ---
        #  18
        self.assertEqual(s.lhs.shape, (18, 18))
        for vec in (s.rhs, s.qd, s.qdd):
            self.assertEqual(len(vec), 18)

        # Check there are no dofs
        self.assertEqual(len(s.q.dofs), 0)
        self.assertEqual(len(s.qd.dofs), 0)
        self.assertEqual(len(s.qdd.dofs), 0)
Пример #4
0
    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
Пример #5
0
    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
Пример #6
0
    def test_nonzero_prescribed_acceleration(self):
        # Test reduction where a prescribed acceleration is non-zero:
        # two sliders in series, with a mass on the end. If the second
        # slider's acceleration is prescribed, the first slider's DOF
        # sees an inertial force corresponding to the acceleration of
        # the mass.
        mass = 36.2
        s = System()
        s1 = PrismaticJoint('slider1', [1, 0, 0])
        s2 = PrismaticJoint('slider2', [1, 0, 0])
        b = RigidBody('body', mass)
        s.add_leaf(s1)
        s1.add_leaf(s2)
        s2.add_leaf(b)
        s.setup()

        s.prescribe(s2, acc=0)

        # With hinge angle = 0, no generalised inertial force
        rsys = ReducedSystem(s)
        assert_aae(rsys.M, mass)
        assert_aae(rsys.Q, 0)

        # With hinge angle = 90deg, do see generalised inertial force
        s.prescribe(s2, acc=2.3)
        rsys = ReducedSystem(s)
        assert_aae(rsys.M, mass)
        assert_aae(rsys.Q, -mass * 2.3)
Пример #7
0
    def test_call(self):
        s = System()
        c = RigidConnection('conn', [1, 0, 0])
        h = Hinge('hinge', [0, 1, 0])
        b = RigidBody('body', 1)
        s.add_leaf(h)
        h.add_leaf(c)
        c.add_leaf(b)
        s.setup()

        # Set hinge angle
        h.xstrain[0] = 0.82
        h.vstrain[0] = 1.2
        h.astrain[0] = -0.3
        s.update_kinematics()
        s.solve_reactions()

        # Test load outputs
        out = LoadOutput('node-1')
        assert_array_equal(out(s), s.joint_reactions['node-1'])

        out = LoadOutput('node-1', local=True)
        F = s.joint_reactions['node-1']
        assert_array_equal(out(s), np.r_[np.dot(b.Rp.T, F[:3]),
                                         np.dot(b.Rp.T, F[3:])])
Пример #8
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)
Пример #9
0
    def test_rigid_body_with_no_dofs(self):
        s = System()
        b = RigidBody('body', 23.7)
        s.add_leaf(b)
        s.setup()

        # Calculate reduced system to get rigid body matrices
        rsys = ReducedSystem(s)
        self.assertEqual(rsys.M.shape, (0, 0))
        self.assertEqual(rsys.Q.shape, (0, ))
Пример #10
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()
Пример #11
0
    def test_assemble(self):
        # Test system:
        #
        #                   /-- m2
        #  m1 -----conn----|
        #                   \-- m3
        s = System()
        m1 = RigidBody('m1', 1.3)
        m2 = RigidBody('m2', 3.4)
        m3 = RigidBody('m3', 7.5)
        conn = RigidConnection('conn')
        s.add_leaf(conn)
        s.add_leaf(m1)
        conn.add_leaf(m2)
        conn.add_leaf(m3)
        s.setup()

        # Check starting mass matrices of elements are as expected
        assert_aae(np.diag(m1.mass_vv[:3, :3]), 1.3)
        assert_aae(np.diag(m2.mass_vv[:3, :3]), 3.4)
        assert_aae(np.diag(m3.mass_vv[:3, :3]), 7.5)

        # Initially make system matrix empty for testing
        s.lhs[:, :] = 0
        assert_aae(s.lhs, 0)

        # After assembly, the mass matrices are put in the correct places:
        #   0:6  -> m1 node
        #   6:12 -> conn constraints
        #  12:18 -> m2 node
        #  12:18 -> m3 node
        s.assemble()
        M = s.lhs.copy()
        # Subtract expected mass
        M[0:6, 0:6] -= m1.mass_vv
        M[12:18, 12:18] -= m2.mass_vv + m3.mass_vv
        # Subtract expected constraints
        M[0:6, 6:12] -= conn.F_vp
        M[6:12, 0:6] -= conn.F_vp
        M[12:18, 6:12] -= conn.F_vd
        M[6:12, 12:18] -= conn.F_vd
        assert_aae(M, 0)
Пример #12
0
    def test_call(self):
        s = System()
        c = RigidConnection('conn', [1, 0, 0])
        h = Hinge('hinge', [0, 1, 0])
        b = RigidBody('body', 1)
        s.add_leaf(h)
        h.add_leaf(c)
        c.add_leaf(b)
        s.setup()

        # Set hinge angle
        h.xstrain[0] = 0.82
        h.vstrain[0] = 1.2
        h.astrain[0] = -0.3
        s.update_kinematics()

        # Test node outputs
        out = StateOutput('node-1')
        assert_array_equal(out(s), np.r_[b.rp, b.Rp.flatten()])

        out = StateOutput('node-1', deriv=1)
        assert_array_equal(out(s), b.vp)

        out = StateOutput('node-1', deriv=2)
        assert_array_equal(out(s), b.ap)

        out = StateOutput('node-1', local=True)
        assert_array_equal(out(s), np.r_[np.dot(b.Rp.T, b.rp),
                                         np.eye(3).flatten()])

        out = StateOutput('node-1', deriv=1, local=True)
        assert_array_equal(out(s), np.r_[np.dot(b.Rp.T, b.vp[:3]),
                                         np.dot(b.Rp.T, b.vp[3:])])

        out = StateOutput('node-1', deriv=2, local=True)
        assert_array_equal(out(s), np.r_[np.dot(b.Rp.T, b.ap[:3]),
                                         np.dot(b.Rp.T, b.ap[3:])])

        # Test strain outputs
        out = StateOutput('hinge-strains')
        assert_array_equal(out(s), 0.82)

        out = StateOutput('hinge-strains', deriv=1)
        assert_array_equal(out(s), 1.2)

        out = StateOutput('hinge-strains', deriv=2)
        assert_array_equal(out(s), -0.3)

        # Strains cannot be transformed to local coordinates
        with self.assertRaises(RuntimeError):
            out = StateOutput('hinge-strains', local=True)
            out(s)
Пример #13
0
    def setUp(self):
        joint = PrismaticJoint('joint', [0, 0, 1])
        joint.stiffness = self.K
        joint.damping = 2 * self.damping_coeff * (self.K * self.M)**0.5

        body = RigidBody('body', self.M)

        system = System()
        system.add_leaf(joint)
        joint.add_leaf(body)
        system.setup()
        system.update_kinematics()

        self.joint, self.body, self.system = joint, body, system
Пример #14
0
    def setUp(self):
        # Rigid body with offset centre of mass
        self.body = RigidBody('body', self.mass, Xc=[self.offset, 0, 0])

        # Hinge with axis along Z axis
        self.hinge = Hinge('hinge', [0, 0, 1])

        # Build system
        self.system = System()
        self.system.add_leaf(self.hinge)
        self.hinge.add_leaf(self.body)
        self.system.setup()
        self.system.update_kinematics()  # Set up nodal values initially
        self.system.update_matrices()
Пример #15
0
    def test_distal_forces_cause_acceleration(self):
        j = FreeJoint('joint')
        b = RigidBody('body', mass=3, inertia=np.diag([7, 7, 7]))
        s = System()
        s.add_leaf(j)
        j.add_leaf(b)
        s.setup()

        # Constant loading
        j.distal_forces = np.array([2, 0, 0, 0, 0, 0])
        s.update_kinematics()
        s.update_matrices()
        s.solve_accelerations()
        s.update_kinematics()
        assert_array_equal(j.ad, [2. / 3, 0, 0, 0, 0, 0])
Пример #16
0
    def test_1dof_linear_system(self):
        s = System()
        j = PrismaticJoint('joint', [0, 0, 1])
        j.stiffness = 5.0
        j.damping = 2.3
        b = RigidBody('body', 10)
        s.add_leaf(j)
        j.add_leaf(b)
        s.setup()

        linsys = LinearisedSystem.from_system(s)

        assert_array_equal(linsys.M, [[10.0]])
        assert_array_equal(linsys.C, [[2.3]])
        assert_array_equal(linsys.K, [[5.0]])
Пример #17
0
    def setUp(self):
        # Parameters
        mass = 11.234
        length = 2.54
        gravity = 9.81

        # Build model
        hinge = Hinge('hinge', [0, 1, 0])
        link = RigidConnection('link', [length, 0, 0])
        body = RigidBody('body', mass)

        system = System(gravity=gravity)
        system.add_leaf(hinge)
        hinge.add_leaf(link)
        link.add_leaf(body)
        system.setup()

        # Custom outputs to calculate correct answer
        def force_body_prox_local(s):
            theta = s.q[hinge.istrain][0]
            thetadot = s.qd[hinge.istrain][0]
            thetadotdot = s.qdd[hinge.istrain][0]
            Fx = mass * (-gravity * np.sin(theta) - length * thetadot**2)
            Fz = mass * (+gravity * np.cos(theta) - length * thetadotdot)
            return [Fx, 0, Fz, 0, 0, 0]

        def force_hinge_prox(s):
            theta = s.q[hinge.istrain][0]
            thetadot = s.qd[hinge.istrain][0]
            thetadotdot = s.qdd[hinge.istrain][0]
            A = np.array([[+np.cos(theta), np.sin(theta)],
                          [-np.sin(theta), np.cos(theta)]])
            Fxz = -mass * length * np.dot(A, [thetadot**2, thetadotdot])
            return [Fxz[0], 0, Fxz[1] + gravity * mass, 0, 0, 0]

        # Solver
        integ = Integrator(system, ('pos', 'vel', 'acc'))
        integ.add_output(LoadOutput(hinge.iprox))
        integ.add_output(LoadOutput(link.iprox))
        integ.add_output(LoadOutput(body.iprox))
        integ.add_output(LoadOutput(body.iprox, local=True))
        integ.add_output(CustomOutput(force_hinge_prox, "correct ground"))
        integ.add_output(
            CustomOutput(force_body_prox_local, "correct link distal local"))

        self.system = system
        self.integ = integ
Пример #18
0
def build_system():
    # Calculate inertia, stiffness and damping
    I2 = mass * length**2
    k = (2 * np.pi * natfreq)**2 * I2
    c = 2 * damping_factor * I2 * (2 * np.pi * natfreq)

    # Build model
    hinge = Hinge('hinge', [0, 0, 1])
    hinge.stiffness = k
    hinge.damping = c
    link = RigidConnection('link', [length, 0, 0])
    body = RigidBody('body', mass)

    system = System()
    system.add_leaf(hinge)
    hinge.add_leaf(link)
    link.add_leaf(body)
    system.setup()

    return system
Пример #19
0
    def test_applied_force(self):
        # Set up a hinge with a mass offset on a rigid body. The
        # reduced system should have 1 DOF -- the hinge rotation --
        # with the associated mass being the inertia of the mass about
        # the hinge, and the associated generalised force being the
        # applied moment.
        mass = 36.2
        zforce = -30
        L = 3.2
        s = System()
        h = Hinge('hinge', [0, 1, 0])
        c = RigidConnection('conn', [L, 0, 0])
        b = RigidBody('body', mass, nodal_load=[0, 0, zforce])
        s.add_leaf(h)
        h.add_leaf(c)
        c.add_leaf(b)
        s.setup()

        rsys = ReducedSystem(s)
        self.assertEqual(rsys.M.shape, (1, 1))
        self.assertEqual(rsys.Q.shape, (1, ))
        self.assertEqual(rsys.M[0, 0], mass * L**2)  # inertial about hinge
        self.assertEqual(rsys.Q[0], -zforce * L)  # moment about hinge
Пример #20
0
    def test_find_equilibrium(self):
        g = 9.81
        m = 23.1
        k = 45.2
        s = System(gravity=g)
        slider = PrismaticJoint('slider', [0, 0, 1])
        slider.stiffness = k
        body = RigidBody('body', mass=m)
        s.add_leaf(slider)
        slider.add_leaf(body)
        s.setup()

        # Initially position should be zero and acceleration nonzero
        s.solve_accelerations()
        assert_aae(slider.xstrain, 0)
        assert_aae(slider.astrain, -g)

        # At equilibrium, position should be nozero and force on body zero
        s.find_equilibrium()
        s.update_matrices()      # recalculate stiffness force
        s.solve_accelerations()
        assert_aae(slider.xstrain, -m * g / k)
        assert_aae(slider.astrain, 0)
Пример #21
0
    def test_adding_elements_with_strains(self):
        slider = PrismaticJoint('slider', [1, 0, 0])
        conn = RigidConnection('conn')
        body = RigidBody('body', mass=1.235)
        s = System()
        s.add_leaf(slider)
        slider.add_leaf(conn)
        conn.add_leaf(body)
        s.setup()

        # Should have dict of elements
        self.assertEqual(s.elements,
                         {'slider': slider, 'conn': conn, 'body': body})

        # Number of states:
        #   6 ground
        # + 6 constraints on slider
        # + 1 strain in slider
        # + 6 <node-0>   between slider and conn
        # + 6 constraints on conn
        # + 6 <node-1>   between conn and body
        # ---
        #  31
        self.assertEqual(s.lhs.shape, (31, 31))
        for vec in (s.rhs, s.qd, s.qdd):
            self.assertEqual(len(vec), 31)

        # Check there is the one slider dof
        self.assertEqual(len(s.q.dofs), 1)
        self.assertEqual(len(s.qd.dofs), 1)
        self.assertEqual(len(s.qdd.dofs), 1)

        # After prescribing the slider, there should be no dofs
        s.prescribe(slider)
        self.assertEqual(len(s.q.dofs), 0)
        self.assertEqual(len(s.qd.dofs), 0)
        self.assertEqual(len(s.qdd.dofs), 0)
Пример #22
0
 def build_body(self):
     Jx = self.radius**2 / 2
     Jyz = (3 * self.radius**2 + self.length**2) / 12
     Jyz_0 = Jyz + (self.length / 2)**2  # parallel axis theorem
     inertia = self.mass * np.diag([Jx, Jyz_0, Jyz_0])
     return RigidBody('body', self.mass, inertia, [self.length / 2, 0, 0])
Пример #23
0
    def setUp(self):
        # Parameters
        mass = 11.234
        length = 2.54
        gravity = 9.81

        # Build model
        slider = PrismaticJoint('slider', [1, 0, 0])
        link = RigidConnection('link', [0, 0, length])
        body = RigidBody('body', mass)

        system = System(gravity=gravity)
        system.add_leaf(slider)
        slider.add_leaf(link)
        link.add_leaf(body)
        system.setup()

        # Prescribe motion -- sinusoidal acceleration
        motion_frequency = 1  # Hz
        motion_amplitude = 2.3  # m

        # x =  motion_amplitude * np.cos(w*t)
        # v = -motion_amplitude * np.sin(w*t) * w
        # a = -motion_amplitude * np.cos(w*t) * w**2
        def prescribed_acceleration(t):
            w = 2 * np.pi * motion_frequency
            return -w**2 * motion_amplitude * np.cos(w * t)

        system.prescribe(slider, prescribed_acceleration)

        # Set the correct initial condition
        system.q[slider.istrain][0] = motion_amplitude
        system.qd[slider.istrain][0] = 0.0

        # Custom outputs to calculate correct answer
        def force_body_prox(s):
            a = prescribed_acceleration(s.time)
            Fx = mass * a
            Fz = mass * gravity
            return [Fx, 0, Fz, 0, 0, 0]

        def force_link_prox(s):
            a = prescribed_acceleration(s.time)
            Fx = mass * a
            Fz = mass * gravity
            My = length * Fx
            return [Fx, 0, Fz, 0, My, 0]

        def force_slider_prox(s):
            a = prescribed_acceleration(s.time)
            x = -a / (2 * np.pi * motion_frequency)**2
            Fx = mass * a
            Fz = mass * gravity
            My = length * Fx - x * Fz
            return [Fx, 0, Fz, 0, My, 0]

        # Solver
        integ = Integrator(system, ('pos', 'vel', 'acc'))
        integ.add_output(LoadOutput(slider.iprox))
        integ.add_output(LoadOutput(link.iprox))
        integ.add_output(LoadOutput(body.iprox))
        integ.add_output(CustomOutput(force_slider_prox, "correct ground"))
        integ.add_output(CustomOutput(force_link_prox, "correct slider dist"))
        integ.add_output(CustomOutput(force_body_prox, "correct link dist"))

        self.system = system
        self.integ = integ