forked from JW-spec-tech/S_land_bio_sim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRun_simulation_Generate_DATA_ONLY.R
120 lines (94 loc) · 3.11 KB
/
Run_simulation_Generate_DATA_ONLY.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
#### Load packages ####
library(arrow)
library(NLMR)
library(sf)
library(raster)
library(fasterize)
library(sspm)
library(rgeos)
library(tidyr)
library(readr)
library(dplyr)
library(mgcv)
library(readr)
library(foreach)
library(doParallel)
library(parallelly)
library(ranger)
library(tidyverse)
library(kableExtra)
#### Load the Functions ####
source(file = "Simulation_Depth_Temp_Coordinates_Biomass_function_V2.R")
source(file = "function_patches.R")
source(file = "Make_local.domain_local.arena.R")
source(file = "Strap_calculations.R")
source(file = "Make_PB_fall.dat.R")
# source(file = "Resampling_test.R")
# source(file = "Run_GAM_predict_replicates.R")
print(paste("Start of Sim generation @",Sys.time(),"test"))
reps= as.numeric(Sys.getenv('REPS')) # Number of Replicates
sims= as.numeric(Sys.getenv('SIMS')) # Number of Sims
size= as.numeric(Sys.getenv('SIZE')) # Size of the Landscape
seed= as.numeric(Sys.getenv('SEED')) # Starting seed
var = as.numeric(Sys.getenv('VAR')) # Variation in biomass field --> higher variation = increased biomass variation
percent = as.numeric(Sys.getenv('PERCENT')) # Sets sampling percentage of the sampling of the entire dataset
#### 1. Create and Start Cluster ####
print(paste("Start of Sim generation @",Sys.time(),"test"))
#create the cluster
# n.cores <- parallelly::availableCores()/2
# For windows
n.cores <- as.numeric(Sys.getenv('OMP_NUM_THREADS'))
main.cluster <- parallel::makeCluster(
n.cores,
type = "PSOCK"
)
#check cluster definition (optional)
print(main.cluster)
#register it to be used by %dopar%
doParallel::registerDoParallel(cl = main.cluster)
#check if it is registered (optional)
foreach::getDoParRegistered()
#how many workers are available? (optional)
foreach::getDoParWorkers()
dir_now <- paste0(getwd(),"/Variation_",var)
dir.create(dir_now)
setwd(paste0(dir_now,"/"))
# dir.create(dir_now)
# setwd("Data_test/")
print(dir_now)
print(paste("Start of Sim generation @",Sys.time(),"Variation",var))
cwd <- getwd()
cwd
foreach(
rep = 1:reps,
.packages = c('mgcv','dplyr','purrr','NLMR','arrow','sspm','raster','foreach','doParallel','parallelly','readr','fasterize')
) %dopar% {
setwd(paste0(dir_now,"/"))
print(paste("Replicate #",rep))
seeds = seed - 10 + rep
set.seed(seeds)
newdir <- paste("Run",rep,"Size",size,"seed",seeds,"nsim",sims,"Percent",percent,Sys.Date(),sep = "_")
dir.create(newdir) # Create new directory
setwd(paste0(newdir,"/"))
cwd=getwd()
cwd
write.table(as.data.frame(newdir),"seed")
#### 1. Run the sim ####
results <- S_land_bio_sim(n=sims,size=size,variation = var) # higher variation = increased biomass variation
# Save size of each strata
patches=results$patches_list$patches
patches=st_set_geometry(patches,NULL)
write_parquet(patches,"patches")
gc()
#### 2. Write the ogmap files ####
Make_patch_domain_arena_DAT(size,patches=results$patches_list$patches,the_stack=results$the_stack,percent=percent)
gc()
#### 3. Slice data file ####
Make_PB_fall.dat()
gc()
#### 4. Run STRAP calculations ####
STRAP()
gc()
}
#### Kill the cluster
parallel::stopCluster(cl = main.cluster)