2021-Kofman-Compact Sparse Symbolic Jacobian Computation
2021-Kofman-Compact Sparse Symbolic Jacobian Computation
a r t i c l e i n f o a b s t r a c t
Article history: This work introduces a novel algorithm that automatically produces computer code for the
Received 31 October 2020 calculation of sparse symbolical Jacobian matrices. More precisely, given the code for com-
Revised 25 February 2021
puting a function f depending on a set of state (independent) variables x, where the code
Accepted 4 March 2021
makes use of intermediate algebraic (auxiliary) variables a(x ), the algorithm automatically
Available online 19 March 2021
produces the code for the symbolic computation of the matrix J = ∂ f/∂ x in sparse repre-
Keywords: sentation.
Large scale models A remarkable feature of the algorithm developed is that it can deal with iterative defini-
Jacobian computation tions of the functions preserving the iterative representation during the whole process up
Set–based graphs
to the final Jacobian computation code. That way, in presence of arrays of functions and
variables, the computational cost of the code generation and the length of the generated
code does not depend on the size of those arrays. This feature is achieved making use of
Set–Based Graph representation.
The main application of the algorithm is the simulation of large scale dynamical systems
with implicit Ordinary Differential Equation (ODE) solvers like CVODE-BDF, whose perfor-
mance are greatly improved when they are invoked using a sparse Jacobian matrix. How-
ever, the algorithm can be used in a more general context for solving large systems of
nonlinear equations.
The paper, besides introducing the algorithm, discusses some aspects of its implementation
in a general purpose ODE solver front-end and analyzes some results obtained.
© 2021 Elsevier Inc. All rights reserved.
1. Introduction
Large scale systems of ODEs are usually stiff so they need implicit ODE solvers to be efficiently simulated [1]. These
numerical integration algorithms use the Jacobian matrix to solve the nonlinear implicit equations involved and their per-
formance is largely improved when the Jacobian matrix is externally provided with a sparse form representation [2,3].
In a simple PDE discretization, the code for the sparse Jacobian computation can be manually obtained without much
effort. However, in more complex models where there are several interacting subsystems, the manual task becomes almost
impossible. Motivated by this problem (and similar problems in a more general context) several algorithms in the field of
algorithmic differentiation have been developed [4–6].
∗
Corresponding author.
E-mail address: [email protected] (J. Fernández).
https://doi.org/10.1016/j.amc.2021.126181
0 096-30 03/© 2021 Elsevier Inc. All rights reserved.
E. Kofman, J. Fernández and D. Marzorati Applied Mathematics and Computation 403 (2021) 126181
A usual approach for computing the Jacobian (either in dense or sparse form) is to build a graph representing the depen-
dences between the function components fi and the state and auxiliary variables. Then, the different paths from a function
component fi to a given state x j represent different terms that accumulate in the expression of ∂ f i /∂ x j . These terms can be
computed using the chain rule as the path traverses different algebraic variables.
To the best of our knowledge, all the existing algorithms assume that function f is composed by individual scalar func-
tions fi depending on individual states x j and algebraic variables ak . Thus, in large scale systems the generation of the
code for computing a Jacobian matrix becomes computationally expensive as it depends at least linearly in the number of
variables involved. In models containing thousands or millions of states, the computational cost associated to the code gen-
eration and the subsequent compilation of the resulting huge piece of code eventually make the problem unsolvable. So the
use of a symbolic Jacobian is only possible if it can be manually obtained.
However, large scale systems are (almost) always defined making use of repeating structures (typically ‘for’ statements)
and arrays of variables. That way, there are sets of functions with identical definitions where the only thing that changes
between components are the indexes of the arrays they involve. Exploiting this feature is the main purpose of the present
work.
For that goal, we propose first to represent the dependence between functions and variables making use of Set–Based
Graphs (SBG) [7], where each vertex (called set–vertex) can represent an entire array of variables or functions and each
edge (called set–edge) represents all the connections between pairs of set–edges. This allows a compact representation
which results independent on the size of the arrays involved. Then, based on this compact representation, we develop an
algorithm that produces the code to compute the sparse Jacobian matrix. A remarkable property is that the computational
cost of this algorithm and the length of the code it produces is independent on the size of the arrays.
The algorithm was implemented as part of a tool called Stand Alone QSS Solver [8] that has a front end to different ODE
solvers, including CVODE, DASSL, DOPRI, as well as the whole family of Quantized State System (QSS) methods. This tool
allows defining the models using a subset of the Modelica language [9] and translates them into plain C code providing
also the code for the symbolic computation of the dense or sparse Jacobian (for algorithms like DASSL and CVODE) and
structural information (incidence matrices) for QSS algorithms. The tool performs all the transformations preserving the
for–loop statements so that the code produced does not depend on the size of the arrays involved in the model.
The article is organized as follows. After the formal statement of the problem below, Section 2 introduces some previous
results and tools that are used in the remaining of the work. Then, Section 3 presents the novel algorithm developed and
Section 4 discusses its implementation. Finally, the performance of this implementation is studied in Section 5 and some
conclusions and future research ideas are proposed in Section 6.
2
E. Kofman, J. Fernández and D. Marzorati Applied Mathematics and Computation 403 (2021) 126181
That way, gi,j is a set of functions with identical definition inside a for loop statement that compute components of ai .
Using the same idea to group the functions fi that define the state derivatives, the model of Eq. (1) can be rewritten in
a compact way as
a1 = g1i ,1 (x1 , . . . , xn , t ), i = 1, . . .
i+s1g ,1
a1 1,2 = g1i ,2 (x , . . . , x
1 n
, a11 , . . . , a1k , t ), i = 1, . . . ; k < i + s1g ,2
i+sg
.. ..
. .
am
i+sm, 1 = gm,i
1
(x1 , . . . , xn , a1 , . . . , am−1 , t ), i = 1, . . .
g
.. ..
. .
am
i+sm, j = gm,i
j
(x1 , . . . , xn , a1 , . . . , am−1 , am
1 , . . . , ak , t ),
m
i = 1, . . . ; k < i + sm,
g
j
g
.. .. (5)
. .
x˙ 1 = fi1,1 (x1 , . . . , xn , a1 , . . . , am , t ), i = 1, . . .
i+s1f ,1
x˙ 1 1,2 = fi1,2 ( x , . . . , x , a , . . . , a , t ),
1 n 1 m
i = 1, . . .
i+s f
.. ..
. .
x˙ n = fin, j (x1 , . . . , xn , a1 , . . . , am , t ), i = 1, . . .
i+sn,
f
j
.. ..
. .
m, j m, j
Here sg is an index shift (with sm,
g
1
= 0 )) so that the i + sg component of am is defined by the ith component of gm,j .
m, j
An analogous role is played by s f for computing the state derivatives.
The purpose of this work is to design and to implement an algorithm that produces the code for computing the Jacobian
matrix of Eq. (5) such that
• The computational cost of producing the code does not depend on the size of the different arrays involved.
• The length of the code produced does not depend on the size of those arrays.
• The code generated is efficient in the sense that it does not repeat unnecessary calculations, but it is not necessary
optimal in any other sense.
In addition, the code produced should be able to compute the Jacobian in sparse representation.
2. Background
In this section we present some previous results and tools that are used along the rest of the paper.
The problem of producing the code for computing the Jacobian matrix of a function lies in the discipline of automatic
differentiation (AD), or, more precisely, in the field of algorithmic differentiation [5].
The literature on AD mainly focus of producing efficient code for computing the derivatives of functions, including here
the problem of the sparse Jacobian computation [4,10,11]. The goal of the discipline is to produce an efficient code to com-
pute the Jacobian (and other higher derivatives), for which the community has developed several techniques and approaches.
However, to the best of our knowledge, the problem of producing a compact piece of code for very large systems has not
been tackled so far.
In this work, we shall only make use of some existing AD techniques in order to obtain the code for computing scalar
partial derivatives which will be part of the final Jacobian computation. Yet, the approach is independent on the way those
derivatives are obtained.
The algorithms presented in this work are based on the use of Set-Based Graphs (SB-Graphs), first defined in [7]. SB-
Graphs are regular graphs in which the vertices and edges are grouped in sets allowing sometimes a compact representation.
We introduced next the main definitions.
3
E. Kofman, J. Fernández and D. Marzorati Applied Mathematics and Computation 403 (2021) 126181
Definition 2 (Set–Edge). Given two Set–Vertices, V a and V b , with V a ∩ V b = ∅, a Set–Edge connecting V a and V b is a set of
non repeated edges E[{V a , V b }] = {e1 , e2 , . . . , en } where each edge is a set of two vertices ei = {vak ∈ V a , vbl ∈ V b }.
Definition 4 (Bipartite Set–Based Graph). A Bipartite Set–Based Graph is a Set–Based Graph G = (V, E ) where two disjoints
sets of set–vertices V1 , V2 can be found verifying V1 ∪ V2 = V, such that for every edge E i = E[{V a , V b }] ∈ E the condition
V a ∈ Vi implies that V b ∈
/ Vi
Definition 5 (Directed Set–Edge). Given two Set–Vertices, V a and V b , with V a ∩ V b = ∅ or V a = V b , a directed Set–Edge
from V a to V b is a set of non repeated edges E[(V a , V b )] = {e1 , e2 , . . . , en } where each edge is an ordered pair of vertices
ei = (vak ∈ V a , vbl ∈ V b ).
Definition 6 (Directed Set–Based Graph). A Directed Set–Based Graph is a pair G = (V, E ) where
An SB-Graph always defines an equivalent regular graph where the set of vertices and edges of the latter is the union
of the set–vertices and set–edges of the former. Thus, a SB–Graph contains the same information than a regular graph.
However, SB-Graphs can have a compact representation of that information provided that every set–edge and every set-
vertex is defined by intension.
The Stand-Alone QSS Solver [8] is a tool that was originally conceived to have an efficient implementation of Quantized
State Systems (QSS) numerical integration algorithms [1]. As these algorithms require the computation of incidence matrices,
and the algorithms themselves are efficient in certain large scale models, the solver includes implementations of efficient
automatic structure analysis methods in which this work is partially inspired.
The models in the Stand-Alone QSS Solver are written in a subset of the Modelica language [9] called micro-Modelica
(μ–Modelica). These models are parsed by the tool and translated into the C code that computes the right hand side of the
corresponding ODE (including zero crossing functions and event handlers, since QSS algorithms are particularly efficient for
discontinuous ODEs). The translation also includes the aforementioned structural analysis and the code produced allows to
compute individual state derivatives and different incidence matrices. A remarkable feature of this translation is that it is
performed preserving the arrays of the model definition and without expanding for loop statements.
Besides implementing QSS methods, the tool offers a front-end to classic ODE and DAE solvers like DASSL, CVODE, IDA,
and DOPRI. For these methods, the model is also automatically translated into C code with all the necessary structures used
by these solvers. In addition, using the theory developed in this work, the code for computing the symbolic Jacobian matrix
is automatically generated by the tool (for CVODE-BDF and IDA).
While there is a large account of works dealing with automatic computation of sparse Jacobian matrices, to the best of
our knowledge the problem of looking for a compact piece of code has not been considered so far. The literature on AD
focus on having a code that minimizes the number of operations when the Jacobian is evaluated, but it does not consider
the cost of producing that piece of code (including the time needed to compile it).
4
E. Kofman, J. Fernández and D. Marzorati Applied Mathematics and Computation 403 (2021) 126181
Among the different works on symbolic computation of Jacobian matrices, there are some that are specifically related to
Modelica simulation [12–14]. We mention this since one of the main motivations of this work is related to expanding the
abilities of Modelica tools to simulate large scale systems, a key problem faced nowadays by the community [15–17]
The algorithm proposed here is inspired by the problem of causalization of large systems of equations presented in [7] for
which the concept of SB-Graphs was developed. There, algorithm of maximum matching and strongly connected components
(Tarjan’s) where developed in the context of SB-Graphs, which were constructed out of sets of equations written in Modelica
language. A remarkable property of those algorithms was that, as in this work, their complexity was independent on the
size of the arrays involved.
3. Main results
This section presents the main result of the work, namely, the novel algorithm for producing the compact code for
computing the sparse Jacobian. For simplicity, we introduce first in Section 3.1 a simple graph based version of the algorithm
that does not take into account the presence of arrays of equations or variables. Then, in Section 3.2 we describe the SB-
Graph representation of a large system like that of Eq. (5), and then in Section 3.3 we finally introduce the novel algorithm.
A preliminary algorithm for computing the sparse Jacobian matrix corresponding to the system of Eq. (1) is proposed
next. Although it is not optimal and it includes some unnecessary steps and the graph representation of the model contains
redundant vertices and edges, it can be easily extended for Set–Based Graphs.
The algorithm requires that the model is represented by a directed bipartite graph that is built based on the following
rules:
Based on this representation, each path that starts in a function vertex F i and finishes in a state vertex X j implies that
there is a term that must be added to compute ∂ f i /∂ x j . All the paths from each function to all the states can be found using
a Depth First Search (DFS) on the graph, which can be exploited to compute also partial derivatives of the type ∂ f i /∂ a j ,
∂ gi /∂ x j , and ∂ gi /∂ a j that are necessary to apply the chain rule whenever it is necessary.
Based on these ideas, we propose the following algorithm that produces the code to compute first all the partial deriva-
tives and then, based on those partial derivatives, it produces the final code to compute the sparse Jacobian. The algorithm
that performs the DFS also computes the set of states that the function depends on.
Here, the notation [i] means that the expression is replaced in the string by the value of i.
The DFS that produces the code to compute the partial derivatives is performed by the following recursive function. This
function also computes the set of states that the function depends on:
The code produced assumes that the algebraic variables ai are all computed before invoking the function. It also assumes
that all partial derivatives are initialized at zero. Function expr(∂ v/∂ xi ) computes the symbolic partial derivative of function
v using automatic differentiation.
In order to illustrate the way the algorithm works, we consider the following system
a1 = x31
a2 = a1 + x2
(6)
x˙ 1 = −a1 + x1
x˙ 2 = −a2 + a21
5
E. Kofman, J. Fernández and D. Marzorati Applied Mathematics and Computation 403 (2021) 126181
The piece of code produced by the algorithm and some of the intermediate steps it performs is listed below:
6
E. Kofman, J. Fernández and D. Marzorati Applied Mathematics and Computation 403 (2021) 126181
The piece of code produced evaluates both the sparse Jacobian and its structure. Taking into account that the structure
usually does not change during simulations, it is preferable to split the code so that the structure (i.e., size and index arrays)
is computed in a separate piece of code. That could be easily done with a straightforward modification of the algorithm
proposed.
Given the ODE of Eq. (5), we could still apply the previous algorithm by rewriting the set of equations in the form
of Eq. (1). However, by doing so, the number of vertices and edges in the graph and the length of the code produced will
depend on the size of the arrays. If the size of the code is very large, then the compiler will take hours to compile. Moreover,
beyond certain size, it will not be able to handle the code at all.
Thus, in order to overcome this problem, we propose first to represent the large system of equations of Eq. (5) using a
set-based graph built according to the following rules:
• A set-vertex X k is associated to each state array xk .
• A set-vertex Ak is associated to each algebraic array ak .
• A set-vertex Gk,l is associated to each array of functions gk,l = [gk1 ,l , gk2 ,l , . . .].
• A set-vertex F k,l is associated to each array of functions fk,l = [fk1 ,l , fk2 ,l , . . .].
Directed set-edges EGk,l are directed from each set vertex Ak to the set-vertices Gk,l , where EGk,l = i { (A , Gk,l )}
• k
i+sk,l i
g
• Given k, m, l, all the pairs (Fik,l , X jm ) such that xm
j
appears in the expression of fik,l form a directed set-edge from F k,l to
X , namely EF X = i, j {(Fi , X j )}.
m k,l,m k,l m
Notice that this definition only requires to know the index functions mapr and they do not depend on the size of the arrays
involved.
The set-edges EFk,l,m
A
k,l,m
, EGX k,l,m
, and EGA can be analogously defined and then all the set–edges can be defined by intension
without any dependence on the size of the arrays involved.
For the set–based graph described above, we propose an extension of Algorithms 1–2 to produce the code for the sparse
Jacobian evaluation.
The algorithm copies the Jacobian matrix values and structure from the matrices that store the partial derivatives of each
function f i, j on the right hand side of Eq. (5). These partial derivatives have also sparse representation and the code for its
computation is produced by a recursive function that extends Algorithm 2.
The new recursive function, besides producing the code for computing the partial derivatives in sparse form, returns
an array of dependences F i, j .depStates(d), with d = 1, . . . , F i, j .numDeps. Each dependence is a subset of a set vertex, i.e.,
F i, j .depStates(d ) ⊆ X k and it indicates that the components of function f i, j depends on the corresponding components of xk .
For each dependence, the recursive function also computes a map F i, j .Map(d ) that establishes the structure of this depen-
i, j
dence, i.e., F i, j .Map(d )(l ) = m implies that fl depends on xkm .
The dependence maps are built making use of the maps that define the different set edges EF X , EF A , etc. The states
grouped in the same dependence set F i, j .depStates(d ) ⊆ X k have the properties that they belong to the same array (xk ) and
7
E. Kofman, J. Fernández and D. Marzorati Applied Mathematics and Computation 403 (2021) 126181
that they are computed through the same map F i, j .Map(d ). Thus, their contribution to the matrix ∂ f i, j /∂ x can be computed
using the same piece of code.
T T
All the partial derivatives must be computed with respect to the global state vector x = [x1 , . . . , xn T ] . Thus, the recur-
sive function makes use of a state map (xMap) that maps the components of the different arrays into the components of
the global state vector, i.e. xxMap(k,l ) = xkl . The construction of this map is straightforward and it is assumed to be known
before the algorithm starts.
Based on these observations, the algorithm for producing the code that computes the partial derivatives in sparse repre-
sentation is the following one:
This algorithm, just like the ordinary graph algorithm (Algorithm 2), first perform a DFS in order to produce the code for
computing all the partial derivatives of functions gi, j that are needed to apply the chain rule. Then, it produces the code for
computing the partial derivatives of the current function ∂ v/∂ x.
The main difference with the ordinary graph algorithm is that now the state dependencies of each function (V .depStates)
are not individual states but are subsets of the state arrays. In addition, for each state dependence Xˆ ⊆ X k , the algorithm
builds an index map V.Map between the components of the function V and the components of the state arrays X k .
These differences are also reflected in the written code. First, the new algorithm writes a for loop sentence that
traverses the different components of v which are translated into rows of the sparse matrix representing ∂ v/∂ x. In addition,
during the traversal of the components of v, the code uses the maps V.Map(d ) to relate the rows with the components of
the X k and checks that the result of applying the map leads effectively to components of each state dependence Xˆ ⊆ X k . It
could happen that only some components of V effectively depend on components of Xˆ .
This algorithm can be improved in several ways. Firstly, it does not take into account the situation in which an algebraic
variable aij depends on another algebraic variable aik that belongs to the same array (with k < j to preserve causality). This
case would be detected at line when Gi, j = V (i.e., when a function Gi, j is a successor of itself). Although for reasons of
space this special case is not treated here, the right code can be generated using the information about the recursive use of
the maps mapm [gi, j ai ].
Another case that is not properly explained is the presence of expressions like aik = j xm j
, where a single algebraic (or
state derivative) variable depends on several components of the same array. As presented in the algorithm above, that case
would be represented by the use of several maps (up to the number of terms in the sum). However, it can be simplified by
generalizing the set–edge representation so that they have not only an image map, but also a domain map, i.e.,
EFk,l,m
X
= {( k,l
Fmap m
k,l ,xm ] (i ) , Xmap [ f k,l ,xm ] (i ) )}
dom [ f im
i
so that a single equation depending on several states can be described by a single map. Then, this compact information can
be used to produce a compact piece of code (including iterations over the states at the right hand side) for the Jacobian
matrix.
Also, as in the previous example, the algorithm can be straightforwardly modified so that it splits the code corresponding
to the Jacobian values and its structure.
In order to illustrate the way this algorithm works, we consider the following model, which represent the equations of a
nonlinear RC transmission line of arbitrary size (given by the parameter N) and with an inductive load.
(10−x11 )3
a11 = R
= g11,1 (· ),
(x1i −x1i+1 )3
a1i+1 = R
= g1i ,2 (· ), i = 1, . . . , N − 1
a1i −a1i+1
x˙ 1i = C
= fi1,1 · ,() i = 1, . . . , N − 1 (7)
a1N −x21
x˙ 1N = C
= f11,2 · , ()
x1N
x˙ 21 = L
= f12,1 · .
()
The set–graph representation for this model is depicted in Fig. 2. It contains three set vertices for the variables X 1 , X 2 ,
A1 , and five set vertices for the functions, F 1,1 , F 1,2 , F 2,1 , G1,1 , and G1,2 .
There are eight set edges characterized by the following maps: map1 [ f 1,1 , a1 ](i ) = i, map2 [ f 1,1 , a1 ](i ) = i + 1,
map1 [ f 1,2 , a1 ](1 ) = N, map1 [ f 1,2 , x2 ](1 ) = 1, map1 [ f 2,1 , x1 ](1 ) = 1, map1 [g1,1 , x1 ](1 ) = 1, map1 [g1,2 , x1 ](i ) = i,
map2 [g1,2 , x1 ](i ) = i + 1, map1 [a1 , g1,1 ](1 ) = 1, and map1 [a1 , g1,2 ](i ) = i + 1.
The steps performed and the code produced by the algorithm on this example is listed in Appendix A.
4. Implementation
A variant of Algorithms 3–4 described in the previous section was implemented as part of the Stand Alone QSS Solver
[8]. The implementation has the following features:
• The models are described in a subset of Modelica language called μ-Modelica. This sub-language allows describing sys-
tems in ODE form and it has support for multi-dimensional arrays.
8
E. Kofman, J. Fernández and D. Marzorati Applied Mathematics and Computation 403 (2021) 126181
9
E. Kofman, J. Fernández and D. Marzorati Applied Mathematics and Computation 403 (2021) 126181
• The Modelica code is parsed and a Set-Based Graph is built based on the rules described in Section 3.2. Set–Based Graph
are implemented by extending features of the Boost library [18].
• The different sets and subset of vertices involved in the algorithm are represented by unions of Multidimensional Interval
Arrays (MDI). Each MDI is defined by three integer numbers in each dimension (initial index, final index and step). That
way, the representation complexity is independent on the size of the arrays.
• There are implementations of operations like union, intersection and mutual exclusion between sets. These operation
also produce sets of MDIs with a complexity that is independent on the size of the arrays.
• The index maps that define the set-edges are limited to linear affine functions between MDIs indices. Thus, each map is
characterized by two integer numbers in each dimension. Also, it is simple to apply the composition of several maps as
it is required by the algorithm.
• There are also operations that compute the image map of sets represented by MDIs that do not depend on the size of
the MDIs.
• Making use of these features, the algorithm for establishing the sets of dependences of a function (v.depStates) is almost
identical to that described by Algorithm 4.
• The code generation, however, has some differences. Here, instead of computing a sparse matrix for the partial derivative
of each function (including those that define algebraic variables), the algorithm directly produces the code for computing
the sparse Jacobian at once. That way, a more compact piece of code is produced, yet the algorithm is more complex.
• The algebraic differentiation needed in lines and of Algorithm 4 is performed using GiNaC [19].
• The final piece of code produced is written in plain C language.
5. Examples
In order to evaluate the code produced by the proposed algorithm, we present two examples where we measure the
Compilation Time (C.T.), Simulation Time (S.T.) and Error for different problem sizes and three different Jacobian representa-
tions:
• Dense Numerical (i.e., no symbolic Jacobian is provided)
• Symbolic Sparse Non-Expanded (with the code obtained using the algorithm introduced in this work)
• Symbolic Sparse Expanded (using a symbolic Jacobian obtained after expanding the for loop statements)
The compilation time is the total time taken by the code generation algorithm and the subsequent C code compilation.
The simulation time is the time it takes to CVODE solver to complete the simulation. Finally, the error on each output
variables is computed using the RMSE (Root Mean Square Error) metric defined as:
(xˆ − x )2
RMSE = (8)
N
Where N is the number of sampled simulation output points and x is the computed ground truth for each variable.
It is worth mentioning that in the Dense Numerical case the model is not expanded and the Jacobian matrix is not
provided. So, the compilation time is expected to be the minimum one.
In both examples we use the following setup:
10
E. Kofman, J. Fernández and D. Marzorati Applied Mathematics and Computation 403 (2021) 126181
11
E. Kofman, J. Fernández and D. Marzorati Applied Mathematics and Computation 403 (2021) 126181
Table 1
RC Transmission Line system results with different size of parameter N.
Size C.T S.T. Error C.T S.T. Error C.T S.T. Error
100 430 38 6.17 · 10−4 8.80 · 104 12 1.48 · 10−4 434 14 4.54 · 10−4
200 423 104 9.44 · 10−5 3.63 · 105 20 2.09 · 10−5 444 21 9.78 · 10−5
500 442 586 2.36 · 10−4 1.96 · 106 65 1.16 · 10−4 452 70 3.70 · 10−4
1000 427 1062 1.21 · 10−4 7.15 · 106 168 2.09 · 10−5 436 162 6.53 · 10−5
2000 445 1.13 · 104 2.49 · 10−4 445 473 2.24 · 10−4
5000 475 7.72 · 104 9.17 · 10−5 447 2584 1.08 · 10−4
10000 448 2.98 · 105 6.84 · 10−5 532 9400 8.27 · 10−5
20000 669 1.11 · 106 4.54 · 10−5 674 3.32 · 104 3.08 · 10−5
• The reported results were obtained using an Intel(R) Core(TM) i7 − [email protected] CPU with 24 GB of RAM memory.
We used QSS Solver v4.0.rc and CVODE_BDF solver on Ubuntu OS.
• For each experiment, 10% of the total output variables were randomly selected to compute the simulation error.
• The reported simulation error was computed as the average of the selected output variables.
• The ground truth values were obtained using DASSL over 50 0 0 sampled points with the following tolerances: tolrel =
1 · 10−10 and tolabs = 1 · 10−10 .
We first revisit the RC transmission line example presented previously and then we evaluate a two dimensional advection
diffusion reaction (ADR) model.
This model defined previously in Eq. (7) represents a nonlinear RC transmission line of arbitrary size, given by the pa-
rameter N and with an inductive load. Additionally, we set the rest of the model parameters to R = 1, C = 1, L = 1 with
tolerances set to tolrel = 1 · 10−4 and tolabs = 1 · 10−4 . Finally, the initial conditions are x1i = 1 for i = 1, . . . , N and x21 = 0.
(10−x11 )3
a11 = R
(x1i −x1i+1 )3
a1i+1 = R
i = 1, . . . , N − 1
a1i −a1i+1
x˙ 1i = i = 1, . . . , N − 1 (9)
C
a1N −x21
x˙ 1N = C
x1N
x˙ 21 = L
Table 1 shows the simulation results obtained for different sizes (N) of the problem. As it was already mentioned, the sys-
tem was simulated using the CVODE_BDF solver with the three different Jacobian representations (dense numerical, sparse
non-expanded, and sparse expanded).
Regarding the simulation time, both sparse representations achieve almost the same performance, while the dense rep-
resentation becomes up to 33 times slower when N = 20 0 0 0 as it could be expected. The fact that all the errors are similar
and that the simulation time of both sparse representations coincide indicate that the non-expanded Jacobian code is cor-
rect.
The compilation time for the non-expanded models is of the order of 500 milliseconds and, as expected, it remains con-
stant as the size of the problem grows. On the contrary, the compilation time when the arrays and for loops are expanded
grows about quadratically with the size N, taking about two hours for N = 10 0 0 and becoming unusable beyond that size.1
That way, the new algorithm allows to achieve the best performance in both processes: compilation and simulation.
The following set of ODEs corresponds to the MOL (Method Of Lines) discretization of a 2D ADR problem:
1
Appendix A contains the full code generated for this example.
12
E. Kofman, J. Fernández and D. Marzorati Applied Mathematics and Computation 403 (2021) 126181
Table 2
2D ADR results for different grid size parameter N.
Size C.T S.T. Error C.T S.T. Error C.T S.T. Error
10x10 406 4 4.91 · 10−5 1.80 · 104 2 3.74 · 10−4 406 2 4.91 · 10−5
20x20 410 34 4.58 · 10−5 2.83 · 105 5 1.64 · 10−4 396 6 4.57 · 10−5
30x30 409 196 6.11 · 10−5 1.44 · 106 11 2.11 · 10−4 404 14 6.12 · 10−5
40x40 418 662 1.03 · 10−4 4.82 · 106 23 1.69 · 10−4 409 25 1.04 · 10−4
50x50 394 1898 1.56 · 10−4 406 39 1.56 · 10−4
100x100 411 3.26 · 104 2.26 · 10−4 410 214 2.26 · 10−4
150x150 407 2.08 · 105 2.16 · 10−4 408 555 2.15 · 10−4
where N is the number of grid points, and we consider parameters ax , ay , r, x and y to be equal to 1, and we set the
initial conditions u1,1 = 1 and ui, j = 0 for i = 2, . . . , N j = 2, . . . , N.
The results obtained are shown in Table 2. As in the previous example, simulation times are several times faster using
the symbolic sparse Jacobian (up to 375 times faster than the dense representation for a grid of size N = 150). The errors
are also similar and the simulation times are almost the same for both sparse representations indicating again that the
non-expanded Jacobian was correctly computed.
Compilation times were almost constant and very similar to each other for both non-expanded approaches (dense and
sparse), while the expanded system took more than one hour to compile for a grid of 40 × 40 becoming untractable beyond
that size.
Again, the proposed algorithm allowed to achieve the best performance in compilation and simulation.
We presented a novel algorithm that automatically computes compact sparse symbolical Jacobian matrices for large sys-
tems of ODEs with algebraic variables and equations. For that goal, the algorithm makes use of Set–Based Graph represen-
tations of the equation systems that allows finding sets of variable dependencies in every single step, producing also the
corresponding pieces of code in a compact way with computational costs that do not depend on the size of the arrays
involved.
We implemented the algorithm in the Stand Alone QSS Solver and presented two examples that show its advantages.
In the examples, the compact representation allowed to produce the right code for very large problems in constant time,
which was not possible using the expanded approach. Without this algorithm, the simulations should have been performed
using the numerical dense Jacobian representation (which is several times slower) or obtaining the Jacobian representation
in a manual way, which requires a lot of effort (particularly in the multidimensional case).
Regarding future work, we are planning to implement this algorithm in more mature simulation tools, such as Open-
Modelica [20]. We are also working on making use of the Set–Based Graph compact representation of large graphs for the
different stages of the compilation of large scale models by extending the work of [7].
The source code including the implementation of the proposed algorithm and the examples presented in this work can
be publicly downloaded from https://github.com/CIFASIS/qss-solver/releases/tag/v4.0.rc.
Funding
This work was partially funded by Agencia Nacional de Promoción Científica y Tecnológica (ANPCYT) grant PICT–2017
2436.
13
E. Kofman, J. Fernández and D. Marzorati Applied Mathematics and Computation 403 (2021) 126181
14
E. Kofman, J. Fernández and D. Marzorati Applied Mathematics and Computation 403 (2021) 126181
15
E. Kofman, J. Fernández and D. Marzorati Applied Mathematics and Computation 403 (2021) 126181
16
E. Kofman, J. Fernández and D. Marzorati Applied Mathematics and Computation 403 (2021) 126181
17
E. Kofman, J. Fernández and D. Marzorati Applied Mathematics and Computation 403 (2021) 126181
References
[1] F.E. Cellier, E. Kofman, Continuous System Simulation, Springer, New York, 2006.
[2] S.D. Cohen, A.C. Hindmarsh, P.F. Dubois, Cvode, a stiff/nonstiff ode solver in C, Comput. Phys. 10 (2) (1996) 138–143.
[3] L.A. Nejad, A comparison of stiff ode solvers for astrochemical kinetics problems, Astrophys. Space Sci. 299 (1) (2005) 1–29.
[4] S.A. Forth, M. Tadjouddine, J.D. Pryce, J.K. Reid, Jacobian code generated by source transformation and vertex elimination can be as efficient as hand–
coding, ACM Trans. Math. Softw. (TOMS) 30 (3) (2004) 266–299.
[5] U. Naumann, The Art of Differentiating Computer Programs: An Introduction to Algorithmic Differentiation, 24, Siam, 2012.
[6] A. Elsheikh, An equation-based algorithmic differentiation technique for differential algebraic equations, J. Comput. Appl. Math. 281 (2015) 135–151.
[7] P. Zimmermann, J. Fernández, E. Kofman, Set-based graph methods for fast equation sorting in large dae systems, in: Proceedings of the 9th Interna-
tional Workshop on Equation-based Object-oriented Modeling Languages and Tools, 2019, pp. 45–54.
[8] J. Fernández, E. Kofman, A stand-alone quantized state system solver for continuous system simulation, Simulation 90 (7) (2014) 782–799.
[9] P. Fritzson, Principles of Object-Oriented Modeling and Simulation With Modelica 3.3: A Cyber-Physical Approach, Wiley-IEEE Press, 2015.
[10] B.M. Averick, J.J. Moré, C.H. Bischof, A. Carle, A. Griewank, Computing large sparse jacobian matrices using automatic differentiation, SIAM J. Sci.
Comput. 15 (2) (1994) 285–294.
[11] E. Varnik, Exploitation of Structural Sparsity in Algorithmic Differentiation., RWTH Aachen University, 2011 Ph.D. thesis.
[12] W. Braun, L. Ochel, B. Bachmann, Symbolically derived Jacobians using automatic differentiation-enhancement of the openmodelica compiler, in: Pro-
ceedings of the 8th International Modelica Conference; March 20th-22nd; Technical University; Dresden; Germany, 063, Linköping University Elec-
tronic Press, 2011, pp. 495–501.
[13] J. Åkesson, W. Braun, P. Lindholm, B. Bachmann, Generation of sparse Jacobians for the function mock-up interface 2.0, in: Proceedings of the 9th
International MODELICA Conference; September 3–5; 2012; Munich; Germany, 076, Linköping University Electronic Press, 2012, pp. 185–196.
[14] W. Braun, K. Kulshreshtha, R. Franke, A. Walther, B. Bachmann, Towards adjoint and directional derivatives in FMI utilizing ADOL-C within openmod-
elica, in: Proceedings of the 12th International Modelica Conference, Prague, Czech Republic, May 15–17, 2017, 132, Linköping University Electronic
Press, 2017, pp. 363–366.
[15] F. Casella, Simulation of large-scale models in modelica: state of the art and future perspectives, in: Proceedings of the 11th International Modelica
Conference, 2015, pp. 459–468.
[16] W. Braun, F. Casella, B. Bachmann, et al., Solving large-scale modelica models: new approaches and experimental results using openmodelica, in:
Proceedings of the 12 International Modelica Conference, Linkoping University Electronic Press, 2017, pp. 557–563.
[17] G. Schweiger, H. Nilsson, J. Schoeggl, W. Birk, A. Posch, Modeling and simulation of large-scale systems: a systematic comparison of modeling
paradigms, Appl. Math. Comput. 365 (2020) 124713.
[18] J. Siek, A. Lumsdaine, L.-Q. Lee, The Boost Graph Library: User Guide and Reference Manual, Addison-Wesley, 2002.
[19] C. Bauer, A. Frink, R. Kreckel, Introduction to the GiNaC framework for symbolic computation within the C++ programming language, J. Symb. Comput.
33 (1) (2002) 1–12.
[20] P. Fritzson, P. Aronsson, H. Lundvall, K. Nystrom, A. Pop, L. Saldamli, D. Broman, The openmodelica modeling, simulation, and development environ-
ment., in: Proceedings of the 46th Conference on Simulation and Modeling (SIMS’05), 2005, pp. 83–90.
18