Have a look at this plan view below to get an illustration of how the 2-dimensions model work.

The water levels are store in a 2-D array. They are numbered as follows:

The water flows are stored in 2 different 2-D arrays.

1. QV: defines water flows between buckets down the screen (in plan view)

2. QH: defines water flows between buckets across the screen.

Let’s get into the modeling by cracking the exercises below. Answers to these exercises are available here. If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.

**Exercise 1**

Set all the required settings for the model:

a. Set the number of time steps. Here we use 1000 (it shows how many time steps we are going to run in the model; you can change it as much as you want.)

b. Set the total number of cells; here we have 25 x 25 water tanks.

c. Set time steps in seconds; here is 1.

d. Set the time at the start of simulations.

e. Set k between each water tank. Here we set a uniform value of k; 0.01.

**Exercise 2**

Create matrix H for the initial water level in the water tank.

**Exercise 3**

Set boundary conditions for the model. Here we have water flow (qh) into the water tanks from three sides (top, left and right) and water flowing out on the bottom (qv; see the plan view.) Water flow to the right and to the bottoms are considered positive. Don’t forget to declare the matrix for qh and qv.

**Exercise 4**

Create an output model for every 100 time steps.

**Exercise 5**

Run the model by creating loop for qh, qv, water storage update and models output (remember the threshold loop on latest previous exercise.)

**Exercise 6**

Plot a model output using a contour plot.

- Spatial Data Analysis: Introduction to Raster Processing: Part-3
- Spatial Data Analysis: Introduction to Raster Processing (Part 1)
- Advanced Techniques With Raster Data: Part 1 – Unsupervised Classification
- Become a Top R Programmer Fast with our Individual Coaching Program
- Explore all our (>4000) R exercises
- Find an R course using our R Course Finder directory

Here, the boundary condition is referred to as water level and/or water flow as the two main variables that can be played. There are basically two options to do that; Dirchlet and von Neuman boundary conditions. The Dirchlet method is just simply defined as q1 and q6 to any number. It shows that the flows q1 and q6 are remaining constant over the simulation. Von Neuman allows the model to define the q1 and q6 based on any other factors that might influence. For example, the level of a river here is represented as “tank water level.”

Answers to these exercises are available here. If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.

**Dirchlet Boundary Conditions**

**Exercise 1**

Try to modify your latest model with a new step: defining the q1 and q6 as a constant value. The model script will look exactly the same as the previous one, just add a constant value declaration of q1 and q6 as the first and the sixth on the q matrix output.

**Exercise 2**

Plot the model simulation.

**Von Neuman Boundary Conditions**

**Exercise 3**

Instead of defining the q1 and q6, try to define the water level on tank 1 and 5. Pay attention while running the models; you only need to calculate water storage for bucket 2 to 5.

**Exercise 4**

Plot the data.

**Numerical Instability**

As we learned, rounding off and truncation might generate some errors in the final result of the model. Therefore, it is important to maintain a relatively small time-step. Furthermore, shortening the time-step will give more useful information. The exercises below will try to simulate how to run the model for 500 time-steps, but only outputting every 10th.

**Exercise 5**

Set initial information and output matrix q and H for calculated water storage. Set all the initial values, including H and k. Use the Dirchlet boundary conditions value on the model script above.

**Exercise 6**

This is the tricky part. Take a deep breath. The purpose of this step is to get the model output and save the data every time-step. Since we will only output for every 10th, we need to create a matrix output that defines the rule.

**Exercise 7**

Create an output array and size equals number outputs by the number of tanks.

**Exercise 8**

Another consideration is that we need to tell the model that only the 10th calculation will be outputted by creating a loop for output time.

**Exercise 9**

Tell the model which row the data should be stored; it is increased by one each time. Also, define the output timer that will reset back to zero when the number of time-steps equal the number when water levels outputted (10th).

**Exercise 10**

Run the model and add conditional formatting (`if function`

)to the output section. Ask the script whether the increased output timer equals the threshold output value (10). If so, then increase our output count by 1 and output data in the next row. Then, reset our output timer back to zero so that the calculation will repeat again.

**Exercise 11**

Plot the data.

- Spatial Data Analysis: Introduction to Raster Processing: Part-3
- Spatial Data Analysis: Introduction to Raster Processing (Part 1)
- Density-Based Clustering Exercises
- Become a Top R Programmer Fast with our Individual Coaching Program
- Explore all our (>4000) R exercises
- Find an R course using our R Course Finder directory

Instead of playing with parameter and loop calculations, we will develop our single water tank into a network. Assume that we add another two similar water tanks that are connected through pipes. It could represent the layers of soil or different areas across a catchment. Below is the illustration of the model. The output of this exercise is to understand how space can be illustrated by a model.

