Commit d4833519 authored by Inge van der Knijff's avatar Inge van der Knijff

Initializing conditions, implemented rule1

parent ea6300c5
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import execute as ex"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"N = 10\n",
"r_r= 1\n",
"dr_o= 12\n",
"dr_a = 6 \n",
"alpha = 250\n",
"theta = 50\n",
"tau = 0.1\n",
"s = 2"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"d = ex.execute(N,r_r,dr_o,dr_a,s)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
import numpy as np
def del_diag(A):
"""This function removes the diagnoal of a matrix A and moves all other element vertically to obtain the new matrix."""
A=np.transpose(np.transpose(A)[~np.eye(A.shape[0],dtype=bool)].reshape((A.shape[0]),A.shape[1]-1))
return A
import initialize as init
import model as md
def execute(N,r_r,dr_o,dr_a,s):
[c,v] = init.init(N,r_r,dr_o,dr_a,s)
[dr, dc] = md.r_ij(N,r_r,dr_o,dr_a,c)
[d, rr] = md.rules(N,dr,r_r,dr_o,dr_a,dc)
return d
\ No newline at end of file
import numpy as np
def init(N,r_r,dr_o,dr_a,s):
"""This function generates the initial positions and initial constant velocity."""
r_zone = r_r+dr_o+dr_a
c = 3*r_zone*np.random.rand(N,3) #positions
#c = r_r*np.random.rand(N,3)
v = np.random.rand(N,3)
v[:,0] = s*v[:,0]/(np.sqrt(v[:,0]**2+v[:,1]**2+v[:,2]**2)) #This should be improved, but I don't know a better way to get a constant velocity.
v[:,1] = s*v[:,1]/(np.sqrt(v[:,0]**2+v[:,1]**2+v[:,2]**2))
v[:,2] = s*v[:,2]/(np.sqrt(v[:,0]**2+v[:,1]**2+v[:,2]**2))
return c, v
\ No newline at end of file
import numpy as np
import del_diag as dd
def r_ij(N,r_r,dr_o,dr_a,c):
"""This function return to matrices, dc and dr. dc is a (N-1,N,3) matrix in which every column corresponds to 1 bird and gives the relative distances to all other birds, in x,y,z. dr is a (N-1,N) matrix in which every column corresponds to a bird and gives the relative radii to all other birds. """
dc1 = np.zeros([N,N,3])
for i in range(N):
dc1[:,i,:]=c[i,:]-c
dc = np.zeros([N-1,N,3])
for i in range(3):
dc[:,:,i]=dd.del_diag(dc1[:,:,i])
dr = np.sqrt(dc[:,:,0]**2+dc[:,:,1]**2+dc[:,:,2]**2)
return dr,dc
def rules(N,dr,r_r,r_o,r_a,dc):
"""This function returns the new desired direction d for every bird in a (N,3) matrix."""
d = np.zeros([N,3])
#rule 1
for i in range(N):
rr = dr[:,i]<r_r
d[i,0] = -np.dot(dc[:,i,0]/dr[:,i],rr)
d[i,1] = -np.dot(dc[:,i,1]/dr[:,i],rr)
d[i,2] = -np.dot(dc[:,i,2]/dr[:,i],rr)
return d,rr
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment