Load parameters and initial conditions from files

This example shows how to load parameters and initial conditions.

The tutorial is adapted from Visual Basic scripts provided by Javier Fluixa Sanmartin (CREALP).

Prerequisites

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.

Open the Selection and plots tab by clicking on its icon in the in the modules toolbar. Define a selection of model results to visualize (here we define two result groups: DischargeGlacier and SnowWaterEquivalent. Save the model.

Loading Assemblies

# 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 settings

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"
}
parameter_file <- if (file.exists("../inst/extdata/Tutorial_Parameters.txt")) {
   "../inst/extdata/Tutorial_Parameters.txt"
} else {
  "https://raw.githubusercontent.com/hydrosolutions/RSMinerveR/blob/main/inst/extdata/Tutorial_Parameters.txt"
}
ic_file <- if (file.exists("../inst/extdata/Tutorial_IC.txt")) {
   "../inst/extdata/Tutorial_IC.txt"
} else {
   "https://raw.githubusercontent.com/hydrosolutions/RSMinerveR/blob/main/inst/extdata/Tutorial_IC.txt"
}


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_GlacierDischarge_savePath <- 
  "C:/Users/<username>/Documents/scriping_rsm/tutorial03-results_GlacierDischarge.dsx"
results_SWE_savePath <- 
  "C:/Users/<username>/Documents/scriping_rsm/tutorial03-results_SWE.dsx"
presimreport_path <- 
  "C:/Users/<username>/Documents/scriping_rsm/tutorial03-preSimuReport.txt"
postsimreport_path <- 
  "C:/Users/<username>/Documents/scriping_rsm/tutorial03-postSimuReport.txt"
ic_save_folder <- 
  "C:/Users/<username>/Documents/scriping_rsm"

Applying settings and running the model

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

# Load initial conditions from file and copy file
clrCall(rsm, "LoadInitialConditionsFromFileAndCopyFile", ic_file, ic_save_folder)
#> NULL

# Load parameters from file
clrCall(rsm, "LoadParametersFromFile", parameter_file)
#> 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

# Write a post-simulation report to check for possible errors (requires full 
# path)
clrCall(rsm, "SavePostSimulationReportAs", postsimreport_path)  
#> NULL

# Save selected results (requires full path)
clrCall(rsm, "SaveSelectionResultsAs", results_GlacierDischarge_savePath, 
        saveDataInDstFile, "DischargeGlacier")  
#> NULL
clrCall(rsm, "SaveSelectionResultsAs", results_SWE_savePath, 
        saveDataInDstFile, "SnowWaterEquivalent")  
#> NULL

# Stop task
clrCall(rsm, "Stop")
#> NULL

Read results

# 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_glacier <- readResultDST(results_GlacierDischarge_savePath, chunk_size)
result_swe <- readResultDST(results_SWE_savePath, chunk_size)

Visualisation example

Glacier melt contributions to discharge

result_glacier |> 
ggplot() + 
  geom_point(aes(Datetime, Value, colour = Object)) +
  theme_bw()

Snow water equivalent in non-glaciated sub-basins

result_swe |> 
ggplot() + 
  geom_point(aes(Datetime, Value, colour = Object)) +
  theme_bw()