コード例 #1
0
    def test_connecting_and_iterate_inputs(self):
        m = osim.Model()
        b = osim.Body('b1', 2.0, osim.Vec3(1, 0, 0), osim.Inertia(1))
        j = osim.PinJoint('pin', m.getGround(), b)

        # Source.
        source = osim.TableSource()
        source.setName("source")

        table = osim.TimeSeriesTable()
        table.setColumnLabels(('col1', 'col2', 'col3', 'col4'))
        row = osim.RowVector([1, 2, 3, 4])
        table.appendRow(0.0, row)
        row = osim.RowVector([2, 3, 4, 5])
        table.appendRow(1.0, row)
        source.setTable(table)

        # Reporter.
        rep = osim.ConsoleReporter()
        rep.setName("rep")

        m.addBody(b)
        m.addJoint(j)
        m.addComponent(source)
        m.addComponent(rep)

        # Connect.
        # There are multiple ways to perform the connection, especially
        # for reporters.
        coord = j.get_coordinates(0)
        rep.updInput('inputs').connect(coord.getOutput('value'))
        rep.connectInput_inputs(coord.getOutput('speed'), 'spd')
        rep.connectInput_inputs(source.getOutput('column').getChannel('col1'))
        rep.addToReport(
            source.getOutput('column').getChannel('col2'), 'second_col')

        s = m.initSystem()

        # Access and iterate through AbstractInputs, using names.
        expectedLabels = [
            '/model/jointset/pin/pin_coord_0|value', 'spd',
            '/model/source|column:col1', 'second_col'
        ]
        i = 0
        for name in rep.getInputNames():
            # Actually, there is only one input, which we connected to 4
            # channels.
            assert rep.getInput(name).getNumConnectees() == 4
            for j in range(4):
                assert (rep.getInput(name).getLabel(j) == expectedLabels[j])
            i += 1

        # Access concrete Input.
        # Input value is column 2 at time 0.
        assert (osim.InputDouble.safeDownCast(rep.getInput('inputs')).getValue(
            s, 3) == 2.0)
コード例 #2
0
    def test_TableSourceReporter(self):
        m = osim.Model()

        # Source.
        source = osim.TableSource()
        source.setName("source")

        table = osim.TimeSeriesTable()
        table.setColumnLabels(('col1', 'col2', 'col3', 'col4'))
        row = osim.RowVector([1, 2, 3, 4])
        table.appendRow(0.0, row)
        row = osim.RowVector([2, 3, 4, 5])
        table.appendRow(1.0, row)

        source.setTable(table)

        # Reporter.
        c_rep = osim.ConsoleReporter()
        c_rep.setName("c_rep")

        t_rep = osim.TableReporter()
        t_rep.setName("t_rep")

        # Add.
        m.addComponent(source)
        m.addComponent(c_rep)
        m.addComponent(t_rep)

        # Connect.
        c_rep.updInput("inputs").connect(
            source.getOutput("column").getChannel("col1"))
        t_rep.updInput("inputs").connect(
            source.getOutput("column").getChannel("col2"))

        # Realize.
        s = m.initSystem()
        s.setTime(0.5)
        m.realizeReport(s)

        # Test.
        # This value is the average of col2 (2.0 and 3.0).
        assert t_rep.getReport().getRowAtIndex(0)[0] == 2.5