Changeset 6b8ad0f in OpenModelica


Ignore:
Timestamp:
2016-03-20T18:45:04+01:00 (8 years ago)
Author:
Martin Sjölund <martin.sjolund@…>
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:
0c3dbc0d
Parents:
b9f7f01
Message:

Fix scaling of getHighestDerivation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Compiler/SimCode/SimCodeUtil.mo

    r73f0312 r6b8ad0f  
    227227  list<tuple<Integer, Integer>> equationSccMapping, eqBackendSimCodeMapping;
    228228  list<tuple<Integer, tuple<DAE.Exp, DAE.Exp, DAE.Exp>>> delayedExps;
     229  constant Boolean debug = false;
    229230algorithm
    230231  try
     
    276277      equationSccMapping, eqBackendSimCodeMapping, backendMapping, sccOffset) :=
    277278          createEquationsForSystems(contSysts, shared, uniqueEqIndex, zeroCrossings, tempvars, 1, backendMapping);
     279    if debug then execStat("simCode: createEquationsForSystems"); end if;
    278280    (clockedPartitions, uniqueEqIndex, backendMapping, equationSccMapping, eqBackendSimCodeMapping, tempvars) :=
    279281          translateClockedEquations(clockedSysts, dlow.shared, sccOffset, uniqueEqIndex,
    280282                                    backendMapping, equationSccMapping, eqBackendSimCodeMapping, tempvars);
     283    if debug then execStat("simCode: translateClockedEquations"); end if;
    281284    outMapping := (uniqueEqIndex /* highestSimEqIndex */, equationSccMapping);
    282285    execStat("simCode: created simulation system equations");
     
    285288    //((uniqueEqIndex, removedEquations)) := BackendEquation.traverseEquationArray(BackendEquation.listEquation(remEqLst), traversedlowEqToSimEqSystem, (uniqueEqIndex, {}));
    286289    ((uniqueEqIndex, removedEquations)) := BackendEquation.traverseEquationArray(removedEqs, traversedlowEqToSimEqSystem, (uniqueEqIndex, {}));
     290    if debug then execStat("simCode: traversedlowEqToSimEqSystem"); end if;
    287291    // Assertions and crap
    288292    // create parameter equations
    289293    ((uniqueEqIndex, startValueEquations)) := BackendDAEUtil.foldEqSystem(dlow, createStartValueEquations, (uniqueEqIndex, {}));
     294    if debug then execStat("simCode: createStartValueEquations"); end if;
    290295    ((uniqueEqIndex, nominalValueEquations)) := BackendDAEUtil.foldEqSystem(dlow, createNominalValueEquations, (uniqueEqIndex, {}));
     296    if debug then execStat("simCode: createNominalValueEquations"); end if;
    291297    ((uniqueEqIndex, minValueEquations)) := BackendDAEUtil.foldEqSystem(dlow, createMinValueEquations, (uniqueEqIndex, {}));
     298    if debug then execStat("simCode: createMinValueEquations"); end if;
    292299    ((uniqueEqIndex, maxValueEquations)) := BackendDAEUtil.foldEqSystem(dlow, createMaxValueEquations, (uniqueEqIndex, {}));
     300    if debug then execStat("simCode: createMaxValueEquations"); end if;
    293301    ((uniqueEqIndex, parameterEquations)) := BackendDAEUtil.foldEqSystem(dlow, createVarNominalAssertFromVars, (uniqueEqIndex, {}));
     302    if debug then execStat("simCode: createVarNominalAssertFromVars"); end if;
    294303    (uniqueEqIndex, parameterEquations) := createParameterEquations(uniqueEqIndex, parameterEquations, inPrimaryParameters, inAllPrimaryParameters);
     304    if debug then execStat("simCode: createParameterEquations"); end if;
    295305    //((uniqueEqIndex, paramAssertSimEqs)) := BackendEquation.traverseEquationArray(BackendEquation.listEquation(paramAsserts), traversedlowEqToSimEqSystem, (uniqueEqIndex, {}));
    296306    //parameterEquations := listAppend(parameterEquations, paramAssertSimEqs);
    297307
    298308    ((uniqueEqIndex, algorithmAndEquationAsserts)) := BackendDAEUtil.foldEqSystem(dlow, createAlgorithmAndEquationAsserts, (uniqueEqIndex, {}));
     309    if debug then execStat("simCode: createAlgorithmAndEquationAsserts"); end if;
    299310    discreteModelVars := BackendDAEUtil.foldEqSystem(dlow, extractDiscreteModelVars, {});
     311    if debug then execStat("simCode: extractDiscreteModelVars"); end if;
    300312    makefileParams := SimCodeFunctionUtil.createMakefileParams(includeDirs, libs, libPaths, false, isFMU);
    301313    (delayedExps, maxDelayedExpIndex) := extractDelayedExpressions(dlow);
     
    310322    // state set stuff
    311323    (dlow, stateSets, uniqueEqIndex, tempvars, numStateSets) := createStateSets(dlow, {}, uniqueEqIndex, tempvars);
     324    if debug then execStat("simCode: createStateSets"); end if;
    312325
    313326    // create model info
    314327    modelInfo := createModelInfo(inClassName, dlow, inInitDAE, functions, {}, numStateSets, inFileDir, listLength(clockedSysts));
     328    if debug then execStat("simCode: createModelInfo"); end if;
    315329    modelInfo := addTempVars(tempvars, modelInfo);
    316330    execStat("simCode: created modelInfo and variables");
     
    57505764  list<SimCodeVar.SimVar> states_2, derivatives_2;
    57515765  Boolean hasLargeEqSystems;
     5766  constant Boolean debug = false;
    57525767algorithm
    57535768  try
     
    57555770    directory := System.trim(fileDir, "\"");
    57565771    vars := createVars(dlow, inInitDAE);
     5772    if debug then execStat("simCode: createVars"); end if;
    57575773    BackendDAE.DAE(shared=BackendDAE.SHARED(info=BackendDAE.EXTRA_INFO(description=description))) := dlow;
    57585774    nx := listLength(vars.stateVars);
     
    57755791    numOptimizeConstraints := listLength(vars.realOptimizeConstraintsVars);
    57765792    numOptimizeFinalConstraints := listLength(vars.realOptimizeFinalConstraintsVars);
     5793    if debug then execStat("simCode: get lengths"); end if;
    57775794    varInfo := createVarInfo(dlow, nx, ny, ndy, np, na, next, numOutVars, numInVars,
    57785795                             ny_int, np_int, na_int, ny_bool, np_bool, na_bool, ny_string, np_string, na_string,
    57795796                             numStateSets, numOptimizeConstraints, numOptimizeFinalConstraints);
     5797    if debug then execStat("simCode: createVarInfo"); end if;
    57805798    maxDer := getHighestDerivation(dlow);
     5799    if debug then execStat("simCode: getHighestDerivation"); end if;
    57815800    hasLargeEqSystems := hasLargeEquationSystems(dlow, inInitDAE);
     5801    if debug then execStat("simCode: hasLargeEquationSystems"); end if;
    57825802    modelInfo := SimCode.MODELINFO(class_, dlow.shared.info.description, directory, varInfo, vars, functions,
    57835803                                   labels, maxDer, arrayLength(dlow.shared.partitionsInfo.basePartitions),
     
    1189911919  list<BackendDAE.Var> vars, states;
    1190011920  list<Integer> idcs;
    11901   array<Boolean> markVars;
     11921  array<Integer> ders, depth;
     11922  BackendDAE.Variables allStates;
     11923  BackendDAE.Var var;
     11924  Integer index, pos, length, curIndex;
     11925  DAE.ComponentRef derCref;
    1190211926algorithm
    1190311927  vars := BackendDAEUtil.getAllVarLst(inDAE);
    1190411928  states := List.filterOnTrue(vars, BackendVariable.isStateVar);
    11905   if listEmpty(states) then
     11929  length := listLength(states);
     11930  if length==0 then
    1190611931    highestDerivation := 0;
    11907   else
    11908     markVars := arrayCreate(listLength(states),false);
    11909     idcs := List.map3(states,getHighestDerivation1,BackendVariable.listVar1(states),markVars,0);
    11910     highestDerivation := List.fold(idcs,intMax,0);
     11932    return;
    1191111933  end if;
     11934  ders := arrayCreate(length,-1 /* Has no derivative */);
     11935  depth := arrayCreate(length,-1 /* Not visited */);
     11936  allStates := BackendVariable.listVar1(states);
     11937  // Setup data structures for a dynamic programming algorithm
     11938  curIndex := 1;
     11939  for state in states loop
     11940    // (_, {curIndex}) := BackendVariable.getVar(state.varName, allStates); // They are all already in order
     11941    _ := matchcontinue state
     11942      case BackendDAE.VAR(varKind=BackendDAE.STATE(index=index /* TODO: Do we need the number of times it was differentiated? */, derName = SOME(derCref)))
     11943        algorithm
     11944          ({var},{pos}) := BackendVariable.getVar(derCref, allStates);
     11945          if not BackendVariable.varEqual(state, var) then
     11946            arrayUpdate(ders, curIndex, pos);
     11947          else
     11948            arrayUpdate(depth, curIndex, 0);
     11949          end if;
     11950        then ();
     11951      case BackendDAE.VAR()
     11952        algorithm
     11953          arrayUpdate(depth, curIndex, 0);
     11954        then ();
     11955    end matchcontinue;
     11956    curIndex := curIndex+1;
     11957  end for;
     11958  // Visit all states, calculating the depth of each one, remembering the result
     11959  for i in 1:length loop
     11960    getHighestDerivationVisit(i, ders, depth);
     11961  end for;
     11962  highestDerivation := max(i for i in depth);
    1191211963end getHighestDerivation;
    1191311964
    11914 protected function getHighestDerivation1"checks if a state is the derivative of another state and so on.
    11915 author: waurich TUD 2015-05"
    11916   input BackendDAE.Var stateIn;
    11917   input BackendDAE.Variables allStates;
    11918   input array<Boolean> markVarsIn;
    11919   input Integer derivationIn;
    11920   output Integer derivationOut;
    11921 algorithm
    11922   derivationOut := matchcontinue(stateIn,allStates,markVarsIn,derivationIn)
    11923     local
    11924       Integer index, pos;
    11925       array<Boolean> markVars;
    11926       BackendDAE.Var var;
    11927       DAE.ComponentRef derCref;
    11928   case(BackendDAE.VAR(varKind=BackendDAE.STATE(index=index,derName = SOME(derCref))),_,_,_)
    11929     algorithm
    11930       // try to find the derivative in the states
    11931       ({var},{pos}) := BackendVariable.getVar(derCref, allStates);
    11932       // has this var already been checked or is the derivative the var itself?
    11933       false := arrayGet(markVarsIn,pos);
    11934       false := BackendVariable.varEqual(stateIn,var);
    11935       markVars := arrayUpdate(markVarsIn,pos,true);
    11936     then getHighestDerivation1(var,allStates,markVars,derivationIn+1);
    11937   else
    11938     algorithm
    11939       for i in List.intRange(arrayLength(markVarsIn)) loop
    11940         _ := arrayUpdate(markVarsIn,i,false);
    11941       end for;
    11942     then derivationIn+1;
    11943   end matchcontinue;
    11944 end getHighestDerivation1;
     11965protected function getHighestDerivationVisit "Uses stack depth of at most max depth"
     11966  input Integer i;
     11967  input array<Integer> ders;
     11968  input array<Integer> depth;
     11969  output Integer d=arrayGet(depth, i);
     11970algorithm
     11971  if d >= 0 then
     11972    return;
     11973  elseif d == -2 then
     11974    d := 0;
     11975    return;
     11976  end if;
     11977  arrayUpdate(depth, i, -2);
     11978  d := getHighestDerivationVisit(arrayGet(ders,i), ders, depth);
     11979  arrayUpdate(depth, i, d);
     11980end getHighestDerivationVisit;
    1194511981
    1194611982protected function hasLargeEquationSystems "Returns true if the model contains large linear or nonlinear equation
Note: See TracChangeset for help on using the changeset viewer.