Skip to content

Commit

Permalink
- Added documentation for referenceEq
Browse files Browse the repository at this point in the history
- Made Builtin.mo load MetaModelicaBuiltin.mo using the correct filename+line numbers
- Added fileName argument to val()


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@11979 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Jun 5, 2012
1 parent c041b94 commit 3353694
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 29 deletions.
22 changes: 18 additions & 4 deletions Compiler/FrontEnd/Builtin.mo
Expand Up @@ -602,6 +602,8 @@ algorithm
String msg,fileModelica,fileMetaModelica,initialFunctionStr,initialFunctionStrMM;
list<tuple<Boolean,Absyn.Program>> assocLst;
Option<Absyn.Program> optProgram;
Absyn.Program initialProgram1,initialProgram2;
list<Absyn.Class> classes,classes1,classes2;
case ()
equation
failure(_ = getGlobalRoot(Global.builtinIndex));
Expand All @@ -613,14 +615,26 @@ algorithm
then Util.assoc(Config.acceptMetaModelicaGrammar(), assocLst);
case ()
equation
b = Config.acceptMetaModelicaGrammar();
false = Config.acceptMetaModelicaGrammar();
fileModelica = Settings.getInstallationDirectoryPath() +& "/lib/omc/ModelicaBuiltin.mo";
initialFunctionStr = System.readFile(fileModelica);
initialProgram = Parser.parsebuiltinstring(initialFunctionStr, fileModelica);
assocLst = getGlobalRoot(Global.builtinIndex);
setGlobalRoot(Global.builtinIndex, (false,initialProgram)::assocLst);
then initialProgram;
case ()
equation
true = Config.acceptMetaModelicaGrammar();
fileModelica = Settings.getInstallationDirectoryPath() +& "/lib/omc/ModelicaBuiltin.mo";
fileMetaModelica = Settings.getInstallationDirectoryPath() +& "/lib/omc/MetaModelicaBuiltin.mo";
initialFunctionStr = System.readFile(fileModelica);
initialFunctionStrMM = Debug.bcallret1(b, System.readFile, fileMetaModelica, "");
initialProgram = Parser.parsebuiltinstring(initialFunctionStr +& initialFunctionStrMM, fileModelica);
initialFunctionStrMM = System.readFile(fileMetaModelica);
Absyn.PROGRAM(classes=classes1,within_=Absyn.TOP()) = Parser.parsebuiltinstring(initialFunctionStr, fileModelica);
Absyn.PROGRAM(classes=classes2,within_=Absyn.TOP()) = Parser.parsebuiltinstring(initialFunctionStrMM, fileMetaModelica);
classes = listAppend(classes1,classes2);
initialProgram = Absyn.PROGRAM(classes,Absyn.TOP(),Absyn.dummyTimeStamp);
assocLst = getGlobalRoot(Global.builtinIndex);
setGlobalRoot(Global.builtinIndex, (b,initialProgram)::assocLst);
setGlobalRoot(Global.builtinIndex, (true,initialProgram)::assocLst);
then initialProgram;
else
equation
Expand Down
71 changes: 71 additions & 0 deletions Compiler/FrontEnd/InstTypes.mo
Expand Up @@ -284,6 +284,77 @@ public uniontype Equation
end NORETCALL_EQUATION;
end Equation;

public uniontype Statement
record ASSIGN_STMT
DAE.Exp lhs "The asignee";
DAE.Exp rhs "The expression";
Absyn.Info info;
end ASSIGN_STMT;

record FOR_STMT
String index "The name of the index/iterator variable.";
DAE.Type indexType "The type of the index/iterator variable.";
DAE.Exp range "The range expression to loop over.";
list<Statement> body "The body of the for loop.";
Absyn.Info info;
end FOR_STMT;

record IF_STMT
list<tuple<DAE.Exp, list<Statement>>> branches
"List of branches, where each branch is a tuple of a condition and a body.";
Absyn.Info info;
end IF_STMT;

