Refactored global vars in stan::agrad to functions with a static
thread_local member