Commit a58d8f73 authored by Dorchies David's avatar Dorchies David
Browse files

Merge branch '9-read-result-simulation-in-sections-from-a-binary-result-files' into 'main'

Resolve "Read result simulation in sections from a binary result files"

Closes #9

See merge request !6
1 merge request!6Resolve "Read result simulation in sections from a binary result files"
Pipeline #33965 passed with stage
in 2 minutes and 46 seconds
Showing with 998 additions and 21 deletions
+998 -21
^.*\.Rproj$ ^.*\.Rproj$
^\.Rproj\.user$ ^\.Rproj\.user$
^data-raw$ ^data-raw$
^man-roxygen$
^LICENSE\.md$
[.]INI$
^\.gitlab-ci\.yml$
^ci$
stages:
- check
variables:
R_CI: "$CI_PROJECT_DIR/ci"
R_LIBS_USER: "$R_CI/lib"
default:
tags: [docker]
image: rocker/verse:devel
cache:
paths:
- $R_CI
before_script:
- mkdir -p $R_LIBS_USER
- echo "R_LIBS='$R_LIBS_USER'" > .Renviron
- sudo apt-get update && sudo apt-get install -y libudunits2-dev proj-bin libgdal-dev libgeos-dev
- R -q -e 'remotes::install_deps(dependencies = TRUE)'
check:
stage: check
script:
- tlmgr update --self && tlmgr install ec epstopdf-pkg
- R -q -e 'remotes::update_packages("rcmdcheck")'
- R -q -e 'rcmdcheck::rcmdcheck(args = "--as-cran", error_on = "error")'
test_all:
stage: check
script:
- R -q -e 'devtools::test()'
...@@ -6,18 +6,23 @@ Authors@R: c( ...@@ -6,18 +6,23 @@ Authors@R: c(
person("David", "Dorchies", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-6595-7984"), email = "david.dorchies@inrae.fr") person("David", "Dorchies", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-6595-7984"), email = "david.dorchies@inrae.fr")
) )
Description: SIC^2 software (Simulation and Integration of Control for Canals (or Channels)) is a hydraulic simulation software adapted to the calculation of flows in irrigation canals, rivers and sewage systems (<https://sic.g-eau.fr/>). This package provides functions to automate the use of SIC2 with R. Description: SIC^2 software (Simulation and Integration of Control for Canals (or Channels)) is a hydraulic simulation software adapted to the calculation of flows in irrigation canals, rivers and sewage systems (<https://sic.g-eau.fr/>). This package provides functions to automate the use of SIC2 with R.
License: What license is it under? License: AGPL (>= 3)
Encoding: UTF-8 Encoding: UTF-8
LazyData: true LazyData: true
Suggests: Suggests:
R.utils, R.utils,
RandomFields, RandomFields,
testthat (>= 3.0.0) testthat (>= 3.0.0),
yaml
Config/testthat/edition: 3 Config/testthat/edition: 3
Depends: Depends:
R (>= 2.10) R (>= 2.10)
RoxygenNote: 7.1.2 RoxygenNote: 7.1.2
Roxygen: list(markdown = TRUE) Roxygen: list(markdown = TRUE)
Imports: Imports:
config,
logger, logger,
terra magrittr,
terra,
tidyquery,
xml2
LICENSE.md 0 → 100644
This diff is collapsed.
# Generated by roxygen2: do not edit by hand # Generated by roxygen2: do not edit by hand
export(cfg_tmp_project)
export(convert_sic_params) export(convert_sic_params)
export(create_section_txt) export(create_section_txt)
export(create_uniform_reach_txt) export(create_uniform_reach_txt)
...@@ -7,10 +8,18 @@ export(dem_to_reach) ...@@ -7,10 +8,18 @@ export(dem_to_reach)
export(dem_to_reach_txt) export(dem_to_reach_txt)
export(dem_to_section) export(dem_to_section)
export(extract_reach) export(extract_reach)
export(get_result)
export(get_result_tree)
export(get_section_centers) export(get_section_centers)
export(loadConfig) export(loadConfig)
export(merge_reaches) export(merge_reaches)
export(read_bin_result_matrix)
export(set_initial_conditions) export(set_initial_conditions)
export(sic_import_reaches)
export(sic_run_export) export(sic_run_export)
export(sic_run_fortran) export(sic_run_fortran)
export(split_reach) export(split_reach)
import(magrittr)
import(utils)
import(xml2)
importFrom(stats,dist)
#' Set a configuration with a temporary project directory
#'
#' @param xml_path [character], the path of the XML SIC project file
#' @param cfg [config], the configuration to modify
#'
#' @return The updated configuration with the temporary project directory
#' @export
#'
#' @examples
#' \dontrun{
#' cfg <- cfg_tmp_project()
#' cfg$project$xml_path
#' }
#'
cfg_tmp_project <- function(xml_path = system.file("sic_project_test1.xml", package = "rsic2"), cfg = loadConfig(xml_path = xml_path)) {
cfg$project$path <- tempfile("sic_project", fileext = ".xml")
file.copy(xml_path,
cfg$project$path,
overwrite = TRUE)
return(cfg)
}
...@@ -11,8 +11,10 @@ ...@@ -11,8 +11,10 @@
#' @export #' @export
#' #'
#' @examples #' @examples
#' convert_sic_params(list(SCE = 1, VAR = 1)) #' \dontrun{
#' #' cfg <- cfg_tmp_project()
#' convert_sic_params(list(SCE = 1, VAR = 1), cfg = cfg)
#' }
convert_sic_params <- function(params, cfg = loadConfig()) { convert_sic_params <- function(params, cfg = loadConfig()) {
if (!"INTERF" %in% names(params)) { if (!"INTERF" %in% names(params)) {
params <- c(list(INTERF = cfg$sic$fortran$prms$INTERF), params) params <- c(list(INTERF = cfg$sic$fortran$prms$INTERF), params)
......
...@@ -8,9 +8,10 @@ ...@@ -8,9 +8,10 @@
#' #'
#' @return [character], section description in SIC text import format. #' @return [character], section description in SIC text import format.
#' @export #' @export
#' @import utils
#' @examples #' @examples
#' # Trapezoidal section #' # Trapezoidal section
#' export_section_txt("Trapeze", 0, "T", list(B = 2, S = 1.5, ZF = 100, ZB = 102)) #' create_section_txt("Trapeze", 0, "T", list(B = 2, S = 1.5, ZF = 100, ZB = 102))
create_section_txt <- function(section_name, abscissa, section_type, profile, distance_majeur = FALSE) { create_section_txt <- function(section_name, abscissa, section_type, profile, distance_majeur = FALSE) {
if (section_type == "T") { if (section_type == "T") {
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
#' @param abscissas [numeric] vector of section abscissas #' @param abscissas [numeric] vector of section abscissas
#' @param upstream_bed_elevation [numeric], upstream bed elevation (m) #' @param upstream_bed_elevation [numeric], upstream bed elevation (m)
#' @param slope [numeric], bed slope of the reach (m/m) #' @param slope [numeric], bed slope of the reach (m/m)
#' @param names [character] vector of section names #' @param section_names [character] vector of section names
#' @inheritParams create_section_txt #' @inheritParams create_section_txt
#' #'
#' @return A [list] from which each item is a section exported by [create_section_txt]. #' @return A [list] from which each item is a section exported by [create_section_txt].
......
...@@ -10,9 +10,10 @@ ...@@ -10,9 +10,10 @@
#' @param start 1-length [numeric], starting value for the chainage (i.e. section abscissa) along the reach #' @param start 1-length [numeric], starting value for the chainage (i.e. section abscissa) along the reach
#' @param major_bed [logical], `TRUE` for major bed, `FALSE` for minor-medium bed #' @param major_bed [logical], `TRUE` for major bed, `FALSE` for minor-medium bed
#' #'
#' @return #' @return A *ReachTxt* object which is a [list] of *SectionTxt* objects (see [create_section_txt]).
#' @rdname dem_to_reach #' @rdname dem_to_reach
#' @export #' @export
#' @importFrom stats dist
#' #'
#' @examples #' @examples
#' ## Inputs preparation #' ## Inputs preparation
...@@ -60,7 +61,9 @@ dem_to_reach_txt <- function(dem, node_coords, space_step, section_width, nb_poi ...@@ -60,7 +61,9 @@ dem_to_reach_txt <- function(dem, node_coords, space_step, section_width, nb_poi
return(reach_txt) return(reach_txt)
} }
#' @rdname dem_to_reach #' @rdname dem_to_reach
#' @param section_centers See return value of [get_section_centers]
#' @export #' @export
dem_to_reach <- function(dem, node_coords, section_centers, section_width, nb_points = 50) { dem_to_reach <- function(dem, node_coords, section_centers, section_width, nb_points = 50) {
lapply(seq_len(nrow(section_centers)), function(i) { lapply(seq_len(nrow(section_centers)), function(i) {
...@@ -75,7 +78,7 @@ dem_to_reach <- function(dem, node_coords, section_centers, section_width, nb_po ...@@ -75,7 +78,7 @@ dem_to_reach <- function(dem, node_coords, section_centers, section_width, nb_po
#' @inheritParams dem_to_reach #' @inheritParams dem_to_reach
#' @param section_center 2-lenght [numeric], coordinates of the section center #' @param section_center 2-lenght [numeric], coordinates of the section center
#' #'
#' @return #' @return A [matrix] with the coordinates of the x-z points in the cross-profile section referential
#' @export #' @export
#' #'
#' @inherit dem_to_reach return examples #' @inherit dem_to_reach return examples
......
R/get_result.R 0 → 100644
#' Get a selection of variables from a simulation result
#'
#' @inheritParams sic_run_export
#' @param filters [character] conditions to select columns in result table, see details
#' @param m [matrix] of results produced by [read_bin_result_matrix]
#'
#' @return [matrix] of results with columns selected by `filters`.
#' @export
#' @import magrittr
#'
#' @examples
#' \dontrun{
#' cfg <- cfg_tmp_project()
#' sic_run_fortran("fluvia", list(SCE = 1), cfg = cfg)
#' get_result(cfg, 1, filters = c("bf==4", "var=='Z'"))
#' }
get_result <- function(cfg,
scenario,
variant = 0,
filters = c(""),
m = read_bin_result_matrix(cfg, scenario, variant)) {
df_col <- get_result_tree(cfg, scenario, variant)
filters <- paste(filters, collapse = " AND ")
if (filters != "") {
df_col %<>% tidyquery::query(paste("SELECT * WHERE", filters))
}
m <- m[, df_col$col, drop = FALSE]
column_names <- sapply(seq_len(nrow(df_col)),
function(i) {
df_col$col <- NULL
cols <- sapply(names(df_col),
function(name) {
if (df_col[i, name] > 0) {
paste(name, df_col[i, name], sep = ":")
} else {
NULL
}
})
cols[sapply(cols, is.null)] <- NULL
paste(cols, collapse = "|")
})
colnames(m) <- column_names
return(m)
}
#' Read matrix of SIC simulation result file
#'
#' @inheritParams sic_run_export
#'
#' @return [matrix] with the simulation results
#' @export
#'
#' @examples
#' \dontrun{
#' cfg <- cfg_tmp_project()
#' sic_run_fortran("fluvia", list(SCE = 1), cfg = cfg)
#' m <- read_bin_result_matrix(cfg, 1)
#' str(m)
#' }
read_bin_result_matrix <- function(cfg, scenario, variant = 0) {
file <- paste0(
paste(gsub("\\.xml", "", cfg$project$path),
scenario, variant, sep = "_"),
".res"
)
con = file(file, "rb")
# Skip header
readBin(con, "raw", n = 4 * 5 + 4 + 8 + 4 + 4)
dims <-
readBin(con,
"integer",
n = 2,
size = 4,
endian = "little")
# Skip (data type code?)
readBin(con, "raw", n = 2)
data <-
readBin(con,
"double",
n = prod(dims),
size = 4,
endian = "little")
readBin(con, "raw", n = 4) # @todo check end file content
close(con)
return(matrix(data, ncol = dims[2], byrow = TRUE))
}
#' Get correspondence between network object and columns in result binary file
#'
#' @inheritParams sic_run_export
#'
#' @return a [data.frame] with following columns:
#'
#' - "bf", "sn", "nd", "pr", "ouv": location of the result with number of respectively reach, section, node, offtake, and device.
#' - "var": the name of the calculated variable
#' - "col": the column number in the matrix produced by [read_bin_result_matrix]
#'
#' @warning
#' Up to now, this function only handle results at sections.
#'
#' @export
#' @import xml2
#' @import magrittr
#'
#' @examples
#' \dontrun{
#' cfg <- cfg_tmp_project()
#' sic_run_fortran("fluvia", list(SCE = 1), cfg = cfg)
#' df <- get_result_tree(cfg, 1)
#' head(df)
#' }
get_result_tree <- function(cfg, scenario, variant = 0) {
x <- read_xml(cfg$project$path)
objs = c("Ouvrage", "Section", "Prise", "Noeud")
names(objs) <- objs
defcol <- get_DefCol(x, scenario, variant)
df <- data.frame(bf = integer(),
sn = integer(),
nd = integer(),
pr = integer(),
ouv = integer(),
var = character(),
col = integer())
# Sections
xpath_biefs <- "/Reseau/Liste_Biefs/Bief"
for (iBf in seq_along(xml_find_all(x, xpath_biefs))) {
xpath_sections <- paste0(xpath_biefs, sprintf("[@Num=%d]/Liste_Sections/SectionMin", iBf))
for (iSn in seq_along(xml_find_all(x, xpath_sections))) {
xpath_res <- paste0(xpath_sections, "[@Num=%d]/Flu[@nScenario=%d]/ListeRes/Res[@nVar=%d]") %>%
sprintf(iSn, scenario, variant)
cols <- x %>% xml_find_first(xpath_res) %>%
xml_attr("nCol") %>% strsplit(":") %>% "[["(1) %>% as.integer
cols <- seq(from = cols[1], length.out = cols[2])
df %<>% result_tree_add(list(bf = iBf, sn = iSn),
defcol$Section,
cols)
}
}
return(df)
}
result_tree_add <- function(df, loc, defcol, cols) {
loc <- modifyList(list(bf = 0, sn = 0, nd = 0, pr = 0, ouv = 0),
loc)
return(rbind(df, data.frame(as.data.frame(loc), var = defcol, col = cols)))
}
get_DefCol <-
function(x,
scenario,
variant,
xpath = "/Reseau/Flu[@nScenario=%d]/ListeRes/Res[@nVar=%d]/ListeDefCol/DefCol[@Objet=\"%s\"]",
objs = c("Ouvrage", "Section", "Prise", "Noeud")) {
names(objs) <- objs
lapply(objs, function(obj)
get_text_xml_path(x, xpath, scenario, variant, obj))
}
get_text_xml_path <- function(x, xpath, scenario, var, obj) {
x %>% xml_find_first(xpath = sprintf(xpath, scenario, var, obj)) %>%
xml_text %>% strsplit("\t") %>% "[["(1)
}
#' Merge several ReachTxt objects into one #' Merge several *ReachTxt* objects into one
#' #'
#' @param ... ReachTxt objects #' @param ... *ReachTxt* objects
#' #'
#' @return #' @return a *ReachTxt* object (See [create_uniform_reach_txt] and [dem_to_reach]) containing the merged reaches.
#' @export #' @export
#' #'
#' @examples #' @examples
......
#' Import reach geometries into a SIC project
#'
#' @param reaches A [list] of ReachTxt objects (See [split_reach])
#' @param import_mode [character], importation mode of EdiSIC (See \url{https://sic.g-eau.fr/Import-sections-in-text-format?lang=en#import-modes-2})
#' @template param_cfg
#'
#' @return A [numeric] code returned by [shell]
#' @export
#'
#' @examples
#' # Minor bed generation
#' profT <- matrix(c(2, 6, 0, 2), ncol = 2)
#' min_reach <- create_uniform_reach_txt(abscissas = seq(0, 10000, 100),
#' upstream_bed_elevation = 8 + 10000 * 0.002,
#' slope = 0.002,
#' section_type = "L",
#' profile = profT)
#'
#' # Major bed generation
#' data("floodam_ead_dem")
#' dem <- terra::rast(floodam_ead_dem)
#' node_coords <- matrix(c(102550, 102550, 110000, 100000), ncol = 2)
#' space_step = 100
#' section_width = 5000
#' maj_reach <- dem_to_reach_txt(dem, node_coords, space_step, section_width, major_bed = TRUE)
#'
#' # Merge minor and major beds and split into 2 reaches
#' reach <- merge_reaches(min_reach, maj_reach)
#' reaches <- split_reach(reach, seq(0, 10000, 5000))
#'
#' \dontrun{
#' # Import with EdiSic
#' cfg <- cfg_tmp_project()
#' sic_import_reaches(reaches, cfg = cfg)
#' }
sic_import_reaches <- function(reaches, import_mode = "ImportXml_UPDATE", cfg = loadConfig()) { sic_import_reaches <- function(reaches, import_mode = "ImportXml_UPDATE", cfg = loadConfig()) {
# Create reach files # Create reach files
import_path <- dirname(cfg$project$path) import_path <- dirname(cfg$project$path)
......
...@@ -7,14 +7,15 @@ ...@@ -7,14 +7,15 @@
#' @details If argument `params` is a [list], arguments are injected in the command line by taking the items of the list with the conversion #' @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] #' `[key]=[value]`. If argument `params` is a [character]
#' #'
#' @return #' @return Error code returned by [shell].
#' @export #' @export
#' #'
#' @examples #' @examples
#' \dontrun{ #' \dontrun{
#' # Run steady simulation for the scenario #1 #' # Run steady simulation for the scenario #1
#' cfg <- cfg_tmp_project()
#' params <- list(SCE=1) #' params <- list(SCE=1)
#' sic_run_fortran("fluvia", params) #' sic_run_fortran("fluvia", params, cfg = cfg)
#'} #'}
sic_run_fortran <- function(prog, params = list(), cfg = loadConfig()) { sic_run_fortran <- function(prog, params = list(), cfg = loadConfig()) {
if (is.list(params)) params <- convert_sic_params(params, cfg) if (is.list(params)) params <- convert_sic_params(params, cfg)
...@@ -27,9 +28,11 @@ sic_run_fortran <- function(prog, params = list(), cfg = loadConfig()) { ...@@ -27,9 +28,11 @@ sic_run_fortran <- function(prog, params = list(), cfg = loadConfig()) {
type = "cmd2" type = "cmd2"
) )
logger::log_debug(cmd_line) logger::log_debug(cmd_line)
shell( ret <- shell(
cmd_line, cmd_line,
wait = T, wait = T,
translate = T translate = T
) )
file.remove("FLUVIA.INI", "SIRENE.INI")
return(ret)
} }
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/cfg_tmp_project.R
\name{cfg_tmp_project}
\alias{cfg_tmp_project}
\title{Set a configuration with a temporary project directory}
\usage{
cfg_tmp_project(
xml_path = system.file("sic_project_test1.xml", package = "rsic2"),
cfg = loadConfig(xml_path = xml_path)
)
}
\arguments{
\item{xml_path}{\link{character}, the path of the XML SIC project file}
\item{cfg}{\link{config}, the configuration to modify}
}
\value{
The updated configuration with the temporary project directory
}
\description{
Set a configuration with a temporary project directory
}
\examples{
\dontrun{
cfg <- cfg_tmp_project()
cfg$project$xml_path
}
}
...@@ -21,6 +21,8 @@ This function is called by \link{sic_run_fortran} to convert list of parameters ...@@ -21,6 +21,8 @@ This function is called by \link{sic_run_fortran} to convert list of parameters
The parameter \code{INTERF} is set to 0 (zero) by default. The parameter \code{INTERF} is set to 0 (zero) by default.
} }
\examples{ \examples{
convert_sic_param(list(SCE = 1, VAR = 1)) \dontrun{
cfg <- cfg_tmp_project()
convert_sic_params(list(SCE = 1, VAR = 1), cfg = cfg)
}
} }
...@@ -31,5 +31,5 @@ Export a section in importation SIC format ...@@ -31,5 +31,5 @@ Export a section in importation SIC format
} }
\examples{ \examples{
# Trapezoidal section # Trapezoidal section
export_section_txt("Trapeze", 0, "T", list(B = 2, S = 1.5, ZF = 100, ZB = 102)) create_section_txt("Trapeze", 0, "T", list(B = 2, S = 1.5, ZF = 100, ZB = 102))
} }
...@@ -24,7 +24,7 @@ create_uniform_reach_txt( ...@@ -24,7 +24,7 @@ create_uniform_reach_txt(
\item{profile}{\link{list} or \link{matrix}, profile of the section (See details)} \item{profile}{\link{list} or \link{matrix}, profile of the section (See details)}
\item{names}{\link{character} vector of section names} \item{section_names}{\link{character} vector of section names}
} }
\value{ \value{
A \link{list} from which each item is a section exported by \link{create_section_txt}. A \link{list} from which each item is a section exported by \link{create_section_txt}.
......
...@@ -31,9 +31,11 @@ dem_to_reach(dem, node_coords, section_centers, section_width, nb_points = 50) ...@@ -31,9 +31,11 @@ dem_to_reach(dem, node_coords, section_centers, section_width, nb_points = 50)
\item{start}{1-length \link{numeric}, starting value for the chainage (i.e. section abscissa) along the reach} \item{start}{1-length \link{numeric}, starting value for the chainage (i.e. section abscissa) along the reach}
\item{major_bed}{\link{logical}, \code{TRUE} for major bed, \code{FALSE} for minor-medium bed} \item{major_bed}{\link{logical}, \code{TRUE} for major bed, \code{FALSE} for minor-medium bed}
\item{section_centers}{See return value of \link{get_section_centers}}
} }
\value{ \value{
A \emph{ReachTxt} object which is a \link{list} of \emph{SectionTxt} objects (see \link{create_section_txt}).
} }
\description{ \description{
The coordinate system of \code{dem} should be a metric orthonormal coordinate system. The coordinate system of \code{dem} should be a metric orthonormal coordinate system.
......
...@@ -18,7 +18,7 @@ dem_to_section(dem, node_coords, section_center, section_width, nb_points = 50) ...@@ -18,7 +18,7 @@ dem_to_section(dem, node_coords, section_center, section_width, nb_points = 50)
\item{nb_points}{1-length \link{numeric}, number of points to describe cross-section geometries} \item{nb_points}{1-length \link{numeric}, number of points to describe cross-section geometries}
} }
\value{ \value{
A \link{matrix} with the coordinates of the x-z points in the cross-profile section referential
} }
\description{ \description{
Create a section cross profile from a DEM Create a section cross profile from a DEM
......
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