Skip to content

Commit

Permalink
Fixed bug in Bouncingball. Added n_bounce variable that counts number…
Browse files Browse the repository at this point in the history
… of bounces so testsuite is more robust

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@2080 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Peter Aronsson committed Feb 3, 2006
1 parent 8dbb93a commit 9479615
Showing 1 changed file with 138 additions and 23 deletions.
161 changes: 138 additions & 23 deletions Compiler/SimCodegen.rml
Expand Up @@ -1507,9 +1507,9 @@ relation add_missing_equations: (int, int list) => int list =


rule n - 1 => n' &
add_missing_equations(n',lst) => lst'
add_missing_equations(n',n::lst) => lst'
--------------------------------------------------
add_missing_equations(n,lst) => n::lst'
add_missing_equations(n,lst) => lst'
end


Expand Down Expand Up @@ -3115,7 +3115,7 @@ relation generate_when_clauses: (string,
int list list) (* blocks *)
=> string =

rule generate_when_clauses2(wc,0,dae,dlow,ass1,ass2,blocks) => when_fcn &
rule generate_when_clauses2(wc,0,dae,dlow,ass1,ass2,blocks,0) => (when_fcn,_) &
Codegen.c_make_function("int","function_when",[],["int i","double *t"]) => when_fcn0 &
Codegen.c_merge_fns([when_fcn0,when_fcn]) => when_fcn' &
Codegen.c_prepend_statements(when_fcn',["switch(i) {"]) => when_fcn'' &
Expand All @@ -3135,10 +3135,12 @@ relation generate_when_clauses2: (DAELow.WhenClause list, int,
DAELow.DAELow,
int vector, (* assignments1 *)
int vector, (* assignments2 *)
int list list) (* blocks *)
=> CFunction =
int list list, (* blocks *)
int (* cg var_id *))
=> (CFunction,int (* cg var_id *)) =

axiom generate_when_clauses2([],_,_,_,_,_,_) => Codegen.c_empty_function
axiom generate_when_clauses2([],_,_,_,_,_,_,cg_id)
=> (Codegen.c_empty_function,cg_id)

rule dump_when_clause_str wc => wc_str &
Exp.get_cref_from_exp(cond) => cond_cref_list &
Expand All @@ -3151,17 +3153,18 @@ relation generate_when_clauses2: (DAELow.WhenClause list, int,
Util.list_map(reinit,build_reinit_str) => reinit_str &
Util.string_append_list(reinit_str) => reinit_str' &
int_string index => index_str &
build_when_blocks(dae,dlow,ass1,ass2,blocks,index) => when_fcn &
build_when_blocks(dae,dlow,ass1,ass2,blocks,index,cg_id) => (when_fcn,cg_id') &
Codegen.c_print_statements(when_fcn) => when_str &
Util.string_append_list([" case ",index_str, ": //",wc_str]) => case_stmt &
Codegen.c_prepend_statements(when_fcn,[case_stmt]) => when_fcn' &
Codegen.c_add_statements(when_fcn',[reinit_str',"break;"]) => when_fcn'' &
index + 1 => index' &
generate_when_clauses2(xs,index',dae,dlow,ass1,ass2,blocks) => when_fcn2 &
generate_when_clauses2(xs,index',dae,dlow,ass1,ass2,blocks,cg_id') => (when_fcn2,cg_id'') &
Codegen.c_merge_fns([when_fcn'',when_fcn2]) => when_fcn'''
--------------------------------------------------
generate_when_clauses2((wc as DAELow.WHEN_CLAUSE(cond,reinit))::xs,
index,dae,dlow,ass1,ass2,blocks) => when_fcn'''
index,dae,dlow,ass1,ass2,blocks,cg_id)
=> (when_fcn''',cg_id'')
end

relation build_reinit_str : (DAELow.ReinitStatement) => string =
Expand All @@ -3178,18 +3181,21 @@ end
**
** Helper relation to build_when_clauses.
**)
relation build_when_blocks : (DAE.DAElist,DAELow.DAELow, int vector, int vector, int list list, int) => CFunction =
relation build_when_blocks : (DAE.DAElist,DAELow.DAELow,
int vector, int vector,
int list list, int, int (*cg var_id *))
=> (CFunction,int (* cg var_id *)) =

axiom build_when_blocks(_,_,_,_,[],_) => Codegen.c_empty_function
axiom build_when_blocks(_,_,_,_,[],_,cg_id) =>(Codegen.c_empty_function,cg_id)

rule build_when_equation(dae,dlow,ass1,ass2,eqn,index) => cfn1 &
build_when_blocks(dae,dlow,ass1,ass2,blocks,index) => cfn2 &
rule build_when_equation(dae,dlow,ass1,ass2,eqn,index,cg_id) => (cfn1,cg_id') &
build_when_blocks(dae,dlow,ass1,ass2,blocks,index,cg_id') => (cfn2,cg_id'') &
Codegen.c_merge_fns([cfn1,cfn2]) => cfn
----------------------------------------
build_when_blocks(dae,dlow,ass1,ass2,(block as [eqn])::blocks,index)
=> cfn
build_when_blocks(dae,dlow,ass1,ass2,(block as [eqn])::blocks,index,cg_id)
=> (cfn,cg_id'')

axiom build_when_blocks(_,_,_,_,_,_) => Codegen.c_empty_function
axiom build_when_blocks(_,_,_,_,_,_,cg_id) => (Codegen.c_empty_function,cg_id)
end

(** relation: build_zero_crossing_eqns
Expand Down Expand Up @@ -3231,8 +3237,9 @@ relation build_when_equation: (DAE.DAElist,
int vector, (* assignments1*)
int vector, (* assignments2*)
int, (* equation no. *)
int
) => CFunction =
int,
int (* cg var_id *)
) => (CFunction,int (* cg var_id*)) =

rule int_sub(e,1) => e' &
DAELow.equation_nth(eqns,e') => DAELow.WHEN_EQUATION(DAELow.WHEN_EQ(wc_ind,cr,expr)) &
Expand All @@ -3241,13 +3248,13 @@ relation build_when_equation: (DAE.DAElist,
int_sub(v,1) => v' &
DAELow.vararray_nth(vararr,v') => (va as DAELow.VAR(cr,kind,_,_,_,_,_,_,_,origname,_,dae_var_attr,comment,flow)) &
Exp.print_component_ref_str(cr) => assignedVar &
build_assignment(dae,cr,expr,origname,0) => (cfn,_) &
build_assignment(dae,cr,expr,origname,cg_id) => (cfn,cg_id') &
Util.string_append_list(["save(",assignedVar,");\n"]) => save_stmt &
Codegen.c_prepend_statements(cfn,[save_stmt]) => cfn
------------------------------------------------------------
build_when_equation(dae,DAELow.DAELOW(DAELow.VARIABLES(_,_,vararr,_,_),_,eqns,_,_,_,_,_),ass1,ass2,e,index) => cfn
build_when_equation(dae,DAELow.DAELOW(DAELow.VARIABLES(_,_,vararr,_,_),_,eqns,_,_,_,_,_),ass1,ass2,e,index,cg_id) => (cfn,cg_id')

axiom build_when_equation(_,_,_,_,_,_) => Codegen.c_empty_function
axiom build_when_equation(_,_,_,_,_,_,cg_id) => (Codegen.c_empty_function,cg_id)
end


Expand Down Expand Up @@ -3304,8 +3311,7 @@ end
"double* p"])
=> cfunc' &
Codegen.c_add_cleanups(cfunc', ["return 0;"]) => cfunc &
(*build_solved_blocks(dae,dlow,ass1,ass2,blocks) => eqn_str_lst &*)
generate_ode_blocks(dlow,ass1,ass2,blocks,0) => (body,_,extra_funcs) &
build_solved_blocks(dae,dlow,ass1,ass2,blocks,0) => (body,_,extra_funcs) &
generate_compute_removed_eqns(dlow) => stmts2 &
Codegen.c_merge_fns([cfunc,body]) => cfunc' &
Codegen.c_add_statements(cfunc',stmts2) => cfunc'' &
Expand Down Expand Up @@ -3348,6 +3354,93 @@ relation generate_compute_removed_eqns_2: DAELow.Equation list => string list =
=> s1::res
end


(** relation: build_solved_blocks
**
** This relation generates code for blocks on solved form, i.e.
** \dot{x} = f(x,y,t)
** It is used for the generation of the output function. If event code
** is generated, it does not include discrete equations in the output code.
**)

relation build_solved_blocks:(DAE.DAElist,
DAELow.DAELow,
int vector, (* assignments1*)
int vector, (* assignments2*)
int list list (* list of blocks *),
int (* cg var_id *))
=> (CFunction,int (* cg var_id*), CFunction list) =

axiom build_solved_blocks(_,_,_,_,[],cg_id)
=> (Codegen.c_empty_function,cg_id,[])

rule (* for single equations *)
use_zerocrossing => true &
build_non_discrete_equation(dae,dlow,ass1,ass2,eqn,cg_id)
=> (cfn1,cg_id') &
build_solved_blocks(dae,dlow,ass1,ass2,blocks,cg_id') => (cfn2,cg_id'',f2) &
Codegen.c_merge_fns([cfn1,cfn2]) => cfn
-------------------------------------
build_solved_blocks(dae,dlow,ass1,ass2,(block as [eqn])::blocks,cg_id)
=> (cfn,cg_id'',f2)

rule (* for single equations *)
use_zerocrossing => false &
build_equation(dae,dlow,ass1,ass2,eqn,cg_id) => (cfn1,cg_id') &
build_solved_blocks(dae,dlow,ass1,ass2,blocks,cg_id')
=> (cfn2,cg_id'',f2) &
Codegen.c_merge_fns([cfn1,cfn2]) => cfn
-------------------------------------
build_solved_blocks(dae,dlow,ass1,ass2,(block as [eqn])::blocks,cg_id)
=> (cfn,cg_id'',f2)


rule (* for blocks *)
generate_ode_system(dlow,ass1,ass2,block,cg_id)
=> (fcn1,cg_id',f1) &
build_solved_blocks(dae,dlow,ass1,ass2,blocks,cg_id')
=> (fcn2,cg_id'',f2) &
Codegen.c_merge_fns([fcn1,fcn2]) => fcn &
list_append(f1,f2) => f
------------------
build_solved_blocks(dae,dlow,ass1,ass2,block::blocks,cg_id)
=> (fcn,cg_id'',f)

rule print "-build_solved_blocks failed\n"
-----------------
build_solved_blocks(_,_,_,_,_,_) => fail
end

(** relation: build_block
**
** This relation returns the code string for solving a block of variables
** in the dae, i.e. a set of coupled equations.
** It is used both for state variables and algebraic variables.
**)

relation build_block: (DAE.DAElist,
DAELow.DAELow,
int vector, (* assignments1 *)
int vector, (* assignments2 *)
int list) (* block of equations*)
=> string =

rule all_same_algorithm(dlow,block) => true &
int_sub(e,1) => e' &
DAELow.equation_nth(eqns,e') => DAELow.ALGORITHM(indx,inputs,outputs) &
array_nth(alg,indx) => alg &
Codegen.generate_algorithm(DAE.ALGORITHM(alg),1)
=> (Codegen.CFUNCTION(_,_,_,_,_,_,stmt_strs,_),_) &
Util.string_delimit_list(stmt_strs,"\n") => res
---------------
build_block(dae,dlow as DAELow.DAELOW(_,_,eqns,_,_,_,alg,_),ass1,as2,block as e::_)
=> res

rule print "#Solving of equation systems not implemented yet.\n"
--------------------------
build_block(dae,dlow,ass1,ass2,block) => fail
end

(** relation: all_same_algorithm
**
** Checks that a block consists only of one algorithm in different -nodes-
Expand Down Expand Up @@ -3395,6 +3488,28 @@ relation all_same_algorithm2:(DAELow.DAELow,

end

(** relation: build_non_discrete_equation
**
** Builds code for non_discrete equations only.
** Used in build_solved_blocks.
**)

relation build_non_discrete_equation:(DAE.DAElist, DAELow.DAELow, int vector, int vector, int,int (*cg var_id*)) => (CFunction,int(*cg var_id*)) =

rule int_sub(e,1) => e' &
vector_nth(ass2,e') => v &
int_sub(v,1) => v' &
DAELow.vararray_nth(vararr,v') => (DAELow.VAR(_,kind,_,_,_,_,_,_,_,_,_,_,_,_)) &
DAELow.is_discrete kind
--------------------------------
build_non_discrete_equation(_,DAELow.DAELOW(DAELow.VARIABLES(_,_,vararr,_,_),_,_,_,_,_,_,_),_,ass2,e,cg_id) => (Codegen.c_empty_function,cg_id)

rule build_equation(dae,dlow,ass1,ass2,eqn,cg_id) => (cfunc,cg_id')
----------------------------------------
build_non_discrete_equation(dae,dlow,ass1,ass2,eqn,cg_id)
=> (cfunc,cg_id')
end

(** relation build_equation
**
** This returns the code string for a specific equation in the dae.
Expand Down

0 comments on commit 9479615

Please sign in to comment.