Skip to content

Commit

Permalink
Don't use matchcontinue for tearing traverseComponents1
Browse files Browse the repository at this point in the history
  - Issue explicit error when giving illegal value for maxSizeNonlinearTearing or maxSizeLinearTearing
  • Loading branch information
AnHeuermann authored and AnHeuermann committed Nov 27, 2020
1 parent 67bec2f commit b616440
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 169 deletions.
142 changes: 84 additions & 58 deletions OMCompiler/Compiler/BackEnd/Tearing.mo
Expand Up @@ -257,6 +257,16 @@ algorithm
if Flags.isSet(Flags.TEARING_DUMPVERBOSE) then
print("\n" + BORDER + "\nBEGINNING of traverseComponents\n\n");
end if;

// Check if maxSizeLinearTearing maxSizeNonlinearTearing flag is illegal
if (Flags.getConfigInt(Flags.MAX_SIZE_LINEAR_TEARING) < 0) then
Error.addMessage(Error.INVALID_FLAG_TYPE, {"maxSizeLinearTearing", "non-negative integer", intString(Flags.getConfigInt(Flags.MAX_SIZE_LINEAR_TEARING))});
fail();
elseif (Flags.getConfigInt(Flags.MAX_SIZE_NONLINEAR_TEARING) < 0) then
Error.addMessage(Error.INVALID_FLAG_TYPE, {"maxSizeNonlinearTearing", "non-negative integer", intString(Flags.getConfigInt(Flags.MAX_SIZE_NONLINEAR_TEARING))});
fail();
end if;

(comps, b, strongComponentIndex) := traverseComponents(comps, isyst, inShared, inTearingMethod, strongComponentIndex);
if Flags.isSet(Flags.TEARING_DUMPVERBOSE) then
print("\nEND of traverseComponents\n" + BORDER + "\n\n");
Expand Down Expand Up @@ -309,80 +319,96 @@ algorithm
else strongComponentIndexOut;
end match;

(oComp, outRunMatching) := matchcontinue (inComp, isyst, ishared, inMethod)
(oComp, outRunMatching) := match (inComp, isyst, ishared, inMethod)
local
Integer maxSize;
list<Integer> eindex, vindx;
Boolean b, b1;
BackendDAE.StrongComponents comps, acc;
BackendDAE.StrongComponent comp, comp1;
Option<list<tuple<Integer, Integer, BackendDAE.Equation>>> ojac;
BackendDAE.JacobianType jacType;
Boolean mixedSystem;
Integer maxSize;
Boolean isLinear, useTearing;

case ((BackendDAE.EQUATIONSYSTEM(eqns=eindex, vars=vindx, jac=BackendDAE.FULL_JACOBIAN(ojac), jacType=jacType, mixedSystem=mixedSystem)), _, _, _) equation
true = BackendDAEUtil.getLinearfromJacType(jacType);
maxSize = Flags.getConfigInt(Flags.MAX_SIZE_LINEAR_TEARING);
if intGt(listLength(vindx), maxSize) and not
// always apply tearing if maxSize > 0 and dense matrices are used
(intGt(maxSize, 0) and stringEqual(Config.simCodeTarget(), "Cpp") and
stringEqual(Flags.getConfigString(Flags.MATRIX_FORMAT), "dense"))
then
Error.addMessage(Error.MAX_TEARING_SIZE, {intString(strongComponentIndexOut), intString(listLength(vindx)),"linear",intString(maxSize)});
fail();
// Tearing
case ((BackendDAE.EQUATIONSYSTEM(eqns=eindex, vars=vindx, jac=BackendDAE.FULL_JACOBIAN(ojac), jacType=jacType, mixedSystem=mixedSystem)), _, _, _) algorithm
isLinear := BackendDAEUtil.getLinearfromJacType(jacType);
if isLinear then
maxSize := Flags.getConfigInt(Flags.MAX_SIZE_LINEAR_TEARING);
else
maxSize := Flags.getConfigInt(Flags.MAX_SIZE_NONLINEAR_TEARING);
end if;
if listMember(strongComponentIndexOut,Flags.getConfigIntList(Flags.NO_TEARING_FOR_COMPONENT)) then

