comparison BMSB.R @ 31:aefd45f2fa43 draft

Uploaded
author greg
date Thu, 15 Dec 2016 11:04:41 -0500
parents
children
comparison
equal deleted inserted replaced
30:53d2ac56c953 31:aefd45f2fa43
1 #!/usr/bin/env Rscript
2
3 suppressPackageStartupMessages(library("optparse"))
4
5 options_list <- list(
6 make_option(c("-adult_mort", "--adult_mort"), action="store", help="Adjustment rate for adult mortality"),
7 make_option(c("-adult_nymph_accum", "--adult_nymph_accum"), action="store", help="Adjustment of DD accumulation (old nymph->adult)"),
8 make_option(c("-egg_mort", "--egg_mort"), action="store", help="Adjustment rate for egg mortality"),
9 make_option(c("-latitude", "--latitude"), action="store", help="Latitude of selected location"),
10 make_option(c("-location", "--location"), action="store", help="Selected location"),
11 make_option(c("-min_clutch_size", "--min_clutch_size"), action="store", help="Adjustment of minimum clutch size"),
12 make_option(c("-max_clutch_size", "--max_clutch_size"), action="store", help="Adjustment of maximum clutch size"),
13 make_option(c("-nymph_mort", "--nymph_mort"), action="store", help="Adjustment rate for nymph mortality"),
14 make_option(c("-old_nymph_accum", "--old_nymph_accum"), action="store", help="Adjustment of DD accumulation (young nymph->old nymph)"),
15 make_option(c("-output", "--output"), action="store", help="Output dataset"),
16 make_option(c("-oviposition", "--oviposition"), action="store", help="Adjustment for oviposition rate"),
17 make_option(c("-photoperiod", "--photoperiod"), action="store", help="Critical photoperiod for diapause induction/termination"),
18 make_option(c("-replications", "--replications"), action="store", help="Number of replications"),
19 make_option(c("-se_plot", "--se_plot"), action="store", help="Plot SE"),
20 make_option(c("-start_year", "--start_year"), action="store", help="Starting year"),
21 make_option(c("-sim_year", "--sim_year"), action="store", help="Simulation year"),
22 make_option(c("-temperature_datasets", "--temperature_datasets"), action="store", help="Temperature data for selected location"),
23 make_option(c("-young_nymph_accum", "--young_nymph_accum"), action="store", help="Adjustment of DD accumulation (egg->young nymph)"),
24 )
25
26 parser <- OptionParser(usage="%prog [options] file", options_list)
27 arguements <- parse_args(parser, positional_arguments=TRUE)
28 opt <- args$options
29 args <- arguments$args
30
31 temperature_datasets <- strsplit(opt$temperature_datasets, ",")
32
33 # read in the input temperature datafile
34 data.input(opt$location, opt$start_year, temperature_datasets)
35
36 input.name<-paste(opt$location, opt$sim_year, ".Rdat" ,sep="")
37 output.name<-paste(opt$location, opt$sim_year, "sim.Rdat", sep="")
38 load(input.name)
39
40 # initialize matrix for results from all replications
41 S0.rep<-S1.rep<-S2.rep<-S3.rep<-S4.rep<-S5.rep<-matrix(rep(0,365*n.rep),ncol=n.rep)
42 newborn.rep<-death.rep<-adult.rep<-pop.rep<-g0.rep<-g1.rep<-g2.rep<-g0a.rep<-g1a.rep<-g2a.rep<-matrix(rep(0,365*n.rep),ncol=n.rep)
43
44 # loop through replications
45 for (N.rep in 1:n.rep)
46 {
47 # during each replication
48 n<-1000 # start with 1000 individuals -- user definable as well?
49 # Generation, Stage, DD, T, Diapause
50 vec.ini<-c(0,3,0,0,0)
51 # overwintering, previttelogenic,DD=0, T=0, no-diapause
52 vec.mat<-rep(vec.ini,n)
53 vec.mat<-t(matrix(vec.mat,nrow=5)) # complete matrix for the population
54 ph.p<-daylength(L) # complete photoperiod profile in a year, requires daylength function
55
56 tot.pop<-NULL # time series of population size
57 gen0.pop<-rep(0,365) # gen.0 pop size
58 gen1.pop<-rep(0,365)
59 gen2.pop<-rep(0,365)
60 S0<-S1<-S2<-S3<-S4<-S5<-rep(0,365)
61 g0.adult<-g1.adult<-g2.adult<-rep(0,365)
62 N.newborn<-N.death<-N.adult<-rep(0,365)
63 dd.day<-rep(0,365)
64
65 ptm <- proc.time() # start tick
66
67 # all the days
68 for (day in 1:365)
69 {
70 photoperiod<-ph.p[day] # photoperiod in the day
71 temp.profile<-hourtemp(L,day)
72 mean.temp<-temp.profile[1]
73 dd.temp<-temp.profile[2]
74 dd.day[day]<-dd.temp
75 death.vec<-NULL # trash bin for death
76 birth.vec<-NULL # new born
77 #n<-length(vec.mat[,1]) # population size at previous day
78
79 # all individuals
80 for (i in 1:n)
81 {
82 vec.ind<-vec.mat[i,] # find individual record
83 # first of all, still alive?
84 # adjustment for late season mortality rate
85 if (L<40)
86 {
87 post.mort<-1
88 day.kill<-300
89 }
90 else
91 {
92 post.mort<-2
93 day.kill<-250
94 }
95 # egg
96 if(vec.ind[2]==0)
97 {
98 death.prob=ar.em*mortality.egg(mean.temp)
99 }
100 else if (vec.ind[2]==1 | vec.ind[2]==2)
101 {
102 death.prob=ar.nm*mortality.nymph(mean.temp)
103 }
104 # for adult
105 else if (vec.ind[2]==3 | vec.ind[2]==4 | vec.ind[2]==5)
106 {
107 if (day<day.kill)
108 {
109 death.prob=ar.am*mortality.adult(mean.temp)
110 }
111 else
112 {
113 death.prob=ar.am*post.mort*mortality.adult(mean.temp)} # increase adult mortality after fall equinox
114 }
115 #(or dependent on temperature and life stage?)
116 u.d<-runif(1)
117 if (u.d<death.prob)
118 {
119 death.vec<-c(death.vec,i)
120 }
121 else
122 {
123 # aggregrate index of dead bug
124 # event 1 end of diapause
125 if (vec.ind[1]==0 && vec.ind[2]==3)
126 {
127 # overwintering adult (previttelogenic)
128 if (photoperiod>ph.cr && vec.ind[3]>68 && day<180)
129 {
130 # add 68C to become fully reproductively matured
131 vec.ind<-c(0,4,0,0,0) # transfer to vittelogenic
132 vec.mat[i,]<-vec.ind
133 }
134 else
135 {
136 vec.ind[3]<-vec.ind[3]+dd.temp # add to DD
137 vec.ind[4]<-vec.ind[4]+1 # add 1 day in current stage
138 vec.mat[i,]<-vec.ind
139 }
140 }
141 if (vec.ind[1]!=0 && vec.ind[2]==3)
142 {
143 # NOT overwintering adult (previttelogenic)
144 current.gen<-vec.ind[1]
145 if (vec.ind[3]>68)
146 {
147 # add 68C to become fully reproductively matured
148 vec.ind<-c(current.gen,4,0,0,0) # transfer to vittelogenic
149 vec.mat[i,]<-vec.ind
150 }
151 else
152 {
153 vec.ind[3]<-vec.ind[3]+dd.temp # add to DD
154 vec.ind[4]<-vec.ind[4]+1 # add 1 day in current stage
155 vec.mat[i,]<-vec.ind
156 }
157 }
158
159 # event 2 oviposition -- where population dynamics comes from
160 if (vec.ind[2]==4 && vec.ind[1]==0 && mean.temp>10)
161 {
162 # vittelogenic stage, overwintering generation
163 if (vec.ind[4]==0)
164 {
165 # just turned in vittelogenic stage
166 n.birth=round(runif(1,2+min.ovi.adj,8+max.ovi.adj))
167 }
168 else
169 {
170 p.birth=ar.ovi*0.01 # daily probability of birth
171 u1<-runif(1)
172 if (u1<p.birth)
173 {
174 n.birth=round(runif(1,2,8))
175 }
176 }
177 vec.ind[3]<-vec.ind[3]+dd.temp # add to DD
178 vec.ind[4]<-vec.ind[4]+1 # add 1 day in current stage
179 vec.mat[i,]<-vec.ind
180 if (n.birth>0)
181 {
182 # add new birth -- might be in different generations
183 new.gen<-vec.ind[1]+1 # generation +1
184 new.ind<-c(new.gen,0,0,0,0) # egg profile
185 new.vec<-rep(new.ind,n.birth)
186 new.vec<-t(matrix(new.vec,nrow=5)) # update batch of egg profile
187 birth.vec<-rbind(birth.vec,new.vec) # group with total eggs laid in that day
188 }
189 }
190
191 # event 2 oviposition -- for gen 1.
192 if (vec.ind[2]==4 && vec.ind[1]==1 && mean.temp>12.5 && day<222)
193 {
194 # vittelogenic stage, 1st generation
195 if (vec.ind[4]==0)
196 {
197 # just turned in vittelogenic stage
198 n.birth=round(runif(1,2+min.ovi.adj,8+max.ovi.adj))
199 }
200 else
201 {
202 p.birth=ar.ovi*0.01 # daily probability of birth
203 u1<-runif(1)
204 if (u1<p.birth)
205 {
206 n.birth=round(runif(1,2,8))
207 }
208 }
209 vec.ind[3]<-vec.ind[3]+dd.temp # add to DD
210 vec.ind[4]<-vec.ind[4]+1 # add 1 day in current stage
211 vec.mat[i,]<-vec.ind
212 if (n.birth>0)
213 {
214 # add new birth -- might be in different generations
215 new.gen<-vec.ind[1]+1 # generation +1
216 new.ind<-c(new.gen,0,0,0,0) # egg profile
217 new.vec<-rep(new.ind,n.birth)
218 new.vec<-t(matrix(new.vec,nrow=5)) # update batch of egg profile
219 birth.vec<-rbind(birth.vec,new.vec) # group with total eggs laid in that day
220 }
221 }
222
223 # event 3 development (with diapause determination)
224 # event 3.1 egg development to young nymph (vec.ind[2]=0 -> egg)
225 if (vec.ind[2]==0)
226 {
227 # egg stage
228 vec.ind[3]<-vec.ind[3]+dd.temp # add to DD
229 if (vec.ind[3]>=(68+dd.adj1))
230 {
231 # from egg to young nymph, DD requirement met
232 current.gen<-vec.ind[1]
233 vec.ind<-c(current.gen,1,0,0,0) # transfer to young nym stage
234 }
235 else
236 {
237 vec.ind[4]<-vec.ind[4]+1 # add 1 day in current stage
238 }
239 vec.mat[i,]<-vec.ind
240 }
241
242 # event 3.2 young nymph to old nymph (vec.ind[2]=1 -> young nymph: determines diapause)
243 if (vec.ind[2]==1)
244 {
245 # young nymph stage
246 vec.ind[3]<-vec.ind[3]+dd.temp # add to DD
247 if (vec.ind[3]>=(250+dd.adj2))
248 {
249 # from young to old nymph, DD requirement met
250 current.gen<-vec.ind[1]
251 vec.ind<-c(current.gen,2,0,0,0) # transfer to old nym stage
252 if (photoperiod<ph.cr && day > 180)
253 {
254 vec.ind[5]<-1
255 } # prepare for diapausing
256 }
257 else
258 {
259 vec.ind[4]<-vec.ind[4]+1 # add 1 day in current stage
260 }
261 vec.mat[i,]<-vec.ind
262 }
263
264 # event 3.3 old nymph to adult: previttelogenic or diapausing?
265 if (vec.ind[2]==2)
266 {
267 # old nymph stage
268 vec.ind[3]<-vec.ind[3]+dd.temp # add to DD
269 if (vec.ind[3]>=(200+dd.adj3))
270 {
271 # from old to adult, DD requirement met
272 current.gen<-vec.ind[1]
273 if (vec.ind[5]==0)
274 {
275 # non-diapausing adult -- previttelogenic
276 vec.ind<-c(current.gen,3,0,0,0)
277 }
278 else
279 {
280 # diapausing
281 vec.ind<-c(current.gen,5,0,0,1)
282 }
283 }
284 else
285 {
286 vec.ind[4]<-vec.ind[4]+1 # add 1 day in current stage
287 }
288 vec.mat[i,]<-vec.ind
289 }
290
291 # event 4 growing of diapausing adult (unimportant, but still necessary)##
292 if (vec.ind[2]==5)
293 {
294 vec.ind[3]<-vec.ind[3]+dd.temp
295 vec.ind[4]<-vec.ind[4]+1
296 vec.mat[i,]<-vec.ind
297 }
298 } # else if it is still alive
299 } # end of the individual bug loop
300
301 # find how many died
302 n.death<-length(death.vec)
303 if (n.death>0)
304 {
305 vec.mat<-vec.mat[-death.vec, ]}
306 # remove record of dead
307 # find how many new born
308 n.newborn<-length(birth.vec[,1])
309 vec.mat<-rbind(vec.mat,birth.vec)
310 # update population size for the next day
311 n<-n-n.death+n.newborn
312
313 # aggregate results by day
314 tot.pop<-c(tot.pop,n)
315 s0<-sum(vec.mat[,2]==0) #egg
316 s1<-sum(vec.mat[,2]==1) # young nymph
317 s2<-sum(vec.mat[,2]==2) # old nymph
318 s3<-sum(vec.mat[,2]==3) # previtellogenic
319 s4<-sum(vec.mat[,2]==4) # vitellogenic
320 s5<-sum(vec.mat[,2]==5) # diapausing
321 gen0<-sum(vec.mat[,1]==0) # overwintering adult
322 gen1<-sum(vec.mat[,1]==1) # first generation
323 gen2<-sum(vec.mat[,1]==2) # second generation
324 n.adult<-sum(vec.mat[,2]==3)+sum(vec.mat[,2]==4)+sum(vec.mat[,2]==5) # sum of all adults
325 gen0.pop[day]<-gen0 # gen.0 pop size
326 gen1.pop[day]<-gen1
327 gen2.pop[day]<-gen2
328 S0[day]<-s0
329 S1[day]<-s1
330 S2[day]<-s2
331 S3[day]<-s3
332 S4[day]<-s4
333 S5[day]<-s5
334 g0.adult[day]<-sum(vec.mat[,1]==0)
335 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))
336 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))
337
338 N.newborn[day]<-n.newborn
339 N.death[day]<-n.death
340 N.adult[day]<-n.adult
341 print(c(N.rep,day,n,n.adult))
342 } # end of 365 days
343
344 #proc.time() - ptm
345 dd.cum<-cumsum(dd.day)
346 # 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="wenatchee2013sim.Rdat")
347 #newborn.rep<-death.rep<-adult.rep<-pop.rep<-g0.rep<-g1.rep<-g2.rep<-g0a.rep<-g1a.rep<-g2a.rep<-matrix(rep(0,365*n.rep),ncol=n.rep)
348 # collect all the outputs
349 S0.rep[,N.rep]<-S0
350 S1.rep[,N.rep]<-S1
351 S2.rep[,N.rep]<-S2
352 S3.rep[,N.rep]<-S3
353 S4.rep[,N.rep]<-S4
354 S5.rep[,N.rep]<-S5
355 newborn.rep[,N.rep]<-N.newborn
356 death.rep[,N.rep]<-N.death
357 adult.rep[,N.rep]<-N.adult
358 pop.rep[,N.rep]<-tot.pop
359 g0.rep[,N.rep]<-gen0.pop
360 g1.rep[,N.rep]<-gen1.pop
361 g2.rep[,N.rep]<-gen2.pop
362 g0a.rep[,N.rep]<-g0.adult
363 g1a.rep[,N.rep]<-g1.adult
364 g2a.rep[,N.rep]<-g2.adult
365 }
366
367 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)
368 # maybe do not need to export this bit, but for now just leave it as-is
369 # do we need to export this Rdat file?
370
371
372 #########################################
373 # input starting year and how many years
374 # n.yr and start.yr needs to be integer
375 # loc.name needs to be CHARACTER and matches exactly the name in the csv file!!!
376 data.input=function(loc, start.yr, temperature.datasets)
377 {
378 n.yr <- length(temperature_datasets)
379 for (i in 1:n.yr)
380 {
381 expdata<-matrix(rep(0,365*3),nrow=365)
382 yr<-start.yr+i # replace 2004 with start. yr
383 name.input<-paste(temperature.datasets[i], ".csv", sep="")
384 namedat<-paste(loc, yr,".Rdat",sep="")
385 temp.data<-read.csv(file=name.input, header=T)
386
387 expdata[,1]<-c(1:365)
388 expdata[,2]<-temp.data[c(1:365),3] #minimum
389 expdata[,3]<-temp.data[c(1:365),2] #maximum
390 save(expdata,file=namedat)
391 }
392 }
393 #########################################
394
395 #########################################
396 daylength=function(L)
397 {
398 # from Forsythe 1995
399 p=0.8333
400 dl<-NULL
401 for (i in 1:365)
402 {
403 theta<-0.2163108+2*atan(0.9671396*tan(0.00860*(i-186)))
404 phi<-asin(0.39795*cos(theta))
405 dl[i]<-24-24/pi*acos((sin(p*pi/180)+sin(L*pi/180)*sin(phi))/(cos(L*pi/180)*cos(phi)))
406 }
407 dl # return a vector of daylength in 365 days
408 }
409 #########################################
410
411 #########################################
412 hourtemp=function(L,date)
413 {
414 threshold<-14.17 # base development threshold for BMSB
415 dnp<-expdata[date,2] # daily minimum
416 dxp<-expdata[date,3] # daily maximum
417 dmean<-0.5*(dnp+dxp)
418 dd<-0 # initialize degree day accumulation
419
420 if (dxp<threshold)
421 {
422 dd<-0
423 }
424 else
425 {
426 dlprofile<-daylength(L) # extract daylength data for entire year
427 T<-NULL # initialize hourly temperature
428 dh<-NULL #initialize degree hour vector
429 # date<-200
430 y<-dlprofile[date] # calculate daylength in given date
431 z<-24-y # night length
432 a<-1.86 # lag coefficient
433 b<-2.20 # night coefficient
434 #tempdata<-read.csv("tempdata.csv") #import raw data set
435 # Should be outside function otherwise its redundant
436 risetime<-12-y/2 # sunrise time
437 settime<-12+y/2 # sunset time
438 ts<-(dxp-dnp)*sin(pi*(settime-5)/(y+2*a))+dnp
439 for (i in 1:24)
440 {
441 if (i>risetime && i<settime)
442 {
443 m<-i-5 # number of hours after Tmin until sunset
444 T[i]=(dxp-dnp)*sin(pi*m/(y+2*a))+dnp
445 if (T[i]<8.4)
446 {
447 dh[i]<-0
448 }
449 else
450 {
451 dh[i]<-T[i]-8.4
452 }
453 }
454 else if (i>settime)
455 {
456 n<-i-settime
457 T[i]=dnp+(ts-dnp)*exp(-b*n/z)
458 if (T[i]<8.4)
459 {
460 dh[i]<-0
461 }
462 else
463 {
464 dh[i]<-T[i]-8.4
465 }
466 }
467 else
468 {
469 n<-i+24-settime
470 T[i]=dnp+(ts-dnp)*exp(-b*n/z)
471 if (T[i]<8.4)
472 {
473 dh[i]<-0
474 }
475 else
476 {
477 dh[i]<-T[i]-8.4
478 }
479 }
480 }
481 dd<-sum(dh)/24
482 }
483 return=c(dmean,dd)
484 return
485 }
486 #########################################
487
488 #########################################
489 dev.egg=function(temperature)
490 {
491 dev.rate=-0.9843*temperature+33.438
492 return=dev.rate
493 return
494 }
495 #########################################
496
497 #########################################
498 dev.young=function(temperature)
499 {
500 n12<--0.3728*temperature+14.68
501 n23<--0.6119*temperature+25.249
502 dev.rate=mean(n12+n23)
503 return=dev.rate
504 return
505 }
506 #########################################
507
508 #########################################
509 dev.old=function(temperature)
510 {
511 n34<--0.6119*temperature+17.602
512 n45<--0.4408*temperature+19.036
513 dev.rate=mean(n34+n45)
514 return=dev.rate
515 return
516 }
517 #########################################
518
519 #########################################
520 dev.emerg=function(temperature)
521 {
522 emerg.rate<--0.5332*temperature+24.147
523 return=emerg.rate
524 return
525 }
526 #########################################
527
528 #########################################
529 mortality.egg=function(temperature)
530 {
531 if (temperature<12.7)
532 {
533 mort.prob=0.8
534 }
535 else
536 {
537 mort.prob=0.8-temperature/40
538 if (mort.prob<0)
539 {
540 mort.prob=0.01
541 }
542 }
543 return=mort.prob
544 return
545 }
546 #########################################
547
548 #########################################
549 mortality.nymph=function(temperature)
550 {
551 if (temperature<12.7)
552 {
553 mort.prob=0.03
554 }
555 else
556 {
557 mort.prob=temperature*0.0008+0.03
558 }
559 return=mort.prob
560 return
561 }
562 #########################################
563
564 #########################################
565 mortality.adult=function(temperature)
566 {
567 if (temperature<12.7)
568 {
569 mort.prob=0.002
570 }
571 else
572 {
573 mort.prob=temperature*0.0005+0.02
574 }
575 return=mort.prob
576 return
577 }
578 #########################################