@@ -608,18 +608,33 @@ def jacp(*args, **kwargs):
608
608
609
609
# if we have changed the equations to include the explicit sensitivity
610
610
# equations, then we also need to update the mass matrix
611
+ n_inputs = model .len_rhs_sens // model .len_rhs
612
+ n_state_without_sens = model .len_rhs_and_alg
611
613
if calculate_sensitivities_explicit :
612
- n_inputs = model .len_rhs_sens // model .len_rhs
613
- model .mass_matrix_inv = pybamm .Matrix (
614
- block_diag (
615
- [model .mass_matrix_inv .entries ] * (n_inputs + 1 ), format = "csr"
614
+ if model .mass_matrix .shape [0 ] == n_state_without_sens :
615
+ model .mass_matrix_inv = pybamm .Matrix (
616
+ block_diag (
617
+ [model .mass_matrix_inv .entries ] * (n_inputs + 1 ),
618
+ format = "csr"
619
+ )
616
620
)
617
- )
618
- model .mass_matrix = pybamm .Matrix (
619
- block_diag (
620
- [model .mass_matrix .entries ] * (n_inputs + 1 ), format = "csr"
621
+ model .mass_matrix = pybamm .Matrix (
622
+ block_diag (
623
+ [model .mass_matrix .entries ] * (n_inputs + 1 ), format = "csr"
624
+ )
625
+ )
626
+ else :
627
+ # take care if calculate_sensitivites used then not used
628
+ n_state_with_sens = model .len_rhs_and_alg * (n_inputs + 1 )
629
+ if model .mass_matrix .shape [0 ] == n_state_with_sens :
630
+ model .mass_matrix_inv = pybamm .Matrix (
631
+ model .mass_matrix_inv .entries [:n_state_without_sens ,
632
+ :n_state_without_sens ]
633
+ )
634
+ model .mass_matrix = pybamm .Matrix (
635
+ model .mass_matrix .entries [:n_state_without_sens ,
636
+ :n_state_without_sens ]
621
637
)
622
- )
623
638
624
639
# Save CasADi functions for the CasADi solver
625
640
# Note: when we pass to casadi the ode part of the problem must be in
0 commit comments