Skip to content

Commit

Permalink
-Added rest of implementation for unit checking, including pretty pri…
Browse files Browse the repository at this point in the history
…nting of units using Mixed Integer Programming based on lpsolve. Unitchecking is deactivated by default. To activate set option "unitChecking" in OptManager to true.

git-svn-id: https://openmodelica.org/svn/OpenModelica/branches/MathCoreOSMC@4141 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Peter Aronsson committed Aug 4, 2009
1 parent 206ddd4 commit aa18de7
Show file tree
Hide file tree
Showing 21 changed files with 2,282 additions and 346 deletions.
32 changes: 30 additions & 2 deletions Compiler/Builtin.mo
Expand Up @@ -51,6 +51,9 @@ public import Env;
/* protected imports */
protected import Types;
protected import ClassInf;
protected import Exp;
protected import Values;


/*
- The primitive types
Expand Down Expand Up @@ -177,7 +180,32 @@ protected constant tuple<Types.TType, Option<Type_a>> int2string =(
Types.T_FUNCTION({("x",(Types.T_INTEGER({}),NONE))},(Types.T_STRING({}),NONE)),NONE);

protected constant tuple<Types.TType, Option<Type_a>> bool2string =(
Types.T_FUNCTION({("x",(Types.T_BOOL({}),NONE))},(Types.T_STRING({}),NONE)),NONE);
Types.T_FUNCTION({("x",(Types.T_BOOL({}),NONE))},(Types.T_STRING({}),NONE)),NONE);

/* type for builtin operator der has unit type parameter to be able to express that derivative of expression
means an addition of 1/s on the unit dimension */
protected constant tuple<Types.TType, Option<Type_a>> derType=(
Types.T_FUNCTION({("x",(Types.T_REAL(
{
Types.VAR(
"unit",
Types.ATTR(false,false,SCode.RW,SCode.PARAM(),Absyn.BIDIR(),Absyn.UNSPECIFIED()),
false,
(Types.T_STRING({}),NONE),
Types.EQBOUND(Exp.SCONST("'p"),SOME(Values.STRING("'p")),Types.C_CONST)
)
}
),NONE))},
/* Return type*/
(Types.T_REAL({
Types.VAR(
"unit",
Types.ATTR(false,false,SCode.RW,SCode.PARAM(),Absyn.BIDIR(),Absyn.UNSPECIFIED()),
false,
(Types.T_STRING({}),NONE),
Types.EQBOUND(Exp.SCONST("'p/s"),SOME(Values.STRING("'p/s")),Types.C_CONST)
)
}),NONE)),NONE);

