Skip to content

Commit cd3def9

Browse files
authored
Merge pull request #13 from proyan/devel2
expose code-handler, lang-c, varname; update readme; add example cppadcg
2 parents 6720db1 + 6831073 commit cd3def9

File tree

14 files changed

+513
-85
lines changed

14 files changed

+513
-85
lines changed

.github/workflows/ci-ubuntu-macos.yml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: PyCppAD CI for MacOS/Linux
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- master
8+
- devel
9+
10+
11+
env:
12+
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
13+
BUILD_TYPE: Release
14+
15+
jobs:
16+
build:
17+
# The CMake configure and build commands are platform agnostic and should work equally
18+
# well on Windows or Mac. You can convert this to a matrix build if you need
19+
# cross-platform coverage.
20+
# See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
21+
runs-on: ${{ matrix.os }}
22+
23+
strategy:
24+
fail-fast: false
25+
matrix:
26+
os: ["ubuntu-latest", "macos-latest"]
27+
28+
steps:
29+
- uses: actions/checkout@v2
30+
31+
- name: Checkout submodules
32+
run: |
33+
git submodule update --init
34+
35+
- uses: conda-incubator/setup-miniconda@v2
36+
with:
37+
activate-environment: pycppad
38+
auto-update-conda: true
39+
environment-file: .github/workflows/conda/conda-env.yml
40+
python-version: 3.8
41+
42+
- name: Install cmake and update conda
43+
shell: bash -l {0}
44+
run: |
45+
conda activate pycppad
46+
conda install cmake -c main
47+
48+
- name: Build PyCppAD
49+
shell: bash -l {0}
50+
run: |
51+
conda activate pycppad
52+
echo $CONDA_PREFIX
53+
54+
mkdir build
55+
cd build
56+
57+
cmake .. -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DPYTHON_EXECUTABLE=$(which python3) -DBUILD_WITH_CPPAD_CODEGEN_BINDINGS=ON
58+
make
59+
make build_tests
60+
export CTEST_OUTPUT_ON_FAILURE=1
61+
make test
62+
make install
63+
64+
- name: Uninstall PyCppAD
65+
shell: bash -l {0}
66+
run: |
67+
cd build
68+
make uninstall

.github/workflows/ci-ubuntu.yml

Lines changed: 0 additions & 52 deletions
This file was deleted.

.github/workflows/ci-windows.yml_bk

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: PyCppAD CI for Windows
2+
on:
3+
pull_request:
4+
push:
5+
branches:
6+
- master
7+
- devel
8+
9+
env:
10+
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
11+
BUILD_TYPE: Release
12+
13+
jobs:
14+
build:
15+
runs-on: ${{ matrix.os }}
16+
strategy:
17+
fail-fast: true
18+
matrix:
19+
name: [windows-latest-clang-cl]
20+
21+
include:
22+
- name: windows-latest-clang-cl
23+
os: windows-2019
24+
compiler: clang-cl
25+
26+
steps:
27+
- uses: actions/checkout@v2
28+
- name: Checkout submodules
29+
run: |
30+
git submodule update --init
31+
- uses: goanpeca/setup-miniconda@v1
32+
env:
33+
ACTIONS_ALLOW_UNSECURE_COMMANDS: 'true'
34+
with:
35+
activate-environment: pycppad
36+
environment-file: .github/workflows/conda/conda-env.yml
37+
python-version: 3.7
38+
- name: Install cmake and update conda
39+
run: |
40+
conda install cmake -c main
41+
42+
- name: Build PyCppAD
43+
shell: cmd /C CALL {0}
44+
env:
45+
ACTIONS_ALLOW_UNSECURE_COMMANDS: 'true'
46+
run: |
47+
:: unset extra Boost envs
48+
set Boost_ROOT=
49+
set BOOST_ROOT_1_69_0=
50+
set BOOST_ROOT_1_72_0=
51+
set PATH=%PATH:C:\hostedtoolcache\windows\Boost\1.72.0;=%
52+
53+
call "%programfiles(x86)%\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" amd64
54+
55+
:: Create build directory
56+
mkdir build
57+
pushd build
58+
59+
:: Configure
60+
cmake ^
61+
-G "Visual Studio 16 2019" -T "ClangCl" -DCMAKE_GENERATOR_PLATFORM=x64 ^
62+
-DCMAKE_INSTALL_PREFIX=%CONDA_PREFIX%\Library ^
63+
-DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} ^
64+
-DPYTHON_SITELIB=%CONDA_PREFIX%\Lib\site-packages ^
65+
-DPYTHON_EXECUTABLE=%CONDA_PREFIX%\python.exe ^
66+
-DBUILD_WITH_CPPAD_CODEGEN_BINDINGS=ON ^
67+
..
68+
69+
:: Build
70+
cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} --target install
71+
72+
:: Testing
73+
set PATH=%PATH%;%CONDA_PREFIX%\Lib\site-packages\pycppad
74+
ctest --output-on-failure -C Release -V
75+
76+
:: Test Python import
77+
cd ..
78+
python -c "import pycppad"

