You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Drop copied examples
The examples were copied to the implementers's guid in
modelica/fmi-guides#20. This commit should
prevent doubling of the examples.
Signed-off-by: Christian Wolf <[email protected]>
* fix typo
---------
Signed-off-by: Christian Wolf <[email protected]>
Co-authored-by: Christian Bertsch <[email protected]>
_and this FMU is connected, so that latexmath:[{y_1, u_1, u_3}] appear in an algebraic loop._
1211
-
_Then the nonlinear solver needs a Jacobian and this Jacobian can be computed (without numerical differentiation) provided the partial derivative of latexmath:[{y_1}] with respect to latexmath:[{u_1}] and latexmath:[{u_3}] is available._
1212
-
_Depending on the environment where the FMUs are connected, these <<derivative,`derivatives`>> can be provided:_
1213
-
1214
-
(a) _with one wrapper function around function <<fmi3GetDirectionalDerivative>> to compute the directional derivatives with respect to these two variables (in other words, latexmath:[{v_{\mathit{unknown}} = y_1}], latexmath:[{v_{\mathit{known}} = \left \{ u_1, u_3 \right \}}]), and then the environment calls this wrapper function with latexmath:[{v_{\mathit{seed}} = \left \{ 1, 0 \right \}}] to compute the partial derivative with respect to latexmath:[{u_1}] and latexmath:[{v_{\mathit{seed}} = \left \{ 0, 1 \right \}}] to compute the partial derivative with respect to latexmath:[{u_3}], or_
1215
-
1216
-
(b) _with two direct function calls of <<fmi3GetDirectionalDerivative>> (in other words, latexmath:[{v_{\mathit{unknown}} = y_1, v_{\mathit{known}} = u_1, v_{\mathit{seed}} = 1}]; and latexmath:[{v_{\mathit{unknown}} = y_1, v_{\mathit{known}} = u_3, v_{\mathit{seed}} = 1}])._
1217
-
1218
-
_Note that a direct implementation of this function with analytic derivatives:_
1219
-
1220
-
(a) _Provides the directional derivative for all <<input>> variables; so in the <<example-directional-derivatives,above example>>: latexmath:[{\Delta y_1 = \frac{\partial g_1}{\partial x} \cdot \Delta x + \frac{\partial g_1}{\partial u_1} \cdot \Delta u_1 + \frac{\partial g_1}{\partial u_3} \cdot \Delta u_3 + \frac{\partial g_1}{\partial u_4} \cdot \Delta u_4}]_
1221
-
1222
-
(b) _Initializes all seed-values to zero; so in the <<example-directional-derivatives,above example>>: latexmath:[{\Delta x = \Delta u_1 = \Delta u_3 = \Delta u_4 = 0}]_
1223
-
1224
-
(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)}]]_
1191
+
_[For some examples on the usage of <<fmi3GetDirectionalDerivative>>, please look at the https://modelica.github.io/fmi-guides/main/fmi-guide/#directionDerivatives[examples in the implementers' guide].]_
1225
1192
1226
1193
_[Note, function <<fmi3GetDirectionalDerivative>> can be utilized for the following purposes:_
1227
1194
@@ -1233,111 +1200,6 @@ _[Note, function <<fmi3GetDirectionalDerivative>> can be utilized for the follow
1233
1200
1234
1201
- _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._]
1235
1202
1236
-
_If a dense matrix shall be computed, the columns of the matrix can be easily constructed by successive calls of <<fmi3GetDirectionalDerivative>>._
1237
-
_For example, constructing the system Jacobian latexmath:[{\mathbf{J} = \frac{\partial \mathbf{f}}{\partial \mathbf{x}}}] as dense matrix can be performed in the following way:_
_If the sparsity of a matrix shall be taken into account, then the matrix can be constructed in the following way:_
1245
-
1246
-
- _The incidence information of the matrix (whether an element is zero or not zero) is extracted from the XML file from element <<ModelStructure>>._
1247
-
1248
-
- _A so called graph coloring algorithm is employed to determine the columns of the matrix that can be computed by one call of `fmi3GetDirectionalDerivative`._
1249
-
1250
-
- _For the columns determined in (2), one call to <<fmi3GetDirectionalDerivative>> is made._
1251
-
_After each such call, the elements of the resulting directional derivative vector are copied into their correct locations of the partial derivative matrix._
1252
-
1253
-
_More details and implementational notes are available from <<ABL12>>._
1254
-
1255
-
_Example:_
1256
-
1257
-
_Directional derivatives for higher dimension variables are almost treated in the same way.
1258
-
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_
1259
-
1260
-
[latexmath]
1261
-
++++
1262
-
K=
1263
-
\begin{bmatrix}
1264
-
a, b
1265
-
\\
1266
-
c, d
1267
-
\\
1268
-
e, f
1269
-
\end{bmatrix}
1270
-
++++
1271
-
_The output latexmath:[{Y=K U}] is a matrix of size 3x2._
1272
-
_The directional derivative of an output element latexmath:[{Y(i,j)}] with respect to the input latexmath:[{U}] and the seed latexmath:[{\Delta U}] is:_
a \Delta U(1,1)+b \Delta U(2,1), a \Delta U(1,2)+ b \Delta U(2,2)
1288
-
\\
1289
-
c \Delta U(1,1)+d \Delta U(2,1), c \Delta U(1,2)+ d \Delta U(2,2)
1290
-
\\
1291
-
e \Delta U(1,1)+f \Delta U(2,1), e \Delta U(1,2)+ f \Delta U(2,2)
1292
-
\end{bmatrix}
1293
-
++++
1294
-
1295
-
_To get the directional derivative of latexmath:[{Y}] with respect to latexmath:[{U(2,1)}] the command `fmi3GetDirectionalDerivative(m, vr_Y, 1, vr_U, 1, {0.0, 0.0, 1.0, 0.0}, 4, dd, 6)` can be used where `vr_Y` and `vr_U` are references of the variable latexmath:[{Y}] and latexmath:[{U}], respectively._
1296
-
_Note that in order to get the directional derivative of latexmath:[{Y}] with respect to latexmath:[{U(2,1)}], the seed value `{0, 0, 1.0, 0}` has been used._
1297
-
_The retrieved directional derivative `dd` is stored in a matrix of size 3x2, so `nSensitivity` is 6._
1298
-
1299
1203
===== Adjoint Derivatives [[adjointDerivatives]]
1300
1204
1301
-
_[Adjoint derivatives are beneficial in several contexts:_
1302
-
1303
-
* _in artificial intelligence (AI) frameworks the adjoint derivatives are called "vector gradient products" (VJPs)._
1304
-
_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>>._
1305
-
1306
-
* _in parameter estimation (see <<BKF17>>)_
1307
-
1308
-
_Typically, reverse mode automatic differentiation (AD) is more efficient for these use cases than forward mode AD, as explained in the cited references._
1309
-
1310
-
_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)._
1311
-
_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}]._
1312
-
_For computing the full Jacobian matrix, the column-wise construct is generally more efficient.]_
1313
-
1314
-
_Example:_ +
1315
-
_Assume an FMU has the output equations_
1316
-
1317
-
[latexmath]
1318
-
++++
1319
-
\begin{bmatrix}
1320
-
y_1
1321
-
\\
1322
-
y_2
1323
-
\end{bmatrix}
1324
-
=
1325
-
\begin{bmatrix}
1326
-
g_1(u_1, u_2)
1327
-
\\
1328
-
g_2(u_1, u_2)
1329
-
\end{bmatrix}
1330
-
++++
1331
-
1332
-
_and latexmath:[\left( w_1, w_2 \right)^T \cdot \mathbf{ \frac{\partial g}{\partial u} }] for some vector latexmath:[\left( w_1, w_2 \right)^T] is needed._
1333
-
_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{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] _._
1334
-
1335
-
_If a dense matrix shall be computed, the rows of the matrix can be easily constructed by successive calls of <<fmi3GetAdjointDerivative>>._
1336
-
_For example, constructing the system Jacobian latexmath:[{\mathbf{J} = \frac{\partial \mathbf{f}}{\partial \mathbf{x}}}] as a dense matrix can be performed in the following way:_
_[For some hints on adjoint derivatives, their calculation and usage, and an example, see the corresponding https://modelica.github.io/fmi-guides/main/fmi-guide/#adjointDerivatives[section in the implementers' guide].]_
0 commit comments