<commit_before>
#include <stan/math/rev.hpp>
#include <gtest/gtest.h>
#include <stan/math/rev/functor/gradient.hpp>
#include <iostream>
#include <sstream>
#include <vector>
#include <stdexcept>

using stan::math::var;
using stan::math::lognormal_rng;

struct pkpd_rhs {
  template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
  inline Eigen::Matrix<stan::return_type_t<T1, T2, T3, T4, T5, T6, T7, T8>, Eigen::Dynamic, 1>
  operator()(const T0& t,
             const Eigen::Matrix<T1, Eigen::Dynamic, 1>& y,
             std::ostream* msgs,
             const T2& ka,
             const T3& ke,
             const T4& k12,
             const T5& k21,
             const T6& kin,
             const T7& kout,
             const T8& ea50) const {
    Eigen::Matrix<stan::return_type_t<T1, T2, T3, T4, T5, T6, T7, T8>, Eigen::Dynamic, 1> dydt(4);

    dydt <<
        -ka * y(0),
        +ka * y(0) - ke * y(1) - k12 * y(1) + k21 * y(2),
        +k12 * y(1) - k21 * y(2),
        +kin - kout * (1.0 - y(1) / (y(1) + ea50) ) * y(3);
    
    return dydt;
  }  
};

TEST(StanMathOdeBench, bdf) {

  double true_CL = 8.0;
  double true_Q = 18.0;
  double true_V1 = 10.0;
  double true_V2 = 14.0;
  double true_ka = log(2.0)/2.0;
  double true_pd0 = 1.0;
  double true_kin = 4.0;
  double true_kout = true_kin/true_pd0;
  double true_ec50 = 0.01;
  std::vector<double> ts{1., 2.0, 4.0, 8.0, 16.0, 32.0, 64.0};
  std::size_t ts_size = ts.size();

  pkpd_rhs ode;

  boost::ecuyer1988 base_rng(563646);

  for(std::size_t i = 0; i != 200; i++) {
    stan::math::nested_rev_autodiff nested;
    
    var CL = lognormal_rng(true_CL, 4.0, base_rng);
    var Q = lognormal_rng(true_Q, 4.0, base_rng);
    var V1 = lognormal_rng(true_V1, 2.0, base_rng);
    var V2 = lognormal_rng(true_V2, 2.0, base_rng);
    var ka = true_ka;
    var pd0 = true_pd0;
    var kin = true_kin;
    var kout =  kin/ pd0;
    var ec50 = true_ec50;
    var ea50 = ec50 * V1;
    
    var ke = CL / V1;
    var k12 = Q / V2;
    var k21 = k12 * V1 / V2;
    
    Eigen::Matrix<var, Eigen::Dynamic, 1> y0(4);
    y0 << 4.0, 0.0, 0.0, pd0;

    double t0 = 0.0;

    std::vector<Eigen::Matrix<var, Eigen::Dynamic, 1>> y
        = ode_bdf_tol(ode, y0, t0, ts, 1E-8, 1E-8, 10000, nullptr, ka, ke, k12, k21, kin, kout, ea50);

    stan::math::grad();
  }

}

TEST(StanMathOdeBench, bdf_adjoint) {

  double true_CL = 8.0;
  double true_Q = 18.0;
  double true_V1 = 10.0;
  double true_V2 = 14.0;
  double true_ka = log(2.0)/2.0;
  double true_pd0 = 1.0;
  double true_kin = 4.0;
  double true_kout = true_kin/true_pd0;
  double true_ec50 = 0.01;
  std::vector<double> ts{1., 2.0, 4.0, 8.0, 16.0, 32.0, 64.0};
  std::size_t ts_size = ts.size();

  pkpd_rhs ode;

  boost::ecuyer1988 base_rng(563646);

  for(std::size_t i = 0; i != 200; i++) {
    stan::math::nested_rev_autodiff nested;
    
    var CL = lognormal_rng(true_CL, 4.0, base_rng);
    var Q = lognormal_rng(true_Q, 4.0, base_rng);
    var V1 = lognormal_rng(true_V1, 2.0, base_rng);
    var V2 = lognormal_rng(true_V2, 2.0, base_rng);
    var ka = true_ka;
    var pd0 = true_pd0;
    var kin = true_kin;
    var kout =  kin/ pd0;
    var ec50 = true_ec50;
    var ea50 = ec50 * V1;
    
    var ke = CL / V1;
    var k12 = Q / V2;
    var k21 = k12 * V1 / V2;
    
    Eigen::Matrix<var, Eigen::Dynamic, 1> y0(4);
    y0 << 4.0, 0.0, 0.0, pd0;

    double t0 = 0.0;

    std::vector<Eigen::Matrix<var, Eigen::Dynamic, 1>> y
        = ode_bdf_adjoint_tol(ode, y0, t0, ts, 1E-8, 1E-8, 10000, nullptr, ka, ke, k12, k21, kin, kout, ea50);

    stan::math::grad();
  }

}
<commit_msg>[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final)<commit_after>
#include <stan/math/rev.hpp>
#include <gtest/gtest.h>
#include <stan/math/rev/functor/gradient.hpp>
#include <iostream>
#include <sstream>
#include <vector>
#include <stdexcept>

