Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ build.sh
build/
*.pyc
.venv
build-meson/
13 changes: 13 additions & 0 deletions INSTALL
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,16 @@ Installation is performed using the command:

Assimulo is then installed under Python dist-packages.
To test the installation, run "pytest".

Building with meson-python (Python >= 3.12, where numpy.distutils no longer exists)
------------------------------------------------------------------------------------
pip install --no-build-isolation \
-Csetup-args=-Dsundials-home=$CONDA_PREFIX \
-Csetup-args=-Dsuperlu-home=$CONDA_PREFIX \
-Csetup-args=-Dblas-home=$CONDA_PREFIX/lib -Csetup-args=-Dlapack-home=$CONDA_PREFIX/lib .

Options (meson.options): sundials-home, superlu-home, blas-home, lapack-home, fortran
(auto/enabled/disabled), extra-fortran-flags (default --std=legacy). Needs meson-python,
Cython >= 3, numpy, a C compiler and, for the Fortran solvers, gfortran. The build produces
the same 19 extension modules as setup.py with the same compiler flags; tools/dump_examples.py
and tests/test_build_equivalence.py compare two builds bit for bit.
4 changes: 4 additions & 0 deletions build-support/assimulo/lib/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
foreach f : ['__init__.py', 'sundials_includes.pxd', 'sundials_constants.pxi', 'sundials_callbacks.pxi',
'sundials_callbacks_ida_cvode.pxi', 'sundials_callbacks_kinsol.pxi']
stage += {'lib/' + f: configure_file(input: '../../../src/lib' / f, output: f, copy: true)}
endforeach
12 changes: 12 additions & 0 deletions build-support/assimulo/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# A mirror of the Cython sources under the package name (what setup.py copies to build/assimulo/),
# so that "from assimulo.ode cimport ODE" resolves and each .pyx is compiled as assimulo.<name>.
stage = {}
foreach f : ['__init__.py', 'constants.pxi',
'ode.pyx', 'ode.pxd', 'explicit_ode.pyx', 'explicit_ode.pxd', 'implicit_ode.pyx', 'implicit_ode.pxd',
'algebraic.pyx', 'algebraic.pxd', 'problem.pyx', 'problem.pxd', 'special_systems.pyx',
'support.pyx', 'support.pxd']
stage += {f: configure_file(input: '../../src' / f, output: f, copy: true)}
endforeach
subdir('lib')
subdir('solvers')
subdir('thirdparty')
3 changes: 3 additions & 0 deletions build-support/assimulo/solvers/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
foreach f : ['__init__.py', 'euler.pyx', 'sundials.pyx', 'kinsol.pyx']
stage += {'solvers/' + f: configure_file(input: '../../../src/solvers' / f, output: f, copy: true)}
endforeach
1 change: 1 addition & 0 deletions build-support/assimulo/thirdparty/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
subdir('radau5')
3 changes: 3 additions & 0 deletions build-support/assimulo/thirdparty/radau5/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
foreach f : ['radau5ode.pyx', 'radau5ode.pxd']
stage += {'thirdparty/radau5/' + f: configure_file(input: '../../../../thirdparty/radau5' / f, output: f, copy: true)}
endforeach
1 change: 1 addition & 0 deletions build-support/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
subdir('assimulo')
9 changes: 9 additions & 0 deletions examples/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# The examples ship inside the package (assimulo.examples); tests/test_examples.py runs them.
example_py = run_command(py, ['-c', 'import glob, os, sys; print("\\n".join(sorted(os.path.basename(f) for f in glob.glob(os.path.join(sys.argv[1], "*.py")))))',
meson.current_source_dir()], check: true).stdout().strip().split('\n')
example_files = []
foreach f : example_py
example_files += files(f)
endforeach
py.install_sources(example_files, subdir: pkg / 'examples')
install_data('kinsol_ors_matrix.mtx', install_dir: py.get_install_dir() / pkg / 'examples')
114 changes: 114 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
project('assimulo', 'c',
version: '3.8.0',
meson_version: '>= 1.1.0',
default_options: ['c_std=c99', 'warning_level=1'])

# ---------------------------------------------------------------- tools and python
py = import('python').find_installation(pure: false)
py_dep = py.dependency()
cc = meson.get_compiler('c')
fs = import('fs')

have_fortran = add_languages('fortran', native: false, required: get_option('fortran'))

np_incdir = run_command(py, ['-c', 'import numpy; print(numpy.get_include())'], check: true).stdout().strip()
np_inc = include_directories(np_incdir)
f2py_srcdir = run_command(py, ['-c', 'import numpy.f2py, os; print(os.path.join(os.path.dirname(numpy.f2py.__file__), "src"))'],
check: true).stdout().strip()
f2py_inc = include_directories(f2py_srcdir)

