Skip to content

Commit

Permalink
Fix -d=evalOutputOnly (#9979)
Browse files Browse the repository at this point in the history
- The flag removes all equations that are unneeded for calculating
  all top level outputs.
- See #8271
  • Loading branch information
phannebohm committed Dec 22, 2022
1 parent 28ffbb1 commit fa321cf
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 31 deletions.
34 changes: 18 additions & 16 deletions OMCompiler/Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -3494,7 +3494,7 @@ end removeConstantsFinder;


// =============================================================================
// reaplace edge and change with (b and not pre(b)) and (v <> pre(v)
// reaplace edge and change with (b and not pre(b)) and (v <> pre(v))
//
// =============================================================================

Expand Down Expand Up @@ -5775,6 +5775,8 @@ protected
array<list<Integer>> mapEqnIncRow;
array<Integer> mapIncRowEqn;
Integer systemNumber=0, numberOfSystems;

constant Boolean debug = false;
algorithm
daeOut := daeIn;

Expand All @@ -5798,21 +5800,21 @@ algorithm
//get output variables
BackendDAE.EQSYSTEM(orderedVars = vars) := syst;
varLst := BackendVariable.varList(vars);
varLst := List.filterOnTrue(varLst,BackendVariable.isOutputVar);
varLst := List.filterOnTrue(varLst,BackendVariable.isVarOnTopLevelAndOutput);

if not listEmpty(varLst) then

//THIS SYSTEM CONTAINS OUTPUT VARIABLES
//-------------------------------------
outputVarIndxs := BackendVariable.getVarIndexFromVars(varLst,vars);
outputTasks := List.map(List.map1(outputVarIndxs,Array.getIndexFirst,varCompMapping),Util.tuple31);
//print("outputTasks "+stringDelimitList(List.map(outputTasks,intString),", ")+"\n");
if debug then print("outputTasks "+stringDelimitList(List.map(outputTasks,intString),", ")+"\n"); end if;

//get all necessary components to calculate the outputs
predecessors := HpcOmTaskGraph.getAllSuccessors(outputTasks,taskGraphT);
predecessors := List.sort(predecessors,intGt);
compsNew := List.map1(listAppend(outputTasks,predecessors),List.getIndexFirst,comps);
//print("predecessors of outputs "+stringDelimitList(List.map(predecessors,intString),", ")+"\n");
if debug then print("predecessors of outputs "+stringDelimitList(List.map(predecessors,intString),", ")+"\n"); end if;

//get equations from the new reduced set of comps
eqLstNew := BackendDAEUtil.getStrongComponentEquations(compsNew,eqs,vars);
Expand All @@ -5823,13 +5825,13 @@ algorithm
varVisited := arrayCreate(BackendVariable.varsSize(vars),-1);
while not listEmpty(eqLstNew) loop
eq::eqLstNew := eqLstNew;
//print("eq: "+BackendDump.equationString(eq)+"\n");
if debug then print("eq: "+BackendDump.equationString(eq)+"\n"); end if;
crefs := BackendEquation.equationCrefs(eq);
crefs := List.filter1OnTrue(crefs,BackendVariable.isState,vars);
(states,stateIndxs) := BackendVariable.getVarLst(crefs,vars);
(stateIndxs,states) := List.filter1OnTrueSync(stateIndxs,stateVarIsNotVisited,varVisited,states);//not yet visited
if not listEmpty(stateIndxs) then
//print("states "+stringDelimitList(List.map(states,BackendDump.varString),"\n ")+"\n");
if debug then print("states "+stringDelimitList(List.map(states,BackendDump.varString),"\n ")+"\n"); end if;
List.map2_0(stateIndxs,Array.updateIndexFirst,1,varVisited);
//add the new tasks which are necessary for the states
stateTasks1 := List.map(List.map1(stateIndxs,Array.getIndexFirst,varCompMapping),Util.tuple31);
Expand All @@ -5845,10 +5847,10 @@ algorithm
//get all necessary components to calculate the outputs and the state derivatives
predecessors := HpcOmTaskGraph.getAllSuccessors(listAppend(outputTasks,stateTasks),taskGraphT);
tasks := List.sort(listAppend(predecessors,listAppend(outputTasks,stateTasks)),intGt);
//print("predecessors of outputs and states "+stringDelimitList(List.map(tasks,intString),", ")+"\n");
if debug then print("predecessors of outputs and states "+stringDelimitList(List.map(tasks,intString),", ")+"\n"); end if;
compsNew := List.map1(tasks,List.getIndexFirst,comps);
compsNew := List.unique(compsNew);
print("There have been "+intString(listLength(comps))+" SCCs and now there are "+intString(listLength(compsNew))+" SCCs.\n");
if debug then print("There have been "+intString(listLength(comps))+" SCCs and now there are "+intString(listLength(compsNew))+" SCCs.\n"); end if;

//get vars and equations from the new reduced set of comps and make a equationIdxMap
eqLstNew := {};
Expand All @@ -5863,8 +5865,8 @@ algorithm
syst.orderedVars := BackendVariable.listVar1(listReverse(varLstNew));
syst.orderedEqs := BackendEquation.listEquation(listReverse(eqLstNew));

syst.m :=NONE();
syst.mT :=NONE();
syst.m := NONE();
syst.mT := NONE();
syst.matching := BackendDAE.NO_MATCHING();
(m,mT) := BackendDAEUtil.adjacencyMatrix(syst,BackendDAE.NORMAL(),NONE(),BackendDAEUtil.isInitializationDAE(shared));
syst.m := SOME(m);
Expand All @@ -5882,16 +5884,16 @@ algorithm
(syst, _, _, mapEqnIncRow, mapIncRowEqn) := BackendDAEUtil.getAdjacencyMatrixScalar(syst, BackendDAE.NORMAL(), SOME(funcTree), BackendDAEUtil.isInitializationDAE(shared));
syst := BackendDAETransform.strongComponentsScalar(syst,shared,mapEqnIncRow,mapIncRowEqn);
syst.removedEqs := BackendEquation.emptyEqns();

systsNew := syst::systsNew;
else
Error.addCompilerNotification("No output variables in this system ("+String(systemNumber)+"/"+String(numberOfSystems)+")");
if debug then print("No output variables in this system ("+intString(systemNumber)+"/"+intString(numberOfSystems)+")\n"); end if;
end if;

systsNew := syst::systsNew;
end for;

//alias vars are not necessary anymore
shared.aliasVars := BackendVariable.emptyVars();
daeOut := BackendDAE.DAE(systsNew,shared);
// alias vars are not necessary anymore
shared.aliasVars := BackendVariable.emptyVars();
daeOut := BackendDAE.DAE(systsNew,shared);
end evaluateOutputsOnly;

protected function stateVarIsNotVisited"checks if the indexed entry in the array is less than 0"
Expand Down
1 change: 0 additions & 1 deletion OMCompiler/Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -7627,7 +7627,6 @@ algorithm
end if;
if Flags.isSet(Flags.EVAL_OUTPUT_ONLY) then
// prepare the equations
dae := BackendDAEOptimize.evaluateOutputsOnly(dae);
end if;
Expand Down
4 changes: 2 additions & 2 deletions OMCompiler/Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -491,7 +491,7 @@ algorithm
// create inline equations if present
if isSome(inInlineData) then
SOME(inlineData) := inInlineData;
(uniqueEqIndex, inlineEquationsTemp, _, _, , _, tempvars, _, _, _, _) :=
(uniqueEqIndex, inlineEquationsTemp, _, _, _, _, tempvars, _, _, _, _) :=
createEquationsForSystems(inlineData.inlineSystems,
shared, uniqueEqIndex, {}, tempvars, uniqueEqIndex,
SimCode.NO_MAPPING(), false);
Expand Down Expand Up @@ -1550,7 +1550,7 @@ algorithm
oequationsForZeroCrossings := Dangerous.listReverseInPlace(oequationsForZeroCrossings);
((ouniqueEqIndex, olocalKnownVars)) := BackendVariable.traverseBackendDAEVars(shared.localKnownVars, traverseKnVarsToSimEqSystem, (ouniqueEqIndex, {}));
else
Error.addInternalError("createEquationsForSystems failed", sourceInfo());
Error.addInternalError(getInstanceName() + " failed", sourceInfo());
fail();
end try;
end createEquationsForSystems;
Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/Util/Flags.mo
Expand Up @@ -430,7 +430,7 @@ constant DebugFlag DUMP_RTEARING = DEBUG_FLAG(128, "dumpRecursiveTearing", false
constant DebugFlag DIS_SYMJAC_FMI20 = DEBUG_FLAG(129, "disableDirectionalDerivatives", true,
Gettext.gettext("For FMI 2.0 only dependecy analysis will be perform."));
constant DebugFlag EVAL_OUTPUT_ONLY = DEBUG_FLAG(130, "evalOutputOnly", false,
Gettext.gettext("Generates equations to calculate outputs only."));
Gettext.gettext("Generates equations to calculate top level outputs only."));
constant DebugFlag HARDCODED_START_VALUES = DEBUG_FLAG(131, "hardcodedStartValues", false,
Gettext.gettext("Embed the start values of variables and parameters into the c++ code and do not read it from xml file."));
constant DebugFlag DUMP_FUNCTIONS = DEBUG_FLAG(132, "dumpFunctions", false,
Expand Down
8 changes: 4 additions & 4 deletions testsuite/simulation/modelica/linear_system/Ticket3926.mos
Expand Up @@ -18,10 +18,10 @@ end foo;
");getErrorString();

setCommandLineOptions("--initOptModules-=calculateStrongComponentJacobians");
simulate(foo);getErrorString();
simulate(foo);getErrorString();

val(p1, 0.0);getErrorString();
val(p2, 0.0);getErrorString();
val(p1, 0.0);getErrorString();
val(p2, 0.0);getErrorString();
val(p3, 0.0);getErrorString();
val(p4, 0.0);getErrorString();
val(q1, 0.0);getErrorString();
Expand Down Expand Up @@ -54,7 +54,7 @@ val(q2, 0.0);getErrorString();
// "Error: A torn linear system has no symbolic jacobian and currently there are no means to solve that numerically. Please compile with the module \"calculateStrongComponentJacobians\" to provide symbolic jacobians for torn linear systems.
// [SimCode/SimCodeUtil.mo:0:0-0:0:writable] Error: Internal error function createOdeSystem failed for component torn linear Equationsystem{{{1:1}},
// {2:2} Size: 1
// [SimCode/SimCodeUtil.mo:0:0-0:0:writable] Error: Internal error createEquationsForSystems failed
// [SimCode/SimCodeUtil.mo:0:0-0:0:writable] Error: Internal error SimCodeUtil.createEquationsForSystems failed
// [SimCode/SimCodeUtil.mo:0:0-0:0:writable] Error: Internal error function createSimCode failed [Transformation from optimised DAE to simulation code structure failed]
// "
//
Expand Down
3 changes: 1 addition & 2 deletions testsuite/simulation/modelica/others/EngineV6_output.mos
Expand Up @@ -8,7 +8,7 @@ loadFile("EngineV6_output.mo");getErrorString();
setDebugFlags("evalOutputOnly"); getErrorString();
setCommandLineOptions("--maxSizeLineartearing=4000"); getErrorString();
simulate(EngineV6_output); getErrorString();

val(crankshaftSpeed,1.0); getErrorString();

// Result:
Expand All @@ -19,7 +19,6 @@ val(crankshaftSpeed,1.0); getErrorString();
// ""
// true
// ""
// There have been 1345 SCCs and now there are 686 SCCs.
// record SimulationResult
// resultFile = "EngineV6_output_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 1.01, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'EngineV6_output', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
Expand Down
6 changes: 1 addition & 5 deletions testsuite/simulation/modelica/parameters/Engine1a_output.mos
Expand Up @@ -13,16 +13,12 @@ simulate(Engine1a_output); getErrorString();
// ""
// true
// ""
// There have been 175 SCCs and now there are 66 SCCs.
// record SimulationResult
// resultFile = "Engine1a_output_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 5.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'Engine1a_output', 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;
// "Notification: No output variables in this system (2/4)
// Notification: No output variables in this system (3/4)
// Notification: No output variables in this system (4/4)
// "
// ""
// endResult

0 comments on commit fa321cf

Please sign in to comment.