@@ -1744,7 +1744,7 @@ try
17441744 if Flags . isSet(Flags . JAC_DUMP2 ) then
17451745 BackendDump . dumpSparsityPattern(sparsePattern, "FMI sparsity" );
17461746 end if ;
1747- outJacobianMatrixes := (SOME ((emptyBDAE,"FMIDER" ,{},{},{})), sparsePattern, sparseColoring)::outJacobianMatrixes;
1747+ outJacobianMatrixes := (SOME ((emptyBDAE,"FMIDER" ,{},{},{}, {} )), sparsePattern, sparseColoring)::outJacobianMatrixes;
17481748 outFunctionTree := inBackendDAE. shared . functionTree;
17491749 else
17501750 // prepare more needed variables
@@ -2001,7 +2001,7 @@ algorithm
20012001 local
20022002 BackendDAE . BackendDAE backendDAE, reducedDAE;
20032003
2004- list< DAE . ComponentRef > comref_vars, comref_differentiatedVars;
2004+ list< DAE . ComponentRef > comref_vars, comref_differentiatedVars, dependencies ;
20052005
20062006 BackendDAE . Shared shared ;
20072007 BackendDAE . Variables globalKnownVars, globalKnownVars1;
@@ -2043,9 +2043,10 @@ algorithm
20432043 if Flags . isSet(Flags . JAC_DUMP2 ) then
20442044 print("analytical Jacobians -> generated Jacobian DAE time: " + realString(clock()) + " \n " );
20452045 end if ;
2046+ dependencies = calcJacobianDependencies((backendDAE, "" , {}, {}, {}, {}));
20462047
20472048 then
2048- ((backendDAE, inName, inDiffVars, diffedVars, inVars), funcs);
2049+ ((backendDAE, inName, inDiffVars, diffedVars, inVars, dependencies ), funcs);
20492050 else
20502051 equation
20512052 Error . addInternalError("function createJacobian failed" , sourceInfo());
@@ -2382,7 +2383,7 @@ algorithm
23822383 BackendDAE . SymbolicJacobians rest;
23832384 String name;
23842385
2385- case (matrix as (SOME ((_,name,_,_,_)), _, _))::_ guard
2386+ case (matrix as (SOME ((_,name,_,_,_,_ )), _, _))::_ guard
23862387 stringEq(name, inJacobianName)
23872388 then SOME (matrix);
23882389
@@ -2393,6 +2394,36 @@ algorithm
23932394 end match;
23942395end getJacobianMatrixbyName;
23952396
2397+
2398+ public function calcJacobianDependencies
2399+ input BackendDAE . SymbolicJacobian jacobian;
2400+ output list< DAE . ComponentRef > dependencies;
2401+ protected
2402+ BackendDAE . EqSystem syst;
2403+ BackendDAE . Shared shared ;
2404+ algorithm
2405+ (BackendDAE . DAE ({syst}, shared ), _, _, _, _, _) := jacobian;
2406+ dependencies := BackendEquation . getCrefsFromEquations(syst. orderedEqs, syst. orderedVars, shared . globalKnownVars);
2407+ end calcJacobianDependencies;
2408+
2409+ public function getJacobianDependencies
2410+ input BackendDAE . Jacobian jacobian;
2411+ output list< DAE . ComponentRef > dependencies;
2412+ algorithm
2413+ dependencies := match(jacobian)
2414+ case (BackendDAE . GENERIC_JACOBIAN (jacobian= SOME ((_, _, _, _, _, dependencies))))
2415+ then dependencies;
2416+
2417+ case (BackendDAE . GENERIC_JACOBIAN (jacobian= NONE ()))
2418+ then {};
2419+
2420+ else equation
2421+ Error . addInternalError("function getJacobianDependencies failed" , sourceInfo());
2422+ then fail();
2423+
2424+ end match;
2425+ end getJacobianDependencies;
2426+
23962427// =============================================================================
23972428// Module for to calculate strong component Jacobains
23982429//
@@ -2826,7 +2857,8 @@ algorithm
28262857
28272858 // create known variables
28282859 knvarLst1 := BackendEquation . equationsVars(eqns, globalKnownVars);
2829- knvarLst2 := BackendEquation . equationsVars(eqns, inAllVars);
2860+ // knvarLst2 := BackendEquation.equationsVars(eqns, inAllVars);
2861+ knvarLst2 := {};
28302862 // Create a list of known variables true *only* for this shared system
28312863 globalKnownVars := BackendVariable . listVar2(knvarLst1,knvarLst2);
28322864 // Remove inputs for the jacobian
@@ -3716,7 +3748,7 @@ protected
37163748 BackendDAE . BackendDAE jacBDAE;
37173749 String name;
37183750algorithm
3719- (jacBDAE, name, _, _, _) := symbolicJacobian;
3751+ (jacBDAE, name, _, _, _, _ ) := symbolicJacobian;
37203752 try
37213753 _ := BackendDAEUtil . mapEqSystem(jacBDAE, checkForNonLinearStrongComponents_work);
37223754 result := true ;
@@ -3754,5 +3786,123 @@ algorithm
37543786 end try ;
37553787end checkForNonLinearStrongComponents_work;
37563788
3789+
3790+ public function getFixedStatesForSelfdependentSets
3791+ " author: kabdelhak
3792+ Returns states to fix for initial problem in the case of selfdependent dynamic state sets"
3793+ input BackendDAE . StateSet stateSet;
3794+ input list< BackendDAE . Var > unfixedStates;
3795+ input Integer toFix;
3796+ output list< BackendDAE . Var > statesToFix;
3797+ protected
3798+ list< tuple< Integer ,BackendDAE . Var >> nonlinearCountLst = {};
3799+ Integer nonlinearCount;
3800+ algorithm
3801+ _:= match(stateSet. jacobian)
3802+ local
3803+ BackendDAE . SymbolicJacobian sJac;
3804+ BackendDAE . BackendDAE dae;
3805+ list< BackendDAE . Var > diffVars;
3806+ String matrixName;
3807+ case (BackendDAE . GENERIC_JACOBIAN (jacobian= SOME (sJac))) algorithm
3808+ ((dae,matrixName,diffVars, _, _,_)) := sJac;
3809+ for var in unfixedStates loop
3810+ nonlinearCountLst := getNonlinearStateCount(var ,unfixedStates,dae,matrixName)::nonlinearCountLst;
3811+ end for ;
3812+ then 0 ;
3813+ end match;
3814+ statesToFix := fixedVarsFromNonlinearCount(nonlinearCountLst, toFix);
3815+ end getFixedStatesForSelfdependentSets;
3816+
3817+ protected function getNonlinearStateCount
3818+ input BackendDAE . Var state;
3819+ input list< BackendDAE . Var > diffVars;
3820+ input BackendDAE . BackendDAE dae;
3821+ input String matrixName;
3822+ output tuple< Integer ,BackendDAE . Var > outTpl;
3823+ protected
3824+ algorithm
3825+ outTpl:= match(dae)
3826+ local
3827+ BackendDAE . EqSystems systs;
3828+ tuple< BackendDAE . Var ,list< BackendDAE . Var > ,Integer ,String > tpl;
3829+ BackendDAE . Var outState;
3830+ Integer nonlinearCount = 0 ;
3831+ case BackendDAE . DAE (eqs= systs) algorithm
3832+ tpl := (state,diffVars,nonlinearCount,matrixName);
3833+ for syst in systs loop
3834+ _:= match(syst)
3835+ local
3836+ BackendDAE . EquationArray eqnarray;
3837+
3838+ case BackendDAE . EQSYSTEM (_,eqnarray,_,_,_,_,_,_) algorithm
3839+ tpl := BackendEquation . traverseEquationArray(eqnarray,getNonlinearStateCount0,tpl);
3840+ then 0 ;
3841+ end match;
3842+ end for ;
3843+ (outState,_,nonlinearCount,_) := tpl;
3844+ then (nonlinearCount,outState);
3845+ end match;
3846+
3847+ end getNonlinearStateCount;
3848+
3849+ protected function getNonlinearStateCount0
3850+ input BackendDAE . Equation inEq;
3851+ input tuple< BackendDAE . Var ,list< BackendDAE . Var > ,Integer ,String > inTpl;
3852+ output BackendDAE . Equation outEq;
3853+ output tuple< BackendDAE . Var ,list< BackendDAE . Var > ,Integer ,String > outTpl;
3854+ algorithm
3855+ outEq := inEq;
3856+ outTpl := match inEq
3857+ local
3858+ DAE . Exp exp, diffExp;
3859+ BackendDAE . Var state;
3860+ list< BackendDAE . Var > diffVars;
3861+ Integer nonlinearCount;
3862+ String matrixName;
3863+ DAE . ComponentRef seedVar;
3864+ list< DAE . Subscript > subs;
3865+ case BackendDAE . EQUATION (scalar= exp) algorithm
3866+ (state,diffVars,nonlinearCount,matrixName) := inTpl;
3867+ // Differentiate equation to look for nonlinear dependencies
3868+ seedVar := Differentiate . createSeedCrefName(BackendVariable . varCref(state),matrixName);
3869+ diffExp := Differentiate . differentiateExpSolve(exp,seedVar,NONE ());
3870+ for var in diffVars loop
3871+ if not ComponentReference . crefEqual(var . varName, state. varName) and Expression . expContains(diffExp,Expression . crefExp(var . varName)) then
3872+ // Heuristic to punish fixed vars with a value of zero
3873+ if BackendVariable . varFixed(var ) and Expression . isZero(BackendVariable . varStartValue(var )) then
3874+ nonlinearCount := nonlinearCount + 2 ;
3875+ else
3876+ nonlinearCount := nonlinearCount + 1 ;
3877+ end if ;
3878+ end if ;
3879+ end for ;
3880+ then (state,diffVars,nonlinearCount,matrixName);
3881+ end match;
3882+ end getNonlinearStateCount0;
3883+
3884+ protected function fixedVarsFromNonlinearCount
3885+ input list< tuple< Integer ,BackendDAE . Var >> tplLst;
3886+ input Integer toFix;
3887+ output list< BackendDAE . Var > fixedVars = {};
3888+ protected
3889+ list< tuple< Integer ,BackendDAE . Var >> sortedTplLst, strippedTplLst;
3890+ BackendDAE . Var fixVar;
3891+ Integer fixInt;
3892+ algorithm
3893+ for tpl in tplLst loop
3894+ (fixInt,fixVar) := tpl;
3895+ end for ;
3896+ // Sort by nonlinear count and take first N states to fix
3897+ sortedTplLst := List . sort(tplLst, Util . compareTupleIntGt);
3898+ strippedTplLst := List . firstN(sortedTplLst,toFix);
3899+ for tpl in strippedTplLst loop
3900+ (_,fixVar) := tpl;
3901+ fixVar. values := DAEUtil . setFixedAttr(fixVar. values,SOME (DAE . BCONST (true )));
3902+ fixedVars := fixVar::fixedVars;
3903+ end for ;
3904+ end fixedVarsFromNonlinearCount;
3905+
3906+
37573907annotation(__OpenModelica_Interface= "backend" );
37583908end SymbolicJacobian ;
0 commit comments