# Same flags as the numpy.distutils build (setup.py), so that both builds give identical numerics
c_flags = cc.get_supported_arguments(['-O2', '-fno-strict-aliasing', '-fwrapv', '-fno-merge-constants', '-msse', '-msse2', '-msse3'])
cython_c_flags = c_flags + ['-DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION'] # Cython modules only; f2py's C uses the older API
fortran_flags = []
if have_fortran
fc = meson.get_compiler('fortran')
fortran_flags = fc.get_supported_arguments(['-O3', '-funroll-loops', '-fno-second-underscore', '-fPIC'] + get_option('extra-fortran-flags'))
endif

# ---------------------------------------------------------------- SUNDIALS
sundials_home = get_option('sundials-home')
if sundials_home == ''
sundials_home = '/usr/local'
endif
sundials_incdir = sundials_home / 'include'
sundials_libdir = sundials_home / 'lib'
have_sundials = fs.exists(sundials_incdir / 'cvodes' / 'cvodes.h')
sundials_libs = []
sundials_cython_env = []
if have_sundials
sconf = run_command(py, [meson.current_source_dir() / 'tools' / 'sundials_config.py', sundials_incdir], check: true).stdout().strip().split(' ')
sundials_version = sconf[0].split('.')
sundials_index_bits = sconf[1]
sundials_with_superlu = sconf[2] == '1'
sundials_rtol_vec = sconf[3] == '1'
message('SUNDIALS @0@ found in @1@ (index @2@ bit, SuperLU_MT @3@)'.format(sconf[0], sundials_home, sundials_index_bits, sundials_with_superlu))
sundials_libnames = ['sundials_cvodes', 'sundials_nvecserial', 'sundials_idas', 'sundials_sunlinsoldense',
'sundials_sunlinsolspgmr', 'sundials_sunmatrixdense', 'sundials_sunmatrixsparse']
if sundials_version[0].to_int() >= 7
sundials_libnames += ['sundials_core']
endif
kinsol_libnames = ['sundials_kinsol', 'sundials_nvecserial']
if sundials_version[0].to_int() >= 7
kinsol_libnames += ['sundials_core']
endif
else
warning('SUNDIALS not found under ' + sundials_home + ': CVode, IDA and Kinsol will not be built (-Dsundials-home=...)')
endif

# ---------------------------------------------------------------- SuperLU_MT
superlu_home = get_option('superlu-home')
have_superlu = false
superlu_libs = []
superlu_inc = []
if superlu_home != ''
# layouts: a SuperLU_MT source tree (SRC/), an install prefix (include/), or a conda-forge
# prefix whose sundials package ships the library (include/superlu_mt/, lib/)
# (slu_mt_ddefs.h is specific to SuperLU_MT; supermatrix.h also exists in plain SuperLU)
superlu_incdir = superlu_home / 'SRC'
foreach cand : [superlu_home / 'include', superlu_home / 'include' / 'superlu_mt']
if not fs.exists(superlu_incdir / 'slu_mt_ddefs.h')
superlu_incdir = cand
endif
endforeach
if fs.exists(superlu_incdir / 'slu_mt_ddefs.h')
have_superlu = true
superlu_inc = include_directories(superlu_incdir)
superlu_libdir = superlu_home / 'lib'
superlu_libs = [cc.find_library('superlu_mt_OPENMP', dirs: [superlu_libdir], static: true),
cc.find_library('blas_OPENMP', dirs: [superlu_libdir], required: false)]
message('SuperLU_MT found in ' + superlu_home)
else
warning('SuperLU_MT not found under ' + superlu_home + ' (no slu_mt_ddefs.h): sparse solvers disabled')
endif
endif

# ---------------------------------------------------------------- BLAS / LAPACK (GLIMDA, SuperLU)
blas_dirs = get_option('blas-home') != '' ? [get_option('blas-home')] : []
lapack_dirs = get_option('lapack-home') != '' ? [get_option('lapack-home')] : []
blas_dep = cc.find_library('blas', dirs: blas_dirs, required: false)
lapack_dep = cc.find_library('lapack', dirs: lapack_dirs, required: false)
m_dep = cc.find_library('m', required: false)
gomp_dep = cc.find_library('gomp', required: false)

