Skip to content

Commit 3f4b41a

Browse files
committed
further improved description of partial derivatives (#26)
1 parent e07c163 commit 3f4b41a

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

fmi-guide/9___advanced.adoc

+39-27
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ FMI 3.0 provides optional access to partial derivatives for variables of an FMU.
66
Partial derivatives can be used:
77

88
* in Newton algorithms to solve algebraic loops,
9-
* in implicit integration algorithms in Model Exchange, and
10-
* in iterative co-simulation algorithms.
9+
* in implicit integration algorithms in Model Exchange,
10+
* in iterative Co-Simulation algorithms, and
11+
* in optimization algorithms.
1112

1213
To avoid expensive numeric approximations of these derivatives, FMI offers dedicated functions to retrieve partial derivatives for variables of an FMU:
1314

@@ -28,16 +29,36 @@ with the Jacobian
2829
\end{bmatrix}
2930
++++
3031

31-
where latexmath:[\mathbf{v}_{\mathit{known}}] are the latexmath:[n] knowns, and latexmath:[\mathbf{g}] are the latexmath:[m] functions to calculate the latexmath:[m] unknown variables latexmath:[\mathbf{v}_{\mathit{unknwon}}] from the knowns.
32+
where latexmath:[\mathbf{v}_{\mathit{known}}] are the latexmath:[n] knowns, and latexmath:[\mathbf{g}] are the latexmath:[m] functions to calculate the latexmath:[m] unknown variables latexmath:[\mathbf{v}_{\mathit{unknwon}}] from the knowns.
3233

33-
Both functions can be used to compute Jacobian-vector products and to construct the partial derivative matrices column-wise resp. row-wise by choosing the seed vector latexmath:[\mathbf{v}_{\mathit{seed}} \in \mathbb{R}^n] resp. latexmath:[\mathbf{\bar{v}}_{\mathit{seed}} \in \mathbb{R}^m] accordingly.
34+
The functions can be used to compute Jacobian-vector products or to construct the partial derivative matrices column-wise or row-wise by choosing the seed vector latexmath:[\mathbf{v}_{\mathit{seed}} \in \mathbb{R}^n] or latexmath:[\mathbf{\bar{v}}_{\mathit{seed}} \in \mathbb{R}^m], respectively, accordingly.
3435

3536
For information on the call signature see the FMI specification.
3637

3738
==== Directional Derivatives [[directionDerivatives]]
39+
The function `fmi3GetDirectionalDerivative` computes the directional derivative
40+
41+
[latexmath]
42+
++++
43+
\mathbf{v}_{\mathit{sensitivity}} = \mathbf{J} \cdot \mathbf{v}_{\mathit{seed}}
44+
++++
45+
46+
One can either retrieve the latexmath:[\mathit{i}]-th column of the Jacobian by specifying the latexmath:[\mathit{i}]-th unit vector latexmath:[\mathbf{e}_{\mathit{i}}] as the seed vector latexmath:[\mathbf{v}_{\mathit{seed}}], or compute a Jacobian-vector product latexmath:[\mathbf{Jv}] by using latexmath:[\mathbf{v}] as the seed vector latexmath:[\mathbf{v}_{\mathit{seed}}]. Therefore, the function can be utilized for the following purposes, among others:
47+
48+
- Solving algebraic loops with a nonlinear solver requires matrix latexmath:[{\frac{\partial \mathbf{g}}{\partial \mathbf{u}}}].
49+
50+
- Numerical integrators of stiff methods need matrix latexmath:[{\frac{\partial \mathbf{f}}{\partial \mathbf{x}}}].
51+
52+
- If the FMU is connected with other FMUs, the partial derivatives of the state derivatives and outputs with respect to the continuous states and the inputs are needed in order to compute the Jacobian for the system of the connected FMUs.
53+
54+
- If the FMU shall be linearized, the same derivatives as in the previous item are needed.
55+
56+
- If the FMU is used as the model for an extended Kalman filter, latexmath:[{\frac{\partial \mathbf{f}}{\partial \mathbf{x}}}] and latexmath:[{\frac{\partial \mathbf{g}}{\partial \mathbf{x}}}] are needed.
57+
58+
- If matrix-free linear solvers shall be used, Jacobian-vector products latexmath:[{\mathbf{Jv}}] are needed (e.g. as a user-supplied routine in CVODE <<CVODE570>>).
3859

3960
[[example-directional-derivatives]]
40-
Example: +
61+
Example 1: +
4162
Assume an FMU has the output equations
4263

4364
[latexmath]
@@ -71,18 +92,7 @@ Note that a direct implementation of this function with analytic derivatives:
7192

7293
(c) Computes the directional derivative with the seed-values provided in the function arguments; so in the <<example-directional-derivatives,above example>>: latexmath:[{v_{\mathit{sensitivity}} = \Delta y_1 (\Delta x = 0, \Delta u_1 = 1, \Delta u_3 = 0, \Delta u_4 = 0)}]] and latexmath:[{v_{\mathit{sensitivity}} = \Delta y_1 (\Delta x = 0, \Delta u_1 = 0, \Delta u_3 = 1, \Delta u_4 = 0)}]]
7394

