朱莉娅挑战-FitzHugh–Nagumo模型PDE Runge-Kutta解算器 [英] Julia challenge - FitzHugh–Nagumo model PDE Runge-Kutta solver
问题描述
我是Julia编程语言的新手,所以我不太了解如何优化代码.我听说Julia与Python相比应该更快,但是我写了一个简单的 Julia代码来解决FitzHugh – Nagumo模型,它似乎没有比Python快.
I am newbie in Julia programming language, so I don't know much of how to optimize a code. I have heard that Julia should be faster in comparison to Python, but I've written a simple Julia code for solving the FitzHugh–Nagumo model , and it doesn't seems to be faster than Python.
FitzHugh–Nagumo模型方程为:
The FitzHugh–Nagumo model equations are:
function FHN_equation(u,v,a0,a1,d,eps,dx)
u_t = u - u.^3 - v + laplacian(u,dx)
v_t = eps.*(u - a1 * v - a0) + d*laplacian(v,dx)
return u_t, v_t
end
其中,u
和v
是变量,它们是2D字段(即2维数组),而a0,a1,d,eps
是模型的参数.参数和变量均为Float类型. dx
是控制拉普拉斯函数的参数,用于控制网格点之间的间隔,拉普拉斯函数是对具有周期性边界条件的有限差分的一种实现.
where u
and v
are the variables, which are 2D fields (that is, 2 dimensional arrays), and a0,a1,d,eps
are the model's parameters. Both parameters and the variables are of type Float. dx
is the parameter that control the separation between grid point, for the use of the laplacian function, which is an implementation of finite differences with periodic boundary conditions.
如果你们中的朱莉娅专家级编码人员可以给我提示如何在朱莉娅中做得更好,我将很高兴听到.
If one of you expert Julia coders can give me a hint of how to do things better in Julia I will be happy to hear.
Runge-Kutte步骤功能为:
The Runge-Kutte step function is:
function uv_rk4_step(Vs,Ps, dt)
u = Vs.u
v = Vs.v
a0=Ps.a0
a1=Ps.a1
d=Ps.d
eps=Ps.eps
dx=Ps.dx
du_k1, dv_k1 = FHN_equation(u,v,a0,a1,d,eps,dx)
u_k1 = dt*du_k1י
v_k1 = dt*dv_k1
du_k2, dv_k2 = FHN_equation((u+(1/2)*u_k1),(v+(1/2)*v_k1),a0,a1,d,eps,dx)
u_k2 = dt*du_k2
v_k2 = dt*dv_k2
du_k3, dv_k3 = FHN_equation((u+(1/2)*u_k2),(v+(1/2)*v_k2),a0,a1,d,eps,dx)
u_k3 = dt*du_k3
v_k3 = dt*dv_k3
du_k4, dv_k4 = FHN_equation((u+u_k3),(v+v_k3),a0,a1,d,eps,dx)
u_k4 = dt*du_k4
v_k4 = dt*dv_k4
u_next = u+(1/6)*u_k1+(1/3)*u_k2+(1/3)*u_k3+(1/6)*u_k4
v_next = v+(1/6)*v_k1+(1/3)*v_k2+(1/3)*v_k3+(1/6)*v_k4
return u_next, v_next
end
我已经使用了PyPlot包中的imshow()来绘制u字段.
And I've used imshow() from PyPlot package to plot the u field.
推荐答案
这不是一个完整的答案,而是尝试对laplacian
函数进行优化的尝试.在10x10矩阵上的原始laplacian
给了我@time:
This is not a complete answer, but a taste of an optimization attempt on the laplacian
function. The original laplacian
on a 10x10 matrix gave me the @time:
0.000038 seconds (51 allocations: 12.531 KB)
此版本:
function laplacian2(a,dx)
# Computes Laplacian of a matrix
# Usage: al=laplacian(a,dx)
# where dx is the grid interval
ns=size(a,1)
ns != size(a,2) && error("Input matrix must be square")
aa=zeros(ns+2,ns+2)
for i=1:ns
aa[i+1,1]=a[i,end]
aa[i+1,end]=a[i,1]
aa[1,i+1]=a[end,i]
aa[end,i+1]=a[1,i]
end
for i=1:ns,j=1:ns
aa[i+1,j+1]=a[i,j]
end
lap = Array{eltype(a),2}(ns,ns)
scale = inv(dx*dx)
for i=1:ns,j=1:ns
lap[i,j]=(aa[i,j+1]+aa[i+2,j+1]+aa[i+1,j]+aa[i+1,j+2]-4*aa[i+1,j+1])*scale
end
return lap
end
给@time:
0.000010 seconds (6 allocations: 2.250 KB)
请注意减少分配.额外的分配通常表明有进行优化的潜力.
Notice the reduction in allocations. Extra allocations usually indicate the potential for optimization.
这篇关于朱莉娅挑战-FitzHugh–Nagumo模型PDE Runge-Kutta解算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!