-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathB_1sp_Model.R
343 lines (282 loc) · 12.5 KB
/
B_1sp_Model.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
##########################
# B sp model
###########################
library(purrr)
#library(tidyverse)
library(lubridate)
library(plyr)
library(dplyr)
library(ggplot2)
# data retrieval tool from USGS
library(dataRetrieval)
#library(doMC)
#library(foreach)
#registerDoMC(4)
#packages for every foreach
#pkgs <- c("purrr", "tidyverse", "lubridate", "plyr", "dplyr")
# Code for HPC - tidyverse has some issues on our HPC because one of the packages is deprecated
# We have to manually load all tidyverse packages
# library(purrr, lib.loc = "/home/ib/kurthena/R_libs/4.2.1")
# library(tibble, lib.loc = "/home/ib/kurthena/R_libs/4.2.1")
# library(tidyr, lib.loc = "/home/ib/kurthena/R_libs/4.2.1")
# library(readr, lib.loc = "/home/ib/kurthena/R_libs/4.2.1")
# library(stringr, lib.loc = "/home/ib/kurthena/R_libs/4.2.1")
# library(forcats, lib.loc = "/home/ib/kurthena/R_libs/4.2.1")
# library(lubridate, lib.loc = "/home/ib/kurthena/R_libs/4.2.1")
# library(plyr, lib.loc = "/home/ib/kurthena/R_libs/4.2.1")
# library(dplyr, lib.loc = "/home/ib/kurthena/R_libs/4.2.1")
# library(ggplot2, lib.loc = "/home/ib/kurthena/R_libs/4.2.1")
# library(dataRetrieval, lib.loc = "/home/ib/kurthena/R_libs/4.2.1")
source("1spFunctions.R")
#------------------------------------------------------------
# Set up location specific data
#-----------------------------------------------------------
#if looking at ColRiver temps read in temperature data from USGS gauge at Lees Ferry, AZ between _ to the end of last water year
# temp <- readNWISdv("09380000", "00010", "2007-10-01", "2021-09-30")
#-------------------------------------------------------------
# create a series of average 2 weekly data
#-------------------------------------------------------------
# calculate mean temperature data for each timestep (2 week interval)
# temps <- average.yearly.temp(temp, "X_00010_00003", "Date")
# Time <- c(1:1825)
# Date <- rep(c(1:365), times = 5)
# Day <- seq(as.Date("2022-01-01"), as.Date("2026-12-31"), by="days")
# Day <- Day[-which(Day == "2024-02-29")]
#
# Temperature <- -7.374528 * (cos(((2*pi)/365)*Date)) + (-1.649263* sin(2*pi/(365)*Date)) + 10.956243
#
#temp <- as.data.frame(cbind(Time, Day, Date, Temperature))
# peaklist <- 0
# peakeach <- length(temp$Temperature)
# iteration <- 1
# baselineK <- 10000
# disturbanceK <- 40000
# peaklist <- 0
# peakeach <- length(temp$Temperature)
# iteration <- 1
# baselineK <- 10000
# disturbanceK <- 40000
# extinct = 50
# #discharge <- rep(0.1, time = length(temp$dts))
# flow.data <- discharge
# temp.data <- temp
# Qmin <- 0.25
# fecundity <- 1200
# dds <- 500
Bmodel <- function(flow.data, temp.data, baselineK, disturbanceK, Qmin, extinct, iteration, peaklist = NULL, peakeach = NULL, fecundity = 1200, dds = 500, stage_output = "all", dens.dep = T){
# set up model
source("NegExpSurv.R")
Q <- as.numeric(flow.data)
temps <- temp.data
degreedays <- as.data.frame(cbind(temps$dts, temps$Temperature * 14))
colnames(degreedays) <- c("dts", "DegreeDay")
degreedays$DegreeDay[degreedays$DegreeDay<0] <- 0
degreedays$dts <- as.Date(degreedays$dts, origin = "1970-01-01")
# need to make ramped increasing hydropeaking index
hp <- c(rep(peaklist, each = peakeach))
# specify iterations
iterations <- iteration
# baseline K in the absence of disturbance
Kb <- as.numeric(baselineK)
# max K after a big disturbance
Kd <- as.numeric(disturbanceK)
# specify baseline transition probabilities for each species at mean temps
G1 = 0.125#move to Stage2 (subimago)
G2 = 0.075 #move to Stage3 (adult)
P1 = 0.75#stay in Stage1 (larvae)
P2 = 0.75#stay in Stage2 (subimago)
# want to run this for one year, in 14 day timesteps
timestep <- seq(2, (length(temps$Temperature) + 1), by = 1)
# create an array to put our output into
# output.N.array <- array(0, dim = c(length(timestep) + 1))
#
# output.N.list <- list(output.N.array)
#
# create array to put the total N of all species into
Total.N <- array(0,
dim <-c((length(timestep) +1 ), iterations),
dimnames <- list(1:(length(timestep) + 1), 1:iterations))
# create list of arrays w/ abundance data for each spp
output.N.list <- array(0,
dim = c(length(timestep) + 1, 3, iterations),
dimnames = list(1:(length(timestep)+1), c("S1", "S2", "S3"), 1:iterations)
)
sizelist <- array(0,
dim = c(length(timestep) + 1, 3, iterations),
dimnames = list(1:(length(timestep)+1), c("S1", "S2", "S3"), 1:iterations)
)
Qmin <- Qmin
a <- 0.01
g <- 0.075
#g <- 0.125
#h <- high$m$getPars()[2]
#k <- high$m$getPars()[1]
h <- med$m$getPars()[2]
k <- med$m$getPars()[1]
iterlist <- c(1:iterations)
extinction <- extinct
#-------------------------
# Outer Loop of Iterations
#--------------------------
# Initializes the progress bar
# pb <- txtProgressBar(min = 0, # Minimum value of the progress bar
# max = iterations, # Maximum value of the progress bar
# style = 3, # Progress bar style (also available style = 1 and style = 2)
# width = 50, # Progress bar width. Defaults to getOption("width")
# char = "=") # Character used to create the bar
for (iter in c(1:iterations)) {
#foreach (iter = c(1:iterations), .combine=cbind, .packages = pkgs) %dopar% {
#source("1spFunctions.R")
# Sets the progress bar to the current state
# setTxtProgressBar(pb, iter)
K <- Kb # need to reset K for each iteration
# pull random values from a uniform distribution
#output.N.list[1,1:3, iter]<- runif(3, min = 1, max = (0.3*K))
output.N.list[1,1:3, iter]<- c(5000, 3000, 100)
# we often want to look at different parameter values after we run code, so we create some lists
# list to input Ks
Klist <- vector()
Klist[1] <- 10000
# list to imput flow morts
flowmortlist <- vector()
Flist <- vector()
emergetime <- vector()
# delta <- vector()
# development <- vector()
TempSurvival <- vector()
for(c in temps$Temperature){
b <- TempSurv(c)
TempSurvival <- append(TempSurvival, b)
}
#-------------------------
# Inner Loop of Timesteps
#-------------------------
for (t in timestep) {
#----------------------------------------------------------
# Calculate how many timesteps emerging adults have matured
emergetime <- append(emergetime, back.count.degreedays(t, dds, degreedays)) # value from Sweeney et al 2017
# delta <- append(delta, round(devtime(temps$Temperature[t-1])/14))
#---------------------------------------------------------
# Calculate fecundity per adult
# we start by pulling fecundities from normal distribution
# assuming 50 50 sex ration, 0.22 of egg masses 'dissapearred', and 0.2 desiccation because of rock drying
F3 = fecundity * hydropeaking.mortality(0.0, 0.2, h = hp[t-1])
#F3 = rnorm(1, mean = 1104.5, sd = 42.75) * 0.5 #Baetidae egg minima and maxima from Degrange, 1960, assuming 1:1 sex ratio and 50% egg mortality
# we can also relate fecundities to body mass.
# in order to iterate through different fecundities
# emergetimes for our temp regime are between 3 and 9
# create a lm for that data, with +10% and -10% of fecundity
x <- c(3,9)
y <- c(fecundity*0.9, fecundity*1.1)
mod <- lm(y~x)
if (t > 19) { # will be erased in burn
size <- emergetime[t-1]
sizes <- c(mean(c(1, emergetime[t-1]/2), na.rm = T),
mean(c(emergetime[t-1]/2, emergetime[t-1]), na.rm = T),
emergetime[t-1])
sizelist[t, 1:3, iter] <- sizes
F3 <- ((size*mod$coefficients[2])+mod$coefficients[1]) * hydropeaking.mortality(0.0, 0.2, h = hp[t-1])
#F3 <- (57*size)+506 * 0.5 * hydropeaking.mortality(0.0, 0.2, h = hp[t-1]) * 0.78 * 0.65
}
# size <- delta[t-1]
# sizelist <- append(sizelist, size)
# F3 <- F3 <- (41.86*size)+200 * 0.5 * hydropeaking.mortality(0.0, 0.2, h = hp[t-1]) * 0.78 * 0.65
#
#--------------------------------------------------
# Calculate the disturbance magnitude-K relationship
# Sets to 0 if below the Qmin
Qf <- Qf.Function(Q[t-1], Qmin, a)
#-------------------------------------------------------------------
# Calculate K carrying capacity immediately following the disturbance
K0 <- K + ((Kd-K)*Qf)
# Calculate final K for timestep, including relationship between K and time since disturbance
K <- post.dist.K(K0, Kb, g, t, Q, Qmin)
Klist <- append(Klist, K)
#---------------------------------------------
# Calculate effect of density dependence on fecundity
if (dens.dep == T){
# Logistic via Rogosch et al. Fish Model
# no immediate egg mortality incorporated
F3 <- Logistic.Dens.Dependence(F3, K, Total.N[t-1, iter])
#
# add F_BAET to list
Flist <- append(Flist, F3)}
if (dens.dep == F){
F3 <- F3
}
#-----------------------------------------------
# Calculate new transition probabilities based on temperature
# This is the growth v development tradeoff
# development measures
# in this function, we assume that if below the min temp threshold (9) no maturation occurs (slow maturation, large growth)
# if above the max temp threshold (15), no one remains more than 1 timestep in each stage (fast maturation, small growth)
if (is.na(emergetime[t-1]) == F) {
P1 <- (1-(1/((emergetime[t-1])/2))) *TempSurvival[t-1]
P2 <- (1-(1/((emergetime[t-1])/2)))*TempSurvival[t-1]
G1 <- (0.2/((emergetime[t-1])/2))*TempSurvival[t-1]
G2 <- (0.4/((emergetime[t-1])/2))*TempSurvival[t-1]
}
if (is.na(emergetime[t-1]) == T) {
G1 <- (0.2/((-0.72 * temps$Temperature[t-1]) + 19.54))*TempSurvival[t-1]
P1 <- (1-(1/((-0.72 * temps$Temperature[t-1]) + 19.54)))*TempSurvival[t-1]
G2 <- (0.4/((-0.72 * temps$Temperature[t-1]) + 19.54))*TempSurvival[t-1]
P2 <- (1-(1/((-0.72 * temps$Temperature[t-1]) + 19.54)))*TempSurvival[t-1]
}
if (G1 > 1) G1 <- 1
if (G1 < 0) G1 <- 0
if (G2 > 1) G2 <- 1
if (G2 < 0) G2 <- 0
if (P1 > 1) P1 <- 1
if (P2 < 0) P2 <- 0
#-----------------------------------------------
# Create Lefkovitch Matrix
T1 <- c(P1, 0, F3)
T2 <- c(G1, P2, 0)
T3 <- c(0, G2, 0)
A <- rbind( T1, T2, T3)
#--------------------------------------
# Calculate abundances for each stage
output.N.list[t, 1:3, iter] <- A %*% output.N.list[t-1, 1:3, iter]
#------------------------------------------
#Calculate immediate mortality due to flows
# mortality due to flooding follows N0 = Nz*e^-hQ
#s1
output.N.list[t, 1, iter] <- flood.mortality(output.N.list[t, 1, iter], k, h, Q[t-1], Qmin)
# #s2Q
output.N.list[t,2,iter] <- flood.mortality(output.N.list[t,2,iter], k, h, Q[t-1], Qmin)
#
output.N.list[t,3,iter] <- flood.mortality(output.N.list[t,3,iter], k, h, Q[t-1], Qmin)
#
#flowmortlist <- append(flowmortlist, flood.mortality(1, k, h, Q[t-1], Qmin))
#
#------------------------------------------------------
# if any values infinity, turn to highest integer, since model breaks and considered Inf non numeric
if( any(is.infinite(output.N.list[t,,iter]))== T){
output.N.list[t, 1:3, iter] <- 5.6e+307 # max value div by 3
Total.N[t, iter] <- 1.7e+308 ## max integer value in R, if we allow it to be INF, causes errors
}
else {
# check extinction threshold and if below set to 0
Total.N[t,iter] <- sum(output.N.list[t,,iter])
if (Total.N[t, iter] < extinction){
output.N.list[t,,iter] <- 0
Total.N[t, iter] <- 0}
}
} #-------------------------
# End Inner Loop
#-------------------------
#close(pb) # close progress bar
} #----------------------
# End Outer Loop
#----------------------
#return(Klist)
if (stage_output == "all"){
return(output.N.list[ , 1:3, ])
}
if (stage_output == "3"){
return(output.N.list[ , 3, ])
}
if (stage_output == "size"){
return(sizelist)
}
}