74-
Note, function `fmi3GetDirectionalDerivative` can be utilized for the following purposes:
75-
76-
- Numerical integrators of stiff methods need matrix latexmath:[{\frac{\partial \mathbf{f}}{\partial \mathbf{x}}}].
77-
78-
- If the FMU is connected with other FMUs, the partial derivatives of the state derivatives and outputs with respect to the continuous states and the inputs are needed in order to compute the Jacobian for the system of the connected FMUs.
79-
80-
- If the FMU shall be linearized, the same derivatives as in the previous item are needed.
81-
82-
- If the FMU is used as the model for an extended Kalman filter, latexmath:[{\frac{\partial \mathbf{f}}{\partial \mathbf{x}}}] and latexmath:[{\frac{\partial \mathbf{g}}{\partial \mathbf{x}}}] are needed.
83-
84-
- If matrix-free linear solvers shall be used, Jacobian-vector products latexmath:[{\mathbf{Jv}}] are needed (e.g. as a user-supplied routine in CVODE <<CVODE570>>).
85-
95+
Example 2: +
8696
If a dense matrix shall be computed, the columns of the matrix can be easily constructed by successive calls of `fmi3GetDirectionalDerivative`.
8797
For example, constructing the system Jacobian latexmath:[{\mathbf{A} = \frac{\partial \mathbf{f}}{\partial \mathbf{x}}}] as dense matrix can be performed in the following way:
8898

@@ -116,8 +126,7 @@ After each such call, the elements of the resulting directional derivative vecto
116126

117127
More details and implementational notes are available from <<ABL12>>.
118128

119-
Example:
120-
129+
Example 3: +
121130
Directional derivatives for higher dimension variables are almost treated in the same way.
122131
Consider, for example, an FMU which calculates its output latexmath:[{Y}] by multiplying its 2x2 input latexmath:[{U}] with a 3x2 constant gain latexmath:[{K}], with
123132

@@ -161,19 +170,22 @@ Note that in order to get the directional derivative of latexmath:[{Y}] with res
161170
The retrieved directional derivative `dd` is stored in a matrix of size 3x2, so `nSensitivity` is 6.
162171

163172
==== Adjoint Derivatives [[adjointDerivatives]]
173+
The function `fmi3GetAdjointDerivative` computes the adjoint derivative
174+
175+
[latexmath]
176+
++++
177+
\mathbf{\bar{v}}_{\mathit{sensitivity}}^T = \mathbf{\bar{v}}_{\mathit{seed}}^T \cdot \mathbf{J} \quad \text{or} \quad \mathbf{\bar{v}}_{\mathit{sensitivity}} = \mathbf{J}^T \cdot \mathbf{\bar{v}}_{\mathit{seed}}
178+
++++
179+
180+
One can either retrieve the latexmath:[\mathit{i}]-th row of the Jacobian by specifying the latexmath:[\mathit{i}]-th unit vector latexmath:[\mathbf{e}_{\mathit{i}}] as the seed vector latexmath:[\mathbf{\bar{v}}_{\mathit{seed}}], or compute a vector-Jacobian product latexmath:[\mathbf{v}^T\mathbf{J}] by using latexmath:[\mathbf{v}] as the seed vector latexmath:[\mathbf{\bar{v}}_{\mathit{seed}}].
164181

165182
Adjoint derivatives are beneficial in several contexts:
166183

167-
* in artificial intelligence (AI) frameworks the adjoint derivatives are called "vector gradient products" (VJPs).
168-
There adjoint derivatives are used in the backpropagation process to perform gradient-based optimization of parameters using reverse mode automatic differentiation (AD), see, e.g., <<BPRS15>>.
184+
* in artificial intelligence (AI) frameworks the adjoint derivatives are called "vector gradient products" (VJPs). There adjoint derivatives are used in the backpropagation process to perform gradient-based optimization of parameters using reverse mode automatic differentiation (AD), see, e.g., <<BPRS15>>.
169185

