Skip to content

Commit

Permalink
- revised initialization
Browse files Browse the repository at this point in the history
-- including full support of start-values
-- including scaling

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@9788 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
lochel committed Sep 12, 2011
1 parent 6a82127 commit 4f1a8f0
Show file tree
Hide file tree
Showing 15 changed files with 1,076 additions and 351 deletions.
12 changes: 6 additions & 6 deletions Compiler/BackEnd/BackendDAE.mo
Expand Up @@ -82,19 +82,19 @@ end VarKind;
public
uniontype Var "- Variables"
record VAR
.DAE.ComponentRef varName "varName ; variable name" ;
DAE.ComponentRef varName "varName ; variable name" ;
VarKind varKind "varKind ; Kind of variable" ;
.DAE.VarDirection varDirection "varDirection ; input, output or bidirectional" ;
DAE.VarDirection varDirection "varDirection ; input, output or bidirectional" ;
Type varType "varType ; builtin type or enumeration" ;
Option< .DAE.Exp> bindExp "bindExp ; Binding expression e.g. for parameters" ;
Option<Values.Value> bindValue "bindValue ; binding value for parameters" ;
.DAE.InstDims arryDim "arryDim ; array dimensions on nonexpanded var" ;
DAE.InstDims arryDim "arryDim ; array dimensions on nonexpanded var" ;
Integer index "index ; index in impl. vector" ;
.DAE.ElementSource source "origin of variable" ;
DAE.ElementSource source "origin of variable" ;
Option< .DAE.VariableAttributes> values "values ; values on builtin attributes" ;
Option<SCode.Comment> comment "comment ; this contains the comment and annotation from Absyn" ;
.DAE.Flow flowPrefix "flow ; if the variable is a flow" ;
.DAE.Stream streamPrefix "stream ; if the variable is a stream variable. Modelica 3.1 specs" ;
DAE.Flow flowPrefix "flow ; if the variable is a flow" ;
DAE.Stream streamPrefix "stream ; if the variable is a stream variable. Modelica 3.1 specs" ;
end VAR;
end Var;

Expand Down
280 changes: 228 additions & 52 deletions Compiler/BackEnd/SimCode.mo

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions Compiler/FrontEnd/DAEUtil.mo
Expand Up @@ -778,6 +778,17 @@ algorithm start:= match (inVariableAttributesOption)
end match;
end getStartAttrFail;

public function getNominalAttrFail "
Return the nominal attribute. or fails"
input Option<DAE.VariableAttributes> inVariableAttributesOption;
output DAE.Exp nominal;
algorithm nominal := match(inVariableAttributesOption)
local
DAE.Exp r;
case (SOME(DAE.VAR_ATTR_REAL(nominal = SOME(r)))) then r;
end match;
end getNominalAttrFail;

public function setVariableAttributes "sets the attributes of a DAE.Element that is VAR"
input DAE.Element var;
input Option<DAE.VariableAttributes> varOpt;
Expand Down
45 changes: 43 additions & 2 deletions Compiler/susan_codegen/SimCode/SimCodeC.tpl
Expand Up @@ -349,6 +349,32 @@ case MODELINFO(varInfo=VARINFO(__), vars=SIMVARS(__)) then
;separator=",\n"%>
};

char hasNominalValue[NX+NY+NP] = {
<%{(vars.stateVars |> SIMVAR(__) =>
'<%globalDataHasNominalValue(nominalValue)%> /* <%crefStr(name)%> */'
;separator=",\n"),
(vars.algVars |> SIMVAR(__) =>
'<%globalDataHasNominalValue(nominalValue)%> /* <%crefStr(name)%> */'
;separator=",\n"),
(vars.paramVars |> SIMVAR(__) =>
'<%globalDataHasNominalValue(nominalValue)%> /* <%crefStr(name)%> */'
;separator=",\n")}
;separator=",\n"%>
};

double nominalValue[NX+NY+NP] = {
<%{(vars.stateVars |> SIMVAR(__) =>
'<%globalDataNominalValue(nominalValue)%> /* <%crefStr(name)%> */'
;separator=",\n"),
(vars.algVars |> SIMVAR(__) =>
'<%globalDataNominalValue(nominalValue)%> /* <%crefStr(name)%> */'
;separator=",\n"),
(vars.paramVars |> SIMVAR(__) =>
'<%globalDataNominalValue(nominalValue)%> /* <%crefStr(name)%> */'
;separator=",\n")}
;separator=",\n"%>
};

