Fix model functional to work with higher-order autodiff by specifying template parameters; add test