Compilation error running forced ODE model

Hi,

I am getting the below errors when trying to compile an ODE model (attached)aslanidi.stan (3.0 KB)
that contains a forcing term. I have tried to compile the model using two different computers across two different interfaces, and seem to get slightly different errors.

I followed the methodology here: Forced ODEs, a start for a case study? to pass a voltage to the ODE solver that varies over time, and affects the RHS of the ODE.

Does anyone know how we can fix this? Best, Ben

In pystan on one computer:

NFO:pystan:COMPILING THE C++ CODE FOR MODEL
anon_model_3d082c9ad94508204702799a040b08dc NOW.
Traceback (most recent call last):
File “./run.py”, line 23, in
model = pystan.StanModel(‘aslanidi-2009.stan’)
File “/usr/lib64/python2.7/site-packages/pystan/model.py”, line 311,
in init
build_extension.run()
File “/usr/lib64/python2.7/distutils/command/build_ext.py”, line 340,
in run
self.build_extensions()
File “/usr/lib64/python2.7/distutils/command/build_ext.py”, line 449,
in build_extensions
self.build_extension(ext)
File “/usr/lib64/python2.7/distutils/command/build_ext.py”, line 499,
in build_extension
depends=ext.depends)
File “/usr/lib64/python2.7/distutils/ccompiler.py”, line 574, in compile
self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
File “/usr/lib64/python2.7/distutils/unixccompiler.py”, line 134, in
_compile
raise CompileError, msg
distutils.errors.CompileError: command ‘gcc’ failed with exit status 1

In RStan using another computer:

Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! In file included from C:/Users/Ben/Documents/R/win-library/3.3/BH/include/boost/config.hpp:39:0,
from C:/Users/Ben/Documents/R/win-library/3.3/BH/include/boost/math/tools/config.hpp:13,
from C:/Users/Ben/Documents/R/win-library/3.3/StanHeaders/include/stan/math/rev/core/var.hpp:7,
from C:/Users/Ben/Documents/R/win-library/3.3/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5,
from C:/Users/Ben/Documents/R/win-library/3.3/StanHeaders/include/stan/math/rev/core.hpp:12,
from C:/Users/Ben/Documents/R/win-library/3.3/StanHeaders/include/stan/math/rev/mat.hpp:4,
from C:/Users/Ben/Documents/R/win-library/3.3/StanHeaders/include/stan/math.hpp:4,
from C:/Users/Ben/Documents/R/win-library/3.3/StanHeaders/include/src/stan/model/model_header.hpp:4,
from file1a3811ae3078.cpp:8:

R session info:

R version 3.3.1 (2016-06-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252 LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C LC_TIME=English_United Kingdom.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] rstan_2.15.1 StanHeaders_2.15.0-1 ggplot2_2.2.1

loaded via a namespace (and not attached):
[1] colorspace_1.2-6 scales_0.4.1 lazyeval_0.2.0 plyr_1.8.3 tools_3.3.1 inline_0.3.14 gtable_0.2.0
[8] tibble_1.3.0 gridExtra_2.2.1 Rcpp_0.12.5 grid_3.3.1 stats4_3.3.1 munsell_0.4.3

Here’s what CmdStan does, still an error (and warning about integer division), but there’s a helpful set of messages… I can’t look at them now but maybe it helps someone. Definitely a bug if it translates but does not compile.

SPH-KSakrejdaL:l1 krzysztof$ make -C ~/packages/cmdstan-develop ~/Downloads/aslanidi

--- Translating Stan model to C++ code ---
bin/stanc  /Users/krzysztof/Downloads/aslanidi.stan --o=/Users/krzysztof/Downloads/aslanidi.hpp
Model name=aslanidi_model
Input file=/Users/krzysztof/Downloads/aslanidi.stan
Output file=/Users/krzysztof/Downloads/aslanidi.hpp
DIAGNOSTIC(S) FROM PARSER:
Warning: integer division implicitly rounds to integer. Found int division: (left_ind + right_ind) / 2
 Positive values rounded down, negative values rounded up or down in platform-dependent way.


