Skip to content

Commit

Permalink
- added config flag for genaration of symbolic jacobian and lineariz…
Browse files Browse the repository at this point in the history
…ation

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@19347 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Willi Braun committed Feb 28, 2014
1 parent b63a956 commit 1519d75
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 66 deletions.
95 changes: 55 additions & 40 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -3095,50 +3095,65 @@ end generateInitialMatricesSparsityPattern;
public function generateSymbolicJacobianPast
input BackendDAE.BackendDAE inBackendDAE;
output BackendDAE.BackendDAE outBackendDAE;
protected
BackendDAE.EqSystems eqs;
BackendDAE.Shared shared;
BackendDAE.SymbolicJacobian symJacA;
BackendDAE.SparsePattern sparsePattern;
BackendDAE.SparseColoring sparseColoring;
DAE.FunctionTree funcs, functionTree;
algorithm
System.realtimeTick(GlobalScript.RT_CLOCK_EXECSTAT_JACOBIANS);
BackendDAE.DAE(eqs=eqs,shared=shared) := inBackendDAE;
(symJacA , sparsePattern, sparseColoring, funcs):= createSymbolicJacobianforStates(inBackendDAE);
shared := BackendDAEUtil.addBackendDAESharedJacobian(symJacA, sparsePattern, sparseColoring, shared);
functionTree := BackendDAEUtil.getFunctions(shared);
functionTree := DAEUtil.joinAvlTrees(functionTree, funcs);
shared := BackendDAEUtil.addFunctionTree(functionTree, shared);
outBackendDAE := BackendDAE.DAE(eqs,shared);
_ := System.realtimeTock(GlobalScript.RT_CLOCK_EXECSTAT_JACOBIANS);
algorithm
outBackendDAE := matchcontinue(inBackendDAE)
local
BackendDAE.EqSystems eqs;
BackendDAE.Shared shared;
BackendDAE.SymbolicJacobian symJacA;
BackendDAE.SparsePattern sparsePattern;
BackendDAE.SparseColoring sparseColoring;
DAE.FunctionTree funcs, functionTree;

case(_) equation
true = Flags.getConfigBool(Flags.GENERATE_SYMBOLIC_JACOBIAN);
System.realtimeTick(GlobalScript.RT_CLOCK_EXECSTAT_JACOBIANS);
BackendDAE.DAE(eqs=eqs,shared=shared) = inBackendDAE;
(symJacA , sparsePattern, sparseColoring, funcs) = createSymbolicJacobianforStates(inBackendDAE);
shared = BackendDAEUtil.addBackendDAESharedJacobian(symJacA, sparsePattern, sparseColoring, shared);
functionTree = BackendDAEUtil.getFunctions(shared);
functionTree = DAEUtil.joinAvlTrees(functionTree, funcs);
shared = BackendDAEUtil.addFunctionTree(functionTree, shared);
outBackendDAE = BackendDAE.DAE(eqs,shared);
_ = System.realtimeTock(GlobalScript.RT_CLOCK_EXECSTAT_JACOBIANS);
then outBackendDAE;

else then inBackendDAE;
end matchcontinue;
end generateSymbolicJacobianPast;

public function generateSymbolicLinearizationPast
input BackendDAE.BackendDAE inBackendDAE;
output BackendDAE.BackendDAE outBackendDAE;
protected
BackendDAE.EqSystems eqs;
BackendDAE.Shared shared;
BackendDAE.SymbolicJacobians linearModelMatrixes;
DAE.FunctionTree funcs, functionTree;
list< .DAE.Constraint> constraints;
BackendDAE.Variables v;
list<BackendDAE.Equation> eqns;
algorithm
System.realtimeTick(GlobalScript.RT_CLOCK_EXECSTAT_JACOBIANS);
BackendDAE.DAE(eqs=eqs,shared=shared) := inBackendDAE;
BackendDAE.SHARED(constraints=constraints) := shared;
eqns := {};
v := BackendVariable.emptyVars();
(v,eqns) := BackendDAECreate.addOptimizationVarsEqns2(constraints,1,v,eqns,false);
(linearModelMatrixes, funcs) := createLinearModelMatrixes(inBackendDAE, Config.acceptOptimicaGrammar(),v);
shared := BackendDAEUtil.addBackendDAESharedJacobians(linearModelMatrixes, shared);
functionTree := BackendDAEUtil.getFunctions(shared);
functionTree := DAEUtil.joinAvlTrees(functionTree, funcs);
shared := BackendDAEUtil.addFunctionTree(functionTree, shared);
outBackendDAE := BackendDAE.DAE(eqs,shared);
_ := System.realtimeTock(GlobalScript.RT_CLOCK_EXECSTAT_JACOBIANS);
output BackendDAE.BackendDAE outBackendDAE;
algorithm
outBackendDAE := matchcontinue(inBackendDAE)
local
BackendDAE.EqSystems eqs;
BackendDAE.Shared shared;
BackendDAE.SymbolicJacobians linearModelMatrixes;
DAE.FunctionTree funcs, functionTree;
list< .DAE.Constraint> constraints;
BackendDAE.Variables v;
list<BackendDAE.Equation> eqns;
case(_) equation
true = Flags.getConfigBool(Flags.GENERATE_SYMBOLIC_LINEARIZATION);
System.realtimeTick(GlobalScript.RT_CLOCK_EXECSTAT_JACOBIANS);
BackendDAE.DAE(eqs=eqs,shared=shared) = inBackendDAE;
BackendDAE.SHARED(constraints=constraints) = shared;
eqns = {};
v = BackendVariable.emptyVars();
(v,eqns) = BackendDAECreate.addOptimizationVarsEqns2(constraints,1,v,eqns,false);
(linearModelMatrixes, funcs) = createLinearModelMatrixes(inBackendDAE, Config.acceptOptimicaGrammar(),v);
shared = BackendDAEUtil.addBackendDAESharedJacobians(linearModelMatrixes, shared);
functionTree = BackendDAEUtil.getFunctions(shared);
functionTree = DAEUtil.joinAvlTrees(functionTree, funcs);
shared = BackendDAEUtil.addFunctionTree(functionTree, shared);
outBackendDAE = BackendDAE.DAE(eqs,shared);
_ = System.realtimeTock(GlobalScript.RT_CLOCK_EXECSTAT_JACOBIANS);
then outBackendDAE;