.github/workflows/conda/conda-env.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: pycppad
2+
channels:
3+
- conda-forge
4+
- nodefaults
5+
dependencies:
6+
- boost
7+
- eigenpy
8+
- python
9+
- cppad
10+
- cppadcodegen

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@
3333

3434
*~
3535
_build*
36+
37+
*.orig

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ IF(BUILD_WITH_CPPAD_CODEGEN_BINDINGS)
7777
LIST(APPEND ${PROJECT_NAME}_HEADERS
7878
include/${PROJECT_NAME}/codegen/cg.hpp
7979
include/${PROJECT_NAME}/codegen/cppadcg-scalar.hpp
80+
include/${PROJECT_NAME}/codegen/code-handler.hpp
81+
include/${PROJECT_NAME}/codegen/lang/c/language-c.hpp
82+
include/${PROJECT_NAME}/codegen/lang/c/lang-c-default-var-name-gen.hpp
8083
)
8184
ENDIF(BUILD_WITH_CPPAD_CODEGEN_BINDINGS)
8285

README.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,57 @@ PyCppAD — Python bindings for CppAD Automatic Differentiation library
22
======
33

44
<p align="center">
5-
<a href="https://opensource.org/licenses/BSD-3-Clause"><img src="https://img.shields.io/badge/License-BSD%203--Clause-green.svg" alt="License"/></a>
5+
<a href="https://opensource.org/licenses/BSD-3-Clause"><img src="https://img.shields.io/badge/License-BSD%203--Clause-green.svg" alt="License"/></a>
6+
<a href="https://anaconda.org/conda-forge/pycppad"><img src="https://img.shields.io/conda/dn/conda-forge/pycppad.svg" alt="Conda Downloads"/></a>
7+
<a href="https://anaconda.org/conda-forge/pycppad"><img src="https://img.shields.io/conda/vn/conda-forge/pycppad.svg" alt="Conda Version"/></a>
8+
<a href="https://conda.anaconda.org/conda-forge"><img src="https://anaconda.org/conda-forge/pycppad/badges/installer/conda.svg" alt="Anaconda-Server Badge"/></a>
69
</p>
710

