Пример #1
0
    def test_markAdopted2(self):
        a = osim.Model()

        # We just need the following not to cause a segfault.

        # Model add*
        pa = osim.PathActuator()
        pa.setName('pa')
        a.addForce(pa)

        probe = osim.Umberger2010MuscleMetabolicsProbe()
        probe.setName('probe')
        a.addProbe(probe)

        ma = osim.MuscleAnalysis()
        ma.setName('ma')
        a.addAnalysis(ma)

        pc = osim.PrescribedController()
        pc.setName('pc')
        a.addController(pc)

        body = osim.Body('body1', 1.0, osim.Vec3(0, 0, 0),
                         osim.Inertia(0, 0, 0))

        loc_in_parent = osim.Vec3(0, 0, 0)
        orient_in_parent = osim.Vec3(0, 0, 0)
        loc_in_body = osim.Vec3(0, 0, 0)
        orient_in_body = osim.Vec3(0, 0, 0)
        print "creating Weld Joint.."
        joint = osim.WeldJoint("weld_joint", a.getGround(), loc_in_parent,
                               orient_in_parent, body, loc_in_body,
                               orient_in_parent)
        print "adding a body .."
        a.addBody(body)
        print "adding a joint .."
        a.addJoint(joint)
        print "Creating a ConstantDistanceConstraint.."
        constr = osim.ConstantDistanceConstraint()
        constr.setBody1ByName("ground")
        constr.setBody1PointLocation(osim.Vec3(0, 0, 0))
        constr.setBody2ByName("body")
        constr.setBody2PointLocation(osim.Vec3(1, 0, 0))
        constr.setConstantDistance(1)
        a.addConstraint(constr)

        f = osim.BushingForce("bushing", "ground", "body", osim.Vec3(2, 2, 2),
                              osim.Vec3(1, 1, 1), osim.Vec3(0, 0, 0),
                              osim.Vec3(0, 0, 0))
        a.addForce(f)

        f2 = osim.BushingForce()
        a.addForce(f2)

        f3 = osim.SpringGeneralizedForce()
        a.addForce(f3)

        model = osim.Model(os.path.join(test_dir, "arm26.osim"))
        g = osim.CoordinateActuator('r_shoulder_elev')
        model.addForce(g)
Пример #2
0
def test_markAdopted():
    a = osim.Model()

    # We just need the following not to not cause a segfault.

    # Model add*
    a.addComponent(osim.PathActuator())
    a.addProbe(osim.Umberger2010MuscleMetabolicsProbe())
    a.addAnalysis(osim.MuscleAnalysis())
    a.addController(osim.PrescribedController())

    body = osim.Body('body', 1.0, osim.Vec3(0, 0, 0), osim.Inertia(0, 0, 0))

    loc_in_parent = osim.Vec3(0, -0, 0)
    orient_in_parent = osim.Vec3(0, 0, 0)
    loc_in_body = osim.Vec3(0, 0, 0)
    orient_in_body = osim.Vec3(0, 0, 0)
    joint = osim.WeldJoint("weld_joint", a.getGroundBody(), loc_in_parent,
                           orient_in_parent, body, loc_in_body,
                           orient_in_parent)

    a.addBody(body)

    constr = osim.ConstantDistanceConstraint()
    constr.setBody1ByName("ground")
    constr.setBody1PointLocation(osim.Vec3(0, 0, 0))
    constr.setBody2ByName("body")
    constr.setBody2PointLocation(osim.Vec3(1, 0, 0))
    constr.setConstantDistance(1)
    a.addConstraint(constr)

    # Force requires body names. If not provided, you get a segfault.
    f = osim.BushingForce()
    f.setBody1ByName("ground")
    f.setBody2ByName("body")
    a.addForce(f)

    model = osim.Model(os.environ['OPENSIM_HOME'] + "/Models/Arm26/arm26.osim")
    g = osim.CoordinateActuator('r_shoulder_elev')
    model.addForce(g)