else then inBackendDAE;
end matchcontinue;
end generateSymbolicLinearizationPast;

protected function createSymbolicJacobianforStates
Expand Down
31 changes: 10 additions & 21 deletions Compiler/Script/CevalScript.mo
Expand Up @@ -1515,15 +1515,9 @@ algorithm
equation

System.realtimeTick(GlobalScript.RT_CLOCK_SIMULATE_TOTAL);
// ensure that generateSymbolicLinearization has been activated.
//we need to ensure that linearization is done before
//we remove all unsued equations.
postOptModStringsOrg = Flags.getConfigStringList(Flags.POST_OPT_MODULES);
postOptModStrings = List.deleteMember(postOptModStringsOrg, "removeUnusedFunctions");
postOptModStrings = Util.if_(List.notMember("generateSymbolicLinearization",postOptModStrings),
listAppend(postOptModStrings,{"generateSymbolicLinearization", "removeUnusedFunctions"}),
postOptModStrings);
Flags.setConfigStringList(Flags.POST_OPT_MODULES, postOptModStrings);

b = Flags.getConfigBool(Flags.GENERATE_SYMBOLIC_LINEARIZATION);
Flags.setConfigBool(Flags.GENERATE_SYMBOLIC_LINEARIZATION, true);

(cache,st,compileDir,executable,method_str,outputFormat_str,_,simflags,resultValues) = buildModel(cache,env,vals,st_1,msg);
Values.REAL(linearizeTime) = getListNthShowError(vals,"try to get stop time",0,2);
Expand All @@ -1550,24 +1544,18 @@ algorithm
newst = Interactive.addVarToSymboltable(
DAE.CREF_IDENT("currentSimulationResult", DAE.T_STRING_DEFAULT, {}),
Values.STRING(result_file), Env.emptyEnv, st);
//Reset PostOptModules flags
Flags.setConfigStringList(Flags.POST_OPT_MODULES, postOptModStringsOrg);
//reset config flag
Flags.setConfigBool(Flags.GENERATE_SYMBOLIC_LINEARIZATION, b);
then
(cache,simValue,newst);

case (cache,env,"optimize",(vals as Values.CODE(Absyn.C_TYPENAME(className))::_),st_1,_)
equation

System.realtimeTick(GlobalScript.RT_CLOCK_SIMULATE_TOTAL);
// ensure that generateSymbolicLinearization has been activated.
//we need to ensure that linearization is done before
//we remove all unsued equations.
postOptModStringsOrg = Flags.getConfigStringList(Flags.POST_OPT_MODULES);
postOptModStrings = List.deleteMember(postOptModStringsOrg, "removeUnusedFunctions");
postOptModStrings = Util.if_(List.notMember("generateSymbolicLinearization",postOptModStrings),
listAppend(postOptModStrings,{"generateSymbolicLinearization", "removeUnusedFunctions"}),
postOptModStrings);
Flags.setConfigStringList(Flags.POST_OPT_MODULES, postOptModStrings);

b = Flags.getConfigBool(Flags.GENERATE_SYMBOLIC_LINEARIZATION);
Flags.setConfigBool(Flags.GENERATE_SYMBOLIC_LINEARIZATION, true);

