Skip to content

Commit

Permalink
- add postoptimization module to replace constants in the system
Browse files Browse the repository at this point in the history
  - used for Modelica.Electrical.Spice3.Examples
- switch off eqequation to alias 
   - ThermoSysPro.Examples.SimpleExamples.TestJunctions4.mos fails because source.Q(start = 500.0) ,  source.Q(start = 100.0), sourceP.Q the same
   - PlanarMechanicsForTesting.Examples.SingleTrackWithEngine.mos - stateselection
   - PlanarMechanicsForTesting.Examples.SimpleCarWithDifferentialGear.mos 
- rename ERROR -> ERROR0 to avoid warning

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@13733 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Oct 31, 2012
1 parent f7770b1 commit 832bc5e
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 34 deletions.
6 changes: 3 additions & 3 deletions Compiler/BackEnd/BackendDAECreate.mo
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,10 @@ algorithm
// effort variable equality equations, seperated to generate alias variables
case (DAE.EQUEQUATION(cr1 = _),_,_,_,_,_,_,_,_,_,_,_,_)
equation
//eqns = lowerEqn(inElement,functionTree,inEqnsLst);
eqns = inEqnsLst;
eqns = lowerEqn(inElement,functionTree,inEqnsLst);
//eqns = inEqnsLst;
then
(inVars,inKnVars,inExVars,eqns,inREqnsLst,inIEqnsLst,inConstraintLst,inClassAttributeLst,inWhenClauseLst,inExtObjClasses,inElement::iAliaseqns);
(inVars,inKnVars,inExVars,eqns,inREqnsLst,inIEqnsLst,inConstraintLst,inClassAttributeLst,inWhenClauseLst,inExtObjClasses,/*inElement::*/iAliaseqns);

// a solved equation
case (DAE.DEFINE(componentRef = _),_,_,_,_,_,_,_,_,_,_,_,_)
Expand Down
90 changes: 90 additions & 0 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Original file line number Diff line number Diff line change
Expand Up @@ -12895,6 +12895,96 @@ algorithm
end traverserExpinputDerivativesUsed;


/*
* remove constants stuff
*
*/
public function removeConstants
"function: removeConstants
autor Frenkel TUD"
input BackendDAE.BackendDAE dae;
output BackendDAE.BackendDAE odae;
algorithm
odae := match (dae)
local
DAE.FunctionTree funcs;
BackendDAE.Variables knvars,exobj,av;
BackendDAE.EquationArray remeqns,inieqns;
array<DAE.Constraint> constrs;
array<DAE.ClassAttributes> clsAttrs;
Env.Cache cache;
Env.Env env;
BackendDAE.EventInfo einfo;
BackendDAE.ExternalObjectClasses eoc;
BackendDAE.SymbolicJacobians symjacs;
BackendVarTransform.VariableReplacements repl;
BackendDAE.BackendDAEType btp;
BackendDAE.EqSystems systs;
list<BackendDAE.Equation> lsteqns;
Boolean b;
case (BackendDAE.DAE(systs,BackendDAE.SHARED(knvars,exobj,av,inieqns,remeqns,constrs,clsAttrs,cache,env,funcs,einfo,eoc,btp,symjacs)))
equation
repl = BackendVarTransform.emptyReplacements();
repl = BackendVariable.traverseBackendDAEVars(knvars,removeConstantsFinder,repl);
Debug.fcall(Flags.DUMP_CONST_REPL, BackendVarTransform.dumpReplacements, repl);
(knvars,(repl,_)) = BackendVariable.traverseBackendDAEVarsWithUpdate(knvars,replaceFinalVarTraverser,(repl,0));
lsteqns = BackendDAEUtil.equationList(remeqns);
(lsteqns,b) = BackendVarTransform.replaceEquations(lsteqns, repl,NONE());
remeqns = Debug.bcallret1(b,BackendDAEUtil.listEquation,lsteqns,remeqns);
lsteqns = BackendDAEUtil.equationList(inieqns);
(lsteqns,b) = BackendVarTransform.replaceEquations(lsteqns, repl,NONE());
inieqns = Debug.bcallret1(b,BackendDAEUtil.listEquation,lsteqns,inieqns);
systs = List.map1(systs,removeConstantsWork,repl);
then
BackendDAE.DAE(systs,BackendDAE.SHARED(knvars,exobj,av,inieqns,remeqns,constrs,clsAttrs,cache,env,funcs,einfo,eoc,btp,symjacs));
end match;
end removeConstants;