--- Linking C++ model ---
g++ -I src -I stan/src -isystem stan/lib/stan_math/ -isystem stan/lib/stan_math/lib/eigen_3.2.9 -isystem stan/lib/stan_math/lib/boost_1.62.0 -isystem stan/lib/stan_math/lib/cvodes_2.9.0/include -Wall -DEIGEN_NO_DEBUG  -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -DFUSION_MAX_VECTOR_SIZE=12 -DNO_FPRINTF_OUTPUT -pipe  -Wno-unused-function -ftemplate-depth-256    -O3 -o /Users/krzysztof/Downloads/aslanidi src/cmdstan/main.cpp -include /Users/krzysztof/Downloads/aslanidi.hpp stan/lib/stan_math/lib/cvodes_2.9.0/lib/libsundials_nvecserial.a stan/lib/stan_math/lib/cvodes_2.9.0/lib/libsundials_cvodes.a 
In file included from <built-in>:336:
In file included from <command line>:7:
In file included from /Users/krzysztof/Downloads/aslanidi.hpp:3:
In file included from stan/src/stan/model/model_header.hpp:4:
In file included from stan/lib/stan_math/stan/math.hpp:4:
In file included from stan/lib/stan_math/stan/math/rev/mat.hpp:56:
stan/lib/stan_math/stan/math/rev/mat/functor/ode_system.hpp:53:15: error: no viable overloaded '='
        dy_dt = f_(t, y, theta_, x_, x_int_, msgs_);
        ~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
stan/lib/stan_math/stan/math/rev/mat/functor/cvodes_ode_data.hpp:96:9: note: in instantiation of member function
      'stan::math::ode_system<aslanidi_model_namespace::deriv_aslanidi_functor__>::operator()' requested here
        ode_system_(t, y_vec, dy_dt_vec);
        ^
stan/lib/stan_math/stan/math/rev/mat/functor/cvodes_ode_data.hpp:68:23: note: in instantiation of member function
      'stan::math::cvodes_ode_data<aslanidi_model_namespace::deriv_aslanidi_functor__, double, double>::rhs' requested here
        explicit_ode->rhs(NV_DATA_S(y), NV_DATA_S(ydot), t);
                      ^
