Skip to content

Commit

Permalink
Fixed bugs in simulation_runtime (initialization of data, stop exactl…
Browse files Browse the repository at this point in the history
…y at stop-time, etc).

Fixed bug with recursive modifiers (A a(x=a.y))


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@2626 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Peter Aronsson committed Dec 9, 2006
1 parent bba486e commit a4a361e
Show file tree
Hide file tree
Showing 13 changed files with 227 additions and 75 deletions.
13 changes: 13 additions & 0 deletions Compiler/Absyn.mo
Expand Up @@ -1570,6 +1570,19 @@ algorithm
end matchcontinue;
end crefGetFirst;

public function crefStripFirst "
Strip the first ident from a ComponentRef
"
input ComponentRef inComponentRef;
output ComponentRef outComponentRef;
algorithm
outComponentRef:=
matchcontinue (inComponentRef)
local ComponentRef cr;
case (CREF_QUAL(componentRef =cr )) then cr;
end matchcontinue;
end crefStripFirst;

public function restrString "function: restrString
Maps a class restriction to the corresponding string for printing
Expand Down
24 changes: 12 additions & 12 deletions Compiler/Builtin.mo
Expand Up @@ -84,44 +84,44 @@ protected constant SCode.Class enumType=SCode.CLASS("EnumType",false,false,SCode

protected constant SCode.Element unit=SCode.COMPONENT("unit",true,false,false,
SCode.ATTR({},false,SCode.RW(),SCode.PARAM(),Absyn.BIDIR()),Absyn.TPATH(Absyn.IDENT("StringType"),NONE),
SCode.MOD(false,Absyn.NON_EACH(),{},SOME(Absyn.STRING(""))),NONE,NONE) "This `unit\' component is used in several places below, and it is
SCode.MOD(false,Absyn.NON_EACH(),{},SOME((Absyn.STRING(""),false))),NONE,NONE) "This `unit\' component is used in several places below, and it is
declared once here to make the definitions below easier to read." ;

protected constant SCode.Element quantity=SCode.COMPONENT("quantity",true,false,false,
SCode.ATTR({},false,SCode.RW(),SCode.PARAM(),Absyn.BIDIR()),Absyn.TPATH(Absyn.IDENT("StringType"),NONE),
SCode.MOD(false,Absyn.NON_EACH(),{},SOME(Absyn.STRING(""))),NONE,NONE);
SCode.MOD(false,Absyn.NON_EACH(),{},SOME((Absyn.STRING(""),false))),NONE,NONE);

protected constant SCode.Element displayUnit=SCode.COMPONENT("displayUnit",true,false,false,
SCode.ATTR({},false,SCode.RW(),SCode.PARAM(),Absyn.BIDIR()),Absyn.TPATH(Absyn.IDENT("StringType"),NONE),
SCode.MOD(false,Absyn.NON_EACH(),{},SOME(Absyn.STRING(""))),NONE,NONE);
SCode.MOD(false,Absyn.NON_EACH(),{},SOME((Absyn.STRING(""),false))),NONE,NONE);

protected constant SCode.Element min=SCode.COMPONENT("min",true,false,false,
SCode.ATTR({},false,SCode.RW(),SCode.PARAM(),Absyn.BIDIR()),Absyn.TPATH(Absyn.IDENT("RealType"),NONE),
SCode.MOD(false,Absyn.NON_EACH(),{},SOME(Absyn.REAL(-1e+099))),NONE,NONE);
SCode.MOD(false,Absyn.NON_EACH(),{},SOME((Absyn.REAL(-1e+099),false))),NONE,NONE);

protected constant SCode.Element max=SCode.COMPONENT("max",true,false,false,
SCode.ATTR({},false,SCode.RW(),SCode.PARAM(),Absyn.BIDIR()),Absyn.TPATH(Absyn.IDENT("RealType"),NONE),
SCode.MOD(false,Absyn.NON_EACH(),{},SOME(Absyn.REAL(1e+099))),NONE,NONE);
SCode.MOD(false,Absyn.NON_EACH(),{},SOME((Absyn.REAL(1e+099),false))),NONE,NONE);

protected constant SCode.Element realStart=SCode.COMPONENT("start",true,false,false,
SCode.ATTR({},false,SCode.RW(),SCode.PARAM(),Absyn.BIDIR()),Absyn.TPATH(Absyn.IDENT("RealType"),NONE),
SCode.MOD(false,Absyn.NON_EACH(),{},SOME(Absyn.REAL(0.0))),NONE,NONE);
SCode.MOD(false,Absyn.NON_EACH(),{},SOME((Absyn.REAL(0.0),false))),NONE,NONE);