char var_attr[NX+NY+NYINT+NYBOOL+NYSTR+NP+NPINT+NPBOOL+NPSTR] = {
<%{(vars.stateVars |> SIMVAR(__) =>
'<%globalDataAttrInt(type_)%>+<%globalDataDiscAttrInt(isDiscrete)%> /* <%crefStr(name)%> */'
Expand Down Expand Up @@ -489,6 +515,21 @@ template globalDataFixedInt(Boolean isFixed)
case false then "0"
end globalDataFixedInt;

template globalDataNominalValue(Option<DAE.Exp> nominal)
"Generates integer for use in arrays in global data section."
::=
match nominal
case NONE() then "0 /* default */"
case SOME(v) then initVal(v)
end globalDataNominalValue;

template globalDataHasNominalValue(Option<DAE.Exp> nominal)
"Generates integer for use in arrays in global data section."
::=
match nominal
case NONE() then "0"
case SOME(v) then "1"
end globalDataHasNominalValue;

template globalDataAttrInt(DAE.ExpType type)
"Generates integer for use in arrays in global data section."
Expand Down Expand Up @@ -762,7 +803,7 @@ template functionInitialResidual(list<SimEqSystem> residualEquations)
if (sim_verbose == LOG_RES_INIT) { printf(" Residual[%d] : <%ExpressionDump.printExpStr(exp)%> = %f\n",i,localData->initialResiduals[i-1]); }'
;separator="\n")
<<
int initial_residual()
int initial_residual(double $P$_lambda)
{
int i = 0;
state mem_state;
Expand Down Expand Up @@ -1088,7 +1129,7 @@ template functionODE_residual()
localData->timeValue = *t;
localData->states = x;
functionODE();
functionODE(1.0);
/* get the difference between the temp_xd(=localData->statesDerivatives)
and xd(=statesDerivativesBackup) */
Expand Down
1 change: 1 addition & 0 deletions Compiler/susan_codegen/SimCode/SimCodeTV.mo
Expand Up @@ -316,6 +316,7 @@ package SimCode
String displayUnit;
Integer index;
Option<DAE.Exp> initialValue;
Option<DAE.Exp> nominalValue;
Boolean isFixed;
DAE.ExpType type_;
Boolean isDiscrete;
Expand Down
3 changes: 3 additions & 0 deletions c_runtime/dummy_model.cpp
Expand Up @@ -704,6 +704,9 @@ int functionODE()
return 0;
}

/*
* calculate start-Values as expressions
*/
int initial_function()
{
return 0;
Expand Down
5 changes: 2 additions & 3 deletions c_runtime/fmu_model_interface.c
Expand Up @@ -545,9 +545,8 @@ fmiStatus fmiInitialize(fmiComponent c, fmiBoolean toleranceControlled, fmiReal
// Evaluate all constant equations
functionAliasEquations();


/*try{*/
if (main_initialize(0))
/*try{
if (initialize(NULL, NULL))
{
comp->functions.logger(c, comp->instanceName, fmiError, "log",
"fmiInitialize: main_initialize failed");
Expand Down
100 changes: 72 additions & 28 deletions c_runtime/simulation_events.cpp
Expand Up @@ -384,36 +384,80 @@ initSample(double start, double stop)

}

void
saveall()
/* function: saveall
*
* stores all the values for use with the pre-operator
*/
void saveall()
{
long i;
for (i = 0; i < globalData->nStates; i++)
{
x_saved[i] = globalData->states[i];
xd_saved[i] = globalData->statesDerivatives[i];
}
for (i = 0; i < globalData->nAlgebraic; i++)
{
y_saved[i] = globalData->algebraics[i];
}
for (i = 0; i < globalData->intVariables.nAlgebraic; i++)
{
int_saved[i] = globalData->intVariables.algebraics[i];
}
for (i = 0; i < globalData->boolVariables.nAlgebraic; i++)
{
bool_saved[i] = globalData->boolVariables.algebraics[i];
}
for (i = 0; i < globalData->nHelpVars; i++)
{
h_saved[i] = globalData->helpVars[i];
}
for (i = 0; i < globalData->stringVariables.nAlgebraic; i++)
{
str_saved[i] = globalData->stringVariables.algebraics[i];
}
for(fortran_integer i=0; i<globalData->nStates; i++)
{
x_saved[i] = globalData->states[i];
xd_saved[i] = globalData->statesDerivatives[i];
}

for(fortran_integer i = 0; i < globalData->nAlgebraic; i++)
{
y_saved[i] = globalData->algebraics[i];
}
for(long i = 0; i < globalData->intVariables.nAlgebraic; i++)
{
int_saved[i] = globalData->intVariables.algebraics[i];
}

for(long i = 0; i < globalData->boolVariables.nAlgebraic; i++)
{
bool_saved[i] = globalData->boolVariables.algebraics[i];
}

for(long i = 0; i < globalData->nHelpVars; i++)
{
h_saved[i] = globalData->helpVars[i];
}

for(long i = 0; i < globalData->stringVariables.nAlgebraic; i++)
{
str_saved[i] = globalData->stringVariables.algebraics[i];
}
}

/** function printAllPreValues
* author: lochel
*/
void printAllPreValues()
{
for(fortran_integer i=0; i<globalData->nStates; i++)
{
cout << "info | x_saved[" << i << "] = " << x_saved[i] << endl;
cout << "info | xd_saved[" << i << "] = " << xd_saved[i] << endl;
}

for(fortran_integer i = 0; i < globalData->nAlgebraic; i++)
{
cout << "info | y_saved[" << i << "] = " << y_saved[i] << endl;
}

for(long i = 0; i < globalData->intVariables.nAlgebraic; i++)
{
cout << "info | int_saved[" << i << "] = " << int_saved[i] << endl;
}

for(long i = 0; i < globalData->boolVariables.nAlgebraic; i++)
{
cout << "info | bool_saved[" << i << "] = " << (bool_saved[i] ? "true" : "false") << endl;
}

for(long i = 0; i < globalData->nHelpVars; i++)
{
cout << "info | h_saved[" << i << "] = " << h_saved[i] << endl;
}

for(long i = 0; i < globalData->stringVariables.nAlgebraic; i++)
{
cout << "info | str_saved[" << i << "] = " << str_saved[i] << endl;
}
}

/** function restoreHelpVars
* author: wbraun
*
Expand Down
4 changes: 2 additions & 2 deletions c_runtime/simulation_events.h
Expand Up @@ -61,8 +61,8 @@ initializeEventData();
void
deinitializeEventData();

void
saveall();
void saveall();
void printAllPreValues();
void
restoreHelpVars();

Expand Down

0 comments on commit 4f1a8f0

Please sign in to comment.