Skip to content

Commit

Permalink
Merge pull request #4 from eWaterCycle/dev_Rolf
Browse files Browse the repository at this point in the history
Dev rolf
  • Loading branch information
RolfHut authored Dec 2, 2024
2 parents 38ffdd3 + c18e8f3 commit 8f466f2
Show file tree
Hide file tree
Showing 178 changed files with 36,141 additions and 68 deletions.
244 changes: 244 additions & 0 deletions book/1_modelling_intro_HBV/Exercise1.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise 1\n",
"\n",
"## Introduction\n",
"\n",
"In this exercise we are considering a simple reservoir as shown below. \n",
"\n",
"![image.png](figures/reservoir.png)\n",
"\n",
"The behavior of this reservoir is defined by the mass balance equation:\n",
"\n",
"$$\n",
"\\begin{equation}\n",
"\\Delta S = P - E - Q\n",
"\\end{equation}\n",
"$$\n",
"\n",
"$P$ and $E$ represent precipitation and the actual evaporation respectively. $S$ is the storage in the reservoir. The discharge $Q$ in the reservoir can be expressed as a function of stored water in the reservoir:\n",
"\n",
"$$\n",
"\\begin{equation}\n",
"Q = f\\left(S\\right)\n",
"\\end{equation}\n",
"$$\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Linear Reservoir without External Drivers\n",
"\n",
"At first we will consider the reservoir without the external drivers ```P``` and ```E``` and we assume that Q is equal to:\n",
"\n",
"\\begin{equation}\n",
"Q = k S ^\\alpha\n",
"\\end{equation}\n",
"\n",
"Next to this, we also assmume a linear reservoir, so $\\alpha$ is equal to 1.\n",
"\n",
"With these assumptions, the system can be solved analytically. This makes it possible to calculate the storage and discharge at any time by knowing the initial condition and the ```k``` value.\n",
"\n",
"### Part 1\n",
"\n",
"Derive the analytical solution of the reservoir, as mentioned without the external drivers and as a **linear** reservoir. \n",
"\n",
"However, for hydrological systems it is not realistic to neglect the external drivers. If we take into account these drivers, the equation cannot be solved analytically, but has to be solved numerically.\n",
"\n",
"An simple example of a numerical solution is the explicit forward Euler method. This method calculates the state of a system at each time step considering the state of the system at the previous time step and rate of changes:\n",
"\n",
"\\begin{equation}\n",
"S(t + \\Delta t) = F(S(t))\n",
"\\end{equation}\n",
"\n",
"If we consider the linear reservoir problem without the external drivers, we obtain:\n",
"\n",
"\\begin{equation}\n",
"\\frac{dS(t)}{dt} = -k S(t) ^\\alpha\n",
"\\end{equation}\n",
"\n",
"The forward Euler method yields:\n",
"\\begin{equation}\n",
"\\frac{S(t + \\Delta t) - S(t)}{\\Delta t} = -kS(t)^\\alpha\n",
"\\end{equation}\n",
"\n",
"This results in the numerical solution:\n",
"\n",
"\\begin{equation}\n",
"S(t + \\Delta t) = S(t) + - k \\Delta t k S(t) ^\\alpha\n",
"\\end{equation}\n",
"\n",
"Hence, if $ S(t) $ is known, $ S(t + \\Delta t) $ can be calculated in one iteration. The big assumption in the forward Euler model is that $ S\\left(t\\right) $ is relatively constant during the timestep. This may not always be true. If $S\\left(t\\right)$ changes fast smaller timesteps need to be used to not make too big of an error. Other, more complicated methods to numerically solve differential equations exist such as Runga-Kutta. See the material of the MUDE course for more information on those. In hydrology it is always important to weigh if the error introduced by your numerical scheme is significant compared to the error introduced by the fact that the model is not perfect to begin with."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Write the functions in the code cells below to calculate the analytical and numerical solution of the linear reservoir problem without external drivers. Return for both the storage array ```S```and the time array ```t```. \n",
"\n",
"The following (starting) values can be used; during the exercise, you can change $ k $ and $ t_{max} $:\n",
"\n",
"- $ S_0 = 75 $ mm\n",
"- $ k = 0.01 $ d$^{-1}$\n",
"- $ t_0 = 0 $ days\n",
"- $ t_{max} = 200 $ days\n",
"\n",
"The following questions might help you write the script:\n",
"\n",
"1. What do you want to calculate?\n",
"2. Which variables need to be defined?\n",
"3. For how many time steps do you want to calculate the output?\n",
"4. How can a for loop help you to calculate the output for multiple time steps (search in help)?\n",
"5. How can you use a function for this exercise?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First import the relevant packages "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"S0 = 75 #mm\n",
"k = 0.01 #per day\n",
"t0 = 0 #days\n",
"tmax = 200 #days\n",
"dt = 1 #days\n",
"alpha = 1 "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def analytical_solve():\n",
"\n",
" return\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def numerical_solve():\n",
" \n",
" return \n",
" \n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Part 3\n",
"\n",
"\n",
"Compute the analytical and numerical solution, both using the same values for the parameters. Plot the solutions on a single graph and see if there are any differences. \n",
"Try alternating the values for the timestep ```dt``` and the ```k``` values to investigate their influence. Observe how the solutions compare to each other."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Linear Reservoir with External Drivers\n",
"\n",
"Until now we considered a model of linear reservoir without the external drivers. While in practice, hydrological models tend to mimic the interactions of the system with external drivers. \n",
"Therefore, as mentioned earlier, neglecting the external drivers is not realistic. \n",
"In the last part of this exercise, you will conside external drivers. This water balance equation cannot be solved analytically because precipitation and evaporation cannot be formulated; therefore, we only rely on numerical analysis."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Part 4\n",
"\n",
"Write a script in the code cell below to calculate the numerical solution of the linear reservoir problem **including** the external drivers (see the model structure in the figure). The values for precipitation and evaporation are given in P_PE_date.txt. Have a look at this file and think about which values represents the precipitation and potential evaporation. \n",
"\n",
"You can use the same values as in the previous part of the assignment for the other variables.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Bonus Question\n",
"\n",
"The average runoff coefficient ܳ$\\bar{Q}$/$\\bar{P}$ of this catchment is between 0.3 and 0.35. Try to change the values of the parameters k and $\\alpha$ in such a way that your modelled runoff coefficient is between 0.3 and 0.35 as well. Do not forget to plot the hydrograph and be careful to have a reasonable hydrograph as well."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading

0 comments on commit 8f466f2

Please sign in to comment.