11
$\begingroup$

The code below crashes the Mathematica kernel in version 10 (not in V9) every time I run it on Windows and on Mac OS. I've sent it to WRI, but was told that they could not reproduce the crash.

Does this really only crash on my computers? Could somebody else test it with Mathematica 10?

pts = Flatten[
   Table[Through[{Cos, Sin}[α]] r, {α, 0., 
     360 ° - 10^-5, 60 °}, {r, 1, 2}], 1];

LocatorPane[Dynamic[pts], 
 Dynamic@Show[
   Graphics[{Arrow /@ Partition[pts, 2]}], {a, b} = 
    Transpose[Map[Module[{p1, p2, d}, {p1, p2} = #;
        d = p2 - p1;
        {d, p1.d}] &, Partition[pts, 2]]];
   RegionPlot[And @@ Thread[a.{x, y} < b], {x, -2, 2}, {y, -2, 2}]]]

ContourPlot[
 Total[Log[Clip[b - a.{x, y}, {10^-10, ∞}]]], {x, -2, 
  2}, {y, -2, 2}, PlotRange -> {-10, 0}]
$\endgroup$
5
  • $\begingroup$ Don't forget to add the crash tag; in addition to categorization it helps to warn people who blindly execute Question code. :-) $\endgroup$
    – Mr.Wizard
    Commented Jul 25, 2014 at 14:54
  • $\begingroup$ Crash confirmed in a fresh kernel. Version 10.0.0 under Windows. $\endgroup$
    – Mr.Wizard
    Commented Jul 25, 2014 at 14:55
  • 1
    $\begingroup$ Crashes for me (Mac OSX), if I put the code in a single cell. If I put the ContourPlot in a separate cell, it does not crash. Reminds me of my experience with this question, which seems to have nothing to do with Dynamic. $\endgroup$
    – Michael E2
    Commented Jul 25, 2014 at 15:11
  • $\begingroup$ I can confirm the crash, regardless of whether I put the ContourPlot in a separate cell or not. OS X 10.9.4, Mathematica 10.0.0. $\endgroup$
    – Szabolcs
    Commented Jul 25, 2014 at 16:20
  • $\begingroup$ FWIW, Dynamic@Refresh[ContourPlot[...], None] does not crash. $\endgroup$
    – Michael E2
    Commented Jul 25, 2014 at 17:28

4 Answers 4

6
$\begingroup$

Edit: This is a rewrite. The original answer was more or less an extended comment containing some seemingly relevant clues. I've tracked down two probable issues, each related to a system variable, and there may be others. At least in either case, the crashing can be triggered or prevented.* It turns out that Dynamic@RegionPlot[..] depends on the variables $PlotTheme and $DisplayFunction and that evaluating ContourPlot triggers an update. Beyond this, I'm not sure how the crash occurs. The evaluation of ContourPlot triggers an update of RegionPlot. This apparently occurs simultaneously, the ContourPlot being requested via the main link and the RegionPlot via the preemptive link. It would not be surprising if one interferes with the other and leads to the crash.

The following shows ContourPlot tickles $PlotTheme and $DisplayFunction. Evaluate ContourPlot repeatedly and see pt and df be incremented.

pt = 0; df = 0;
Dynamic[{++pt, $PlotTheme}, TrackedSymbols :> {$PlotTheme}]
Dynamic[{++df, $DisplayFunction}, TrackedSymbols :> {$DisplayFunction}]

(* new cell *)
ContourPlot[x + y, {x, -2, 2}, {y, -2, 2}]

The following shows that Dynamic@RegionPlot depends on $PlotTheme and $DisplayFunction. Evaluate the plot and then evaluate the others in turn and see the RegionPlot be updated.

Dynamic[RegionPlot[x^2 + y^2 < 1, {x, -2, 2}, {y, -2, 2}]]

$DisplayFunction = Hold

Mathematica graphics

$DisplayFunction = Identity
$PlotTheme = "Web"

$PlotTheme = Automatic

