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