Skip to content

Commit 296c776

Browse files
committed
Fix crash for models without state variables
Previously, `nx_solver=0` would have in a crash when calling `amici::SUNMatrixWrapper::capacity` on `SUNMatrixWrapper` of 0-sized sparse matrix. Unrelated: Replace some unnecessary `std::vector::at` calls.
1 parent 9ea6aef commit 296c776

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

src/model.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2875,25 +2875,28 @@ void Model::fdwdp(realtype const t, realtype const* x, bool include_static) {
28752875
}
28762876

28772877
void Model::fdwdx(realtype const t, realtype const* x, bool include_static) {
2878+
// NOTE: (at least) Model_{ODE,DAE}::fJSparse rely on `fw` and `fdwdw`
2879+
// being called from here. They need to be executed even if nx_solver==0.
28782880
if (!nw)
28792881
return;
28802882

28812883
fw(t, x, include_static);
28822884

28832885
derived_state_.dwdx_.zero();
28842886
if (pythonGenerated) {
2885-
if (!derived_state_.dwdx_hierarchical_.at(0).capacity())
2886-
return;
2887-
28882887
fdwdw(t, x, include_static);
28892888

2889+
auto&& dwdx_hierarchical_0 = derived_state_.dwdx_hierarchical_.at(0);
2890+
if (!dwdx_hierarchical_0.data() || !dwdx_hierarchical_0.capacity())
2891+
return;
2892+
28902893
if (include_static) {
2891-
derived_state_.dwdx_hierarchical_.at(0).zero();
2892-
fdwdx_colptrs(derived_state_.dwdx_hierarchical_.at(0));
2893-
fdwdx_rowvals(derived_state_.dwdx_hierarchical_.at(0));
2894+
dwdx_hierarchical_0.zero();
2895+
fdwdx_colptrs(dwdx_hierarchical_0);
2896+
fdwdx_rowvals(dwdx_hierarchical_0);
28942897
}
28952898
fdwdx(
2896-
derived_state_.dwdx_hierarchical_.at(0).data(), t, x,
2899+
dwdx_hierarchical_0.data(), t, x,
28972900
state_.unscaledParameters.data(), state_.fixedParameters.data(),
28982901
state_.h.data(), derived_state_.w_.data(), state_.total_cl.data(),
28992902
derived_state_.spl_.data(), include_static

0 commit comments

Comments
 (0)