#! /usr/bin/env python
# -*- coding: utf-8 -*-
#-------------------------------------------------
#- (c) Yaya Youssouf
[email protected]
# Created : 20/09/2022
# Last update: 20/09/2022
#-------------------------------------------------
import random as rd
import numpy as np
import pandas as pd
import spp_module as spp
from time import process_time
Begin_Time = process_time()
pp = spp.PreyPred()
#-- Global Setup -----------------------------------------#
pi = np.pi
ST = 2002 # Number of time steps
dt = 1 # Time step
B = 125 # Size of the periodic box
B2 = B*0.5
P = B
t = 0
NAME = "Predator-Prey"
epsilon = 0.00000001
File_format = "%5.4f %5.4f %5.4f %5.4f\n"
#-- Predator's setUp ---------------------------------------#
#-- Initialization ------------------------------------#
COOR_q = True
POL_q = True
File_Name_q = 'PredPosVel'
#-- Motion variables -----------------------------------#
#eta_q = 0.09 # Noise of Gaussian distribution
eta_q = 0.05 # Adapted from old standard parameters to new DEVC_q definition
#DEVC_q = eta_q*SQRT(pi/2)
DEVC_q = 2*(pi/3)*eta_q
Rr_q = 1. # Radius of repulsion
Ra_q = 4.0 # Radius of alignment
Rb_q = 6.0 # Radius of zone of Predator birth
#Ra_q = Ra_q**2
#Rr_q = Rr_q**2
#Rb_q = Rb_q**2
Vs_q = 0.5 # Predator's speed
angle_q = -1.0
alpha_q = np.arccos(angle_q)
#-- Evolution variables ---------------------------------#
np_q = 20000 # Maximum number of Predators
npo_q = 200 # Initial number of Predators
Rp_q = 1.0 # Radius of predation
SigmaE_q = np.sqrt(7.5) # Enerige deviation
Em_q = 0.01 # Energie deducted after each time step
Ea_q = 0.025 # Energie added after each eating
Er_q = 8.0 # Energie Limite to split
Es_q = -8.0 # Energie limit to die
#-- Prey's setup -------------------------------------#
#-- Initialization ------------------------------------#
COOR_p = True
Rx_p = 0.0
Ry_p = 0.0
File_Name_p = 'PreyPosVel'
#-- Motion variables -----------------------------------#
gamma_p = 0.2 # Coefficient of viscous friction
Q_p = 0.01 # Temperature
Vo_p = 1.0 # Speed
M_p = 1.0 # Particle Mass
inv_p = 1/(2*dt)
C_p = np.sqrt(2*gamma_p*Q_p)
#-- Evolution variables ---------------------------------#
np_p = 25000 # Maximum number of Prey
npo_p = 2000 # Initial number of Prey
Rb_p = 0.5 # Radius of zone of Prey birth
SigmaE_p = np.sqrt(8.5) # Enerige deviation
Er_p = 6.0 # Energie Limite to split
Es_p = -8.0 # Energie limit to die
#-- Predator's memory allocation ------------------------------!
ALLIGN_q=[False]*np_q
REPUL_q =[False]*np_q
Ext_q = [False]*np_q
Rx_q, Ry_q, Vx_q, Vy_q , Vax_q, Vay_q = np.zeros([np_p]), np.zeros([np_p]), np.zeros([np_p]), np.zeros([np_p]), np.zeros([np_p]), np.zeros([np_p])
V1x_q, V1y_q ,Rs_q, R1_q, R2_q, E_q = np.zeros([np_p]), np.zeros([np_p]), np.zeros([np_p]), np.zeros([np_p]), np.zeros([np_p]), np.zeros([np_p])
#-- Prey's memory allocation ------------------------------!
Ext_p = [False]*np_p
Rx_p, Ry_p, R1x_p, R1y_p, Vx_p, Vy_p = np.zeros([np_p]), np.zeros([np_p]), np.zeros([np_p]), np.zeros([np_p]), np.zeros([np_p]), np.zeros([np_p])
Roldx_p,Roldy_p, Fx_p, Fy_p, zetax_p, zetay_p, E_p =np.zeros([np_p]), np.zeros([np_p]), np.zeros([np_p]), np.zeros([np_p]), np.zeros([np_p]), np.zeros([np_p]), np.zeros([np_p])
#-- Predator's position & velocity setup --------------------#
Ext_q[0:npo_q-1] = [True]*npo_q
Rep_q = [False]*np_q
L= 1
for i in range(npo_q):
Rx_q[i], Ry_q[i], Vx_q[i], Vy_q[i] = pp.PredPosVel(B, Vs_q)
E_q[i], Xres = pp.Normal(0.0, SigmaE_q)
#print(i, Rx_q[i],Ry_q[i], Vx_q[i], Vy_q[i])
#-- Prey's position & velocity setup --------------------#
Ext_p[0:npo_p-1] = [True]*npo_p
Rep_p = [False]*np_p
for i in range(npo_p):
[Rx_p[i],Ry_p[i],Roldx_p[i],Roldy_p[i],Vx_p[i],Vy_p[i]] = pp.PreyPosVel(B, Rb_p, 0., 1/3.)
#print( i, Rx_p[i])
# Creation des Fichiers de sortie
NP = open("NP.dat", "w")
PredPosVel = open("PredPosVel.dat", "w")
Polar = open("Polar.dat", "w")
#-----------------------------------------------------------!
#------------- Main Loop -------------------------------!
#!-----------------------------------------------------------!
for L in range(ST):
t+= dt
ord_q = 0
speed_q = 0.0
if (L>=1000) and (L<=2000):
COOR_p, COOR_q , POL_q = True, True, True
# NP.write(str(L)+ 2*" "+str(npo_q)+ 2*" "+ str (npo_p)+"\n")
else: COOR_p, COOR_q , POL_q = False, False, False
#-- Predator programme part -------------------------------- !
File_NameTemp_q = File_Name_q+str(L+1)
fq_object = open(File_NameTemp_q, "w")
#-- 1.Motion -----------------------------------------------!
for i in range(np_q): # First i loop
if (Ext_q[i]==False):
continue
Vax_q[i] = 0.0
Vay_q[i] = 0.0
Xij_q = 0.
Yij_q = 0.
ALLIGN_q[i] = False
REPUL_q[i] = False
for j in range(i+1, np_q): # j loop
if (Ext_q[j]==False):
continue
#print[j]
R1_q[j] = Rx_q[j] - Rx_q[i]
R2_q[j] = Ry_q[j] - Ry_q[i]
#print(j, Rx_q[j])
R1_q[j], R2_q[j] = pp.AdjustPos(R1_q[j], R2_q[j], B)
Rs_q[j] =np.sqrt( R1_q[j]**2 + R2_q[j]**2)
#print (j,Rs_q[j],Rr_q)
if (Rs_q[j] < Rr_q):
REPUL_q[i] = True
Xij_q = Xij_q + (Rx_q[j] - Rx_q[i])
Yij_q = Yij_q + (Ry_q[j] - Ry_q[i])
#print("REPUL_q[i] =" ,REPUL_q[i] )
# End of j loop
if (REPUL_q[i]==True ):
Vn_q = np.sqrt(Xij_q**2 + Yij_q**2)
if(Vn_q != 0.0):
Vn_q = 1/Vn_q
V1x_q[i] = -Xij_q*Vn_q*Vs_q
V1y_q[i] = -Yij_q*Vn_q*Vs_q
else:
REPUL_q[i] = False
V1x_q[i] = Vx_q[i]
V1y_q[i] = Vy_q[i]
if (REPUL_q[i]==False ):
for j in range (i+1,np_q):
if (Ext_q[j] == False):
continue
if (Rs_q[j] < Ra_q) :
MM2_q = R1_q[j]*Vx_q[i] + R2_q[j]*Vy_q[i]
RSQ1_q = np.sqrt(R1_q[j]**2 + R2_q[j]**2)
SSQ1_q =np.sqrt(Vx_q[i]**2 + Vy_q[i]**2)
RV_q = MM2_q/(RSQ1_q*SSQ1_q)
if (RV_q > angle_q):
ALLIGN_q[i] =True
Vax_q[i] = Vax_q[i] + Vx_q[j]
Vay_q[i] = Vay_q[i] + Vy_q[j]
#print("REPUL_q[i], ALLIGN_q[i] =" ,REPUL_q[i], ALLIGN_q[i] )
if (ALLIGN_q[i]==True ):
Vax_q[i] = Vax_q[i] + Vx_q[i]
Vay_q[i] = Vay_q[i] + Vy_q[i]
Vn_q = np.sqrt(Vax_q[i]**2 + Vay_q[i]**2)
if (Vn_q != 0.0):
Vn_q = 1/Vn_q
V1x_q[i] = Vax_q[i]*Vn_q*Vs_q
V1y_q[i] = Vay_q[i]*Vn_q*Vs_q
else:
ALLIGN_q[i]= False
V1x_q[i] = Vx_q[i]
V1y_q[i] = Vy_q[i]
if (ALLIGN_q[i]==False ):
if (REPUL_q[i]==False):
V1x_q[i] = Vx_q[i]
V1y_q[i] = Vy_q[i]
#End Loop i
for i in range(np_q): # Sencond i loop
if (Ext_q[i]==False): continue
# Guassian Noise
Vx_q[i], Vy_q[i]= pp.GAUSSIAN_NOISE(V1x_q[i],V1y_q[i],Vs_q,0.0,DEVC_q)
# Integration
Rx_q[i] = Rx_q[i] + Vx_q[i]*dt
Ry_q[i] = Ry_q[i] + Vy_q[i]*dt
Rx_q[i] = np.mod((Rx_q[i]),P)
Ry_q[i] = np.mod((Ry_q[i]),P)
if ( COOR_q):
fq_object.write(File_format % (Rx_q[i],Ry_q[i],Vx_q[i],Vy_q[i]))
#print(COOR_q)
if (COOR_q):
#print(i, Rx_q[i])
PredPosVel.write(str(L)+ 2*" "+str(Rx_q[i])+ 2*" "+ str (Ry_q[i])+ 2*" "+str(Vx_q[i])+ 2*" "+ str (Vy_q[i])+"\n")
# !--- Order parameters
if (POL_q):
speed_q = np.sqrt(Vx_q[i]**2+Vy_q[i]**2)
ord_q = ord_q+np.complex(Vx_q[i],Vy_q[i])/speed_q
# End second i loop
# !--- writing Order parameters
if (POL_q):
ord_q = ord_q/npo_q
polar_q = np.sqrt((ord_q.real)**2+(ord_q.imag)**2)
Polar.write("%i %5.4f\n" %( L,polar_q))
#print (polar_q)
#!-- 2. Predator Eating ------------------------------------------ !
d_p, d_q = 0., 0. # Counter of died Prey and Pred
for i in range(np_q):
if (Ext_q[i]==False): continue
for j in range(1, np_p):
if (Ext_p[j]==False): continue
R1=Rx_q[i]-Rx_p[j]
R2=Ry_q[i]-Ry_p[j]
R1, R2 = pp.AdjustPos( R1, R2, B)
Rs = np.sqrt(R1**2+R2**2)
if (Rs < Rp_q):
E_q[i]=E_q[i]+Ea_q #! Update Energie of i-th Predator
npo_p=npo_p-1 #! One more Prey is died
d_p = d_p+1
Ext_p[j] = False
E_q[i]=E_q[i]-Em_q #! Metabolism
if (E_q[i] > Er_q): # ! Reproduction condition
Rep_q[i]= True
else: Rep_q[i]= False
if (E_q[i] < Es_q ): # Death check
npo_q = npo_q - 1
Ext_q[i]= False
d_q=d_q+1
# !-- 4.Predator reproduction --------------------------------
b_q = 0 #! Counter of new Predator born
for i in range(np_q):
if ( Rep_q[i] == False): continue
for j in range(np_q):
if (Ext_q[j]==True): continue
Ext_q[j] = True
npo_q = npo_q+1
b_q = b_q+1
E_q[j],Xres = pp.Normal(0., SigmaE_q)
Rx_q[j],Ry_q[j],Vx_q[j],Vy_q[j] = pp.CreateNewPred(Rx_q[i],Ry_q[i],Vs_q,Rb_q)
if (Ext_q[j] ==True): break
pp.GardeFou(npo_q,np_q,npo_p,np_p,L)
#!-- END of Predator programme part --------------------
#!-- Prey programme part -------------- !
File_NameTemp_p = File_Name_p+str(L+1)
fp_object = open(File_NameTemp_p, "w")
#!--1. Motion------------------------------------
for i in range(np_p):
if (Ext_p[i]==False): continue
R1x_p[i]=np.mod(Rx_p[i],P)
R1y_p[i]=np.mod(Ry_p[i],P)
if (COOR_p):
fp_object.write(File_format % (R1x_p[i],R1y_p[i],Vx_p[i],Vy_p[i]))
#pp.WriteIn_PosVel('PreyPosVel',L,"%5.4f %5.4f %5.4f %5.4f\n",15,R1x_p[i],R1y_p[i],Vx_p[i],Vy_p[i])
#print(R1x_p[i],R1y_p[i],Vx_p[i],Vy_p[i])
zetax_p[i],zetay_p[i] = pp.Normal(0.,1.)
Fx_p[i] = -gamma_p*Vx_p[i] + C_p*zetax_p[i]
Fy_p[i] = -gamma_p*Vy_p[i] + C_p*zetay_p[i]
R1_p = 2*Rx_p[i] - Roldx_p[i] + Fx_p[i]*dt**2
R2_p = 2*Ry_p[i] - Roldy_p[i] + Fy_p[i]*dt**2
Vx_p[i] = (R1_p - Roldx_p[i])*inv_p
Vy_p[i] = (R2_p - Roldy_p[i])*inv_p
Roldx_p[i] = Rx_p[i]
Roldy_p[i] = Ry_p[i]
Rx_p[i] = R1_p
Ry_p[i] = R2_p
# !-- 2. Prey Survive ------------------------------------------ !
for i in range(np_p):
if (Ext_p[i]==False): continue
E_p[i], Xres = pp.Normal(0.0,SigmaE_p)
# !-- 3.Prey reproduction --------------------------------
b_p = 0 #! Counter of new Prey born
for i in range(np_p):
if (Ext_p[i]==False): continue
if (E_p[i] >= Er_p ):
Rep_p[i] = True
else : Rep_p[i] = False
for i in range(np_p):
if (Rep_p[i] == False) : continue
for j in range(np_p):
if (Ext_p[j] == True) : continue
Ext_p[j] = True
npo_p = npo_p+1
b_p = b_p+1
E_p[j],Xres= pp.Normal(0.0,SigmaE_p)
Rx_p[j],Ry_p[j], Roldx_p[j],Roldy_p[j],Vx_p[j],Vy_p[j] = pp.PreyPosVel(Rb_p, B, 0., 1./3)
if (Ext_p[j] == True) : break
# !-- 4.Prey death----------------------------------
d_p = 0 #! Counter of number of Predator died
for i in range(np_p):
if (Ext_p[i] == True) : continue
if (E_p[i] <= Es_p) :
npo_p = npo_p - 1
Ext_p[i] = False
d_p = d_p + 1
# !-- END of Prey programme part-----------------!
#!------------------------------------------------------!
# END DO !---- END of the main loop
#!------------------------------------------------------!
#!-- Deallocation of Prey data
if (COOR_p):
NP.close()
fp_object.close()
# !-- Deallocation of Predator data
if (POL_q):
Polar.close()
if (COOR_q):
fq_object.close()
NP.close()
PredPosVel.close()
End_Time = process_time()
Time_Elapsed = End_Time-Begin_Time
print ( "Program: ", NAME ,'is well Finished at ', End_Time)
print ("Time duration :", Time_Elapsed, "Seconds" )
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 20 17:35:04 2022
@author: yaya
"""
import random as rd
import numpy as np
pi = np.pi
class PreyPred(object):
def Normal(self, Mu, Sigma):
"""!!! Return two normal distributed value Z1 and Z2 with Mu mean an Sigma
! standaUniform deviation
! Using Normal transformation"""
u1, u2 =rd.random(),rd.random()
z1 = Mu + Sigma*np.sqrt(-2*np.log(u1))*np.cos(2*pi*u2)
z2 = Mu + Sigma*np.sqrt(-2*np.log(u1))*np.sin(2*pi*u2)
return z1, z2
def normalize(self, dx,dy):
return dx/np.sqrt(dx**2+dy**2), dy/np.sqrt(dx**2+dy**2)
def PredPosVel(self,Box,Vs_q ):
"""!!! For a given Box size (Box) and Speed (Vs), this subroutine
! return X and Y random position (Rx an Ry) from uniform distribution
! inside the box and assign to each position a ranom normalized velocity
! which component are Vx and Vy """
rx, ry = rd.uniform(0,Box), rd.uniform(0,Box)
Dx, Dy =rd.random(), rd.random()
Dx = 2*(Dx - 0.5)
Dy = 2*(Dy - 0.5)
Vn = self.normalize(Dx,Dy)
Vx = Vn[0]*Vs_q
Vy = Vn[1]*Vs_q
return rx, ry, Vx, Vy
def PreyPosVel(self, Box, Rb, Mu, Sigma ):
"""! Create a prey inside the periodic box of legth Box at (Rx, Ry)
! The old position is computed around the zone of birth with radius Rb
! the prey has a Normal velocity with Mu Mean and Sigma standard deviation"""
Rx, Ry =rd.random(),rd.random()
Rx, Ry = Rx*Box+1,Ry*Box+1
Rx_old, Ry_old =rd.random(),rd.random()
Rx_old = Rx_old*Rb + Rx - (Rb/2)
Ry_old = Ry_old*Rb + Ry - (Rb/2)
Vx, Vy = self.Normal(Mu,Sigma)
return [Rx,Ry,Rx_old,Ry_old,Vx,Vy]
def AdjustPos(self, R1, R2, B):
B2 = B*0.5
if ( R1 > B2 ):
R1=R1-B
elif (R1 < -B2):
R1=R1+B
if ( R2 > B2 ):
R2=R2-B
elif (R2 < -B2):
R2=R2+B
return R1, R2
def GAUSSIAN_NOISE(self, Vx1,Vy1,Vs,Mu,Sigma):
Angle, Xres = self.Normal(Mu, Sigma)
Vx = Vx1*np.cos(Angle) - Vy1*np.sin(Angle)
Vy = Vx1*np.sin(Angle) + Vy1*np.cos(Angle)
Vn = np.sqrt(Vx**2 + Vy**2)
Vn = 1/Vn
Vx = Vx*Vn*Vs
Vy = Vy*Vn*Vs
return Vx, Vy
def CreateNewPred(self,Rx, Ry, Speed,Birth_Radius):
u1, u2, DX, DY = rd.random(),rd.random(),rd.random(),rd.random()
Rx1 = u1*Birth_Radius+ Rx-(Birth_Radius/2)
Ry1 = u2*Birth_Radius+ Ry-(Birth_Radius/2)
DX = 2*(DX - 0.5)
DY = 2*(DY - 0.5)
Vx1,Vy1 = self.normalize(DX, DY)
Vx1,Vy1 = Speed*Vx1,Speed*Vy1
return Rx1,Ry1,Vx1,Vy1
def GardeFou(self,Nb_Pred,MaxNb_Pred,Nb_Prey,MaxNb_Prey, Time_Step):
if ((Nb_Pred<=10) or (Nb_Pred>=(MaxNb_Pred-1))) :
print('The number of Predator is too large or too small')
print('Programme is stopped at time', Time_Step)
print('The number of Pred is',Nb_Pred)
exit()
if ((Nb_Prey<=10)or(Nb_Prey>=(MaxNb_Prey-1))):
print('The number of Prey is too large or too small')
print('Programme is stopped at time', Time_Step)
print('The number of Prey is',Nb_Prey)
exit()
def wrtObject(self,File_Name, Time_Step ):
File_NameTemp = File_Name+str(Time_Step+1)
f_object = open(File_NameTemp, "w")
return f_object
def gen_idy():
text =[0,1,2,3,4,5]
text[0] ="AZERTYUIOPQSDFGHJKLMWXCVBN"
text[1] = "azertyuiopqsdfghjklmwxcvbn"
text[2] ="132922799578491587290380706028034457626584559915698317458076141205606891525316911983139663491615228241121378304"
text[3] = "161730926992298808937186184655864453575832806478406599576092318269218083089908234280468459894595445540070540355238386428052606631732586480323016481331702297098820479338186895528168360391078014797449"
text[4] ="_%@-§?µ£~€¤ù"
text[5] = "AZERTYUIOPQSDFGHJKLMWXCVBN"
tirage = ""
for i in range(6):
#elt = rd.choice(liste)
tirage = tirage + rd.choice(text[i])
#print(i)
#liste.remove(elt)
#print (tirage)
return tirage
def print_idy(n):
out = open("gen_idy.txt","w")
i=0
while (i < n):
tirage = gen_idy()
i+=1
out.write(tirage+"\n")
class Variables(object):
def __init__(self, box):
self.box= box
class Pred(object):
def __init__(self, idy = "1x", pos = [0,1.0] , vel = [0.5, 0.5]):
self.idy = idy
self.pos = pos
self.vel = vel
def perform_repultion(self, idy, pos, vel):
pass
def init_pred(nb, box):
pred_list =[]
idy_list =[]
i=0
while (i < nb):
i+=1
rx,ry = rd.uniform(0,B),rd.uniform(0,B)
Dx, Dy =rd.random(),rd.random()
Dx = 2*(Dx - 0.5)
Dy = 2*(Dy - 0.5)
Vn = self.normalize(Dx,Dy)
Vx = Vn[0]*Vs_q
Vy = Vn[1]*Vs_q
cle = gen_idy()
idy_list = idy_list +[cle]
pred_list =pred_list + [Pred(idy = cle, pos = [rx,ry], vel = [Vx, Vy])]
#print("id =" , pred_list[0].idy , "pos=", pred_list[0].pos, "vel=", pred_list[0].vel)
return pred_list, idy_list
class Function(object):
def init_pred(self, nb, box):
pass
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
#-------------------------------------------------
#- (c) Yaya Youssouf
# Created : 28/01/2018
# Last update: 05/02/2018
#-------------------------------------------------
import os
import numpy
import math
import matplotlib.pyplot as pyplot
import matplotlib.animation as animation
import matplotlib.patches as patches
from pylab import genfromtxt
Box_Size = 125 # Length of box
Initial_Time = 1 #
Final_Time = 20 #Initial_Time and Final_Time are the integers part of the File_Name
Total_Time = Final_Time - Initial_Time
PreyFile_Name = 'PreyPosVel'
PredFile_Name = 'PredPosVel'
#pred_color, Prey_color ="red", "mediumblue"
#pred_color, Prey_color = "mediumblue","darkorange"
pred_color, Prey_color = "mediumblue","orangered"
class ParticlesMotion(object):
""" Animate Preyator-Prey Class
initial_state is 4*N lise [ [Rx, Ry, Vx, Vy] , ...] which contain
(X,Y) position of any particle an its corresponding velocity
coordinates (Vx,Vy), File_Name is the character part of the file name.
"""
def __init__(self,init_state = [],File_Name = ''):
# Set up the different parameters
self.init_state = init_state
self.state = self.init_state.copy()
self.time_elapsed = Initial_Time
self.File_Name = File_Name
def step(self, dt):
"""execute one time step of length dt and update state"""
self.time_elapsed += dt-Initial_Time # Temps passé
# update positions
self.state = genfromtxt(self.File_Name + str(dt))
# set up initial state and global variables
pred_init_state = genfromtxt(PredFile_Name + str(Initial_Time))
PredMotion = ParticlesMotion(pred_init_state, File_Name = PredFile_Name )
prey_init_state = genfromtxt(PreyFile_Name + str(Initial_Time))
PreyMotion = ParticlesMotion(prey_init_state, File_Name = PreyFile_Name)
#dt = 1./30
# set up figure and animation
fig = pyplot.figure()
fig.subplots_adjust(left=0, right=1, bottom=0.09, top=1)
ax = fig.add_subplot(111, aspect='equal',
xlim=(-5, Box_Size+5 ), ylim=(-5, Box_Size+8))
time_text = ax.text(0.02, 0.95, '', transform=ax.transAxes)
# particles holds the locations of the particles
Prey_particles, = ax.plot([], [], 'o', color = Prey_color, ms=2, markerfacecolor = None)
Pred_particles, = ax.plot([], [], 'p', color = pred_color, ms=4)
# rect is the box edge
rect = patches.Rectangle(
(0.0, 0.0),
Box_Size,
Box_Size,
fill = False , # Remove background
linewidth = 1)
ax.add_patch(rect)
def init():
"""initialize animation"""
global PreyMotion, PredMpotion, rect
time_text.set_text('')
Pred_particles.set_data([], [])
Prey_particles.set_data([], [])
rect.set_edgecolor('none')
return Pred_particles,Prey_particles, rect,time_text
def animate(i):
"""perform animation step"""
global PredMotion, PreyMotion, rect
PredMotion.step(i+ Initial_Time)
PreyMotion.step(i+ Initial_Time)
timer = PredMotion.time_elapsed
time_text.set_text('time = %d%s ' % (timer, ' secondes') )
# update pieces of the animation
rect.set_edgecolor('r')
Pred_particles.set_data(PredMotion.state[:, 0], PredMotion.state[:, 1])
Prey_particles.set_data(PreyMotion.state[:, 0], PreyMotion.state[:, 1])
return Pred_particles,Prey_particles, rect,time_text
ani = animation.FuncAnimation(fig, animate, frames= Total_Time,
interval=100, blit=True, init_func=init)
# save the animation as an mp4.
ani.save('PredPrey_Motion.mp4', fps=30, extra_args=['-vcodec', 'libx264'])
pyplot.show()
1. Run the "main.py" to create files of data containing posisitions and velocities of particles.
This programm uses the module "spp_modules.py" which should be in same files
2. Run "anim.py" to see video of dynamics.