Пример #3
0
def test_addMetabolicProbes():

    model = osim.Model(os.environ['OPENSIM_HOME'] +
            "/Models/Gait10dof18musc/gait10dof18musc.osim")

    # Twitch ratios for gait1018.
    twitchRatios = { 'hamstrings': 0.49, 'bifemsh': 0.53, 'glut_max': 0.55,
            'iliopsoas': 0.50, 'rect_fem': 0.39, 'vasti': 0.50, 'gastroc':
            0.54, 'soleus': 0.80, 'tib_ant': 0.70}
    
    # Parameters used for all probes
    # ------------------------------
    # The following booleans are constructor arguments for the Umberger probe.
    # These settings are used for all probes.
    activationMaintenanceRateOn = True
    shorteningRateOn = True
    basalRateOn = False
    mechanicalWorkRateOn = True
    
    # The mass of each muscle will be calculated using data from the model:
    #   muscleMass = (maxIsometricForce / sigma) * rho * optimalFiberLength
    # where sigma = 0.25e6 is the specific tension of mammalian muscle (in
    # Pascals) and rho = 1059.7 is the density of mammalian muscle (in kg/m^3).
    
    # The slow-twitch ratio used for muscles that either do not appear in the
    # file, or appear but whose proportion of slow-twitch fibers is unknown.
    defaultTwitchRatio = 0.5
    
    # Whole-body probe
    # ----------------
    # Define a whole-body probe that will report the total metabolic energy
    # consumption over the simulation.
    wholeBodyProbe = osim.Umberger2010MuscleMetabolicsProbe(
        activationMaintenanceRateOn,
        shorteningRateOn,
        basalRateOn,
        mechanicalWorkRateOn)
    wholeBodyProbe.setOperation("value")
    wholeBodyProbe.set_report_total_metabolics_only(False);
    
    # Add the probe to the model and provide a name.
    # TODO SEGFAULT AT THIS NEXT LINE:
    model.addProbe(wholeBodyProbe)
    wholeBodyProbe.setName("metabolic_power")
    
    # Loop through all muscles, adding parameters for each into the whole-body
    # probe.
    for iMuscle in range(model.getMuscles().getSize()):
        thisMuscle = model.getMuscles().get(iMuscle)
        
        # Get the slow-twitch ratio from the data we read earlier. Start with
        # the default value.
        slowTwitchRatio = defaultTwitchRatio
        
        # Set the slow-twitch ratio to the physiological value, if it is known.
        for key, val in twitchRatios.items():
            if thisMuscle.getName().startswith(key) and val != -1:
                slowTwitchRatio = val
        
        # Add this muscle to the whole-body probe. The arguments are muscle
        # name, slow-twitch ratio, and muscle mass. Note that the muscle mass
        # is ignored unless we set useProvidedMass to True.
        wholeBodyProbe.addMuscle(thisMuscle.getName(),
                                 slowTwitchRatio)

    name = os.path.join(this_file_dir, 'gait10dof18musc_probed.osim')
    model.printToXML(name)

    assert (open(name).readlines() ==
            open(name.replace('.osim', '_desired.osim')).readlines())

    os.remove(name)
def test_markAdoptedSets():

    # Set's.
    fus = osim.FunctionSet()
    fu1 = osim.Constant()
    fus.adoptAndAppend(fu1)
    del fus
    del fu1

    s = osim.ScaleSet()
    o = osim.Scale()
    s.adoptAndAppend(o)
    del s
    del o

    s = osim.ControlSet()
    o = osim.ControlLinear()
    s.adoptAndAppend(o)
    del s
    del o

    s = osim.BodyScaleSet()
    o = osim.BodyScale()
    s.adoptAndAppend(o)
    del s
    del o

    s = osim.PathPointSet()
    o = osim.PathPoint()
    s.adoptAndAppend(o)
    del s
    del o

    s = osim.IKTaskSet()
    o = osim.IKMarkerTask()
    s.adoptAndAppend(o)
    del s
    del o

    s = osim.MarkerPairSet()
    o = osim.MarkerPair()
    s.adoptAndAppend(o)
    del s
    del o

    s = osim.MeasurementSet()
    o = osim.Measurement()
    s.adoptAndAppend(o)
    del s
    del o

    s = osim.FrameSet()
    o = osim.Body()
    s.adoptAndAppend(o)
    del s
    del o

    s = osim.ForceSet()
    o = osim.CoordinateLimitForce()
    s.adoptAndAppend(o)
    del s
    del o
     
    s = osim.ForceSet()
    o = osim.SpringGeneralizedForce()
    s.append(o)
 
    s = osim.ProbeSet()
    o = osim.Umberger2010MuscleMetabolicsProbe()
    s.adoptAndAppend(o)
    del s
    del o

    a = osim.Model()
    body = osim.Body('body',
            1.0,
            osim.Vec3(0, 0, 0),
            osim.Inertia(0, 0, 0)
            )

    loc_in_parent = osim.Vec3(0, -0, 0)
    orient_in_parent = osim.Vec3(0, 0, 0)
    loc_in_body = osim.Vec3(0, 0, 0)
    orient_in_body = osim.Vec3(0, 0, 0)
    joint = osim.WeldJoint("weld_joint",
            a.getGround(),
            loc_in_parent, orient_in_parent,
            body,
            loc_in_body, orient_in_parent)
    a.addBody(body)


    constr = osim.ConstantDistanceConstraint()
    constr.setBody1ByName("ground")
    constr.setBody1PointLocation(osim.Vec3(0, 0, 0))
    constr.setBody2ByName("body")
    constr.setBody2PointLocation(osim.Vec3(1, 0, 0))
    constr.setConstantDistance(1)
    a.addConstraint(constr)
