Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions .github/workflows/benchmarks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Benchmarks

on:
pull_request:
branches:
- "main"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
pytest:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [macos-latest]
python-version: ["3.12"]

steps:
- uses: actions/checkout@v5
with:
submodules: 'recursive'

- name: Install dependencies (macOS)
if: matrix.os == 'macos-15-intel' || matrix.os == 'macos-latest'
env:
HOMEBREW_NO_INSTALL_CLEANUP: 1
HOMEBREW_NO_AUTO_UPDATE: 1
HOMEBREW_NO_COLOR: 1
NONINTERACTIVE: 1
run: |
brew analytics off
brew install libomp
brew reinstall gcc
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v6
with:
python-version: ${{ matrix.python-version }}

- name: Install nox
run: |
pip install nox
- name: Run benchmarks
run: |
nox -s benchmarks
27 changes: 5 additions & 22 deletions .github/workflows/integration_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,11 @@ jobs:
with:
python-version: ${{ matrix.python-version }}

- name: Clone PyBaMM repository
uses: actions/checkout@v5
with:
repository: pybamm-team/PyBaMM
path: PyBaMM
fetch-depth: 0
fetch-tags: true
- name: Install nox, uv
run: |
pip install nox uv

- name: Build and test
run: |
# Install PyBaMM with dependencies
cd ./PyBaMM
pip install -e ".[all,dev,jax]"
cd ..

# Replace PyBaMM solvers
pip uninstall pybammsolvers --yes
python install_KLU_Sundials.py
pip install "numpy>=2" --upgrade
pip install .

# Run pybamm tests
cd ./PyBaMM
pytest tests/unit
pytest tests/integration
# Use PyBaMM-tests nox session
nox -s pybamm-tests
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ build
# Extra directories for local work
workspace
deploy
PyBaMM

# benchmark results
performance_results.json
54 changes: 40 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,25 +137,51 @@ if (${USE_PYTHON_CASADI})
message("Found Python CasADi library directory: ${CASADI_LIB_DIR}")
target_link_directories(idaklu PRIVATE ${CASADI_LIB_DIR})

set_target_properties(
idaklu PROPERTIES
INSTALL_RPATH "${CASADI_LIB_DIR}"
INSTALL_RPATH_USE_LINK_PATH TRUE
)
# Attempt to link the casadi library directly if found
find_library(CASADI_LIBRARY NAMES casadi PATHS ${CASADI_LIB_DIR} NO_DEFAULT_PATH)
if (CASADI_LIBRARY)
message("Found CasADi library: ${CASADI_LIBRARY}")
target_link_libraries(idaklu PRIVATE ${CASADI_LIBRARY})
else ()
message(WARNING "CasADi library not found in ${CASADI_LIB_DIR}. The target will rely on transitive linkage via CMake config if available.")
endif ()
# Set RPATH to find libraries relative to the module location
# This allows finding casadi in the same Python environment at runtime
# Module is at: site-packages/pybammsolvers/idaklu.so
# CasADi is at: site-packages/casadi/libcasadi.dylib
# SuiteSparse/SUNDIALS are found via DYLD_LIBRARY_PATH/LD_LIBRARY_PATH
# (set by noxfile or user environment) pointing to .idaklu/lib
# Note: Windows uses vcpkg with static linking, no RPATH needed

# For CI wheel builds, use BUILD_WITH_INSTALL_RPATH=FALSE so that
# wheel repair tools (delocate/auditwheel) can properly analyze dependencies.
# For local development, use BUILD_WITH_INSTALL_RPATH=TRUE so the module
# works immediately after pip install without wheel repair.
if(DEFINED ENV{CIBUILDWHEEL})
set(USE_INSTALL_RPATH_AT_BUILD FALSE)
else()
set(USE_INSTALL_RPATH_AT_BUILD TRUE)
endif()

if (APPLE)
set_target_properties(
idaklu PROPERTIES
BUILD_RPATH "${CASADI_LIB_DIR}"
BUILD_RPATH_USE_LINK_PATH FALSE
INSTALL_RPATH "@loader_path/../casadi"
BUILD_WITH_INSTALL_RPATH ${USE_INSTALL_RPATH_AT_BUILD}
)
else()
set_target_properties(
idaklu PROPERTIES
BUILD_RPATH "${CASADI_LIB_DIR}"
BUILD_RPATH_USE_LINK_PATH FALSE
INSTALL_RPATH "$ORIGIN/../casadi"
BUILD_WITH_INSTALL_RPATH ${USE_INSTALL_RPATH_AT_BUILD}
)
endif()
# Link against casadi by name, not absolute path, to avoid issues with
# pip's isolated build environments changing paths between configure and build
target_link_libraries(idaklu PRIVATE casadi)
Comment on lines +175 to +177
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good catches here, thanks! and sorry for the trouble here, I realise I was responsible for all this 😄

else ()
message(FATAL_ERROR "Could not find CasADi library directory")
endif ()
else ()
message("Trying to link against any casadi package apart from the Python one")
find_package(casadi CONFIG REQUIRED)
target_link_libraries(idaklu PRIVATE casadi)
endif ()

# openmp
Expand All @@ -180,7 +206,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR})
find_package(SUNDIALS REQUIRED)
message("SUNDIALS found in ${SUNDIALS_INCLUDE_DIR}: ${SUNDIALS_LIBRARIES}")
target_include_directories(idaklu PRIVATE ${SUNDIALS_INCLUDE_DIR})
target_link_libraries(idaklu PRIVATE ${SUNDIALS_LIBRARIES} casadi)
target_link_libraries(idaklu PRIVATE ${SUNDIALS_LIBRARIES})

# link suitesparse
# if using vcpkg, use config mode to
Expand Down
40 changes: 36 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ pip install pybammsolvers
The following solvers are available:
- PyBaMM's IDAKLU solver

## Local builds
## Development

### Local builds

For testing new solvers and unsupported architectures, local builds are possible.

### Nox
#### Nox (Recommended)

Nox can be used to do a quick build:
```bash
Expand All @@ -26,7 +28,7 @@ nox
```
This will setup an environment and attempt to build the library.

### MacOS
#### MacOS

Mac dependencies can be installed using brew
```bash
Expand All @@ -37,7 +39,7 @@ python install_KLU_Sundials.py
pip install .
```

### Linux
#### Linux

Linux installs may vary based on the distribution, however, the basic build can
be performed with the following commands:
Expand All @@ -48,3 +50,33 @@ pip install cmake casadi setuptools wheel "pybind11[global]"
python install_KLU_Sundials.py
pip install .
```

### Testing

The project includes comprehensive test suites:

#### Unit Tests
Test pybammsolvers functionality in isolation:
```bash
nox -s unit # Run all unit tests
nox -s integration # Run all integration tests
```

#### PyBaMM Integration Tests
Verify compatibility with PyBaMM:
```bash
nox -s pybamm-tests # Clone/update PyBaMM and run all tests
nox -s pybamm-tests -- --unit-only # Run only unit tests
nox -s pybamm-tests -- --integration-only # Run only integration tests
nox -s pybamm-tests -- --no-update # Skip git pull (use current version)
nox -s pybamm-tests -- --pybamm-dir ./custom/path # Use existing PyBaMM clone
nox -s pybamm-tests -- --branch develop # Use specific branch/tag
```

The integration tests ensure that changes to pybammsolvers don't break PyBaMM functionality.

### Benchmarks
Test for performance regressions against released PyBaMM:
```bash
nox -s benchmarks
```
Loading
Loading