-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun_retrospectives.R
68 lines (62 loc) · 2.37 KB
/
run_retrospectives.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
# Script for performing retrospective analyses on BASA
# Joshua Zahner | 05/17/2022
#
# Creates a new subdirectory ('retrospectives/') and runs
# BASA on successivley fewer years of data (up to a provided
# number of 'peels'). Final biomass estimates and recrtuiment
# deviates are aggregated and analysed for bias patterns relative
# to the most recent (current year) full BASA run.
library(here)
library(doParallel)
library(tidyverse)
library(icesAdvice)
setwd(here::here())
source(file=file.path(here::here(), "functions/fun_read_dat.R"))
source(file=file.path(here::here(), "functions/fun_write_dat.R"))
source(file=file.path(here::here(), "functions/run_basa.R"))
source(file=file.path(here::here(), "plotting/compute_plot_products.R"))
n.peels <- 5
main.basa.directory <- paste0(here::here("model/"))
run.parallel <- FALSE
total.cores <- parallel::detectCores()
parallel.runs <- (total.cores-1) %/% 4 # BASA runs using 4 nodes
run.retropspective <- function(i, ...){
new.dir.name <- paste0("basa-", i)
if(!dir.exists(new.dir.name)){
dir.create(new.dir.name)
}
setwd(new.dir.name)
if(!file.exists("mcmc_out/PFRBiomass.csv")){
print(paste("mcmc_out directory does not exist in", new.dir.name))
file.symlink(paste0(main.basa.directory, "PWS_ASA.TPL"), ".")
file.symlink(paste0(main.basa.directory, "PWS_ASA(par).ctl"), ".")
file.symlink(paste0(main.basa.directory, "PWS_ASA(phases).ctl"), ".")
file.symlink(paste0(main.basa.directory, "PWS_ASA(sim_settings).ctl"), ".")
dat.files <- read.data.files(main.basa.directory)
fun_write_dat(dat.files, i)
run.basa(paste0("retrospectives/", new.dir.name))
}else{
print("mcmc_out directory already exists")
}
setwd("..")
}
# Set up retrospectives directory
if(!dir.exists("retrospectives/")){
dir.create("retrospectives/")
}
setwd("retrospectives/")
# Need to copy and then modify all of input files so they only reflect the data
# that was available in that year. Then actually run the assessment on all of
# the old datasets. Do this in parallel to speed things up.
if(run.parallel){
cluster <- parallel::makeCluster(parallel.runs, type="FORK")
registerDoParallel(cluster)
foreach(i=1:n.peels) %dopar% {
run.retropspective(i)
}
stopCluster(cluster)
}else{
for(i in 1:5){
run.retropspective(i=i)
}
}