一千萬個為什麽

搜索

使用操作時圖形性能顯著下降

我一直在制作這些mathematica 3D物體,這些物體涉及一種在其上方顯示某種表面的分子。這很容易通過以下方式完成: Molecule=Graphics3D[{{x,y,z},...},Sphere[1, .24]...,Cylinder[{1, 2}, .15]] PES=ListPlot3D[{{x,y,z},...}] Show[Molecule,PES]

一個python腳本很容易為分子鞭打它;對我來說它比wolfram化學數據庫更好,因為它是更好的單位(埃)並且可以更容易定向。它工作得很好,對象操作流暢。現在我想做一些非常好的最終產品。在完成在線演示之後,我開始使用初始化代碼: Molecule[coord, conf, col, rad, bondstyle_] := Module[{intconf, atomplot, tuples, bonds, bondplot}, intconf = ReplacePart[conf, [email protected][# -> ii & /@ ([email protected] Position[conf, DeleteDuplicates[conf][[ii]]]), {ii, 1, [email protected][conf]}]]; atomplot = {col[[intconf[[#]]]], Sphere[coord[[#]], rad[[intconf[[#]]]]]} & /@ Range[Length[intconf]]; tuples = Select[Subsets[ Range[Length[ coord]], {2}], ((conf[[#[[1]]]] =!= "H" || conf[[#[[2]]]] =!= "H") && Norm[coord[[#[[1]]]] - coord[[#[[2]]]]] <= bondstyle[[1]]) &]; bonds = coord[[#]] & /@ tuples; bondplot = Table[{col[[intconf[[tuples[[#, ii]]]]]], Tube[{bonds[[#, ii]], .5*Total[bonds[[#]]]}, bondstyle[[2]]]} & /@ Range[Length[bonds]], {ii, 1, 2}]; {atomplot, bondplot}]; coord["Acrolein"] := {{-1.4373, -1.1224, 0.4127}, {0.1565, 0.3728, -0.3457}, {-1.1456, -0.2881, -0.399}, {1.0519, 0.0649, 0.6058}, {0.4003, 1.1146, -1.0773}, {-1.8435, -0.0263, -1.1667}, {0.8173, -0.6754, 1.3418}, {2.0003, 0.5599, 0.6283}}; conf["Acrolein"] := {"O", "C", "C", "C", "H", "H", "H", "H"};

我決定使用它,因為我可以使用Map來操縱對象的位置和比例。它真的只是一種替換我的python腳本的方法。使用僅用於復制象限的Map,再次可以輕松創建曲面: AcrWater=Import["xyzlist.csv" surface["Acrolein", "Water"] = ListPlot3D[Union[AcrWater, Map[#*{1, -1, 1} &, AcrWater]], Mesh -> 3, ColorFunction -> "Rainbow", PlotStyle -> Opacity[0.9]];

下一部分剛剛在我的實驗中完成,使圖形更流暢: graphic["Acrolein", "Water"] = Show[

Graphics3D[{Specularity[White, 100], Molecule[coord["Acrolein"], conf["Acrolein"], ColorData["Atoms", #] & /@ DeleteDuplicates[ conf["Acrolein"]], {.4, .4, .4, .4}, {1.5, .193}]}],

surface["Acrolein", "Water"],

ImageSize -> {600, 600}, Background -> White, ViewPoint -> Top, Axes -> True, Lighting -> "Neutral"];

最後是操縱參數: Manipulate[ graphic[substance, solvent], {{substance, "Acrolein", "Molecule"}, {"Acrolein"}}, {{solvent, "1C", "Solvent"}, {"Water"}}]

所有這一切都很有效!唯一的問題是操縱最終產品的速度非常慢,即使在一臺新的,裝備精良的機器上,抗鋸齒一直向下。總共將有大約30種不同的組合,只實施一個組合,表明演示的總體感覺顯著下降;添加更多甚至更糟。當它以前一種方式完成時,即使有更大的系統也沒有明顯的滯後,這似乎很奇怪。我的問題是這裏的主要放緩點在哪裏。據我所知,矢量對象首先被評估,然後由操作命令顯示。如果沒有簡單的解決方法,還有另一種方法可以避免減速問題。

編輯:感謝您的提示到目前為止動態和跟蹤符號肯定做了明顯的改進。這裏有一大堆工作代碼可以模仿我想要做的事情。這是一個單一的例子,表面只有25個點(實際表面將有1000-2000)。我不完全確定如何做到這一點,但使用數據的正確表示法重新創建表面的更好方法是[x,y,sin [x + y]]其中x和y以0-3為步長0.1。而不是動態我也試過刷新命令無濟於事: Molecule[coord, conf, col, rad, bondstyle_] := Module[{intconf, atomplot, tuples, bonds, bondplot}, intconf = ReplacePart[conf, [email protected][# -> ii & /@ ([email protected] Position[conf, DeleteDuplicates[conf][[ii]]]), {ii, 1, [email protected][conf]}]]; atomplot = {col[[intconf[[#]]]], Sphere[coord[[#]], rad[[intconf[[#]]]]]} & /@ Range[Length[intconf]]; tuples = Select[Subsets[ Range[Length[ coord]], {2}], ((conf[[#[[1]]]] =!= "H" || conf[[#[[2]]]] =!= "H") && Norm[coord[[#[[1]]]] - coord[[#[[2]]]]] <= bondstyle[[1]]) &]; bonds = coord[[#]] & /@ tuples; bondplot = Table[{col[[intconf[[tuples[[#, ii]]]]]], Tube[{bonds[[#, ii]], .5*Total[bonds[[#]]]}, bondstyle[[2]]]} & /@ Range[Length[bonds]], {ii, 1, 2}]; {atomplot, bondplot}]; coord["Acrolein"] := {{-1.4373, -1.1224, 0.4127}, {0.1565, 0.3728, -0.3457}, {-1.1456, -0.2881, -0.399}, {1.0519, 0.0649, 0.6058}, {0.4003, 1.1146, -1.0773}, {-1.8435, -0.0263, -1.1667}, {0.8173, -0.6754, 1.3418}, {2.0003, 0.5599, 0.6283}}; conf["Acrolein"] := {"O", "C", "C", "C", "H", "H", "H", "H"};

AcrWater := Table[Sin[j + i] + 2, {i, 0, 2 Pi, Pi/4}, {j, 0, 2 Pi, Pi/4}]

surface["Acrolein", "Water"] := ListPlot3D[AcrWater, Mesh -> 3, ColorFunction -> "Rainbow", PlotStyle -> Opacity[0.9]];

graphic["Acrolein", "Water"] := Dynamic[Show[ Graphics3D[{Specularity[White, 100], Molecule[coord["Acrolein"], conf["Acrolein"], ColorData["Atoms", #] & /@ DeleteDuplicates[ conf["Acrolein"]], {.4, .4, .4, .4}, {1.5, .193}]}], surface["Acrolein", "Water"], ImageSize -> {600, 600}, Background -> White, ViewPoint -> Top, Axes -> True, Lighting -> "Neutral"], None];

Manipulate[ graphic[substance, solvent], {{substance, "Acrolein", "Molecule"}, {"Acrolein"}}, {{solvent, "Water", "Solvent"}, {"Water"}}, TrackedSymbols :> {substance, solvent}]

最佳答案

我覺得你很痛苦。問題是,在簡化的自包含示例中很難復制減速。我的建議如下:

Put in a couple of Print statments to see how many times your code is re-evaluated. You'll probably be surprised, that it's a lot more than you thought.

Use Memoization whenever possible (but since graphic is defined with Set you should be ok...).

Use TrackedSymbols :> {substance, solvent} to prevent needless re-evaluation.

如果你設法生成一個演示問題的工作示例,那麽我可以更具體。

轉載註明原文: 使用操作時圖形性能顯著下降