Using R for physically based modelling and visualisation of

Using R for physically based
modelling and visualisation of river
system dynamics
Grace Garner
@DinosaurusGrace
[email protected]
Environmental scientists and managers require numerical models to
represent physical environmental processes, in order to:
1.
Validate conceptual models (“how we think stuff works”) of
processes against observational data
and
2.
Predict environmental dynamics in un-monitored systems
or
3.
Quantify the effects of changes in these processes (e.g. due
to climate or mitigation strategies)
R has some useful tools that allow us to solve these problems, for
free!
Environmental data analysis requires visualisation and modelling,
often of variables in space and time. Really useful tools:
• lattice for 3-D (space-time-response) plots (personal preference.
Also possible in Base Graphics, ggplot2 & plot3d)
• deSolve for solving initial value problem ordinary and partial
differential equations (personal experience. Other types of
differential equation solvable in bvpSolve, ReacTran, rootSolve, sde,
pomp… )
Energy Flux (MJm-2
Visualising differences in water temperature within a 1 km stretch of
stream over 7 days?
0
−500
01 Jul 02 Jul 03 Jul 04 Jul 05 Jul 06 Jul 07 Jul 08 Jul
Tw (°C)
ul
ul
500
24
22
20
18
16
14
12
10
8
6
4
2
0
−2
−4
AWSOpen
AWSFDS
Difference
01 Jul 02 Jul 03 Jul 04 Jul 05 Jul 06 Jul 07 Jul 08 Jul
“Traditional” approach in Base
Graphics- upstream and
downstream temperatures plus
difference
Visualising differences in water temperature within a 1 km stretch of
stream over 7 days?
Alternatively, levelplot in lattice
package:
#require 3 matrices:
#(1)temperatures, (2) monitoring
#locations, and (3) times and dates
levelplot(responseMatrix~xaxisMatrix*y-axisMatrix)
Qn (MJm 2d 1)
Canopy Density (%)
80
900
600
300
Also, wireframe in lattice package
60
40
0
300
20
#require 3 matrices: #(1)responses, (2) monitoring #locations, and
0
(3) times and dates
/07
07
0
/07
06
100 200 300 400 500 600 700 800 900 1000 1100
/07
05
/07
04
/07
02
40
0
8
60 00
0
wireframe(responseMatrix~x-axisMatrix*y-axisMatrix)
/07
01
(d)
900
600
300
0
300
5
3:4
2
/07
02
20
0
Di
2 1
Q n ( M Jm d )
2 1
Qn( MJm d )
(c)
0
sta
do
nce
wn
00
Distance downstream (m)
10
/07
03
st
m
rea
(m
)
900
600
300
0
300
:00
18
7
/0
02
5
3:4
2
/07
06
:00
12
7
0
/
02
:00
06
7
/0
02
:00
00
7
/0
02
0
20
0
40
0
sta
Di
10
00
8
60 00
0
nce
s
wn
do
am
tre
(m
)
:00
18
7
/0
06
:00
12
7
0
/
06
:00
06
7
/0
06
:00
00
7
/0
06
0
10
00
8
60 00
)
0
40
(m
0
20
m
a
0
tre
Di
sta
wn
do
e
c
n
s
Modelling changes in water temperature within a 1 km stretch of
river using deSolve
• Energy flux model incorporating flow routing (semi-Lagrangian model)
• Model simulates change in temperature (dTw) of water parcels released from
upstream at 1 m resolution as a function of change in space (dx) i.e. dTw/dx
Tw initial
+/- Qn
+/- Qn
+/- Qn
Tw
Tw
Tw
Steps for model implementation in R:
1.
Define the model equation as a function
2.
3.
4.
Define model parameters
Generate dataframe of “driving data”
Define approximation functions for driving data
5.
6.
Set initial conditions
Set locations for which model should solve
7.
Solve ODE using deSolve
8.
Visualise results
1. Define the model equation as a function
Function (dTw), inputs are:
2.
1. Location within the river (x)
Initial condition (state): water temperature, T
3. Model parameters (parms): sigma
dTw <- function(x, state, parms) {
with(as.list(c(state, parms)), {
dTw <- (r(x)-sigma*T^4)
list(c(dTw))
})
}
•
Returns a list of derivatives
2. Define model parameters (parms)
parms <- c(sigma=5.67*10^-8)
3. Generate dataframe of “driving data” (i.e. Containing values of r)
#For each required model run, route a parcel of water through the reach
#and extract the meteorological conditions dependent
#on the location of the parcel
4. Define approximation function for driving data (i.e. for r)
One of these functions must be provided for each variable. Allow approximation
of each variable at any value of x that the ODE solver requires
r <- function(x){
r=approx(DrivingData[values of x],DrivingData[values of r],x)$y
return(r)
}
5. Set initial conditions (i.e. T, initial water temperature)
state <- c(T=10.5)
6. Define locations (x) at which the model will be solved (i.e. 1 m
intervals)
locations <-seq(min(DrivingData[values of x]),max(DrivingData[values of x]), by=1)
7. Solve the ODE
Require(deSolve)
mod<-ode(state, locations, dTw, parms)
Returns a matrix of state variables (i.e. water temperatures) and locations within
the river
Future possibilities…
• Interactive visualisation of model outputs using a Shiny app
(http://shiny.rstudio.com/)