export_models: Create C++ code to export 'TMB' models from package.

View source: R/export_models.R

export_modelsR Documentation

Create C++ code to export TMB models from package.

Description

Create C++ code to export TMB models from package.

Usage

export_models(pkg = ".")

Arguments

pkg

Character string: any subdirectory of the package source code.

Details

TMB models should be saved as C++ header files of the form src/TMB/*.hpp, and written almost exactly as with usual TMB *.cpp models. So for example, src/TMB/ModelA.hpp would be written as:

// __DO NOT__ '#include <TMB.hpp>' as file is not include-guarded

#undef TMB_OBJECTIVE_PTR
#define TMB_OBJECTIVE_PTR obj

// name of function _must_ match file name (ModelA)
template<class Type>
Type ModelA(objective_function<Type>* obj) {

  // _exactly_ the same code as for usual 'ModelA.cpp'

}

#undef TMB_OBJECTIVE_PTR
#define TMB_OBJECTIVE_PTR this

The function export_models creates a file src/TMB/pkgname_TMBExports.cpp containing a single TMB model object which dispatches the appropriate ModelA.hpp, ModelB.hpp, etc. using if/else statements. At the R level, the correct model is invoked from TMB::MakeADFun exactly as for a single TMB model, except the data list argument gets an additional element model specifying the name of the model, e.g., model = "ModelA".

export_models assumes that each file of the form src/TMB/*.hpp contains exactly one TMB model. In order for these to #include additional .hpp files, these additional files must be placed either in a subfolder of src/TMB, or in (a subfolder of) inst/include. The advantage of the latter approach is that the additional files are available to other R packages via LinkingTo: pkgname is the other package's DESCRIPTION. If the latter approach is used, the TMB compiler must be notified of the additional include directory. This is done by setting the TMB_FLAGS in src/Makevars[.win] to

TMB_FLAGS = -I"../../inst/include"

Other flags specific to the TMB compiler can be set here as well, as can the usual CXX_FLAGS, etc. for other source code in src, which is compiled independently of that in src/TMB.

Value

Invisible; called for its side effects.

See Also

use_tmb


mlysy/TMBtools documentation built on April 1, 2022, 6:18 p.m.