-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.r
1601 lines (1304 loc) · 55.4 KB
/
main.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This is the main script where you run the simulation (or experiment new things)
#setting things up -----------------------
rm(list = ls())
require(scales)
require(ggplot2)#because always need these two
require(reshape2)
require(plyr)# for aaply
require(grid)# for grid.newpage (plotSummary)
require(abind) # to use abind (bind of arrays)
require(rmarkdown)
require(RColorBrewer)
require(tictoc)
require(limSolve)
#require(tidyverse)
source("MizerParams-class.r") #to get the Constructor
source("selectivity_funcs.r") #to get the knife_edge function
source("methods.r") #I'm doing my own methods then!
source("summaryFunction.r") #to have all the GetSomething functions
source("plotFunction.r") #to draw the plots
source("TBM1.r") # the model from mizer (more like a set up)
source("model.r") # my model
source("utility.r") # helpful functions
# # little script to check sim content ----------------
# a<- get(load("eta5/fisheries/run1/run.Rdata"))
# a@params@species_params$knife_edge_size
# a@params@interaction
# a@params@species_params$r_max
# multi species simulations ----------
file_name = "/Sim9"
noInter = F
# PARAMETERS
# physio
no_sp = 9
min_w_inf <- 10
max_w_inf <- 1e5
RMAX = T
w_inf <- 10^seq(from=log10(min_w_inf), to = log10(max_w_inf), length=no_sp) # for fisheries gear
# varying param
# parameters worth checking: h, ks, z0pre, sigma, beta, f0, erepro, w_pp_cutoff
# defaults
h = 85
ks = 4
z0pre = 2
sigma = 1
beta = 100
f0 = 0.5
erepro = 1
w_pp_cutoff = 1
interaction = 0.5
overlap = 0.5
eta = 0.25
mAmplitude = 0.2
mu=1
kappa = 0.05
if(noInter)
{
w_pp_cutoff = 1e5
interaction = 0
overlap = 0
kappa = 0.5
}
# fisheries
gear_names <- rep("FishingStuff", no_sp)
knife_edges <- w_inf * eta
# other
t_max = 50
no_run = 60
no_sim = 10
i_start = 1
# or
simulationVec <- c(10)
# initialisation phase (4000 yr)
#for (i in i_start:no_sim)
for (i in simulationVec)
{
# switch(i,
# "1" = {mu = 0.01},
# "2" = {mu = 0.1},
# "3" = {mu = 0.5},
# "4" = {mu = 1},
# "5" = {mu = 1.5},
# "6" = {mu = 3},
# "7" = {mu = 5},
# {})
tic()
cat(sprintf("Simulation number %g\n",i))
path_to_save = paste(getwd(),file_name,"/init/run", i, sep = "")
sim <- myModel(no_sp = no_sp, eta = eta, t_max = t_max, no_run = no_run, min_w_inf = min_w_inf,extinct = T,
max_w_inf = max_w_inf, RMAX = RMAX,
ken = F, initTime = 1, initPool = 9, ks = ks, z0pre = z0pre, f0 = f0, overlap = overlap, sigma = sigma, beta = beta, w_pp_cutoff = w_pp_cutoff,
kappa = kappa,
OptMutant = "M5", mAmplitude = mAmplitude, mu= mu,
effort = 0, #knife_edge_size = knife_edges, gear_names = gear_names,
save_it = T, path_to_save = path_to_save,
print_it = T, normalFeeding = F, Traits = "eta")
#rm(sim)
for (j in 1:20) gc()
toc()
}
# simulation after initialisation
folder <- paste(getwd(),file_name,sep="")
initFolder <- paste(folder,"/init",sep="")
dirContent <- dir(initFolder)[1:11]
#dirContent <- "run4"
no_run = 60
i_start = 2
# NO fisheries
for (i in i_start:length(dirContent))
{
# switch(i,
# "1" = {mu = 0.01},
# "2" = {mu = 0.1},
# "3" = {mu = 0.5},
# "4" = {mu = 1},
# "5" = {mu = 1.5},
# "6" = {mu = 3},
# "7" = {mu = 5},
# {})
if (file.exists(paste(initFolder,"/",dirContent[i],"/run.Rdata",sep = "")))
{
sim <- get(load(paste(initFolder,"/",dirContent[i],"/run.Rdata",sep = "")))
path_to_save <- paste(folder,"/normal/",dirContent[i],sep = "")
cat(sprintf("Using %s\n",i))
output <- myModel(no_sp = no_sp, eta = eta, t_max = t_max, no_run = no_run, min_w_inf = min_w_inf,extinct = T,
max_w_inf = max_w_inf, RMAX = RMAX,
ken = F, initTime = 1, initPool = 9, ks = ks, z0pre = z0pre, f0 = f0, overlap = overlap, sigma = sigma, beta = beta, w_pp_cutoff = w_pp_cutoff,
kappa = kappa,
OptMutant = "M5", mAmplitude = mAmplitude, mu = mu, initCondition = sim,
effort = 0, #knife_edge_size = knife_edges, gear_names = gear_names,
save_it = T, path_to_save = path_to_save,
print_it = T, normalFeeding = F, Traits = "eta")
rm(output)
for (j in 1:20) gc()
}
}
# Fisheries
dirContent <- "run6"
for (i in i_start:length(dirContent))
{
# switch(i,
# "1" = {mu = 0.01},
# "2" = {mu = 0.1},
# "3" = {mu = 0.5},
# "4" = {mu = 1},
# "5" = {mu = 1.5},
# "6" = {mu = 3},
# "7" = {mu = 5},
# {})
if (file.exists(paste(initFolder,"/",dirContent[i],"/run.Rdata",sep = "")))
{
sim <- get(load(paste(initFolder,"/",dirContent[i],"/run.Rdata",sep = "")))
path_to_save <- paste(folder,"/fisheries/",dirContent[i],sep = "")
cat(sprintf("Using %s\n",i))
output <- myModel(no_sp = no_sp, eta = eta, t_max = t_max, no_run = no_run, min_w_inf = min_w_inf,extinct = T,
max_w_inf = max_w_inf, RMAX = RMAX,
ken = F, initTime = 1, initPool = 9, ks = ks, z0pre = z0pre, f0 = f0, overlap = overlap, sigma = sigma, beta = beta, w_pp_cutoff = w_pp_cutoff,
kappa = kappa,
OptMutant = "M5", mAmplitude = mAmplitude, mu= mu, initCondition = sim,
effort = 0.8, knife_edge_size = knife_edges, gear_names = gear_names,
save_it = T, path_to_save = path_to_save,
print_it = T, normalFeeding = F, Traits = "eta")
rm(output)
for (j in 1:20) gc()
}
}
# Varying effort
for (i in i_start:length(dirContent))
{
for (effort in c(seq(0.1,0.7,0.1),0.9,1))
{
if (file.exists(paste(initFolder,"/",dirContent[1],"/run.Rdata",sep = "")))
{
sim <- get(load(paste(initFolder,"/",dirContent[1],"/run.Rdata",sep = "")))
path_to_save <- paste(folder,"/fisheries/effort",effort,"/",dirContent[i],sep = "")
cat(sprintf("Using %s\n",i))
output <- myModel(no_sp = no_sp, eta = eta, t_max = t_max, no_run = no_run, min_w_inf = min_w_inf,extinct = T,
max_w_inf = max_w_inf, RMAX = RMAX,
ken = F, initTime = 1, initPool = 9, ks = ks, z0pre = z0pre, f0 = f0, overlap = overlap, sigma = sigma, beta = beta, w_pp_cutoff = w_pp_cutoff, kappa = kappa,
OptMutant = "M5", mAmplitude = mAmplitude, mu= mu, initCondition = sim,
effort = effort, knife_edge_size = knife_edges, gear_names = gear_names,
save_it = T, path_to_save = path_to_save,
print_it = T, normalFeeding = F, Traits = "eta")
rm(output)
for (j in 1:20) gc()
}
}
}
#with parallel / need to update the function---------------------------
rm(list = ls())
library(parallel)
library(ggplot2)#because always need these two
library(reshape2)
library(plyr)# for aaply
library(grid)# for grid.newpage (plotSummary)
library(abind) # to use abind (bind of arrays)
library(rmarkdown)
library(RColorBrewer)
library(tictoc)
source("MizerParams-class.r") #to get the Constructor
source("selectivity_funcs.r") #to get the knife_edge function
source("methods.r") #I'm doing my own methods then!
source("summaryFunction.r") #to have all the GetSomething functions
source("plotFunction.r") #to draw the plots
source("TBM1.r") # the model from mizer (more like a set up)
source("model.r") # my model
source("utility.r")
#(optional) record start time, for timing
ptm=proc.time()
tic()
#unsure what this setting does
options(warn=-1) #?
#Adjust this for num of targeted cpu/cores
# e.g. Numcores = detectCores()-1
where = paste(getwd(),"/parallel",sep="")
numcores=4
cl <- makeForkCluster(getOption("cl.cores", numcores), outfile = "")
sim <- clusterApplyLB(cl
,x=1:numcores
,fun=multiRun
,no_sp = 9
,t_max = 50
,mu = 5
,no_run = 80
,min_w_inf = 10
,max_w_inf = 10e5
,effort = 0
)
stopCluster(cl)
## Option 1: future package (and safely)
library(future)
plan(multiprocess)
## optionally, safely
safe_multiRun <- purrr::safely(multiRun)
sim <- future::future_lapply(1:numcores, safe_multiRun, no_sp , )
library(purrr)
## Option 2: purrr package
safe_multiRun <- purrr::safely(multiRun)
sim <- purrr::map(1:numcores, safe_multiRun, no_sp = 9, ...)
#(optional) compare end with start time, for timing
# saving
for (i in 1:length(sim))
{
path_to_save = paste(where,"/run",i,sep="")
ifelse(!dir.exists(file.path(path_to_save)), dir.create(file.path(path_to_save),recursive = T), FALSE)
saveRDS(file = paste(path_to_save,"/run.RDS",sep=""),object = sim[[i]])
}
print((proc.time()-ptm)/60.0)
toc()
# working on that right now -------------------
rownames(interactionBeta) <- c("1","2","3","4")
colnames(interactionBeta) <- c("1","2","3","4")
rownames(interactionAlpha) <- c("1","2","3","4","5")
colnames(interactionAlpha) <- c("1","2","3","4","5")
interactionAlpha<-interactionAlpha[-3,-3]
which(rownames(interactionAlpha) != rownames(interactionBeta))
a <- rownames(interactionAlpha)
b <- rownames(interactionBeta)
c <- which(!(a %in% b))
interactionSave <- rbind(interactionBeta,interactionAlpha[c,])
interactionSave <- cbind(interactionSave,interactionAlpha[,c])
# investigate this fucking growth
object <- get(load("ParamChap1/init/run4/run.Rdata"))
# Plot realised intake versus maximum intake of small and large individuals to see what is causing decrease in growth at large sizes.
# Is it different among large and small species?
# Is it food limitation or is metabolism too high?
# If this is food limitation that affects growth, would changing PPMR or feeding kernel improve growth?
# And how much do you need to change it for some substantial effect to happen?
#look at mortality
plotScythe(object)
# trait value picking
# Trait = eta
mAmplitude = 0.05
eta = 0.5
sd = as.numeric(mAmplitude * eta) # standard deviation
#x <- eta + rnorm(1, 0, sd) # change a bit eta
df = NULL
for (i in 1:10000) df <- c(df,( rnorm(1, 0, sd)))
summary(df)
plot(df)
plot(density(df))
x <- seq(-0.5,0.5, length=500)
y<-dnorm(x,mean=0, sd=0.025)
plot(x,y, type="l")
# Plots of every kind of output + for loop to see the variation of one parameter ------------------
res = 1000 # figure resolution
subdir = "/weighted" # where to store the plots
parameter = "none" # name of parameter varying for plot title
t_max = 100
no_sp = 4
mu = 5
for (i in c(1 %o% 10^(-5:0)))
{
output <- myModel(no_sp = no_sp, t_max = t_max, mu = mu, OptMutant = "yo", no_run = 1,
min_w_inf = 10, ks=2, max_w_inf = 10000,
#param = sim@params, # option to give param of another sim to have mutant relations
effort = 0, data = TRUE)
# when data = true, mutation do not work but I get the values of lots of function at each time step of the simulation
# do that for short runs
# sort the output
energy = output[[1]]
rd = output[[2]]
eggs = output[[3]]
sim = output[[4]]
food = output[[5]]
m2 = output[[6]]
z = output[[7]]
m2_background = output[[8]]
phi_fish = output[[9]]
phi_pltk = output[[10]]
end = dim(energy)[1]
# thing to fix: if I give parameters to the sim, it won't have the right name (sp name instead of ecotype)
# if there are no mutants I guess its fine
dimnames(sim@n)$sp = sim@params@species_params$ecotype
ifelse(!dir.exists(file.path(dir, subdir)), dir.create(file.path(dir, subdir)), FALSE) #create the file if it does not exists
dir.create(file.path(dir,subdir,"/reproduction")) # create tree files to ease comparison
dir.create(file.path(dir,subdir,"/growth"))
dir.create(file.path(dir,subdir,"/mortality"))
dir.create(file.path(dir,subdir,"/spawn"))
dir.create(file.path(dir,subdir,"/RDD"))
dir.create(file.path(dir,subdir,"/feeding"))
# plots ----------------
plotDynamics(sim)
setwd(paste(dir,subdir, sep = "")) #to have the figures in the right directory
mytitle = paste("biomass_", parameter, "_",i,".png", sep = "")
dev.print(png, mytitle, width = res, height = 0.6*res)
plotSS(sim)
setwd(paste(dir,subdir, sep = "")) #to have the figures in the right directory
mytitle = paste("sizespectrum_", parameter, "_",i,".png", sep = "")
dev.print(png, mytitle, width = res, height = 0.6*res)
# RDI
rdi <- rd[,,1]
RDI <- melt(rdi)
print(ggplot(RDI) +
geom_line(aes(x=Time,y=value ,colour = as.factor(Species))) +
scale_x_continuous(name = "Time") +
scale_y_log10(name = "Energy") +
scale_colour_discrete(name = "Species") +
ggtitle("Reproduction Density Independent"))
setwd(paste(dir,subdir, sep = ""))
mytitle = paste("rdi_", parameter, "_",i,".png", sep = "")
dev.print(png, mytitle, width = res, height = 0.6*res)
# RDD
rdd <- rd[,,2]
RDD <- melt(rdd)
print(ggplot(RDD) +
geom_line(aes(x=Time,y=value ,colour = as.factor(Species))) +
scale_x_continuous(name = "Time") +
scale_y_log10(name = "Energy") +
scale_colour_discrete(name = "Species") +
ggtitle("Reproduction Density Dependent"))
setwd(paste(dir,subdir, sep = ""))
mytitle = paste("rdd_", parameter, "_",i,".png", sep = "")
dev.print(png, mytitle, width = res, height = 0.6*res)
# ratio RDD/RDI
ratio <- rdd/rdi
RAT <- melt(ratio)
print(ggplot(RAT) +
geom_line(aes(x=Time,y=value ,colour = as.factor(Species))) +
scale_x_continuous(name = "Time") +
scale_y_log10(name = "Ratio", breaks = c(1 %o% 10^(-5:-2)) ) +
scale_colour_discrete(name = "Species") +
ggtitle("RDD/RDI"))
setwd(paste(dir,subdir, sep = ""))
mytitle = paste("RddRdi_", parameter, "_",i,".png", sep = "")
dev.print(png, mytitle, width = res, height = 0.6*res)
# e
# energy after metabolism, for the moment equal between every species
e <- energy[,,,1]
etot = apply(e, c(1,2), sum)
E <- melt(etot)
ggplot(E) +
geom_line(aes(x=Time,y=value, colour = as.factor(Species))) + # the as.factor convert to discrete as linetype doesnt work with continuous value
scale_x_continuous(name = "Time") +
scale_y_continuous(name = "Energy")+
scale_colour_discrete(name = "Species") +
ggtitle("Total energy available after metabolism")
setwd(paste(dir,subdir, sep = ""))
mytitle = paste("energy_", parameter, "_",i,".png", sep = "")
dev.print(png, mytitle, width = res, height = 0.6*res)
# energy by weight by sp at simulation end
eSP = e[end,,]
ESP <- melt(eSP)
ggplot(ESP) +
geom_line(aes(x=Size,y=value,colour = as.factor(Species))) +
scale_x_log10(name = "Weight") +
scale_y_continuous(name = "Energy")+
scale_colour_discrete(name = "Species") +
ggtitle("Energy available after metabolism by weight")
setwd(paste(dir,subdir, sep = ""))
mytitle = paste("energy_size_", parameter, "_",i,".png", sep = "")
dev.print(png, mytitle, width = res, height = 0.6*res)
#growth
# energy for through time
g <- energy[,,,3]
gtot = apply(g, c(1,2), sum)
G <- melt(gtot)
ggplot(G) +
geom_line(aes(x=Time,y=value, colour = as.factor(Species)))+
scale_x_continuous(name = "Time") +
scale_y_continuous(name = "Energy") +
scale_colour_discrete(name = "Species") +
ggtitle("Energy available for growth")
setwd(paste(dir,subdir, sep = ""))
mytitle = paste("growth_", parameter, "_",i,".png", sep = "")
dev.print(png, mytitle, width = res, height = 0.6*res)
# plot of energy by weight by sp at simulation end
gSP = g[end,,]
GSP <- melt(gSP)
ggplot(GSP) +
geom_line(aes(x=Size,y=value,colour = as.factor(Species)))+
scale_x_log10(name = "Weight") +
scale_y_continuous(name = "Energy") +
scale_colour_discrete(name = "Species") +
ggtitle("Energy available for growth by weight")
setwd(paste(dir,subdir, sep = ""))
mytitle = paste("growth_size_", parameter, "_",i,".png", sep = "")
dev.print(png, mytitle, width = res, height = 0.6*res)
# reproduction
# energy through time
s <- energy[,,,2]
stot = apply(s, c(1,2), sum)
S <- melt(stot)
print(ggplot(S) +
geom_line(aes(x=Time,y=value, colour = as.factor(Species)))+
scale_x_continuous(name = "Time") +
scale_y_continuous(name = "Energy") +
scale_colour_discrete(name = "Species") +
ggtitle("Energy available for reproduction"))
setwd(paste(dir,subdir, sep = ""))
mytitle = paste("reproduction_", parameter, "_",i,".png", sep = "")
dev.print(png, mytitle, width = res, height = 0.6*res)
# energy by weight by sp at simulation end
sSP = s[end,,]
stot = apply(s, c(1,2), sum)
SSP <- melt(sSP)
print(ggplot(SSP) +
geom_line(aes(x=Size,y=value,colour = as.factor(Species))) +
scale_x_log10(name = "Weight") +
scale_y_continuous(name = "Energy") +
scale_colour_discrete(name = "Species") +
ggtitle("Energy available for reproduction by weight"))
setwd(paste(dir,subdir, sep = ""))
mytitle = paste("reproduction_size_", parameter, "_",i,".png", sep = "")
dev.print(png, mytitle, width = res, height = 0.6*res)
# energy by weight by sp at simulation end and weighted by n
sSP = s[end,,]
sSPN = sSP * sim@n[dim(sim@n)[1],,]
SSPN <- melt(sSPN)
print(ggplot(SSPN) +
geom_line(aes(x=Size,y=value,colour = as.factor(Species))) +
scale_x_log10(name = "Weight") +
scale_y_log10(name = "Eggs in g/m3") +
scale_colour_discrete(name = "Species") +
ggtitle("Real reproduction"))
setwd(paste(dir,subdir, sep = ""))
mytitle = paste("weighted_reproduction_", parameter, "_",i,".png", sep = "")
dev.print(png, mytitle, width = res, height = 0.6*res)
# plot of number of eggs by sp by size at end sim
EGG = melt(eggs)
print(ggplot(EGG) +
geom_line(aes(x=Time,y=value,colour = as.factor(Species))) +
scale_x_continuous(name = "TIme") +
scale_y_log10(name = "Eggs in g/m3") +
scale_colour_discrete(name = "Species") +
ggtitle("Boudarie condition"))
setwd(paste(dir,subdir, sep = ""))
mytitle = paste("spawn_", parameter, "_",i,".png", sep = "")
dev.print(png, mytitle, width = res, height = 0.6*res)
# feeding
# throught time
feeding <- energy [,,,4]
ftot = apply(feeding, c(1,2), sum)
FEED <- melt(ftot)
ggplot(FEED) +
geom_line(aes(x=Time,y=value, colour = as.factor(Species)))+
scale_x_continuous(name = "Time") +
scale_y_continuous(name = "Energy") +
scale_colour_discrete(name = "Species") +
ggtitle("Energy issue from feeding")
setwd(paste(dir,subdir, sep = ""))
mytitle = paste("feeding_", parameter, "_",i,".png", sep = "")
dev.print(png, mytitle, width = res, height = 0.6*res)
# energy by weight by sp at simulation end
fSP = feeding[end,,]
FSP <- melt(fSP)
ggplot(FSP) +
geom_line(aes(x=Size,y=value,colour = as.factor(Species)))+
scale_x_log10(name = "Weight") +
scale_y_continuous(name = "Energy") +
scale_colour_discrete(name = "Species") +
ggtitle("Energy issue from feeding by weight")
setwd(paste(dir,subdir, sep = ""))
mytitle = paste("feeding_size_", parameter, "_",i,".png", sep = "")
dev.print(png, mytitle, width = res, height = 0.6*res)
# Phi
a = phi_fish[end,1,]
A = melt(a)
b= phi_pltk[end,1,]
B = melt(b)
feeding = energy[end,1,,4] # feeding level of one sp as they have the same profile
Fe = melt(feeding)
S = melt(sim@params@search_vol[1,]) # search volume
# plot of phi and others
ggplot()+
geom_line(data = A,aes(x = as.numeric(rownames(A)), y = value, color = "Phi fish")) +
geom_line(data = B, aes(x = as.numeric(rownames(B)), y = value, color = "Phi plankton")) +
# geom_line(data = Fe, aes(x = as.numeric(rownames(Fe)), y = value, color = "Feeding level")) +
#geom_line(data = S, aes(x = as.numeric(rownames(S)), y = value, color = "Search Volume")) +
scale_x_log10(name = "Predator size",breaks = c(1 %o% 10^(-10:5)))+
scale_y_continuous(name = "value of phy prey")+
ggtitle("Relative proportion of food eaten between plankton and fish")
setwd(paste(dir,subdir, sep = ""))
mytitle = paste("phi_", parameter, "_",i,".png", sep = "")
dev.print(png, mytitle, width = res, height = 0.6*res)
# Mortality
# predation mortality
a = m2[end,,]
A = melt(a)
ggplot(A) +
geom_line(aes(x = PreySize, y = value, color = as.factor(PreySp)))+
scale_x_log10()+
scale_y_continuous( limits = c(0,30))
setwd(paste(dir,subdir, sep = ""))
mytitle = paste("PredMort_", parameter, "_",i,".png", sep = "")
dev.print(png, mytitle, width = res, height = 0.6*res)
# total mortality
a = z[end,,]
A = melt(a)
ggplot(A) +
geom_line(aes(x = PreySize, y = value, color = as.factor(PreySp)))+
scale_x_log10()+
scale_y_continuous( limits = c(0,30))
setwd(paste(dir,subdir, sep = ""))
mytitle = paste("TotMort_", parameter, "_",i,".png", sep = "")
dev.print(png, mytitle, width = res, height = 0.6*res)
#mortality on plankton
a = m2_background[end,]
A = melt(a)
A = cbind(A,rownames(A))
colnames(A) = c("value", "size")
ggplot(A) +
geom_line(aes(x = as.numeric(size), y = value, group = 1))+
scale_y_log10() +
scale_x_log10()
setwd(paste(dir,subdir, sep = ""))
mytitle = paste("PlktMort_", parameter, "_",i,".png", sep = "")
dev.print(png, mytitle, width = res, height = 0.6*res)
#weighted plots -------------------
for (j in seq(t_max,t_max*10,t_max))
{
time = j
if (time == 1000) time = 992 # I know my sim is weird (last step is 992)
# reproduction by weight by sp at simulation end and weighted by n
s <- energy[,,,2]
sSP = s[time,,]
sSPN = sSP * sim@n[time,,]
SSPN <- melt(sSPN)
name = paste("Real reproduction at time ",time, sep ="")
print(ggplot(SSPN) +
geom_line(aes(x=Size,y=value,colour = as.factor(Species))) +
scale_x_log10(name = "Size") +
scale_y_log10(name = "Eggs in g/m3") +
scale_colour_discrete(name = "Species") +
ggtitle(name))
setwd(paste(dir,subdir,"/reproduction", sep = ""))
mytitle = paste("weighted_reproduction_", parameter, "_",i,".png", sep = "")
dev.print(png, mytitle, width = res, height = 0.6*res)
# growth by weight by sp at simulation end and weighted by n
g <- energy[,,,3]
gSP = g[time,,]
gSPN = gSP * sim@n[time,,]
GSPN <- melt(gSPN)
name = paste("Real growth at time ",time, sep ="")
ggplot(GSPN) +
geom_line(aes(x=Size,y=value,colour = as.factor(Species)))+
scale_x_log10(name = "Size") +
scale_y_log10(name = "Energy") +
scale_colour_discrete(name = "Species") +
ggtitle(name)
setwd(paste(dir,subdir,"/growth", sep = ""))
mytitle = paste("growth_size_", parameter, "_",i,".png", sep = "")
dev.print(png, mytitle, width = res, height = 0.6*res)
# energy by weight by sp at simulation end
feeding <- energy[,,,4]
fSP = feeding[time,,]
fSPN = fSP * sim@n[time,,]
FSPN <- melt(fSPN)
name = paste("Energy issue from feeding weighted by abundance of species at time ",time, sep ="")
ggplot(FSPN) +
geom_line(aes(x=Size,y=value,colour = as.factor(Species)))+
scale_x_log10(name = "Size") +
scale_y_log10(name = "Feeding level") +
scale_colour_discrete(name = "Species") +
ggtitle(name)
setwd(paste(dir,subdir,"/feeding", sep = ""))
mytitle = paste("feeding_size_", parameter, "_",i,".png", sep = "")
dev.print(png, mytitle, width = res, height = 0.6*res)
# # predation rate (to set up)
# pred <- food[,,,4]
# fSP = feeding[time,,]
# fSPN = fSP * sim@n[time,,]
# FSPN <- melt(fSPN)
# name = paste("Energy issue from feeding weighted by abundance of species at time ",time, sep ="")
# ggplot(FSPN) +
# geom_line(aes(x=Size,y=value,colour = as.factor(Species)))+
# scale_x_log10(name = "Size") +
# scale_y_log10(name = "Feeding level") +
# scale_colour_discrete(name = "Species") +
# ggtitle(name)
#
# setwd(paste(dir,subdir, sep = ""))
# mytitle = paste("feeding_size_", parameter, "_",i,".png", sep = "")
# dev.print(png, mytitle, width = res, height = 0.6*res)
# total mortality
mortality = z[time,,]
mN = mortality * sim@n[time,,]
MN = melt(mN)
name = paste("Total mortality at time ",time, sep ="")
ggplot(MN) +
geom_line(aes(x = PreySize, y = value, color = as.factor(PreySp)))+
scale_x_log10()+
scale_y_log10()+
scale_colour_discrete(name = "Species") +
ggtitle(name)
setwd(paste(dir,subdir,"/mortality", sep = ""))
mytitle = paste("TotMort_", parameter, "_",i,".png", sep = "")
dev.print(png, mytitle, width = res, height = 0.6*res)
# egg number
EGG = melt(eggs)
print(ggplot(EGG) +
geom_line(aes(x=Time,y=value,colour = as.factor(Species))) +
scale_x_continuous(name = "TIme") +
scale_y_log10(name = "Eggs in g/m3") +
scale_colour_discrete(name = "Species") +
ggtitle("Boudarie condition"))
setwd(paste(dir,subdir,"/spawn", sep = ""))
mytitle = paste("spawn_", parameter, "_",i,".png", sep = "")
dev.print(png, mytitle, width = res, height = 0.6*res)
# RDD
rdd <- rd[,,2]
RDD <- melt(rdd)
print(ggplot(RDD) +
geom_line(aes(x=Time,y=value ,colour = as.factor(Species))) +
scale_x_continuous(name = "Time") +
scale_y_log10(name = "Energy") +
scale_colour_discrete(name = "Species") +
ggtitle("Reproduction Density Dependent"))
setwd(paste(dir,subdir,"RDD", sep = ""))
mytitle = paste("rdd_", parameter, "_",i,".png", sep = "")
dev.print(png, mytitle, width = res, height = 0.6*res)
# RDI
rdi <- rd[,,1]
RDI <- melt(rdi)
RDI <- RDI[RDI$value >= min_value,]
print(ggplot(RDI) +
geom_line(aes(x=Time,y=value ,colour = as.factor(Species))) +
scale_x_continuous(name = "Time") +
scale_y_log10(name = "Energy") +
scale_colour_discrete(name = "Species") +
ggtitle("Reproduction Density Independent"))
setwd(paste(dir,subdir,"/RDI", sep = ""))
mytitle = paste("rdi_",time,"_", parameter, "_",i,".png", sep = "")
dev.print(png, mytitle, width = res, height = 0.6*res)
}
}
# predation traits analyses / detail of the predation equation here (not updated though)--------------
# phi prey
# n_eff_prey is the total prey abundance by size exposed to each predator
# (prey not broken into species - here we are just working out how much a predator eats - not which species are being eaten - that is in the mortality calculation
n_eff_prey <- sweep(object@interaction %*% n, 2, object@w * object@dw, "*")
# Quick reference to just the fish part of the size spectrum
idx_sp <- (length(object@w_full) - length(object@w) + 1):length(object@w_full)
# predKernal is predator x predator size x prey size
# So multiply 3rd dimension of predKernal by the prey abundance
# Then sum over 3rd dimension to get total eaten by each predator by predator size
phi_prey_species <- rowSums(sweep(object@pred_kernel[,,idx_sp,drop=FALSE],c(1,3),n_eff_prey,"*"),dims=2)
# Eating the background
phi_prey_background <- rowSums(sweep(object@pred_kernel,3,object@dw_full*object@w_full*n_pp,"*"),dims=2)
return(phi_prey_species+phi_prey_background)
#feeding level
encount <- object@search_vol * phi_prey
# calculate feeding level
f <- encount/(encount + object@intake_max)
return(f)
#pred rate
n_total_in_size_bins <- sweep(n, 2, object@dw, '*')
pred_rate <- sweep(object@pred_kernel,c(1,2),(1-feeding_level)*object@search_vol*n_total_in_size_bins,"*")
return(pred_rate)
#pred kernel
res@pred_kernel[] <- object$beta
res@pred_kernel <- exp(-0.5*sweep(log(sweep(sweep(res@pred_kernel,3,res@w_full,"*")^-1,2,res@w,"*")),1,object$sigma,"/")^2)
res@pred_kernel <- sweep(res@pred_kernel,c(2,3),combn(res@w_full,1,function(x,w)x<w,w=res@w),"*") # find out the untrues and then multiply
# trait study --------------
# draw plots that show the growth rate with different trait varying
# need some n values to get the rest
sim <- myModel(no_sp = 9, t_max = 50, mu = 5, OptMutant = "yo", RMAX = TRUE, hartvig = TRUE)
endList <- length(sim) # shortcut to have ref to the last simulation which has the right dim, names, ...
PSim <- sim[[endList]] # if I want to look at params and such I'm taking the last sim
PSim@params@species_params
plotDynamics(PSim)
end = dim(PSim@n)[1]
# and some parameters
eta = 0.25
z0pre = 0.84
n = 0.75 # exponent of maximum intake (scaling of intake)
q = 0.8 # exponent of search volume
kappa = 0.005 # ressource spectrum carrying capacity
lambda = 2+q-n # exponent of the background spectrum.
h = 85 # factor of maximum intake
f0 = 0.6 # average feeding level of the community/feeding level of small individuals feeding on background
# Asymptotic size
min_w_inf = 10
max_w_inf = 10e5
w_inf <- 10^seq(from=log10(min_w_inf), to = log10(max_w_inf), length=1000) # asymptotic mass of the species
w_mat <- w_inf * eta
z0 <- z0pre * w_inf^(n-1)
size = data.frame(w_inf,w_mat,z0)
ggplot(size) +
geom_line(aes(x = w_inf, y = w_mat, color = "Maturation size")) +
geom_line(aes(x = w_inf, y = z0, color = "Background mortality")) +
scale_x_log10(name = "Asymptotic size") +
scale_y_log10(name = "Size") +
ggtitle("Effect of varition of asymptotic size")
# w_mat is only used in psi (allocation reproduction)
# w_inf is used for h and I dont know what that is
# PPMR
beta = 100 # preferred predator-prey weight ratio
sigma = 1.3 # width of selection function
beta = seq (10,200,10)
alpha_e <- sqrt(2*pi) * sigma * beta^(lambda-2) * exp((lambda-2)^2 * sigma^2 / 2)
gamma <- h * f0 / (alpha_e * kappa * (1-f0))
PPMR <- data.frame(beta,gamma)
ggplot(PPMR)+
geom_line(aes(x = beta, y = gamma))+
ggtitle("Gamma function of beta")
# impact of beta variation
beta_min = 10
beta_max = 200
dBeta = 10
results = list()
for (i in seq (beta_min,beta_max,dBeta))
{
sim <- myModel(no_sp = 9, t_max = 50, OptMutant = "yo", RMAX = TRUE, min_w_inf = 10, max_w_inf = 10000, beta = i, extinct = FALSE, hartvig = TRUE)
sim <- sim[[endList]]
a = getPhiPrey(object = sim@params, n=sim@n[end,,], n_pp = sim@n_pp[end,])
b = getFeedingLevel(object = sim@params, n=sim@n[end,,], n_pp = sim@n_pp[end,], phi_prey = a)
betaPred = cbind(a[2,],b[2,])
name <- paste('beta',i,sep='')
results[[name]] = betaPred
}
#plots
# beta by size
res = 600
for (i in seq (beta_min,beta_max,dBeta))
{
name <- paste('beta',i,sep='')
pred = as.data.frame(results[[name]])
print(
ggplot(pred) +
geom_line(aes(x = as.numeric(rownames(pred)), y=V2, colour = "Feeding level"), group = 1)+
geom_line(aes(x = as.numeric(rownames(pred)), y=V1,colour = "Phi prey"), group = 1)+
scale_x_log10(name = "Size")+
scale_y_continuous(name = "Function output",limits = c(0,0.7))+
ggtitle(name)
)
setwd(paste(dir,"/Traits/Beta", sep = ""))
mytitle = paste(name,".png", sep = "")
dev.print(png, mytitle, width = res, height = res)
}
# global impact of beta on feeding and phi when summing weights
bigBeta = matrix(data = NA, nrow = length(seq (beta_min,beta_max,dBeta)), ncol = 2, dimnames = list(c(seq (beta_min,beta_max,dBeta)), c("Phi","Feed")))
for (i in seq (beta_min,beta_max,dBeta))
{
name <- paste('beta',i,sep='')
bigBeta[i/dBeta,] = colSums(results[[name]])
}
bigBeta = as.data.frame(bigBeta)
ggplot(bigBeta) +
geom_line(aes(x = as.numeric(rownames(bigBeta)), y=Feed, colour = "Feeding level"), group = 1)+
geom_line(aes(x = as.numeric(rownames(bigBeta)), y=Phi,colour = "Phi prey"), group = 1)+
scale_x_continuous(name = "Beta value")+
scale_y_continuous(name = "Function output")+
ggtitle("Impact of beta")
setwd(paste(dir,"/Traits/Beta", sep = ""))
mytitle = paste("betaVar",".png", sep = "")
dev.print(png, mytitle, width = res, height = res)
# sigma
sigma_min = 0.1
sigma_max = 2
dSigma = 0.1
results = list()
for (i in seq (sigma_min,sigma_max,dSigma))
{
sim <- myModel(no_sp = 9, t_max = 50, OptMutant = "yo", RMAX = TRUE, min_w_inf = 10, max_w_inf = 10000, sigma = i, extinct = FALSE, hartvig = TRUE)
sim <- sim[[endList]] # if I want to look at params and such I'm taking the last sim
a = getPhiPrey(object = sim@params, n=sim@n[end,,], n_pp = sim@n_pp[end,])
b = getFeedingLevel(object = sim@params, n=sim@n[end,,], n_pp = sim@n_pp[end,], phi_prey = a)
sigmaPred = cbind(a[2,],b[2,])
name <- paste('sigma',i,sep='')
results[[name]] = sigmaPred
}
#plots
# sigma by size
res = 600
for (i in seq (sigma_min,sigma_max,dSigma))
{
name <- paste('sigma',i,sep='')
pred = as.data.frame(results[[name]])
print(
ggplot(pred) +
geom_line(aes(x = as.numeric(rownames(pred)), y=V2, colour = "Feeding level"), group = 1)+
geom_line(aes(x = as.numeric(rownames(pred)), y=V1,colour = "Phi prey"), group = 1)+
scale_x_log10(name = "Size")+
scale_y_continuous(name = "Function output",limits = c(0,0.8))+
ggtitle(name)
)
setwd(paste(dir,"/Traits/Sigma", sep = "")) #to have the figures in the right directory
mytitle = paste(name,".png", sep = "")
dev.print(png, mytitle, width = res, height = res)
}
# energy by sigma
bigSigma = matrix(data = NA, nrow = length(seq (sigma_min,sigma_max,dSigma)), ncol = 2, dimnames = list(c(seq (sigma_min,sigma_max,dSigma)), c("Phi","Feed")))
for (i in seq (sigma_min,sigma_max,dSigma))
{
name <- paste('sigma',i,sep='')
idx = i/dSigma
bigSigma[idx,] = colSums(results[[name]])
}
bigSigma = as.data.frame(bigSigma)
ggplot(bigSigma) +
geom_line(aes(x = as.numeric(rownames(bigSigma)), y=Feed, colour = "Feeding level"), group = 1)+
geom_line(aes(x = as.numeric(rownames(bigSigma)), y=Phi,colour = "Phi prey"), group = 1)+
scale_x_continuous(name = "Sigma value")+
scale_y_continuous(name = "Function output")+
ggtitle("Impact of sigma")
setwd(paste(dir,"/Traits/Sigma", sep = "")) #to have the figures in the right directory
mytitle = paste("sigmaVar",".png", sep = "")