Skip to content

Commit

Permalink
Transformation of if-equations to expressions
Browse files Browse the repository at this point in the history
Updated helptext
Updated setup-project

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@1973 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Håkan Lundvall committed Nov 17, 2005
1 parent 4c5a96c commit 040c3d1
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 29 deletions.
63 changes: 62 additions & 1 deletion Compiler/DAE.rml
Expand Up @@ -163,6 +163,7 @@ module DAE:
relation dump: DAElist => ()
relation dump2: DAElist => ()
relation dump_elements : Element list => ()
relation dump_elements_str : Element list => string
relation dump_str: DAElist => string
relation dump_debug: DAElist => ()
relation dump_debug_element : Element => ()
Expand Down Expand Up @@ -200,7 +201,7 @@ module DAE:
relation to_modelica_form: (DAElist) => DAElist
relation get_named_function : (Absyn.Path, Element list) => Element list
relation get_all_exps : Element list => Exp.Exp list

relation transform_if_eq_to_expr : DAElist => DAElist
end

with "RTOpts.rml"
Expand Down Expand Up @@ -2737,3 +2738,63 @@ relation get_all_exps_extarg : ExtArg => Exp.Exp list =
get_all_exps_extarg _ => fail

end

(** relation: transform_if_eq_to_expr
** transform all if equations to ordinary equations involving if-expressions
**)
relation transform_if_eq_to_expr : DAElist => DAElist =

axiom transform_if_eq_to_expr(DAE([])) => DAE([])

rule transform_if_eq_to_expr(sublist) => sublist_result &
transform_if_eq_to_expr(DAE(rest)) => DAE(rest_result) &
let subresult = COMP(name,sublist_result) &
let result = DAE(subresult::rest_result)
--------------------------------------------------
transform_if_eq_to_expr(DAE((COMP(name,sublist))::rest)) => result

rule transform_if_eq_to_expr(DAE(rest)) => DAE(res2) &
if_eq_to_expr(el) => res1 &
list_append(res1, res2) => res
--------------------------------------------------
transform_if_eq_to_expr(DAE(el::rest)) => DAE(res)

rule transform_if_eq_to_expr(DAE(rest)) => DAE(res)
--------------------------------------------------
transform_if_eq_to_expr(DAE(el::rest)) => DAE(el::res)
end

(** relation: if_eq_to_expr
** Transform one if-equation into equations involving if-expressions
**)
relation if_eq_to_expr : Element => Element list =

rule list_length(true_branch) => true_eq &
list_length(false_branch) => false_eq &
true_eq == false_eq => false &
Print.print_error_buf "# Error, different number of equations in the branches of the if equation.\n"
--------------------------------------------------
if_eq_to_expr(IF_EQUATION(cond,true_branch,false_branch)) => []

rule list_length(true_branch) => true_eq &
list_length(false_branch) => false_eq &
true_eq == false_eq => true &
make_equations_from_if(cond, true_branch, false_branch) => equations
--------------------------------------------------
if_eq_to_expr(IF_EQUATION(cond,true_branch,false_branch)) => equations

axiom if_eq_to_expr(_) => fail
end

relation make_equations_from_if : (Exp.Exp, Element list, Element list) => (Element list) =

axiom make_equations_from_if(_,[],[]) => []

rule make_equations_from_if(cond,rest1,rest2) => rest_res &
let tb = Exp.BINARY(exp1,Exp.SUB(Exp.REAL),exp2) &
let fb = Exp.BINARY(exp3,Exp.SUB(Exp.REAL),exp4) &
let eq = EQUATION(Exp.RCONST(0.0),Exp.IFEXP(cond,tb,fb))
--------------------------------------------------
make_equations_from_if(cond, (EQUATION(exp1,exp2))::rest1, (EQUATION(exp3,exp4))::rest2) => eq::rest_res

end
3 changes: 2 additions & 1 deletion Compiler/Main.rml
Expand Up @@ -310,7 +310,8 @@ relation translate_file : string list => () =