Пример #5
0
def createHangingMuscleModel(ignore_activation_dynamics,
                             ignore_tendon_compliance):
    model = osim.Model()
    model.setName("hanging_muscle")
    model.set_gravity(osim.Vec3(9.81, 0, 0))
    body = osim.Body("body", 0.5, osim.Vec3(0), osim.Inertia(1))
    model.addComponent(body)

    # Allows translation along x.
    joint = osim.SliderJoint("joint", model.getGround(), body)
    coord = joint.updCoordinate()
    coord.setName("height")
    model.addComponent(joint)

    # The point mass is supported by a muscle.
    # The DeGrooteFregly2016Muscle is the only muscle model in OpenSim that
    # has been tested with Moco.
    actu = osim.DeGrooteFregly2016Muscle()
    actu.setName("muscle")
    actu.set_max_isometric_force(30.0)
    actu.set_optimal_fiber_length(0.10)
    actu.set_tendon_slack_length(0.05)
    actu.set_tendon_strain_at_one_norm_force(0.10)
    actu.set_ignore_activation_dynamics(ignore_activation_dynamics)
    actu.set_ignore_tendon_compliance(ignore_tendon_compliance)
    actu.set_fiber_damping(0.01)
    # The DeGrooteFregly2016Muscle is the only muscle model in OpenSim that
    # can express its tendon compliance dynamics using an implicit
    # differential equation.
    actu.set_tendon_compliance_dynamics_mode("implicit")
    actu.set_max_contraction_velocity(10)
    actu.set_pennation_angle_at_optimal(0.10)
    actu.addNewPathPoint("origin", model.updGround(), osim.Vec3(0))
    actu.addNewPathPoint("insertion", body, osim.Vec3(0))
    model.addForce(actu)

    # Add metabolics probes: one for the total metabolic rate,
    # and one for each term in the metabolics model.
    probe = osim.Umberger2010MuscleMetabolicsProbe()
    probe.setName("metabolics")
    probe.addMuscle("muscle", 0.5)
    model.addProbe(probe)

    probe = osim.Umberger2010MuscleMetabolicsProbe()
    probe.setName("activation_maintenance_rate")
    probe.set_activation_maintenance_rate_on(True)
    probe.set_shortening_rate_on(False)
    probe.set_basal_rate_on(False)
    probe.set_mechanical_work_rate_on(False)
    probe.addMuscle("muscle", 0.5)
    model.addProbe(probe)

    probe = osim.Umberger2010MuscleMetabolicsProbe()
    probe.setName("shortening_rate")
    probe.set_activation_maintenance_rate_on(False)
    probe.set_shortening_rate_on(True)
    probe.set_basal_rate_on(False)
    probe.set_mechanical_work_rate_on(False)
    probe.addMuscle("muscle", 0.5)
    model.addProbe(probe)

    probe = osim.Umberger2010MuscleMetabolicsProbe()
    probe.setName("basal_rate")
    probe.set_activation_maintenance_rate_on(False)
    probe.set_shortening_rate_on(False)
    probe.set_basal_rate_on(True)
    probe.set_mechanical_work_rate_on(False)
    probe.addMuscle("muscle", 0.5)
    model.addProbe(probe)

    probe = osim.Umberger2010MuscleMetabolicsProbe()
    probe.setName("mechanical_work_rate")
    probe.set_activation_maintenance_rate_on(False)
    probe.set_shortening_rate_on(False)
    probe.set_basal_rate_on(False)
    probe.set_mechanical_work_rate_on(True)
    probe.addMuscle("muscle", 0.5)
    model.addProbe(probe)

    body.attachGeometry(osim.Sphere(0.05))

    model.finalizeConnections()

    return model