Skip to content

Commit

Permalink
[NB] adding simplification of when conditions (#11858)
Browse files Browse the repository at this point in the history
- adding CombiTimeTable test (not yet running)
  • Loading branch information
kabdelhak committed Jan 23, 2024
1 parent d3aac72 commit b1b7812
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
27 changes: 26 additions & 1 deletion OMCompiler/Compiler/NBackEnd/Classes/NBEquation.mo
Expand Up @@ -1283,7 +1283,9 @@ public
case ALGORITHM() then eq;
case IF_EQUATION() then eq;
case FOR_EQUATION() then eq;
case WHEN_EQUATION() then eq;
case WHEN_EQUATION() algorithm
eq.body := WhenEquationBody.simplify(eq.body, name, indent);
then eq;
case AUX_EQUATION() then eq;
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for: " + Equation.toString(eq)});
Expand Down Expand Up @@ -2467,6 +2469,29 @@ public
bodies := listReverse(bodies);
end split;

function simplify
input output WhenEquationBody body;
input String name = "";
input String indent = "";
algorithm
// simplify condition if it is an array with surplus false conditions
body.condition := match body.condition
local
Expression condition;
list<Expression> conditions;
case condition as Expression.ARRAY() algorithm
conditions := list(elem for elem guard(not Expression.isFalse(elem)) in arrayList(condition.elements));
body.condition := Expression.makeArrayCheckLiteral(Type.ARRAY(Type.BOOLEAN(), {Dimension.fromInteger(listLength(conditions))}), listArray(conditions));
then body.condition;
else body.condition;
end match;

// ToDo: add simplification of body! (WhenStatements)

body.condition := SimplifyExp.simplifyDump(body.condition, true, name, indent);
body.else_when := Util.applyOption(body.else_when, function simplify(name = name, indent = indent));
end simplify;

protected
function collectForSplit
"collects all discrete states and regular states for splitting up
Expand Down
@@ -0,0 +1,27 @@
// name: ScalableTestSuite.Elementary.Tables.Models.CombiTimeTable
// keywords: NewBackend
// status: correct
// cflags: --newBackend

loadModel(ScalableTestSuite); getErrorString();

simulate(ScalableTestSuite.Elementary.Tables.Models.CombiTimeTable); getErrorString();

res := OpenModelica.Scripting.compareSimulationResults("ScalableTestSuite.Elementary.Tables.Models.CombiTimeTable_res.mat",
"ReferenceFiles/ScalableTestSuite.Elementary.Tables.Models.CombiTimeTable_res.mat",
"ScalableTestSuite.Elementary.Tables.Models.CombiTimeTable_diff.csv",0.01,0.0001,
{"combiTimeTable.y[1]"});

// Result:
// true
// ""
// record SimulationResult
// resultFile = "ScalableTestSuite.Elementary.Tables.Models.CombiTimeTable_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 1, tolerance = 1e-6, method = 'dassl', fileNamePrefix = 'ScalableTestSuite.Elementary.Tables.Models.CombiTimeTable', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
// messages = "LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// LOG_SUCCESS | info | The simulation finished successfully.
// "
// end SimulationResult;
// ""
// {"Files Equal!"}
// endResult
Expand Up @@ -27,6 +27,7 @@ SimpleAdvection.mos \
# test that currently fail. Move up when fixed.
# Run make failingtest
FAILINGTESTFILES = \
CombiTimeTable.mos \
TransmissionLineEquations.mos \

# Dependency files that are not .mo .mos or Makefile
Expand Down
Binary file not shown.

0 comments on commit b1b7812

Please sign in to comment.