# What causes Manipulate to stop evaluating expressions?

I'm working on a N-body simulation within Mathematica, and currently I have something along these lines:

Manipulate[
{r, v, a} = Step[G, m, r, v, a, ts dt];
Show[Visualize[names, colors, radius1, r1],
PlotRange -> {{-d, +d}, {-d, +d}}, ImageSize -> 1200],
Dynamic[MatrixForm[r]],
{{dt, 0.02}, 0.0001, 1, 0.0001}]


Step[...] simply steps the simulation forward in time. This works fine and I can confirm it works perfectly fine independent of anything else.

However, occasionally the Dynamic[MatrixForm[r]] expression stops evaluating. I can't really figure out why it does this.

Furthermore, sometime after the Dynamic stops evaluating, the Show[Visualize[...], ..] expression stops evaluating.

At some point earlier when I had something much simpler along the lines of

Manipulate[f = Step[f]; Show[SimpleVisualize[f]], {...}]


The code within Manipulate would run indefinitely. Now it seems to randomly stop. It never stops at the same time, but it always does. Any ideas why?

An example of where this occurs is available here: http://dl.dropbox.com/u/9450720/minimal.nb

Environment Information: Mathematica 8.0.4.0 on Windows 7 64-bit

Here is a basic example of what I'm doing:

Step[r_, v_, a_, dt_] := Block[{nr, nv, na},
nv = v + dt/2 a;
nr = r + dt nv;
na = -nr;
nv = nv + dt/2 a;
{nr, nv, na}]

r = {1, 0};
v = {0, 1};
a = {-1, 0};

Manipulate[{r, v, a} = Step[r, v, a, dt];
Dynamic[Show[{Graphics@Circle[{0, 0}, 1],
Graphics[{PointSize[Large], Red, Point[r]}]}]],
Dynamic[{r, Norm@r}],
{{dt, 0.005}, 0.00001, 1, 0.00001}]


This evaluates fine and runs indefinitely.

-
Could you post a minimal non-working example? I can't really reproduce the error. –  David Jan 18 '12 at 5:31
@David: I'm trying to find the minimal example that displays it. It's a bit late right now, so I may not get around it until tomorrow. –  Mike Bantegui Jan 18 '12 at 5:32
@David: I added the code that repros the error. –  Mike Bantegui Jan 18 '12 at 5:40
It's been running for some time now. On what timescale does it stop? –  David Jan 18 '12 at 6:02
@David: I've updated it slightly to have a time counter. For me it stops at t = 3.64 –  Mike Bantegui Jan 18 '12 at 6:05

## 2 Answers

Please try this. I prefer to remove all unneeded dynamics from the code by making the Manipulate use a Trigger so that I see better what is the dynamic object in all of these things, and added TrackedSymbols on only the trigger variable, and moved globals into the Manipulate as I do not like to see globals. (you can also use Module inside Manipulate if you want).

Not seeing any hangups any more. The Trigger is probably what you want to use for this type of simulation setup.

Manipulate[
{r, v, a} = Step[r, v, a, dt];

Grid[{
{Show[{Graphics@Circle[{0, 0}, 1],
Graphics[{PointSize[Large], Red, Point[r]}]}]},
{r, Norm@r}
}
],

Control[{{dt, 0.005, "dt:"}, 0.00001, 1, 0.00001,
ControlType -> Trigger, DisplayAllSteps -> True,
ImageSize -> Small, AnimationRate -> Automatic}],

{{r, {1, 0}}, None},
{{v, {0, 1}}, None},
{{a, {-1, 0}}, None},
TrackedSymbols :> {dt},
Initialization :>
{
Step[r_, v_, a_, dt_] := Block[{nr, nv, na},
nv = v + dt/2 a;
nr = r + dt nv;
na = -nr;
nv = nv + dt/2 a;
{nr, nv, na}
]
}
]

-
@NasserMAbbasi: The "simple example" code I posted above runs indefinitely as I stated in my answer. The code that fails to run is posted as a link. –  Mike Bantegui Jan 18 '12 at 6:09
@NasserAMAbbasi: I also note that I can't adjust the time step dt. –  Mike Bantegui Jan 18 '12 at 6:16

I downloaded your full example and confirmed that it stopped after some time. I was able to make it run indefinitely by adding the following option at the end of the Manipulate:

SynchronousUpdating -> True


That's all. Hope it works for you too.

-