Commit 460ffa79 authored by Dorchies David's avatar Dorchies David
Browse files

Merge branch '4-import-initial-conditions-from-a-result-to-a-new-scenario-variant' into 'main'

Resolve "Import initial conditions from a result to a new scenario/variant"

Closes #4

See merge request !2
1 merge request!2Resolve "Import initial conditions from a result to a new scenario/variant"
Showing with 637 additions and 520 deletions
+637 -520
......@@ -19,4 +19,5 @@ Depends:
RoxygenNote: 7.1.1
Roxygen: list(markdown = TRUE)
Imports:
logger,
terra
# Generated by roxygen2: do not edit by hand
export(convert_sic_params)
export(loadConfig)
export(set_initial_conditions)
export(sic_run_fortran)
#' Convert a list of parameters into command line arguments
#'
#' @param params a [list] of [character] containing the parameters to send to the fortran program with format `list(param=value, ...)`
#' @template param_cfg
#'
#' @return A [character] with each parameter is converted to `[key param]=[value param]` and each parameter separated by a space character.
#' @export
#'
#' @examples
#' convert_sic_param(list(SCE = 1, VAR = 1))
#'
convert_sic_params <- function(params, cfg = loadConfig()) {
if (!"INTERF" %in% names(params)) {
params <- c(list(INTERF = cfg$sic$fortran$prms$INTERF), params)
}
params <- sapply(names(params), function(key) {
paste(key, params[[key]], sep= "=")
})
paste(params, collapse = " ")
}
#' Import initial conditions
#'
#' Import initial conditions from a scenario/variant result calculation to a new scenario/variant.
#'
#' @param params [numeric], arguments passed to Edisic for importing initial conditions. See details
#' @template param_cfg
#'
#' @details `params` [numeric] vector of length 5. Each number correspond to:
#'
#' 1. number of the scenario from which import initial conditions
#' 1. number of the variant from which import initial conditions (Use "0" for no variant)
#' 1. Time in seconds of the initial conditions to import (Use "0" for a single permanent simulation with no time steps)
#' 1. number of the scenario where the initial conditions are exported to
#' 1. number of the variant where the initial conditions are exported to (Use "0" for no variant)
#'
#' @export
#'
#' @examples
#' \dontrun{
#' # Import initial conditions from the scenario #1 without variant at time 0s
#' # to the variant #1 in the scenario #1
#' set_initial_conditions(c(1, 0, 0, 1, 1))
#' }
set_initial_conditions <- function(params, cfg = loadConfig()) {
if (!is.numeric(params) || length(params) != 5) {
stop("`params` should be a numeric of length 5")
}
sArgs <- paste(params, collapse = " ")
cmd_line <- shQuote(
paste(
file.path(cfg$sic$path, cfg$sic$edisic),
shQuote(dirname(cfg$project$path), type = "cmd"),
"CI",
shQuote(basename(cfg$project$path), type = "cmd"),
sArgs
),
type = "cmd2"
)
logger::log_debug(cmd_line)
shell(
cmd_line,
wait = T,
translate = T
)
}
......@@ -2,7 +2,7 @@
#'
#' @param prog [character], the program to run. Should be one of "talweg"
#' @param params [list] or [character], see details
#' @param cfg
#' @template param_cfg
#'
#' @details If argument `params` is a [list], arguments are injected in the command line by taking the items of the list with the conversion
#' `[key]=[value]`. If argument `params` is a [character]
......@@ -18,14 +18,17 @@
#'}
sic_run_fortran <- function(prog, params, cfg = loadConfig()) {
if (is.list(params)) params <- convert_sic_params(params, cfg)
cmd_line <- shQuote(
paste(
file.path(cfg$sic$path, cfg$sic[[prog]]),
shQuote(cfg$project$path, type = "cmd"),
params
),
type = "cmd2"
)
logger::log_debug(cmd_line)
shell(
shQuote(
paste(
file.path(cfg$sic$path, cfg$sic[[prog]]),
shQuote(cfg$project$path, type = "cmd"),
params
),
type = "cmd2"),
cmd_line,
wait = T,
translate = T
)
......
This diff is collapsed.
#' @param cfg a [config] object. Configuration to use. See [loadConfig] for details
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/convert_sic_params.R
\name{convert_sic_params}
\alias{convert_sic_params}
\title{Convert a list of parameters into command line arguments}
\usage{
convert_sic_params(params, cfg = loadConfig())
}
\arguments{
\item{params}{a \link{list} of \link{character} containing the parameters to send to the fortran program with format \code{list(param=value, ...)}}
\item{cfg}{a \link{config} object. Configuration to use. See \link{loadConfig} for details}
}
\value{
A \link{character} with each parameter is converted to \verb{[key param]=[value param]} and each parameter separated by a space character.
}
\description{
Convert a list of parameters into command line arguments
}
\examples{
convert_sic_param(list(SCE = 1, VAR = 1))
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/set_initial_conditions.R
\name{set_initial_conditions}
\alias{set_initial_conditions}
\title{Import initial conditions}
\usage{
set_initial_conditions(params, cfg = loadConfig())
}
\arguments{
\item{params}{\link{numeric}, arguments passed to Edisic for importing initial conditions. See details}
\item{cfg}{a \link{config} object. Configuration to use. See \link{loadConfig} for details}
}
\description{
Import initial conditions from a scenario/variant result calculation to a new scenario/variant.
}
\details{
\code{params} \link{numeric} vector of length 5. Each number correspond to:
\enumerate{
\item number of the scenario from which import initial conditions
\item number of the variant from which import initial conditions (Use "0" for no variant)
\item Time in seconds of the initial conditions to import (Use "0" for a single permanent simulation with no time steps)
\item number of the scenario where the initial conditions are exported to
\item number of the variant where the initial conditions are exported to (Use "0" for no variant)
}
}
\examples{
\dontrun{
# Import initial conditions from the scenario #1 without variant at time 0s
# to the variant #1 in the scenario #1
set_initial_conditions(c(1, 0, 0, 1, 1))
}
}
......@@ -11,7 +11,7 @@ sic_run_fortran(prog, params, cfg = loadConfig())
\item{params}{\link{list} or \link{character}, see details}
\item{cfg}{}
\item{cfg}{a \link{config} object. Configuration to use. See \link{loadConfig} for details}
}
\value{
......
# Set local configuration for SIC tests
xml_path <- tempfile("sic_project", fileext = ".xml")
file.copy(system.file("sic_project_test1.xml", package = "rsic2"),
xml_path,
overwrite = TRUE)
cfg <- loadConfig(xml_path = xml_path)
loadLocalConfig <- function() {
xml_path <- tempfile("sic_project", fileext = ".xml")
file.copy(system.file("sic_project_test1.xml", package = "rsic2"),
xml_path,
overwrite = TRUE)
loadConfig(xml_path = xml_path)
}
skip_on_ci()
test_that("set_initial_conditions works", {
cfg <- loadLocalConfig()
sic_run_fortran("fluvia", list(SCE = 1), cfg = cfg)
set_initial_conditions(c(1, 0, 0, 1, 1), cfg = cfg)
sic_run_fortran("sirene", list(SCE = 1, VAR = 1), cfg = cfg)
expect_true(file.exists(gsub("\\.xml", "_1_1.res", cfg$project$path)))
})
skip_on_ci()
test_that("fluvia on SCE=1 should create a binary result file", {
cfg <- loadLocalConfig()
sic_run_fortran("fluvia", list(SCE = 1), cfg = cfg)
expect_true(file.exists(gsub("\\.xml", "_1_0.res", cfg$project$path)))
expect_true(file.exists(gsub("\\.xml", "_1_0.rci", cfg$project$path)))
......
Supports Markdown
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