@@ -92,20 +92,22 @@ algorithm
9292 list< SimCode . SimEqSystem > eqs;
9393 list< list< Integer >> s;
9494 list< DAE . ComponentRef > states,disc,algs;
95+ list< SimCode . SampleCondition > sampleConditions;
9596 BackendDAE . EquationArray eqsdae;
9697 BackendDAE . Shared shared ;
97- case (dlow as BackendDAE . DAE ({BackendDAE . EQSYSTEM (_,eqsdae,_,_,_)},shared ), ass1, ass2, m, mt, comps,SimCode . SIMCODE (odeEquations= {eqs}))
98+ case (dlow as BackendDAE . DAE ({BackendDAE . EQSYSTEM (_,eqsdae,_,_,_)},shared ), ass1, ass2,
99+ m, mt, comps,SimCode . SIMCODE (odeEquations= {eqs},sampleConditions= sampleConditions))
98100 equation
99101 print(" \n ---------------------------- \n " );
100102 print("BackEndQSS analysis initialized" );
101103 print(" \n ---------------------------- \n " );
102104 (allVarsList, stateVarsList,orderedVarsList) = getAllVars(dlow);
103105 stateVarsList = List . filterOnTrue(orderedVarsList,BackendVariable . isStateVar);
104- algVarsList = List . filterOnTrue(orderedVarsList,BackendVariable . isVarAlg);
105106 discVarsLst = List . filterOnTrue(orderedVarsList,isDiscreteVar);
106107 disc = List . map(discVarsLst,getCref);
107- algs = List . map(algVarsList,getCref );
108+ disc = listAppend(disc, createDummyVars(listLength(sampleConditions)) );
108109 states = List . map(stateVarsList,getCref);
110+ algs = computeAlgs(eqs,states,{});
109111 s = computeStateRef(List . map(states,ComponentReference . crefPrefixDer),eqs,{});
110112 then
111113 (QSSINFO (s,states,disc,algs,eqsdae),simCode);
@@ -337,32 +339,36 @@ algorithm
337339
338340 case ((e as DAE . CREF (componentRef = cr as DAE . CREF_IDENT (_,t1,subs),ty= t),(states,disc,algs)))
339341 equation
340- p = List . position (cr,states);
342+ p = List . positionOnTrue (cr,states, ComponentReference . crefEqual );
341343 ident = stringAppend(stringAppend("x[" ,intString(p+ 1 )),"]" );
342344 then ((DAE . CREF (DAE . CREF_IDENT (ident,t1,subs),t),(states,disc,algs)));
343345 case ((e as DAE . CREF (componentRef = cr as DAE . CREF_QUAL (_,t1,subs,_),ty= t),(states,disc,algs)))
344346 equation
345- p = List . position(cr,states);
347+ // p = List.position(cr,states);
348+ p = List . positionOnTrue(cr,states,ComponentReference . crefEqual);
346349 ident = stringAppend(stringAppend("x[" ,intString(p+ 1 )),"]" );
347350 then ((DAE . CREF (DAE . CREF_IDENT (ident,t1,subs),t),(states,disc,algs)));
348351 case ((e as DAE . CREF (componentRef = cr as DAE . CREF_IDENT (_,t1,subs),ty= t),(states,disc,algs)))
349352 equation
350- p = List . position(cr,disc);
353+ // p = List.position(cr,disc);
354+ p = List . positionOnTrue(cr,disc,ComponentReference . crefEqual);
351355 ident = stringAppend(stringAppend("d[" ,intString(p+ 1 )),"]" );
352356 then ((DAE . CREF (DAE . CREF_IDENT (ident,t1,subs),t),(states,disc,algs)));
353357 case ((e as DAE . CREF (componentRef = cr as DAE . CREF_QUAL (_,t1,subs,_),ty= t),(states,disc,algs)))
354358 equation
355- p = List . position (cr,disc);
359+ p = List . positionOnTrue (cr,disc, ComponentReference . crefEqual );
356360 ident = stringAppend(stringAppend("d[" ,intString(p+ 1 )),"]" );
357361 then ((DAE . CREF (DAE . CREF_IDENT (ident,t1,subs),t),(states,disc,algs)));
358362 case ((e as DAE . CREF (componentRef = cr as DAE . CREF_IDENT (_,t1,subs),ty= t),(states,disc,algs)))
359363 equation
360- p = List . position(cr,algs);
364+ // p = List.position(cr,algs);
365+ p = List . positionOnTrue(cr,algs,ComponentReference . crefEqual);
361366 ident = stringAppend(stringAppend("a[" ,intString(p+ 1 )),"]" );
362367 then ((DAE . CREF (DAE . CREF_IDENT (ident,t1,subs),t),(states,disc,algs)));
363368 case ((e as DAE . CREF (componentRef = cr as DAE . CREF_QUAL (_,t1,subs,_),ty= t),(states,disc,algs)))
364369 equation
365- p = List . position(cr,algs);
370+ // p = List.position(cr,algs);
371+ p = List . positionOnTrue(cr,algs,ComponentReference . crefEqual);
366372 ident = stringAppend(stringAppend("a[" ,intString(p+ 1 )),"]" );
367373 then ((DAE . CREF (DAE . CREF_IDENT (ident,t1,subs),t),(states,disc,algs)));
368374 case ((e as DAE . CREF (cr as DAE . CREF_IDENT (_,t1,subs),t),(states,disc,algs)))
@@ -385,6 +391,11 @@ algorithm
385391 then ((e,(states,disc,algs)));
386392 case ((DAE . CALL (Absyn . IDENT ("DIVISION" ),{e,e2,_},_),(states,disc,algs)))
387393 then ((DAE . BINARY (e,DAE . DIV (DAE . T_REAL_DEFAULT ),e2),(states,disc,algs)));
394+ case ((DAE . LBINARY (e as DAE . RCONST (_),DAE . AND (_),e2 as DAE . RELATION (_,_,_,_,_)),(states,disc,algs)))
395+ then ((DAE . BINARY (e,DAE . MUL (DAE . T_REAL_DEFAULT ),DAE . CALL (Absyn . IDENT ("boolToReal" ),{e2},DAE . callAttrBuiltinReal)),(states,disc,algs)));
396+ case ((DAE . LBINARY (e as DAE . RELATION (_,_,_,_,_),DAE . AND (_),e2 as DAE . RCONST (_)),(states,disc,algs)))
397+ then ((DAE . BINARY (e2,DAE . MUL (DAE . T_REAL_DEFAULT ),DAE . CALL (Absyn . IDENT ("boolToReal" ),{e},DAE . callAttrBuiltinReal)),(states,disc,algs)));
398+
388399 case ((e,(states,disc,algs)))
389400 equation
390401 /*
@@ -495,7 +506,8 @@ algorithm
495506 equation
496507 p= List . position(cr,algs);
497508 then stringAppend(stringAppend("a[" ,intString(p+ 1 )),"]" );
498-
509+ case (_,_,_,_)
510+ then ComponentReference . printComponentRefStr(cr);
499511 end matchcontinue;
500512end replaceCref;
501513
@@ -560,8 +572,226 @@ algorithm
560572 end matchcontinue;
561573end generateHandler;
562574
575+ function createDummyVars
576+ input Integer n;
577+ output list< DAE . ComponentRef > o;
578+ algorithm
579+ o:= match n
580+ case 0 then {};
581+ case _ then listAppend({DAE . CREF_IDENT ("dummy" ,DAE . T_REAL_DEFAULT ,{})},createDummyVars(n- 1 ));
582+ end match;
583+ end createDummyVars;
563584
585+ function computeAlgs
586+ input list< SimCode . SimEqSystem > eqs;
587+ input list< DAE . ComponentRef > states;
588+ input list< DAE . ComponentRef > i_algs;
589+ output list< DAE . ComponentRef > algs;
590+ algorithm
591+ algs:= matchcontinue (eqs,states,i_algs)
592+ local
593+ list< SimCode . SimEqSystem > tail;
594+ list< SimCode . SimVar > vars;
595+ SimCode . SimEqSystem eq;
596+ DAE . ComponentRef cref;
597+ list< DAE . ComponentRef > vars_cref;
598+ case (SimCode . SES_SIMPLE_ASSIGN (cref,_,_) :: tail,_,i_algs)
599+ equation
600+ true = List . notMember(cref,List . map(states,ComponentReference . crefPrefixDer));
601+ true = List . notMember(cref,states);
602+ print("Adding algebraic var:" );
603+ print(ComponentReference . printComponentRefStr(cref));
604+ print(" \n " );
605+ then computeAlgs(tail,states,listAppend(i_algs,{cref}));
606+ case ((SimCode . SES_LINEAR (vars= vars)) :: tail,_,i_algs)
607+ equation
608+ vars_cref = List . map(vars,SimCode . varName);
609+ then computeAlgs(tail,states,listAppend(i_algs,vars_cref));
610+ case ({},_,i_algs)
611+ equation
612+ then i_algs;
613+ case (_ :: tail,_,i_algs)
614+ then computeAlgs(tail,states,i_algs);
615+ end matchcontinue;
616+ end computeAlgs;
564617
618+ function getExpResidual
619+ input SimCode . SimEqSystem i;
620+ output DAE . Exp o;
621+ algorithm
622+ o:= match (i)
623+ local DAE . Exp e;
624+ case (SimCode . SES_RESIDUAL (e,_)) then e;
625+ end match;
626+ end getExpResidual;
627+
628+ function getExpCrefs
629+ input tuple< DAE . Exp ,list< DAE . ComponentRef > > i;
630+ output tuple< DAE . Exp ,list< DAE . ComponentRef >> out;
631+ algorithm
632+ out:= matchcontinue (i)
633+ local
634+ list< DAE . ComponentRef > l;
635+ DAE . ComponentRef cr;
636+ DAE . Exp e;
637+ case ((e as DAE . CREF (cr,_),l)) then ((e,listAppend(l,{cr})));
638+ case ((e,l)) then ((e,l));
639+ end matchcontinue;
640+ end getExpCrefs;
641+
642+ function getCrefs
643+ input list< DAE . Exp > e;
644+ input list< DAE . ComponentRef > acc;
645+ output list< DAE . ComponentRef > out;
646+ algorithm
647+ out:= match(e,acc)
648+ local DAE . Exp e1;
649+ list< DAE . Exp > tail;
650+ list< DAE . ComponentRef > l;
651+ case ({},acc) then acc;
652+ case (e1 :: tail ,acc)
653+ equation
654+ ((_,l)) = Expression . traverseExp(e1,getExpCrefs,{});
655+ then getCrefs(tail,listAppend(acc,l));
656+ end match;
657+ end getCrefs;
658+
659+ public function getRHSVars
660+ input list< DAE . Exp > beqs;
661+ input list< SimCode . SimVar > vars;
662+ input list< tuple< Integer , Integer , SimCode . SimEqSystem >> simJac;
663+ input list< DAE . ComponentRef > states,disc,algs;
664+ output list< DAE . ComponentRef > out;
665+ algorithm
666+ out:= matchcontinue (beqs,vars,simJac,states,disc,algs)
667+ local
668+ list< DAE . ComponentRef > vars_cref;
669+ list< DAE . Exp > eqs;
670+ case (_,_,_,_,_,_)
671+ equation
672+ vars_cref = getCrefs(beqs,{});
673+ eqs = List . map(List . map(simJac,Util . tuple33),getExpResidual);
674+ vars_cref = getCrefs(eqs,vars_cref);
675+ /* TODO: Check matrix A for discrete values */
676+ vars_cref = List . intersectionOnTrue(listAppend(states,listAppend(disc,algs)),vars_cref,ComponentReference . crefEqual);
677+ then vars_cref;
678+ end matchcontinue;
679+ end getRHSVars;
680+
681+ function getInitExp
682+ input list< SimCode . SimVar > vars;
683+ input DAE . ComponentRef d;
684+ output String s;
685+ algorithm
686+ s:=
687+ matchcontinue(vars,d)
688+ local
689+ list< SimCode . SimVar > tail;
690+ DAE . Exp initialExp;
691+ DAE . ComponentRef name;
692+ String t;
693+ case ({},_) then stringAppend(stringAppend("0.0 /*" ,ComponentReference . crefStr(d))," */ ;" );
694+ case (SimCode . SIMVAR (name= name,initialValue= SOME (initialExp as DAE . BCONST (_))):: tail,_)
695+ equation
696+ true = ComponentReference . crefEqual(name,d);
697+ t = stringAppend("boolToReal(" ,ExpressionDump . printExpStr(replaceVars(initialExp,{},{},{})));
698+ t = stringAppend(t,") /* " );
699+ t = stringAppend(t,ComponentReference . crefStr(d));
700+ t = stringAppend(t,"*/;" );
701+ then t;
702+ case (SimCode . SIMVAR (name= name,initialValue= SOME (initialExp)):: tail,_)
703+ equation
704+ true = ComponentReference . crefEqual(name,d);
705+ t = stringAppend("" ,ExpressionDump . printExpStr(replaceVars(initialExp,{},{},{})));
706+ t = stringAppend(t," /* " );
707+ t = stringAppend(t,ComponentReference . crefStr(d));
708+ t = stringAppend(t,"*/" );
709+ then t;
710+
711+ case (_:: tail,_) then getInitExp(tail,d);
712+ end matchcontinue;
713+ end getInitExp;
714+
715+ function getStartTime
716+ input SimCode . SampleCondition cond;
717+ output String s;
718+ algorithm
719+ s:=
720+ matchcontinue (cond)
721+ local
722+ DAE . Exp start;
723+ case ((DAE . CALL (path= Absyn . IDENT (name= "sample" ),expLst= (start:: _)),_) )
724+ then ExpressionDump . printExpStr(replaceVars(start,{},{},{}));
725+ end matchcontinue;
726+ end getStartTime;
727+
728+ public function generateDInit
729+ input list< DAE . ComponentRef > disc;
730+ input list< SimCode . SampleCondition > sample;
731+ input SimCode . SimVars vars;
732+ input Integer acc;
733+ input Integer total;
734+ input Integer nWhenClause;
735+ output String out;
736+ algorithm
737+ out:= matchcontinue(disc,sample,vars,acc,total,nWhenClause)
738+ local
739+ list< DAE . ComponentRef > tail;
740+ DAE . ComponentRef cref;
741+ String s;
742+ list< SimCode . SimVar > intAlgVars;
743+ list< SimCode . SimVar > boolAlgVars;
744+ case ({},_,_,_,_,_) then "" ;
745+ case (cref::tail,_,_,_,_,_)
746+ equation
747+ true = total - acc -1 < nWhenClause;
748+ s= stringAppend("" ,"d[" );
749+ s= stringAppend(s,intString(acc+ 1 ));
750+ s= stringAppend(s,"]:=" );
751+ s= stringAppend(s,getStartTime(listNth(sample,total- acc- 2 )));
752+ s= stringAppend(s,"; \n " );
753+ then stringAppend(s,generateDInit(tail,sample,vars,acc+ 1 ,total,nWhenClause));
754+
755+ case (cref::tail,_,SimCode . SIMVARS (intAlgVars= intAlgVars,boolAlgVars= boolAlgVars),_,_,_)
756+ equation
757+ s= stringAppend("" ,"d[" );
758+ s= stringAppend(s,intString(acc+ 1 ));
759+ s= stringAppend(s,"]:=" );
760+ s= stringAppend(s,getInitExp(listAppend(intAlgVars,boolAlgVars),cref));
761+ s= stringAppend(s," \n " );
762+ then stringAppend(s,generateDInit(tail,sample,vars,acc+ 1 ,total,nWhenClause));
763+
764+ end matchcontinue;
765+ end generateDInit;
766+
767+ public function generateExtraParams
768+ input SimCode . SimEqSystem eq;
769+ input SimCode . SimVars vars;
770+ output String s;
771+ algorithm
772+ s:=
773+ matchcontinue (eq,vars)
774+ local
775+ DAE . ComponentRef cref;
776+ list< SimCode . SimVar > paramVars;
777+ list< SimCode . SimVar > intParamVars;
778+ list< SimCode . SimVar > boolParamVars;
779+ Integer i;
780+ DAE . Exp exp;
781+ String t;
782+ case (SimCode . SES_SIMPLE_ASSIGN (cref= cref,exp= exp),
783+ SimCode . SIMVARS (paramVars= paramVars,intParamVars= intParamVars,boolParamVars= boolParamVars))
784+ equation
785+ failure(i = List . position(cref,List . map(paramVars,SimCode . varName)));
786+ failure(i = List . position(cref,List . map(intParamVars,SimCode . varName)));
787+ failure(i = List . position(cref,List . map(boolParamVars,SimCode . varName)));
788+ t = stringAppend("parameter Real " ,System . stringReplace(replaceCref(cref,{},{},{}),"." ,"_" ));
789+ t = stringAppend(t," = " );
790+ t = stringAppend(t,ExpressionDump . printExpStr(replaceVars(exp,{},{},{})));
791+ t = stringAppend(t,";" );
792+ then t;
793+ end matchcontinue;
794+ end generateExtraParams;
565795// //////////////////////////////////////////////////////////////////////////////////////////////////
566796// /// END OF PACKAGE
567797// //////////////////////////////////////////////////////////////////////////////////////////////////
0 commit comments