170186
* in parameter estimation (see <<BKF17>>)
171187

172-
Typically, reverse mode automatic differentiation (AD) is more efficient for these use cases than forward mode AD, as explained in the cited references.
173-
174-
If one would like to construct the full Jacobian matrix, one can use either `fmi3GetDirectionalDerivative` (to column-wise construct it) or `fmi3GetAdjointDerivative` (to row-wise construct it, possibly improved with coloring methods as mentioned above).
175-
However in the applications motivating the adjoint derivatives, one does not need the full Jacobian matrix latexmath:[\mathbf{J}], but vector latexmath:[\mathbf{v}^T] multiplied from the left to the Jacobian, i.e. latexmath:[\mathbf{v}^T\mathbf{J}].
176-
For computing the full Jacobian matrix, the column-wise construct is generally more efficient.
188+
Typically, reverse mode automatic differentiation (AD) is more efficient for these use cases than forward mode AD because the number of knowns is much higher than the number of unknowns (latexmath:[\mathit{n} \gg \mathit{m}]), as explained in the cited references. If the full Jacobian is needed and the number of knowns and unknowns are somewhat equal (latexmath:[\mathit{m} \approx \mathit{n}]) or small, the column-wise construct using `fmi3GetDirectionalDerivative` is generally more efficient.
177189

178190
Example: +
179191
Assume an FMU has the output equations
@@ -194,7 +206,7 @@ h_2(u_1, u_2)
194206
++++
195207

196208
and latexmath:[\left( w_1, w_2 \right)^T \cdot \mathbf{ \frac{\partial h}{\partial u} }] for some vector latexmath:[\left( w_1, w_2 \right)^T] is needed.
197-
Then one can get this with one function call of `fmi3GetAdjointDerivative` (with arguments_ latexmath:[\mathbf{v}_{\mathit{unknown}} = \text{valueReferences of} \left \{ y_1, y_2 \right \}, \mathbf{v}_{\mathit{known}} = \text{valueReferences of} \left \{ u_1, u_2 \right \}, \mathbf{\bar{v}}_{\mathit{seed}} = \left( w_1, w_2 \right)^T]), while with `fmi3GetDirectionalDerivative` at least two calls would be necessary to first construct the Jacobian column-wise and then multiplying from the right with latexmath:[\left( w_1, w_2 \right)^T].
209+
Then one can get this with one function call of `fmi3GetAdjointDerivative` (with arguments latexmath:[\mathbf{v}_{\mathit{unknown}} = \text{valueReferences of} \left \{ y_1, y_2 \right \}], latexmath:[\mathbf{v}_{\mathit{known}} = \text{valueReferences of} \left \{ u_1, u_2 \right \}], latexmath:[\mathbf{\bar{v}}_{\mathit{seed}} = \left( w_1, w_2 \right)^T]), while with `fmi3GetDirectionalDerivative` at least two calls would be necessary to first construct the Jacobian column-wise and then multiplying from the right with latexmath:[\left( w_1, w_2 \right)^T].
198210

199211
If a dense matrix shall be computed, the rows of the matrix can be easily constructed by successive calls of `fmi3GetAdjointDerivative`.
200212
For example, constructing the system Jacobian latexmath:[{\mathbf{A} = \frac{\partial \mathbf{f}}{\partial \mathbf{x}}}] as a dense matrix can be performed in the following way:

fmi-guide/A___literature.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
- [[[SSP10]]] Modelica Association: **System Structure and Parameterization 1.0**. March 2019. https://ssp-standard.org/publications/SSP10/SystemStructureAndParameterization10.pdf
99

10-
- [[[CVODE570]]] Hindmarsh A., Serban R., Balos C., Gardner D., Reynolds D., Woodward C.: *User Documentation for CVODE v5.7.0*. February 2021. https://computing.llnl.gov/sites/default/files/cv_guide-5.7.0.pdf
10+
- [[[CVODE570]]] Hindmarsh A., Serban R., Balos C., Gardner D., Reynolds D., Woodward C.: *User Documentation for CVODE v5.7.0*. February 2021, p. 85ff. https://computing.llnl.gov/sites/default/files/cv_guide-5.7.0.pdf
1111

1212
- [[[ABL12]]] &#197;kesson J., Braun W., Lindholm P., and Bachmann B. (2012): **Generation of Sparse Jacobians for the Functional Mockup Interface 2.0**. 9th International Modelica Conference, Munich, 2012. http://www.ep.liu.se/ecp/076/018/ecp12076018.pdf
1313

0 commit comments

Comments
 (0)