Skip to content

Commit c9ec81a

Browse files
author
x02lucpo
committed
added a relation that replace all the der(...) with _D_... in all the equations in DAE
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@1346 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 0206e67 commit c9ec81a

File tree

1 file changed

+142
-33
lines changed

1 file changed

+142
-33
lines changed

modeq/tornado.rml

Lines changed: 142 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ with "debug.rml"
4242
**)
4343
relation generate_code: (Absyn.Program, DAE.DAElist, Absyn.Path) => () =
4444

45-
rule DAELow.lower_with_simple_equations(dae) => dae' &
45+
rule DAELow.lower(dae) => dae' &
4646
Absyn.path_string(class) => cname &
4747
generate_header(cname) => header &
4848
generate_impl_header(cname) => implheader &
@@ -441,22 +441,27 @@ end
441441
**)
442442
relation generate_compute_methods: (string,DAELow.DAELow,Absyn.Program) => string =
443443

444-
rule generate_blt(dae) => (blt,ass1,ass2) &
444+
rule
445+
generate_blt(dae) => (blt,ass1,ass2) &
445446

446447
(** ass1 is what var is solved in what eqn **)
447448
(** ass2 is what eqn is solves what var **)
448449
generate_state_partition(blt,dae,ass1,ass2) => (blt_states,blt_no_states) &
449450

450451

452+
translate_dae(dae) => dae' &
453+
451454
Debug.fprint("bltdump", "++++++++++++++++++++++++++++++++++blt_states\n") &
452455
Debug.fcall("bltdump", DAELow.dump_components, blt_states) &
453456
Debug.fprint("bltdump", "++++++++++++++++++++++++++++blt_no_states\n") &
454457
Debug.fcall("bltdump", DAELow.dump_components, blt_no_states) &
455458

