Skip to content

Commit

Permalink
[SimCode] update linearize
Browse files Browse the repository at this point in the history
 - linearizing a system provides information about states, inputs and outputs
 - ticket #5927

[testsuite] update
  • Loading branch information
kabdelhak authored and adrpo committed May 3, 2020
1 parent 97aec02 commit e647134
Show file tree
Hide file tree
Showing 26 changed files with 217 additions and 50 deletions.
3 changes: 3 additions & 0 deletions OMCompiler/Compiler/SimCode/SimCode.mo
Expand Up @@ -211,6 +211,9 @@ uniontype ModelInfo "Container for metadata about a Modelica model."
record MODELINFO
Absyn.Path name;
String description;
String stateInfo;
String inputInfo;
String outputInfo;
String directory;
VarInfo varInfo;
SimCodeVar.SimVars vars;
Expand Down
38 changes: 33 additions & 5 deletions OMCompiler/Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -7211,7 +7211,7 @@ public function createModelInfo
input list<SimCodeVar.SimVar> tempVars;
output SimCode.ModelInfo modelInfo;
protected
String description, directory;
String description, directory, stateInfo = "", inputInfo = "", outputInfo = "";
SimCode.VarInfo varInfo;
SimCodeVar.SimVars vars;
Integer nx, ny, ndy, np, na, next, numOutVars, numInVars, ny_int, np_int, na_int, ny_bool, np_bool, dim_1, dim_2, numOptimizeConstraints, numOptimizeFinalConstraints;
Expand All @@ -7226,6 +7226,13 @@ algorithm
directory := System.trim(fileDir, "\"");
vars := createVars(dlow, inInitDAE, tempVars);

// Somehow this flag is not always active when needed...
//if Flags.getConfigBool(Flags.GENERATE_SYMBOLIC_LINEARIZATION) then
stateInfo := createInfoString(vars.stateVars, "states");
inputInfo := createInfoString(vars.inputVars, "inputs");
outputInfo := createInfoString(vars.outputVars, "outputs");
//end if;

