Running Static Optimization in Matlab

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Md Sanzid Bin Hossain
Posts: 15
Joined: Mon Aug 26, 2019 7:58 am

Running Static Optimization in Matlab

Post by Md Sanzid Bin Hossain » Mon Sep 30, 2019 2:27 pm

I run Static Optimization(SO) in the OpenSim software and save the setup file to run SO in Matlab.

When I run a specific setup file in the Matlab, the optimizer failed all the time. It seems that Matlab generates the result without considering the additional force set file (strong actuator file) although actuator file is present in the setup file. So, Matlab generates a similar result of OpenSim when we do SO without additional force file (Strong actuator file) in the OpenSim.

Can anyone please figure out what went wrong?

Tags:

User avatar
Md Sanzid Bin Hossain
Posts: 15
Joined: Mon Aug 26, 2019 7:58 am

Re: Running Static Optimization in Matlab

Post by Md Sanzid Bin Hossain » Fri Oct 04, 2019 1:55 pm



Here is the code for my simulation. I add the forceset file in the code. but it doesn't take account this actuator file. As a result, optimizer fails even though I have appended the actuator file in the .XML setup file.

Can anyone figure out what went wrong?

Code: Select all

clc;
close all;
clear all;

import org.opensim.modeling.*

model=Model('subject01.osim');
XMLTemplate="static_setup.XML";
Force_set='gait10dof18musc_Strong_actuators.xml';

%% 
analysis=AnalyzeTool(XMLTemplate,0);
analysis.setReplaceForceSet(false);
forceSet = ArrayStr();
forceSet.append(Force_set);
analysis.setForceSetFiles(forceSet);

 %% Model Setting %%

analysis.setModel(model);
analysis.setModelFilename(model.getDocumentFileName());
analysis.setLoadModelAndInput(1);

  %%  Analysis settings  %%

analysis.setInitialTime(0.3)
analysis.setFinalTime(0.32)
setup='static_setup_2.XML';
analysis.print(setup)
analysis.run();

User avatar
Thomas Uchida
Posts: 1793
Joined: Wed May 16, 2012 11:40 am

Re: Running Static Optimization in Matlab

Post by Thomas Uchida » Sat Oct 05, 2019 7:12 pm

