Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sensitivities for AlgebraicRule models #2101

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions src/model_dae.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,36 @@ void Model_DAE::fdxdotdp(
const realtype t, const const_N_Vector x, const const_N_Vector dx
) {
auto x_pos = computeX_pos(x);
fdwdp(t, N_VGetArrayPointerConst(x_pos));

if (pythonGenerated) {
// python generated, not yet implemented for DAEs
throw AmiException("Wrapping of DAEs is not yet implemented from Python"
);
// python generated
derived_state_.dxdotdp_explicit.zero();
derived_state_.dxdotdp_implicit.zero();
if (derived_state_.dxdotdp_explicit.capacity()) {
fdxdotdp_explicit_colptrs(derived_state_.dxdotdp_explicit);
fdxdotdp_explicit_rowvals(derived_state_.dxdotdp_explicit);
fdxdotdp_explicit(
derived_state_.dxdotdp_explicit.data(), t,
N_VGetArrayPointerConst(x_pos),
state_.unscaledParameters.data(), state_.fixedParameters.data(),
state_.h.data(), N_VGetArrayPointerConst(dx), derived_state_.w_.data()
);
}

fdxdotdw(t, x_pos, dx);
/* Sparse matrix multiplication
dxdotdp_implicit += dxdotdw * dwdp */
derived_state_.dxdotdw_.sparse_multiply(
derived_state_.dxdotdp_implicit, derived_state_.dwdp_
);

derived_state_.dxdotdp_full.sparse_add(
derived_state_.dxdotdp_explicit, 1.0,
derived_state_.dxdotdp_implicit, 1.0
);
} else {
// matlab generated
fdwdp(t, N_VGetArrayPointerConst(x_pos));

for (int ip = 0; ip < nplist(); ip++) {
N_VConst(0.0, derived_state_.dxdotdp.getNVector(ip));
Expand Down Expand Up @@ -496,9 +518,15 @@ void Model_DAE::fsxdot(
}

if (pythonGenerated) {
// python generated, not yet implemented for DAEs
throw AmiException("Wrapping of DAEs is not yet implemented from Python"
);
/* copy dxdotdp and the implicit version over */
// initialize
N_VConst(0.0, sxdot);
realtype* sxdot_tmp = N_VGetArrayPointer(sxdot);

derived_state_.dxdotdp_full.scatter(
plist(ip), 1.0, nullptr, gsl::make_span(sxdot_tmp, nx_solver), 0,
nullptr, 0
);
} else {
/* copy dxdotdp over */
N_VScale(1.0, derived_state_.dxdotdp.getNVector(ip), sxdot);
Expand Down
7 changes: 4 additions & 3 deletions tests/testSBMLSuite.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,15 @@ def test_sbml_testsuite_case(test_number, result_path, sbml_semantic_cases_dir):
current_test_path,
test_id,
model_dir,
generate_sensitivity_code=test_id in sensitivity_check_cases,
generate_sensitivity_code=True,
)
settings = read_settings_file(current_test_path, test_id)

atol, rtol = apply_settings(settings, solver, model, test_id)

solver.setSensitivityOrder(amici.SensitivityOrder.first)
solver.setSensitivityMethod(amici.SensitivityMethod.forward)

# simulate model
rdata = amici.runAmiciSimulation(model, solver)
if rdata["status"] != amici.AMICI_SUCCESS:
Expand All @@ -98,8 +101,6 @@ def test_sbml_testsuite_case(test_number, result_path, sbml_semantic_cases_dir):

# check sensitivities for selected models
if epsilon := sensitivity_check_cases.get(test_id):
solver.setSensitivityOrder(amici.SensitivityOrder.first)
solver.setSensitivityMethod(amici.SensitivityMethod.forward)
check_derivatives(model, solver, epsilon=epsilon)

except amici.sbml_import.SBMLException as err:
Expand Down