Skip to content

Commit f14f22b

Browse files
author
Peter Aronsson
committed
-Implemented support for linspace() function in frontend.
-Fixed bug with slicing in modifiers in several sub-component modifiers, e.g. PositivePin p[2](i(start={1,2})); -Fixed bug in prefixExp on general ASUB expressions git-svn-id: https://openmodelica.org/svn/OpenModelica/branches/MathCoreOSMC@4162 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 5f53d7f commit f14f22b

File tree

7 files changed

+147
-12
lines changed

7 files changed

+147
-12
lines changed

Compiler/Builtin.mo

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2160,6 +2160,14 @@ protected constant tuple<Types.TType, Option<Type_a>> intInt2vectorreal=(
21602160
("y",(Types.T_INTEGER({}),NONE))},
21612161
(Types.T_ARRAY(Types.DIM(SOME(1)),(Types.T_REAL({}),NONE)),
21622162
NONE)),NONE);
2163+
2164+
protected constant tuple<Types.TType, Option<Type_a>> realRealInt2vectorreal=(
2165+
Types.T_FUNCTION(
2166+
{("x",(Types.T_REAL({}),NONE)),
2167+
("y",(Types.T_REAL({}),NONE)),
2168+
("n",(Types.T_INTEGER({}),NONE))},
2169+
(Types.T_ARRAY(Types.DIM(NONE),(Types.T_REAL({}),NONE)),
2170+
NONE)),NONE);
21632171