useTearing := checkTearingSettings(maxSize, isLinear, strongComponentIndexOut, listLength(vindx));
if useTearing then
if debugFlag then
print("\nTearing deactivated by user.\n");
print("\nTearing of " + (if isLinear then "LINEAR" else "NONLINEAR") + " component\n" +
"Use Flag '-d=tearingdumpV' and '-d=iterationVars' for more details\n\n");
end if;
Error.addMessage(Error.NO_TEARING_FOR_COMPONENT, {intString(strongComponentIndexOut)});
fail();
end if;
if debugFlag then
print("\nTearing of LINEAR component\nUse Flag '-d=tearingdumpV' and '-d=iterationVars' for more details\n\n");
end if;
if Flags.isSet(Flags.TEARING_DUMPVERBOSE) then
print("Jacobian:\n" + BackendDump.dumpJacobianStr(ojac) + "\n\n");
end if;
if debug then execStat("Tearing.traverseComponents1 linear start"); end if;
(comp1, true) = callTearingMethod(inMethod, isyst, ishared, eindex, vindx, ojac, jacType, mixedSystem, strongComponentIndexOut);
then (comp1, true);

// tearing of non-linear systems
case ((BackendDAE.EQUATIONSYSTEM(eqns=eindex, vars=vindx, jac=BackendDAE.FULL_JACOBIAN(ojac), jacType=jacType, mixedSystem=mixedSystem)), _, _, _) equation
false = BackendDAEUtil.getLinearfromJacType(jacType);
maxSize = Flags.getConfigInt(Flags.MAX_SIZE_NONLINEAR_TEARING);
if intGt(listLength(vindx), maxSize) and not
// always apply tearing if maxSize > 0 and dense matrices are used
(intGt(maxSize, 0) and stringEqual(Config.simCodeTarget(), "Cpp") and
stringEqual(Flags.getConfigString(Flags.MATRIX_FORMAT), "dense"))
then
Error.addMessage(Error.MAX_TEARING_SIZE, {intString(strongComponentIndexOut), intString(listLength(vindx)),"nonlinear",intString(maxSize)});
fail();
end if;
if listMember(strongComponentIndexOut,Flags.getConfigIntList(Flags.NO_TEARING_FOR_COMPONENT)) then
if debugFlag then
print("\nTearing deactivated by user.\n");
if Flags.isSet(Flags.TEARING_DUMPVERBOSE) then
print("Jacobian:\n" + BackendDump.dumpJacobianStr(ojac) + "\n\n");
end if;
Error.addMessage(Error.NO_TEARING_FOR_COMPONENT, {intString(strongComponentIndexOut)});
fail();
end if;
if debugFlag then
print("\nTearing of NONLINEAR component\nUse Flag '-d=tearingdumpV' and '-d=iterationVars' for more details\n\n");
end if;
if Flags.isSet(Flags.TEARING_DUMPVERBOSE) then
print("Jacobian:\n" + BackendDump.dumpJacobianStr(ojac) + "\n\n");
if debug then
execStat("Tearing.traverseComponents1 " + (if isLinear then "LS" else "NLS") + " start");
end if;
try
oComp := callTearingMethod(inMethod, isyst, ishared, eindex, vindx, ojac, jacType, mixedSystem, strongComponentIndexOut);
outRunMatching := true;
else
oComp := inComp;
outRunMatching := false;
end try;
else
oComp := inComp;
outRunMatching := false;
end if;
if debug then execStat("Tearing.traverseComponents1 NLS start"); end if;
(comp1, true) = callTearingMethod(inMethod, isyst, ishared, eindex, vindx, ojac, jacType, mixedSystem, strongComponentIndexOut);
then (comp1, true);
then(oComp, outRunMatching);

// no component for tearing
else (inComp, false);
end matchcontinue;
else then(inComp, false);
end match;
end traverseComponents1;


protected function checkTearingSettings
"Checks if we want to do tearing for the current component.
It will also issue optional maesages if not."
input Integer maxSize;
input Boolean isLinear;
input Integer strongComponentIndex;
input Integer numVars;
output Boolean activateTearing=false;
protected
Boolean debugFlag = Flags.isSet(Flags.TEARING_DUMP) or Flags.isSet(Flags.TEARING_DUMPVERBOSE);
Boolean forcedTearing;
Boolean isCpp;
Boolean isDense;
algorithm

// Check if tearing is disabled (maxSize=0)
if maxSize == 0 then
return;
end if;

// Check if (numVars < maxSize) or (isCpp and isDense)
isCpp := stringEqual(Config.simCodeTarget(), "Cpp");
isDense := stringEqual(Flags.getConfigString(Flags.MATRIX_FORMAT), "dense");
forcedTearing := isCpp and isDense;
if numVars > maxSize and not forcedTearing then
Error.addMessage(Error.MAX_TEARING_SIZE, {intString(strongComponentIndex), intString(numVars), (if isLinear then "linear" else "nonlinear"),intString(maxSize)});
return;
end if;

