Hi OpenSim-users,
For our study we have to perform the joint reaction analysis and muscle analysis for a lot of trials. Because performing all trials manually in the Analyze-tool in OpenSim is very time demanding, we want to use Matlab to perform these analysis all in ones. However, when using de option 'AnalyzeTool' in Matlab, Matlab crashes and gives the error in the attachment. Is anyone familiar with this problem and knows how to solve this problem?
We succeeded to perform de InverseKinematicsTool and InverseDynamicsTool in Matlab, so we are wondering why this AnalyzeTool does not work.
AnalyzeTool Matlab
- Lars van Rengs
- Posts: 16
- Joined: Sat Jan 08, 2022 7:21 am
Re: AnalyzeTool Matlab
Hi Lars,
I recall encountering this problem but I'm not 100% sure if this is the right solution. I assume you are creating the AnalyzeTool and then calling it using AnalyzeTool.run(). Something I've found that can solve these random crashes is to save the tool - i.e. AnalyzeTool.print('file.xml') - and then reload the tool to run it - i.e. runTool = AnalyzeTool('file.xml') > runTool.run()
This might solve your problem, but it also might not...
Aaron
I recall encountering this problem but I'm not 100% sure if this is the right solution. I assume you are creating the AnalyzeTool and then calling it using AnalyzeTool.run(). Something I've found that can solve these random crashes is to save the tool - i.e. AnalyzeTool.print('file.xml') - and then reload the tool to run it - i.e. runTool = AnalyzeTool('file.xml') > runTool.run()
This might solve your problem, but it also might not...
Aaron
- Lars van Rengs
- Posts: 16
- Joined: Sat Jan 08, 2022 7:21 am
Re: AnalyzeTool Matlab
Hi Aaron Fox,
First of all, thank you very much for your quick response.
We did some changes in de xml-file and now it is working pretty fine. We have one last question about the AnalyzeTool. To perform a Joint Reaction Analysis we have to select a force_file in the AnalysisSet (in the attached xml-file: AnalysisSet.objects.JointReaction.forces_file). This force_file has to change for every subject and trial but we don't know how to do this in de xml setup file.
For the other options in the xml-file we simple use the code below, but for this force_file we don't know what kind of code we need to change this parameter for every trial (i.e. we want to use the outname (Sub01_278ms_level) as a variable in this xml-line for the force_file but we don't know the right method to do so).
analyzeTool = AnalyzeTool(fullfile('JRA_tool_set-up.xml'));
analyzeTool.setName(outname);
analyzeTool.setModel(MyModel);
analyzeTool.setModelFilename(char(model_in));
analyzeTool.setResultsDir(output_jra);
analyzeTool.setInitialTime(Times(1,1));
analyzeTool.setFinalTime(Times(2,1));
analyzeTool.setExternalLoadsFileName(fullfile(path_output,['ExternalLoads\' outname '_ExternalLoads.xml']));
analyzeTool.setCoordinatesFileName(IkFile);
analyzeTool.setLowpassCutoffFrequency(ik_filter);
analyzeTool.print(fullfile(setupid_jra,[outname '_JRA_setup.xml']));
analyzeTool.run();
Lars
First of all, thank you very much for your quick response.
We did some changes in de xml-file and now it is working pretty fine. We have one last question about the AnalyzeTool. To perform a Joint Reaction Analysis we have to select a force_file in the AnalysisSet (in the attached xml-file: AnalysisSet.objects.JointReaction.forces_file). This force_file has to change for every subject and trial but we don't know how to do this in de xml setup file.
For the other options in the xml-file we simple use the code below, but for this force_file we don't know what kind of code we need to change this parameter for every trial (i.e. we want to use the outname (Sub01_278ms_level) as a variable in this xml-line for the force_file but we don't know the right method to do so).
analyzeTool = AnalyzeTool(fullfile('JRA_tool_set-up.xml'));
analyzeTool.setName(outname);
analyzeTool.setModel(MyModel);
analyzeTool.setModelFilename(char(model_in));
analyzeTool.setResultsDir(output_jra);
analyzeTool.setInitialTime(Times(1,1));
analyzeTool.setFinalTime(Times(2,1));
analyzeTool.setExternalLoadsFileName(fullfile(path_output,['ExternalLoads\' outname '_ExternalLoads.xml']));
analyzeTool.setCoordinatesFileName(IkFile);
analyzeTool.setLowpassCutoffFrequency(ik_filter);
analyzeTool.print(fullfile(setupid_jra,[outname '_JRA_setup.xml']));
analyzeTool.run();
Lars
- Attachments
-
- Sub_01_278ms_level_JRA_setup.xml
- (6.16 KiB) Downloaded 64 times
- Mohammadreza Rezaie
- Posts: 407
- Joined: Fri Nov 24, 2017 12:48 am
Re: AnalyzeTool Matlab
Hi,
You can run Analyze tool directly by the following changes:
You can also access to the JointReaction properties by calling the tool and appending it to the analyzeTool:
Hope this helps.
Regards,
Mohammadreza
You can run Analyze tool directly by the following changes:
Code: Select all
model = Model(PATH_TO_MODEL);
state = model.initSystem();
analyzeTool = AnalyzeTool(model);
analyzeTool.loadStatesFromFile(state);
Code: Select all
JR = JointReaction()
JR.setForcesFileName(PATH_TO_FORCE_FILE)
frame = osim.ArrayStr()
frame.set(0, 'child')
JR.setInFrame(frame)
analyzeTool.getAnalysisSet().cloneAndAppend(JR)
analyzeTool.addAnalysisSetToModel()
Regards,
Mohammadreza
- Lars van Rengs
- Posts: 16
- Joined: Sat Jan 08, 2022 7:21 am
Re: AnalyzeTool Matlab
Hi,
Thanks for you reply Mohammadreza Rezaie. Now it's clear how to use the Joint Reaction in the AnalyzeTool in Matlab. In this way it's also possible to change the joints, bodies en frames of the joint reaction forces.
Lars
Thanks for you reply Mohammadreza Rezaie. Now it's clear how to use the Joint Reaction in the AnalyzeTool in Matlab. In this way it's also possible to change the joints, bodies en frames of the joint reaction forces.
Lars
Re: AnalyzeTool Matlab
Hi All,
I am encountering a similair problem. My Matlab often crashes or closes alltogether when I am running my adjusted run_analyze script (My script runs both the SO and JRA). I have adjusted my model according to the proposal of Aaron. This however, does not seem to work for me. Would anyone be able to tell me what could cause my model to crash? I have added my custom code down below.
I have written the code to sequentially analyze 1 .trc file with the corresponding scaled model. The required model scaling and IK is done using different script. the variables yourfolder and yourfolder2 direct Matlab to the proper folder on my computer.
The Matlab Crash Report gives the following description:
Java Crash Report:
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000022f4c530080, pid=21764, tid=0x0000000000003528
#
# JRE version: Java(TM) SE Runtime Environment (8.0_202-b08) (build 1.8.0_202-b08)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.202-b08 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C 0x0000022f4c530080
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
I am encountering a similair problem. My Matlab often crashes or closes alltogether when I am running my adjusted run_analyze script (My script runs both the SO and JRA). I have adjusted my model according to the proposal of Aaron. This however, does not seem to work for me. Would anyone be able to tell me what could cause my model to crash? I have added my custom code down below.
I have written the code to sequentially analyze 1 .trc file with the corresponding scaled model. The required model scaling and IK is done using different script. the variables yourfolder and yourfolder2 direct Matlab to the proper folder on my computer.
The Matlab Crash Report gives the following description:
Java Crash Report:
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000022f4c530080, pid=21764, tid=0x0000000000003528
#
# JRE version: Java(TM) SE Runtime Environment (8.0_202-b08) (build 1.8.0_202-b08)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.202-b08 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C 0x0000022f4c530080
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
Code: Select all
% Example AnalyseTool script using SO
% Load OpenSim Libs
import org.opensim.modeling.*
%% Define file names and paths
%LKneeMarker
% yourfolder = ['W:\staff-umbrella\MultiscaleModelling\Data\S0\GaitAnalysis\Workingfiles_David\Msc_Thesis\MSM_Msc_Project\OpenSim_JAM\opensim-jam-release\analysis\MatlabOpenSimFunctions\New\L.KneeMarkerStatic'];
% yourfolder2 = ['W:\staff-umbrella\MultiscaleModelling\Data\S0\GaitAnalysis\Workingfiles_David\Msc_Thesis\MSM_Msc_Project\OpenSim_JAM\opensim-jam-release\analysis\MatlabOpenSimFunctions\BatchIKResults\LKneeMarker']
% fold = 1 %ResultsDir
%LKneeMedMarker
yourfolder = ['W:\staff-umbrella\MultiscaleModelling\Data\S0\GaitAnalysis\Workingfiles_David\Msc_Thesis\MSM_Msc_Project\OpenSim_JAM\opensim-jam-release\analysis\MatlabOpenSimFunctions\New\L.KneeMedMarkerStatic'];
yourfolder2 = ['W:\staff-umbrella\MultiscaleModelling\Data\S0\GaitAnalysis\Workingfiles_David\Msc_Thesis\MSM_Msc_Project\OpenSim_JAM\opensim-jam-release\analysis\MatlabOpenSimFunctions\BatchIKResults\LKneeMedMarker']
fold = 2 %ResultsDir
%LKneeFibMarker
%yourfolder = ['W:\staff-umbrella\MultiscaleModelling\Data\S0\GaitAnalysis\Workingfiles_David\Msc_Thesis\MSM_Msc_Project\OpenSim_JAM\opensim-jam-release\analysis\MatlabOpenSimFunctions\New\L.KneeFibMarkerStatic'];
%yourfolder2 = ['W:\staff-umbrella\MultiscaleModelling\Data\S0\GaitAnalysis\Workingfiles_David\Msc_Thesis\MSM_Msc_Project\OpenSim_JAM\opensim-jam-release\analysis\MatlabOpenSimFunctions\BatchIKResults\LKneeFibMarker']
%fold = 3 %ResultsDir
a=dir([yourfolder '/*.osim']);
TempTable = struct2table(a);
TempTable = sortrows(TempTable, 'date');
a = table2struct(TempTable);
nTrials =size(a,1);
b=dir([yourfolder2 '/*.mot']);
TempTable = struct2table(b);
TempTable = sortrows(TempTable, 'date');
b = table2struct(TempTable);
for trial = 1:nTrials
modelFileName = [a(trial).name]
forceSetFileName = 'W:\staff-umbrella\MultiscaleModelling\Data\S0\GaitAnalysis\Workingfiles_David\Msc_Thesis\MSM_Msc_Project\OpenSim_JAM\opensim-jam-release\analysis\MatlabOpenSimFunctions\WiSn_SO_Actuators.xml';
externalLoadsFileName = 'W:\staff-umbrella\MultiscaleModelling\Data\S0\GaitAnalysis\Workingfiles_David\Msc_Thesis\MSM_Msc_Project\OpenSim_JAM\opensim-jam-release\analysis\MatlabOpenSimFunctions\GRFw7_3.xml';
coordinatesFileName = fullfile(yourfolder2,b(trial).name) ;
%Pick resultsdir
if fold == 1
resultsDir = sprintf('LKneeMarkerResults_%d',trial);
end
if fold == 2
resultsDir = sprintf('LKneeMedMarkerResults_%d',trial);
end
if fold ==3
resultsDir = sprintf('LKneeFibMarkerResults_%d',trial);
end
sTime = 5.84;
fTime = 6.62;
cutoff = 6;
setup_file_name = sprintf('SO_Setup_%s.xml',modelFileName(1:end-5));
%% Build SO analysis (Part I)
so = StaticOptimization();
so.setOn(true);
so.setStartTime(sTime);
so.setEndTime(fTime);
%% Instantiate an AnalyzeTool and set the properties
at = AnalyzeTool();
%at.setModel(Model(modelFileName));%fullfile(yourfolder,a(trial).name))); %adjusted used to be only modelFileName after Model
%at.setModelFilename(modelFileName)
%at.setForceSetFiles(ArrayStr(forceSetFileName));
A=ArrayStr();
A.set(0,forceSetFileName);
at.setForceSetFiles(A);
%at.setResultsDir(resultsDir);
at.setInitialTime(sTime);
at.setFinalTime(fTime);
at.setSolveForEquilibrium(false);
at.setExternalLoadsFileName(externalLoadsFileName);
at.setCoordinatesFileName(coordinatesFileName);
at.setLowpassCutoffFrequency(cutoff);
%% Work-around for setForceSetFiles() of AnalyzeTool not being honored at execution in 4.0API
model = Model(fullfile(yourfolder, 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)
%ModelFileName = modelFileName_actuated
at.setModel(model)
at.setModelFilename(modelFileName_actuated)
%copyfile modelfileName_actuated resultsDir;
% Add SO to the tool
at.getAnalysisSet().cloneAndAppend(so)
%% Print the tool to file
Result_Folder=[resultsDir];
setupFileDir=[Result_Folder];
if exist(setupFileDir,'dir') ~= 7
mkdir(setupFileDir);
end
[status,message,messageId] = copyfile(modelFileName_actuated, setupFileDir, 'f');
status
at.print([setupFileDir '\' setup_file_name ]);
% We have to re instanstiate so that the tool can build a states storage
% internally
runTool = AnalyzeTool([setupFileDir '\' setup_file_name ]); %at = AnalyzeTool(setup_file_name);
%% Run the tool
runTool.run(); %Do not forget to uncheck when running SO
%- i.e. AnalyzeTool.print('file.xml') - and then reload the tool to run it - i.e. runTool = AnalyzeTool('file.xml') > runTool.run()
% %% Build JRA Analysis
% JR = JointReaction()
% JR.setForcesFileName(fullfile(sprintf('BatchSOResults_%d',trial),'_StaticOptimization_forces.sto'))
% frame = osim.ArrayStr()
% frame.set(0, 'child')
% JR.setInFrame(frame)
%
% analyzeTool.getAnalysisSet().cloneAndAppend(JR)
% analyzeTool.addAnalysisSetToModel()
% JR.run();
%% Setup for JRF (Part II)
osimModel = Model(modelFileName_actuated);
osimModel.initSystem();
%analyzeTool()
analyzeTool=AnalyzeTool() %'JRA_testw7_11-06-2022.xml');
%Set the model
analyzeTool.setModel(osimModel);
analyzeTool.setModelFilename(osimModel.getDocumentFileName());
% Analyse_Name=SOForce_FileName(1:end-29)
% analyzeTool.setName(Analyse_Name)
% Set the Name
% Name=[C{end-3},'_',C{end-1}]
Name=sprintf('JRAResults_%d',trial)
% Name= Name(find(~isspace(Name)));
analyzeTool.setName(Name)
%%
%if exist(Result_Folder,'dir') ~= 7
% mkdir (Result_Folder);
%end
% Set Actuator
% Instantiate a String Array
%as = ArrayStr();
% Set the first value of the array to the actuators file
%as.set(0,Actuator_directory);
% Set the force set file to use the new array
%analyzeTool.setForceSetFiles(ArrayStr('WiSn_SO_Actuators.xml'))
A=ArrayStr();
A.set(0,forceSetFileName);
analyzeTool.setForceSetFiles(A);
analyzeTool.setReplaceForceSet(false);
%analyzeTool.setResultsDir(setupFileDir)%resultsDir);
analyzeTool.setOutputPrecision(8)
% Get mot data to determine time range
%motData = Storage(SOForce_directory);
% Get initial and intial time
%initial_time = motData.getFirstTime();
%final_time = motData.getLastTime();
analyzeTool.setInitialTime(sTime);
analyzeTool.setFinalTime(fTime);
% Other set up
analyzeTool.setSolveForEquilibrium(false);
analyzeTool.setMaximumNumberOfSteps(20000);
%analyzeTool.setMaxDT(1e-005)
analyzeTool.setMaxDT(1);
analyzeTool.setMinDT(1e-008);
analyzeTool.setErrorTolerance(1e-005);
%% Joint reaction Analysis
%ma = createMuscleAnalysisObj(initial_time, final_time);
%Append to analyzeTool
%analyzeTool.getAnalysisSet().adoptAndAppend(ma);
%since setComputeMoments(true) from API does not work, it is necessary to
%load the XMLTemplate, and set just initial and final time
%
% analyzeTool.getAnalysisSet().get(0).setStartTime(initial_time);
% analyzeTool.getAnalysisSet().get(0).setEndTime(final_time);
% Create a JR Object and load in Analysis
% import org.opensim.modeling.*
%analysis=analyzeTool.getAnalysisSet.get(0);
%analysis.getConcreteClassName
%NewJRF =JointReaction.safeDownCast(analysis );
NewJRF = JointReaction();
NewJRF.setName('JointReaction');
NewJRF.setOn(true);
NewJRF.setStartTime(sTime);
NewJRF.setEndTime(fTime);
NewJRF.setStepInterval(1);
NewJRF.setInDegrees(true);
NewJRF.setForcesFileName(fullfile('W:\staff-umbrella\MultiscaleModelling\Data\S0\GaitAnalysis\Workingfiles_David\Msc_Thesis\MSM_Msc_Project\OpenSim_JAM\opensim-jam-release\analysis\MatlabOpenSimFunctions',resultsDir ,'_StaticOptimization_force.sto'));
%Assign bodies JRF are applied to
bodies = ArrayStr();
bodies.append('child');
NewJRF.setOnBody(bodies);
%Assign bodies to express JRF in
expIn = ArrayStr();
expIn.append('child');
NewJRF.setInFrame(expIn);
%NewJRF.setForceSetFiles(fullfile(sprintf('BatchSOResults_%d',trial),'_StaticOptimization_forces.sto'));
%NewJRF.loadForcesFromFile(fullfile(sprintf('BatchSOResults_%d',trial),'_StaticOptimization_forces.sto'));
%% Save the settings in the Setup folder
%Result_Folder = sprintf('BatchSOResults_%d',trial);
%setupFileDir=[Result_Folder '\Setup'];
% if exist(setupFileDir,'dir') ~= 7
% mkdir (setupFileDir);
% end
%
setupFile=[sprintf('JRA_Setupw7_%d_20.xml',trial)]%['JRA_testw7_11-06-2022.xml'];
setupFile= setupFile(find(~isspace(setupFile)));
%%
analyzeTool.setExternalLoadsFileName(externalLoadsFileName);
analyzeTool.setCoordinatesFileName(coordinatesFileName);
analyzeTool.setLowpassCutoffFrequency(cutoff);
% if selections.JRALowPass >=1
% analyzeTool.setLowpassCutoffFrequency(selections.JRALowPass);
% else
% analyzeTool.setLowpassCutoffFrequency(-1); %the default value is -1.0, so no filtering
% end
%% Save set up in XML file
%Append to analyzeTool
%analyzeTool.getAnalysisSet.get(0).delete
analyzeTool.print([setupFileDir '\' setupFile ]);
analyzeTool.getAnalysisSet().adoptAndAppend(NewJRF);
analyzeTool.addAnalysisSetToModel();
%at.getAnalysisSet().cloneAndAppend(NewJRF)
% analyzeTool.print([setupFileDir '\' setupFilepost ]);
analyzeTool.setLoadModelAndInput(true); %Another problem is the motion vs state input. If the coordinates are specified from a .mot file (e.g. IK) then the _loadModelAndInput must be set to true, which is not well documented. This should be handled automatically when the user sets the corresponding inp
analyzeTool.print([setupFileDir '\' setupFile ]);
%
% %% Run
% analyzeTool.run
% internally
runTool = AnalyzeTool([setupFileDir '\' setupFile]); %at = AnalyzeTool(setup_file_name);
%% Run the tool
runTool.run();
%runAnalyzeTool(setupFileDir, setupFile);
% % RUNTOOL=analyzeTool.run
% %Save the log file in a Log folder for each trial
% logFolder=[Result_Folder '\Log'];
% if exist(logFolder,'dir') ~= 7
% mkdir (logFolder);
% end
% % movefile([setupFileDir '\out.log'],[logFolder '\out.log'])
% % movefile([setupFileDir '\err.log'],[logFolder '\err.log'])
% cd(originalPath)
end