-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEcosystemEngineering.R
More file actions
433 lines (341 loc) · 16.3 KB
/
EcosystemEngineering.R
File metadata and controls
433 lines (341 loc) · 16.3 KB
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#Ecosystem Engineering
#This code generates the figures from the manuscript
rm(list = ls())
library(Rcpp)
library(colorRamps) #For colorful plots
library(parallel)
library(lattice)
library(viridis)
library(gridGraphics)
grab_grob <- function(){
grid.echo()
grid.grab()
}
setwd()
sourceCpp("spartina.cpp")
#The C++ script depends on several proprietary header files created by Numerical
#Recipes 3, which I have not included. If you have access to these, you can
#include them to compile the program. Otherwise you can use your own favorite
#Romberg integration routine, or any other numerical quadrature you prefer.
source("EcosystemEngineeringFunctions.R")
################################################################################
# EXAMPLES
################################################################################
DataA <- runModel(subintervals = 3000, #Need 2000 for accuracy
timesteps = 50,
d = 0.1, #deterioration, erosion
m = 0.1, #habitat modification, engineering
l = 2.1, #effect of nearby individuals on engineering ability
mu = 0.0, #value of optimal habitat quality
sigma = 0.5, #std dev about that optimal habitat quality
rMax = 2.0, #maximum growth rate (at optimal quality)
b = 0.4, #dispersal ability of propagules 0.4
c = 0, #rate of sea level rise
initialHabitatQuality = 2,
doEngineer = 0,
doErosion = 1,
doRiseSeaLevel =0,
doGrow = 0)
DataB <- runModel(subintervals = 3000, #Need 2000 for accuracy
timesteps = 50,
d = 0.1, #deterioration, erosion
m = 0.1, #habitat modification, engineering
l = 2.1, #effect of nearby individuals on engineering ability
mu = 0.0, #value of optimal habitat quality
sigma = 0.5, #std dev about that optimal habitat quality
rMax = 2.0, #maximum growth rate (at optimal quality)
b = 0.4, #dispersal ability of propagules 0.4
c = 0, #rate of sea level rise
initialHabitatQuality = 2,
doEngineer = 1,
doErosion = 0,
doRiseSeaLevel =0,
doGrow = 0)
DataC <- runModel(subintervals = 3000, #Need 2000 for accuracy
timesteps = 30,
d = 0.0, #deterioration, erosion
m = 0.0, #habitat modification, engineering
l = 2.1, #effect of nearby individuals on engineering ability
mu = 0.0, #value of optimal habitat quality
sigma = 0.5, #std dev about that optimal habitat quality
rMax = 2.0, #maximum growth rate (at optimal quality)
b = 0.4, #dispersal ability of propagules 0.4
c = 0.1, #rate of sea level rise
initialHabitatQuality = 2,
doEngineer = 1,
doErosion = 1,
doRiseSeaLevel =1,
doGrow = 0)
DataD <- runModel(subintervals = 3000, #Need 2000 for accuracy
timesteps = 30,
d = 0.1, #deterioration, erosion
m = 0.1, #habitat modification, engineering
l = 2.1, #effect of nearby individuals on engineering ability
mu = 0.0, #value of optimal habitat quality
sigma = 0.5, #std dev about that optimal habitat quality
rMax = 2.0, #maximum growth rate (at optimal quality)
b = 0.4, #dispersal ability of propagules 0.4
c = 0, #rate of sea level rise
initialHabitatQuality = 2,
doEngineer = 0,
doErosion = 0,
doRiseSeaLevel =0,
doGrow = 1)
par(cex.axis=1.5)
par(cex.lab=2.5)
par(oma=c(0,2,0,0)) #outer margins (bottom, left, top, right)
par(mar=c(5.1, 5.0, 4.1, 2.1)) #margins (5.1, 4.1, 4.1, 2.1)
makePlot(DataA, byGeneration = 3, "")
dev.copy(pdf, 'Fig 1A.pdf', width=9, height=6);dev.off()
makePlot(DataB, byGeneration = 3, "")
dev.copy(pdf, 'Fig 1B.pdf', width=9, height=6);dev.off()
makePlot(DataC, byGeneration = 3, "")
dev.copy(pdf, 'Fig 1C.pdf', width=9, height=6);dev.off()
makePlotO(DataD, byGeneration = 3, "")
dev.copy(pdf, 'Fig 1D.pdf', width=9, height=6);dev.off()
plotTimestepO(DataA, tmstp = 0, "Growth")
plotTimestep(DataA, tmstp = 7, "Erosion")
#Appendix
plotTimestepAppendix(DataA, tmstp = 7, "")
################################################################################
#FIGURE 2
################################################################################
#FIGURE 2A
engineerRates <- c(3.0, 6.0, 0.0)
fig1ListLow <- mclapply(engineerRates, iterate,
subintervals = 3000,
timesteps = 100,
d = 0.2, #deterioration, erosion
l = 0.1, #effect of nearby individuals on engineering ability
mu = 0.0, #value of optimal habitat quality
sigma = 0.3, #std dev about that optimal habitat quality
rMax = 3.0, #maximum growth rate (at optimal quality)
b = 0.4, #dispersal ability of propagules 0.4
c = 0, #rate of sea level rise
initialHabitatQuality = 0, #Initially suboptimal
doEngineer = 1,
doErosion = 1,
doRiseSeaLevel =0,
doGrow = 1)
fig1MatLow <- matrix(unlist(fig1ListLow), ncol=length(engineerRates), nrow=101)
dev.off()
x11(width=4, height=5)
times <- 0:100
par(bg=NA, las=1)
plot(times, fig1MatLow[,1], type='l', xlab="", ylab="area occupied",
ylim=c(0.0,1.0), lwd=1.5, cex.axis=0.8)
title(xlab = "year", mgp = c(2, 1, 0))
lines(times, fig1MatLow[,2], lty=3, lwd=1.5)
lines(times, fig1MatLow[,3], lty=5, lwd=1.5)
legend(60, 0.85,c("m = 0.0", "m = 3.0", "m = 6.0"),
lty=c(5, 1, 3), cex=0.8, y.intersp=1.5)
plot1 <- grab_grob()
#FIGURE 2C
dvalues <- seq(0, 0.5, 0.025)
mvalues <- seq(0, 10.0, 0.5)
fig2List <- mcmapply(iterate, rep(dvalues,each=length(mvalues)), mvalues,
subintervals = 3000,
timesteps = 100,
l = 0.1, #effect of nearby individuals on engineering ability
mu = 0.0, #value of optimal habitat quality
sigma = 0.3, #std dev about that optimal habitat quality
rMax = 3.0, #maximum growth rate (at optimal quality)
b = 0.4, #dispersal ability of propagules 0.4
c = 0, #rate of sea level rise
initialHabitatQuality = 0, #Initially suboptimal
doEngineer = 1,
doErosion = 1,
doRiseSeaLevel =0,
doGrow = 1)
fig2ListLow <- readRDS("fig2List_lowL.rds")
fig2MatLow <- readRDS("fig2Mat_lowL.rds")
xvalues <- mvalues
yvalues <- dvalues
grid <- expand.grid(x=xvalues, y=yvalues)
grid$z <- fig2MatLow[40,]
plot3 <- levelplot(z~x*y, grid, xlab="engineering (m)",
ylab="erosion (d)", col.regions = viridis(100))
#FIGURE 2B
engineerRates <- c(0.22, 0.4, 0.0)
fig1ListHigh <- mclapply(engineerRates, iterate,
subintervals = 3000,
timesteps = 100,
d = 0.2, #deterioration, erosion
l = 2.1, #effect of nearby individuals on engineering ability
mu = 0.0, #value of optimal habitat quality
sigma = 0.3, #std dev about that optimal habitat quality
rMax = 3.0, #maximum growth rate (at optimal quality)
b = 0.4, #dispersal ability of propagules 0.4
c = 0, #rate of sea level rise
initialHabitatQuality = 0, #Initially suboptimal
doEngineer = 1,
doErosion = 1,
doRiseSeaLevel =0,
doGrow = 1)
fig1MatHigh <- matrix(unlist(fig1ListHigh), ncol=length(engineerRates), nrow=101)
dev.off()
x11(width=4, height=5)
times <- 0:100
par(bg=NA, las=1)
plot(times, fig1MatHigh[,1], type='l', xlab="", ylab="area occupied",
lwd=1.5, ylim=c(0.0, 1.0), cex.axis=0.8)
title(xlab = "year", mgp = c(2, 1, 0))
lines(times, fig1MatHigh[,2], lty=3, lwd=1.5)
lines(times, fig1MatHigh[,3], lty=5, lwd=1.5)
legend(60, 0.85,c("m = 0.00", "m = 0.22", "m = 0.40"),
lty=c(5, 1, 3), cex=0.8, y.intersp=1.5)
plot2 <- grab_grob()
#FIGURE 2D
dvalues <- seq(0, 0.4, 0.025)
mvalues <- seq(0, 1.0, 0.05)
fig2ListHigh <- mcmapply(iterate, rep(dvalues,each=length(mvalues)), mvalues,
subintervals = 3000,
timesteps = 100,
l = 2.1, #effect of nearby individuals on engineering ability
mu = 0.0, #value of optimal habitat quality
sigma = 0.3, #std dev about that optimal habitat quality
rMax = 3.0, #maximum growth rate (at optimal quality)
b = 0.4, #dispersal ability of propagules 0.4
c = 0, #rate of sea level rise
initialHabitatQuality = 0, #Initially suboptimal
doEngineer = 1,
doErosion = 1,
doRiseSeaLevel =0,
doGrow = 1)
fig2ListHigh <- readRDS("fig2List_highL.rds")
fig2MatHigh <- readRDS("fig2Mat_highL.rds")
xvalues <- mvalues
yvalues <- dvalues
grid <- expand.grid(x=xvalues, y=yvalues)
grid$z <- fig2MatHigh[40,]
plot4 <- levelplot(z~x*y, grid, xlab="engineering (m)",
ylab="erosion (d)", col.regions = viridis(100))
library(gridExtra)
grid.arrange(plot1,plot2,plot3,plot4, ncol=2)
grid.text(c("(a)","(b)", "(c)", "(d)"), x=c(0.05, 0.55, 0.05, 0.55), y=c(0.95, 0.95, 0.47, 0.47), vjust=1, hjust=0, gp=gpar(fontface=1))
################################################################################
#FIGURE 3
################################################################################
# FIGURE 3A
engineerRates <- c(0.22, 0.4, 0.0)
fig3aList <- mclapply(engineerRates, iterate,
subintervals = 3000,
timesteps = 100,
d = 0.2, #deterioration, erosion
l = 2.1, #effect of nearby individuals on engineering ability
mu = 0.0, #value of optimal habitat quality
sigma = 0.3, #std dev about that optimal habitat quality
rMax = 3.0, #maximum growth rate (at optimal quality)
b = 0.4, #dispersal ability of propagules 0.4
c = 0.1, #rate of sea level rise
initialHabitatQuality = 0, #Initially suboptimal
doEngineer = 1,
doErosion = 1,
doRiseSeaLevel =1,
doGrow = 1)
fig3aMat <- matrix(unlist(fig3aList), ncol=length(engineerRates), nrow=101)
dev.off()
x11(width=4, height=5)
times <- 0:100
par(bg=NA, las=1)
plot(times, fig3aMat[,1], type='l', xlab="", ylab="area occupied",
lwd=1.5, ylim=c(0.0, 1.0), cex.axis=0.8)
title(xlab = "year", mgp = c(2, 1, 0))
lines(times, fig3aMat[,2], lty=3, lwd=1.5)
lines(times, fig3aMat[,3], lty=5, lwd=1.5)
legend(60, 0.85,c("m = 0.00", "m = 0.22", "m = 0.40"),
lty=c(5, 1, 3), cex=0.8, y.intersp=1.5)
plot3a <- grab_grob()
#FIGURE 3B
cvalues <- seq(0, 0.4, 0.025)
mvalues <- seq(0, 1.0, 0.05)
fig3List <- mcmapply(iterate, rep(mvalues,each=length(cvalues)), cvalues,
subintervals = 3000,
timesteps = 50,
d = 0.2,
l = 2.1, #effect of nearby individuals on engineering ability
mu = 0.0, #value of optimal habitat quality
sigma = 0.3, #std dev about that optimal habitat quality
rMax = 3.0, #maximum growth rate (at optimal quality)
b = 0.4, #dispersal ability of propagules 0.4
initialHabitatQuality = 0, #Initially suboptimal
doEngineer = 1,
doErosion = 1,
doRiseSeaLevel =1,
doGrow = 1)
fig3Mat <- matrix(unlist(fig3List), ncol=length(mvalues)*length(cvalues), byrow=FALSE)
xvalues <- cvalues
yvalues <- mvalues
grid <- expand.grid(x=xvalues, y=yvalues)
grid$z <- fig3Mat[40,]
plot3b <- levelplot(z~x*y, grid, xlab="sea-level rise (c)",
ylab="engineering (m)", col.regions = viridis(100))
library(gridExtra)
grid.arrange(plot3a,plot3b, ncol=1)
grid.text(c("(a)","(b)"), x=c(0.05, 0.05), y=c(0.95, 0.47), vjust=1, hjust=0, gp=gpar(fontface=1))
################################################################################
# FIGURE 4
################################################################################
#Figure 4A
initialhq <- c(0,1,2)
fig4aList <- mclapply(initialhq, iterate,
subintervals = 3000,
timesteps = 50,
d = 0.1, #deterioration, erosion
m = 0.2,
l = 2.1, #effect of nearby individuals on engineering ability
mu = 0.0, #value of optimal habitat quality
sigma = 0.3, #std dev about that optimal habitat quality
rMax = 2.0, #maximum growth rate (at optimal quality)
b = 0.4, #dispersal ability of propagules 0.4
c = 0, #rate of sea level rise
doEngineer = 1,
doErosion = 1,
doRiseSeaLevel =0,
doGrow = 1)
fig4aMat <- matrix(unlist(fig4aList), ncol=3, nrow=51)
dev.off()
x11(width=4, height=5)
times <- 0:50
par(bg=NA, las=1)
plot(times, fig4aMat[,1], type='l', xlab="", ylab="area occupied",
lwd=1.5, ylim=c(0.0, 1.0), cex.axis=0.8)
title(xlab = "year", mgp = c(2, 1, 0))
lines(times, fig4aMat[,2], lty=3, lwd=1.5)
lines(times, fig4aMat[,3], lty=5, lwd=1.5)
legend(20, 0.95,c("suboptimal", "neutral", "optimal"),
lty=c(1, 3, 5), cex=0.8, y.intersp=1.5)
plot4a <- grab_grob()
#Figure 4B
initialhq <- c(0,1,2)
fig4bList <- mclapply(initialhq, iterate,
subintervals = 3000,
timesteps = 50,
d = 0.1, #deterioration, erosion
m = 0.2,
l = 2.1, #effect of nearby individuals on engineering ability
mu = 0.0, #value of optimal habitat quality
sigma = 0.3, #std dev about that optimal habitat quality
rMax = 2.0, #maximum growth rate (at optimal quality)
b = 0.4, #dispersal ability of propagules 0.4
c = 0.1, #rate of sea level rise
doEngineer = 1,
doErosion = 1,
doRiseSeaLevel =1,
doGrow = 1)
fig4bMat <- matrix(unlist(fig4bList), ncol=3, nrow=51)
dev.off()
x11(width=4, height=5)
times <- 0:50
par(bg=NA, las=1)
plot(times, fig4bMat[,1], type='l', xlab="", ylab="area occupied",
lwd=1.5, ylim=c(0.0, 1.0), cex.axis=0.8)
title(xlab = "year", mgp = c(2, 1, 0))
lines(times, fig4bMat[,2], lty=3, lwd=1.5)
lines(times, fig4bMat[,3], lty=5, lwd=1.5)
legend(20, 0.5,c("suboptimal", "neutral", "optimal"),
lty=c(1, 3, 5), cex=0.8, y.intersp=1.5)
plot4b <- grab_grob()
library(gridExtra)
grid.arrange(plot4a,plot4b, ncol=2)
grid.text(c("(a)","(b)"), x=c(0.05, 0.55), y=c(0.95, 0.95), vjust=1, hjust=0, gp=gpar(fontface=1))