Skip to content

Commit

Permalink
[SimCode] update linearization dump languages
Browse files Browse the repository at this point in the history
  - ticket #5927
  • Loading branch information
kabdelhak authored and adrpo committed May 11, 2020
1 parent c747575 commit 074bb17
Show file tree
Hide file tree
Showing 5 changed files with 279 additions and 1 deletion.
57 changes: 56 additions & 1 deletion OMCompiler/Compiler/Template/CodegenC.tpl
Expand Up @@ -4709,6 +4709,11 @@ template functionlinearmodelMatlab(ModelInfo modelInfo, String modelNamePrefix)
<%matrixB%>
<%matrixC%>
<%matrixD%>
"\n"
<%getVarNameMatlab(vars.stateVars, "x")%>
<%getVarNameMatlab(vars.inputVars, "u")%>
<%getVarNameMatlab(vars.outputVars, "y")%>
<%getVarNameMatlab(vars.algVars, "z")%>
"end";
}
const char *<%symbolName(modelNamePrefix,"linear_model_datarecovery_frame")%>()
Expand All @@ -4724,6 +4729,11 @@ template functionlinearmodelMatlab(ModelInfo modelInfo, String modelNamePrefix)
<%matrixB%>
<%matrixC%>
<%matrixD%>
"\n"
<%getVarNameMatlab(vars.stateVars, "x")%>
<%getVarNameMatlab(vars.inputVars, "u")%>
<%getVarNameMatlab(vars.outputVars, "y")%>
<%getVarNameMatlab(vars.algVars, "z")%>
"end";
}
>>
Expand Down Expand Up @@ -4756,6 +4766,12 @@ template functionlinearmodelJulia(ModelInfo modelInfo, String modelNamePrefix) "
<%matrixB%>
<%matrixC%>
<%matrixD%>
"\n"
<%getVarNameJulia(vars.stateVars, "x")%>
<%getVarNameJulia(vars.inputVars, "u")%>
<%getVarNameJulia(vars.outputVars, "y")%>
<%getVarNameJulia(vars.algVars, "z")%>
"\n"
" (n, p, q, x0, u0, A, B, C, D)\n"
"end";
}
Expand All @@ -4772,14 +4788,19 @@ template functionlinearmodelJulia(ModelInfo modelInfo, String modelNamePrefix) "
<%matrixB%>
<%matrixC%>
<%matrixD%>
"\n"
<%getVarNameJulia(vars.stateVars, "x")%>
<%getVarNameJulia(vars.inputVars, "u")%>
<%getVarNameJulia(vars.outputVars, "y")%>
<%getVarNameJulia(vars.algVars, "z")%>
"\n"
" (n, p, q, x0, u0, A, B, C, D)\n"
"end";
}
>>
end match
end functionlinearmodelJulia;


template functionlinearmodelPython(ModelInfo modelInfo, String modelNamePrefix) "template functionlinearmodelPython
Generates python functions in simulation file."
::=
Expand All @@ -4806,6 +4827,12 @@ template functionlinearmodelPython(ModelInfo modelInfo, String modelNamePrefix)
<%matrixB%>
<%matrixC%>
<%matrixD%>
"\n"
<%getVarNameMatlab(vars.stateVars, "x")%>
<%getVarNameMatlab(vars.inputVars, "u")%>
<%getVarNameMatlab(vars.outputVars, "y")%>
<%getVarNameMatlab(vars.algVars, "z")%>
"\n"
" return (n, p, q, x0, u0, A, B, C, D)\n"
"end";
}
Expand All @@ -4822,6 +4849,12 @@ template functionlinearmodelPython(ModelInfo modelInfo, String modelNamePrefix)
<%matrixB%>
<%matrixC%>
<%matrixD%>
"\n"
<%getVarNameMatlab(vars.stateVars, "x")%>
<%getVarNameMatlab(vars.inputVars, "u")%>
<%getVarNameMatlab(vars.outputVars, "y")%>
<%getVarNameMatlab(vars.algVars, "z")%>
"\n"
" return (n, p, q, x0, u0, A, B, C, D)\n"
"end";
}
Expand All @@ -4840,6 +4873,28 @@ template getVarName(list<SimVar> simVars, String arrayName) "template getVarName
; empty
end getVarName;