protected constant SCode.Element integerStart=SCode.COMPONENT("start",true,false,false,
SCode.ATTR({},false,SCode.RW(),SCode.PARAM(),Absyn.BIDIR()),Absyn.TPATH(Absyn.IDENT("IntegerType"),NONE),
SCode.MOD(false,Absyn.NON_EACH(),{},SOME(Absyn.INTEGER(0))),NONE,NONE);
SCode.MOD(false,Absyn.NON_EACH(),{},SOME((Absyn.INTEGER(0),false))),NONE,NONE);

protected constant SCode.Element stringStart=SCode.COMPONENT("start",true,false,false,
SCode.ATTR({},false,SCode.RW(),SCode.PARAM(),Absyn.BIDIR()),Absyn.TPATH(Absyn.IDENT("StringType"),NONE),
SCode.MOD(false,Absyn.NON_EACH(),{},SOME(Absyn.STRING(""))),NONE,NONE);
SCode.MOD(false,Absyn.NON_EACH(),{},SOME((Absyn.STRING(""),false))),NONE,NONE);

protected constant SCode.Element booleanStart=SCode.COMPONENT("start",true,false,false,
SCode.ATTR({},false,SCode.RW(),SCode.PARAM(),Absyn.BIDIR()),Absyn.TPATH(Absyn.IDENT("BooleanType"),NONE),
SCode.MOD(false,Absyn.NON_EACH(),{},SOME(Absyn.BOOL(false))),NONE,NONE);
SCode.MOD(false,Absyn.NON_EACH(),{},SOME((Absyn.BOOL(false),false))),NONE,NONE);

protected constant SCode.Element fixed=SCode.COMPONENT("fixed",true,false,false,
SCode.ATTR({},false,SCode.RW(),SCode.PARAM(),Absyn.BIDIR()),Absyn.TPATH(Absyn.IDENT("BooleanType"),NONE),
SCode.MOD(false,Absyn.NON_EACH(),{},SOME(Absyn.BOOL(false))),NONE,NONE) "Should be true for variables" ;
SCode.MOD(false,Absyn.NON_EACH(),{},SOME((Absyn.BOOL(false),false))),NONE,NONE) "Should be true for variables" ;

protected constant SCode.Element nominal=SCode.COMPONENT("nominal",true,false,false,
SCode.ATTR({},false,SCode.RW(),SCode.PARAM(),Absyn.BIDIR()),Absyn.TPATH(Absyn.IDENT("RealType"),NONE),
Expand All @@ -130,9 +130,9 @@ protected constant SCode.Element nominal=SCode.COMPONENT("nominal",true,false,fa
protected constant SCode.Element stateSelect=SCode.COMPONENT("stateSelect",true,false,false,
SCode.ATTR({},false,SCode.RW(),SCode.PARAM(),Absyn.BIDIR()),Absyn.TPATH(Absyn.IDENT("StateSelect"),NONE),
SCode.MOD(false,Absyn.NON_EACH(),{},
SOME(
SOME((
Absyn.CREF(
Absyn.CREF_QUAL("StateSelect",{},Absyn.CREF_IDENT("default",{}))))),NONE,NONE);
Absyn.CREF_QUAL("StateSelect",{},Absyn.CREF_IDENT("default",{}))),false))),NONE,NONE);

