MATLAB crash when computing forward dynamics

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
User avatar
Elena Bardi
Posts: 11
Joined: Sun May 17, 2020 6:49 am

MATLAB crash when computing forward dynamics

Post by Elena Bardi » Fri Jul 17, 2020 1:45 am

Hi everyone,

I am using Matlab to run and display a forward dynamics simulation using a controller at the elbow joint. Everything works fine if I use a for cycle in the script, but if I call a function to perform the integration step, Matlab crashes. I need to call a function because I would like to use the Opensim model in Simulink (using an S-function), so that I can implement a closed loop control. The crash does not occur immediately, but it takes approximately 60-90 seconds (sometimes even more but eventually it occurs).

Mathworks support told me it could be a memory allocation problem but I wasn't able to find a solution.

Can anyone help me with this issue? I tried to look on the forum but I couldn't find a similar problem. If there's already a discussion about this topic I would be happy to read it.

Thank you very much!

Elena

Tags:

User avatar
Ayman Habib
Posts: 2248
Joined: Fri Apr 01, 2005 12:24 pm

Re: MATLAB crash when computing forward dynamics

Post by Ayman Habib » Fri Jul 17, 2020 10:13 am

Hi Elena,

Indeed this looks like a memory management issue, please post a small code snippet that reproduces the issue and we'll help you troubleshoot.

Best regards,
-Ayman

User avatar
Elena Bardi
Posts: 11
Joined: Sun May 17, 2020 6:49 am

Re: MATLAB crash when computing forward dynamics

Post by Elena Bardi » Sat Jul 18, 2020 5:03 am

Hi Ayman,

thank you so much for the quick answer!

This is the script from which I call the function "forward_dyn":

Code: Select all

import org.opensim.modeling.*
path='C:\OpenSim 4.1\Geometry';
ModelVisualizer.addDirToGeometrySearchPaths(path);

mymodel = Model('arm26_Copy.osim');
mymodel.setUseVisualizer(true);
state = mymodel.initSystem();

dt = 0.01;  
controls = mymodel.getDefaultControls();
  
for time = 0:dt:180
    state = forward_dyn(time, mymodel, state, controls); 
end
And this is the function "forward_dyn":

Code: Select all

function state = forward_dyn(time, mymodel, state, controls)
    import org.opensim.modeling.*
    dt = 0.01;
    controls.set(6, time/100);
    mymodel.setControls(state, controls);
    manager = Manager(mymodel);
    state.setTime(time); 
    manager.initialize(state);
    state = manager.integrate(time+dt);

Attached you can also find a modified version of Arm26 in which I added a coordinate actuator at the elbow joint.

Thank you so much in advance,

Elena
Attachments
arm26_Copy.osim
(87.72 KiB) Downloaded 113 times

User avatar
Daan de Kanter
Posts: 3
Joined: Tue Feb 11, 2020 2:51 am

Re: MATLAB crash when computing forward dynamics

Post by Daan de Kanter » Mon Jul 20, 2020 12:50 am

Hi Elena,

I was experiencing the same problem when trying to run a simulation.
I got the error that: "MATLAB has encountered an internal problem and needs to close."

I resolved the error by placing code of the manager, and thus the simulation step, not in a function but in my main .m file. If you run it there, you likely will be able to run your code!

Apparently, there goes something wrong when calling the manager in a MATLAB function with memory allocation.

Best regards,

Daan

User avatar
Elena Bardi
Posts: 11
Joined: Sun May 17, 2020 6:49 am

Re: MATLAB crash when computing forward dynamics

Post by Elena Bardi » Mon Jul 20, 2020 1:24 am

Hi Daan,

thank you so much for the answer!

I indeed do not have any issue when running the code in the main, however I am not sure how could I avoid using the manager in a function if I need to run a simulation in simulink. Do you have any suggestions for that?

Best regards,

Elena

User avatar
Pavlos Silvestros
Posts: 43
Joined: Sun Oct 16, 2016 4:10 am

Re: MATLAB crash when computing forward dynamics

Post by Pavlos Silvestros » Mon Jul 20, 2020 1:28 am

Hi Elena,

I also had a similar problem with some sort of memory allocation problem when running many FD simulations. In my case MATLAB would hard crash and I had to look through the error log to find out why.

At the start I tried clearing the heap memory manually through MATLAB but that didn't work too well. A quick fix I found was almost opposite to what Daan proposed.

I moved the majority of my code that was being looped into a separate function (.m) that didn't return any arguments (unlike your

Code: Select all

state = forward_dyn(time, mymodel, state, controls)
that returns

Code: Select all

state
). From what I understand after each time that function was called and executed by MATLAB it would internally clear any allocated memory used (either by MATLAB or the OpenSim API) when the function exited and before the next iteration occurred. Maybe if you can structure your code in a way like that it will give you some insight into what is happening or at last a quick fix.

I am sure they're more eloquent ways of fixing these issues but that saved me a lot of time.

Best regards,
Pavlos

User avatar
Elena Bardi
Posts: 11
Joined: Sun May 17, 2020 6:49 am

Re: MATLAB crash when computing forward dynamics

Post by Elena Bardi » Mon Jul 20, 2020 4:15 am

Hi Pavlos,

thank you for your help!

Could you please explain to me how to not return any variable and still make the simulation continue at the proper state at each loop? This is not very clear to me.

Best regards,

Elena

User avatar
Daan de Kanter
Posts: 3
Joined: Tue Feb 11, 2020 2:51 am

Re: MATLAB crash when computing forward dynamics

Post by Daan de Kanter » Tue Jul 21, 2020 2:27 am

Hi Elena and Pavlos,

Nice to hear that it works for you too Elena!
I, unfortunately, don't have experience with running a simulation using Simulink and the OpenSim/Simbody functions.
Currently, I am scripting everything in the MATLAB environment and this so far suits my goal.

Pavlos, I am curious how you managed to run your simulation code in a separate .m function without having this MATLAB error.
Could you maybe add some pseudo-code that shows your approach?

Thanks in advance!

Best regards,

Daan

User avatar
Pavlos Silvestros
Posts: 43
Joined: Sun Oct 16, 2016 4:10 am

Re: MATLAB crash when computing forward dynamics

Post by Pavlos Silvestros » Fri Jul 24, 2020 6:46 am

Hi guys,

For my needs I did something like this:

Code: Select all

Set up model and simulation environment(paths, variables etc.)
	for_loop var1
		for_loop var2
			functionToAdjustModelAndExecuteSimulation(model,var1,var2, simulation_setings)
				This function applied changes to model coordinates, muscle activations, external forces and then ran the forward simulation			
		end for_loop var2
	end for_loop var1	
end						
Elena as you seem to be stepping through time it might need some playing around.

Hope you can solve your issue!

Cheers,
Pavlos

User avatar
Elena Bardi
Posts: 11
Joined: Sun May 17, 2020 6:49 am

Re: MATLAB crash when computing forward dynamics

Post by Elena Bardi » Sun Jul 26, 2020 6:39 am

Hi guys,

unfortuntely I wans't able to solve my problem. However, if I manage to find a solution I will post it here.

Thanks for all the help.

Elena

POST REPLY