Skip to content

Commit

Permalink
BackendDAECreate.mo:
Browse files Browse the repository at this point in the history
 - deactivated minmax and nominal assert, due to the fact that Bug [# 1454] is caused.
SimCodeC.tpl:
- changed measure_time_flag to be able activate it by simulation flag "-v"
SimCode.mo:
- added case for generation only the "jacobian" dg/dx with debug flag "+d=jacobian"
BackendDAEOptimize.mo:
 - adjusted to SimCode.mo "+d=jacobian"
 solver_main.cpp and simulation_runtime.cpp: 
 - activated "euler" solver again, was  deactived for some reason.
 - added additional  solver names : "dassljac" and "dasslnum" for using a symbolic jacobian and own numerical jacobian in dassl.
 - clean up some things.
 testsuite:
 - deactivated some tests that initialise wrong, due to the Bug [# 1455]
 - update some tests to correct results. 

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@8349 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Willi Braun committed Mar 24, 2011
1 parent 8b4f5a3 commit 7792d25
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 96 deletions.
18 changes: 12 additions & 6 deletions Compiler/BackEnd/BackendDAECreate.mo
Expand Up @@ -309,8 +309,10 @@ algorithm
e2 = Inline.inlineExp(e1,(SOME(functionTree),{DAE.NORM_INLINE()}));
vars = BackendVariable.addVar(backendVar2, vars);
e1 = Expression.crefExp(cr);
algs1 = listAppend(algs,minmax);
ialgs1 = listAppend(ialgs,nominal);
//algs1 = listAppend(algs,minmax);
//ialgs1 = listAppend(ialgs,nominal);
algs1 = algs;
ialgs1 = ialgs;
then
(vars,knvars,extVars,BackendDAE.EQUATION(e1, e2, source)::eqns,reqns,ieqns,aeqns,iaeqns,algs1,ialgs1,whenclauses_1,extObjCls,states);

Expand All @@ -321,8 +323,10 @@ algorithm
(backendVar1,NONE(),states,minmax,nominal) = lowerVar(daeEl, states);
SOME(backendVar2) = Inline.inlineVarOpt(SOME(backendVar1),(SOME(functionTree),{DAE.NORM_INLINE()}));
vars = BackendVariable.addVar(backendVar2, vars);
algs1 = listAppend(algs,minmax);
ialgs1 = listAppend(ialgs,nominal);
//algs1 = listAppend(algs,minmax);
//ialgs1 = listAppend(ialgs,nominal);
algs1 = algs;
ialgs1 = ialgs;
then
(vars,knvars,extVars,eqns,reqns,ieqns,aeqns,iaeqns,algs1,ialgs1,whenclauses_1,extObjCls,states);

Expand All @@ -332,8 +336,10 @@ algorithm
(backendVar1,minmax,nominal) = lowerKnownVar(daeEl) "in previous rule, lower_var failed." ;
SOME(backendVar2) = Inline.inlineVarOpt(SOME(backendVar1),(SOME(functionTree),{DAE.NORM_INLINE()}));
knvars = BackendVariable.addVar(backendVar2, knvars);
ialgs1 = listAppend(ialgs,minmax);
ialgs1 = listAppend(ialgs1,nominal);
//ialgs1 = listAppend(ialgs,minmax);
//ialgs1 = listAppend(ialgs1,nominal);
algs1 = algs;
ialgs1 = ialgs;
then
(vars,knvars,extVars,eqns,reqns,ieqns,aeqns,iaeqns,algs,ialgs1,whenclauses_1,extObjCls,states);

Expand Down
47 changes: 43 additions & 4 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -2205,7 +2205,7 @@ algorithm
then (BackendDAE.DAE(v,kv,exv,av,e,re,ie,ae,al,ev,eoc),listArray({}),listArray({}),{});
case(dlow as BackendDAE.DAE(v,kv,exv,av,e,re,ie,ae,al,ev,eoc),functionTree,eqvars,diffvars,varlst)
equation

true = RTOpts.debugFlag("linearization");
// prepare index for Matrix and variables for simpleEquations
derivedVariables = BackendDAEUtil.varList(v);
(varTuple) = determineIndices(eqvars, diffvars, 0, varlst);
Expand Down Expand Up @@ -2235,7 +2235,9 @@ algorithm
Debug.fcall("jacdump2", BackendDump.dumpMatching, v1);
(comps1) = BackendDAETransform.strongComponents(m, mT, v1, v2);
Debug.fcall("jacdump2", BackendDump.dumpComponents, comps1);

Debug.fcall("execJacstat",print, "*** analytical Jacobians -> performed matching and sorting: " +& realString(clock()) +& "\n" );


// figure out wich comps are needed to evaluate all derivedVariables
derivedVariables = BackendDAEUtil.varList(v);
(derivedVars,_) = Util.listSplitOnTrue(derivedVariables,checkIndex);
Expand All @@ -2248,9 +2250,46 @@ algorithm
v4 = arrayCreate(listLength(comps1),0);
v4 = markArray(v3,comps1,v4);
(comps1,_) = splitBlocks2(comps1,v4,1);

Debug.fcall("jacdump2", BackendDump.dumpComponents, comps1);
//Debug.fcall("execJacstat",print, "*** analytical Jacobians -> performed splitig the system: " +& realString(clock()) +& "\n" );
then (dlow,v1,v2,comps1);

case(dlow as BackendDAE.DAE(v,kv,exv,av,e,re,ie,ae,al,ev,eoc),functionTree,eqvars,diffvars,varlst)
equation
true = RTOpts.debugFlag("jacobian");
// prepare index for Matrix and variables for simpleEquations
derivedVariables = BackendDAEUtil.varList(v);
(varTuple) = determineIndices(eqvars, diffvars, 0, varlst);
//BackendDump.printTuple(varTuple);
jacElements = BackendDAE.emptyBintree;
(derivedVariables,jacElements) = changeIndices(derivedVariables, varTuple, jacElements);
v = BackendDAEUtil.listVar(derivedVariables);
dlow = BackendDAE.DAE(v,kv,exv,av,e,re,ie,ae,al,ev,eoc);

// Remove simple Equtaion and
elimLevel = RTOpts.eliminationLevel();
RTOpts.setEliminationLevel(2) "Full elimination";

(dlow, NONE(), NONE()) = removeSimpleEquations(dlow,functionTree,NONE(),NONE());
(dlow, NONE(), NONE()) = removeSimpleEquations(dlow,functionTree,NONE(),NONE());
(dlow, NONE(), NONE()) = removeSimpleEquations(dlow,functionTree,NONE(),NONE());
(dlow, NONE(), NONE()) = removeSimpleEquations(dlow,functionTree,NONE(),NONE());

Debug.fcall("execJacstat",print, "*** analytical Jacobians -> removed simply equations: " +& realString(clock()) +& "\n" );
// figure out new matching and the strong components
m = BackendDAEUtil.incidenceMatrix(dlow, BackendDAE.NORMAL());
mT = BackendDAEUtil.transposeMatrix(m);
(v1,v2,dlow,m,mT) = BackendDAETransform.matchingAlgorithm(dlow, m, mT, (BackendDAE.NO_INDEX_REDUCTION(), BackendDAE.EXACT(), BackendDAE.KEEP_SIMPLE_EQN()),functionTree);
Debug.fcall("jacdump2", BackendDump.dumpIncidenceMatrix, m);
Debug.fcall("jacdump2", BackendDump.dumpIncidenceMatrixT, mT);
Debug.fcall("jacdump2", BackendDump.dump, dlow);
Debug.fcall("jacdump2", BackendDump.dumpMatching, v1);
(comps1) = BackendDAETransform.strongComponents(m, mT, v1, v2);
Debug.fcall("jacdump2", BackendDump.dumpComponents, comps1);
Debug.fcall("execJacstat",print, "*** analytical Jacobians -> performed matching and sorting: " +& realString(clock()) +& "\n" );

Debug.fcall("jacdump2", BackendDump.dumpComponents, comps1);
then (dlow,v1,v2,comps1);
case(_, _, _, _, _) equation
Error.addMessage(Error.INTERNAL_ERROR, {"Linearization.generateLinearMatrix failed"});
Expand Down Expand Up @@ -2578,11 +2617,11 @@ algorithm
Debug.fcall("jacdumpeqn", BackendDump.dumpEqns, {currEquation});
Debug.fcall("jacdumpeqn", print, "\n");
//dummycref = ComponentReference.makeCrefIdent("$pDERdummy", DAE.ET_REAL(), {});
Debug.fcall("execJacstat",print, "*** analytical Jacobians -> derive one equation: " +& realString(clock()) +& "\n" );
//Debug.fcall("execJacstat",print, "*** analytical Jacobians -> derive one equation: " +& realString(clock()) +& "\n" );
currDerivedEquations = derive(currEquation, vars, functions, inputVars, paramVars, stateVars, knownVars, algorithmsLookUp, inorderedVars, inDiffVars);
Debug.fcall("jacdumpeqn", BackendDump.dumpEqns, currDerivedEquations);
Debug.fcall("jacdumpeqn", print, "\n");
Debug.fcall("execJacstat",print, "*** analytical Jacobians -> created other equations from that: " +& realString(clock()) +& "\n" );
//Debug.fcall("execJacstat",print, "*** analytical Jacobians -> created other equations from that: " +& realString(clock()) +& "\n" );
restDerivedEquations = deriveAll(restEquations, vars, functions, inputVars, paramVars, stateVars, knownVars, algorithmsLookUp, inorderedVars, inDiffVars);
derivedEquations = listAppend(currDerivedEquations, restDerivedEquations);
then derivedEquations;
Expand Down
59 changes: 55 additions & 4 deletions Compiler/BackEnd/SimCode.mo
Expand Up @@ -2104,18 +2104,29 @@ algorithm
BackendDAE.EventInfo ev;
BackendDAE.ExternalObjectClasses eoc;

list<JacobianMatrix> LinearMats;
BackendDAE.Variables NewVars;
BackendDAE.EquationArray NewEqns;

list<JacobianMatrix> LinearMats;
Integer nEqns;

case (functions,dlow as BackendDAE.DAE(v,kv,exv,av,e,re,ie,ae,al,ev,eoc),ass1,ass2,comps,m,mt)
equation
true = RTOpts.debugFlag("jacobian");

//TODO: split the equations and vaiables from the rest
(blt_states, _) = BackendDAEUtil.generateStatePartition(comps, dlow, ass1, ass2, m, mt);

NewEqns = BackendDAEUtil.listEquation({});
NewVars = BackendDAEUtil.emptyVars();
(NewEqns, NewVars) = splitoutEquationAndVars(blt_states,e,v,ass2,NewEqns,NewVars);
BackendDAE.EQUATION_ARRAY(numberOfElement = nEqns) = NewEqns;
dlow = BackendDAE.DAE(NewVars,kv,exv,av,NewEqns,re,ie,ae,al,ev,eoc);

// Prepare all needed variables
varlst = BackendDAEUtil.varList(v);
varlst = BackendDAEUtil.varList(NewVars);
varlst1 = BackendDAEUtil.varList(kv);
states = BackendVariable.getAllStateVarFromVariables(v);
states = BackendVariable.getAllStateVarFromVariables(NewVars);
states = Util.sort(states, BackendVariable.varIndexComparer);
inputvars = Util.listSelect(varlst1,BackendDAEUtil.isInput);
inputvars = Util.sort(inputvars, BackendVariable.varIndexComparer);
Expand All @@ -2133,7 +2144,7 @@ algorithm
// Differentiate the ODE system w.r.t states for jacobian
Debug.fcall("linmodel",print,"Differentiate System w.r.t. states.\n");
deriveddlow1 = BackendDAEOptimize.generateSymbolicJacobian(dlow, functions, comref_states, BackendDAEUtil.listVar(states), BackendDAEUtil.listVar(inputvars), BackendDAEUtil.listVar(paramvars));
Debug.fcall("execJacstat",print, "*** analytical Jacobians -> generated system for ODE-Block : " +& realString(clock()) +& "\n" );
Debug.fcall("execJacstat",print, "*** analytical Jacobians -> generated SymbolicJacobian for ODE-Block with "+& intString(nEqns) +& " Equations ->" +& intString(listLength(states)*nEqns) +& " : " +& realString(clock()) +& "\n" );

// create Matrix A and variables
Debug.fcall("jacdump2", print, "Dump of daelow for Matrix A.\n");
Expand Down Expand Up @@ -2163,6 +2174,46 @@ algorithm
end matchcontinue;
end createJacobianMatrix;

protected function splitoutEquationAndVars
input list<list<Integer>> inNeededBlocks;
input BackendDAE.EquationArray inEqns;
input BackendDAE.Variables inVars;
input array<Integer> inIntegerArray3;
input BackendDAE.EquationArray inEqnsNew;
input BackendDAE.Variables inVarsNew;
output BackendDAE.EquationArray outEqns;
output BackendDAE.Variables outVars;
algorithm
(outEqns,outVars) := matchcontinue( inNeededBlocks,inEqns,inVars,inIntegerArray3, inEqnsNew, inVarsNew)
local
Integer eqNum;
list<Integer> eqnNums;
list<list<Integer>> rest;
BackendDAE.Equation eqn;
BackendDAE.Var var;
list<BackendDAE.Equation> eqn_lst;
list<BackendDAE.Var> var_lst;
array<Integer> ass2;
BackendDAE.EquationArray eqnsNew;
BackendDAE.Variables varsNew;
case ({},inEqns,inVars,inIntegerArray3,eqnsNew,varsNew) then (eqnsNew,varsNew);
case ({eqNum}::rest,inEqns,inVars,inIntegerArray3,eqnsNew,varsNew)
equation
(eqnsNew,varsNew) = splitoutEquationAndVars(rest,inEqns,inVars,inIntegerArray3,eqnsNew,varsNew);
(eqn, var) = getEquationAndSolvedVar(eqNum, inEqns, inVars, inIntegerArray3);
eqnsNew = BackendEquation.equationAdd(eqn, eqnsNew);
varsNew = BackendVariable.addVar(var, varsNew);
then (eqnsNew,varsNew);
case ((eqnNums as (_ :: (_ :: _))) :: rest,inEqns,inVars,inIntegerArray3,eqnsNew,varsNew)
equation
(eqnsNew,varsNew) = splitoutEquationAndVars(rest,inEqns,inVars,inIntegerArray3,eqnsNew,varsNew);
(eqn_lst,var_lst) = Util.listMap32(eqnNums, getEquationAndSolvedVar, inEqns, inVars, inIntegerArray3);
eqnsNew = BackendEquation.addEquations(eqn_lst, eqnsNew);
varsNew = BackendVariable.addVars(var_lst, varsNew);
then (eqnsNew,varsNew);
end matchcontinue;
end splitoutEquationAndVars;

protected function createLinearModelMatrixes
input DAE.FunctionTree functions;
input BackendDAE.BackendDAE inBackendDAE2;
Expand Down
4 changes: 2 additions & 2 deletions Compiler/susan_codegen/SimCode/SimCodeC.tpl
Expand Up @@ -92,9 +92,9 @@ case SIMCODE(__) then
<%simulationFileHeader(simCode)%>
<%externalFunctionIncludes(externalFunctionIncludes)%>
#ifdef _OMC_MEASURE_TIME
const int measure_time_flag = 1;
int measure_time_flag = 1;
#else
const int measure_time_flag = 0;
int measure_time_flag = 0;
#endif
<%globalData(modelInfo,fileNamePrefix)%>

Expand Down
2 changes: 1 addition & 1 deletion c_runtime/simulation_init.cpp
Expand Up @@ -62,7 +62,7 @@ void leastSquare(long *nz, double *z, double *funcValue)

functionODE();
functionAlgebraics();
functionAliasEquations();
//functionAliasEquations();
/*
int needToIterate = 0;
int IterationNum = 0;
Expand Down
14 changes: 11 additions & 3 deletions c_runtime/simulation_runtime.cpp
Expand Up @@ -409,7 +409,7 @@ startNonInteractiveSimulation(int argc, char**argv)
cout << "Linear model is created!" << endl;
}

if (retVal == 0 && measure_time_flag) {
if (retVal == 0 && measure_time_flag && ! sim_verbose) {
const string modelInfo = string(globalData->modelFilePrefix) + "_prof.xml";
const string plotFile = string(globalData->modelFilePrefix) + "_prof.plt";
rt_accumulate(SIM_TIMER_TOTAL);
Expand Down Expand Up @@ -459,9 +459,9 @@ callSolver(int argc, char**argv, string method, string outputFormat,
<< "'" << endl;
}

if (method == std::string("euler")) {
if (method == std::string("")) {
if (sim_verbose >= LOG_SOLVER) {
cout << "No Recognized solver, using dassl." << endl;
cout << "No solver is set, using dassl." << endl;
}
retVal = solver_main(argc,argv,start,stop,stepSize,outputSteps,tolerance,3);
} else if (method == std::string("euler")) {
Expand All @@ -485,6 +485,12 @@ callSolver(int argc, char**argv, string method, string outputFormat,
}
jac_flag = 1;
retVal = solver_main(argc, argv, start, stop, stepSize, outputSteps, tolerance, 3);
} else if (method == std::string("dasslnum")) {
if (sim_verbose >= LOG_SOLVER) {
cout << "Recognized solver: " << method << "." << endl;
}
num_jac_flag = 1;
retVal = solver_main(argc, argv, start, stop, stepSize, outputSteps, tolerance, 3);
} else if (method == std::string("inline-euler")) {
if (!_omc_force_solver || std::string(_omc_force_solver) != std::string("inline-euler")) {
cout << "Recognized solver: " << method
Expand Down Expand Up @@ -566,6 +572,8 @@ initRuntimeAndSimulation(int argc, char**argv)
#endif
int verbose_flags = verboseLevel(argc, argv);
sim_verbose = verbose_flags ? verbose_flags : sim_verbose;
if (sim_verbose)
measure_time_flag = 1;

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion c_runtime/simulation_runtime.h
Expand Up @@ -90,7 +90,7 @@ int callSolver(int, char**, string, string, string, double, double, double, long

#endif /* cplusplus */

extern const int measure_time_flag;
extern int measure_time_flag;
extern int sim_verbose; /* control debug output during simulation. */
extern int sim_noemit; /* control emitting result data to file */
extern int acceptedStep; /* !=0 when accepted step is calculated, 0 otherwise. */
Expand Down

0 comments on commit 7792d25

Please sign in to comment.