protected constant list<SCode.Element> stateSelectComps={
SCode.COMPONENT("never",true,false,false,
Expand Down
43 changes: 14 additions & 29 deletions Compiler/Ceval.mo
Expand Up @@ -601,14 +601,16 @@ algorithm
local
Real lhv,rhv;
Option<Integer> dim;
Values.Value res1;
equation
(cache,res1,st_1) = ceval(cache,env, rh, impl, st, dim, msg);
true = Values.isZero(res1);
lh_str = Exp.printExpStr(lh);
rh_str = Exp.printExpStr(rh);
Error.addMessage(Error.DIVISION_BY_ZERO, {lh_str,rh_str});
then
fail();


//ADD (integer or real)
case (cache,env,Exp.BINARY(exp1 = lh,operator = Exp.ADD(ty=_),exp2 = rh),impl,st,dim,msg)
local
Expand All @@ -633,27 +635,8 @@ algorithm
then
(cache,res3,st_2);


/*
case (cache,env,Exp.BINARY(exp1 = lh,operator = Exp.DIV(ty = Exp.REAL()),exp2 = rh),impl,st,dim,msg)
local
Real lhv,rhv;
Option<Integer> dim;
equation
(cache,Values.REAL(lhv),st_1) = ceval(cache,env, lh, impl, st, dim, msg);
(cache,Values.REAL(rhv),st_2) = ceval(cache,env, rh, impl, st_1, dim, msg);
failure(_ = realDiv(lhv, rhv));
lh_str = Exp.printExpStr(lh);
rh_str = Exp.printExpStr(rh);
Error.addMessage(Error.DIVISION_BY_ZERO, {lh_str,rh_str});
then
fail();
*/




case (cache,env,Exp.UNARY(operator = Exp.UMINUS_ARR(ty = _),exp = exp),impl,st,dim,msg) /* unary minus of array */
/* unary minus of array */
case (cache,env,Exp.UNARY(operator = Exp.UMINUS_ARR(ty = _),exp = exp),impl,st,dim,msg)
local
Exp.Exp exp;
Option<Integer> dim;
Expand Down Expand Up @@ -682,7 +665,8 @@ algorithm
then
(cache,v,st_1);

case (cache,env,Exp.LBINARY(exp1 = lh,operator = Exp.AND(),exp2 = rh),impl,st,dim,msg) /* Logical */
/* Logical */
case (cache,env,Exp.LBINARY(exp1 = lh,operator = Exp.AND(),exp2 = rh),impl,st,dim,msg)
local
Boolean lhv,rhv,x;
Option<Integer> dim;
Expand Down Expand Up @@ -712,7 +696,8 @@ algorithm
then
(cache,Values.BOOL(b_1),st_1);

case (cache,env,Exp.RELATION(exp1 = lhs,operator = relop,exp2 = rhs),impl,st,dim,msg) /* Relations */
/* Relations */
case (cache,env,Exp.RELATION(exp1 = lhs,operator = relop,exp2 = rhs),impl,st,dim,msg)
local Option<Integer> dim;
equation
(cache,lhs_1,st_1) = ceval(cache,env, lhs, impl, st, dim, msg);
Expand Down Expand Up @@ -852,7 +837,8 @@ algorithm
then
fail();

case (cache,env,e,_,_,_,MSG()) /* ceval can apparently fa-il and that is ok, catched by other rules... */
/* ceval can fail and that is ok, catched by other rules... */
case (cache,env,e,_,_,_,MSG())
equation
Debug.fprint("failtrace", "- ceval failed: ");
str = Exp.printExpStr(e);
Expand Down Expand Up @@ -2027,14 +2013,13 @@ algorithm
lstVarVal = iv,
compiledFunctions = cf)),msg)
equation
(cache,executable,method_str,st,_) = buildModel(cache,env, exp, st_1, msg) "FIXME: Should ceval be called with impl=true here? Build and simulate model" ;
(cache,executable,method_str,st,_) = buildModel(cache,env, exp, st_1, msg) "Build and simulate model" ;
cit = winCitation();
pd = System.pathDelimiter();
executableSuffixedExe = stringAppend(executable, ".exe");
sim_call = Util.stringAppendList(
{cit,executableSuffixedExe,cit," -m ",method_str,
" > output.log 2>&1"});
//print(sim_call);
{cit,executableSuffixedExe,cit," > output.log 2>&1"});
//print(sim_call);
0 = System.systemCall(sim_call);
result_file = Util.stringAppendList({executable,"_res.plt"});
simValue = Values.RECORD(Absyn.IDENT("SimulationResult"),
Expand Down
92 changes: 89 additions & 3 deletions Compiler/Inst.mo
Expand Up @@ -158,6 +158,7 @@ protected import Print;
protected import Ceval;
protected import Error;
protected import ErrorExt;
protected import System;

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

Expand Down Expand Up @@ -3185,6 +3186,7 @@ algorithm
might have changed.. comp used in redeclare_type below..." ;
classmod_1 = Mod.lookupModificationP(mods_1, t);
mm_1 = Mod.lookupCompModification(mods_1, n);
(cache,m) = removeSelfModReference(cache,n,m); // Remove self-reference i.e. A a(x=a.y);
(cache,m_1) = Mod.elabMod(cache,env2, pre, m, impl);
mod = Mod.merge(classmod_1, mm_1, env2, pre);
mod1 = Mod.merge(mod, m_1, env2, pre);
Expand Down Expand Up @@ -3234,6 +3236,90 @@ algorithm
end matchcontinue;
end instElement;

protected function removeSelfModReference "Help function to elabMod, removes self-references in modifiers.
For instance, A a(x = a.y)
the modifier references the component itself. This is removed to avoid a circular dependency, resulting in
A a(x=y);
"
input Env.Cache inCache;
input Ident preId;
input SCode.Mod inMod;
output Env.Cache outCache;
output SCode.Mod outMod;
algorithm
outExp := matchcontinue(inCache,preId,inMod)
local Absyn.Exp e,e1; String id;
Absyn.Each ea;
Boolean fi;
list<SCode.SubMod> subs;
Env.Cache cache;
Integer cnt;
Boolean delayTpCheck;
case(cache,id,SCode.MOD(fi,ea,subs,SOME((e,_)))) equation
((e1,(_,cnt))) = Interactive.traverseExp(e,removeSelfModReferenceExp,(id,0));
(cache,subs) = removeSelfModReferenceSubs(cache,id,subs);
delayTpCheck = cnt > 0 ;
then (cache,SCode.MOD(fi,ea,subs,SOME((e1,delayTpCheck)))); // true to delay type checking/elabExp

case(cache,id,SCode.MOD(fi,ea,subs,NONE)) equation
(cache,subs) = removeSelfModReferenceSubs(cache,id,subs);
then (cache,SCode.MOD(fi,ea,subs,NONE));
case(cache,id,inMod) then (cache,inMod);
end matchcontinue;
end removeSelfModReference;

protected function removeSelfModReferenceSubs "Help function to removeSelfModeReference"
input Env.Cache inCache;
input String id;
input list<SCode.SubMod> inSubs;
output Env.Cache outCache;
output list<SCode.SubMod> outSubs;
algorithm
(outCache,outSubs) :=
matchcontinue(inCache,id,inSubs)
local Env.Cache cache;
list<SCode.Subscript> idxs;
list<SCode.SubMod> subs;
SCode.Mod mod;
Env.Cache cache;
String ident;
case (cache,id,{}) then (cache,{});

case(cache, id,SCode.NAMEMOD(ident,mod)::subs) equation
(cache,mod) = removeSelfModReference(cache,id,mod);
(cache,subs) = removeSelfModReferenceSubs(cache,id,subs);
then (cache,SCode.NAMEMOD(ident,mod)::subs);

case(cache,id,SCode.IDXMOD(idxs,mod)::subs) equation
(cache,mod) = removeSelfModReference(cache,id,mod);
(cache,subs) = removeSelfModReferenceSubs(cache,id,subs);
then (cache,SCode.IDXMOD(idxs,mod)::subs);
end matchcontinue;
end removeSelfModReferenceSubs;

protected function removeSelfModReferenceExp "Help function to removeSelfModReference."
input tuple<Absyn.Exp,tuple<String,Integer>> inExp;
output tuple<Absyn.Exp,tuple<String,Integer>> outExp;
algorithm
outExp := matchcontinue(inExp)
local Absyn.ComponentRef cr,cr1;
Absyn.Exp e,e1;
String id,id2;
Absyn.ComponentRef cr1;
Integer cnt;
case( (Absyn.CREF(cr),(id,cnt))) equation
Absyn.CREF_IDENT(id2,_) = Absyn.crefGetFirst(cr);
// prefix == first part of cref
0 = System.strcmp(id2,id);
cr1 = Absyn.crefStripFirst(cr);
then ((Absyn.CREF(cr1),(id,cnt+1)));

// other expressions falltrough
case((e,(id,cnt))) then ((e,(id,cnt)));
end matchcontinue;
end removeSelfModReferenceExp;


protected function checkRecursiveDefinition "Checks that a class does not have a recursive definition,
i.e. an instance of itself. This is not allowed in Modelica."
input Env.Env env;
Expand Down Expand Up @@ -3961,7 +4047,7 @@ algorithm
then
res;
case (SCode.REDECL(final_ = b,elementLst = {})) then {};
case ((mod as SCode.MOD(subModLst = submods,absynExpOption = SOME(e)))) /* Find in sub modifications e.g A(B=3) find B */
case ((mod as SCode.MOD(subModLst = submods,absynExpOption = SOME((e,_))))) /* Find in sub modifications e.g A(B=3) find B */
equation
l1 = getCrefFromSubmods(submods);
l2 = Absyn.getCrefFromExp(e);
Expand Down Expand Up @@ -8739,9 +8825,9 @@ algorithm
Debug.fprint("failtrace", "- inst_mod_equation failed\n type: ");
Debug.fcall("failtrace", Types.printType, t);
Debug.fprint("failtrace", "\n cref: ");
Debug.fcall("failtrace", Exp.printComponentRef, c);
Debug.fprint("failtrace", Exp.printComponentRefStr(c));
Debug.fprint("failtrace", "\n mod:");
Debug.fcall("failtrace", Mod.printMod, m);
Debug.fprint("failtrace", Mod.printModStr(m));
Debug.fprint("failtrace", "\n");
then
fail();
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Lookup.mo
Expand Up @@ -1626,7 +1626,7 @@ algorithm
{
SCode.NAMEMOD(id,
SCode.MOD(false,Absyn.NON_EACH(),{},
SOME(Absyn.CREF(Absyn.CREF_IDENT(id,{})))))},NONE)) :: restmod);
SOME((Absyn.CREF(Absyn.CREF_IDENT(id,{})),false))))},NONE)) :: restmod);
case ({}) then {};
end matchcontinue;
end buildRecordConstructorResultMod;
Expand Down
21 changes: 16 additions & 5 deletions Compiler/Mod.mo
Expand Up @@ -76,6 +76,7 @@ protected import Util;
protected import Ceval;
protected import Error;
protected import Print;
protected import Interactive;

