Page 1 of 1

Matlab crash afterI'm using model scaling using API

Posted: Fri Mar 09, 2012 12:28 am
by dvidemnr
Hi,

I would like to use OpenSim API in Matlab to programmatically scale my model. I wrote down
the followjng code to do that:

import org.opensim.modeling.*

% Get the original model
M_Model = Model('Delp1990.osim');

% Create the ScaleSet object
leg_ScaleSet = ScaleSet();

segNames = {'ground','pelvis','femur_r','tibia_r','talus_r','calcn_r','toes_r','femur_l','tibia_l','talus_l','calcn_l','toes_l','torso'};
for i = 1 : length(segNames)
seg_Scale(i) = Scale();
seg_Scale(i).setSegmentName(segNames{i});
if strcmp(segNames{i},'tibia_r')
seg_Scale(i).setScaleFactors([0.3, 0.3, 0.3]);
else
seg_Scale(i).setScaleFactors([1, 1, 1]);
end
seg_Scale(i).setApply(true);
leg_ScaleSet.append(seg_Scale(i));
end

% Scale the model
state_State = M_Model.initSystem;
scaled = M_Model.scale(state_State,leg_ScaleSet);

% Save it to .osim file
M_Model.print('Delp1990_scaled.osim');

When I lunch the function the first time it greatly works. But when I lunch it again, Matlab crashes and, in the hs_err_pidXXXX.log file generated in the same folder of the .m script, I have the following lines:

Stack: [0x0c340000,0x0c3c0000], sp=0x0c3bf7d0, free space=509k
Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j org.opensim.modeling.opensimModelJNI.delete_Scale(J)V+0
j org.opensim.modeling.Scale.delete()V+25
j org.opensim.modeling.Scale.finalize()V+1
v ~StubRoutines::call_stub
j java.lang.ref.Finalizer.invokeFinalizeMethod(Ljava/lang/Object;)V+0
j java.lang.ref.Finalizer.runFinalizer()V+45
j java.lang.ref.Finalizer.access$100(Ljava/lang/ref/Finalizer;)V+1
j java.lang.ref.Finalizer$FinalizerThread.run()V+11
v ~StubRoutines::call_stub

By ther way, the output model Delp1990_scaled.osim is asved before the crash and it' correct.
I guess there must be something about Scale or ScaleSet object deallocations. Am I using the Java objects in a wrong way?
I'm using Matlab 32bit on a Win 7 64bit OS.

Thank you very much in advance.

Davide

Re: Matlab crash afterI'm using model scaling using API

Posted: Fri Mar 09, 2012 11:07 am
by aymanh
Hi Davide,

Great to know that you're using this Matlab interface. We're building it up and will provide more documentation and examples with the next OpenSim release. If you want to volunteer to help with this effort either as a beta tester or by providing sample scripts that you wrote and found useful please let us know (through the forum or email).

For your specific question, my guess would be that it's an issue with the ScaleSet() taking ownership of the Scale objects passed on to it, so when the set gets deleted/garbage-collected the Scale objects get deleted as well. Probably the garbage collector is triggered by the second call since that makes the first ScaleSet inaccessible. I'll send you a separate email to test this hypothesis and come up with a solution then we can post that to the forum.

Best regards,
-Ayman

Re: Matlab crash afterI'm using model scaling using API

Posted: Sun Nov 29, 2015 10:53 pm
by saxbyd
Hi Ayman and Davide,

I am experiencing the same issue where I customize a scale set and add it to the model scaler. The scaler executes but then crashes Matlab.

Did you come to a conclusion about why this happens, or how it can be worked around/avoided?

Best,

David Saxby

Re: Matlab crash afterI'm using model scaling using API

Posted: Mon Nov 30, 2015 4:34 pm
by saxbyd
Hi Guys,

A few other things I noticed.

First, I'm using 64-bit Matlab 2015b and 64 bit OpenSim v3.3. I have the same issue using a 32b Matlab 2011a and 32b OpenSim v3.1. That is not exhaustive, but I doubt this is a version to version issue.

Second, I commented out my section of code that creates a custom scale for a particular body, so now the scale tool is simply executing based on a template setup_scale.xml. This works!

I tried a few other things I saw on the forum such as clearing all the OpenSim objects from the Matlab workspace once I was done with them, increasing the Java heap memory, running the garbage collector explicitly with java.lang.System.gc(), but with the scale object passed to the modelScaler it always crashes Matlab.

I also tried printing the customized scale_setup and running from the shell, no luck. I all cases it does scale the model but it then crashes Matlab.

So my options are really only to print the custom scaler and run it in the GUI or cmd.

Any ideas?

Best,

David

Re: Matlab crash afterI'm using model scaling using API

Posted: Mon Nov 30, 2015 6:07 pm
by saxbyd
Problem solved.

1) I did not realize that somewhere in the scaleTool or modelScaler constructors the unassigned pelvis will cause a scale set of [1 1 1] to be generated. Not so for outboard bodies left unassigned.

2) I was then adding another pelvis scale to the existing scale set.

3) On top of that, I was generating those scale factors in a bad way. I was using the method get_location_in_parent, which I suppose was not a good idea as it is not even documented. A colleague (Claudio Pizollato) set me straight about that, and now the tool runs perfectly.

Hope that is of help to other users.

Best,

DJS

Re: Matlab crash afterI'm using model scaling using API

Posted: Tue Dec 01, 2015 12:05 pm
by aymanh
Hi David,

Thanks for reporting back and I'm glad you resolved your issues.

Just to follow up on my answer earlier in this thread (in case the crash was happening deleting a Scale object) the solution is to replace the line

Code: Select all

leg_ScaleSet.append(seg_Scale(i)); //obsolete 
with

Code: Select all

leg_ScaleSet.cloneAndAppend(seg_Scale(i));
The latter line of code makes a copy of the Scale object and adds it to the set so memory management is handled properly and the Scale objects can be deleted safely.

All the best,
-Ayman