manager.getStatesTable and osimTableToStruct is very slow

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Delyle Polet
Posts: 12
Joined: Mon Nov 02, 2020 12:05 pm

manager.getStatesTable and osimTableToStruct is very slow

Post by Delyle Polet » Fri Aug 12, 2022 6:27 am

I am running forward dynamics for a passive dynamic rimless wheel via the OpenSim API in Matlab.

A big bottle neck is reading the states table from the manager, i.e. the line

Code: Select all

sTable = manager.getStatesTable()
, where manager is a member of the Manager class.

Another is the osimTableToStruct method, with sTable above as an argument.

For example, to run a 30s simulation, it takes the forward dynamics 7 s to run, while manager.getStatesTable() takes 9 s (all settings default for the integrator except minimum step size = 0.001), and the osimTableToStruct method takes 15 s.

For a 60s simulation, it takes the forward dynamics 14 s, getStatesTable 40 s and osimTableToStruct 29 s.

I'm wondering if there is an alternative to getStatesTable and osimTableToStruct? Or is there a way to speed up those processes themselves?

I don't need all the data this is spitting out, and would be fine extracting the states at every 10 time steps. Is there a way to do that, and will it speed things up?

Tags:

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

Re: manager.getStatesTable and osimTableToStruct is very slow

Post by Thomas Uchida » Fri Aug 12, 2022 7:16 pm

You could try the StatesTrajectoryReporter (https://simtk.org/api_docs/opensim/api_ ... orter.html), which might be faster and also allows you to set the time interval between rows in the table (via the set_report_time_interval() method, https://simtk.org/api_docs/opensim/api_ ... 7841134779). Alternatively, if you just need a few states (e.g., coordinates of joints or locations of markers), you could use a TableReporter (https://simtk.org/api_docs/opensim/api_ ... ter__.html). Some examples can be found on GitHub:
- StatesTrajectoryReporter: https://github.com/opensim-org/opensim- ... ryReporter
- TableReporter: https://github.com/opensim-org/opensim- ... leReporter

User avatar
Delyle Polet
Posts: 12
Joined: Mon Nov 02, 2020 12:05 pm

Re: manager.getStatesTable and osimTableToStruct is very slow

Post by Delyle Polet » Wed Sep 21, 2022 8:38 am

Thank you Thomas!

After looking through the examples, I got the StatesTrajectoryReporter to work. Now, for a 30 second simulation, writing to a table takes 0.07 seconds, and osimTableToStruct takes 1.5 seconds (a 100 fold improvement!)

For posterity, here's the relevant MATLAB code using StatesTrajectoryReporter

Code: Select all

osimModel = Model(fileName);

% set up a StatesTrajectoryReporter
reporter = StatesTrajectoryReporter();
reporter.setName('reporter');
reporter.set_report_time_interval(0.01); % Note: for my setup, 0.01 is 10x the minimum integrator step size
osimModel.addComponent(reporter);

% Initialize the computational system 
state = osimModel.initSystem();

%%% some code to run the forward tool using a Manager
%%%

% Get states from trajectory reporter
statesTraj = reporter.getStates();
sTable = statesTraj.exportToTable(osimModel);

% Get Data in Matlab Format
simData = osimTableToStruct(sTable);

POST REPLY