stan/lib/stan_math/stan/math/rev/mat/functor/integrate_ode_bdf.hpp:140:60: note: in instantiation of member function
      'stan::math::cvodes_ode_data<aslanidi_model_namespace::deriv_aslanidi_functor__, double, double>::ode_rhs' requested here
        cvodes_check_flag(CVodeInit(cvodes_mem, &ode_data::ode_rhs,
                                                           ^
/Users/krzysztof/Downloads/aslanidi.hpp:300:80: note: in instantiation of function template specialization
      'stan::math::integrate_ode_bdf<aslanidi_model_namespace::deriv_aslanidi_functor__, double, double>' requested here
            return stan::math::promote_scalar<fun_return_scalar_t__>(to_matrix(integrate_ode_bdf(deriv_aslanidi_functor__(...
                                                                               ^
/Users/krzysztof/Downloads/aslanidi.hpp:724:44: note: in instantiation of function template specialization
      'aslanidi_model_namespace::solve_aslanidi_forced_ode<double, double, double, double>' requested here
                stan::math::assign(I_temp, solve_aslanidi_forced_ode(ts,X0,theta,V, pstream__));
                                           ^
stan/src/stan/services/util/initialize.hpp:103:39: note: in instantiation of function template specialization
      'aslanidi_model_namespace::aslanidi_model::log_prob<false, true, double>' requested here
            log_prob = model.template log_prob<false, true>
                                      ^
stan/src/stan/services/diagnose/diagnose.hpp:54:19: note: in instantiation of function template specialization
      'stan::services::util::initialize<aslanidi_model_namespace::aslanidi_model,
      boost::random::additive_combine_engine<boost::random::linear_congruential_engine<unsigned int, 40014, 0, 2147483563>,
      boost::random::linear_congruential_engine<unsigned int, 40692, 0, 2147483399> > >' requested here
          = util::initialize(model, init, rng, init_radius,
                  ^
src/cmdstan/command.hpp:132:49: note: in instantiation of function template specialization
      'stan::services::diagnose::diagnose<aslanidi_model_namespace::aslanidi_model>' requested here
        return_code = stan::services::diagnose::diagnose(model,
                                                ^
src/cmdstan/main.cpp:8:21: note: in instantiation of function template specialization
      'cmdstan::command<aslanidi_model_namespace::aslanidi_model>' requested here
    return cmdstan::command<stan_model>(argc,argv);
                    ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:552:13: note: candidate function not viable: no known
      conversion from 'typename boost::math::tools::promote_args<double, double, double, double>::type' (aka 'double') to 'const
      std::__1::vector<double, std::__1::allocator<double> >' for 1st argument
    vector& operator=(const vector& __x);
            ^
In file included from <built-in>:336:
In file included from <command line>:7:
In file included from /Users/krzysztof/Downloads/aslanidi.hpp:3:
In file included from stan/src/stan/model/model_header.hpp:4:
In file included from stan/lib/stan_math/stan/math.hpp:4:
In file included from stan/lib/stan_math/stan/math/rev/mat.hpp:56:
stan/lib/stan_math/stan/math/rev/mat/functor/ode_system.hpp:79:23: error: no viable conversion from 'typename
      boost::math::tools::promote_args<double, var, double, double>::type' (aka 'stan::math::var') to 'vector<stan::math::var>'
          vector<var> dy_dt_var = f_(t, y_var, theta_, x_, x_int_, msgs_);
                      ^           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
stan/lib/stan_math/stan/math/rev/mat/functor/cvodes_ode_data.hpp:104:21: note: in instantiation of function template
      specialization 'stan::math::ode_system<aslanidi_model_namespace::deriv_aslanidi_functor__>::jacobian<Eigen::Matrix<double,
      -1, 1, 0, -1, 1>, Eigen::Map<Eigen::Matrix<double, -1, -1, 0, -1, -1>, 0, Eigen::Stride<0, 0> > >' requested here
        ode_system_.jacobian(t, y_vec, fy, Jy_map);
                    ^
stan/lib/stan_math/stan/math/rev/mat/functor/cvodes_ode_data.hpp:89:30: note: in instantiation of member function
      'stan::math::cvodes_ode_data<aslanidi_model_namespace::deriv_aslanidi_functor__, double, double>::dense_jacobian' requested
      here
        return explicit_ode->dense_jacobian(NV_DATA_S(y), J, t);
                             ^
stan/lib/stan_math/stan/math/rev/mat/functor/integrate_ode_bdf.hpp:157:57: note: in instantiation of member function
      'stan::math::cvodes_ode_data<aslanidi_model_namespace::deriv_aslanidi_functor__, double, double>::dense_jacobian' requested
      here
                                             &ode_data::dense_jacobian),
                                                        ^
/Users/krzysztof/Downloads/aslanidi.hpp:300:80: note: in instantiation of function template specialization
      'stan::math::integrate_ode_bdf<aslanidi_model_namespace::deriv_aslanidi_functor__, double, double>' requested here
            return stan::math::promote_scalar<fun_return_scalar_t__>(to_matrix(integrate_ode_bdf(deriv_aslanidi_functor__(...
                                                                               ^
/Users/krzysztof/Downloads/aslanidi.hpp:724:44: note: in instantiation of function template specialization
      'aslanidi_model_namespace::solve_aslanidi_forced_ode<double, double, double, double>' requested here
                stan::math::assign(I_temp, solve_aslanidi_forced_ode(ts,X0,theta,V, pstream__));
                                           ^
stan/src/stan/services/util/initialize.hpp:103:39: note: in instantiation of function template specialization
      'aslanidi_model_namespace::aslanidi_model::log_prob<false, true, double>' requested here
            log_prob = model.template log_prob<false, true>
                                      ^
stan/src/stan/services/diagnose/diagnose.hpp:54:19: note: in instantiation of function template specialization
      'stan::services::util::initialize<aslanidi_model_namespace::aslanidi_model,
      boost::random::additive_combine_engine<boost::random::linear_congruential_engine<unsigned int, 40014, 0, 2147483563>,
      boost::random::linear_congruential_engine<unsigned int, 40692, 0, 2147483399> > >' requested here
          = util::initialize(model, init, rng, init_radius,
                  ^
src/cmdstan/command.hpp:132:49: note: in instantiation of function template specialization
      'stan::services::diagnose::diagnose<aslanidi_model_namespace::aslanidi_model>' requested here
        return_code = stan::services::diagnose::diagnose(model,
                                                ^
src/cmdstan/main.cpp:8:21: note: in instantiation of function template specialization
      'cmdstan::command<aslanidi_model_namespace::aslanidi_model>' requested here
    return cmdstan::command<stan_model>(argc,argv);
                    ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:549:5: note: candidate constructor not viable: no known
      conversion from 'typename boost::math::tools::promote_args<double, var, double, double>::type' (aka 'stan::math::var') to
      'const std::__1::vector<stan::math::var, std::__1::allocator<stan::math::var> > &' for 1st argument
    vector(const vector& __x);
    ^
In file included from <built-in>:336:
In file included from <command line>:7:
In file included from /Users/krzysztof/Downloads/aslanidi.hpp:3:
In file included from stan/src/stan/model/model_header.hpp:4:
In file included from stan/lib/stan_math/stan/math.hpp:4:
In file included from stan/lib/stan_math/stan/math/rev/mat.hpp:58:
stan/lib/stan_math/stan/math/rev/mat/functor/cvodes_ode_data.hpp:104:21: error: no matching member function for call to 'jacobian'
        ode_system_.jacobian(t, y_vec, fy, Jy_map);
        ~~~~~~~~~~~~^~~~~~~~
stan/lib/stan_math/stan/math/rev/mat/functor/cvodes_ode_data.hpp:89:30: note: in instantiation of member function
      'stan::math::cvodes_ode_data<aslanidi_model_namespace::deriv_aslanidi_functor__, stan::math::var,
      stan::math::var>::dense_jacobian' requested here
        return explicit_ode->dense_jacobian(NV_DATA_S(y), J, t);
                             ^
stan/lib/stan_math/stan/math/rev/mat/functor/integrate_ode_bdf.hpp:157:57: note: in instantiation of member function
      'stan::math::cvodes_ode_data<aslanidi_model_namespace::deriv_aslanidi_functor__, stan::math::var,
      stan::math::var>::dense_jacobian' requested here
                                             &ode_data::dense_jacobian),
                                                        ^
/Users/krzysztof/Downloads/aslanidi.hpp:300:80: note: in instantiation of function template specialization
      'stan::math::integrate_ode_bdf<aslanidi_model_namespace::deriv_aslanidi_functor__, stan::math::var, stan::math::var>'
      requested here
            return stan::math::promote_scalar<fun_return_scalar_t__>(to_matrix(integrate_ode_bdf(deriv_aslanidi_functor__(...
                                                                               ^
/Users/krzysztof/Downloads/aslanidi.hpp:724:44: note: in instantiation of function template specialization
      'aslanidi_model_namespace::solve_aslanidi_forced_ode<double, stan::math::var, stan::math::var, double>' requested here
                stan::math::assign(I_temp, solve_aslanidi_forced_ode(ts,X0,theta,V, pstream__));
                                           ^
stan/src/stan/model/log_prob_grad.hpp:44:28: note: in instantiation of function template specialization
      'aslanidi_model_namespace::aslanidi_model::log_prob<true, true, stan::math::var>' requested here
          = model.template log_prob<propto, jacobian_adjust_transform>
                           ^
stan/src/stan/services/util/initialize.hpp:126:35: note: in instantiation of function template specialization
      'stan::model::log_prob_grad<true, true, aslanidi_model_namespace::aslanidi_model>' requested here
          log_prob = stan::model::log_prob_grad<true, true>
                                  ^
stan/src/stan/services/diagnose/diagnose.hpp:54:19: note: in instantiation of function template specialization
      'stan::services::util::initialize<aslanidi_model_namespace::aslanidi_model,
      boost::random::additive_combine_engine<boost::random::linear_congruential_engine<unsigned int, 40014, 0, 2147483563>,
      boost::random::linear_congruential_engine<unsigned int, 40692, 0, 2147483399> > >' requested here
          = util::initialize(model, init, rng, init_radius,
                  ^
src/cmdstan/command.hpp:132:49: note: in instantiation of function template specialization
      'stan::services::diagnose::diagnose<aslanidi_model_namespace::aslanidi_model>' requested here
        return_code = stan::services::diagnose::diagnose(model,
                                                ^
src/cmdstan/main.cpp:8:21: note: in instantiation of function template specialization
      'cmdstan::command<aslanidi_model_namespace::aslanidi_model>' requested here
    return cmdstan::command<stan_model>(argc,argv);
                    ^
stan/lib/stan_math/stan/math/rev/mat/functor/ode_system.hpp:67:12: note: candidate template ignored: substitution failure [with
      Derived1 = Eigen::Matrix<double, -1, 1, 0, -1, 1>, Derived2 = Eigen::Map<Eigen::Matrix<double, -1, -1, 0, -1, -1>, 0,
      Eigen::Stride<0, 0> >]
      void jacobian(double t, const std::vector<double>& y,
           ^
stan/lib/stan_math/stan/math/rev/mat/functor/ode_system.hpp:106:12: note: candidate function template not viable: requires 5
      arguments, but 4 were provided
      void jacobian(double t, const std::vector<double>& y,
           ^
stan/lib/stan_math/stan/math/rev/mat/functor/ode_system.hpp:125:23: error: no viable conversion from 'typename
      boost::math::tools::promote_args<double, var, var, double>::type' (aka 'stan::math::var') to 'vector<stan::math::var>'
          vector<var> dy_dt_var = f_(t, y_var, theta_var, x_, x_int_, msgs_);
                      ^           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
stan/lib/stan_math/stan/math/rev/mat/functor/cvodes_ode_data.hpp:147:21: note: in instantiation of function template
      specialization 'stan::math::ode_system<aslanidi_model_namespace::deriv_aslanidi_functor__>::jacobian<Eigen::Matrix<double,
      -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >' requested here
        ode_system_.jacobian(t, y, dy_dt, Jy, Jtheta);
                    ^
stan/lib/stan_math/stan/math/rev/mat/functor/cvodes_ode_data.hpp:79:23: note: in instantiation of member function
      'stan::math::cvodes_ode_data<aslanidi_model_namespace::deriv_aslanidi_functor__, stan::math::var,
      stan::math::var>::rhs_sens' requested here
        explicit_ode->rhs_sens(explicit_ode->y0_, explicit_ode->theta_,
                      ^
stan/lib/stan_math/stan/math/rev/mat/functor/integrate_ode_bdf.hpp:174:54: note: in instantiation of member function
      'stan::math::cvodes_ode_data<aslanidi_model_namespace::deriv_aslanidi_functor__, stan::math::var,
      stan::math::var>::ode_rhs_sens' requested here
                                          &ode_data::ode_rhs_sens,
                                                     ^
/Users/krzysztof/Downloads/aslanidi.hpp:300:80: note: in instantiation of function template specialization
      'stan::math::integrate_ode_bdf<aslanidi_model_namespace::deriv_aslanidi_functor__, stan::math::var, stan::math::var>'
      requested here
            return stan::math::promote_scalar<fun_return_scalar_t__>(to_matrix(integrate_ode_bdf(deriv_aslanidi_functor__(...
                                                                               ^
/Users/krzysztof/Downloads/aslanidi.hpp:724:44: note: in instantiation of function template specialization
      'aslanidi_model_namespace::solve_aslanidi_forced_ode<double, stan::math::var, stan::math::var, double>' requested here
                stan::math::assign(I_temp, solve_aslanidi_forced_ode(ts,X0,theta,V, pstream__));
                                           ^
stan/src/stan/model/log_prob_grad.hpp:44:28: note: in instantiation of function template specialization
      'aslanidi_model_namespace::aslanidi_model::log_prob<true, true, stan::math::var>' requested here
          = model.template log_prob<propto, jacobian_adjust_transform>
                           ^
stan/src/stan/services/util/initialize.hpp:126:35: note: in instantiation of function template specialization
      'stan::model::log_prob_grad<true, true, aslanidi_model_namespace::aslanidi_model>' requested here
          log_prob = stan::model::log_prob_grad<true, true>
                                  ^
stan/src/stan/services/diagnose/diagnose.hpp:54:19: note: in instantiation of function template specialization
      'stan::services::util::initialize<aslanidi_model_namespace::aslanidi_model,
      boost::random::additive_combine_engine<boost::random::linear_congruential_engine<unsigned int, 40014, 0, 2147483563>,
      boost::random::linear_congruential_engine<unsigned int, 40692, 0, 2147483399> > >' requested here
          = util::initialize(model, init, rng, init_radius,
                  ^
src/cmdstan/command.hpp:132:49: note: in instantiation of function template specialization
      'stan::services::diagnose::diagnose<aslanidi_model_namespace::aslanidi_model>' requested here
        return_code = stan::services::diagnose::diagnose(model,
                                                ^
src/cmdstan/main.cpp:8:21: note: in instantiation of function template specialization
      'cmdstan::command<aslanidi_model_namespace::aslanidi_model>' requested here
    return cmdstan::command<stan_model>(argc,argv);
                    ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:549:5: note: candidate constructor not viable: no known
      conversion from 'typename boost::math::tools::promote_args<double, var, var, double>::type' (aka 'stan::math::var') to
      'const std::__1::vector<stan::math::var, std::__1::allocator<stan::math::var> > &' for 1st argument
    vector(const vector& __x);
    ^
4 errors generated.
make: *** [/Users/krzysztof/Downloads/aslanidi] Error 1

@Ben_Lambert

The return value from the function passed to the ode solver must be real[] (even though your ODE only has one state):

Change the signature to:
real[] deriv_aslanidi(real t, real[] I, real[] theta, real[] x_r, int[] x_i)

And the return value to:

real dydt[1];
dydt[1] = (xinf - I[1]) / xtau;
return dydt;

@sakrejda Tnx for compile.

I think the line in the C++ was this:

stan/lib/stan_math/stan/math/rev/mat/functor/ode_system.hpp:79:23: error: no viable conversion from 'typename
      boost::math::tools::promote_args<double, var, double, double>::type' (aka 'stan::math::var') to 'vector<stan::math::var>'
          vector<var> dy_dt_var = f_(t, y_var, theta_, x_, x_int_, msgs_);

Ooh, yeah, that shouldn’t translate from Stan code
in the first place. K

Great, thank you. That sorted it (although have found another bug which I will mention in a separate topic). Best, Ben

We’re not properly tracing the data/non-data status of variables through function calls. It’s tricky and I don’t know if we’ll be able to get it sorted any time soon. We went with the more expressive but less error-checking approach, which may have been a bad idea (it can lead to Stan programs that translate to C++ but don’t compile). The alternative is to eliminate some Stan programs that would translate to C++ and compile.