Why is an empty simulation so slow with smaller time steps?

Typically: "How do I... ", "How can I... " questions
Post Reply
benjamin
Posts: 13
Joined: 12 Feb 2015, 11:50

Why is an empty simulation so slow with smaller time steps?

Post by benjamin »

I've been using V-REP/CoppeliaSim for years now and I always found the simulation to run to slow. I though it was because of the scene to simulate or the communication through the remote API but I just made a very simple test and I can't understand the result.

With the default empty scene, a time step of 1ms, the ODE solver and in real-time mode I get a real time factor of 0.15.

Now if on the same computer I do the same with Gazebo (defaults to ODE at 1ms, so same condition) I get a real-time factor of 1. Even if I include a PR2 robot in the scene (with its camera and laser scanner) the real-time factor stays at 1.

I can understand performance differences between physics engine, communication protocols, etc but doing the same thing with the same physics engine should yield the same result.

Why is there such a difference?

And yes, if I want to be as close to the real hardware I simulate I need a 1 to 5ms physics time step.

benjamin
Posts: 13
Joined: 12 Feb 2015, 11:50

Re: Why is an empty simulation so slow with smaller time steps?

Post by benjamin »

Out of curiosity, I ran perf/hotspot on CoppeliaSim with the configuration I described in the previous post and got:
Image

This is the part related to the simulation thread loop. If I understand things correctly, the highlighted part (simAdvanceSimulationByOneStep_internal) is where the physics is run and is only a tiny part of the total thread execution time. The rest seems to be mainly related to the handling of scripts. This would explain why lowering the time step has such a dramatic effect, the script handling takes a constant, non negligible, time.

coppelia
Site Admin
Posts: 10339
Joined: 14 Dec 2012, 00:25

Re: Why is an empty simulation so slow with smaller time steps?

Post by coppelia »

Hello,

in CoppeliaSim you have the simulation loop (or simulation time step), and the physics engine time step, which are 2 different things: by default:
  • the simulation time step is 50ms, which means that every 50ms (in simulation time), the main script and in general all scripts will be run at least once. If you set the simulation time step to 1 ms, then the scripts will run 50 times more often, which of course slows everything down. e.g. if you previously checked for collision 20 times a secons (in simulation time), now you'd check for collision 1000 times a second, which doesn't make sense. In general, it very rarely makes sense to run the scripts 1000 per seconds, or perform a sensing or actuator task 1000 times per second.
  • the physics engine time step is 5ms. this means that by default, the physics engine will be called 10 times per simulation loop. This is because the physics calculation needs a maximum time step size in order to operate accurately (e.g. setting a physics engine time step of 50 ms, you'd see objects fall trhough the floor, collisions not correctly registered, precision greatly reduced, etc.). You can/should set the time step for the physics engine to 1ms, and keep the simulation time step at 50ms if you need a more fine grained dyn. simulation.
You can adjust both time steps independently. The physics engine time step will however always be smaller or equal to the simulation loop time step. During simulation, check the message in the rendering window that says by default Dynamics handling enabled (ODE) Calculation passes: 10

The physics engine is run in the main script with sim.handleDynamics. By default, sim.handleDynamics will internally call 10 times the physics engine step function.

Additionally, between each simulation loop step, there will be a rendering action. The frequency of rendering can also be adjusted in the simulation dialog.

Cheers

benjamin
Posts: 13
Joined: 12 Feb 2015, 11:50

Re: Why is an empty simulation so slow with smaller time steps?

Post by benjamin »

I think I understand how it works but still, I have to disagree with you about running the simulation loop at 1ms being nonsensical.

Let's say I have a real Franka Panda robot and I want to test things in simulation first.

The real robot will send its current state (position, velocity, torques, etc) at 1kHz and expects a new command shortly after.

In CoppeliaSim, even if I set the physics time step to 1ms, as long as I keep the simulation loop at 50ms (or anything higher than 1ms in fact) it will be like interacting with a different system since the elapsed time between two data point will be larger than what I get in the real life. And that simulation time step end up being my control time step. This means that the controller should be configured differently for simulation/reality which can have an impact on its behavior. This also means that I can be fooled thinking that my controller runs fine while in reality it will take too long to execute when later switching to the real robot.

It's okay that when using a simulator the program execution time gets longer but I still think that this is a bit pathological for CoppeliaSim. This is especially true when using the synchronous operation mode with the remote API which tends to half the real time factor.

And for simple scenes and with the right tools, it is definitely doable to run the physics + collision detection in less than 1ms.

coppelia
Site Admin
Posts: 10339
Joined: 14 Dec 2012, 00:25

Re: Why is an empty simulation so slow with smaller time steps?

Post by coppelia »

If you need to run control code at the same rate as the physics engine, then use a dynamics callback function, or a joint callback function.

And if you need to run some external control code at the same rate as the physics engine, then from within a dynamics callback function, exchange information with the outside world via ROS, ZeroMQ, sockets, etc. Using the remote API in that case is a bit overkill and would slow down things too much.

Cheers

benjamin
Posts: 13
Joined: 12 Feb 2015, 11:50

Re: Why is an empty simulation so slow with smaller time steps?

Post by benjamin »

Thanks I didn't know that.

I guess it could work but it requires to have a customized scene with a customized communication API to do the equivalent of the remote API.

It would be best if I can avoid all that since many people in my lab will end up using my CoppeliaSim interface for their various simulations and it's not something I really want to bother them with.

Is there a way to inject a script from the remote API? That way I could inject my custom code automatically without anyone knowing it's there.

coppelia
Site Admin
Posts: 10339
Joined: 14 Dec 2012, 00:25

Re: Why is an empty simulation so slow with smaller time steps?

Post by coppelia »

have a look at sim.executeScriptString, sim.addScript, sim.associateScriptWithObject and sim.initScript.

But instead of injecting a script, why not simply using a specific add-on?

Cheers

benjamin
Posts: 13
Joined: 12 Feb 2015, 11:50

Re: Why is an empty simulation so slow with smaller time steps?

Post by benjamin »

Since all of this isn't just for me, but many people in my lab, I'd rather have a solution that doesn't require any modification to CoppeliaSim, including compiling and installing a plugin for all the platforms they will want to run the simulator on.

In this regard the remote API is great because I know my "driver" code will work with any scene and version of CoppeliaSim people will use. I'll take a look at the functions you mentioned as I can probably use them to do everything without the user knowing what's happening behind the scenes.

Post Reply