Search code examples
pythonfipy

Computing simultaneous Partial differential equation using FIPY in python


I was trying to solve pde using fipy, since I'm new to it; I'm unable to debug error in my code. It is not giving any spatial pattern but just different monochromatic square(s) over a period of time.The equation which I'm trying to solve are-

(∂u(x,y,t))/∂t=G_1 (u,v)+d_11 ∇^2 u+d_12 ∇^2 v and

(∂v(x,y,t))/∂t=G_2 (u,v)+d_21 ∇^2 u+d_22 ∇^2 v

from fipy import *

nx=ny=200
dx=dy=0.25
L=dx*nx
dt=0.01
E=1.0
A=0.91881 #Alpha
B=0.0327 #Beta
D=0.05 #Delta
G=0.15 #Gamma
d11=0.1
d12=0.01
d21=0.5
d22=1.5

mesh =Grid2D(dx=dx,dy=dy,nx=nx,ny=ny)
u=CellVariable(name='u Variable',mesh=mesh) #pray
v=CellVariable(name='v Variable',mesh=mesh) #predator

u.setValue(GaussianNoiseVariable(mesh=mesh,mean=0.18,variance=0.01))
v.setValue(GaussianNoiseVariable(mesh=mesh,mean=0.6,variance=0.01))

eq_u=(TransientTerm(coeff=1,var=u)==u*(1-u)-(u*v*E)/(u+A*v)+ImplicitDiffusionTerm(coeff=d11,var=u)+ImplicitDiffusionTerm(coeff=d12,var=v))
eq_v=(TransientTerm(coeff=1,var=v)==B*v*(1-v)+(u*v*G)/(u+A*v)-D*v+ImplicitDiffusionTerm(coeff=d21,var=u)+ImplicitDiffusionTerm(coeff=d22,var=v))

#creating viewer
if __name__ == "__main__":
    viewer_u=Viewer(vars=u,datamin=0.16,datamax=0.18) 
    viewer_u.plot()
    viewer_v=Viewer(vars=v,datamin=0.0,datamax=0.4)
    viewer_v.plot()

#solving
steps=250
for step in range(steps):
    eq_u.solve(var=u,dt=dt)
    eq_v.solve(var=v,dt=dt)
    if __name__ == "__main__":
        viewer_u.plot()
        viewer_v.plot()

Solution

    • These equations are ODEs, not PDEs. FiPy may be able to solve them, but there are other tools much better optimized for solving ODEs, such as SciPy's odeint and scikits.odes.
    • I am not sure exactly what you mean by "It is not giving any pattern but just different monochromatic square(s) over a period of time." The random initial condition does not appear to evolve, but if I increase dt to 1.0, then V evolves to a uniform value of about 0.5. U does not appear to evolve for me, but that's a display glitch of some kind. V evolves to 0.598 and U evolves to 0.179. There is no pattern in space (if that's what you were expecting) because these equations have no spatial dependence; they only have partial derivatives in time, so there is absolutely nothing to cause the variables to flux from one cell to another.
    • You appear to get away with it here, but your variables should always be defined on the same mesh, rather than u_mesh for U and v_mesh for V.
    • No-flux boundary conditions are the default in FiPy, so there's no reason to specify U.faceGrad.constrain, etc.
      • Furthermore, there are no fluxes in these equations (because they're ODEs), so setting boundary fluxes isn't meaningful.
    • Normally I would recommend making your equations more implicit, coupling them together, and sweeping, but none of this seems to have much effect on their evolution in this case.