Page 1 of 1

Visualizing a Storage File Without Solving [MATLAB]

Posted: Fri Dec 11, 2020 4:13 pm
by trainordt
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

Re: Visualizing a Storage File Without Solving [MATLAB]

Posted: Fri Dec 11, 2020 7:25 pm
by jiachengweng
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

Re: Visualizing a Storage File Without Solving [MATLAB]

Posted: Fri Dec 11, 2020 10:46 pm
by trainordt
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.

Re: Visualizing a Storage File Without Solving [MATLAB]

Posted: Sun Dec 13, 2020 9:17 pm
by nbianco
Thanks Jiacheng.

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