/
Example-03.Rmd
223 lines (175 loc) · 8.21 KB
/
Example-03.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
---
title: "Example-03"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Example-03}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
# 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](https://github.com/hydrosolutions/RSMinerveR#readme) for instructions and links).
Copy this vignette to your local computer and adapt the paths.
Open the model in \code{RSMinerveR/data/Model.rsm} 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.
```{r setup, message=FALSE,warning=FALSE,results='hide'}
library(riversCentralAsia)
library(rClr)
library(tidyverse)
library(lubridate)
library(R.utils)
```
## Loading Assemblies
```{r load-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
```{r 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.
```{r, echo=FALSE}
output_path <- file.path("C:", "Users", "marti", "Dropbox (hydrosolutions)",
"HYDRO4U_TECHNICAL_WORK", "MODELS",
"test_RSMinerveRvignettes")
if (dir.exists(output_path)) {
results_GlacierDischarge_savePath <- file.path(output_path,
"tutorial03-results_GlacierDischarge.dsx")
results_SWE_savePath <- file.path(output_path, "tutorial03-results_SWE.dsx")
presimreport_path <- file.path(output_path, "tutorial03-preSimulationReport.txt")
postsimreport_path <- file.path(output_path, "tutorial03-postSimulationReport.txt")
ic_save_folder <- output_path
} else {
cat("Warning: Result folder not found. Attemping to write to temporary folder in the current working directory. \n")
dir.create("temporary_RSMinerveR_output")
if (dir.exists("temporary_RSMinerveR_output")) {
results_GlacierDischarge_savePath <- normalizePath(file.path("temporary_RSMinerveR_output",
"tutorial03-results_GlacierDischarge.dsx"),
mustWork = FALSE)
results_SWE_savePath <- normalizePath(file.path("temporary_RSMinerveR_output",
"tutorial03-results_SWE.dsx"),
mustWork = FALSE)
presimreport_path <- normalizePath(file.path("temporary_RSMinerveR_output",
"tutorial03-preSimulationReport.txt"),
mustWork = FALSE)
postsimreport_path <- normalizePath(file.path("temporary_RSMinerveR_output",
"tutorial03-postSimulationReport.txt"),
mustWork = FALSE)
ic_save_folder <- normalizePath("temporary_RSMinerveR_output",
mustWork = FALSE)
}
}
```
```{r result-paths, eval=FALSE}
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 \code{clrCall} 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.
```{r run-model}
# Define a clr task
rsm <- clrNew("RSMinerve.RS.Task")
# Start-up the model
clrCall(rsm, "Start", model_file)
# Load data
clrCall(rsm, "LoadDatasetAndSetDates", input_dataset, TRUE, TRUE)
# Set simulation parameters
clrCall(rsm, "SetDates", start_date, end_date)
# SetSimulationTimeStep and SetRecordingTimeStep require characters
clrCall(rsm, "SetSimulationTimeStep", simulationTimeStep, timeStepUnit)
clrCall(rsm, "SetRecordingTimeStep", recordingTimeStep, timeStepUnit)
# Load initial conditions from file and copy file
clrCall(rsm, "LoadInitialConditionsFromFileAndCopyFile", ic_file, ic_save_folder)
# Load parameters from file
clrCall(rsm, "LoadParametersFromFile", parameter_file)
# Write a pre-simulation report to check for possible errors (requires full
# path)
clrCall(rsm, "SavePreSimulationReportAs", presimreport_path)
# Run a simulation
clrCall(rsm, "Simulate")
# Write a post-simulation report to check for possible errors (requires full
# path)
clrCall(rsm, "SavePostSimulationReportAs", postsimreport_path)
# Save selected results (requires full path)
clrCall(rsm, "SaveSelectionResultsAs", results_GlacierDischarge_savePath,
saveDataInDstFile, "DischargeGlacier")
clrCall(rsm, "SaveSelectionResultsAs", results_SWE_savePath,
saveDataInDstFile, "SnowWaterEquivalent")
# Stop task
clrCall(rsm, "Stop")
```
## Read results
```{r, message=FALSE, warning=FALSE, results='hide'}
# 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
```{r, fig.fullwidth=TRUE, fig.width=6}
result_glacier |>
ggplot() +
geom_point(aes(Datetime, Value, colour = Object)) +
theme_bw()
```
### Snow water equivalent in non-glaciated sub-basins
```{r, fig.fullwidth=TRUE, fig.width=6}
result_swe |>
ggplot() +
geom_point(aes(Datetime, Value, colour = Object)) +
theme_bw()
```