public function elabMod "function: elabMod
Expand Down Expand Up @@ -105,7 +106,7 @@ algorithm
Exp.Exp e_1,e_2;
Types.Properties prop;
Option<Values.Value> e_val;
Absyn.Exp e;
Absyn.Exp e,e1;
list<tuple<SCode.Element, Types.Mod>> elist_1;
list<SCode.Element> elist;
Ident str;
Expand All @@ -116,7 +117,9 @@ algorithm
(cache,subs_1) = elabSubmods(cache,env, pre, subs, impl);
then
(cache,Types.MOD(final_,each_,subs_1,NONE));
case (cache,env,pre,(m as SCode.MOD(final_ = final_,each_ = each_,subModLst = subs,absynExpOption = SOME(e))),impl)

// Only elaborate expressions with non-delayed type checking, see SCode.MOD.
case (cache,env,pre,(m as SCode.MOD(final_ = final_,each_ = each_,subModLst = subs,absynExpOption = SOME((e,false)))),impl)
equation
(cache,subs_1) = elabSubmods(cache,env, pre, subs, impl);
(cache,e_1,prop,_) = Static.elabExp(cache,env, e, impl, NONE,true);
Expand All @@ -126,6 +129,14 @@ algorithm
from outer modifications.." ;
then
(cache,Types.MOD(final_,each_,subs_1,SOME(Types.TYPED(e_2,e_val,prop))));

