Content uploaded by Atiyah M. G. Elsheikh
Author content
All content in this area was uploaded by Atiyah M. G. Elsheikh on Feb 05, 2019
Content may be subject to copyright.
Outline
●First testing experiences
–Mathematical algorithms in Modelica
●Unit testing in Modelica
–existing technologies
●A bit more “formal” Unit testing
–Upgrading to Physical Modeling
Motivation
“Simulation tools not only perform numerical
solutions based on the system equations but
also assist the modeler in systems analysis.
Doubtlessly the most important systems analysis
tool is Sensitivity Analysis ...
Wiechert, W., Noack, S., and Elsheikh, A. (2010).
Modeling languages for biochemical network
simulation: Reaction vs equation based approaches.
Advances in Biochemical Engineering / Biotechnology
31/05/18 4
SA of Modelica Models
Modelica model
F
( ˙
x , x , p , t
)=
0
x
(
0
)=
x
0
(
p
)
x
0
x
(
t
)
p
simulate
Dynamic Parameter
Sensitivities (DPS)
∂
x
∂
p
(
t
)
∂
x
∂
x
0
(
t
)
31/05/18 5
ADModelica (2007)
OMC
Compiler
XML
Modelica
XML
Parser
AnalyzerVisualizer
Unparser
ADModelica
31/05/18 6
Sample of generated code
(I/II)
•Declaration part
31/05/18 7
Sample of generated code
(II/II)
•Equation part
31/05/18 8
Library L
Component C1
Component C2
Component C3
Library L'
Component C'1
Component C'2
Component C'3
AD
Algorithmic Differentiation (AD)
of Modelica libraries (2011)
extend
31/05/18 9
Algorithmic differentiation Modelica Libraries
Algorithmically differentiated
Modelica library for biochmeical
reaction networks
https://github.com/modelica-3rdparty/ADGenKinetics
The ADGenKinetics Library The ADMSL Library
Serves as an example of
algorithmically differentiated
Modelica library
https://github.com/AIT-CES-LAB/ADMSL
2011 2014
31/05/18 10
Chua Circuit
Standard Modelica model for an electrical circuit
Modelica.Electrical.Analog.Basic.Examples.ChuaCircuit
This examples simulates
the current and voltage
at all components
31/05/18 11
Chua Circuit importing ADMSL
The sensitivities of the current w.r.t. L
∂X.i
∂L.L
31/05/18 12
Chua Circuit importing ADMSL
The sensitivities of the voltage at L
w.r.t. all parameters
∂VL
∂X.p
Applications of DPS
1) Modeling-Orietned
Control Coefficients, Local SA, Parameter Sweeping Studies,
Model Simplification, ...
2) Statistical
Regression Analysis, Global SA, Identifiability Analysis, ...
3) Optimization
Cost-functions expressed in terms of DPS
Atiyah Elsheikh and Sergei Kucherenko. (2019)
Dynamic parameter sensitivities: Summary of applications – version 1.0.
Technical Report, to appear
31/05/18 14
Several snapshots at different time points
Scaled Parameter Sensitivities
CC
p
x
=
p
x
(
t
)
∂
x
∂
p
(
t
)
31/05/18 15
Correlation among parameters
Based on Fischer Information Matrices
Strongly
Correlated
Parameters
Correlation among parameters
31/05/18 16
Testing AD
∂
x
∂
p
j
(
t , p
)=
x
(
t , p
+
e
j
δ
j
)−
x
(
t , p
)
δ
j
+
O
(
δ
j
)
δ
j
=
ϵ
p
j
Finite Difference Methods
1)Evaluate DPS with FD
externally with Matlab
small models
2) Simulate AD models
3) Check if the curves behave similarly
Sometimes compute Relative errors
4)Investigate cause of errors, if any
Adhoc Testing (2007-2014)
PSTools Library
●Promotes the usage of PS at Modelica level
●Serves as utility package for arbitrary Modelica libraries
●PS Package
–Extensive set of examples for analytical derivatives
●Including hybrid systems
–Generic Models for advanced FD
–Second-order DPS
●Tools Package
–Taylor series approximation
–Parameter Sweeping studies
–Control Coefficients
–...
Under Development (2019)
Highly Experimental
Unit Testing is must
FD with PSTools
model ParM
extends Utilities.Parameterized(
NP = 2,
_P = {0.4, 0.5},
PNAME = {"p1", "p2"},
NX = 2,
_X = {_M.x1, _M.x2},
XNAME = {"x1", "x2"}
);
protected
MyModel _M(
p1 = _P[1],
p2 = _P[2]);
end ParM;
model FDParM
PSTools.PS.FD.CD2
PS(redeclare replaceable
Model ParModel = ParM);
end FDParM;
Parameter Sweeping Studies
Jan Peter Axelsson & Atiyah Elsheikh
Example of Sensitivity Analysis with the Bioprocess Library
Modprod 2019
Unit Testing Technologies for Modelica
●CSV Compare – ESI ITI
●BuildingPy – LBNL
●PySimulator – DLR
●test.openmodelica.org
●XogenyTest – Xogeny
Marco Kessler, Testing Tutorial
Dassault Systems, Modelica 2017 Prague (Link)
●Model Management –
Dymola
●Testing tool kits (many
Companies)
●Testing Library --
Dymola
XogenyTest
●Pure Modelica
●Minimal
●external scripting
●log files
● Easy to use
● No dependencies
Michael Tiller
https://github.com/xogeny/XogenyTest
Tiller, Michael M., and Burit Kittirungsi.
"UnitTesting: A Library for Modelica Unit Testing"
Modelica Conference Vienna, Austria, 2006
Not meant to be
comprehensive but
definitely an ideal
getting start
XogenyTest Low level functions
asserting whether
an expected value lies within
-neighbourhood of a reference
value (i.e. )
within the same order of
magnitude
x
ϵ
N
ϵ
(
r
)
r
|
x
−
r
|
< ϵ
or
|
log
x
r
|
< ϵ
XogenyTest
Models
asserting an expected
value w.r.t. a reference
value at time
or at time
x
r
T∈{t0,tf}
|
x
(
T
)−
r
|
< ϵ
t∈(t0, t f)
|
x
(
t
)−
r
|
< ϵ
XogenyTest
Models
asserting an expected
event occurs at time
Asserting a trajectory at
discrete time points
w.r.t. reference values
|
x
(
t
i
)−
r
(
t
i
)
|
<ϵ
r
(
t
i
)
e
(
t
)
T
e
([
T
−ϵ
,T
+ϵ])→
True
x
(
t
)
{
t
1,
t
2
,
...
, t
N
}
Examples (I/II)
model CheckSuccess
Real x = time^2;
AssertTrajectory check_x(
actual=x,
expected=[0,0; 1,1; 2,4; 3,9]);
Annotation(
TestCase(
action="simulate",
result="success"),
experiment(StopTime=4));
end CheckSuccess;
Examples (II/II)
model CheckSuccess
Real x = time;
AssertBecomesTrueAt
check_event(event=(x>2), at=2);
Annotation(
TestCase(
action="simulate",
result="success"),
experiment(StopTime=4));
end CheckSuccess;
XogenyTest
Models
Assert thata signal
is of an average value
over
Asset that difference between
trajectories and
Is less than accu. error
1
t
f
−
t
0
|
∫
t
0
t
f
u
(
t
)
dt
|
−
a
<ϵ
u
(
t
)
a
[
t
0
,t
f
]
x
1
(
t
)
x
2
(
t
)
A
err
∫
t
0
t
f
|
x
1
(
t
)−
x
2
(
t
)
|
dt
<
A
err
Testing AD vs. FD
model TestT1BioProcess
import PSTools.Utilities.unitVector;
Utilities.Validate dmu_dKs(
AccErr=1E-1,
name="test T1.Processions dmu / dKs");
PSTools.PS.T1.BioProcess.Bioprocess PSAD(
NG=1,
g_Ks=unitVector(1, NG));
PSTools.PS.FD.BioProcess.BioProcess PSFD;
...
equation
dmu_dKs.T1 = PSAD.g_mu[1];
dmu_dKs.T2 = PSFD.g_mu[1];
end TestT1BioProcess;
Visual unit testing
Utilities.Validate
T1
T2
Check
Case Study 1 PSTools Library
// der(VS) = -qS * VX;
g_VS[i] = -g_qS[i] * VX
- qS * g_VX[i];
UT_TestT1_FAIL
// der(VS) = -qS * VX;
der(g_VS[i]) = -g_qS[i] * VX
- qS * g_VX[i];
UT_TestT1_WORK
GenKinetics
2018 R 1.0-alpha
v=∏a
KAa+ [ Aa]
KAa
⋅∏b
KIb
KIb+ [ Ib]⋅
Vmax
fwd ∏i
[Si]
KmS i
−Vmax
bwd ∏j
[Pj]
KmP j
Vmax
fwd ∏i
1+[Si]
KmS i
+Vmax
bwd ∏j
1+[Pj]
KmP j
−1
Formal unit testing procedure
1) a unit test model for each component C using
- Example of a small model employing C
- Equivalent model implemented only by equations
2) Execute all unit tests by *.mos & OMShell
- after each significant modification
3) Investigate errors , if any
Most useful when the component checks but
the UT does not translate or simulate
A library is shipped with its unit tests
All unit tests are within a package called UnitTests
Currently about 40 unit tests distributed GenKinetics
Case Study 2 ADGenKinetics Library
A = product({KA[i] / (KA[i] + mc_A[i].c) for i in 1:NA});
OMC 1.12.0
A = product({ (KA .+ mc_A.c) ./ KA for i in 1:NA});
OMC 1.13.0
2012
2019
Case Study 3 GenKinetics / Biochem
Libraries
der(c) = if
(c < tolerance) then 0 else r_net;
Numerical Problem
der(c) = if
(c < tolerance and r_net < 0) then 0 else r_net;
Solution
Outlook
●a user experience report from GenKinetics
●Combine Relative Error w. Absolute Error
●Continuous Integration Solution
Asghar, Adeel, Andreas Pfeiffer, Arunkumar Palanisamy, Alachew Mengist,
Martin Sjölund, Adrian Pop, and Peter Fritzson.
"Automatic Regression Testing of Simulation Models and Concept for
Simulation of Connected FMUs in PySimulator."
InProceedings of the 11th International Modelica Conference, Versailles. 2015.