Skip to content

Commit af50a9f

Browse files
authored
Feature preequilibration sensitivities via FSA (#410)
* feature(core) refactor createSteadystateSimSolver * feature(core) enable preequilibration sensitivities via FSA * feature(core) add option to control whether FSA is used to computed steady state sensitivities * feature(test) add test cases for FSA steadystate sensis * fix(test) fix solver setup routine in unittest * fix(core) fix segfault * feature(core) add monotonicity checks for setTimepoints * feature(core) use std::is_sorted for monotonicity check. * fix(core) include header for std::is_sorted * fix(core) wrong header file for std::is_sorted * fix(doc) update documentation * feature(core) implement pos_pow to deal with positivity issues * fix(core) fix pos_pow regex * fix(steadystateproblem) use t0 not ts[0] for initializaiton of t after preequilibration * fix(doc) fix typo in documentation * fix(steadystateproblem) reduce sensi computation logic * fix(sbml) enable linking of fsx0 in model header * feature(sbml) properly process initial assignments of state variables TODO: do this for other variables aswell. * feature(steadystate) reset initialStates that are defined via fixedParameters after Preequilibration * fix(python-sbml) fix identification of 0.0 values and specifiy variable name for x0_fixedParameters * fix(steadystate) perform x0_fixedParameters update after updating the fixedParameters * fix(python) fix some codacy issues * fix(tests) fix codacy issues * fix(matlab) fix compile error in CalcMD5.c * feature(steadystate) add option for more sensitivity computation modes in the future * fix(steadystate) remove unnecessary intialization from initial condition for non-preequilibration * fix(matlab) fix matlab interface after update from enum classes * fix(steadystate) fix typo and update hdf
1 parent a18f2a7 commit af50a9f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+562
-390
lines changed

include/amici/defines.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,20 @@ enum class StateOrdering {
135135
COLAMD,
136136
natural
137137
};
138+
139+
/** Sensitivity computation mode in steadyStateProblem */
140+
enum class SteadyStateSensitivityMode {
141+
newtonOnly,
142+
simulationFSA
143+
};
138144

139-
/**
140-
* @brief msgIdAndTxtFp
141-
* @param identifier string with error message identifier
142-
* @param format string with error message printf-style format
143-
* @param ... arguments to be formatted
144-
*/
145-
typedef void (*msgIdAndTxtFp)(const char *identifier, const char *format, ...);
145+
/**
146+
* @brief msgIdAndTxtFp
147+
* @param identifier string with error message identifier
148+
* @param format string with error message printf-style format
149+
* @param ... arguments to be formatted
150+
*/
151+
typedef void (*msgIdAndTxtFp)(const char *identifier, const char *format, ...);
146152

147153
// clang-format on
148154

include/amici/interface_matlab.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ ReturnDataMatlab *setupReturnData(mxArray *plhs[], int nlhs);
4545
*/
4646
std::unique_ptr<ExpData> expDataFromMatlabCall(const mxArray *prhs[], const Model &model);
4747

48-
void amici_dgemv(AMICI_BLAS_LAYOUT layout, AMICI_BLAS_TRANSPOSE TransA,
48+
void amici_dgemv(BLASLayout layout, BLASTranspose TransA,
4949
const int M, const int N, const double alpha, const double *A,
5050
const int lda, const double *X, const int incX,
5151
const double beta, double *Y, const int incY);
5252

53-
void amici_dgemm(AMICI_BLAS_LAYOUT layout, AMICI_BLAS_TRANSPOSE TransA,
54-
AMICI_BLAS_TRANSPOSE TransB, const int M, const int N,
53+
void amici_dgemm(BLASLayout layout, BLASTranspose TransA,
54+
BLASTranspose TransB, const int M, const int N,
5555
const int K, const double alpha, const double *A,
5656
const int lda, const double *B, const int ldb,
5757
const double beta, double *C, const int ldc);

include/amici/model.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,16 @@ namespace amici {
172172
virtual void fJv(realtype t, AmiVector *x, AmiVector *dx, AmiVector *xdot,
173173
AmiVector *v, AmiVector *nJv, realtype cj) = 0;
174174

175+
/** Initial states
176+
* @param x pointer to state variables
177+
*/
175178
void fx0(AmiVector *x);
176179

180+
/** Sets only those initial states that are specified via fixedParmeters
181+
* @param x pointer to state variables
182+
*/
183+
void fx0_fixedParameters(AmiVector *x);
184+
177185
/** Initial value for time derivative of states (only necessary for DAEs)
178186
* @param x0 Vector with the initial states
179187
* @param dx0 Vector to which the initial derivative states will be
@@ -627,6 +635,22 @@ namespace amici {
627635
* @return the ids
628636
*/
629637
virtual std::vector<std::string> getObservableIds() const { return std::vector<std::string>(); }
638+
639+
/**
640+
* @brief sets the mode how sensitivities are computed in the steadystate simulation
641+
* @param mode steadyStateSensitivityMode
642+
*/
643+
void setSteadyStateSensitivityMode (const SteadyStateSensitivityMode mode) {
644+
steadyStateSensitivityMode = mode;
645+
}
646+
647+
/**
648+
* @brief gets the mode how sensitivities are computed in the steadystate simulation
649+
* @return flag value
650+
*/
651+
SteadyStateSensitivityMode getSteadyStateSensitivityMode () const {
652+
return steadyStateSensitivityMode;
653+
}
630654

631655
/** number of states */
632656
const int nx;
@@ -708,6 +732,15 @@ namespace amici {
708732
throw AmiException("Requested functionality is not supported as (%s) is not implemented for this model!",__func__);
709733
}
710734

735+
/** model specific implementation of fx0
736+
* @param x0 initial state
737+
* @param t initial time
738+
* @param p parameter vector
739+
* @param k constant vector
740+
**/
741+
virtual void fx0_fixedParameters(realtype *x0, const realtype t, const realtype *p, const realtype *k) {
742+
}
743+
711744
/** model specific implementation of fsx0
712745
* @param sx0 initial state sensitivities
713746
* @param t initial time
@@ -1250,6 +1283,10 @@ namespace amici {
12501283

12511284
/** starting time */
12521285
double tstart = 0.0;
1286+
1287+
/** flag indicating whether steadystate sensivities are to be computed
1288+
via FSA when steadyStateSimulation is used */
1289+
SteadyStateSensitivityMode steadyStateSensitivityMode = SteadyStateSensitivityMode::newtonOnly;
12531290

12541291
};
12551292

include/amici/newton_solver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class NewtonSolver {
3232

3333
void getStep(int ntry, int nnewt, AmiVector *delta);
3434

35-
void getSensis(const int it, AmiVectorArray *sx);
35+
void computeNewtonSensis(AmiVectorArray *sx);
3636

3737
/**
3838
* Writes the Jacobian for the Newton iteration and passes it to the linear

include/amici/solver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class Solver {
7474
*/
7575
virtual Solver* clone() const = 0;
7676

77-
void setup(ForwardProblem *fwd, Model *model);
77+
void setup(AmiVector *x, AmiVector *dx, AmiVectorArray *sx, AmiVectorArray *sdx, Model *model);
7878

7979
void setupAMIB(BackwardProblem *bwd, Model *model);
8080

include/amici/steadystateproblem.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "amici/defines.h"
55
#include "amici/vector.h"
6+
#include "amici/solver_cvodes.h"
67
#include <amici/newton_solver.h>
78

89
#include <nvector/nvector_serial.h>
@@ -33,13 +34,13 @@ class SteadystateProblem {
3334
void applyNewtonsMethod(ReturnData *rdata, Model *model,
3435
NewtonSolver *newtonSolver, int newton_try);
3536

36-
void getNewtonOutput(ReturnData *rdata, int newton_status,
37-
double run_time, int it);
37+
void getNewtonOutput(ReturnData *rdata, const Model *model,
38+
int newton_status, double run_time, int it);
3839

3940
void getSteadystateSimulation(ReturnData *rdata, Solver *solver,
4041
Model *model, int it);
4142

42-
std::unique_ptr<void, std::function<void(void *)>> createSteadystateSimSolver(Solver *solver, Model *model, realtype tstart);
43+
std::unique_ptr<CVodeSolver> createSteadystateSimSolver(Solver *solver, Model *model, realtype tstart);
4344

4445
/** default constructor
4546
* @param t pointer to time variable

include/amici/symbolic_functions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ double Dmin(int id, double a, double b, double c);
1212
double max(double a, double b, double c);
1313
double Dmax(int id, double a, double b, double c);
1414

15+
double pos_pow(double base, double exponent);
16+
1517
int isNaN(double what);
1618
int isInf(double what);
1719
double getNaN();

matlab/auxiliary/CalcMD5/CalcMD5.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ void MD5Init(MD5_CTX *context)
205205
void MD5Update(MD5_CTX *context, UCHAR *input, UINT inputLen)
206206
{
207207
UINT index, partLen;
208-
int i, inputLenM63;
208+
int inputLenM63;
209209

210210
/* Compute number of bytes mod 64: */
211211
index = (UINT)((context->count[0] >> 3) & 0x3F);
@@ -220,6 +220,7 @@ void MD5Update(MD5_CTX *context, UCHAR *input, UINT inputLen)
220220

221221
/* Transform as many times as possible: */
222222
if (inputLen >= partLen) {
223+
int i;
223224
memcpy((POINTER)&context->buffer[index], (POINTER)input, partLen);
224225
MD5Transform(context->state, context->buffer);
225226

matlab/auxiliary/compileAMICIDependencies.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
function includesstr = getIncludeString(amici_root_path, sundials_path, ssparse_path)
6262
includesstr = '';
6363
includesstr = strcat(includesstr,' -I"', fullfile(sundials_path, 'include'), '"');
64+
includesstr = strcat(includesstr,' -I"', fullfile(sundials_path, 'src'), '"');
6465
includesstr = strcat(includesstr,' -I"', fullfile(amici_root_path), '"');
6566
includesstr = strcat(includesstr,' -I"', fullfile(amici_root_path, 'src'), '"');
6667
includesstr = strcat(includesstr,' -I"', fullfile(amici_root_path, 'include'), '"');

python/amici/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ def rdataToNumPyArrays(rdata):
8989
9090
"""
9191
npReturnData = {'ptr': rdata}
92-
fieldNames = ['t', 'x', 'x0', 'sx', 'sx0', 'y', 'sigmay', 'sy', 'ssigmay',
93-
'z', 'rz', 'sigmaz', 'sz', 'srz', 'ssigmaz', 'sllh', 's2llh',
92+
fieldNames = ['t', 'x', 'x0', 'sx', 'sx0', 'y', 'sigmay', 'sy', 'ssigmay',
93+
'z', 'rz', 'sigmaz', 'sz', 'srz', 'ssigmaz', 'sllh', 's2llh',
9494
'J', 'xdot', 'status', 'llh', 'chi2', 'res', 'sres', 'FIM',
95-
'newton_numlinsteps', 'newton_numsteps',
96-
'numsteps', 'numrhsevals', 'numerrtestfails', 'numnonlinsolvconvfails',
95+
'newton_numlinsteps', 'newton_numsteps',
96+
'numsteps', 'numrhsevals', 'numerrtestfails', 'numnonlinsolvconvfails',
9797
'order', 'numstepsB', 'numrhsevalsB', 'numerrtestfailsB', 'numnonlinsolvconvfailsB']
9898

9999
for field in fieldNames:

0 commit comments

Comments
 (0)