Changeset 17340ed in OpenModelica


Ignore:
Timestamp:
2016-04-03T23:16:21+02:00 (8 years ago)
Author:
hudson <openmodelica@…>
Branches:
Added-citation-metadata, maintenance/v1.14, maintenance/v1.15, maintenance/v1.16, maintenance/v1.17, maintenance/v1.18, maintenance/v1.19, maintenance/v1.20, maintenance/v1.21, maintenance/v1.22, maintenance/v1.23, master, omlib-staging
Children:
a2edcfd
Parents:
d7ed464
git-author:
vruge <vruge@…> (04/03/16 23:16:21)
git-committer:
hudson <openmodelica@…> (04/03/16 23:16:21)
Message:

improved simplifyComplexFunction

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Compiler/BackEnd/BackendDAEOptimize.mo

    r7eb2bd0 r17340ed  
    41534153//
    41544154// e.g. (a,-b) = f(.) -> (a,c) = f(.) with c = -b
     4155//      (a,b) = (c,d) -> a=c and b = d
     4156//      {a,b} = {c,d} -> a=c and b = d
    41554157//      (a,b) = f(a) fixed iterration var
    41564158// author: Vitalij Ruge
    41574159// =============================================================================
    41584160
     4161
    41594162public function simplifyComplexFunction
    41604163  input BackendDAE.BackendDAE inDAE;
     4164  output BackendDAE.BackendDAE outDAE;
     4165algorithm
     4166  outDAE := simplifyComplexFunction1(inDAE, true);
     4167end simplifyComplexFunction;
     4168
     4169public function simplifyComplexFunction1
     4170  input BackendDAE.BackendDAE inDAE;
     4171  input Boolean withTmpVars = false;
    41614172  output BackendDAE.BackendDAE outDAE = inDAE;
    41624173protected
     
    41844195    case BackendDAE.SHARED(backendDAEType=BackendDAE.SIMULATION()) then "$OMC$CF$sim";
    41854196    case BackendDAE.SHARED(backendDAEType=BackendDAE.INITIALSYSTEM()) then "$OMC$CF$init";
    4186     else fail();
     4197    else "$OMC$CF$unknown";
    41874198  end match;
    41884199
    41894200  for syst in inDAE.eqs loop
     4201    indRemove := {};
    41904202    BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns) := syst;
    41914203    BackendDAE.EQUATION_ARRAY(numberOfElement = n) := eqns;
    41924204    update := false;
    4193   indRemove := {};
     4205    indRemove := {};
     4206
    41944207    for i in 1:n loop
    4195       eqn := BackendEquation.equationNth1(eqns, i);
    4196       if BackendEquation.isComplexEquation(eqn) then
    4197         BackendDAE.COMPLEX_EQUATION(left=left, right=right, size=size, attr= attr, source=source) := eqn;
     4208      try
     4209        eqn := BackendEquation.equationNth1(eqns, i);
     4210      else
     4211        continue;
     4212      end try;
     4213      if BackendEquation.isComplexEquation(eqn) or BackendEquation.isArrayEquation(eqn) then
     4214        if BackendEquation.isComplexEquation(eqn) then
     4215          BackendDAE.COMPLEX_EQUATION(size=size,left=left, right=right, attr= attr, source=source) := eqn;
     4216        else
     4217          BackendDAE.ARRAY_EQUATION(left=left, right=right, attr= attr, source=source) := eqn;
     4218        end if;
     4219
    41984220        if Expression.isTuple(left) and Expression.isTuple(right) then // tuple() = tuple()
    41994221          //print(BackendDump.equationString(eqn) + "--In--\n");
     
    42224244            end if; // isWild
    42234245          end for;
    4224          elseif Expression.isTuple(left) and Expression.isCall(right) then //tuple() = call()
    4225           DAE.TUPLE(PR = left_lst) := left;
    4226           DAE.CALL(path=path,expLst = expLst, attr= cattr) := right;
    4227           expLst := {};
    4228           for e1 in left_lst loop
    4229             if Expression.isCref(e1) then
    4230                DAE.CREF(componentRef = cr) := e1;
    4231                if Expression.expHasCrefNoPreOrStart(right, cr) then
    4232                 update := true;
    4233                 cr  := ComponentReference.makeCrefIdent(tmpVarPrefix + intString(idx), Expression.typeof(e1) , {});
    4234                 idx := idx + 1;
    4235                 e := Expression.crefExp(cr);
    4236                 tmpvar := BackendVariable.makeVar(cr);
    4237                 tmpvar := BackendVariable.setVarTS(tmpvar,SOME(BackendDAE.AVOID()));
    4238                 vars := BackendVariable.addVar(tmpvar, vars);
    4239 
    4240                 eqn1 := BackendDAE.EQUATION(e, e1, DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
    4241                 eqns := BackendEquation.addEquation(eqn1, eqns);
    4242                else
    4243                 e := e1;
    4244                end if;
     4246      elseif Expression.isArray(left) and Expression.isArray(right)
     4247      then // array{} = array{} // not work with arrayType
     4248          //print(BackendDump.equationString(eqn) + "--In--\n");
     4249        try
     4250          left_lst := Expression.getArrayOrRangeContents(left);
     4251          right_lst := Expression.getArrayOrRangeContents(right);
     4252          update := true;
     4253          indRemove := i :: indRemove;
     4254          for e1 in left_lst loop
     4255          e2 :: right_lst := right_lst;
     4256          //print("=>" +  ExpressionDump.printExpStr(e2) + " = " +  ExpressionDump.printExpStr(e1) + "\n");
     4257          if not Expression.isWild(e1) then
     4258            if Expression.isScalar(e2) then
     4259            eqn1 := BackendEquation.generateEquation(e1, e2, source, attr);
     4260            eqns := BackendEquation.addEquation(eqn1, eqns);
     4261            //print(BackendDump.equationString(eqn1) + "--new--\n");
     4262            else
     4263            expLst := simplifyComplexFunction2(e1);
     4264            arrayLst := simplifyComplexFunction2(e2);
     4265            for e_asub in arrayLst loop
     4266              e3 :: expLst := expLst;
     4267              eqn1 := BackendEquation.generateEquation(e_asub, e3, source, attr);
     4268              eqns := BackendEquation.addEquation(eqn1, eqns);
     4269              //print(BackendDump.equationString(eqn1) + "--new--\n");
     4270            end for;
     4271            end if; //isScalar
     4272          end if; // isWild
     4273          end for;
     4274        else
     4275          continue;
     4276        end try;
     4277      elseif withTmpVars and  Expression.isTuple(left) and Expression.isCall(right)  //tuple() = call()
     4278      then
     4279        DAE.TUPLE(PR = left_lst) := left;
     4280        DAE.CALL(path=path,expLst = expLst, attr= cattr) := right;
     4281        expLst := {};
     4282        for e1 in left_lst loop
     4283          if Expression.isCref(e1) then
     4284            DAE.CREF(componentRef = cr) := e1;
     4285            if Expression.expHasCrefNoPreOrStart(right, cr) then
     4286              update := true;
     4287              cr  := ComponentReference.makeCrefIdent(tmpVarPrefix + intString(idx), Expression.typeof(e1) , {});
     4288              idx := idx + 1;
     4289              e := Expression.crefExp(cr);
     4290              tmpvar := BackendVariable.makeVar(cr);
     4291              tmpvar := BackendVariable.setVarTS(tmpvar,SOME(BackendDAE.AVOID()));
     4292              vars := BackendVariable.addVar(tmpvar, vars);
     4293
     4294              eqn1 := BackendDAE.EQUATION(e, e1, DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
     4295              eqns := BackendEquation.addEquation(eqn1, eqns);
     4296            else
     4297              e := e1;
     4298            end if;
    42454299            elseif Expression.isUnaryCref(e1) then
    42464300              update := true;
     
    42854339          end for; // lhs
    42864340          left := DAE.TUPLE(MetaModelica.Dangerous.listReverseInPlace(expLst));
    4287           eqn := BackendDAE.COMPLEX_EQUATION(size, left, right, source, attr);
     4341          eqn := BackendEquation.generateEquation(left, right, source, attr);
    42884342          eqns := BackendEquation.setAtIndex(eqns, i, eqn);
    42894343        end if; // lhs <-> rhs
     
    42934347
    42944348    if update then
    4295       for i in indRemove loop
     4349      for i in listReverse(indRemove) loop
     4350      //print("\neqns:" + intString(i) + "\n");
     4351      //BackendDump.printEquationArray(eqns);
    42964352        eqns := BackendEquation.equationRemove(i,eqns);
    42974353      end for;
     
    43044360
    43054361  outDAE.eqs := systlst;
    4306 end simplifyComplexFunction;
     4362end simplifyComplexFunction1;
    43074363
    43084364function simplifyComplexFunction2
Note: See TracChangeset for help on using the changeset viewer.