initial value propagation from sub-model
I have a model which sets initial values in a sub-model, but the initial values do not seem to propagate to the main model. The modia code (which is similar to my model) is:
@model SubModel begin
b = Float(start = 1.0)
end
@model MainModel begin
a = Float()
sMod = SubModel()
@equations begin
a = sMod.b
der(a) = 0.01
end
end
result = simulate(MainModel, 1.0)
which is simulated as:
Simulating model: MainModel
Number of equations: 2
Number of variables: 3
Number of continuous states: 1
... ModiaMath.simulate! (version 0.3.0 from 2018-12-04 11:57) to simulate model: MainModel
Dict{AbstractString,Any} with 4 entries:
"der(a)" => [0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01 … 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01]
"time" => [0.0, 0.001001, 0.002002, 0.003003, 0.004004, 0.00500501, 0.00600601, 0.00700701, 0.00800801, 0.00900901 … 0.990991, 0.991992, 0.992993, 0.993994, 0.994995, 0.995996, 0.996997, 0.997998, 0.998999, 1.0]
"a" => [0.0, 1.001e-5, 2.002e-5, 3.003e-5, 4.004e-5, 5.00501e-5, 6.00601e-5, 7.00701e-5, 8.00801e-5, 9.00901e-5 … 0.00990991, 0.00991992, 0.00992993, 0.00993994, 0.00994995, 0.00995996, 0.00996997, 0.00997998, 0.00998999, 0.01]
"sMod.b" => [0.0, 1.001e-5, 2.002e-5, 3.003e-5, 4.004e-5, 5.00501e-5, 6.00601e-5, 7.00701e-5, 8.00801e-5, 9.00901e-5 … 0.00990991, 0.00991992, 0.00992993, 0.00993994, 0.00994995, 0.00995996, 0.00996997, 0.00997998, 0.00998999, 0.01]
where a and sMod.b's initial values start from 0.0, which should start from 1.0? If I set 'start = 1.0' for a in MainModel instead of SubModel then it works fine.
The corresponding Modelica model seems to work with a and sMod.b start from 1.0:
package TestInitialValues
model SubModel
Real b(start=1.0);
end SubModel;
model MainModel
Real a;
SubModel sMod;
equation
a = sMod.b;
der(a) = 0.01;
end MainModel;
end TestInitialValues;
Could please give any suggestion? Thanks.
I don't know exactly but probably has something to do about the order the equations are processed internally. Furthermore, the real problem is not the propagation to the main model because if you create b the same way in the main model the problem remains. In your case, the initial values of b are ignored because b is not differentiable. I don't know if it's on purpose or a real issue...
Using your example but with only one model:
@model MainModel begin
a = Float()
b = Float(start=1.0)
@equations begin
a = b
der(a) = 0.01
end
end;
res = simulate(MainModel,1.0)
The result is the same you get:
Simulating model: MainModel
Number of equations: 2
Number of variables: 3
Number of continuous states: 1
... ModiaMath.simulate! (version 0.3.1-dev from 2019-01-5 10:13) to simulate model: MainModel
Dict{AbstractString,Any} with 4 entries:
"der(a)" => [0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01 … 0.01, 0.01, 0.01, 0.01, 0…
"time" => [0.0, 0.001001, 0.002002, 0.003003, 0.004004, 0.00500501, 0.00600601, 0.00700701, 0.00800…
"b" => [0.0, 1.001e-5, 2.002e-5, 3.003e-5, 4.004e-5, 5.00501e-5, 6.00601e-5, 7.00701e-5, 8.00801…
"a" => [0.0, 1.001e-5, 2.002e-5, 3.003e-5, 4.004e-5, 5.00501e-5, 6.00601e-5, 7.00701e-5, 8.00801…
But if you put b as differentiable, the result is the same you seek:
@model MainModel begin
a = Float()
b = Float(start=1.0)
@equations begin
a = b
der(b) = 0.01
end
end;
res = simulate(MainModel,1.0)
Simulating model: MainModel
Number of equations: 2
Number of variables: 3
Number of continuous states: 1
... ModiaMath.simulate! (version 0.3.1-dev from 2019-01-5 10:13) to simulate model: MainModel
Dict{AbstractString,Any} with 4 entries:
"time" => [0.0, 0.001001, 0.002002, 0.003003, 0.004004, 0.00500501, 0.00600601, 0.00700701, 0.00800…
"der(b)" => [0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01 … 0.01, 0.01, 0.01, 0.01, 0…
"b" => [1.0, 1.00001, 1.00002, 1.00003, 1.00004, 1.00005, 1.00006, 1.00007, 1.00008, 1.00009 … …
"a" => [1.0, 1.00001, 1.00002, 1.00003, 1.00004, 1.00005, 1.00006, 1.00007, 1.00008, 1.00009 … …
Again, I can't tell you if this behaviour is intentional or an issue, but for now this is the workaround I came up with...