Skip to content

Commit

Permalink
Optimized some code. Commented, etc.
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@1347 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Peter Aronsson committed Nov 22, 2004
1 parent c9ec81a commit 2104f18
Show file tree
Hide file tree
Showing 14 changed files with 253 additions and 89 deletions.
61 changes: 45 additions & 16 deletions modeq/daelow.rml
Expand Up @@ -598,13 +598,18 @@ relation states: (DAE.DAElist) => Exp.ComponentRef list =
rule states(DAE.DAE(xs)) => s &
states_exp(e1) => s1 &
states_exp(e2) => s2 &
Util.list_list_union_p([s,s1,s2],Exp.cref_equal) => res
Util.list_map(s,Exp.stringify_component_ref) => s' &
Util.list_map(s1,Exp.stringify_component_ref) => s1' &
Util.list_map(s2,Exp.stringify_component_ref) => s2' &
Util.list_list_union_p([s',s1',s2'],Exp.cref_equal) => res
-----------------------------------
states(DAE.DAE(DAE.EQUATION(e1,e2)::xs)) => res

rule states(dae) => s1 &
states(DAE.DAE(xs)) => s2 &
Util.list_union_p(s1,s2,Exp.cref_equal) => res
Util.list_map(s1,Exp.stringify_component_ref) => s1' &
Util.list_map(s2,Exp.stringify_component_ref) => s2' &
Util.list_union_p(s1',s2',Exp.cref_equal) => res
-------------------------
states(DAE.DAE(DAE.COMP(_,dae)::xs)) => res

Expand Down Expand Up @@ -732,32 +737,53 @@ end
relation lower_eqn: (DAE.Element) => Equation =

rule Exp.simplify(e1) => e1' &
Exp.simplify(e2) => e2'
Exp.simplify(e2) => e2' &
Exp.stringify_crefs(e1') => e1'' &
Exp.stringify_crefs(e2') => e2''
-----------------------
lower_eqn(DAE.EQUATION(e1,e2)) => EQUATION(e1',e2')
lower_eqn(DAE.EQUATION(e1,e2)) => EQUATION(e1'',e2'')
end

(** relation: lower_var
** Transforms a DAE variable to DAELOW variable.
** Includes changing the ComponentRef name to a simpler form 'a'.'b'.'c' becomes
** 'a.b.c' (as CREF_IDENT("a.b.c",[]) )
**)
relation lower_var: (DAE.Element, Exp.ComponentRef list) => Var =

rule lower_varkind(kind,name,states) => kind' &
Exp.print_component_ref_str name => origname
rule Exp.print_component_ref_str name => origname &
let newname = Exp.CREF_IDENT(origname,[]) &
lower_varkind(kind,newname,states) => kind' &
lower_binding(bind) => bind' &
lower_binding(start) => start'
----------------------------------------
lower_var (DAE.VAR(name,kind,dir,tp,bind,dims,start,_),states)
=> VAR(name,kind',dir,tp,bind,NONE,dims,start,-1,origname)
=> VAR(newname,kind',dir,tp,bind',NONE,dims,start',-1,origname)

(* rule print "-lower_var failed\n"
------------------------
lower_var (_,_) => fail*)

end

relation lower_binding: Exp.Exp option => Exp.Exp option =

axiom lower_binding NONE => NONE

rule Exp.stringify_crefs(e) => e'
----------------------------
lower_binding(SOME(e)) => SOME(e)
end

relation lower_known_var: (DAE.Element) => Var =

rule lower_known_varkind(kind) => kind' &
Exp.print_component_ref_str name => origname
Exp.print_component_ref_str name => origname &
lower_binding(bind) => bind' &
lower_binding(start) => start'
----------------------------------------
lower_known_var (DAE.VAR(name,kind,dir,tp,bind,dims,start,_))
=> VAR(name,kind',dir,tp,bind,NONE,dims,start,-1,origname)
=> VAR(Exp.CREF_IDENT(origname,[]),kind',dir,tp,bind',NONE,dims,start',-1,origname)
end

relation lower_varkind: (DAE.VarKind, Exp.ComponentRef, Exp.ComponentRef list)
Expand Down Expand Up @@ -1102,13 +1128,16 @@ relation matching_algorithm2: (DAELow,IncidenceMatrix, IncidenceMatrixT
(* print "reduced index, new DAE:\n" &
dump dae &*)
(* and try again, restarting. This could be optimized later. It should not
be necessary to restart the matching. Instead one could continue the matching
as usual. This has not been tested yet.*)
(*list_length(vars) => nv' &
list_length(eqns) => nf' &
be necessary to restart the matching, according to Bernard Bachmann. Instead one
could continue the matching as usual. This was tested (2004-11-22) and it does not
work to continue without restarting.
For instance the Influenca model "../testsuite/mofiles/Influenca.mo" does not work if
not restarting.*)
list_length(vars) => nv' &
list_length(eqns) => nf' &
Util.list_fill(0,nv') => ass1 &
Util.list_fill(0,nf') => ass2 &*)
matching_algorithm2(dae,m,mt,nv,nf,(*1*)i,ass1,ass2)
Util.list_fill(0,nf') => ass2 &
matching_algorithm2(dae,m,mt,nv,nf,1,ass1,ass2)
=> (ass1',ass2',dae,m,mt)
------------------------
matching_algorithm2(dae,m,mt,nv,nf,i,ass1,ass2)
Expand Down Expand Up @@ -2313,7 +2342,7 @@ relation transform_variables: (Var list, Exp.Exp list, Exp.Exp list) => (Var lis
rule transform_variables(vs,s,t) => vs' &
transform_variable(i,kind) => cr' &
Exp.print_component_ref_str(cr) => name &
Exp.replace_exp_list(e,s,t) => (e',_)
Exp.replace_exp_list(e,s,t) => (e',_)
-------------------
transform_variables(VAR(cr,kind,a,b,SOME(e), c,d,f,i,_)::vs,s,t)
=> (VAR(cr',kind,a,b,SOME(e'),c,d,f,i,name)::vs')
Expand Down
136 changes: 129 additions & 7 deletions modeq/exp.rml
Expand Up @@ -165,6 +165,8 @@ module Exp:
=> (Exp,int)
relation replace_exp_list: (Exp (*expr*), Exp list (* source list*), Exp list (* target list*))
=> (Exp,int)
relation stringify_crefs: (Exp) => Exp
relation stringify_component_ref: ComponentRef => ComponentRef
relation binop_symbol_1 : Operator => string
relation unaryop_symbol : Operator => string
relation lunaryop_symbol : Operator => string
Expand All @@ -181,6 +183,7 @@ with "util.rml"
with "print.rml"
with "modutil.rml"
with "derive.rml"
with "system.rml"
with "dump.rml"

val rconstone = RCONST(1.0)
Expand Down Expand Up @@ -289,27 +292,36 @@ relation cref_equal : (ComponentRef, ComponentRef) => bool =
cref_equal (CREF_IDENT(n1, idx1), CREF_IDENT(n2, idx2)) => true

rule n1 = n2 &
subscript_equal(idx1,idx2) => true &
cref_equal (cr1, cr2) => true
cref_equal (cr1, cr2) => true &
subscript_equal(idx1,idx2) => true
-----------------------------
cref_equal (CREF_QUAL(n1, idx1, cr1), CREF_QUAL(n2, idx2, cr2))
=> true

rule (* There is a bug here somewhere or in RML.
* Therefore as a last resort, print the strings and compare.
**)
print_component_ref_str cr1 => s1 &
print_component_ref_str cr2 => s2 &
s1 = s2
------------------------
cref_equal(cr1,cr2) => true

axiom cref_equal (_, _) => false

end

relation subscript_equal: ( Subscript list, Subscript list) => bool =
(* P.A: Just compare constant integer values for now. Might need to lookup variables
in environment for evaluating arbitrary expressions in future impl. *)
rule i1 = i2 &
rule int_eq(i1,i2) => true &
subscript_equal(xs1,xs2) => res
-------------------------------
subscript_equal(INDEX(ICONST(i1))::xs1,INDEX(ICONST(i2))::xs2) => res

axiom subscript_equal([],[]) => true

axiom subscript_equal(_,_) => false
axiom subscript_equal( _,_) => false
end

relation prepend_string_cref : (string, ComponentRef) => ComponentRef =
Expand Down Expand Up @@ -919,7 +931,7 @@ relation print_component_ref2 =

rule RTOpts.modelica_output => false &
Print.print_buf s &
Print.print_buf "<cref>[" & print_list(l,print_subscript,",") & Print.print_buf "]"
Print.print_buf "[" & print_list(l,print_subscript,",") & Print.print_buf "]"
-------------------------------------------------------
print_component_ref2 (s,l)

Expand Down Expand Up @@ -1413,7 +1425,7 @@ relation print_component_ref2_str : (Ident, Subscript list) => string =

rule RTOpts.modelica_output => false &
print_list_str (l, print_subscript_str, ",") => str &
string_append (s, "[") => str' &
string_append (s, "<cref>[") => str' &
string_append (str', str) => str'' &
string_append (str'', "]") => str'''
-------------------------------------------------------
Expand Down Expand Up @@ -2046,13 +2058,123 @@ relation replace_exp: (Exp (*expr*), Exp(* source expr*),Exp(*target expr*))
replace_exp(REDUCTION(p,e,id,r),source,target)
=> (REDUCTION(p,e',id,r'),c)

axiom replace_exp(e,_,_) => (e,0)
(* rule print "WARNING WARNING replace_exp failed for Exp:" &
print_exp_str e => es & print es & print " source: " &
print_exp_str s => ss & print ss & print "\n"
------------------------------
replace_exp(e,s,_) => (e,0)*)
axiom replace_exp(e,s,_) => (e,0)
end


relation stringify_component_ref: ComponentRef => ComponentRef =

rule print_component_ref_str(cr) => crs
----------------------------------
stringify_component_ref(cr) => CREF_IDENT(crs,[])
end

(** relation: stringify_crefs
** This relation takes an expression and transforms all component reference names
** contained in the expression to a simpler form.
** For instance CREF_QUAL("a",[], CREF_IDENT("b",[])) becomes
** CREF_IDENT("a.b",[])
**)
relation stringify_crefs: Exp => Exp =

axiom stringify_crefs(e as ICONST(_)) => e
axiom stringify_crefs(e as RCONST(_)) => e
axiom stringify_crefs(e as SCONST(_)) => e
axiom stringify_crefs(e as BCONST(_)) => e

rule stringify_component_ref(cr) => cr'
----------------------------------
stringify_crefs(CREF(cr,t)) => CREF(cr',t)

rule stringify_crefs(e1) => e1' &
stringify_crefs(e2) => e2'
-------------------
stringify_crefs(BINARY(e1,op,e2)) => BINARY(e1',op,e2')

rule stringify_crefs(e) => e'
-------------------
stringify_crefs(UNARY(op,e)) => UNARY(op,e')

rule stringify_crefs(e1) => e1' &
stringify_crefs(e2) => e2'
-------------------
stringify_crefs(LBINARY(e1,op,e2)) => BINARY(e1',op,e2')

rule stringify_crefs(e) => e'
-------------------
stringify_crefs(LUNARY(op,e)) => UNARY(op,e')

rule stringify_crefs(e1) => e1' &
stringify_crefs(e2) => e2'
--------------------------
stringify_crefs(RELATION(e1,op,e2)) => RELATION(e1',op,e2')

rule stringify_crefs(e1) => e1' &
stringify_crefs(e2) => e2' &
stringify_crefs(e3) => e3'
--------------------------
stringify_crefs(IFEXP(e1,e2,e3)) => IFEXP(e1',e2',e3')

rule Util.list_map(expl,stringify_crefs) => expl'
-----------------------
stringify_crefs(CALL(p,expl,t,b)) => CALL(p,expl',t,b)

rule Util.list_map(expl,stringify_crefs) => expl'
-----------------------
stringify_crefs(ARRAY(t,b,expl)) => ARRAY(t,b,expl')

rule print "stringify_crefs on MATRIX not impl. yet\n"
-----------------------
stringify_crefs(e as MATRIX(t,b,expl)) => e

rule stringify_crefs(e1) => e1' &
stringify_crefs(e2) => e2' &
stringify_crefs(e3) => e3'
-----------------------
stringify_crefs(RANGE(t,e1,SOME(e2),e3)) => RANGE(t,e1',SOME(e2'),e3')

rule stringify_crefs(e1) => e1' &
stringify_crefs(e3) => e3'
-----------------------
stringify_crefs(RANGE(t,e1,NONE,e3)) => RANGE(t,e1',NONE,e3')


rule Util.list_map(expl,stringify_crefs) => expl'
-------------------
stringify_crefs(TUPLE(expl)) => TUPLE(expl')

rule stringify_crefs(e1) => e1'
--------------------------
stringify_crefs(CAST(t,e1)) => CAST(t,e1')

rule stringify_crefs(e1) => e1'
--------------------------
stringify_crefs(ASUB(e1,i)) => ASUB(e1',i)

rule stringify_crefs(e1) => e1' &
stringify_crefs(e2) => e2'
--------------------------
stringify_crefs(SIZE(e1,SOME(e2))) => SIZE(e1',SOME(e2'))

rule stringify_crefs(e1) => e1'
--------------------------
stringify_crefs(SIZE(e1,NONE)) => SIZE(e1',NONE)

axiom stringify_crefs(e as CODE(_,_)) => e

rule stringify_crefs(e1) => e1' &
stringify_crefs(e2) => e2'
--------------------------
stringify_crefs( REDUCTION(p,e1,id,e2)) => REDUCTION(p,e1',id,e2')

axiom stringify_crefs END => END
axiom stringify_crefs (e) => e
end


relation dump_exp_graphviz : Exp => Graphviz.Node =
Expand Down
8 changes: 0 additions & 8 deletions modeq/inst.rml
Expand Up @@ -1996,7 +1996,6 @@ end
relation get_cref_from_elabed_mod: Types.Mod => Absyn.ComponentRef list =

rule (* For redeclarations e.g "redeclare B2 b(cref=<expr>)", find cref *)
Print.print_buf "cref from redeclare, mod:" &
get_cref_from_elabed_mod(Types.REDECL(b,xs)) => res1 &
get_cref_from_mod(m) => res2 &
list_append(res1,res2) => res
Expand Down Expand Up @@ -2278,21 +2277,14 @@ relation inst_array : (Env.Env,
=> (DAE.Element list, Connect.Sets, Types.Type) =

(* rule (* If is a function var.*)
Print.print_buf "inst_array, is function" &
Mod.mod_equation(mod) => SOME(Types.TYPED(e,p)) & (* Which has an
expression binding *)
Print.print_buf "inst_array, calling inst_class\n" &
inst_class(env,mod,pre,csets,cl,inst_dims,true,INNER_CALL)
=> (_,_,_,ty,st) &
Print.print_buf "got type: " &
Types.print_type ty &
Print.print_buf "\n" &
Print.print_buf "instantated the class\n" &
(* Check their types... *)
Prefix.prefix_cref(pre,Exp.CREF_IDENT(n,[])) => cr &
Types.elab_type(ty) => ty' &
Types.match_prop(e,Types.PROP(ty,false),p) => e' &
Print.print_buf "matched the types \n" &
make_dae_equation(Exp.CREF(cr,ty'),e',NON_INITIAL) => dae
---------------------------------------------------
inst_array(env,ci_state as ClassInf.FUNCTION(_),mod,pre,csets,n,(cl,attr),i,DIMEXP(_,_),dims,idxs,inst_dims,impl)
Expand Down
14 changes: 10 additions & 4 deletions modeq/main.rml
Expand Up @@ -290,12 +290,14 @@ relation translate_file : string list => () =
& Debug.fcall ("daedump2", DAE.dump2, d)
& Debug.fcall ("daedumpdebug", DAE.dump_debug, d)
& Debug.fcall ("daedumpgraphv", DAE.dump_graphviz, d)
& ModUtil.string_prefix_params d => d'
& Debug.fcall ("codegen", Codegen.generate_functions, d')
(*& ModUtil.string_prefix_params d => d'*)
& Debug.fcall ("codegen", Codegen.generate_functions, d)
& run_backend_q() => runbe
& Debug.bcall (runbe, optimize_dae,d)
& Print.get_string () => str
& print str
& RTOpts.silent() => silent
& bool_not(silent) => notsilent
& Debug.bcall(notsilent,print, str)
------------------------------
translate_file [f]

Expand Down Expand Up @@ -351,12 +353,16 @@ end
(* Used for both parallization and for normal execution.*)
relation optimize_dae: (DAE.DAElist) => () =

rule DAELow.lower(d) => d'
rule print "entering optimize_dae" &
DAELow.lower(d) => d'
& print "lowered DAE\n"
& Debug.fcall("dumpdaelow", DAELow.dump, d')
& clock() => t1
& DAELow.incidence_matrix(d') => m
& print "computed incidence matrix\n"
& clock() => t2
& DAELow.transpose_matrix(m) => mT
& print "transposed incidence matrix\n"
& Debug.fcall("bltdump", DAELow.dump_incidence_matrix, m)
& Debug.fcall("bltdump", DAELow.dump_incidence_matrix_inv, mT)
& clock() => t3
Expand Down

0 comments on commit 2104f18

Please sign in to comment.