# Why does SystemModelSimulate not simulate a model when parameters and inputs are changed within a single association?

A support case with the identification [CASE:4265480] was created.

I am a bit confused about the following behavior in Mathematica 12.0 (Windows 64) simulating a very simple Modelica model using SystemModelSimulate.

Here is a simple model that has an array of inputs u whose dimension is given by a parameter n. The output simply is the sum of these inputs:

modelString = "model Model
input Real[n] u;
output Real y;
parameter Integer n(min = 1) = 2;
equation
y = sum(u);
end Model;";

model = ImportString[ modelString, "mo" ];



Now we simulate the model with n = 2 for the time interval {0,2} given two simple, identical time series as input:

sim = SystemModelSimulate[ model, {"y"}, {0, 2},
Association[
"ParameterValues" -> {
"n" -> 2
},
"Inputs" -> {
"u[1]" -> TimeSeries[{-1, 0, 1}, {0, 2}],
"u[2]" -> TimeSeries[{-1, 0, 1}, {0, 2}]
}
]
];
SystemModelPlot[ sim, {"y"} ]


All fine. But let's add another identical input, i.e. n = 3:

sim = SystemModelSimulate[ model, {"y"}, {0, 2},
Association[
"ParameterValues" -> {
"n" -> 3
},
"Inputs" -> {
"u[1]" -> TimeSeries[{-1, 0, 1}, {0, 2}],
"u[2]" -> TimeSeries[{-1, 0, 1}, {0, 2}],
"u[3]" -> TimeSeries[{-1, 0, 1}, {0, 2}]
}
]
];


I am getting an error message SystemModelSimulate::val:

The variables {u[3]} were not recognized, or the types do not match the model.

What is going on?

EDIT:

I have reported the issue to WRI (see above) as the example will work perfectly fine in System Modeler. Modelica to my knowledge definitely allows to have the dimension of an array of inputs depend upon a parameter. SystemModelSimulate[] should compile the model using the modified parameter value and then make use of the given inputs. The error remains unclear to me.

UPDATE:

It looks like the issue is a more general problem. It seems, that one cannot change "ParameterValues" and "Inputs" at the same time using SystemModelSimulate. Not good news for using VerificationTest to write test notebooks in Mathematica.

• Change your modelString =  to indicate n=3, perhaps? – LouisB May 25 '19 at 5:04
• @LouisB Indeed that works out. But why? n is a parameter and can be set from Mathematica to modify simulations (e.g. do a parameter sweep) according to docs. – gwr May 25 '19 at 5:39

I have just gotten an answer from Wolfram Research and indeed it is currently not possible to change structural ParameterValues (cf. Malte‘s comment below) and Inputs within a single association using SystemModelSimulate.

## Work-Around

The work-around is to use an intermediate model with changed parameter values:

sim = With[
{ intermediateModel = SystemModel[ model, Association[ "ParameterValues" -> {"n" -> 3} ] ] }
,
SystemModelSimulate[ intermediateModel, {"y"}, {0, 2},
Association[
"Inputs" -> {
"u[1]" -> TimeSeries[{-1, 0, 1}, {0, 2}],
"u[2]" -> TimeSeries[{-1, 0, 1}, {0, 2}],
"u[3]" -> TimeSeries[{-1, 0, 1}, {0, 2}]}
]
]
];

SystemModelPlot[ sim, {"y"} ]


Something like this should in my opinion be integrated into the way SystemModelSimulate goes about things and thus I am happy to read this sentence in the answer I got from support:

I have forwarded a suggestion report to our developers so that something similar to the above could be done behind the scene.

• It is possible to change both "ParameterValues" and "Inputs" within a single association using SystemModelSimulate. What is not currently possible is changing what is often informally called "structural parameters". These are parameters that change the structure of the model (such as dimensions of arrays) or have to be evaluated to their literal values by the Modelica compiler for other reasons. For these cases, an intermediate model has to be created. – Malte Lenz Jun 10 '19 at 8:05
• @Malte Thank you for that clarification; I have updated my post for completeness. – gwr Jun 10 '19 at 10:36