811
**PyCppAD** is an open source framework which provides bindings for the CppAD Automatic Differentiation([CppAD](https://coin-or.github.io/CppAD/doc/cppad.htm)) C++ library in Python.
912
**PyCppAD** also includes support for the CppADCodeGen ([CppADCodeGen](https://github.com/joaoleal/CppADCodeGen)), C++ library, which exploits CppAD functionality to perform code generation.
1013

14+
## Installing PyCppAD
15+
16+
### Installation via <img src="https://s3.amazonaws.com/conda-dev/conda_logo.svg" height="18">
17+
18+
As simple as that:
19+
```bash
20+
conda install pycppad -c conda-forge
21+
```
22+
23+
### Installation by source
24+
25+
#### Dependencies
26+
27+
PyCppAD dependes on the following dependencies:
28+
* [Boost.Python](http://boostorg.github.io/python/doc/html/index.html)
29+
* [Eigen3](https://eigen.tuxfamily.org)
30+
* [EigenPy](https://github.com/stack-of-tasks/eigenpy)
31+
* [CppAD](https://coin-or.github.io/CppAD/doc/install.htm)
32+
* [CppADCodeGen](https://github.com/joaoleal/CppADCodeGen) (Optional)
33+
34+
#### Compiling PyCppAD
35+
36+
Once the dependencies have been installed, follow the following commands to compile PyCppAD:
37+
38+
```
39+
cd ${path_to_your_development_source}/
40+
git clone --recursive https://github.com/Simple-Robotics/pycppad.git
41+
cd pycppad
42+
mkdir _build-RELEASE
43+
cd _build-RELEASE
44+
cmake .. -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=${path_to_your_installation}
45+
make install
46+
```
47+
48+
Set up the `path_to_your_installation` and `path_to_your_development_source` based on your personal development environment.
49+
50+
Alternatively, in order to compile with [CppADCodeGen](https://github.com/joaoleal/CppADCodeGen) support, add `-DBUILD_WITH_CPPAD_CODEGEN_BINDINGS=ON` to the cmake command, and follow the build sequence as before:
51+
52+
```
53+
cmake .. -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=${path_to_your_installation} -DBUILD_WITH_CPPAD_CODEGEN_BINDINGS=ON
54+
```
55+
1156
## Acknowledgments
1257

1358
The development of **PyCppAD** is supported by the [Willow team](https://www.di.ens.fr/willow/) [@INRIA](http://www.inria.fr).

example/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
ADD_PYTHON_UNIT_TEST("add_eq" "example/add_eq.py" "python")
2-
ADD_PYTHON_UNIT_TEST("cppad_type" "example/cppad_type.py" "python")
2+
IF(BUILD_WITH_CPPAD_CODEGEN_BINDINGS)
3+
ADD_PYTHON_UNIT_TEST("cppadcg_c_codegen" "example/cppadcg_c_codegen.py" "python")
4+
ENDIF(BUILD_WITH_CPPAD_CODEGEN_BINDINGS)

example/cppad_type.py

Lines changed: 0 additions & 31 deletions
This file was deleted.

example/cppadcg_c_codegen.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from pycppad import AD, ADCG, CG, Independent, Value, ADCGFun, CodeHandler, LanguageC, LangCDefaultVariableNameGenerator
2+
import numpy as np
3+
4+
#/***************************************************************************
5+
# the model
6+
#*************************************************************************/
7+
8+
# independent variable vector
9+
n = 2
10+
11+
x = np.array([ADCG(CG(0.)),]*n)
12+
x[0] = ADCG(CG(2.))
13+
x[1] = ADCG(CG(3.))
14+
15+
Independent(x)
16+
17+
# dependent variable vector
18+
m=1
19+
y = np.array([ADCG(CG(0)),]*m)
20+
21+
#the model
22+
a = x[0] / ADCG(CG(1.)) + x[1] * x[1]
23+
y[0] = a / ADCG(CG(2.))
24+
25+
fun = ADCGFun(x, y); # the model tape
26+
27+
28+
# /***************************************************************************
29+
# * Generate the C source code
30+
# **************************************************************************/
31+
32+
# /**
33+
# * start the special steps for source code generation for a Jacobian
34+
# */
35+
handler = CodeHandler(50)
36+
37+
indVars = np.array([CG(0.)]*n)
38+
handler.makeVariables(indVars)
39+
40+
jac = fun.Jacobian(indVars)
41+
42+
langC = LanguageC("double", 3)
43+
nameGen = LangCDefaultVariableNameGenerator("y","x","v","array","sarray")
44+
code = handler.generateCode(langC, jac, nameGen, "source")
45+
print(code)

0 commit comments

Comments
 (0)