Page 1 of 1

Extracting force analysis with manager tool in Matlab

Posted: Fri Mar 03, 2023 10:58 am
by atabakmehrabani
Hello,

I'm trying to run a simulation and I'm using Manager tool with MATLAB API. Simulation runs perfectly and I can get the states.sto file and investigate/visualize it but I can't seem to find the functions to print out the forces (or any type of analysis that you could add while running Forward dynamic within the GUI). I'm just not sure how to extract the forces after I ran the simulation...
It would be amazing if someone could help me with this or to reference a code/file... I attached the portion of the code that runs the simulation in my code

import org.opensim.modeling.*
modelPath='D:\...
model = Model(modelPath);

%adding Reporters
forcerep = ForceReporter(model);
model.addAnalysis(forcerep);

%Running Simulation
state = model.initSystem();

manager = Manager(model);
manager.setPerformAnalyses(true);
state.setTime(0);
manager.initialize(state);
state = manager.integrate(0.5);

sTable = manager.getStatesTable();
stofiles = STOFileAdapter();
if ~isdir('ResultsFWD')
mkdir ResultsFWD
end
stofiles.write(sTable, 'ResultsFWD/simulation_states.sto');

display('Simulation Finished.');

Re: Extracting force analysis with manager tool in Matlab

Posted: Tue Mar 07, 2023 11:46 pm
by coppersulfate
I'm not sure but maybe you can insert these sentences before 'state = model.initSystem();' to see if it works:
'Forceset=model.get_ForceSet();
reporter = ConsoleReporter();
reporter.set_report_time_interval(1.0); %1.0 is the time interval which can be changed
for i=0:N-1 %N is the number of objects in Forceset. I didn't find a function to measure the size of Forceset, so you have to check your model and specify it yourself
reporter.addToReport(Forceset.get(i).getOutput('fiber_force')); %'fiber_force' can also be 'fiber_length', 'activation' and 'tendon_force', as far as I know
end
model.addComponent(reporter);'
If it does work, please notify me.
By the way, do you have a way to specify the initial state? Or you just use the default values?