Can anyone figure out what went wrong?
Please refer to the API documentation for ForceSet (https://simtk.org/api_docs/opensim/api_ ... ceSet.html). It looks like you are passing a string (a filename) as an argument to ForceSet::append(), which I don't see in the documentation.

Code: Select all

Force_set='gait10dof18musc_Strong_actuators.xml';
...
forceSet.append(Force_set);

User avatar
Md Sanzid Bin Hossain
Posts: 15
Joined: Mon Aug 26, 2019 7:58 am

Re: Running Static Optimization in Matlab

Post by Md Sanzid Bin Hossain » Mon Oct 07, 2019 10:33 am

I can actually run the code without appending the forceset. I just save the setup file of the OpenSim when I run static optimization. I append the actuator forceset in the setup. So the actuator forceset is present in the .XML setup file which can be seen from the file below. But when I run the same setup file in the Matlab, it simply does not take into account this force file. That's why optimizer fails.

I hope you understand the problem.

Code: Select all


.XML file
<?xml version="1.0" encoding="UTF-8" ?>
<OpenSimDocument Version="40000">
	<AnalyzeTool name="Scaled_Model">
		<!--Name of the .osim file used to construct a model.-->
		<model_file>subject01.osim</model_file>
		<!--Replace the model's force set with sets specified in <force_set_files>? If false, the force set is appended to.-->
		<replace_force_set>false</replace_force_set>
		<!--List of xml files used to construct a force set for the model.-->
		<force_set_files> gait10dof18musc_Strong_actuators.xml</force_set_files>
		<!--Directory used for writing results.-->
		<results_directory>Final_result</results_directory>
		<!--Output precision.  It is 8 by default.-->
		<output_precision>8</output_precision>
		<!--Initial time for the simulation.-->
		<initial_time>0.29999999999999999</initial_time>
		<!--Final time for the simulation.-->
		<final_time>0.32000000000000001</final_time>
		<!--Flag indicating whether or not to compute equilibrium values for states other than the coordinates or speeds.  For example, equilibrium muscle fiber lengths or muscle forces.-->
		<solve_for_equilibrium_for_auxiliary_states>false</solve_for_equilibrium_for_auxiliary_states>
		<!--Maximum number of integrator steps.-->
		<maximum_number_of_integrator_steps>20000</maximum_number_of_integrator_steps>
		<!--Maximum integration step size.-->
		<maximum_integrator_step_size>1</maximum_integrator_step_size>
		<!--Minimum integration step size.-->
		<minimum_integrator_step_size>1e-08</minimum_integrator_step_size>
		<!--Integrator error tolerance. When the error is greater, the integrator step size is decreased.-->
		<integrator_error_tolerance>1.0000000000000001e-05</integrator_error_tolerance>
		<!--Set of analyses to be run during the investigation.-->
		<AnalysisSet name="Analyses">
			<objects>
				<StaticOptimization name="StaticOptimization">
					<!--Flag (true or false) specifying whether on. True by default.-->
					<on>true</on>
					<!--Start time.-->
					<start_time>0.29999999999999999</start_time>
					<!--End time.-->
					<end_time>1.5</end_time>
					<!--Specifies how often to store results during a simulation. More specifically, the interval (a positive integer) specifies how many successful integration steps should be taken before results are recorded again.-->
					<step_interval>1</step_interval>
					<!--Flag (true or false) indicating whether the results are in degrees or not.-->
					<in_degrees>true</in_degrees>
					<!--If true, the model's own force set will be used in the static optimization computation.  Otherwise, inverse dynamics for coordinate actuators will be computed for all unconstrained degrees of freedom.-->
					<use_model_force_set>true</use_model_force_set>
					<!--A double indicating the exponent to raise activations to when solving static optimization.  -->
					<activation_exponent>2</activation_exponent>
					<!--If true muscle force-length curve is observed while running optimization.-->
					<use_muscle_physiology>true</use_muscle_physiology>
					<!--Value used to determine when the optimization solution has converged-->
					<optimizer_convergence_criterion>0.0001</optimizer_convergence_criterion>
					<!--An integer for setting the maximum number of iterations the optimizer can use at each time.  -->
					<optimizer_max_iterations>100</optimizer_max_iterations>
				</StaticOptimization>
			</objects>
			<groups />
		</AnalysisSet>
		<!--Controller objects in the model.-->
		<ControllerSet name="Controllers">
			<objects />
			<groups />
		</ControllerSet>
		<!--XML file (.xml) containing the forces applied to the model as ExternalLoads.-->
		<external_loads_file>subject01_walk_grf.xml</external_loads_file>
		<!--Storage file (.sto) containing the time history of states for the model. This file often contains multiple rows of data, each row being a time-stamped array of states. The first column contains the time.  The rest of the columns contain the states in the order appropriate for the model. In a storage file, unlike a motion file (.mot), non-uniform time spacing is allowed.  If the user-specified initial time for a simulation does not correspond exactly to one of the time stamps in this file, interpolation is NOT used because it is sometimes necessary to use an exact set of states for analyses.  Instead, the closest earlier set of states is used.-->
		<states_file />
		<!--Motion file (.mot) or storage file (.sto) containing the time history of the generalized coordinates for the model. These can be specified in place of the states file.-->
		<coordinates_file>subject_adjusted_Kinematics_q.sto</coordinates_file>
		<!--Storage file (.sto) containing the time history of the generalized speeds for the model. If coordinates_file is used in place of states_file, these can be optionally set as well to give the speeds. If not specified, speeds will be computed from coordinates by differentiation.-->
		<speeds_file />
		<!--Low-pass cut-off frequency for filtering the coordinates_file data (currently does not apply to states_file or speeds_file). A negative value results in no filtering. The default value is -1.0, so no filtering.-->
		<lowpass_cutoff_frequency_for_coordinates>-1</lowpass_cutoff_frequency_for_coordinates>
	</AnalyzeTool>
</OpenSimDocument>




[code]
Matlab Code


clc;
close all;
clear all;

import org.opensim.modeling.*

model=Model('subject01.osim');
XMLTemplate="static_optimization_setup.XML";
analysis=AnalyzeTool(XMLTemplate,0);

analysis.setModel(model);
analysis.setModelFilename(model.getDocumentFileName());
analysis.setLoadModelAndInput(1);

analysis.setInitialTime(0.3)
analysis.setFinalTime(0.32)
analysis.run();

User avatar
jimmy d
Posts: 1375
Joined: Thu Oct 04, 2007 11:51 pm

Re: Running Static Optimization in Matlab

Post by jimmy d » Mon Nov 04, 2019 9:41 pm

Attached is an example folder that includes a script, run_analyzetool.m, that builds an AnalyzeTool from scratch and runs static optimization.

Goodluck
Attachments
matlab_static_optimization.zip
(513.73 KiB) Downloaded 152 times

User avatar
Adam Yoder
Posts: 16
Joined: Thu Jul 19, 2012 4:24 pm

Re: Running Static Optimization in Matlab

Post by Adam Yoder » Thu Oct 28, 2021 3:10 pm

I had the same issue, and the above *zip example also fails to converge for static opt if run in MATLAB API 4.3, due to missing appended actuators (I assume it may work in v3, but I didnt try)

Anyways if you paste this code snippet into run_analyzetool.m of the above zip, static opt will converge successfully

Code: Select all

%% Work-around for at.setForceSetFiles() not being honored
model = Model(modelFileName);
so_actuators = ForceSet(forceSetFileName);
forceset = model.updForceSet; 
for fi=(1:so_actuators.getSize)-1
    forceset.cloneAndAppend(so_actuators.get(fi));
end
modelFileName_actuated = strrep(modelFileName,'.osim','_actuators.osim');
model.print(modelFileName_actuated)

%% Instantiate an AnalyzeTool and set the properties
at = AnalyzeTool();
at.setModel(model);
at.setModelFilename(modelFileName_actuated)
More detail posted here:
viewtopicPhpbb.php?f=91&t=11791&p=40468 ... 8e8dedc123

User avatar
Mohammadhossein Akhavanfar
Posts: 19
Joined: Mon Oct 16, 2017 9:11 am

Re: Running Static Optimization in Matlab

Post by Mohammadhossein Akhavanfar » Mon Nov 22, 2021 1:44 pm

The only problem of the zip file is this line:

at.setForceSetFiles(ArrayStr(forceSetFileName));

The input of setForceSetFiles should be an ArrayStr. However, the way it was constructed is wrong. That line should be replaced with this:

A=ArrayStr();
A.set(0,forceSetFileName);
at.setForceSetFiles(A);

POST REPLY