1

给出的代码使用直接使用 Dynamics 的快速算法绘制 ODE 解决方案的结果,我发现它在屏幕上非常快地绘制解决方案。

我将此算法集成到 Manipulate[] 中,并注意到绘图部分现在比以前慢得多。

我在这上面花了 4 个小时,但不明白为什么会这样。我希望有人能发现问题以及问题所在。

该算法是 Leonid 今天刚刚在他对我的另一个问题的回答中发布的算法再次感谢 Leonid!)

该算法非常快,并且也可以快速渲染绘图。但它直接使用动态。我想在 Manipulate 中使用它。

我确实将它集成到 Manipulate 中,尽我所知,因为代码对我来说是先进的,我不确定我是否做得对,但结果是正确的。

该绘图确实有效并生成了正确的绘图,与原始算法一样,但现在绘图速度现在要慢得多。两种情况下的所有参数都相同(即问题参数)。这是我长期以来一直在努力解决的问题。使用 Manipulate 时如何加快 fps。

所以,问题可能在于我将它集成到 Manipulate 中运行,我做了一些效率不高的事情,或者可能是因为 Manipulate 已经使用了 DynamicModule[] 并且这对使绘图渲染更慢或整个过程更慢有副作用。

我将发布我的 Manipulate 代码,我在其中集成了 Leonid 代码(我尝试了许多不同的方法,但它们都绘制得很慢,这是下面的一个版本)。

