annotate insect_phenology_model.R @ 0:244c373f2a34 draft

Uploaded
author greg
date Tue, 08 Aug 2017 13:14:39 -0400
parents
children 24fa0d35a8bf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
244c373f2a34 Uploaded
greg
parents:
diff changeset
1 #!/usr/bin/env Rscript
244c373f2a34 Uploaded
greg
parents:
diff changeset
2
244c373f2a34 Uploaded
greg
parents:
diff changeset
3 suppressPackageStartupMessages(library("optparse"))
244c373f2a34 Uploaded
greg
parents:
diff changeset
4
244c373f2a34 Uploaded
greg
parents:
diff changeset
5 option_list <- list(
244c373f2a34 Uploaded
greg
parents:
diff changeset
6 make_option(c("-a", "--adult_mort"), action="store", dest="adult_mort", type="integer", help="Adjustment rate for adult mortality"),
244c373f2a34 Uploaded
greg
parents:
diff changeset
7 make_option(c("-b", "--adult_accum"), action="store", dest="adult_accum", type="integer", help="Adjustment of DD accumulation (old nymph->adult)"),
244c373f2a34 Uploaded
greg
parents:
diff changeset
8 make_option(c("-c", "--egg_mort"), action="store", dest="egg_mort", type="integer", help="Adjustment rate for egg mortality"),
244c373f2a34 Uploaded
greg
parents:
diff changeset
9 make_option(c("-d", "--latitude"), action="store", dest="latitude", type="double", help="Latitude of selected location"),
244c373f2a34 Uploaded
greg
parents:
diff changeset
10 make_option(c("-e", "--location"), action="store", dest="location", help="Selected location"),
244c373f2a34 Uploaded
greg
parents:
diff changeset
11 make_option(c("-f", "--min_clutch_size"), action="store", dest="min_clutch_size", type="integer", help="Adjustment of minimum clutch size"),
244c373f2a34 Uploaded
greg
parents:
diff changeset
12 make_option(c("-i", "--max_clutch_size"), action="store", dest="max_clutch_size", type="integer", help="Adjustment of maximum clutch size"),
244c373f2a34 Uploaded
greg
parents:
diff changeset
13 make_option(c("-j", "--nymph_mort"), action="store", dest="nymph_mort", type="integer", help="Adjustment rate for nymph mortality"),
244c373f2a34 Uploaded
greg
parents:
diff changeset
14 make_option(c("-k", "--old_nymph_accum"), action="store", dest="old_nymph_accum", type="integer", help="Adjustment of DD accumulation (young nymph->old nymph)"),
244c373f2a34 Uploaded
greg
parents:
diff changeset
15 make_option(c("-o", "--output"), action="store", dest="output", help="Output dataset"),
244c373f2a34 Uploaded
greg
parents:
diff changeset
16 make_option(c("-p", "--oviposition"), action="store", dest="oviposition", type="integer", help="Adjustment for oviposition rate"),
244c373f2a34 Uploaded
greg
parents:
diff changeset
17 make_option(c("-q", "--photoperiod"), action="store", dest="photoperiod", type="double", help="Critical photoperiod for diapause induction/termination"),
244c373f2a34 Uploaded
greg
parents:
diff changeset
18 make_option(c("-s", "--replications"), action="store", dest="replications", type="integer", help="Number of replications"),
244c373f2a34 Uploaded
greg
parents:
diff changeset
19 make_option(c("-t", "--se_plot"), action="store", dest="se_plot", help="Plot SE"),
244c373f2a34 Uploaded
greg
parents:
diff changeset
20 make_option(c("-u", "--year"), action="store", dest="year", type="integer", help="Starting year"),
244c373f2a34 Uploaded
greg
parents:
diff changeset
21 make_option(c("-v", "--temperature_dataset"), action="store", dest="temperature_dataset", help="Temperature data for selected location"),
244c373f2a34 Uploaded
greg
parents:
diff changeset
22 make_option(c("-y", "--young_nymph_accum"), action="store", dest="young_nymph_accum", type="integer", help="Adjustment of DD accumulation (egg->young nymph)")
244c373f2a34 Uploaded
greg
parents:
diff changeset
23 )
244c373f2a34 Uploaded
greg
parents:
diff changeset
24
244c373f2a34 Uploaded
greg
parents:
diff changeset
25 parser <- OptionParser(usage="%prog [options] file", option_list=option_list)
244c373f2a34 Uploaded
greg
parents:
diff changeset
26 args <- parse_args(parser, positional_arguments=TRUE)
244c373f2a34 Uploaded
greg
parents:
diff changeset
27 opt <- args$options
244c373f2a34 Uploaded
greg
parents:
diff changeset
28
244c373f2a34 Uploaded
greg
parents:
diff changeset
29 data.input=function(loc, year, temperature.dataset)
244c373f2a34 Uploaded
greg
parents:
diff changeset
30 {
244c373f2a34 Uploaded
greg
parents:
diff changeset
31 expdata <- matrix(rep(0, 365 * 3), nrow=365)
244c373f2a34 Uploaded
greg
parents:
diff changeset
32 namedat <- paste(loc, year, ".Rdat", sep="")
244c373f2a34 Uploaded
greg
parents:
diff changeset
33 temp.data <- read.csv(file=temperature.dataset, header=T)
244c373f2a34 Uploaded
greg
parents:
diff changeset
34
244c373f2a34 Uploaded
greg
parents:
diff changeset
35 expdata[,1] <- c(1:365)
244c373f2a34 Uploaded
greg
parents:
diff changeset
36 # Minimum
244c373f2a34 Uploaded
greg
parents:
diff changeset
37 expdata[,2] <- temp.data[c(1:365), 3]
244c373f2a34 Uploaded
greg
parents:
diff changeset
38 # Maximum
244c373f2a34 Uploaded
greg
parents:
diff changeset
39 expdata[,3] <- temp.data[c(1:365), 2]
244c373f2a34 Uploaded
greg
parents:
diff changeset
40 save(expdata, file=namedat)
244c373f2a34 Uploaded
greg
parents:
diff changeset
41 namedat
244c373f2a34 Uploaded
greg
parents:
diff changeset
42 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
43
244c373f2a34 Uploaded
greg
parents:
diff changeset
44 daylength=function(latitude)
244c373f2a34 Uploaded
greg
parents:
diff changeset
45 {
244c373f2a34 Uploaded
greg
parents:
diff changeset
46 # from Forsythe 1995
244c373f2a34 Uploaded
greg
parents:
diff changeset
47 p=0.8333
244c373f2a34 Uploaded
greg
parents:
diff changeset
48 dl <- NULL
244c373f2a34 Uploaded
greg
parents:
diff changeset
49 for (i in 1:365) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
50 theta <- 0.2163108 + 2 * atan(0.9671396 * tan(0.00860 * (i - 186)))
244c373f2a34 Uploaded
greg
parents:
diff changeset
51 phi <- asin(0.39795 * cos(theta))
244c373f2a34 Uploaded
greg
parents:
diff changeset
52 dl[i] <- 24 - 24 / pi * acos((sin(p * pi / 180) + sin(latitude * pi / 180) * sin(phi)) / (cos(latitude * pi / 180) * cos(phi)))
244c373f2a34 Uploaded
greg
parents:
diff changeset
53 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
54 dl # return a vector of daylength in 365 days
244c373f2a34 Uploaded
greg
parents:
diff changeset
55 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
56
244c373f2a34 Uploaded
greg
parents:
diff changeset
57 hourtemp=function(latitude, date, temperature_file_path)
244c373f2a34 Uploaded
greg
parents:
diff changeset
58 {
244c373f2a34 Uploaded
greg
parents:
diff changeset
59 load(temperature_file_path)
244c373f2a34 Uploaded
greg
parents:
diff changeset
60 threshold <- 14.17 # base development threshold for BMSB
244c373f2a34 Uploaded
greg
parents:
diff changeset
61 dnp <- expdata[date, 2] # daily minimum
244c373f2a34 Uploaded
greg
parents:
diff changeset
62 dxp <- expdata[date, 3] # daily maximum
244c373f2a34 Uploaded
greg
parents:
diff changeset
63 dmean <- 0.5 * (dnp + dxp)
244c373f2a34 Uploaded
greg
parents:
diff changeset
64 dd <- 0 # initialize degree day accumulation
244c373f2a34 Uploaded
greg
parents:
diff changeset
65
244c373f2a34 Uploaded
greg
parents:
diff changeset
66 if (dxp<threshold) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
67 dd <- 0
244c373f2a34 Uploaded
greg
parents:
diff changeset
68 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
69 else {
244c373f2a34 Uploaded
greg
parents:
diff changeset
70 dlprofile <- daylength(latitude) # extract daylength data for entire year
244c373f2a34 Uploaded
greg
parents:
diff changeset
71 T <- NULL # initialize hourly temperature
244c373f2a34 Uploaded
greg
parents:
diff changeset
72 dh <- NULL #initialize degree hour vector
244c373f2a34 Uploaded
greg
parents:
diff changeset
73 # date <- 200
244c373f2a34 Uploaded
greg
parents:
diff changeset
74 y <- dlprofile[date] # calculate daylength in given date
244c373f2a34 Uploaded
greg
parents:
diff changeset
75 z <- 24 - y # night length
244c373f2a34 Uploaded
greg
parents:
diff changeset
76 a <- 1.86 # lag coefficient
244c373f2a34 Uploaded
greg
parents:
diff changeset
77 b <- 2.20 # night coefficient
244c373f2a34 Uploaded
greg
parents:
diff changeset
78 #tempdata <- read.csv("tempdata.csv") #import raw data set
244c373f2a34 Uploaded
greg
parents:
diff changeset
79 # Should be outside function otherwise its redundant
244c373f2a34 Uploaded
greg
parents:
diff changeset
80 risetime <- 12 - y / 2 # sunrise time
244c373f2a34 Uploaded
greg
parents:
diff changeset
81 settime <- 12 + y / 2 # sunset time
244c373f2a34 Uploaded
greg
parents:
diff changeset
82 ts <- (dxp - dnp) * sin(pi * (settime - 5) / (y + 2 * a)) + dnp
244c373f2a34 Uploaded
greg
parents:
diff changeset
83 for (i in 1:24) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
84 if (i > risetime && i<settime) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
85 m <- i - 5 # number of hours after Tmin until sunset
244c373f2a34 Uploaded
greg
parents:
diff changeset
86 T[i]=(dxp - dnp) * sin(pi * m / (y + 2 * a)) + dnp
244c373f2a34 Uploaded
greg
parents:
diff changeset
87 if (T[i]<8.4) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
88 dh[i] <- 0
244c373f2a34 Uploaded
greg
parents:
diff changeset
89 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
90 else {
244c373f2a34 Uploaded
greg
parents:
diff changeset
91 dh[i] <- T[i] - 8.4
244c373f2a34 Uploaded
greg
parents:
diff changeset
92 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
93 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
94 else if (i > settime) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
95 n <- i - settime
244c373f2a34 Uploaded
greg
parents:
diff changeset
96 T[i]=dnp + (ts - dnp) * exp( - b * n / z)
244c373f2a34 Uploaded
greg
parents:
diff changeset
97 if (T[i]<8.4) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
98 dh[i] <- 0
244c373f2a34 Uploaded
greg
parents:
diff changeset
99 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
100 else {
244c373f2a34 Uploaded
greg
parents:
diff changeset
101 dh[i] <- T[i] - 8.4
244c373f2a34 Uploaded
greg
parents:
diff changeset
102 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
103 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
104 else {
244c373f2a34 Uploaded
greg
parents:
diff changeset
105 n <- i + 24 - settime
244c373f2a34 Uploaded
greg
parents:
diff changeset
106 T[i]=dnp + (ts - dnp) * exp( - b * n / z)
244c373f2a34 Uploaded
greg
parents:
diff changeset
107 if (T[i]<8.4) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
108 dh[i] <- 0
244c373f2a34 Uploaded
greg
parents:
diff changeset
109 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
110 else {
244c373f2a34 Uploaded
greg
parents:
diff changeset
111 dh[i] <- T[i] - 8.4
244c373f2a34 Uploaded
greg
parents:
diff changeset
112 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
113 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
114 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
115 dd <- sum(dh) / 24
244c373f2a34 Uploaded
greg
parents:
diff changeset
116 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
117 return=c(dmean, dd)
244c373f2a34 Uploaded
greg
parents:
diff changeset
118 return
244c373f2a34 Uploaded
greg
parents:
diff changeset
119 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
120
244c373f2a34 Uploaded
greg
parents:
diff changeset
121 dev.egg = function(temperature)
244c373f2a34 Uploaded
greg
parents:
diff changeset
122 {
244c373f2a34 Uploaded
greg
parents:
diff changeset
123 dev.rate= -0.9843 * temperature + 33.438
244c373f2a34 Uploaded
greg
parents:
diff changeset
124 return = dev.rate
244c373f2a34 Uploaded
greg
parents:
diff changeset
125 return
244c373f2a34 Uploaded
greg
parents:
diff changeset
126 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
127
244c373f2a34 Uploaded
greg
parents:
diff changeset
128 dev.young = function(temperature)
244c373f2a34 Uploaded
greg
parents:
diff changeset
129 {
244c373f2a34 Uploaded
greg
parents:
diff changeset
130 n12 <- -0.3728 * temperature + 14.68
244c373f2a34 Uploaded
greg
parents:
diff changeset
131 n23 <- -0.6119 * temperature + 25.249
244c373f2a34 Uploaded
greg
parents:
diff changeset
132 dev.rate = mean(n12 + n23)
244c373f2a34 Uploaded
greg
parents:
diff changeset
133 return = dev.rate
244c373f2a34 Uploaded
greg
parents:
diff changeset
134 return
244c373f2a34 Uploaded
greg
parents:
diff changeset
135 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
136
244c373f2a34 Uploaded
greg
parents:
diff changeset
137 dev.old = function(temperature)
244c373f2a34 Uploaded
greg
parents:
diff changeset
138 {
244c373f2a34 Uploaded
greg
parents:
diff changeset
139 n34 <- -0.6119 * temperature + 17.602
244c373f2a34 Uploaded
greg
parents:
diff changeset
140 n45 <- -0.4408 * temperature + 19.036
244c373f2a34 Uploaded
greg
parents:
diff changeset
141 dev.rate = mean(n34 + n45)
244c373f2a34 Uploaded
greg
parents:
diff changeset
142 return = dev.rate
244c373f2a34 Uploaded
greg
parents:
diff changeset
143 return
244c373f2a34 Uploaded
greg
parents:
diff changeset
144 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
145
244c373f2a34 Uploaded
greg
parents:
diff changeset
146 dev.emerg = function(temperature)
244c373f2a34 Uploaded
greg
parents:
diff changeset
147 {
244c373f2a34 Uploaded
greg
parents:
diff changeset
148 emerg.rate <- -0.5332 * temperature + 24.147
244c373f2a34 Uploaded
greg
parents:
diff changeset
149 return = emerg.rate
244c373f2a34 Uploaded
greg
parents:
diff changeset
150 return
244c373f2a34 Uploaded
greg
parents:
diff changeset
151 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
152
244c373f2a34 Uploaded
greg
parents:
diff changeset
153 mortality.egg = function(temperature)
244c373f2a34 Uploaded
greg
parents:
diff changeset
154 {
244c373f2a34 Uploaded
greg
parents:
diff changeset
155 if (temperature < 12.7) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
156 mort.prob = 0.8
244c373f2a34 Uploaded
greg
parents:
diff changeset
157 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
158 else {
244c373f2a34 Uploaded
greg
parents:
diff changeset
159 mort.prob = 0.8 - temperature / 40.0
244c373f2a34 Uploaded
greg
parents:
diff changeset
160 if (mort.prob < 0) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
161 mort.prob = 0.01
244c373f2a34 Uploaded
greg
parents:
diff changeset
162 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
163 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
164 return = mort.prob
244c373f2a34 Uploaded
greg
parents:
diff changeset
165 return
244c373f2a34 Uploaded
greg
parents:
diff changeset
166 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
167
244c373f2a34 Uploaded
greg
parents:
diff changeset
168 mortality.nymph = function(temperature)
244c373f2a34 Uploaded
greg
parents:
diff changeset
169 {
244c373f2a34 Uploaded
greg
parents:
diff changeset
170 if (temperature < 12.7) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
171 mort.prob = 0.03
244c373f2a34 Uploaded
greg
parents:
diff changeset
172 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
173 else {
244c373f2a34 Uploaded
greg
parents:
diff changeset
174 mort.prob = temperature * 0.0008 + 0.03
244c373f2a34 Uploaded
greg
parents:
diff changeset
175 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
176 return = mort.prob
244c373f2a34 Uploaded
greg
parents:
diff changeset
177 return
244c373f2a34 Uploaded
greg
parents:
diff changeset
178 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
179
244c373f2a34 Uploaded
greg
parents:
diff changeset
180 mortality.adult = function(temperature)
244c373f2a34 Uploaded
greg
parents:
diff changeset
181 {
244c373f2a34 Uploaded
greg
parents:
diff changeset
182 if (temperature < 12.7) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
183 mort.prob = 0.002
244c373f2a34 Uploaded
greg
parents:
diff changeset
184 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
185 else {
244c373f2a34 Uploaded
greg
parents:
diff changeset
186 mort.prob = temperature * 0.0005 + 0.02
244c373f2a34 Uploaded
greg
parents:
diff changeset
187 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
188 return = mort.prob
244c373f2a34 Uploaded
greg
parents:
diff changeset
189 return
244c373f2a34 Uploaded
greg
parents:
diff changeset
190 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
191
244c373f2a34 Uploaded
greg
parents:
diff changeset
192 cat("Replications: ", opt$replications, "\n")
244c373f2a34 Uploaded
greg
parents:
diff changeset
193 cat("Photoperiod: ", opt$photoperiod, "\n")
244c373f2a34 Uploaded
greg
parents:
diff changeset
194 cat("Oviposition rate: ", opt$oviposition, "\n")
244c373f2a34 Uploaded
greg
parents:
diff changeset
195 cat("Egg mortality rate: ", opt$egg_mort, "\n")
244c373f2a34 Uploaded
greg
parents:
diff changeset
196 cat("Nymph mortality rate: ", opt$nymph_mort, "\n")
244c373f2a34 Uploaded
greg
parents:
diff changeset
197 cat("Adult mortality rate: ", opt$adult_mort, "\n")
244c373f2a34 Uploaded
greg
parents:
diff changeset
198 cat("Min clutch size: ", opt$min_clutch_size, "\n")
244c373f2a34 Uploaded
greg
parents:
diff changeset
199 cat("Max clutch size: ", opt$max_clutch_size, "\n")
244c373f2a34 Uploaded
greg
parents:
diff changeset
200 cat("(egg->young nymph): ", opt$young_nymph_accum, "\n")
244c373f2a34 Uploaded
greg
parents:
diff changeset
201 cat("(young nymph->old nymph): ", opt$old_nymph_accum, "\n")
244c373f2a34 Uploaded
greg
parents:
diff changeset
202 cat("(old nymph->adult): ", opt$adult_accum, "\n")
244c373f2a34 Uploaded
greg
parents:
diff changeset
203
244c373f2a34 Uploaded
greg
parents:
diff changeset
204 # Read in the input temperature datafile
244c373f2a34 Uploaded
greg
parents:
diff changeset
205 temperature_file_path <- data.input(opt$location, opt$year, opt$temperature_dataset)
244c373f2a34 Uploaded
greg
parents:
diff changeset
206
244c373f2a34 Uploaded
greg
parents:
diff changeset
207 # Initialize matrix for results from all replications
244c373f2a34 Uploaded
greg
parents:
diff changeset
208 S0.rep <- S1.rep <- S2.rep <- S3.rep <- S4.rep <- S5.rep <- matrix(rep(0, 365 * opt$replications), ncol = opt$replications)
244c373f2a34 Uploaded
greg
parents:
diff changeset
209 newborn.rep <- death.rep <- adult.rep <- pop.rep <- g0.rep <- g1.rep <- g2.rep <- g0a.rep <- g1a.rep <- g2a.rep <- matrix(rep(0, 365 * opt$replications), ncol=opt$replications)
244c373f2a34 Uploaded
greg
parents:
diff changeset
210
244c373f2a34 Uploaded
greg
parents:
diff changeset
211 # loop through replications
244c373f2a34 Uploaded
greg
parents:
diff changeset
212 for (N.rep in 1:opt$replications) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
213 # during each replication
244c373f2a34 Uploaded
greg
parents:
diff changeset
214 # start with 1000 individuals -- user definable as well?
244c373f2a34 Uploaded
greg
parents:
diff changeset
215 n <- 1000
244c373f2a34 Uploaded
greg
parents:
diff changeset
216 # Generation, Stage, DD, T, Diapause
244c373f2a34 Uploaded
greg
parents:
diff changeset
217 vec.ini <- c(0, 3, 0, 0, 0)
244c373f2a34 Uploaded
greg
parents:
diff changeset
218 # overwintering, previttelogenic, DD=0, T=0, no-diapause
244c373f2a34 Uploaded
greg
parents:
diff changeset
219 vec.mat <- rep(vec.ini, n)
244c373f2a34 Uploaded
greg
parents:
diff changeset
220 # complete matrix for the population
244c373f2a34 Uploaded
greg
parents:
diff changeset
221 vec.mat <- t(matrix(vec.mat, nrow=5))
244c373f2a34 Uploaded
greg
parents:
diff changeset
222 # complete photoperiod profile in a year, requires daylength function
244c373f2a34 Uploaded
greg
parents:
diff changeset
223 ph.p <- daylength(opt$latitude)
244c373f2a34 Uploaded
greg
parents:
diff changeset
224
244c373f2a34 Uploaded
greg
parents:
diff changeset
225 # time series of population size
244c373f2a34 Uploaded
greg
parents:
diff changeset
226 tot.pop <- NULL
244c373f2a34 Uploaded
greg
parents:
diff changeset
227 # gen.0 pop size
244c373f2a34 Uploaded
greg
parents:
diff changeset
228 gen0.pop <- rep(0, 365)
244c373f2a34 Uploaded
greg
parents:
diff changeset
229 gen1.pop <- rep(0, 365)
244c373f2a34 Uploaded
greg
parents:
diff changeset
230 gen2.pop <- rep(0, 365)
244c373f2a34 Uploaded
greg
parents:
diff changeset
231 S0 <- S1 <- S2 <- S3 <- S4 <- S5 <- rep(0, 365)
244c373f2a34 Uploaded
greg
parents:
diff changeset
232 g0.adult <- g1.adult <- g2.adult <- rep(0, 365)
244c373f2a34 Uploaded
greg
parents:
diff changeset
233 N.newborn <- N.death <- N.adult <- rep(0, 365)
244c373f2a34 Uploaded
greg
parents:
diff changeset
234 dd.day <- rep(0, 365)
244c373f2a34 Uploaded
greg
parents:
diff changeset
235
244c373f2a34 Uploaded
greg
parents:
diff changeset
236 # start tick
244c373f2a34 Uploaded
greg
parents:
diff changeset
237 ptm <- proc.time()
244c373f2a34 Uploaded
greg
parents:
diff changeset
238
244c373f2a34 Uploaded
greg
parents:
diff changeset
239 # all the days
244c373f2a34 Uploaded
greg
parents:
diff changeset
240 for (day in 1:365) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
241 # photoperiod in the day
244c373f2a34 Uploaded
greg
parents:
diff changeset
242 photoperiod <- ph.p[day]
244c373f2a34 Uploaded
greg
parents:
diff changeset
243 temp.profile <- hourtemp(opt$latitude, day, temperature_file_path)
244c373f2a34 Uploaded
greg
parents:
diff changeset
244 mean.temp <- temp.profile[1]
244c373f2a34 Uploaded
greg
parents:
diff changeset
245 dd.temp <- temp.profile[2]
244c373f2a34 Uploaded
greg
parents:
diff changeset
246 dd.day[day] <- dd.temp
244c373f2a34 Uploaded
greg
parents:
diff changeset
247 # trash bin for death
244c373f2a34 Uploaded
greg
parents:
diff changeset
248 death.vec <- NULL
244c373f2a34 Uploaded
greg
parents:
diff changeset
249 # new born
244c373f2a34 Uploaded
greg
parents:
diff changeset
250 birth.vec <- NULL
244c373f2a34 Uploaded
greg
parents:
diff changeset
251
244c373f2a34 Uploaded
greg
parents:
diff changeset
252 # all individuals
244c373f2a34 Uploaded
greg
parents:
diff changeset
253 for (i in 1:n) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
254 # find individual record
244c373f2a34 Uploaded
greg
parents:
diff changeset
255 vec.ind <- vec.mat[i,]
244c373f2a34 Uploaded
greg
parents:
diff changeset
256 # first of all, still alive?
244c373f2a34 Uploaded
greg
parents:
diff changeset
257 # adjustment for late season mortality rate
244c373f2a34 Uploaded
greg
parents:
diff changeset
258 if (opt$latitude < 40.0) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
259 post.mort <- 1
244c373f2a34 Uploaded
greg
parents:
diff changeset
260 day.kill <- 300
244c373f2a34 Uploaded
greg
parents:
diff changeset
261 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
262 else {
244c373f2a34 Uploaded
greg
parents:
diff changeset
263 post.mort <- 2
244c373f2a34 Uploaded
greg
parents:
diff changeset
264 day.kill <- 250
244c373f2a34 Uploaded
greg
parents:
diff changeset
265 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
266 if (vec.ind[2] == 0) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
267 # egg
244c373f2a34 Uploaded
greg
parents:
diff changeset
268 death.prob = opt$egg_mort * mortality.egg(mean.temp)
244c373f2a34 Uploaded
greg
parents:
diff changeset
269 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
270 else if (vec.ind[2] == 1 | vec.ind[2] == 2) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
271 death.prob = opt$nymph_mort * mortality.nymph(mean.temp)
244c373f2a34 Uploaded
greg
parents:
diff changeset
272 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
273 else if (vec.ind[2] == 3 | vec.ind[2] == 4 | vec.ind[2] == 5) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
274 # for adult
244c373f2a34 Uploaded
greg
parents:
diff changeset
275 if (day < day.kill) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
276 death.prob = opt$adult_mort * mortality.adult(mean.temp)
244c373f2a34 Uploaded
greg
parents:
diff changeset
277 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
278 else {
244c373f2a34 Uploaded
greg
parents:
diff changeset
279 # increase adult mortality after fall equinox
244c373f2a34 Uploaded
greg
parents:
diff changeset
280 death.prob = opt$adult_mort * post.mort * mortality.adult(mean.temp)
244c373f2a34 Uploaded
greg
parents:
diff changeset
281 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
282 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
283 # (or dependent on temperature and life stage?)
244c373f2a34 Uploaded
greg
parents:
diff changeset
284 u.d <- runif(1)
244c373f2a34 Uploaded
greg
parents:
diff changeset
285 if (u.d < death.prob) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
286 death.vec <- c(death.vec, i)
244c373f2a34 Uploaded
greg
parents:
diff changeset
287 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
288 else {
244c373f2a34 Uploaded
greg
parents:
diff changeset
289 # aggregrate index of dead bug
244c373f2a34 Uploaded
greg
parents:
diff changeset
290 # event 1 end of diapause
244c373f2a34 Uploaded
greg
parents:
diff changeset
291 if (vec.ind[1] == 0 && vec.ind[2] == 3) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
292 # overwintering adult (previttelogenic)
244c373f2a34 Uploaded
greg
parents:
diff changeset
293 if (photoperiod > opt$photoperiod && vec.ind[3] > 68 && day < 180) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
294 # add 68C to become fully reproductively matured
244c373f2a34 Uploaded
greg
parents:
diff changeset
295 # transfer to vittelogenic
244c373f2a34 Uploaded
greg
parents:
diff changeset
296 vec.ind <- c(0, 4, 0, 0, 0)
244c373f2a34 Uploaded
greg
parents:
diff changeset
297 vec.mat[i,] <- vec.ind
244c373f2a34 Uploaded
greg
parents:
diff changeset
298 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
299 else {
244c373f2a34 Uploaded
greg
parents:
diff changeset
300 # add to DD
244c373f2a34 Uploaded
greg
parents:
diff changeset
301 vec.ind[3] <- vec.ind[3] + dd.temp
244c373f2a34 Uploaded
greg
parents:
diff changeset
302 # add 1 day in current stage
244c373f2a34 Uploaded
greg
parents:
diff changeset
303 vec.ind[4] <- vec.ind[4] + 1
244c373f2a34 Uploaded
greg
parents:
diff changeset
304 vec.mat[i,] <- vec.ind
244c373f2a34 Uploaded
greg
parents:
diff changeset
305 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
306 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
307 if (vec.ind[1] != 0 && vec.ind[2] == 3) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
308 # NOT overwintering adult (previttelogenic)
244c373f2a34 Uploaded
greg
parents:
diff changeset
309 current.gen <- vec.ind[1]
244c373f2a34 Uploaded
greg
parents:
diff changeset
310 if (vec.ind[3] > 68) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
311 # add 68C to become fully reproductively matured
244c373f2a34 Uploaded
greg
parents:
diff changeset
312 # transfer to vittelogenic
244c373f2a34 Uploaded
greg
parents:
diff changeset
313 vec.ind <- c(current.gen, 4, 0, 0, 0)
244c373f2a34 Uploaded
greg
parents:
diff changeset
314 vec.mat[i,] <- vec.ind
244c373f2a34 Uploaded
greg
parents:
diff changeset
315 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
316 else {
244c373f2a34 Uploaded
greg
parents:
diff changeset
317 # add to DD
244c373f2a34 Uploaded
greg
parents:
diff changeset
318 vec.ind[3] <- vec.ind[3] + dd.temp
244c373f2a34 Uploaded
greg
parents:
diff changeset
319 # add 1 day in current stage
244c373f2a34 Uploaded
greg
parents:
diff changeset
320 vec.ind[4] <- vec.ind[4] + 1
244c373f2a34 Uploaded
greg
parents:
diff changeset
321 vec.mat[i,] <- vec.ind
244c373f2a34 Uploaded
greg
parents:
diff changeset
322 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
323 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
324
244c373f2a34 Uploaded
greg
parents:
diff changeset
325 # event 2 oviposition -- where population dynamics comes from
244c373f2a34 Uploaded
greg
parents:
diff changeset
326 if (vec.ind[2] == 4 && vec.ind[1] == 0 && mean.temp > 10) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
327 # vittelogenic stage, overwintering generation
244c373f2a34 Uploaded
greg
parents:
diff changeset
328 if (vec.ind[4] == 0) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
329 # just turned in vittelogenic stage
244c373f2a34 Uploaded
greg
parents:
diff changeset
330 n.birth=round(runif(1, 2 + opt$min_clutch_size, 8 + opt$max_clutch_size))
244c373f2a34 Uploaded
greg
parents:
diff changeset
331 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
332 else {
244c373f2a34 Uploaded
greg
parents:
diff changeset
333 # daily probability of birth
244c373f2a34 Uploaded
greg
parents:
diff changeset
334 p.birth = opt$oviposition * 0.01
244c373f2a34 Uploaded
greg
parents:
diff changeset
335 u1 <- runif(1)
244c373f2a34 Uploaded
greg
parents:
diff changeset
336 if (u1 < p.birth) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
337 n.birth=round(runif(1, 2, 8))
244c373f2a34 Uploaded
greg
parents:
diff changeset
338 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
339 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
340 # add to DD
244c373f2a34 Uploaded
greg
parents:
diff changeset
341 vec.ind[3] <- vec.ind[3] + dd.temp
244c373f2a34 Uploaded
greg
parents:
diff changeset
342 # add 1 day in current stage
244c373f2a34 Uploaded
greg
parents:
diff changeset
343 vec.ind[4] <- vec.ind[4] + 1
244c373f2a34 Uploaded
greg
parents:
diff changeset
344 vec.mat[i,] <- vec.ind
244c373f2a34 Uploaded
greg
parents:
diff changeset
345 if (n.birth > 0) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
346 # add new birth -- might be in different generations
244c373f2a34 Uploaded
greg
parents:
diff changeset
347 # generation + 1
244c373f2a34 Uploaded
greg
parents:
diff changeset
348 new.gen <- vec.ind[1] + 1
244c373f2a34 Uploaded
greg
parents:
diff changeset
349 # egg profile
244c373f2a34 Uploaded
greg
parents:
diff changeset
350 new.ind <- c(new.gen, 0, 0, 0, 0)
244c373f2a34 Uploaded
greg
parents:
diff changeset
351 new.vec <- rep(new.ind, n.birth)
244c373f2a34 Uploaded
greg
parents:
diff changeset
352 # update batch of egg profile
244c373f2a34 Uploaded
greg
parents:
diff changeset
353 new.vec <- t(matrix(new.vec, nrow=5))
244c373f2a34 Uploaded
greg
parents:
diff changeset
354 # group with total eggs laid in that day
244c373f2a34 Uploaded
greg
parents:
diff changeset
355 birth.vec <- rbind(birth.vec, new.vec)
244c373f2a34 Uploaded
greg
parents:
diff changeset
356 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
357 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
358
244c373f2a34 Uploaded
greg
parents:
diff changeset
359 # event 2 oviposition -- for gen 1.
244c373f2a34 Uploaded
greg
parents:
diff changeset
360 if (vec.ind[2] == 4 && vec.ind[1] == 1 && mean.temp > 12.5 && day < 222) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
361 # vittelogenic stage, 1st generation
244c373f2a34 Uploaded
greg
parents:
diff changeset
362 if (vec.ind[4] == 0) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
363 # just turned in vittelogenic stage
244c373f2a34 Uploaded
greg
parents:
diff changeset
364 n.birth=round(runif(1, 2 + opt$min_clutch_size, 8 + opt$max_clutch_size))
244c373f2a34 Uploaded
greg
parents:
diff changeset
365 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
366 else {
244c373f2a34 Uploaded
greg
parents:
diff changeset
367 # daily probability of birth
244c373f2a34 Uploaded
greg
parents:
diff changeset
368 p.birth = opt$oviposition * 0.01
244c373f2a34 Uploaded
greg
parents:
diff changeset
369 u1 <- runif(1)
244c373f2a34 Uploaded
greg
parents:
diff changeset
370 if (u1 < p.birth) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
371 n.birth = round(runif(1, 2, 8))
244c373f2a34 Uploaded
greg
parents:
diff changeset
372 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
373 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
374 # add to DD
244c373f2a34 Uploaded
greg
parents:
diff changeset
375 vec.ind[3] <- vec.ind[3] + dd.temp
244c373f2a34 Uploaded
greg
parents:
diff changeset
376 # add 1 day in current stage
244c373f2a34 Uploaded
greg
parents:
diff changeset
377 vec.ind[4] <- vec.ind[4] + 1
244c373f2a34 Uploaded
greg
parents:
diff changeset
378 vec.mat[i,] <- vec.ind
244c373f2a34 Uploaded
greg
parents:
diff changeset
379 if (n.birth > 0) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
380 # add new birth -- might be in different generations
244c373f2a34 Uploaded
greg
parents:
diff changeset
381 # generation + 1
244c373f2a34 Uploaded
greg
parents:
diff changeset
382 new.gen <- vec.ind[1] + 1
244c373f2a34 Uploaded
greg
parents:
diff changeset
383 # egg profile
244c373f2a34 Uploaded
greg
parents:
diff changeset
384 new.ind <- c(new.gen, 0, 0, 0, 0)
244c373f2a34 Uploaded
greg
parents:
diff changeset
385 new.vec <- rep(new.ind, n.birth)
244c373f2a34 Uploaded
greg
parents:
diff changeset
386 # update batch of egg profile
244c373f2a34 Uploaded
greg
parents:
diff changeset
387 new.vec <- t(matrix(new.vec, nrow=5))
244c373f2a34 Uploaded
greg
parents:
diff changeset
388 # group with total eggs laid in that day
244c373f2a34 Uploaded
greg
parents:
diff changeset
389 birth.vec <- rbind(birth.vec, new.vec)
244c373f2a34 Uploaded
greg
parents:
diff changeset
390 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
391 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
392
244c373f2a34 Uploaded
greg
parents:
diff changeset
393 # event 3 development (with diapause determination)
244c373f2a34 Uploaded
greg
parents:
diff changeset
394 # event 3.1 egg development to young nymph (vec.ind[2]=0 -> egg)
244c373f2a34 Uploaded
greg
parents:
diff changeset
395 if (vec.ind[2] == 0) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
396 # egg stage
244c373f2a34 Uploaded
greg
parents:
diff changeset
397 # add to DD
244c373f2a34 Uploaded
greg
parents:
diff changeset
398 vec.ind[3] <- vec.ind[3] + dd.temp
244c373f2a34 Uploaded
greg
parents:
diff changeset
399 if (vec.ind[3] >= (68 + opt$young_nymph_accum)) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
400 # from egg to young nymph, DD requirement met
244c373f2a34 Uploaded
greg
parents:
diff changeset
401 current.gen <- vec.ind[1]
244c373f2a34 Uploaded
greg
parents:
diff changeset
402 # transfer to young nym stage
244c373f2a34 Uploaded
greg
parents:
diff changeset
403 vec.ind <- c(current.gen, 1, 0, 0, 0)
244c373f2a34 Uploaded
greg
parents:
diff changeset
404 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
405 else {
244c373f2a34 Uploaded
greg
parents:
diff changeset
406 # add 1 day in current stage
244c373f2a34 Uploaded
greg
parents:
diff changeset
407 vec.ind[4] <- vec.ind[4] + 1
244c373f2a34 Uploaded
greg
parents:
diff changeset
408 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
409 vec.mat[i,] <- vec.ind
244c373f2a34 Uploaded
greg
parents:
diff changeset
410 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
411
244c373f2a34 Uploaded
greg
parents:
diff changeset
412 # event 3.2 young nymph to old nymph (vec.ind[2]=1 -> young nymph: determines diapause)
244c373f2a34 Uploaded
greg
parents:
diff changeset
413 if (vec.ind[2] == 1) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
414 # young nymph stage
244c373f2a34 Uploaded
greg
parents:
diff changeset
415 # add to DD
244c373f2a34 Uploaded
greg
parents:
diff changeset
416 vec.ind[3] <- vec.ind[3] + dd.temp
244c373f2a34 Uploaded
greg
parents:
diff changeset
417 if (vec.ind[3] >= (250 + opt$old_nymph_accum)) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
418 # from young to old nymph, DD requirement met
244c373f2a34 Uploaded
greg
parents:
diff changeset
419 current.gen <- vec.ind[1]
244c373f2a34 Uploaded
greg
parents:
diff changeset
420 # transfer to old nym stage
244c373f2a34 Uploaded
greg
parents:
diff changeset
421 vec.ind <- c(current.gen, 2, 0, 0, 0)
244c373f2a34 Uploaded
greg
parents:
diff changeset
422 if (photoperiod < opt$photoperiod && day > 180) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
423 vec.ind[5] <- 1
244c373f2a34 Uploaded
greg
parents:
diff changeset
424 } # prepare for diapausing
244c373f2a34 Uploaded
greg
parents:
diff changeset
425 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
426 else {
244c373f2a34 Uploaded
greg
parents:
diff changeset
427 # add 1 day in current stage
244c373f2a34 Uploaded
greg
parents:
diff changeset
428 vec.ind[4] <- vec.ind[4] + 1
244c373f2a34 Uploaded
greg
parents:
diff changeset
429 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
430 vec.mat[i,] <- vec.ind
244c373f2a34 Uploaded
greg
parents:
diff changeset
431 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
432
244c373f2a34 Uploaded
greg
parents:
diff changeset
433 # event 3.3 old nymph to adult: previttelogenic or diapausing?
244c373f2a34 Uploaded
greg
parents:
diff changeset
434 if (vec.ind[2] == 2) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
435 # old nymph stage
244c373f2a34 Uploaded
greg
parents:
diff changeset
436 # add to DD
244c373f2a34 Uploaded
greg
parents:
diff changeset
437 vec.ind[3] <- vec.ind[3] + dd.temp
244c373f2a34 Uploaded
greg
parents:
diff changeset
438 if (vec.ind[3] >= (200 + opt$adult_accum)) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
439 # from old to adult, DD requirement met
244c373f2a34 Uploaded
greg
parents:
diff changeset
440 current.gen <- vec.ind[1]
244c373f2a34 Uploaded
greg
parents:
diff changeset
441 if (vec.ind[5] == 0) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
442 # non-diapausing adult -- previttelogenic
244c373f2a34 Uploaded
greg
parents:
diff changeset
443 vec.ind <- c(current.gen, 3, 0, 0, 0)
244c373f2a34 Uploaded
greg
parents:
diff changeset
444 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
445 else {
244c373f2a34 Uploaded
greg
parents:
diff changeset
446 # diapausing
244c373f2a34 Uploaded
greg
parents:
diff changeset
447 vec.ind <- c(current.gen, 5, 0, 0, 1)
244c373f2a34 Uploaded
greg
parents:
diff changeset
448 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
449 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
450 else {
244c373f2a34 Uploaded
greg
parents:
diff changeset
451 # add 1 day in current stage
244c373f2a34 Uploaded
greg
parents:
diff changeset
452 vec.ind[4] <- vec.ind[4] + 1
244c373f2a34 Uploaded
greg
parents:
diff changeset
453 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
454 vec.mat[i,] <- vec.ind
244c373f2a34 Uploaded
greg
parents:
diff changeset
455 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
456
244c373f2a34 Uploaded
greg
parents:
diff changeset
457 # event 4 growing of diapausing adult (unimportant, but still necessary)##
244c373f2a34 Uploaded
greg
parents:
diff changeset
458 if (vec.ind[2] == 5) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
459 vec.ind[3] <- vec.ind[3] + dd.temp
244c373f2a34 Uploaded
greg
parents:
diff changeset
460 vec.ind[4] <- vec.ind[4] + 1
244c373f2a34 Uploaded
greg
parents:
diff changeset
461 vec.mat[i,] <- vec.ind
244c373f2a34 Uploaded
greg
parents:
diff changeset
462 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
463 } # else if it is still alive
244c373f2a34 Uploaded
greg
parents:
diff changeset
464 } # end of the individual bug loop
244c373f2a34 Uploaded
greg
parents:
diff changeset
465
244c373f2a34 Uploaded
greg
parents:
diff changeset
466 # find how many died
244c373f2a34 Uploaded
greg
parents:
diff changeset
467 n.death <- length(death.vec)
244c373f2a34 Uploaded
greg
parents:
diff changeset
468 if (n.death > 0) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
469 vec.mat <- vec.mat[-death.vec, ]
244c373f2a34 Uploaded
greg
parents:
diff changeset
470 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
471 # remove record of dead
244c373f2a34 Uploaded
greg
parents:
diff changeset
472 # find how many new born
244c373f2a34 Uploaded
greg
parents:
diff changeset
473 n.newborn <- length(birth.vec[,1])
244c373f2a34 Uploaded
greg
parents:
diff changeset
474 vec.mat <- rbind(vec.mat, birth.vec)
244c373f2a34 Uploaded
greg
parents:
diff changeset
475 # update population size for the next day
244c373f2a34 Uploaded
greg
parents:
diff changeset
476 n <- n - n.death + n.newborn
244c373f2a34 Uploaded
greg
parents:
diff changeset
477
244c373f2a34 Uploaded
greg
parents:
diff changeset
478 # aggregate results by day
244c373f2a34 Uploaded
greg
parents:
diff changeset
479 tot.pop <- c(tot.pop, n)
244c373f2a34 Uploaded
greg
parents:
diff changeset
480 # egg
244c373f2a34 Uploaded
greg
parents:
diff changeset
481 s0 <- sum(vec.mat[,2] == 0)
244c373f2a34 Uploaded
greg
parents:
diff changeset
482 # young nymph
244c373f2a34 Uploaded
greg
parents:
diff changeset
483 s1 <- sum(vec.mat[,2] == 1)
244c373f2a34 Uploaded
greg
parents:
diff changeset
484 # old nymph
244c373f2a34 Uploaded
greg
parents:
diff changeset
485 s2 <- sum(vec.mat[,2] == 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
486 # previtellogenic
244c373f2a34 Uploaded
greg
parents:
diff changeset
487 s3 <- sum(vec.mat[,2] == 3)
244c373f2a34 Uploaded
greg
parents:
diff changeset
488 # vitellogenic
244c373f2a34 Uploaded
greg
parents:
diff changeset
489 s4 <- sum(vec.mat[,2] == 4)
244c373f2a34 Uploaded
greg
parents:
diff changeset
490 # diapausing
244c373f2a34 Uploaded
greg
parents:
diff changeset
491 s5 <- sum(vec.mat[,2] == 5)
244c373f2a34 Uploaded
greg
parents:
diff changeset
492 # overwintering adult
244c373f2a34 Uploaded
greg
parents:
diff changeset
493 gen0 <- sum(vec.mat[,1] == 0)
244c373f2a34 Uploaded
greg
parents:
diff changeset
494 # first generation
244c373f2a34 Uploaded
greg
parents:
diff changeset
495 gen1 <- sum(vec.mat[,1] == 1)
244c373f2a34 Uploaded
greg
parents:
diff changeset
496 # second generation
244c373f2a34 Uploaded
greg
parents:
diff changeset
497 gen2 <- sum(vec.mat[,1] == 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
498 # sum of all adults
244c373f2a34 Uploaded
greg
parents:
diff changeset
499 n.adult <- sum(vec.mat[,2] == 3) + sum(vec.mat[,2] == 4) + sum(vec.mat[,2] == 5)
244c373f2a34 Uploaded
greg
parents:
diff changeset
500 # gen.0 pop size
244c373f2a34 Uploaded
greg
parents:
diff changeset
501 gen0.pop[day] <- gen0
244c373f2a34 Uploaded
greg
parents:
diff changeset
502 gen1.pop[day] <- gen1
244c373f2a34 Uploaded
greg
parents:
diff changeset
503 gen2.pop[day] <- gen2
244c373f2a34 Uploaded
greg
parents:
diff changeset
504 S0[day] <- s0
244c373f2a34 Uploaded
greg
parents:
diff changeset
505 S1[day] <- s1
244c373f2a34 Uploaded
greg
parents:
diff changeset
506 S2[day] <- s2
244c373f2a34 Uploaded
greg
parents:
diff changeset
507 S3[day] <- s3
244c373f2a34 Uploaded
greg
parents:
diff changeset
508 S4[day] <- s4
244c373f2a34 Uploaded
greg
parents:
diff changeset
509 S5[day] <- s5
244c373f2a34 Uploaded
greg
parents:
diff changeset
510 g0.adult[day] <- sum(vec.mat[,1] == 0)
244c373f2a34 Uploaded
greg
parents:
diff changeset
511 g1.adult[day] <- sum((vec.mat[,1] == 1 & vec.mat[,2] == 3) | (vec.mat[,1] == 1 & vec.mat[,2] == 4) | (vec.mat[,1] == 1 & vec.mat[,2] == 5))
244c373f2a34 Uploaded
greg
parents:
diff changeset
512 g2.adult[day] <- sum((vec.mat[,1]== 2 & vec.mat[,2] == 3) | (vec.mat[,1] == 2 & vec.mat[,2] == 4) | (vec.mat[,1] == 2 & vec.mat[,2] == 5))
244c373f2a34 Uploaded
greg
parents:
diff changeset
513
244c373f2a34 Uploaded
greg
parents:
diff changeset
514 N.newborn[day] <- n.newborn
244c373f2a34 Uploaded
greg
parents:
diff changeset
515 N.death[day] <- n.death
244c373f2a34 Uploaded
greg
parents:
diff changeset
516 N.adult[day] <- n.adult
244c373f2a34 Uploaded
greg
parents:
diff changeset
517 #print(c(N.rep, day, n, n.adult))
244c373f2a34 Uploaded
greg
parents:
diff changeset
518 } # end of 365 days
244c373f2a34 Uploaded
greg
parents:
diff changeset
519
244c373f2a34 Uploaded
greg
parents:
diff changeset
520 dd.cum <- cumsum(dd.day)
244c373f2a34 Uploaded
greg
parents:
diff changeset
521 # collect all the outputs
244c373f2a34 Uploaded
greg
parents:
diff changeset
522 S0.rep[,N.rep] <- S0
244c373f2a34 Uploaded
greg
parents:
diff changeset
523 S1.rep[,N.rep] <- S1
244c373f2a34 Uploaded
greg
parents:
diff changeset
524 S2.rep[,N.rep] <- S2
244c373f2a34 Uploaded
greg
parents:
diff changeset
525 S3.rep[,N.rep] <- S3
244c373f2a34 Uploaded
greg
parents:
diff changeset
526 S4.rep[,N.rep] <- S4
244c373f2a34 Uploaded
greg
parents:
diff changeset
527 S5.rep[,N.rep] <- S5
244c373f2a34 Uploaded
greg
parents:
diff changeset
528 newborn.rep[,N.rep] <- N.newborn
244c373f2a34 Uploaded
greg
parents:
diff changeset
529 death.rep[,N.rep] <- N.death
244c373f2a34 Uploaded
greg
parents:
diff changeset
530 adult.rep[,N.rep] <- N.adult
244c373f2a34 Uploaded
greg
parents:
diff changeset
531 pop.rep[,N.rep] <- tot.pop
244c373f2a34 Uploaded
greg
parents:
diff changeset
532 g0.rep[,N.rep] <- gen0.pop
244c373f2a34 Uploaded
greg
parents:
diff changeset
533 g1.rep[,N.rep] <- gen1.pop
244c373f2a34 Uploaded
greg
parents:
diff changeset
534 g2.rep[,N.rep] <- gen2.pop
244c373f2a34 Uploaded
greg
parents:
diff changeset
535 g0a.rep[,N.rep] <- g0.adult
244c373f2a34 Uploaded
greg
parents:
diff changeset
536 g1a.rep[,N.rep] <- g1.adult
244c373f2a34 Uploaded
greg
parents:
diff changeset
537 g2a.rep[,N.rep] <- g2.adult
244c373f2a34 Uploaded
greg
parents:
diff changeset
538 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
539
244c373f2a34 Uploaded
greg
parents:
diff changeset
540 # save(dd.day, dd.cum, S0.rep, S1.rep, S2.rep, S3.rep, S4.rep, S5.rep, newborn.rep, death.rep, adult.rep, pop.rep, g0.rep, g1.rep, g2.rep, g0a.rep, g1a.rep, g2a.rep, file=opt$output)
244c373f2a34 Uploaded
greg
parents:
diff changeset
541 # maybe do not need to export this bit, but for now just leave it as-is
244c373f2a34 Uploaded
greg
parents:
diff changeset
542 # do we need to export this Rdat file?
244c373f2a34 Uploaded
greg
parents:
diff changeset
543
244c373f2a34 Uploaded
greg
parents:
diff changeset
544 # Data analysis and visualization
244c373f2a34 Uploaded
greg
parents:
diff changeset
545 # default: plot 1 year of result
244c373f2a34 Uploaded
greg
parents:
diff changeset
546 # but can be expanded to accommodate multiple years
244c373f2a34 Uploaded
greg
parents:
diff changeset
547 n.yr <- 1
244c373f2a34 Uploaded
greg
parents:
diff changeset
548 day.all <- c(1:365 * n.yr)
244c373f2a34 Uploaded
greg
parents:
diff changeset
549
244c373f2a34 Uploaded
greg
parents:
diff changeset
550 # mean value for adults
244c373f2a34 Uploaded
greg
parents:
diff changeset
551 sa <- apply((S3.rep + S4.rep + S5.rep), 1, mean)
244c373f2a34 Uploaded
greg
parents:
diff changeset
552 # mean value for nymphs
244c373f2a34 Uploaded
greg
parents:
diff changeset
553 sn <- apply((S1.rep + S2.rep), 1,mean)
244c373f2a34 Uploaded
greg
parents:
diff changeset
554 # mean value for eggs
244c373f2a34 Uploaded
greg
parents:
diff changeset
555 se <- apply(S0.rep, 1, mean)
244c373f2a34 Uploaded
greg
parents:
diff changeset
556 # mean value for P
244c373f2a34 Uploaded
greg
parents:
diff changeset
557 g0 <- apply(g0.rep, 1, mean)
244c373f2a34 Uploaded
greg
parents:
diff changeset
558 # mean value for F1
244c373f2a34 Uploaded
greg
parents:
diff changeset
559 g1 <- apply(g1.rep, 1, mean)
244c373f2a34 Uploaded
greg
parents:
diff changeset
560 # mean value for F2
244c373f2a34 Uploaded
greg
parents:
diff changeset
561 g2 <- apply(g2.rep, 1, mean)
244c373f2a34 Uploaded
greg
parents:
diff changeset
562 # mean value for P adult
244c373f2a34 Uploaded
greg
parents:
diff changeset
563 g0a <- apply(g0a.rep, 1, mean)
244c373f2a34 Uploaded
greg
parents:
diff changeset
564 # mean value for F1 adult
244c373f2a34 Uploaded
greg
parents:
diff changeset
565 g1a <- apply(g1a.rep, 1, mean)
244c373f2a34 Uploaded
greg
parents:
diff changeset
566 # mean value for F2 adult
244c373f2a34 Uploaded
greg
parents:
diff changeset
567 g2a <- apply(g2a.rep, 1, mean)
244c373f2a34 Uploaded
greg
parents:
diff changeset
568
244c373f2a34 Uploaded
greg
parents:
diff changeset
569 # SE for adults
244c373f2a34 Uploaded
greg
parents:
diff changeset
570 sa.se <- apply((S3.rep + S4.rep + S5.rep), 1, sd) / sqrt(opt$replications)
244c373f2a34 Uploaded
greg
parents:
diff changeset
571 # SE for nymphs
244c373f2a34 Uploaded
greg
parents:
diff changeset
572 sn.se <- apply((S1.rep + S2.rep) / sqrt(opt$replications), 1, sd)
244c373f2a34 Uploaded
greg
parents:
diff changeset
573 # SE for eggs
244c373f2a34 Uploaded
greg
parents:
diff changeset
574 se.se <- apply(S0.rep, 1, sd) / sqrt(opt$replications)
244c373f2a34 Uploaded
greg
parents:
diff changeset
575 # SE value for P
244c373f2a34 Uploaded
greg
parents:
diff changeset
576 g0.se <- apply(g0.rep, 1, sd) / sqrt(opt$replications)
244c373f2a34 Uploaded
greg
parents:
diff changeset
577 # SE for F1
244c373f2a34 Uploaded
greg
parents:
diff changeset
578 g1.se <- apply(g1.rep, 1, sd) / sqrt(opt$replications)
244c373f2a34 Uploaded
greg
parents:
diff changeset
579 # SE for F2
244c373f2a34 Uploaded
greg
parents:
diff changeset
580 g2.se <- apply(g2.rep, 1, sd) / sqrt(opt$replications)
244c373f2a34 Uploaded
greg
parents:
diff changeset
581 # SE for P adult
244c373f2a34 Uploaded
greg
parents:
diff changeset
582 g0a.se <- apply(g0a.rep, 1, sd) / sqrt(opt$replications)
244c373f2a34 Uploaded
greg
parents:
diff changeset
583 # SE for F1 adult
244c373f2a34 Uploaded
greg
parents:
diff changeset
584 g1a.se <- apply(g1a.rep, 1, sd) / sqrt(opt$replications)
244c373f2a34 Uploaded
greg
parents:
diff changeset
585 # SE for F2 adult
244c373f2a34 Uploaded
greg
parents:
diff changeset
586 g2a.se <- apply(g2a.rep, 1, sd) / sqrt(opt$replications)
244c373f2a34 Uploaded
greg
parents:
diff changeset
587
244c373f2a34 Uploaded
greg
parents:
diff changeset
588 dev.new(width=20, height=20)
244c373f2a34 Uploaded
greg
parents:
diff changeset
589
244c373f2a34 Uploaded
greg
parents:
diff changeset
590 # Start PDF device driver to save charts to output.
244c373f2a34 Uploaded
greg
parents:
diff changeset
591 pdf(file=opt$output, height=20, width=20, bg="white")
244c373f2a34 Uploaded
greg
parents:
diff changeset
592
244c373f2a34 Uploaded
greg
parents:
diff changeset
593 par(mar = c(5, 6, 4, 4), mfrow=c(3, 1))
244c373f2a34 Uploaded
greg
parents:
diff changeset
594
244c373f2a34 Uploaded
greg
parents:
diff changeset
595 # Subfigure 2: population size by life stage
244c373f2a34 Uploaded
greg
parents:
diff changeset
596 plot(day.all, sa, main = "BSMB Total Population Size by Life Stage", type = "l", ylim = c(0, max(se + se.se, sn + sn.se, sa + sa.se)), axes = F, lwd = 2, xlab = "", ylab = "Number", cex = 2, cex.lab = 2, cex.axis = 2, cex.main = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
597 # Young and old nymphs
244c373f2a34 Uploaded
greg
parents:
diff changeset
598 lines(day.all, sn, lwd = 2, lty = 1, col = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
599 # Eggs
244c373f2a34 Uploaded
greg
parents:
diff changeset
600 lines(day.all, se, lwd = 2, lty = 1, col = 4)
244c373f2a34 Uploaded
greg
parents:
diff changeset
601 axis(1, at = c(1:12) * 30 - 15, cex.axis = 2, labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
244c373f2a34 Uploaded
greg
parents:
diff changeset
602 axis(2, cex.axis = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
603 leg.text <- c("Egg", "Nymph", "Adult")
244c373f2a34 Uploaded
greg
parents:
diff changeset
604 legend("topleft", leg.text, lty = c(1, 1, 1), col = c(4, 2, 1), cex = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
605 if (opt$se_plot == 1) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
606 # add SE lines to plot
244c373f2a34 Uploaded
greg
parents:
diff changeset
607 # SE for adults
244c373f2a34 Uploaded
greg
parents:
diff changeset
608 lines (day.all, sa + sa.se, lty = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
609 lines (day.all, sa - sa.se, lty = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
610 # SE for nymphs
244c373f2a34 Uploaded
greg
parents:
diff changeset
611 lines (day.all, sn + sn.se, col = 2, lty = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
612 lines (day.all, sn - sn.se, col = 2, lty = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
613 # SE for eggs
244c373f2a34 Uploaded
greg
parents:
diff changeset
614 lines (day.all, se + se.se, col = 4, lty = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
615 lines (day.all, se - se.se, col = 4, lty = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
616 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
617
244c373f2a34 Uploaded
greg
parents:
diff changeset
618 # Subfigure 3: population size by generation
244c373f2a34 Uploaded
greg
parents:
diff changeset
619 plot(day.all, g0, main = "BSMB Total Population Size by Generation", type = "l", ylim = c(0, max(g2)), axes = F, lwd = 2, xlab = "", ylab = "Number", cex = 2, cex.lab = 2, cex.axis = 2, cex.main = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
620 lines(day.all, g1, lwd = 2, lty = 1, col = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
621 lines(day.all, g2, lwd = 2, lty = 1, col = 4)
244c373f2a34 Uploaded
greg
parents:
diff changeset
622 axis(1, at = c(1:12) * 30 - 15, cex.axis = 2, labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
244c373f2a34 Uploaded
greg
parents:
diff changeset
623 axis(2, cex.axis = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
624 leg.text <- c("P", "F1", "F2")
244c373f2a34 Uploaded
greg
parents:
diff changeset
625 legend("topleft", leg.text, lty = c(1, 1, 1), col =c(1, 2, 4), cex = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
626 if (opt$se_plot == 1) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
627 # add SE lines to plot
244c373f2a34 Uploaded
greg
parents:
diff changeset
628 # SE for adults
244c373f2a34 Uploaded
greg
parents:
diff changeset
629 lines (day.all, g0 + g0.se, lty = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
630 lines (day.all, g0 - g0.se, lty = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
631 # SE for nymphs
244c373f2a34 Uploaded
greg
parents:
diff changeset
632 lines (day.all, g1 + g1.se, col = 2, lty = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
633 lines (day.all, g1 - g1.se, col = 2, lty = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
634 # SE for eggs
244c373f2a34 Uploaded
greg
parents:
diff changeset
635 lines (day.all, g2 + g2.se, col = 4, lty = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
636 lines (day.all, g2 - g2.se, col = 4, lty = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
637 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
638
244c373f2a34 Uploaded
greg
parents:
diff changeset
639 # Subfigure 4: adult population size by generation
244c373f2a34 Uploaded
greg
parents:
diff changeset
640 plot(day.all, g0a, ylim = c(0, max(g2a) + 100), main = "BSMB Adult Population Size by Generation", type = "l", axes = F, lwd = 2, xlab = "Year", ylab = "Number", cex = 2, cex.lab = 2, cex.axis = 2, cex.main = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
641 lines(day.all, g1a, lwd = 2, lty = 1, col = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
642 lines(day.all, g2a, lwd = 2, lty = 1, col = 4)
244c373f2a34 Uploaded
greg
parents:
diff changeset
643 axis(1, at = c(1:12) * 30 - 15, cex.axis = 2, labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
244c373f2a34 Uploaded
greg
parents:
diff changeset
644 axis(2, cex.axis = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
645 leg.text <- c("P", "F1", "F2")
244c373f2a34 Uploaded
greg
parents:
diff changeset
646 legend("topleft", leg.text, lty = c(1, 1, 1), col = c(1, 2, 4), cex = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
647 if (opt$se_plot == 1) {
244c373f2a34 Uploaded
greg
parents:
diff changeset
648 # add SE lines to plot
244c373f2a34 Uploaded
greg
parents:
diff changeset
649 # SE for adults
244c373f2a34 Uploaded
greg
parents:
diff changeset
650 lines (day.all, g0a + g0a.se, lty = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
651 lines (day.all, g0a - g0a.se, lty = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
652 # SE for nymphs
244c373f2a34 Uploaded
greg
parents:
diff changeset
653 lines (day.all, g1a + g1a.se, col = 2, lty = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
654 lines (day.all, g1a - g1a.se, col = 2, lty = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
655 # SE for eggs
244c373f2a34 Uploaded
greg
parents:
diff changeset
656 lines (day.all, g2a + g2a.se, col = 4, lty = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
657 lines (day.all, g2a - g2a.se, col = 4, lty = 2)
244c373f2a34 Uploaded
greg
parents:
diff changeset
658 }
244c373f2a34 Uploaded
greg
parents:
diff changeset
659
244c373f2a34 Uploaded
greg
parents:
diff changeset
660 # Turn off device driver to flush output.
244c373f2a34 Uploaded
greg
parents:
diff changeset
661 dev.off()