Skip to content

Commit

Permalink
Added compiler option +e=level where level is 0,1 or 2, indicating th…
Browse files Browse the repository at this point in the history
…e degree of elimination of algebraic equations (0 -none, 1 -partial, 2-full)

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@2579 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Peter Aronsson committed Oct 19, 2006
1 parent 152b87b commit 8e9fc2b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
32 changes: 28 additions & 4 deletions Compiler/DAELow.mo
Expand Up @@ -382,6 +382,7 @@ public
type MatchingOptions = tuple<IndexReduction, EquationConstraints, EquationReduction> "- Matching Options" ;

protected import OpenModelica.Compiler.Util;
protected import OpenModelica.Compiler.RTOpts;
protected import OpenModelica.Compiler.DAEEXT;
protected import OpenModelica.Compiler.Print;
protected import OpenModelica.Compiler.Derive;
Expand Down Expand Up @@ -2796,95 +2797,118 @@ algorithm
Exp.Exp e;
Exp.Type t;
// a = b;
case (EQUATION(e1 as Exp.CREF(componentRef = _),e2 as Exp.CREF(componentRef = _)),swap) then (e1,e2);
case (EQUATION(e1 as Exp.CREF(componentRef = _),e2 as Exp.CREF(componentRef = _)),swap)
equation
true = RTOpts.eliminationLevel() > 0;
then (e1,e2);
// a-b = 0
case (EQUATION(Exp.BINARY(e1 as Exp.CREF(_,_),Exp.SUB(_),e2 as Exp.CREF(_,_)),e),false)
equation
true = RTOpts.eliminationLevel() > 0;
true = Exp.isZero(e);
then
(e1,e2);
// a-b = 0 swap
case (EQUATION(Exp.BINARY(e1 as Exp.CREF(_,_),Exp.SUB(_),e2 as Exp.CREF(_,_)),e),true)
equation
true = RTOpts.eliminationLevel() > 0;
true = Exp.isZero(e);
then
(e2,e1);
// 0 = a-b
case (EQUATION(e,Exp.BINARY(e1 as Exp.CREF(_,_),Exp.SUB(_),e2 as Exp.CREF(_,_))),false)
equation
true = RTOpts.eliminationLevel() > 0;
true = Exp.isZero(e);
then
(e1,e2);

// 0 = a-b swap
case (EQUATION(e,Exp.BINARY(e1 as Exp.CREF(_,_),Exp.SUB(_),e2 as Exp.CREF(_,_))),false)
equation
true = RTOpts.eliminationLevel() > 0;
true = Exp.isZero(e);
then
(e2,e1);

// a + b = 0
case (EQUATION(Exp.BINARY(e1 as Exp.CREF(_,_),Exp.ADD(t),e2 as Exp.CREF(_,_)),e),false) equation
true = RTOpts.eliminationLevel() > 1;
true = Exp.isZero(e);
then (e1,Exp.UNARY(Exp.UMINUS(t),e2));

// a + b = 0 swap
case (EQUATION(Exp.BINARY(e1 as Exp.CREF(_,_),Exp.ADD(t),e2 as Exp.CREF(_,_)),e),true) equation
true = Exp.isZero(e);
then (e2,Exp.UNARY(Exp.UMINUS(t),e1));
true = RTOpts.eliminationLevel() > 1;
true = Exp.isZero(e);
then (e2,Exp.UNARY(Exp.UMINUS(t),e1));

// 0 = a+b
case (EQUATION(e,Exp.BINARY(e1 as Exp.CREF(_,_),Exp.ADD(t),e2 as Exp.CREF(_,_))),false) equation
true = RTOpts.eliminationLevel() > 1;
true = Exp.isZero(e);
then (e1,Exp.UNARY(Exp.UMINUS(t),e2));

// 0 = a+b swap
case (EQUATION(e,Exp.BINARY(e1 as Exp.CREF(_,_),Exp.ADD(t),e2 as Exp.CREF(_,_))),true) equation
true = RTOpts.eliminationLevel() > 1;
true = Exp.isZero(e);
then (e2,Exp.UNARY(Exp.UMINUS(t),e1));

// a = -b
case (EQUATION(e1 as Exp.CREF(_,_),e2 as Exp.UNARY(Exp.UMINUS(_),Exp.CREF(_,_))),swap)
equation
true = RTOpts.eliminationLevel() > 1;
then (e1,e2);

// -a = b => a = -b
case (EQUATION(Exp.UNARY(Exp.UMINUS(t),e1 as Exp.CREF(_,_)),e2 as Exp.CREF(_,_)),swap)
then (e1,Exp.UNARY(Exp.UMINUS(t),e2));
equation
true = RTOpts.eliminationLevel() > 1;
then (e1,Exp.UNARY(Exp.UMINUS(t),e2));

