@@ -9438,20 +9438,18 @@ protected function fixUnderDeterminedInitialSystem "protected function fixUnderD
94389438 input BackendDAE . BackendDAE inDAE;
94399439 input BackendDAE . Variables inVars;
94409440 input BackendDAE . EquationArray inEqns;
9441- output Boolean outFixed ;
9441+ output Boolean outSucceed ;
94429442 output BackendDAE . Variables outVars;
94439443 output BackendDAE . EquationArray outEqns;
94449444algorithm
9445- (outFixed , outVars, outEqns) := matchcontinue(inDAE, inVars, inEqns)
9445+ (outSucceed , outVars, outEqns) := matchcontinue(inDAE, inVars, inEqns)
94469446 local
94479447 BackendDAE . SymbolicJacobian jacG;
9448- BackendDAE . SparsePattern sparsityPattern; // type SparsePattern = tuple<list<tuple< .DAE.ComponentRef, list< .DAE.ComponentRef>>>, // column-wise sparse pattern
9449- // tuple<list< .DAE.ComponentRef>, // diff vars
9450- // list< .DAE.ComponentRef>>>; // diffed vars
9448+ BackendDAE . SparsePattern sparsityPattern;
94519449 BackendDAE . BackendDAE dae;
94529450
9453- . DAE . ComponentRef cr;
9454- list< . DAE . ComponentRef > diffVars, diffedVars;
9451+ . DAE . ComponentRef cr, currRes ;
9452+ list< . DAE . ComponentRef > diffVars, diffedVars, currDependencies, someStates ;
94559453 String str;
94569454 list< BackendDAE . Var > vars; // all vars
94579455 list< BackendDAE . Var > outputs; // $res1 ... $resN (initial equations)
@@ -9461,12 +9459,12 @@ algorithm
94619459 Integer nVars, nStates, nEqns;
94629460 BackendDAE . EquationArray eqns;
94639461
9462+ list< tuple< . DAE . ComponentRef , list< . DAE . ComponentRef >>> dep;
9463+
9464+ // fix all states
94649465 case (_, _, eqns) equation
94659466 (dae, outputs) = BackendDAEOptimize . generateInitialMatricesDAE(inDAE);
9466-
9467- vars = varList(inVars);
9468- (sparsityPattern, _) = BackendDAEOptimize . generateSparsePattern(dae, vars, outputs);
9469-
9467+
94709468 BackendDAE . DAE (eqs= systs) = inDAE;
94719469 ivars = emptyVars();
94729470 ivars = List . fold(systs, collectUnfixedStatesFromSystem, ivars);
@@ -9477,16 +9475,126 @@ algorithm
94779475 nEqns = equationSize(eqns);
94789476 true = intEq(nVars, nEqns+ nStates);
94799477
9480- Debug . fcall(Flags . PEDANTIC , Error . addCompilerWarning, "Setting all (" +& intString(nStates) +& ") states to fixed=true to solve the initial system." );
9481-
9478+ Debug . fcall(Flags . PEDANTIC , Error . addCompilerWarning, "Assuming fixed start value for the following " +& intString(nVars- nEqns) +& " states:" );
94829479 eqns = addStartValueEquations(states, eqns);
94839480 then (true , inVars, eqns);
94849481
9482+ // fix a subset of unfixed states
9483+ case (_, _, eqns) equation
9484+ (dae, outputs) = BackendDAEOptimize . generateInitialMatricesDAE(inDAE);
9485+
9486+ BackendDAE . DAE (eqs= systs) = inDAE;
9487+ ivars = emptyVars();
9488+ ivars = List . fold(systs, collectUnfixedStatesFromSystem, ivars);
9489+ states = varList(ivars);
9490+
9491+ nVars = BackendVariable . varsSize(inVars);
9492+ nEqns = equationSize(eqns);
9493+ true = intLt(nEqns, nVars);
9494+
9495+ Debug . fcall(Flags . PEDANTIC , Error . addCompilerWarning, "Assuming fixed start value for the following " +& intString(nVars- nEqns) +& " variables:" );
9496+
9497+ // type SparsePattern = tuple<list<tuple< .DAE.ComponentRef, list< .DAE.ComponentRef>>>, // column-wise sparse pattern
9498+ // tuple<list< .DAE.ComponentRef>, // diff vars
9499+ // list< .DAE.ComponentRef>>>; // diffed vars
9500+ (sparsityPattern, _) = BackendDAEOptimize . generateSparsePattern(dae, states, outputs);
9501+ (dep, _) = sparsityPattern;
9502+ someStates = collectIndependentVars(dep, {});
9503+
9504+ // printSparsityPattern(sparsityPattern);
9505+ // print("selection (" +& intString(listLength(someStates)) +& ") ########\n");
9506+ // ComponentReference.printComponentRefList(someStates);
9507+
9508+ eqns = addStartValueEquations1(someStates, eqns);
9509+ then (true , inVars, eqns);
9510+
94859511 else
94869512 then (false , inVars, inEqns);
94879513 end matchcontinue;
94889514end fixUnderDeterminedInitialSystem;
94899515
9516+ protected function printSparsityPattern "protected function printSparsityPattern
9517+ author lochel"
9518+ input BackendDAE . SparsePattern inPattern;
9519+ protected
9520+ list< tuple< . DAE . ComponentRef , list< . DAE . ComponentRef >>> pattern;
9521+ list< . DAE . ComponentRef > diffVars, diffedVars;
9522+ algorithm
9523+ (pattern, (diffVars, diffedVars)) := inPattern;
9524+
9525+ print("diffVars (" +& intString(listLength(diffVars)) +& ") ######## \n " );
9526+ ComponentReference . printComponentRefList(diffVars);
9527+
9528+ print("diffedVars (" +& intString(listLength(diffedVars)) +& ") ######## \n " );
9529+ ComponentReference . printComponentRefList(diffedVars);
9530+
9531+ printSparsityPattern1(pattern);
9532+ end printSparsityPattern;
9533+
9534+ protected function printSparsityPattern1 "protected function printSparsityPattern1
9535+ author lochel"
9536+ input list< tuple< . DAE . ComponentRef , list< . DAE . ComponentRef >>> inPattern;
9537+ algorithm
9538+ () := matchcontinue(inPattern)
9539+ local
9540+ tuple< . DAE . ComponentRef , list< . DAE . ComponentRef >> curr;
9541+ list< tuple< . DAE . ComponentRef , list< . DAE . ComponentRef >>> rest;
9542+ . DAE . ComponentRef cr;
9543+ list< . DAE . ComponentRef > crList;
9544+ String crStr;
9545+
9546+ case ({})
9547+ then ();
9548+
9549+ case (curr::rest) equation
9550+ (cr, crList) = curr;
9551+ crStr = ComponentReference . crefStr(cr);
9552+ print(crStr +& " (" +& intString(listLength(crList)) +& ") ######## \n " );
9553+ ComponentReference . printComponentRefList(crList);
9554+
9555+ printSparsityPattern1(rest);
9556+ then ();
9557+
9558+ else equation
9559+ Error . addMessage(Error . INTERNAL_ERROR , {"./Compiler/BackEnd/BackendDAEUtil.mo: function printSparsityPattern1 failed" });
9560+ then fail();
9561+ end matchcontinue;
9562+ end printSparsityPattern1;
9563+
9564+ protected function collectIndependentVars "protected function collectIndependentVars
9565+ author lochel"
9566+ input list< tuple< . DAE . ComponentRef , list< . DAE . ComponentRef >>> inPattern;
9567+ input list< . DAE . ComponentRef > inVars;
9568+ output list< . DAE . ComponentRef > outVars;
9569+ algorithm
9570+ outVars := matchcontinue(inPattern, inVars)
9571+ local
9572+ tuple< . DAE . ComponentRef , list< . DAE . ComponentRef >> curr;
9573+ list< tuple< . DAE . ComponentRef , list< . DAE . ComponentRef >>> rest;
9574+ . DAE . ComponentRef cr;
9575+ list< . DAE . ComponentRef > crList, vars;
9576+
9577+ case ({}, _)
9578+ then inVars;
9579+
9580+ case (curr::rest, _) equation
9581+ (cr, crList) = curr;
9582+ 0 = listLength(crList);
9583+
9584+ vars = collectIndependentVars(rest, inVars);
9585+ vars = cr::vars;
9586+ then vars;
9587+
9588+ case (curr::rest, _) equation
9589+ vars = collectIndependentVars(rest, inVars);
9590+ then vars;
9591+
9592+ else equation
9593+ Error . addMessage(Error . INTERNAL_ERROR , {"./Compiler/BackEnd/BackendDAEUtil.mo: function collectIndependentVars failed" });
9594+ then fail();
9595+ end matchcontinue;
9596+ end collectIndependentVars;
9597+
94909598protected function addStartValueEquations "function addStartValueEquations
94919599 author lochel"
94929600 input list< BackendDAE . Var > inVars;
@@ -9503,20 +9611,26 @@ algorithm
95039611 DAE . Exp e, e1, crefExp, startExp;
95049612 DAE . ComponentRef cref;
95059613 DAE . Type tp;
9614+ String crStr;
95069615
95079616 case ({}, _)
95089617 then inEqns;
95099618
95109619 case (var ::vars, eqns) equation
95119620 cref = BackendVariable . varCref(var );
9512- crefExp = DAE . CREF (cref, DAE . T_REAL_DEFAULT );
9621+ tp = BackendVariable . varType(var );
9622+
9623+ crefExp = DAE . CREF (cref, tp);
95139624
95149625 e = Expression . crefExp(cref);
95159626 tp = Expression . typeof(e);
95169627 startExp = Expression . makeBuiltinCall("$_start" , {e}, tp);
95179628
95189629 eqn = BackendDAE . EQUATION (crefExp, startExp, DAE . emptyElementSource);
95199630
9631+ crStr = ComponentReference . crefStr(cref);
9632+ Debug . fcall(Flags . PEDANTIC , Error . addCompilerWarning, " " +& crStr);
9633+
95209634 eqns = BackendEquation . equationAdd(eqn, eqns);
95219635 eqns = addStartValueEquations(vars, eqns);
95229636 then eqns;
@@ -9527,6 +9641,50 @@ algorithm
95279641 end matchcontinue;
95289642end addStartValueEquations;
95299643
9644+ protected function addStartValueEquations1 "function addStartValueEquations1
9645+ author lochel
9646+ Same as addStartValueEquations - just with list<DAE.ComponentRef> instead of list<BackendDAE.Var>"
9647+ input list< DAE . ComponentRef > inVars;
9648+ input BackendDAE . EquationArray inEqns;
9649+ output BackendDAE . EquationArray outEqns;
9650+ algorithm
9651+ outEqns := matchcontinue(inVars, inEqns)
9652+ local
9653+ DAE . ComponentRef var ;
9654+ list< DAE . ComponentRef > vars;
9655+ BackendDAE . Equation eqn;
9656+ BackendDAE . EquationArray eqns;
9657+
9658+ DAE . Exp e, e1, crefExp, startExp;
9659+ DAE . ComponentRef cref;
9660+ DAE . Type tp;
9661+ String crStr;
9662+
9663+ case ({}, _)
9664+ then inEqns;
9665+
9666+ case (var ::vars, eqns) equation
9667+ crefExp = DAE . CREF (var , DAE . T_REAL_DEFAULT );
9668+
9669+ e = Expression . crefExp(var );
9670+ tp = Expression . typeof(e);
9671+ startExp = Expression . makeBuiltinCall("$_start" , {e}, tp);
9672+
9673+ eqn = BackendDAE . EQUATION (crefExp, startExp, DAE . emptyElementSource);
9674+
9675+ crStr = ComponentReference . crefStr(var );
9676+ Debug . fcall(Flags . PEDANTIC , Error . addCompilerWarning, " " +& crStr);
9677+
9678+ eqns = BackendEquation . equationAdd(eqn, eqns);
9679+ eqns = addStartValueEquations1(vars, eqns);
9680+ then eqns;
9681+
9682+ else equation
9683+ Error . addMessage(Error . INTERNAL_ERROR , {"./Compiler/BackEnd/BackendDAEUtil.mo: function addStartValueEquations1 failed" });
9684+ then fail();
9685+ end matchcontinue;
9686+ end addStartValueEquations1;
9687+
95309688protected function collectInitialVarsEqnsSystem "function collectInitialVarsEqnsSystem
95319689 author Frenkel TUD 2012-10"
95329690 input BackendDAE . EqSystem isyst;
0 commit comments