21 #include <boost/test/unit_test.hpp>
22 #include <boost/utility/binary.hpp>
24 BOOST_AUTO_TEST_SUITE(BOOST_TEST_MODULE)
29 typedef CppAD::cg::CG<Scalar> CGScalar;
30 typedef CppAD::AD<CGScalar> ADScalar;
32 typedef Eigen::Matrix<ADScalar, Eigen::Dynamic, 1> ADVector;
42 model.lowerPositionLimit.head<3>().
fill(-1.);
43 model.upperPositionLimit.head<3>().
fill(1.);
46 ADModel ad_model =
model.cast<ADScalar>();
47 ADData ad_data(ad_model);
50 typedef Model::ConfigVectorType ConfigVectorType;
51 typedef Model::TangentVectorType TangentVectorType;
52 ConfigVectorType
q(
model.nq);
55 TangentVectorType
v(TangentVectorType::Random(
model.nv));
56 TangentVectorType
a(TangentVectorType::Random(
model.nv));
58 typedef ADModel::ConfigVectorType ADConfigVectorType;
59 typedef ADModel::TangentVectorType ADTangentVectorType;
61 ADConfigVectorType ad_q =
q.cast<ADScalar>();
62 ADTangentVectorType ad_v =
v.cast<ADScalar>();
63 ADTangentVectorType ad_a =
a.cast<ADScalar>();
65 ADTangentVectorType &
X = ad_a;
66 CppAD::Independent(
X);
72 CppAD::ADFun<CGScalar>
fun(
X,
Y);
75 CppAD::cg::ModelCSourceGen<Scalar> cgen(
fun,
"rnea");
76 cgen.setCreateJacobian(
true);
77 cgen.setCreateForwardZero(
true);
78 cgen.setCreateForwardOne(
true);
79 cgen.setCreateReverseOne(
true);
80 cgen.setCreateReverseTwo(
true);
81 CppAD::cg::ModelLibraryCSourceGen<Scalar> libcgen(cgen);
84 CppAD::cg::DynamicModelLibraryProcessor<Scalar>
p(libcgen);
86 CppAD::cg::GccCompiler<Scalar> compiler(PINOCCHIO_CXX_COMPILER);
87 std::unique_ptr<CppAD::cg::DynamicLib<Scalar>> dynamicLib =
p.createDynamicLibrary(compiler);
90 CppAD::cg::SaveFilesModelLibraryProcessor<Scalar> p2(libcgen);
94 std::unique_ptr<CppAD::cg::GenericModel<Scalar>> rnea_generated = dynamicLib->model(
"rnea");
97 Eigen::Map<TangentVectorType>(
x.data(),
model.nv, 1) =
a;
99 CPPAD_TESTVECTOR(
Scalar)
tau = rnea_generated->ForwardZero(
x);
101 Eigen::Map<TangentVectorType> tau_map(
tau.data(),
model.nv, 1);
103 BOOST_CHECK(tau_map.isApprox(tau_ref));
106 data.M.triangularView<Eigen::StrictlyLower>() =
107 data.M.transpose().triangularView<Eigen::StrictlyLower>();
112 BOOST_CHECK(M_map.isApprox(
data.M));
118 typedef CppAD::cg::CG<Scalar> CGScalar;
119 typedef CppAD::AD<CGScalar> ADScalar;
120 typedef CppAD::ADFun<CGScalar> ADCGFun;
122 typedef Eigen::Matrix<ADScalar, Eigen::Dynamic, 1> ADVector;
123 typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
RowMatrixXs;
135 model.lowerPositionLimit.head<3>().
fill(-1.);
136 model.upperPositionLimit.head<3>().
fill(1.);
139 ADModel ad_model =
model.cast<ADScalar>();
140 ADData ad_data(ad_model);
143 typedef Model::ConfigVectorType ConfigVectorType;
144 typedef Model::TangentVectorType TangentVectorType;
145 ConfigVectorType
q(
nq);
148 TangentVectorType
v(TangentVectorType::Random(
nv));
149 TangentVectorType
a(TangentVectorType::Random(
nv));
151 typedef ADModel::ConfigVectorType ADConfigVectorType;
152 typedef ADModel::TangentVectorType ADTangentVectorType;
154 ADConfigVectorType ad_q =
q.cast<ADScalar>();
155 ADTangentVectorType ad_v =
v.cast<ADScalar>();
156 ADTangentVectorType ad_a =
a.cast<ADScalar>();
158 ADConfigVectorType ad_X = ADConfigVectorType::Zero(
nq + 2 *
nv);
159 Eigen::DenseIndex
i = 0;
160 ad_X.segment(
i,
nq) = ad_q;
162 ad_X.segment(
i,
nv) = ad_v;
164 ad_X.segment(
i,
nv) = ad_a;
168 std::unique_ptr<CppAD::cg::ModelCSourceGen<Scalar>> cgen_ptr;
169 std::unique_ptr<CppAD::cg::ModelLibraryCSourceGen<Scalar>> libcgen_ptr;
170 std::unique_ptr<CppAD::cg::DynamicModelLibraryProcessor<Scalar>> dynamicLibManager_ptr;
171 std::unique_ptr<CppAD::cg::DynamicLib<Scalar>> dynamicLib_ptr;
172 std::unique_ptr<CppAD::cg::GenericModel<Scalar>> generatedFun_ptr;
174 const std::string & function_name =
"rnea";
175 const std::string & library_name =
"cg_rnea_eval";
176 const std::string & compile_options =
"-Ofast";
178 CppAD::Independent(ad_X);
180 ad_model, ad_data, ad_X.segment(0,
nq), ad_X.segment(
nq,
nv), ad_X.segment(
nq +
nv,
nv));
181 ADVector ad_Y = ad_data.tau;
182 ad_fun.Dependent(ad_X, ad_Y);
183 ad_fun.optimize(
"no_compare_op");
187 cgen_ptr = std::unique_ptr<CppAD::cg::ModelCSourceGen<Scalar>>(
188 new CppAD::cg::ModelCSourceGen<Scalar>(ad_fun, function_name));
189 cgen_ptr->setCreateForwardZero(
true);
190 cgen_ptr->setCreateJacobian(
true);
191 libcgen_ptr = std::unique_ptr<CppAD::cg::ModelLibraryCSourceGen<Scalar>>(
192 new CppAD::cg::ModelLibraryCSourceGen<Scalar>(*cgen_ptr));
194 dynamicLibManager_ptr = std::unique_ptr<CppAD::cg::DynamicModelLibraryProcessor<Scalar>>(
195 new CppAD::cg::DynamicModelLibraryProcessor<Scalar>(*libcgen_ptr, library_name));
197 CppAD::cg::GccCompiler<Scalar> compiler(PINOCCHIO_CXX_COMPILER);
198 std::vector<std::string> compile_flags = compiler.getCompileFlags();
199 compile_flags[0] = compile_options;
200 compiler.setCompileFlags(compile_flags);
201 dynamicLibManager_ptr->createDynamicLibrary(compiler,
false);
203 const auto it = dynamicLibManager_ptr->getOptions().find(
"dlOpenMode");
204 if (
it == dynamicLibManager_ptr->getOptions().end())
206 dynamicLib_ptr.reset(
new CppAD::cg::LinuxDynamicLib<Scalar>(
207 dynamicLibManager_ptr->getLibraryName()
208 + CppAD::cg::system::SystemInfo<>::DYNAMIC_LIB_EXTENSION));
212 int dlOpenMode = std::stoi(
it->second);
213 dynamicLib_ptr.reset(
new CppAD::cg::LinuxDynamicLib<Scalar>(
214 dynamicLibManager_ptr->getLibraryName()
215 + CppAD::cg::system::SystemInfo<>::DYNAMIC_LIB_EXTENSION,
219 generatedFun_ptr = dynamicLib_ptr->model(function_name.c_str());
221 CppAD::cg::ArrayView<Scalar> jac_(
jac.data(), (
size_t)
jac.size());
222 for (
size_t it = 0;
it < 3; ++
it)
224 std::cout <<
"test " <<
it << std::endl;
226 TangentVectorType
v(TangentVectorType::Random(
nv));
227 TangentVectorType
a(TangentVectorType::Random(
nv));
229 ConfigVectorType
x = ConfigVectorType::Zero(
nq + 2 *
nv);
231 x.segment(
i,
nq) =
q;
233 x.segment(
i,
nv) =
v;
235 x.segment(
i,
nv) =
a;
238 CppAD::cg::ArrayView<const Scalar> x_(
x.data(), (
size_t)
x.size());
239 generatedFun_ptr->Jacobian(x_, jac_);
242 data.M.triangularView<Eigen::StrictlyLower>() =
243 data.M.transpose().triangularView<Eigen::StrictlyLower>();
248 BOOST_AUTO_TEST_SUITE_END()