template getVarNameMatlab(list<SimVar> simVars, String arrayName) "template getVarName
Generates name for a varables."
::=
simVars |> var hasindex arrindex fromindex 1 =>
(match var
case SIMVAR(__) then
<<" <%arrayName%>_<%Util.stringReplaceChar(crefStrNoUnderscore(name), '.', "_")%> = <%arrayName%>[<%arrindex%>];\n">>
end match)
; empty
end getVarNameMatlab;

template getVarNameJulia(list<SimVar> simVars, String arrayName) "template getVarName
Generates name for a varables."
::=
simVars |> var hasindex arrindex fromindex 1 =>
(match var
case SIMVAR(__) then
<<" local <%arrayName%>_<%Util.stringReplaceChar(crefStrNoUnderscore(name), '.', "_")%> = <%arrayName%>[<%arrindex%>];\n">>
end match)
; empty
end getVarNameJulia;

template genMatrix(String name, String row, String col, Integer rowI, Integer colI) "template genMatrix
Generates Matrix for linear model"
::=
Expand Down
7 changes: 7 additions & 0 deletions OMCompiler/Compiler/Template/SimCodeTV.mo
Expand Up @@ -3234,6 +3234,13 @@ package Util
end DATETIME;
end DateTime;

function stringReplaceChar
input String inString1;
input String inString2;
input String inString3;
output String outString;
end stringReplaceChar;

function escapeModelicaStringToCString
input String modelicaString;
output String cString;
Expand Down
1 change: 1 addition & 0 deletions testsuite/openmodelica/linearization/Makefile
Expand Up @@ -22,6 +22,7 @@ test_04.mos \
test_05.mos \
test_06.mos \
test_07.mos \
test_dump_languages.mos \
testArrayAlg.mos \
testDrumBoiler.mos \
testknownvar.mos \
Expand Down
37 changes: 37 additions & 0 deletions testsuite/openmodelica/linearization/test_dump_languages.mo
@@ -0,0 +1,37 @@
// name: test_04.mo
// keywords: <insert keywords here>
// status: correct
//
// <insert description here>
//

model simple_test
number x1(x(start=1));
Real x2(start=2);
parameter Real a=6,b=2,c=4;
input Real u = sin(0);
output Real y;
class number
Real x;
end number;
equation
der(x1.x) = x1.x*(a-b*x1.x-x2);
der(x2) = x2*(c-x1.x-x2);
y = x1.x * u + x2 * u;
end simple_test;

// Result:
// class simple_test
// Real x1(start = 1.0);
// Real x2(start = 2.0);
// parameter Real a = 6.0;
// parameter Real b = 2.0;
// parameter Real c = 4.0;
// input Real u = 0.0;
// output Real y;
// equation
// der(x1) = x1 * (a - b * x1 - x2);
// der(x2) = x2 * (c - x1 - x2);
// y = x1 * u + x2 * u;
// end simple_test;
// endResult
178 changes: 178 additions & 0 deletions testsuite/openmodelica/linearization/test_dump_languages.mos
@@ -0,0 +1,178 @@
// name: test_dump_languages.mos
// keywords: <...>
// status: correct
// teardown_command: rm -rf *simple_test* output.log
//
// <insert description here>
//

loadFile("test_dump_languages.mo"); getErrorString();

setCommandLineOptions("--linearizationDumpLanguage=modelica");
linearize(simple_test, stopTime=0); getErrorString();
readFile("linear_simple_test.mo"); getErrorString();

setCommandLineOptions("--linearizationDumpLanguage=matlab");
linearize(simple_test, stopTime=0); getErrorString();
readFile("linear_simple_test.mo"); getErrorString();

setCommandLineOptions("--linearizationDumpLanguage=julia");
linearize(simple_test, stopTime=0); getErrorString();
readFile("linear_simple_test.mo"); getErrorString();

setCommandLineOptions("--linearizationDumpLanguage=python");
linearize(simple_test, stopTime=0); getErrorString();
readFile("linear_simple_test.mo"); getErrorString();