protected function removeConstantsWork
"function: removeConstantsWork
autor Frenkel TUD"
input BackendDAE.EqSystem isyst;
input BackendVarTransform.VariableReplacements repl;
output BackendDAE.EqSystem osyst;
algorithm
osyst := match (isyst,repl)
local
BackendDAE.Variables vars;
BackendDAE.EquationArray eqns,eqns1;
list<BackendDAE.Equation> eqns_1,lsteqns;
Boolean b;
BackendDAE.EqSystem syst;
case (BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns),_)
equation
(vars,_) = BackendVariable.traverseBackendDAEVarsWithUpdate(vars,replaceFinalVarTraverser,(repl,0));
lsteqns = BackendDAEUtil.equationList(eqns);
(eqns_1,b) = BackendVarTransform.replaceEquations(lsteqns, repl,NONE());
eqns1 = Debug.bcallret1(b,BackendDAEUtil.listEquation,eqns_1,eqns);
syst = Util.if_(b,BackendDAE.EQSYSTEM(vars,eqns1,NONE(),NONE(),BackendDAE.NO_MATCHING()),isyst);
then
syst;
end match;
end removeConstantsWork;

protected function removeConstantsFinder
"autor: Frenkel TUD 2012-10"
input tuple<BackendDAE.Var, BackendVarTransform.VariableReplacements> inTpl;
output tuple<BackendDAE.Var, BackendVarTransform.VariableReplacements> outTpl;
algorithm
outTpl:=
matchcontinue (inTpl)
local
BackendDAE.Var v;
BackendVarTransform.VariableReplacements repl,repl_1;
DAE.ComponentRef varName;
DAE.Exp exp;
case ((v as BackendDAE.VAR(varName=varName,varKind=BackendDAE.CONST(),bindExp=SOME(exp)),repl))
equation
repl_1 = BackendVarTransform.addReplacement(repl, varName, exp,NONE());
then ((v,repl_1));
case _ then inTpl;
end matchcontinue;
end removeConstantsFinder;