// Delayed type checking
case (cache,env,pre,(m as SCode.MOD(final_ = final_,each_ = each_,subModLst = subs,absynExpOption = SOME((e,true)))),impl)
equation
(cache,subs_1) = elabSubmods(cache,env, pre, subs, impl);
then
(cache,Types.MOD(final_,each_,subs_1,SOME(Types.UNTYPED(e))));

case (cache,env,pre,(m as SCode.REDECL(final_ = final_,elementLst = elist)),impl)
equation
elist_1 = Inst.addNomod(elist);
Expand Down Expand Up @@ -201,15 +212,15 @@ algorithm
equation
subs_1 = unelabSubmods(subs);
then
SCode.MOD(final_,each_,subs_1,SOME(e));
SCode.MOD(final_,each_,subs_1,SOME((e,false))); // Default type checking non-delayed
case ((m as Types.MOD(final_ = final_,each_ = each_,subModLst = subs,eqModOption = SOME(Types.TYPED(e,_,p)))))
local Exp.Exp e;
equation
es = Exp.printExpStr(e);
subs_1 = unelabSubmods(subs);
e_1 = Exp.unelabExp(e);
then
SCode.MOD(final_,each_,subs_1,SOME(e_1));
SCode.MOD(final_,each_,subs_1,SOME((e_1,false))); // default typechecking non-delayed
case ((m as Types.REDECL(final_ = final_,tplSCodeElementModLst = elist)))
equation
elist_1 = Util.listMap(elist, Util.tuple21);
Expand Down Expand Up @@ -459,7 +470,7 @@ algorithm
subs_1 = elabUntypedSubmods(subs, env, pre);
then
Types.MOD(final_,each_,subs_1,NONE);
case ((m as SCode.MOD(final_ = final_,each_ = each_,subModLst = subs,absynExpOption = SOME(e))),env,pre)
case ((m as SCode.MOD(final_ = final_,each_ = each_,subModLst = subs,absynExpOption = SOME((e,_)))),env,pre)
equation
subs_1 = elabUntypedSubmods(subs, env, pre);
then
Expand Down

0 comments on commit a4a361e

Please sign in to comment.