Tips & Tricks

Codes and more that we use often

January 27, 2020 by

Soil Hydraulic Functions in R

Goodness of fit

    RMSE = function(m, o){
            sqrt(mean((m - o)^2))
            }
       
    MAE = function(m, o){
            mean(abs(m - o) )
          }

Water Retention Functions

The van Genuchten (1980) model is the most widely used water retention curve function:

\[\Theta = \left( 1 +(\alpha \psi)^n\right)^{-m}\]

where the effective saturation is defined as: \(\Theta = (\theta - \theta_{r}) / (\theta_{s} - \theta_{r})\)


      vG  = function(h,a,n,ths,thr){
        m = 1-1/n
       Se = (1+(a*(h))^n)^(-m)
    theta = thr+(ths-thr)*Se
    # uncomment on the following two
    #return(Se)
    return(theta) 
	

NEw

   vG  = function(h,a,n,ths,thr){
     m = 1-1/n
    Se = (1+(a*(h))^n)^(-m)
 theta = thr+(ths-thr)*Se
 # uncomment on the following two
 #return(Se)
 return(theta) 
     }


  vGr  = function(th,a,n,ths,thr){
     m = 1-1/n
    TH = (th-thr)/(ths-thr)
    psi = ((TH^(-1/m) - 1)^(1/n))*(1/a)
     }
	   
Durner = function(h,a1,n1,a2,n2,w,ths,thr){
    m1 = 1-1/n1
    m2 = 1-1/n2
    Se = (1-w)*(1+(a1*(h))^n1)^(-m1)  + w*(1+(a2*(h))^n2)^(-m2)
 theta = thr+(ths-thr)*Se        
    }
	

Fit parameters of van Gnuchten Model

fit.vG = function(vwc, h,por){
    ft =  nls(vwc ~ vG(h,a,n,por,thr),
                    start = list(a = 0.5,    n=1.5,   thr=0.01),
                    lower = list(a = 0.0001, n=1.1, thr=0),
                    upper = list(a = 1000,   n=5,   thr=0.80*por),
                algorithm = "port"
             )
     coef(summary(ft))
     }