Skip to content

Commit a4e077f

Browse files
authored
Release 0.10.16 (Merge pull request #877 from ICB-DCM/release_0.10.16)
* **Sparsify dwdp to reduce computation time for adjoints (#858)** * Fix(matlab) update example name example_dae_events->example_calvetti (Closes #866) * Fix nullptr deferencing for simulations with events when no measurements are provided (Fixes #866) * Fix accessing empty vector during adjoint state event update (Closes #866) * Fix pysb_import (fixes #878)
2 parents d549d93 + 96cb13e commit a4e077f

24 files changed

+688
-253
lines changed

.github/workflows/test-large-model.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ on:
44
branches:
55
- develop
66
- master
7-
- fix_862_powsimp
7+
- feature_sparse_quadratures
8+
89
pull_request:
910
branches:
1011
- master
@@ -73,3 +74,9 @@ jobs:
7374
- name: adjoint_sensitivities
7475
run: |
7576
check_time.sh adjoint_sensitivities tests/performance/test.py adjoint_sensitivities
77+
- name: forward_simulation_non_optimal_parameters
78+
run: |
79+
check_time.sh forward_simulation_non_optimal_parameters tests/performance/test.py forward_simulation_non_optimal_parameters
80+
- name: adjoint_sensitivities_non_optimal_parameters
81+
run: |
82+
check_time.sh adjoint_sensitivities_non_optimal_parameters tests/performance/test.py adjoint_sensitivities_non_optimal_parameters

include/amici/abstract_model.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ class AbstractModel {
108108
const AmiVector &dx) = 0;
109109

110110
/**
111-
* @brief Parameter derivative of residual function
111+
* @brief Model specific sparse implementation of
112+
explicit parameter derivative of right hand side
112113
* @param t time
113114
* @param x state
114115
* @param dx time derivative of state (DAE only)
@@ -668,6 +669,18 @@ class AbstractModel {
668669
const realtype *p, const realtype *k, const realtype *h,
669670
const realtype *w, const realtype *tcl,
670671
const realtype *stcl);
672+
673+
/**
674+
* @brief Model specific implementation for dwdp, column pointers
675+
* @param indexptrs column pointers
676+
**/
677+
virtual void fdwdp_colptrs(sunindextype *indexptrs);
678+
679+
/**
680+
* @brief Model specific implementation for dwdp, row values
681+
* @param indexvals row values
682+
**/
683+
virtual void fdwdp_rowvals(sunindextype *indexvals);
671684

672685
/**
673686
* @brief Model specific sensitivity implementation of dwdp

include/amici/model.h

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,18 @@ class Model : public AbstractModel {
7171
* @param plist indexes wrt to which sensitivities are to be computed
7272
* @param idlist indexes indicating algebraic components (DAE only)
7373
* @param z2event mapping of event outputs to events
74+
* @param pythonGenerated flag indicating matlab or python wrapping
75+
* @param ndxdotdp_explicit number of nonzero elements dxdotdp_explicit
76+
* @param ndxdotdp_implicit number of nonzero elements dxdotdp_implicit
7477
*/
7578
Model(int nx_rdata, int nxtrue_rdata, int nx_solver, int nxtrue_solver,
7679
int ny, int nytrue, int nz, int nztrue, int ne, int nJ, int nw,
77-
int ndwdx, int ndwdp, int ndxdotdw, std::vector<int> ndJydy, int nnz,
78-
int ubw, int lbw, amici::SecondOrderMode o2mode,
80+
int ndwdx, int ndwdp, int ndxdotdw, std::vector<int> ndJydy,
81+
int nnz, int ubw, int lbw, amici::SecondOrderMode o2mode,
7982
const std::vector<amici::realtype> &p, std::vector<amici::realtype> k,
8083
const std::vector<int> &plist, std::vector<amici::realtype> idlist,
81-
std::vector<int> z2event);
84+
std::vector<int> z2event, bool pythonGenerated=false,
85+
int ndxdotdp_explicit=0, int ndxdotdp_implicit=0);
8286

8387
/** destructor */
8488
~Model() override = default;
@@ -130,6 +134,8 @@ class Model : public AbstractModel {
130134
using AbstractModel::fdsigmaydp;
131135
using AbstractModel::fdsigmazdp;
132136
using AbstractModel::fdwdp;
137+
using AbstractModel::fdwdp_colptrs;
138+
using AbstractModel::fdwdp_rowvals;
133139
using AbstractModel::fdwdx;
134140
using AbstractModel::fdwdx_colptrs;
135141
using AbstractModel::fdwdx_rowvals;
@@ -1033,12 +1039,6 @@ class Model : public AbstractModel {
10331039
*/
10341040
bool getAlwaysCheckFinite() const;
10351041

1036-
/**
1037-
* @brief check whether the model was generated from python
1038-
* @return that
1039-
*/
1040-
virtual bool wasPythonGenerated() const { return false; }
1041-
10421042
/**
10431043
* @brief Initial states
10441044
* @param x pointer to state variables
@@ -1130,7 +1130,6 @@ class Model : public AbstractModel {
11301130

11311131
/** number of nonzero entries in dxdotdw */
11321132
int ndxdotdw{0};
1133-
11341133
/** number of nonzero entries in dJydy */
11351134
std::vector<int> ndJydy;
11361135

@@ -1145,18 +1144,34 @@ class Model : public AbstractModel {
11451144

11461145
/** lower bandwith of the jacobian */
11471146
int lbw{0};
1148-
1147+
1148+
/** flag indicating Matlab or python based model generation */
1149+
bool pythonGenerated;
1150+
1151+
/** number of nonzero entries in ndxdotdp_explicit */
1152+
int ndxdotdp_explicit{0};
1153+
1154+
/** number of nonzero entries in ndxdotdp_implicit */
1155+
int ndxdotdp_implicit{0};
1156+
11491157
/** flag indicating whether for sensi == AMICI_SENSI_ORDER_SECOND
11501158
* directional or full second order derivative will be computed */
11511159
SecondOrderMode o2mode{SecondOrderMode::none};
11521160

11531161
/** flag array for DAE equations */
11541162
std::vector<realtype> idlist;
11551163

1156-
/** temporary storage of dxdotdp data across functions (dimension: nplist x
1157-
* nx_solver, row-major) */
1164+
/** temporary storage of dxdotdp data across functions, Python only
1165+
(dimension: nplist x nx_solver, nnz: ndxdotdp_explicit, type CSC_MAT) */
1166+
mutable SUNMatrixWrapper dxdotdp_explicit;
1167+
1168+
/** temporary storage of dxdotdp_implicit data across functions, Python only
1169+
(dimension: nplist x * nx_solver, nnz: ndxdotdp_implicit, type CSC_MAT) */
1170+
mutable SUNMatrixWrapper dxdotdp_implicit;
1171+
1172+
/** temporary storage of dxdotdp data across functions, Matlab only
1173+
(dimension: nplist x nx_solver, row-major) */
11581174
AmiVectorArray dxdotdp;
1159-
11601175
/** AMICI context */
11611176
AmiciApplication *app = &defaultContext;
11621177

@@ -1258,7 +1273,7 @@ class Model : public AbstractModel {
12581273
virtual void fdJydy_colptrs(sunindextype *indexptrs, int index);
12591274

12601275
/**
1261-
* @brief Model specific implementation of fdxdotdw row vals
1276+
* @brief Model specific implementation of fdJydy row vals
12621277
* @param indexptrs row val pointers
12631278
* @param index ytrue index
12641279
*/
@@ -1560,6 +1575,9 @@ class Model : public AbstractModel {
15601575
/** Sparse dxdotdw temporary storage (dimension: ndxdotdw) */
15611576
mutable SUNMatrixWrapper dxdotdw;
15621577

1578+
/** Sparse dwdp temporary storage (dimension: ndwdp) */
1579+
mutable SUNMatrixWrapper dwdp;
1580+
15631581
/** Sparse dwdx temporary storage (dimension: ndwdx) */
15641582
mutable SUNMatrixWrapper dwdx;
15651583

@@ -1573,12 +1591,12 @@ class Model : public AbstractModel {
15731591
mutable std::vector<realtype> mz;
15741592

15751593
/** Sparse observable derivative of data likelihood,
1576-
* only used if wasPythonGenerated()==true
1594+
* only used if pythonGenerated==true
15771595
* (dimension nytrue, nJ x ny, row-major) */
15781596
mutable std::vector<SUNMatrixWrapper> dJydy;
15791597

15801598
/** observable derivative of data likelihood,
1581-
* only used if wasPythonGenerated()==false
1599+
* only used if pythonGenerated==false
15821600
* (dimension nJ x ny x nytrue, row-major)
15831601
*/
15841602
mutable std::vector<realtype> dJydy_matlab;
@@ -1661,11 +1679,6 @@ class Model : public AbstractModel {
16611679
/** tempory storage of w data across functions (dimension: nw) */
16621680
mutable std::vector<realtype> w;
16631681

1664-
/** tempory storage of sparse/dense dwdp data across functions
1665-
* (dimension: ndwdp)
1666-
*/
1667-
mutable std::vector<realtype> dwdp;
1668-
16691682
/** tempory storage for flattened sx,
16701683
* (dimension: nx_solver x nplist, row-major)
16711684
*/

include/amici/model_dae.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,24 @@ class Model_DAE : public Model {
5858
* @param plist indexes wrt to which sensitivities are to be computed
5959
* @param idlist indexes indicating algebraic components (DAE only)
6060
* @param z2event mapping of event outputs to events
61+
* @param pythonGenerated flag indicating matlab or python wrapping
62+
* @param ndxdotdp_explicit number of nonzero elements dxdotdp_explicit
63+
* @param ndxdotdp_implicit number of nonzero elements dxdotdp_implicit
6164
*/
6265
Model_DAE(const int nx_rdata, const int nxtrue_rdata, const int nx_solver,
6366
const int nxtrue_solver, const int ny, const int nytrue,
6467
const int nz, const int nztrue, const int ne, const int nJ,
65-
const int nw, const int ndwdx, const int ndwdp,
66-
const int ndxdotdw, std::vector<int> ndJydy, const int nnz,
67-
const int ubw, const int lbw, const SecondOrderMode o2mode,
68+
const int nw, const int ndwdx, const int ndwdp, const int ndxdotdw,
69+
std::vector<int> ndJydy, const int nnz, const int ubw,
70+
const int lbw, const SecondOrderMode o2mode,
6871
std::vector<realtype> const &p, std::vector<realtype> const &k,
69-
std::vector<int> const &plist,
70-
std::vector<realtype> const &idlist,
71-
std::vector<int> const &z2event)
72+
std::vector<int> const &plist, std::vector<realtype> const &idlist,
73+
std::vector<int> const &z2event, const bool pythonGenerated=false,
74+
const int ndxdotdp_explicit=0, const int ndxdotdp_implicit=0)
7275
: Model(nx_rdata, nxtrue_rdata, nx_solver, nxtrue_solver, ny, nytrue,
73-
nz, nztrue, ne, nJ, nw, ndwdx, ndwdp, ndxdotdw,
74-
std::move(ndJydy), nnz, ubw, lbw, o2mode, p, k, plist, idlist,
75-
z2event) {}
76+
nz, nztrue, ne, nJ, nw, ndwdx, ndwdp, ndxdotdw, std::move(ndJydy),
77+
nnz, ubw, lbw, o2mode, p, k, plist, idlist, z2event,
78+
pythonGenerated, ndxdotdp_explicit, ndxdotdp_implicit) {}
7679

7780
void fJ(realtype t, realtype cj, const AmiVector &x, const AmiVector &dx,
7881
const AmiVector &xdot, SUNMatrix J) override;

include/amici/model_ode.h

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,25 @@ class Model_ODE : public Model {
5858
* @param plist indexes wrt to which sensitivities are to be computed
5959
* @param idlist indexes indicating algebraic components (DAE only)
6060
* @param z2event mapping of event outputs to events
61+
* @param pythonGenerated flag indicating matlab or python wrapping
62+
* @param ndxdotdp_explicit number of nonzero elements dxdotdp_explicit
63+
* @param ndxdotdp_implicit number of nonzero elements dxdotdp_implicit
6164
*/
6265
Model_ODE(const int nx_rdata, const int nxtrue_rdata, const int nx_solver,
6366
const int nxtrue_solver, const int ny, const int nytrue,
6467
const int nz, const int nztrue, const int ne, const int nJ,
6568
const int nw, const int ndwdx, const int ndwdp,
66-
const int ndxdotdw, std::vector<int> ndJydy, const int nnz,
67-
const int ubw, const int lbw, const SecondOrderMode o2mode,
68-
std::vector<realtype> const &p, std::vector<realtype> const &k,
69-
std::vector<int> const &plist,
69+
const int ndxdotdw, std::vector<int> ndJydy,
70+
const int nnz, const int ubw, const int lbw,
71+
const SecondOrderMode o2mode, std::vector<realtype> const &p,
72+
std::vector<realtype> const &k, std::vector<int> const &plist,
7073
std::vector<realtype> const &idlist,
71-
std::vector<int> const &z2event)
74+
std::vector<int> const &z2event, const bool pythonGenerated=false,
75+
const int ndxdotdp_explicit=0, const int ndxdotdp_implicit=0)
7276
: Model(nx_rdata, nxtrue_rdata, nx_solver, nxtrue_solver, ny, nytrue,
73-
nz, nztrue, ne, nJ, nw, ndwdx, ndwdp, ndxdotdw,
74-
std::move(ndJydy), nnz, ubw, lbw, o2mode, p, k, plist, idlist,
75-
z2event) {}
77+
nz, nztrue, ne, nJ, nw, ndwdx, ndwdp, ndxdotdw, std::move(ndJydy),
78+
nnz, ubw, lbw, o2mode, p, k, plist, idlist, z2event,
79+
pythonGenerated, ndxdotdp_explicit, ndxdotdp_implicit) {}
7680

7781
void fJ(realtype t, realtype cj, const AmiVector &x, const AmiVector &dx,
7882
const AmiVector &xdot, SUNMatrix J) override;
@@ -218,7 +222,7 @@ class Model_ODE : public Model {
218222
*/
219223
void fdxdotdw(realtype t, const N_Vector x);
220224

221-
/** Sensitivity of dx/dt wrt model parameters p
225+
/** Explicit sensitivity of dx/dt wrt model parameters p
222226
* @param t timepoint
223227
* @param x Vector with the states
224228
* @return status flag indicating successful execution
@@ -402,7 +406,7 @@ class Model_ODE : public Model {
402406
const realtype *p, const realtype *k, const realtype *h,
403407
const realtype *w) = 0;
404408

405-
/** model specific implementation of fdxdotdp, with w chainrule
409+
/** model specific implementation of fdxdotdp, with w chainrule (Matlab)
406410
* @param dxdotdp partial derivative xdot wrt p
407411
* @param t timepoint
408412
* @param x Vector with the states
@@ -418,19 +422,39 @@ class Model_ODE : public Model {
418422
const realtype *h, int ip, const realtype *w,
419423
const realtype *dwdp);
420424

421-
/** model specific implementation of fdxdotdp, without w chainrule
422-
* @param dxdotdp partial derivative xdot wrt p
425+
/** model specific implementation of fdxdotdp_explicit, no w chainrule (Py)
426+
* @param dxdotdp_explicit partial derivative xdot wrt p
423427
* @param t timepoint
424428
* @param x Vector with the states
425429
* @param p parameter vector
426430
* @param k constants vector
427431
* @param h heavyside vector
428-
* @param ip parameter index
429432
* @param w vector with helper variables
430433
*/
431-
virtual void fdxdotdp(realtype *dxdotdp, realtype t, const realtype *x,
432-
const realtype *p, const realtype *k,
433-
const realtype *h, int ip, const realtype *w);
434+
virtual void fdxdotdp_explicit(realtype *dxdotdp_explicit, realtype t,
435+
const realtype *x, const realtype *p,
436+
const realtype *k, const realtype *h,
437+
const realtype *w);
438+
439+
/** model specific implementation of fdxdotdp_explicit, colptrs part
440+
* @param indexptrs column pointers
441+
*/
442+
virtual void fdxdotdp_explicit_colptrs(sunindextype *indexptrs);
443+
444+
/** model specific implementation of fdxdotdp_explicit, rowvals part
445+
* @param indexvals row values
446+
*/
447+
virtual void fdxdotdp_explicit_rowvals(sunindextype *indexvals);
448+
449+
/** model specific implementation of fdxdotdp_implicit, colptrs part
450+
* @param indexptrs column pointers
451+
*/
452+
virtual void fdxdotdp_implicit_colptrs(sunindextype *indexptrs);
453+
454+
/** model specific implementation of fdxdotdp_implicit, rowvals part
455+
* @param indexvals row values
456+
*/
457+
virtual void fdxdotdp_implicit_rowvals(sunindextype *indexvals);
434458

435459
/** model specific implementation of fdxdotdw, data part
436460
* @param dxdotdw partial derivative xdot wrt w
@@ -450,12 +474,11 @@ class Model_ODE : public Model {
450474
*/
451475
virtual void fdxdotdw_colptrs(sunindextype *indexptrs);
452476

453-
/** model specific implementation of fdxdotdw, colptrs part
477+
/** model specific implementation of fdxdotdw, rowvals part
454478
* @param indexvals row values
455479
*/
456480
virtual void fdxdotdw_rowvals(sunindextype *indexvals);
457481
};
458-
459482
} // namespace amici
460483

461484
#endif // MODEL_H

include/amici/serialization.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ void serialize(Archive &ar, amici::Model &u, const unsigned int version) {
111111
ar &u.pscale;
112112
ar &u.tstart;
113113
ar &u.stateIsNonNegative;
114+
ar &u.pythonGenerated;
115+
ar &u.ndxdotdp_explicit;
116+
ar &u.ndxdotdp_implicit;
114117
}
115118

116119

0 commit comments

Comments
 (0)