& Debug.fprint ("info", "\n------------------------------------------------------------ \n")
& Debug.fprint ("info", "---instantiating\n")
& Inst.instantiate(p') => d'
& Inst.instantiate(p') => d''
& DAE.transform_if_eq_to_expr(d'') => d'
& Debug.fprint ("beforefixmodout", "Explicit part:\n")
& Debug.fcall ("beforefixmodout", DAE.dump_debug, d')
(* & Inst.instantiate_implicit(pfunc') => dimpl'
Expand Down
65 changes: 39 additions & 26 deletions Compiler/helptext.txt
@@ -1,77 +1,90 @@
Open Source Modelica functions
OpenModelica Builtin Commands
------------------------------
Exit Mosh by pressing Ctrl-d
Exit Mosh/Winmosh by pressing Ctrl-d

up-arrow Get previously given command.
down-arrow Get next command.

<tab> Command completion of these builtin commands.

cd() - Return the current directory

cd(dir) - Change directory to the directory given as string.
Ex: cd("myModels/myLibrary")

clear() - Clears everything.
clear() - Clear everything.

clearVariables() - Clears the variables.
clearVariables() - Clear the variables.

help() - Print this helptext (returned as a string).

instantiateModel(modelname) - Instantiates a model/class and returns a string
containing the flat class definition.
Ex: instantiateModel(dcmotor)

list() - Returns a string containing all class definitions.
list() - Return a string containing all class definitions.

list(modelname) - Returns a string containing the class definition of
list(modelname) - Return a string containing the class definition of
the named class.
Ex: list(dcmotor)

loadFile(strFile) - Load modelica file given as string argument.
Ex: loadFile("../myLibrary/myModels.mo")

loadFile(str) - Load modelica file given as string argument.
Ex: loadFile("../myLibrary/myModels.mo")

loadModel(classname) - Load model or package relative to $MODELICAPATH.
loadModel(name) - Load model, function, or package relative to $MODELICAPATH.
Ex: loadModel(Modelica.Electrical)
Note: if e.g. loadModel(Modelica) fails, you may have
MODELICAPATH pointing at the wrong location.

listVariables() - Return a vector of the currently defined variable names.


listVariables() - Return a vector of the defined variables names.
plot(var) - Plot a variable from the most recently simulated model.
Ex: plot(x)

plot(vars) - Plots the variables given as a vector using an
external plotter.
plot(vars) - Plot variables from the most recently simulated model
given as a vector.
Ex: plot({x,y})
Ex: list(dcmotor)

readFile(str) - Load file given as string and return a string of the
file content.
Ex: readFile("myModel/myModelr.mo")

readSimulationResultSize(filename) -
Returns the size of the record resulting from a simulation
The size is read from the result file, given as a string
readSimulationResultSize(strFile) -
Return the size of the record resulting from a simulation
The size is read from the result file, given as a string
Ex: readSimulationResultSize("dcmotor_res.plt");

readSimulationResult(filename, variables, size) -
Reads the results for a simulation from a file.
Size is the size of the resulting record
Variables is a vector of the variables to investigate
Ex: readSimulationResult("dcmotor_res.plt", {R1.i,L1.v}, 10)
readSimulationResult(strFile, variables, size) -
Read the results of a simulation from a file named by
the string argument strFile.
Here size is the size of the resulting record and
variables is a vector of the variables to investigate.
Ex: readSimulationResult("dcmotor_res.plt",{R1.i,L1.v}, 10)

runScript(str) - Exectute script file (.mos) given as string argument.
runScript(strFile) - Exectute script file (.mos) given as string argument.
Ex: runScript("simulation.mos")

saveModel(str,modelname) - Save the model/class with name modelname in the
saveModel(strFile,modelname) - Save the model/class with name modelname in the
file given by the string argument.

simulate(modelname [,startTime=<Real>][,stopTime=<Real>][,numberOfIntervals=<Integer>])
simulate(modelname[,startTime=<Real>][,stopTime=<Real>][,numberOfIntervals=<Integer>])
Translates a model and simulates it.
Ex: simulate(dcmotor)
Ex: simulate(dcmotor,startTime=0, stopTime=10,
numberOfIntervals=1000)
numberOfIntervals=1000)

system(str) - Execute str as a system(shell) command, return integer
success value.
Ex: system("touch myFile")



timing(expr) - Evaluates expression and returns the number of seconds
the evaluation took.
Ex: timing(x*4711+5)

typeOf(variable) - Returns the type of the variable as a string.
Ex: typeOf(R1.v)


2 changes: 1 addition & 1 deletion flat_modelica_parser/src/flatmodelicaTokenTypes.txt
@@ -1,4 +1,4 @@
// $ANTLR 2.7.5 (20050213): flat_modelica_lexer.g -> flatmodelicaTokenTypes.txt$
// $ANTLR 2.7.4: flat_modelica_lexer.g -> flatmodelicaTokenTypes.txt$
flatmodelica // output token vocab name
ALGORITHM="algorithm"=4
AND="and"=5
Expand Down

0 comments on commit 040c3d1

Please sign in to comment.