// Result:
// true
// ""
// true
// record SimulationResult
// resultFile = "simple_test_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 0.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'simple_test', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
// messages = "stdout | info | Linearization will performed at point of time: 0.000000
// LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// LOG_SUCCESS | info | The simulation finished successfully.
// stdout | info | Linear model is created!
// "
// end SimulationResult;
// "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").
// "
// "model linear_simple__test
// parameter Integer n = 2 \"number of states\";
// parameter Integer p = 1 \"number of inputs\";
// parameter Integer q = 1 \"number of outputs\";
//
// parameter Real x0[n] = {1, 2};
// parameter Real u0[p] = {0};
//
// parameter Real A[n, n] = [-2.665600749850023e-07, -1.000000001354202; -2.000000002708404, -1.00000010131423];
// parameter Real B[n, p] = [0; 0];
// parameter Real C[q, n] = [0, 0];
// parameter Real D[q, p] = [3];
//
// Real x[n](start=x0);
// input Real u[p](start=u0);
// output Real y[q];
//
// Real 'x_x1.x' = x[1];
// Real 'x_x2' = x[2];
// Real 'u_u' = u[1];
// Real 'y_y' = y[1];
// equation
// der(x) = A * x + B * u;
// y = C * x + D * u;
// end linear_simple__test;
// "
// ""
// true
// record SimulationResult
// resultFile = "simple_test_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 0.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'simple_test', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
// messages = "stdout | info | Linearization will performed at point of time: 0.000000
// LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// LOG_SUCCESS | info | The simulation finished successfully.
// stdout | info | Linear model is created!
// "
// end SimulationResult;
// "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").
// "
// "function [n, p, q, x0, u0, A, B, C, D] = simple_test_GetLinearModel()
// % der(x) = A * x + B * u
// % y = C * x + D * u
// n = 2; % number of states
// p = 1; % number of inputs
// q = 1; % number of outputs
//
// x0 = {1, 2};
// u0 = {0};
//
// A = [-2.665600749850023e-07, -1.000000001354202; -2.000000002708404, -1.00000010131423];
// B = [0; 0];
// C = [0, 0];
// D = [3];
//
// x_x1_x = x[1];
// x_x2 = x[2];
// u_u = u[1];
// y_y = y[1];
// z_u = z[1];
// z_y = z[2];
// end"
// ""
// true
// record SimulationResult
// resultFile = "simple_test_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 0.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'simple_test', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
// messages = "stdout | info | Linearization will performed at point of time: 0.000000
// LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// LOG_SUCCESS | info | The simulation finished successfully.
// stdout | info | Linear model is created!
// "
// end SimulationResult;
// "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").
// "
// "function simple_test_GetLinearModel()
// #= der(x) = A * x + B * u =#
// #= y = C * x + D * u =#
// local n = 2 #= number of states =#
// local p = 1 #= number of inputs =#
// local q = 1 #= number of outputs =#
//
// local x0 = {1, 2}
// local u0 = {0}
//
// local A = [-2.665600749850023e-07, -1.000000001354202; -2.000000002708404, -1.00000010131423]
// local B = [0; 0]
// local C = [0, 0]
// local D = [3]
//
// local x_x1_x = x[1];
// local x_x2 = x[2];
// local u_u = u[1];
// local y_y = y[1];
// local z_u = z[1];
// local z_y = z[2];
//
// (n, p, q, x0, u0, A, B, C, D)
// end"
// ""
// true
// record SimulationResult
// resultFile = "simple_test_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 0.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'simple_test', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
// messages = "stdout | info | Linearization will performed at point of time: 0.000000
// LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// LOG_SUCCESS | info | The simulation finished successfully.
// stdout | info | Linear model is created!
// "
// end SimulationResult;
// "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").
// "
// "def simple_test_GetLinearModel()
// # der(x) = A * x + B * u
// # y = C * x + D * u
// n = 2 # number of states
// p = 1 # number of inputs
// q = 1 # number of outputs
//
// x0 = {1, 2}
// u0 = {0}
//
// A = [-2.665600749850023e-07, -1.000000001354202; -2.000000002708404, -1.00000010131423];
// B = [0; 0];
// C = [0, 0];
// D = [3];
//
// x_x1_x = x[1];
// x_x2 = x[2];
// u_u = u[1];
// y_y = y[1];
// z_u = z[1];
// z_y = z[2];
//
// return (n, p, q, x0, u0, A, B, C, D)
// end"
// ""
// endResult

0 comments on commit 074bb17

Please sign in to comment.