# Water flow modeling

## Streams and basins

Set the region to the area where the computation should be performed (we are using the extent and resolution of the elevation raster map): ```g.region rast=elevation -p ``` Compute accumulation (number of cells that drain through each cell) and basins. ```r.watershed -a -b elevation=elevation thresh=10000 accumulation=accumulation_10K basin=basin_10K ``` To look at the basins in relation to elevation and accumulation, in the Layer Manager, right click on basins layer, select Change opacity level, and set the opacity to 40. Move the basins layer over the accumulation elevation layers. By zooming and checking and unchecking the elevation and accumulation layers explore their relations.

Now, find minimum and maximum of the accumulation raster map using right click on the layer and selecting Metadata or using the following command:

``````r.info elevation=accumulation_10K
``````
Show only the potential streams by limiting the displayed values in Properties dialog of the accumulation layer or add a new layer using the following command (uncheck the existing accumulation layer): ```d.rast map=accumulation_10K values=10000-1000000 ``` Note that the upper limit is just arbitrary value higher than the raster maximum value.

Change the selection of values to see possible stream definitions.

## Convert streams and basins to vectors

Convert basins raster to vector areas. ```r.to.vect -s input=basin_10K output=basin_10K type=area ``` Using raster algebra select detailed stream cells with accumulation higher than 100. Use constant value where the stream potentially is and NULL (no data) value in all the other areas. ```r.mapcalc "streams_derived = if(abs(accumulation_10K) > 100, 1, null())" ``` Some of the streams are now several cells wide, thin the raster streams, so that the stream position is clear before the conversion to vector. ```r.thin input=streams_derived output=streams_derived_thin ``` Convert streams raster to vector lines. ```r.to.vect -s input=streams_derived_thin output=streams_derived type=line ```

## Flow pattern

```g.region rast=elev_lid792_1m -p r.flow elevation=elev_lid792_1m flowline=flow_lines flowlength=flow_length_1m flowaccum=flow_accumulation_1m ```

## Overland flow depth and discharge

Besides elevation the r.sim.water module requires derivatives in X and Y direction which we can get using r.slope.aspect module. ```g.region rast=elev_lid792_1m -p r.slope.aspect elevation=elev_lid792_1m dx=elev_lid792_dx_1m dy=elev_lid792_dy_1m ``` There is a lot of different settings which can be used to fine-tune the simulation. Here we are using mostly the defaults. ```r.sim.water elevation=elev_lid792_1m dx=elev_lid792_dx_1m dy=elev_lid792_dy_1m depth=water_depth_1m disch=water_discharge_1m nwalk=10000 rain_value=100 niter=5 ```