-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_aux.py
184 lines (136 loc) · 3.88 KB
/
model_aux.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from numpy import *
from scipy import *
from numpy.random import random_sample
from scipy.integrate import odeint
def gaus(p,po,sig):
return exp(-(p-po)**2/2./sig**2)
def Ma(P,t,po,pc,cp,K):
kgrowth=10
kdeath=10
return list(r_[P[:-1]*(1-P[:-1])*(heaviside(-abs(P[-1]-po)+pc,0.5)*(kgrowth+kdeath)-kdeath),
dot(K*cp,P[:-1])])
def Ma_gauss(P,t,po,pc,cp,K):
kgrowth=10
kdeath=10
return list(r_[P[:-1]*(1-P[:-1])*(gaus(P[-1],po,pc)*(kgrowth+kdeath)-kdeath),
dot(K*cp,P[:-1])])
def simul_gauss(NrSp,interaction_strength):
#parameters
K=1e10
#start values
P0=[10**-2]*NrSp
P0.append(7)
P0=array(P0).astype('float')
#interaction and reaction parameters
po=random_sample(size=NrSp)*5.+4.5
pc=(array([2.5]*NrSp)).astype('float')
cp=2*(random_sample(size=NrSp)-0.5)*interaction_strength
t=linspace(0,1,10000)
MA_all=[]
start=copy(P0)
MA_final=[start]
for i in range(80):
MA = odeint(Ma_gauss, P0, t, args=(po,pc,cp,K))
MA_all.extend(MA)
MA_final.append(MA[-1])
P0[:-1]=MA[-1,:-1]/10. # dilute
P0[P0<10**(-9)]=0
P0[-1]=0.9*7+0.1*MA[-1,-1]
return array(MA_all)
def simul(NrSp,interaction_strength):
#parameters
K=1e10
#start values
P0=[10**-2]*NrSp
P0.append(7)
P0=array(P0).astype('float')
#interaction and reaction parameters
po=random_sample(size=NrSp)*5.+4.5
pc=(array([2.5]*NrSp)).astype('float')
cp=2*(random_sample(size=NrSp)-0.5)*interaction_strength
t=linspace(0,1,10000)
MA_all=[]
start=copy(P0)
MA_final=[start]
for i in range(80):
MA = odeint(Ma, P0, t, args=(po,pc,cp,K))
MA_all.extend(MA)
MA_final.append(MA[-1])
P0[:-1]=MA[-1,:-1]/10. # dilute
P0[P0<10**(-9)]=0
P0[-1]=0.9*7+0.1*MA[-1,-1]
return array(MA_all)
def simulbis(P00,po,pc,cp):
#parameters
K=1e10
NrSp=len(P00)
P0=hstack([P00,7])
t=linspace(0,1,100000)
MA_all=[]
start=copy(P0)
MA_final=[start]
for i in range(80):
MA = odeint(Ma, P0, t, args=(po,pc,cp,K))
#MA_all.extend(MA)
#MA_final.append(MA[-1])
P0[:-1]=MA[-1,:-1]/10. # dilute
#P0[P0<0.1]=0
P0[-1]=0.9*7+0.1*MA[-1,-1]
return array(MA[-1,:-1])
def simulbis2(P00,po,pc,cp):
#parameters
K=1e10
NrSp=len(P00)
P0=hstack([P00,7])
t=linspace(0,1,100000)
MA_all=[]
start=copy(P0)
MA_final=[start]
daily_fin=[]
for i in range(80):
MA = odeint(Ma, P0, t, args=(po,pc,cp,K))
#MA_all.extend(MA)
#MA_final.append(MA[-1])
P0[:-1]=MA[-1,:-1]/10. # dilute
#P0[P0<0.1]=0
P0[-1]=0.9*7+0.1*MA[-1,-1]
daily_fin.append(MA[-1,:-1])
return array(daily_fin)
def simulbisc(interaction_strength,P00,po,pc,cp):
#parameters
K=1e10
NrSp=len(P00)
P0=hstack([P00,7])
t=linspace(0,1,10000)
MA_all=[]
start=copy(P0)
MA_final=[start]
daily_fin=[]
for i in range(80):
MA = odeint(Ma, P0, t, args=(po,pc,cp,K))
P0[:-1]=MA[-1,:-1]/5. # dilute
#P0[P0<0.1]=0
P0[-1]=0.8*7+0.2*MA[-1,-1]
daily_fin.append(MA[-1,:-1])
return array(daily_fin)
def diversity(x):
#x[0] and x[1] are fractions of species 1 and 2
# if both species are present
if all(x>0):
return exp(-sum(x*log(x)))
# if one species dominates
if any(x!=0):
return 1 #exp(0)
if all(x==0):
return 0
def diversity_com(x):
# if at least two species are present
if sum(x!=0)>1:
## take care here to not take log of 0 !!
return exp(-sum(x[x>0]*log(x[x>0])))
# if one species dominates
if sum(x!=0)==1:
return 1
#no species survives
if sum(x!=0)==0:
return 0