record WHEN_STMT
list<tuple<DAE.Exp, list<Statement>>> branches
"List of branches, where each branch is a tuple of a condition and a body.";
Absyn.Info info;
end WHEN_STMT;

record ASSERT_STMT
DAE.Exp condition "The assert condition.";
DAE.Exp message "The message to display if the assert fails.";
Absyn.Info info;
end ASSERT_STMT;

record TERMINATE_STMT
DAE.Exp message "The message to display if the terminate triggers.";
Absyn.Info info;
end TERMINATE_STMT;

record REINIT_STMT
DAE.ComponentRef cref "The variable to reinitialize.";
DAE.Exp reinitExp "The new value of the variable.";
Absyn.Info info;
end REINIT_STMT;

record NORETCALL_STMT
Absyn.Path funcName;
list<DAE.Exp> funcArgs;
Absyn.Info info;
end NORETCALL_STMT;

record STMT_WHILE
DAE.Exp exp;
list<Statement> statementLst;
Absyn.Info info;
end STMT_WHILE;

record STMT_RETURN
Absyn.Info info;
end STMT_RETURN;

record STMT_BREAK
Absyn.Info info;
end STMT_BREAK;

record STMT_FAILURE
list<Statement> body;
Absyn.Info info;
end STMT_FAILURE;

end Statement;

public uniontype FunctionSlot
record SLOT
String name;
Expand Down
12 changes: 9 additions & 3 deletions Compiler/FrontEnd/MetaModelicaBuiltin.mo
Expand Up @@ -727,12 +727,18 @@ external "builtin";
end valueHashMod;

