Visualizing a Storage File Without Solving [MATLAB]

OpenSim Moco is a software toolkit to solve optimal control problems with musculoskeletal models defined in OpenSim using the direct collocation method.
POST REPLY
User avatar
D T
Posts: 24
Joined: Thu Nov 21, 2019 3:40 pm

Visualizing a Storage File Without Solving [MATLAB]

Post by D T » Fri Dec 11, 2020 4:13 pm

Hi all,

I'd like to visualize an already made MOCO solution storage file, but can not seem to get the visualize() function working. All of the available examples require that you first solve a MOCO study, then visualize the solution. The documentation page says you should be able to do visualize(model, storage), however I am unable to do this. All I would like to do is take a storage file that was produced by a previous MOCO solution and load it into the looping visualizer executable.

Here is an example of what I would like to do in MATLAB, which does not work:

Code: Select all

import org.opensim.modeling.*;
storagePath = 'test.sto';
storage = Storage(storagePath);
model = Model('Mymodel.osim');
visualize(model, storage);
Here is an example of the workaround I have had to do, which takes much longer and is not desirable for use with batch files. It essentially creates a fake problem to solve in order to get a solution variable rather than just a storage one:

Code: Select all

import org.opensim.modeling.*;
storagePath = 'test.sto';
    
% Start MocoStudy
study = MocoStudy();
problem = study.updProblem();

% Import Model
model = Model('Mymodel.osim');
problem.setModel(model);

% Configure the solver.
solver = study.initCasADiSolver();
solver.set_num_mesh_intervals(5);

% Loosen the tolerance values to extreme amounts
solver.set_optim_convergence_tolerance(1e+10);
solver.set_optim_constraint_tolerance(1e+10);

% Set the initial guess using the optimizer solution storage file
solver.setGuessFile(storagePath);

% Solve and visualize
trackingSolution = study.solve();
study.visualize(trackingSolution);
Is there a better way to do this? Any help/guidance would be appreciated.

Thank you

User avatar
Jiacheng Weng
Posts: 26
Joined: Wed Jan 15, 2020 1:05 pm

Re: Visualizing a Storage File Without Solving [MATLAB]

Post by Jiacheng Weng » Fri Dec 11, 2020 7:25 pm

Hi Dylan,

Based on my understanding, the .sto file generated by the MocoStudy.solve() is a MocoTrajectory file (e.g. a table with state and control values over time). If you just want to visualize the trajectory without resolving the problem, you can read in the .sto file using MocoTrajectory("filePath/solution.sto") and visualize in the MocoStudy.

Please see the example code below:

Code: Select all

study = MocoStudy();
study.setName('gaitPrediction');
problem = study.updProblem();
modelProcessor = ModelProcessor('yourModel.osim');
problem.setModelProcessor(modelProcessor);
model = modelProcessor.process();
state = model.initSystem();

traj = MocoTrajectory('gaitPrediction_solution.sto');
study.visualize(traj);
Hope this helps.
Jiacheng

User avatar
D T
Posts: 24
Joined: Thu Nov 21, 2019 3:40 pm

Re: Visualizing a Storage File Without Solving [MATLAB]

Post by D T » Fri Dec 11, 2020 10:46 pm

jiachengweng wrote:
Fri Dec 11, 2020 7:25 pm
Hi Dylan,

Based on my understanding, the .sto file generated by the MocoStudy.solve() is a MocoTrajectory file (e.g. a table with state and control values over time). If you just want to visualize the trajectory without resolving the problem, you can read in the .sto file using MocoTrajectory("filePath/solution.sto") and visualize in the MocoStudy.

Please see the example code below:

Code: Select all

study = MocoStudy();
study.setName('gaitPrediction');
problem = study.updProblem();
modelProcessor = ModelProcessor('yourModel.osim');
problem.setModelProcessor(modelProcessor);
model = modelProcessor.process();
state = model.initSystem();

traj = MocoTrajectory('gaitPrediction_solution.sto');
study.visualize(traj);
Hope this helps.
Jiacheng
This is exactly the format I was looking for, thank you! I was not aware that the saved storage file is treated as a trajectory.

User avatar
Nicholas Bianco
Posts: 988
Joined: Thu Oct 04, 2012 8:09 pm

Re: Visualizing a Storage File Without Solving [MATLAB]

Post by Nicholas Bianco » Sun Dec 13, 2020 9:17 pm

Thanks Jiacheng.

Yes, we use the STO file format to be compatible with the rest of OpenSim (i.e., visualization in the GUI).

POST REPLY