mxlpy-native 0.7.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.7.0/src/mxlpy_native.egg-info → mxlpy_native-0.8.0}/PKG-INFO +1 -1
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/mxn_solvers.c +114 -37
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/VENDORED +15 -15
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/adjoint_wrapper.c +8 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/decsol.c +24 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/dop853.c +14 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/dopri5.c +14 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/finite_jacobian.h +40 -8
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/fit_experiments.c +38 -6
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/fit_experiments.h +6 -1
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/jacobian_wrapper.c +29 -18
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/lsoda.c +16 -3
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/lsoda.h +4 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/radau5_wrapper.c +200 -147
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/solver_common.c +45 -11
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/solver_common.h +17 -1
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/pyproject.toml +1 -1
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0/src/mxlpy_native.egg-info}/PKG-INFO +1 -1
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/src/mxlpy_native.egg-info/SOURCES.txt +1 -0
- mxlpy_native-0.8.0/tests/test_contracts.py +287 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/LICENSE +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/MANIFEST.in +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/README.md +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/HAIRER-LICENCE.txt +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/SCIPY-LICENCE.txt +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/SUNDIALS-LICENCE.txt +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/SUNDIALS-NOTICE.txt +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/blaslapack_declarations.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/dc_decsol.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/discrete_adjoint.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/discrete_adjoint.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/f2c.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/libf2c_stubs.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/minpack/COPYRIGHT.txt +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/minpack/cminpack.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/minpack/cminpackP.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/minpack/dpmpar.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/minpack/enorm.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/minpack/fdjac2.c +1 -1
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/minpack/lmder.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/minpack/lmdif.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/minpack/lmpar.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/minpack/minpackP.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/minpack/qrfac.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/minpack/qrsolv.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/mxs_lapack.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/nordsieck_tape.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/radau5.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/cvode/cvode.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/cvode/cvode_diag.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/cvode/cvode_ls.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/cvode/cvode_proj.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/nvector/nvector_serial.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/priv/sundials_context_impl.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/priv/sundials_errors_impl.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/priv/sundials_logger_macros.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_adaptcontroller.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_adjointcheckpointscheme.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_adjointstepper.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_band.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_config.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_context.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_core.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_dense.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_direct.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_domeigestimator.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_errors.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_export.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_futils.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_iterative.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_linearsolver.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_logger.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_math.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_matrix.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_memory.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_nonlinearsolver.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_nvector.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_nvector_senswrapper.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_profiler.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_stepper.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_types.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sundials/sundials_version.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sunlinsol/sunlinsol_dense.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sunmatrix/sunmatrix_band.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sunmatrix/sunmatrix_dense.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sunmatrix/sunmatrix_sparse.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sunnonlinsol/sunnonlinsol_fixedpoint.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/include/sunnonlinsol/sunnonlinsol_newton.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode_diag_impl.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode_fused_stubs.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode_impl.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode_io.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode_ls.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode_ls_impl.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode_nls.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode_proj.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/cvode/cvode_proj_impl.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/nvector/serial/nvector_serial.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/stl/sunstl_vector.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_adiak_metadata.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_band.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_context.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_debug.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_dense.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_errors.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_futils.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_hashmap_impl.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_linearsolver.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_logger.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_logger_impl.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_macros.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_math.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_matrix.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_memory.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_nonlinearsolver.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_nvector.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_nvector_senswrapper.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_utils.h +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sundials/sundials_version.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sunlinsol/dense/sunlinsol_dense.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sunmatrix/band/sunmatrix_band.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sunmatrix/dense/sunmatrix_dense.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sunnonlinsol/fixedpoint/sunnonlinsol_fixedpoint.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/c/vendor/sundials/src/sunnonlinsol/newton/sunnonlinsol_newton.c +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/setup.cfg +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/setup.py +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/src/mxlpy_native/__init__.py +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/src/mxlpy_native/__main__.py +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/src/mxlpy_native.egg-info/dependency_links.txt +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/src/mxlpy_native.egg-info/requires.txt +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/src/mxlpy_native.egg-info/top_level.txt +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/tests/test_adjoint.py +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/tests/test_build_errors.py +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/tests/test_discrete_adjoint.py +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/tests/test_fit.py +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/tests/test_solvers.py +0 -0
- {mxlpy_native-0.7.0 → mxlpy_native-0.8.0}/tests/test_vendor.py +0 -0
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
#include <sunlinsol/sunlinsol_dense.h>
|
|
13
13
|
#include <sunmatrix/sunmatrix_dense.h>
|
|
14
14
|
#include <sunnonlinsol/sunnonlinsol_fixedpoint.h>
|
|
15
|
+
#include <float.h>
|
|
15
16
|
#include <math.h>
|
|
16
17
|
#include <stdint.h>
|
|
17
18
|
#include <stdlib.h>
|
|
@@ -46,6 +47,31 @@ static const double *g_t_out;
|
|
|
46
47
|
static double *g_y_out;
|
|
47
48
|
static int g_n_out, g_next;
|
|
48
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
|
+
|
|
49
75
|
/* A function pointer natively, a table index in wasm. */
|
|
50
76
|
MXN_API void mxn_set_model(intptr_t model) { g_model = (ModelFn)model; }
|
|
51
77
|
|
|
@@ -64,14 +90,17 @@ static int radau_jac(integer *n, doublereal *x, doublereal *y, doublereal *dfy,
|
|
|
64
90
|
}
|
|
65
91
|
static int no_mas(void) { return 0; }
|
|
66
92
|
|
|
93
|
+
static double g_dir; /* direction of integration, +1 or -1 */
|
|
94
|
+
|
|
67
95
|
static int solout(integer *nr, doublereal *xold, doublereal *x, doublereal *y, doublereal *cont, integer *lrc,
|
|
68
96
|
integer *n, doublereal *rpar, integer *ipar, integer *irtrn) {
|
|
69
|
-
(void)
|
|
97
|
+
(void)xold; (void)rpar; (void)ipar;
|
|
70
98
|
*irtrn = 1;
|
|
71
|
-
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) {
|
|
72
100
|
double *row = g_y_out + (size_t)g_next * (size_t)*n;
|
|
73
101
|
doublereal s = g_t_out[g_next];
|
|
74
|
-
|
|
102
|
+
/* CONTR5 only exists from the second call on (nr == 1 comes before any step). */
|
|
103
|
+
if (s == *x || *nr < 2) {
|
|
75
104
|
memcpy(row, y, (size_t)*n * sizeof(double));
|
|
76
105
|
} else {
|
|
77
106
|
for (integer i = 1; i <= *n; i++) row[i - 1] = contr5_(&i, &s, cont, lrc);
|
|
@@ -81,13 +110,19 @@ static int solout(integer *nr, doublereal *xold, doublereal *x, doublereal *y, d
|
|
|
81
110
|
return 0;
|
|
82
111
|
}
|
|
83
112
|
|
|
84
|
-
/* Integrates from t_out[0] to t_out[n_out - 1], writing rows of n values into y_out.
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
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. */
|
|
89
121
|
MXN_API int mxn_radau(int n, double *y, double *pars, intptr_t jac, double rtol, double atol, int nmax,
|
|
90
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;
|
|
91
126
|
g_jac = (ModelFn)jac;
|
|
92
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;
|
|
93
128
|
integer lwork = 4 * N * N + 12 * N + 20, liwork = 3 * N + 20;
|
|
@@ -114,6 +149,7 @@ MXN_API int mxn_radau(int n, double *y, double *pars, intptr_t jac, double rtol,
|
|
|
114
149
|
g_y_out = y_out;
|
|
115
150
|
g_n_out = n_out;
|
|
116
151
|
g_next = 0;
|
|
152
|
+
g_dir = xend >= x ? 1.0 : -1.0;
|
|
117
153
|
radau5_(&N, (U_fp)fcn_dispatch, &x, y, &xend, &h, &rt, &at, &itol, (U_fp)radau_jac, &ijac, &mljac, &mujac,
|
|
118
154
|
(U_fp)no_mas, &imas, &mlmas, &mumas, (U_fp)solout, &iout, work, &lwork, iwork, &liwork, pars, &ipar, &idid);
|
|
119
155
|
free(work);
|
|
@@ -135,13 +171,17 @@ static void lsoda_jac(int *n, double *t, double *y, int *ml, int *mu, double *pd
|
|
|
135
171
|
}
|
|
136
172
|
|
|
137
173
|
/* Same contract as mxn_radau, with LSODA (SciPy's C translation), using jac or its
|
|
138
|
-
* internal finite-difference Jacobian. nmax caps the total number of steps
|
|
139
|
-
* success, or LSODA's negative ISTATE: -1 too many steps, -2 too much accuracy
|
|
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
|
|
140
176
|
* requested, -3 bad input, -4 repeated error test failures, -5 repeated convergence
|
|
141
|
-
* failures, -6 an error weight became zero. Returns -
|
|
142
|
-
*
|
|
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. */
|
|
143
180
|
MXN_API int mxn_lsoda(int n, double *y, double *pars, intptr_t jac, double rtol, double atol, int nmax,
|
|
144
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;
|
|
145
185
|
g_jac = (ModelFn)jac;
|
|
146
186
|
/* Work array sizes as in SciPy's odepack: Adams up to order 12, BDF up to 5. */
|
|
147
187
|
const int lrn = 20 + n * 13 + 3 * n, lrs = 20 + n * 6 + 3 * n + n * n + 2;
|
|
@@ -153,20 +193,30 @@ MXN_API int mxn_lsoda(int n, double *y, double *pars, intptr_t jac, double rtol,
|
|
|
153
193
|
free(iwork);
|
|
154
194
|
return -9;
|
|
155
195
|
}
|
|
156
|
-
|
|
157
|
-
|
|
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 */
|
|
158
202
|
lsoda_common_struct_t S = {0};
|
|
159
203
|
double t = t_out[0];
|
|
160
204
|
g_pars = pars;
|
|
161
|
-
memcpy(y_out, y, (size_t)n * sizeof(double));
|
|
162
|
-
|
|
163
|
-
double tout =
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
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);
|
|
168
216
|
}
|
|
169
|
-
|
|
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;
|
|
170
220
|
}
|
|
171
221
|
free(rwork);
|
|
172
222
|
free(iwork);
|
|
@@ -196,15 +246,23 @@ static int cvode_rhs(sunrealtype t, N_Vector y, N_Vector dydt, void *user_data)
|
|
|
196
246
|
return 0;
|
|
197
247
|
}
|
|
198
248
|
|
|
249
|
+
/* mxn_cvode: the step size became negligible (see there). */
|
|
250
|
+
#define MXN_CVODE_STALLED (-98)
|
|
251
|
+
|
|
199
252
|
/* Same contract as mxn_lsoda, with SUNDIALS' CVODE: BDF with Newton iteration and jac or a
|
|
200
253
|
* dense difference-quotient Jacobian, or with adams != 0, Adams-Moulton with fixed-point
|
|
201
|
-
* iteration. nmax caps the total number of steps. Returns 1 on success, or
|
|
202
|
-
* negative flag: -1 too many steps, -2 too much accuracy requested, -3 repeated
|
|
203
|
-
* test failures, -4 repeated convergence failures, -8 the right-hand side kept
|
|
204
|
-
* (non-finite), -22 bad input
|
|
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
|
|
205
260
|
* result isn't finite. */
|
|
206
261
|
MXN_API int mxn_cvode(int n, double *y, double *pars, intptr_t jac, double rtol, double atol, int nmax,
|
|
207
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;
|
|
208
266
|
g_jac = (ModelFn)jac;
|
|
209
267
|
SUNContext ctx = NULL;
|
|
210
268
|
if (SUNContext_Create(SUN_COMM_NULL, &ctx) != 0) return -99;
|
|
@@ -228,21 +286,40 @@ MXN_API int mxn_cvode(int n, double *y, double *pars, intptr_t jac, double rtol,
|
|
|
228
286
|
flag = ls ? CVodeSetLinearSolver(mem, ls, A) : -99;
|
|
229
287
|
if (flag == CV_SUCCESS && jac) flag = CVodeSetJacFn(mem, cvode_jac);
|
|
230
288
|
}
|
|
231
|
-
|
|
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;
|
|
232
297
|
double t = t_out[0];
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
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));
|
|
240
316
|
}
|
|
241
|
-
CVodeSetMaxNumSteps(mem, nmax - steps);
|
|
242
|
-
flag = CVode(mem, t_out[k], v, &t, CV_NORMAL);
|
|
243
317
|
}
|
|
244
|
-
|
|
318
|
+
double h = 0;
|
|
319
|
+
CVodeGetLastStep(mem, &h);
|
|
320
|
+
if (fabs(h) <= 4 * DBL_EPSILON * fabs(t) && ++tiny_steps >= 10) flag = MXN_CVODE_STALLED;
|
|
245
321
|
}
|
|
322
|
+
if (row) N_VDestroy(row);
|
|
246
323
|
CVodeFree(&mem);
|
|
247
324
|
SUNNonlinSolFree(nls);
|
|
248
325
|
SUNLinSolFree(ls);
|
|
@@ -1,35 +1,35 @@
|
|
|
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
33
|
1f1a41d4612766746ca6a48f2dea2d310b9c87c905813d8cc3b847c87dd862a1 discrete_adjoint.c
|
|
34
34
|
7428b0dc5def76c85612661c687f8d11249b78a01e3734077fbc284b72a5dd7d discrete_adjoint.h
|
|
35
35
|
27f173597a62f8c3b51355a9c9ac5a528e64c4a9157dd383a8c83177ee8a8bb0 nordsieck_tape.h
|
|
@@ -114,4 +114,4 @@ acc17ac1685b3ef154387b62ec8fe8f2b93d634c820d59d04b6d044f5c73bd66 sundials/src/s
|
|
|
114
114
|
221e59f5e910fd7f94e44f0dac77436a11338c285c6346232e4a850a50da0e94 SCIPY-LICENCE.txt
|
|
115
115
|
d83f244e1bef3d00a88d043d37d9cd2493c7aa19c9fbf35a837b310e867897a6 SUNDIALS-LICENCE.txt
|
|
116
116
|
5cb9c6b0ca7c09b26fe2d1f9ef45231086c9ea2cdb32eb5f9dce7012e2364bc2 SUNDIALS-NOTICE.txt
|
|
117
|
-
|
|
117
|
+
259c2dbed2a135cdb7be33b58733c7ea2f85142f9a06543508e2bbb0d4c8f63e finite_jacobian.h
|
|
@@ -106,6 +106,9 @@ void adjoint_set_progress_fn(intptr_t table_idx) {
|
|
|
106
106
|
/* adjoint_vjp returns a failed solve's code (negative IDID or EXP_NOT_CONVERGED)
|
|
107
107
|
* offset by this, so it can't collide with its own setup errors (-1 .. -7). */
|
|
108
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)
|
|
109
112
|
|
|
110
113
|
/* ------------------------------------------------------------------ */
|
|
111
114
|
/* Forward-trajectory snapshot + cubic Hermite interpolation. */
|
|
@@ -687,6 +690,9 @@ static int adam_step(AdjointSession *s, double *out_loss, double *out_grad_norm)
|
|
|
687
690
|
for (int k = 0; k < n_theta; k++) grad_norm_sq += grad[k] * grad[k];
|
|
688
691
|
*out_grad_norm = sqrt(grad_norm_sq);
|
|
689
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;
|
|
690
696
|
|
|
691
697
|
/* Adam update. */
|
|
692
698
|
s->adam_t += 1;
|
|
@@ -830,6 +836,8 @@ int adjoint_init(int n_y, double *y0, int n_pars, double *pars, int n_theta, int
|
|
|
830
836
|
* ADJOINT_INFO_TARGET_REACHED (3) = residual norm crossed
|
|
831
837
|
* target_residual_norm;
|
|
832
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);
|
|
833
841
|
* any other negative value = a solver IDID failure from adam_step.
|
|
834
842
|
*/
|
|
835
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;
|
|
@@ -827,6 +827,20 @@ L1:
|
|
|
827
827
|
deno = 1.;
|
|
828
828
|
}
|
|
829
829
|
err = abs(*h__) * err * sqrt(1. / (*n * deno));
|
|
830
|
+
/* --- A NaN error estimate fails "err <= 1" but makes the new step size NaN too, so the
|
|
831
|
+
* solver spins until NMAX. A non-finite step must be rejected like any other large error
|
|
832
|
+
* (same patch as radau5.c), so the step size shrinks until it is too small (IDID = -3). */
|
|
833
|
+
i__1 = *n;
|
|
834
|
+
for (i__ = 1; i__ <= i__1; ++i__) {
|
|
835
|
+
d__1 = k5[i__];
|
|
836
|
+
if (d__1 - d__1 != 0.) {
|
|
837
|
+
err = 1e6;
|
|
838
|
+
break;
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
if (err != err) {
|
|
842
|
+
err = 1e6;
|
|
843
|
+
}
|
|
830
844
|
/* --- COMPUTATION OF HNEW */
|
|
831
845
|
fac11 = pow_dd(&err, &expo1);
|
|
832
846
|
/* --- LUND-STABILIZATION */
|
|
@@ -740,6 +740,20 @@ L1:
|
|
|
740
740
|
}
|
|
741
741
|
}
|
|
742
742
|
err = sqrt(err / *n);
|
|
743
|
+
/* --- A NaN error estimate fails "err <= 1" but makes the new step size NaN too, so the
|
|
744
|
+
* solver spins until NMAX. A non-finite step must be rejected like any other large error
|
|
745
|
+
* (same patch as radau5.c), so the step size shrinks until it is too small (IDID = -3). */
|
|
746
|
+
i__1 = *n;
|
|
747
|
+
for (i__ = 1; i__ <= i__1; ++i__) {
|
|
748
|
+
d__1 = y1[i__];
|
|
749
|
+
if (d__1 - d__1 != 0.) {
|
|
750
|
+
err = 1e6;
|
|
751
|
+
break;
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
if (err != err) {
|
|
755
|
+
err = 1e6;
|
|
756
|
+
}
|
|
743
757
|
/* --- COMPUTATION OF HNEW */
|
|
744
758
|
fac11 = pow_dd(&err, &expo1);
|
|
745
759
|
/* --- LUND-STABILIZATION */
|
|
@@ -23,9 +23,15 @@
|
|
|
23
23
|
typedef void (*FjModelFn)(int, double, double *, double *, double *);
|
|
24
24
|
|
|
25
25
|
/* Writes df/dy at (t, y) to J (column-major n x n) with jac, then replaces the
|
|
26
|
-
* columns with a non-finite entry by central differences of model
|
|
27
|
-
*
|
|
28
|
-
*
|
|
26
|
+
* columns with a non-finite entry by central differences of model, with step
|
|
27
|
+
* h = cbrt(eps) max(|y_j|, 1e-5). A column whose central difference has a
|
|
28
|
+
* non-finite entry is retried with the forward difference (f(y + h1) - f(y)) / h1,
|
|
29
|
+
* then the backward one (f(y) - f(y - h1)) / h1, with h1 = sqrt(eps) max(|y_j|, 1e-5)
|
|
30
|
+
* (error-optimal for one-sided differences); the first fully finite one is kept,
|
|
31
|
+
* otherwise the central one. f(y) is evaluated at most once, and only if some
|
|
32
|
+
* column needs it. y isn't modified. Returns the number of replaced columns
|
|
33
|
+
* (including those that stayed non-finite), or -1 if their scratch space
|
|
34
|
+
* couldn't be allocated (J then holds jac's values). */
|
|
29
35
|
static inline int finite_jacobian(FjModelFn model, FjModelFn jac, int n, double t,
|
|
30
36
|
const double *y, double *J, double *pars) {
|
|
31
37
|
jac(n, t, (double *)y, J, pars);
|
|
@@ -34,12 +40,12 @@ static inline int finite_jacobian(FjModelFn model, FjModelFn jac, int n, double
|
|
|
34
40
|
if (!isfinite(J[k])) first_bad = k / n;
|
|
35
41
|
}
|
|
36
42
|
if (first_bad < 0) return 0;
|
|
37
|
-
double *work = (double *)malloc((size_t)
|
|
43
|
+
double *work = (double *)malloc((size_t)4 * (size_t)n * sizeof(double));
|
|
38
44
|
if (!work) return -1;
|
|
39
|
-
double *yh = work, *up = work + n, *down = work + 2 * n;
|
|
45
|
+
double *yh = work, *up = work + n, *down = work + 2 * n, *f0 = work + 3 * n;
|
|
40
46
|
memcpy(yh, y, (size_t)n * sizeof(double));
|
|
41
|
-
const double rel = cbrt(DBL_EPSILON);
|
|
42
|
-
int replaced = 0;
|
|
47
|
+
const double rel = cbrt(DBL_EPSILON), rel1 = sqrt(DBL_EPSILON);
|
|
48
|
+
int have_f0 = 0, replaced = 0;
|
|
43
49
|
for (int j = first_bad; j < n; j++) {
|
|
44
50
|
double *col = J + (size_t)j * n;
|
|
45
51
|
int finite = 1;
|
|
@@ -51,8 +57,34 @@ static inline int finite_jacobian(FjModelFn model, FjModelFn jac, int n, double
|
|
|
51
57
|
yh[j] = y[j] - h;
|
|
52
58
|
model(n, t, yh, down, pars);
|
|
53
59
|
yh[j] = y[j];
|
|
54
|
-
|
|
60
|
+
finite = 1;
|
|
61
|
+
for (int i = 0; i < n; i++) {
|
|
62
|
+
col[i] = (up[i] - down[i]) / (2.0 * h);
|
|
63
|
+
finite = finite && isfinite(col[i]);
|
|
64
|
+
}
|
|
55
65
|
replaced++;
|
|
66
|
+
if (finite) continue;
|
|
67
|
+
/* One-sided differences, written to up / down and copied only if finite */
|
|
68
|
+
if (!have_f0) {
|
|
69
|
+
model(n, t, yh, f0, pars);
|
|
70
|
+
have_f0 = 1;
|
|
71
|
+
}
|
|
72
|
+
const double h1 = rel1 * fmax(fabs(y[j]), 1e-5);
|
|
73
|
+
for (int side = 0; side < 2; side++) {
|
|
74
|
+
double *d = side == 0 ? up : down;
|
|
75
|
+
yh[j] = side == 0 ? y[j] + h1 : y[j] - h1;
|
|
76
|
+
model(n, t, yh, d, pars);
|
|
77
|
+
yh[j] = y[j];
|
|
78
|
+
finite = 1;
|
|
79
|
+
for (int i = 0; i < n; i++) {
|
|
80
|
+
d[i] = side == 0 ? (d[i] - f0[i]) / h1 : (f0[i] - d[i]) / h1;
|
|
81
|
+
finite = finite && isfinite(d[i]);
|
|
82
|
+
}
|
|
83
|
+
if (finite) {
|
|
84
|
+
memcpy(col, d, (size_t)n * sizeof(double));
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
56
88
|
}
|
|
57
89
|
free(work);
|
|
58
90
|
return replaced;
|
|
@@ -105,6 +105,23 @@ int exp_problem_init(ExpProblem *P, int n_y, const double *y0, int n_pars, int n
|
|
|
105
105
|
P->ss_tol = ss_tol;
|
|
106
106
|
P->ss_t_max = ss_t_max;
|
|
107
107
|
|
|
108
|
+
/* Every target must index an existing state or derived output and have a finite, nonzero
|
|
109
|
+
* scale (it divides the residual); every override must name a parameter. */
|
|
110
|
+
for (int k = 0; k < n_targets; k++) {
|
|
111
|
+
const int bound = target_kind[k] == TARGET_STATE ? n_y : n_derived;
|
|
112
|
+
if (target_index[k] < 0 || target_index[k] >= bound || !isfinite(target_scale[k]) ||
|
|
113
|
+
target_scale[k] == 0.0) {
|
|
114
|
+
exp_problem_free(P);
|
|
115
|
+
return -3;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
for (int i = 0; i < n_ovr; i++) {
|
|
119
|
+
if (ovr_idx[i] < 0 || ovr_idx[i] >= n_pars) {
|
|
120
|
+
exp_problem_free(P);
|
|
121
|
+
return -3;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
108
125
|
int seg_off = 0, pt_off = 0;
|
|
109
126
|
for (int e = 0; e < n_exp; e++) {
|
|
110
127
|
P->exp_seg_off[e] = seg_off;
|
|
@@ -132,6 +149,15 @@ int exp_problem_init(ExpProblem *P, int n_y, const double *y0, int n_pars, int n
|
|
|
132
149
|
return 0;
|
|
133
150
|
}
|
|
134
151
|
|
|
152
|
+
int exp_check_fit_idx(const ExpProblem *P, int n_fit, const int *fit_idx) {
|
|
153
|
+
for (int i = 0; i < n_fit; i++) {
|
|
154
|
+
if (fit_idx[i] < 0 || fit_idx[i] >= P->n_pars) return -6;
|
|
155
|
+
for (int j = 0; j < i; j++)
|
|
156
|
+
if (fit_idx[j] == fit_idx[i]) return -6;
|
|
157
|
+
}
|
|
158
|
+
return 0;
|
|
159
|
+
}
|
|
160
|
+
|
|
135
161
|
int exp_overrides_fitted(const ExpProblem *P, int n_fit, const int *fit_idx) {
|
|
136
162
|
for (int i = 0; i < P->n_ovr; i++) {
|
|
137
163
|
int fitted = 0;
|
|
@@ -231,11 +257,12 @@ static int walk_time_course(const ExpProblem *P, int e, ModelFn fn, int n, doubl
|
|
|
231
257
|
|
|
232
258
|
init_output(2000, n);
|
|
233
259
|
if (t_end > t) {
|
|
234
|
-
/*
|
|
235
|
-
*
|
|
236
|
-
* is only second-order accurate, which limits fits to ~1e-4
|
|
237
|
-
* precision. An on_traj caller (the adjoint) needs every step
|
|
238
|
-
|
|
260
|
+
/* Every solver records the data points of this segment exactly from its
|
|
261
|
+
* own interpolant (dense output). Linear interpolation between accepted
|
|
262
|
+
* steps is only second-order accurate, which limits fits to ~1e-4
|
|
263
|
+
* relative precision. An on_traj caller (the adjoint) needs every step
|
|
264
|
+
* instead. */
|
|
265
|
+
if (!on_traj) {
|
|
239
266
|
int k = j;
|
|
240
267
|
while (k < n_e && t_data[k] <= t_end) k++;
|
|
241
268
|
if (k > j) set_output_grid(t_data + j, k - j);
|
|
@@ -345,7 +372,12 @@ static void residual_point(void *vctx, int e, int seg, int j, double t, const do
|
|
|
345
372
|
: c->derived_out[P->target_index[k]];
|
|
346
373
|
size_t idx = exp_index(P, e, k, j);
|
|
347
374
|
double data_v = P->data_y[idx];
|
|
348
|
-
|
|
375
|
+
/* A non-finite model value (e.g. a derived target at log(0)) gets the same penalty as a
|
|
376
|
+
* failed integration: NaN or Inf in fvec would break lmdif's / lmder's QR and every
|
|
377
|
+
* convergence test after it. */
|
|
378
|
+
c->fvec[idx] = isnan(data_v) ? 0.0
|
|
379
|
+
: !isfinite(model_v) ? EXP_PENALTY
|
|
380
|
+
: (model_v - data_v) / P->target_scale[k];
|
|
349
381
|
if (c->model_out) c->model_out[idx] = model_v;
|
|
350
382
|
}
|
|
351
383
|
}
|
|
@@ -83,7 +83,8 @@ typedef struct {
|
|
|
83
83
|
} ExpProblem;
|
|
84
84
|
|
|
85
85
|
/* Copies every array. Returns 0, -1 on allocation failure, -3 if the
|
|
86
|
-
* experiment layout is inconsistent (fit_init's doc comment)
|
|
86
|
+
* experiment layout is inconsistent (fit_init's doc comment) or a target or
|
|
87
|
+
* override index, or a target scale (zero, non-finite), is invalid. */
|
|
87
88
|
int exp_problem_init(ExpProblem *P, int n_y, const double *y0, int n_pars, int n_targets,
|
|
88
89
|
const int *target_kind, const int *target_index,
|
|
89
90
|
const double *target_scale, int n_derived, int n_ovr, const int *ovr_idx,
|
|
@@ -102,6 +103,10 @@ static inline size_t exp_index(const ExpProblem *P, int e, int k, int j) {
|
|
|
102
103
|
return (size_t)P->n_targets * P->exp_pt_off[e] + (size_t)k * P->exp_n_points[e] + j;
|
|
103
104
|
}
|
|
104
105
|
|
|
106
|
+
/* 0, or -6 if a fitted parameter index is out of range or repeated (a
|
|
107
|
+
* repeated one would give lmdif / lmder a zero Jacobian column). */
|
|
108
|
+
int exp_check_fit_idx(const ExpProblem *P, int n_fit, const int *fit_idx);
|
|
109
|
+
|
|
105
110
|
/* 1 if some segment overrides one of the n_fit parameters fit_idx — the
|
|
106
111
|
* gradient backends can't differentiate through an override. */
|
|
107
112
|
int exp_overrides_fitted(const ExpProblem *P, int n_fit, const int *fit_idx);
|