Skip to content

Commit

Permalink
update discrete system before evaluating DAE (#12461)
Browse files Browse the repository at this point in the history
  • Loading branch information
arun3688 committed May 23, 2024
1 parent c8e35cd commit 8cc47a0
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ fmi2Status internalEventUpdate(fmi2Component c, fmi2EventInfo* eventInfo)
}
}

/* fix issue https://github.com/OpenModelica/OpenModelica/issues/12350
* we need to update discreteSystem during event update, before evaluating functionDAE
*/
updateDiscreteSystem(comp->fmuData, threadData);

comp->fmuData->callback->functionDAE(comp->fmuData, comp->threadData);

/* deactivate sample events */
Expand Down Expand Up @@ -333,10 +338,6 @@ fmi2Status internalEventUpdate(fmi2Component c, fmi2EventInfo* eventInfo)
* since the iteration seem not starting.
*/
storePreValues(comp->fmuData);
/* fix issue https://github.com/OpenModelica/OpenModelica/issues/12350
* we need to update discreteSystem during event update
*/
updateDiscreteSystem(comp->fmuData, threadData);
updateRelationsPre(comp->fmuData);

nextSampleEventDefined = getNextSampleTimeFMU(comp->fmuData, &nextSampleEvent);
Expand Down
75 changes: 54 additions & 21 deletions testsuite/omsimulator/whenTest.mos
Original file line number Diff line number Diff line change
@@ -1,39 +1,72 @@
// keywords: fmu export import
// status: correct
// teardown_command: rm -rf WhenTest.fmu WhenTest.log WhenTest_res.mat WhenTest_info.json WhenTest_systemCall.log/
// teardown_command: rm -rf WhenTest*/

loadString("
model WhenTest
Real s(start=0, fixed=true);
Integer stop(start=0, fixed=true);
equation
der(s) = if stop > 0 then 0 else 2;
// Variant A
stop = if s >= 1 then 1 else 0;
when pre(stop) > 0 then
reinit(s, 1);
end when;
annotation(
experiment(StartTime = 0, StopTime = 1, Tolerance = 1e-06, Interval = 0.002));
end WhenTest;
model WhenTestA
Real s(start=0, fixed=true);
Integer stop(start=0, fixed=true);
equation
der(s) = if stop > 0 then 0 else 2;
// Variant A
stop = if s >= 1 then 1 else 0;
when pre(stop) > 0 then
reinit(s, 1);
end when;
annotation(
experiment(StartTime = 0, StopTime = 1, Tolerance = 1e-06, Interval = 0.002));
end WhenTestA;

model WhenTestB
Real s(start=0, fixed=true);
Integer stop(start=0, fixed=true);
equation
der(s) = if stop > 0 then 0 else 2;
// Variant A
stop = if s >= 1 then 1 else 0;
when stop > 0 then
reinit(s, 1);
end when;
annotation(
experiment(StartTime = 0, StopTime = 1, Tolerance = 1e-06, Interval = 0.002));
end WhenTestB;
"); getErrorString();

buildModelFMU(WhenTest, version="2.0", fmuType="me", platforms={"static"}); getErrorString();
buildModelFMU(WhenTestA, version="2.0", fmuType="me", platforms={"static"}); getErrorString();

system(getInstallationDirectoryPath() + "/bin/OMSimulator WhenTestA.fmu -r=WhenTestA_res.mat", "WhenTestA_systemCall.log");
readFile("WhenTestA_systemCall.log");

val(der(s), 1.0, "WhenTestA_res.mat");
val(stop, 1.0, "WhenTestA_res.mat");

buildModelFMU(WhenTestB, version="2.0", fmuType="me", platforms={"static"}); getErrorString();

system(getInstallationDirectoryPath() + "/bin/OMSimulator WhenTest.fmu -r=WhenTest_res.mat", "WhenTest_systemCall.log");
readFile("WhenTest_systemCall.log");
system(getInstallationDirectoryPath() + "/bin/OMSimulator WhenTestB.fmu -r=WhenTestB_res.mat", "WhenTestB_systemCall.log");
readFile("WhenTestB_systemCall.log");

val(der(s), 1.0, "WhenTest_res.mat");
val(stop, 1.0, "WhenTest_res.mat");
val(der(s), 1.0, "WhenTestB_res.mat");
val(stop, 1.0, "WhenTestB_res.mat");

// Result:
// true
// ""
// "WhenTest.fmu"
// "WhenTestA.fmu"
// ""
// 0
// "info: maximum step size for 'model.root': 0.002000
// info: Result file: WhenTestA_res.mat (bufferSize=1)
// info: Final Statistics for 'model.root':
// NumSteps = 250 NumRhsEvals = 251 NumLinSolvSetups = 14
// NumNonlinSolvIters = 250 NumNonlinSolvConvFails = 0 NumErrTestFails = 0
// "
// 0.0
// 1.0
// "WhenTestB.fmu"
// ""
// 0
// "info: maximum step size for 'model.root': 0.002000
// info: Result file: WhenTest_res.mat (bufferSize=1)
// info: Result file: WhenTestB_res.mat (bufferSize=1)
// info: Final Statistics for 'model.root':
// NumSteps = 250 NumRhsEvals = 251 NumLinSolvSetups = 14
// NumNonlinSolvIters = 250 NumNonlinSolvConvFails = 0 NumErrTestFails = 0
Expand Down

0 comments on commit 8cc47a0

Please sign in to comment.