9
$\begingroup$

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.

$\endgroup$
8
  • $\begingroup$ Could you post a minimal non-working example? I can't really reproduce the error. $\endgroup$
    – David
    Commented Jan 18, 2012 at 5:31
  • $\begingroup$ @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. $\endgroup$ Commented Jan 18, 2012 at 5:32
  • $\begingroup$ @David: I added the code that repros the error. $\endgroup$ Commented Jan 18, 2012 at 5:40
  • $\begingroup$ It's been running for some time now. On what timescale does it stop? $\endgroup$
    – David
    Commented Jan 18, 2012 at 6:02
  • $\begingroup$ @David: I've updated it slightly to have a time counter. For me it stops at t = 3.64 $\endgroup$ Commented Jan 18, 2012 at 6:05

2 Answers 2

3
$\begingroup$

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}
     ]
   }
 ]
$\endgroup$
2
  • $\begingroup$ @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. $\endgroup$ Commented Jan 18, 2012 at 6:09
  • $\begingroup$ @NasserAMAbbasi: I also note that I can't adjust the time step dt. $\endgroup$ Commented Jan 18, 2012 at 6:16
1
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.