Mercurial > repos > greg > bmsb
comparison bmsb.R @ 19:d965e188feab draft
Uploaded
| author | greg |
|---|---|
| date | Tue, 16 Aug 2016 13:05:09 -0400 |
| parents | c6668285a216 |
| children | ce78cd25b873 |
comparison
equal
deleted
inserted
replaced
| 18:c6668285a216 | 19:d965e188feab |
|---|---|
| 1 #!/usr/bin/env Rscript | 1 #!/usr/bin/env Rscript |
| 2 | 2 |
| 3 #suppressPackageStartupMessages(library("optparse")) | |
| 4 | |
| 3 options_list <- list( | 5 options_list <- list( |
| 4 make_option(c("-o", "--output"), action="store", help="Output dataset") | 6 make_option(c("-s", "--save_log"), action="store_true", default=FALSE, help="Save R logs"), |
| 7 make_option(c("-m", "--output_r_log"), help="Output dataset for R logs"), | |
| 8 make_option(c("-o", "--output"), help="Output dataset") | |
| 5 ) | 9 ) |
| 6 | 10 |
| 7 parser <- OptionParser(usage="%prog [options] file", options_list) | 11 parser <- OptionParser(usage="%prog [options] file", options_list) |
| 8 args <- parse_args(parser, positional_arguments=TRUE) | 12 args <- parse_args(parser, positional_arguments=TRUE) |
| 9 opt <- args$options | 13 opt <- args$options |
| 10 | 14 |
| 11 | 15 |
| 12 fileConn<-file(opt$output) | 16 daylength = function(L){ |
| 13 writeLines(c("Hello World!"), fileConn) | 17 # from Forsythe 1995 |
| 14 close(fileConn) | 18 p = 0.8333 |
| 19 dl <- NULL | |
| 20 for (i in 1:365) { | |
| 21 theta <- 0.2163108 + 2 * atan(0.9671396 * tan(0.00860 * (i - 186))) | |
| 22 phi <- asin(0.39795 * cos(theta)) | |
| 23 dl[i] <- 24 - 24/pi * acos((sin(p * pi/180) + sin(L * pi/180) * sin(phi))/(cos(L * pi/180) * cos(phi))) | |
| 24 } | |
| 25 # return a vector of daylength in 365 days | |
| 26 dl | |
| 27 } | |
| 28 | |
| 29 | |
| 30 # source("daylength.R") | |
| 31 hourtemp = function(L,date){ | |
| 32 # L = 37.5 specify this in main program | |
| 33 # base development threshold for BMSB | |
| 34 threshold <- 12.7 | |
| 35 # threshold2 <- threshold/24 degree hour accumulation | |
| 36 #expdata <- tempdata[1:365,11:13] # Use daily max, min, mean | |
| 37 # daily minimum | |
| 38 dnp <- expdata[date,2] | |
| 39 # daily maximum | |
| 40 dxp <- expdata[date,3] | |
| 41 dmean <- 0.5 * (dnp + dxp) | |
| 42 #if (dmean>0) { | |
| 43 #dnp <- dnp - k1 * dmean | |
| 44 #dxp <- dxp + k2 * dmean | |
| 45 #} else { | |
| 46 #dnp <- dnp + k1 * dmean | |
| 47 #dxp <- dxp - k2 * dmean | |
| 48 #} | |
| 49 dd <- 0 # initialize degree day accumulation | |
| 50 | |
| 51 if (dxp<threshold) { | |
| 52 dd <- 0 | |
| 53 } else { | |
| 54 # extract daylength data for entire year | |
| 55 dlprofile <- daylength(L) | |
| 56 # initialize hourly temperature | |
| 57 T <- NULL | |
| 58 #initialize degree hour vector | |
| 59 dh <- NULL | |
| 60 # calculate daylength in given date | |
| 61 # date <- 200 | |
| 62 y <- dlprofile[date] | |
| 63 # night length | |
| 64 z <- 24 - y | |
| 65 # lag coefficient | |
| 66 a <- 1.86 | |
| 67 # night coefficient | |
| 68 b <- 2.20 | |
| 69 #import raw data set | |
| 70 #tempdata <- read.csv("tempdata.csv") | |
| 71 # Should be outside function otherwise its redundant | |
| 72 # sunrise time | |
| 73 risetime <- 12 - y/2 | |
| 74 # sunset time | |
| 75 settime <- 12 + y/2 | |
| 76 ts <- (dxp - dnp) * sin(pi * (settime-5)/(y + 2 * a)) + dnp | |
| 77 for (i in 1:24) { | |
| 78 if (i > risetime && i < settime) { | |
| 79 # number of hours after Tmin until sunset | |
| 80 m <- i - 5 | |
| 81 T[i] = (dxp - dnp) * sin(pi * m/(y + 2 * a)) + dnp | |
| 82 if (T[i]<8.4) { | |
| 83 dh[i] <- 0 | |
| 84 } else { | |
| 85 dh[i] <- T[i] - 8.4 | |
| 86 } | |
| 87 } else if (i>settime) { | |
| 88 n <- i - settime | |
| 89 T[i] = dnp + (ts - dnp) * exp(-b * n/z) | |
| 90 if (T[i]<8.4) { | |
| 91 dh[i] <- 0 | |
| 92 } else { | |
| 93 dh[i] <- T[i] - 8.4 | |
| 94 } | |
| 95 } else { | |
| 96 n <- i + 24 - settime | |
| 97 T[i] = dnp + (ts - dnp) * exp(-b * n / z) | |
| 98 if (T[i]<8.4) { | |
| 99 dh[i] <- 0 | |
| 100 } else { | |
| 101 dh[i] <- T[i] - 8.4 | |
| 102 } | |
| 103 } | |
| 104 } | |
| 105 dd <- sum(dh) / 24 | |
| 106 } | |
| 107 return = c(dmean, dd) | |
| 108 return | |
| 109 } | |
| 110 | |
| 111 | |
| 112 mortality.egg = function(temperature) { | |
| 113 if (temperature < 12.7) { | |
| 114 mort.prob = 1 | |
| 115 } else { | |
| 116 # 100% mortality if <12.7 | |
| 117 mort.prob = 0.8 - temperature / 40 | |
| 118 if (mort.prob<0) { | |
| 119 mort.prob = 0.01 | |
| 120 } | |
| 121 } | |
| 122 return = mort.prob | |
| 123 return | |
| 124 } | |
| 125 | |
| 126 | |
| 127 mortality.nymph = function(temperature) { | |
| 128 if (temperature<12.7) { | |
| 129 mort.prob = 0.03 | |
| 130 } else { | |
| 131 # at low temperature | |
| 132 mort.prob = -temperature * 0.0008 + 0.03 | |
| 133 } | |
| 134 return = mort.prob | |
| 135 return | |
| 136 } | |
| 137 | |
| 138 | |
| 139 mortality.adult = function(temperature) { | |
| 140 if (temperature < 12.7) { | |
| 141 mort.prob = 0.002 | |
| 142 } else { | |
| 143 mort.prob = -temperature * 0.0005 + 0.02 | |
| 144 } | |
| 145 return = mort.prob | |
| 146 return | |
| 147 } | |
| 148 | |
| 149 | |
| 150 # model initialization | |
| 151 # TODO: add tool params for the following options. | |
| 152 # start with 1000 individuals | |
| 153 n <- 1000 | |
| 154 # Generation, Stage, DD, T, Diapause | |
| 155 vec.ini <- c(0,3,0,0,0) | |
| 156 # overwintering, previttelogenic, DD = 0, T = 0, no-diapause | |
| 157 vec.mat <- rep(vec.ini,n) | |
| 158 # complete matrix for the population | |
| 159 vec.mat <- t(matrix(vec.mat, nrow = 5)) | |
| 160 # latitude for Asheville NC | |
| 161 L <- 35.58 | |
| 162 # complete photoperiod profile in a year, requires daylength function | |
| 163 ph.p <- daylength(L) | |
| 164 | |
| 165 # load temperature data@location/year | |
| 166 load("asheville2014.Rdat") | |
| 167 | |
| 168 # time series of population size | |
| 169 tot.pop <- NULL | |
| 170 | |
| 171 # gen.0 pop size | |
| 172 gen0.pop <- rep(0, 365) | |
| 173 gen1.pop <- rep(0, 365) | |
| 174 gen2.pop <- rep(0, 365) | |
| 175 | |
| 176 # aggregate | |
| 177 S0 <- S1 <- S2 <- S3 <- S4 <- S5 <- rep(0, 365) | |
| 178 g0.adult <- g1.adult <- g2.adult <- rep(0, 365) | |
| 179 | |
| 180 # birth death adults | |
| 181 N.newborn <- N.death <- N.adult <- rep(0, 365) | |
| 182 | |
| 183 # degree-day accumulation | |
| 184 dd.day <- rep(0, 365) | |
| 185 | |
| 186 # start tick | |
| 187 ptm <- proc.time() | |
| 188 | |
| 189 for (n.sim in 1:1000) { | |
| 190 # loop through 1000 simulations | |
| 191 for (day in 1:365) { | |
| 192 # loop through 365 day/yr | |
| 193 photoperiod <- ph.p[day] | |
| 194 # photoperiod in the day | |
| 195 temp.profile <- hourtemp(L,day) | |
| 196 # temperature profile | |
| 197 mean.temp <- temp.profile[1] | |
| 198 # mean temp | |
| 199 dd.temp <- temp.profile[2] | |
| 200 # degree-day | |
| 201 dd.day[day] <- dd.temp | |
| 202 death.vec <- NULL | |
| 203 # trash bin for death | |
| 204 birth.vec <- NULL | |
| 205 # record new born | |
| 206 for (i in 1:n) { | |
| 207 # loop through all individual | |
| 208 vec.ind <- vec.mat[i,] | |
| 209 # find individual record | |
| 210 # first of all, still alive? | |
| 211 if (vec.ind[2] == 0) { | |
| 212 # egg | |
| 213 death.prob = mortality.egg(mean.temp) | |
| 214 } else if (vec.ind[2] == 1 | vec.ind[2] == 2) { | |
| 215 # nymph | |
| 216 death.prob = mortality.nymph(mean.temp) | |
| 217 } else if (vec.ind[2] == 3 | vec.ind[2] == 4 | vec.ind[2] == 5) { | |
| 218 # for adult | |
| 219 death.prob = mortality.adult(mean.temp) | |
| 220 } | |
| 221 u.d <- runif(1) | |
| 222 if (u.d<death.prob) { | |
| 223 death.vec <- c(death.vec,i) | |
| 224 } else { | |
| 225 # aggregrate index of dead bug | |
| 226 # event 1 end of diapause | |
| 227 if (vec.ind[1] == 0 && vec.ind[2] == 3) { | |
| 228 # overwintering adult (previttelogenic) | |
| 229 if (photoperiod>13.5 && vec.ind[3] > 68 && day < 180) { | |
| 230 # add 68C to become fully reproductively matured | |
| 231 # transfer to vittelogenic | |
| 232 vec.ind <- c(0,4,0,0,0) | |
| 233 vec.mat[i,] <- vec.ind | |
| 234 } else { | |
| 235 # add to DD | |
| 236 vec.ind[3] <- vec.ind[3] + dd.temp | |
| 237 vec.ind[4] <- vec.ind[4] + 1 # add 1 day in current stage | |
| 238 vec.mat[i,] <- vec.ind | |
| 239 } | |
| 240 } | |
| 241 if (vec.ind[1]!=0 && vec.ind[2] == 3) { | |
| 242 # NOT overwintering adult (previttelogenic) | |
| 243 current.gen <- vec.ind[1] | |
| 244 if (vec.ind[3]>68) { | |
| 245 # add 68C to become fully reproductively matured | |
| 246 # transfer to vittelogenic | |
| 247 vec.ind <- c(current.gen,4,0,0,0) | |
| 248 vec.mat[i,] <- vec.ind | |
| 249 } else { | |
| 250 # add to DD | |
| 251 vec.ind[3] <- vec.ind[3] + dd.temp | |
| 252 # add 1 day in current stage | |
| 253 vec.ind[4] <- vec.ind[4] + 1 | |
| 254 vec.mat[i,] <- vec.ind | |
| 255 } | |
| 256 } | |
| 257 # event 2 oviposition -- where population dynamics comes from | |
| 258 # vittelogenic stage, overwintering generation | |
| 259 if (vec.ind[2] == 4 && vec.ind[1] == 0 && mean.temp>10) { | |
| 260 if (vec.ind[4] == 0) { | |
| 261 # just turned in vittelogenic stage | |
| 262 n.birth = round(runif(1,10,20)) | |
| 263 } else { | |
| 264 p.birth = 1/4/75 | |
| 265 # prob of birth | |
| 266 u1 <- runif(1) | |
| 267 if (u1<p.birth) { | |
| 268 n.birth = n.birth | |
| 269 } | |
| 270 } | |
| 271 # add to DD | |
| 272 vec.ind[3] <- vec.ind[3] + dd.temp | |
| 273 # add 1 day in current stage | |
| 274 vec.ind[4] <- vec.ind[4] + 1 | |
| 275 vec.mat[i,] <- vec.ind | |
| 276 if (n.birth>0) { | |
| 277 # add new birth -- might be in different generations | |
| 278 # generation + 1 | |
| 279 new.gen <- vec.ind[1] + 1 | |
| 280 # egg profile | |
| 281 new.ind <- c(new.gen,0,0,0,0) | |
| 282 new.vec <- rep(new.ind,n.birth) | |
| 283 # update batch of egg profile | |
| 284 new.vec <- t(matrix(new.vec,nrow = 5)) | |
| 285 # group with total eggs laid in that day | |
| 286 birth.vec <- rbind(birth.vec,new.vec) | |
| 287 } | |
| 288 } | |
| 289 # event 2 oviposition -- for gen 1. | |
| 290 if (vec.ind[2] == 4 && vec.ind[1] == 1 && mean.temp>12.5 && day<222) { | |
| 291 # vittelogenic stage, 1st generation | |
| 292 if (vec.ind[4] == 0) { | |
| 293 # just turned in vittelogenic stage | |
| 294 n.birth = round(runif(1,10,20)) | |
| 295 } else { | |
| 296 p.birth = 1/4/75 | |
| 297 u1 <- runif(1) | |
| 298 if (u1<p.birth) { | |
| 299 n.birth = n.birth | |
| 300 } | |
| 301 } | |
| 302 # add to DD | |
| 303 vec.ind[3] <- vec.ind[3] + dd.temp | |
| 304 # add 1 day in current stage | |
| 305 vec.ind[4] <- vec.ind[4] + 1 | |
| 306 vec.mat[i,] <- vec.ind | |
| 307 if (n.birth>0) { | |
| 308 # add new birth -- might be in different generations | |
| 309 # generation + 1 | |
| 310 new.gen <- vec.ind[1] + 1 | |
| 311 # egg profile | |
| 312 new.ind <- c(new.gen,0,0,0,0) | |
| 313 new.vec <- rep(new.ind,n.birth) | |
| 314 # update batch of egg profile | |
| 315 new.vec <- t(matrix(new.vec,nrow = 5)) | |
| 316 # group with total eggs laid in that day | |
| 317 birth.vec <- rbind(birth.vec,new.vec) | |
| 318 } | |
| 319 } | |
| 320 # event 3 development (with diapause determination) | |
| 321 # event 3.1 egg development to young nymph (vec.ind[2] = 0 -> egg) | |
| 322 # egg stage | |
| 323 if (vec.ind[2] == 0) { | |
| 324 # add to DD | |
| 325 vec.ind[3] <- vec.ind[3] + dd.temp | |
| 326 # from egg to young nymph | |
| 327 if (vec.ind[3] >= 53.30 && -0.9843 * dd.temp + 33.438>0) { | |
| 328 current.gen <- vec.ind[1] | |
| 329 # transfer to young nym stage | |
| 330 vec.ind <- c(current.gen,1,0,0,0) | |
| 331 } else { | |
| 332 # add 1 day in current stage | |
| 333 vec.ind[4] <- vec.ind[4] + 1 | |
| 334 } | |
| 335 vec.mat[i,] <- vec.ind | |
| 336 } | |
| 337 # event 3.2 young nymph to old nymph (vec.ind[2] = 1 -> young nymph: determines diapause) | |
| 338 # young nymph stage | |
| 339 if (vec.ind[2] == 1) { | |
| 340 # add to DD | |
| 341 vec.ind[3] <- vec.ind[3] + dd.temp | |
| 342 # from young to old nymph | |
| 343 if (vec.ind[3] >= 537/2 && -0.45 * dd.temp + 18>0) { | |
| 344 current.gen <- vec.ind[1] | |
| 345 # transfer to old nym stage | |
| 346 vec.ind <- c(current.gen,2,0,0,0) | |
| 347 # prepare for diapausing | |
| 348 if (photoperiod<13.5 && day > 180) { | |
| 349 vec.ind[5] <- 1 | |
| 350 } | |
| 351 } else { | |
| 352 # add 1 day in current stage | |
| 353 vec.ind[4] <- vec.ind[4] + 1 | |
| 354 } | |
| 355 vec.mat[i,] <- vec.ind | |
| 356 } | |
| 357 # event 3.3 old nymph to adult: previttelogenic or diapausing? | |
| 358 # old nymph stage | |
| 359 if (vec.ind[2] == 2) { | |
| 360 # add to DD | |
| 361 vec.ind[3] <- vec.ind[3] + dd.temp | |
| 362 # from old to adult | |
| 363 if (vec.ind[3] >= 537/2 && -0.50 * dd.temp + 22>0) { | |
| 364 current.gen <- vec.ind[1] | |
| 365 # non-diapausing adult -- previttelogenic | |
| 366 if (vec.ind[5] == 0) { | |
| 367 vec.ind <- c(current.gen,3,0,0,0) | |
| 368 # diapausing | |
| 369 } else { | |
| 370 vec.ind <- c(current.gen,5,0,0,1) | |
| 371 } | |
| 372 } else { | |
| 373 # add 1 day in current stage | |
| 374 vec.ind[4] <- vec.ind[4] + 1 | |
| 375 } | |
| 376 vec.mat[i,] <- vec.ind | |
| 377 } | |
| 378 # event 4 growing of diapausing adult (unimportant, but still necessary)## | |
| 379 if (vec.ind[2] == 5) { | |
| 380 vec.ind[3] <- vec.ind[3] + dd.temp | |
| 381 vec.ind[4] <- vec.ind[4] + 1 | |
| 382 vec.mat[i,] <- vec.ind | |
| 383 } | |
| 384 } # else if it is still alive | |
| 385 } # end of the individual bug loop | |
| 386 # find how many died | |
| 387 n.death <- length(death.vec) | |
| 388 if (n.death>0) { | |
| 389 vec.mat <- vec.mat[-death.vec, ] | |
| 390 } | |
| 391 # remove record of dead | |
| 392 # find how many new born | |
| 393 n.newborn <- length(birth.vec[,1]) | |
| 394 vec.mat <- rbind(vec.mat,birth.vec) | |
| 395 # update population size for the next day | |
| 396 n <- n-n.death + n.newborn | |
| 397 | |
| 398 # aggregate results by day | |
| 399 tot.pop <- c(tot.pop,n) | |
| 400 # egg | |
| 401 s0 <- sum(vec.mat[,2] == 0) | |
| 402 # young nymph | |
| 403 s1 <- sum(vec.mat[,2] == 1) | |
| 404 # old nymph | |
| 405 s2 <- sum(vec.mat[,2] == 2) | |
| 406 # previtellogenic | |
| 407 s3 <- sum(vec.mat[,2] == 3) | |
| 408 # vitellogenic | |
| 409 s4 <- sum(vec.mat[,2] == 4) | |
| 410 # diapausing | |
| 411 s5 <- sum(vec.mat[,2] == 5) | |
| 412 # overwintering adult | |
| 413 gen0 <- sum(vec.mat[,1] == 0) | |
| 414 # first generation | |
| 415 gen1 <- sum(vec.mat[,1] == 1) | |
| 416 # second generation | |
| 417 gen2 <- sum(vec.mat[,1] == 2) | |
| 418 # sum of all adults | |
| 419 n.adult <- sum(vec.mat[,2] == 3) + sum(vec.mat[,2] == 4) + sum(vec.mat[,2] == 5) | |
| 420 # gen.0 pop size | |
| 421 gen0.pop[day] <- gen0 | |
| 422 gen1.pop[day] <- gen1 | |
| 423 gen2.pop[day] <- gen2 | |
| 424 S0[day] <- s0 | |
| 425 S1[day] <- s1 | |
| 426 S2[day] <- s2 | |
| 427 S3[day] <- s3 | |
| 428 S4[day] <- s4 | |
| 429 S5[day] <- s5 | |
| 430 g0.adult[day] <- sum(vec.mat[,1] == 0) | |
| 431 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)) | |
| 432 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)) | |
| 433 N.newborn[day] <- n.newborn | |
| 434 N.death[day] <- n.death | |
| 435 N.adult[day] <- n.adult | |
| 436 } | |
| 437 #print(n.sim) | |
| 438 } | |
| 439 | |
| 440 proc.time() - ptm | |
| 441 dd.cum <- cumsum(dd.day) | |
| 442 save(dd.day, dd.cum, S0, S1, S2, S3, S4, S5, N.newborn, N.death, N.adult, tot.pop, gen0.pop, gen1.pop, gen2.pop, g0.adult, g1.adult, g2.adult, file=opt$output) |
