Skip to content

Commit

Permalink
Prefer tearing variables with start value for initialization (#256)
Browse files Browse the repository at this point in the history
* Prefer tearing variables with start value for initialization
* Deactivate preOptModule removeEqualFunctionCalls
* Fix IndexReduction.addStateOrderFinder
* Disable two unstable optimization test cases
* Update expected output
  • Loading branch information
lochel committed Jun 19, 2019
1 parent 55e5d89 commit 20f03ff
Show file tree
Hide file tree
Showing 56 changed files with 1,687 additions and 2,246 deletions.
11 changes: 5 additions & 6 deletions OMCompiler/Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -203,7 +203,7 @@ public function varUnreplaceable "author: lochel
output Boolean outUnreplaceable = inVar.unreplaceable;
end varUnreplaceable;

public function setVarUnreplaceable "author: arun
public function setVarUnreplaceable "author: arun
sets the unreplaceable attribute of a variable to be false or true"
input BackendDAE.Var inVar;
input Boolean value;
Expand Down Expand Up @@ -490,14 +490,13 @@ end varHasStateDerivative;

public function setStateDerivative "author: Frenkel TUD
sets the state derivative."
input BackendDAE.Var inVar;
input Option<DAE.ComponentRef> dcr;
output BackendDAE.Var outVar = inVar;
input output BackendDAE.Var var;
input Option<DAE.ComponentRef> derName;
protected
Integer index;
algorithm
BackendDAE.STATE(index=index) := inVar.varKind;
outVar.varKind := BackendDAE.STATE(index, dcr);
BackendDAE.STATE(index=index) := var.varKind;
var.varKind := BackendDAE.STATE(index, derName);
end setStateDerivative;

public function getVariableAttributefromType
Expand Down
39 changes: 18 additions & 21 deletions OMCompiler/Compiler/BackEnd/IndexReduction.mo
Expand Up @@ -4178,32 +4178,29 @@ protected function addStateOrderFinder
input BackendDAE.Variables inVars;
output BackendDAE.Variables oVars;
algorithm
oVars := match(iVlst,iDerVlst,inVars)
oVars := match(iVlst,iDerVlst)
local
DAE.ComponentRef cr,dcr;
DAE.ComponentRef dcr;
BackendDAE.Var var,dvar;
list<BackendDAE.Var> vlst,dvlst;
BackendDAE.Variables vars;
String msg;
case ({},_,_) then inVars;
case ((var as BackendDAE.VAR(varKind=BackendDAE.STATE(derName=NONE())))::vlst,
BackendDAE.VAR(varName=dcr)::dvlst,_)
equation
var = BackendVariable.setStateDerivative(var,SOME(dcr));
vars = BackendVariable.addVar(var,inVars);
then
addStateOrderFinder(vlst,dvlst,vars);
case(var::_,dvar::_,_)
equation
msg = "IndexReduction.addStateOrderFinder failed for " + BackendDump.varString(var) + " with derivative " + BackendDump.varString(dvar) + "\n";
Error.addMessage(Error.INTERNAL_ERROR, {msg});
then
fail();
else
equation
Error.addMessage(Error.INTERNAL_ERROR, {"IndexReduction.addStateOrderFinder failed!"});
then
fail();

case ({}, _) then inVars;

case ((var as BackendDAE.VAR(varKind=BackendDAE.STATE()))::vlst, BackendDAE.VAR(varName=dcr)::dvlst) equation
var = BackendVariable.setStateDerivative(var, SOME(dcr));
vars = BackendVariable.addVar(var, inVars);
then addStateOrderFinder(vlst, dvlst, vars);

case (var::_, dvar::_) equation
msg = "IndexReduction.addStateOrderFinder failed for " + BackendDump.varString(var) + " with derivative " + BackendDump.varString(dvar) + "\n";
Error.addMessage(Error.INTERNAL_ERROR, {msg});
then fail();

else equation
Error.addMessage(Error.INTERNAL_ERROR, {"IndexReduction.addStateOrderFinder failed!"});
then fail();
end match;
end addStateOrderFinder;

Expand Down
3 changes: 1 addition & 2 deletions OMCompiler/Compiler/Util/Flags.mo
Expand Up @@ -798,7 +798,6 @@ constant ConfigFlag PRE_OPT_MODULES = CONFIG_FLAG(12, "preOptModules",
"evaluateParameters",
"simplifyIfEquations",
"expandDerOperator",
"removeEqualFunctionCalls",
"clockPartitioning",
"findStateOrder",
"replaceEdgeChange",
Expand Down Expand Up @@ -1371,7 +1370,7 @@ constant ConfigFlag DYNAMIC_TEARING_FOR_INITIALIZATION = CONFIG_FLAG(104, "dynam
NONE(), EXTERNAL(), BOOL_FLAG(false), NONE(),
Util.gettext("Enable Dynamic Tearing also for the initialization system."));
constant ConfigFlag PREFER_TVARS_WITH_START_VALUE = CONFIG_FLAG(105, "preferTVarsWithStartValue",
NONE(), EXTERNAL(), BOOL_FLAG(false), NONE(),
NONE(), EXTERNAL(), BOOL_FLAG(true), NONE(),
Util.gettext("Prefer tearing variables with start value for initialization."));
constant ConfigFlag EQUATIONS_PER_FILE = CONFIG_FLAG(106, "equationsPerFile",
NONE(), EXTERNAL(), INT_FLAG(2000), NONE(),
Expand Down
1,230 changes: 455 additions & 775 deletions testsuite/openmodelica/cruntime/debugDumps/testDumpEvents.mos

Large diffs are not rendered by default.

19 changes: 9 additions & 10 deletions testsuite/openmodelica/cruntime/optimization/basic/Makefile
Expand Up @@ -38,8 +38,6 @@ testAlgLoop1.mos \
testAlgLoop2.mos \
testAlgLoop3.mos \
testAlgLoop4.mos \
testAlgLoop5.mos \
testAlgLoop6.mos \
testAlgLoop7.mos \
testAlgLoop8.mos \
testAlgLoop9.mos \
Expand All @@ -58,12 +56,13 @@ TFCtestFlag.mos \
TT.mos \


# test that currently fail. Move up when fixed.
# test that currently fail. Move up when fixed.
# Run make testfailing
FAILINGTESTFILES = \
TT2.mos \
TT3.mos \

testAlgLoop5.mos \
testAlgLoop6.mos \

# Dependency files that are not .mo .mos or Makefile
# Add them here or they will be cleaned.
Expand All @@ -84,8 +83,8 @@ test:
@echo
@echo OPENMODELICAHOME=" $(OPENMODELICAHOME) "
@$(TEST) $(TESTFILES)
# Cleans all files that are not listed as dependencies

# Cleans all files that are not listed as dependencies
clean :
@echo $(DEPENDENCIES) | sed 's/ /\\|/g' > deps.tmp
@rm -f $(CLEAN)
Expand All @@ -94,11 +93,11 @@ clean :
# do it after cleaning and updating the folder
# then you can get a list of file names (which must be dependencies
# since you got them from repository + your own new files)
# then add them to the DEPENDENCIES. You can find the
# list in deps.txt
getdeps:
# then add them to the DEPENDENCIES. You can find the
# list in deps.txt
getdeps:
@echo $(DEPENDENCIES) | sed 's/ /\\|/g' > deps.tmp
@echo $(CLEAN) | sed -r 's/deps.txt|deps.tmp//g' | sed 's/ / \\\n/g' > deps.txt
@echo $(CLEAN) | sed -r 's/deps.txt|deps.tmp//g' | sed 's/ / \\\n/g' > deps.txt
@echo Dependency list saved in deps.txt.
@echo Copy the list from deps.txt and add it to the Makefile @DEPENDENCIES

Expand Down
40 changes: 20 additions & 20 deletions testsuite/openmodelica/cruntime/optimization/basic/pendel.mos
Expand Up @@ -17,7 +17,7 @@ model pendel

Real x(start=x0,min=0,max=1);
Real y(start=y0, fixed=true, min=0,max=1);

Real vx(start=0);
Real vy(start=0,min=-1,max=0.0);
Real cost(nominal=100) =( cos(Fx)-sin(Fy))*sin(time^2) annotation(isLagrange = true);
Expand Down Expand Up @@ -47,8 +47,8 @@ echo(true);
res.resultFile;
if regexBool(messages, "EXIT: Optimal Solution Found.\n") then
print("Optimal Solution Found!\n");
if regexBool(messages,"Number of Iterations....: 64") then
print("Number of Iterations....: 64\n");
if regexBool(messages,"Number of Iterations....: 91") then
print("Number of Iterations....: 91\n");
else
print("wrong Number of Iterations! Update test\n");
end if;
Expand Down Expand Up @@ -90,26 +90,26 @@ val(Fx,5.0);
// true
// "pendel_res.mat"
// Optimal Solution Found!
// Number of Iterations....: 64
// Number of Iterations....: 91
//
// ""
// ""
// -0.0
// -1.659802327078357
// -0.1810650803865342
// -0.8129041257192998
// -0.06888096353801151
// -0.03166509723098989
// 0.4999999993334863
// 0.4999999833331176
// 0.4999999666607621
// 0.4999999501537176
// 0.4999999336917155
// 0.4999999180081858
// -3.30761299558503
// -3.307611589086046
// -5.0
// -3.307611465132354
// -1.659802327071079
// -0.1810650803223208
// -0.8129041257070627
// -0.06888096344942475
// -0.03166506452173983
// 0.4999999993337779
// 0.4999999833403194
// 0.4999999666751017
// 0.4999999501796328
// 0.4999999337320414
// 0.4999999182288091
// -3.307612995835737
// -3.307611589112298
// -5.0
// -4.999999953525733
// -3.307611465222483
// -4.999999999999887
// -4.999999953525426
// endResult
Expand Up @@ -46,7 +46,7 @@ readSimulationResultSize("testModel_res.mat");
// LOG_SUCCESS | info | The simulation finished successfully.
// "
// end SimulationResult;
// 692
// 630
// record SimulationResult
// resultFile = "testModel_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 2.0, numberOfIntervals = 20, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'testModel', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = '-noEventEmit'",
Expand Down
Expand Up @@ -55,31 +55,31 @@ readSimulationResultSize("testModel_res.mat");
// LOG_SUCCESS | info | The simulation finished successfully.
// "
// end SimulationResult;
// 211
// 124
// record SimulationResult
// resultFile = "testModel_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 2.0, numberOfIntervals = 20, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'testModel', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = '-noEquidistantTimeGrid -noEventEmit'",
// messages = "LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// LOG_SUCCESS | info | The simulation finished successfully.
// "
// end SimulationResult;
// 186
// 107
// record SimulationResult
// resultFile = "testModel_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 2.0, numberOfIntervals = 20, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'testModel', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = '-noEquidistantTimeGrid -maxStepSize=0.001'",
// messages = "LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// LOG_SUCCESS | info | The simulation finished successfully.
// "
// end SimulationResult;
// 2134
// 2076
// record SimulationResult
// resultFile = "testModel_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 2.0, numberOfIntervals = 20, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'testModel', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = '-noEquidistantTimeGrid -maxIntegrationOrder=1'",
// messages = "LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// LOG_SUCCESS | info | The simulation finished successfully.
// "
// end SimulationResult;
// 6691
// 6419
// record SimulationResult
// resultFile = "testModel_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 0.4, numberOfIntervals = 20, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'testModel', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = '-noEquidistantTimeGrid'",
Expand Down
Expand Up @@ -46,7 +46,7 @@ readSimulationResultSize("testModel_res.mat");
// LOG_SUCCESS | info | The simulation finished successfully.
// "
// end SimulationResult;
// 602
// 566
// record SimulationResult
// resultFile = "testModel_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 2.0, numberOfIntervals = 20, tolerance = 1e-06, method = 'euler', fileNamePrefix = 'testModel', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = '-noEventEmit'",
Expand Down
Expand Up @@ -46,7 +46,7 @@ readSimulationResultSize("testModel_res.mat");
// LOG_SUCCESS | info | The simulation finished successfully.
// "
// end SimulationResult;
// 584
// 558
// record SimulationResult
// resultFile = "testModel_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 3.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'rungekutta', fileNamePrefix = 'testModel', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = '-noEventEmit'",
Expand Down
40 changes: 0 additions & 40 deletions testsuite/openmodelica/debugDumps/lateInline.mos
Expand Up @@ -145,46 +145,6 @@ buildModel(testOptdaedump); getErrorString();
//
//
// ########################################
// pre-optimization module removeEqualFunctionCalls (simulation)
// ########################################
//
//
// unknown partition
// ========================================
//
// Variables (2)
// ========================================
// 1: y:VARIABLE() type: Real
// 2: x:VARIABLE() type: Real
//
//
// Equations (2, 2)
// ========================================
// 1/1 (1): x = testOptdaedump.f(time) [binding |0|0|0|0|]
// 2/2 (1): y = exp(time ^ 3.0) + x [binding |0|0|0|0|]
//
//
// Incidence Matrix (row: equation)
// ========================================
// number of rows: 2
// 1: 2
// 2: 2 1
//
// Transposed Incidence Matrix (row: variable)
// ========================================
// number of rows: 2
// 1: 2
// 2: 2 1
//
// no matching
//
//
//
// BackendDAEType: simulation
//
//
//
// ########################################
// pre-optimization module clockPartitioning (simulation)
// ########################################
//
Expand Down

0 comments on commit 20f03ff

Please sign in to comment.