# ---------------------------------------------------------------- Cython
# The .pyx files cimport "assimulo.ode" etc.: build-support/assimulo/ holds copies of the .pxd
# files under that package name for the Cython include path (the sources live in src/).
# Cython is driven by tools/cythonize_one.py (a compile-time env with a version tuple cannot
# be passed on cython's command line).
subdir('build-support')
cythonize = [py, meson.current_source_dir() / 'tools' / 'cythonize_one.py']
stage_dir = meson.current_build_dir() / 'build-support'
cython_inc = ['-I', stage_dir, '-I', stage_dir / 'assimulo', '-I', stage_dir / 'assimulo' / 'lib',
'-I', stage_dir / 'assimulo' / 'thirdparty' / 'radau5'] # same as setup.py's include_path
sundials_cython_env = []
if have_sundials
sundials_cython_env = ['-E', 'SUNDIALS_VERSION=(@0@,@1@,@2@)'.format(sundials_version[0], sundials_version[1], sundials_version[2]),
'-E', 'SUNDIALS_WITH_SUPERLU=' + ((sundials_with_superlu and have_superlu) ? 'True' : 'False'),
'-E', 'SUNDIALS_VECTOR_SIZE="@0@"'.format(sundials_index_bits),
'-E', 'SUNDIALS_CVODE_RTOL_VEC=' + (sundials_rtol_vec ? 'True' : 'False')]
endif

subdir('src')
subdir('thirdparty')
subdir('examples')
12 changes: 12 additions & 0 deletions meson.options
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
option('sundials-home', type: 'string', value: '',
description: 'Prefix of the SUNDIALS installation (include/ and lib/ below it); empty: /usr/local')
option('superlu-home', type: 'string', value: '',
description: 'Prefix of SuperLU_MT (include/ or SRC/ with supermatrix.h, lib/ with libsuperlu_mt_OPENMP.a); empty: no sparse Radau5/CVode')
option('blas-home', type: 'string', value: '',
description: 'Directory containing libblas (GLIMDA, SuperLU); empty: compiler default paths')
option('lapack-home', type: 'string', value: '',
description: 'Directory containing liblapack (GLIMDA); empty: compiler default paths')
option('fortran', type: 'feature', value: 'auto',
description: 'Build the Fortran solvers (Dopri5, RodasODE, Radau5/Radar5 Fortran, LSODAR, ODASSL, DASP3, GLIMDA)')
option('extra-fortran-flags', type: 'array', value: ['--std=legacy'],
description: 'Extra Fortran compile flags')
25 changes: 25 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[build-system]
build-backend = "mesonpy"
requires = ["meson-python>=0.15.0", "Cython>=3.0", "numpy>=1.25"]

[project]
name = "Assimulo"
version = "3.8.0"
description = "A package for solving ordinary differential equations and differential algebraic equations"
readme = "README.md"
license = {text = "LGPL-3.0"}
requires-python = ">=3.9"
dependencies = ["numpy", "scipy", "matplotlib"]
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)",
"Topic :: Scientific/Engineering :: Mathematics",
]

[project.urls]
Homepage = "https://github.com/modelon-community/Assimulo"

[tool.meson-python.args]
# The numerics of the shipped solvers are compared bit for bit against the numpy.distutils
# build (-O2 for C, -O3 -funroll-loops for Fortran); the flags live in meson.build.
setup = ["-Dbuildtype=release"]
3 changes: 3 additions & 0 deletions src/lib/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
py.install_sources(['__init__.py', 'radau_core.py'], subdir: pkg / 'lib')
sundials_pxi = files('sundials_includes.pxd', 'sundials_constants.pxi', 'sundials_callbacks.pxi',
'sundials_callbacks_ida_cvode.pxi', 'sundials_callbacks_kinsol.pxi')
33 changes: 33 additions & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# ---------------------------------------------------------------- package files
pkg = 'assimulo'
version_txt = configure_file(output: 'version.txt', command: [py, '-c', 'import sys; print("@0@")'.format(meson.project_version())],
capture: true)
install_data(version_txt, install_dir: py.get_install_dir() / pkg)
py.install_sources(['__init__.py', 'exception.py', 'problem_algebraic.py',
'ode.pxd', 'explicit_ode.pxd', 'implicit_ode.pxd', 'algebraic.pxd', 'problem.pxd', 'support.pxd'],
subdir: pkg)
install_data(['../LICENSE', '../CHANGELOG', '../README.md'], install_dir: py.get_install_dir() / pkg)

# ---------------------------------------------------------------- core Cython modules
core_pxd = files('ode.pxd', 'explicit_ode.pxd', 'implicit_ode.pxd', 'algebraic.pxd', 'problem.pxd', 'support.pxd', 'constants.pxi')