Here is a simple workaround: add TrackedSymbols :> {pts}. Below you can uncomment the other symbols, if you wish to observe the kernel crashing. (It seems to be a race situation. It doesn't always crash when you evaluate the ContourPlot, but it usually does. It crashes if you uncomment one, the other, or both symbols.)

LocatorPane[Dynamic[pts], 
 Dynamic[Show[
   Graphics[{Arrow /@ Partition[pts, 2]}], {a1, b1} = 
    Transpose[Map[Module[{p1, p2, d}, {p1, p2} = #;
        d = p2 - p1;
        {d, p1.d}] &, Partition[pts, 2]]];
   RegionPlot[
    And @@ Thread[a1.{x1, y1} < b1], {x1, -2, 2}, {y1, -2, 2}]], 
  TrackedSymbols :> {pts(*, $PlotTheme*)(*, $DisplayFunction*)}]]

Acknowledgement: Karsten 7.'s observation about SynchronousUpdating -> False led me to observe that evaluating ContourPlot causes the dynamic RegionPlot to be reevaluated. Further investigation led me as far as $PlotTheme and $DisplayFunction.

$\endgroup$
3
  • $\begingroup$ +1 Nice troubleshooting. Wrapping the ContourPlot in PreemptProtect seems to work too. $\endgroup$ Commented Jul 26, 2014 at 18:57
  • $\begingroup$ @SimonWoods Thanks, I'd forgotten about PreemptProtect. It makes sense it would work. $\endgroup$
    – Michael E2
    Commented Jul 26, 2014 at 19:02
  • $\begingroup$ @SimonWoods I find that, if all code is in one cell, while PreemptProtect@ContourPlot[..] prevents a crash for the first execution of both plots, the next time I execute both plots, things lock up and the dynamic updating of Graphics/RegionPlot does not finish. Also, {a, b} is not initialized when ContourPlot is executed. If the code for the plots is in separate cells, then everything seems to work fine. $\endgroup$
    – Michael E2
    Commented Jul 27, 2014 at 3:27
3
$\begingroup$

The x and y of the ContourPlot are interfering with the x and y inside of Dynamic.
If you use for example

ContourPlot[Total[Log[Clip[b-a.{x2,y2},{10^-10,\[Infinity]}]]],{x2,-2,2},{y2,-2,2},PlotRange->{-10,0}]

it'll not crash.
I guess this shouldn't be necessary.

$\endgroup$
6
  • $\begingroup$ It still crashes for me (Mac OSX). $\endgroup$
    – Michael E2
    Commented Jul 25, 2014 at 15:09
  • $\begingroup$ This works for me. Windows 8.1 $\endgroup$
    – RunnyKine
    Commented Jul 25, 2014 at 15:13
  • $\begingroup$ I'm using v10.0.0 under Win7 $\endgroup$
    – Karsten7
    Commented Jul 25, 2014 at 15:15
  • 1
    $\begingroup$ Yes, putting the ContourPlot inside Module[{x,y}, ... works, too. (I actually included that workaround in my WRI support ticket, but forgot it in the question here...) $\endgroup$ Commented Jul 25, 2014 at 15:22
  • 1
    $\begingroup$ @MichaelE2: You're right, this looks like a race condition: If I assign all Mathematica processes to a single CPU core, it doesn't crash anymore (or at least not that often). Maybe the WRI support people can't reproduce it because they have single core CPUs? $\endgroup$ Commented Jul 25, 2014 at 15:56
2
$\begingroup$

The more general solution to this problem is to make use of the Option SynchronousUpdating->False:

pts=Flatten[Table[Through[{Cos,Sin}[\[Alpha]]] r,{\[Alpha],0.,360 \[Degree]-10^-5,60 \[Degree]},{r,1,2}],1];
LocatorPane[Dynamic[pts], 
  Dynamic[Show[
   Graphics[{Arrow /@ Partition[pts, 2]}], {a, b} = 
    Transpose[Map[Module[{p1, p2, d}, {p1, p2} = #;
      d = p2 - p1;
      {d, p1.d}] &, Partition[pts, 2]]];
   RegionPlot[And @@ Thread[a.{x, y} < b], {x, -2, 2}, {y, -2, 2}]], 
   SynchronousUpdating -> False]]

and than

ContourPlot[Total[Log[Clip[b-a.{x,y},{10^-10,\[Infinity]}]]],{x,-2,2},{y,-2,2},PlotRange->{-10,0}]
$\endgroup$
2
  • $\begingroup$ @MichaelE2 does this also work on a Mac? $\endgroup$
    – Karsten7
    Commented Jul 25, 2014 at 23:06
  • $\begingroup$ Sorry, somehow I wasn't pinged. It does work (at least for the several times I tried it). Of course, a, b are not set until the RegionPlot is updated. Also executing ContourPlot[Total[Log[Clip[b2 - a2.{x2, y2}, {10^-10, \[Infinity]}]]], {x2, -2, 2}, {y2, -2, 2}, PlotRange -> {-10, 0}] causes the RegionPlot to update. (The variables a2, b2 are copies of a, b, resp.) $\endgroup$
    – Michael E2
    Commented Jul 26, 2014 at 2:35
0
$\begingroup$

This, I think, is a simplified version of the issue:
If one executes

Dynamic[Print[x]; x]

and than with the Messages window open

Plot[x,{x,-2,2}]

x is printet into the Messages window two times whenever the prior Plot comment is executed (using Mma v10).
However, in Mma v9 only one x is printed into the Messages window for every execution of the Plot command.

$\endgroup$
3
  • $\begingroup$ Please note that on my system if instead of x, y, I use RegionPlot[And @@ Thread[a.{x1, y1} < b], {x1, -2, 2}, {y1, -2, 2}] and ContourPlot[Total[Log[Clip[b - a.{x2, y2}, {10^-10, \[Infinity]}]]], {x2, -2, 2}, {y2, -2, 2}, PlotRange -> {-10, 0}] causes a crash. So at least for this example on a Mac, there's something beyond a symbol clash. $\endgroup$
    – Michael E2
    Commented Jul 25, 2014 at 16:45
  • $\begingroup$ @MichaelE2 what if you also lokalize a and b for the ContourPlot? $\endgroup$
    – Karsten7
    Commented Jul 25, 2014 at 16:58
  • $\begingroup$ I tried that. And localizing a, b, x and y. They all crash. $\endgroup$
    – Michael E2
    Commented Jul 25, 2014 at 17:22

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.