// -b - a = 0 => a = -b
case (EQUATION(Exp.BINARY(e2 as Exp.UNARY(Exp.UMINUS(_),Exp.CREF(_,_)),Exp.SUB(_),e1 as Exp.CREF(_,_)),e),false)
equation
true = RTOpts.eliminationLevel() > 1;
true = Exp.isZero(e);
then (e1,e2);

// -b - a = 0 => a = -b swap
case (EQUATION(Exp.BINARY(Exp.UNARY(Exp.UMINUS(t),e2 as Exp.CREF(_,_)),Exp.SUB(_),e1 as Exp.CREF(_,_)),e),true)
equation
true = RTOpts.eliminationLevel() > 1;
true = Exp.isZero(e);
then (e2,Exp.UNARY(Exp.UMINUS(t),e1));

// 0 = -b - a => a = -b
case (EQUATION(e,Exp.BINARY(e2 as Exp.UNARY(Exp.UMINUS(_),Exp.CREF(_,_)),Exp.SUB(_),e1 as Exp.CREF(_,_))),false)
equation
true = RTOpts.eliminationLevel() > 1;
true = Exp.isZero(e);
then (e1,e2);

// 0 = -b - a => a = -b swap
case (EQUATION(e,Exp.BINARY(Exp.UNARY(Exp.UMINUS(t),e2 as Exp.CREF(_,_)),Exp.SUB(_),e1 as Exp.CREF(_,_))),true)
equation
true = RTOpts.eliminationLevel() > 1;
true = Exp.isZero(e);
then (e2,Exp.UNARY(Exp.UMINUS(t),e1));

// -a = -b
case (EQUATION(Exp.UNARY(Exp.UMINUS(_),e1 as Exp.CREF(_,_)),Exp.UNARY(Exp.UMINUS(_),e2 as Exp.CREF(_,_))),swap)
equation
true = RTOpts.eliminationLevel() > 1;
then (e1,e2);
// a = constant
case (EQUATION(e1 as Exp.CREF(_,_),e),swap) equation
true = RTOpts.eliminationLevel() > 1;
true = Exp.isConst(e);
then (e1,e);

// -a = constant
case (EQUATION(Exp.UNARY(Exp.UMINUS(t),e1 as Exp.CREF(_,_)),e),swap) equation
true = RTOpts.eliminationLevel() > 1;
true = Exp.isConst(e);
then (e1,Exp.UNARY(Exp.UMINUS(t),e));
end matchcontinue;
Expand Down
6 changes: 6 additions & 0 deletions Compiler/RTOpts.mo
Expand Up @@ -96,6 +96,12 @@ public function noProc
external "C" ;
end noProc;

public function eliminationLevel
output Integer level;

external "C" ;
end eliminationLevel;

public function latency
output Real outReal;

Expand Down
26 changes: 26 additions & 0 deletions Compiler/runtime/rtopts.c
Expand Up @@ -52,6 +52,13 @@ static int debug_flag_info;
static int params_struct;
static int version_request;

/* Level of eliminations of equations.
* 0 - None
* 1 - Only aliases (a=b)
* 2 - Full (default) (a=-b, a=b, a=constant)
* */
static int elimination_level=2;

static char **debug_flags;
static char *debug_flagstr;
static int debug_flagc;
Expand Down Expand Up @@ -249,6 +256,18 @@ RML_BEGIN_LABEL(RTOpts__args)
RML_TAILCALLK(rmlFC);
}
break;
// Which level of algebraic elimination to use.
case 'e':
if (arg[2] != '=') {
fprintf(stderr, "# Flag Usage: +e=<algebraic_elimination_level 0, 1 or 2>") ;
RML_TAILCALLK(rmlFC);
}
elimination_level = (int)atoi(&arg[3]);
if (elimination_level < 0 || elimination_level > 2) {
elimination_level = 2;
fprintf(stderr, "Warning, wrong value of elimination level, will use default = %d\n",elimination_level) ;
}
break;
default:
fprintf(stderr, "# Unknown option: %s\n", arg);
RML_TAILCALLK(rmlFC);
Expand Down Expand Up @@ -299,6 +318,13 @@ RML_BEGIN_LABEL(RTOpts__silent)
}
RML_END_LABEL

RML_BEGIN_LABEL(RTOpts__eliminationLevel)
{
rmlA0 = mk_icon(elimination_level);
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL

RML_BEGIN_LABEL(RTOpts__debugFlag)
{
void *str = rmlA0;
Expand Down

0 comments on commit 8e9fc2b

Please sign in to comment.