Analyze Tool Set Force Files

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Abigail Eustace
Posts: 3
Joined: Wed Jan 13, 2016 8:21 pm

Analyze Tool Set Force Files

Post by Abigail Eustace » Thu Nov 01, 2018 9:06 am

I trying to perform static optimization in MatLab with the analyze tool but I am having difficulties applying reserves with the set force file command. It will print the correct path to the force set file in my setup file but it is not actually using the reserve file when running static optimization. When I run the same setup file that I printed from MatLab in OpenSim 3.3 it uses the reverse.

Here is the code I am running:

Code: Select all

   %% Static Optimization

        soTool = AnalyzeTool(fullfile(GeneralPath, '\', 'SetUp_SO_generic.xml'));
        
            soTool.setModel(model);
            soTool.setModelFilename(fullfile(modelFilePath,modelFile));
        soTool.setName(filelabel);
        soTool.setInitialTime(initial_time);
        soTool.setFinalTime(final_time);
        soTool.setResultsDir(SOPath);
        soTool.setCoordinatesFileName(fullfile(IKResultsPath,'\', sprintf('%s_IK.mot',filelabel)));
        soTool.setExternalLoadsFileName(fullfile(IDResultsPath, '\',sprintf('Setup_ExtLoads_%s.xml',filelabel)));
        % Instantiate a String Array
        Reserve = ArrayStr();
        % Int sets the number of spaces in the xml file
        Reserve.set(0, [GeneralPath '\' 'StaticTIGHT_Reserves.xml']);

        soTool.setForceSetFiles(Reserve);



        so_setup_file = ['Setup_SO_' filelabel '.xml'];
        soTool.print(fullfile(SOPath,'\',so_setup_file));
        
          display(['Performing SO on ' filelabel]);
          
          % Run Static Op Tool
          soTool.run()
I am thinking it is not reading the reserve file correctly in MatLab but I am not sure. Any suggestions would be greatly appreciated!

Tags:

User avatar
Dimitar Stanev
Posts: 1096
Joined: Fri Jan 31, 2014 5:14 am

Re: Analyze Tool Set Force Files

Post by Dimitar Stanev » Fri Nov 02, 2018 1:43 am

Hi,

This is the code that I use with Python, which can be translated directly:

Code: Select all

def perform_so(model_file, ik_file, grf_file, grf_xml, reserve_actuators,
               results_dir):
    """Performs Static Optimization using OpenSim.

    Parameters
    ----------
    model_file: str
        OpenSim model (.osim)
    ik_file: str
        kinematics calculated from Inverse Kinematics
    grf_file: str
        the ground reaction forces
    grf_xml: str
        xml description containing how to apply the GRF forces
    reserve_actuators: str
        path to the reserve actuator .xml file
    results_dir: str
        directory to store the results
    """
    # model
    model = opensim.Model(model_file)

    # prepare external forces xml file
    name = os.path.basename(grf_file)[:-8]
    external_loads = opensim.ExternalLoads(model, grf_xml)
    external_loads.setExternalLoadsModelKinematicsFileName(ik_file)
    external_loads.setDataFileName(grf_file)
    external_loads.setLowpassCutoffFrequencyForLoadKinematics(6)
    external_loads.printToXML(results_dir + name + '.xml')

    # add reserve actuators
    force_set = opensim.ForceSet(model, reserve_actuators)
    force_set.setMemoryOwner(False)  # model will be the owner
    for i in range(0, force_set.getSize()):
        model.updForceSet().append(force_set.get(i))

    # construct static optimization
    motion = opensim.Storage(ik_file)
    static_optimization = opensim.StaticOptimization()
    static_optimization.setStartTime(motion.getFirstTime())
    static_optimization.setEndTime(motion.getLastTime())
    static_optimization.setUseModelForceSet(True)
    static_optimization.setUseMusclePhysiology(True)
    static_optimization.setActivationExponent(2)
    static_optimization.setConvergenceCriterion(0.0001)
    static_optimization.setMaxIterations(100)
    model.addAnalysis(static_optimization)

    # analysis
    analysis = opensim.AnalyzeTool(model)
    analysis.setName(name)
    analysis.setModel(model)
    analysis.setInitialTime(motion.getFirstTime())
    analysis.setFinalTime(motion.getLastTime())
    analysis.setLowpassCutoffFrequency(6)
    analysis.setCoordinatesFileName(ik_file)
    analysis.setExternalLoadsFileName(results_dir + name + '.xml')
    analysis.setLoadModelAndInput(True)
    analysis.setResultsDir(results_dir)
    analysis.run()
    so_force_file = results_dir + name + '_StaticOptimization_force.sto'
    so_activations_file = results_dir + name + \
                          '_StaticOptimization_activation.sto'
    return (so_force_file, so_activations_file)
Hope this helps.

User avatar
Abigail Eustace
Posts: 3
Joined: Wed Jan 13, 2016 8:21 pm

Re: Analyze Tool Set Force Files

Post by Abigail Eustace » Fri Nov 02, 2018 8:58 am

I will try updating my code with that and see if it works. I do have one question is the "reserve file" that you have as an input to your opensim.ForceSet is that a xml file defining your reserves?

User avatar
Abigail Eustace
Posts: 3
Joined: Wed Jan 13, 2016 8:21 pm

Re: Analyze Tool Set Force Files

Post by Abigail Eustace » Fri Nov 02, 2018 1:24 pm

I was able to get it working with modifying your Python code. Thanks!

User avatar
Sven Paldauf
Posts: 9
Joined: Wed Jun 06, 2018 2:08 am

Re: Analyze Tool Set Force Files

Post by Sven Paldauf » Mon Apr 08, 2019 10:45 am

Hi Community,

I am trying to enter the <AnalysisSet> <objects> class with MatLab scripting, in particular the StaticOptimization to change <start_time> and <end_time> (see line 35 and 37 in the attached file).

Code: Select all

% here is a snippet of how far I got:
analysis = AnalyzeTool('myAnalyzeToolGenericSetup')
analysis.getAnalysisSet()
analysis.getAnalysisSet() returns the name of the AnalysisSet, but I cannot find the methods how to further go intho the StaticOptimization object.

To make it clearer, I am looking for something like:

analysis.getAnalysisSet().getObject().setStartTime(0.1)

analysis.getAnalysisSet().getObject().setEndTime(0.5)

Is there any possibility how to do this?

Further, I would like to know how I can specify an object in the AnalysisSet after creating it by Scripting

Code: Select all

        static_optimization = StaticOptimization()
        static_optimization.setStartTime(0.1)
        static_optimization.setEndTime(0.5)
        static_optimization.setUseModelForceSet(true)
        static_optimization.setUseMusclePhysiology(true)
        static_optimization.setActivationExponent(2)
        static_optimization.setConvergenceCriterion(0.0001)
        static_optimization.setMaxIterations(100)

        % Setup ANalyze Tool
        analysis = AnalyzeTool('myAnalyzeToolGenericSetup')
        % now, I want to do something like:
        analysis.getAnalysisSet().setObject(static_optimization)
In the last line, I miss the correct method to assign the Object. I really appreciate any help.


Thank you very much guys.

Sven
Attachments
SOSetupGen.xml
(5.54 KiB) Downloaded 10 times

User avatar
Dimitar Stanev
Posts: 1096
Joined: Fri Jan 31, 2014 5:14 am

Re: Analyze Tool Set Force Files

Post by Dimitar Stanev » Tue Apr 09, 2019 12:44 am

You must create the static optimization object and add it to the analysis tool as in the code above. You can get the analysis object as follows:

Code: Select all

analysis.getAnalysisSet().get(0).setStartTime(0.1)

User avatar
Sven Paldauf
Posts: 9
Joined: Wed Jun 06, 2018 2:08 am

Re: Analyze Tool Set Force Files

Post by Sven Paldauf » Sat Apr 13, 2019 9:20 am

Thank you Dimitar, that was exactly what I was looking for

POST REPLY