Skip to content

Commit

Permalink
renamed functions:
Browse files Browse the repository at this point in the history
- bound_parameter -> updateBoundParameters
- initial_function -> updateBoundStartValues

[BUG #1676]
- solving under-determined initialization problems

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@10931 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
lochel committed Jan 20, 2012
1 parent 9f113c7 commit b7cccec
Show file tree
Hide file tree
Showing 11 changed files with 338 additions and 148 deletions.
22 changes: 16 additions & 6 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -426,16 +426,14 @@ end checkAssertCondition;

public function checkInitialSystem "function: checkInitialSystem
author: Frenkel TUD 2010-12

- check if the inital conditions full specified and fix it
if not.
"

- check if the inital conditions full specified and fix it if not."
input BackendDAE.BackendDAE inDAE;
input DAE.FunctionTree funcs;
output BackendDAE.BackendDAE outDAE;
algorithm
outDAE := mapEqSystem1(inDAE,checkInitialSystemWork,funcs);
// do not use this stuff
// outDAE := mapEqSystem1(inDAE,checkInitialSystemWork,funcs);
outDAE := inDAE;
end checkInitialSystem;

protected function checkInitialSystemWork "function: checkInitialSystem
Expand Down Expand Up @@ -486,6 +484,18 @@ algorithm
end matchcontinue;
end checkInitialSystemWork;

public function countInitialEquations "function: countInitialEquations
author: lochel
- count all initial equations and algorithms"
input BackendDAE.BackendDAE inDAE;
output Integer outE;
protected
BackendDAE.EquationArray initialEqs;
algorithm
BackendDAE.DAE(_, BackendDAE.SHARED(initialEqs=initialEqs)) := inDAE;
outE := equationSize(initialEqs);
end countInitialEquations;

protected function checkInitialSystem1"function: checkInitialSystem
author: Frenkel TUD 2010-12"
input Integer inUnfixed;
Expand Down
42 changes: 24 additions & 18 deletions Compiler/BackEnd/SimCode.mo
Expand Up @@ -228,6 +228,7 @@ uniontype VarInfo
Integer numBoolParams;
Integer numOutVars;
Integer numInVars;
Integer numInitEquations;
Integer numResiduals;
Integer numExternalObjects;
Integer numStringAlgVars;
Expand Down Expand Up @@ -2154,11 +2155,11 @@ algorithm
// new variables
ModelInfo modelInfo;
list<SimEqSystem> allEquations;
list<list<SimEqSystem>> odeEquations; // --> functionODE
list<list<SimEqSystem>> odeEquations; // --> functionODE
list<SimEqSystem> algebraicEquations; // --> functionAlgebraics
list<SimEqSystem> residualEquations; // --> initial_residual
list<SimEqSystem> initialEquations; // --> initial_function
list<SimEqSystem> parameterEquations;
list<SimEqSystem> initialEquations; // --> updateBoundStartValues
list<SimEqSystem> parameterEquations; // --> updateBoundParameters
list<SimEqSystem> removedEquations;
list<SimEqSystem> sampleEquations;
list<Algorithm.Statement> algorithmAndEquationAsserts;
Expand Down Expand Up @@ -2189,6 +2190,8 @@ algorithm
list<DAE.Exp> lits;
list<String> labels;

Integer nE;

/* //create simCode for labeled DAE
case (functionTree,dlow,class_,filenamePrefix,fileDir,functions,externalFunctionIncludes,includeDirs,libs,simSettingsOpt,recordDecls,literals,args)
equation
Expand Down Expand Up @@ -2410,8 +2413,9 @@ algorithm
//Debug.fcall(Flags.CPP_VAR,print, "is that Cpp? : " +& Dump.printBoolStr(ifcpp) +& "\n");
cname = Absyn.pathStringNoQual(class_);

(helpVarInfo,dlow2,sampleEqns) = generateHelpVarInfo(dlow);
(dlow2 as BackendDAE.DAE(systs,shared as BackendDAE.SHARED(removedEqs=removedEqs,algorithms=algs))) = BackendDAEUtil.checkInitialSystem(dlow2,functionTree);
(helpVarInfo, dlow2, sampleEqns) = generateHelpVarInfo(dlow);
(dlow2 as BackendDAE.DAE(systs, shared as BackendDAE.SHARED(removedEqs=removedEqs, algorithms=algs))) = BackendDAEUtil.checkInitialSystem(dlow2, functionTree);
nE = BackendDAEUtil.countInitialEquations(dlow2);

residualEquations = createResidualEquations(dlow2);
nres = listLength(residualEquations);
Expand All @@ -2425,7 +2429,7 @@ algorithm
n_h = listLength(helpVarInfo);

// Add model info
modelInfo = createModelInfo(class_, dlow2, functions, {}, n_h, nres, fileDir,ifcpp);
modelInfo = createModelInfo(class_, dlow2, functions, {}, n_h, nE, nres, fileDir,ifcpp);

// equation generation for euler, dassl2, rungekutta
(odeEquations,algebraicEquations,allEquations) = createEquationsForSystems(ifcpp,systs,shared,0,helpVarInfo,{},{},{});
Expand Down Expand Up @@ -2581,11 +2585,11 @@ algorithm
list<Function> functions;
list<String> labels;
Integer nx, ny, np, ng, ng_sam, ng_sam_1, next, ny_string, np_string, ng_1;
Integer nhv,nin, nresi, nout, ny_int, np_int, ny_bool, np_bool, na, na_int, na_bool, na_string;
Integer nhv,nin, niniteq, nresi, nout, ny_int, np_int, ny_bool, np_bool, na, na_int, na_bool, na_string;
Integer njacvars;
Option<Integer> dim1,dim2;
case(inJacs,minfo as (MODELINFO(name,dir,VARINFO(nhv, ng_1, ng_sam_1, nx, ny, ny_int, ny_bool, na, na_int, na_bool, np, np_int, np_bool, nout, nin,
nresi, next, ny_string, np_string, na_string,_,dim1,dim2),vars,functions,labels)))
niniteq, nresi, next, ny_string, np_string, na_string,_,dim1,dim2),vars,functions,labels)))
equation
jacvars = appendAllVars(inJacs);
jacvars = List.unionOnTrue(jacvars,jacvars,compareSimVarName);
Expand All @@ -2594,7 +2598,7 @@ algorithm
linearVars = SIMVARS({}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {},{},{},{},{},{},jacvars,{},{},{},{});
simvarsandlinearvars = mergeVars(vars,linearVars);
then MODELINFO(name, dir, VARINFO(nhv, ng_1, ng_sam_1, nx, ny, ny_int, ny_bool, na, na_int, na_bool, np, np_int, np_bool, nout, nin,
nresi, next, ny_string, np_string, na_string, njacvars,dim1,dim2), simvarsandlinearvars, functions,labels);
niniteq, nresi, next, ny_string, np_string, na_string, njacvars,dim1,dim2), simvarsandlinearvars, functions,labels);
end match;
end expandModelInfoVars;

Expand Down Expand Up @@ -6665,13 +6669,14 @@ protected function createModelInfo
input list<Function> functions;
input list<String> labels;
input Integer numHelpVars;
input Integer numInitEquations;
input Integer numResiduals;
input String fileDir;
input Boolean ifcpp;
output ModelInfo modelInfo;
algorithm
modelInfo :=
matchcontinue (class_, dlow, functions, labels, numHelpVars, numResiduals, fileDir,ifcpp)
matchcontinue (class_, dlow, functions, labels, numHelpVars, numInitEquations, numResiduals, fileDir,ifcpp)
local
String directory;
VarInfo varInfo;
Expand Down Expand Up @@ -6702,7 +6707,7 @@ algorithm
list<SimVar> states1,states_lst,states_lst2,der_states_lst;
list<SimVar> states_2,derivatives_2,derivative;

case (class_, dlow, functions, labels, numHelpVars, numResiduals, fileDir, true)
case (class_, dlow, functions, labels, numHelpVars, numInitEquations, numResiduals, fileDir, true)
equation
//name = Absyn.pathStringNoQual(class_);
directory = System.trim(fileDir, "\"");
Expand All @@ -6728,7 +6733,7 @@ algorithm
next = listLength(extObjVars);
(dim_1,dim_2)= dimensions(dlow);
Debug.fcall(Flags.CPP,print,"create varinfo \n");
varInfo = createVarInfo(dlow,nx, ny, np, na, next, numOutVars, numInVars, numHelpVars, numResiduals,
varInfo = createVarInfo(dlow,nx, ny, np, na, next, numOutVars, numInVars, numHelpVars, numInitEquations, numResiduals,
ny_int, np_int, na_int, ny_bool, np_bool, na_bool, ny_string, np_string, na_string,dim_1,dim_2);
Debug.fcall(Flags.CPP,print,"create state index \n");
states1 = stateindex1(stateVars,dlow);
Expand All @@ -6748,7 +6753,7 @@ algorithm
intAliasVars,boolAliasVars,paramVars,intParamVars,boolParamVars,stringAlgVars,stringParamVars,stringAliasVars,extObjVars,jacobianVars,constVars,intConstVars,boolConstVars,stringConstVars),
functions, labels);

case (class_, dlow, functions, labels, numHelpVars, numResiduals, fileDir, false)
case (class_, dlow, functions, labels, numHelpVars, numInitEquations, numResiduals, fileDir, false)
equation
//name = Absyn.pathStringNoQual(class_);
directory = System.trim(fileDir, "\"");
Expand All @@ -6773,7 +6778,7 @@ algorithm
np_string = listLength(stringParamVars);
na_string = listLength(stringAliasVars);
next = listLength(extObjVars);
varInfo = createVarInfo(dlow,nx, ny, np, na, next, numOutVars, numInVars, numHelpVars, numResiduals,
varInfo = createVarInfo(dlow,nx, ny, np, na, next, numOutVars, numInVars, numHelpVars, numInitEquations, numResiduals,
ny_int, np_int, na_int, ny_bool, np_bool, na_bool, ny_string, np_string, na_string,0,0);
then
MODELINFO(class_, directory, varInfo, vars, functions, labels);
Expand All @@ -6797,6 +6802,7 @@ protected function createVarInfo
input Integer numOutVars;
input Integer numInVars;
input Integer numHelpVars;
input Integer numInitEquations;
input Integer numResiduals;
input Integer ny_int;
input Integer np_int;
Expand All @@ -6812,21 +6818,21 @@ protected function createVarInfo
output VarInfo varInfo;
algorithm
varInfo :=
matchcontinue (dlow, nx, ny, np, na, next, numOutVars, numInVars, numHelpVars, numResiduals,
matchcontinue (dlow, nx, ny, np, na, next, numOutVars, numInVars, numHelpVars, numInitEquations, numResiduals,
ny_int, np_int, na_int, ny_bool, np_bool, na_bool, ny_string, np_string, na_string,dim_1,dim_2)
local
Integer ng, ng_sam, ng_sam_1, ng_1;
String s1,s2;
case (dlow, nx, ny, np, na, next, numOutVars, numInVars, numHelpVars, numResiduals,
case (dlow, nx, ny, np, na, next, numOutVars, numInVars, numHelpVars, numInitEquations, numResiduals,
ny_int, np_int, na_int, ny_bool, np_bool, na_bool, ny_string, np_string, na_string,dim_1,dim_2)
equation
(ng, ng_sam) = BackendDAEUtil.numberOfZeroCrossings(dlow);
ng_1 = filterNg(ng);
ng_sam_1 = filterNg(ng_sam);
then
VARINFO(numHelpVars, ng_1, ng_sam_1, nx, ny, ny_int, ny_bool, na, na_int, na_bool, np, np_int, np_bool, numOutVars, numInVars,
numResiduals, next, ny_string, np_string, na_string, 0,SOME(dim_1),SOME(dim_2));
case (_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_)
numInitEquations, numResiduals, next, ny_string, np_string, na_string, 0,SOME(dim_1),SOME(dim_2));
case (_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_)
equation
Error.addMessage(Error.INTERNAL_ERROR, {"createVarInfoCPP failed"});
then
Expand Down
27 changes: 18 additions & 9 deletions Compiler/susan_codegen/SimCode/CodegenC.tpl
Expand Up @@ -254,6 +254,7 @@ case MODELINFO(varInfo=VARINFO(__)) then

data->modelData.nZeroCrossings = <%varInfo.numZeroCrossings%>;
data->modelData.nSamples = <%varInfo.numTimeEvents%>;
data->modelData.nInitEquations = <%varInfo.numInitEquations%>;
data->modelData.nResiduals = <%varInfo.numResiduals%>;
data->modelData.nExtObjs = <%varInfo.numExternalObjects%>;
data->modelData.nFunctions = <%listLength(functions)%>;
Expand Down Expand Up @@ -693,7 +694,7 @@ template functionInitial(list<SimEqSystem> initialEquations)
;separator="\n")
<<
<%&tmp%>
int initial_function(DATA *data)
int updateBoundStartValues(DATA *data)
{
<%varDecls%>
Expand Down Expand Up @@ -798,7 +799,7 @@ template functionBoundParameters(list<SimEqSystem> parameterEquations)
;separator="\n")
<<
<%&tmp%>
int bound_parameters(DATA *data)
int updateBoundParameters(DATA *data)
{
state mem_state;
<%varDecls%>
Expand Down Expand Up @@ -6773,11 +6774,11 @@ template ScalarVariableType(String unit, String displayUnit, Option<DAE.Exp> min
"Generates code for ScalarVariable Type file for FMU target."
::=
match type_
case T_INTEGER(__) then '<Integer <%ScalarVariableTypeStartAttribute(initialValue)%> <%ScalarVariableTypeFixedAttribute(isFixed)%> <%ScalarVariableTypeNominalAttribute(nominalValue)%> <%ScalarVariableTypeIntegerMinAttribute(minValue)%> <%ScalarVariableTypeIntegerMaxAttribute(maxValue)%> <%ScalarVariableTypeUnitAttribute(unit)%> <%ScalarVariableTypeDisplayUnitAttribute(displayUnit)%> />'
case T_REAL(__) then '<Real <%ScalarVariableTypeStartAttribute(initialValue)%> <%ScalarVariableTypeFixedAttribute(isFixed)%> <%ScalarVariableTypeNominalAttribute(nominalValue)%> <%ScalarVariableTypeRealMinAttribute(minValue)%> <%ScalarVariableTypeRealMaxAttribute(maxValue)%> <%ScalarVariableTypeUnitAttribute(unit)%> <%ScalarVariableTypeDisplayUnitAttribute(displayUnit)%> />'
case T_BOOL(__) then '<Boolean <%ScalarVariableTypeStartAttribute(initialValue)%> <%ScalarVariableTypeFixedAttribute(isFixed)%> <%ScalarVariableTypeNominalAttribute(nominalValue)%> <%ScalarVariableTypeUnitAttribute(unit)%> <%ScalarVariableTypeDisplayUnitAttribute(displayUnit)%> />'
case T_STRING(__) then '<String <%ScalarVariableTypeStartAttribute(initialValue)%> <%ScalarVariableTypeFixedAttribute(isFixed)%> <%ScalarVariableTypeNominalAttribute(nominalValue)%> <%ScalarVariableTypeUnitAttribute(unit)%> <%ScalarVariableTypeDisplayUnitAttribute(displayUnit)%> />'
case T_ENUMERATION(__) then '<Integer <%ScalarVariableTypeStartAttribute(initialValue)%> <%ScalarVariableTypeFixedAttribute(isFixed)%> <%ScalarVariableTypeNominalAttribute(nominalValue)%> <%ScalarVariableTypeUnitAttribute(unit)%> <%ScalarVariableTypeDisplayUnitAttribute(displayUnit)%> />'
case T_INTEGER(__) then '<Integer <%ScalarVariableTypeStartAttribute(initialValue)%> <%ScalarVariableTypeFixedAttribute(isFixed)%> <%ScalarVariableTypeNominalAttribute(nominalValue, initialValue)%> <%ScalarVariableTypeIntegerMinAttribute(minValue)%> <%ScalarVariableTypeIntegerMaxAttribute(maxValue)%> <%ScalarVariableTypeUnitAttribute(unit)%> <%ScalarVariableTypeDisplayUnitAttribute(displayUnit)%> />'
case T_REAL(__) then '<Real <%ScalarVariableTypeStartAttribute(initialValue)%> <%ScalarVariableTypeFixedAttribute(isFixed)%> <%ScalarVariableTypeNominalAttribute(nominalValue, initialValue)%> <%ScalarVariableTypeRealMinAttribute(minValue)%> <%ScalarVariableTypeRealMaxAttribute(maxValue)%> <%ScalarVariableTypeUnitAttribute(unit)%> <%ScalarVariableTypeDisplayUnitAttribute(displayUnit)%> />'
case T_BOOL(__) then '<Boolean <%ScalarVariableTypeStartAttribute(initialValue)%> <%ScalarVariableTypeFixedAttribute(isFixed)%> <%ScalarVariableTypeNominalAttribute(nominalValue, initialValue)%> <%ScalarVariableTypeUnitAttribute(unit)%> <%ScalarVariableTypeDisplayUnitAttribute(displayUnit)%> />'
case T_STRING(__) then '<String <%ScalarVariableTypeStartAttribute(initialValue)%> <%ScalarVariableTypeFixedAttribute(isFixed)%> <%ScalarVariableTypeNominalAttribute(nominalValue, initialValue)%> <%ScalarVariableTypeUnitAttribute(unit)%> <%ScalarVariableTypeDisplayUnitAttribute(displayUnit)%> />'
case T_ENUMERATION(__) then '<Integer <%ScalarVariableTypeStartAttribute(initialValue)%> <%ScalarVariableTypeFixedAttribute(isFixed)%> <%ScalarVariableTypeNominalAttribute(nominalValue, initialValue)%> <%ScalarVariableTypeUnitAttribute(unit)%> <%ScalarVariableTypeDisplayUnitAttribute(displayUnit)%> />'
else 'UNKOWN_TYPE'
end ScalarVariableType;

Expand All @@ -6797,14 +6798,22 @@ fixed="<%isFixed%>"
>>
end ScalarVariableTypeFixedAttribute;

template ScalarVariableTypeNominalAttribute(Option<DAE.Exp> nominalValue)
template ScalarVariableTypeNominalAttribute(Option<DAE.Exp> nominalValue, Option<DAE.Exp> startValue)
"generates code for nominal attribute"
::=
match nominalValue
case SOME(exp) then 'nominal="<%initValXml(exp)%>"'
case NONE() then 'nominal="1.0"'
case NONE() then '<%ScalarVariableTypeNominalAttribute1(startValue)%>'
end ScalarVariableTypeNominalAttribute;

template ScalarVariableTypeNominalAttribute1(Option<DAE.Exp> startValue)
"generates code for nominal attribute"
::=
match startValue
case SOME(exp) then 'nominal="<%initValXml(exp)%>"'
case NONE() then 'nominal="1.0"'
end ScalarVariableTypeNominalAttribute1;

template ScalarVariableTypeUnitAttribute(String unit)
"generates code for unit attribute"
::=
Expand Down
1 change: 1 addition & 0 deletions Compiler/susan_codegen/SimCode/SimCodeTV.mo
Expand Up @@ -284,6 +284,7 @@ package SimCode
Integer numBoolParams;
Integer numOutVars;
Integer numInVars;
Integer numInitEquations;
Integer numResiduals;
Integer numExternalObjects;
Integer numStringAlgVars;
Expand Down

0 comments on commit b7cccec

Please sign in to comment.