-
Notifications
You must be signed in to change notification settings - Fork 46
Implement a Eigen-CppAD skeleton #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
62ce4e8
Implement some CppAD-Eigen utilities
GiulioRomualdi 65defd3
Implement the AutoDiff cmake skeleton
GiulioRomualdi 22f4779
Implement CppAD unit test
GiulioRomualdi f06aec3
Implement Findcppad.cmake
GiulioRomualdi 0b36dfc
Add CppAD as custom dependency
GiulioRomualdi 2db6ebd
Enable the compilation of AutoDiff component
GiulioRomualdi 7690c8b
Install CppAD in GitHub CI [Linux and MacOS]
GiulioRomualdi 27d0d76
Bugfix in Findcppad.cmake if CppAD is not found
GiulioRomualdi 2814e3b
Rename VectorXcppAD typedef to VectorXAD
GiulioRomualdi be65efd
Add cppad as public dependency in the main CMakeLists.txt
GiulioRomualdi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| #.rst: | ||
| # Findcppad | ||
| # --------- | ||
| # | ||
| # Try to locate the cppad library | ||
| # | ||
| # On non Windows systems, use pkg-config to try to locate the library, | ||
| # if this fails then try to locate the library in the directory pointed | ||
| # by the cppad_DIR environment variable. | ||
| # | ||
| # On Windows systems, it is not supported yet | ||
| # | ||
| # Create the following variables:: | ||
| # | ||
| # cppad_INCLUDE_DIRS - Directories to include to use cppad | ||
| # cppad_LIBRARIES - Default library to link against to use cppad | ||
| # cppad_DEFINITIONS - Flags to be added to linker's options | ||
| # cppad_LINK_FLAGS - Flags to be added to linker's options | ||
| # cppad_FOUND - If false, don't try to use cppad | ||
|
|
||
| #============================================================================= | ||
|
|
||
| if(NOT WIN32) | ||
| # On non Windows systems we use PkgConfig to find cppad | ||
| find_package(PkgConfig QUIET) | ||
| if(PKG_CONFIG_FOUND) | ||
|
|
||
| if(cppad_FIND_VERSION) | ||
| if(cppad_FIND_VERSION_EXACT) | ||
| pkg_check_modules(_PC_cppad QUIET cppad=${cppad_FIND_VERSION}) | ||
| else() | ||
| pkg_check_modules(_PC_cppad QUIET cppad>=${cppad_FIND_VERSION}) | ||
| endif() | ||
| else() | ||
| pkg_check_modules(_PC_cppad QUIET cppad) | ||
| endif() | ||
|
|
||
| if(_PC_cppad_FOUND) | ||
| set(cppad_INCLUDE_DIRS ${_PC_cppad_INCLUDE_DIRS} CACHE PATH "cppad include directory") | ||
| set(cppad_DEFINITIONS ${_PC_cppad_CFLAGS} CACHE STRING "Additional compiler flags for cppad") | ||
|
|
||
| find_library(${_PC_cppad_LIBRARIES}_PATH | ||
| NAMES ${_PC_cppad_LIBRARIES} | ||
| PATHS ${_PC_cppad_LIBRARY_DIRS}) | ||
|
|
||
| set(cppad_LIBRARIES ${${_PC_cppad_LIBRARIES}_PATH} CACHE PATH "cppad libraries" FORCE) | ||
|
|
||
| else() | ||
| set(cppad_DEFINITIONS "") | ||
| endif() | ||
|
|
||
| endif() | ||
|
|
||
| set(cppad_LINK_FLAGS "") | ||
|
|
||
| # If pkg-config fails, try to find the package using cppad_DIR | ||
| if(NOT _PC_cppad_FOUND) | ||
| set(cppad_DIR_TEST $ENV{cppad_DIR}) | ||
| if(cppad_DIR_TEST) | ||
| set(cppad_DIR $ENV{cppad_DIR} CACHE PATH "Path to cppad build directory") | ||
| else() | ||
| set(cppad_DIR /usr CACHE PATH "Path to cppad build directory") | ||
| endif() | ||
|
|
||
| find_path(cppad_INCLUDE_DIRS | ||
| NAMES cppad.hpp PATH_SUFFIXES cppad PATHS ${cppad_DIR}/include/cppad) | ||
|
|
||
| find_library(cppad_LIBRARIES cppad ${cppad_DIR}/lib ${cppad_DIR}/lib/cppad) | ||
|
|
||
| set(cppad_DEFINITIONS "") | ||
| set(cppad_LINK_FLAGS "") | ||
| endif() | ||
|
|
||
| # Windows platforms | ||
| else() | ||
| message(WARNING "Windows is not supported yet.") | ||
| endif() | ||
|
|
||
|
|
||
| mark_as_advanced(cppad_INCLUDE_DIRS | ||
| cppad_LIBRARIES | ||
| cppad_DEFINITIONS | ||
| cppad_LINK_FLAGS) | ||
|
|
||
| include(FindPackageHandleStandardArgs) | ||
| find_package_handle_standard_args(cppad DEFAULT_MSG cppad_LIBRARIES) | ||
|
|
||
| if(NOT cppad_FOUND) | ||
| return() | ||
| endif() | ||
|
|
||
| if(NOT TARGET cppad) | ||
| add_library(cppad UNKNOWN IMPORTED) | ||
| set_target_properties(cppad PROPERTIES IMPORTED_LOCATION ${cppad_LIBRARIES}) | ||
| set_target_properties(cppad PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${cppad_INCLUDE_DIRS}") | ||
| endif() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # Copyright (C) 2020 Istituto Italiano di Tecnologia (IIT). All rights reserved. | ||
| # This software may be modified and distributed under the terms of the | ||
| # GNU Lesser General Public License v2.1 or any later version. | ||
|
|
||
| if(FRAMEWORK_COMPILE_AutoDiffCppAD) | ||
|
|
||
| add_bipedal_locomotion_library( | ||
| NAME AutoDiffCppAD | ||
| PUBLIC_HEADERS include/BipedalLocomotion/AutoDiff/CppAD.h | ||
| PUBLIC_LINK_LIBRARIES Eigen3::Eigen cppad | ||
| IS_INTERFACE | ||
| SUBDIRECTORIES tests) | ||
|
|
||
| endif() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /** | ||
| * @file CppAD.h | ||
| * @authors Giulio Romualdi | ||
| * @copyright 2020 Istituto Italiano di Tecnologia (IIT). This software may be modified and | ||
| * distributed under the terms of the GNU Lesser General Public License v2.1 or any later version. | ||
| */ | ||
|
|
||
| #ifndef BIPEDAL_LOCOMOTION_AUTODIFF_CPPAD_H | ||
| #define BIPEDAL_LOCOMOTION_AUTODIFF_CPPAD_H | ||
|
|
||
| #include <Eigen/Dense> | ||
| #include <cppad/cppad.hpp> | ||
| #include <cppad/example/cppad_eigen.hpp> | ||
|
|
||
| namespace Eigen | ||
| { | ||
| namespace internal | ||
| { | ||
|
|
||
| /** | ||
| * cast_impl | ||
| * @note Please find more information here: | ||
| * https://stackoverflow.com/questions/10088002/how-to-implement-static-cast-in-c/10088161 | ||
| */ | ||
| template <typename Scalar> struct cast_impl<CppAD::AD<Scalar>, Scalar> | ||
| { | ||
| // Define eigen function | ||
| EIGEN_DEVICE_FUNC | ||
|
|
||
| static inline Scalar run(const CppAD::AD<Scalar>& x) | ||
| { | ||
| return CppAD::Value(x); | ||
| } | ||
| }; | ||
| } // namespace internal | ||
| } // namespace Eigen | ||
|
|
||
| namespace BipedalLocomotion | ||
| { | ||
| namespace AutoDiff | ||
| { | ||
| namespace CppAD | ||
| { | ||
|
|
||
| // Generate the VectorX and MatrixX typedef for CppAD | ||
| #define EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix) \ | ||
| typedef Eigen::Matrix<Type, Size, Size> Matrix##SizeSuffix##TypeSuffix; \ | ||
| typedef Eigen::Matrix<Type, Size, 1> Vector##SizeSuffix##TypeSuffix; \ | ||
| typedef Eigen::Matrix<Type, 1, Size> RowVector##SizeSuffix##TypeSuffix; | ||
|
|
||
| #define EIGEN_MAKE_TYPEDEFS_ALL_SIZES(Type, TypeSuffix) \ | ||
| EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Eigen::Dynamic, X) | ||
|
|
||
| EIGEN_MAKE_TYPEDEFS_ALL_SIZES(::CppAD::AD<double>, AD) | ||
|
|
||
| #undef EIGEN_MAKE_TYPEDEFS_ALL_SIZES | ||
| #undef EIGEN_MAKE_TYPEDEFS | ||
| #undef EIGEN_MAKE_FIXED_TYPEDEFS | ||
|
|
||
| } // namespace CppAD | ||
| } // namespace AutoDiff | ||
| } // namespace BipedalLocomotion | ||
|
|
||
| #endif // BIPEDAL_LOCOMOTION_AUTODIFF_CPPAD_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # Copyright (C) 2020 Istituto Italiano di Tecnologia (IIT). All rights reserved. | ||
| # This software may be modified and distributed under the terms of the | ||
| # GNU Lesser General Public License v2.1 or any later version. | ||
|
|
||
| add_bipedal_test( | ||
| NAME CppADTest | ||
| SOURCES CppADTest.cpp | ||
| LINKS BipedalLocomotion::AutoDiffCppAD) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /** | ||
| * @file CppADTest.cpp | ||
| * @authors Giulio Romualdi | ||
| * @copyright 2020 Istituto Italiano di Tecnologia (IIT). This software may be modified and | ||
| * distributed under the terms of the GNU Lesser General Public License v2.1 or any later version. | ||
| */ | ||
|
|
||
| // Catch2 | ||
| #include <catch2/catch.hpp> | ||
|
|
||
| #include <BipedalLocomotion/AutoDiff/CppAD.h> | ||
|
|
||
| TEST_CASE("CppAD and Eigen") | ||
| { | ||
| // Some coefficients useful for the test | ||
| constexpr double a = 4; | ||
| constexpr double b = 1.1341; | ||
| constexpr double c = 2.3213; | ||
|
|
||
| BipedalLocomotion::AutoDiff::CppAD::VectorXAD x(3); | ||
|
|
||
| // Start recording | ||
| CppAD::Independent(x); | ||
| BipedalLocomotion::AutoDiff::CppAD::VectorXAD y = a * x.array() + c * (b * x.array()).sin(); | ||
|
|
||
| // stop recording | ||
| CppAD::ADFun<double> f(x, y); | ||
|
|
||
| Eigen::VectorXd xNum = Eigen::VectorXd::Random(3); | ||
| Eigen::VectorXd jac = f.Jacobian(xNum); | ||
|
|
||
| // The Jacobian is stored as a row-major vector | ||
| Eigen::Map<Eigen::Matrix<double, 3, 3, Eigen::RowMajor>> jacMatrix(jac.data()); | ||
|
|
||
| // Compute the analytic Jacobian | ||
| auto analyticJacobian = [&a, &b](const Eigen::Ref<Eigen::Vector3d>& x) -> Eigen::Matrix3d { | ||
| Eigen::Vector3d jacobian; | ||
| jacobian = a + c * b * (b * x.array()).cos(); | ||
| return jacobian.asDiagonal(); | ||
| }; | ||
|
|
||
| constexpr double tolerance = 1e-4; | ||
| REQUIRE(jacMatrix.isApprox(analyticJacobian(xNum), tolerance)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.