if debug then execStat("simCode: createVars"); end if;
BackendDAE.DAE(shared=BackendDAE.SHARED(info=BackendDAE.EXTRA_INFO(description=description))) := dlow;
nx := getNumScalars(vars.stateVars);
Expand Down Expand Up @@ -7254,8 +7261,8 @@ algorithm
if debug then execStat("simCode: createVarInfo"); end if;
hasLargeEqSystems := hasLargeEquationSystems(dlow, inInitDAE);
if debug then execStat("simCode: hasLargeEquationSystems"); end if;
modelInfo := SimCode.MODELINFO(class_, dlow.shared.info.description, directory, varInfo, vars, functions,
labels,
modelInfo := SimCode.MODELINFO(class_, dlow.shared.info.description, stateInfo, inputInfo, outputInfo,
directory, varInfo, vars, functions, labels,
if Flags.getConfigBool(Flags.BUILDING_FMU) then getResources(program.classes, dlow, inInitDAE) else {},
List.sort(program.classes, AbsynUtil.classNameGreater),
arrayLength(dlow.shared.partitionsInfo.basePartitions),
Expand Down Expand Up @@ -8211,7 +8218,6 @@ algorithm
end match;
end simVarString;


protected function printVarIndx
input Option<Integer> i;
output String s;
Expand All @@ -8234,7 +8240,6 @@ algorithm
end if;
end dumpVarLst;


public function printVarLstCrefs
input list<SimCodeVar.SimVar> inVars;
output String str;
Expand All @@ -8249,6 +8254,29 @@ algorithm
end for;
end printVarLstCrefs;

public function createInfoString
input list<SimCodeVar.SimVar> inVars;
input String name;
output String str = "";
protected
SimCodeVar.SimVar var;
list<SimCodeVar.SimVar> rest;
DAE.ComponentRef cref;
algorithm
str := name + "[" + intString(listLength(inVars))+ "] = [";
try
var :: rest := inVars;
SimCodeVar.SIMVAR(name= cref) := var;
str := str + ComponentReference.printComponentRefStr(cref);
for var in rest loop
SimCodeVar.SIMVAR(name= cref) := var;
str := str + ", " + ComponentReference.printComponentRefStr(cref);
end for;
else
end try;
str := str + "]";
end createInfoString;

protected function dumpVariablesString "dumps a list of SimCode.Variables to stdout.
author: Waurich TUD 2014-09"
input list<SimCodeFunction.Variable> vars;
Expand Down
16 changes: 13 additions & 3 deletions OMCompiler/Compiler/Template/CodegenC.tpl
Expand Up @@ -4619,7 +4619,7 @@ template functionlinearmodel(ModelInfo modelInfo, String modelNamePrefix) "templ
Generates function in simulation file."
::=
match modelInfo
case MODELINFO(varInfo=VARINFO(__), vars=SIMVARS(__)) then
case MODELINFO(stateInfo=stateInfo, inputInfo=inputInfo, outputInfo=outputInfo, varInfo=VARINFO(__), vars=SIMVARS(__)) then
let matrixA = genMatrix("A", "n", "n", varInfo.numStateVars, varInfo.numStateVars)
let matrixB = genMatrix("B", "n", "p", varInfo.numStateVars, varInfo.numInVars)
let matrixC = genMatrix("C", "q", "n", varInfo.numOutVars, varInfo.numStateVars)
Expand All @@ -4634,7 +4634,12 @@ template functionlinearmodel(ModelInfo modelInfo, String modelNamePrefix) "templ
<<
const char *<%symbolName(modelNamePrefix,"linear_model_frame")%>()
{
return "model linear_<%underscorePath(name)%>\n parameter Integer n = <%varInfo.numStateVars%> \"number of states\";\n parameter Integer p = <%varInfo.numInVars%> \"number of inputs\";\n parameter Integer q = <%varInfo.numOutVars%> \"number of outputs\";\n"
return ""
"model linear_<%underscorePath(name)%> \"\n"
" <%stateInfo%>\n"
" <%inputInfo%>\n"
" <%outputInfo%>\"\n"
" parameter Integer n = <%varInfo.numStateVars%> \"number of states\";\n parameter Integer p = <%varInfo.numInVars%> \"number of inputs\";\n parameter Integer q = <%varInfo.numOutVars%> \"number of outputs\";\n"
"\n"
" parameter Real x0[n] = %s;\n"
" parameter Real u0[p] = %s;\n"
Expand All @@ -4655,7 +4660,12 @@ template functionlinearmodel(ModelInfo modelInfo, String modelNamePrefix) "templ
}
const char *<%symbolName(modelNamePrefix,"linear_model_datarecovery_frame")%>()
{
return "model linear_<%underscorePath(name)%>\n parameter Integer n = <%varInfo.numStateVars%> \"number of states\";\n parameter Integer p = <%varInfo.numInVars%> \"number of inputs\";\n parameter Integer q = <%varInfo.numOutVars%> \"number of outputs\";\n parameter Integer nz = <%varInfo.numAlgVars%> \"data recovery variables\";\n"
return ""
"model linear_<%underscorePath(name)%> \"\n"
" <%stateInfo%>\n"
" <%inputInfo%>\n"
" <%outputInfo%>\"\n"
" parameter Integer n = <%varInfo.numStateVars%> \"number of states\";\n parameter Integer p = <%varInfo.numInVars%> \"number of inputs\";\n parameter Integer q = <%varInfo.numOutVars%> \"number of outputs\";\n parameter Integer nz = <%varInfo.numAlgVars%> \"data recovery variables\";\n"
"\n"
" parameter Real x0[<%varInfo.numStateVars%>] = %s;\n"
" parameter Real u0[<%varInfo.numInVars%>] = %s;\n"
Expand Down
3 changes: 3 additions & 0 deletions OMCompiler/Compiler/Template/SimCodeTV.mo
Expand Up @@ -687,6 +687,9 @@ package SimCode
record MODELINFO
Absyn.Path name;
String description;
String stateInfo;
String inputInfo;
String outputInfo;
String directory;
VarInfo varInfo;
SimCodeVar.SimVars vars;
Expand Down
10 changes: 8 additions & 2 deletions testsuite/openmodelica/linearization/linmodel.mos
Expand Up @@ -47,7 +47,10 @@ readFile("linear_linearmodel.log"); // Check that output log is empty
// "Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// true
// "model linear_linearmodel
// "model linear_linearmodel \"
// states[4] = [x1, x2, x3, x4]
// inputs[0] = []
// outputs[0] = []\"
// parameter Integer n = 4 \"number of states\";
// parameter Integer p = 0 \"number of inputs\";
// parameter Integer q = 0 \"number of outputs\";
Expand Down Expand Up @@ -94,7 +97,10 @@ readFile("linear_linearmodel.log"); // Check that output log is empty
// "Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// true
// "model linear_linearmodel
// "model linear_linearmodel \"
// states[4] = [x1, x2, x3, x4]
// inputs[0] = []
// outputs[0] = []\"
// parameter Integer n = 4 \"number of states\";
// parameter Integer p = 0 \"number of inputs\";
// parameter Integer q = 0 \"number of outputs\";
Expand Down
10 changes: 8 additions & 2 deletions testsuite/openmodelica/linearization/simLotkaVolterra.mos
Expand Up @@ -47,7 +47,10 @@ readFile("linear_LotkaVolterra.log"); // Check that output log is empty
// "Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// true
// "model linear_LotkaVolterra
// "model linear_LotkaVolterra \"
// states[2] = [x, y]
// inputs[0] = []
// outputs[0] = []\"
// parameter Integer n = 2 \"number of states\";
// parameter Integer p = 0 \"number of inputs\";
// parameter Integer q = 0 \"number of outputs\";
Expand Down Expand Up @@ -92,7 +95,10 @@ readFile("linear_LotkaVolterra.log"); // Check that output log is empty
// "Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// true
// "model linear_LotkaVolterra
// "model linear_LotkaVolterra \"
// states[2] = [x, y]
// inputs[0] = []
// outputs[0] = []\"
// parameter Integer n = 2 \"number of states\";
// parameter Integer p = 0 \"number of inputs\";
// parameter Integer q = 0 \"number of outputs\";
Expand Down
10 changes: 8 additions & 2 deletions testsuite/openmodelica/linearization/simNonlinear.mos
Expand Up @@ -48,7 +48,10 @@ readFile("linear_p_nonlinsys.log"); // Check that output log is empty
// "Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// true
// "model linear_p_nonlinsys
// "model linear_p_nonlinsys \"
// states[3] = [b1.x, c1.x1, c1.x2]
// inputs[1] = [u1]
// outputs[2] = [y1, y2]\"
// parameter Integer n = 3 \"number of states\";
// parameter Integer p = 1 \"number of inputs\";
// parameter Integer q = 2 \"number of outputs\";
Expand Down Expand Up @@ -97,7 +100,10 @@ readFile("linear_p_nonlinsys.log"); // Check that output log is empty
// "Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// true
// "model linear_p_nonlinsys
// "model linear_p_nonlinsys \"
// states[3] = [b1.x, c1.x1, c1.x2]
// inputs[1] = [u1]
// outputs[2] = [y1, y2]\"
// parameter Integer n = 3 \"number of states\";
// parameter Integer p = 1 \"number of inputs\";
// parameter Integer q = 2 \"number of outputs\";
Expand Down
10 changes: 8 additions & 2 deletions testsuite/openmodelica/linearization/simTwoTank.mos
Expand Up @@ -48,7 +48,10 @@ readFile("linear_twoflattankmodel.log"); // Check that output log is empty
// "Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// true
// "model linear_twoflattankmodel
// "model linear_twoflattankmodel \"
// states[2] = [h1, h2]
// inputs[1] = [F]
// outputs[1] = [F2]\"
// parameter Integer n = 2 \"number of states\";
// parameter Integer p = 1 \"number of inputs\";
// parameter Integer q = 1 \"number of outputs\";
Expand Down Expand Up @@ -95,7 +98,10 @@ readFile("linear_twoflattankmodel.log"); // Check that output log is empty
// "Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// true
// "model linear_twoflattankmodel
// "model linear_twoflattankmodel \"
// states[2] = [h1, h2]
// inputs[1] = [F]
// outputs[1] = [F2]\"
// parameter Integer n = 2 \"number of states\";
// parameter Integer p = 1 \"number of inputs\";
// parameter Integer q = 1 \"number of outputs\";
Expand Down
10 changes: 8 additions & 2 deletions testsuite/openmodelica/linearization/simVanDerPol.mos
Expand Up @@ -42,7 +42,10 @@ readFile("linear_VanDerPol.log"); // Check that output log is empty
// "Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// true
// "model linear_VanDerPol
// "model linear_VanDerPol \"
// states[2] = [x, y]
// inputs[0] = []
// outputs[0] = []\"
// parameter Integer n = 2 \"number of states\";
// parameter Integer p = 0 \"number of inputs\";
// parameter Integer q = 0 \"number of outputs\";
Expand Down Expand Up @@ -92,7 +95,10 @@ readFile("linear_VanDerPol.log"); // Check that output log is empty
// "Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// true
// "model linear_VanDerPol
// "model linear_VanDerPol \"
// states[2] = [x, y]
// inputs[0] = []
// outputs[0] = []\"
// parameter Integer n = 2 \"number of states\";
// parameter Integer p = 0 \"number of inputs\";
// parameter Integer q = 0 \"number of outputs\";
Expand Down
20 changes: 16 additions & 4 deletions testsuite/openmodelica/linearization/simextfunction.mos
Expand Up @@ -70,7 +70,10 @@ readFile("linear_extfunction.log"); // Check that output log is empty
// "Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// true
// "model linear_extfunction
// "model linear_extfunction \"
// states[2] = [x, y]
// inputs[0] = []
// outputs[0] = []\"
// parameter Integer n = 2 \"number of states\";
// parameter Integer p = 0 \"number of inputs\";
// parameter Integer q = 0 \"number of outputs\";
Expand Down Expand Up @@ -123,7 +126,10 @@ readFile("linear_extfunction.log"); // Check that output log is empty
// "Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// true
// "model linear_extfunction
// "model linear_extfunction \"
// states[2] = [x, y]
// inputs[0] = []
// outputs[0] = []\"
// parameter Integer n = 2 \"number of states\";
// parameter Integer p = 0 \"number of inputs\";
// parameter Integer q = 0 \"number of outputs\";
Expand Down Expand Up @@ -176,7 +182,10 @@ readFile("linear_extfunction.log"); // Check that output log is empty
// "Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// true
// "model linear_extfunction
// "model linear_extfunction \"
// states[2] = [x, y]
// inputs[0] = []
// outputs[0] = []\"
// parameter Integer n = 2 \"number of states\";
// parameter Integer p = 0 \"number of inputs\";
// parameter Integer q = 0 \"number of outputs\";
Expand Down Expand Up @@ -221,7 +230,10 @@ readFile("linear_extfunction.log"); // Check that output log is empty
// "Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// true
// "model linear_extfunction
// "model linear_extfunction \"
// states[2] = [x, y]
// inputs[0] = []
// outputs[0] = []\"
// parameter Integer n = 2 \"number of states\";
// parameter Integer p = 0 \"number of inputs\";
// parameter Integer q = 0 \"number of outputs\";
Expand Down
10 changes: 8 additions & 2 deletions testsuite/openmodelica/linearization/smallValues.mos
Expand Up @@ -47,7 +47,10 @@ readFile("linear_VanDerPolSmallValue.log"); // Check that output log is empty
// "Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// true
// "model linear_VanDerPolSmallValue
// "model linear_VanDerPolSmallValue \"
// states[2] = [x, y]
// inputs[0] = []
// outputs[0] = []\"
// parameter Integer n = 2 \"number of states\";
// parameter Integer p = 0 \"number of inputs\";
// parameter Integer q = 0 \"number of outputs\";
Expand Down Expand Up @@ -92,7 +95,10 @@ readFile("linear_VanDerPolSmallValue.log"); // Check that output log is empty
// "Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// true
// "model linear_VanDerPolSmallValue
// "model linear_VanDerPolSmallValue \"
// states[2] = [x, y]
// inputs[0] = []
// outputs[0] = []\"
// parameter Integer n = 2 \"number of states\";
// parameter Integer p = 0 \"number of inputs\";
// parameter Integer q = 0 \"number of outputs\";
Expand Down
5 changes: 4 additions & 1 deletion testsuite/openmodelica/linearization/testArrayAlg.mos
Expand Up @@ -54,7 +54,10 @@ getErrorString();
// end SimulationResult;
// ""
// true
// "model linear_ticket4545_Test__vector
// "model linear_ticket4545_Test__vector \"
// states[2] = [x[1], x[2]]
// inputs[0] = []
// outputs[0] = []\"
// parameter Integer n = 2 \"number of states\";
// parameter Integer p = 0 \"number of inputs\";
// parameter Integer q = 0 \"number of outputs\";
Expand Down
7 changes: 5 additions & 2 deletions testsuite/openmodelica/linearization/testDrumBoiler.mos
Expand Up @@ -35,14 +35,17 @@ list(linear_testDrumBoilerLin);
// "Warning: The model contains alias variables with conflicting start and/or nominal values. It is recommended to resolve the conflicts, because otherwise the system could be hard to solve. To print the conflicting alias sets and the chosen candidates please use -d=aliasConflicts.
// "
// true
// "model linear_testDrumBoilerLin
// "model linear_testDrumBoilerLin \"
// states[3] = [controller.x, evaporator.V_l, evaporator.p]
// inputs[2] = [Y_Valve, q_F]
// outputs[4] = [T_S, V_l, p_S, qm_S]\"
// parameter Integer n = 3 \"number of states\";
// parameter Integer p = 2 \"number of inputs\";
// parameter Integer q = 4 \"number of outputs\";
// parameter Real x0[n] = {0, 67, 100000};
// parameter Real u0[p] = {0, 0};
// parameter Real A[n, n] = [-0, -0.008333333333333333, -0; 0.01043953430921842, -0.01043953430921842, 0; 0.1178989396709848, -0.1178989396709848, 4.135580766728708e-15];
// parameter Real B[n, p] = [0, 0; -0.001308242749261165, 0.0001170710024614632; -19.14622757506175, 8.475892309753029];
// parameter Real B[n, p] = [0, 0; -0.001308242749261165, 0.0001170710024614629; -19.14622757506173, 8.475892309753018];
// parameter Real C[q, n] = [0, 0, 0.0001439468903880623; 0, 1, 0; 0, 0, 1e-05; 0, 0, 0];
// parameter Real D[q, p] = [0, 0; 0, 0; 0, 0; 1, 0];
// Real x[n](start = x0);
Expand Down
10 changes: 8 additions & 2 deletions testsuite/openmodelica/linearization/testMathFuncs.mos
Expand Up @@ -59,7 +59,10 @@ readFile("linear_mathFuncsTest.log"); // Check that output log is empty
// "Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// true
// "model linear_mathFuncsTest
// "model linear_mathFuncsTest \"
// states[2] = [x, y]
// inputs[0] = []
// outputs[6] = [r1, r2, r3, r4, r5, r6]\"
// parameter Integer n = 2 \"number of states\";
// parameter Integer p = 0 \"number of inputs\";
// parameter Integer q = 6 \"number of outputs\";
Expand Down Expand Up @@ -123,7 +126,10 @@ readFile("linear_mathFuncsTest.log"); // Check that output log is empty
// "Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// true
// "model linear_mathFuncsTest
// "model linear_mathFuncsTest \"
// states[2] = [x, y]
// inputs[0] = []
// outputs[6] = [r1, r2, r3, r4, r5, r6]\"
// parameter Integer n = 2 \"number of states\";
// parameter Integer p = 0 \"number of inputs\";
// parameter Integer q = 6 \"number of outputs\";
Expand Down
5 changes: 4 additions & 1 deletion testsuite/openmodelica/linearization/testRecordDiff.mos
Expand Up @@ -59,7 +59,10 @@ list(linear_recordDiffTest);
// "Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
// "
// true
// "model linear_recordDiffTest
// "model linear_recordDiffTest \"
// states[1] = [x]
// inputs[0] = []
// outputs[1] = [o]\"
// parameter Integer n = 1 \"number of states\";
// parameter Integer p = 0 \"number of inputs\";
// parameter Integer q = 1 \"number of outputs\";
Expand Down

0 comments on commit e647134

Please sign in to comment.