- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » Problems with redeclarable package...
Problems with redeclarable package and functions
Problems with redeclarable package and functions
I am encountering some strange behavior regarding a redeclarable package and its use in a model. When I want to declare a parameter and set its value to the result of one of the functions, this does not work if the input parameter is a parameter itself.
Take a look at the following simple model, declaring a partial package, a concrete package and a model using this package.
Code:
package BasePackage
replaceable partial function func
input Real value;
output Real result;
end func;
end BasePackage;
package ConcretePackage
extends BasePackage;
redeclare function extends func
algorithm
result := value;
end func;
end ConcretePackage;
model TestModel
replaceable package Function = BasePackage; // Does not work
//replaceable package Function = ConcretePackage; // This way it works
final parameter Real value = 1;
parameter Real result1 = Function.func(value);
parameter Real result2 = Function.func(1);
Real x;
equation
x = Function.func(time + 1);
end TestModel;
model Test
TestModel mod(redeclare package Function = ConcretePackage);
equation
end Test;
If I simulate this, result1 will be 0, while result2 1 and x will have the correct values. However, if I instead directly refer to the ConcretePackage (commented line), the code does work and result1 also has value 1.
Is this an error or intendended behavior?
Best regards,
Michael
Re: Problems with redeclarable package and functions
Hi,
This is clearly a bug somewhere in the simulation runtime I think.
I'm running this script:
Code:
loadFile("RedeclarePackage.mo"); getErrorString();
instantiateModel(Test); getErrorString();
simulate(Test); getErrorString();
"mod.result1@0";
val(mod.result1, 0);
"mod.result2@0";
val(mod.result2, 0);
"mod.value@0";
val(mod.value, 0);
Code:
adrpo@ida-liu050 ~/dev/OpenModelica/build/bin/mburisch
$ ../omc RedeclarePackage.mos
true
""
"function TestModel.Function.func
input Real value;
output Real result;
algorithm
result := value;
end TestModel.Function.func;
class Test
parameter Real mod.value = 1.0;
parameter Real mod.result2 = 1.0;
Real mod.x;
parameter Real mod.result1 = TestModel.Function.func(mod.value);
equation
mod.x = TestModel.Function.func(1.0 + time);
end Test;
"
""
record SimulationResult
resultFile = "c:/bin/cygwin/home/adrpo/dev/OpenModelica/build/bin/mburisch/Test_res.mat",
simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 500, tolerance = 1e-006, method = 'dassl', fileNamePrefix = 'Test', storeInTemp = false, noClean = false, options = '', outputFormat = 'mat', variableFilter = '.*', measureTime = false, cflags = ''",
messages = "",
timeFrontend = 0.00878583210949665,
timeBackend = 0.00225044799159258,
timeSimCode = 0.00803632437093511,
timeTemplates = 0.00941215418994843,
timeCompile = 1.5899761520265,
timeSimulation = 0.0866534113765805,
timeTotal = 1.70516167155239
end SimulationResult;
""
"mod.result1@0"
0.0
"mod.result2@0"
1.0
"mod.value@0"
1.0
The flattened model looks OK.
However the mod.result1 at time 0 should be 1.
The dependent parameters are calculated via a function in the generated c code called bound_parameters and the call to the function mod.result1 = TestModel.Function.func(mod.value) is there, but somehow the result is not propagated to mod1.result1.
If you run Test.exe with debug flags you get this:
Code:
adrpo@ida-liu050 ~/dev/OpenModelica/build/bin/mburisch
$ ./Test.exe -lv LOG_DEBUG
read start = 0 from init file.
read stop = 1 from init file.
read stepSize = 0.002 from init file.
read tolerance = 1e-006 from init file.
read method = dassl from init file.
read outputFormat = mat from init file.
read variableFilter = .* from init file.
read $dummy = 0 from init file.
read der($dummy) = 0 from init file.
read mod.x = 0 from init file.
read mod.value = 1 from init file.
read mod.result2 = 1 from init file.
read mod.result1 = 0 from init file.
Read parameter data from file Test_init.xml
| info LOG_EVENTS | Number of sorted, unique sample events: 0
initDelay called with startTime = 0.000000
Allocated simulation result data storage for method 'mat' and file='Test_res.mat'
Recognized solver: dassl.
| info LOG_SOLVER | Calculated bound parameters
....
so the function to calculate bound parameters is called, but somehow the result read via the _init.xml file "read mod.result1 = 0 from init file" is kept.
I'll add a bug about this.
Cheers,
Adrian Pop/
- adrpo
- 885 Posts
Re: Problems with redeclarable package and functions
I found the bug and also added a bug report about it here:
https://openmodelica.org:8443/cb/issue/1652
so it should be fixed in the near future.
Basically, during the simulation the values of the parameters are fine,
but we only write them to the result file once and we do that before
bound_parameters function is called so the value of the parameters
in the result file will always be the start value.
Cheers,
Adrian Pop/
- adrpo
- 885 Posts
Re: Problems with redeclarable package and functions
Hi,
This was fixed in revision: 10562.
If you're on Windows you will need a new build (let me know if you need one).
If you're on Linux then you can svn up and compile or wait for a new build package.
Cheers,
Adrian Pop/
- adrpo
- 885 Posts
- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » Problems with redeclarable package...