// Check if tearing is disabled for this component
if listMember(strongComponentIndex,Flags.getConfigIntList(Flags.NO_TEARING_FOR_COMPONENT)) then
if debugFlag then
print("\nTearing deactivated by user.\n");
end if;
Error.addMessage(Error.NO_TEARING_FOR_COMPONENT, {intString(strongComponentIndex)});
return;
end if;

activateTearing := true;
end checkTearingSettings;

protected function getUserTearingSet
input list<Integer> userTVars;
input list<Integer> userResiduals;
Expand Down
6 changes: 1 addition & 5 deletions testsuite/openmodelica/debugDumps/dumpSparsePatternLin.mos
Expand Up @@ -76,9 +76,5 @@ buildModel(problem3); getErrorString();
// The following (1) independents belong to one color
// 1: {x[2]}
// {"problem3","problem3_init.xml"}
// "Notification: Tearing is skipped for strong component 1 because system size of 2 exceeds maximum system size for tearing of linear systems (0).
// To adjust the maximum system size for tearing use --maxSizeLinearTearing=<size> and --maxSizeNonlinearTearing=<size>.
// Notification: Tearing is skipped for strong component 2 because system size of 2 exceeds maximum system size for tearing of linear systems (0).
// To adjust the maximum system size for tearing use --maxSizeLinearTearing=<size> and --maxSizeNonlinearTearing=<size>.
// "
// ""
// endResult
4 changes: 1 addition & 3 deletions testsuite/simulation/modelica/daemode/testDAEScaling.mos
Expand Up @@ -57,7 +57,5 @@ simulate(Test3, stopTime = 2, simflags="-noEquidistantTimeGrid");getErrorString(
// LOG_SUCCESS | info | The simulation finished successfully.
// "
// end SimulationResult;
// "Notification: Tearing is skipped for strong component 1 because system size of 199 exceeds maximum system size for tearing of nonlinear systems (0).
// To adjust the maximum system size for tearing use --maxSizeLinearTearing=<size> and --maxSizeNonlinearTearing=<size>.
// "
// ""
// endResult
6 changes: 1 addition & 5 deletions testsuite/simulation/modelica/linear_system/NPendulum.mos
Expand Up @@ -134,11 +134,7 @@ res := OpenModelica.Scripting.compareSimulationResults("NPendulum.pendulum_res.m
// "Warning: 'compareSimulationResults' is deprecated. It is recommended to use 'diffSimulationResults' instead.
// "
// {"NPendulum.pendulum","NPendulum.pendulum_init.xml"}
// "Notification: Tearing is skipped for strong component 1 because system size of 278 exceeds maximum system size for tearing of linear systems (0).
// To adjust the maximum system size for tearing use --maxSizeLinearTearing=<size> and --maxSizeNonlinearTearing=<size>.
// Notification: Tearing is skipped for strong component 2 because system size of 275 exceeds maximum system size for tearing of linear systems (0).
// To adjust the maximum system size for tearing use --maxSizeLinearTearing=<size> and --maxSizeNonlinearTearing=<size>.
// "
// ""
// 0
// "stdout | info | Maximum system size for using linear sparse solver changed to 4001
// LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
Expand Down
8 changes: 1 addition & 7 deletions testsuite/simulation/modelica/linear_system/NPendulum40.mos
Expand Up @@ -30,13 +30,7 @@ res := OpenModelica.Scripting.compareSimulationResults("NPendulum.pendulum40_res
// true
// true
// {"NPendulum.pendulum40","NPendulum.pendulum40_init.xml"}
// "Notification: Tearing is skipped for strong component 1 because system size of 1178 exceeds maximum system size for tearing of linear systems (0).
// To adjust the maximum system size for tearing use --maxSizeLinearTearing=<size> and --maxSizeNonlinearTearing=<size>.
// Notification: Tearing is skipped for strong component 2 because system size of 1175 exceeds maximum system size for tearing of linear systems (0).
// To adjust the maximum system size for tearing use --maxSizeLinearTearing=<size> and --maxSizeNonlinearTearing=<size>.
// Notification: Tearing is skipped for strong component 3 because system size of 1175 exceeds maximum system size for tearing of linear systems (0).
// To adjust the maximum system size for tearing use --maxSizeLinearTearing=<size> and --maxSizeNonlinearTearing=<size>.
// "
// ""
// stdout | info | Maximum system size for using linear sparse solver changed to 4001
// LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// LOG_SUCCESS | info | The simulation finished successfully.
Expand Down
Expand Up @@ -3,7 +3,7 @@
// status: correct
// teardown_command: rm -rf linear_system.problem3* output.log
// cflags: -d=-newInst
//
//
// Solving of a linear system of equations with analytical jacobian

loadFile("linearTestPackage.mo"); getErrorString();
Expand Down Expand Up @@ -64,11 +64,7 @@ val(x[2], 1.0);
// LOG_SUCCESS | info | The simulation finished successfully.
// "
// end SimulationResult;
// "Notification: Tearing is skipped for strong component 1 because system size of 2 exceeds maximum system size for tearing of linear systems (0).
// To adjust the maximum system size for tearing use --maxSizeLinearTearing=<size> and --maxSizeNonlinearTearing=<size>.
// Notification: Tearing is skipped for strong component 2 because system size of 2 exceeds maximum system size for tearing of linear systems (0).
// To adjust the maximum system size for tearing use --maxSizeLinearTearing=<size> and --maxSizeNonlinearTearing=<size>.
// "
// ""
// 10.0
// 9.980736544657136
// 1.0
Expand All @@ -80,11 +76,7 @@ val(x[2], 1.0);
// LOG_SUCCESS | info | The simulation finished successfully.
// "
// end SimulationResult;
// "Notification: Tearing is skipped for strong component 1 because system size of 2 exceeds maximum system size for tearing of linear systems (0).
// To adjust the maximum system size for tearing use --maxSizeLinearTearing=<size> and --maxSizeNonlinearTearing=<size>.
// Notification: Tearing is skipped for strong component 2 because system size of 2 exceeds maximum system size for tearing of linear systems (0).
// To adjust the maximum system size for tearing use --maxSizeLinearTearing=<size> and --maxSizeNonlinearTearing=<size>.
// "
// ""
// 10.0
// 9.980736544657136
// 0.9999999999999999
Expand All @@ -96,11 +88,7 @@ val(x[2], 1.0);
// LOG_SUCCESS | info | The simulation finished successfully.
// "
// end SimulationResult;
// "Notification: Tearing is skipped for strong component 1 because system size of 2 exceeds maximum system size for tearing of linear systems (0).
// To adjust the maximum system size for tearing use --maxSizeLinearTearing=<size> and --maxSizeNonlinearTearing=<size>.
// Notification: Tearing is skipped for strong component 2 because system size of 2 exceeds maximum system size for tearing of linear systems (0).
// To adjust the maximum system size for tearing use --maxSizeLinearTearing=<size> and --maxSizeNonlinearTearing=<size>.
// "
// ""
// 10.0
// 9.980736544657136
// 1.0
Expand All @@ -112,11 +100,7 @@ val(x[2], 1.0);
// LOG_SUCCESS | info | The simulation finished successfully.
// "
// end SimulationResult;
// "Notification: Tearing is skipped for strong component 1 because system size of 2 exceeds maximum system size for tearing of linear systems (0).
// To adjust the maximum system size for tearing use --maxSizeLinearTearing=<size> and --maxSizeNonlinearTearing=<size>.
// Notification: Tearing is skipped for strong component 2 because system size of 2 exceeds maximum system size for tearing of linear systems (0).
// To adjust the maximum system size for tearing use --maxSizeLinearTearing=<size> and --maxSizeNonlinearTearing=<size>.
// "
// ""
// 9.999999999999998
// 9.980736544657136
// 1.0
Expand All @@ -128,11 +112,7 @@ val(x[2], 1.0);
// LOG_SUCCESS | info | The simulation finished successfully.
// "
// end SimulationResult;
// "Notification: Tearing is skipped for strong component 1 because system size of 2 exceeds maximum system size for tearing of linear systems (0).
// To adjust the maximum system size for tearing use --maxSizeLinearTearing=<size> and --maxSizeNonlinearTearing=<size>.
// Notification: Tearing is skipped for strong component 2 because system size of 2 exceeds maximum system size for tearing of linear systems (0).
// To adjust the maximum system size for tearing use --maxSizeLinearTearing=<size> and --maxSizeNonlinearTearing=<size>.
// "
// ""
// 10.0
// 9.980736544657136
// 1.0
Expand All @@ -144,11 +124,7 @@ val(x[2], 1.0);
// LOG_SUCCESS | info | The simulation finished successfully.
// "
// end SimulationResult;
// "Notification: Tearing is skipped for strong component 1 because system size of 2 exceeds maximum system size for tearing of linear systems (0).
// To adjust the maximum system size for tearing use --maxSizeLinearTearing=<size> and --maxSizeNonlinearTearing=<size>.
// Notification: Tearing is skipped for strong component 2 because system size of 2 exceeds maximum system size for tearing of linear systems (0).
// To adjust the maximum system size for tearing use --maxSizeLinearTearing=<size> and --maxSizeNonlinearTearing=<size>.
// "
// ""
// 9.999999999999998
// 9.980736544657136
// 1.0
Expand Down

0 comments on commit b616440

Please sign in to comment.