This example demonstrates how to run a simple model in RSMinerve from R.
The tutorial is adapted from Visual Basic scripts provided by Javier Fluixa Sanmartin (CREALP).
Install R, RTools, RStudio, RSMinerve and the R package rClr (see README for instructions and links).
Copy this vignette to your local computer and adapt the paths.
Open the model in and load & link the data base with the model objects. If you don’t know how to do this please follow the examples in the RS Minerve User Manual prior to continuing with this tutorial.
# Adapt path in dir_RSM_install to your local installation of RSMinerve.
dir_RSM_install <- "C:/Program Files (x86)/RS MINERVE"
clrLoadAssembly(file.path(dir_RSM_install, 'log4net.dll'))
clrLoadAssembly(file.path(dir_RSM_install, 'Microsoft.Practices.Prism.Mvvm.dll'))
clrLoadAssembly(file.path(dir_RSM_install, 'RSMinerve.RS.dll'))
clrLoadAssembly(file.path(dir_RSM_install, 'RSMinerve.DB.dll'))
clrLoadAssembly(file.path(dir_RSM_install, 'RSMinerve.Base.dll'))
model_file <- if (file.exists("../inst/extdata/Tutorial_Model.rsm")) {
"../inst/extdata/Tutorial_Model.rsm"
} else {
"https://raw.githubusercontent.com/hydrosolutions/RSMinerveR/blob/main/inst/extdata/Tutorial_Model.rsm"
}
input_dataset <- if (file.exists("../inst/extdata/Tutorial_DataMeteo.dsx")) {
"../inst/extdata/Tutorial_DataMeteo.dsx"
} else {
"https://raw.githubusercontent.com/hydrosolutions/RSMinerveR/blob/main/inst/extdata/Tutorial_DataMeteo.dst"
}
saveDataInDstFile <- TRUE
# The RS Minerve documentation describes the date format to be "%d.%m.%Y".
start_date <- "02.09.2013 00:00:00" # format = "%d.%m.%Y %H:%M:%S"
end_date <- "09.09.2013 00:00:00" # format = "%d.%m.%Y %H:%M:%S"
simulationTimeStep <- "600"
recordingTimeStep <- "3600"
timeStepUnit <- "Seconds"
The paths for the result files need to be full paths. Below are example paths. You’ll need to adapt these.
results_savePath <-
"C:/Users/<username>/Documents/scriping_rsm/tutorial01-results.dsx"
presimreport_path <-
"C:/Users/<username>/Documents/scriping_rsm/tutorial01-preSimuReport.txt"
The commands allow to call the Visual Basics commands documented in the RS Minerve Technical Manual. The calls return NULL when successful. Check your local paths to see if the output files have been created.
# Define a clr task
rsm <- clrNew("RSMinerve.RS.Task")
# Start-up the model
clrCall(rsm, "Start", model_file)
#> NULL
# Load data
clrCall(rsm, "LoadDatasetAndSetDates", input_dataset, TRUE, TRUE)
#> NULL
# Set simulation parameters
clrCall(rsm, "SetDates", start_date, end_date)
#> NULL
# SetSimulationTimeStep and SetRecordingTimeStep require characters
clrCall(rsm, "SetSimulationTimeStep", simulationTimeStep, timeStepUnit)
#> NULL
clrCall(rsm, "SetRecordingTimeStep", recordingTimeStep, timeStepUnit)
#> NULL
# Write a pre-simulation report to check for possible errors (requires full
# path)
clrCall(rsm, "SavePreSimulationReportAs", presimreport_path)
#> NULL
# Run a simulation
clrCall(rsm, "Simulate")
#> NULL
# Save results (requires full path)
clrCall(rsm, "SaveFullResultsAs", results_savePath, saveDataInDstFile)
#> NULL
# Stop task
clrCall(rsm, "Stop")
#> NULL
# Calculate the number of time steps to read for each model component, i.e. the
# chunk size. Including the header of each chunk.
chunk_size <- getChunkSize(
lubridate::as_datetime(start_date,format = "%d.%m.%Y %H:%M:%S"),
lubridate::as_datetime(end_date, format = "%d.%m.%Y %H:%M:%S"),
recordingTimeStep
)
result_file_path <- normalizePath(file.path("..", "tests", "testthat",
"Tutorial01-results.dst"))
result <- readResultDST(result_file_path, chunk_size)