src_inc = include_directories('.') # ode_event_locator.h, declared in explicit_ode.pxd

core_modules = {
'explicit_ode': ['ode_event_locator.c'],
'algebraic': [], 'implicit_ode': [], 'ode': [], 'problem': [], 'special_systems': [], 'support': [],
}
foreach name, extra_c : core_modules
c_src = custom_target(name + '_cython',
input: stage[name + '.pyx'], output: name + '.c',
command: cythonize + ['@INPUT@', '@OUTPUT@', '-m', pkg + '.' + name] + cython_inc,
depend_files: core_pxd)
py.extension_module(name, [c_src] + extra_c,
include_directories: [np_inc, src_inc],
c_args: cython_c_flags,
dependencies: [py_dep],
install: true, subdir: pkg)
endforeach

subdir('lib')
subdir('solvers')
36 changes: 36 additions & 0 deletions src/solvers/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
py.install_sources(['__init__.py', 'dasp3.py', 'glimda.py', 'odassl.py', 'odepack.py', 'radar5.py', 'radau5.py',
'rosenbrock.py', 'runge_kutta.py'], subdir: pkg / 'solvers')

# Euler
euler_c = custom_target('euler_cython', input: stage['solvers/euler.pyx'], output: 'euler.c',
command: cythonize + ['@INPUT@', '@OUTPUT@', '-m', pkg + '.solvers.euler'] + cython_inc, depend_files: core_pxd)
py.extension_module('euler', euler_c, include_directories: [np_inc, src_inc], c_args: cython_c_flags,
dependencies: [py_dep], install: true, subdir: pkg / 'solvers')

# SUNDIALS: CVode / IDA and Kinsol
if have_sundials
sundials_incs = [np_inc, src_inc, include_directories(sundials_incdir)]
sundials_deps = [py_dep]
foreach l : sundials_libnames
sundials_deps += cc.find_library(l, dirs: [sundials_libdir])
endforeach
kinsol_deps = [py_dep]
foreach l : kinsol_libnames
kinsol_deps += cc.find_library(l, dirs: [sundials_libdir])
endforeach
if sundials_with_superlu and have_superlu
sundials_deps += [cc.find_library('sundials_sunlinsolsuperlumt', dirs: [sundials_libdir])] + superlu_libs
kinsol_deps += superlu_libs
sundials_incs += [superlu_inc]
endif
sundials_c = custom_target('sundials_cython', input: stage['solvers/sundials.pyx'], output: 'sundials.c',
command: cythonize + ['@INPUT@', '@OUTPUT@', '-m', pkg + '.solvers.sundials'] + cython_inc + sundials_cython_env,
depend_files: core_pxd + sundials_pxi)
py.extension_module('sundials', sundials_c, include_directories: sundials_incs, c_args: cython_c_flags,
dependencies: sundials_deps, install: true, subdir: pkg / 'solvers')
kinsol_c = custom_target('kinsol_cython', input: stage['solvers/kinsol.pyx'], output: 'kinsol.c',
command: cythonize + ['@INPUT@', '@OUTPUT@', '-m', pkg + '.solvers.kinsol'] + cython_inc + sundials_cython_env,
depend_files: core_pxd + sundials_pxi)
py.extension_module('kinsol', kinsol_c, include_directories: sundials_incs, c_args: cython_c_flags,
dependencies: kinsol_deps, install: true, subdir: pkg / 'solvers')
endif
44 changes: 44 additions & 0 deletions tests/test_build_equivalence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (C) 2026 Modelon AB
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Bit-for-bit equivalence of this build with a reference build.

python tools/dump_examples.py reference.npz # with the reference build installed
ASSIMULO_REFERENCE_RESULTS=reference.npz pytest tests/test_build_equivalence.py

Skipped when no reference file is given. The reference is specific to the machine and the
compilers (the point is to compare two builds of the same sources on the same machine, e.g.
the numpy.distutils build against the meson-python build).
"""
import os
import sys

import pytest

TOOLS = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "tools")


@pytest.mark.skipif(not os.environ.get("ASSIMULO_REFERENCE_RESULTS"),
reason="set ASSIMULO_REFERENCE_RESULTS to a dump made with tools/dump_examples.py")
def test_examples_bit_identical_to_reference(tmp_path):
sys.path.insert(0, TOOLS)
import dump_examples
out, skipped = dump_examples.run_all()
assert not skipped, skipped
current = str(tmp_path / "current.npz")
dump_examples.save(current, out, skipped)
assert dump_examples.compare(os.environ["ASSIMULO_REFERENCE_RESULTS"], current)
Loading