The new data-set required can be downloaded here.

Answers to these exercises are available here. If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.

Before starting the real exercises, try to load the data-set and plot it to see the data characteristic.

**Exercise 1**

Set the initial time as 0, with timestep = 1 and final time step = 100. Define the parameters of each water tank including k (parameter) and s (initial water level) and create an empty output `matrix`

, this time, with four columns.

**Exercise 2**

Similar to before, try to run the loop through the number of time-steps.

**Exercise 3**

Plot the model output for each water tank; pay attention to the range of the result.

You can try to play with longer time-steps and varieties in the parameter to see if the model is realistic enough to predict the future.

What if the water tank arrangement has two-way interactions? In many situations, water flow direction varies over time, such as water flow in a soil profile under different conditions. The figure below is the schematic diagram of the next model.

On the diagram above, each tank has a hole that connects to the adjacent bucket. If the water level in tank 1 is higher than tank 2, water will obviously flow from tank 1 into 2 until the water levels are equal and vice versa. So, water will flow in either direction. The rate of flow (q) depends on the difference between two tanks (H1-H2) and the size of the hole (k) or pipe that connect the two tanks, written mathematically as follows:

q = k x (H1-H2) x t

Assume that there is no hole on the outside of either bucket 1 or 5. So that k, q1, and q6 are zero.

Clear the previous values before starting the following exercises.

**Exercise 4**

Define initial conditions, including t, time-steps, final time-steps, and ncell as the number of water tanks available. Create an empty `matrix`

of the water level (H) and the water flux output (q).

**Exercise 5**

Set the initial parameter for H and k.

**Exercise 6**

Try to use theĀ `sequence`

function to declare time-steps.

**Exercise 7**

Run the loop based on the parameters and the initial condition above.

**Exercise 8**

Plot the water level for each water tank. Pay attention to the output range.

Fancy to use a loop in water level plotting, huh?

Does the model meet your expectations?

Try to play around with the parameters and see how the model responds with those alterations.

Answers to these exercises are available here. If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.

**Exercise 1**

Recall the previous initial values of all parameters (t,k,S,ntimesteps, time step). It is better to use the same script as the previous exercise so that we can directly compare the original models to the modified one (based on error fixing approach on this exercise). Create a new empty matrix to store the following result of the calculation.

**Exercise 2**

Create a loop of calculation that includes the rounding up process.

**Exercise 3**

Plot the revised model on the same graph as the plot of the original model. Make notes of the performance of the model and where the error on the model occurred.

**Exercise 4**

Now, what happens if we change the time step? Recall all parameters on the original model, but change the final time step. Create a new output matrix for the output.

**Exercise 5**

Re-run the loop with the new parameter.

**Exercise 6**

Re-plot the original model and plot the time step reduced model. Models with longer time steps simulate lower water levels; why? How do we investigate which one is closer to the correct values?

**Exercise 7**

Create a regression equation from the data-set. Use it to compare results from analytical (regression equation) and the latest revised model visually. What can be inferred from it?

- Spatial Data Analysis: Introduction to Raster Processing (Part 1)
- Advanced Techniques With Raster Data: Part 1 – Unsupervised Classification
- Spatial Data Analysis: Introduction to Raster Processing: Part-3
- Become a Top R Programmer Fast with our Individual Coaching Program
- Explore all our (>4000) R exercises
- Find an R course using our R Course Finder directory

Natural environment is a continuous system that consists of discrete units/events. In this exercise, we will try to build continuous relationships that consist of a discrete event. The continuous relationship is represented with a simple calculation that repeats over and over. The model needs to count the space and time variables to make it continuous. In this case, we will use a rainfall catchment as an example that represents water flow through the water tank. Download the data-set used for this exercise here.

Answers to these exercises are available here. If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.

**Exercise 1**

Load and plot the data table. Assume variable 1 as time and variable 2 as the water level in the tank.

Imagine the amount of water that flows out from the system is dependent on the reduction of water levels inside the water tank. Furthermore, the reduction of water levels is caused by air pressure that is acting on the surface of the water. This is the mechanistic model that needs to be found and fully understood first.

**Exercise 2**

Try to write the mechanistic model mathematically that says that the water level in the tank is equal to the change of water in time.

**Exercise 3**

Let’s say we have a bucket with 15 cm of water in it and we know and assume that k= 0.05. Between 0 and 1 seconds, how much water would we expect to lose? What is the new water level? Do the same calculation from 1 to 2 seconds.

The “k” parameter represents how fast water flows from the tank.

**Exercise 4**

Try to create a loop that consists of the calculations in exercise 3. Consider thinking about time, initial water level, model parameter, time step, and final time step.

**Exercise 5**

Plot the water level based off the tank over the time.