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.
Files changed (138) hide show
  1. {mxlpy_native-0.6.0/src/mxlpy_native.egg-info → mxlpy_native-0.8.0}/PKG-INFO +2 -2
  2. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/README.md +1 -1
  3. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/mxn_solvers.c +192 -50
  4. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/VENDORED +20 -16
  5. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/adjoint_wrapper.c +20 -6
  6. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/decsol.c +24 -0
  7. mxlpy_native-0.8.0/c/vendor/discrete_adjoint.c +1529 -0
  8. mxlpy_native-0.8.0/c/vendor/discrete_adjoint.h +109 -0
  9. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/dop853.c +14 -0
  10. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/dopri5.c +14 -0
  11. mxlpy_native-0.8.0/c/vendor/finite_jacobian.h +93 -0
  12. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/fit_experiments.c +38 -6
  13. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/fit_experiments.h +6 -1
  14. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/jacobian_wrapper.c +29 -18
  15. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/lsoda.c +35 -10
  16. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/lsoda.h +12 -0
  17. mxlpy_native-0.8.0/c/vendor/nordsieck_tape.h +46 -0
  18. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/radau5_wrapper.c +200 -147
  19. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/solver_common.c +45 -11
  20. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/solver_common.h +17 -1
  21. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode.c +34 -0
  22. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode_impl.h +4 -0
  23. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/pyproject.toml +1 -1
  24. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/setup.py +2 -1
  25. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/src/mxlpy_native/__init__.py +1 -1
  26. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0/src/mxlpy_native.egg-info}/PKG-INFO +2 -2
  27. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/src/mxlpy_native.egg-info/SOURCES.txt +6 -0
  28. mxlpy_native-0.8.0/tests/test_contracts.py +287 -0
  29. mxlpy_native-0.8.0/tests/test_discrete_adjoint.py +141 -0
  30. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/tests/test_solvers.py +13 -2
  31. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/LICENSE +0 -0
  32. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/MANIFEST.in +0 -0
  33. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/HAIRER-LICENCE.txt +0 -0
  34. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/SCIPY-LICENCE.txt +0 -0
  35. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/SUNDIALS-LICENCE.txt +0 -0
  36. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/SUNDIALS-NOTICE.txt +0 -0
  37. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/blaslapack_declarations.h +0 -0
  38. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/dc_decsol.c +0 -0
  39. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/f2c.h +0 -0
  40. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/libf2c_stubs.c +0 -0
  41. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/minpack/COPYRIGHT.txt +0 -0
  42. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/minpack/cminpack.h +0 -0
  43. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/minpack/cminpackP.h +0 -0
  44. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/minpack/dpmpar.c +0 -0
  45. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/minpack/enorm.c +0 -0
  46. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/minpack/fdjac2.c +1 -1
  47. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/minpack/lmder.c +0 -0
  48. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/minpack/lmdif.c +0 -0
  49. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/minpack/lmpar.c +0 -0
  50. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/minpack/minpackP.h +0 -0
  51. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/minpack/qrfac.c +0 -0
  52. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/minpack/qrsolv.c +0 -0
  53. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/mxs_lapack.c +0 -0
  54. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/radau5.c +0 -0
  55. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/cvode/cvode.h +0 -0
  56. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/cvode/cvode_diag.h +0 -0
  57. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/cvode/cvode_ls.h +0 -0
  58. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/cvode/cvode_proj.h +0 -0
  59. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/nvector/nvector_serial.h +0 -0
  60. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/priv/sundials_context_impl.h +0 -0
  61. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/priv/sundials_errors_impl.h +0 -0
  62. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/priv/sundials_logger_macros.h +0 -0
  63. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_adaptcontroller.h +0 -0
  64. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_adjointcheckpointscheme.h +0 -0
  65. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_adjointstepper.h +0 -0
  66. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_band.h +0 -0
  67. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_config.h +0 -0
  68. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_context.h +0 -0
  69. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_core.h +0 -0
  70. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_dense.h +0 -0
  71. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_direct.h +0 -0
  72. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_domeigestimator.h +0 -0
  73. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_errors.h +0 -0
  74. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_export.h +0 -0
  75. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_futils.h +0 -0
  76. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_iterative.h +0 -0
  77. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_linearsolver.h +0 -0
  78. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_logger.h +0 -0
  79. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_math.h +0 -0
  80. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_matrix.h +0 -0
  81. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_memory.h +0 -0
  82. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_nonlinearsolver.h +0 -0
  83. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_nvector.h +0 -0
  84. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_nvector_senswrapper.h +0 -0
  85. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_profiler.h +0 -0
  86. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_stepper.h +0 -0
  87. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_types.h +0 -0
  88. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_version.h +0 -0
  89. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sunlinsol/sunlinsol_dense.h +0 -0
  90. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sunmatrix/sunmatrix_band.h +0 -0
  91. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sunmatrix/sunmatrix_dense.h +0 -0
  92. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sunmatrix/sunmatrix_sparse.h +0 -0
  93. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sunnonlinsol/sunnonlinsol_fixedpoint.h +0 -0
  94. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sunnonlinsol/sunnonlinsol_newton.h +0 -0
  95. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode_diag_impl.h +0 -0
  96. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode_fused_stubs.c +0 -0
  97. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode_io.c +0 -0
  98. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode_ls.c +0 -0
  99. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode_ls_impl.h +0 -0
  100. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode_nls.c +0 -0
  101. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode_proj.c +0 -0
  102. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode_proj_impl.h +0 -0
  103. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/nvector/serial/nvector_serial.c +0 -0
  104. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/stl/sunstl_vector.h +0 -0
  105. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_adiak_metadata.h +0 -0
  106. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_band.c +0 -0
  107. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_context.c +0 -0
  108. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_debug.h +0 -0
  109. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_dense.c +0 -0
  110. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_errors.c +0 -0
  111. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_futils.c +0 -0
  112. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_hashmap_impl.h +0 -0
  113. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_linearsolver.c +0 -0
  114. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_logger.c +0 -0
  115. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_logger_impl.h +0 -0
  116. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_macros.h +0 -0
  117. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_math.c +0 -0
  118. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_matrix.c +0 -0
  119. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_memory.c +0 -0
  120. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_nonlinearsolver.c +0 -0
  121. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_nvector.c +0 -0
  122. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_nvector_senswrapper.c +0 -0
  123. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_utils.h +0 -0
  124. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_version.c +0 -0
  125. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sunlinsol/dense/sunlinsol_dense.c +0 -0
  126. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sunmatrix/band/sunmatrix_band.c +0 -0
  127. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sunmatrix/dense/sunmatrix_dense.c +0 -0
  128. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sunnonlinsol/fixedpoint/sunnonlinsol_fixedpoint.c +0 -0
  129. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sunnonlinsol/newton/sunnonlinsol_newton.c +0 -0
  130. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/setup.cfg +0 -0
  131. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/src/mxlpy_native/__main__.py +0 -0
  132. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/src/mxlpy_native.egg-info/dependency_links.txt +0 -0
  133. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/src/mxlpy_native.egg-info/requires.txt +0 -0
  134. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/src/mxlpy_native.egg-info/top_level.txt +0 -0
  135. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/tests/test_adjoint.py +0 -0
  136. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/tests/test_build_errors.py +0 -0
  137. {mxlpy_native-0.6.0 → mxlpy_native-0.8.0}/tests/test_fit.py +0 -0
  138. {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.6.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 4
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 no_jac(void) { return 0; }
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)nr; (void)xold; (void)rpar; (void)ipar;
97
+ (void)xold; (void)rpar; (void)ipar;
59
98
  *irtrn = 1;
