1 Brett’s GitHub Page

For other R Notebooks on our evolving ideas about geomorphology, see Brett’s GitHub Page

2 Overview

The issue of stability in a gravel bed stream with alluvial banks has been tackled most often by considering the threshold for entrainment of the median bed material size, and then assuming that (according to the idea of equal mobility) all grain sizes start to move at about the same time.

The first attempts to deal with this problem simply took the work done by Shields using uni-modal sized spheres and assumed that the corresponding grain size for gravel beds was the median size of the bulk sediment mixture. This is the basis for early work by Parker (e.g. 1978) and for the early experimental work trying to account relative grain size in entrainment functions (Andrews 1983).

Later work on entrainment and sediment transport linked the bed surface \(D_{50}\) to the Shields curve, which continues to be the standard for most work on the topic (Komar 1987; Parker and Klingeman 1982; Parker 1990; Parker and Toro-Escobar 2002). Interestingly, the idea that the only reasonable equivalence is one between a uni-modal sediment size and and the median for a bed of mixed sediment sizes has seldom been seriously questioned, despite the general agreement that things like flow resistance and near-bed boundary characteristics are more closely correlated with the \(D_{84}\) than with the \(D_{50}\).

When considering bed entrainment and sediment transport, it makes sense to associate the threshold(s) with grain size. But bed entrainment, and the stability of a stream’s channel morphology are not the same thing. In fact, we often use the analogy of a “dynamic equilibrium” to describe channels that maintain their general shape while transporting sediment and migrating laterally. The analogy was made in the past primarily out of necessity, and does not represent the behavior of most natural streams (even those built in laboratories!). In particular, gravel bed streams are characterized by highly non-linear processes, and exhibit both complex and chaotic dynamics which, while deterministic, are not amenable to prediction.

One important aspect of this non-linearity is that field and laboratory evidence demonstrates that there exists a threshold for gravel bed rivers above which the channel form changes dramatically and quickly. This kind of threshold behavior as long been acknowledged by geomorphologists (Schumm’s (1969) river metamorphosis ideas come to mind), and have been describe conceptually, but attempts to numerically model this threshold have simply relied upon analogy with the entrainment threshold. The nature of geomorphic thresholds in gravel bed streams does not seem to have been seriously studied in its own right.

Rather than being linked to entrainment of a given particle size (Andrews 1982; Henderson 1963; Kaufmann et al. 2008; Olsen, Whitaker, and Potts 1997), we believe that the threshold for morphologic stability could just as easily be linked to the fraction of the bed surface that remains stable during a given flow. That is, stability may be attributable to the fraction of the bed covered by imobile particles, which is slighlty different than saying it is related to a particular grain size.

Recent experiments have conclusively demonstrated that it is not the surface \(D_{50}\) that controls channel stability (MacKenzie and Eaton 2017), and that channel morphology can be maintained even when relatively large caliber sediment such as the bed surface \(D_{84}\) is fully mobile (Eaton and MacKenzie, in prep).

3 Predicting the immobile fraction

One issue that seems to be often overlooked when thinking about bed stability is that, even once a grain size begins to move, most of the particles of that size on the bed will not be moved. It is only when the shear stresses approach two times the threshold for entrainment that nearly all particles of a given size can be mobilized and transported. We can use the experimental work done by Wilcock in the 1990’s to constrain the problem, and to estimate the the fraction of the bed surface that is immobile for a given bed surface grain size distribution and shear stress (Wilcock 1993; Wilcock and McArdell 1993; Wilcock 1997; Wilcock and McArdell 1997).

Based on Wilcock’s work (Wilcock 1997), we can generally say that, for \(\tau \approx \tau_c\), only about 10% of the grains on the bed surface are likely to be entrained and transported. We can also posit that, for \(\tau \approx 2\tau_c\), about 90% of the grains are likely to be transported. We can approximate the underlying physics using a logistic equation, which predicts the fraction of a sediment in a give size class that will remain on the bed as a function of the relative shear stress (which I take to be the shear stress divided by the entrainment shear stress).

rm(list = ls())
stab_fraction = function(T, Tci, a = -4.3, b = -1.5){
  t =  T / Tci  #the relative shear stress
  stab.frac =  1 - (1)/(1 + exp(a*(t + b)))
}

