Commit 94ca888c authored by Reineking Bjoern's avatar Reineking Bjoern

Test version simnet

parent 92967c47
# Generated by roxygen2: do not edit by hand
export(allargs)
export(make_configuration)
export(make_simnet_simulation_df)
export(make_species_pools)
export(read_plantpars)
export(run_simnet)
export(write_plantpars)
importFrom(Rcpp,sourceCpp)
useDynLib(dissmodelr, .registration = TRUE)
#' Get function arguments with evaluated values
#'
#' @return List with function arguments
#' @export
allargs <- function() {
# simplified from:
......
......@@ -34,6 +34,7 @@
#' @param dewAmount TODO
#' @param dewDays TODO
#' @return Text with configuration file
#' @export
make_configuration <- function(repetitions = 1, gridWidth = 20, gridHeight = 20,
cellwidth = 0.25, numyears = 500, timeStepsPerYear = 365,
timestepLength = 1, soilDepth = 0.3, thetaSat = 0.4,
......
#' Make simulation data frame for simnet
#'
#' @param Model Name of model
#' @param Ninitial Initial number of species
#' @param Rep Replicate of species pool
#' @param SeedRain Percentage of reference seed rain
#' @param Stage Simulation stage
#' @param Wdh Stochastic replicate for given species pool
#' @return Data frame with simulation parameter values
#' @export
make_simnet_simulation_df <- function(Model = "bjoern",
Ninitial = as.integer(2^(0:6)),
Rep = 1:64,
SeedRain = c(0, 5, 10, 50, 100, 1000),
Stage = c("assembly"),
Wdh = 5) {
simulations <- expand.grid("Model" = Model,
"Ninitial" = Ninitial,
"Rep" = Rep,
"SeedRain" = SeedRain,
"Stage" = Stage,
"Wdh" = seq(Wdh), stringsAsFactors = FALSE)
simulations[-which(simulations$Ninitial == 64 & simulations$Rep > 1),]
}
#' Make simulation pools
#'
#' @param simulations Data frame with simulation parameters
#' @return Named list with species pools
#' @export
make_species_pools <- function(simulations) {
xx <- simulations[simulations$SeedRain == 0 & simulations$Wdh == 1, c("Ninitial", "Rep")]
pool <- apply(xx, 1, function(x) sort(sample.int(64, x[["Ninitial"]])))
# Need to check that there are no duplicates...
pool_size <- sapply(pool, length)
pool_size_1 <- which(pool_size == 1)
for (i in seq(pool_size_1)) {
pool[[pool_size_1[i]]] <- i
}
for (i in unique(pool_size)) {
if(any(duplicated(unname(sapply(pool[pool_size == i], function(x) x))))) {
stop("Duplicated species in pool size", i)
}
}
names(pool) <- paste0(formatC(xx$Ninitial, width = 2, flag = "0"),
"_",
formatC(xx$Rep, width = 2, flag = "0"))
pool
}
......@@ -2,6 +2,7 @@
#'
#' @param file File containing plant parameter values
#' @return Data frame with plant parameter values
#' @export
read_plantpars <- function(file) {
x <- readLines(file)
x <- gsub(" = ", " ", x)
......
#' Run simnet experiment
#'
#' @param wdh Number of replicates
#' @param base_path Base path for writing files
#' @param mean_annual_seedrain_monoculture_m2 Number of seeds
#' @param test Number of simulations to test (negative values mean use all)
#' @param mcseed Random number seed for simulation
#' @return Data frame with plant parameter values
#' @export
run_simnet <- function(wdh, base_path = "~/simnet",
mean_annual_seedrain_monoculture_m2 = 19402,
test = -1,
mcseed = 3) {
base_path <- path.expand(base_path)
simulations <- make_simnet_simulation_df(Wdh = wdh)
# return(split(simulations[, c("Ninitial", "Rep", "SeedRain", "Wdh")], factor(1:nrow(simulations)))[1:5])
set.seed(2)
pools <- make_species_pools(simulations)
species_64 <- read_plantpars(system.file("input/plantPars_64.txt", package = "dissmodelr"))
dir.create(base_path, showWarnings = FALSE)
input_path <- file.path(base_path, "input")
result_path <- file.path(base_path, "results")
dir.create(input_path, showWarnings = FALSE)
dir.create(result_path, showWarnings = FALSE)
# Write species parameter files
for (i in names(pools)) {
write_plantpars(species_64[pools[[i]], ],
file.path(input_path, paste0("plantPars_", i, ".txt")))
}
# Now do this in parallel
# library("future")
# library("future.apply")
run_single_simulation <- function(x) {
j <- paste0(formatC(x[["Ninitial"]], width = 2, flag = "0"),
"_",
formatC(x[["Rep"]], width = 2, flag = "0"))
plant_file <- file.path(input_path, paste0("plantPars_", j, ".txt"))
seed_multiplier <- x[["SeedRain"]]/100
file_base <- paste0(formatC(x[["Ninitial"]], width = 2, flag = "0"),
"_",
formatC(x[["Rep"]], width = 2, flag = "0"),
"_",
formatC(x[["SeedRain"]], width = 2, flag = "0"),
"_",
formatC(x[["Wdh"]], width = 2, flag = "0"))
expParFile <- file.path(base_path, paste0("ExpPars_", file_base, ".txt"))
writeLines(make_configuration(repetitions = 1,
plantParsFile = plant_file,
numyears = 1200,
seedrainIntensity = round(mean_annual_seedrain_monoculture_m2 * seed_multiplier) + 30,
seedrainYears = 600,
outputFilename = paste0("out_biomass_", file_base, ".txt")),
expParFile)
res <- dissmodelr:::simulate(expParFile)
i <- seq(3, 1200, by = 3)
# Extract key values
res <- list("biomass" = res$biomass[i,, drop = FALSE],
"productivity" = res$productivity[i,, drop = FALSE])
# Save as .rds
saveRDS(res, file.path(result_path, paste0("res_", file_base, ".rds")))
unlink(expParFile)
rm(res)
}
# plan(multiprocess)
# future_apply(simulations[1:10, c("Ninitial", "Rep", "SeedRain", "Wdh")],
# MARGIN = 1L, FUN = run_single_simulation,
# future.seed = 0xBEEF)
library("parallel")
RNGkind("L'Ecuyer-CMRG")
set.seed(3)
mc.reset.stream()
simulation_list <- split(simulations[, c("Ninitial", "Rep", "SeedRain", "Wdh")], factor(1:nrow(simulations)))
if (test > 0) simulation_list <- simulation_list[1:test]
mclapply(simulation_list, run_single_simulation, mc.cores = detectCores())
}
......@@ -2,6 +2,7 @@
#'
#' @param x Data framw with plant parameters
#' @param file Name of file to create
#' @export
write_plantpars <- function(x, file) {
text <- apply(x, 1, function(z) paste(colnames(x), z, sep = " = ", collapse = " "))
writeLines(text, file)
......
This diff is collapsed.
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/make_simnet_simulation_df.R
\name{make_simnet_simulation_df}
\alias{make_simnet_simulation_df}
\title{Make simulation data frame for simnet}
\usage{
make_simnet_simulation_df(
Model = "bjoern",
Ninitial = as.integer(2^(0:6)),
Rep = 1:64,
SeedRain = c(0, 5, 10, 50, 100, 1000),
Stage = c("assembly"),
Wdh = 5
)
}
\arguments{
\item{Model}{Name of model}
\item{Ninitial}{Initial number of species}
\item{Rep}{Replicate of species pool}
\item{SeedRain}{Percentage of reference seed rain}
\item{Stage}{Simulation stage}
\item{Wdh}{Stochastic replicate for given species pool}
}
\value{
Data frame with simulation parameter values
}
\description{
Make simulation data frame for simnet
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/make_species_pools.R
\name{make_species_pools}
\alias{make_species_pools}
\title{Make simulation pools}
\usage{
make_species_pools(simulations)
}
\arguments{
\item{simulations}{Data frame with simulation parameters}
}
\value{
Named list with species pools
}
\description{
Make simulation pools
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/run_simnet.R
\name{run_simnet}
\alias{run_simnet}
\title{Run simnet experiment}
\usage{
run_simnet(
wdh,
base_path = "~/simnet",
mean_annual_seedrain_monoculture_m2 = 19402,
test = -1,
mcseed = 3
)
}
\arguments{
\item{wdh}{Number of replicates}
\item{base_path}{Base path for writing files}
\item{mean_annual_seedrain_monoculture_m2}{Number of seeds}
\item{test}{Number of simulations to test (negative values mean use all)}
\item{mcseed}{Random number seed for simulation}
}
\value{
Data frame with plant parameter values
}
\description{
Run simnet experiment
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment