modelon-community / Assimulo

Assimulo is a simulation package for solving ordinary differential equations.
https://jmodelica.org/assimulo/index.html
GNU Lesser General Public License v3.0
66 stars 16 forks source link

Difficulty in understanding the meaning of the `Explicit_problem` field `jac_nnz` #75

Closed kmaitreys closed 7 months ago

kmaitreys commented 7 months ago

I was going through this example and I'm having difficulty in understanding what does exp_mod.jac_nnz exactly mean. In the sundials.pyx module, jac_nnz is kind of defined though this exception:

#Specify the use of CVSPGMR linear solver.
if self.problem_info["jac_fcn_nnz"] == -1:
    raise AssimuloException("Need to specify the number of non zero elements in the Jacobian via the option 'jac_nnz'")

which makes sense, as nnz has mean the number of non-zero elements in the parlance of ODE solvers. But then I don't understand why in the example it is set as exp_mod.jac_nnz = 9. Because if one calculates, the 2-D jacobian of that system, you will find that it only has at most 7 non-zero elements. So, I'm not sure how this 9 was calculated.

I'm sorry if this is really a trivial question and I have made a very doltish mistake.

PeterMeisrimelModelon commented 7 months ago

Hi kmaitreys,

This is indeed not the best example. The more correct one would be

        colptrs = [0,2,5,7]
        rowvals = [0, 1, 0, 1, 2, 0, 1]
        data = [-0.04, 0.04, 1e4*y[2], -1e4*y[2]-6e7*y[1], 6e7*y[1], 1e4*y[1], -1e4*y[1]]

with exp_mod.jac_nnz = 7.

While the point of the sparse format is to omit structural zeros, having them included is not wrong - just inefficient. Since one assumes that structural zeros are omitted, the jac_nnz really is "number of elements in the sparse representation", which is 9 in this case. If you want to use the minimal jac_nnz = 7, you'll also have to change the sparse representation as above.

PeterMeisrimelModelon commented 7 months ago

https://github.com/modelon-community/Assimulo/pull/76