using stan::math::lognormal_rng;
using stan::math::var;

struct pkpd_rhs {
  template <typename T0, typename T1, typename T2, typename T3, typename T4,
            typename T5, typename T6, typename T7, typename T8>
  inline Eigen::Matrix<stan::return_type_t<T1, T2, T3, T4, T5, T6, T7, T8>,
                       Eigen::Dynamic, 1>
  operator()(const T0& t, const Eigen::Matrix<T1, Eigen::Dynamic, 1>& y,
             std::ostream* msgs, const T2& ka, const T3& ke, const T4& k12,
             const T5& k21, const T6& kin, const T7& kout,
             const T8& ea50) const {
    Eigen::Matrix<stan::return_type_t<T1, T2, T3, T4, T5, T6, T7, T8>,
                  Eigen::Dynamic, 1>
        dydt(4);

    dydt << -ka * y(0), +ka * y(0) - ke * y(1) - k12 * y(1) + k21 * y(2),
        +k12 * y(1) - k21 * y(2),
        +kin - kout * (1.0 - y(1) / (y(1) + ea50)) * y(3);

    return dydt;
  }
};

TEST(StanMathOdeBench, bdf) {
  double true_CL = 8.0;
  double true_Q = 18.0;
  double true_V1 = 10.0;
  double true_V2 = 14.0;
  double true_ka = log(2.0) / 2.0;
  double true_pd0 = 1.0;
  double true_kin = 4.0;
  double true_kout = true_kin / true_pd0;
  double true_ec50 = 0.01;
  std::vector<double> ts{1., 2.0, 4.0, 8.0, 16.0, 32.0, 64.0};
  std::size_t ts_size = ts.size();

  pkpd_rhs ode;

  boost::ecuyer1988 base_rng(563646);

  for (std::size_t i = 0; i != 200; i++) {
    stan::math::nested_rev_autodiff nested;

    var CL = lognormal_rng(true_CL, 4.0, base_rng);
    var Q = lognormal_rng(true_Q, 4.0, base_rng);
    var V1 = lognormal_rng(true_V1, 2.0, base_rng);
    var V2 = lognormal_rng(true_V2, 2.0, base_rng);
    var ka = true_ka;
    var pd0 = true_pd0;
    var kin = true_kin;
    var kout = kin / pd0;
    var ec50 = true_ec50;
    var ea50 = ec50 * V1;

    var ke = CL / V1;
    var k12 = Q / V2;
    var k21 = k12 * V1 / V2;

    Eigen::Matrix<var, Eigen::Dynamic, 1> y0(4);
    y0 << 4.0, 0.0, 0.0, pd0;

    double t0 = 0.0;

    std::vector<Eigen::Matrix<var, Eigen::Dynamic, 1>> y
        = ode_bdf_tol(ode, y0, t0, ts, 1E-8, 1E-8, 10000, nullptr, ka, ke, k12,
                      k21, kin, kout, ea50);

    stan::math::grad();
  }
}

TEST(StanMathOdeBench, bdf_adjoint) {
  double true_CL = 8.0;
  double true_Q = 18.0;
  double true_V1 = 10.0;
  double true_V2 = 14.0;
  double true_ka = log(2.0) / 2.0;
  double true_pd0 = 1.0;
  double true_kin = 4.0;
  double true_kout = true_kin / true_pd0;
  double true_ec50 = 0.01;
  std::vector<double> ts{1., 2.0, 4.0, 8.0, 16.0, 32.0, 64.0};
  std::size_t ts_size = ts.size();

  pkpd_rhs ode;

  boost::ecuyer1988 base_rng(563646);

  for (std::size_t i = 0; i != 200; i++) {
    stan::math::nested_rev_autodiff nested;

    var CL = lognormal_rng(true_CL, 4.0, base_rng);
    var Q = lognormal_rng(true_Q, 4.0, base_rng);
    var V1 = lognormal_rng(true_V1, 2.0, base_rng);
    var V2 = lognormal_rng(true_V2, 2.0, base_rng);
    var ka = true_ka;
    var pd0 = true_pd0;
    var kin = true_kin;
    var kout = kin / pd0;
    var ec50 = true_ec50;
    var ea50 = ec50 * V1;

    var ke = CL / V1;
    var k12 = Q / V2;
    var k21 = k12 * V1 / V2;

    Eigen::Matrix<var, Eigen::Dynamic, 1> y0(4);
    y0 << 4.0, 0.0, 0.0, pd0;

    double t0 = 0.0;

    std::vector<Eigen::Matrix<var, Eigen::Dynamic, 1>> y
        = ode_bdf_adjoint_tol(ode, y0, t0, ts, 1E-8, 1E-8, 10000, nullptr, ka,
                              ke, k12, k21, kin, kout, ea50);

    stan::math::grad();
  }
}

{'fraction_non_alphanumeric': 0.16026936026936026, 'fraction_numerical': 0.08632996632996633, 'mean_word_length': 2.5891735137747705, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3869146', 'n_tokens_mistral': 3642, 'n_tokens_neox': 3312, 'n_words': 795}