4
$\begingroup$

I am working on creating a demonstration and came across an instance where my code works fine on my machine locally but breaks down when deployed to the cloud (so that it can be publicly accessed). I really do want to deploy it; can someone help make it render/work from the cloud?

Here is the code:

Func[x_, u1_, u2_] := Exp[-(x - u1)^2/2] + Exp[-(x - u2)^2/2] 
Sensor[loc_, u1_, u2_] := Graphics3D@{Red, PointSize[0.05], Point@Join[loc,{Func[loc[[1]], u1, u2]}]} 
SurfPlot[f_, u1_, u2_, sens1_, sens2_] := Show[Plot3D[f, {x, -5, 5}, {y, -5, 5}, PlotPoints
-> 50, PlotRange -> {{-5, 5}, {-5, 5}}], Sensor[sens1, u1, u2], Sensor[sens2, u1, u2]]
TwoDPlot[u1_, u2_] := Plot[{Exp[-(x - u1)^2/2], Exp[-(x - u2)^2/2]}, {x, -5, 5}, PlotRegion -> Full] 

That sets up a 3D surface and two red points on the surface; also a 2D representation of them is made. Next, I want to be able to move the two "sensors" around in manipulate, so I am going to set up a LocatorPane.

LocSlider[loc_, init_] := {{loc, init, "Position of Sensor"}, {-5,-5}, {5, 5}, ControlType -> None}
LocBox = {Gray, EdgeForm[Black], Rectangle[{-5, -5}, {5, 5}]};
SensorBullsEye := {Black, Line[{{-5, #[[2]]}, {5, #[[2]]}}], Line[{{#[[1]], -5}, {#[[1]], 5}}]} &;

There is a relationship between the 2D plot and the location of the sensors so after defining a helper function for that, I call Manipulate.

Phase[u1_, u2_] := Abs[u2 - u1]
SenseDiff[{x1_, x2_}] := Abs[x2 - x1]
Manipulate[
 GraphicsGrid[{{SurfPlot[Func[x, u1, u2], u1, u2, xy1, xy2], 
    TwoDPlot[u1, 
     u2 - (Phase[u1, u2] - SenseDiff[First /@ {xy1, xy2}])]}}, 
  ImageSize -> Large],
 {{u1, -2.5, ""}, 0, 0, ControlType -> None}, {{u2, 2.5, ""}, 0, 0, 
  ControlType -> None},
 Evaluate@LocSlider[xy1, {2, 2}], Evaluate@LocSlider[xy2, {-2, -2}],
 Dynamic[LocatorPane[Dynamic[{xy1, xy2}], 
   Graphics[{LocBox, {Dynamic[SensorBullsEye /@ {xy1, xy2}]}}, 
    ImageSize -> 100]]], SaveDefinitions -> True]

This works fine locally on my Mac, but it hangs when published using WolframCloud.

enter image description here

Here is the published version. Can someone show me what I can do better to make the deployed version work? I found this answer but I haven't played around with the CDF format much. What are some general principles to keep in mind when writing notebooks that are intended to deployed/interactive?

I understand that WolframCloud has kernel memory and computation limitations, so my intention is not to use it as a real-time computing resource for anything like data-science. But I feel something simple like this should be do-able.

$\endgroup$

1 Answer 1

2
+25
$\begingroup$

This is a simple oneliner which kind of works. Check out the published version here: public cloud link. I personally still find most of these cloud-deployed Manipulate (or DynamicModule) toy-examples way too slow, but maybe someone from Wolfram has a clever trick to get a similar responsiveness as in a local notebook?

   SystemOpen @ 
  CloudDeploy[#, "somecode",Permissions->"Public"]& @
DynamicModule[{xy1 = {2, 2}, xy2 = {-2, -2}, u1 = -2.5, u2 = 2.5},
  Column @ {
    LocatorPane[ Dynamic[{xy1, xy2}], 
                 Dynamic @ Graphics[{LocBox,Map[SensorBullsEye]@{xy1,xy2}}, ImageSize -> 100]
    ]
    ,
    Row[{ Dynamic @ SurfPlot[Func, u1, u2, xy1, xy2],
          Dynamic @ TwoDPlot[u1, u2 - (Phase[u1, u2] - SenseDiff[First /@ {xy1, xy2}])]
    }]
  }
  ,
  Initialization :> (
    Func = Function[{x,u1,u2},Exp[-(x - u1)^2 / 2] + Exp[-(x - u2)^2 / 2]];
    Sensor[loc_, u1_, u2_] := Graphics3D @ {Red, PointSize[0.05], Point @ Join[loc, {Func[loc[[1]], u1, u2]}]}; 
    SurfPlot[f_Function, u1_, u2_, sens1_, sens2_] := Show[
        Plot3D[f[x,u1,u2], {x, -5, 5}, {y, -5, 5}, PlotPoints -> 50, PlotRange -> {{-5, 5}, {-5, 5}}]
      , Sensor[sens1, u1, u2], Sensor[sens2, u1, u2], ImageSize -> Medium
    ];
    TwoDPlot[u1_, u2_] := Plot[{Exp[-(x - u1)^2 / 2], Exp[-(x - u2)^2 / 2]}, {x, -5, 5}, PlotRegion -> Full, ImageSize -> Medium];
    LocBox = {Gray, EdgeForm[Black], Rectangle[{-5, -5}, {5, 5}]};
    Phase[u1_, u2_] := Abs[u2 - u1];
    SenseDiff[{x1_, x2_}] := Abs[x2 - x1];
    SensorBullsEye = {Black, Line[{{-5, #[[2]]}, {5, #[[2]]}}], Line[{{#[[1]], -5}, {#[[1]], 5}}]} &)
, 
SaveDefinitions->False
]
$\endgroup$

Your Answer

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

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