Julia中的分段错误不一致

时间:2017-03-06 15:28:39

标签: segmentation-fault julia differential-equations plots.jl

我创造了一个阻尼摆的相平面的gif。 为此,我使用包Differentialququations.jl进行集成,使用包Plots.jl包含pyplot后端和ImageMagick.jl进行绘图和动画。为了做到这一点,我将函数集成到不同的初始值,并且每个函数都进行了很多次,因此我得到了一些小片段,它们以gif的形式一起形成最终的绘图。代码如下:

using DifferentialEquations
using Plots ; default(size=(1000,700))
pyplot()
using ImageMagick

x = linspace(0.0,50.0,101)
tfinal = collect(x)
b = 0.2
p = plot()
u0vals = ([pi,-2.41],[2.0*pi,-2.41],[-2.0*pi,-3.0],[-2pi,3.0])
anim = @animate for j=1:endof(tfinal)
  for i=1:endof(u0vals)
      f = function(t,u,du)
        du[1] = u[2]
        du[2] = -b*u[2] -sin(u[1])
      end
      u0 = u0vals[i]
      tspan=(0.0,tfinal[j])
      prob = ODEProblem(f, u0, tspan)
      sol = solve(prob)
      plot!(p, sol, vars=(1,2),arrow=true,legend = false, titlefont=font(15, "cursive"),title = "STRO Figure 6.7.7", linewidth=1,linecolor=:"black",xlims=(-15.0,10.0),border=false,ticks=nothing,grid=false)
  end
end
gif(anim, "STRO_6_7_7_GIF_.gif",fps=5)

这个代码第一次运行时效果很好。但是,我尝试再次运行它来更改tfinal并显示:signal (11): Segmentation fault: 11(这只是来自julia的消息的第一行)。然后我尝试再次运行初始代码,我收到相同的消息。重新启动julia和计算机后,我收到相同的消息。我再次更改了tfinal,它再次有效,并且我想要使用的每个tfinal。我有我想要的输出。然而,令我困扰的是,julia"反应"我的代码不一致。如果需要,我可以粘贴完整的错误但很长。

编辑:这是完整的错误消息

signal (11): Segmentation fault: 11
while loading no file, in expression starting on line 0
ode_interpolation at /Users/pacagal/.julia/v0.5/OrdinaryDiffEq/src/dense/generic_dense.jl:134
[inlined]
InterpolationData at /Users/pacagal/.julia/v0.5/OrdinaryDiffEq/src/interp_func.jl:20
unknown function (ip: 0x32b584519)
macro expansion at /Users/pacagal/.julia/v0.5/DiffEqBase/src/solutions/solution_interface.jl:72 [inlined]
apply_recipe at /Users/pacagal/.julia/v0.5/RecipesBase/src/RecipesBase.jl:238
unknown function (ip: 0x32b5808c6)
jl_call_method_internal at /Users/osx/buildbot/slave/package_osx10_9 x64/build/src/./julia_internal.h:189 [inlined]
jl_apply_generic at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/gf.c:1942
jl_apply at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/./julia.h:1392 [inlined]
jl_f__apply at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/builtins.c:547
_process_userrecipes at /Users/pacagal/.julia/v0.5/Plots/src/pipeline.jl:73
_plot! at /Users/pacagal/.julia/v0.5/Plots/src/plot.jl:171
unknown function (ip: 0x32b57bbed)
jl_call_method_internal at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/./julia_internal.h:189 [inlined]
jl_apply_generic at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/gf.c:1942
#plot!#266 at /Users/pacagal/.julia/v0.5/Plots/src/plot.jl:151
jl_call_method_internal at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/./julia_internal.h:189 [inlined]
jl_apply_generic at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/gf.c:1942
jl_apply at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/./julia.h:1392 [inlined]
jl_f__apply at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/builtins.c:547
#plot! at ./<missing>:0
jl_call_method_internal at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/./julia_internal.h:189 [inlined]
jl_apply_generic at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/gf.c:1942
macro expansion; at ./REPL[10]:11 [inlined]
macro expansion; at /Users/pacagal/.julia/v0.5/Plots/src/animation.jl:154 [inlined]
anonymous at ./<missing> (unknown line)
unknown function (ip: 0x32b5566ff)
jl_call_method_internal at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/./julia_internal.h:189 [inlined]
jl_toplevel_eval_flex at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/toplevel.c:569
jl_toplevel_eval_in_warn at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/builtins.c:590
eval at ./boot.jl:234
jlcall_eval_19752 at /Applications/Julia-0.5.app/Contents/Resources/julia/lib/julia/sys.dylib (unknown line)
jl_call_method_internal at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/./julia_internal.h:189 [inlined]
jl_apply_generic at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/gf.c:1942
eval_user_input at ./REPL.jl:64
unknown function (ip: 0x3192d9e66)
jl_call_method_internal at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/./julia_internal.h:189 [inlined]
jl_apply_generic at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/gf.c:1942
macro expansion at ./REPL.jl:95 [inlined]
#3 at ./event.jl:68
unknown function (ip: 0x3192d30df)
jl_call_method_internal at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/./julia_internal.h:189 [inlined]
jl_apply_generic at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/gf.c:1942
jl_apply at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/./julia.h:1392 [inlined]
start_task at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/task.c:253
Allocations: 40525029 (Pool: 40521241; Big: 3788); GC: 71

[Proceso completado]

1 个答案:

答案 0 :(得分:1)

分段错误是因为你的时间跨度很小。

x = linspace(0.0,50.0,101)
tfinal = collect(x)

然后注意tfinal[1]=0.0,因此第一个时间跨度为(0.0,0.0)。它正在做的是提供一个简单的diffeq解决方案,然后尝试绘制它,最后得到一个边界错误,但边界检查被关闭。哎哟。我提交了一个提交,这样下一个补丁就会抛出一个错误,警告用户时间跨度很小。

这意味着解决这个问题的方法是删除第一个时间点:

x = linspace(0.0,50.0,101)
tfinal = collect(x)[2:end]

这样可以。

您可能还想查看提供的animation interface。不确定它是否可以轻松地按照您想要的顺序为所有内容设置动画:可能需要更多Plots.jl控件。