Mercurial > repos > greg > insect_phenology_model
comparison insect_phenology_model.R @ 26:3c06cab3db2c draft
Uploaded
author | greg |
---|---|
date | Thu, 09 Nov 2017 11:30:27 -0500 |
parents | 5354a82ed295 |
children | 24949e72f7ec |
comparison
equal
deleted
inserted
replaced
25:3852133fb058 | 26:3c06cab3db2c |
---|---|
23 | 23 |
24 parser <- OptionParser(usage="%prog [options] file", option_list=option_list) | 24 parser <- OptionParser(usage="%prog [options] file", option_list=option_list) |
25 args <- parse_args(parser, positional_arguments=TRUE) | 25 args <- parse_args(parser, positional_arguments=TRUE) |
26 opt <- args$options | 26 opt <- args$options |
27 | 27 |
28 get_temperature_file_path=function(loc, temperature_data) | 28 convert_csv_to_rdata=function(loc, temperature_data) |
29 { | 29 { |
30 expdata <- matrix(rep(0, opt$num_days * 3), nrow=opt$num_days) | 30 expdata <- matrix(rep(0, opt$num_days * 6), nrow=opt$num_days) |
31 expdata[,1] <- c(1:opt$num_days) | 31 expdata[,1] <- c(1:opt$num_days) |
32 # Minimum | 32 # Minimum |
33 expdata[,2] <- temperature_data[c(1:opt$num_days), 3] | 33 expdata[,2] <- temperature_data[c(1:opt$num_days), 5] |
34 # Maximum | 34 # Maximum |
35 expdata[,3] <- temperature_data[c(1:opt$num_days), 2] | 35 expdata[,3] <- temperature_data[c(1:opt$num_days), 6] |
36 date <- temperature_data[1, 3] | 36 namedat <- "tempdata.Rdat" |
37 year <- substr(date, 1, 4) | |
38 namedat <- paste(loc, year, ".Rdat", sep="") | |
39 save(expdata, file=namedat) | 37 save(expdata, file=namedat) |
40 namedat | 38 namedat |
41 } | 39 } |
42 | 40 |
43 daylength=function(latitude, num_days) | 41 daylength=function(latitude, num_days) |
44 { | 42 { |
45 # from Forsythe 1995 | 43 # From Forsythe 1995. |
46 p=0.8333 | 44 p=0.8333 |
47 dl <- NULL | 45 dl <- NULL |
48 for (i in 1:num_days) { | 46 for (i in 1:num_days) { |
49 theta <- 0.2163108 + 2 * atan(0.9671396 * tan(0.00860 * (i - 186))) | 47 theta <- 0.2163108 + 2 * atan(0.9671396 * tan(0.00860 * (i - 186))) |
50 phi <- asin(0.39795 * cos(theta)) | 48 phi <- asin(0.39795 * cos(theta)) |
51 dl[i] <- 24 - 24 / pi * acos((sin(p * pi / 180) + sin(latitude * pi / 180) * sin(phi)) / (cos(latitude * pi / 180) * cos(phi))) | 49 dl[i] <- 24 - 24 / pi * acos((sin(p * pi / 180) + sin(latitude * pi / 180) * sin(phi)) / (cos(latitude * pi / 180) * cos(phi))) |
52 } | 50 } |
53 # return a vector of daylength for the number of | 51 # Return a vector of daylength for the number of |
54 # days specifie din the input temperature data | 52 # days specified in the input temperature data. |
55 dl | 53 dl |
56 } | 54 } |
57 | 55 |
58 hourtemp=function(latitude, date, temperature_file_path, num_days) | 56 hourtemp=function(latitude, date, temperature_file_path, num_days) |
59 { | 57 { |
60 load(temperature_file_path) | 58 load(temperature_file_path) |
61 # base development threshold for BMSB | 59 # Base development threshold for Brown Marmolated Stink Bug |
60 # insect phenology model. | |
62 threshold <- 14.17 | 61 threshold <- 14.17 |
63 dnp <- expdata[date, 2] # daily minimum | 62 dnp <- expdata[date, 2] # daily minimum |
64 dxp <- expdata[date, 3] # daily maximum | 63 dxp <- expdata[date, 3] # daily maximum |
65 dmean <- 0.5 * (dnp + dxp) | 64 dmean <- 0.5 * (dnp + dxp) |
66 dd <- 0 # initialize degree day accumulation | 65 dd <- 0 # initialize degree day accumulation |
67 | 66 |
68 if (dxp<threshold) { | 67 if (dxp<threshold) { |
69 dd <- 0 | 68 dd <- 0 |
70 } | 69 } |
71 else { | 70 else { |
72 # extract daylength data for the number of | 71 # Extract daylength data for the number of |
73 # days specified in the input temperature data | 72 # days specified in the input temperature data. |
74 dlprofile <- daylength(latitude, num_days) | 73 dlprofile <- daylength(latitude, num_days) |
75 T <- NULL # initialize hourly temperature | 74 # Initialize hourly temperature. |
76 dh <- NULL #initialize degree hour vector | 75 T <- NULL |
77 # calculate daylength in given date | 76 # Initialize degree hour vector. |
77 dh <- NULL | |
78 # Calculate daylength in given date. | |
78 y <- dlprofile[date] | 79 y <- dlprofile[date] |
79 # night length | 80 # Night length. |
80 z <- 24 - y | 81 z <- 24 - y |
81 # lag coefficient | 82 # Lag coefficient. |
82 a <- 1.86 | 83 a <- 1.86 |
83 # night coefficient | 84 # Night coefficient. |
84 b <- 2.20 | 85 b <- 2.20 |
85 # sunrise time | 86 # Sunrise time. |
86 risetime <- 12 - y / 2 | 87 risetime <- 12 - y / 2 |
87 # sunset time | 88 # Sunset time. |
88 settime <- 12 + y / 2 | 89 settime <- 12 + y / 2 |
89 ts <- (dxp - dnp) * sin(pi * (settime - 5) / (y + 2 * a)) + dnp | 90 ts <- (dxp - dnp) * sin(pi * (settime - 5) / (y + 2 * a)) + dnp |
90 for (i in 1:24) { | 91 for (i in 1:24) { |
91 if (i > risetime && i<settime) { | 92 if (i > risetime && i<settime) { |
92 # number of hours after Tmin until sunset | 93 # Number of hours after Tmin until sunset. |
93 m <- i - 5 | 94 m <- i - 5 |
94 T[i]=(dxp - dnp) * sin(pi * m / (y + 2 * a)) + dnp | 95 T[i]=(dxp - dnp) * sin(pi * m / (y + 2 * a)) + dnp |
95 if (T[i]<8.4) { | 96 if (T[i]<8.4) { |
96 dh[i] <- 0 | 97 dh[i] <- 0 |
97 } | 98 } |
195 } | 196 } |
196 return = mort.prob | 197 return = mort.prob |
197 return | 198 return |
198 } | 199 } |
199 | 200 |
200 cat("Replications: ", opt$replications, "\n") | |
201 cat("Photoperiod: ", opt$photoperiod, "\n") | |
202 cat("Oviposition rate: ", opt$oviposition, "\n") | |
203 cat("Egg mortality rate: ", opt$egg_mort, "\n") | |
204 cat("Nymph mortality rate: ", opt$nymph_mort, "\n") | |
205 cat("Adult mortality rate: ", opt$adult_mort, "\n") | |
206 cat("Min clutch size: ", opt$min_clutch_size, "\n") | |
207 cat("Max clutch size: ", opt$max_clutch_size, "\n") | |
208 cat("(egg->young nymph): ", opt$young_nymph_accum, "\n") | |
209 cat("(young nymph->old nymph): ", opt$old_nymph_accum, "\n") | |
210 cat("(old nymph->adult): ", opt$adult_accum, "\n") | |
211 | |
212 # Read in the input temperature datafile into a Data Frame object. | 201 # Read in the input temperature datafile into a Data Frame object. |
213 temperature_data <- read.csv(file=opt$input, header=T, sep=",") | 202 temperature_data <- read.csv(file=opt$input, header=T, sep=",") |
214 temperature_file_path <- get_temperature_file_path(opt$location, temperature_data) | 203 temperature_file_path <- convert_csv_to_rdata(opt$location, temperature_data) |
215 latitude <- temperature_data[1, 1] | 204 latitude <- temperature_data[1, 1] |
205 | |
206 cat("Number of days: ", opt$num_days, ", ") | |
207 cat("Latitude: ", latitude, "\n") | |
208 cat("Replications: ", opt$replications, ", ") | |
209 cat("Photoperiod: ", opt$photoperiod, "\n") | |
210 cat("Oviposition rate: ", opt$oviposition, ", ") | |
211 cat("Egg mortality rate: ", opt$egg_mort, "\n") | |
212 cat("Nymph mortality rate: ", opt$nymph_mort, ", ") | |
213 cat("Adult mortality rate: ", opt$adult_mort, "\n") | |
214 cat("Min clutch size: ", opt$min_clutch_size, ", ") | |
215 cat("Max clutch size: ", opt$max_clutch_size, "\n") | |
216 cat("(egg->young nymph): ", opt$young_nymph_accum, ", ") | |
217 cat("(young nymph->old nymph): ", opt$old_nymph_accum, "\n") | |
218 cat("(old nymph->adult): ", opt$adult_accum | |
216 | 219 |
217 # Initialize matrix for results from all replications | 220 # Initialize matrix for results from all replications |
218 S0.rep <- S1.rep <- S2.rep <- S3.rep <- S4.rep <- S5.rep <- matrix(rep(0, opt$num_days * opt$replications), ncol = opt$replications) | 221 S0.rep <- S1.rep <- S2.rep <- S3.rep <- S4.rep <- S5.rep <- matrix(rep(0, opt$num_days * opt$replications), ncol = opt$replications) |
219 newborn.rep <- death.rep <- adult.rep <- pop.rep <- g0.rep <- g1.rep <- g2.rep <- g0a.rep <- g1a.rep <- g2a.rep <- matrix(rep(0, opt$num_days * opt$replications), ncol=opt$replications) | 222 newborn.rep <- death.rep <- adult.rep <- pop.rep <- g0.rep <- g1.rep <- g2.rep <- g0a.rep <- g1a.rep <- g2a.rep <- matrix(rep(0, opt$num_days * opt$replications), ncol=opt$replications) |
220 | 223 |
221 # loop through replications | 224 # loop through replications |
222 for (N.rep in 1:opt$replications) { | 225 for (N.rep in 1:opt$replications) { |
223 # during each replication | 226 # During each replication start with 1000 individuals. |
224 # start with 1000 individuals -- user definable as well? | 227 # TODO: user definable as well? |
225 n <- 1000 | 228 n <- 1000 |
226 # Generation, Stage, DD, T, Diapause | 229 # Generation, Stage, DD, T, Diapause. |
227 vec.ini <- c(0, 3, 0, 0, 0) | 230 vec.ini <- c(0, 3, 0, 0, 0) |
228 # overwintering, previttelogenic, DD=0, T=0, no-diapause | 231 # Overwintering, previttelogenic, DD=0, T=0, no-diapause. |
229 vec.mat <- rep(vec.ini, n) | 232 vec.mat <- rep(vec.ini, n) |
230 # complete matrix for the population | 233 # Complete matrix for the population. |
231 vec.mat <- t(matrix(vec.mat, nrow=5)) | 234 vec.mat <- base::t(matrix(vec.mat, nrow=5)) |
232 # complete photoperiod profile in a year, requires daylength function | 235 # Complete photoperiod profile in a year, requires daylength function. |
233 ph.p <- daylength(latitude, opt$num_days) | 236 ph.p <- daylength(latitude, opt$num_days) |
234 | 237 |
235 # time series of population size | 238 # Time series of population size. |
236 tot.pop <- NULL | 239 tot.pop <- NULL |
237 # gen.0 pop size | |
238 gen0.pop <- rep(0, opt$num_days) | 240 gen0.pop <- rep(0, opt$num_days) |
239 gen1.pop <- rep(0, opt$num_days) | 241 gen1.pop <- rep(0, opt$num_days) |
240 gen2.pop <- rep(0, opt$num_days) | 242 gen2.pop <- rep(0, opt$num_days) |
241 S0 <- S1 <- S2 <- S3 <- S4 <- S5 <- rep(0, opt$num_days) | 243 S0 <- S1 <- S2 <- S3 <- S4 <- S5 <- rep(0, opt$num_days) |
242 g0.adult <- g1.adult <- g2.adult <- rep(0, opt$num_days) | 244 g0.adult <- g1.adult <- g2.adult <- rep(0, opt$num_days) |
243 N.newborn <- N.death <- N.adult <- rep(0, opt$num_days) | 245 N.newborn <- N.death <- N.adult <- rep(0, opt$num_days) |
244 dd.day <- rep(0, opt$num_days) | 246 dd.day <- rep(0, opt$num_days) |
245 | 247 |
246 # start tick | |
247 ptm <- proc.time() | |
248 | |
249 # All the days included in the input temperature dataset. | 248 # All the days included in the input temperature dataset. |
250 for (day in 1:opt$num_days) { | 249 for (day in 1:opt$num_days) { |
251 # photoperiod in the day | 250 # Photoperiod in the day. |
252 photoperiod <- ph.p[day] | 251 photoperiod <- ph.p[day] |
253 temp.profile <- hourtemp(latitude, day, temperature_file_path, opt$num_days) | 252 temp.profile <- hourtemp(latitude, day, temperature_file_path, opt$num_days) |
254 mean.temp <- temp.profile[1] | 253 mean.temp <- temp.profile[1] |
255 dd.temp <- temp.profile[2] | 254 dd.temp <- temp.profile[2] |
256 dd.day[day] <- dd.temp | 255 dd.day[day] <- dd.temp |
257 # trash bin for death | 256 # Trash bin for death. |
258 death.vec <- NULL | 257 death.vec <- NULL |
259 # new born | 258 # Newborn. |
260 birth.vec <- NULL | 259 birth.vec <- NULL |
261 | 260 |
262 # all individuals | 261 # All individuals. |
263 for (i in 1:n) { | 262 for (i in 1:n) { |
264 # find individual record | 263 # Find individual record. |
265 vec.ind <- vec.mat[i,] | 264 vec.ind <- vec.mat[i,] |
266 # first of all, still alive? | 265 # First of all, still alive? |
267 # adjustment for late season mortality rate | 266 # Adjustment for late season mortality rate. |
268 if (latitude < 40.0) { | 267 if (latitude < 40.0) { |
269 post.mort <- 1 | 268 post.mort <- 1 |
270 day.kill <- 300 | 269 day.kill <- 300 |
271 } | 270 } |
272 else { | 271 else { |
273 post.mort <- 2 | 272 post.mort <- 2 |
274 day.kill <- 250 | 273 day.kill <- 250 |
275 } | 274 } |
276 if (vec.ind[2] == 0) { | 275 if (vec.ind[2] == 0) { |
277 # egg | 276 # Egg. |
278 death.prob = opt$egg_mort * mortality.egg(mean.temp) | 277 death.prob = opt$egg_mort * mortality.egg(mean.temp) |
279 } | 278 } |
280 else if (vec.ind[2] == 1 | vec.ind[2] == 2) { | 279 else if (vec.ind[2] == 1 | vec.ind[2] == 2) { |
281 death.prob = opt$nymph_mort * mortality.nymph(mean.temp) | 280 death.prob = opt$nymph_mort * mortality.nymph(mean.temp) |
282 } | 281 } |
283 else if (vec.ind[2] == 3 | vec.ind[2] == 4 | vec.ind[2] == 5) { | 282 else if (vec.ind[2] == 3 | vec.ind[2] == 4 | vec.ind[2] == 5) { |
284 # for adult | 283 # For adult. |
285 if (day < day.kill) { | 284 if (day < day.kill) { |
286 death.prob = opt$adult_mort * mortality.adult(mean.temp) | 285 death.prob = opt$adult_mort * mortality.adult(mean.temp) |
287 } | 286 } |
288 else { | 287 else { |
289 # increase adult mortality after fall equinox | 288 # Increase adult mortality after fall equinox. |
290 death.prob = opt$adult_mort * post.mort * mortality.adult(mean.temp) | 289 death.prob = opt$adult_mort * post.mort * mortality.adult(mean.temp) |
291 } | 290 } |
292 } | 291 } |
293 # (or dependent on temperature and life stage?) | 292 # (or dependent on temperature and life stage?) |
294 u.d <- runif(1) | 293 u.d <- runif(1) |
295 if (u.d < death.prob) { | 294 if (u.d < death.prob) { |
296 death.vec <- c(death.vec, i) | 295 death.vec <- c(death.vec, i) |
297 } | 296 } |
298 else { | 297 else { |
299 # aggregrate index of dead bug | 298 # Aggregrate index of dead bug. |
300 # event 1 end of diapause | 299 # Event 1 end of diapause. |
301 if (vec.ind[1] == 0 && vec.ind[2] == 3) { | 300 if (vec.ind[1] == 0 && vec.ind[2] == 3) { |
302 # overwintering adult (previttelogenic) | 301 # Overwintering adult (previttelogenic). |
303 if (photoperiod > opt$photoperiod && vec.ind[3] > 68 && day < 180) { | 302 if (photoperiod > opt$photoperiod && vec.ind[3] > 68 && day < 180) { |
304 # add 68C to become fully reproductively matured | 303 # Add 68C to become fully reproductively matured. |
305 # transfer to vittelogenic | 304 # Transfer to vittelogenic. |
306 vec.ind <- c(0, 4, 0, 0, 0) | 305 vec.ind <- c(0, 4, 0, 0, 0) |
307 vec.mat[i,] <- vec.ind | 306 vec.mat[i,] <- vec.ind |
308 } | 307 } |
309 else { | 308 else { |
310 # add to DD | 309 # Add to dd. |
311 vec.ind[3] <- vec.ind[3] + dd.temp | 310 vec.ind[3] <- vec.ind[3] + dd.temp |
312 # add 1 day in current stage | 311 # Add 1 day in current stage. |
313 vec.ind[4] <- vec.ind[4] + 1 | 312 vec.ind[4] <- vec.ind[4] + 1 |
314 vec.mat[i,] <- vec.ind | 313 vec.mat[i,] <- vec.ind |
315 } | 314 } |
316 } | 315 } |
317 if (vec.ind[1] != 0 && vec.ind[2] == 3) { | 316 if (vec.ind[1] != 0 && vec.ind[2] == 3) { |
318 # NOT overwintering adult (previttelogenic) | 317 # Not overwintering adult (previttelogenic). |
319 current.gen <- vec.ind[1] | 318 current.gen <- vec.ind[1] |
320 if (vec.ind[3] > 68) { | 319 if (vec.ind[3] > 68) { |
321 # add 68C to become fully reproductively matured | 320 # Add 68C to become fully reproductively matured. |
322 # transfer to vittelogenic | 321 # Transfer to vittelogenic. |
323 vec.ind <- c(current.gen, 4, 0, 0, 0) | 322 vec.ind <- c(current.gen, 4, 0, 0, 0) |
324 vec.mat[i,] <- vec.ind | 323 vec.mat[i,] <- vec.ind |
325 } | 324 } |
326 else { | 325 else { |
327 # add to DD | 326 # Add to dd. |
328 vec.ind[3] <- vec.ind[3] + dd.temp | 327 vec.ind[3] <- vec.ind[3] + dd.temp |
329 # add 1 day in current stage | 328 # Add 1 day in current stage. |
330 vec.ind[4] <- vec.ind[4] + 1 | 329 vec.ind[4] <- vec.ind[4] + 1 |
331 vec.mat[i,] <- vec.ind | 330 vec.mat[i,] <- vec.ind |
332 } | 331 } |
333 } | 332 } |
334 | 333 |
335 # event 2 oviposition -- where population dynamics comes from | 334 # Event 2 oviposition -- where population dynamics comes from. |
336 if (vec.ind[2] == 4 && vec.ind[1] == 0 && mean.temp > 10) { | 335 if (vec.ind[2] == 4 && vec.ind[1] == 0 && mean.temp > 10) { |
337 # vittelogenic stage, overwintering generation | 336 # Vittelogenic stage, overwintering generation. |
338 if (vec.ind[4] == 0) { | 337 if (vec.ind[4] == 0) { |
339 # just turned in vittelogenic stage | 338 # Just turned in vittelogenic stage. |
340 n.birth=round(runif(1, 2 + opt$min_clutch_size, 8 + opt$max_clutch_size)) | 339 n.birth=round(runif(1, 2 + opt$min_clutch_size, 8 + opt$max_clutch_size)) |
341 } | 340 } |
342 else { | 341 else { |
343 # daily probability of birth | 342 # Daily probability of birth. |
344 p.birth = opt$oviposition * 0.01 | 343 p.birth = opt$oviposition * 0.01 |
345 u1 <- runif(1) | 344 u1 <- runif(1) |
346 if (u1 < p.birth) { | 345 if (u1 < p.birth) { |
347 n.birth=round(runif(1, 2, 8)) | 346 n.birth=round(runif(1, 2, 8)) |
348 } | 347 } |
349 } | 348 } |
350 # add to DD | 349 # Add to dd. |
351 vec.ind[3] <- vec.ind[3] + dd.temp | 350 vec.ind[3] <- vec.ind[3] + dd.temp |
352 # add 1 day in current stage | 351 # Add 1 day in current stage. |
353 vec.ind[4] <- vec.ind[4] + 1 | 352 vec.ind[4] <- vec.ind[4] + 1 |
354 vec.mat[i,] <- vec.ind | 353 vec.mat[i,] <- vec.ind |
355 if (n.birth > 0) { | 354 if (n.birth > 0) { |
356 # add new birth -- might be in different generations | 355 # Add new birth -- might be in different generations. |
357 # generation + 1 | |
358 new.gen <- vec.ind[1] + 1 | 356 new.gen <- vec.ind[1] + 1 |
359 # egg profile | 357 # Egg profile. |
360 new.ind <- c(new.gen, 0, 0, 0, 0) | 358 new.ind <- c(new.gen, 0, 0, 0, 0) |
361 new.vec <- rep(new.ind, n.birth) | 359 new.vec <- rep(new.ind, n.birth) |
362 # update batch of egg profile | 360 # Update batch of egg profile. |
363 new.vec <- t(matrix(new.vec, nrow=5)) | 361 new.vec <- t(matrix(new.vec, nrow=5)) |
364 # group with total eggs laid in that day | 362 # Group with total eggs laid in that day. |
365 birth.vec <- rbind(birth.vec, new.vec) | 363 birth.vec <- rbind(birth.vec, new.vec) |
366 } | 364 } |
367 } | 365 } |
368 | 366 |
369 # event 2 oviposition -- for gen 1. | 367 # Event 2 oviposition -- for gen 1. |
370 if (vec.ind[2] == 4 && vec.ind[1] == 1 && mean.temp > 12.5 && day < 222) { | 368 if (vec.ind[2] == 4 && vec.ind[1] == 1 && mean.temp > 12.5 && day < 222) { |
371 # vittelogenic stage, 1st generation | 369 # Vittelogenic stage, 1st generation |
372 if (vec.ind[4] == 0) { | 370 if (vec.ind[4] == 0) { |
373 # just turned in vittelogenic stage | 371 # Just turned in vittelogenic stage. |
374 n.birth=round(runif(1, 2 + opt$min_clutch_size, 8 + opt$max_clutch_size)) | 372 n.birth=round(runif(1, 2 + opt$min_clutch_size, 8 + opt$max_clutch_size)) |
375 } | 373 } |
376 else { | 374 else { |
377 # daily probability of birth | 375 # Daily probability of birth. |
378 p.birth = opt$oviposition * 0.01 | 376 p.birth = opt$oviposition * 0.01 |
379 u1 <- runif(1) | 377 u1 <- runif(1) |
380 if (u1 < p.birth) { | 378 if (u1 < p.birth) { |
381 n.birth = round(runif(1, 2, 8)) | 379 n.birth = round(runif(1, 2, 8)) |
382 } | 380 } |
383 } | 381 } |
384 # add to DD | 382 # Add to dd. |
385 vec.ind[3] <- vec.ind[3] + dd.temp | 383 vec.ind[3] <- vec.ind[3] + dd.temp |
386 # add 1 day in current stage | 384 # Add 1 day in current stage. |
387 vec.ind[4] <- vec.ind[4] + 1 | 385 vec.ind[4] <- vec.ind[4] + 1 |
388 vec.mat[i,] <- vec.ind | 386 vec.mat[i,] <- vec.ind |
389 if (n.birth > 0) { | 387 if (n.birth > 0) { |
390 # add new birth -- might be in different generations | 388 # Add new birth -- might be in different generations. |
391 # generation + 1 | |
392 new.gen <- vec.ind[1] + 1 | 389 new.gen <- vec.ind[1] + 1 |
393 # egg profile | 390 # Egg profile. |
394 new.ind <- c(new.gen, 0, 0, 0, 0) | 391 new.ind <- c(new.gen, 0, 0, 0, 0) |
395 new.vec <- rep(new.ind, n.birth) | 392 new.vec <- rep(new.ind, n.birth) |
396 # update batch of egg profile | 393 # Update batch of egg profile. |
397 new.vec <- t(matrix(new.vec, nrow=5)) | 394 new.vec <- t(matrix(new.vec, nrow=5)) |
398 # group with total eggs laid in that day | 395 # Group with total eggs laid in that day. |
399 birth.vec <- rbind(birth.vec, new.vec) | 396 birth.vec <- rbind(birth.vec, new.vec) |
400 } | 397 } |
401 } | 398 } |
402 | 399 |
403 # event 3 development (with diapause determination) | 400 # Event 3 development (with diapause determination). |
404 # event 3.1 egg development to young nymph (vec.ind[2]=0 -> egg) | 401 # Event 3.1 egg development to young nymph (vec.ind[2]=0 -> egg). |
405 if (vec.ind[2] == 0) { | 402 if (vec.ind[2] == 0) { |
406 # egg stage | 403 # Egg stage. |
407 # add to DD | 404 # Add to dd. |
408 vec.ind[3] <- vec.ind[3] + dd.temp | 405 vec.ind[3] <- vec.ind[3] + dd.temp |
409 if (vec.ind[3] >= (68 + opt$young_nymph_accum)) { | 406 if (vec.ind[3] >= (68 + opt$young_nymph_accum)) { |
410 # from egg to young nymph, DD requirement met | 407 # From egg to young nymph, DD requirement met. |
411 current.gen <- vec.ind[1] | 408 current.gen <- vec.ind[1] |
412 # transfer to young nym stage | 409 # Transfer to young nymph stage. |
413 vec.ind <- c(current.gen, 1, 0, 0, 0) | 410 vec.ind <- c(current.gen, 1, 0, 0, 0) |
414 } | 411 } |
415 else { | 412 else { |
416 # add 1 day in current stage | 413 # Add 1 day in current stage. |
417 vec.ind[4] <- vec.ind[4] + 1 | 414 vec.ind[4] <- vec.ind[4] + 1 |
418 } | 415 } |
419 vec.mat[i,] <- vec.ind | 416 vec.mat[i,] <- vec.ind |
420 } | 417 } |
421 | 418 |
422 # event 3.2 young nymph to old nymph (vec.ind[2]=1 -> young nymph: determines diapause) | 419 # Event 3.2 young nymph to old nymph (vec.ind[2]=1 -> young nymph: determines diapause). |
423 if (vec.ind[2] == 1) { | 420 if (vec.ind[2] == 1) { |
424 # young nymph stage | 421 # young nymph stage. |
425 # add to DD | 422 # add to dd. |
426 vec.ind[3] <- vec.ind[3] + dd.temp | 423 vec.ind[3] <- vec.ind[3] + dd.temp |
427 if (vec.ind[3] >= (250 + opt$old_nymph_accum)) { | 424 if (vec.ind[3] >= (250 + opt$old_nymph_accum)) { |
428 # from young to old nymph, DD requirement met | 425 # From young to old nymph, dd requirement met. |
429 current.gen <- vec.ind[1] | 426 current.gen <- vec.ind[1] |
430 # transfer to old nym stage | 427 # Transfer to old nym stage. |
431 vec.ind <- c(current.gen, 2, 0, 0, 0) | 428 vec.ind <- c(current.gen, 2, 0, 0, 0) |
432 if (photoperiod < opt$photoperiod && day > 180) { | 429 if (photoperiod < opt$photoperiod && day > 180) { |
433 vec.ind[5] <- 1 | 430 vec.ind[5] <- 1 |
434 } # prepare for diapausing | 431 } # Prepare for diapausing. |
435 } | 432 } |
436 else { | 433 else { |
437 # add 1 day in current stage | 434 # Add 1 day in current stage. |
438 vec.ind[4] <- vec.ind[4] + 1 | 435 vec.ind[4] <- vec.ind[4] + 1 |
439 } | 436 } |
440 vec.mat[i,] <- vec.ind | 437 vec.mat[i,] <- vec.ind |
441 } | 438 } |
442 | 439 |
443 # event 3.3 old nymph to adult: previttelogenic or diapausing? | 440 # Event 3.3 old nymph to adult: previttelogenic or diapausing? |
444 if (vec.ind[2] == 2) { | 441 if (vec.ind[2] == 2) { |
445 # old nymph stage | 442 # Old nymph stage. |
446 # add to DD | 443 # add to dd. |
447 vec.ind[3] <- vec.ind[3] + dd.temp | 444 vec.ind[3] <- vec.ind[3] + dd.temp |
448 if (vec.ind[3] >= (200 + opt$adult_accum)) { | 445 if (vec.ind[3] >= (200 + opt$adult_accum)) { |
449 # from old to adult, DD requirement met | 446 # From old to adult, dd requirement met. |
450 current.gen <- vec.ind[1] | 447 current.gen <- vec.ind[1] |
451 if (vec.ind[5] == 0) { | 448 if (vec.ind[5] == 0) { |
452 # non-diapausing adult -- previttelogenic | 449 # Non-diapausing adult -- previttelogenic. |
453 vec.ind <- c(current.gen, 3, 0, 0, 0) | 450 vec.ind <- c(current.gen, 3, 0, 0, 0) |
454 } | 451 } |
455 else { | 452 else { |
456 # diapausing | 453 # Diapausing. |
457 vec.ind <- c(current.gen, 5, 0, 0, 1) | 454 vec.ind <- c(current.gen, 5, 0, 0, 1) |
458 } | 455 } |
459 } | 456 } |
460 else { | 457 else { |
461 # add 1 day in current stage | 458 # Add 1 day in current stage. |
462 vec.ind[4] <- vec.ind[4] + 1 | 459 vec.ind[4] <- vec.ind[4] + 1 |
463 } | 460 } |
464 vec.mat[i,] <- vec.ind | 461 vec.mat[i,] <- vec.ind |
465 } | 462 } |
466 | 463 |
467 # event 4 growing of diapausing adult (unimportant, but still necessary)## | 464 # Event 4 growing of diapausing adult (unimportant, but still necessary). |
468 if (vec.ind[2] == 5) { | 465 if (vec.ind[2] == 5) { |
469 vec.ind[3] <- vec.ind[3] + dd.temp | 466 vec.ind[3] <- vec.ind[3] + dd.temp |
470 vec.ind[4] <- vec.ind[4] + 1 | 467 vec.ind[4] <- vec.ind[4] + 1 |
471 vec.mat[i,] <- vec.ind | 468 vec.mat[i,] <- vec.ind |
472 } | 469 } |
473 } # else if it is still alive | 470 } # Else if it is still alive. |
474 } # end of the individual bug loop | 471 } # End of the individual bug loop. |
475 | 472 |
476 # find how many died | 473 # Find how many died. |
477 n.death <- length(death.vec) | 474 n.death <- length(death.vec) |
478 if (n.death > 0) { | 475 if (n.death > 0) { |
479 vec.mat <- vec.mat[-death.vec, ] | 476 vec.mat <- vec.mat[-death.vec, ] |
480 } | 477 } |
481 # remove record of dead | 478 # Remove record of dead. |
482 # find how many new born | 479 # Find how many new born. |
483 n.newborn <- length(birth.vec[,1]) | 480 n.newborn <- length(birth.vec[,1]) |
484 vec.mat <- rbind(vec.mat, birth.vec) | 481 vec.mat <- rbind(vec.mat, birth.vec) |
485 # update population size for the next day | 482 # Update population size for the next day. |
486 n <- n - n.death + n.newborn | 483 n <- n - n.death + n.newborn |
487 | 484 |
488 # aggregate results by day | 485 # Aggregate results by day. |
489 tot.pop <- c(tot.pop, n) | 486 tot.pop <- c(tot.pop, n) |
490 # egg | 487 # Egg. |
491 s0 <- sum(vec.mat[,2] == 0) | 488 s0 <- sum(vec.mat[,2] == 0) |
492 # young nymph | 489 # Young nymph. |
493 s1 <- sum(vec.mat[,2] == 1) | 490 s1 <- sum(vec.mat[,2] == 1) |
494 # old nymph | 491 # Old nymph. |
495 s2 <- sum(vec.mat[,2] == 2) | 492 s2 <- sum(vec.mat[,2] == 2) |
496 # previtellogenic | 493 # Previtellogenic. |
497 s3 <- sum(vec.mat[,2] == 3) | 494 s3 <- sum(vec.mat[,2] == 3) |
498 # vitellogenic | 495 # Vitellogenic. |
499 s4 <- sum(vec.mat[,2] == 4) | 496 s4 <- sum(vec.mat[,2] == 4) |
500 # diapausing | 497 # Diapausing. |
501 s5 <- sum(vec.mat[,2] == 5) | 498 s5 <- sum(vec.mat[,2] == 5) |
502 # overwintering adult | 499 # Overwintering adult. |
503 gen0 <- sum(vec.mat[,1] == 0) | 500 gen0 <- sum(vec.mat[,1] == 0) |
504 # first generation | 501 # First generation. |
505 gen1 <- sum(vec.mat[,1] == 1) | 502 gen1 <- sum(vec.mat[,1] == 1) |
506 # second generation | 503 # Second generation. |
507 gen2 <- sum(vec.mat[,1] == 2) | 504 gen2 <- sum(vec.mat[,1] == 2) |
508 # sum of all adults | 505 # Sum of all adults. |
509 n.adult <- sum(vec.mat[,2] == 3) + sum(vec.mat[,2] == 4) + sum(vec.mat[,2] == 5) | 506 n.adult <- sum(vec.mat[,2] == 3) + sum(vec.mat[,2] == 4) + sum(vec.mat[,2] == 5) |
510 # gen.0 pop size | 507 # Gen eration 0 pop size. |
511 gen0.pop[day] <- gen0 | 508 gen0.pop[day] <- gen0 |
512 gen1.pop[day] <- gen1 | 509 gen1.pop[day] <- gen1 |
513 gen2.pop[day] <- gen2 | 510 gen2.pop[day] <- gen2 |
514 S0[day] <- s0 | 511 S0[day] <- s0 |
515 S1[day] <- s1 | 512 S1[day] <- s1 |
525 N.death[day] <- n.death | 522 N.death[day] <- n.death |
526 N.adult[day] <- n.adult | 523 N.adult[day] <- n.adult |
527 } # end of days specified in the input temperature data | 524 } # end of days specified in the input temperature data |
528 | 525 |
529 dd.cum <- cumsum(dd.day) | 526 dd.cum <- cumsum(dd.day) |
530 # collect all the outputs | 527 # Collect all the outputs. |
531 S0.rep[,N.rep] <- S0 | 528 S0.rep[,N.rep] <- S0 |
532 S1.rep[,N.rep] <- S1 | 529 S1.rep[,N.rep] <- S1 |
533 S2.rep[,N.rep] <- S2 | 530 S2.rep[,N.rep] <- S2 |
534 S3.rep[,N.rep] <- S3 | 531 S3.rep[,N.rep] <- S3 |
535 S4.rep[,N.rep] <- S4 | 532 S4.rep[,N.rep] <- S4 |