Manipulate[

 Module[{ll = emptyList[], sol, plot, t, y, angle, 
   llaux = emptyList[]},
  plot[] := 
   Graphics[{Hue[0.67`, 0.6`, 0.6`], Line[fromLinkedList[ll]]}, 
    AspectRatio -> 1/GoldenRatio, Axes -> True, 
    AxesLabel -> {"time", "angle"}, 
    PlotRange -> {{0, max}, {-Pi/4, Pi/4}}, PlotRangeClipping -> True];

  llaux = ll;
  sol := First@
    NDSolve[{y''[t] + 0.01 y'[t] + Sin[y[t]] == 0, y[0] == Pi/4, 
      y'[0] == 0}, y, {t, time, time + 1}];
  angle := y /. sol;

  ll := With[{res = 
      If[llaux === emptyList[] || pop[llaux][[1]] != time, 
       addToList[llaux, {time, angle[time]}],(*else*)llaux]},
    llaux = res];

  Dynamic[plot[]]
  ]
 ,
 {{time, 0, "run"}, 0, max, Dynamic@delT, AnimationRate -> 1, 
  ControlType -> Trigger}, {{delT, 0.01, "delT"}, 0.01, 1, 0.01, 
  Appearance -> "Labeled"},
 {{y0, Pi/4, "y(0)"}, -Pi, Pi, Pi/100, Appearance -> "Labeled"},
 {{yder0, 0, "y'(0)"}, -1, 1, .1, Appearance -> "Labeled"},
 {{linkedList, {}}, None},

 TrackedSymbols :> {time},
 Initialization :> (
   max = 200;
   toLinkedList[data_List] := Fold[linkedList, linkedList[], data]; 
   fromLinkedList[ll_linkedList] := 
    List @@ Flatten[ll, Infinity, linkedList]; 
   addToList[ll_, value_] := linkedList[ll, value];
   pop[ll_] := Last@ll;
   emptyList[] := linkedList[];
   )
 ]

这是原始代码,与 Leonid在此处发布的完全相同,但我在顶部添加了 2 个参数,因此两个版本将运行完全相同的参数以更轻松地比较速度。当你运行它时,你会注意到,与上面相比,屏幕上的绘图生成速度有多快。

我想帮助找出速度差异的原因。我现在假设绘图的速度差异是由于 Manipulate 内部的 Dyanmics 交互,因为我知道算法在外面非常快。

max = 200; delT = 0.01;

ClearAll[linkedList, toLinkedList, fromLinkedList, addToList, pop, 
  emptyList];

(*SetAttributes[linkedList,HoldAllComplete];*)
toLinkedList[data_List] := Fold[linkedList, linkedList[], data];
fromLinkedList[ll_linkedList] := 
  List @@ Flatten[ll, Infinity, linkedList];
addToList[ll_, value_] := linkedList[ll, value];
pop[ll_] := Last@ll;
emptyList[] := linkedList[];

Clear[getData];
Module[{ll = emptyList[], time = 0, restart, plot, y}, 
 getData[] := fromLinkedList[ll];

 plot[] := 
  Graphics[{Hue[0.67`, 0.6`, 0.6`], Line[fromLinkedList[ll]]}, 
   AspectRatio -> 1/GoldenRatio, Axes -> True, 
   AxesLabel -> {"time", "angle"}, 
   PlotRange -> {{0, max}, {-Pi/4, Pi/4}}, PlotRangeClipping -> True];

 DynamicModule[{sol, angle, llaux}, 
  restart[] := (time = 0; llaux = emptyList[]);
  llaux = ll;
  sol := First@
    NDSolve[{y''[t] + 0.01 y'[t] + Sin[y[t]] == 0, y[0] == Pi/4, 
      y'[0] == 0}, y, {t, time, time + 1}];
  angle := y /. sol;

  ll := With[{res = 
      If[llaux === emptyList[] || pop[llaux][[1]] != time, 
       addToList[llaux, {time, angle[time]}],(*else*)llaux]},
    llaux = res
    ];

  Column[{
    Row[{Dynamic@delT, Slider[Dynamic[delT], {0.01, 1., 0.01}]}],
    Dynamic[time, {None, Automatic, None}], 
    Row[{Trigger[Dynamic[time], {0, max, Dynamic@delT}, 
       AppearanceElements -> {"PlayPauseButton"}],
      Button[Style["Restart", Small], restart[]]}
     ],
    Dynamic[plot[]]}, Frame -> True
   ]
  ]
 ]

在此处输入图像描述

再次感谢任何提示或尝试的事情。

更新

好的,这变得有趣了。我从来不知道只使用 Dynamics 就可以制作 CDF,我认为必须使用 Manipulate。但是我错了。我刚试过一个,它确实有效! 这是在我的网站上,阻尼驱动摆的模拟(由于关节上存在驱动力而表现出混沌运动),仅使用 Dynamics 编写,没有 Manipulate。

上述代码如下:

DynamicModule[{sol, angle, bob, r, time = 0, animationRate = 1},
 (*simulation of damped and driven pendulum, exhibit chaotic motion*)

 Dynamic@Grid[{
    {Trigger[Dynamic[time], {0, Infinity, 0.01}, animationRate, 
      AppearanceElements -> {"PlayPauseButton", "ResetButton"}], 
     Style["time (sec)", 10], Dynamic[time]},
    {
     Dynamic@Show[Graphics[{
         {Dashed, Gray, Thin, Circle[{0, 0}, 1]},
         {Red, Thick, Line[{{0, 0}, bob}]},
         {Blue, PointSize[0.1], Point[bob]}
         }, ImagePadding -> 10], ImageSize -> 300], SpanFromLeft
     }}, Frame -> True, Alignment -> Left],

 Initialization :>
  (
   sol := 
    First@NDSolve[{y''[t] + 0.1 y'[t] + Sin[y[t]] == 1.5 Cos[t], 
       y[0] == Pi/4, y'[0] == 0}, y, {t, time, time + 1}, 
      Sequence@ndsolveOptions];
   bob := {Sin[(y /. sol)[time]], - Cos[(y /. sol)[time]]};

   ndsolveOptions = {MaxSteps -> Infinity, 
     Method -> {"StiffnessSwitching", 
       Method -> {"ExplicitRungeKutta", Automatic}}, 
     AccuracyGoal -> 10, PrecisionGoal -> 10};
   )
 ]

在此处输入图像描述

这是我第一个使用直接动态的 CDF。如果你想看看更新屏幕的性能差异,这里是上面的一个版本,使用 Manipulate。在这种情况下,我没有注意到太大的区别,但请注意这是在绘制钟摆位置,不需要缓冲,也不需要数据处理。简单地逐点绘制鲍勃位置。

Manipulate[
 (
  sol = First@
    NDSolve[{y''[t] + 0.1 y'[t] + Sin[y[t]] == 1.5 Cos[t], 
      y[0] == Pi/4, y'[0] == 0}, y, {t, time, time + 1}, 
     Sequence@ndsolveOptions];
  bob = {Sin[(y /. sol)[time]], - Cos[(y /. sol)[time]]};

  Show[Graphics[{
     {Dashed, Gray, Thin, Circle[{0, 0}, 1]},
     {Red, Thick, Line[{{0, 0}, bob}]},
     {Blue, PointSize[0.1], Point[bob]}
     }, ImagePadding -> 10], ImageSize -> 300]
  ),

 {{time, 0, "run"}, 0, Infinity, 0.01, AnimationRate -> animationRate,
   AppearanceElements -> {"PlayPauseButton", "ResetButton"}},

 Initialization :>
  (
   animationRate = 1;
   ndsolveOptions = {MaxSteps -> Infinity, 
     Method -> {"StiffnessSwitching", 
       Method -> {"ExplicitRungeKutta", Automatic}}, 
     AccuracyGoal -> 10, PrecisionGoal -> 10};
   )
 ]

我认为现在可以仅从 Dynamics 制作 CDF 非常有趣。

4

1 回答 1

4

这是 AnimationRate 的问题。在两者中设置相同会给出相同的时间(有点难以触发顺便说一句):评估:

    Clear[getData];
Module[{ll = emptyList[], time = 0, restart, plot, y, timeinit}, 
 getData[] := fromLinkedList[ll];
 timeinit[n_ /; 0.01 < n < .5] := (init = AbsoluteTime[]);
 plot[] := 
  Graphics[{Hue[0.67`, 0.6`, 0.6`], Line[fromLinkedList[ll]]}, 
   AspectRatio -> 1/GoldenRatio, Axes -> True, 
   AxesLabel -> {"time", "angle"}, 
   PlotRange -> {{0, max}, {-Pi/4, Pi/4}}, PlotRangeClipping -> True, 
   PlotLabel :> 
    Row[{"seconds used: ", (timeinit[time]; 
       If[time < 1, "", Round[AbsoluteTime[] - init]])}]];
 DynamicModule[{sol, angle, llaux}, 
  restart[] := (time = 0; llaux = emptyList[]);
  llaux = ll;
  sol := First@
    NDSolve[{y''[t] + 0.01 y'[t] + Sin[y[t]] == 0, y[0] == Pi/4, 
      y'[0] == 0}, y, {t, time, time + 1}];
  angle := y /. sol;
  ll := With[{res = 
      If[llaux === emptyList[] || pop[llaux][[1]] != time, 
       addToList[llaux, {time, angle[time]}],(*else*)llaux]}, 
    llaux = res];
  Column[{Row[{Dynamic@delT, 
      Slider[Dynamic[delT], {0.01, 1., 0.01}]}], 
    Dynamic[time, {None, Automatic, None}], 
    Row[{Trigger[Dynamic[time], {0, max, Dynamic@delT}, 
       AppearanceElements -> {"PlayPauseButton"}, 
       AnimationRate -> 15], 
      Button[Style["Restart", Small], restart[]]}], Dynamic[plot[]]}, 
   Frame -> True]]]

(* 和: *)

    Manipulate[
 Module[{ll = emptyList[], sol, plot, t, y, angle, 
   llaux = emptyList[], timeinit, init},
  timeinit[n_ /; 0.01 < n < .5] := (init = AbsoluteTime[]);
  plot[] := 
   Graphics[{Hue[0.67`, 0.6`, 0.6`], Line[fromLinkedList[ll]]}, 
    AspectRatio -> 1/GoldenRatio, Axes -> True,
    PlotLabel :> 
     Row[{"seconds used: ", (timeinit[time]; 
        If[time < 1, "", Round[AbsoluteTime[] - init]])}],
    AxesLabel -> {"time", "angle"}, 
    PlotRange -> {{0, max}, {-Pi/4, Pi/4}}, PlotRangeClipping -> True];
  llaux = ll;
  sol := First@
    NDSolve[{y''[t] + 0.01 y'[t] + Sin[y[t]] == 0, y[0] == Pi/4, 
      y'[0] == 0}, y, {t, time, time + 1}];
  angle := y /. sol;
  ll := With[{res = 
      If[llaux === emptyList[] || pop[llaux][[1]] != time, 
       addToList[llaux, {time, angle[time]}],(*else*)llaux]}, 
    llaux = res];
  Dynamic[plot[]]], {{time, 0, "run"}, 0, max, Dynamic@delT, 
  AnimationRate -> 15, ControlType -> Trigger}, {{delT, 0.01, "delT"},
   0.01, 1, 0.01, Appearance -> "Labeled"}, {{y0, Pi/4, "y(0)"}, -Pi, 
  Pi, Pi/100, Appearance -> "Labeled"}, {{yder0, 0, "y'(0)"}, -1, 
  1, .1, Appearance -> "Labeled"}, {{linkedList, {}}, None}, 
 TrackedSymbols :> {time}, Initialization :> (max = 200;
   toLinkedList[data_List] := Fold[linkedList, linkedList[], data];
   fromLinkedList[ll_linkedList] := 
    List @@ Flatten[ll, Infinity, linkedList];
   addToList[ll_, value_] := linkedList[ll, value];
   pop[ll_] := Last@ll;
   emptyList[] := linkedList[];)]
于 2011-09-06T10:51:06.193 回答