mxlpy-native 0.6.0__tar.gz → 0.8.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {mxlpy_native-0.6.0/src/mxlpy_native.egg-info → mxlpy_native-0.8.0}/PKG-INFO +2 -2
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/README.md +1 -1
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/mxn_solvers.c +192 -50
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/VENDORED +20 -16
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/adjoint_wrapper.c +20 -6
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/decsol.c +24 -0
- mxlpy_native-0.8.0/c/vendor/discrete_adjoint.c +1529 -0
- mxlpy_native-0.8.0/c/vendor/discrete_adjoint.h +109 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/dop853.c +14 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/dopri5.c +14 -0
- mxlpy_native-0.8.0/c/vendor/finite_jacobian.h +93 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/fit_experiments.c +38 -6
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/fit_experiments.h +6 -1
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/jacobian_wrapper.c +29 -18
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/lsoda.c +35 -10
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/lsoda.h +12 -0
- mxlpy_native-0.8.0/c/vendor/nordsieck_tape.h +46 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/radau5_wrapper.c +200 -147
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/solver_common.c +45 -11
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/solver_common.h +17 -1
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode.c +34 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode_impl.h +4 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/pyproject.toml +1 -1
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/setup.py +2 -1
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/src/mxlpy_native/__init__.py +1 -1
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0/src/mxlpy_native.egg-info}/PKG-INFO +2 -2
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/src/mxlpy_native.egg-info/SOURCES.txt +6 -0
- mxlpy_native-0.8.0/tests/test_contracts.py +287 -0
- mxlpy_native-0.8.0/tests/test_discrete_adjoint.py +141 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/tests/test_solvers.py +13 -2
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/LICENSE +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/MANIFEST.in +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/HAIRER-LICENCE.txt +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/SCIPY-LICENCE.txt +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/SUNDIALS-LICENCE.txt +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/SUNDIALS-NOTICE.txt +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/blaslapack_declarations.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/dc_decsol.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/f2c.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/libf2c_stubs.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/minpack/COPYRIGHT.txt +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/minpack/cminpack.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/minpack/cminpackP.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/minpack/dpmpar.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/minpack/enorm.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/minpack/fdjac2.c +1 -1
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/minpack/lmder.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/minpack/lmdif.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/minpack/lmpar.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/minpack/minpackP.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/minpack/qrfac.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/minpack/qrsolv.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/mxs_lapack.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/radau5.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/cvode/cvode.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/cvode/cvode_diag.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/cvode/cvode_ls.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/cvode/cvode_proj.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/nvector/nvector_serial.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/priv/sundials_context_impl.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/priv/sundials_errors_impl.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/priv/sundials_logger_macros.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_adaptcontroller.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_adjointcheckpointscheme.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_adjointstepper.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_band.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_config.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_context.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_core.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_dense.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_direct.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_domeigestimator.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_errors.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_export.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_futils.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_iterative.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_linearsolver.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_logger.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_math.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_matrix.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_memory.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_nonlinearsolver.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_nvector.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_nvector_senswrapper.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_profiler.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_stepper.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_types.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_version.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sunlinsol/sunlinsol_dense.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sunmatrix/sunmatrix_band.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sunmatrix/sunmatrix_dense.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sunmatrix/sunmatrix_sparse.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sunnonlinsol/sunnonlinsol_fixedpoint.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sunnonlinsol/sunnonlinsol_newton.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode_diag_impl.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode_fused_stubs.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode_io.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode_ls.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode_ls_impl.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode_nls.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode_proj.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode_proj_impl.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/nvector/serial/nvector_serial.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/stl/sunstl_vector.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_adiak_metadata.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_band.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_context.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_debug.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_dense.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_errors.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_futils.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_hashmap_impl.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_linearsolver.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_logger.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_logger_impl.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_macros.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_math.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_matrix.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_memory.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_nonlinearsolver.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_nvector.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_nvector_senswrapper.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_utils.h +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_version.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sunlinsol/dense/sunlinsol_dense.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sunmatrix/band/sunmatrix_band.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sunmatrix/dense/sunmatrix_dense.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sunnonlinsol/fixedpoint/sunnonlinsol_fixedpoint.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sunnonlinsol/newton/sunnonlinsol_newton.c +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/setup.cfg +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/src/mxlpy_native/__main__.py +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/src/mxlpy_native.egg-info/dependency_links.txt +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/src/mxlpy_native.egg-info/requires.txt +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/src/mxlpy_native.egg-info/top_level.txt +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/tests/test_adjoint.py +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/tests/test_build_errors.py +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/tests/test_fit.py +0 -0
- {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/tests/test_vendor.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: mxlpy-native
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.8.0
|
|
4
4
|
Summary: Compiled ODE solvers (RADAU5, LSODA, SUNDIALS CVODE) for MxlPy's Native integrator
|
|
5
5
|
Author-email: Marvin van Aalst <marvin.vanaalst@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -63,7 +63,7 @@ uv sync # builds the extension in place
|
|
|
63
63
|
uv run pytest
|
|
64
64
|
```
|
|
65
65
|
|
|
66
|
-
`c/vendor/` is copied from [mxl-solvers](https://github.com/Computational-Biology-Aachen/mxl-solvers) by its `scripts/vendor.sh`. `vendor/VENDORED` records the commit and file hashes, which `tests/test_vendor.py` checks. Fix solvers there, not here. `c/mxn_solvers.c` is the glue MxlPy calls (`mxn_radau` / `mxn_lsoda` / `mxn_cvode` for simulations, `mxn_fit_*` for least-squares fits, `mxn_adjoint_*` for adjoint gradients), compiled unchanged into the wheel and into MxlPy's `solvers.wasm` (`scripts/build_native_wasm.sh` in MxlPy).
|
|
66
|
+
`c/vendor/` is copied from [mxl-solvers](https://github.com/Computational-Biology-Aachen/mxl-solvers) by its `scripts/vendor.sh`. `vendor/VENDORED` records the commit and file hashes, which `tests/test_vendor.py` checks. Fix solvers there, not here. `c/mxn_solvers.c` is the glue MxlPy calls (`mxn_radau` / `mxn_lsoda` / `mxn_cvode` for simulations, optionally with the model's analytic Jacobian, `mxn_fit_*` for least-squares fits, `mxn_adjoint_*` for continuous and `mxn_da_*` for discrete adjoint gradients), compiled unchanged into the wheel and into MxlPy's `solvers.wasm` (`scripts/build_native_wasm.sh` in MxlPy).
|
|
67
67
|
|
|
68
68
|
When an exported function's signature or meaning changes, bump `MXN_ABI_VERSION` in `c/mxn_solvers.c`, `ABI_VERSION` in `src/mxlpy_native/__init__.py` and the ABI MxlPy expects, then release both. MxlPy refuses a mismatched pair with an upgrade hint instead of crashing.
|
|
69
69
|
|
|
@@ -39,7 +39,7 @@ uv sync # builds the extension in place
|
|
|
39
39
|
uv run pytest
|
|
40
40
|
```
|
|
41
41
|
|
|
42
|
-
`c/vendor/` is copied from [mxl-solvers](https://github.com/Computational-Biology-Aachen/mxl-solvers) by its `scripts/vendor.sh`. `vendor/VENDORED` records the commit and file hashes, which `tests/test_vendor.py` checks. Fix solvers there, not here. `c/mxn_solvers.c` is the glue MxlPy calls (`mxn_radau` / `mxn_lsoda` / `mxn_cvode` for simulations, `mxn_fit_*` for least-squares fits, `mxn_adjoint_*` for adjoint gradients), compiled unchanged into the wheel and into MxlPy's `solvers.wasm` (`scripts/build_native_wasm.sh` in MxlPy).
|
|
42
|
+
`c/vendor/` is copied from [mxl-solvers](https://github.com/Computational-Biology-Aachen/mxl-solvers) by its `scripts/vendor.sh`. `vendor/VENDORED` records the commit and file hashes, which `tests/test_vendor.py` checks. Fix solvers there, not here. `c/mxn_solvers.c` is the glue MxlPy calls (`mxn_radau` / `mxn_lsoda` / `mxn_cvode` for simulations, optionally with the model's analytic Jacobian, `mxn_fit_*` for least-squares fits, `mxn_adjoint_*` for continuous and `mxn_da_*` for discrete adjoint gradients), compiled unchanged into the wheel and into MxlPy's `solvers.wasm` (`scripts/build_native_wasm.sh` in MxlPy).
|
|
43
43
|
|
|
44
44
|
When an exported function's signature or meaning changes, bump `MXN_ABI_VERSION` in `c/mxn_solvers.c`, `ABI_VERSION` in `src/mxlpy_native/__init__.py` and the ABI MxlPy expects, then release both. MxlPy refuses a mismatched pair with an upgrade hint instead of crashing.
|
|
45
45
|
|
|
@@ -3,13 +3,16 @@
|
|
|
3
3
|
* themselves are vendored from mxl-solvers (see vendor/VENDORED); CVODE is SUNDIALS'
|
|
4
4
|
* (vendor/sundials, BSD-3-Clause, see vendor/SUNDIALS-LICENCE.txt). RADAU5 and this
|
|
5
5
|
* file keep global state, so only one solve can run at a time in a process. */
|
|
6
|
+
#include "discrete_adjoint.h"
|
|
6
7
|
#include "f2c.h"
|
|
8
|
+
#include "finite_jacobian.h"
|
|
7
9
|
#include "lsoda.h"
|
|
8
10
|
#include <cvode/cvode.h>
|
|
9
11
|
#include <nvector/nvector_serial.h>
|
|
10
12
|
#include <sunlinsol/sunlinsol_dense.h>
|
|
11
13
|
#include <sunmatrix/sunmatrix_dense.h>
|
|
12
14
|
#include <sunnonlinsol/sunnonlinsol_fixedpoint.h>
|
|
15
|
+
#include <float.h>
|
|
13
16
|
#include <math.h>
|
|
14
17
|
#include <stdint.h>
|
|
15
18
|
#include <stdlib.h>
|
|
@@ -23,7 +26,7 @@
|
|
|
23
26
|
|
|
24
27
|
/* Bump whenever an exported signature or its meaning changes, together with ABI_VERSION
|
|
25
28
|
* in mxlpy_native/__init__.py and the version MxlPy's Native integrator expects. */
|
|
26
|
-
#define MXN_ABI_VERSION
|
|
29
|
+
#define MXN_ABI_VERSION 5
|
|
27
30
|
MXN_API int mxn_abi_version(void) { return MXN_ABI_VERSION; }
|
|
28
31
|
|
|
29
32
|
extern int radau5_(integer *n, U_fp fcn, doublereal *x, doublereal *y, doublereal *xend, doublereal *h,
|
|
@@ -36,11 +39,39 @@ extern double radau5_jac_growth;
|
|
|
36
39
|
|
|
37
40
|
typedef void (*ModelFn)(int, double, double *, double *, double *);
|
|
38
41
|
static ModelFn g_model;
|
|
42
|
+
/* The model's Jacobian df/dy (ModelFn signature, n x n column-major), NULL = the solvers'
|
|
43
|
+
* finite differences. Called through finite_jacobian, which differences its non-finite columns. */
|
|
44
|
+
static ModelFn g_jac;
|
|
39
45
|
static double *g_pars; /* LSODA's right-hand side takes no parameter array. */
|
|
40
46
|
static const double *g_t_out;
|
|
41
47
|
static double *g_y_out;
|
|
42
48
|
static int g_n_out, g_next;
|
|
43
49
|
|
|
50
|
+
/* Shared by mxn_radau, mxn_lsoda and mxn_cvode: 1 if the request is unusable — no output time,
|
|
51
|
+
* a non-finite time, state or tolerance, a negative tolerance, or output times that aren't
|
|
52
|
+
* monotone (repeats are fine) — so the solvers refuse it with their bad-input code instead of
|
|
53
|
+
* reading out of bounds or integrating nonsense. */
|
|
54
|
+
static int bad_request(int n, const double *y, double rtol, double atol, int n_out, const double *t_out) {
|
|
55
|
+
if (n < 0 || n_out < 1 || !isfinite(rtol) || !isfinite(atol) || rtol < 0 || atol < 0) return 1;
|
|
56
|
+
for (int i = 0; i < n; i++)
|
|
57
|
+
if (!isfinite(y[i])) return 1;
|
|
58
|
+
const double dir = t_out[n_out - 1] >= t_out[0] ? 1.0 : -1.0;
|
|
59
|
+
for (int k = 0; k < n_out; k++) {
|
|
60
|
+
if (!isfinite(t_out[k])) return 1;
|
|
61
|
+
if (k > 0 && (t_out[k] - t_out[k - 1]) * dir < 0) return 1;
|
|
62
|
+
}
|
|
63
|
+
return 0;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/* Nothing to integrate (no state, or all output times equal): every row is y. */
|
|
67
|
+
static int trivial_request(int n, const double *y, int n_out, const double *t_out, double *y_out) {
|
|
68
|
+
if (n > 0 && t_out[n_out - 1] != t_out[0]) return 0;
|
|
69
|
+
for (int k = 0; k < n_out; k++) memcpy(y_out + (size_t)k * (size_t)n, y, (size_t)n * sizeof(double));
|
|
70
|
+
return 1;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
#define DEFAULT_NMAX 100000
|
|
74
|
+
|
|
44
75
|
/* A function pointer natively, a table index in wasm. */
|
|
45
76
|
MXN_API void mxn_set_model(intptr_t model) { g_model = (ModelFn)model; }
|
|
46
77
|
|
|
@@ -50,17 +81,26 @@ static int fcn_dispatch(integer *n, doublereal *x, doublereal *y, doublereal *f,
|
|
|
50
81
|
return 0;
|
|
51
82
|
}
|
|
52
83
|
|
|
53
|
-
static int
|
|
84
|
+
static int radau_jac(integer *n, doublereal *x, doublereal *y, doublereal *dfy, integer *ldfy,
|
|
85
|
+
doublereal *rpar, integer *ipar) {
|
|
86
|
+
(void)ldfy; /* full Jacobian: ldfy == n */
|
|
87
|
+
(void)ipar;
|
|
88
|
+
finite_jacobian(g_model, g_jac, (int)*n, *x, y, dfy, rpar);
|
|
89
|
+
return 0;
|
|
90
|
+
}
|
|
54
91
|
static int no_mas(void) { return 0; }
|
|
55
92
|
|
|
93
|
+
static double g_dir; /* direction of integration, +1 or -1 */
|
|
94
|
+
|
|
56
95
|
static int solout(integer *nr, doublereal *xold, doublereal *x, doublereal *y, doublereal *cont, integer *lrc,
|
|
57
96
|
integer *n, doublereal *rpar, integer *ipar, integer *irtrn) {
|
|
58
|
-
(void)
|
|
97
|
+
(void)xold; (void)rpar; (void)ipar;
|
|
59
98
|
*irtrn = 1;
|
|
60
|
-
while (g_next < g_n_out && g_t_out[g_next]
|
|
99
|
+
while (g_next < g_n_out && (g_t_out[g_next] - *x) * g_dir <= 0) {
|
|
61
100
|
double *row = g_y_out + (size_t)g_next * (size_t)*n;
|
|
62
101
|
doublereal s = g_t_out[g_next];
|
|
63
|
-
|
|
102
|
+
/* CONTR5 only exists from the second call on (nr == 1 comes before any step). */
|
|
103
|
+
if (s == *x || *nr < 2) {
|
|
64
104
|
memcpy(row, y, (size_t)*n * sizeof(double));
|
|
65
105
|
} else {
|
|
66
106
|
for (integer i = 1; i <= *n; i++) row[i - 1] = contr5_(&i, &s, cont, lrc);
|
|
@@ -70,13 +110,21 @@ static int solout(integer *nr, doublereal *xold, doublereal *x, doublereal *y, d
|
|
|
70
110
|
return 0;
|
|
71
111
|
}
|
|
72
112
|
|
|
73
|
-
/* Integrates from t_out[0] to t_out[n_out - 1], writing rows of n values into y_out.
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
113
|
+
/* Integrates from t_out[0] to t_out[n_out - 1], writing rows of n values into y_out. The
|
|
114
|
+
* output times are monotone in either direction (t_out[n_out - 1] < t_out[0] integrates
|
|
115
|
+
* backward) and may repeat. y holds the initial state on entry and the final state on
|
|
116
|
+
* return. jac is the model's Jacobian (see g_jac), 0 for finite differences. nmax <= 0 means
|
|
117
|
+
* 100000 steps. Returns RADAU5's IDID: positive on success, -1 bad input (also no output
|
|
118
|
+
* time, non-finite values, unsorted output times), -2 too many steps, -3 step too small
|
|
119
|
+
* (also a non-finite right-hand side), -4 singular matrix. Returns -5 if allocation fails
|
|
120
|
+
* or the result isn't finite. */
|
|
121
|
+
MXN_API int mxn_radau(int n, double *y, double *pars, intptr_t jac, double rtol, double atol, int nmax,
|
|
122
|
+
int reuse_jacobian, int n_out, const double *t_out, double *y_out) {
|
|
123
|
+
if (bad_request(n, y, rtol, atol, n_out, t_out)) return -1;
|
|
124
|
+
if (trivial_request(n, y, n_out, t_out, y_out)) return 1;
|
|
125
|
+
if (nmax <= 0) nmax = DEFAULT_NMAX;
|
|
126
|
+
g_jac = (ModelFn)jac;
|
|
127
|
+
integer N = n, itol = 0, ijac = jac ? 1 : 0, mljac = n, mujac = n, imas = 0, mlmas = n, mumas = n, iout = 1, idid = 0, ipar = 0;
|
|
80
128
|
integer lwork = 4 * N * N + 12 * N + 20, liwork = 3 * N + 20;
|
|
81
129
|
doublereal *work = calloc((size_t)lwork, sizeof(doublereal));
|
|
82
130
|
integer *iwork = calloc((size_t)liwork, sizeof(integer));
|
|
@@ -101,7 +149,8 @@ MXN_API int mxn_radau(int n, double *y, double *pars, double rtol, double atol,
|
|
|
101
149
|
g_y_out = y_out;
|
|
102
150
|
g_n_out = n_out;
|
|
103
151
|
g_next = 0;
|
|
104
|
-
|
|
152
|
+
g_dir = xend >= x ? 1.0 : -1.0;
|
|
153
|
+
radau5_(&N, (U_fp)fcn_dispatch, &x, y, &xend, &h, &rt, &at, &itol, (U_fp)radau_jac, &ijac, &mljac, &mujac,
|
|
105
154
|
(U_fp)no_mas, &imas, &mlmas, &mumas, (U_fp)solout, &iout, work, &lwork, iwork, &liwork, pars, &ipar, &idid);
|
|
106
155
|
free(work);
|
|
107
156
|
free(iwork);
|
|
@@ -116,14 +165,24 @@ MXN_API int mxn_radau(int n, double *y, double *pars, double rtol, double atol,
|
|
|
116
165
|
|
|
117
166
|
static void lsoda_fcn(int *n, double *t, double *y, double *dydt) { g_model(*n, *t, y, dydt, g_pars); }
|
|
118
167
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
*
|
|
168
|
+
static void lsoda_jac(int *n, double *t, double *y, int *ml, int *mu, double *pd, int *nrowpd) {
|
|
169
|
+
(void)ml; (void)mu; (void)nrowpd; /* full Jacobian: nrowpd == n */
|
|
170
|
+
finite_jacobian(g_model, g_jac, *n, *t, y, pd, g_pars);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/* Same contract as mxn_radau, with LSODA (SciPy's C translation), using jac or its
|
|
174
|
+
* internal finite-difference Jacobian. nmax caps the total number of steps (<= 0: 100000).
|
|
175
|
+
* Returns 2 on success, or LSODA's negative ISTATE: -1 too many steps, -2 too much accuracy
|
|
122
176
|
* requested, -3 bad input, -4 repeated error test failures, -5 repeated convergence
|
|
123
|
-
* failures, -6 an error weight became zero. Returns -
|
|
124
|
-
*
|
|
125
|
-
|
|
126
|
-
|
|
177
|
+
* failures, -6 an error weight became zero. Returns -7 if the step size became negligible
|
|
178
|
+
* (a singularity or a non-finite right-hand side), -9 if allocation fails or the result
|
|
179
|
+
* isn't finite. */
|
|
180
|
+
MXN_API int mxn_lsoda(int n, double *y, double *pars, intptr_t jac, double rtol, double atol, int nmax,
|
|
181
|
+
int n_out, const double *t_out, double *y_out) {
|
|
182
|
+
if (bad_request(n, y, rtol, atol, n_out, t_out)) return -3;
|
|
183
|
+
if (trivial_request(n, y, n_out, t_out, y_out)) return 2;
|
|
184
|
+
if (nmax <= 0) nmax = DEFAULT_NMAX;
|
|
185
|
+
g_jac = (ModelFn)jac;
|
|
127
186
|
/* Work array sizes as in SciPy's odepack: Adams up to order 12, BDF up to 5. */
|
|
128
187
|
const int lrn = 20 + n * 13 + 3 * n, lrs = 20 + n * 6 + 3 * n + n * n + 2;
|
|
129
188
|
const int lrw = lrn > lrs ? lrn : lrs, liw = 20 + n;
|
|
@@ -134,20 +193,30 @@ MXN_API int mxn_lsoda(int n, double *y, double *pars, double rtol, double atol,
|
|
|
134
193
|
free(iwork);
|
|
135
194
|
return -9;
|
|
136
195
|
}
|
|
137
|
-
|
|
138
|
-
|
|
196
|
+
/* One step at a time (ITASK = 5) and never past the last output time (TCRIT): models
|
|
197
|
+
* needn't be defined beyond it. Rows come from LSODA's interpolant over each step. */
|
|
198
|
+
const double t_end = t_out[n_out - 1], dir = t_end >= t_out[0] ? 1.0 : -1.0;
|
|
199
|
+
int itask = 5, istate = 1, iopt = 1, k = 0;
|
|
200
|
+
rwork[0] = t_end; /* RWORK(1): TCRIT */
|
|
201
|
+
iwork[5] = nmax; /* IWORK(6): steps allowed per call */
|
|
139
202
|
lsoda_common_struct_t S = {0};
|
|
140
203
|
double t = t_out[0];
|
|
141
204
|
g_pars = pars;
|
|
142
|
-
memcpy(y_out, y, (size_t)n * sizeof(double));
|
|
143
|
-
|
|
144
|
-
double tout =
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
205
|
+
for (; k < n_out && t_out[k] == t; k++) memcpy(y_out + (size_t)k * (size_t)n, y, (size_t)n * sizeof(double));
|
|
206
|
+
while (k < n_out && istate > 0) {
|
|
207
|
+
double tout = t_end;
|
|
208
|
+
lsoda(lsoda_fcn, n, y, &t, &tout, 1, &rtol, &atol, &itask, &istate, &iopt, rwork, lrw, iwork, liw,
|
|
209
|
+
jac ? lsoda_jac : NULL, jac ? 1 : 2, &S);
|
|
210
|
+
if (istate < 0) break;
|
|
211
|
+
for (; k < n_out && (t_out[k] - t) * dir <= 0; k++) {
|
|
212
|
+
double *row = y_out + (size_t)k * (size_t)n;
|
|
213
|
+
int iflag = 0;
|
|
214
|
+
if (t_out[k] == t) memcpy(row, y, (size_t)n * sizeof(double));
|
|
215
|
+
else lsoda_intdy(t_out[k], 0, rwork, row, &iflag, &S);
|
|
149
216
|
}
|
|
150
|
-
|
|
217
|
+
if (iwork[10] > nmax) istate = -1; /* IWORK(11): steps so far */
|
|
218
|
+
/* |h| below the spacing of doubles at t: LSODA only warns (MXHNIL = 10 times). */
|
|
219
|
+
else if (S.nhnil >= 10) istate = -7;
|
|
151
220
|
}
|
|
152
221
|
free(rwork);
|
|
153
222
|
free(iwork);
|
|
@@ -158,6 +227,14 @@ MXN_API int mxn_lsoda(int n, double *y, double *pars, double rtol, double atol,
|
|
|
158
227
|
return 2;
|
|
159
228
|
}
|
|
160
229
|
|
|
230
|
+
static int cvode_jac(sunrealtype t, N_Vector y, N_Vector fy, SUNMatrix J, void *user_data, N_Vector tmp1,
|
|
231
|
+
N_Vector tmp2, N_Vector tmp3) {
|
|
232
|
+
(void)fy; (void)user_data; (void)tmp1; (void)tmp2; (void)tmp3;
|
|
233
|
+
finite_jacobian(g_model, g_jac, (int)N_VGetLength(y), t, N_VGetArrayPointer(y), SM_DATA_D(J),
|
|
234
|
+
g_pars); /* column-major */
|
|
235
|
+
return 0;
|
|
236
|
+
}
|
|
237
|
+
|
|
161
238
|
static int cvode_rhs(sunrealtype t, N_Vector y, N_Vector dydt, void *user_data) {
|
|
162
239
|
(void)user_data;
|
|
163
240
|
int n = (int)N_VGetLength(y);
|
|
@@ -169,15 +246,24 @@ static int cvode_rhs(sunrealtype t, N_Vector y, N_Vector dydt, void *user_data)
|
|
|
169
246
|
return 0;
|
|
170
247
|
}
|
|
171
248
|
|
|
172
|
-
/*
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
*
|
|
177
|
-
*
|
|
249
|
+
/* mxn_cvode: the step size became negligible (see there). */
|
|
250
|
+
#define MXN_CVODE_STALLED (-98)
|
|
251
|
+
|
|
252
|
+
/* Same contract as mxn_lsoda, with SUNDIALS' CVODE: BDF with Newton iteration and jac or a
|
|
253
|
+
* dense difference-quotient Jacobian, or with adams != 0, Adams-Moulton with fixed-point
|
|
254
|
+
* iteration. nmax caps the total number of steps (<= 0: 100000). Returns 1 on success, or
|
|
255
|
+
* CVODE's negative flag: -1 too many steps, -2 too much accuracy requested, -3 repeated
|
|
256
|
+
* error test failures, -4 repeated convergence failures, -8 the right-hand side kept
|
|
257
|
+
* failing (non-finite), -22 bad input (also no output time, non-finite values, unsorted
|
|
258
|
+
* output times), ... (see cvode.h). Returns -98 if the step size became negligible (a
|
|
259
|
+
* singularity, or a right-hand side that stays non-finite). Returns -99 if allocation fails or the
|
|
178
260
|
* result isn't finite. */
|
|
179
|
-
MXN_API int mxn_cvode(int n, double *y, double *pars, double rtol, double atol, int nmax,
|
|
180
|
-
const double *t_out, double *y_out) {
|
|
261
|
+
MXN_API int mxn_cvode(int n, double *y, double *pars, intptr_t jac, double rtol, double atol, int nmax,
|
|
262
|
+
int adams, int n_out, const double *t_out, double *y_out) {
|
|
263
|
+
if (bad_request(n, y, rtol, atol, n_out, t_out)) return CV_ILL_INPUT;
|
|
264
|
+
if (trivial_request(n, y, n_out, t_out, y_out)) return 1;
|
|
265
|
+
if (nmax <= 0) nmax = DEFAULT_NMAX;
|
|
266
|
+
g_jac = (ModelFn)jac;
|
|
181
267
|
SUNContext ctx = NULL;
|
|
182
268
|
if (SUNContext_Create(SUN_COMM_NULL, &ctx) != 0) return -99;
|
|
183
269
|
N_Vector v = N_VMake_Serial(n, y, ctx); /* works on y in place */
|
|
@@ -198,22 +284,42 @@ MXN_API int mxn_cvode(int n, double *y, double *pars, double rtol, double atol,
|
|
|
198
284
|
A = SUNDenseMatrix(n, n, ctx);
|
|
199
285
|
ls = A ? SUNLinSol_Dense(v, A, ctx) : NULL;
|
|
200
286
|
flag = ls ? CVodeSetLinearSolver(mem, ls, A) : -99;
|
|
287
|
+
if (flag == CV_SUCCESS && jac) flag = CVodeSetJacFn(mem, cvode_jac);
|
|
201
288
|
}
|
|
202
|
-
|
|
289
|
+
/* One step at a time (CV_ONE_STEP, never past the stop time), rows from CVODE's
|
|
290
|
+
* interpolant: the same steps as CV_NORMAL, but a step that no longer moves t (a
|
|
291
|
+
* singularity, or a right-hand side that is non-finite beyond some t, which CVODE retries
|
|
292
|
+
* with ever smaller steps) is caught after MXHNIL = 10 such steps instead of using up
|
|
293
|
+
* nmax. */
|
|
294
|
+
const double t_end = t_out[n_out - 1], dir = t_end >= t_out[0] ? 1.0 : -1.0;
|
|
295
|
+
N_Vector row = flag == CV_SUCCESS ? N_VNew_Serial(n, ctx) : NULL;
|
|
296
|
+
if (flag == CV_SUCCESS && !row) flag = -99;
|
|
203
297
|
double t = t_out[0];
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
298
|
+
int k = 0, tiny_steps = 0;
|
|
299
|
+
for (; k < n_out && t_out[k] == t; k++) memcpy(y_out + (size_t)k * (size_t)n, y, (size_t)n * sizeof(double));
|
|
300
|
+
while (k < n_out && flag >= 0) {
|
|
301
|
+
long int steps = 0;
|
|
302
|
+
CVodeGetNumSteps(mem, &steps);
|
|
303
|
+
if (steps >= nmax) {
|
|
304
|
+
flag = CV_TOO_MUCH_WORK;
|
|
305
|
+
break;
|
|
306
|
+
}
|
|
307
|
+
flag = CVode(mem, t_end, v, &t, CV_ONE_STEP);
|
|
308
|
+
if (flag < 0) break;
|
|
309
|
+
for (; k < n_out && (t_out[k] - t) * dir <= 0; k++) {
|
|
310
|
+
double *dst = y_out + (size_t)k * (size_t)n;
|
|
311
|
+
if (t_out[k] == t) {
|
|
312
|
+
memcpy(dst, y, (size_t)n * sizeof(double));
|
|
313
|
+
} else {
|
|
314
|
+
CVodeGetDky(mem, t_out[k], 0, row);
|
|
315
|
+
memcpy(dst, N_VGetArrayPointer(row), (size_t)n * sizeof(double));
|
|
211
316
|
}
|
|
212
|
-
CVodeSetMaxNumSteps(mem, nmax - steps);
|
|
213
|
-
flag = CVode(mem, t_out[k], v, &t, CV_NORMAL);
|
|
214
317
|
}
|
|
215
|
-
|
|
318
|
+
double h = 0;
|
|
319
|
+
CVodeGetLastStep(mem, &h);
|
|
320
|
+
if (fabs(h) <= 4 * DBL_EPSILON * fabs(t) && ++tiny_steps >= 10) flag = MXN_CVODE_STALLED;
|
|
216
321
|
}
|
|
322
|
+
if (row) N_VDestroy(row);
|
|
217
323
|
CVodeFree(&mem);
|
|
218
324
|
SUNNonlinSolFree(nls);
|
|
219
325
|
SUNLinSolFree(ls);
|
|
@@ -330,7 +436,8 @@ MXN_API void mxn_adjoint_set_fns(intptr_t model, intptr_t adjoint, intptr_t deri
|
|
|
330
436
|
/* Arguments as adjoint_vjp (adjoint_wrapper.c, fit_experiments.h's experiment layout), with the
|
|
331
437
|
* cotangent in data_y's layout. Returns 0, -1 out of memory, -3 inconsistent experiments, -4 a
|
|
332
438
|
* differentiated parameter is overridden, -5 derived targets without derived functions, -7
|
|
333
|
-
* functions not set, or a failed
|
|
439
|
+
* functions not set, or for a failed forward or backward solve -1000 plus the solver's negative
|
|
440
|
+
* code. */
|
|
334
441
|
MXN_API int mxn_adjoint_vjp(int n_y, double *y0, int n_pars, double *pars, int n_theta,
|
|
335
442
|
int *theta_idx, int n_targets, int *target_kind, int *target_index,
|
|
336
443
|
int n_ovr, int *ovr_idx, int n_exp, int *exp_kind, int *exp_n_seg,
|
|
@@ -344,6 +451,41 @@ MXN_API int mxn_adjoint_vjp(int n_y, double *y0, int n_pars, double *pars, int n
|
|
|
344
451
|
solver_id, rtol, atol, ss_tol, ss_t_max, out_pred, out_grad, out_lambda0);
|
|
345
452
|
}
|
|
346
453
|
|
|
454
|
+
/* ------------------------------------------------------------------ */
|
|
455
|
+
/* Discrete adjoint gradients */
|
|
456
|
+
/* ------------------------------------------------------------------ */
|
|
457
|
+
/* Exported wrappers around mxl-solvers' discrete_adjoint.c (see its header for the
|
|
458
|
+
* arguments): mxn_da_forward integrates a protocol time course with RADAU5, LSODA or CVODE
|
|
459
|
+
* and records it on a tape (*out_tape, an opaque handle), mxn_da_backward returns the exact
|
|
460
|
+
* gradient of sum(cotangent * predictions) of that solution for any number of cotangents,
|
|
461
|
+
* mxn_da_free releases the tape. Tapes are independent, but a forward solve uses the
|
|
462
|
+
* solvers' global state, so only one runs at a time. */
|
|
463
|
+
MXN_API int mxn_da_forward(intptr_t model, intptr_t jac, intptr_t derived, int method, int n_y,
|
|
464
|
+
const double *y0, int n_pars, int n_seg, const double *seg_t_end,
|
|
465
|
+
const double *seg_pars, int n_points, const double *t_points,
|
|
466
|
+
int n_targets, const int *target_kind, const int *target_index,
|
|
467
|
+
int n_derived, double rtol, double atol, int nmax, int options,
|
|
468
|
+
double newton_scale, double max_tape_bytes, double *out_pred,
|
|
469
|
+
DaTape **out_tape) {
|
|
470
|
+
return da_forward(model, jac, derived, method, n_y, y0, n_pars, n_seg, seg_t_end, seg_pars,
|
|
471
|
+
n_points, t_points, n_targets, target_kind, target_index, n_derived, rtol,
|
|
472
|
+
atol, nmax, options, newton_scale, max_tape_bytes, out_pred, out_tape);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
MXN_API int mxn_da_backward(DaTape *tape, intptr_t adjoint, intptr_t derived_grad, int n_theta,
|
|
476
|
+
const double *cotangent, double *out_grad, double *out_lambda0) {
|
|
477
|
+
return da_backward(tape, adjoint, derived_grad, n_theta, cotangent, out_grad, out_lambda0);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
MXN_API int mxn_da_replay(const DaTape *tape, const double *seg_pars, const double *y0,
|
|
481
|
+
double *out_pred) {
|
|
482
|
+
return da_replay(tape, seg_pars, y0, out_pred);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
MXN_API void mxn_da_free(DaTape *tape) { da_free(tape); }
|
|
486
|
+
MXN_API int mxn_da_tape_steps(const DaTape *tape) { return da_tape_steps(tape); }
|
|
487
|
+
MXN_API double mxn_da_tape_bytes(const DaTape *tape) { return da_tape_bytes(tape); }
|
|
488
|
+
|
|
347
489
|
#ifdef MXN_WASM
|
|
348
490
|
/* Model math stays inside wasm, rather than calling back into Python. */
|
|
349
491
|
#define F1(name) double mxn_##name(double x) { return name(x); }
|
|
@@ -1,35 +1,38 @@
|
|
|
1
|
-
# Vendored from https://github.com/Computational-Biology-Aachen/mxl-solvers at
|
|
1
|
+
# Vendored from https://github.com/Computational-Biology-Aachen/mxl-solvers at e1c882d5e25d9dc43daa4e67dc96b360dbd3d53f.
|
|
2
2
|
# Don't edit these files here: change mxl-solvers, then re-run its scripts/vendor.sh.
|
|
3
3
|
da5d9b896c422aa002b6d8fc3c7003a6e6345454afd700ae65fa3cd944ab12e5 radau5.c
|
|
4
4
|
8d2173c6ae898e77a71c2dc52e027ce2b6185d91170bd5f031b185aff6e1d354 dc_decsol.c
|
|
5
|
-
|
|
5
|
+
1d54d320f3c0ffc3e5e517611ed7eb8452506eb7c6f81be9ba4e2c8d632b29a4 decsol.c
|
|
6
6
|
cf5d31866e63553809a79416b6baa957be45e3c03a5da59254fbbb317c735cfc f2c.h
|
|
7
7
|
5ab404a0cbf7e02c21105b9f8322cb65ef78e10f40937740a9cc90b36796b8b8 libf2c_stubs.c
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
0fa1530b38214432f0247009732d2566ff564ceec5a286cff89008fba9140545 lsoda.c
|
|
9
|
+
56b8b6681164974983cc0924ea6b63de94ccffb96a3d26dff5fa261a17cfe5e8 lsoda.h
|
|
10
10
|
8076d5b1bd27484ce6214cc5f9df7d6c88225182498538c9a878ed14d1743836 blaslapack_declarations.h
|
|
11
11
|
c615cf28e6d41f150f631d0b7fa73f6b2d8b694a88818a38851f54b0efec042c mxs_lapack.c
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
12
|
+
d51fce872eb051f4ea48db39c2b2efa71ded7ab5c858f499c2be3720bb0cb200 dop853.c
|
|
13
|
+
183e63d7d5dc64c7943e4c94e0482c460942520da114a86038e342a3b60639a4 dopri5.c
|
|
14
|
+
dd58e34c2e6e2d845569529bd0b63054dab4c0963d983a262fbeae31561235b2 radau5_wrapper.c
|
|
15
|
+
00c1927551eab7f2ca3b63d95770d431731056a82830207bc8b9cd7778401b74 solver_common.c
|
|
16
|
+
c9444e803d4c641e05142e45e9797ff802ab766b423d14def4f568fd4d435d7e solver_common.h
|
|
17
|
+
c4fd1fcb4a680838a97dd43f4ebb5e2887d245b8456a9ea6c355feff85fa4b06 fit_experiments.c
|
|
18
|
+
55b78ac74a9104624c02693ab8816e6b700eaaa249c4971bd0e07c6053746cf1 fit_experiments.h
|
|
19
|
+
98795e9b96688796dcc488dc073f213f664e70d9a90de12432652b09609e53c1 jacobian_wrapper.c
|
|
20
20
|
8887036b1039e299ebcc8b95a73ea1538df548393fb8462361c80bea605a4fbe minpack/COPYRIGHT.txt
|
|
21
21
|
75538b4e14682e150d10a08b5aa2029d29cfc52095c78492606667bdb0973ada minpack/cminpack.h
|
|
22
22
|
fd9a039e295a835d7122d6335300c7a200dfac46cc27a01b641098f92764ad0b minpack/cminpackP.h
|
|
23
23
|
e06eb647cee0ff1b548718b289f72f1ef02e3e53621cdcd343fd1475796691da minpack/dpmpar.c
|
|
24
24
|
0544f8fae8145d5bc213c108926a2096487ca7b0e29524c0a79d1286b938b76e minpack/enorm.c
|
|
25
|
-
|
|
25
|
+
edb0b22e0880f487d59ea15dafcb94b813ea8fa41f9589f87c6617401b142179 minpack/fdjac2.c
|
|
26
26
|
0d4721d0d87b67ffc88101c6aa66921f180a398eee0e04cb3fe6468f73d9c105 minpack/lmder.c
|
|
27
27
|
d659e132db5cd9c9b61706397145e2005af0fef3b877c250fb31be5b382c795a minpack/lmdif.c
|
|
28
28
|
faac2ffbb50a7e0dad295bdf7f76e62fe625d3e6343ab1aa31b348d6fe5a411e minpack/lmpar.c
|
|
29
29
|
e69d3dde78e2724a74f0c94e328d38222aef85dd5f76dda49a3209d31eb4f58a minpack/minpackP.h
|
|
30
30
|
58079701d6d569e71888e9fb3446f7e691db8f73ae0d8096cdfd92311eb4b0c6 minpack/qrfac.c
|
|
31
31
|
fae6ee028024a1ea9f1fcf05cb0a840ee96c934aecd9e461d0266fda06b5abd1 minpack/qrsolv.c
|
|
32
|
-
|
|
32
|
+
6f2b4be7291366900e3725024fd146a3baeb1c90d16063e410723501d2817a80 adjoint_wrapper.c
|
|
33
|
+
1f1a41d4612766746ca6a48f2dea2d310b9c87c905813d8cc3b847c87dd862a1 discrete_adjoint.c
|
|
34
|
+
7428b0dc5def76c85612661c687f8d11249b78a01e3734077fbc284b72a5dd7d discrete_adjoint.h
|
|
35
|
+
27f173597a62f8c3b51355a9c9ac5a528e64c4a9157dd383a8c83177ee8a8bb0 nordsieck_tape.h
|
|
33
36
|
0e9a5c3ba2637f05909967c3eeeac9c7b9a4a4c6e92801a408afd2aa725024e2 sundials/include/cvode/cvode.h
|
|
34
37
|
adb1e5836989c630b595b14c0fa213426ddbfd3a87d14b581a470d5272b5881b sundials/include/cvode/cvode_diag.h
|
|
35
38
|
84dfab497850c933b8020cc0b2236a2e85bfe5fceffe4b5aefc8a1b518dd68a3 sundials/include/cvode/cvode_ls.h
|
|
@@ -70,10 +73,10 @@ b1835f8bd9eecc879653d78782602123ca62b12fad1861f4c1973cadae2ec492 sundials/inclu
|
|
|
70
73
|
76306ea569f779f18a1a79f82a27e6422db37e9bee0953099e65ec73095d109d sundials/include/sunmatrix/sunmatrix_sparse.h
|
|
71
74
|
18afbd9ff9fd2a3ba9bbaecbb029250ec462571fa52f5562fa4f90255eba68af sundials/include/sunnonlinsol/sunnonlinsol_fixedpoint.h
|
|
72
75
|
ed507a3bb80066f07a267fdfe6e1af050b75dc896202585be69dcf8c8426761f sundials/include/sunnonlinsol/sunnonlinsol_newton.h
|
|
73
|
-
|
|
76
|
+
9ca065b7dd23a221d77d66db727a41a93022e880d2658e1d41547d3063b39418 sundials/src/cvode/cvode.c
|
|
74
77
|
8a803cbdd874d462e4f8f1bebacea3e5aebed7b8cdc02dd94eaddc8637dcdee4 sundials/src/cvode/cvode_diag_impl.h
|
|
75
78
|
33781c0673da6318a75b8577fe6d9f98800c6adcfff497ceeb15c9d8f95ef2d7 sundials/src/cvode/cvode_fused_stubs.c
|
|
76
|
-
|
|
79
|
+
75a9d7dbb251fb09cd477756c1891c37b35b4e8944e9dfa02800ac1ff71bc075 sundials/src/cvode/cvode_impl.h
|
|
77
80
|
e8fbe1785079b69f42c8d8c449c800512ab3a31f3c79f3226c7580283788b789 sundials/src/cvode/cvode_io.c
|
|
78
81
|
31b16188b4257930832aeb5a4984d4fcebcb050c30cadf01603d63eb1ce07240 sundials/src/cvode/cvode_ls.c
|
|
79
82
|
1c151437138df09b3b3f6d36be2453f068bfc5e04c05cc757152d85a0ff9a026 sundials/src/cvode/cvode_ls_impl.h
|
|
@@ -111,3 +114,4 @@ acc17ac1685b3ef154387b62ec8fe8f2b93d634c820d59d04b6d044f5c73bd66 sundials/src/s
|
|
|
111
114
|
221e59f5e910fd7f94e44f0dac77436a11338c285c6346232e4a850a50da0e94 SCIPY-LICENCE.txt
|
|
112
115
|
d83f244e1bef3d00a88d043d37d9cd2493c7aa19c9fbf35a837b310e867897a6 SUNDIALS-LICENCE.txt
|
|
113
116
|
5cb9c6b0ca7c09b26fe2d1f9ef45231086c9ea2cdb32eb5f9dce7012e2364bc2 SUNDIALS-NOTICE.txt
|
|
117
|
+
259c2dbed2a135cdb7be33b58733c7ea2f85142f9a06543508e2bbb0d4c8f63e finite_jacobian.h
|
|
@@ -103,6 +103,12 @@ void adjoint_set_progress_fn(intptr_t table_idx) {
|
|
|
103
103
|
#define ADJOINT_INFO_BUDGET_REACHED 4
|
|
104
104
|
/* Negative: genuine failure (allocation, or a solver IDID<0). */
|
|
105
105
|
#define ADJOINT_INFO_ERROR (-1)
|
|
106
|
+
/* adjoint_vjp returns a failed solve's code (negative IDID or EXP_NOT_CONVERGED)
|
|
107
|
+
* offset by this, so it can't collide with its own setup errors (-1 .. -7). */
|
|
108
|
+
#define ADJOINT_VJP_SOLVE_FAILED (-1000)
|
|
109
|
+
/* adjoint_chunk: the loss or the gradient came out NaN / Inf. The step is
|
|
110
|
+
* not applied, so the parameters and Adam's moments stay usable. */
|
|
111
|
+
#define ADJOINT_INFO_NONFINITE (-1100)
|
|
106
112
|
|
|
107
113
|
/* ------------------------------------------------------------------ */
|
|
108
114
|
/* Forward-trajectory snapshot + cubic Hermite interpolation. */
|
|
@@ -505,8 +511,10 @@ static int backward_experiment(Pass *c, int e, const double *cot, double *grad_o
|
|
|
505
511
|
* Returns 0, -1 on allocation failure, -3 if the experiment layout is
|
|
506
512
|
* inconsistent, -4 if a parameter in theta_idx is also overridden, -5 if
|
|
507
513
|
* derived targets lack set_derived_fn/set_derived_grad_fn, -7 without the
|
|
508
|
-
* forward or adjoint function, or a failed
|
|
509
|
-
* (
|
|
514
|
+
* forward or adjoint function, or, for a failed forward or backward solve,
|
|
515
|
+
* ADJOINT_VJP_SOLVE_FAILED (-1000) plus the solver's negative code
|
|
516
|
+
* (EXP_NOT_CONVERGED for a steady state that never converged), so -1003 is
|
|
517
|
+
* RADAU5's IDID -3.
|
|
510
518
|
*/
|
|
511
519
|
int adjoint_vjp(int n_y, double *y0, int n_pars, double *pars, int n_theta, int *theta_idx,
|
|
512
520
|
int n_targets, int *target_kind, int *target_index, int n_ovr, int *ovr_idx,
|
|
@@ -543,12 +551,13 @@ int adjoint_vjp(int n_y, double *y0, int n_pars, double *pars, int n_theta, int
|
|
|
543
551
|
}
|
|
544
552
|
for (int e = 0; e < P.n_exp && rc == 0; e++) {
|
|
545
553
|
rc = forward_experiment(&c, e, pars, y_work);
|
|
546
|
-
if (rc ==
|
|
547
|
-
if (rc == 0) {
|
|
554
|
+
if (rc == 0)
|
|
548
555
|
rc = backward_experiment(&c, e, P.data_y, out_grad,
|
|
549
556
|
out_lambda0 ? out_lambda0 + (size_t)e * n_y : NULL);
|
|
550
|
-
|
|
551
|
-
|
|
557
|
+
if (rc == EXP_NO_MEMORY)
|
|
558
|
+
rc = -1;
|
|
559
|
+
else if (rc < 0)
|
|
560
|
+
rc += ADJOINT_VJP_SOLVE_FAILED;
|
|
552
561
|
}
|
|
553
562
|
pass_free(&c);
|
|
554
563
|
free(y_work);
|
|
@@ -681,6 +690,9 @@ static int adam_step(AdjointSession *s, double *out_loss, double *out_grad_norm)
|
|
|
681
690
|
for (int k = 0; k < n_theta; k++) grad_norm_sq += grad[k] * grad[k];
|
|
682
691
|
*out_grad_norm = sqrt(grad_norm_sq);
|
|
683
692
|
*out_loss = sqrt(loss);
|
|
693
|
+
/* A NaN gradient would poison adam_m / adam_v (and through them every later step) and the
|
|
694
|
+
* parameters themselves. Refuse the step instead. */
|
|
695
|
+
if (!isfinite(grad_norm_sq) || !isfinite(loss)) return ADJOINT_INFO_NONFINITE;
|
|
684
696
|
|
|
685
697
|
/* Adam update. */
|
|
686
698
|
s->adam_t += 1;
|
|
@@ -824,6 +836,8 @@ int adjoint_init(int n_y, double *y0, int n_pars, double *pars, int n_theta, int
|
|
|
824
836
|
* ADJOINT_INFO_TARGET_REACHED (3) = residual norm crossed
|
|
825
837
|
* target_residual_norm;
|
|
826
838
|
* ADJOINT_INFO_ERROR (-1) = allocation failure;
|
|
839
|
+
* ADJOINT_INFO_NONFINITE (-1100) = the loss or gradient was NaN/Inf
|
|
840
|
+
* (the step was not applied, the parameters are those before it);
|
|
827
841
|
* any other negative value = a solver IDID failure from adam_step.
|
|
828
842
|
*/
|
|
829
843
|
int adjoint_chunk(int maxIterations) {
|
|
@@ -54,6 +54,10 @@
|
|
|
54
54
|
|
|
55
55
|
/* Function Body */
|
|
56
56
|
*ier = 0;
|
|
57
|
+
/* n = 0 (an empty system): nothing to factor, and ip[n] would be ip(0). */
|
|
58
|
+
if (*n <= 0) {
|
|
59
|
+
return 0;
|
|
60
|
+
}
|
|
57
61
|
ip[*n] = 1;
|
|
58
62
|
if (*n == 1) {
|
|
59
63
|
goto L70;
|
|
@@ -239,6 +243,10 @@ L50:
|
|
|
239
243
|
|
|
240
244
|
/* Function Body */
|
|
241
245
|
*ier = 0;
|
|
246
|
+
/* n = 0 (an empty system): nothing to factor, and ip[n] would be ip(0). */
|
|
247
|
+
if (*n <= 0) {
|
|
248
|
+
return 0;
|
|
249
|
+
}
|
|
242
250
|
ip[*n] = 1;
|
|
243
251
|
if (*n == 1) {
|
|
244
252
|
goto L70;
|
|
@@ -435,6 +443,10 @@ L50:
|
|
|
435
443
|
|
|
436
444
|
/* Function Body */
|
|
437
445
|
*ier = 0;
|
|
446
|
+
/* n = 0 (an empty system): nothing to factor, and ip[n] would be ip(0). */
|
|
447
|
+
if (*n <= 0) {
|
|
448
|
+
return 0;
|
|
449
|
+
}
|
|
438
450
|
ip[*n] = 1;
|
|
439
451
|
if (*n == 1) {
|
|
440
452
|
goto L70;
|
|
@@ -691,6 +703,10 @@ L50:
|
|
|
691
703
|
|
|
692
704
|
/* Function Body */
|
|
693
705
|
*ier = 0;
|
|
706
|
+
/* n = 0 (an empty system): nothing to factor, and ip[n] would be ip(0). */
|
|
707
|
+
if (*n <= 0) {
|
|
708
|
+
return 0;
|
|
709
|
+
}
|
|
694
710
|
ip[*n] = 1;
|
|
695
711
|
if (*lb == 0) {
|
|
696
712
|
goto L70;
|
|
@@ -957,6 +973,10 @@ L50:
|
|
|
957
973
|
|
|
958
974
|
/* Function Body */
|
|
959
975
|
*ier = 0;
|
|
976
|
+
/* n = 0 (an empty system): nothing to factor, and ip[n] would be ip(0). */
|
|
977
|
+
if (*n <= 0) {
|
|
978
|
+
return 0;
|
|
979
|
+
}
|
|
960
980
|
ip[*n] = 1;
|
|
961
981
|
md = *ml + *mu + 1;
|
|
962
982
|
md1 = md + 1;
|
|
@@ -1208,6 +1228,10 @@ L50:
|
|
|
1208
1228
|
|
|
1209
1229
|
/* Function Body */
|
|
1210
1230
|
*ier = 0;
|
|
1231
|
+
/* n = 0 (an empty system): nothing to factor, and ip[n] would be ip(0). */
|
|
1232
|
+
if (*n <= 0) {
|
|
1233
|
+
return 0;
|
|
1234
|
+
}
|
|
1211
1235
|
ip[*n] = 1;
|
|
1212
1236
|
md = *ml + *mu + 1;
|
|
1213
1237
|
md1 = md + 1;
|