-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sim2.2_Migr_assym_SingEx.R
executable file
·403 lines (341 loc) · 19 KB
/
Sim2.2_Migr_assym_SingEx.R
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# ---------------------------- #
# Simulation 2.2 for scenario
# ---------------------------- #
# Pesticide event: single exposure, on day 120
# asymmetric migration from an upstream patch
library(simecol)
# needs ajdustment to your path:
setwd("~/Desktop/Modelling_Twopatch/Latest_scripts/results")
### Parameters ###
# Carrying capacity
K <- 3000 # following Speirs 2000
# Recovery threshold
N_recov <- K * 0.9
# Initial population sizes
N_i = N_j <- K
# parameter for theta-logistic growth
theta = 1
# we only model logistic growth, but setting the parameter to different values
# allows to implement theta logistic growth, but see discussion in:
# Clark F., Brook B.W., Delean S., Reşit Akçakaya H. & Bradshaw C.J.A. (2010)
# The theta-logistic is unreliable for modelling most census data.
# Methods in Ecology and Evolution 1, 253–262.
# growth rates
m_linst <- -log(1/2)/149 # Mortality rate for water phase
l_earlinst <- 40
r_earlinst <- -(log((1/0.9^theta-1)*(0.5*K)^theta/(K^theta-(0.5*K)^theta))/(l_earlinst*theta))
# Immigration parameters (assymetric migration) (from Bergey & Wand 1989 and Mueller 1982)
# proposed "colonisation cycle" from Mueller:
eps_linst_up = 0.08 # mobility of late instar larvae upstream
eps_linst_down = 0.32 # mobility of late instar larvae downstream (ca. 80% of total linst migration 0.4)
# higher mobility of adults:
eps_adult_up = 0.48 # mobility of adults upstream (ca. 80% of total adult migration 0.6)
eps_adult_down = 0.12 # mobility of adults downstream (ca. 20% of total adult migration 0.6)
d_linst <- 0.7 # 1-d = migration mortality, i.e. 0.3 for late instar larvae and 0.2 for adults
d_adult <- 0.7
s = 2 # # densitiy dependent migration shape parameter
### Pesticide Parameters ###
day_pesticide = 120 # day where the pesticide enters the water column
# Reduction of parameters due to pesticide event
f_mort <- c(1, 0.75,0.5, 0.25) # reduces population size of prey (Ni, Nj)
f_emerg <- c(1, 0.75,0.5, 0.25) # reduces emergence rate
f_fecundity <- c(1, 0.75,0.5, 0.25) # reduces growth rate (r2) in the adult/egg phase
# ----------------------------------------------------------------------------------- #
# years
sim_years <- 10 # simulation years
i = seq(0,360*sim_years,by = 360)+1 # each start day of the year (e.g. 1, 361 ...)
#Matrix for population sizes at the beginning of the year
N_year <- matrix(NA, nrow=sim_years+1, ncol=2, dimnames = list(c(1:(sim_years+1)),c("Ni", "Nj")))
N_year[1,] <- c(N_i,N_j)
#Matrix for the daily population sizes, i.a. for plotting
N_daily <- matrix(NA, nrow=sim_years*360, ncol=3)
N_daily[1,] <- c(as.integer(1), N_i,N_j)
N_daily[,1] <- 1:(sim_years*360)
### Simulate first year with pesticide event and no migration
d <- NULL
for (N in f_mort){
for (E in f_emerg){
for (G in f_fecundity){
# ---------------------------------------------------- #
# Single exposure in the first year of the simulation:
# ---------------------------------------------------- #
# Day 1 to day_pesticide exponential decrease of aquatic late instars (see paper)
linst1_x <- new("odeModel",
main = function (time, init, parms) {
with(as.list(c(init, parms)), {
# Patch I (polluted)
dNi <- -r1 * Ni +
r2 * Ni * (1 - (Ni / K) ^ theta) -
eps_up * (Ni / K) ^ s * Ni +
d * eps_down * (Nj / K) ^ s * Nj
# Patch J (upstream)
dNj <- -r1 * Nj +
r2 * Nj * (1 - (Nj / K) ^ theta) -
eps_down * (Nj / K) ^ s * Nj +
d * eps_up * (Ni / K) ^ s * Ni
list(c(dNi,dNj))
})
},
parms = c(r1 = m_linst, K = K,d = d_linst, eps_up = eps_linst_up,
eps_down = eps_linst_down,r2 = 0, s = s, theta = theta),
times = c(from = 1, to = day_pesticide, by = 1),
init = c(Ni = N_year[1,1],Nj = N_year[1,2]),
solver = "lsoda"
)
# Call ODE model
res_linst1_x <- sim(linst1_x)
# Store results as matrix
res_linst1_x_mat <-as.matrix(out(res_linst1_x))
# Day day_pesticide to 150 Pesticide exposure & decrease of aquatic late instars
linstx_150 <- new("odeModel",
main = function (time, init, parms) {
with(as.list(c(init, parms)), {
# Patch I (polluted)
dNi <- -r1 * Ni +
r2 * Ni * (1 - (Ni / K) ^ theta) -
eps_up * (Ni / K) ^ s * Ni +
d * eps_down * (Nj / K) ^ s * Nj
# Patch J (upstream)
dNj <- -r1 * Nj +
r2 * Nj * (1 - (Nj / K) ^ theta) -
eps_down * (Nj / K) ^ s * Nj +
d * eps_up * (Ni / K) ^ s * Ni
list(c(dNi,dNj))
})
},
parms = c(r1 = m_linst, K = K,d = d_linst, eps_up = eps_linst_up,
eps_down = eps_linst_down, r2 = 0, s = s, theta = theta),
times = c(from = day_pesticide, to = 150, by = 1),
# Pesticide exposure (f_mort), only Patch I (polluted) effected
init = c(Ni = N*as.vector(res_linst1_x_mat[nrow(res_linst1_x_mat),2]),
Nj = as.vector(res_linst1_x_mat[nrow(res_linst1_x_mat),3])),
solver = "lsoda"
)
# Call ODE model
res_linstx_150 <- sim(linstx_150)
# Store results as matrix
res_linstx_150_mat <-as.matrix(out(res_linstx_150))
# Day 151-190 Adults emerge
adult151_190 <- new("odeModel",
main = function (time, init, parms) {
with(as.list(c(init, parms)), {
# Patch I (polluted)
dNi <- -r1 * Ni +
r2 * Ni * (1 - (Ni / K) ^ theta) -
eps_up * (Ni / K) ^ s * Ni +
d * eps_down * (Nj / K) ^ s * Nj
# Patch J (upstream)
dNj <- -r1 * Nj +
r2 * Nj * (1 - (Nj / K) ^ theta) -
eps_down * (Nj / K) ^ s * Nj +
d * eps_up * (Ni / K) ^ s * Ni
list(c(dNi,dNj))
})
},
parms = c(r1 = 0, K = K,d = d_adult, eps_up = eps_adult_up,
eps_down = eps_adult_down, r2 = 0, s = s, theta= theta),
times = c(from = 151, to = 190, by = 1),
# hatching success: mean(c(0.7,0.9)) (Huryn & Wallace 2000)
# emergence reduced by f_emerg, only Patch I (polluted) effected
init = c(Ni = E * as.vector(res_linstx_150_mat[nrow(res_linstx_150_mat),2]),
Nj = as.vector(res_linstx_150_mat[nrow(res_linstx_150_mat),3])),
solver = "lsoda"
)
# Call ODE model
res_adult151_190 <- sim(adult151_190)
# Store results as matrix
res_adult151_190_mat <-as.matrix(res_adult151_190@out)
# Day 191-360 Oviposition and growing of the population
earlinst191_360 <- new("odeModel",
main = function (time, init, parms) {
with(as.list(c(init, parms)), {
# Patch I (polluted)
# Pesticide event - reduction in r2: G * r2
dNi <- -r1 * Ni +
G * r2 * Ni * (1 - (Ni / K) ^ theta) -
eps * (Ni / K) ^ s * Ni +
d * eps * (Nj / K) ^ s * Nj
# Patch J (upstream) (no pesticide)
dNj <- -r1 * Nj +
r2 * Nj * (1 - (Nj / K) ^ theta) -
eps * (Nj / K) ^ s * Nj +
d * eps * (Ni / K) ^ s * Ni
list(c(dNi,dNj))
})
},
parms = c(r1 = 0, K = K,d = d_linst, eps = 0,
r2 = r_earlinst, s = s, theta = theta),
times = c(from = 190, to = 360, by = 1),
init = c(Ni = as.vector(res_adult151_190_mat[nrow(res_adult151_190_mat),2]),
Nj = as.vector(res_adult151_190_mat[nrow(res_adult151_190_mat),3])),
solver = "lsoda"
)
# Call ODE model
res_earlinst191_360 <- sim(earlinst191_360)
# Store results as matrix
res_earlinst191_360_mat <-as.matrix(res_earlinst191_360@out)
# store results of the whole year in N_daily-matrix for plotting
N_daily[i[1]:(i-1)[2],2:3] <- rbind(res_linst1_x_mat[,-1],
res_linstx_150_mat[-1,-1],
res_adult151_190_mat[,-1],
res_earlinst191_360_mat[-1,-1])
#Storing population size in N for following year
N_year[2,1] <- res_earlinst191_360_mat[nrow(res_earlinst191_360_mat),2]
N_year[2,2] <- res_earlinst191_360_mat[nrow(res_earlinst191_360_mat),3]
# --------------------------------------------------- #
# Rest of the simulation period: no further exposure
# --------------------------------------------------- #
for(j in 2:sim_years) {
# Day 1 to 150 exponential decrease of aquatic late instars (see paper)
linst1_150 <- new("odeModel",
main = function (time, init, parms) {
with(as.list(c(init, parms)), {
# Patch I (polluted)
dNi <- -r1 * Ni +
r2 * Ni * (1 - (Ni / K) ^ theta) -
eps_up * (Ni / K) ^ s * Ni +
d * eps_down * (Nj / K) ^ s * Nj
# Patch J (upstream)
dNj <- -r1 * Nj +
r2 * Nj * (1 - (Nj / K) ^ theta) -
eps_down * (Nj / K) ^ s * Nj +
d * eps_up * (Ni / K) ^ s * Ni
list(c(dNi,dNj))
})
},
parms = c(r1 = m_linst, K = K,d = d_linst, eps_up = eps_linst_up,
eps_down = eps_linst_down,r2 = 0, s = s, theta = theta),
times = c(from = 1, to = 150, by = 1),
init = c(Ni = N_year[j,1],Nj = N_year[j,2]),
solver = "lsoda"
)
# Call ODE model
res_linst1_150 <- sim(linst1_150)
# Store results as matrix
res_linst1_150_mat <-as.matrix(out(res_linst1_150))
# Day 151-190 Adults emerge
adult151_190 <- new("odeModel",
main = function (time, init, parms) {
with(as.list(c(init, parms)), {
# Patch I (polluted)
dNi <- -r1 * Ni +
r2 * Ni * (1 - (Ni / K) ^ theta) -
eps_up * (Ni / K) ^ s * Ni +
d * eps_down * (Nj / K) ^ s * Nj
# Patch J (upstream)
dNj <- -r1 * Nj +
r2 * Nj * (1 - (Nj / K) ^ theta) -
eps_down * (Nj / K) ^ s * Nj +
d * eps_up * (Ni / K) ^ s * Ni
list(c(dNi,dNj))
})
},
parms = c(r1 = 0, K = K,d = d_adult, eps_up = eps_adult_up,
eps_down = eps_adult_down, r2 = 0, s = s, theta= theta),
times = c(from = 151, to = 190, by = 1),
init = c(Ni = 0.8*as.vector(res_linst1_150_mat[nrow(res_linst1_150_mat),2]),
Nj = 0.8*as.vector(res_linst1_150_mat[nrow(res_linst1_150_mat),3])),
solver = "lsoda"
)
# Call ODE model
res_adult151_190 <- sim(adult151_190)
# Store results as matrix
res_adult151_190_mat <-as.matrix(res_adult151_190@out)
# Day 191-360 Egg laying, growing of the population till carrying capacity is reached
earlinst191_360 <- new("odeModel",
main = function (time, init, parms) {
with(as.list(c(init, parms)), {
# Patch I (polluted)
dNi <- -r1 * Ni +
r2 * Ni * (1 - (Ni / K) ^ theta) -
eps * (Ni / K) ^ s * Ni +
d * eps * (Nj / K) ^ s * Nj
# Patch J (upstream)
dNj <- -r1 * Nj +
r2 * Nj * (1 - (Nj / K) ^ theta) -
eps * (Nj / K) ^ s * Nj +
d * eps * (Ni / K) ^ s * Ni
list(c(dNi,dNj))
})
},
parms = c(r1 = 0, K = K,d = d_linst, eps = 0,
r2 = r_earlinst, s = s, theta = theta),
times = c(from = 190, to = 360, by = 1),
init = c(Ni = as.vector(res_adult151_190_mat[nrow(res_adult151_190_mat),2]),
Nj = as.vector(res_adult151_190_mat[nrow(res_adult151_190_mat),3])),
solver = "lsoda"
)
# Call ODE model
res_earlinst191_360 <- sim(earlinst191_360)
# Store results as matrix
res_earlinst191_360_mat <-as.matrix(res_earlinst191_360@out)
# store results of the whole year in N_daily-matrix for plotting
N_daily[i[j]:(i-1)[j+1],2:3] <- rbind(res_linst1_150_mat[,-1],
res_adult151_190_mat[,-1],
res_earlinst191_360_mat[-1,-1])
#Storing population size in N for following year
N_year[j+1,1] <- res_earlinst191_360_mat[nrow(res_earlinst191_360_mat),2]
N_year[j+1,2] <- res_earlinst191_360_mat[nrow(res_earlinst191_360_mat),3]
} # end of time loop
# store output
d <- rbind(d, cbind(as.data.frame(N_daily), N, E, G))
}
}
}
names(d) <- c("Day", "N_i", "N_j", "f_mort", "f_emerg", "f_fecundity")
d$treat <- as.factor(paste(d$f_mort,d$f_emerg, d$f_fecundity, sep="_"))
# compute minimum of Nj
min_nj <- do.call("rbind", (as.list(by(d[,3], factor(d$treat), min))))
### compute minimum for Ni
v_min <- do.call("rbind", (as.list(by(d[,2], factor(d$treat), min))))
### Calculate recovery times
# first value below threshold of K * 0.9
v1 <- do.call("rbind", (as.list(by(d[,2], factor(d$treat), function(x) which(x < N_recov)[1]))))
# time until the threshold is reached again = recovery time
rec_time <- data.frame(matrix(data = NA, ncol=2, nrow=length(v1)))
names(rec_time) <- c("Rec_time", "treat")
temp_rec <- NULL
for(I in 1:length(levels(d$treat))) {
# extract data
temp_rec <- d[d$treat == levels(d$treat)[I],2]
rec_time[I,1] <- which(temp_rec[v1[I]:length(temp_rec)] >= N_recov)[1]
rec_time[I,2] <- levels(d$treat)[I]
}
# merge results
v2 <- data.frame(v1, row.names=NULL)
v2$treat <- rownames(v1)
min_nj2 <- data.frame(min_nj, row.names=NULL)
min_nj2$treat <- rownames(min_nj)
v3 <- data.frame(v_min, row.names=NULL)
v3$treat <- rownames(v_min)
diff_temp_a <- merge(rec_time, v2)
diff_temp_b <- merge(diff_temp_a, min_nj2)
diff_temp <- merge(diff_temp_b, v3)
# for comparison of different scenarios with different baseline recovery times
# calculation of difference to baseline scenario for recovery and minimum:
diff_temp$diff = abs(diff_temp[diff_temp$treat=="1_1_1", "Rec_time"] - diff_temp[ , "Rec_time"])
diff_temp$diff_min = round(abs(diff_temp[diff_temp$treat=="1_1_1", "min_nj"] - diff_temp[ , "min_nj"]))
# add individual factors
fac1 <- unique(d[ ,4:6])
fac1$treat <- as.factor(paste(fac1$f_mort,fac1$f_emerg, fac1$f_fecundity, sep="_"))
final <- merge(diff_temp, fac1)
# calculation for upstream patch
# dir.create(paste0(getwd(),"/figures"))
# Plot Ni (Patch I (polluted)) and Nj
for(I in 1:length(levels(d$treat)))
{
png(filename = file.path(getwd(), paste0("figures/", "Sim2_SingleEvent_asymmMigr", levels(d$treat)[I], ".png")))
plot(d[d$treat == levels(d$treat)[I],1], d[d$treat == levels(d$treat)[I],2], xlab = "Time [d]", ylab = "Population size",
type = "l", lty = 2, lwd = 2, ylim = c(0,7000))
lines(d[d$treat == levels(d$treat)[I],1], d[d$treat == levels(d$treat)[I],3], type = "l", lty = 1, lwd = 2)
legend(20,6980, c("Ni"),lty = 2, lwd=2)
legend(20,5900, c("Nj"), lty=1, lwd=2)
text(c(2200,2200,3200,3200,3200), c(6980,5900,6980,6480,5900), labels=c(
paste0(eps_linst_up," / ",eps_linst_down),paste0(eps_adult_up," / ",eps_adult_down), unique(d[d$treat == levels(d$treat)[I],"f_mort"]), unique(d[d$treat == levels(d$treat)[I],"f_emerg"]), unique(d[d$treat == levels(d$treat)[I],"f_fecundity"])), cex = 0.9)
text(c(1700,1700,2800,2800,2800), c(6980,5900,6980,6480,5900), c("eps_linst = ", "eps_adult = ","f_mort =","f_emerg =","f_fecundity ="), cex = 0.9)
dev.off()
}
# Export daily population size
write.csv(final, file= paste0(getwd(),"/Sim2_asymmMigr_SingEx_rec_time_new.csv"), row.names=F)
# Save recovery times
write.csv(d, file = paste0(getwd(),"/Z_Sim2_asymmMigr_SingEx_daily_Ns.csv"))