Changeset fbcb97e in OpenModelica


Ignore:
Timestamp:
2020-10-21T15:15:32+02:00 (3 years ago)
Author:
Adrian Pop <adrian.pop@…>
Children:
319df87
Parents:
ca0708d0
git-author:
arun3688 <rain100falls@…> (09/28/20 14:36:39)
git-committer:
Adrian Pop <adrian.pop@…> (10/21/20 15:15:32)
Message:

implement minimal set-s dataReconciliation algorithm

File:
1 edited

Legend:

Unmodified
Added
Removed
  • OMCompiler/Compiler/BackEnd/DataReconciliation.mo

    rc0e8727d rfbcb97e  
    8080  list<list<String>> allBlocksStatusVarInfo;
    8181  list<tuple<Integer, BackendDAE.Equation>> e_BLT_EquationsWithIndex;
    82   ExtAdjacencyMatrix eBltAdjacencyMatrix, sBltAdjacencyMatrix;
     82  ExtAdjacencyMatrix eBltAdjacencyMatrix, sBltAdjacencyMatrix, setS_BLTAdjacencyMatrix;
    8383  list<tuple<Integer, Integer>> e_BLTSolvedEqsAndVars;
    8484  list<tuple<list<Integer>, Integer>> e_BLTBlockRanks, s_BLTBlockRanks;
     
    9191  String str, modelicaOutput, modelicaFileName;
    9292
    93   list<Integer> allVarsList, knowns, unknowns, boundaryConditionVars, exactEquationVars, extractedVarsfromSetS, constantVars, knownVariablesWithEquationBinding, boundaryConditionTaggedEquationSolvedVars;
     93  list<Integer> allVarsList, knowns, unknowns, boundaryConditionVars, exactEquationVars, extractedVarsfromSetS, constantVars, knownVariablesWithEquationBinding, boundaryConditionTaggedEquationSolvedVars, unknownVarsInSetC;
    9494  BackendDAE.Variables inputVars, outDiffVars, outOtherVars, outResidualVars;
    9595
     
    329329  end if;
    330330
    331   extractedVarsfromSetS := getVariablesAfterExtraction({}, tempSetS, sBltAdjacencyMatrix);
    332   extractedVarsfromSetS := List.setDifferenceOnTrue(extractedVarsfromSetS, knowns, intEq);
     331  //extractedVarsfromSetS := getVariablesAfterExtraction({}, tempSetS, sBltAdjacencyMatrix);
     332  //extractedVarsfromSetS := List.setDifferenceOnTrue(extractedVarsfromSetS, knowns, intEq);
    333333
    334334  setC := List.unique(getAbsoluteIndexHelper(tempSetC, mapIncRowEqn));
    335335  setS := List.unique(getAbsoluteIndexHelper(tempSetS, mapIncRowEqn));
    336 
    337   //print("\nFINAL SET OF EQUATIONS After Mapping \n" + UNDERLINE + "\n" +"SET_C: "+dumplistInteger(setC)+"\n" +"SET_S: "+ dumplistInteger(setS)+ "\n\n" );
    338336
    339337  setC_Eq := getEquationsFromSBLTAndEBLT(setC, currentSystem.orderedEqs, e_BLT_EquationsWithIndex);
     
    344342  BackendDump.dumpEquationArray(BackendEquation.listEquation(setC_Eq), "SET_C");
    345343  BackendDump.dumpEquationArray(BackendEquation.listEquation(setS_Eq), "SET_S");
     344
     345  unknownVarsInSetC := getVariablesAfterExtraction(tempSetC, {}, sBltAdjacencyMatrix);
     346  unknownVarsInSetC := listReverse(List.setDifferenceOnTrue(unknownVarsInSetC, knowns, intEq));
     347
     348  // get the blt adjacencyMatrix asscoiated with extracted SET-S
     349  setS_BLTAdjacencyMatrix := getSetSAdjacencyMatrix(sBltAdjacencyMatrix, tempSetS);
     350
     351  if debug then
     352    print("\nStart of Extract Minimal Set-S Algorithm\n" + UNDERLINE + "\n");
     353    print("\nSet-S Adjacency MAtrix : " +  intString(listLength(setS_BLTAdjacencyMatrix)) + "\n" + UNDERLINE + "\n" + anyString(setS_BLTAdjacencyMatrix));
     354    print("\nS'        : {}");
     355    print("\nV_C       :" +  dumplistInteger(unknownVarsInSetC) + "\n");
     356  end if;
     357
     358  // run the minimal SET-S extraction algorithm
     359  (_, tempSetS) := extractMinimalSetS(unknownVarsInSetC, setS_BLTAdjacencyMatrix, knowns, currentSystem.orderedVars, currentSystem.orderedEqs, mapIncRowEqn, {}, debug);
     360
     361  if debug then
     362    print("\n****End of Minimal extraction Algorithm****\n");
     363    print("\nSet-S after running minimal extraction algorithm \n" + UNDERLINE + "\n" +"SET_S: "+dumplistInteger(tempSetS)+ "\n\n");
     364  end if;
     365
     366  extractedVarsfromSetS := getVariablesAfterExtraction({}, tempSetS, sBltAdjacencyMatrix);
     367  extractedVarsfromSetS := List.setDifferenceOnTrue(extractedVarsfromSetS, knowns, intEq);
     368
     369  setC := List.unique(getAbsoluteIndexHelper(tempSetC, mapIncRowEqn));
     370  setS := List.unique(getAbsoluteIndexHelper(tempSetS, mapIncRowEqn));
     371
     372  //print("\nFINAL SET OF EQUATIONS After Mapping \n" + UNDERLINE + "\n" +"SET_C: "+dumplistInteger(setC)+"\n" +"SET_S: "+ dumplistInteger(setS)+ "\n\n" );
     373
     374  setC_Eq := getEquationsFromSBLTAndEBLT(setC, currentSystem.orderedEqs, e_BLT_EquationsWithIndex);
     375  setS_Eq := getEquationsFromSBLTAndEBLT(setS, currentSystem.orderedEqs, e_BLT_EquationsWithIndex);
     376
     377  // dump minimal SET-S equations
     378  BackendDump.dumpEquationArray(BackendEquation.listEquation(setS_Eq), "SET_S_After_Minimal_Extraction");
    346379
    347380  // prepare outdiff vars (i.e) variables of interest
     
    418451  outDAE := BackendDAE.DAE({currentSystem}, shared);
    419452end extractionAlgorithm;
     453
     454protected function getSetSAdjacencyMatrix
     455 "return the adjacency matrix associated with set-S"
     456  input ExtAdjacencyMatrix sBltAdjacencyMatrix;
     457  input list<Integer> setS;
     458  output ExtAdjacencyMatrix setS_BltAdjacencyMatrix = {};
     459protected
     460  Integer eq;
     461algorithm
     462  for i in sBltAdjacencyMatrix loop
     463    (eq, _) := i;
     464     if listMember(eq, setS) then
     465       setS_BltAdjacencyMatrix := i :: setS_BltAdjacencyMatrix;
     466     end if;
     467   end for;
     468end getSetSAdjacencyMatrix;
     469
     470protected function extractMinimalSetS
     471  "construct a minimal set-S using recursive algorithm, which are needed to solve intermediate
     472  variables in set-c and also avoid complication when calculating jacobians"
     473  input output list<Integer> unknownsInSetC;
     474  input ExtAdjacencyMatrix sBltAdjacencyMatrix;
     475  input list<Integer> knownVars;
     476  input BackendDAE.Variables orderedVars;
     477  input BackendDAE.EquationArray orderedEqs;
     478  input array<Integer> mapIncRowEqn;
     479  input output list<Integer> minimalSetS = {};
     480  input Boolean debug;
     481protected
     482  Integer firstMatchedEquation, mappedEq;
     483  BackendDAE.Var var;
     484  BackendDAE.Equation tmpEq;
     485  list<Integer> rest, vars, intermediateVars = {}, V_EQ;
     486algorithm
     487  for varIndex in unknownsInSetC loop
     488    // break the recursion loop
     489    if listEmpty(unknownsInSetC) then
     490      break;
     491    end if;
     492    if debug then
     493      print("\nIntermediate varList : " + dumplistInteger(unknownsInSetC) + "\n" + UNDERLINE + "\n");
     494    end if;
     495    _ :: rest := unknownsInSetC;
     496    (firstMatchedEquation, vars) := getVariableFirstOccurrenceInEquation(sBltAdjacencyMatrix, varIndex, minimalSetS);
     497    var := BackendVariable.getVarAt(orderedVars, varIndex);
     498
     499    if not intEq(firstMatchedEquation, 0) then // equation exists
     500      minimalSetS := firstMatchedEquation :: minimalSetS; // insert the equation into S'
     501      minimalSetS := List.unique(minimalSetS);
     502      intermediateVars := List.setDifferenceOnTrue(vars, knownVars, intEq); // get intermediate vars in matched equations
     503      V_EQ := List.unique(listAppend(intermediateVars, rest));
     504      if debug then
     505        dumpMininimalExtraction(varIndex, var, firstMatchedEquation, mapIncRowEqn, orderedEqs, minimalSetS, intermediateVars, rest, V_EQ, false);
     506      end if;
     507      // V_EQ exist, recursive call
     508      (unknownsInSetC, minimalSetS) := extractMinimalSetS(V_EQ, sBltAdjacencyMatrix, knownVars, orderedVars, orderedEqs, mapIncRowEqn, minimalSetS, debug);
     509    else // equation not exist
     510      if debug then
     511        dumpMininimalExtraction(varIndex, var, 0, mapIncRowEqn, orderedEqs, {}, {}, rest, {}, true);
     512      end if;
     513      unknownsInSetC := rest; // update the list with the remaining Vars
     514    end if;
     515  end for;
     516end extractMinimalSetS;
     517
     518protected function dumpMininimalExtraction
     519  "dumps the minimal set-S extraction algorithm"
     520  input Integer varIndex;
     521  input BackendDAE.Var var;
     522  input Integer firstMatchedEquation;
     523  input array<Integer> mapIncRowEqn;
     524  input BackendDAE.EquationArray orderedEqs;
     525  input list<Integer> minimalSetS;
     526  input list<Integer> intermediateVars;
     527  input list<Integer> rest;
     528  input list<Integer> V_EQ;
     529  input Boolean falseBlock;
     530protected
     531  Integer mappedEq;
     532  BackendDAE.Equation tmpEq;
     533algorithm
     534  if falseBlock then
     535    print("\nVarIndex           : " + intString(varIndex));
     536    print("\nVariable Name      : " + ComponentReference.printComponentRefStr(var.varName));
     537    print("\nEquation Not Exist : " + "NIL");
     538    print("\nRemainingVars      : " + dumplistInteger(rest) + "\n");
     539  else
     540    mappedEq := listGet(arrayList(mapIncRowEqn), firstMatchedEquation);
     541    tmpEq := BackendEquation.get(orderedEqs, mappedEq);
     542    //print("\n" + "   ("  + intString(mappedEq) + "/"  + intString(firstMatchedEquation)  + "): " + BackendDump.equationString(tmpEq) + "\n");
     543    print("\nVarIndex                     : " + intString(varIndex));
     544    print("\nVariable Name                : " + ComponentReference.printComponentRefStr(var.varName));
     545    print("\nEquation Exist               : " + intString(firstMatchedEquation));
     546    print("\nmappedEquation               : " + intString(mappedEq));
     547    print("\nMatched Equation             : " + BackendDump.equationString(tmpEq));
     548    print("\nS'                           : " + dumplistInteger(minimalSetS));
     549    print("\nUnknowns in matchedEquation  : " + dumplistInteger(intermediateVars));
     550    print("\nRemaining Vars               : " + dumplistInteger(rest));
     551    print("\nV_EQ                         : " + dumplistInteger(V_EQ) + "\n");
     552  end if;
     553end dumpMininimalExtraction;
     554
     555protected function getVariableFirstOccurrenceInEquation
     556  "returns the first equation that contains the variable index (e.g)
     557   var index = 6
     558   BLT = {(1, {26, 5}), (2, {25, 6}, (3, {1, 2, 6})}
     559   result = (2,{25, 6})"
     560  input ExtAdjacencyMatrix m;
     561  input Integer varIndex;
     562  input list<Integer> minimalSetS;
     563  output tuple<Integer, list<Integer>> matchedEquation = (0, {}) "default value 0 means equation does not exist";
     564protected
     565  list<Integer> ret, vars, matchedeq;
     566  Integer eq, eqnum, varnum;
     567algorithm
     568  for i in m loop
     569    (eq, vars) := i;
     570    //print("\nVarcheck : " + intString(varIndex) + "=>" + anyString(eq) + " => " + anyString(vars));
     571    if eq > 0 then
     572      if not listMember(eq, minimalSetS) then
     573        if listMember(varIndex, vars) then
     574          //print("\n Found the first equation : " + anyString(i));
     575          matchedEquation := i;
     576          break;
     577        end if;
     578      end if;
     579    end if;
     580  end for;
     581end getVariableFirstOccurrenceInEquation;
    420582
    421583protected function dumpResidualVars
     
    510672
    511673  if debug then
    512      BackendDump.dumpVarList(daeVarsLst, "boundaryConditionVarsTaggedAsParmeters");
     674    BackendDump.dumpVarList(daeVarsLst, "boundaryConditionVarsTaggedAsParmeters");
    513675  end if;
    514676
     
    12451407  list<tuple<list<String>,Integer>> targetvarlist;
    12461408  list<String> blockvarlst;
    1247   list<Integer> ranklist,blocks1;
     1409  list<Integer> ranklist, blocks1;
    12481410  Integer rank;
    12491411  list<tuple<list<Integer>,Integer>> updatedblocks;
Note: See TracChangeset for help on using the changeset viewer.