Month: October 2014

JHU R Programming Course Note

Gathering long list of file names:

files_list <- list.files(directory, full.name=TRUE)

Construct a data.frame by rowbind

dat <- rbind(dat,read.csv(files_list[i]))

#Understanding lexical scoping
#<- operator vs. <<- operator
crazy <- function() {
x <<- 3.14 #variable x in the containing environment is updated to be 3.14
print(x) #no local variable x exists within functin ‘crazy’ R searches the containing environments
{print(x);
x <- 42; print(x) #local variable x is declared and assigned the value 42; overrides the variable x in
} #the containing environment
print(x) #since local variable x now exists within the function there is no need to search the containing
} #environment

x #variable x outside the function is updated value after the first statement within function crazy()
#the super-assignment operator does not update a variable of the same name inside an inner function but the innermost environment
#inherits any changes unless a local variable of the same name exists within the inner function as demonstrated
#by x <- 42;

crazy <- function() {
x <- 42
x <<- 3.14
print(x)
}

#> x <-0
#> x
#[1] 0
#> crazy()
#[1] 42
#> x
#[1] 3.14

#Declare and define a function named crazy()
crazy <- function() {
x <- 3.14 #asigns the value 3.14 to local variable x not the variable x in the containing environment
print(x)

{print(x);
x <<- 42; # assigns the value 42 to variable x in the containing environment
print(x)
}
print(x)
}

#> x <-0
#> x
#[1] 0
#> crazy()
#[1] 3.14
#[1] 3.14
#[1] 3.14
#[1] 3.14
#> x
#[1] 42

makeVector <- function(x = numeric()){
m <- NULL
set <- function(y){
x <<- y
m <<- NULL
}
get <- function()x
setmean <- function(mean)m <<-mean
getmean <- function()m
list(set=set, get=get,
setmean=setmean,
getmean=getmean)
}

cachemean <- function(x, …){
m <- x$getmean()
if(!is.null(m)){
message(“getting cached data”)
return(m)
}
data <- x$get()
m <- mean(data,…)
x$setmean(m)
m
}

Split and Lapply

        bystate <- split(rawData, rawData$State)

        result <- lapply(bystate, function(x) x[order(x[,11], x[,2]),])

        hospital<- lapply(result, function(x) x[num,c(2,7)])

        rank <- do.call(rbind, hospital)

Dispersion corrected DFT theory

Commonly used DFs do not describe the long-range dispersion interactions correctly. All semilocal DFs and conventional hybrid functionals asymptotically cannot provide the correct -C6/R^6 dependence of the dispersion interaction energy on the interatomic (molecular) distance R.

The failure of standard DFs comes from its inability to describe instantaneous electron correlations. In  more precise picture, electromagnetic zero-point energy fluctuations in the vacuum lead to ‘virtual’ excitations to allowed atomic or molecular electronic states. The corresponding densities interact electrostatically. They are not represented by conventional (hybrid) functionals that only consider electron exchange but do not employ virtual orbitals.

The computationally most efficient basic approaches to account for London dispersion effects in DFT includes:

  • Nonlocal vdW-DFs
  • ‘pure’ [semilocal (hybrid] DFs, which are highly parameterized forms of standard meta hybrid approximations (e.g., the M0XX family of functionals)
  • DFT-D methods (atom pairwise sum over  -C6/R^6 potentials)
  • Dispersion-correcting atom-centered one-electron potentials (1ePOT, called DCACP or in local variants LAP or DCP)

vdW-DF and Related Methods

(vdW-DF(2004), vdW-DF (2010), VV09, AND VV10)

Currently most widely used form, a nonempirical way to compute the dispersion energy. A supermolecular calculation of the total energy of the complex and the fragments is performed to obtain the interaction energy.

Approximation:

Total exchange-correlation energy Exc = Ex^LDA/GGA + Ec^LDA/GGA+Ec^NL

LDA (local density approximation) or GGA (semilocal) type are used for the short-ranged parts;

Ec^NL represents the nonlocal term describing the dispersion energy

Nonlocal vs Local (examination on the terminology)

In the DFT community, the dispersion energy is understood as an inherently nonlocal property. It must be described by a kernel which depends on two electron coordinates simultaneously.

In a WF picture, long-range dispersion has no nonlocal component.

Physically, dispersion is the Coulomb interaction between (local, fragment centered) transition densities. These can be plotted like ‘normal’ densities and have no ‘mysterious’ nonlocal character (except that virtual orbitals are needed for their construction).

Summary

  • Typically, Ec^NL is computed non-self-consistently, i.e., it is simply an add-on to the self-consistent filed (SCF)-DF energy similar as in DFT-D.
  • vdW-DF works better with short-range components that are basically repulsive such as Hartree-Fock.
  • Dispersion effects are naturally included via the charge density so that charge-transfer dependence of dispersion is automatically included in a physically sound manner. If performed self-consistently, the correction in turn also changes the density.
  • Whether double-counting effects of correlation at short range are present in the mentioned vdW-DFs is currently unknown.

Reference

Grimme, S., Density functional theory with London dispersion corrections. Wiley Interdisciplinary Reviews: Computational Molecular Science 2011, 1 (2), 211-228.