60
- while (g_next < g_n_out && g_t_out[g_next] <= *x) {
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
- if (s == *x) {
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
- * y holds the initial state on entry and the final state on return. Returns RADAU5's
75
- * IDID: positive on success, -1 bad input, -2 too many steps, -3 step too small,
76
- * -4 singular matrix. Returns -5 if allocation fails or the result isn't finite. */
77
- MXN_API int mxn_radau(int n, double *y, double *pars, double rtol, double atol, int nmax, int reuse_jacobian,
78
- int n_out, const double *t_out, double *y_out) {
79
- integer N = n, itol = 0, ijac = 0, mljac = n, mujac = n, imas = 0, mlmas = n, mumas = n, iout = 1, idid = 0, ipar = 0;
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
- radau5_(&N, (U_fp)fcn_dispatch, &x, y, &xend, &h, &rt, &at, &itol, (U_fp)no_jac, &ijac, &mljac, &mujac,
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
- /* Same contract as mxn_radau, with LSODA (SciPy's C translation) and its internal
120
- * finite-difference Jacobian. nmax caps the total number of steps. Returns 2 on
121
- * success, or LSODA's negative ISTATE: -1 too many steps, -2 too much accuracy
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 -9 if allocation fails or the
124
- * result isn't finite. */
125
- MXN_API int mxn_lsoda(int n, double *y, double *pars, double rtol, double atol, int nmax, int n_out,
126
- const double *t_out, double *y_out) {
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
- int itask = 1, istate = 1, iopt = 1;
138
- iwork[5] = nmax; /* IWORK(6): steps allowed per output point */
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
- for (int k = 1; k < n_out && istate > 0; k++) {
144
- double tout = t_out[k];
145
- if (tout != t_out[k - 1]) {
146
- lsoda(lsoda_fcn, n, y, &t, &tout, 1, &rtol, &atol, &itask, &istate, &iopt, rwork, lrw, iwork, liw, NULL,
147
- 2, &S);
148
- if (istate > 0 && iwork[10] > nmax) istate = -1; /* IWORK(11): steps so far */
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
- memcpy(y_out + (size_t)k * (size_t)n, y, (size_t)n * sizeof(double));
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
- /* Same contract as mxn_lsoda, with SUNDIALS' CVODE: BDF with Newton iteration and a dense
173
- * difference-quotient Jacobian, or with adams != 0, Adams-Moulton with fixed-point
174
- * iteration. nmax caps the total number of steps. Returns 1 on success, or CVODE's
175
- * negative flag: -1 too many steps, -2 too much accuracy requested, -3 repeated error
176
- * test failures, -4 repeated convergence failures, -8 the right-hand side kept failing
177
- * (non-finite), -22 bad input, ... (see cvode.h). Returns -99 if allocation fails or the
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, int adams, int n_out,
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
- memcpy(y_out, y, (size_t)n * sizeof(double));
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
- for (int k = 1; k < n_out && flag >= 0; k++) {
205
- if (t_out[k] != t_out[k - 1]) {
206
- long int steps = 0;
207
- CVodeGetNumSteps(mem, &steps);
208
- if (steps >= nmax) {
209
- flag = CV_TOO_MUCH_WORK;
210
- break;
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
- if (flag >= 0) memcpy(y_out + (size_t)k * (size_t)n, y, (size_t)n * sizeof(double));
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 experiment's negative solver code. */
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 183c01543986ea804afaaeae797d01207c1c7fa7.
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
- 9dc2c0ede63b1511813fbde4300c23a6c1e0536e4d316c6fd5492ae0f20d8195 decsol.c
5
+ 1d54d320f3c0ffc3e5e517611ed7eb8452506eb7c6f81be9ba4e2c8d632b29a4 decsol.c
6
6
  cf5d31866e63553809a79416b6baa957be45e3c03a5da59254fbbb317c735cfc f2c.h
7
7
  5ab404a0cbf7e02c21105b9f8322cb65ef78e10f40937740a9cc90b36796b8b8 libf2c_stubs.c
8
- 2f05643511057c06ceed0d8a1d7181857f93a759a60139d7506a4b6123654ac7 lsoda.c
9
- 751d5aa397f303b491d78f689dbf5b779f32b2d158b58c2b861d58143702e1f9 lsoda.h
8
+ 0fa1530b38214432f0247009732d2566ff564ceec5a286cff89008fba9140545 lsoda.c
9
+ 56b8b6681164974983cc0924ea6b63de94ccffb96a3d26dff5fa261a17cfe5e8 lsoda.h
10
10
  8076d5b1bd27484ce6214cc5f9df7d6c88225182498538c9a878ed14d1743836 blaslapack_declarations.h
11
11
  c615cf28e6d41f150f631d0b7fa73f6b2d8b694a88818a38851f54b0efec042c mxs_lapack.c
12
- 7d8f8f994e1c477fd023e0197fb7efde6287900010e96de3a3f188bc961cfb51 dop853.c
13
- 41652bfb7aa54277f9d2dff987a0c7cdbc63e68dac94a644daccf5c8ecd491b5 dopri5.c
14
- 78d34d672f5310c707bfed79d703c7fd3a8798872af32985f3924d3d43adf06f radau5_wrapper.c
15
- 1bbc4b4460dd58c8a7840b60df51dfe81c0cce8ac768eb15b3fb50d82146cb6b solver_common.c
16
- f0a0dfe5b6796b71ff096f663ecfb8869770080ed7212ce1a97683e99c7918a1 solver_common.h
17
- 55c070b491fd31620bf0784a3c38535c293b210564acdd50bc062ff0945082b5 fit_experiments.c
18
- 9629a05dbd209b582d69dbf94c45392194d0bcb0fe3f7bad69e7e9e945f916af fit_experiments.h
19
- ee273d15c2594e34c0abc8be341804c56e60cdb2377a563160e52e04168be414 jacobian_wrapper.c
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
- 4dbe4f56e14fef106a28b7d967ad608b8c7b0b7063555a12b0471fe3dfd32fca minpack/fdjac2.c
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
- 3d71958c917a4f7eb85fdaeade0b274940a5e8b54b6c892dc877457c15b243d4 adjoint_wrapper.c
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
- 7590f4ec73215229be22479b3af8856395232191985eff364e30709aebe2322a sundials/src/cvode/cvode.c
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
- d22ef38c930b247dfaefc1d921ad3444f629a521998f0697c0227b29841da87d sundials/src/cvode/cvode_impl.h
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 experiment's negative solver code
509
- * (EXP_NOT_CONVERGED for a steady state that never converged).
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 == EXP_NO_MEMORY) rc = -1;
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
- if (rc == EXP_NO_MEMORY) rc = -1;
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;