21642172
protected constant tuple<Types.TType, Option<Type_a>> array1dimint2array3dimint=(
21652173
Types.T_FUNCTION(
@@ -3154,7 +3162,7 @@ algorithm
31543162
env = Env.extendFrameT(env, "array", n6real2arrayreal);
31553163
env = Env.extendFrameT(env, "array", n7real2arrayreal);
31563164
env = Env.extendFrameT(env, "array", n8real2arrayreal);
3157-
env = Env.extendFrameT(env, "linspace", intInt2vectorreal);
3165+
env = Env.extendFrameT(env, "linspace", realRealInt2vectorreal);
31583166
env = Env.extendFrameT(env, "min", intInt2int);
31593167
env = Env.extendFrameT(env, "min", realReal2real);
31603168
env = Env.extendFrameT(env, "min", array1dimint2int);

Compiler/Ceval.mo

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,7 @@ algorithm
923923
case "identity" then cevalBuiltinIdentity;
924924
case "promote" then cevalBuiltinPromote;
925925
case "String" then cevalBuiltinString;
926+
case "linspace" then cevalBuiltinLinspace;
926927
//case "semiLinear" then cevalBuiltinSemiLinear;
927928
//case "delay" then cevalBuiltinDelay;
928929
case id
@@ -1971,6 +1972,70 @@ algorithm
19711972
end matchcontinue;
19721973
end cevalBuiltinString;
19731974

1975+
protected function cevalBuiltinLinspace "
1976+
author: PA
1977+
Evaluates the linpace function"
1978+
input Env.Cache inCache;
1979+
input Env.Env inEnv;
1980+
input list<Exp.Exp> inExpExpLst;
1981+
input Boolean inBoolean;
1982+
input Option<Interactive.InteractiveSymbolTable> inInteractiveInteractiveSymbolTableOption;
1983+
input Msg inMsg;
1984+
output Env.Cache outCache;
1985+
output Values.Value outValue;
1986+
output Option<Interactive.InteractiveSymbolTable> st;
1987+
algorithm
1988+
(outCache,outValue,outInteractiveInteractiveSymbolTableOption):=
1989+
matchcontinue (inCache,inEnv,inExpExpLst,inBoolean,st,inMsg)
1990+
local
1991+
Exp.Exp x,y,n; Integer size;
1992+
Real rx,ry; list<Values.Value> valLst; Env.Cache cache; Boolean impl; Env.Env env; Msg msg;
1993+
case (cache,env,{x,y,n},impl,st,msg) equation
1994+
(cache,Values.INTEGER(size),_) = ceval(cache,env, n, impl, st, NONE, msg);
1995+
verifyLinspaceN(size,{x,y,n});
1996+
(cache,Values.REAL(rx),_) = ceval(cache,env, x, impl, st, NONE, msg);
1997+
(cache,Values.REAL(ry),_) = ceval(cache,env, y, impl, st, NONE, msg);
1998+
valLst = cevalBuiltinLinspace2(rx,ry,size,1);
1999+
then (cache,Values.ARRAY(valLst),st);
2000+
2001+
end matchcontinue;
2002+
end cevalBuiltinLinspace;
2003+
2004+
protected function verifyLinspaceN "checks that n>=2 for linspace(x,y,n) "
2005+
input Integer n;
2006+
input list<Exp.Exp> expl;
2007+
algorithm
2008+
_ := matchcontinue(n,expl)
2009+
local String s; Exp.Exp x,y,nx;
2010+
case(n,_) equation
2011+
true = n >= 2;
2012+
then ();
2013+
case(_,{x,y,nx}) equation
2014+
s = "linspace("+&Exp.printExpStr(x)+&", "+&Exp.printExpStr(y)+&", "+&Exp.printExpStr(nx)+&")";
2015+
Error.addMessage(Error.LINSPACE_ILLEGAL_SIZE_ARG,{s});
2016+
then fail();
2017+
end matchcontinue;
2018+
end verifyLinspaceN;
2019+
2020+
protected function cevalBuiltinLinspace2 "Helper function to cevalBuiltinLinspace"
2021+
input Real rx;
2022+
input Real ry;
2023+
input Integer size;
2024+
input Integer i "iterator 1 <= i <= size";
2025+
output list<Values.Value> valLst;
2026+
algorithm
2027+
valLst := matchcontinue(rx,ry,size,i)
2028+
local Real r;
2029+
case(rx,ry,size,i) equation
2030+
true = i > size;
2031+
then {};
2032+
case(rx,ry,size,i) equation
2033+
r = rx +. (ry -. rx)*. intReal(i-1) /. intReal(size - 1);
2034+
valLst = cevalBuiltinLinspace2(rx,ry,size,i+1);
2035+
then Values.REAL(r)::valLst;
2036+
end matchcontinue;
2037+
end cevalBuiltinLinspace2;
2038+
19742039
protected function cevalCat "function: cevalCat
19752040
evaluates the cat operator given a list of
19762041
array values and a concatenation dimension."

Compiler/Error.mo

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ public constant ErrorID STRUCT_SINGULAR_SYSTEM_INITIALIZATION=111;
198198
public constant ErrorID CIRCULAR_EQUATION=112;
199199
public constant ErrorID IF_EQUATION_NO_ELSE=113;
200200
public constant ErrorID IF_EQUATION_UNBALANCED=114;
201+
public constant ErrorID LINSPACE_ILLEGAL_SIZE_ARG=115
202+
;
201203
public constant ErrorID UNBOUND_PARAMETER_WARNING=500;
202204
public constant ErrorID BUILTIN_FUNCTION_SUM_HAS_SCALAR_PARAMETER=501;
203205
public constant ErrorID BUILTIN_FUNCTION_PRODUCT_HAS_SCALAR_PARAMETER=502;
@@ -487,7 +489,9 @@ protected constant list<tuple<Integer, MessageType, Severity, String>> errorTabl
487489
(SELECTED_STATES,TRANSLATION(),NOTIFICATION(), "The following variables are selected as states: %s"),
488490
(INCONSISTENT_UNITS, TRANSLATION(),WARNING(),"The system of units is inconsistent in term %s with the units %s and %s respectively."),
489491
(IF_EQUATION_NO_ELSE, TRANSLATION(),ERROR(),"In equation %s. If-equation with conditions that are not parameter expressions must have an else branch, in equation."),
490-
(IF_EQUATION_UNBALANCED, TRANSLATION(),ERROR(),"In equation %s. If-equation with conditions that are not parameter expressions must have the same number of equations in each branch, equation count is %s for each respective branch.")
492+
(IF_EQUATION_UNBALANCED, TRANSLATION(),ERROR(),"In equation %s. If-equation with conditions that are not parameter expressions must have the same number of equations in each branch, equation count is %s for each respective branch."),
493+
(LINSPACE_ILLEGAL_SIZE_ARG,TRANSLATION(),ERROR(),"In expression %s, third argument to linspace must be >= 2")
494+
491495
};
492496

493497
protected import ErrorExt;

Compiler/Inst.mo

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,7 @@ algorithm
14501450
case(cache,env,id,_,bind,expectedTp,Types.PROP(bindTp,_)) local String s1,s2;
14511451
equation
14521452
failure((_,_) = Types.matchType(bind,bindTp,expectedTp));
1453-
s1 = "builtin attribute " +& id;
1453+
s1 = "builtin attribute " +& id +& " of type "+&Types.unparseType(bindTp);
14541454
s2 = Types.unparseType(expectedTp);
14551455
Error.addMessage(Error.TYPE_ERROR,{s1,s2});
14561456
then fail();
@@ -5835,7 +5835,7 @@ algorithm
58355835
case (cache,env,store,ci_state,mod,pre,csets,n,cl,attr,prot,(dim :: dims),idxs,inst_dims,impl,comment,io,finalPrefix,graph)
58365836
equation
58375837
dime = instDimExp(dim, impl);
5838-
inst_dims_1 = Util.listListAppendLast(inst_dims, {dime});
5838+
inst_dims_1 = Util.listListAppendLast(inst_dims, {dime});
58395839
(cache,compenv,store,dae,Connect.SETS(_,crs,dc,oc),ty,graph) =
58405840
instArray(cache,env,store, ci_state, mod, pre, csets, n, (cl,attr),prot, 1, dim, dims, idxs, inst_dims_1, impl, comment,io,finalPrefix,graph);
58415841
dimt = instDimType(dim);
@@ -6747,7 +6747,7 @@ algorithm
67476747
(cache,env_1,store,{dae},csets,ty,graph);
67486748

67496749
case (cache,env,store,ci_state,mod,pre,csets,n,(cl,attr),prot,i,DIMEXP(subscript = _),dims,idxs,inst_dims,impl,comment,io,finalPrefix,graph)
6750-
equation
6750+
equation
67516751
(cache,compenv,store,daeLst,csets,ty,graph) =
67526752
instVar2(cache, env, store,ci_state, mod, pre, csets, n, cl, attr, prot, dims, (i :: idxs), inst_dims, impl, comment,io,finalPrefix,graph);
67536753
then

Compiler/Mod.mo

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,10 +1134,7 @@ algorithm
11341134
(mod_1,subs_1) = lookupIdxModification2(subs, NONE, idx);
11351135
mod_2 = merge(Types.MOD(f,each_,subs_1,NONE), mod_1, {}, Prefix.NOPRE());
11361136
eq_1 = indexEqmod(eq, {idx});
1137-
mod_3 = merge(mod_2, Types.MOD(f,each_,{},eq_1), {}, Prefix.NOPRE()) " & print \"lookup_idx_modificaton input :\" &
1138-
print_mod_str inmod => s & print s & print \"\\n\"
1139-
& print \"lookup_idx_modificaton returns :\" &
1140-
print_mod_str mod\'\'\' => s & print s & print \"\\n\"" ;
1137+
mod_3 = merge(mod_2, Types.MOD(f,each_,{},eq_1), {}, Prefix.NOPRE());
11411138
then
11421139
mod_3;
11431140
case (mod,idx)
@@ -1235,15 +1232,16 @@ algorithm
12351232
local
12361233
Option<Types.EqMod> eq_1,eq;
12371234
Boolean f;
1238-
list<Types.SubMod> subs;
1235+
list<Types.SubMod> subs,subs_1;
12391236
Integer idx;
12401237
case (Types.NOMOD(),_) then Types.NOMOD(); /* indx */
12411238
case (Types.REDECL(finalPrefix = _),_) then Types.NOMOD();
12421239
case (Types.MOD(finalPrefix = f,each_ = Absyn.NON_EACH(),subModLst = subs,eqModOption = eq),idx)
12431240
equation
1241+
(_,subs_1) = lookupIdxModification2(subs, NONE, idx);
12441242
eq_1 = indexEqmod(eq, {idx});
12451243
then
1246-
Types.MOD(f,Absyn.NON_EACH(),subs,eq_1);
1244+
Types.MOD(f,Absyn.NON_EACH(),subs_1,eq_1);
12471245
case (Types.MOD(finalPrefix = f,each_ = Absyn.EACH(),subModLst = subs,eqModOption = eq),idx) then Types.MOD(f,Absyn.EACH(),subs,eq);
12481246
case (_,_) equation
12491247
Debug.fprint("failtrace", "-lookupIdxModification3 failed\n");

Compiler/Prefix.mo

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,15 @@ algorithm
361361
e2 = Exp.ASUB(Exp.CREF(p_1,t),es_1);
362362
then (cache,e2);
363363

364+
case (cache,env,(e as Exp.ASUB(exp = e1, sub = expl)),pre)
365+
local list<Exp.Exp> expl;
366+
equation
367+
(cache,es_1) = prefixExpList(cache,env, expl, pre);
368+
(cache,e1) = prefixExp(cache,env,e1,pre);
369+
e2 = Exp.ASUB(e1,es_1);
370+
then (cache,e2);
371+
372+
364373
case (cache,env,Exp.CREF(componentRef = p,ty = t),pre)
365374
equation
366375
(cache,_,_,_) = Lookup.lookupVarLocal(cache,env, p);

Compiler/Static.mo

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5122,7 +5122,57 @@ algorithm
51225122
Types.PROP((Types.T_STRING({}),NONE),c));
51235123
end matchcontinue;
51245124
end elabBuiltinString;
5125-
5125+
5126+
protected function elabBuiltinLinspace "
5127+
author: PA
5128+
5129+
This function handles the built-in linspace function.
5130+
e.g. linspace(1,3,3) => {1,2,3}
5131+
linspace(0,1,5) => {0,0.25,0.5,0.75,1.0}
5132+
"
5133+
input Env.Cache inCache;
5134+
input Env.Env inEnv;
5135+
input list<Absyn.Exp> expl;
5136+
input list<Absyn.NamedArg> inNamedArg;
5137+
input Boolean impl;
5138+
output Env.Cache outCache;
5139+
output Exp.Exp outExp;
5140+
output Types.Properties outProperties;
5141+
algorithm
5142+
(outCache,outExp,outProperties):=
5143+
matchcontinue (inCache,inEnv,expl,inNamedArg,impl)
5144+
local Absyn.Exp x,y,n;
5145+
Exp.Exp x1,y1,n1,x2,y2; Types.Type tp1,tp2,tp3,tp11,tp22; Types.Const c1,c2,c3,c;
5146+
Integer size;
5147+
Env.Cache cache; Env.Env env;
5148+
5149+
/* linspace(x,y,n) where n is constant or parameter */
5150+
case (cache,env,{x,y,n},_,impl) equation
5151+
(cache,x1,Types.PROP(tp1,c1),_) = elabExp(cache,env, x, impl, NONE,true);
5152+
(cache,y1,Types.PROP(tp2,c2),_) = elabExp(cache,env, y, impl, NONE,true);
5153+
(x2,tp11) = Types.matchType(x1,tp1,(Types.T_REAL({}),NONE));
5154+
(y2,tp22) = Types.matchType(y1,tp2,(Types.T_REAL({}),NONE));
5155+
(cache,n1,Types.PROP(tp3 as (Types.T_INTEGER(_),_),c3),_) = elabExp(cache,env, n, impl, NONE,true);
5156+
true = Types.isParameterOrConstant(c3);
5157+
(cache,Values.INTEGER(size),_) = Ceval.ceval(cache,env, n1, false, NONE, NONE, Ceval.MSG());
5158+
c = Types.constAnd(c1,c2);
5159+
then (cache, Exp.CALL(Absyn.IDENT("linspace"),{x2,y2,n1},false,true,Exp.T_ARRAY(Exp.REAL(),{SOME(size)})),
5160+
Types.PROP((Types.T_ARRAY(Types.DIM(SOME(size)),tp11),NONE),c));
5161+
5162+
/* linspace(x,y,n) where n is variable time expression */
5163+
case (cache,env,{x,y,n},_,impl) equation
5164+
(cache,x1,Types.PROP(tp1,c1),_) = elabExp(cache,env, x, impl, NONE,true);
5165+
(cache,y1,Types.PROP(tp2,c2),_) = elabExp(cache,env, y, impl, NONE,true);
5166+
(x2,tp11) = Types.matchType(x1,tp1,(Types.T_REAL({}),NONE));
5167+
(y2,tp22) = Types.matchType(y1,tp2,(Types.T_REAL({}),NONE));
5168+
(cache,n1,Types.PROP(tp3 as (Types.T_INTEGER(_),_),c3),_) = elabExp(cache,env, n, impl, NONE,true);
5169+
false = Types.isParameterOrConstant(c3);
5170+
c = Types.constAnd(c1,Types.constAnd(c2,c3));
5171+
then (cache, Exp.CALL(Absyn.IDENT("linspace"),{x2,y2,n1},false,true,Exp.T_ARRAY(Exp.REAL(),{NONE})),
5172+
Types.PROP((Types.T_ARRAY(Types.DIM(NONE),tp11),NONE),c));
5173+
end matchcontinue;
5174+
end elabBuiltinLinspace;
5175+
51265176
protected function elabBuiltinVector "function: elabBuiltinVector
51275177
author: PA
51285178
@@ -5403,6 +5453,7 @@ algorithm
54035453
case "cross" then elabBuiltinCross;
54045454
case "skew" then elabBuiltinSkew;
54055455
case "String" then elabBuiltinString;
5456+
case "linspace" then elabBuiltinLinspace;
54065457
end matchcontinue;
54075458
end elabBuiltinHandler;
54085459

0 commit comments

Comments
 (0)