(cache,st,compileDir,executable,method_str,outputFormat_str,_,simflags,resultValues) = buildModel(cache,env,vals,st_1,msg);
cit = winCitation();
Expand All @@ -1588,7 +1576,8 @@ algorithm
timeSimulation = System.realtimeTock(GlobalScript.RT_CLOCK_SIMULATE_SIMULATION);
timeTotal = System.realtimeTock(GlobalScript.RT_CLOCK_SIMULATE_TOTAL);
(cache,simValue,newst) = createSimulationResultFromcallModelExecutable(resI,timeTotal,timeSimulation,resultValues,cache,className,vals,st,result_file,logFile);
Flags.setConfigStringList(Flags.POST_OPT_MODULES, postOptModStringsOrg);
//reset config flag
Flags.setConfigBool(Flags.GENERATE_SYMBOLIC_LINEARIZATION, b);
then
(cache,simValue,newst);

Expand Down
22 changes: 17 additions & 5 deletions Compiler/Util/Flags.mo
Expand Up @@ -631,10 +631,11 @@ constant ConfigFlag POST_OPT_MODULES = CONFIG_FLAG(16, "postOptModules",
"encapsulateWhenConditions", // must called after remove simple equations
"tearingSystem", // must be the last one, otherwise the torn systems are lost when throw away the matching information
"countOperations",
"removeUnusedFunctions",
"inputDerivativesUsed",
"detectJacobianSparsePattern",
// "generateSymbolicJacobian",
"generateSymbolicJacobian",
"generateSymbolicLinearization",
"removeUnusedFunctions",
"removeConstants"
// "partitionIndependentBlocks",
// "addInitialStmtsToAlgorithms"
Expand All @@ -659,8 +660,8 @@ constant ConfigFlag POST_OPT_MODULES = CONFIG_FLAG(16, "postOptModules",
("relaxSystem",Util.notrans("DESCRIBE ME")),
("countOperations", Util.gettext("Count the mathematic operations of the system.")),
("dumpComponentsGraphStr", Util.notrans("DESCRIBE ME")),
("generateSymbolicJacobian", Util.gettext("Generates symbolic Jacobian.")),
("generateSymbolicLinearization", Util.gettext("Generates symbolic Linearization Matrixes A,B,C,D for Linear Model:\n\t\t\\dot x = Ax + Bu\n\t\ty = Cx +Du")),
("generateSymbolicJacobian", Util.gettext("Generates symbolic jacobian matrix, where der(x) is differentiated w.r.t x. This matrix can be used to simulate with dasslColorSymJac.")),
("generateSymbolicLinearization", Util.gettext("Generates symbolic linearization matrixes A,B,C,D for linear model:\n\t\t\\dot x = Ax + Bu\n\t\ty = Cx +Du")),
("collapseIndependentBlocks", Util.gettext("Collapses all equation systems back into one big system again (undo partitionIndependentBlocks).")),
("removeUnusedFunctions", Util.gettext("Removed all unused functions from functionTree.")),
("simplifyTimeIndepFuncCalls", Util.gettext("Simplifies time independent built in function calls like pre(param) -> param, der(param) -> 0.0, change(param) -> false, edge(param) -> false.")),
Expand Down Expand Up @@ -847,7 +848,16 @@ constant ConfigFlag REPLACE_HOMOTOPY = CONFIG_FLAG(54, "replaceHomotopy",
("simplified", Util.gettext("Replace homotopy(actual, simplified with simplified."))
})),
Util.gettext("Replaces homotopy(actual, simplified) with the actual expression or the simplified expression. Good for debugging models which use homotopy. The default is to not replace homotopy."));

constant ConfigFlag GENERATE_SYMBOLIC_JACOBIAN = CONFIG_FLAG(55, "generateSymbolicJacobian",
NONE(), EXTERNAL(), BOOL_FLAG(false), NONE(),
Util.gettext("Generates symbolic jacobian matrix, where der(x) is differentiated w.r.t x. This matrix can be used to simulate with dasslColorSymJac."));

constant ConfigFlag GENERATE_SYMBOLIC_LINEARIZATION = CONFIG_FLAG(56, "generateSymbolicLinearization",
NONE(), EXTERNAL(), BOOL_FLAG(false), NONE(),
Util.gettext("Generates symbolic linearization matrixes A,B,C,D for linear model:\n\t\t\\dot x = Ax + Bu\n\t\ty = Cx +Du"));


// This is a list of all configuration flags. A flag can not be used unless it's
// in this list, and the list is checked at initialization so that all flags are
// sorted by index (and thus have unique indices).
Expand Down Expand Up @@ -905,7 +915,9 @@ constant list<ConfigFlag> allConfigFlags = {
TEARING_HEURISTIC,
HPCOM_CODE,
REWRITE_RULES_FILE,
REPLACE_HOMOTOPY
REPLACE_HOMOTOPY,
GENERATE_SYMBOLIC_JACOBIAN,
GENERATE_SYMBOLIC_LINEARIZATION
};

public function new
Expand Down

0 comments on commit 1519d75

Please sign in to comment.