A plot of the function over a range of relative shear stress values from 0.5 to 2.5 looks like this for the default constants in the function.

The value the function at the threshold for entrainment is 0.9, while the value at twice the threshold is 0.1. These values are indicated on the graph using dotted red lines.

While the details of this approximation curve could obviously be refined, the function captures the transition from immobility to partial mobility to full mobility in a general way. It also makes clear that equating channel instability to the shear stress required to entrain the \(D_{50}\) is probably not a good assumption, since:

  1. fully 90% of the size class containing the \(D_{50}\) will not be entrained by that flow
  2. all the sediment coarser than the \(D_{50}\) will be at least that stable, and
  3. a large fraction of the grain sizes smaller than \(D_{50}\) will also remain immobile

4 Integrating the imobile fraction

We can use the function above to estimate the total fraction of a bed surface that will remain immobile for a given shear stress, as well. For example, consider a bed surface with a log-distribution of sediment sizes.

We can estimate the critical shear stress for entrainment using the function proposed by Wilcock and Crowe to account for relative grain size.

crit_stress = function(Di, D50, TC50 = 0.036){
  g = 9.81
  rho = 1000
  rho.sed = 2650
  Tci = numeric(length(Di))
  if(length(D50) == 1){
    Tcrit = TC50 * g * (rho.sed - rho) * D50 / 1000
    Tci[which(Di < D50)] = Tcrit * (Di[which(Di < D50)]/D50)^0.12
    Tci[which(Di >= D50)] = Tcrit * (Di[which(Di >= D50)]/D50)^0.67
    return(Tci)
  }else{
    return(NA)
  }
}

For our grain size distribution, the critical shear stress values are:

bed.entrain = crit_stress(bed.sizes, median(D))
plot(bed.sizes, bed.entrain,
     type = "b",
     log = "x",
     col = "blue",
     lty = 2,
     xlab = "grain size (mm)",
     ylab = "crit. shear stress (Pa)")
abline(v = median(D), col = "red",lty = 3)

Using these data, we can estimate the proportion of the bed surface that would be stable for a given shear stress.

4.1 Entraining D50

For example, assume that \(\tau = 25\) Pa; then we can calculate the fraction of the bed that would remain immobile.

bed.stress = 25
bed.immob = bed.prop * stab_fraction(bed.stress, bed.entrain)
plot(bed.sizes, bed.prop, 
     col = "blue",
     log = "x",
     type = "b",
     lty = 2,
     xlab = "grain size",
     ylab = "proportion in size class")
lines(bed.sizes, bed.immob,
      col = "red",
      lty =1)

The total fraction of the bed surface that would be immobile under these conditions (and given the above assumptions) is the area under the red curve, which is about 0.92.

The stress required to entrain the bed surface \(D_{50}\) is about 26 Pa, so the above curve represents the bed stability at flows close to that required to entrain the bed surface, which some would have use believe represents the formative flow! We know from experiments that shear stresses of this magnitude do little to affect stream channel morphology in gravel bed rivers.

4.2 Fully Mobilizing D50

What about for conditions that should fully mobilize the \(D_{50}\)?

bed.stress = 2*crit_stress(median(D),median(D))
bed.immob2 = bed.prop * stab_fraction(bed.stress, bed.entrain)
plot(bed.sizes, bed.prop, 
     col = "blue",
     log = "x",
     type = "b",
     lty = 2,
     xlab = "grain size",
     ylab = "proportion in size class")
lines(bed.sizes, bed.immob2, 
      col = "green",
      lty = 1)
lines(bed.sizes, bed.immob,  #plotted for reference
      col = "red",
      lty = 2)

These flows mobilize quite a bit more of the bed, but leave 0.29 of the bed immobile. Note that the stable fraction for the entrainment of the \(D_{50}\) is also shown for comparision on this figure, in red.

4.3 Fully Mobilizing D84

In our experiments, we find that morphologic instability only occurs when the fraction of the bed that is immobile falls below about 0.1. That corresponds to full mobility of something like the \(D_{84}\).

Di = quantile(D, probs = 0.84)
bed.stress = 2*crit_stress(Di,median(D))
bed.immob3 = bed.prop * stab_fraction(bed.stress, bed.entrain)
plot(bed.sizes, bed.prop, 
     col = "blue",
     log = "x",
     type = "b",
     lty = 2,
     xlab = "grain size",
     ylab = "proportion in size class")