protected constant tuple<Types.TType, Option<Type_a>> real2real=(
Types.T_FUNCTION({("x",(Types.T_REAL({}),NONE))},(Types.T_REAL({}),NONE)),NONE);
Expand Down Expand Up @@ -2793,7 +2821,7 @@ algorithm
env = Env.extendFrameT(env, "semiLinear", realRealReal2Real);
env = Env.extendFrameT(env, "change", real2bool);
env = Env.extendFrameT(env, "edge", bool2bool);
env = Env.extendFrameT(env, "der", real2real);
env = Env.extendFrameT(env, "der", derType);
/* Removed due to handling in static.mo
env = Env.extendFrameT(env, "delay", realReal2real);
env = Env.extendFrameT(env, "delay", realRealReal2Real);
Expand Down
3 changes: 2 additions & 1 deletion Compiler/Ceval.mo
Expand Up @@ -67,6 +67,7 @@ uniontype Msg
record NO_MSG "Do not give error message" end NO_MSG;
end Msg;

protected import UnitAbsyn;
protected import Static;
protected import Print;
protected import ModUtil;
Expand Down Expand Up @@ -1226,7 +1227,7 @@ algorithm
(cache,c,env_1) = Lookup.lookupClass(cache,env, funcname, false);
compnames = SCode.componentNames(c);
mod = Types.valuesToMods(vallst, compnames);
(cache,dae,_,_,_,_,_,_) = Inst.instClass(cache,env_1, mod, Prefix.NOPRE(), Connect.emptySet, c, {}, impl,
(cache,dae,_,_,_,_,_,_,_) = Inst.instClass(cache,env_1,UnitAbsyn.noStore, mod, Prefix.NOPRE(), Connect.emptySet, c, {}, impl,
Inst.TOP_CALL(),ConnectionGraph.EMPTY);
value = DAE.daeToRecordValue(funcname, dae, impl);
then
Expand Down
5 changes: 3 additions & 2 deletions Compiler/CevalScript.mo
Expand Up @@ -84,6 +84,7 @@ protected import System;
protected import Error;
protected import Static;
protected import ConnectionGraph;
protected import UnitAbsyn;

public function cevalInteractiveFunctions
"function cevalInteractiveFunctions
Expand Down Expand Up @@ -3350,7 +3351,7 @@ algorithm
(cache,(c as SCode.CLASS(n,_,encflag,r,_)),env_1) = Lookup.lookupClass(cache,env, classname_1, true);
env3 = Env.openScope(env_1, encflag, SOME(n));
ci_state = ClassInf.start(r, n);
(cache,dae1,env4,csets_1,ci_state_1,tys,_,_,_,_) = Inst.instClassIn(cache,env3, Types.NOMOD(), Prefix.NOPRE(), Connect.emptySet,
(cache,dae1,env4,_,csets_1,ci_state_1,tys,_,_,_,_) = Inst.instClassIn(cache,env3, UnitAbsyn.noStore,Types.NOMOD(), Prefix.NOPRE(), Connect.emptySet,
ci_state, c, false, {}, false, ConnectionGraph.EMPTY,NONE);
cref_1 = Exp.joinCrefs(cref, Exp.CREF_IDENT("stateSelect",Exp.OTHER(),{}));
(cache,attr,ty,Types.EQBOUND(exp,_,_),_,_) = Lookup.lookupVar(cache,env4, cref_1);
Expand Down Expand Up @@ -3389,7 +3390,7 @@ algorithm
(cache,(c as SCode.CLASS(n,_,encflag,r,_)),env_1) = Lookup.lookupClass(cache,env, classname_1, true);
env3 = Env.openScope(env_1, encflag, SOME(n));
ci_state = ClassInf.start(r, n);
(cache,dae1,env4,csets_1,ci_state_1,tys,_,_,_,_) = Inst.instClassIn(cache,env3, Types.NOMOD(), Prefix.NOPRE(), Connect.emptySet,
(cache,dae1,env4,_,csets_1,ci_state_1,tys,_,_,_,_) = Inst.instClassIn(cache,env3, UnitAbsyn.noStore,Types.NOMOD(), Prefix.NOPRE(), Connect.emptySet,
ci_state, c, false, {}, false, ConnectionGraph.EMPTY,NONE);
cref_1 = Exp.joinCrefs(cref, Exp.CREF_IDENT(attribute,Exp.OTHER(),{}));
(cache,attr,ty,Types.VALBOUND(v),_,_) = Lookup.lookupVar(cache,env4, cref_1);
Expand Down
5 changes: 3 additions & 2 deletions Compiler/Cevalfunc.mo
Expand Up @@ -31,6 +31,7 @@ protected import Debug;
protected import Lookup;
protected import Static;
protected import Inst;
protected import UnitAbsyn;

protected constant String forScopeName="$for loop scope$";

Expand Down Expand Up @@ -1166,8 +1167,8 @@ algorithm
case(p ,env)
equation
(_,typeClass as SCode.CLASS(name=className),env1) = Lookup.lookupClass(Env.emptyCache, env, p, false);
(_,_,env2,_,ty,_,_,_) = Inst.instClass(
Env.emptyCache,env1,Types.NOMOD(),Prefix.NOPRE(),Connect.emptySet,typeClass,{}, true, Inst.INNER_CALL, ConnectionGraph.EMPTY);
(_,_,env2,_,_,ty,_,_,_) = Inst.instClass(
Env.emptyCache,env1,UnitAbsyn.noStore,Types.NOMOD(),Prefix.NOPRE(),Connect.emptySet,typeClass,{}, true, Inst.INNER_CALL, ConnectionGraph.EMPTY);
then
ty;
case (_,_)
Expand Down
15 changes: 15 additions & 0 deletions Compiler/DAE.mo
Expand Up @@ -1664,6 +1664,21 @@ algorithm
Print.printBuf(res);
end dumpVariableAttributes;

public function getUnitAttr "
Return the unit attribute
"
input Option<VariableAttributes> inVariableAttributesOption;
output Exp.Exp start;
algorithm
start:=
matchcontinue (inVariableAttributesOption)
local
Exp.Exp u;
case (SOME(VAR_ATTR_REAL(_,SOME(u),_,_,_,_,_,_,_,_,_))) then u;
case (_) then Exp.SCONST("");
end matchcontinue;
end getUnitAttr;

public function getStartAttrEmpty "
Return the start attribute.
"
Expand Down
5 changes: 3 additions & 2 deletions Compiler/Error.mo
Expand Up @@ -214,6 +214,7 @@ public constant ErrorID DERIVATIVE_NON_REAL=514;
public constant ErrorID UNUSED_MODIFIER=515;
public constant ErrorID SELECTED_STATES=515;
public constant ErrorID MULTIPLE_MODIFIER=516;
public constant ErrorID INCONSISTENT_UNITS=517;


public constant ErrorID INDEX_REDUCTION_NOTIFICATION=1000;
Expand Down Expand Up @@ -481,8 +482,8 @@ protected constant list<tuple<Integer, MessageType, Severity, String>> errorTabl
(STRUCT_SINGULAR_SYSTEM_INITIALIZATION,TRANSLATION(),ERROR(),
"The initialization problem of model is structurally singular, error found sorting equations %s for variables %s"),
(CIRCULAR_EQUATION, TRANSLATION(),ERROR(), " Equation : '%s' has circular references for variable %s."),
(SELECTED_STATES,TRANSLATION(),NOTIFICATION(), "The following variables are selected as states: %s")
(SELECTED_STATES,TRANSLATION(),NOTIFICATION(), "The following variables are selected as states: %s"),
(INCONSISTENT_UNITS, TRANSLATION(),WARNING(),"The system of units is inconsistent in term %s with the units %s and %s respectively.")
};

protected import ErrorExt;
Expand Down
3 changes: 2 additions & 1 deletion Compiler/HashTable.mo
Expand Up @@ -295,9 +295,10 @@ algorithm
indx_1 = indx - 1;
varr_1 = valueArrayClearnth(varr, indx);
then HASHTABLE(hashvec,varr_1,bsize,n);
case (_,_)
case (_,hashTable)
equation
print("-HashTable.delete failed\n");
print("content:"); dumpHashTable(hashTable);
then
fail();
end matchcontinue;
Expand Down

0 comments on commit aa18de7

Please sign in to comment.