Running Static optimization in Matlab

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Maud Hendriksen
Posts: 20
Joined: Thu May 14, 2020 12:00 am

Running Static optimization in Matlab

Post by Maud Hendriksen » Thu Aug 06, 2020 3:22 am

Dear OpenSim users,

I am running the static optimization through matlab. I encounter troubles loading the force set file into the analyze tool. I am using the following code and after running the force setfile is not included in the xml setup file of the analyze tool. Is there anyone that knows what I'm doing wrong? Neither is it working when I run the tool with an existing xml setup file including the force set file in the setup file. (using this code line:analysis=AnalyzeTool(XMLTemplate,0) )

Code: Select all

for trial= 1%:ntrials
    
% external loads Xml file preparation 
external_loads = ExternalLoads(extLoadsXml,1); 
external_loads.setDataFileName(fullfile(Folder_N(1).folder,char(name1(trial)))) ;
external_loads.setLowpassCutoffFrequencyForLoadKinematics(6);
external_loads.print(fullfile(EL_folder, strcat('ELSetup_', regexprep(char(name(trial)),'_ik.mot','.xml'))));

% construct static optimization
    static_optimization = StaticOptimization();
    static_optimization.setStartTime(sTime);
    static_optimization.setEndTime(fTime);
    static_optimization.setUseModelForceSet(true);
    static_optimization.setUseMusclePhysiology(true);
    static_optimization.setActivationExponent(2);
    static_optimization.setConvergenceCriterion(0.0001);
    static_optimization.setMaxIterations(100);
    scaled_model.addAnalysis(static_optimization);

    analysis = AnalyzeTool(scaled_model);
    analysis.setName(strcat(regexprep(char(name(trial)),'_ik.mot','')));
    analysis.setModel(scaled_model);
    analysis.setForceSetFiles(ArrayStr(fullfile(AT_res(1).folder, 'gait2392_RRA_Actuators.xml')));
    analysis.setInitialTime(sTime);
    analysis.setFinalTime(fTime);
    analysis.setLowpassCutoffFrequency(6);
    analysis.setCoordinatesFileName(MotionFile);
    analysis.setExternalLoadsFileName(fullfile(EL_folder, strcat('ELSetup_', regexprep(char(name(trial)),'_ik.mot','.xml'))));
    analysis.setLoadModelAndInput(true)
    analysis.setResultsDir(AT_res(1).folder); 
    outfile = ['Setup_Analyze_' regexprep(char(name(trial)),'_ik.mot','.xml')];
    analysis.print(fullfile(AT_results, outfile));
    analysis.run()
    
end    
Thank you for any advice.

Tags:

User avatar
Ayman Habib
Posts: 2244
Joined: Fri Apr 01, 2005 12:24 pm

Re: Running Static optimization in Matlab

Post by Ayman Habib » Thu Aug 06, 2020 9:38 am

Hello Maud,

Typically these issues are related to path specification of files using relative path when it isn't always clear what the assumed current/working directory is at the time the file is accessed. My suggestion would be to create the setup file in the GUI, save it, make sure it runs (we go through a ton of hoops to make this work as smoothly as possible). Once done, you can use Matlab to "edit" the filename or content of the files from Matlab keeping in mind to preserve the same directory structure that works.

Matlab adds an extra layer of complexity with its own working directory and search paths, so you may want to test that your files work by running from the command line where you know for sure your working directory.

Hope this helps,
-Ayman

User avatar
Maud Hendriksen
Posts: 20
Joined: Thu May 14, 2020 12:00 am

Re: Running Static optimization in Matlab

Post by Maud Hendriksen » Tue Aug 11, 2020 12:13 am

Thank you for your help! It works now without using setModel and the xml setup file.

POST REPLY