Linux GNU 11.4.0 Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 56.5% 235 / 0 / 416
Functions: -% 0 / 1 / 1
Branches: 38.6% 81 / 0 / 210

OMCompiler/Compiler/FrontEnd/FUnitCheck.mo
Line Branch Exec Source
1 /*
2 * This file is part of OpenModelica.
3 *
4 * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC),
5 * c/o Linköpings universitet, Department of Computer and Information Science,
6 * SE-58183 Linköping, Sweden.
7 *
8 * All rights reserved.
9 *
10 * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF AGPL VERSION 3 LICENSE OR
11 * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8.
12 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
13 * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GNU AGPL
14 * VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
15 *
16 * The OpenModelica software and the OSMC (Open Source Modelica Consortium)
17 * Public License (OSMC-PL) are obtained from OSMC, either from the above
18 * address, from the URLs:
19 * http://www.openmodelica.org or
20 * https://github.com/OpenModelica/ or
21 * http://www.ida.liu.se/projects/OpenModelica,
22 * and in the OpenModelica distribution.
23 *
24 * GNU AGPL version 3 is obtained from:
25 * https://www.gnu.org/licenses/licenses.html#GPL
26 *
27 * This program is distributed WITHOUT ANY WARRANTY; without
28 * even the implied warranty of MERCHANTABILITY or FITNESS
29 * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
30 * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
31 *
32 * See the full OSMC Public License conditions for more details.
33 *
34 */
35
36 encapsulated package FUnitCheck
37 " file: UnitCheck.mo
38 package: UnitCheck
39 description: This package provides everything for advanced unit checking:
40 - for all variables unspecified units get calculated if possible
41 - inconsistent equations get reported in a user friendly way
42 authors: Jan Hagemann and Lennart Ochel (FH Bielefeld, Germany)"
43
44 public
45 import Absyn;
46 import AbsynUtil;
47 import AvlTreePathFunction;
48 import DAE;
49 import Unit = FUnit;
50 import System;
51
52 protected
53 import BaseHashTable;
54 import ComponentReference;
55 protected import ComponentReferenceBasics;
56 import Error;
57 import Expression;
58 protected import ExpressionBasics;
59 import ExpressionDump;
60 import Flags;
61 import HashTableCrToUnit = FHashTableCrToUnit;
62 import HashTableStringToUnit = FHashTableStringToUnit;
63 import HashTableUnitToString = FHashTableUnitToString;
64 import List;
65 import DAEUtil;
66 import DAEDump;
67 import SCode;
68 import ExecStat.execStat;
69
70 public uniontype Functionargs
71 record FUNCTIONUNITS
72 String name;
73 list<String> invars;
74 list<String> outvars;
75 list<String> inunits;
76 list<String> outunits;
77 end FUNCTIONUNITS;
78 end Functionargs;
79
80
81 public function checkUnits
82 input DAE.DAElist inDAE;
83 input AvlTreePathFunction.Tree func;
84 output DAE.DAElist outDAE = inDAE;
85 protected
86 DAE.DAElist elts1, elts2;
87 list<DAE.Element> eqlist, varlist, newdaelist;
88 list<DAE.Function> functionlist;
89 list<Functionargs> args;
90 HashTableCrToUnit.HashTable HtCr2U1, HtCr2U2;
91 HashTableStringToUnit.HashTable HtS2U;
92 HashTableUnitToString.HashTable HtU2S;
93 algorithm
94
5/6
✓ Branch 1 taken 990 times.
✓ Branch 2 taken 10 times.
✓ Branch 4 taken 14 times.
✓ Branch 5 taken 976 times.
✓ Branch 7 taken 14 times.
✗ Branch 8 not taken.
1000 if not (Flags.getConfigBool(Flags.UNIT_CHECKING) or (Flags.getConfigBool(Flags.CHECK_MODEL) and Flags.isSet(Flags.SCODE_INST))) then
95 990 return;
96 end if;
97 try
98 10 (elts1, elts2) := DAEUtil.splitDAEIntoVarsAndEquations(inDAE);
99 10 varlist := GetVarList(elts1);
100 10 eqlist := GetElementList(elts2);
101 10 functionlist := DAEUtil.getFunctionList(func);
102
103 10 HtCr2U1 := HashTableCrToUnit.emptyHashTableSized(Util.nextPrime(integer(10+1.4*listLength(varlist))));
104 10 HtS2U := Unit.getKnownUnits();
105 10 HtU2S := Unit.getKnownUnitsInverse();
106
107 args := {FUNCTIONUNITS("", {}, {}, {}, {})};
108
109 10 args := List.mapFlat(functionlist, parseFunctionList);
110
111 // new instantiation
112 //((HtCr2U1, HtS2U, HtU2S)) := List.fold(varlist, convertUnitString2unit, (HtCr2U1, HtS2U, HtU2S));
113 // old instantiation
114 10 (HtCr2U1, HtS2U, HtU2S) := List.fold(varlist, convertUnitString2unit_old, (HtCr2U1, HtS2U, HtU2S));
115 10 HtCr2U2 := BaseHashTable.copy(HtCr2U1);
116 10 (HtCr2U2, HtS2U, HtU2S) := algo(varlist, eqlist, args, HtCr2U2, HtS2U, HtU2S);
117 10 varlist := List.map2(varlist, returnVar, HtCr2U2, HtU2S);
118 10 newdaelist := listAppend(varlist, eqlist);
119
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 if Flags.isSet(Flags.DUMP_UNIT) then
120 10 BaseHashTable.dumpHashTable(HtCr2U2);
121 10 print("######## UnitCheck COMPLETED ########\n");
122 end if;
123 10 notification(HtCr2U1, HtCr2U2, HtU2S);
124 10 outDAE := updateDAElist(inDAE, newdaelist);
125 else
126 ✗ Error.addInternalError(getInstanceName() + ": unit check module failed", sourceInfo());
127 end try;
128
129 10 execStat(getInstanceName());
130 end checkUnits;
131
132
133
134 protected function parseFunctionList
135 input DAE.Function infunction;
136 output list<Functionargs> outTpl;
137 protected
138 list<DAE.Element> inelt, outelt;
139 list<String> inunits, outunits, inargs, outargs;
140 String s;
141 algorithm
142 20 s := getFunctionName(infunction);
143 20 inelt := DAEUtil.getFunctionInputVars(infunction);
144 20 outelt := DAEUtil.getFunctionOutputVars(infunction);
145 20 inunits := List.filterMap(inelt,getUnits);
146 20 outunits := List.filterMap(outelt,getUnits);
147 20 inargs := List.filterMap(inelt,getVars);
148 20 outargs := List.filterMap(outelt,getVars);
149 20 outTpl := {FUNCTIONUNITS(s,inargs,outargs,inunits,outunits)};
150 end parseFunctionList;
151
152
153
154 public function getFunctionName
155 input DAE.Function inFunction;
156 output String outString = AbsynUtil.pathString(AbsynUtil.makeNotFullyQualified(DAEUtil.functionName(inFunction)));
157 end getFunctionName;
158
159 function getVars
160 input DAE.Element inElement;
161 output String outString;
162 algorithm
163 outString := match inElement
164 local
165 DAE.ComponentRef cr;
166
167 case DAE.VAR(componentRef=cr)
168 40 then ComponentReference.crefStr(cr);
169
170 else "";
171 end match;
172 end getVars;
173
174
175 function getUnits
176 input DAE.Element inElement;
177 output String outString;
178 algorithm
179 outString := match inElement
180 local
181 String unitString;
182
183 case DAE.VAR(ty=DAE.T_REAL(), variableAttributesOption=SOME(DAE.VAR_ATTR_REAL(unit=SOME(DAE.SCONST(unitString)))))
184 guard(unitString <> "")
185 then unitString;
186
187 else "NONE";
188 end match;
189 end getUnits;
190
191
192
193
194 protected function updateDAElist
195 input DAE.DAElist indaelist;
196 input list<DAE.Element> indaevarlist;
197 output DAE.DAElist outdaelist;
198 algorithm
199 outdaelist:= match(indaelist,indaevarlist)
200 local
201 list<DAE.Element> varlist2;
202 DAE.DAElist outdae;
203 String ident;
204 DAE.ElementSource eltsrc;
205 Option<SCode.Comment> comment;
206
207 case(DAE.DAE(elementLst={DAE.COMP(ident=ident,source=eltsrc,comment=comment)}),varlist2)
208 algorithm
209 20 outdae:=DAE.DAE({DAE.COMP(ident,varlist2,eltsrc,comment)});
210 then
211 (outdae);
212 end match;
213 end updateDAElist;
214
215
216 protected function returnVar "returns the new calculated units in DAE"
217 input DAE.Element inVar;
218 input HashTableCrToUnit.HashTable inHtCr2U;
219 input HashTableUnitToString.HashTable inHtU2S;
220 output DAE.Element outVar;
221 algorithm
222 outVar := match inVar
223 local
224 DAE.Element var;
225 DAE.ComponentRef cr;
226 Unit.Unit ut;
227 Option<DAE.VariableAttributes> attr;
228 String s;
229
230 case DAE.VAR(variableAttributesOption=SOME(DAE.VAR_ATTR_REAL(unit=SOME(_)))) then inVar;
231
232 case DAE.VAR(componentRef=cr,variableAttributesOption=attr) algorithm
233
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 if BaseHashTable.hasKey(cr, inHtCr2U) then
234 10 ut := BaseHashTable.get(cr, inHtCr2U);
235
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2 times.
10 if Unit.isUnit(ut) then
236 8 s := Unit.unitString(ut, inHtU2S);
237 8 attr := DAEUtil.setUnitAttr(attr, DAE.SCONST(s));
238 8 inVar.variableAttributesOption := attr;
239 var := inVar;
240 else
241 var := inVar;
242 end if;
243 else
244 var := inVar;
245 end if;
246 then var;
247 end match;
248 end returnVar;
249
250
251
252 protected function notification "dumps the calculated units"
253 input HashTableCrToUnit.HashTable inHtCr2U1;
254 input HashTableCrToUnit.HashTable inHtCr2U2;
255 input HashTableUnitToString.HashTable inHtU2S;
256 protected
257 String str;
258 list<tuple<DAE.ComponentRef, Unit.Unit>> lt1;
259 algorithm
260 10 lt1 := BaseHashTable.hashTableList(inHtCr2U1);
261 10 str := notification2(lt1, inHtCr2U2, inHtU2S);
262
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 6 times.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
10 if Flags.isSet(Flags.DUMP_UNIT) and str<>"" then
263 6 Error.addCompilerNotification(str);
264 end if;
265 end notification;
266
267
268 protected function notification2 "help-function"
269 input list<tuple<DAE.ComponentRef, Unit.Unit>> inLt1;
270 input HashTableCrToUnit.HashTable inHtCr2U2;
271 input HashTableUnitToString.HashTable inHtU2S;
272 output String outS;
273 protected
274 DAE.ComponentRef cr1=DAE.emptyCref;
275 Real factor1=0;
276 Integer i1=0, i2=0, i3=0, i4=0, i5=0, i6=0, i7=0;
277 algorithm
278
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 10 times.
53 outS := stringAppendList(list(
279 // We already assigned the variables before
280 "\"" + ComponentReference.crefStr(cr1) + "\" has the Unit \"" + Unit.unitString(Unit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7), inHtU2S) + "\"\n"
281 // Do the filtering and unboxing stuff at the same time; then we only need one hashtable call
282 // And we only use a try-block for MASTER nodes
283 for t1 guard match t1 local Boolean b; case (cr1,Unit.MASTER()) algorithm
284 b := false;
285 try
286
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 10 times.
12 Unit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7) := BaseHashTable.get(cr1, inHtCr2U2);
287 b := true;
288 else
289 end try;
290 then b; else false; end match in inLt1
291 ));
292 end notification2;
293
294
295 public function algo "algorithm to check the consistency"
296 input list<DAE.Element> invarlist;
297 input list<DAE.Element> ineqList;
298 input list<Functionargs> inargs;
299 input HashTableCrToUnit.HashTable inHtCr2U;
300 input HashTableStringToUnit.HashTable inHtS2U;
301 input HashTableUnitToString.HashTable inHtU2S;
302 output tuple<HashTableCrToUnit.HashTable /* outHtCr2U */, HashTableStringToUnit.HashTable /* outHtS2U */, HashTableUnitToString.HashTable /* outHtU2S */> outTpl;
303 protected
304 HashTableCrToUnit.HashTable HtCr2U;
305 HashTableStringToUnit.HashTable HtS2U;
306 HashTableUnitToString.HashTable HtU2S;
307 Boolean b1, b2, b3;
308 algorithm
309 10 (HtCr2U, b1, HtS2U, HtU2S) := List.fold(invarlist, foldBindingExp, (inHtCr2U, true, inHtS2U, inHtU2S));
310 10 (HtCr2U, b2, HtS2U, HtU2S) := List.fold1(ineqList, foldEquation ,inargs,(HtCr2U, true, HtS2U, HtU2S));
311 10 b3 := BaseHashTable.hasKey(FUnit.UPDATECREF, HtCr2U);
312 //outTpl := algo2(b1, b2, b3, invarlist, ineqList, HtCr2U, HtS2U, HtU2S);
313 10 outTpl :=(HtCr2U, HtS2U, HtU2S);
314 end algo;
315
316
317 protected function foldBindingExp "folds the Binding expressions"
318 input DAE.Element inVar;
319 input tuple<HashTableCrToUnit.HashTable /* inHtCr2U */, Boolean /* success */, HashTableStringToUnit.HashTable /* inHtS2U */, HashTableUnitToString.HashTable /* inHtU2S */> inTpl;
320 output tuple<HashTableCrToUnit.HashTable /* outHtCr2U */, Boolean /* success */, HashTableStringToUnit.HashTable /* outHtS2U */, HashTableUnitToString.HashTable /* outHtU2S */> outTpl;
321 algorithm
322 outTpl := match(inVar, inTpl)
323 local
324 DAE.Exp exp, crefExp;
325 DAE.ComponentRef cref;
326 HashTableCrToUnit.HashTable HtCr2U;
327 HashTableStringToUnit.HashTable HtS2U;
328 HashTableUnitToString.HashTable HtU2S;
329 Boolean b;
330 DAE.Element eq;
331 DAE.ElementSource source;
332
333 case (DAE.VAR(componentRef=cref, ty=DAE.T_REAL(), binding=SOME(exp),source=source), (HtCr2U, b, HtS2U, HtU2S))
334 algorithm
335 12 crefExp := Expression.crefExp(cref);
336 12 eq := DAE.EQUATION(crefExp, exp, source);
337
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 (HtCr2U, b, HtS2U, HtU2S):=foldEquation(eq,{},(HtCr2U, b, HtS2U, HtU2S));
338
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 then ((HtCr2U, b, HtS2U, HtU2S));
339
340 case (DAE.VAR(ty=DAE.T_REAL(), binding=SOME(_)), (HtCr2U, _, HtS2U, HtU2S))
341 ✗ then ((HtCr2U, false, HtS2U, HtU2S));
342
343 else inTpl;
344 end match;
345 end foldBindingExp;
346
347
348
349 protected function foldEquation "folds the equations or return the error message of incosistent equations"
350 input DAE.Element inEq;
351 input list<Functionargs> inargs;
352 input tuple<HashTableCrToUnit.HashTable /* inHtCr2U */, Boolean /* success */, HashTableStringToUnit.HashTable /* inHtS2U */, HashTableUnitToString.HashTable /* inHtU2S */> inTpl;
353 output tuple<HashTableCrToUnit.HashTable /* outHtCr2U */, Boolean /* success */, HashTableStringToUnit.HashTable /* outHtS2U */, HashTableUnitToString.HashTable /* outHtU2S */> outTpl;
354 protected
355 HashTableCrToUnit.HashTable HtCr2U;
356 HashTableStringToUnit.HashTable HtS2U;
357 HashTableUnitToString.HashTable HtU2S;
358 list<list<tuple<DAE.Exp, Unit.Unit>>> expListList;
359 Boolean b;
360 algorithm
361 42 (HtCr2U, b, HtS2U, HtU2S):=inTpl;
362 42 (HtCr2U, HtS2U, HtU2S, expListList):=foldEquation2(inEq, HtCr2U, HtS2U, HtU2S,inargs);
363 42 List.map2_0(expListList, Errorfunction, inEq, HtU2S);
364
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 outTpl := (HtCr2U, b, HtS2U, HtU2S);
365 end foldEquation;
366
367
368 protected function foldEquation2 "help-function"
369 input DAE.Element eq;
370 input output HashTableCrToUnit.HashTable htCr2U;
371 input output HashTableStringToUnit.HashTable htS2U;
372 input output HashTableUnitToString.HashTable htU2S;
373 input list<Functionargs> args;
374 output list<list<tuple<DAE.Exp, Unit.Unit>>> inconsistentUnits = {};
375 algorithm
376 inconsistentUnits := match eq
377 local
378 DAE.Exp temp, lhs;
379 list<list<tuple<DAE.Exp, Unit.Unit>>> expList2, expList3;
380 Absyn.Path path;
381 Boolean b;
382 Unit.Unit ut1, ut2;
383 String s1, formalargs, formalvar;
384 list<String> outvars, outunitlist;
385 list<DAE.Exp> expl;
386
387 // solved Equation
388 case DAE.DEFINE()
389 algorithm
390 ✗ lhs := DAE.CREF(eq.componentRef, DAE.T_REAL_DEFAULT);
391 ✗ temp := DAE.BINARY(eq.exp, DAE.SUB(DAE.T_REAL_DEFAULT), lhs);
392
393 ✗ if Flags.isSet(Flags.DUMP_EQ_UNIT_STRUCT) then
394 ✗ ExpressionDump.dumpExp(temp);
395 end if;
396
397 ✗ (_, (htCr2U, htS2U, htU2S), inconsistentUnits) :=
398 insertUnitInEquation(temp, (htCr2U, htS2U, htU2S), Unit.MASTER({}), args);
399 then
400 inconsistentUnits;
401
402 case DAE.INITIALDEFINE()
403 algorithm
404 ✗ lhs := DAE.CREF(eq.componentRef, DAE.T_REAL_DEFAULT);
405 ✗ temp := DAE.BINARY(eq.exp, DAE.SUB(DAE.T_REAL_DEFAULT), lhs);
406
407 ✗ if Flags.isSet(Flags.DUMP_EQ_UNIT_STRUCT) then
408 ✗ ExpressionDump.dumpExp(temp);
409 end if;
410
411 ✗ (_, (htCr2U, htS2U, htU2S), inconsistentUnits) :=
412 insertUnitInEquation(temp, (htCr2U, htS2U, htU2S), Unit.MASTER({}), args);
413 then
414 inconsistentUnits;
415
416 case DAE.EQUATION(exp = DAE.Exp.TUPLE(PR = expl),
417 scalar = DAE.CALL(path = Absyn.FULLYQUALIFIED(path)))
418 algorithm
419 ✗ s1 := AbsynUtil.pathString(path);
420 ✗ s1 := System.trim(s1,".");
421 ✗ (_, outvars, _, outunitlist) := getNamedUnitlist(s1, args);
422 ✗ (htCr2U, htS2U, htU2S, expList2) :=
423 foldCallArg1(expl, htCr2U, htS2U, htU2S, Unit.MASTER({}), outunitlist, outvars, s1);
424 ✗ (_, (htCr2U, htS2U, htU2S), expList3) :=
425 insertUnitInEquation(eq.scalar, (htCr2U, htS2U, htU2S), Unit.MASTER({}), args);
426 ✗ then
427 List.append_reverse(expList2, expList3);
428
429 case DAE.EQUATION(exp = lhs,
430 scalar = DAE.CALL(path = Absyn.FULLYQUALIFIED(path)))
431 algorithm
432 1 s1 := AbsynUtil.pathString(path);
433 1 s1 := System.trim(s1,".");
434 1 (_, outvars, _, outunitlist) := getNamedUnitlist(s1,args);
435 1 (ut1, (htCr2U, htS2U, htU2S), _) :=
436 insertUnitInEquation(lhs, (htCr2U, htS2U, htU2S), Unit.MASTER({}), args);
437 1 formalargs := listHead(outunitlist);
438 1 formalvar := listHead(outvars);
439
440
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 ut2 := if formalargs == "NONE" then Unit.MASTER({}) else Unit.parseUnitString(formalargs);
441
442 1 b := UnitTypesEqual(ut1, ut2, htCr2U);
443
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if b then
444 1 expList2 := {};
445 else
446 ✗ temp := makenewcref(lhs, formalvar, s1);
447 ✗ expList2 := {(lhs, ut1), (temp, ut2)} :: {};
448 end if;
449 // rhs
450 1 (_, (htCr2U, htS2U, htU2S), expList3) :=
451 insertUnitInEquation(eq.scalar, (htCr2U, htS2U, htU2S), Unit.MASTER({}), args);
452 1 then
453 List.append_reverse(expList2, expList3);
454
455 case DAE.EQUATION()
456 algorithm
457 41 temp := DAE.BINARY(eq.scalar, DAE.SUB(DAE.T_REAL_DEFAULT), eq.exp);
458
459
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
41 if Flags.isSet(Flags.DUMP_EQ_UNIT_STRUCT) then
460 ✗ ExpressionDump.dumpExp(temp);
461 end if;
462
463 41 (_, (htCr2U, htS2U, htU2S), inconsistentUnits) :=
464 insertUnitInEquation(temp, (htCr2U, htS2U, htU2S), Unit.MASTER({}), args);
465 then
466 inconsistentUnits;
467
468 case DAE.EQUEQUATION() then {};
469
470 case DAE.INITIALEQUATION()
471 algorithm
472 ✗ temp := DAE.BINARY(eq.exp2, DAE.SUB(DAE.T_REAL_DEFAULT), eq.exp1);
473
474 ✗ if Flags.isSet(Flags.DUMP_EQ_UNIT_STRUCT) then
475 ✗ ExpressionDump.dumpExp(temp);
476 end if;
477
478 ✗ (_, (htCr2U, htS2U, htU2S), inconsistentUnits) :=
479 insertUnitInEquation(temp, (htCr2U, htS2U, htU2S), Unit.MASTER({}), args);
480 then
481 inconsistentUnits;
482
483 case DAE.ARRAY_EQUATION()
484 algorithm
485 ✗ temp := DAE.BINARY(eq.array, DAE.SUB(DAE.T_REAL_DEFAULT), eq.exp);
486
487 ✗ if Flags.isSet(Flags.DUMP_EQ_UNIT_STRUCT) then
488 ✗ ExpressionDump.dumpExp(temp);
489 end if;
490
491 ✗ (_, (htCr2U, htS2U, htU2S), inconsistentUnits) :=
492 insertUnitInEquation(temp, (htCr2U, htS2U, htU2S), Unit.MASTER({}), args);
493 then
494 inconsistentUnits;
495
496 case DAE.INITIAL_ARRAY_EQUATION()
497 algorithm
498 ✗ temp := DAE.BINARY(eq.array, DAE.SUB(DAE.T_REAL_DEFAULT), eq.exp);
499
500 ✗ if Flags.isSet(Flags.DUMP_EQ_UNIT_STRUCT) then
501 ✗ ExpressionDump.dumpExp(temp);
502 end if;
503
504 ✗ (_, (htCr2U, htS2U, htU2S), inconsistentUnits) :=
505 insertUnitInEquation(temp, (htCr2U, htS2U, htU2S), Unit.MASTER({}), args);
506 then
507 inconsistentUnits;
508
509 case DAE.COMPLEX_EQUATION()
510 algorithm
511 ✗ temp := DAE.BINARY(eq.rhs, DAE.SUB(DAE.T_REAL_DEFAULT), eq.lhs);
512
513 ✗ if Flags.isSet(Flags.DUMP_EQ_UNIT_STRUCT) then
514 ✗ ExpressionDump.dumpExp(temp);
515 end if;
516
517 ✗ (_, (htCr2U, htS2U, htU2S), inconsistentUnits) :=
518 insertUnitInEquation(temp, (htCr2U, htS2U, htU2S), Unit.MASTER({}), args);
519 then
520 inconsistentUnits;
521
522 case DAE.INITIAL_COMPLEX_EQUATION()
523 algorithm
524 ✗ temp := DAE.BINARY(eq.rhs, DAE.SUB(DAE.T_REAL_DEFAULT), eq.lhs);
525
526 ✗ if Flags.isSet(Flags.DUMP_EQ_UNIT_STRUCT) then
527 ✗ ExpressionDump.dumpExp(temp);
528 end if;
529
530 ✗ (_, (htCr2U, htS2U, htU2S), inconsistentUnits) :=
531 insertUnitInEquation(temp, (htCr2U, htS2U, htU2S), Unit.MASTER({}), args);
532 then
533 inconsistentUnits;
534
535 case DAE.WHEN_EQUATION()
536 algorithm
537 ✗ for e in eq.equations loop
538 ✗ (htCr2U, htS2U, htU2S, inconsistentUnits) := foldEquation2(e, htCr2U, htS2U, htU2S, args);
539 end for;
540 ✗ then
541 inconsistentUnits;
542
543 case DAE.IF_EQUATION() then {};
544 case DAE.INITIAL_IF_EQUATION() then {};
545
546 case DAE.NORETCALL()
547 algorithm
548 ✗ (_, (htCr2U, htS2U, htU2S), inconsistentUnits) :=
549 insertUnitInEquation(eq.exp, (htCr2U, htS2U, htU2S), Unit.MASTER({}), args);
550 then
551 inconsistentUnits;
552
553 case DAE.INITIAL_NORETCALL()
554 algorithm
555 ✗ (_, (htCr2U, htS2U, htU2S), inconsistentUnits) :=
556 insertUnitInEquation(eq.exp, (htCr2U, htS2U, htU2S), Unit.MASTER({}), args);
557 then
558 inconsistentUnits;
559
560 case DAE.INITIAL_ASSERT() then {};
561 case DAE.ASSERT() then {};
562 case DAE.TERMINATE() then {};
563 case DAE.INITIAL_TERMINATE() then {};
564 case DAE.REINIT() then {};
565 case DAE.ALGORITHM() then {};
566 case DAE.INITIALALGORITHM() then {};
567
568 else
569 algorithm
570 ✗ Error.addInternalError(getInstanceName() + " failed on: " +
571 DAEDump.dumpEquationStr(eq), sourceInfo());
572 ✗ then
573 fail();
574 end match;
575 end foldEquation2;
576
577 protected function makenewcref
578 input DAE.Exp inexp;
579 input String instring;
580 input String instring1;
581 output DAE.Exp outexp;
582 algorithm
583 outexp:=match(inexp,instring,instring1)
584 local
585 DAE.ComponentRef cr;
586 String name,s1,s2;
587 case (DAE.CREF(componentRef=DAE.CREF_IDENT(ident=name )),s1,s2)
588 algorithm
589 ✗ name:=s2+"()"+"."+s1;
590 ✗ cr:=ComponentReference.makeUntypedCrefIdent(name);
591 ✗ inexp.componentRef:=cr;
592 outexp:=inexp;
593 then
594 outexp;
595 end match;
596 end makenewcref;
597
598 protected function insertUnitInEquation "inserts the units in Equation and check if the equation is consistent or not"
599 input DAE.Exp inEq;
600 input tuple<HashTableCrToUnit.HashTable /* inHtCr2U */, HashTableStringToUnit.HashTable /* inHtS2U */, HashTableUnitToString.HashTable /* inHtU2S */> inTpl;
601 input Unit.Unit inUt;
602 input list<Functionargs> inargs;
603 output Unit.Unit outUt;
604 output tuple<HashTableCrToUnit.HashTable /* outHtCr2U */, HashTableStringToUnit.HashTable /* outHtS2U */, HashTableUnitToString.HashTable /* outHtU2S */> outTpl;
605 output list<list<tuple<DAE.Exp, Unit.Unit>>> outexpList;
606 algorithm
607 (outUt, outTpl, outexpList) := matchcontinue(inEq, inTpl, inUt)
608 local
609 DAE.ComponentRef cr;
610 DAE.Exp exp1, exp2, exp3;
611 DAE.Type ty;
612 HashTableCrToUnit.HashTable HtCr2U;
613 HashTableStringToUnit.HashTable HtS2U;
614 HashTableUnitToString.HashTable HtU2S;
615 Integer i, i1, i2, i3, i4, i5, i6, i7;
616 list<DAE.ComponentRef> lcr;
617 list<DAE.Exp> ExpList;
618 list<list<tuple<DAE.Exp, Unit.Unit>>> expListList, expListList2, expListList3;
619 Real factor1;
620 Real r;
621 String s1;
622 Absyn.Path path;
623 Unit.Unit ut, ut2;
624 list<String> invars,inunitlist;
625
626 //SUB equal summands
627 case (DAE.BINARY(exp1, DAE.SUB(), exp2), (HtCr2U, HtS2U, HtU2S), _) algorithm
628
2/2
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 17 times.
48 (ut as Unit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
629 17 (ut2, (HtCr2U, HtS2U, HtU2S), expListList2) := insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), ut, inargs);
630
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 13 times.
17 (true, ut, HtCr2U) := UnitTypesEqual(ut, ut2, HtCr2U);
631 13 expListList:=List.append_reverse(expListList, expListList2);
632 13 then
633 (ut, (HtCr2U, HtS2U, HtU2S), expListList);
634
635 //SUB equal summands
636 case (DAE.BINARY(exp1, DAE.SUB(), exp2), (HtCr2U, HtS2U, HtU2S), _) algorithm
637 31 (ut2, (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
638 31 (ut, (HtCr2U, HtS2U, HtU2S), expListList2) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), ut2, inargs);
639
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 27 times.
31 (true, ut, HtCr2U) := UnitTypesEqual(ut, ut2, HtCr2U);
640 27 expListList:=List.append_reverse(expListList, expListList2);
641 27 then
642 (ut, (HtCr2U, HtS2U, HtU2S), expListList);
643
644 //SUB unequal summands
645 case (DAE.BINARY(exp1, DAE.SUB(), exp2), (HtCr2U, HtS2U, HtU2S), _) algorithm
646
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 (ut as Unit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
647 4 (ut2, (HtCr2U, HtS2U, HtU2S), expListList2) := insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), ut, inargs);
648
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 (false, _, _) := UnitTypesEqual(ut, ut2, HtCr2U);
649 4 expListList := List.append_reverse(expListList, expListList2);
650 8 expListList := {(exp1, ut), (exp2, ut2)}::expListList;
651 4 then
652 (Unit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
653
654 //SUB unequal summands
655 case (DAE.BINARY(exp1, DAE.SUB(), exp2), (HtCr2U, HtS2U, HtU2S), _) algorithm
656 ✗ (ut2, (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
657 ✗ (ut, (HtCr2U, HtS2U, HtU2S), expListList2) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), ut2, inargs);
658 ✗ (false, _, _) := UnitTypesEqual(ut, ut2, HtCr2U);
659 ✗ expListList := List.append_reverse(expListList, expListList2);
660 ✗ expListList := {(exp1, ut), (exp2, ut2)}::expListList;
661 ✗ then
662 (Unit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
663
664 //ADD equal summands
665 case (DAE.BINARY(exp1, DAE.ADD(), exp2), (HtCr2U, HtS2U, HtU2S), _) algorithm
666
2/2
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 8 times.
15 (ut as Unit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
667 8 (ut2, (HtCr2U, HtS2U, HtU2S), expListList2) := insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), ut, inargs);
668
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
8 (true, ut, HtCr2U) := UnitTypesEqual(ut, ut2, HtCr2U);
669 4 expListList := List.append_reverse(expListList, expListList2);
670 4 then
671 (ut, (HtCr2U, HtS2U, HtU2S), expListList);
672
673
674 //ADD equal summands
675 case (DAE.BINARY(exp1, DAE.ADD(), exp2), (HtCr2U, HtS2U, HtU2S), _) algorithm
676 7 (ut2, (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
677 7 (ut, (HtCr2U, HtS2U, HtU2S), expListList2) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), ut2, inargs);
678
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 3 times.
7 (true, ut, HtCr2U) := UnitTypesEqual(ut, ut2, HtCr2U);
679 3 expListList := List.append_reverse(expListList, expListList2);
680 3 then
681 (ut, (HtCr2U, HtS2U, HtU2S), expListList);
682
683 //ADD unequal summands
684 case (DAE.BINARY(exp1, DAE.ADD(), exp2), (HtCr2U, HtS2U, HtU2S), _) algorithm
685
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 (ut as Unit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
686 4 (ut2, (HtCr2U, HtS2U, HtU2S), expListList2) := insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), ut, inargs);
687
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 (false, _, _) := UnitTypesEqual(ut, ut2, HtCr2U);
688 4 expListList := List.append_reverse(expListList, expListList2);
689 8 expListList := {(exp1, ut), (exp2, ut2)}::expListList;
690 4 then
691 (Unit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
692
693 //ADD unequal
694 case (DAE.BINARY(exp1, DAE.ADD(), exp2), (HtCr2U, HtS2U, HtU2S), _) algorithm
695 ✗ (ut2, (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
696 ✗ (ut, (HtCr2U, HtS2U, HtU2S), expListList2) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), ut2, inargs);
697 ✗ (false, _, _) := UnitTypesEqual(ut, ut2, HtCr2U);
698 ✗ expListList := List.append_reverse(expListList, expListList2);
699 ✗ expListList := {(exp1, ut), (exp2, ut2)}::expListList;
700 ✗ then
701 (Unit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
702
703 //MUL
704 case (DAE.BINARY(exp1, DAE.MUL(), exp2), (HtCr2U, HtS2U, HtU2S), _) algorithm
705
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
5 (ut as Unit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
706
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
5 (ut2 as Unit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList2) := insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
707 //s1="(" + Unit.unitString(ut, HtU2S) + ").(" + Unit.unitString(ut2, HtU2S) + ")";
708 5 ut := Unit.unitMul(ut, ut2);
709 5 s1 := Unit.unitString(ut, HtU2S);
710 5 expListList := List.append_reverse(expListList, expListList2);
711 5 HtS2U := addUnit2HtS2U((s1, ut), HtS2U);
712 5 HtU2S := addUnit2HtU2S((s1, ut), HtU2S);
713 5 then
714 (ut, (HtCr2U, HtS2U, HtU2S), expListList);
715
716 case (DAE.BINARY(exp1, DAE.MUL(), exp2), (HtCr2U, HtS2U, HtU2S), Unit.MASTER()) algorithm
717 ✗ (Unit.MASTER(), (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
718 ✗ (Unit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList2) := insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
719 ✗ expListList := List.append_reverse(expListList, expListList2);
720 ✗ then
721 (Unit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
722
723 case (DAE.BINARY(exp1, DAE.MUL(), exp2), (HtCr2U, HtS2U, HtU2S), Unit.UNIT()) algorithm
724 ✗ (Unit.MASTER(varList=lcr), (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
725 ✗ (ut2 as Unit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList2) := insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
726 //s1="(" + Unit.unitString(inUt, HtU2S) + ")/(" + Unit.unitString(ut2, HtU2S) + ")";
727 ✗ ut := Unit.unitDiv(inUt, ut2);
728 ✗ s1 := Unit.unitString(ut, HtU2S);
729 ✗ HtCr2U := List.fold1(lcr, updateHtCr2U, ut, HtCr2U);
730 ✗ expListList := List.append_reverse(expListList, expListList2);
731 ✗ HtS2U := addUnit2HtS2U((s1, ut), HtS2U);
732 ✗ HtU2S := addUnit2HtU2S((s1, ut), HtU2S);
733 ✗ then
734 (inUt, (HtCr2U, HtS2U, HtU2S), expListList);
735
736 case (DAE.BINARY(exp1, DAE.MUL(), exp2), (HtCr2U, HtS2U, HtU2S), Unit.MASTER()) algorithm
737 ✗ (Unit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
738 ✗ (Unit.MASTER(), (HtCr2U, HtS2U, HtU2S), expListList2) := insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
739 ✗ expListList := List.append_reverse(expListList, expListList2);
740 ✗ then
741 (Unit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
742
743 case (DAE.BINARY(exp1, DAE.MUL(), exp2), (HtCr2U, HtS2U, HtU2S), Unit.UNIT()) algorithm
744 ✗ (ut2 as Unit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
745 ✗ (Unit.MASTER(varList=lcr), (HtCr2U, HtS2U, HtU2S), expListList2) := insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
746 //s1="(" + Unit.unitString(inUt, HtU2S) + ")/(" + Unit.unitString(ut2, HtU2S) + ")";
747 ✗ ut := Unit.unitDiv(inUt, ut2);
748 ✗ s1 := Unit.unitString(ut, HtU2S);
749 ✗ HtCr2U := List.fold1(lcr, updateHtCr2U, ut, HtCr2U);
750 ✗ expListList := List.append_reverse(expListList, expListList2);
751 ✗ HtS2U := addUnit2HtS2U((s1, ut), HtS2U);
752 ✗ HtU2S := addUnit2HtU2S((s1, ut), HtU2S);
753 ✗ then
754 (inUt, (HtCr2U, HtS2U, HtU2S), expListList);
755
756 case (DAE.BINARY(exp1, DAE.MUL(), exp2), (HtCr2U, HtS2U, HtU2S), _) algorithm
757 ✗ (Unit.MASTER(), (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
758 ✗ (Unit.MASTER(), (HtCr2U, HtS2U, HtU2S), expListList2) := insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
759 ✗ expListList := List.append_reverse(expListList, expListList2);
760 ✗ then
761 (Unit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
762
763 //DIV
764 case (DAE.BINARY(exp1, DAE.DIV(), exp2), (HtCr2U, HtS2U, HtU2S), _) algorithm
765
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 (ut as Unit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
766
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 (ut2 as Unit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList2) := insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
767 //s1="(" + Unit.unitString(ut, HtU2S) + ")/(" + Unit.unitString(ut2, HtU2S) + ")";
768 3 ut := Unit.unitDiv(ut, ut2);
769 3 s1 := Unit.unitString(ut, HtU2S);
770 3 expListList := List.append_reverse(expListList, expListList2);
771 3 HtS2U := addUnit2HtS2U((s1, ut), HtS2U);
772 3 HtU2S := addUnit2HtU2S((s1, ut), HtU2S);
773 3 then
774 (ut, (HtCr2U, HtS2U, HtU2S), expListList);
775
776 case (DAE.BINARY(exp1, DAE.DIV(), exp2), (HtCr2U, HtS2U, HtU2S), Unit.MASTER()) algorithm
777 ✗ (Unit.MASTER(), (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
778 ✗ (Unit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList2) := insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
779 ✗ expListList := List.append_reverse(expListList, expListList2);
780 ✗ then
781 (Unit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
782
783 case (DAE.BINARY(exp1, DAE.DIV(), exp2), (HtCr2U, HtS2U, HtU2S), Unit.UNIT()) algorithm
784 ✗ (Unit.MASTER(varList=lcr), (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
785 ✗ (ut2 as Unit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList2) := insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
786 //s1="(" + Unit.unitString(inUt, HtU2S) + ").(" + Unit.unitString(ut2, HtU2S) + ")";
787 ✗ ut := Unit.unitMul(inUt, ut2);
788 ✗ s1 := Unit.unitString(ut, HtU2S);
789 ✗ HtCr2U := List.fold1(lcr, updateHtCr2U, ut, HtCr2U);
790 ✗ expListList := List.append_reverse(expListList, expListList2);
791 ✗ HtS2U := addUnit2HtS2U((s1, ut), HtS2U);
792 ✗ HtU2S := addUnit2HtU2S((s1, ut), HtU2S);
793 ✗ then
794 (inUt, (HtCr2U, HtS2U, HtU2S), expListList);
795
796 case (DAE.BINARY(exp1, DAE.DIV(), exp2), (HtCr2U, HtS2U, HtU2S), Unit.MASTER()) algorithm
797 ✗ (Unit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
798 ✗ (Unit.MASTER(), (HtCr2U, HtS2U, HtU2S), expListList2) := insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
799 ✗ expListList := List.append_reverse(expListList, expListList2);
800 ✗ then
801 (Unit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
802
803 case (DAE.BINARY(exp1, DAE.DIV(), exp2), (HtCr2U, HtS2U, HtU2S), Unit.UNIT()) algorithm
804 ✗ (ut2 as Unit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
805 ✗ (Unit.MASTER(varList=lcr), (HtCr2U, HtS2U, HtU2S), expListList2) := insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
806 //s1="(" + Unit.unitString(ut2, HtU2S) + ")/(" + Unit.unitString(inUt, HtU2S) + ")";
807 ✗ ut := Unit.unitDiv(ut2, inUt);
808 ✗ s1 := Unit.unitString(ut, HtU2S);
809 ✗ HtCr2U := List.fold1(lcr, updateHtCr2U, ut, HtCr2U);
810 ✗ expListList := List.append_reverse(expListList, expListList2);
811 ✗ HtS2U := addUnit2HtS2U((s1, ut), HtS2U);
812 ✗ HtU2S := addUnit2HtU2S((s1, ut), HtU2S);
813 ✗ then
814 (inUt, (HtCr2U, HtS2U, HtU2S), expListList);
815
816 case (DAE.BINARY(exp1, DAE.DIV(), exp2), (HtCr2U, HtS2U, HtU2S), _) algorithm
817 ✗ (Unit.MASTER(), (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
818 ✗ (Unit.MASTER(), (HtCr2U, HtS2U, HtU2S), expListList2) := insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
819 ✗ expListList := List.append_reverse(expListList, expListList2);
820 ✗ then
821 (Unit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
822
823 //POW
824 case (DAE.BINARY(exp1, DAE.POW(), DAE.RCONST(r)), (HtCr2U, HtS2U, HtU2S), _) algorithm
825
1/2
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 (ut as Unit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
826 ✗ i := realInt(r);
827 ✗ true := realEq(r, intReal(i));
828 ✗ ut := Unit.unitPow(ut, i);
829 ✗ s1 := Unit.unitString(ut, HtU2S);
830 ✗ HtS2U := addUnit2HtS2U((s1, ut), HtS2U);
831 ✗ HtU2S := addUnit2HtU2S((s1, ut), HtU2S);
832 ✗ then
833 (ut, (HtCr2U, HtS2U, HtU2S), expListList);
834
835 case (DAE.BINARY(exp1, DAE.POW(), DAE.RCONST(r)), (HtCr2U, HtS2U, HtU2S), ut as Unit.UNIT()) algorithm
836
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
1 (Unit.MASTER(lcr), (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
837
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1 Unit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7) := Unit.unitRoot(ut, r);
838 1 HtCr2U := List.fold1(lcr, updateHtCr2U, Unit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7), HtCr2U);
839 1 s1 := Unit.unitString(Unit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7), HtU2S);
840 1 HtS2U := addUnit2HtS2U((s1, Unit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7)), HtS2U);
841 1 HtU2S := addUnit2HtU2S((s1, Unit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7)), HtU2S);
842 1 then
843 (inUt, (HtCr2U, HtS2U, HtU2S), expListList);
844
845 case (DAE.BINARY(exp1, DAE.POW(), DAE.RCONST(_)), (HtCr2U, HtS2U, HtU2S), _) algorithm
846 2 (_, (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
847 2 then
848 (Unit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
849
850 //PRE
851 case (DAE.CALL(path=Absyn.IDENT(name="pre"), expLst={exp1}), (HtCr2U, HtS2U, HtU2S), _) algorithm
852 ✗ (ut, (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
853 ✗ then (ut, (HtCr2U, HtS2U, HtU2S), expListList);
854
855 //DER
856 case (DAE.CALL(path=Absyn.IDENT(name="der"), expLst={exp1}), (HtCr2U, HtS2U, HtU2S), Unit.UNIT()) algorithm
857
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 (Unit.MASTER(lcr), (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
858 ✗ ut := Unit.unitMul(inUt, Unit.UNIT(1e0, 0, 0, 0, 1, 0, 0, 0));
859 ✗ HtCr2U := List.fold1(lcr, updateHtCr2U, ut, HtCr2U);
860 ✗ s1 := Unit.unitString(ut, HtU2S);
861 ✗ HtS2U := addUnit2HtS2U((s1, ut), HtS2U);
862 ✗ HtU2S := addUnit2HtU2S((s1, ut), HtU2S);
863 ✗ then
864 (inUt, (HtCr2U, HtS2U, HtU2S), expListList);
865
866 case (DAE.CALL(path = Absyn.IDENT(name="der"), expLst={exp1}), (HtCr2U, HtS2U, HtU2S), _) algorithm
867
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 (ut as Unit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList):=insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
868 8 ut:=Unit.unitDiv(ut, Unit.UNIT(1e0, 0, 0, 0, 1, 0, 0, 0));
869 8 s1:=Unit.unitString(ut, HtU2S);
870 8 HtS2U:=addUnit2HtS2U((s1, ut), HtS2U);
871 8 HtU2S:=addUnit2HtU2S((s1, ut), HtU2S);
872 8 then (ut, (HtCr2U, HtS2U, HtU2S), expListList);
873
874 case (DAE.CALL(path = Absyn.IDENT(name="der"), expLst={exp1}), (HtCr2U, HtS2U, HtU2S), Unit.MASTER()) algorithm
875 ✗ (Unit.MASTER(), (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
876 ✗ then (Unit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
877
878 //SQRT
879 case (DAE.CALL(path=Absyn.IDENT(name="sqrt"), expLst={exp1}), (HtCr2U, HtS2U, HtU2S), _) algorithm
880
1/2
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 (ut as Unit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
881 ✗ Unit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7) := Unit.unitRoot(ut, 2.0);
882 ✗ s1 := Unit.unitString(Unit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7), HtU2S);
883 ✗ HtS2U := addUnit2HtS2U((s1, Unit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7)), HtS2U);
884 ✗ HtU2S := addUnit2HtU2S((s1, Unit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7)), HtU2S);
885 ✗ then (Unit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7), (HtCr2U, HtS2U, HtU2S), expListList);
886
887 case (DAE.CALL(path=Absyn.IDENT(name="sqrt"), expLst={exp1}), (HtCr2U, HtS2U, HtU2S), Unit.UNIT()) algorithm
888
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
1 (Unit.MASTER(lcr), (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
889 1 ut := Unit.unitPow(inUt, 2);
890 1 s1 := Unit.unitString(ut, HtU2S);
891 1 HtCr2U := List.fold1(lcr, updateHtCr2U, ut, HtCr2U);
892 1 HtS2U := addUnit2HtS2U((s1, ut), HtS2U);
893 1 HtU2S := addUnit2HtU2S((s1, ut), HtU2S);
894 1 then (inUt, (HtCr2U, HtS2U, HtU2S), expListList);
895
896 case (DAE.CALL(path=Absyn.IDENT(name="sqrt"), expLst={exp1}), (HtCr2U, HtS2U, HtU2S), _) algorithm
897 1 (_, (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
898 1 then (Unit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
899
900 //IFEXP
901 case (DAE.IFEXP(_, exp2, exp3), (HtCr2U, HtS2U, HtU2S), _) algorithm
902 //(_, (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
903 2 (ut, (HtCr2U, HtS2U, HtU2S), expListList2) := insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
904 2 (ut2, (HtCr2U, HtS2U, HtU2S), expListList3) := insertUnitInEquation(exp3, (HtCr2U, HtS2U, HtU2S), ut, inargs);
905
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 (true, ut, HtCr2U) := UnitTypesEqual(ut, ut2, HtCr2U);
906 //expListList = List.append_reverse(expListList, expListList2);
907 //expListList = List.append_reverse(expListList, expListList3);
908 ✗ expListList := List.append_reverse(expListList2, expListList3);
909 ✗ then (ut, (HtCr2U, HtS2U, HtU2S), expListList);
910
911 case (DAE.IFEXP(_, exp2, exp3), (HtCr2U, HtS2U, HtU2S), _) algorithm
912 //(_, (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), Unit.MASTER({}), inargs);
913 2 (ut, (HtCr2U, HtS2U, HtU2S), expListList2) := insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
914 2 (ut2, (HtCr2U, HtS2U, HtU2S), expListList3) := insertUnitInEquation(exp3, (HtCr2U, HtS2U, HtU2S), ut, inargs);
915
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 (false, _, _) := UnitTypesEqual(ut, ut2, HtCr2U);
916 //expListList = List.append_reverse(expListList, expListList2);
917 //expListList = List.append_reverse(expListList, expListList3);
918 2 expListList := List.append_reverse(expListList2, expListList3);
919 4 expListList := {(exp2, ut), (exp3, ut2)}::expListList;
920 2 then (Unit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
921
922 //RELATIONS
923 case (DAE.RELATION(exp1=exp1), (HtCr2U, HtS2U, HtU2S), _) algorithm
924 ✗ (ut, (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
925 ✗ (ut2, (HtCr2U, HtS2U, HtU2S), expListList2) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
926 ✗ (true, ut, HtCr2U) := UnitTypesEqual(ut, ut2, HtCr2U);
927 ✗ expListList := List.append_reverse(expListList, expListList2);
928 ✗ then (ut, (HtCr2U, HtS2U, HtU2S), expListList);
929
930 case (DAE.RELATION(exp1=exp1, exp2=exp2), (HtCr2U, HtS2U, HtU2S), _) algorithm
931 ✗ (ut, (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
932 ✗ (ut2, (HtCr2U, HtS2U, HtU2S), expListList2) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
933 ✗ (false, _, _) := UnitTypesEqual(ut, ut2, HtCr2U);
934 ✗ expListList := List.append_reverse(expListList, expListList2);
935 ✗ expListList := {(exp1, ut), (exp2, ut2)}::expListList;
936 ✗ then (Unit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
937
938 // builtin function calls
939 case (DAE.CALL(path=Absyn.IDENT(), expLst=ExpList), (HtCr2U, HtS2U, HtU2S), _) algorithm
940 4 (HtCr2U, HtS2U, HtU2S, expListList) := foldCallArg(ExpList, HtCr2U, HtS2U, HtU2S);
941 4 then (Unit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
942
943
944 //user defined function CALL
945 case (DAE.CALL(path=Absyn.FULLYQUALIFIED(path), expLst=ExpList), (HtCr2U, HtS2U, HtU2S), _) algorithm
946 1 s1 := AbsynUtil.pathString(path);
947 1 s1 := System.trim(s1,".");
948 1 (invars, _, inunitlist, _) := getNamedUnitlist(s1, inargs);
949 1 (HtCr2U, HtS2U, HtU2S, expListList) := foldCallArg1(ExpList, HtCr2U, HtS2U, HtU2S, inUt, inunitlist, invars, s1);
950 1 then (Unit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
951
952 //UMINUS
953 case (DAE.UNARY(DAE.UMINUS(), exp1), (HtCr2U, HtS2U, HtU2S), _) algorithm
954 6 (ut, (HtCr2U, HtS2U, HtU2S), expListList) := insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
955 6 then (ut, (HtCr2U, HtS2U, HtU2S), expListList);
956
957 //"time"
958 case (DAE.CREF(componentRef=cr), (HtCr2U, HtS2U, HtU2S), _) algorithm
959
2/2
✓ Branch 1 taken 139 times.
✓ Branch 2 taken 3 times.
142 true := ComponentReferenceBasics.crefEqual(cr, DAE.crefTime);
960 ut := Unit.UNIT(1e0, 0, 0, 0, 1, 0, 0, 0);
961 3 HtS2U := addUnit2HtS2U(("time", ut), HtS2U);
962 3 HtU2S := addUnit2HtU2S(("time", ut), HtU2S);
963 3 then (ut, (HtCr2U, HtS2U, HtU2S), {});
964
965 //CREF
966 case (DAE.CREF(componentRef=cr, ty=DAE.T_REAL()), (HtCr2U, _, _), _) algorithm
967 139 ut := BaseHashTable.get(cr, HtCr2U);
968 then (ut, inTpl, {});
969
970 //NO UNIT IN EQUATION
971 // all unhandled expressions, e.g. DAE.CAST, DAE.TUPLE, ...
972 else
973 //Error.addInternalError("./Compiler/FrontEnd/FUnitCheck.mo: function insertUnitInEquation failed for " + ExpressionBasics.printExpStr(inEq), sourceInfo());
974 then (Unit.MASTER({}), inTpl, {});
975 end matchcontinue;
976 end insertUnitInEquation;
977
978 protected function getNamedUnitlist
979 input String instring;
980 input list<Functionargs> inargs;
981 output list<String> outargs;
982 output list<String> outargs2;
983 output list<String> outargs3;
984 output list<String> outargs4;
985 algorithm
986 (outargs,outargs2,outargs3,outargs4):=match(instring, inargs)
987 local
988 list<Functionargs> rest;
989 String fnname,fnname1;
990 list<String> invars,inunitlist, outunitlist,outvars;
991 case(fnname,FUNCTIONUNITS(fnname1,invars,outvars,inunitlist,outunitlist)::_)
992 guard stringEq(fnname,fnname1)
993 algorithm
994 inunitlist:=inunitlist;
995 outunitlist:=outunitlist;
996 then
997 (invars,outvars,inunitlist,outunitlist);
998 case(fnname,_::rest)
999 algorithm
1000 6 (invars,outvars,inunitlist,outunitlist):=getNamedUnitlist(fnname,rest);
1001 then
1002 (invars,outvars,inunitlist,outunitlist);
1003 case(_,_) then ({},{},{},{});
1004 end match;
1005 end getNamedUnitlist;
1006
1007
1008 protected function UnitTypesEqual "checks equality of two UnitExp's"
1009 input Unit.Unit inut;
1010 input Unit.Unit inut2;
1011 input HashTableCrToUnit.HashTable inHtCr2U;
1012 output Boolean b;
1013 output Unit.Unit outUt;
1014 output HashTableCrToUnit.HashTable outHtCr2U;
1015 algorithm
1016 (b, outUt, outHtCr2U) := matchcontinue(inut, inut2)
1017 local
1018 String s, s2;
1019 Integer i1, i2, i3, i4, i5, i6, i7;
1020 Integer j1, j2, j3, j4, j5, j6, j7;
1021 list<DAE.ComponentRef> lcr, lcr2;
1022 HashTableCrToUnit.HashTable HtCr2U;
1023 Real factor1, factor2, r;
1024 Unit.Unit ut;
1025
1026 case (Unit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7), Unit.UNIT(factor2, j1, j2, j3, j4, j5, j6, j7)) algorithm
1027
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
41 true := realEq(factor1,factor2);
1028
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
41 true := intEq(i1, j1);
1029
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
41 true := intEq(i2, j2);
1030
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 32 times.
41 true := intEq(i3, j3);
1031
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 13 times.
32 true := intEq(i4, j4);
1032
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 true := intEq(i5, j5);
1033
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 true := intEq(i6, j6);
1034
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 true := intEq(i7, j7);
1035 13 then (true, Unit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7), inHtCr2U);
1036
1037 case (Unit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7), Unit.UNIT(factor2, j1, j2, j3, j4, j5, j6, j7)) algorithm
1038 28 r:=realMax(realAbs(factor1), realAbs(factor2));
1039
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 28 times.
28 true := realLe(realDiv(realAbs(realSub(factor1,factor2)),r),1e-3);
1040
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 true := intEq(i1, j1);
1041
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 true := intEq(i2, j2);
1042
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 19 times.
28 true := intEq(i3, j3);
1043
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 true := intEq(i4, j4);
1044 ✗ true := intEq(i5, j5);
1045 ✗ true := intEq(i6, j6);
1046 ✗ true := intEq(i7, j7);
1047 ✗ then (true, Unit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7), inHtCr2U);
1048
1049 case (ut as Unit.UNIT(), Unit.MASTER(lcr)) algorithm
1050 6 HtCr2U := List.fold1(lcr, updateHtCr2U, ut, inHtCr2U);
1051 then (true, ut , HtCr2U);
1052
1053 case (Unit.MASTER(lcr), ut as Unit.UNIT()) algorithm
1054 22 HtCr2U := List.fold1(lcr, updateHtCr2U, ut, inHtCr2U);
1055 then (true, ut, HtCr2U);
1056
1057 case (Unit.MASTER(lcr), Unit.MASTER(lcr2)) algorithm
1058 8 lcr2 := List.append_reverse(lcr, lcr2);
1059 8 then (true, Unit.MASTER(lcr2), inHtCr2U);
1060
1061 case (Unit.UNKNOWN(s), Unit.UNKNOWN(s2)) algorithm
1062 ✗ true := stringEqual(s, s2);
1063 ✗ then (true, Unit.UNKNOWN(s), inHtCr2U);
1064
1065 ✗ case (Unit.UNKNOWN(s), _) then (true, Unit.UNKNOWN(s), inHtCr2U);
1066 ✗ case (_, Unit.UNKNOWN(s)) then (true, Unit.UNKNOWN(s), inHtCr2U);
1067
1068 else (false, inut, inHtCr2U);
1069 end matchcontinue;
1070 end UnitTypesEqual;
1071
1072
1073
1074 protected function updateHtCr2U
1075 input DAE.ComponentRef inCr;
1076 input Unit.Unit inUt;
1077 input HashTableCrToUnit.HashTable inHtCr2U;
1078 output HashTableCrToUnit.HashTable outHtCr2U;
1079 algorithm
1080 outHtCr2U:=matchcontinue inHtCr2U
1081 local
1082 HashTableCrToUnit.HashTable HtCr2U;
1083
1084 case _
1085 algorithm
1086
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2 times.
10 true := BaseHashTable.hasKey(FUnit.UPDATECREF, inHtCr2U);
1087 2 BaseHashTable.update((inCr,inUt),inHtCr2U);
1088 then inHtCr2U;
1089
1090 else
1091 algorithm
1092 8 HtCr2U := BaseHashTable.add((FUnit.UPDATECREF, Unit.MASTER({})),inHtCr2U);
1093 8 BaseHashTable.update((inCr,inUt),HtCr2U);
1094 then HtCr2U;
1095
1096 end matchcontinue;
1097 end updateHtCr2U;
1098
1099
1100
1101 protected function Errorfunction "returns the incostinent Equation with sub-expression"
1102 input list<tuple<DAE.Exp, Unit.Unit>> inexpList;
1103 input DAE.Element inEq;
1104 input HashTableUnitToString.HashTable inHtU2S;
1105 algorithm
1106 () := match inexpList
1107 local
1108 String s, s1, s2;
1109 list<tuple<DAE.Exp, Unit.Unit>> expList;
1110 SourceInfo info;
1111 case expList
1112 algorithm
1113 6 info:=getSourceInfo(inEq);
1114 6 s := DAEDump.dumpEquationStr(inEq);
1115 6 s1 := Errorfunction2(expList, inHtU2S);
1116 6 s2:="The following equation is INCONSISTENT due to specified unit information:" + s +"\n";
1117 6 Error.addSourceMessage(Error.COMPILER_WARNING,{s2},info);
1118 6 Error.addCompilerWarning("The units of following sub-expressions need to be equal:\n" + s1);
1119
1120 /*
1121 Error.addCompilerWarning("The following NEWFRONTEND UNIT CHECK equation is INCONSISTENT due to specified unit information: " + s + "\n" +
1122 "The units of following sub-expressions need to be equal:\n" + s1 );*/
1123 then ();
1124 end match;
1125 end Errorfunction;
1126
1127 protected function getSourceInfo
1128 input DAE.Element inequation;
1129 output SourceInfo outinfo;
1130 algorithm
1131 outinfo:=match inequation
1132 local
1133 SourceInfo info;
1134 case DAE.EQUATION(source=DAE.SOURCE(info=info)) then info;
1135 end match;
1136 end getSourceInfo;
1137
1138 protected function Errorfunction2 "help-function"
1139 input list<tuple<DAE.Exp, Unit.Unit>> inexpList;
1140 input HashTableUnitToString.HashTable inHtU2S;
1141 output String outS;
1142 algorithm
1143 outS := match inexpList
1144 local
1145 list<tuple<DAE.Exp, Unit.Unit>> expList;
1146 DAE.Exp exp;
1147 Unit.Unit ut;
1148 String s, s1, s2;
1149
1150 case (exp, ut)::{} algorithm
1151 6 s := ExpressionBasics.printExpStr(exp);
1152 6 s1 := Unit.unitString(ut, inHtU2S);
1153 6 s := "- sub-expression \"" + s + "\" has unit \"" + s1 + "\"";
1154 then s;
1155
1156 case (exp, ut)::expList algorithm
1157 6 s := ExpressionBasics.printExpStr(exp);
1158 6 s1 := Unit.unitString(ut, inHtU2S);
1159 6 s2 := Errorfunction2(expList, inHtU2S);
1160 6 s := "- sub-expression \"" + s + "\" has unit \"" + s1 + "\"\n" + s2;
1161 then s;
1162 end match;
1163 end Errorfunction2;
1164
1165
1166 public function GetVarList
1167 input DAE.DAElist indaelist;
1168 output list<DAE.Element> outstring;
1169 protected
1170 list<DAE.Element> varlist;
1171 algorithm
1172 outstring:=match indaelist
1173 case DAE.DAE({DAE.COMP(dAElist =varlist)}) then varlist;
1174 case _ then {};
1175 end match;
1176 end GetVarList;
1177
1178 public function GetElementList
1179 input DAE.DAElist eqlist;
1180 output list<DAE.Element> outstring;
1181 protected
1182 list<DAE.Element> eq1;
1183 algorithm
1184 outstring:=match eqlist
1185 case DAE.DAE(eq1) then eq1;
1186 end match;
1187 end GetElementList;
1188
1189
1190 protected function foldCallArg "help-function for CALL case in function insertUnitInEquation"
1191 input list<DAE.Exp> inExpList;
1192 input HashTableCrToUnit.HashTable inHtCr2U;
1193 input HashTableStringToUnit.HashTable inHtS2U;
1194 input HashTableUnitToString.HashTable inHtU2S;
1195 output HashTableCrToUnit.HashTable outHtCr2U = inHtCr2U;
1196 output HashTableStringToUnit.HashTable outHtS2U = inHtS2U;
1197 output HashTableUnitToString.HashTable outHtU2S = inHtU2S;
1198 output list<list<tuple<DAE.Exp, Unit.Unit>>> outExpListList = {};
1199 protected
1200 list<list<tuple<DAE.Exp, Unit.Unit>>> expListList;
1201 algorithm
1202
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 for exp in inExpList loop
1203 4 (_, (outHtCr2U, outHtS2U, outHtU2S), expListList) :=
1204 insertUnitInEquation(exp, (outHtCr2U, outHtS2U, outHtU2S), Unit.MASTER({}),{});
1205 4 outExpListList := List.append_reverse(expListList, outExpListList);
1206 end for;
1207
1208 4 outExpListList := listReverse(outExpListList);
1209 end foldCallArg;
1210
1211
1212 protected function foldCallArg1 "help-function for CALL case in userdefined top level function insertUnitInEquation"
1213 input list<DAE.Exp> inExpList;
1214 input HashTableCrToUnit.HashTable inHtCr2U;
1215 input HashTableStringToUnit.HashTable inHtS2U;
1216 input HashTableUnitToString.HashTable inHtU2S;
1217 input Unit.Unit inunit;
1218 input list<String> unitlist;
1219 input list<String> invars;
1220 input String fname;
1221 output HashTableCrToUnit.HashTable outHtCr2U = inHtCr2U;
1222 output HashTableStringToUnit.HashTable outHtS2U = inHtS2U;
1223 output HashTableUnitToString.HashTable outHtU2S = inHtU2S;
1224 output list<list<tuple<DAE.Exp, Unit.Unit>>> outExpListList = {};
1225 protected
1226 list<list<tuple<DAE.Exp, Unit.Unit>>> expListList;
1227 Unit.Unit ut,ut1;
1228 String s,formalarg,formalvar;
1229 DAE.Exp exp,temp;
1230 Boolean b;
1231 algorithm
1232
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
2 for i in 1:listLength(inExpList) loop
1233 1 exp:=listGet(inExpList,i);
1234 1 formalarg:=listGet(unitlist,i);
1235 1 formalvar:=listGet(invars,i);
1236 1 (ut, (outHtCr2U, outHtS2U, outHtU2S), expListList) :=
1237 insertUnitInEquation(exp, (outHtCr2U, outHtS2U, outHtU2S), inunit,{});
1238
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 if (formalarg=="NONE") then
1239 ut1:=Unit.MASTER({});
1240 else
1241 1 ut1:=Unit.parseUnitString(formalarg);
1242 end if;
1243 1 s:=Unit.unitString(ut,outHtU2S);
1244
1245 1 (b, ut,_) := UnitTypesEqual(ut, ut1, outHtCr2U);
1246 //if(stringEq(s,formalargs)==true) then
1247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if(b==true) then
1248 expListList:={};
1249 else
1250 ✗ temp :=makenewcref(exp,formalvar,fname);
1251 ✗ expListList := {(exp, ut),(temp, ut1)}::{};
1252 end if;
1253 1 outExpListList := List.append_reverse(expListList, outExpListList);
1254 end for;
1255 end foldCallArg1;
1256
1257
1258 protected function addUnit2HtS2U
1259 input tuple<String, Unit.Unit> inTpl;
1260 input HashTableStringToUnit.HashTable inHtS2U;
1261 output HashTableStringToUnit.HashTable outHtS2U;
1262 algorithm
1263 46 outHtS2U := BaseHashTable.add(inTpl,inHtS2U);
1264 end addUnit2HtS2U;
1265
1266 protected function addUnit2HtU2S
1267 input tuple<String, Unit.Unit> inTpl;
1268 input HashTableUnitToString.HashTable inHtU2S;
1269 output HashTableUnitToString.HashTable outHtU2S;
1270 algorithm
1271 outHtU2S := matchcontinue inTpl
1272 local
1273 String s;
1274 Unit.Unit ut;
1275 HashTableUnitToString.HashTable HtU2S;
1276
1277 case (s, ut)
1278 algorithm
1279
2/2
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 31 times.
46 false := BaseHashTable.hasKey(ut, inHtU2S);
1280 31 HtU2S := BaseHashTable.add((ut,s),inHtU2S);
1281 then HtU2S;
1282
1283 else inHtU2S;
1284 end matchcontinue;
1285 end addUnit2HtU2S;
1286
1287 // get unit information based on old instantiation
1288 protected function convertUnitString2unit_old "converts String to unit"
1289 input DAE.Element var;
1290 input tuple<HashTableCrToUnit.HashTable /* inHtCr2U */, HashTableStringToUnit.HashTable /* HtS2U */, HashTableUnitToString.HashTable /* HtU2S */> inTpl;
1291 output tuple<HashTableCrToUnit.HashTable /* outHtCr2U */, HashTableStringToUnit.HashTable /* HtS2U */, HashTableUnitToString.HashTable /* HtU2S */> outTpl;
1292 algorithm
1293 outTpl := match(var, inTpl)
1294 local
1295 String unitString;
1296 DAE.ComponentRef cr;
1297 Unit.Unit ut;
1298 HashTableStringToUnit.HashTable HtS2U;
1299 HashTableUnitToString.HashTable HtU2S;
1300 HashTableCrToUnit.HashTable HtCr2U;
1301
1302 case(DAE.VAR(componentRef=cr,ty=DAE.T_REAL(),variableAttributesOption=SOME(DAE.VAR_ATTR_REAL(unit=SOME(DAE.SCONST(unitString))))),(HtCr2U, HtS2U, HtU2S))
1303 guard(unitString <> "")
1304 algorithm
1305 31 (ut, HtS2U, HtU2S) := parse(unitString, cr, HtS2U, HtU2S);
1306 31 HtCr2U := BaseHashTable.add((cr,ut),HtCr2U);
1307 31 then
1308 ((HtCr2U, HtS2U, HtU2S));
1309
1310 // no units
1311 case(DAE.VAR(componentRef=cr),(HtCr2U, HtS2U, HtU2S))
1312 algorithm
1313 //print ("\n inside Nounits_old ");
1314 12 HtCr2U := BaseHashTable.add((cr,Unit.MASTER({cr})),HtCr2U);
1315 12 HtS2U := addUnit2HtS2U(("-",Unit.MASTER({cr})),HtS2U);
1316 12 HtU2S := addUnit2HtU2S(("-",Unit.MASTER({cr})),HtU2S);
1317 12 then
1318 ((HtCr2U, HtS2U, HtU2S));
1319
1320 else inTpl; //skip Non-Real Variables
1321
1322 end match;
1323 end convertUnitString2unit_old;
1324
1325
1326
1327 //based on new Instantiation currently not fully operational
1328 protected function convertUnitString2unit "converts String to unit"
1329 input DAE.Element var;
1330 input tuple<HashTableCrToUnit.HashTable /* inHtCr2U */, HashTableStringToUnit.HashTable /* HtS2U */, HashTableUnitToString.HashTable /* HtU2S */> inTpl;
1331 output tuple<HashTableCrToUnit.HashTable /* outHtCr2U */, HashTableStringToUnit.HashTable /* HtS2U */, HashTableUnitToString.HashTable /* HtU2S */> outTpl;
1332 algorithm
1333 outTpl := match(var, inTpl)
1334 local
1335 String unitString;
1336 DAE.ComponentRef cr;
1337 Unit.Unit ut;
1338 list<DAE.Var> varlst;
1339 HashTableStringToUnit.HashTable HtS2U;
1340 HashTableUnitToString.HashTable HtU2S;
1341 HashTableCrToUnit.HashTable HtCr2U;
1342
1343 case(DAE.VAR(componentRef=cr,ty=DAE.T_REAL(varLst=varlst)),(HtCr2U, HtS2U, HtU2S))
1344 guard (false==listEmpty(varlst))
1345 algorithm
1346 ✗ unitString:=parseVarList(varlst);
1347 ✗ (ut, HtS2U, HtU2S) := parse(unitString, cr, HtS2U, HtU2S);
1348 ✗ HtCr2U := BaseHashTable.add((cr,ut),HtCr2U);
1349 ✗ then
1350 ((HtCr2U, HtS2U, HtU2S));
1351
1352 // no units
1353 case(DAE.VAR(componentRef=cr),(HtCr2U, HtS2U, HtU2S))
1354 algorithm
1355 ✗ HtCr2U := BaseHashTable.add((cr,Unit.MASTER({cr})),HtCr2U);
1356 ✗ HtS2U := addUnit2HtS2U(("-",Unit.MASTER({cr})),HtS2U);
1357 ✗ HtU2S := addUnit2HtU2S(("-",Unit.MASTER({cr})),HtU2S);
1358 ✗ then
1359 ((HtCr2U, HtS2U, HtU2S));
1360 end match;
1361 end convertUnitString2unit;
1362
1363 function parseVarList
1364 input list<DAE.Var> invarlist;
1365 output String outstring;
1366 algorithm
1367 outstring:=match invarlist
1368 local
1369 list<DAE.Var> varlist;
1370 DAE.Binding eqbind;
1371 String s,name;
1372 case DAE.Var.TYPES_VAR(name=name,binding=eqbind)::_
1373 guard stringEq(name,"unit")
1374 algorithm
1375 ✗ s:=getStringFromExp(eqbind);
1376 then
1377 s;
1378 case _::varlist
1379 algorithm
1380 ✗ s:=parseVarList(varlist);
1381 then
1382 s;
1383 case {} then "None";
1384 end match;
1385 end parseVarList;
1386
1387 public function getStringFromExp
1388 input DAE.Binding binding;
1389 output String str;
1390 algorithm
1391 str := match binding
1392 local
1393 String str1;
1394 case DAE.UNBOUND() then "";
1395 case DAE.EQBOUND(exp=DAE.SCONST(str1)) then str1;
1396 case _ then "None";
1397 end match;
1398 end getStringFromExp;
1399
1400 protected function parse "author: lochel"
1401 input String inUnitString;
1402 input DAE.ComponentRef inCref;
1403 input HashTableStringToUnit.HashTable inHtS2U;
1404 input HashTableUnitToString.HashTable inHtU2S;
1405 output Unit.Unit outUnit;
1406 output HashTableStringToUnit.HashTable outHtS2U = inHtS2U;
1407 output HashTableUnitToString.HashTable outHtU2S = inHtU2S;
1408 algorithm
1409
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
31 if inUnitString == "" then
1410 ✗ outUnit := Unit.MASTER({inCref});
1411 ✗ return;
1412 end if;
1413 try
1414 31 outUnit := BaseHashTable.get(inUnitString, inHtS2U);
1415 else
1416 try
1417 13 outUnit := Unit.parseUnitString(inUnitString, inHtS2U);
1418 else
1419 ✗ outUnit := Unit.UNKNOWN(inUnitString);
1420 end try;
1421 13 outHtS2U := addUnit2HtS2U((inUnitString, outUnit), outHtS2U);
1422 13 outHtU2S := addUnit2HtU2S((inUnitString, outUnit), outHtU2S);
1423 end try;
1424 end parse;
1425
1426 annotation(__OpenModelica_Interface="frontend");
1427 end FUnitCheck;
1428