function referenceEq<A>
"This is a very fast comparison of two values.
It only checks if the pointers are equal."
""
input A a1;
input A a2;
output Boolean b;
annotation(__OpenModelica_Impure = true);
annotation(__OpenModelica_Impure = true, Documentation(info="<html>
<p>This is a very fast comparison of two values which only checks if the pointers are equal.</p>
<p>The intended way of using the function is to speed up comparisons.</p>
<p>If you know that all occurances of REC(1.5) are the same pointer (e.g. if you made a pass on your datastructure that replaced all occurances with a single one),
you can use referenceEq instead of structural equality (<a href=\"modelica://valueEq\">valueEq</a> or a user-provided comparison).</p>
<p>You can also use the function to speed up comparsions if the rate of success is expected to be high or the cost of structural equality is high. But then you need to do a structural equality check after to make sure nothing is wrong.</p>
<p>You can use the function to avoid reconstructing an identical datastructure on traversals, which saves memory and time spent on garbage collection: case rec as REC(x) equation nx = f(x); then if referenceEq(x,nx) then rec else REC(nx);</p>
</html>"));
external "builtin";
end referenceEq;

Expand Down
33 changes: 19 additions & 14 deletions Compiler/FrontEnd/ModelicaBuiltin.mo
Expand Up @@ -1865,14 +1865,7 @@ partial function basePlotFunction "Extending this does not seem to work at the m
annotation(preferredView="text");
end basePlotFunction;

function plot "Launches a plot window using OMPlot. Returns true on success.
Don't require sendData support.
Example command sequences:
simulate(A);plot({x,y,z});
simulate(A);plot(x, externalWindow=true);
simulate(A,fileNamePrefix=\"B\");simulate(C);plot(z,\"B.mat\",legend=false);
"
function plot "Launches a plot window using OMPlot."
input VariableNames vars "The variables you want to plot";
input Boolean externalWindow := false "Opens the plot in a new plot window";
input String fileName := "<default>" "The filename containing the variables. <default> will read the last simulation result";
Expand All @@ -1888,7 +1881,16 @@ function plot "Launches a plot window using OMPlot. Returns true on success.
output Boolean success "Returns true on success";
output String[:] result "Returns list i.e {\"_omc_PlotResult\",\"<fileName>\",\"<title>\",\"<legend>\",\"<grid>\",\"<PlotType>\",\"<logX>\",\"<logY>\",\"<xLabel>\",\"<yLabel>\",\"<xRange>\",\"<yRange>\",\"<PlotVariables>\"}";
external "builtin";
annotation(preferredView="text");
annotation(preferredView="text",Documentation(info="<html>
<p>Launches a plot window using OMPlot. Returns true on success.</p>
<p>Example command sequences:</p>
<ul>
<li>simulate(A);plot({x,y,z});</li>
<li>simulate(A);plot(x, externalWindow=true);</li>
<li>simulate(A,fileNamePrefix=\"B\");simulate(C);plot(z,\"B.mat\",legend=false);</li>
</ul>
</html>"));
end plot;

function plotAll "Works in the same way as plot(), but does not accept any
Expand Down Expand Up @@ -1994,15 +1996,18 @@ external "builtin";
annotation(preferredView="text");
end compareSimulationResults;

function val "Works on the filename pointed to by the scripting variable currentSimulationResult.
The result is the value of the variable at a certain time point.
For parameters, any time may be given. For variables the startTime<=time<=stopTime needs to hold.
On error, nan (Not a Number) is returned and the error buffer contains the message."
function val "Return the value of a variable at a given time in the simulation results"
input VariableName var;
input Real time;
input String fileName := "<default>" "The contents of the currentSimulationResult variable";
output Real valAtTime;
external "builtin";
annotation(preferredView="text");
annotation(preferredView="text",Documentation(info="<html>
<p>Return the value of a variable at a given time in the simulation results.</p>
<p>Works on the filename pointed to by the scripting variable currentSimulationResult or a given filename.</p>
<p>For parameters, any time may be given. For variables the startTime<=time<=stopTime needs to hold.</p>
<p>On error, nan (Not a Number) is returned and the error buffer contains the message.</p>
</html>"));
end val;

function closeSimulationResultFile "Closes the current simulation result file.
Expand Down
66 changes: 61 additions & 5 deletions Compiler/FrontEnd/SCodeInst.mo
Expand Up @@ -80,6 +80,7 @@ public type Modifier = InstTypes.Modifier;
public type ParamType = InstTypes.ParamType;
public type Prefixes = InstTypes.Prefixes;
public type Prefix = InstTypes.Prefix;
public type Statement = InstTypes.Statement;

protected type FunctionSlot = InstTypes.FunctionSlot;
protected type Item = SCodeEnv.Item;
Expand Down Expand Up @@ -180,6 +181,7 @@ algorithm
SCodeEnv.AvlTree cls_and_vars;
String name, err_msg;
list<Equation> eq, ieq;
list<Statement> alg, ialg;
DAE.Type ty;
Absyn.ArrayDim dims;
list<DAE.Var> vars;
Expand Down Expand Up @@ -223,7 +225,7 @@ algorithm
(elems, cse) = instElementList(mel, inPrefixes, exts, env, inPrefix, ip);

// Instantiate all equation and algorithm sections.
(eq, ieq) = instSections(cdef, env, inPrefix, ip);
(eq, ieq, alg, ialg) = instSections(cdef, env, inPrefix, ip);

// Create the class.
state = ClassInf.start(res, Absyn.IDENT(name));
Expand Down Expand Up @@ -2250,22 +2252,28 @@ protected function instSections
input InstPolicy inInstPolicy;
output list<Equation> outEquations;
output list<Equation> outInitialEquations;
output list<Statement> outStatements;
output list<Statement> outInitialStatements;
algorithm
(outEquations, outInitialEquations) :=
(outEquations, outInitialEquations, outStatements, outInitialStatements) :=
match(inClassDef, inEnv, inPrefix, inInstPolicy)
local
list<SCode.Equation> snel, siel;
list<SCode.AlgorithmSection> snal, sial;
list<Equation> inel, iiel;
list<Statement> inal, iial;

case (SCode.PARTS(normalEquationLst = snel, initialEquationLst = siel), _,
case (SCode.PARTS(normalEquationLst = snel, initialEquationLst = siel, normalAlgorithmLst = snal, initialAlgorithmLst = sial), _,
_, INST_ALL())
equation
inel = instEquations(snel, inEnv, inPrefix);
iiel = instEquations(siel, inEnv, inPrefix);
inal = instAlgorithmSections(snal, inEnv, inPrefix);
iial = instAlgorithmSections(sial, inEnv, inPrefix);
then
(inel, iiel);
(inel, iiel, inal, iial);

case (_, _, _, INST_ONLY_CONST()) then ({}, {});
case (_, _, _, INST_ONLY_CONST()) then ({}, {}, {}, {});

end match;
end instSections;
Expand Down Expand Up @@ -2415,6 +2423,54 @@ algorithm
end match;
end instEEquation;

protected function instAlgorithmSections
input list<SCode.AlgorithmSection> inSections;
input Env inEnv;
input Prefix inPrefix;
output list<Statement> outStatements;
algorithm
outStatements := listReverse(List.fold2(inSections, instStatements, inEnv, inPrefix, {}));
end instAlgorithmSections;

protected function instStatements
input SCode.AlgorithmSection inSection;
input Env inEnv;
input Prefix inPrefix;
input list<Statement> inStatements;
output list<Statement> outStatements;
protected
list<SCode.Statement> sstatements;
algorithm
SCode.ALGORITHM(statements=sstatements) := inSection;
outStatements := List.map2_tail(sstatements, instStatement, inEnv, inPrefix, inStatements);
end instStatements;

protected function instStatement
input SCode.Statement statement;
input Env inEnv;
input Prefix inPrefix;
output Statement outStatement;
algorithm
outStatement := match (statement,inEnv,inPrefix)
local
Absyn.Exp exp1, exp2;
Absyn.Info info;
DAE.Exp dexp1, dexp2;
case (SCode.ALG_ASSIGN(exp1, exp2, _, info), _, _)
equation
dexp1 = instExp(exp1, inEnv, inPrefix);
dexp2 = instExp(exp2, inEnv, inPrefix);
then InstTypes.ASSIGN_STMT(dexp1, dexp2, info);

case (SCode.ALG_ASSIGN(exp1, exp2, _, info), _, _)
equation
dexp1 = instExp(exp1, inEnv, inPrefix);
dexp2 = instExp(exp2, inEnv, inPrefix);
then InstTypes.ASSIGN_STMT(dexp1, dexp2, info);

end match;
end instStatement;

protected function instIfBranch
input Absyn.Exp inCondition;
input list<SCode.EEquation> inBody;
Expand Down
5 changes: 3 additions & 2 deletions Compiler/Script/CevalScript.mo
Expand Up @@ -2056,9 +2056,10 @@ algorithm
then
(cache,Values.BOOL(false),st);

case (cache,env,"val",{cvar,Values.REAL(timeStamp)},st,msg)
case (cache,env,"val",{cvar,Values.REAL(timeStamp),Values.STRING(filename)},st,msg)
equation
(cache,Values.STRING(filename),_) = Ceval.ceval(cache,env,buildCurrentSimulationResultExp(), true, SOME(st),msg);
(cache,Values.STRING(str),_) = Ceval.ceval(cache,env,buildCurrentSimulationResultExp(), true, SOME(st),msg);
filename = Util.if_(stringEq(filename,"<default>"),str,filename);
varNameStr = ValuesUtil.printCodeVariableName(cvar);
val = SimulationResults.val(filename,varNameStr,timeStamp);
then (cache,Values.REAL(val),st);
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Util/List.mo
Expand Up @@ -2573,7 +2573,7 @@ algorithm
outList := listReverse(map2_tail(inList, inFunc, inArg1, inArg2, {}));
end map2;

protected function map2_tail
public function map2_tail
"Tail-recursive implementation of map2"
input list<ElementInType> inList;
input MapFunc inFunc;
Expand Down

0 comments on commit 3353694

Please sign in to comment.