456-
generate_compute_output(cname,dae,p,ass1,ass2,blt_no_states) => coutput &
457-
generate_compute_initial(cname,dae,p) => cinitial &
458-
generate_compute_state(cname,dae,p,ass1,ass2,blt_states) => cstate &
459-
generate_compute_terminal(cname,dae,p) => cterm &
459+
460+
461+
generate_compute_output(cname,dae',p,ass1,ass2,blt_no_states) => coutput &
462+
generate_compute_initial(cname,dae',p) => cinitial &
463+
generate_compute_state(cname,dae',p,ass1,ass2,blt_states) => cstate &
464+
generate_compute_terminal(cname,dae',p) => cterm &
460465
Util.string_append_list(["void C",cname,"::\n",
461466
"ComputeOutput()\n",
462467
"{\n",
@@ -479,7 +484,7 @@ relation generate_compute_methods: (string,DAELow.DAELow,Absyn.Program) => strin
479484
"ComputeTerminal()\n",
480485
"{\n",
481486
cterm,
482-
"}\n",
487+
"}\n",
483488
"\n"]) => methods
484489
---------------------------
485490
generate_compute_methods(cname,dae,p) => methods
@@ -629,26 +634,144 @@ relation check_component_for_statevars:(int, int list, DAELow.DAELow,int vector,
629634

630635
end
631636

637+
(** relation translate_dae
638+
** This relation translates the DAE so that all the der(x) is _D_X in the equations
639+
**
640+
**)
641+
relation translate_dae: DAELow.DAELow => DAELow.DAELow =
642+
643+
rule extract_der_from_var_list(var_list) => (from_list,to_list) &
644+
replace_exp_in_eqns(eqn_list,from_list,to_list) => res_eqns
645+
-------------------------------------------------------------
646+
translate_dae(dae as DAELow.DAELOW(var_list,a,eqn_list,b))
647+
=> DAELow.DAELOW(var_list,a,res_eqns,b)
648+
649+
650+
651+
rule print "-translate_dae failed\n"
652+
-------------------------------------------------------------
653+
translate_dae(dae) => fail
654+
655+
656+
657+
end
658+
659+
(** relation extract_der_from_var_list
660+
** This relation generates to Exp.Exp list of the form:
661+
** [Exp.CALL("der,Exp.Exp("x")...)] and [Exp.cref("x")]
662+
**
663+
**)
664+
relation extract_der_from_var_list: DAELow.Var list => (Exp.Exp list, Exp.Exp list) =
665+
666+
rule
667+
-------------------------------------------------------------
668+
extract_der_from_var_list([]) => ([],[])
669+
670+
rule is_non_state kind &
671+
(*print "extract_der_from_var_list Is not state\n" &*)
672+
extract_der_from_var_list(var_list) => (res1,res2)
673+
-------------------------------------------------------------
674+
extract_der_from_var_list((v as DAELow.VAR(cr,kind,_,_,_,_,_,_,_,origname))::var_list)
675+
=> (res1,res2)
676+
677+
rule (*print "extract_der_from_var_list Is state" &*)
678+
679+
Exp.print_component_ref_str(cr) => cr_str &
680+
681+
let old_varexp = Exp.CREF(cr,Exp.REAL) &
682+
683+
let dercall = Exp.CALL(Absyn.IDENT("der"),
684+
[old_varexp],
685+
false,
686+
false) &
687+
688+
Util.string_append_list(["_D_",cr_str]) => new_id &
689+
let new_cr = Exp.CREF_IDENT(new_id,[]) &
690+
let new_varexp = Exp.CREF(new_cr,Exp.REAL) &
691+
692+
693+
(* print " from \"" & print cr_str & print "\" to \"" & print new_id & print "\"\n" & *)
694+
695+
extract_der_from_var_list(var_list) => (res1,res2)
696+
-------------------------------------------------------------
697+
extract_der_from_var_list((v as DAELow.VAR(cr,kind,_,_,_,_,_,_,_,origname))::var_list)
698+
=> (dercall::res1, new_varexp::res2)
699+
700+
701+
rule print "-extract_der_from_var_list failed\n"
702+
-------------------------------------------------------------
703+
extract_der_from_var_list(_) => fail
704+
end
705+
706+
(** relation replace_exp_in_eqns
707+
** This takes a DAELow.Equation list and two Exp.Exp lists: ("from","to") and does
708+
** a "replace" all on the equations;
709+
**
710+
**)
711+
712+
relation replace_exp_in_eqns:(DAELow.Equation list, Exp.Exp list, Exp.Exp list) => DAELow.Equation list =
713+
714+
rule
715+
-------------------------------------------------------------
716+
replace_exp_in_eqns([],from_exp,to_exp) => []
717+
718+
rule (*print "replace_exp_in_eqns not list" &*)
719+
Exp.replace_exp_list(exp1,from_exp,to_exp) => (res_exp1,nr_of_times1) &
720+
Exp.replace_exp_list(exp2,from_exp,to_exp) => (res_exp2,nr_of_times2) &
721+
replace_exp_in_eqns(eqns_rest,from_exp,to_exp) => res_eqns
722+
-------------------------------------------------------------
723+
replace_exp_in_eqns(DAELow.EQUATION(exp1,exp2)::eqns_rest,from_exp,to_exp) => (DAELow.EQUATION(res_exp1,res_exp2)::res_eqns)
724+
725+
726+
rule print "-replace_exp_in_eqns failed\n"
727+
-------------------------------------------------------------
728+
replace_exp_in_eqns(_,_,_) => fail
729+
end
730+
632731

633732

634733
(** relation: generate_compute_output
635734
** This relation generates the code for the calculation of the output variables.
636735
**)
637-
relation generate_compute_output:(string,DAELow.DAELow,Absyn.Program,int vector, int vector, int list list) => string =
736+
relation generate_compute_output:(string,
737+
DAELow.DAELow,
738+
Absyn.Program,
739+
int vector,
740+
int vector,
741+
int list list) => string =
638742

639743
rule (*Util.string_append_list(["/* output code here*/\n"]) => coutput &*)
640-
(*print("----------- START building block OUTPUT---------------\n") &*)
744+
print("----------- START building block OUTPUT---------------\n") &
641745
build_blocks(dae,ass1,ass2,blocks) => eqn_str_list &
642-
Util.string_append_list(eqn_str_list) => coutput
643-
(*print("----------- END building block OUTPUT---------------\n") *)
644-
746+
Util.list_map(seqn,build_simple_equation) => simple_eqn_list &
747+
Util.string_append_list(simple_eqn_list) => coutput1 &
748+
Util.string_append_list(eqn_str_list) => coutput2 &
749+
Util.string_append_list([coutput1,coutput2]) => coutput &
750+
print("----------- END building block OUTPUT---------------\n")
751+
645752
-------------------------------------------------------------
646753
generate_compute_output(cname,
647-
dae,
754+
dae as DAELow.DAELOW(vars,knvars,eqn,seqn),
648755
p,ass1,ass2,blocks) => coutput
649-
756+
650757
end
651758

759+
760+
relation build_simple_equation:(DAELow.Equation) => string =
761+
762+
rule Exp.print_exp_str(exp1) => exp1_str &
763+
Exp.print_exp_str(exp2) => exp2_str &
764+
Util.string_append_list([" ",exp1_str, " = ", exp2_str, ";\n"]) => eqn_str &
765+
print "\n build_simple_equation: " & print(eqn_str)
766+
--------------------------------
767+
build_simple_equation(DAELow.EQUATION(exp1,exp2)) => eqn_str
768+
769+
770+
rule
771+
-------------------------------------------------------------
772+
build_simple_equation(DAELow.EQUATION(exp1,exp2)) => "// generating simple equation failed"
773+
774+
end
652775
(** relation: generate_compute_initial
653776
** This relation generates the code for the calculation of the initial conditions.
654777
**)
@@ -740,7 +863,7 @@ end
740863
relation build_equation:(DAELow.DAELow, int vector, int vector, int) => string =
741864

742865
rule (* Solving for non-states *)
743-
int_sub(e,1) => e' &
866+
int_sub(e,1) => e' &
744867
list_nth(eqns,e') => DAELow.EQUATION(e1,e2) &
745868
vector_nth(ass2,e') => v &
746869
int_sub(v,1) => v' &
@@ -776,30 +899,16 @@ relation build_equation:(DAELow.DAELow, int vector, int vector, int) => string =
776899

777900
Exp.print_component_ref_str(cr) => cr_str &
778901

779-
let old_varexp = Exp.CREF(cr,Exp.REAL) &
780-
781-
let dercall = Exp.CALL(Absyn.IDENT("der"),
782-
[old_varexp],
783-
false,
784-
false) &
785902

786903
Util.string_append_list(["_D_",cr_str]) => new_id &
787904
let new_cr = Exp.CREF_IDENT(new_id,[]) &
788905
let new_varexp = Exp.CREF(new_cr,Exp.REAL) &
789906

790-
(* this replace the der(x) with _D_x in the expressions *)
791-
Exp.replace_exp(e1,dercall,new_varexp) => (new_e1,a) &
792-
Exp.replace_exp(e2,dercall,new_varexp) => (new_e2,b) &
793907

794-
Exp.solve(new_e1,new_e2,new_varexp) => expr &
908+
Exp.solve(e1,e2,new_varexp) => expr &
795909

796-
(* build the component ref from "x" to the form: "_D_x_" *)
797-
(*Util.string_append_list(["_D_",origname]) => id_ass &
798-
let cr_ass = Exp.CREF_IDENT(id_ass,[]) &*)
799-
800-
801-
Exp.print_exp_str new_e1 => new_e1s &
802-
Exp.print_exp_str new_e2 => new_e2s &
910+
Exp.print_exp_str e1 => new_e1s &
911+
Exp.print_exp_str e2 => new_e2s &
803912
print " Equation " & print new_e1s & print " = " & print new_e2s &
804913
print " solved for " & Exp.print_exp_str new_varexp => s &
805914
print s & print " giving " &
@@ -823,7 +932,7 @@ relation build_equation:(DAELow.DAELow, int vector, int vector, int) => string =
823932

824933
rule print "-build_equation failed\n"
825934
--------------------------------
826-
build_equation(_,_,_,_) => fail
935+
build_equation(_,_,_,_) => "//build_equation failed\n"
827936
end
828937

829938
(** relation build_assignment

0 commit comments

Comments
 (0)