lines(bed.sizes, bed.immob,  #plotted for reference
      col = "red",
      lty =2)
lines(bed.sizes, bed.immob2,  #plotted for reference
      col = "green",
      lty = 2)
lines(bed.sizes, bed.immob3,
      col = "orange",
      lty =1)

When the \(D_{84}\) is fully mobile, only 0.07 of the bed is likely to remain immobile. This seems to correspond to the conditions for morphologic instability that we have observed in our experiments, and is a much higher threshold than is usually discussed when considering the thresholds for geomorphic change in gravel bed rivers.

5 References

Andrews, E. D. 1982. “Bank Stability and Channel Width Adjustment, East Fork River, Wyoming.” Water Resources Research 18 (4): 1184–92.

———. 1983. “Entrainment of Gravel from Naturally Sorted Riverbed Material.” Geological Society of America Bulletin 94: 1225–31. doi:10.1130/0016-7606(1983)94$<$1225:EOGFNS$>$2.0.CO;2.

Henderson, F. M. 1963. “Stability of Alluvial Channels.” Transactions of the ASCE 128: 657–86.

Kaufmann, Philip R, John M Faustini, David P Larsen, and Mostafa A Shirazi. 2008. “A Roughness-Corrected Index of Relative Bed Stability for Regional Stream Surveys.” Geomorphology 99 (1). Elsevier: 150–70. doi:10.1016/j.geomorph.2007.10.007.

Komar, P. D. 1987. “Selective Grain Entrainment by a Current from a Bed of Mixed Sizes: A Reanalysis.” Journal of Sedimentary Petrology 57 (2): 203–11. doi:10.1306/212F8AE4-2B24-11D7-8648000102C1865D.

MacKenzie, Lucy G., and Brett C. Eaton. 2017. “Large Grains Matter: Contrasting Bed Stability and Morphodynamics During Two Nearly Identical Experiments.” Earth Surface Processes and Landforms 42 (8): 1287–95. doi:10.1002/esp.4122.

Olsen, DS, AC Whitaker, and DF Potts. 1997. “Assessing Stream Channel Stability Thresholds Using Flow Competence Estimates at Bankfull Stage.” Water Resources Bulletin 33 (6). American Water Resources Association: 1197–1207. doi:10.1111/j.1752-1688.1997.tb03546.x.

Parker, G. 1978. “Self-Formed Rivers with Equilibrium Banks and Mobile Bed: Part 2. the Gravel Rier.” Journal of Fluid Mechanics 89 (127-146).

———. 1990. “Surface-Based Bedload Transport Relation for Gravel Rivers.” Journal of Hydraulic Research 28 (4): 417–36.

Parker, G., and P. C. Klingeman. 1982. “On Why Gravel Bed Streams Are Paved.” Water Resources Research 18 (5): 1409–23.

Parker, G., and C. M. Toro-Escobar. 2002. “Equal Mobility of Gravel in Streams: The Remains of the Day.” Water Resources Research 38 (11).

Schumm, S. A. 1969. “River Metamorphosis.” Journal of the Hydraulics Division-ASCE 95 (HY1): 255–73.

Wilcock, P. R. 1993. “Critical Shear Stress of Natural Sediments.” Journal of Hydraulic Engineering 119 (4): 491–505.

———. 1997. “Entrainment, Displacement and Transport of Tracer Gravels.” Earth Surface Processes and Landforms 22: 1125–38.

Wilcock, P. R., and B. W. McArdell. 1993. “Surface-Based Fractional Transport Rates: Mobilization Thresholds and Partial Transport of a Sand-Gravel Sediment.” Water Resources Research 29 (4). Wiley Online Library: 1297–1312. doi:10.1029/92WR02748.

———. 1997. “Partial Transport of a Sand/Gravel Sediment.” Water Resources Research 33 (1). Wiley Online Library: 235–45. doi:10.1029/96WR02672.

LS0tCnRpdGxlOiAiRGlzYW1iaWd1YXRpbmcgY2hhbm5lbCBzdGFiaWxpdHkgZnJvbSBiZWQgZW50cmFpbm1lbnQiCmF1dGhvcjogIkJyZXR0IEVhdG9uLCBHZW9ncmFwaHksIFVCQyIKZGF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