Changeset 6b8ad0f in OpenModelica
- Timestamp:
- 2016-03-20T18:45:04+01:00 (8 years ago)
- 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
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Compiler/SimCode/SimCodeUtil.mo
r73f0312 r6b8ad0f 227 227 list<tuple<Integer, Integer>> equationSccMapping, eqBackendSimCodeMapping; 228 228 list<tuple<Integer, tuple<DAE.Exp, DAE.Exp, DAE.Exp>>> delayedExps; 229 constant Boolean debug = false; 229 230 algorithm 230 231 try … … 276 277 equationSccMapping, eqBackendSimCodeMapping, backendMapping, sccOffset) := 277 278 createEquationsForSystems(contSysts, shared, uniqueEqIndex, zeroCrossings, tempvars, 1, backendMapping); 279 if debug then execStat("simCode: createEquationsForSystems"); end if; 278 280 (clockedPartitions, uniqueEqIndex, backendMapping, equationSccMapping, eqBackendSimCodeMapping, tempvars) := 279 281 translateClockedEquations(clockedSysts, dlow.shared, sccOffset, uniqueEqIndex, 280 282 backendMapping, equationSccMapping, eqBackendSimCodeMapping, tempvars); 283 if debug then execStat("simCode: translateClockedEquations"); end if; 281 284 outMapping := (uniqueEqIndex /* highestSimEqIndex */, equationSccMapping); 282 285 execStat("simCode: created simulation system equations"); … … 285 288 //((uniqueEqIndex, removedEquations)) := BackendEquation.traverseEquationArray(BackendEquation.listEquation(remEqLst), traversedlowEqToSimEqSystem, (uniqueEqIndex, {})); 286 289 ((uniqueEqIndex, removedEquations)) := BackendEquation.traverseEquationArray(removedEqs, traversedlowEqToSimEqSystem, (uniqueEqIndex, {})); 290 if debug then execStat("simCode: traversedlowEqToSimEqSystem"); end if; 287 291 // Assertions and crap 288 292 // create parameter equations 289 293 ((uniqueEqIndex, startValueEquations)) := BackendDAEUtil.foldEqSystem(dlow, createStartValueEquations, (uniqueEqIndex, {})); 294 if debug then execStat("simCode: createStartValueEquations"); end if; 290 295 ((uniqueEqIndex, nominalValueEquations)) := BackendDAEUtil.foldEqSystem(dlow, createNominalValueEquations, (uniqueEqIndex, {})); 296 if debug then execStat("simCode: createNominalValueEquations"); end if; 291 297 ((uniqueEqIndex, minValueEquations)) := BackendDAEUtil.foldEqSystem(dlow, createMinValueEquations, (uniqueEqIndex, {})); 298 if debug then execStat("simCode: createMinValueEquations"); end if; 292 299 ((uniqueEqIndex, maxValueEquations)) := BackendDAEUtil.foldEqSystem(dlow, createMaxValueEquations, (uniqueEqIndex, {})); 300 if debug then execStat("simCode: createMaxValueEquations"); end if; 293 301 ((uniqueEqIndex, parameterEquations)) := BackendDAEUtil.foldEqSystem(dlow, createVarNominalAssertFromVars, (uniqueEqIndex, {})); 302 if debug then execStat("simCode: createVarNominalAssertFromVars"); end if; 294 303 (uniqueEqIndex, parameterEquations) := createParameterEquations(uniqueEqIndex, parameterEquations, inPrimaryParameters, inAllPrimaryParameters); 304 if debug then execStat("simCode: createParameterEquations"); end if; 295 305 //((uniqueEqIndex, paramAssertSimEqs)) := BackendEquation.traverseEquationArray(BackendEquation.listEquation(paramAsserts), traversedlowEqToSimEqSystem, (uniqueEqIndex, {})); 296 306 //parameterEquations := listAppend(parameterEquations, paramAssertSimEqs); 297 307 298 308 ((uniqueEqIndex, algorithmAndEquationAsserts)) := BackendDAEUtil.foldEqSystem(dlow, createAlgorithmAndEquationAsserts, (uniqueEqIndex, {})); 309 if debug then execStat("simCode: createAlgorithmAndEquationAsserts"); end if; 299 310 discreteModelVars := BackendDAEUtil.foldEqSystem(dlow, extractDiscreteModelVars, {}); 311 if debug then execStat("simCode: extractDiscreteModelVars"); end if; 300 312 makefileParams := SimCodeFunctionUtil.createMakefileParams(includeDirs, libs, libPaths, false, isFMU); 301 313 (delayedExps, maxDelayedExpIndex) := extractDelayedExpressions(dlow); … … 310 322 // state set stuff 311 323 (dlow, stateSets, uniqueEqIndex, tempvars, numStateSets) := createStateSets(dlow, {}, uniqueEqIndex, tempvars); 324 if debug then execStat("simCode: createStateSets"); end if; 312 325 313 326 // create model info 314 327 modelInfo := createModelInfo(inClassName, dlow, inInitDAE, functions, {}, numStateSets, inFileDir, listLength(clockedSysts)); 328 if debug then execStat("simCode: createModelInfo"); end if; 315 329 modelInfo := addTempVars(tempvars, modelInfo); 316 330 execStat("simCode: created modelInfo and variables"); … … 5750 5764 list<SimCodeVar.SimVar> states_2, derivatives_2; 5751 5765 Boolean hasLargeEqSystems; 5766 constant Boolean debug = false; 5752 5767 algorithm 5753 5768 try … … 5755 5770 directory := System.trim(fileDir, "\""); 5756 5771 vars := createVars(dlow, inInitDAE); 5772 if debug then execStat("simCode: createVars"); end if; 5757 5773 BackendDAE.DAE(shared=BackendDAE.SHARED(info=BackendDAE.EXTRA_INFO(description=description))) := dlow; 5758 5774 nx := listLength(vars.stateVars); … … 5775 5791 numOptimizeConstraints := listLength(vars.realOptimizeConstraintsVars); 5776 5792 numOptimizeFinalConstraints := listLength(vars.realOptimizeFinalConstraintsVars); 5793 if debug then execStat("simCode: get lengths"); end if; 5777 5794 varInfo := createVarInfo(dlow, nx, ny, ndy, np, na, next, numOutVars, numInVars, 5778 5795 ny_int, np_int, na_int, ny_bool, np_bool, na_bool, ny_string, np_string, na_string, 5779 5796 numStateSets, numOptimizeConstraints, numOptimizeFinalConstraints); 5797 if debug then execStat("simCode: createVarInfo"); end if; 5780 5798 maxDer := getHighestDerivation(dlow); 5799 if debug then execStat("simCode: getHighestDerivation"); end if; 5781 5800 hasLargeEqSystems := hasLargeEquationSystems(dlow, inInitDAE); 5801 if debug then execStat("simCode: hasLargeEquationSystems"); end if; 5782 5802 modelInfo := SimCode.MODELINFO(class_, dlow.shared.info.description, directory, varInfo, vars, functions, 5783 5803 labels, maxDer, arrayLength(dlow.shared.partitionsInfo.basePartitions), … … 11899 11919 list<BackendDAE.Var> vars, states; 11900 11920 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; 11902 11926 algorithm 11903 11927 vars := BackendDAEUtil.getAllVarLst(inDAE); 11904 11928 states := List.filterOnTrue(vars, BackendVariable.isStateVar); 11905 if listEmpty(states) then 11929 length := listLength(states); 11930 if length==0 then 11906 11931 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; 11911 11933 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); 11912 11963 end getHighestDerivation; 11913 11964 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; 11965 protected 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); 11970 algorithm 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); 11980 end getHighestDerivationVisit; 11945 11981 11946 11982 protected function hasLargeEquationSystems "Returns true if the model contains large linear or nonlinear equation
Note: See TracChangeset
for help on using the changeset viewer.