/*
* optimize inital system
Expand Down
1 change: 1 addition & 0 deletions Compiler/BackEnd/BackendDAEUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -8896,6 +8896,7 @@ algorithm
(BackendDAEOptimize.simplifyTimeIndepFuncCalls,"simplifyTimeIndepFuncCalls",false),
(BackendDAEOptimize.inputDerivativesUsed,"inputDerivativesUsed",false),
(BackendDAEOptimize.simplifysemiLinear,"simplifysemiLinear",false),
(BackendDAEOptimize.removeConstants,"removeConstants",false),
(BackendDAEOptimize.optimizeInitialSystem,"optimizeInitialSystem",false),
(BackendDAEOptimize.detectSparsePatternODE,"detectJacobianSparsePattern",false)
};
Expand Down
12 changes: 9 additions & 3 deletions Compiler/Util/Flags.mo
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,9 @@ constant DebugFlag SCODE_INST_SHORTCUT = DEBUG_FLAG(105, "scodeInstShortcut",
Util.gettext("Enables experimental SCode instantiation shortcut phase."));
constant DebugFlag SHOW_SCODE = DEBUG_FLAG(106, "showSCode",
Util.gettext("Shows the SCode result of +d=scodeInstShortcut."));
constant DebugFlag DUMP_CONST_REPL = DEBUG_FLAG(107, "dumpConstrepl",
Util.gettext("dump the found replacements for constants"));


// This is a list of all debug flags, to keep track of which flags are used. A
// flag can not be used unless it's in this list, and the list is checked at
Expand Down Expand Up @@ -487,7 +490,8 @@ constant list<DebugFlag> allDebugFlags = {
DUMP_INITIAL_SYSTEM,
SOLVE_INITIAL_SYSTEM,
SCODE_INST_SHORTCUT,
SHOW_SCODE
SHOW_SCODE,
DUMP_CONST_REPL
};

// CONFIGURATION FLAGS
Expand Down Expand Up @@ -535,7 +539,7 @@ public constant ConfigFlag PRE_OPT_MODULES = CONFIG_FLAG(12, "preOptModules",
"evaluateParameters",
"simplifyIfEquations",
"removeEqualFunctionCalls",
"partitionIndependentBlocks",
// "partitionIndependentBlocks",
"expandDerOperator",
"inlineArrayEqn",
"removeSimpleEquations"
Expand Down Expand Up @@ -606,7 +610,8 @@ constant ConfigFlag POST_OPT_MODULES = CONFIG_FLAG(16, "postOptModules",
"removeUnusedFunctions",
"simplifyTimeIndepFuncCalls",
"inputDerivativesUsed",
// "detectJacobianSparsePattern",
// "detectJacobianSparsePattern",
"removeConstants",
"optimizeInitialSystem"
}),
SOME(STRING_DESC_OPTION({
Expand All @@ -630,6 +635,7 @@ constant ConfigFlag POST_OPT_MODULES = CONFIG_FLAG(16, "postOptModules",
("simplifyTimeIndepFuncCalls", Util.gettext("simplifies time independent built in function calls like pre(param) -> param, der(param) -> 0.0, change(param) -> false, edge(param) -> false")),
("inputDerivativesUsed", Util.gettext("checks if derivatives of inputs are need to calculate the model.")),
("simplifysemiLinear", Util.gettext("simplifies calls to semiLinear")),
("removeConstants", Util.gettext("remove all constants in the system")),
("optimizeInitialSystem", Util.gettext("simplifies time initial system")),
("detectJacobianSparsePattern", Util.gettext("detects the sparse pattern for Jacobian A"))
})),
Expand Down
56 changes: 28 additions & 28 deletions SimulationRuntime/c/util/omc_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3
* AND THIS OSMC PUBLIC LICENSE (OSMC-PL).
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3
* AND THIS OSMC PUBLIC LICENSE (OSMC-PL).
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
* ACCEPTANCE OF THE OSMC PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköping University, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
Expand Down Expand Up @@ -137,7 +137,7 @@ void Message(int type, int stream, char *msg, int subline);
#define WARNING9(stream, msg, a, b, c, d, e, f, g, h, i) do{sprintf(logBuffer, msg, a, b, c, d, e, f, g, h, i); Message(LOG_TYPE_WARNING, stream, logBuffer, 0);}while(0)
#define WARNING10(stream, msg, a, b, c, d, e, f, g, h, i, j) do{sprintf(logBuffer, msg, a, b, c, d, e, f, g, h, i, j); Message(LOG_TYPE_WARNING, stream, logBuffer, 0);}while(0)

#define ERROR(stream, msg) do{sprintf(logBuffer, msg); Message(LOG_TYPE_ERROR, stream, logBuffer, 0);}while(0)
#define ERROR0(stream, msg) do{sprintf(logBuffer, msg); Message(LOG_TYPE_ERROR, stream, logBuffer, 0);}while(0)
#define ERROR1(stream, msg, a) do{sprintf(logBuffer, msg, a); Message(LOG_TYPE_ERROR, stream, logBuffer, 0);}while(0)
#define ERROR2(stream, msg, a, b) do{sprintf(logBuffer, msg, a, b); Message(LOG_TYPE_ERROR, stream, logBuffer, 0);}while(0)
#define ERROR3(stream, msg, a, b, c) do{sprintf(logBuffer, msg, a, b, c); Message(LOG_TYPE_ERROR, stream, logBuffer, 0);}while(0)
Expand All @@ -162,17 +162,17 @@ void Message(int type, int stream, char *msg, int subline);
#define ASSERT9(exp, msg, a, b, c, d, e, f, g, h, i) do{if(!(exp)){sprintf(logBuffer, msg, a, b, c, d, e, f, g, h, i); Message(LOG_TYPE_ASSERT, LOG_ASSERT, logBuffer, 0); longjmp(globalJmpbuf, 1);}}while(0)
#define ASSERT10(exp, msg, a, b, c, d, e, f, g, h, i, j) do{if(!(exp)){sprintf(logBuffer, msg, a, b, c, d, e, f, g, h, i, j); Message(LOG_TYPE_ASSERT, LOG_ASSERT, logBuffer, 0); longjmp(globalJmpbuf, 1);}}while(0)
#else
#define ASSERT(exp, msg)
#define ASSERT1(exp, msg, a)
#define ASSERT2(exp, msg, a, b)
#define ASSERT3(exp, msg, a, b, c)
#define ASSERT4(exp, msg, a, b, c, d)
#define ASSERT5(exp, msg, a, b, c, d, e)
#define ASSERT6(exp, msg, a, b, c, d, e, f)
#define ASSERT7(exp, msg, a, b, c, d, e, f, g)
#define ASSERT8(exp, msg, a, b, c, d, e, f, g, h)
#define ASSERT9(exp, msg, a, b, c, d, e, f, g, h, i)
#define ASSERT10(exp, msg, a, b, c, d, e, f, g, h, i, j)
#define ASSERT(exp, msg)
#define ASSERT1(exp, msg, a)
#define ASSERT2(exp, msg, a, b)
#define ASSERT3(exp, msg, a, b, c)
#define ASSERT4(exp, msg, a, b, c, d)
#define ASSERT5(exp, msg, a, b, c, d, e)
#define ASSERT6(exp, msg, a, b, c, d, e, f)
#define ASSERT7(exp, msg, a, b, c, d, e, f, g)
#define ASSERT8(exp, msg, a, b, c, d, e, f, g, h)
#define ASSERT9(exp, msg, a, b, c, d, e, f, g, h, i)
#define ASSERT10(exp, msg, a, b, c, d, e, f, g, h, i, j)
#endif

#define THROW(msg) do{sprintf(logBuffer, msg); Message(LOG_TYPE_ASSERT, LOG_ASSERT, logBuffer, 0); longjmp(globalJmpbuf, 1);}while(0)
Expand Down Expand Up @@ -200,17 +200,17 @@ void Message(int type, int stream, char *msg, int subline);
#define DEBUG9(stream, msg, a, b, c, d, e, f, g, h, i) do{sprintf(logBuffer, msg, a, b, c, d, e, f, g, h, i); Message(LOG_TYPE_DEBUG, stream, logBuffer, 0);}while(0)
#define DEBUG10(stream, msg, a, b, c, d, e, f, g, h, i, j) do{sprintf(logBuffer, msg, a, b, c, d, e, f, g, h, i, j); Message(LOG_TYPE_DEBUG, stream, logBuffer, 0);}while(0)
#else
#define DEBUG(stream, msg)
#define DEBUG1(stream, msg, a)
#define DEBUG2(stream, msg, a, b)
#define DEBUG3(stream, msg, a, b, c)
#define DEBUG4(stream, msg, a, b, c, d)
#define DEBUG5(stream, msg, a, b, c, d, e)
#define DEBUG6(stream, msg, a, b, c, d, e, f)
#define DEBUG7(stream, msg, a, b, c, d, e, f, g)
#define DEBUG8(stream, msg, a, b, c, d, e, f, g, h)
#define DEBUG9(stream, msg, a, b, c, d, e, f, g, h, i)
#define DEBUG10(stream, msg, a, b, c, d, e, f, g, h, i, j)
#define DEBUG(stream, msg)
#define DEBUG1(stream, msg, a)
#define DEBUG2(stream, msg, a, b)
#define DEBUG3(stream, msg, a, b, c)
#define DEBUG4(stream, msg, a, b, c, d)
#define DEBUG5(stream, msg, a, b, c, d, e)
#define DEBUG6(stream, msg, a, b, c, d, e, f)
#define DEBUG7(stream, msg, a, b, c, d, e, f, g)
#define DEBUG8(stream, msg, a, b, c, d, e, f, g, h)
#define DEBUG9(stream, msg, a, b, c, d, e, f, g, h, i)
#define DEBUG10(stream, msg, a, b, c, d, e, f, g, h, i, j)
#endif

#ifdef __cplusplus
Expand Down

0 comments on commit 832bc5e

Please sign in to comment.