Linux GNU 11.4.0 Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 76.5% 562 / 0 / 735
Functions: -% 0 / 1 / 1
Branches: 60.7% 404 / 0 / 666

OMCompiler/Compiler/BackEnd/BackendVariable.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 BackendVariable
37 "BackendVariables contains the function that deals with the datytypes
38 BackendDAE.VAR BackendDAE.Variables and BackendVariablesArray.
39 "
40
41 import BackendDAE;
42 import DAE;
43 import SCode;
44 import Values;
45
46 protected
47
48 import Absyn;
49 import Array;
50 import BackendDAEUtil;
51 import BaseHashSet;
52 import BaseHashTable;
53 import ComponentReference;
54 import ClassInf;
55 import ComponentReferenceBasics;
56 import CommonSubExpression;
57 import DAEUtil;
58 import Debug;
59 import ElementSource;
60 import Error;
61 import Expression;
62 import ExpressionSimplify;
63 import Flags;
64 import Global;
65 import HashSet;
66 import List;
67 import MetaModelica.Dangerous;
68 import Mutable;
69 import SCodeUtil;
70 import StringUtil;
71 import System;
72 import Types;
73 import TypesDump;
74 import Util;
75 import ExpressionBasics;
76 import Dump;
77
78 /* =======================================================
79 *
80 * Section for functions that deals with Var
81 *
82 * =======================================================
83 */
84
85 public function varEqual "author: PA
86 Returns true if two vars are equal."
87 input BackendDAE.Var inVar1;
88 input BackendDAE.Var inVar2;
89 output Boolean outBoolean = ComponentReferenceBasics.crefEqualNoStringCompare(inVar1.varName, inVar2.varName) "a BackendDAE.Var is identified by its component reference";
90 end varEqual;
91
92 public function setVarFixed "author: PA
93 Sets the fixed attribute of a variable."
94 input BackendDAE.Var inVar;
95 input Boolean inBoolean;
96 output BackendDAE.Var outVar = inVar;
97 protected
98 Option<DAE.VariableAttributes> oattr;
99 algorithm
100
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 370285 times.
✓ Branch 2 taken 105679 times.
✓ Branch 3 taken 264606 times.
370285 oattr := if isSome(inVar.values) then inVar.values else SOME(getVariableAttributefromType(inVar.varType));
101
2/2
✓ Branch 0 taken 223129 times.
✓ Branch 1 taken 147156 times.
963699 outVar.values := DAEUtil.setFixedAttr(oattr, SOME(DAE.BCONST(inBoolean)));
102 end setVarFixed;
103
104 public function removeFixedAttribute
105 input output BackendDAE.Var var;
106 algorithm
107
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 116 times.
✓ Branch 2 taken 116 times.
✗ Branch 3 not taken.
116 if isSome(var.values) then
108 116 var.values := DAEUtil.setFixedAttr(var.values, NONE());
109 end if;
110 end removeFixedAttribute;
111
112 public function removeStartAttribute
113 input output BackendDAE.Var var;
114 algorithm
115
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 116 times.
✓ Branch 2 taken 116 times.
✗ Branch 3 not taken.
116 if isSome(var.values) then
116 116 var.values := DAEUtil.setStartAttrOption(var.values, NONE());
117 end if;
118 end removeStartAttribute;
119
120 public function varFixed "author: PA
121 Extracts the fixed attribute of a variable.
122 The default fixed value is used if not found. Default is true for parameters
123 (and constants) and false for variables."
124 input BackendDAE.Var inVar;
125 output Boolean outBoolean;
126 algorithm
127 outBoolean := match inVar
128 local
129 Boolean fixed;
130
131 case BackendDAE.VAR(values=SOME(DAE.VAR_ATTR_REAL(fixed=SOME(DAE.BCONST(fixed))))) then fixed;
132 case BackendDAE.VAR(values=SOME(DAE.VAR_ATTR_INT(fixed=SOME(DAE.BCONST(fixed))))) then fixed;
133 case BackendDAE.VAR(values=SOME(DAE.VAR_ATTR_BOOL(fixed=SOME(DAE.BCONST(fixed))))) then fixed;
134 case BackendDAE.VAR(values=SOME(DAE.VAR_ATTR_STRING(fixed=SOME(DAE.BCONST(fixed))))) then fixed;
135 case BackendDAE.VAR(values=SOME(DAE.VAR_ATTR_ENUMERATION(fixed=SOME(DAE.BCONST(fixed))))) then fixed;
136
137 // params and consts are by default fixed
138 case BackendDAE.VAR(varKind=BackendDAE.PARAM()) then true;
139 case BackendDAE.VAR(varKind=BackendDAE.CONST(),bindExp=SOME(_)) then true;
140
141 // rest defaults to false
142 else false;
143 end match;
144 end varFixed;
145
146 public function setVarStartValue "author: Frenkel TUD
147 Sets the start value attribute of a variable."
148 input BackendDAE.Var inVar;
149 input DAE.Exp inExp;
150 output BackendDAE.Var outVar = inVar;
151 protected
152 Option<DAE.VariableAttributes> oattr;
153 algorithm
154
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 16457 times.
✓ Branch 2 taken 264 times.
✓ Branch 3 taken 16193 times.
16457 oattr := if isSome(inVar.values) then inVar.values else SOME(getVariableAttributefromType(inVar.varType));
155 16457 outVar.values := DAEUtil.setStartAttr(oattr, inExp);
156 end setVarStartValue;
157
158 public function setVarStartValueOption "author: Frenkel TUD
159 Sets the start value attribute of a variable."
160 input BackendDAE.Var inVar;
161 input Option<DAE.Exp> inExp;
162 output BackendDAE.Var outVar = inVar;
163 protected
164 Option<DAE.VariableAttributes> oattr;
165 algorithm
166
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 315246 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 315246 times.
315246 oattr := if isSome(inVar.values) then inVar.values else SOME(getVariableAttributefromType(inVar.varType));
167 315246 outVar.values := DAEUtil.setStartAttrOption(oattr, inExp);
168 end setVarStartValueOption;
169
170 public function setVarStartOrigin "author: Frenkel TUD
171 Sets the startOrigin attribute of a variable."
172 input BackendDAE.Var inVar;
173 input Option<DAE.StartOrigin> startOrigin;
174 output BackendDAE.Var outVar = inVar;
175 protected
176 Option<DAE.VariableAttributes> oattr;
177 algorithm
178
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 9316 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9316 times.
9316 oattr := if isSome(inVar.values) then inVar.values else SOME(getVariableAttributefromType(inVar.varType));
179 9316 outVar.values := DAEUtil.setStartOrigin(oattr, startOrigin);
180 end setVarStartOrigin;
181
182 public function setVarAttributes "sets the variable attributes of a variable.
183 author: Peter Aronsson (paronsson@wolfram.com)"
184 input BackendDAE.Var inVar;
185 input Option<DAE.VariableAttributes> inAttr;
186 output BackendDAE.Var outVar = inVar;
187 algorithm
188 756391 outVar.values := inAttr;
189 end setVarAttributes;
190
191 public function varStartValue "author: PA
192 Returns the DAE.StartValue of a variable."
193 input BackendDAE.Var inVar;
194 output DAE.Exp sv;
195 algorithm
196 212980 sv := DAEUtil.getStartAttr(inVar.values, inVar.varType);
197 end varStartValue;
198
199 public function varUnreplaceable "author: lochel
200 Returns the unreplaceable attribute of a variable."
201 input BackendDAE.Var inVar;
202 output Boolean outUnreplaceable = inVar.unreplaceable;
203 end varUnreplaceable;
204
205 public function setVarUnreplaceable "author: arun
206 sets the unreplaceable attribute of a variable to be false or true"
207 input BackendDAE.Var inVar;
208 input Boolean value;
209 output BackendDAE.Var outVar = inVar;
210 algorithm
211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 201 times.
201 outVar.unreplaceable:= value;
212 end setVarUnreplaceable;
213
214 public function setVarInitNonlinear
215 "sets the unreplaceable attribute of a variable to be false or true"
216 input output BackendDAE.Var var;
217 input Boolean value;
218 algorithm
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 386 times.
386 var.initNonlinear:= value;
220 end setVarInitNonlinear;
221
222 public function varStartValueFail "author: Frenkel TUD
223 Returns the DAE.StartValue of a variable if there is one.
224 Otherwise fail"
225 input BackendDAE.Var v;
226 output DAE.Exp sv;
227 protected
228 Option<DAE.VariableAttributes> attr;
229 algorithm
230 88757 BackendDAE.VAR(values = attr) := v;
231 88757 sv := DAEUtil.getStartAttrFail(attr);
232 end varStartValueFail;
233
234 public function varNominalValueFail "author: lochel
235 Returns the DAE.NominalValue of a variable if there is one.
236 Otherwise fails."
237 input BackendDAE.Var v;
238 output DAE.Exp sv;
239 protected
240 Option<DAE.VariableAttributes> attr;
241 algorithm
242 253037 BackendDAE.VAR(values = attr) := v;
243 253037 sv := DAEUtil.getNominalAttrFail(attr);
244 end varNominalValueFail;
245
246 public function varMinValueFail "author: lochel
247 Returns the DAE.MinValue of a variable if there is one.
248 Otherwise fails."
249 input BackendDAE.Var v;
250 output DAE.Exp sv;
251 protected
252 Option<DAE.VariableAttributes> attr;
253 algorithm
254 253037 BackendDAE.VAR(values = attr) := v;
255 253037 sv := DAEUtil.getMinAttrFail(attr);
256 end varMinValueFail;
257
258 public function varMaxValueFail "author: lochel
259 Returns the DAE.MaxValue of a variable if there is one.
260 Otherwise fails."
261 input BackendDAE.Var v;
262 output DAE.Exp sv;
263 protected
264 Option<DAE.VariableAttributes> attr;
265 algorithm
266 253037 BackendDAE.VAR(values = attr) := v;
267 253037 sv := DAEUtil.getMaxAttrFail(attr);
268 end varMaxValueFail;
269
270 public function varStartValueType "author: Frenkel TUD 2012-11
271 Returns the DAE.StartValue of a variable. If nothing is set the type specific one is used"
272 input BackendDAE.Var v;
273 output DAE.Exp sv;
274 protected
275 Option<DAE.VariableAttributes> attr;
276 DAE.Type ty;
277 algorithm
278 ✗ BackendDAE.VAR(values=attr, varType=ty) := v;
279 ✗ sv := DAEUtil.getStartAttr(attr, ty);
280 end varStartValueType;
281
282 public function varStartValueOption "author: Frenkel TUD
283 Returns the DAE.StartValue of a variable if there is one.
284 Otherwise fail"
285 input BackendDAE.Var v;
286 output Option<DAE.Exp> sv;
287 algorithm
288 sv := matchcontinue v
289 local
290 Option<DAE.VariableAttributes> attr;
291 DAE.Exp exp;
292
293 case BackendDAE.VAR(values=attr) algorithm
294 223114 exp := DAEUtil.getStartAttrFail(attr);
295 then SOME(exp);
296
297 else NONE();
298 end matchcontinue;
299 end varStartValueOption;
300
301 public function varHasStartValue
302 input BackendDAE.Var inVar;
303 output Boolean outHasStartValue;
304 protected
305 Option<DAE.VariableAttributes> attr;
306 algorithm
307 389827 BackendDAE.VAR(values=attr) := inVar;
308 389827 outHasStartValue := DAEUtil.hasStartAttr(attr);
309 end varHasStartValue;
310
311 public function varHasNoStartValue
312 input BackendDAE.Var inVar;
313 output Boolean outHasNoStartValue;
314 algorithm
315 61719 outHasNoStartValue := not varHasStartValue(inVar);
316 end varHasNoStartValue;
317
318 public function varStartOrigin "author: Frenkel TUD
319 Returns the StartOrigin of a variable."
320 input BackendDAE.Var v;
321 output Option<DAE.StartOrigin> so;
322 protected
323 Option<DAE.VariableAttributes> attr;
324 algorithm
325 209180 BackendDAE.VAR(values = attr) := v;
326 209180 so := DAEUtil.getStartOrigin(attr);
327 end varStartOrigin;
328
329 public function varStartFromType
330 "Returns true if the start attribute of a variable comes from its type."
331 input BackendDAE.Var v;
332 output Boolean fromType;
333 algorithm
334 fromType := match varStartOrigin(v)
335 case SOME(DAE.StartOrigin.TYPE_CONFIDENCE()) then true;
336 case SOME(DAE.StartOrigin.TYPE_ORIGIN()) then true;
337 else false;
338 end match;
339 end varStartFromType;
340
341 public function varBindExp "author: Frenkel TUD 2010-12
342 Returns the bindExp of a variable if available otherwise fails."
343 input BackendDAE.Var v;
344 output DAE.Exp sv;
345 algorithm
346
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 548478 times.
✓ Branch 2 taken 2821 times.
✓ Branch 3 taken 545657 times.
548478 BackendDAE.VAR(bindExp=SOME(sv)) := v;
347 end varBindExp;
348
349 public function varHasConstantBindExp
350 "Returns true if the bindExp is constant otherwise false."
351 input BackendDAE.Var v;
352 output Boolean out;
353 algorithm
354 out := match v
355 local
356 DAE.Exp e;
357
358 case BackendDAE.VAR(bindExp=SOME(e))
359 41888 then Expression.isConst(e);
360
361 else false;
362 end match;
363 end varHasConstantBindExp;
364
365 public function varHasNonConstantBindExpOrStartValue
366 "Returns true if the bindExp is not constant otherwise false."
367 input BackendDAE.Var v;
368 output Boolean out;
369 algorithm
370 out := match v
371 local
372 DAE.Exp e;
373
374 case BackendDAE.VAR(bindExp=SOME(e))
375 41982 then not Expression.isConstValue(e) /* Do not use isConst here; we need to evaluate non-literals at runtime */;
376
377 86 else not varHasConstantStartExp(v);
378 end match;
379 end varHasNonConstantBindExpOrStartValue;
380
381 public function varHasConstantStartExp
382 "Returns the true if the binding/start value is constant otherwise false."
383 input BackendDAE.Var v;
384 output Boolean out;
385 protected
386 DAE.Exp e;
387 algorithm
388 try
389 17122 e := varStartValueFail(v);
390 12273 out := Expression.isConstValue(e) /* Do not use isConst here; we need to evaluate non-literals at runtime */;
391 else
392 out := true;
393 end try;
394 end varHasConstantStartExp;
395
396 public function varHasBindExp
397 "Returns the true if a bindExp exists otherwise false."
398 input BackendDAE.Var v;
399 output Boolean out;
400 algorithm
401 out := match v
402 local
403
404 case BackendDAE.VAR(bindExp = SOME(_))
405 then true;
406
407 else false;
408 end match;
409 end varHasBindExp;
410
411 public function varBindExpStartValue "author: Frenkel TUD 2010-12
412 Returns the binding or the start value if no binding is available.
413 This function fails if there is neither a binding nor a start value."
414 input BackendDAE.Var v;
415 output DAE.Exp sv;
416 algorithm
417 sv := match v
418 local
419 DAE.Exp e;
420
421 case BackendDAE.VAR(bindExp=SOME(e))
422 then e;
423
424 118 else varStartValueFail(v);
425 end match;
426 end varBindExpStartValue;
427
428 public function varBindExpStartValueNoFail
429 "Returns the binding or the start value if no binding is available."
430 input BackendDAE.Var v;
431 output DAE.Exp sv;
432 algorithm
433 sv := match v
434 local
435 DAE.Exp e;
436
437 case BackendDAE.VAR(bindExp=SOME(e))
438 then e;
439
440 3945 else varStartValue(v);
441 end match;
442 end varBindExpStartValueNoFail;
443
444 public function varStateSelect "author: PA
445 Extracts the state select attribute of a variable. If no stateselect explicilty set, return
446 StateSelect.default"
447 input BackendDAE.Var inVar;
448 output DAE.StateSelect outStateSelect;
449 algorithm
450 outStateSelect := match inVar
451 local
452 DAE.StateSelect stateselect;
453
454 case BackendDAE.VAR(values=SOME(DAE.VAR_ATTR_REAL(stateSelectOption=SOME(stateselect))))
455 then stateselect;
456
457 else DAE.DEFAULT();
458 end match;
459 end varStateSelect;
460
461 public function varHasStateSelect
462 "author: kabdelhak FHB 2019-12
463 Returns false if the StateSelect attribute is NONE(), true otherwise."
464 input BackendDAE.Var inVar;
465 output Boolean b;
466 algorithm
467 b := match inVar
468 local
469
470 case BackendDAE.VAR(values=SOME(DAE.VAR_ATTR_REAL(stateSelectOption=SOME(_))))
471 then true;
472
473 else false;
474 end match;
475 end varHasStateSelect;
476
477 public function varStateSelectAlways
478 "author: Frenkel TUD 2012-06
479 return true if var is StateSelect.always else false"
480 input BackendDAE.Var v;
481 output Boolean b;
482 algorithm
483 b := match v
484 case BackendDAE.VAR(varKind=BackendDAE.STATE(),values = SOME(DAE.VAR_ATTR_REAL(stateSelectOption = SOME(DAE.ALWAYS())))) then true;
485 else false;
486 end match;
487 end varStateSelectAlways;
488
489 public function notVarStateSelectAlways
490 "author: Frenkel TUD 2012-06
491 true if var is not StateSelect.always"
492 input BackendDAE.Var v;
493 input Integer level;
494 output Boolean b;
495 algorithm
496 b := match v
497 local Integer diffcount;
498 case BackendDAE.VAR(varKind=BackendDAE.STATE(index=diffcount))
499
3/4
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 8524 times.
✓ Branch 3 taken 19 times.
✗ Branch 4 not taken.
8543 then not(varStateSelectAlways(v) and (diffcount == level or diffcount == 1));
500 else true;
501 end match;
502 end notVarStateSelectAlways;
503
504 public function varStateSelectNever
505 "author: kabdelhak
506 Returns true, if the state select attribute is DAE.NEVER()"
507 input BackendDAE.Var inVar;
508 output Boolean isNever;
509 algorithm
510 isNever := match varStateSelect(inVar)
511 case DAE.NEVER() then true;
512 else false;
513 end match;
514 end varStateSelectNever;
515
516 public function varStateSelectAvoid
517 "Returns true, if the state select attribute is DAE.AVOID()"
518 input BackendDAE.Var inVar;
519 output Boolean isAvoid;
520 algorithm
521 isAvoid := match varStateSelect(inVar)
522 case DAE.AVOID() then true;
523 else false;
524 end match;
525 end varStateSelectAvoid;
526
527 public function varStateSelectPrefer
528 "Returns true, if the state select attribute is DAE.PREFER()"
529 input BackendDAE.Var inVar;
530 output Boolean isPrefer;
531 algorithm
532 isPrefer := match varStateSelect(inVar)
533 case DAE.PREFER() then true;
534 else false;
535 end match;
536 end varStateSelectPrefer;
537
538 public function setVarStateSelect "Sets the state select attribute of a variable."
539 input BackendDAE.Var inVar;
540 input DAE.StateSelect stateSelect;
541 output BackendDAE.Var outVar = inVar;
542 protected
543 Option<DAE.VariableAttributes> oattr;
544 algorithm
545
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4231 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4231 times.
4231 oattr := if isSome(inVar.values) then inVar.values else SOME(getVariableAttributefromType(inVar.varType));
546 4231 outVar.values := DAEUtil.setStateSelect(oattr, stateSelect);
547 end setVarStateSelect;
548
549 public function varStateSelectForced
550 "author kabdelhak FHB 2019-09
551 Returns true if the variable has a state select attribute that forces it to be a state."
552 input BackendDAE.Var inVar;
553 output Boolean isForced;
554 algorithm
555 isForced := match inVar
556 case BackendDAE.VAR(values=SOME(DAE.VAR_ATTR_REAL(stateSelectOption=SOME(DAE.ALWAYS()))))
557 then true;
558 case BackendDAE.VAR(values=SOME(DAE.VAR_ATTR_REAL(stateSelectOption=SOME(DAE.PREFER()))))
559 then true;
560 else false;
561 end match;
562 end varStateSelectForced;
563
564 public function isNaturalState
565 "author kabdelhak FHB 2019-08
566 Returns true if the input is a natural state.
567 A state is considered natural, if it occurs differentiated before index reduction."
568 input BackendDAE.Var var;
569 output Boolean natural;
570 algorithm
571 natural := match var
572 local
573 Boolean n;
574 case BackendDAE.VAR(varKind = BackendDAE.STATE(natural = n))
575 then n;
576 else false;
577 end match;
578 end isNaturalState;
579
580 public function isArtificialState
581 "author kabdelhak FHB 2019-08
582 Returns true if the input is an artificial state.
583 A state is considered artificial, if it does not occur differentiated before
584 index reduction, but is forced to be a state by stateSelect attribute."
585 input BackendDAE.Var var;
586 output Boolean artificial;
587 algorithm
588 artificial := match var
589 local
590 Boolean n;
591 case BackendDAE.VAR(varKind = BackendDAE.STATE(natural = n))
592 13241 then not n;
593 else false;
594 end match;
595 end isArtificialState;
596
597 public function varStateDerivative "author: Frenkel TUD 2013-01
598 Returns the name of the Derivative. Is no Derivative known the function will fail."
599 input BackendDAE.Var inVar;
600 output DAE.ComponentRef dcr;
601 algorithm
602 ✗ BackendDAE.VAR(varKind=BackendDAE.STATE(derName=SOME(dcr))) := inVar;
603 end varStateDerivative;
604
605 public function varHasStateDerivative "author: Frenkel TUD 2013-01
606 Returns the name of the Derivative. Is no Derivative known the function will fail."
607 input BackendDAE.Var inVar;
608 output Boolean b;
609 algorithm
610 b := match inVar
611 case BackendDAE.VAR(varKind=BackendDAE.STATE(derName=SOME(_))) then true;
612 else false;
613 end match;
614 end varHasStateDerivative;
615
616 public function setStateDerivative "author: Frenkel TUD
617 sets the state derivative."
618 input output BackendDAE.Var var;
619 input Option<DAE.ComponentRef> derName;
620 protected
621 Integer index;
622 Boolean natural;
623 algorithm
624
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5562 times.
5562 BackendDAE.STATE(index=index, natural=natural) := var.varKind;
625
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5562 times.
5562 var.varKind := BackendDAE.STATE(index, derName, natural);
626 end setStateDerivative;
627
628 public function getVariableAttributefromType
629 input DAE.Type inType;
630 output DAE.VariableAttributes attr;
631 algorithm
632 attr := match inType
633 case DAE.T_REAL() then DAE.VAR_ATTR_REAL(NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE());
634 case DAE.T_INTEGER() then DAE.VAR_ATTR_INT(NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE());
635 case DAE.T_BOOL() then DAE.VAR_ATTR_BOOL(NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE());
636 case DAE.T_STRING() then DAE.VAR_ATTR_STRING(NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE());
637 case DAE.T_ENUMERATION() then DAE.VAR_ATTR_ENUMERATION(NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE());
638 else algorithm
639 // repord a warning on failtrace
640
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
46 if Flags.isSet(Flags.FAILTRACE) then
641 ✗ Debug.trace("getVariableAttributefromType called with unsopported Type!\n");
642 end if;
643 then DAE.VAR_ATTR_REAL(NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE());
644 end match;
645 end getVariableAttributefromType;
646
647 public function setVarFinal "Sets the final attribute of a variable."
648 input BackendDAE.Var inVar;
649 input Boolean finalPrefix;
650 output BackendDAE.Var outVar = inVar;
651 protected
652 Option<DAE.VariableAttributes> oattr;
653 algorithm
654
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 69897 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 69897 times.
69897 oattr := if isSome(inVar.values) then inVar.values else SOME(getVariableAttributefromType(inVar.varType));
655 69897 outVar.values := DAEUtil.setFinalAttr(oattr, finalPrefix);
656 end setVarFinal;
657
658 public function setVarMinMax "Sets the min/max attribute of a variable.
659 lochel: This function will not clear min/max attributes if both inMin and
660 inMax are NONE(). Is that the intended behaviour?"
661 input BackendDAE.Var inVar;
662 input Option<DAE.Exp> inMin;
663 input Option<DAE.Exp> inMax;
664 output BackendDAE.Var outVar = inVar;
665 protected
666 Option<DAE.VariableAttributes> oattr;
667 algorithm
668
6/8
✗ Branch 0 not taken.
✓ Branch 1 taken 30238 times.
✓ Branch 2 taken 26322 times.
✓ Branch 3 taken 3916 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 26322 times.
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 26318 times.
30238 if isSome(inMin) or isSome(inMax) then
669
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3920 times.
✓ Branch 2 taken 60 times.
✓ Branch 3 taken 3860 times.
3920 oattr := if isSome(inVar.values) then inVar.values else SOME(getVariableAttributefromType(inVar.varType));
670 3920 outVar.values := DAEUtil.setMinMax(oattr, inMin, inMax);
671 end if;
672 end setVarMinMax;
673
674 public function varNominalValue "author: Frenkel TUD"
675 input BackendDAE.Var inVar;
676 output DAE.Exp outExp;
677 algorithm
678
8/10
✗ Branch 0 not taken.
✓ Branch 1 taken 200924 times.
✓ Branch 2 taken 738 times.
✓ Branch 3 taken 200186 times.
✓ Branch 5 taken 7214 times.
✓ Branch 6 taken 192972 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 192972 times.
✓ Branch 10 taken 177952 times.
✓ Branch 11 taken 15020 times.
200924 BackendDAE.VAR(values=SOME(DAE.VAR_ATTR_REAL(nominal=SOME(outExp)))) := inVar;
679 end varNominalValue;
680
681 public function setVarNominalValue "author: Frenkel TUD
682 Sets the nominal value attribute of a variable."
683 input BackendDAE.Var inVar;
684 input DAE.Exp inExp;
685 output BackendDAE.Var outVar = inVar;
686 protected
687 Option<DAE.VariableAttributes> oattr;
688 algorithm
689
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3101 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3101 times.
3101 oattr := if isSome(inVar.values) then inVar.values else SOME(getVariableAttributefromType(inVar.varType));
690 3101 outVar.values := DAEUtil.setNominalAttr(oattr, inExp);
691 end setVarNominalValue;
692
693 public function varType "author: PA
694 extracts the type of a variable"
695 input BackendDAE.Var inVar;
696 output BackendDAE.Type outType;
697 algorithm
698 2302 BackendDAE.VAR(varType=outType) := inVar;
699 end varType;
700
701 public function varKind "author: PA
702 extracts the kind of a variable"
703 input BackendDAE.Var inVar;
704 output BackendDAE.VarKind outVarKind;
705 algorithm
706 367396 BackendDAE.VAR(varKind=outVarKind) := inVar;
707 end varKind;
708
709 public function varNominal "author: PA
710 Extacts the nominal attribute of a variable. If the variable has no
711 nominal value, the function fails."
712 input BackendDAE.Var inVar;
713 output Real outReal;
714 algorithm
715 ✗ BackendDAE.VAR(values = SOME(DAE.VAR_ATTR_REAL(nominal=SOME(DAE.RCONST(outReal))))) := inVar;
716 end varNominal;
717
718 public function varHasNominalValue "author: BB"
719 input BackendDAE.Var inVar;
720 output Boolean outBool;
721 algorithm
722 try
723 ✗ BackendDAE.VAR(values = SOME(DAE.VAR_ATTR_REAL(nominal=SOME(DAE.RCONST())))) := inVar;
724 outBool := true;
725 else
726 outBool :=false;
727 end try;
728 end varHasNominalValue;
729
730 public function varCref "author: PA
731 extracts the ComponentRef of a variable"
732 input BackendDAE.Var inVar;
733 output DAE.ComponentRef outComponentRef;
734 algorithm
735 2355412 BackendDAE.VAR(varName=outComponentRef) := inVar;
736 end varCref;
737
738 public function isStateVar
739 "Returns true for state variables, false otherwise."
740 input BackendDAE.Var inVar;
741 output Boolean outBoolean;
742 algorithm
743 outBoolean := match inVar
744 case BackendDAE.VAR(varKind = BackendDAE.STATE()) then true;
745 else false;
746 end match;
747 end isStateVar;
748
749 public function isState
750 input DAE.ComponentRef inCref;
751 input BackendDAE.Variables inVars;
752 output Boolean outBool;
753 algorithm
754 outBool:=
755 matchcontinue inVars
756 case _ algorithm
757
3/4
✗ Branch 1 not taken.
✓ Branch 2 taken 17260 times.
✓ Branch 3 taken 14086 times.
✓ Branch 4 taken 3174 times.
21058 ((BackendDAE.VAR(varKind = BackendDAE.STATE()) :: _),_) := getVar(inCref, inVars);
758 then true;
759
760 else false;
761 end matchcontinue;
762 end isState;
763
764 public function isNonStateVar
765 "this equation checks if the the varkind is state of variable
766 used both in build_equation and generate_compute_state"
767 input BackendDAE.Var inVar;
768 output Boolean outBoolean;
769 algorithm
770 outBoolean := match inVar
771 case BackendDAE.VAR(varKind=BackendDAE.VARIABLE()) then true;
772 case BackendDAE.VAR(varKind=BackendDAE.DUMMY_DER()) then true;
773 case BackendDAE.VAR(varKind=BackendDAE.DUMMY_STATE()) then true;
774 case BackendDAE.VAR(varKind=BackendDAE.DISCRETE()) then true;
775 case BackendDAE.VAR(varKind=BackendDAE.STATE_DER()) then true;
776 case BackendDAE.VAR(varKind=BackendDAE.OPT_CONSTR()) then true;
777 case BackendDAE.VAR(varKind=BackendDAE.OPT_FCONSTR()) then true;
778 case BackendDAE.VAR(varKind=BackendDAE.OPT_INPUT_WITH_DER()) then true;
779 case BackendDAE.VAR(varKind=BackendDAE.OPT_INPUT_DER()) then true;
780 case BackendDAE.VAR(varKind=BackendDAE.OPT_TGRID()) then true;
781 case BackendDAE.VAR(varKind=BackendDAE.OPT_LOOP_INPUT()) then true;
782 case BackendDAE.VAR(varKind=BackendDAE.ALG_STATE()) then true;
783 case BackendDAE.VAR(varKind=BackendDAE.LOOP_ITERATION()) then true;
784 case BackendDAE.VAR(varKind=BackendDAE.LOOP_SOLVED()) then true;
785 else false;
786 end match;
787 end isNonStateVar;
788
789 public function isClockedStateVar
790 "Returns true for clocked state variables, false otherwise."
791 input BackendDAE.Var inVar;
792 output Boolean outBool;
793 algorithm
794 outBool := match inVar
795 case BackendDAE.VAR(varKind = BackendDAE.CLOCKED_STATE()) then true;
796 else false;
797 end match;
798 end isClockedStateVar;
799
800 public function isClockedState
801 "Returns true for clocked state variables, false otherwise."
802 input DAE.ComponentRef inCref;
803 input BackendDAE.Variables inVars;
804 output Boolean outBool;
805 algorithm
806 outBool :=
807 matchcontinue inVars
808 case _ algorithm
809 ✗ ((BackendDAE.VAR(varKind = BackendDAE.CLOCKED_STATE()) :: _),_) := getVar(inCref, inVars);
810 then true;
811 else false;
812 end matchcontinue;
813 end isClockedState;
814
815 public function varHasUncertainValueRefine "author: Daniel Hedberg, 2011-01
816 modified by: Leonardo Laguna, 2012-01
817
818 Returns true if the specified variable has the attribute uncertain and the
819 value of it is Uncertainty.refine, false otherwise."
820 input BackendDAE.Var var;
821 output Boolean b;
822 algorithm
823 b := match var
824 case BackendDAE.VAR(values=SOME(DAE.VAR_ATTR_REAL(uncertainOption=SOME(DAE.REFINE())))) then true;
825 case BackendDAE.VAR(values=SOME(DAE.VAR_ATTR_INT(uncertainOption=SOME(DAE.REFINE())))) then true;
826 else false;
827 end match;
828 end varHasUncertainValueRefine;
829
830 public function varHasUncertainValuePropagate
831 "Returns true if the specified variable has the attribute uncertain and the
832 value of it is Uncertainty.propagate, false otherwise."
833 input BackendDAE.Var var;
834 output Boolean b;
835 algorithm
836 b := match var
837 case BackendDAE.VAR(values=SOME(DAE.VAR_ATTR_REAL(uncertainOption=SOME(DAE.PROPAGATE())))) then true;
838 else false;
839 end match;
840 end varHasUncertainValuePropagate;
841
842 public function varDistribution "author: Peter Aronsson, 2012-05
843 Returns Distribution record of a variable."
844 input BackendDAE.Var var;
845 output DAE.Distribution d;
846 algorithm
847 d := match var
848 case BackendDAE.VAR(values=SOME(DAE.VAR_ATTR_REAL(distributionOption=SOME(d)))) then d;
849 case BackendDAE.VAR(values=SOME(DAE.VAR_ATTR_INT(distributionOption=SOME(d)))) then d;
850 end match;
851 end varDistribution;
852
853 public function varTryGetDistribution "author: Peter Aronsson, 2012-05
854 Returns Distribution record of a variable."
855 input BackendDAE.Var var;
856 output Option<DAE.Distribution> dout;
857 protected
858 Option<DAE.Distribution> d;
859 algorithm
860 dout := match var
861 case BackendDAE.VAR(values=SOME(DAE.VAR_ATTR_REAL(distributionOption=d as SOME(_)))) then d;
862 case BackendDAE.VAR(values=SOME(DAE.VAR_ATTR_INT(distributionOption=d as SOME(_)))) then d;
863 else NONE();
864 end match;
865 end varTryGetDistribution;
866
867 public function varUncertainty "author: Peter Aronsson, 2012-05
868 Returns Uncertainty of a variable."
869 input BackendDAE.Var var;
870 output DAE.Uncertainty u;
871 algorithm
872 u := match var
873 case BackendDAE.VAR(values=SOME(DAE.VAR_ATTR_REAL(uncertainOption=SOME(u)))) then u;
874 case BackendDAE.VAR(values=SOME(DAE.VAR_ATTR_INT(uncertainOption=SOME(u)))) then u;
875 end match;
876 end varUncertainty;
877
878 public function varHasDistributionAttribute "author: Peter Aronsson, 2012-05
879 Returns true if the specified variable has the attribute distribution set."
880 input BackendDAE.Var var;
881 output Boolean b;
882 algorithm
883 b := match var
884 case BackendDAE.VAR(values=SOME(DAE.VAR_ATTR_REAL(distributionOption=SOME(_)))) then true;
885 case BackendDAE.VAR(values=SOME(DAE.VAR_ATTR_INT(distributionOption=SOME(_)))) then true;
886 else false;
887 end match;
888 end varHasDistributionAttribute;
889
890 public function varHasUncertaintyAttribute "author: Peter Aronsson, 2012-05
891 Returns true if the specified variable has the attribute uncertain set."
892 input BackendDAE.Var var;
893 output Boolean b;
894 algorithm
895 b := match var
896 case BackendDAE.VAR(values=SOME(DAE.VAR_ATTR_REAL(uncertainOption=SOME(_)))) then true;
897 case BackendDAE.VAR(values=SOME(DAE.VAR_ATTR_INT(uncertainOption=SOME(_)))) then true;
898 else false;
899 end match;
900 end varHasUncertaintyAttribute;
901
902 public function isDummyStateVar "Returns true for dummy state variables, false otherwise."
903 input BackendDAE.Var inVar;
904 output Boolean outBoolean;
905 algorithm
906 outBoolean := match inVar
907 case BackendDAE.VAR(varKind = BackendDAE.DUMMY_STATE()) then true;
908 else false;
909 end match;
910 end isDummyStateVar;
911
912 public function isDummyDerVar
913 "Returns true for dummy state variables, false otherwise."
914 input BackendDAE.Var inVar;
915 output Boolean outBoolean;
916 algorithm
917 outBoolean := match inVar
918 case BackendDAE.VAR(varKind = BackendDAE.DUMMY_DER()) then true;
919 else false;
920 end match;
921 end isDummyDerVar;
922
923 public function isStateDerVar "
924 Returns true for der(state) variables, false otherwise."
925 input BackendDAE.Var inVar;
926 output Boolean outBoolean;
927 algorithm
928 outBoolean := match inVar
929 case BackendDAE.VAR(varKind = BackendDAE.STATE_DER()) then true;
930 else false;
931 end match;
932 end isStateDerVar;
933
934 public function isStateorStateDerVar
935 "Returns true for state and der(state) variables, false otherwise."
936 input BackendDAE.Var inVar;
937 output Boolean outBoolean;
938 algorithm
939 outBoolean := match inVar
940 case BackendDAE.VAR(varKind = BackendDAE.STATE()) then true;
941 case BackendDAE.VAR(varKind = BackendDAE.STATE_DER()) then true;
942 else false;
943 end match;
944 end isStateorStateDerVar;
945
946 public function isVarDiscrete
947 "This functions checks if BackendDAE.Var is discrete"
948 input BackendDAE.Var inVar;
949 output Boolean outBoolean;
950 algorithm
951 outBoolean := match inVar
952 case BackendDAE.VAR(varKind = BackendDAE.DISCRETE()) then true;
953 case BackendDAE.VAR(varKind = BackendDAE.PARAM()) then true;
954 case BackendDAE.VAR(varKind = BackendDAE.CONST()) then true;
955 case BackendDAE.VAR(varType = DAE.T_INTEGER()) then true;
956 case BackendDAE.VAR(varType = DAE.T_BOOL()) then true;
957 case BackendDAE.VAR(varType = DAE.T_ENUMERATION()) then true;
958 else false;
959 end match;
960 end isVarDiscrete;
961
962 public function isVarNonDifferentiable
963 "This functions checks if BackendDAE.Var is not differentiable"
964 input BackendDAE.Var inVar;
965 output Boolean outBoolean;
966 algorithm
967 outBoolean := match inVar
968 case BackendDAE.VAR(varKind = BackendDAE.DISCRETE()) then true;
969 case BackendDAE.VAR(varType = DAE.T_INTEGER()) then true;
970 case BackendDAE.VAR(varType = DAE.T_BOOL()) then true;
971 case BackendDAE.VAR(varType = DAE.T_ENUMERATION()) then true;
972 else false;
973 end match;
974 end isVarNonDifferentiable;
975
976 public function isVarClockedState
977 "This functions checks if BackendDAE.Var is a clocked state"
978 input BackendDAE.Var inVar;
979 output Boolean outBoolean;
980 protected
981 String test;
982 algorithm
983 outBoolean := match inVar
984 case BackendDAE.VAR(varKind = BackendDAE.CLOCKED_STATE()) then true;
985 else false;
986 end match;
987 test := "";
988 end isVarClockedState;
989
990 public function isDiscrete
991 "This functions checks if BackendDAE.Var is discrete"
992 input DAE.ComponentRef cr;
993 input BackendDAE.Variables vars;
994 output Boolean outBoolean;
995 protected
996 BackendDAE.Var v;
997 algorithm
998 34 (v,_) := getVarSingle(cr,vars);
999 32 outBoolean := isVarDiscrete(v);
1000 end isDiscrete;
1001
1002 public function isVarNonDiscrete
1003 input BackendDAE.Var inVar;
1004 output Boolean outBoolean;
1005 algorithm
1006 4 outBoolean := not isVarDiscrete(inVar);
1007 end isVarNonDiscrete;
1008
1009 public function hasDiscreteVar
1010 "Returns true if var list contains a discrete time variable."
1011 input list<BackendDAE.Var> inBackendDAEVarLst;
1012 output Boolean outBoolean = false;
1013 algorithm
1014
1015 ✗ for v in inBackendDAEVarLst loop
1016 ✗ outBoolean := isVarDiscrete(v);
1017 ✗ if outBoolean then
1018 break;
1019 end if;
1020 end for;
1021
1022 end hasDiscreteVar;
1023
1024 public function hasContinuousVar
1025 "Returns true if var list contains a continuous time variable."
1026 input list<BackendDAE.Var> inBackendDAEVarLst;
1027 output Boolean outBoolean;
1028 algorithm
1029 outBoolean := match inBackendDAEVarLst
1030 local
1031 list<BackendDAE.Var> vs;
1032
1033 case BackendDAE.VAR(varKind=BackendDAE.VARIABLE(), varType = DAE.T_REAL()) :: _ then true;
1034 case BackendDAE.VAR(varKind=BackendDAE.VARIABLE(), varType = DAE.T_ARRAY(ty=DAE.T_REAL())) :: _ then true;
1035 case BackendDAE.VAR(varKind=BackendDAE.STATE()) :: _ then true;
1036 case BackendDAE.VAR(varKind=BackendDAE.STATE_DER()) :: _ then true;
1037 case BackendDAE.VAR(varKind=BackendDAE.DUMMY_DER()) :: _ then true;
1038 case BackendDAE.VAR(varKind=BackendDAE.DUMMY_STATE()) :: _ then true;
1039 case BackendDAE.VAR(varKind=BackendDAE.OPT_CONSTR()) :: _ then true;
1040 case BackendDAE.VAR(varKind=BackendDAE.OPT_FCONSTR()) :: _ then true;
1041 case BackendDAE.VAR(varKind=BackendDAE.OPT_INPUT_WITH_DER()) :: _ then true;
1042 case BackendDAE.VAR(varKind=BackendDAE.OPT_INPUT_DER()) :: _ then true;
1043 case BackendDAE.VAR(varKind=BackendDAE.OPT_TGRID()) :: _ then true;
1044 case BackendDAE.VAR(varKind=BackendDAE.OPT_LOOP_INPUT()) :: _ then true;
1045 case BackendDAE.VAR(varKind=BackendDAE.ALG_STATE()) :: _ then true;
1046 145 case _ :: vs then hasContinuousVar(vs);
1047 case {} then false;
1048 end match;
1049 end hasContinuousVar;
1050
1051 public function isVarNonDiscreteAlg
1052 input BackendDAE.Var var;
1053 output Boolean result;
1054 algorithm
1055 result := match var
1056 /* Real non discrete variable */
1057 case BackendDAE.VAR(varType = DAE.T_REAL()) algorithm
1058
6/6
✓ Branch 1 taken 208880 times.
✓ Branch 2 taken 25599 times.
✓ Branch 4 taken 461 times.
✓ Branch 5 taken 208419 times.
✓ Branch 7 taken 16 times.
✓ Branch 8 taken 26044 times.
234479 then (isVarAlg(var) and not isVarDiscreteRealAlg(var)) or isOptInputVar(var);
1059
1060 else false;
1061 end match;
1062 end isVarNonDiscreteAlg;
1063
1064 public function isOptInputVar
1065 input BackendDAE.Var var;
1066 output Boolean b;
1067 algorithm
1068 b := match var.varKind
1069 case BackendDAE.OPT_LOOP_INPUT() then true;
1070 case BackendDAE.OPT_INPUT_WITH_DER() then true;
1071 case BackendDAE.OPT_INPUT_DER() then true;
1072 else false;
1073 end match;
1074 end isOptInputVar;
1075
1076 public function isVarDiscreteRealAlg
1077 input BackendDAE.Var var;
1078 output Boolean result;
1079 algorithm
1080 result := match var
1081 /* Real discrete variable */
1082 case BackendDAE.VAR(varKind = BackendDAE.DISCRETE(), varType = DAE.T_REAL()) then true;
1083 else false;
1084 end match;
1085 end isVarDiscreteRealAlg;
1086
1087 public function isVarAlg
1088 input BackendDAE.Var var;
1089 output Boolean result;
1090 algorithm
1091 result := match var.varKind
1092 case BackendDAE.VARIABLE() then true;
1093 case BackendDAE.DISCRETE() then true;
1094 case BackendDAE.DUMMY_DER() then true;
1095 case BackendDAE.DUMMY_STATE() then true;
1096 case BackendDAE.CLOCKED_STATE() then true;
1097 else false;
1098 end match;
1099 end isVarAlg;
1100
1101 public function isVarConst
1102 input BackendDAE.Var var;
1103 output Boolean result;
1104 algorithm
1105 result :=
1106 match var
1107 /* bool variable */
1108 case BackendDAE.VAR(varType = DAE.T_BOOL())
1109 then false;
1110 /* int variable */
1111 case BackendDAE.VAR(varType = DAE.T_INTEGER())
1112 then false;
1113 /* enum variable */
1114 case BackendDAE.VAR(varType = DAE.T_ENUMERATION())
1115 then false;
1116 /* string variable */
1117 case BackendDAE.VAR(varType = DAE.T_STRING())
1118 then false;
1119 /* non-string variable */
1120 case _ guard isConst(var)
1121 then true;
1122 else
1123 false;
1124 end match;
1125 end isVarConst;
1126
1127 public function isVarStringConst
1128 input BackendDAE.Var var;
1129 output Boolean result;
1130 algorithm
1131 result := match var
1132 /* string variable */
1133 case BackendDAE.VAR(varType = DAE.T_STRING()) guard isConst(var) then true;
1134 else false;
1135 end match;
1136 end isVarStringConst;
1137
1138 public function isVarIntConst
1139 input BackendDAE.Var var;
1140 output Boolean result;
1141 algorithm
1142 result := match var
1143 local
1144 /* int variable */
1145 case BackendDAE.VAR(varType = DAE.T_INTEGER()) guard isConst(var)
1146 then true;
1147 case BackendDAE.VAR(varType = DAE.T_ENUMERATION()) guard isConst(var)
1148 then true;
1149 else
1150 false;
1151 end match;
1152 end isVarIntConst;
1153
1154 public function isVarBoolConst
1155 input BackendDAE.Var var;
1156 output Boolean result;
1157 algorithm
1158 result := match var
1159 /* string variable */
1160 case BackendDAE.VAR(varType = DAE.T_BOOL()) guard isConst(var)
1161 then true;
1162 else
1163 false;
1164 end match;
1165 end isVarBoolConst;
1166
1167 /* TODO: Is this correct? */
1168 public function isVarParam
1169 input BackendDAE.Var var;
1170 output Boolean result;
1171 algorithm
1172 result := match var
1173 local
1174 /* bool variable */
1175 case BackendDAE.VAR(varType = DAE.T_BOOL())
1176 then false;
1177 /* int variable */
1178 case BackendDAE.VAR(varType = DAE.T_INTEGER())
1179 then false;
1180 /* string variable */
1181 case BackendDAE.VAR(varType = DAE.T_STRING())
1182 then false;
1183 /* enum variable */
1184 case BackendDAE.VAR(varType = DAE.T_ENUMERATION())
1185 then false;
1186 /* non-string variable */
1187 case _ guard isParam(var)
1188 then true;
1189 else
1190 false;
1191 end match;
1192 end isVarParam;
1193
1194
1195 public function isVarStringParam
1196 input BackendDAE.Var var;
1197 output Boolean result;
1198 algorithm
1199 result := match var
1200 /* string variable */
1201 case BackendDAE.VAR(varType = DAE.T_STRING()) guard isParam(var)
1202 then true;
1203 else
1204 false;
1205 end match;
1206 end isVarStringParam;
1207
1208
1209 public function isVarIntParam
1210 input BackendDAE.Var var;
1211 output Boolean result;
1212 algorithm
1213 result := match var
1214 // int variable
1215 case BackendDAE.VAR(varType = DAE.T_INTEGER()) guard isParam(var)
1216 then true;
1217 // enum is also mapped to long
1218 case BackendDAE.VAR(varType = DAE.T_ENUMERATION()) guard isParam(var)
1219 then true;
1220 else
1221 false;
1222 end match;
1223 end isVarIntParam;
1224
1225 public function isVarBoolParam
1226 input BackendDAE.Var var;
1227 output Boolean result;
1228 algorithm
1229 result := match var
1230 /* string variable */
1231 case BackendDAE.VAR(varType = DAE.T_BOOL()) guard isParam(var)
1232 then true;
1233 else
1234 false;
1235 end match;
1236 end isVarBoolParam;
1237
1238 public function isVarConnector
1239 input BackendDAE.Var var;
1240 output Boolean result;
1241 algorithm
1242 result := match var
1243 case BackendDAE.VAR(connectorType = DAE.NON_CONNECTOR()) then false;
1244 else true;
1245 end match;
1246 end isVarConnector;
1247
1248 public function isFlowVar
1249 "Returns true for flow variables, false otherwise."
1250 input BackendDAE.Var inVar;
1251 output Boolean outBoolean;
1252 algorithm
1253 outBoolean:= match inVar
1254 case BackendDAE.VAR(connectorType = DAE.FLOW()) then true;
1255 else false;
1256 end match;
1257 end isFlowVar;
1258
1259 public function isConst
1260 "Return true if variable is a constant."
1261 input BackendDAE.Var inVar;
1262 output Boolean outBoolean;
1263 algorithm
1264 outBoolean:= match inVar
1265 case BackendDAE.VAR(varKind = BackendDAE.CONST()) then true;
1266 else false;
1267 end match;
1268 end isConst;
1269
1270 public function isParam
1271 "Return true if variable is a parameter."
1272 input BackendDAE.Var inVar;
1273 output Boolean outBoolean;
1274 algorithm
1275 outBoolean:= match inVar
1276 case BackendDAE.VAR(varKind = BackendDAE.PARAM()) then true;
1277 case BackendDAE.VAR(varKind = BackendDAE.OPT_TGRID()) then true;
1278 else false;
1279 end match;
1280 end isParam;
1281
1282 public function makeParam
1283 "Change variable to parameter"
1284 input output BackendDAE.Var var;
1285 algorithm
1286 1343 var.varKind := BackendDAE.PARAM();
1287 end makeParam;
1288
1289 public function makeParamOutputsOnly
1290 "Change variable to parameter"
1291 input output BackendDAE.Var var;
1292 input output Boolean fixed; // also output for traversing
1293 algorithm
1294 163 var.varKind := BackendDAE.PARAM();
1295 163 var := setHideResult(var, SOME(DAE.BCONST(true)));
1296
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 163 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 163 times.
163 var.values := if isSome(var.values) then var.values else SOME(getVariableAttributefromType(var.varType));
1297
3/4
✗ Branch 1 not taken.
✓ Branch 2 taken 163 times.
✓ Branch 5 taken 42 times.
✓ Branch 6 taken 121 times.
163 if isNone(DAEUtil.getFixedAttr(var.values)) then
1298
1/2
✓ Branch 0 taken 121 times.
✗ Branch 1 not taken.
363 var.values := DAEUtil.setFixedAttr(var.values, SOME(DAE.BCONST(fixed)));
1299 end if;
1300 end makeParamOutputsOnly;
1301
1302 public function isParamOrConstant
1303 "Return true if variable is parameter or constant"
1304 input BackendDAE.Var invar;
1305 output Boolean outbool=false;
1306 algorithm
1307
3/4
✓ Branch 1 taken 5611 times.
✓ Branch 2 taken 1054 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5611 times.
6665 outbool := isParam(invar) or isConst(invar);
1308 end isParamOrConstant;
1309
1310 public function isIntParam
1311 "Return true if variable is a parameter and integer."
1312 input BackendDAE.Var inVar;
1313 output Boolean outBoolean;
1314 algorithm
1315 outBoolean:= match inVar
1316 case BackendDAE.VAR(varKind = BackendDAE.PARAM(),varType = DAE.T_INTEGER()) then true;
1317 case BackendDAE.VAR(varKind = BackendDAE.PARAM(),varType = DAE.T_ENUMERATION()) then true;
1318 else false;
1319 end match;
1320 end isIntParam;
1321
1322 public function isBoolParam
1323 "Return true if variable is a parameter and boolean."
1324 input BackendDAE.Var inVar;
1325 output Boolean outBoolean;
1326 algorithm
1327 outBoolean:= match inVar
1328 case BackendDAE.VAR(varKind = BackendDAE.PARAM(),varType = DAE.T_BOOL()) then true;
1329 else false;
1330 end match;
1331 end isBoolParam;
1332
1333 public function isStringParam
1334 "Return true if variable is a parameter."
1335 input BackendDAE.Var inVar;
1336 output Boolean outBoolean;
1337 algorithm
1338 outBoolean:= match inVar
1339 case BackendDAE.VAR(varKind = BackendDAE.PARAM(),varType = DAE.T_STRING()) then true;
1340 else false;
1341 end match;
1342 end isStringParam;
1343
1344 public function isExtObj
1345 "Return true if variable is an external object."
1346 input BackendDAE.Var inVar;
1347 output Boolean outBoolean;
1348 algorithm
1349 outBoolean:= match inVar
1350 case BackendDAE.VAR(varKind = BackendDAE.EXTOBJ(_)) then true;
1351 else false;
1352 end match;
1353 end isExtObj;
1354
1355 public function isAlgState
1356 "Return true if variable is alg state"
1357 input BackendDAE.Var inVar;
1358 output Boolean outBoolean;
1359 algorithm
1360 outBoolean := match inVar
1361 case BackendDAE.VAR(varKind=BackendDAE.ALG_STATE()) then true;
1362 else false;
1363 end match;
1364 end isAlgState;
1365
1366 public function isRealParam
1367 "Return true if variable is a parameter of real-type"
1368 input BackendDAE.Var inVar;
1369 output Boolean outBoolean;
1370 algorithm
1371 outBoolean := match inVar
1372 case BackendDAE.VAR(varKind = BackendDAE.PARAM(),varType = DAE.T_REAL()) then true;
1373 else false;
1374 end match;
1375 end isRealParam;
1376
1377 public function isRealOptimizeConstraintsVars
1378 "Return true if variable is a constraint(slack variable)"
1379 input BackendDAE.Var inVar;
1380 output Boolean outBoolean;
1381 algorithm
1382 outBoolean := match inVar
1383 case BackendDAE.VAR(varKind = BackendDAE.OPT_CONSTR()) then true;
1384 else false;
1385 end match;
1386 end isRealOptimizeConstraintsVars;
1387
1388 public function isDAEmodeVar
1389 "Return true if variable is a daeMode variable"
1390 input BackendDAE.Var inVar;
1391 output Boolean outBoolean;
1392 algorithm
1393 outBoolean := match inVar
1394 case BackendDAE.VAR(varKind = BackendDAE.DAE_RESIDUAL_VAR()) then true;
1395 case BackendDAE.VAR(varKind = BackendDAE.DAE_AUX_VAR()) then true;
1396 else false;
1397 end match;
1398 end isDAEmodeVar;
1399
1400 public function isDAEmodeResVar
1401 "Return true if variable is a daeMode variable"
1402 input BackendDAE.Var inVar;
1403 output Boolean outBoolean;
1404 algorithm
1405 outBoolean := match inVar
1406 case BackendDAE.VAR(varKind = BackendDAE.DAE_RESIDUAL_VAR()) then true;
1407 else false;
1408 end match;
1409 end isDAEmodeResVar;
1410
1411 public function isDAEmodeAuxVar
1412 "Return true if variable is a daeMode variable"
1413 input BackendDAE.Var inVar;
1414 output Boolean outBoolean;
1415 algorithm
1416 outBoolean := match inVar
1417 case BackendDAE.VAR(varKind = BackendDAE.DAE_AUX_VAR()) then true;
1418 else false;
1419 end match;
1420 end isDAEmodeAuxVar;
1421
1422 public function isRealOptimizeFinalConstraintsVars
1423 "Return true if variable is a final constraint(slack variable)"
1424 input BackendDAE.Var inVar;
1425 output Boolean outBoolean;
1426 algorithm
1427 outBoolean := match inVar
1428 case BackendDAE.VAR(varKind = BackendDAE.OPT_FCONSTR()) then true;
1429 else false;
1430 end match;
1431 end isRealOptimizeFinalConstraintsVars;
1432
1433 public function isRealOptimizeDerInput
1434 "Return true if variable replaced der(Input)"
1435 input BackendDAE.Var inVar;
1436 output Boolean outBoolean;
1437 algorithm
1438 outBoolean := match inVar
1439 case BackendDAE.VAR(varKind = BackendDAE.OPT_INPUT_DER()) then true;
1440 else false;
1441 end match;
1442 end isRealOptimizeDerInput;
1443
1444 public function isAlgebraicOldState
1445 "Return true if variable is old algebraic variable for inline integration."
1446 input BackendDAE.Var inVar;
1447 output Boolean outBoolean;
1448 algorithm
1449 outBoolean := match inVar
1450 case BackendDAE.VAR(varKind = BackendDAE.ALG_STATE_OLD()) then true;
1451 else false;
1452 end match;
1453 end isAlgebraicOldState;
1454
1455 public function isCSEVar
1456 "Return true if variable is introduced by wrapFunctionCall and prefixed with '$cse'"
1457 input BackendDAE.Var inVar;
1458 output Boolean outBoolean;
1459 algorithm
1460 outBoolean := match inVar
1461 case BackendDAE.VAR() guard CommonSubExpression.isCSECref(inVar.varName) then true;
1462 else false;
1463 end match;
1464 end isCSEVar;
1465
1466 public function isRESVar
1467 input BackendDAE.Var inVar;
1468 output Boolean outBoolean;
1469 algorithm
1470 outBoolean := match inVar.varName
1471 local
1472 String s;
1473 430 case DAE.CREF_IDENT(ident=s) then StringUtil.startsWith(s, "$res");
1474 16259 case DAE.CREF_QUAL(ident=s) then StringUtil.startsWith(s, "$res");
1475 else false;
1476 end match;
1477 end isRESVar;
1478
1479 public function hasMayerTermAnno
1480 "author: Vitalij Ruge
1481 Return true if variable has isMayer=true annotation"
1482 input BackendDAE.Var inVar;
1483 output Boolean outBoolean;
1484 algorithm
1485 outBoolean := match inVar
1486 local SCode.Comment comm;
1487
1488 case BackendDAE.VAR(comment=SOME(comm) )
1489 1019 then SCodeUtil.commentHasBooleanNamedAnnotation(comm, "isMayer");
1490 else false;
1491 end match;
1492 end hasMayerTermAnno;
1493
1494 public function hasOpenModelicaBoundaryConditionAnnotation
1495 "author: arun
1496 Return true if variable has __OpenModelica_BoundaryCondition=true annotation"
1497 input BackendDAE.Var inVar;
1498 output Boolean outBoolean;
1499 algorithm
1500 outBoolean := match inVar
1501 local SCode.Comment comm;
1502 7697 case BackendDAE.VAR(comment=SOME(comm)) then SCodeUtil.commentHasBooleanNamedAnnotation(comm, "__OpenModelica_BoundaryCondition");
1503 else false;
1504 end match;
1505 end hasOpenModelicaBoundaryConditionAnnotation;
1506
1507 public function hasLagrangeTermAnno
1508 "author: Vitalij Ruge
1509 Return true if variable has isLagrange=true annotation"
1510 input BackendDAE.Var inVar;
1511 output Boolean outBoolean;
1512 algorithm
1513 outBoolean := match inVar
1514 local SCode.Comment comm;
1515
1516 case BackendDAE.VAR(comment=SOME(comm) )
1517 1019 then SCodeUtil.commentHasBooleanNamedAnnotation(comm, "isLagrange");
1518 else false;
1519 end match;
1520 end hasLagrangeTermAnno;
1521
1522 public function hasConTermAnno
1523 "author: Vitalij Ruge
1524 Return true if variable has isConstraint=true annotation"
1525 input BackendDAE.Var inVar;
1526 output Boolean outBoolean;
1527 algorithm
1528 outBoolean := match inVar
1529 local SCode.Comment comm;
1530
1531 case BackendDAE.VAR(comment=SOME(comm) )
1532 1019 then SCodeUtil.commentHasBooleanNamedAnnotation(comm, "isConstraint");
1533 else false;
1534 end match;
1535 end hasConTermAnno;
1536
1537 public function hasFinalConTermAnno
1538 "author: Vitalij Ruge
1539 Return true if variable has isFinalConstraint=true annotation"
1540 input BackendDAE.Var inVar;
1541 output Boolean outBoolean;
1542 algorithm
1543 outBoolean := match inVar
1544 local SCode.Comment comm;
1545
1546 case BackendDAE.VAR(comment=SOME(comm) )
1547 1019 then SCodeUtil.commentHasBooleanNamedAnnotation(comm, "isFinalConstraint");
1548 else false;
1549 end match;
1550 end hasFinalConTermAnno;
1551
1552 public function hasTimeGridAnno
1553 "author: Vitalij Ruge
1554 Return true if variable has isTimeGrid=true annotation"
1555 input BackendDAE.Var inVar;
1556 output Boolean outBoolean;
1557 algorithm
1558 outBoolean := match inVar
1559 local SCode.Comment comm;
1560
1561 case BackendDAE.VAR(comment=SOME(comm) )
1562 584 then SCodeUtil.commentHasBooleanNamedAnnotation(comm, "isTimeGrid");
1563 else false;
1564 end match;
1565 end hasTimeGridAnno;
1566
1567
1568 public function isNonRealParam
1569 "Return true if variable is NOT a parameter of real-type"
1570 input BackendDAE.Var inVar;
1571 output Boolean outBoolean;
1572 algorithm
1573 ✗ outBoolean := not isRealParam(inVar);
1574 end isNonRealParam;
1575
1576 public function isInput
1577 "Returns true if variable is declared as input.
1578 See also is_ouput above"
1579 input BackendDAE.Var inVar;
1580 output Boolean outBoolean;
1581 algorithm
1582 outBoolean:= match inVar
1583 case BackendDAE.VAR(varDirection = DAE.INPUT()) then true;
1584 else false;
1585 end match;
1586 end isInput;
1587
1588 public function isOutputVar "Return true if variable is declared as output. Note that the output
1589 attribute sticks with a variable even if it is originating from a sub
1590 component, which is not the case for Dymola."
1591 input BackendDAE.Var inVar;
1592 output Boolean outBoolean;
1593 algorithm
1594 outBoolean := match inVar
1595 case BackendDAE.VAR(varDirection = DAE.OUTPUT()) then true;
1596 else false;
1597 end match;
1598 end isOutputVar;
1599
1600 public function isOutputAliasVar "Return true if variable is declared as output alias.
1601 introduce by preoptmodule introduceOutputAliases"
1602 input BackendDAE.Var inVar;
1603 output Boolean outBoolean;
1604 protected
1605 String s;
1606 algorithm
1607 outBoolean := match inVar
1608 ✗ case BackendDAE.VAR(varName = DAE.CREF_IDENT(ident= s)) then
1609 if StringUtil.startsWith(s, "$outputAlias") then true else false;
1610 else false;
1611 end match;
1612 end isOutputAliasVar;
1613
1614 public function isRealVar "Return true if variable is type Real"
1615 input BackendDAE.Var inVar;
1616 output Boolean outBoolean;
1617 algorithm
1618 outBoolean := match inVar
1619 case BackendDAE.VAR(varType = DAE.T_REAL()) then true;
1620 else false;
1621 end match;
1622 end isRealVar;
1623
1624 public function isRealOutputVar "Return true if variable is declared as output and type is Real. Note that the output
1625 attribute sticks with a variable even if it is originating from a sub
1626 component, which is not the case for Dymola."
1627 input BackendDAE.Var inVar;
1628 output Boolean outBoolean;
1629 algorithm
1630 outBoolean := match inVar
1631 case BackendDAE.VAR(varDirection = DAE.OUTPUT(), varType = DAE.T_REAL()) then true;
1632 else false;
1633 end match;
1634 end isRealOutputVar;
1635
1636 public function isOutput
1637 input DAE.ComponentRef inCref;
1638 input BackendDAE.Variables inVars;
1639 output Boolean outBool;
1640 algorithm
1641 outBool:=
1642 matchcontinue inVars
1643 case _ algorithm
1644 ✗ ((BackendDAE.VAR(varDirection = DAE.OUTPUT()) :: _),_) := getVar(inCref, inVars);
1645 then true;
1646
1647 else false;
1648 end matchcontinue;
1649 end isOutput;
1650
1651 public function isProtectedVar
1652 "Returns the DAE.Protected attribute"
1653 input BackendDAE.Var v;
1654 output Boolean hidden = DAEUtil.getProtectedAttr(v.values);
1655 end isProtectedVar;
1656
1657 public function isProtected
1658 "Returns the DAE.isProtected attribute."
1659 input BackendDAE.Var v;
1660 output Boolean b;
1661 algorithm
1662 b := match v.values
1663 case SOME(DAE.VAR_ATTR_REAL(isProtected=SOME(b))) then b;
1664 case SOME(DAE.VAR_ATTR_INT(isProtected=SOME(b))) then b;
1665 case SOME(DAE.VAR_ATTR_BOOL(isProtected=SOME(b))) then b;
1666 case SOME(DAE.VAR_ATTR_STRING(isProtected=SOME(b))) then b;
1667 case SOME(DAE.VAR_ATTR_ENUMERATION(isProtected=SOME(b))) then b;
1668 else false;
1669 end match;
1670 end isProtected;
1671
1672 public function hasVarEvaluateAnnotationTrueOrFinal
1673 input BackendDAE.Var inVar;
1674 output Boolean select;
1675 algorithm
1676
3/4
✓ Branch 1 taken 159 times.
✓ Branch 2 taken 371 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 159 times.
530 select := isFinalVar(inVar) or hasVarEvaluateAnnotationTrue(inVar);
1677 end hasVarEvaluateAnnotationTrueOrFinal;
1678
1679 public function hasVarEvaluateAnnotationTrueOrProtected
1680 input BackendDAE.Var inVar;
1681 output Boolean select;
1682 algorithm
1683 ✗ select := isProtectedVar(inVar) or hasVarEvaluateAnnotationTrue(inVar);
1684 end hasVarEvaluateAnnotationTrueOrProtected;
1685
1686 public function hasVarEvaluateAnnotationTrueOrFinalOrProtected
1687 input BackendDAE.Var inVar;
1688 output Boolean select;
1689 algorithm
1690
4/4
✓ Branch 1 taken 56106 times.
✓ Branch 2 taken 138362 times.
✓ Branch 4 taken 97 times.
✓ Branch 5 taken 56009 times.
194468 select := isFinalOrProtectedVar(inVar) or hasVarEvaluateAnnotationTrue(inVar);
1691 end hasVarEvaluateAnnotationTrueOrFinalOrProtected;
1692
1693 public function hasVarEvaluateAnnotation
1694 input BackendDAE.Var inVar;
1695 output Boolean select;
1696 algorithm
1697 select := match inVar
1698 local
1699 SCode.Annotation anno;
1700 // Parameter with evaluate annotation
1701 case BackendDAE.VAR(comment=SOME(SCode.COMMENT(annotation_ = SOME(anno))))
1702 ✗ then SCodeUtil.hasBooleanNamedAnnotation(anno,"Evaluate");
1703 else false;
1704 end match;
1705 end hasVarEvaluateAnnotation;
1706
1707 public function hasVarEvaluateAnnotationTrue
1708 "Returns true if var has Evaluate=true annotation"
1709 input BackendDAE.Var inVar;
1710 output Boolean isTrue;
1711 protected
1712 SCode.Annotation ann;
1713 Absyn.Exp val;
1714 algorithm
1715 try
1716
6/8
✗ Branch 0 not taken.
✓ Branch 1 taken 197048 times.
✓ Branch 2 taken 615 times.
✓ Branch 3 taken 196433 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 196433 times.
✓ Branch 6 taken 146862 times.
✓ Branch 7 taken 49571 times.
197048 BackendDAE.VAR(comment=SOME(SCode.COMMENT(annotation_ = SOME(ann)))) := inVar;
1717
3/4
✗ Branch 1 not taken.
✓ Branch 2 taken 49571 times.
✓ Branch 3 taken 34508 times.
✓ Branch 4 taken 15063 times.
49571 SOME(val) := SCodeUtil.lookupAnnotationBinding(ann, "Evaluate");
1718
2/4
✓ Branch 1 taken 15063 times.
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 15063 times.
15063 isTrue := stringEqual(Dump.printExpStr(val), "true");
1719 else
1720 isTrue := false;
1721 end try;
1722 end hasVarEvaluateAnnotationTrue;
1723
1724 public function hasVarEvaluateAnnotationFalse
1725 "Returns true if var has Evaluate=false annotation
1726 author: ptaeuber"
1727 input BackendDAE.Var inVar;
1728 output Boolean isFalse;
1729 protected
1730 SCode.Annotation ann;
1731 Absyn.Exp val;
1732 algorithm
1733 try
1734
5/8
✗ Branch 0 not taken.
✓ Branch 1 taken 10126 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10126 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 10126 times.
✓ Branch 6 taken 8291 times.
✓ Branch 7 taken 1835 times.
10126 BackendDAE.VAR(comment=SOME(SCode.COMMENT(annotation_ = SOME(ann)))) := inVar;
1735
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 1835 times.
✓ Branch 3 taken 1835 times.
✗ Branch 4 not taken.
1835 SOME(val) := SCodeUtil.lookupAnnotationBinding(ann, "Evaluate");
1736 ✗ isFalse := stringEqual(Dump.printExpStr(val), "false");
1737 else
1738 isFalse := false;
1739 end try;
1740 end hasVarEvaluateAnnotationFalse;
1741
1742 public function hasAnnotation"checks if the variable has an annotation"
1743 input BackendDAE.Var inVar;
1744 output Boolean hasAnnot;
1745 algorithm
1746 hasAnnot := match inVar
1747 case BackendDAE.VAR(comment=SOME(SCode.COMMENT(annotation_ = SOME(_)))) then true;
1748 else false;
1749 end match;
1750 end hasAnnotation;
1751
1752 public function getNamedAnnotation
1753 "Returns the value of the given annotation, or fails if the variable doesn't
1754 have the annotation."
1755 input BackendDAE.Var inVar;
1756 input String inName;
1757 output Absyn.Exp outValue;
1758 protected
1759 SCode.Annotation ann;
1760 algorithm
1761 ✗ BackendDAE.VAR(comment = SOME(SCode.COMMENT(annotation_ = SOME(ann)))) := inVar;
1762 ✗ SOME(outValue) := SCodeUtil.lookupAnnotationBinding(ann, inName);
1763 end getNamedAnnotation;
1764
1765 public function getAnnotationComment"gets the annotation comment, if there is one"
1766 input BackendDAE.Var inVar;
1767 output Option<SCode.Comment> comment;
1768 algorithm
1769 comment := match inVar
1770 local
1771 Option<SCode.Comment> com;
1772 case BackendDAE.VAR(comment=com)
1773 then com;
1774 else fail();
1775 end match;
1776 end getAnnotationComment;
1777
1778 public function createpDerVar
1779 "Creates a variable with $pDER.v as cref for jacobian variables."
1780 input BackendDAE.Var inVar;
1781 output BackendDAE.Var outVar;
1782 protected
1783 DAE.ComponentRef cr;
1784 algorithm
1785 24975 cr := varCref(inVar);
1786 24975 cr := ComponentReferenceBasics.makeCrefQual(BackendDAE.partialDerivativeNamePrefix, DAE.T_REAL_DEFAULT, {}, cr);
1787 24975 outVar := copyVarNewName(cr,inVar);
1788 24975 outVar := setVarKind(outVar,BackendDAE.JAC_TMP_VAR());
1789 end createpDerVar;
1790
1791 public function createClockedState
1792 "Creates a variable with $CLKPRE.v as cref for jacobian variables."
1793 input BackendDAE.Var inVar;
1794 output BackendDAE.Var outVar;
1795 protected
1796 DAE.ComponentRef cr;
1797 algorithm
1798 12 cr := ComponentReferenceBasics.makeCrefQual(DAE.previousNamePrefix, DAE.T_REAL_DEFAULT, {}, inVar.varName);
1799 12 outVar := copyVarNewName(cr,inVar);
1800 12 outVar := setVarKind(outVar,BackendDAE.JAC_TMP_VAR());
1801 end createClockedState;
1802
1803 public function createAliasDerVar
1804 "Creates an alias variable with the name $DER_inCref for a der-call."
1805 input DAE.ComponentRef inCref;
1806 output BackendDAE.Var outVar;
1807 protected
1808 DAE.ComponentRef cr;
1809 algorithm
1810 5257 cr := ComponentReference.prependStringCref(BackendDAE.derivativeNamePrefix, inCref);
1811 5257 outVar := BackendDAE.VAR(cr, BackendDAE.VARIABLE(),DAE.BIDIR(),DAE.NON_PARALLEL(),DAE.T_REAL_DEFAULT,NONE(),NONE(),{},
1812 DAE.emptyElementSource,
1813 NONE(),
1814 NONE(), NONE(), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), false, false, false);
1815 end createAliasDerVar;
1816
1817 public function createVar
1818 "Creates a variable with <input> as cref"
1819 input DAE.ComponentRef inCref;
1820 input String prependStringCref;
1821 output BackendDAE.Var outVar;
1822 protected
1823 DAE.ComponentRef cr;
1824 algorithm
1825 89 cr := ComponentReference.appendStringLastIdent(prependStringCref, inCref);
1826 89 outVar := makeVar(cr);
1827 end createVar;
1828
1829 public function createTmpVar
1830 "Creates a variable with <input> as cref and unique index"
1831 input DAE.ComponentRef inCref;
1832 input String prependStringCref;
1833 output BackendDAE.Var outVar;
1834 algorithm
1835 85 outVar := createVar(inCref, prependStringCref+intString(System.tmpTickIndex(Global.tmpVariableIndex)));
1836 end createTmpVar;
1837
1838 public function createCSEVar "Creates a cse variable with the name of inCref.
1839 TODO: discrete real variables are not treated correctly"
1840 input DAE.ComponentRef inCref;
1841 input DAE.Type inType;
1842 output BackendDAE.Var outVar;
1843 algorithm
1844 outVar := match inCref
1845 local
1846 DAE.ElementSource source;
1847 Absyn.Path path;
1848 BackendDAE.VarKind varKind;
1849
1850 case _ guard(ComponentReference.traverseCref(inCref, ComponentReference.crefIsRec, false)) algorithm
1851 ✗ DAE.T_COMPLEX(complexClassType=ClassInf.RECORD(path)) := inType;
1852 ✗ source := DAE.SOURCE(Absyn.dummyInfo, {}, DAE.NOCOMPPRE(), {}, {path}, {}, {});
1853 ✗ varKind := if Types.isDiscreteType(inType) then BackendDAE.DISCRETE() else BackendDAE.VARIABLE();
1854 ✗ outVar := BackendDAE.VAR(inCref, varKind, DAE.BIDIR(), DAE.NON_PARALLEL(), inType, NONE(), NONE(), {}, source, DAEUtil.setProtectedAttr(NONE(), true), SOME(BackendDAE.NEVER()), SOME(DAE.BCONST(true)), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), true,false,false);
1855 then outVar;
1856
1857 else algorithm
1858
2/2
✓ Branch 1 taken 6271 times.
✓ Branch 2 taken 70 times.
6341 varKind := if Types.isDiscreteType(inType) then BackendDAE.DISCRETE() else BackendDAE.VARIABLE();
1859 6341 outVar := BackendDAE.VAR(inCref, varKind, DAE.BIDIR(), DAE.NON_PARALLEL(), inType, NONE(), NONE(), {}, DAE.emptyElementSource, DAEUtil.setProtectedAttr(NONE(), true), SOME(BackendDAE.NEVER()), SOME(DAE.BCONST(true)), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), true,false,false);
1860 then outVar;
1861 end match;
1862 end createCSEVar;
1863
1864 public function generateVar
1865 "author: Frenkel TUD 2012-08"
1866 input DAE.ComponentRef cr;
1867 input BackendDAE.VarKind varKind;
1868 input DAE.Type varType;
1869 input DAE.InstDims subs;
1870 input Option<DAE.VariableAttributes> attr;
1871 output BackendDAE.Var var;
1872 algorithm
1873 107 var := BackendDAE.VAR(cr,varKind,DAE.BIDIR(),DAE.NON_PARALLEL(),varType,NONE(),NONE(),subs,DAE.emptyElementSource,attr,NONE(),NONE(),NONE(),DAE.NON_CONNECTOR(),DAE.NOT_INNER_OUTER(),false,false,false);
1874 end generateVar;
1875
1876 public function generateArrayVar
1877 "author: Frenkel TUD 2012-08"
1878 input DAE.ComponentRef name;
1879 input BackendDAE.VarKind varKind;
1880 input DAE.Type varType;
1881 input Option<DAE.VariableAttributes> attr;
1882 output list<BackendDAE.Var> outVars;
1883 algorithm
1884 outVars := match varType
1885 local
1886 list<DAE.ComponentRef> crlst;
1887 BackendDAE.Var var;
1888 list<BackendDAE.Var> vars;
1889 DAE.Dimensions dims;
1890 DAE.Type tp;
1891 case DAE.T_ARRAY(ty=tp,dims=dims)
1892 algorithm
1893 26 crlst := ComponentReference.expandCref(name,false);
1894 /*
1895 TODO: mahge: what is this supposed to do?.
1896 Why are even these dims needed separetely in BackendDAE.VAR
1897 They are already in the cref */
1898 /*
1899 ilst = Expression.dimensionsSizes(dims);
1900 subs = Expression.intSubscripts(ilst);
1901 */
1902 // the rest not
1903 26 vars := List.map4(crlst,generateVar,varKind,tp,dims,NONE());
1904 then
1905 vars;
1906 case _
1907 algorithm
1908 37 var := BackendDAE.VAR(name,varKind,DAE.BIDIR(),DAE.NON_PARALLEL(),varType,NONE(),NONE(),{},DAE.emptyElementSource,attr,NONE(),NONE(),NONE(),DAE.NON_CONNECTOR(),DAE.NOT_INNER_OUTER(), false, false, false);
1909 then
1910 {var};
1911 end match;
1912 end generateArrayVar;
1913
1914 public function createCSEArrayVar "Creates a cse array variable with the name of inCref.
1915 TODO: discrete real variables are not treated correctly"
1916 input DAE.ComponentRef inCref;
1917 input DAE.Type inType;
1918 input DAE.InstDims inArryDim;
1919 output BackendDAE.Var outVar;
1920 algorithm
1921 outVar := match inCref
1922 local
1923 DAE.ElementSource source;
1924 Absyn.Path path;
1925 BackendDAE.VarKind varKind;
1926
1927 case _ guard(ComponentReference.traverseCref(inCref, ComponentReference.crefIsRec, false)) algorithm
1928 ✗ DAE.T_COMPLEX(complexClassType=ClassInf.RECORD(path)) := inType;
1929 ✗ source := DAE.SOURCE(Absyn.dummyInfo, {}, DAE.NOCOMPPRE(), {}, {path}, {}, {});
1930 ✗ varKind := if Types.isDiscreteType(inType) then BackendDAE.DISCRETE() else BackendDAE.VARIABLE();
1931 ✗ outVar := BackendDAE.VAR(inCref, varKind, DAE.BIDIR(), DAE.NON_PARALLEL(), inType, NONE(), NONE(), inArryDim, source, DAEUtil.setProtectedAttr(NONE(), true), SOME(BackendDAE.NEVER()), SOME(DAE.BCONST(true)), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), true, false, false);
1932 then outVar;
1933
1934 else algorithm
1935
2/2
✓ Branch 1 taken 7232 times.
✓ Branch 2 taken 613 times.
7845 varKind := if Types.isDiscreteType(inType) then BackendDAE.DISCRETE() else BackendDAE.VARIABLE();
1936 7845 outVar := BackendDAE.VAR(inCref, varKind, DAE.BIDIR(), DAE.NON_PARALLEL(), inType, NONE(), NONE(), inArryDim, DAE.emptyElementSource, DAEUtil.setProtectedAttr(NONE(), true), SOME(BackendDAE.NEVER()), SOME(DAE.BCONST(true)), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), true, false, false);
1937 then outVar;
1938 end match;
1939 end createCSEArrayVar;
1940
1941 public function copyVarNewName "author: Frenkel TUD 2012-5
1942 Create variable with new name as cref from other var."
1943 input DAE.ComponentRef cr;
1944 input BackendDAE.Var inVar;
1945 output BackendDAE.Var outVar=inVar;
1946 algorithm
1947 272239 outVar.varName := cr;
1948 end copyVarNewName;
1949
1950 public function setVarKindForVar"updates the varkind for an indexed var inside the variable-array.
1951 author:Waurich TUD 2015-02"
1952 input Integer idx;
1953 input BackendDAE.VarKind kind;
1954 input BackendDAE.Variables varsIn;
1955 output BackendDAE.Variables varsOut;
1956 protected
1957 BackendDAE.Var var;
1958 algorithm
1959 4896 var := getVarAt(varsIn,idx);
1960 4896 var := setVarKind(var,kind);
1961 4896 varsOut := setVarAt(varsIn,idx,var);
1962 end setVarKindForVar;
1963
1964 public function setVarsKind "author: lochel
1965 This function sets the BackendDAE.VarKind of a variable-list."
1966 input list<BackendDAE.Var> inVars;
1967 input BackendDAE.VarKind inVarKind;
1968 output list<BackendDAE.Var> outVars;
1969 algorithm
1970 ✗ outVars := List.map1(inVars,setVarKind,inVarKind);
1971 end setVarsKind;
1972
1973 public function setVarKind "author: PA
1974 Sets the BackendDAE.VarKind of a variable"
1975 input BackendDAE.Var inVar;
1976 input BackendDAE.VarKind inVarKind;
1977 output BackendDAE.Var outVar = inVar;
1978 algorithm
1979 214600 outVar.varKind := inVarKind;
1980 // kabdelhak: state select always variables cannot be dummy states
1981 // not modelica compliant, but only throw warnings
1982 // ticket #3689
1983
3/4
✓ Branch 1 taken 205634 times.
✓ Branch 2 taken 8966 times.
✓ Branch 4 taken 8966 times.
✗ Branch 5 not taken.
214600 if isDummyStateVar(outVar) and varStateSelectAlways(outVar) then
1984 ✗ Error.addMessage(Error.NON_STATE_STATESELECT_ALWAYS, {ComponentReference.crefStr(BackendVariable.varCref(outVar))});
1985 end if;
1986 end setVarKind;
1987
1988 public function setVarTS "Sets the BackendDAE.TearingSelect of a variable"
1989 input BackendDAE.Var inVar;
1990 input Option<BackendDAE.TearingSelect> inTS;
1991 output BackendDAE.Var outVar = inVar;
1992 algorithm
1993 356 outVar.tearingSelectOption := inTS;
1994 // referenceUpdate(inVar, 2, new_kind);
1995 end setVarTS;
1996
1997
1998 public function setBindExp "author: lochel"
1999 input BackendDAE.Var inVar;
2000 input Option<DAE.Exp> inBindExp;
2001 output BackendDAE.Var outVar = inVar;
2002 algorithm
2003 571534 outVar.bindExp := inBindExp;
2004 end setBindExp;
2005
2006
2007 public function setHideResult "Sets BackendDAE.VAR.hideResult expression.
2008 author: vwaurich 10 2016"
2009 input BackendDAE.Var varIn;
2010 input Option<DAE.Exp> hideResultB;
2011 output BackendDAE.Var varOut=varIn;
2012 algorithm
2013 1338 varOut.hideResult := hideResultB;
2014 end setHideResult;
2015
2016 public function setVarDirectionTpl
2017 input output BackendDAE.Var var;
2018 input output DAE.VarDirection dir;
2019 algorithm
2020
1/2
✓ Branch 0 taken 6011 times.
✗ Branch 1 not taken.
6011 var.varDirection := dir;
2021 end setVarDirectionTpl;
2022
2023 public function setVarDirection "author: lochel
2024 Sets the DAE.VarDirection of a variable"
2025 input BackendDAE.Var inVar;
2026 input DAE.VarDirection inVarDirection;
2027 output BackendDAE.Var outVar = inVar;
2028 algorithm
2029 208952 outVar.varDirection := inVarDirection; // referenceUpdate(inVar, 3, varDirection);
2030 end setVarDirection;
2031
2032 public function getVarDirection "Get the DAE.VarDirection of a variable"
2033 input BackendDAE.Var inVar;
2034 output DAE.VarDirection varDirection = inVar.varDirection;
2035 end getVarDirection;
2036
2037 public function getVarNominalValue "
2038 Returns the DAE.NominalValue or default value of a variable."
2039 input BackendDAE.Var InVar;
2040 output DAE.Exp nom = DAEUtil.getNominalAttr(InVar.values);
2041 end getVarNominalValue;
2042
2043 public function getVarKind "
2044 Get the DAE.VarKind of a variable"
2045 input BackendDAE.Var inVar;
2046 output BackendDAE.VarKind varKind = inVar.varKind;
2047 end getVarKind;
2048
2049 public function getVarKindForVar"fetch the varkind for an indexed var inside the variable-array."
2050 input Integer idx;
2051 input BackendDAE.Variables varsIn;
2052 output BackendDAE.VarKind kind;
2053 protected
2054 BackendDAE.Var var;
2055 algorithm
2056 ✗ var := getVarAt(varsIn,idx);
2057 ✗ kind := getVarKind(var);
2058 end getVarKindForVar;
2059
2060 public function isVarOnTopLevelAndOutput "has the DAE.VarDirection = OUTPUT
2061 Don't check for top level here as this is done by NFConvertDAE.makeDAEVar.
2062 Otherwise the list of model variables may contradict with model structure."
2063 input BackendDAE.Var inVar;
2064 //output Boolean outBoolean = DAEUtil.topLevelOutput(inVar.varName, inVar.varDirection, inVar.connectorType);
2065 output Boolean outBoolean = isOutputVar(inVar);
2066 end isVarOnTopLevelAndOutput;
2067
2068 public function isVarOnTopLevelAndInput "has the DAE.VarDirection = INPUT
2069 Don't check for top level here as this is done by NFConvertDAE.makeDAEVar.
2070 Otherwise the list of model variables may contradict with model structure."
2071 input BackendDAE.Var inVar;
2072 //output Boolean outBoolean = DAEUtil.topLevelInput(inVar.varName, inVar.varDirection, inVar.connectorType);
2073 output Boolean outBoolean = isInput(inVar);
2074 end isVarOnTopLevelAndInput;
2075
2076 public function isVarOnTopLevelAndInputNoDerInput
2077 input BackendDAE.Var inVar;
2078 output Boolean outBoolean = isVarOnTopLevelAndInput(inVar) and not isRealOptimizeDerInput(inVar);
2079 end isVarOnTopLevelAndInputNoDerInput;
2080
2081 public function isFinalVar "Returns true if the variable is final."
2082 input BackendDAE.Var inVar;
2083 output Boolean b = DAEUtil.getFinalAttr(inVar.values);
2084 end isFinalVar;
2085
2086 public function isFinalOrProtectedVar
2087 input BackendDAE.Var inVar;
2088 output Boolean b = isFinalVar(inVar) or isProtectedVar(inVar);
2089 end isFinalOrProtectedVar;
2090
2091 public function isChangeable "Returns true if the variable should be changeable after compilation (e.g. in OMEdit)"
2092 input BackendDAE.Var v;
2093 output Boolean isValueChangeable = isVarOnTopLevelAndInput(v) or (
2094 varFixed(v) and not hasVarEvaluateAnnotationTrueOrFinalOrProtected(v)
2095 // FIXME currently bindings or start attributes can be overwritten only when they are constant
2096 and (if isParam(v) then varHasConstantBindExp(v) or (not varHasBindExp(v) and varHasConstantStartExp(v))
2097 else varHasConstantStartExp(v)));
2098 end isChangeable;
2099
2100 public function getVariableAttributes "Returns the DAE.VariableAttributes of a variable."
2101 input BackendDAE.Var inVar;
2102 output Option<DAE.VariableAttributes> outAttr = inVar.values;
2103 end getVariableAttributes;
2104
2105 public function getVarSource "Returns the DAE.ElementSource of a variable"
2106 input BackendDAE.Var inVar;
2107 output DAE.ElementSource outSource = inVar.source;
2108 end getVarSource;
2109
2110 public function getVarType "author: marcusw
2111 returns the BackendDAE.Type of the variable"
2112 input BackendDAE.Var inVar;
2113 output BackendDAE.Type outType = inVar.varType;
2114 end getVarType;
2115
2116 public function getMinMaxAsserts "author: Frenkel TUD 2011-03"
2117 input BackendDAE.Var inVar;
2118 input list<DAE.Algorithm> inAsserts = {};
2119 output BackendDAE.Var outVar = inVar;
2120 output list<DAE.Algorithm> outAsserts;
2121 algorithm
2122 outAsserts := matchcontinue inVar
2123 local
2124 DAE.Exp e, cond, msg, level;
2125 Option<DAE.Exp> min, max;
2126 String str, format;
2127 DAE.Type tp;
2128 DAE.ComponentRef name;
2129 Option<DAE.VariableAttributes> attr;
2130 BackendDAE.Type varType;
2131 DAE.ElementSource source;
2132
2133 case BackendDAE.VAR(varKind=BackendDAE.CONST())
2134 then inAsserts;
2135
2136 case BackendDAE.VAR(varName=name, values=attr, varType=varType, source=source) algorithm
2137 195081 (min, max) := DAEUtil.getMinMaxValues(attr);
2138
6/8
✗ Branch 0 not taken.
✓ Branch 1 taken 195081 times.
✓ Branch 2 taken 164259 times.
✓ Branch 3 taken 30822 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 164259 times.
✓ Branch 6 taken 164254 times.
✓ Branch 7 taken 5 times.
195081 if isNone(min) and isNone(max) then
2139 164254 fail();
2140 end if;
2141 30827 e := Expression.crefExp(name);
2142 30827 tp := BackendDAEUtil.makeExpType(varType);
2143
2144 // do not add if const true
2145 30827 cond := getMinMaxAsserts1(min, max, e, tp);
2146 30827 (cond, _) := ExpressionSimplify.simplify(cond);
2147
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30827 times.
30827 false := Expression.isConstTrue(cond);
2148 30827 str := getMinMaxAsserts1Str(min, max, ComponentReferenceBasics.printComponentRefStr(name));
2149
2150
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30827 times.
30827 if Flags.isSet(Flags.WARNING_MINMAX_ATTRIBUTES) then
2151 level := DAE.ASSERTIONLEVEL_WARNING;
2152 else
2153 level := DAE.ASSERTIONLEVEL_ERROR;
2154 end if;
2155
2156 // if is real use %g otherwise use %d (ints and enums)
2157
2/2
✓ Branch 1 taken 6469 times.
✓ Branch 2 taken 24358 times.
30827 format := if Types.isRealOrSubTypeReal(tp) then "g" else "d";
2158 61654 msg := DAE.BINARY(DAE.SCONST(str), DAE.ADD(DAE.T_STRING_DEFAULT), DAE.CALL(Absyn.IDENT("String"), {e, DAE.SCONST(format)}, DAE.callAttrBuiltinString));
2159 30827 BackendDAEUtil.checkAssertCondition(cond, msg, level, ElementSource.getElementSourceFileInfo(source));
2160 61654 then DAE.ALGORITHM_STMTS({DAE.STMT_ASSERT(cond, msg, level, source)})::inAsserts;
2161
2162 else inAsserts;
2163 end matchcontinue;
2164 end getMinMaxAsserts;
2165
2166 protected function getMinMaxAsserts1 "author: Frenkel TUD 2011-03"
2167 input Option<DAE.Exp> omin,omax;
2168 input DAE.Exp e;
2169 input DAE.Type tp;
2170 output DAE.Exp cond;
2171 algorithm
2172 cond := match (omin,omax)
2173 local
2174 DAE.Exp min, max;
2175
2176 case (SOME(min),SOME(max))
2177 12911 then DAE.LBINARY(DAE.RELATION(e, DAE.GREATEREQ(tp), min, -1, NONE()), DAE.AND(DAE.T_BOOL_DEFAULT), DAE.RELATION(e, DAE.LESSEQ(tp), max, -1, NONE()));
2178
2179 case (SOME(min),NONE())
2180 17911 then DAE.RELATION(e, DAE.GREATEREQ(tp), min, -1, NONE());
2181
2182 case (NONE(),SOME(max))
2183 5 then DAE.RELATION(e, DAE.LESSEQ(tp), max, -1, NONE());
2184 end match;
2185 end getMinMaxAsserts1;
2186
2187 protected function getMinMaxAsserts1Str "author: Frenkel TUD 2011-03"
2188 input Option<DAE.Exp> omin,omax;
2189 input String varStr;
2190 output String msg;
2191 algorithm
2192 msg := match (omin,omax)
2193 local
2194 DAE.Exp min, max;
2195
2196 case (SOME(min),SOME(max))
2197 12911 then "Variable violating min/max constraint: " + ExpressionBasics.printExpStr(min) + " <= " + varStr + " <= " + ExpressionBasics.printExpStr(max) + ", has value: ";
2198
2199 case (SOME(min),NONE())
2200 17911 then "Variable violating min constraint: " + ExpressionBasics.printExpStr(min) + " <= " + varStr + ", has value: ";
2201
2202 case (NONE(),SOME(max))
2203 5 then "Variable violating max constraint: " + varStr + " <= " + ExpressionBasics.printExpStr(max) + ", has value: ";
2204 end match;
2205 end getMinMaxAsserts1Str;
2206
2207 public function varSortFunc "A sorting function (greatherThan) for Variables based on crefs"
2208 input BackendDAE.Var v1;
2209 input BackendDAE.Var v2;
2210 output Boolean greaterThan;
2211 algorithm
2212 ✗ greaterThan := ComponentReferenceBasics.crefSortFunc(varCref(v1), varCref(v2));
2213 end varSortFunc;
2214
2215 public function sortInitialVars
2216 "author:kabdelhak 2021-9
2217 Sorts fixables to be at the end of the array, also prefers variables with start values"
2218 input output BackendDAE.Variables vars;
2219 input BackendDAE.Variables fixableVars;
2220 protected
2221 list<BackendDAE.Var> var_lst, fixable_start, fixable, non_fixable;
2222 algorithm
2223 47540 var_lst := varList(vars);
2224 47540 (fixable, non_fixable) := List.splitOnTrue(var_lst, function containsVar(inVariables = fixableVars));
2225 47540 (fixable_start, fixable) := List.splitOnTrue(fixable, varHasStartValue);
2226 47540 var_lst := listAppend(listAppend(fixable_start, listReverse(fixable)), listReverse(non_fixable));
2227 47540 vars := listVar(var_lst);
2228 end sortInitialVars;
2229
2230 public function getAlias
2231 " author: Frenkel TUD 2012-11
2232 returns the original Varname of an AliasVar"
2233 input BackendDAE.Var inVar;
2234 output DAE.ComponentRef outCr;
2235 output Boolean negated;
2236 protected
2237 DAE.Exp e;
2238 algorithm
2239 ✗ e := varBindExp(inVar);
2240 ✗ (outCr,negated) := getAlias1(e);
2241 end getAlias;
2242
2243 protected function getAlias1
2244 input DAE.Exp inExp;
2245 output DAE.ComponentRef outCr;
2246 output Boolean negated;
2247 algorithm
2248 (outCr,negated) :=
2249 match inExp
2250 local
2251 DAE.ComponentRef name;
2252
2253 case DAE.CREF(componentRef=name) then (name, false);
2254 case DAE.UNARY(operator=DAE.UMINUS(_),exp=DAE.CREF(componentRef=name)) then (name,true);
2255 case DAE.UNARY(operator=DAE.UMINUS_ARR(_),exp=DAE.CREF(componentRef=name)) then (name,true);
2256 case DAE.LUNARY(operator=DAE.NOT(_),exp=DAE.CREF(componentRef=name)) then (name,true);
2257 case DAE.CALL(path=Absyn.IDENT(name = "der"), expLst={DAE.CREF(componentRef=name)})
2258 algorithm
2259 ✗ name := ComponentReference.crefPrefixDer(name);
2260 then (name, false);
2261 case DAE.UNARY(operator=DAE.UMINUS(_),exp=DAE.CALL(path=Absyn.IDENT(name = "der"), expLst={DAE.CREF(componentRef=name)}))
2262 algorithm
2263 ✗ name := ComponentReference.crefPrefixDer(name);
2264 then (name,true);
2265 case DAE.UNARY(operator=DAE.UMINUS_ARR(_),exp=DAE.CALL(path=Absyn.IDENT(name = "der"), expLst={DAE.CREF(componentRef=name)}))
2266 algorithm
2267 ✗ name := ComponentReference.crefPrefixDer(name);
2268 then (name,true);
2269 end match;
2270 end getAlias1;
2271
2272 public function daenumVariables
2273 input BackendDAE.EqSystem syst;
2274 output Integer n;
2275 protected
2276 BackendDAE.Variables vars;
2277 algorithm
2278 188987 vars := daeVars(syst);
2279 188987 n := varsSize(vars);
2280 end daenumVariables;
2281
2282 /* =======================================================
2283 *
2284 * Section for functions that deals with VariablesArray
2285 *
2286 * =======================================================
2287 */
2288
2289 protected function copyArray
2290 "Makes a copy of a variable array."
2291 input BackendDAE.VariableArray inVariableArray;
2292 output BackendDAE.VariableArray outVariableArray = inVariableArray;
2293 algorithm
2294 38546 outVariableArray.varOptArr := arrayCopy(inVariableArray.varOptArr);
2295 end copyArray;
2296
2297 protected function vararrayEmpty
2298 input Integer inSize;
2299 output BackendDAE.VariableArray outArray;
2300 protected
2301 array<Option<BackendDAE.Var>> arr;
2302 algorithm
2303 249344 arr := arrayCreate(inSize, NONE());
2304 249344 outArray := BackendDAE.VARIABLE_ARRAY(0, arr);
2305 end vararrayEmpty;
2306
2307 protected function vararrayAdd
2308 "author: PA
2309 Adds a variable last to the BackendDAE.VariableArray, increasing array size
2310 if no space left by factor 1.4"
2311 input BackendDAE.VariableArray inVariableArray;
2312 input BackendDAE.Var inVar;
2313 output BackendDAE.VariableArray outVariableArray;
2314 protected
2315 Integer num_elems;
2316 array<Option<BackendDAE.Var>> arr;
2317 algorithm
2318 3792604 BackendDAE.VARIABLE_ARRAY(num_elems, arr) := inVariableArray;
2319 3792604 num_elems := num_elems + 1;
2320 3792604 arr := Array.expandOnDemand(num_elems, arr, 1.4, NONE());
2321 3792604 arrayUpdate(arr, num_elems, SOME(inVar));
2322 3792604 outVariableArray := BackendDAE.VARIABLE_ARRAY(num_elems, arr);
2323 end vararrayAdd;
2324
2325 protected function vararraySetnth
2326 "Sets the n:th variable in the array."
2327 input BackendDAE.VariableArray inVariableArray;
2328 input Integer inIndex;
2329 input BackendDAE.Var inVar;
2330 output BackendDAE.VariableArray outVariableArray = inVariableArray;
2331 algorithm
2332
2/2
✓ Branch 0 taken 272 times.
✓ Branch 1 taken 316025 times.
316297 true := inIndex <= inVariableArray.numberOfElements;
2333 316025 arrayUpdate(inVariableArray.varOptArr, inIndex, SOME(inVar));
2334 end vararraySetnth;
2335
2336 protected function vararrayNth
2337 "Returns the n:th variable in the array."
2338 input BackendDAE.VariableArray inVariableArray;
2339 input Integer inIndex;
2340 output BackendDAE.Var outVar;
2341 algorithm
2342
2/2
✓ Branch 0 taken 8414 times.
✓ Branch 1 taken 18250804 times.
18259218 true := inIndex <= inVariableArray.numberOfElements;
2343
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 18250804 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 18250804 times.
18250804 SOME(outVar) := arrayGet(inVariableArray.varOptArr, inIndex);
2344 end vararrayNth;
2345
2346 protected function vararrayDelete
2347 input BackendDAE.VariableArray inVariableArray;
2348 input Integer inIndex;
2349 output BackendDAE.VariableArray outVariableArray = inVariableArray;
2350 output BackendDAE.Var outVar;
2351 algorithm
2352
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 162693 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 162693 times.
162693 SOME(outVar) := arrayGet(outVariableArray.varOptArr, inIndex);
2353 162693 arrayUpdate(outVariableArray.varOptArr, inIndex, NONE());
2354 end vararrayDelete;
2355
2356 protected function vararrayList
2357 "Returns a list of all the variables in the variable array."
2358 input BackendDAE.VariableArray inArray;
2359 output list<BackendDAE.Var> outVars;
2360 protected
2361 array<Option<BackendDAE.Var>> varOptArr;
2362 algorithm
2363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112561 times.
112561 BackendDAE.VARIABLE_ARRAY(varOptArr=varOptArr) := inArray;
2364 outVars := {};
2365
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112561 times.
73988715 for i in arrayLength(varOptArr):-1:1 loop
2366
3/4
✗ Branch 1 not taken.
✓ Branch 2 taken 73876154 times.
✓ Branch 3 taken 2279933 times.
✓ Branch 4 taken 71596221 times.
73876154 if isSome(varOptArr[i]) then
2367 2279933 outVars := Util.getOption(varOptArr[i])::outVars;
2368 end if;
2369 end for;
2370 end vararrayList;
2371
2372 /* =======================================================
2373 *
2374 * Section for functions that deals with Variables
2375 *
2376 * =======================================================
2377 */
2378
2379 public function copyVariables
2380 "Makes a copy of a Variables."
2381 input BackendDAE.Variables inVariables;
2382 output BackendDAE.Variables outVariables;
2383 algorithm
2384 outVariables := inVariables;
2385
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 38546 times.
77092 outVariables.crefIndices := arrayCopy(inVariables.crefIndices);
2386 outVariables.prefixIndices := arrayCreate(1, arrayCopy(arrayGet(inVariables.prefixIndices, 1)));
2387 outVariables.varArr := copyArray(inVariables.varArr);
2388 end copyVariables;
2389
2390 public function emptyVars
2391 "Creates a new empty Variable structure."
2392 input Integer inSize = BaseHashTable.bigBucketSize;
2393 output BackendDAE.Variables outVariables;
2394 protected
2395 array<list<BackendDAE.CrefIndex>> indices;
2396 Integer buckets, arr_size;
2397 BackendDAE.VariableArray arr;
2398 algorithm
2399 arr_size := max(inSize, BaseHashTable.lowBucketSize);
2400 buckets := bucketCount(arr_size);
2401 249344 indices := arrayCreate(buckets, {});
2402 249344 arr := vararrayEmpty(arr_size);
2403 249344 outVariables := BackendDAE.VARIABLES(indices, arrayCreate(1, arrayCreate(0, {})), arr, buckets, 0, false);
2404 end emptyVars;
2405
2406 protected function bucketCount
2407 input Integer numVars;
2408 output Integer buckets = realInt(intReal(max(numVars, BaseHashTable.lowBucketSize)) * 1.4);
2409 end bucketCount;
2410
2411 protected function growBuckets
2412 "Rebuilds the cref index with buckets for twice the current number of
2413 variables and drops the prefix index; the variable array is kept as it is."
2414 input output BackendDAE.Variables vars;
2415 protected
2416 array<list<BackendDAE.CrefIndex>> indices;
2417 Integer buckets, idx;
2418 BackendDAE.Var v;
2419 algorithm
2420 68 buckets := bucketCount(2 * vars.numberOfVars);
2421 68 indices := arrayCreate(buckets, {});
2422 68 vars.crefIndices := indices;
2423 vars.prefixIndices := arrayCreate(1, arrayCreate(0, {}));
2424 vars.bucketSize := buckets;
2425
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
89387 for i in 1:vars.varArr.numberOfElements loop
2426
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 89319 times.
✓ Branch 3 taken 89319 times.
✗ Branch 4 not taken.
89319 if isSome(vars.varArr.varOptArr[i]) then
2427 89319 SOME(v) := vars.varArr.varOptArr[i];
2428 89319 idx := intMod(ComponentReferenceBasics.hashComponentRef(v.varName), buckets) + 1;
2429 178638 arrayUpdate(indices, idx, BackendDAE.CREFINDEX(v.varName, i - 1) :: indices[idx]);
2430 end if;
2431 end for;
2432 end growBuckets;
2433
2434 public function emptyVarsSized
2435 "Returns a BackendDAE.Variables data structure that is empty."
2436 input Integer size;
2437 output BackendDAE.Variables outVariables = emptyVars(size);
2438 end emptyVarsSized;
2439
2440 public function isCrefInVarList "O(n)"
2441 input DAE.ComponentRef inCref;
2442 input list<BackendDAE.Var> inVars;
2443 output Boolean isInList = false;
2444 algorithm
2445 ✗ for v in inVars loop
2446 ✗ if ComponentReferenceBasics.crefEqual(BackendVariable.varCref(v), inCref) then
2447 isInList := true;
2448 ✗ return;
2449 end if;
2450 end for;
2451 end isCrefInVarList;
2452
2453 public function areAllCrefsInVarList "O(n^2)"
2454 input list<DAE.ComponentRef> inCrefs;
2455 input list<BackendDAE.Var> inVars;
2456 output Boolean isInList = true;
2457 algorithm
2458 ✗ for cref in inCrefs loop
2459 ✗ if not isCrefInVarList(cref, inVars) then
2460 isInList := false;
2461 ✗ return;
2462 end if;
2463 end for;
2464 end areAllCrefsInVarList;
2465
2466 public function areAllCrefsPrimaryParameters
2467 input list<DAE.ComponentRef> inCrefs;
2468 input BackendDAE.Variables inVars;
2469 output Boolean isPrimary = true;
2470 protected
2471 BackendDAE.Var v;
2472 algorithm
2473
2/2
✓ Branch 0 taken 6154 times.
✓ Branch 1 taken 23241 times.
29395 for cref in inCrefs loop
2474 try
2475 6154 (v, _) := getVar2(cref, inVars);
2476
3/4
✓ Branch 1 taken 6103 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6054 times.
✓ Branch 5 taken 49 times.
6103 true := isParam(v) and varFixed(v);
2477 else
2478 isPrimary := false;
2479 100 return;
2480 end try;
2481 end for;
2482 end areAllCrefsPrimaryParameters;
2483
2484 public function varList
2485 "Takes a BackendDAE.Variables and returns a list of all variables in it,
2486 useful for e.g. dumping.
2487
2488 NOTE: This function will fail if the Variables contains more than one set.
2489 This is because mergeVariables doesn't do a real merging, so if we just
2490 append all the variables from the sets we might get duplicates."
2491 input BackendDAE.Variables inVariables;
2492 output list<BackendDAE.Var> outVarLst;
2493 algorithm
2494 112561 outVarLst := vararrayList(inVariables.varArr);
2495 end varList;
2496
2497 public function listVar
2498 "author: PA
2499 Takes Var list and creates a BackendDAE.Variables structure, see also var_list."
2500 input list<BackendDAE.Var> inVarLst;
2501 output BackendDAE.Variables outVariables;
2502 protected
2503 Integer size;
2504 algorithm
2505 67633 size := listLength(inVarLst);
2506 67633 outVariables := emptyVarsSized(size);
2507 67633 outVariables := addVars(listReverse(inVarLst), outVariables);
2508 end listVar;
2509
2510 public function listVarSized "author: Frenkel TUD 2012-05
2511 Takes BackendDAE.Var list and creates a BackendDAE.Variables structure, see also var_list."
2512 input list<BackendDAE.Var> inVarLst;
2513 input Integer size;
2514 output BackendDAE.Variables outVariables;
2515 algorithm
2516 ✗ outVariables := List.fold(inVarLst,addVar,emptyVarsSized(size));
2517 end listVarSized;
2518
2519 public function listVar1 "author: Frenkel TUD 2012-05
2520 ToDo: replace all listVar calls with this function, tailrecursive implementation
2521 Takes BackendDAE.Var list and creates a BackendDAE.Variables structure, see also var_list."
2522 input list<BackendDAE.Var> inVarLst;
2523 output BackendDAE.Variables outVariables;
2524 protected
2525 Integer size;
2526 algorithm
2527 109361 size := listLength(inVarLst);
2528 109361 outVariables := List.fold(inVarLst,addVar,emptyVarsSized(size));
2529 end listVar1;
2530
2531 public function listVar2 "author: Frenkel TUD 2012-05
2532 ToDo: replace all listVar calls with this function, tailrecursive implementation
2533 Takes BackendDAE.Var list and creates a BackendDAE.Variables structure, see also var_list."
2534 input list<BackendDAE.Var> inVarLst1,inVarLst2;
2535 output BackendDAE.Variables outVariables;
2536 protected
2537 Integer size;
2538 algorithm
2539 2293 size := listLength(inVarLst1)+listLength(inVarLst2);
2540 2293 outVariables := List.fold(inVarLst2,addVar,List.fold(inVarLst1,addVar,emptyVarsSized(size)));
2541 end listVar2;
2542
2543 public function equationSystemsVarsLst
2544 input BackendDAE.EqSystems systs;
2545 output list<BackendDAE.Var> outVars = {};
2546 protected
2547 list<BackendDAE.Var> vars;
2548 BackendDAE.Variables v;
2549 algorithm
2550
2/2
✓ Branch 0 taken 6037 times.
✓ Branch 1 taken 2932 times.
8969 for es in systs loop
2551 6037 BackendDAE.EQSYSTEM(orderedVars=v) := es;
2552 6037 vars := varList(v);
2553 6037 outVars := List.append_reverse(vars, outVars);
2554 end for;
2555 2932 outVars := MetaModelica.Dangerous.listReverseInPlace(outVars);
2556 end equationSystemsVarsLst;
2557
2558 public function daeVars "returns orderedVars"
2559 input BackendDAE.EqSystem inEqSystem;
2560 output BackendDAE.Variables vars = inEqSystem.orderedVars;
2561 end daeVars;
2562
2563 public function daeGlobalKnownVars
2564 input BackendDAE.Shared inShared;
2565 output BackendDAE.Variables outGlobalKnownVars = inShared.globalKnownVars;
2566 end daeGlobalKnownVars;
2567
2568 public function daeAliasVars
2569 input BackendDAE.Shared inShared;
2570 output BackendDAE.Variables outAliasVars = inShared.aliasVars;
2571 end daeAliasVars;
2572
2573 public function varsSize
2574 "Returns the number of variables in the Variables structure."
2575 input BackendDAE.Variables inVariables;
2576 output Integer outNumVariables = inVariables.varArr.numberOfElements;
2577 end varsSize;
2578
2579 /*
2580 public function varDim
2581 "Returns the dimension of variables in the Variables structure.
2582 NOTE: function fail if dimension is not constant
2583 "
2584 input BackendDAE.Var inVar;
2585 output Integer outDimVariables = 1;
2586 protected
2587 DAE.Dimensions dims;
2588 Integer n;
2589 algorithm
2590 BackendDAE.VAR(arryDim=dims) := inVar;
2591 for dim in dims loop
2592 DAE.DIM_INTEGER(n) := dim;
2593 outDimVariables := n * outDimVariables;
2594 end for;
2595 end varDim;
2596 */
2597
2598 protected function varsLoadFactor
2599 input BackendDAE.Variables inVariables;
2600 input Integer inIncrease = 0;
2601 output Real outLoadFactor;
2602 algorithm
2603
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4813 times.
4813 outLoadFactor := intReal(inVariables.numberOfVars + inIncrease) / intReal(inVariables.bucketSize);
2604 end varsLoadFactor;
2605
2606 public function isVariable
2607 "
2608 This function takes a DAE.ComponentRef and two Variables. It searches
2609 the two sets of variables and succeed if the variable is STATE or
2610 VARIABLE. Otherwise it fails.
2611 Note: An array variable is currently assumed that each scalar element has
2612 the same type.
2613 inputs: (DAE.ComponentRef,
2614 Variables, /* vars */
2615 Variables) /* known vars */
2616 outputs: ()"
2617 input DAE.ComponentRef inComponentRef1;
2618 input BackendDAE.Variables inVariables2;
2619 input BackendDAE.Variables inVariables3;
2620 algorithm
2621 ():=
2622 matchcontinue (inComponentRef1, inVariables2, inVariables3)
2623 local
2624 DAE.ComponentRef cr;
2625 BackendDAE.Variables vars, globalKnownVars;
2626 BackendDAE.VarKind kind;
2627
2628 case (cr, vars, _) algorithm
2629 ✗ ((BackendDAE.VAR(varKind=kind)::_), _) := getVar(cr, vars);
2630 ✗ isVarKindVariable(kind);
2631 then ();
2632
2633 case (cr, _, globalKnownVars) algorithm
2634 ✗ ((BackendDAE.VAR(varKind = kind) :: _), _) := getVar(cr, globalKnownVars);
2635 ✗ isVarKindVariable(kind);
2636 then ();
2637 end matchcontinue;
2638 end isVariable;
2639
2640 public function isVarKindVariable "This function takes a DAE.ComponentRef and two Variables. It searches
2641 the two sets of variables and succeed if the variable is STATE or
2642 VARIABLE. Otherwise it fails.
2643 Note: An array variable is currently assumed that each scalar element has
2644 the same type.
2645 inputs: (DAE.ComponentRef,
2646 Variables, /* vars */
2647 Variables) /* known vars */
2648 outputs: ()"
2649 input BackendDAE.VarKind inVarKind;
2650 algorithm
2651 ():=
2652 match inVarKind
2653 case BackendDAE.VARIABLE() then ();
2654 case BackendDAE.STATE() then ();
2655 case BackendDAE.DUMMY_STATE() then ();
2656 case BackendDAE.DUMMY_DER() then ();
2657 case BackendDAE.DISCRETE() then ();
2658 end match;
2659 end isVarKindVariable;
2660
2661 public function isVarKindState
2662 input BackendDAE.VarKind inVarKind;
2663 output Boolean result;
2664 algorithm
2665 result :=
2666 match inVarKind
2667 case BackendDAE.STATE() then true;
2668 else false;
2669 end match;
2670 end isVarKindState;
2671
2672 public function isTopLevelInputOrOutput "author: LP
2673
2674 This function checks if the provided cr is from a var that is on top model
2675 and is an input or an output, and returns true for such variables.
2676 It also returns true for input/output connector variables, i.e. variables
2677 instantiated from a connector class, that are instantiated on the top level.
2678 The check for top-model is done by spliting the name at \'.\' and
2679 check if the list-length is 1.
2680 Note: The function needs the known variables to search for input variables
2681 on the top level.
2682 inputs: (cref: DAE.ComponentRef,
2683 vars: Variables, /* BackendDAE.Variables */
2684 globalKnownVars: BackendDAE.Variables /* Known BackendDAE.Variables */)
2685 outputs: bool"
2686 input DAE.ComponentRef inComponentRef;
2687 input BackendDAE.Variables inVars;
2688 input BackendDAE.Variables inGlobalKnownVars;
2689 output Boolean outBoolean;
2690 algorithm
2691 outBoolean := matchcontinue inComponentRef
2692 local
2693 BackendDAE.Var v;
2694 case _
2695 ✗ algorithm (v::_, _) := getVar(inComponentRef, inVars);
2696 ✗ then isVarOnTopLevelAndOutput(v);
2697 case _
2698 ✗ algorithm (v::_, _) := getVar(inComponentRef, inGlobalKnownVars);
2699 ✗ then isVarOnTopLevelAndInput(v);
2700 else false;
2701 end matchcontinue;
2702 end isTopLevelInputOrOutput;
2703
2704 public function deleteCrefs "author: wbraun
2705 Removes a list of DAE.ComponentRef from BackendDAE.Variables"
2706 input list<DAE.ComponentRef> varlst;
2707 input BackendDAE.Variables vars;
2708 output BackendDAE.Variables vars_1;
2709 algorithm
2710 ✗ vars_1 := List.fold(varlst, removeCref, vars);
2711 ✗ vars_1 := listVar1(varList(vars_1));
2712 end deleteCrefs;
2713
2714 public function deleteVars "author: Frenkel TUD 2011-04
2715 Deletes variables from Variables. This is an expensive operation
2716 since we need to create a new binary tree with new indexes as well
2717 as a new compacted vector of variables."
2718 input BackendDAE.Variables inDelVars;
2719 input BackendDAE.Variables inVariables;
2720 output BackendDAE.Variables outVariables;
2721 algorithm
2722 outVariables := matchcontinue inVariables
2723 local
2724 BackendDAE.Variables newvars;
2725 case _
2726 algorithm
2727
2/2
✓ Branch 1 taken 1094 times.
✓ Branch 2 taken 2 times.
1096 true := intGt(varsSize(inDelVars),0);
2728 2 newvars := traverseBackendDAEVars(inDelVars, deleteVars1, inVariables);
2729 2 newvars := listVar1(varList(newvars));
2730 then
2731 newvars;
2732 else
2733 then
2734 inVariables;
2735 end matchcontinue;
2736 end deleteVars;
2737
2738 protected function deleteVars1
2739 input BackendDAE.Var inVar;
2740 input BackendDAE.Variables inVars;
2741 output BackendDAE.Var outVar = inVar;
2742 output BackendDAE.Variables outVars;
2743 algorithm
2744 191 outVars := removeCref(inVar.varName, inVars);
2745 end deleteVars1;
2746
2747 public function deleteVar
2748 "author: PA
2749 Deletes a variable from Variables."
2750 input DAE.ComponentRef inComponentRef;
2751 input BackendDAE.Variables inVariables;
2752 output BackendDAE.Variables outVariables;
2753 algorithm
2754 outVariables := match inComponentRef
2755 local
2756 BackendDAE.Variables vars;
2757 DAE.ComponentRef cr;
2758 list<Integer> ilst;
2759
2760 case cr algorithm
2761 ✗ (_,ilst) := getVar(cr,inVariables);
2762 ✗ (vars,_) := removeVars(ilst,inVariables,{});
2763 ✗ vars := listVar1(varList(vars));
2764 then vars;
2765 end match;
2766 end deleteVar;
2767
2768 public function deleteVarIfExistsAndReturn
2769 "author: ptaeuber
2770 Deletes a variable from Variables and returns it."
2771 input DAE.ComponentRef inComponentRef;
2772 input BackendDAE.Variables inVariables;
2773 output list<BackendDAE.Var> outVarLst = {};
2774 output BackendDAE.Variables outVariables = inVariables;
2775 protected
2776 list<Integer> ilst;
2777 algorithm
2778 try
2779 425 (outVarLst,ilst) := getVar(inComponentRef,inVariables);
2780 425 (outVariables,_) := removeVars(ilst,inVariables,{});
2781 425 outVariables := listVar1(varList(outVariables));
2782 else
2783 end try;
2784 end deleteVarIfExistsAndReturn;
2785
2786 public function removeCrefs "author: wbraun
2787 Removes a list of DAE.ComponentRef from BackendDAE.Variables"
2788 input list<DAE.ComponentRef> varlst;
2789 input BackendDAE.Variables vars;
2790 output BackendDAE.Variables vars_1;
2791 algorithm
2792 4617 vars_1 := List.fold(varlst, removeCref, vars);
2793 end removeCrefs;
2794
2795 public function removeCref
2796 "author: Frenkel TUD 2012-09
2797 Deletes a variable from Variables."
2798 input DAE.ComponentRef inComponentRef;
2799 input BackendDAE.Variables inVariables;
2800 output BackendDAE.Variables outVariables;
2801 algorithm
2802 outVariables := matchcontinue inComponentRef
2803 local
2804 BackendDAE.Variables vars;
2805 DAE.ComponentRef cr;
2806 list<Integer> ilst;
2807 case cr
2808 algorithm
2809 38295 (_,ilst) := getVar(cr,inVariables);
2810 464 (vars,_) := removeVars(ilst,inVariables,{});
2811 then
2812 vars;
2813 else inVariables;
2814 end matchcontinue;
2815 end removeCref;
2816
2817 public function removeVars "author: Frenkel TUD 2012-09
2818 Removes vars from the vararray but does not scaling down the array"
2819 input list<Integer> inVarPos "Position of vars to delete 1 based";
2820 input BackendDAE.Variables inVariables;
2821 input list<BackendDAE.Var> iAcc;
2822 output BackendDAE.Variables outVariables;
2823 output list<BackendDAE.Var> outVars "deleted vars in reverse order";
2824 algorithm
2825 (outVariables,outVars) := matchcontinue inVarPos
2826 local
2827 BackendDAE.Variables vars;
2828 list<Integer> ilst;
2829 Integer i;
2830 BackendDAE.Var v;
2831 list<BackendDAE.Var> acc;
2832 889 case {} then (inVariables,iAcc);
2833 case i::ilst
2834 algorithm
2835 889 (vars,v) := removeVar(i,inVariables);
2836 889 (vars,acc) := removeVars(ilst,vars,v::iAcc);
2837 then
2838 (vars,acc);
2839 case _::ilst
2840 algorithm
2841 ✗ (vars,acc) := removeVars(ilst,inVariables,iAcc);
2842 then
2843 (vars,acc);
2844 end matchcontinue;
2845 end removeVars;
2846
2847 public function removeVarDAE
2848 "author: Frenkel TUD 2012-11
2849 Removes a var from the vararray but does not scaling down the array"
2850 input Integer inVarPos "1 based index";
2851 input BackendDAE.EqSystem inEqSystem;
2852 output BackendDAE.EqSystem outEqSystem;
2853 output BackendDAE.Var outVar;
2854 protected
2855 BackendDAE.Variables vars;
2856 algorithm
2857 ✗ (vars, outVar) := removeVar(inVarPos, inEqSystem.orderedVars);
2858 ✗ outEqSystem := BackendDAEUtil.setEqSystVars(inEqSystem, vars);
2859 end removeVarDAE;
2860
2861 public function removeAliasVars "remove alias Vars"
2862 input BackendDAE.Shared inShared;
2863 output BackendDAE.Shared outShared;
2864 algorithm
2865 2 outShared := BackendDAEUtil.setSharedAliasVars(inShared, emptyVars());
2866 end removeAliasVars;
2867
2868 public function removeVar
2869 "Removes a var from the vararray but does not scaling down the array.
2870 The prefix index keeps the index: getVar skips it once the slot is empty,
2871 and indices are never reused, so dropping it there would only cost a scan."
2872 input Integer inIndex;
2873 input BackendDAE.Variables inVariables;
2874 output BackendDAE.Variables outVariables;
2875 output BackendDAE.Var outVar;
2876 protected
2877 array<list<BackendDAE.CrefIndex>> indices;
2878 list<BackendDAE.CrefIndex> cr_indices;
2879 BackendDAE.VariableArray arr;
2880 Integer buckets, num_vars, hash_idx;
2881 DAE.ComponentRef cr;
2882 algorithm
2883 162693 BackendDAE.VARIABLES(crefIndices = indices, varArr = arr, bucketSize = buckets, numberOfVars = num_vars) := inVariables;
2884 162693 (arr, outVar as BackendDAE.VAR(varName = cr)) := vararrayDelete(arr, inIndex);
2885 162693 hash_idx := intMod(ComponentReferenceBasics.hashComponentRef(cr), buckets) + 1;
2886 162693 cr_indices := indices[hash_idx];
2887 162693 cr_indices := List.deleteMemberOnTrue(BackendDAE.CREFINDEX(cr, inIndex - 1), cr_indices, removeVar2);
2888 162693 arrayUpdate(indices, hash_idx, cr_indices);
2889 outVariables := inVariables;
2890 162693 outVariables.varArr := arr;
2891 outVariables.numberOfVars := num_vars - 1;
2892 end removeVar;
2893
2894 protected function removeVar2
2895 input BackendDAE.CrefIndex inCrefIndex1;
2896 input BackendDAE.CrefIndex inCrefIndex2;
2897 output Boolean outMatch;
2898 algorithm
2899 189388 outMatch := inCrefIndex1.index == inCrefIndex2.index;
2900 end removeVar2;
2901
2902 public function isKnownAndParam
2903 "Returns true if all contained crefs are knownVariables and parameters"
2904 input DAE.Exp inExp;
2905 input BackendDAE.Variables knownVars;
2906 output Boolean outBoolean;
2907 protected
2908 tuple<Boolean,BackendDAE.Variables> tpl = (true,knownVars);
2909 algorithm
2910 4542 (_,(outBoolean,_)) := Expression.traverseExpBottomUp(inExp, isKnownAndParamWork, tpl);
2911 end isKnownAndParam;
2912
2913 protected function isKnownAndParamWork
2914 input output DAE.Exp inExp;
2915 input output tuple<Boolean,BackendDAE.Variables> tpl;
2916 protected
2917 Boolean outBoolean;
2918 BackendDAE.Variables knownVars;
2919 algorithm
2920 26025 (outBoolean,knownVars) := tpl;
2921 tpl := match (inExp, outBoolean)
2922 local
2923 DAE.ComponentRef cr;
2924 case (_,false)
2925 11734 then (false,knownVars);
2926 case (DAE.CREF(componentRef = cr),_)
2927
2/2
✓ Branch 1 taken 4298 times.
✓ Branch 2 taken 267 times.
8863 then (BackendVariable.crefIsParam(cr,knownVars),knownVars);
2928 9726 else (true,knownVars);
2929 end match;
2930 end isKnownAndParamWork;
2931
2932 public function crefIsParam
2933 input DAE.ComponentRef inComponentRef;
2934 input BackendDAE.Variables inVariables;
2935 output Boolean outBool = true;
2936 protected
2937 list<BackendDAE.Var> varlst;
2938 algorithm
2939 try
2940 4565 varlst := getVar(inComponentRef, inVariables);
2941
2/2
✓ Branch 0 taken 333 times.
✓ Branch 1 taken 267 times.
600 for var in varlst loop
2942 333 outBool := isParam(var);
2943
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 267 times.
333 if not outBool then
2944 66 return;
2945 end if;
2946 end for;
2947 else
2948 outBool := false;
2949 end try;
2950 end crefIsParam;
2951
2952 public function existsVar
2953 "author: PA
2954 Return true if a variable exists in the vector"
2955 input DAE.ComponentRef inComponentRef;
2956 input BackendDAE.Variables inVariables;
2957 input Boolean skipDiscrete;
2958 output Boolean outExists;
2959 protected
2960 list<BackendDAE.Var> varlst;
2961 algorithm
2962 try
2963 371340 varlst := getVar(inComponentRef, inVariables);
2964
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 298652 times.
298652 varlst := if skipDiscrete then List.select(varlst, isVarNonDiscrete) else varlst;
2965 298652 outExists := not listEmpty(varlst);
2966 else
2967 outExists := false;
2968 end try;
2969 end existsVar;
2970
2971 public function existsAnyVar
2972 "author: PA
2973 Return true if a variable exists in the vector"
2974 input list<DAE.ComponentRef> inComponentRefs;
2975 input BackendDAE.Variables inVariables;
2976 input Boolean skipDiscrete = false;
2977 output Boolean outExists = false;
2978 algorithm
2979
2/2
✓ Branch 0 taken 66143 times.
✓ Branch 1 taken 166750 times.
232893 for cref in inComponentRefs loop
2980
4/4
✓ Branch 1 taken 5167 times.
✓ Branch 2 taken 60976 times.
✓ Branch 4 taken 418 times.
✓ Branch 5 taken 4749 times.
66143 if existsVar(cref, inVariables, skipDiscrete) and not isState(cref, inVariables) then
2981 outExists := true;
2982 break;
2983 end if;
2984 end for;
2985 end existsAnyVar;
2986
2987 public function makeVar
2988 input DAE.ComponentRef cr;
2989 output BackendDAE.Var v;
2990 protected
2991 DAE.Type tp = ComponentReference.crefLastType(cr);
2992 DAE.Dimensions dims = Expression.arrayDimension(tp);
2993 algorithm
2994 15649 v := BackendDAE.VAR(cr, BackendDAE.VARIABLE(), DAE.BIDIR(), DAE.NON_PARALLEL(), Types.arrayElementType(tp), NONE(), NONE(), dims, DAE.emptyElementSource, NONE(), NONE(), NONE(), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), false, false, false);
2995 end makeVar;
2996
2997 public function addVarDAE
2998 "author: Frenkel TUD 2011-04
2999 Add a variable to Variables of a BackendDAE.
3000 If the variable already exists, the function updates the variable."
3001 input BackendDAE.Var inVar;
3002 input BackendDAE.EqSystem inEqSystem;
3003 output BackendDAE.EqSystem outEqSystem;
3004 algorithm
3005 2755 outEqSystem := BackendDAEUtil.setEqSystVars(inEqSystem, addVar(inVar, inEqSystem.orderedVars));
3006 end addVarDAE;
3007
3008 public function addVarsDAE
3009 "author: Frenkel TUD 2011-04
3010 Add a variable to Variables of a BackendDAE.
3011 If the variable already exists, the function updates the variable."
3012 input list<BackendDAE.Var> inVars;
3013 input BackendDAE.EqSystem inEqSystem;
3014 output BackendDAE.EqSystem outEqSystem = inEqSystem;
3015 algorithm
3016 120 outEqSystem := List.fold(inVars, addVarDAE, outEqSystem);
3017 end addVarsDAE;
3018
3019 public function addGlobalKnownVarDAE
3020 "author: Frenkel TUD 2011-04
3021 Add a variable to Variables of a BackendDAE.
3022 If the variable already exists, the function updates the variable."
3023 input BackendDAE.Var inGlobalKnownVar;
3024 input BackendDAE.Shared inShared;
3025 output BackendDAE.Shared outShared;
3026 algorithm
3027 45027 outShared := BackendDAEUtil.setSharedGlobalKnownVars(inShared, addVar(inGlobalKnownVar, inShared.globalKnownVars));
3028 end addGlobalKnownVarDAE;
3029
3030 public function addNewGlobalKnownVarDAE
3031 "author: Frenkel TUD 2011-04
3032 Add a variable to Variables of a BackendDAE.
3033 No Check if variable already exist. Use only for new variables"
3034 input BackendDAE.Var inGlobalKnownVar;
3035 input BackendDAE.Shared inShared;
3036 output BackendDAE.Shared outShared;
3037 algorithm
3038 ✗ outShared := BackendDAEUtil.setSharedGlobalKnownVars(inShared, addNewVar(inGlobalKnownVar, inShared.globalKnownVars));
3039 end addNewGlobalKnownVarDAE;
3040
3041 public function addAliasVarDAE
3042 "author: Frenkel TUD 2012-09
3043 Add a alias variable to Variables of a BackendDAE.Shared
3044 If the variable already exists, the function updates the variable."
3045 input BackendDAE.Var inVar;
3046 input BackendDAE.Shared inShared;
3047 output BackendDAE.Shared outShared;
3048 algorithm
3049 116860 outShared := BackendDAEUtil.setSharedAliasVars(inShared, addVar(inVar, inShared.aliasVars));
3050 end addAliasVarDAE;
3051
3052 public function addNewAliasVarDAE
3053 "author: Frenkel TUD 2012-09
3054 Add a alias variable to Variables of a BackendDAE.Shared
3055 No Check if variable already exist. Use only for new variables"
3056 input BackendDAE.Var inVar;
3057 input BackendDAE.Shared inShared;
3058 output BackendDAE.Shared outShared;
3059 algorithm
3060 ✗ outShared := BackendDAEUtil.setSharedAliasVars(inShared, addNewVar(inVar, inShared.aliasVars));
3061 end addNewAliasVarDAE;
3062
3063 public function addVar
3064 "Adds a variable to the set, or updates it if it already exists."
3065 input BackendDAE.Var inVar;
3066 input BackendDAE.Variables inVariables;
3067 output BackendDAE.Variables outVariables = inVariables;
3068 protected
3069 Integer hash, hash_idx, arr_idx;
3070 list<BackendDAE.CrefIndex> indices;
3071 algorithm
3072 3717243 hash := ComponentReferenceBasics.hashComponentRef(inVar.varName);
3073
1/2
✓ Branch 0 taken 3717243 times.
✗ Branch 1 not taken.
3717243 hash_idx := intMod(hash, inVariables.bucketSize) + 1;
3074 3717243 indices := arrayGet(inVariables.crefIndices, hash_idx);
3075
3076 try
3077 3717243 arr_idx := findCrefIndex(inVar.varName, indices);
3078
2/2
✓ Branch 0 taken 3585040 times.
✓ Branch 1 taken 132203 times.
3717243 true := arr_idx >= 0;
3079 132203 outVariables.varArr := vararraySetnth(inVariables.varArr, arr_idx+1, inVar);
3080 else
3081
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 3585244 times.
3585312 if outVariables.numberOfVars >= outVariables.bucketSize then
3082 68 outVariables := growBuckets(outVariables);
3083
1/2
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
68 hash_idx := intMod(hash, outVariables.bucketSize) + 1;
3084 68 indices := arrayGet(outVariables.crefIndices, hash_idx);
3085 end if;
3086 3585312 outVariables.varArr := vararrayAdd(outVariables.varArr, inVar);
3087 7170624 arrayUpdate(outVariables.crefIndices, hash_idx, (BackendDAE.CREFINDEX(inVar.varName, outVariables.numberOfVars)::indices));
3088
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3585312 times.
7170624 updatePrefixIndices(inVar.varName, outVariables.numberOfVars, arrayGet(outVariables.prefixIndices, 1));
3089
2/2
✓ Branch 0 taken 3544499 times.
✓ Branch 1 taken 40813 times.
3585312 outVariables.numberOfVars := outVariables.numberOfVars + 1;
3090
4/4
✓ Branch 0 taken 3544499 times.
✓ Branch 1 taken 40813 times.
✓ Branch 3 taken 595 times.
✓ Branch 4 taken 3543904 times.
3585312 if not outVariables.hasStartVars and ComponentReference.isStartCref(inVar.varName) then
3091 595 outVariables.hasStartVars := true;
3092 end if;
3093 end try;
3094 end addVar;
3095
3096 public function addVars
3097 "Adds a list of variables to the Variables structure. If any variable already
3098 exists it's updated instead."
3099 input list<BackendDAE.Var> inVars;
3100 input BackendDAE.Variables inVariables;
3101 output BackendDAE.Variables outVariables;
3102 algorithm
3103 76911 outVariables := List.fold(inVars, addVar, inVariables);
3104 end addVars;
3105
3106 public function addNewVars
3107 "Adds a list of variables to the Variables structure. If any variable already
3108 exists it's updated instead."
3109 input list<BackendDAE.Var> inVars;
3110 input BackendDAE.Variables inVariables;
3111 output BackendDAE.Variables outVariables;
3112 algorithm
3113 5192 outVariables := List.fold(inVars, addNewVar, inVariables);
3114 end addNewVars;
3115
3116 public function addNewVar
3117 "Add a new variable to the set, without checking if it already exists."
3118 input BackendDAE.Var inVar;
3119 input BackendDAE.Variables inVariables;
3120 output BackendDAE.Variables outVariables;
3121 protected
3122 array<list<BackendDAE.CrefIndex>> hashvec;
3123 BackendDAE.VariableArray varr;
3124 Integer bsize, num_vars, idx;
3125 list<BackendDAE.CrefIndex> indices;
3126 algorithm
3127
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 207292 times.
207292 outVariables := if inVariables.numberOfVars >= inVariables.bucketSize then growBuckets(inVariables) else inVariables;
3128 207292 BackendDAE.VARIABLES(crefIndices = hashvec, varArr = varr, bucketSize = bsize, numberOfVars = num_vars) := outVariables;
3129 207292 idx := intMod(ComponentReferenceBasics.hashComponentRef(inVar.varName), bsize) + 1;
3130 207292 varr := vararrayAdd(varr, inVar);
3131 207292 indices := hashvec[idx];
3132 414584 arrayUpdate(hashvec, idx, (BackendDAE.CREFINDEX(inVar.varName, num_vars)::indices));
3133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 207292 times.
414584 updatePrefixIndices(inVar.varName, num_vars, arrayGet(outVariables.prefixIndices, 1));
3134 207292 outVariables.varArr := varr;
3135 outVariables.numberOfVars := num_vars + 1;
3136
4/4
✓ Branch 0 taken 18498 times.
✓ Branch 1 taken 188794 times.
✓ Branch 3 taken 188757 times.
✓ Branch 4 taken 37 times.
207292 if not outVariables.hasStartVars and ComponentReference.isStartCref(inVar.varName) then
3137 37 outVariables.hasStartVars := true;
3138 end if;
3139 end addNewVar;
3140
3141 public function addVariables
3142 "Adds the content of one Variables to another."
3143 input BackendDAE.Variables inSrcVars;
3144 input BackendDAE.Variables inDestVars;
3145 output BackendDAE.Variables outVars = inDestVars;
3146 protected
3147 array<Option<BackendDAE.Var>> vars;
3148 Integer num_vars;
3149 BackendDAE.Var var;
3150 Option<BackendDAE.Var> ovar;
3151 algorithm
3152 // TODO: Don't rehash if the sets have the same size!
3153 19007 BackendDAE.VARIABLES(varArr=BackendDAE.VARIABLE_ARRAY(numberOfElements=num_vars, varOptArr=vars)) := inSrcVars;
3154
3155
2/2
✓ Branch 0 taken 2981 times.
✓ Branch 1 taken 16026 times.
209340 for i in 1:num_vars loop
3156 190333 ovar := vars[i];
3157
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 190333 times.
✓ Branch 2 taken 190080 times.
✓ Branch 3 taken 253 times.
190333 if isSome(ovar) then
3158 190080 SOME(var) := ovar;
3159 190080 outVars := addVar(var, outVars);
3160 end if;
3161 end for;
3162 end addVariables;
3163
3164 public function getVarAt
3165 "Returns the variable at a given position."
3166 input BackendDAE.Variables inVariables;
3167 input Integer inIndex;
3168 output BackendDAE.Var outVar;
3169 algorithm
3170 5230971 outVar := vararrayNth(inVariables.varArr, inIndex);
3171 end getVarAt;
3172
3173 public function setVarAt
3174 input BackendDAE.Variables inVariables;
3175 input Integer inIndex;
3176 input BackendDAE.Var inVar;
3177 output BackendDAE.Variables outVariables = inVariables;
3178 algorithm
3179 184094 vararraySetnth(inVariables.varArr, inIndex, inVar);
3180 end setVarAt;
3181
3182 public function getVarAtIndexFirst
3183 "author: marcusw
3184 Return variable at a given position, enumerated from 1..n, but with the index as first argument, so that it can be used with fold an map functions."
3185 input Integer inIndex;
3186 input BackendDAE.Variables inVariables;
3187 output BackendDAE.Var outVar;
3188 algorithm
3189 116390 outVar := getVarAt(inVariables, inIndex);
3190 end getVarAtIndexFirst;
3191
3192 public function getVarSharedAt
3193 "author: Frenkel TUD 2012-12
3194 return a Variable."
3195 input Integer inInteger;
3196 input BackendDAE.Shared inShared;
3197 output BackendDAE.Var outVar;
3198 algorithm
3199 8603 outVar := getVarAt(inShared.globalKnownVars, inInteger);
3200 end getVarSharedAt;
3201
3202 public function getVarDAE
3203 "author: Frenkel TUD 2012-05
3204 return a Variable."
3205 input DAE.ComponentRef inComponentRef;
3206 input BackendDAE.EqSystem inEqSystem;
3207 output list<BackendDAE.Var> outVarLst;
3208 output list<Integer> outIntegerLst;
3209 algorithm
3210 ✗ (outVarLst, outIntegerLst) := getVar(inComponentRef, inEqSystem.orderedVars);
3211 end getVarDAE;
3212
3213 public function getVarShared
3214 "author: Frenkel TUD 2012-05
3215 return a Variable."
3216 input DAE.ComponentRef inComponentRef;
3217 input BackendDAE.Shared inShared;
3218 output list<BackendDAE.Var> outVarLst;
3219 output list<Integer> outIntegerLst;
3220 algorithm
3221 8589 (outVarLst, outIntegerLst) := getVar(inComponentRef, inShared.globalKnownVars);
3222 end getVarShared;
3223
3224 public function containsVar
3225 input BackendDAE.Var var;
3226 input BackendDAE.Variables inVariables;
3227 output Boolean outB = containsCref(var.varName, inVariables);
3228 end containsVar;
3229
3230 public function containsCref
3231 input DAE.ComponentRef cr;
3232 input BackendDAE.Variables inVariables;
3233 output Boolean outB;
3234 algorithm
3235 try
3236 487797 getVar(cr, inVariables);
3237 outB := true;
3238 else
3239 outB := false;
3240 end try;
3241 end containsCref;
3242
3243 public function getVar
3244 "author: PA
3245 Return a variable(s) and its index(es) in the vector.
3246 The indexes is enumerated from 1..n
3247 Normally a variable has only one index, but in case of an array variable
3248 it may have several indexes and several scalar variables,
3249 therefore a list of variables and a list of indexes is returned.
3250 inputs: (DAE.ComponentRef, BackendDAE.Variables)
3251 outputs: (Var list, int list /* indexes */)"
3252 input DAE.ComponentRef cr;
3253 input BackendDAE.Variables inVariables;
3254 output list<BackendDAE.Var> outVarLst = {};
3255 output list<Integer> outIntegerLst = {};
3256 protected
3257 BackendDAE.Var v;
3258 Integer hash, indx;
3259 Boolean found;
3260 algorithm
3261 8398204 hash := ComponentReferenceBasics.hashComponentRef(cr);
3262 try
3263 8398204 (v, indx) := getVarHashed(cr, hash, inVariables);
3264 4142157 outVarLst := {v};
3265
2/2
✓ Branch 0 taken 3128682 times.
✓ Branch 1 taken 1013475 times.
4142157 outIntegerLst := if isPresent(outIntegerLst) then {indx} else {};
3266 found := true;
3267 else
3268 found := false;
3269 end try;
3270
2/2
✓ Branch 0 taken 4142157 times.
✓ Branch 1 taken 4256047 times.
8398204 if not found then
3271 4256047 (outVarLst, outIntegerLst) := getVarExpanded(cr, hash, inVariables);
3272 end if;
3273 end getVar;
3274
3275 public function getVarExpanded
3276 "The part of getVar for a cref that is not itself a variable: an array or
3277 record prefix, or an array element with variable indices. hash is
3278 ComponentReferenceBasics.hashComponentRef(cr)."
3279 input DAE.ComponentRef cr;
3280 input Integer hash;
3281 input BackendDAE.Variables inVariables;
3282 output list<BackendDAE.Var> outVarLst = {};
3283 output list<Integer> outIntegerLst = {};
3284 protected
3285 BackendDAE.Var v;
3286 Integer depth;
3287 list<DAE.ComponentRef> crlst;
3288 DAE.ComponentRef cr1;
3289 list<Integer> indices, live = {};
3290 Integer nsubs, bucket;
3291 Boolean died = false;
3292 Option<BackendDAE.Var> var_opt;
3293 DAE.Type ty;
3294 Boolean prefix, scalar;
3295 list<DAE.Dimension> dims;
3296 algorithm
3297 6549017 (prefix, scalar) := queryShape(cr);
3298
2/2
✓ Branch 0 taken 3449821 times.
✓ Branch 1 taken 3099196 times.
6549017 if prefix then
3299 3449821 (indices, depth, nsubs, bucket) := getPrefixIndices(cr, hash, inVariables);
3300 85694 (ty, dims) := TypesDump.flattenArrayType(ComponentReference.crefLastType(cr));
3301 // latest added first, as expanding cr and looking its elements up gave
3302
2/2
✓ Branch 1 taken 697111 times.
✓ Branch 2 taken 85694 times.
782805 for i in listReverse(indices) loop
3303 697111 var_opt := arrayGet(inVariables.varArr.varOptArr, i + 1);
3304
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 697111 times.
✓ Branch 2 taken 697109 times.
✓ Branch 3 taken 2 times.
697111 if isSome(var_opt) then
3305 // walking oldest first, so consing restores the stored order
3306 live := i :: live;
3307
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 697109 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 697109 times.
697109 SOME(v) := var_opt;
3308
1/2
✓ Branch 2 taken 697109 times.
✗ Branch 3 not taken.
697109 if isElementOf(v.varName, depth, ty, listLength(dims)) then
3309 outVarLst := v :: outVarLst;
3310 outIntegerLst := (i + 1) :: outIntegerLst;
3311 end if;
3312 else
3313 died := true;
3314 end if;
3315 end for;
3316
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 85693 times.
85694 if died then
3317 1 setPrefixIndices(cr, depth, nsubs, bucket, live, inVariables);
3318 end if;
3319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 85694 times.
85694 if listEmpty(outVarLst) then
3320 ✗ fail();
3321 end if;
3322 85694 return;
3323 end if;
3324
3325
2/2
✓ Branch 0 taken 3081531 times.
✓ Branch 1 taken 17665 times.
3099196 if scalar then
3326 3081531 fail();
3327 end if;
3328
3329 try
3330 17665 crlst := ComponentReference.expandCref(cr, true);
3331
2/2
✓ Branch 1 taken 11655 times.
✓ Branch 2 taken 6010 times.
17665 (outVarLst as _::_, outIntegerLst) := getVarLst(crlst, inVariables);
3332 else
3333 // try again check if variable indexes used
3334
2/2
✓ Branch 1 taken 7927 times.
✓ Branch 2 taken 3728 times.
13590 (cr1, true) := replaceVarWithWholeDim(cr, false);
3335 3728 crlst := ComponentReference.expandCref(cr1, true);
3336
2/2
✓ Branch 1 taken 1935 times.
✓ Branch 2 taken 1793 times.
3728 (outVarLst as _::_, outIntegerLst) := getVarLst(crlst, inVariables);
3337 end try;
3338 end getVarExpanded;
3339
3340 protected function queryShape
3341 "prefix: getVar can answer cr from the prefix index (an array or record with
3342 constant indices, every qualifier but the last fully indexed, so that
3343 expandCref would only append subscripts and record fields to cr).
3344 scalar: expandCref would return cr itself (every identifier fully indexed
3345 with constant subscripts, the last one not a record), so a hash miss is
3346 final. The qualifiers are checked once for both."
3347 input DAE.ComponentRef cr;
3348 output Boolean prefix;
3349 output Boolean scalar;
3350 algorithm
3351 (prefix, scalar) := match cr
3352 local
3353 Boolean ints, p, s;
3354 case DAE.CREF_IDENT()
3355 algorithm
3356 6548532 ints := allIntSubscripts(cr.subscriptLst);
3357
10/10
✓ Branch 0 taken 6531352 times.
✓ Branch 1 taken 17180 times.
✓ Branch 3 taken 3081531 times.
✓ Branch 4 taken 3449821 times.
✓ Branch 5 taken 6531352 times.
✓ Branch 6 taken 17180 times.
✓ Branch 9 taken 6478408 times.
✓ Branch 10 taken 52944 times.
✓ Branch 13 taken 65406 times.
✓ Branch 14 taken 6413002 times.
9630063 then (ints and isExpandableType(cr.identType),
3358 ints and listLength(cr.subscriptLst) >= Types.numberOfDimensions(cr.identType)
3359 and not Types.isRecord(Types.arrayElementType(cr.identType)));
3360 case DAE.CREF_QUAL()
3361 guard allIntSubscripts(cr.subscriptLst)
3362 and listLength(cr.subscriptLst) >= Types.numberOfDimensions(cr.identType)
3363 algorithm
3364 14930917 (p, s) := queryShape(cr.componentRef);
3365 then (p, s);
3366 else (false, false);
3367 end match;
3368 end queryShape;
3369
3370 protected function allIntSubscripts
3371 input list<DAE.Subscript> subs;
3372 output Boolean b = true;
3373 algorithm
3374
2/2
✓ Branch 0 taken 4805977 times.
✓ Branch 1 taken 21462316 times.
26268293 for sub in subs loop
3375
2/2
✓ Branch 1 taken 17224 times.
✓ Branch 2 taken 4788753 times.
4805977 if not isIntSubscript(sub) then
3376 b := false;
3377 17224 return;
3378 end if;
3379 end for;
3380 end allIntSubscripts;
3381
3382 protected function isExpandableType
3383 input DAE.Type ty;
3384 output Boolean b;
3385 algorithm
3386 b := match ty
3387 case DAE.T_ARRAY() then true;
3388 case DAE.T_COMPLEX(complexClassType = ClassInf.RECORD()) then true;
3389 else false;
3390 end match;
3391 end isExpandableType;
3392
3393 protected function isElementOf
3394 "Whether var, which the query (depth qualifiers, last one of element type ty
3395 with ndims dimensions) is a proper prefix of, is one of the elements
3396 expandCref lists for it: fully indexed, then only record fields. Jacobian
3397 seeds like x.SeedNLSJac0 extend the scalar x without being part of it."
3398 input DAE.ComponentRef var;
3399 input Integer depth;
3400 input DAE.Type ty;
3401 input Integer ndims;
3402 output Boolean b = false;
3403 protected
3404 DAE.ComponentRef v = var;
3405 DAE.Type t = ty;
3406 Integer n = ndims;
3407 list<DAE.Dimension> dims;
3408 Option<DAE.Type> fty;
3409 algorithm
3410
2/2
✓ Branch 0 taken 286907 times.
✓ Branch 1 taken 410202 times.
1333896 for i in 2:depth loop
3411 636787 v := ComponentReference.crefRest(v);
3412 end for;
3413
1/2
✓ Branch 2 taken 1253774 times.
✗ Branch 3 not taken.
1253774 while listLength(ComponentReference.crefFirstSubs(v)) == n loop
3414
2/2
✓ Branch 1 taken 697109 times.
✓ Branch 2 taken 556665 times.
1253774 if ComponentReference.crefIsIdent(v) then
3415 b := true;
3416 697109 return;
3417 end if;
3418 556665 v := ComponentReference.crefRest(v);
3419 556665 fty := recordFieldType(t, ComponentReferenceBasics.crefFirstIdent(v));
3420
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 556665 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 556665 times.
556665 if isNone(fty) then
3421 ✗ return;
3422 end if;
3423 556665 (t, dims) := TypesDump.flattenArrayType(Util.getOption(fty));
3424 556665 n := listLength(dims);
3425 end while;
3426 end isElementOf;
3427
3428 protected function recordFieldType
3429 input DAE.Type ty;
3430 input String name;
3431 output Option<DAE.Type> fty = NONE();
3432 algorithm
3433 fty := match ty
3434 case DAE.T_COMPLEX(complexClassType = ClassInf.RECORD())
3435 algorithm
3436
1/2
✓ Branch 0 taken 3505558 times.
✗ Branch 1 not taken.
3505558 for f in ty.varLst loop
3437
4/4
✓ Branch 0 taken 1354472 times.
✓ Branch 1 taken 2151086 times.
✓ Branch 3 taken 556665 times.
✓ Branch 4 taken 797807 times.
3505558 if f.name == name then
3438 556665 fty := SOME(f.ty);
3439 556665 break;
3440 end if;
3441 end for;
3442 then fty;
3443 else NONE();
3444 end match;
3445 end recordFieldType;
3446
3447 protected function isIntSubscript
3448 input DAE.Subscript sub;
3449 output Boolean b;
3450 algorithm
3451 b := match sub
3452 case DAE.INDEX(DAE.ICONST()) then true;
3453 else false;
3454 end match;
3455 end isIntSubscript;
3456
3457 protected function getPrefixIndices
3458 "0-based indices of the variables cr is a proper prefix of, latest added
3459 first. Removed variables keep their index here; the caller drops the ones
3460 whose slot in the variable array is gone and hands the rest back to
3461 setPrefixIndices."
3462 input DAE.ComponentRef cr;
3463 input Integer hash;
3464 input BackendDAE.Variables vars;
3465 output list<Integer> indices;
3466 output Integer depth = 0 "qualifiers of cr";
3467 output Integer nsubs = 0 "subscripts on the last qualifier";
3468 output Integer bucket;
3469 protected
3470 DAE.ComponentRef c = cr;
3471 Boolean last = false;
3472 array<list<BackendDAE.PrefixIndex>> table;
3473 algorithm
3474
2/2
✓ Branch 0 taken 11208962 times.
✓ Branch 1 taken 3449821 times.
14658783 while not last loop
3475 (nsubs, last, c) := match c
3476 3449821 case DAE.CREF_IDENT() then (listLength(c.subscriptLst), true, c);
3477 7759141 case DAE.CREF_QUAL() then (0, false, c.componentRef);
3478 end match;
3479 11208962 depth := depth + 1;
3480 end while;
3481 3449821 table := prefixIndexTable(vars);
3482 3449821 bucket := intMod(hash, arrayLength(table)) + 1;
3483
2/2
✓ Branch 1 taken 548764 times.
✓ Branch 2 taken 3364127 times.
3912891 for e in arrayGet(table, bucket) loop
3484
6/6
✓ Branch 0 taken 233124 times.
✓ Branch 1 taken 315640 times.
✓ Branch 2 taken 109807 times.
✓ Branch 3 taken 123317 times.
✓ Branch 5 taken 85694 times.
✓ Branch 6 taken 24113 times.
548764 if e.depth == depth and e.numSubscripts == nsubs and prefixEqual(e.cref, cr, depth, nsubs) then
3485 85694 indices := e.indices;
3486
1/2
✓ Branch 0 taken 85694 times.
✗ Branch 1 not taken.
85694 return;
3487 end if;
3488 end for;
3489 3364127 fail();
3490 end getPrefixIndices;
3491
3492 protected function prefixIndexTable
3493 "The prefix index of vars, built on first use."
3494 input BackendDAE.Variables vars;
3495 output array<list<BackendDAE.PrefixIndex>> table = arrayGet(vars.prefixIndices, 1);
3496 protected
3497 BackendDAE.Var v;
3498 algorithm
3499
2/2
✓ Branch 0 taken 3432150 times.
✓ Branch 1 taken 17671 times.
3449821 if arrayLength(table) > 0 then
3500 3432150 return;
3501 end if;
3502 17671 table := arrayCreate(vars.bucketSize, {});
3503 17671 arrayUpdate(vars.prefixIndices, 1, table);
3504
2/2
✓ Branch 0 taken 933 times.
✓ Branch 1 taken 16738 times.
1571020 for i in 1:vars.varArr.numberOfElements loop
3505
3/4
✗ Branch 1 not taken.
✓ Branch 2 taken 1553349 times.
✓ Branch 3 taken 1553198 times.
✓ Branch 4 taken 151 times.
1553349 if isSome(vars.varArr.varOptArr[i]) then
3506 1553198 SOME(v) := vars.varArr.varOptArr[i];
3507 1553198 updatePrefixIndices(v.varName, i - 1, table);
3508 end if;
3509 end for;
3510 end prefixIndexTable;
3511
3512 protected function setPrefixIndices
3513 "Narrows the entry cr names to indices, dropping the entry when none is left.
3514 getVar uses it to retire the indices of removed variables it just walked
3515 past, so a prefix is scanned for them at most once."
3516 input DAE.ComponentRef cr;
3517 input Integer depth;
3518 input Integer nsubs;
3519 input Integer bucket;
3520 input list<Integer> indices;
3521 input BackendDAE.Variables vars;
3522 protected
3523 array<list<BackendDAE.PrefixIndex>> table = arrayGet(vars.prefixIndices, 1);
3524 list<BackendDAE.PrefixIndex> entries = arrayGet(table, bucket), acc = {};
3525 BackendDAE.PrefixIndex e;
3526 algorithm
3527
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 while not listEmpty(entries) loop
3528 1 e :: entries := entries;
3529
3/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 time.
✗ Branch 6 not taken.
1 if e.depth == depth and e.numSubscripts == nsubs and prefixEqual(e.cref, cr, depth, nsubs) then
3530
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 e.indices := indices;
3531
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 arrayUpdate(table, bucket, List.append_reverse(acc, if listEmpty(indices) then entries else e :: entries));
3532 1 return;
3533 end if;
3534 acc := e :: acc;
3535 end while;
3536 end setPrefixIndices;
3537
3538 protected function updatePrefixIndices
3539 "Adds index under every proper prefix of cr that queryShape's prefix query
3540 can name: each qualifier of an array or record type, with each leading part
3541 of its subscripts. The hash of a prefix is the hashComponentRef of that prefix, so
3542 every qualifier is walked even where nothing is stored. Does nothing while
3543 table is not built."
3544 input DAE.ComponentRef cr;
3545 input Integer index;
3546 input array<list<BackendDAE.PrefixIndex>> table;
3547 protected
3548 DAE.ComponentRef c = cr;
3549 Integer hash = ComponentReferenceBasics.crefHashSeed, depth = 0, nsubs, count;
3550 list<DAE.Subscript> subs;
3551 DAE.Type ty;
3552 Boolean last, ok, store;
3553 algorithm
3554
2/2
✓ Branch 0 taken 1652748 times.
✓ Branch 1 taken 3693054 times.
5345802 if arrayLength(table) == 0 then
3555 3693054 return;
3556 end if;
3557 4124825 while true loop
3558 (subs, ty, last, ok) := match c
3559 1652748 case DAE.CREF_IDENT() then (c.subscriptLst, c.identType, true, true);
3560 4124825 case DAE.CREF_QUAL() then (c.subscriptLst, c.identType, false, true);
3561 else ({}, DAE.T_UNKNOWN_DEFAULT, true, false);
3562 end match;
3563
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5777573 times.
5777573 if not ok then
3564 ✗ return;
3565 end if;
3566 5777573 depth := depth + 1;
3567 5777573 hash := ComponentReferenceBasics.crefHashIdent(ComponentReferenceBasics.crefFirstIdent(c), hash);
3568 5777573 count := listLength(subs);
3569 nsubs := 0;
3570 5777573 store := isExpandableType(ty);
3571
4/4
✓ Branch 0 taken 1667770 times.
✓ Branch 1 taken 4109803 times.
✓ Branch 2 taken 1667123 times.
✓ Branch 3 taken 647 times.
5777573 if store and not (last and count == 0) then
3572 1667123 updatePrefixIndex(cr, depth, nsubs, hash, index, table);
3573 end if;
3574
2/2
✓ Branch 0 taken 1532095 times.
✓ Branch 1 taken 5777573 times.
7309668 for sub in subs loop
3575 1532095 hash := ComponentReferenceBasics.crefHashSubscript(sub, hash);
3576 1532095 nsubs := nsubs + 1;
3577
3/4
✓ Branch 0 taken 1532095 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 510588 times.
✓ Branch 3 taken 1021507 times.
1532095 if store and not (last and nsubs == count) then
3578 510588 updatePrefixIndex(cr, depth, nsubs, hash, index, table);
3579 end if;
3580 end for;
3581
2/2
✓ Branch 0 taken 1652748 times.
✓ Branch 1 taken 4124825 times.
5777573 if last then
3582 1652748 return;
3583 end if;
3584 4124825 c := match c case DAE.CREF_QUAL() then c.componentRef; end match;
3585 end while;
3586 end updatePrefixIndices;
3587
3588 protected function updatePrefixIndex
3589 input DAE.ComponentRef cr;
3590 input Integer depth;
3591 input Integer nsubs;
3592 input Integer hash;
3593 input Integer index;
3594 input array<list<BackendDAE.PrefixIndex>> table;
3595 protected
3596 Integer b = intMod(hash, arrayLength(table)) + 1;
3597 list<BackendDAE.PrefixIndex> entries = arrayGet(table, b), acc = {};
3598 BackendDAE.PrefixIndex e;
3599 algorithm
3600
2/2
✓ Branch 0 taken 1760061 times.
✓ Branch 1 taken 493296 times.
2253357 while not listEmpty(entries) loop
3601 1760061 e :: entries := entries;
3602
6/6
✓ Branch 0 taken 1710307 times.
✓ Branch 1 taken 49754 times.
✓ Branch 2 taken 1699559 times.
✓ Branch 3 taken 10748 times.
✓ Branch 5 taken 1684415 times.
✓ Branch 6 taken 15144 times.
1760061 if e.depth == depth and e.numSubscripts == nsubs and prefixEqual(e.cref, cr, depth, nsubs) then
3603 3368830 e.indices := index :: e.indices;
3604 1684415 arrayUpdate(table, b, List.append_reverse(acc, e :: entries));
3605 1684415 return;
3606 end if;
3607 acc := e :: acc;
3608 end while;
3609 986592 arrayUpdate(table, b, BackendDAE.PREFIXINDEX(cr, depth, nsubs, {index}) :: arrayGet(table, b));
3610 end updatePrefixIndex;
3611
3612 protected function prefixEqual
3613 "Whether the first depth qualifiers of cr1 and cr2 are equal, comparing only
3614 the first nsubs subscripts of the last one. Both must be at least that long."
3615 input DAE.ComponentRef cr1;
3616 input DAE.ComponentRef cr2;
3617 input Integer depth;
3618 input Integer nsubs;
3619 output Boolean equal = false;
3620 protected
3621 DAE.ComponentRef c1 = cr1, c2 = cr2;
3622 list<DAE.Subscript> s1, s2;
3623 DAE.Subscript sub1, sub2;
3624 algorithm
3625
2/2
✓ Branch 0 taken 1604441 times.
✓ Branch 1 taken 204926 times.
5743595 for i in 1:depth-1 loop
3626 (equal, c1, c2) := match (c1, c2)
3627 case (DAE.CREF_QUAL(), DAE.CREF_QUAL())
3628
8/8
✓ Branch 0 taken 3949219 times.
✓ Branch 1 taken 19110 times.
✓ Branch 3 taken 3934817 times.
✓ Branch 4 taken 14402 times.
✓ Branch 6 taken 589 times.
✓ Branch 7 taken 3934228 times.
✓ Branch 8 taken 34101 times.
✓ Branch 9 taken 3934228 times.
3968918 then (c1.ident == c2.ident and ExpressionBasics.subscriptEqual(c1.subscriptLst, c2.subscriptLst),
3629 c1.componentRef, c2.componentRef);
3630 end match;
3631
2/2
✓ Branch 0 taken 34101 times.
✓ Branch 1 taken 3934228 times.
3968329 if not equal then
3632 34101 return;
3633 end if;
3634 end for;
3635 equal := false;
3636
4/4
✓ Branch 2 taken 1770312 times.
✓ Branch 3 taken 4954 times.
✓ Branch 7 taken 120 times.
✓ Branch 8 taken 1770192 times.
1775266 if ComponentReferenceBasics.crefFirstIdent(c1) <> ComponentReferenceBasics.crefFirstIdent(c2) then
3637 5074 return;
3638 end if;
3639 1770192 s1 := ComponentReference.crefFirstSubs(c1);
3640 1770192 s2 := ComponentReference.crefFirstSubs(c2);
3641
2/2
✓ Branch 0 taken 369744 times.
✓ Branch 1 taken 1400448 times.
2142543 for i in 1:nsubs loop
3642
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 372433 times.
372433 sub1 :: s1 := s1;
3643
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 372433 times.
372433 sub2 :: s2 := s2;
3644
2/2
✓ Branch 1 taken 82 times.
✓ Branch 2 taken 372351 times.
372433 if not subscriptEq(sub1, sub2) then
3645 82 return;
3646 end if;
3647 end for;
3648 equal := true;
3649 end prefixEqual;
3650
3651 protected function subscriptEq
3652 input DAE.Subscript sub1;
3653 input DAE.Subscript sub2;
3654 output Boolean equal;
3655 algorithm
3656 equal := match (sub1, sub2)
3657 case (DAE.WHOLEDIM(), DAE.WHOLEDIM()) then true;
3658 372433 case (DAE.INDEX(), DAE.INDEX()) then ExpressionBasics.expEqual(sub1.exp, sub2.exp);
3659 ✗ case (DAE.SLICE(), DAE.SLICE()) then ExpressionBasics.expEqual(sub1.exp, sub2.exp);
3660 ✗ case (DAE.WHOLE_NONEXP(), DAE.WHOLE_NONEXP()) then ExpressionBasics.expEqual(sub1.exp, sub2.exp);
3661 else false;
3662 end match;
3663 end subscriptEq;
3664
3665 public function getVarSingle
3666 " Return a variable and its index in the vector.
3667 The indexes is enumerated from 1..n
3668 Normally a variable has only one index, but in case of an array variable
3669 it may have several indexes and several scalar variables,
3670 therefore a list of variables and a list of indexes is returned.
3671
3672 This function fails if there are more than a single returned value"
3673 input DAE.ComponentRef cr;
3674 input BackendDAE.Variables inVariables;
3675 output BackendDAE.Var outVar;
3676 output Integer outInteger;
3677 algorithm
3678 (outVar,outInteger) := matchcontinue inVariables
3679 local
3680 BackendDAE.Var v;
3681 Integer indx;
3682 list<DAE.ComponentRef> crlst;
3683 DAE.ComponentRef cr1;
3684 case _
3685 algorithm
3686 1014213 (v,indx) := getVar2(cr, inVariables) "if scalar found, return it";
3687 then (v,indx);
3688 case _ /* check if array or record */
3689 algorithm
3690 // TODO: Don't expand if > length 1
3691 437383 crlst := ComponentReference.expandCref(cr,true);
3692
2/2
✓ Branch 0 taken 7771 times.
✓ Branch 1 taken 429612 times.
437383 if isPresent(outInteger) then
3693
6/8
✓ Branch 1 taken 2197 times.
✓ Branch 2 taken 5574 times.
✓ Branch 3 taken 5539 times.
✓ Branch 4 taken 35 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 35 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 35 times.
437285 ({v},{indx}) := getVarLst(crlst,inVariables);
3694 else
3695
4/4
✓ Branch 1 taken 428329 times.
✓ Branch 2 taken 1283 times.
✓ Branch 3 taken 1185 times.
✓ Branch 4 taken 98 times.
429612 ({v},_) := getVarLst(crlst,inVariables);
3696 indx := 0;
3697 end if;
3698 133 then (v,indx);
3699 // try again check if variable indexes used
3700 case _
3701 algorithm
3702 // TODO: Don't expand if > length 1
3703 // replace variables with WHOLEDIM()
3704
2/2
✓ Branch 1 taken 435959 times.
✓ Branch 2 taken 1291 times.
438093 (cr1,true) := replaceVarWithWholeDim(cr, false);
3705 1291 crlst := ComponentReference.expandCref(cr1,true);
3706
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1285 times.
1291 if isPresent(outInteger) then
3707
1/8
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
6 ({v},{indx}) := getVarLst(crlst,inVariables);
3708 else
3709
4/4
✓ Branch 1 taken 502 times.
✓ Branch 2 taken 783 times.
✓ Branch 3 taken 335 times.
✓ Branch 4 taken 448 times.
1285 ({v},_) := getVarLst(crlst,inVariables);
3710 indx := 0;
3711 end if;
3712 448 then (v,indx);
3713 /* failure
3714 case (_,_)
3715 algorithm
3716 fprintln(Flags.DAE_LOW, "- getVar failed on component reference: " + ComponentReferenceBasics.printComponentRefStr(cr));
3717 then
3718 fail();
3719 */
3720 end matchcontinue;
3721 end getVarSingle;
3722
3723 public function getVarTryHard
3724 "author: kabdelhak
3725 This function tries to get a variable with from a given cref as hard as possible
3726 by removing subscripts and considering array representations. Should be replaced
3727 with proper array handling."
3728 input DAE.ComponentRef cref;
3729 input BackendDAE.Variables vars;
3730 output Option<list<BackendDAE.Var>> var_lst_opt;
3731 protected
3732 BackendDAE.Var var;
3733 list<BackendDAE.Var> var_lst;
3734 DAE.ComponentRef strippedCref;
3735 algorithm
3736 try
3737 549 (var, _) := getVarSingle(cref, vars);
3738 var_lst_opt := SOME({var});
3739 else try
3740 25 (var_lst, _) := getVar(cref, vars);
3741 var_lst_opt := SOME(var_lst);
3742 else try
3743 25 strippedCref := ComponentReference.crefStripSubsExceptModelSubs(cref);
3744 25 var := BackendVariable.getVarSingle(strippedCref, vars);
3745 var_lst_opt := SOME({var});
3746 else
3747 var_lst_opt := NONE();
3748 end try; end try; end try;
3749 end getVarTryHard;
3750
3751 protected function replaceVarWithWholeDim
3752 "Helper function to traverseExp. Traverses any expressions in a
3753 component reference (i.e. in it's subscripts)."
3754 input DAE.ComponentRef inCref;
3755 input Boolean iPerformed;
3756 output DAE.ComponentRef outCref;
3757 output Boolean oPerformed;
3758 algorithm
3759 (outCref, oPerformed) := match inCref
3760 local
3761 DAE.Ident name;
3762 DAE.ComponentRef cr,cr_1;
3763 DAE.Type ty;
3764 list<DAE.Subscript> subs,subs_1;
3765 Boolean b;
3766
3767 case DAE.CREF_QUAL(ident = name, identType = ty, subscriptLst = subs, componentRef = cr)
3768 algorithm
3769 978648 (subs_1, b) := replaceVarWithWholeDimSubs(subs, iPerformed);
3770 978648 (cr_1, b) := replaceVarWithWholeDim(cr, b);
3771
2/2
✓ Branch 0 taken 692394 times.
✓ Branch 1 taken 286254 times.
978648 then
3772 (if referenceEq(subs_1,subs) and referenceEq(cr_1,cr) then inCref else DAE.CREF_QUAL(name, ty, subs_1, cr_1), b);
3773
3774 case DAE.CREF_IDENT(ident = name, identType = ty, subscriptLst = subs)
3775 algorithm
3776 448511 (subs_1, b) := replaceVarWithWholeDimSubs(subs, iPerformed);
3777
2/2
✓ Branch 0 taken 279802 times.
✓ Branch 1 taken 168709 times.
448511 then
3778 (if referenceEq(subs_1,subs) then inCref else DAE.CREF_IDENT(name, ty, subs_1), b);
3779
3780 case DAE.OPTIMICA_ATTR_INST_CREF() then (inCref, iPerformed);
3781 case DAE.WILD() then (inCref, iPerformed);
3782
3783 else
3784 algorithm
3785 ✗ Error.addMessage(Error.INTERNAL_ERROR, {"BackendVariable.replaceVarWithWholeDim: Unknown cref"});
3786 ✗ then fail();
3787 end match;
3788 end replaceVarWithWholeDim;
3789
3790 protected function replaceVarWithWholeDimSubs
3791 input list<DAE.Subscript> inSubscript;
3792 input Boolean iPerformed;
3793 output list<DAE.Subscript> outSubscript;
3794 output Boolean oPerformed;
3795 algorithm
3796 (outSubscript, oPerformed) := match inSubscript
3797 local
3798 DAE.Subscript sub;
3799 DAE.Exp sub_exp, sub_exp_;
3800 list<DAE.Subscript> rest,res;
3801 Boolean b,const,calcRange;
3802
3803 case {} then (inSubscript,iPerformed);
3804 case DAE.WHOLEDIM()::rest
3805 algorithm
3806 315 (_,b) := replaceVarWithWholeDimSubs(rest,iPerformed);
3807 315 then (DAE.WHOLEDIM()::rest, b);
3808
3809 case (sub as DAE.SLICE(exp = sub_exp))::rest
3810 algorithm
3811 76 (res,b) := replaceVarWithWholeDimSubs(rest,iPerformed);
3812 76 const := Expression.isConst(sub_exp);
3813
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 68 times.
76 res := if const then sub::rest else (DAE.WHOLEDIM()::rest);
3814
3/4
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 68 times.
76 then
3815 (res, b or not const);
3816
3817 case (sub as DAE.INDEX(exp = sub_exp))::rest
3818 algorithm
3819 422640 (sub_exp_,calcRange) := computeRangeExps(sub_exp); // the fact that if it can be calculated, we can take the wholedim is a bit weird, anyway, the whole function is weird
3820 422640 (res,b) := replaceVarWithWholeDimSubs(rest,iPerformed);
3821 422640 const := Expression.isConst(sub_exp_);
3822
4/4
✓ Branch 0 taken 417644 times.
✓ Branch 1 taken 4996 times.
✓ Branch 2 taken 19 times.
✓ Branch 3 taken 417625 times.
422640 res := (if const then if referenceEq(sub_exp,sub_exp_) then sub else DAE.INDEX(sub_exp_) else DAE.WHOLEDIM())::rest;
3823
6/6
✓ Branch 0 taken 422575 times.
✓ Branch 1 taken 65 times.
✓ Branch 2 taken 417643 times.
✓ Branch 3 taken 4932 times.
✓ Branch 4 taken 417624 times.
✓ Branch 5 taken 19 times.
422640 then
3824 (res, b or not const or calcRange);
3825 case (sub as DAE.WHOLE_NONEXP(exp = sub_exp))::rest
3826 algorithm
3827 ✗ (res,b) := replaceVarWithWholeDimSubs(rest,iPerformed);
3828 ✗ const := Expression.isConst(sub_exp);
3829 ✗ res := if const then sub::rest else (DAE.WHOLEDIM()::rest);
3830 ✗ then
3831 (res, b or not const);
3832 end match;
3833 end replaceVarWithWholeDimSubs;
3834
3835 protected function computeRangeExps"computes the maximal range expression for calculated ranges like [i1+i2]."
3836 input DAE.Exp inExp;
3837 output DAE.Exp outExp;
3838 output Boolean isCalculated;
3839 algorithm
3840 (outExp,isCalculated) := match inExp
3841 local
3842 Integer stop1,stop2;
3843 DAE.Exp exp;
3844 DAE.Type ty;
3845 case DAE.BINARY(exp1=DAE.RANGE(ty=ty,start=DAE.ICONST(integer=1),stop=DAE.ICONST(integer=stop1)), operator=DAE.ADD(), exp2=DAE.RANGE(start=DAE.ICONST(integer=1),stop=DAE.ICONST(integer=stop2)))
3846 algorithm
3847 19 stop2:= stop1+stop2;
3848 19 exp := DAE.RANGE(ty,DAE.ICONST(1),NONE(),DAE.ICONST(stop2));
3849 then (exp,true);
3850 else
3851 then (inExp, false);
3852 end match;
3853 end computeRangeExps;
3854
3855 public function getVarLst
3856 input list<DAE.ComponentRef> inComponentRefLst;
3857 input BackendDAE.Variables inVariables;
3858 output list<BackendDAE.Var> outVarLst = {};
3859 output list<Integer> outIntegerLst = {};
3860 protected
3861 BackendDAE.Var v;
3862 Integer indx;
3863 algorithm
3864
2/2
✓ Branch 0 taken 37819 times.
✓ Branch 1 taken 430903 times.
468722 if isPresent(outIntegerLst) then
3865
2/2
✓ Branch 0 taken 171886 times.
✓ Branch 1 taken 37819 times.
209705 for cr in inComponentRefLst loop
3866 try
3867 171886 (v,indx) := getVar2(cr, inVariables);
3868 122871 outVarLst := v::outVarLst;
3869 122871 outIntegerLst := indx::outIntegerLst;
3870 else
3871 // skip this element
3872 end try;
3873 end for;
3874 else
3875
2/2
✓ Branch 0 taken 453633 times.
✓ Branch 1 taken 430903 times.
884536 for cr in inComponentRefLst loop
3876 try
3877 453633 (v,indx) := getVar2(cr, inVariables);
3878 outVarLst := v::outVarLst;
3879 else
3880 // skip this element
3881 end try;
3882 end for;
3883 end if;
3884 end getVarLst;
3885
3886 public function getVar2
3887 input DAE.ComponentRef inCref;
3888 input BackendDAE.Variables inVariables;
3889 output BackendDAE.Var outVar;
3890 output Integer outIndex;
3891 algorithm
3892 1646236 (outVar, outIndex) := getVarHashed(inCref, ComponentReferenceBasics.hashComponentRef(inCref), inVariables);
3893 end getVar2;
3894
3895 public function getVarHashed
3896 "The variable inCref names and its index, without expanding arrays; fails
3897 if it names none. hash is ComponentReferenceBasics.hashComponentRef(inCref)."
3898 input DAE.ComponentRef inCref;
3899 input Integer hash;
3900 input BackendDAE.Variables inVariables;
3901 output BackendDAE.Var outVar;
3902 output Integer outIndex;
3903 algorithm
3904
1/2
✓ Branch 0 taken 20492264 times.
✗ Branch 1 not taken.
40984528 outIndex := findCrefIndex(inCref, arrayGet(inVariables.crefIndices, intMod(hash, inVariables.bucketSize) + 1));
3905
2/2
✓ Branch 0 taken 7464017 times.
✓ Branch 1 taken 13028247 times.
20492264 true := outIndex >= 0;
3906 13028247 outIndex := outIndex + 1;
3907 13028247 outVar := vararrayNth(inVariables.varArr, outIndex);
3908
2/2
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 13019820 times.
13019839 true := ComponentReferenceBasics.crefEqualNoStringCompare(outVar.varName, inCref);
3909 end getVarHashed;
3910
3911 protected function findCrefIndex
3912 "The index stored for cr in a crefIndices bucket, or -1."
3913 input DAE.ComponentRef cr;
3914 input list<BackendDAE.CrefIndex> indices;
3915 output Integer index = -1;
3916 algorithm
3917
2/2
✓ Branch 0 taken 20338007 times.
✓ Branch 1 taken 11049057 times.
31387064 for ci in indices loop
3918
2/2
✓ Branch 1 taken 13160450 times.
✓ Branch 2 taken 7177557 times.
20338007 if ComponentReferenceBasics.crefEqualNoStringCompare(ci.cref, cr) then
3919 13160450 index := ci.index;
3920 13160450 return;
3921 end if;
3922 end for;
3923 end findCrefIndex;
3924
3925 public function getVarIndexFromVars
3926 input list<BackendDAE.Var> inVars;
3927 input BackendDAE.Variables inVariables;
3928 output list<Integer> outIndices = {};
3929 algorithm
3930
2/2
✓ Branch 0 taken 24679 times.
✓ Branch 1 taken 4807 times.
29486 for var in inVars loop
3931 24679 (_, outIndices) := traversingVarIndexFinder(var, inVariables, outIndices);
3932 end for;
3933 4807 outIndices := listReverse(outIndices);
3934 end getVarIndexFromVars;
3935
3936 public function getVarIndexFromVariables
3937 input BackendDAE.Variables inVariables;
3938 input BackendDAE.Variables inVariables2;
3939 output list<Integer> v_lst;
3940 algorithm
3941 1822 v_lst := listReverse(traverseBackendDAEVars(inVariables,
3942 function traversingVarIndexFinder(inVars = inVariables2), {}));
3943 end getVarIndexFromVariables;
3944
3945 protected function traversingVarIndexFinder
3946 "author: Frenkel TUD 2010-11"
3947 input BackendDAE.Var inVar;
3948 input BackendDAE.Variables inVars;
3949 input list<Integer> inIndices;
3950 output BackendDAE.Var outVar = inVar;
3951 output list<Integer> outIndices;
3952 protected
3953 DAE.ComponentRef cr;
3954 list<Integer> indices;
3955 algorithm
3956 try
3957 30491 cr := varCref(inVar);
3958 30491 (_, indices) := getVar(cr, inVars);
3959 30491 outIndices := List.append_reverse(indices, inIndices);
3960 else
3961 outIndices := inIndices;
3962 end try;
3963 end traversingVarIndexFinder;
3964
3965 public function getVarIndexFromVariablesIndexInFirstSet
3966 input BackendDAE.Variables inVariables;
3967 input BackendDAE.Variables inVariables2;
3968 output list<Integer> v_lst;
3969 protected
3970 Mutable<list<Integer>> a;
3971 algorithm
3972 ✗ (_,a,_) := traverseBackendDAEVars(inVariables, traversingVarIndexInFirstSetFinder, (inVariables2,Mutable.create({}),Mutable.create(1)));
3973 ✗ v_lst := listReverse(Mutable.access(a));
3974 end getVarIndexFromVariablesIndexInFirstSet;
3975
3976 protected function traversingVarIndexInFirstSetFinder
3977 "author: Frenkel TUD 2010-11"
3978 input output BackendDAE.Var var;
3979 input output tuple<BackendDAE.Variables,Mutable<list<Integer>>,Mutable<Integer>> data;
3980 protected
3981 DAE.ComponentRef cr;
3982 BackendDAE.Variables vars;
3983 Mutable<list<Integer>> l;
3984 Mutable<Integer> i;
3985 algorithm
3986 ✗ (vars,l,i) := data;
3987 try
3988 ✗ cr := varCref(var);
3989 ✗ getVar(cr, vars);
3990 ✗ Mutable.update(l, Mutable.access(i)::Mutable.access(l));
3991 else
3992 end try;
3993 ✗ Mutable.update(i, Mutable.access(i)+1);
3994 end traversingVarIndexInFirstSetFinder;
3995
3996 public function mergeVariables
3997 "Merges two sets of Variables, where the variables of the first set takes
3998 precedence over the second set."
3999 input BackendDAE.Variables inVariables1;
4000 input BackendDAE.Variables inVariables2;
4001 input Boolean copy=true;
4002 output BackendDAE.Variables outVariables;
4003 protected
4004 Integer num_vars;
4005 algorithm
4006 2643 num_vars := varsSize(inVariables2);
4007
4008
2/2
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 2626 times.
2643 if varsLoadFactor(inVariables1, num_vars) > 1 then
4009 17 outVariables := emptyVarsSized(varsSize(inVariables1) + num_vars);
4010 17 outVariables := addVariables(inVariables1, outVariables);
4011 elseif copy then
4012 2626 outVariables := copyVariables(inVariables1);
4013 else
4014 outVariables := inVariables1;
4015 end if;
4016
4017 2643 outVariables := addVariables(inVariables2, outVariables);
4018 end mergeVariables;
4019
4020 public function rehashVariables
4021 input BackendDAE.Variables inVariables;
4022 output BackendDAE.Variables outVariables;
4023 protected
4024 Real load = varsLoadFactor(inVariables, 0);
4025 algorithm
4026
3/4
✓ Branch 0 taken 213 times.
✓ Branch 1 taken 1957 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 213 times.
2170 if load < 0.5 or load > 1.0 then
4027 1957 outVariables := emptyVarsSized(varsSize(inVariables));
4028 1957 outVariables := addVariables(inVariables, outVariables);
4029 else
4030 outVariables := inVariables;
4031 end if;
4032 end rehashVariables;
4033
4034 public function traverseBackendDAEVars<ArgT>
4035 "Traverse all vars of a BackendDAE.Variables array."
4036 input BackendDAE.Variables inVariables;
4037 input FuncType inFunc;
4038 input ArgT inArg;
4039 output ArgT outArg;
4040
4041 partial function FuncType
4042 input BackendDAE.Var inVar;
4043 input ArgT inArg;
4044 output BackendDAE.Var outVar;
4045 output ArgT outArg;
4046 end FuncType;
4047 protected
4048 Integer num_vars;
4049 array<Option<BackendDAE.Var>> vars;
4050 algorithm
4051 329511 BackendDAE.VARIABLES(varArr=BackendDAE.VARIABLE_ARRAY(numberOfElements=num_vars, varOptArr=vars)) := inVariables;
4052 329511 outArg := BackendDAEUtil.traverseArrayNoCopy(vars, inFunc, traverseBackendDAEVars2, inArg, num_vars);
4053 end traverseBackendDAEVars;
4054
4055 partial function filterFunc
4056 input BackendDAE.Var var;
4057 output Boolean b;
4058 end filterFunc;
4059
4060 public function filterCrefs
4061 input BackendDAE.Variables variables;
4062 input filterFunc func;
4063 input output list<DAE.ComponentRef> acc;
4064 algorithm
4065 6509 acc := traverseBackendDAEVars(variables, function filterTraverse(func = func), acc);
4066 end filterCrefs;
4067
4068 protected function filterTraverse
4069 input output BackendDAE.Var var;
4070 input filterFunc func;
4071 input output list<DAE.ComponentRef> acc;
4072 algorithm
4073
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 22662 times.
✓ Branch 4 taken 22662 times.
✗ Branch 5 not taken.
22662 if func(var) then
4074 ✗ acc := var.varName :: acc;
4075 end if;
4076 end filterTraverse;
4077
4078 protected function traverseBackendDAEVars2<ArgT>
4079 input Option<BackendDAE.Var> inVar;
4080 input FuncType inFunc;
4081 input ArgT inArg;
4082 output ArgT outArg;
4083
4084 partial function FuncType
4085 input BackendDAE.Var inVar;
4086 input ArgT inArg;
4087 output BackendDAE.Var outVar;
4088 output ArgT outArg;
4089 end FuncType;
4090 algorithm
4091 outArg := match inVar
4092 local
4093 BackendDAE.Var v;
4094 ArgT arg;
4095
4096 case SOME(v)
4097 algorithm
4098
2/2
✓ Branch 0 taken 725774 times.
✓ Branch 1 taken 4003445 times.
4729219 (_, arg) := inFunc(v, inArg);
4099 4729219 then
4100 arg;
4101 else inArg;
4102 end match;
4103 end traverseBackendDAEVars2;
4104
4105 public function traverseBackendDAEVarsWithStop<ArgT>
4106 "Traverse all vars of a BackendDAE.Variables array."
4107 input BackendDAE.Variables inVariables;
4108 input FuncType inFunc;
4109 input ArgT inArg;
4110 output ArgT outArg;
4111
4112 partial function FuncType
4113 input BackendDAE.Var inVar;
4114 input ArgT inArg;
4115 output BackendDAE.Var outVar;
4116 output Boolean outContinue;
4117 output ArgT outArg;
4118 end FuncType;
4119 protected
4120 Integer num_vars;
4121 array<Option<BackendDAE.Var>> vars;
4122 algorithm
4123 ✗ BackendDAE.VARIABLES(varArr=BackendDAE.VARIABLE_ARRAY(numberOfElements=num_vars, varOptArr=vars)) := inVariables;
4124 ✗ outArg := BackendDAEUtil.traverseArrayNoCopyWithStop(vars, inFunc,traverseBackendDAEVarsWithStop2, inArg, num_vars);
4125 end traverseBackendDAEVarsWithStop;
4126
4127 protected function traverseBackendDAEVarsWithStop2<ArgT>
4128 input Option<BackendDAE.Var> inVar;
4129 input FuncType inFunc;
4130 input ArgT inArg;
4131 output Boolean outContinue;
4132 output ArgT outArg;
4133
4134 partial function FuncType
4135 input BackendDAE.Var inVar;
4136 input ArgT inArg;
4137 output BackendDAE.Var outVar;
4138 output Boolean outContinue;
4139 output ArgT outArg;
4140 end FuncType;
4141 algorithm
4142 (outContinue, outArg) := match inVar
4143 local
4144 BackendDAE.Var v;
4145 ArgT arg;
4146 Boolean cont;
4147
4148 case NONE() then (true, inArg);
4149 case SOME(v)
4150 algorithm
4151 ✗ (_, cont, arg) := inFunc(v, inArg);
4152 then
4153 (cont, arg);
4154 end match;
4155 end traverseBackendDAEVarsWithStop2;
4156
4157 public function traverseBackendDAE<ArgT>
4158 "Traverse all vars of a BackendDAE"
4159 input output BackendDAE.BackendDAE dae;
4160 input FuncType inFunc;
4161 input output ArgT arg;
4162
4163 partial function FuncType
4164 input output BackendDAE.Var var;
4165 input output ArgT arg;
4166 end FuncType;
4167 algorithm
4168
2/2
✓ Branch 0 taken 1583 times.
✓ Branch 1 taken 1085 times.
2668 for syst in dae.eqs loop
4169 1583 (_, arg) := traverseBackendDAEVarsWithUpdate(syst.orderedVars, inFunc, arg);
4170 end for;
4171
4172 1085 (_, arg) := traverseBackendDAEVarsWithUpdate(dae.shared.globalKnownVars, inFunc, arg);
4173 1085 (_, arg) := traverseBackendDAEVarsWithUpdate(dae.shared.localKnownVars, inFunc, arg);
4174 1085 (_, arg) := traverseBackendDAEVarsWithUpdate(dae.shared.externalObjects, inFunc, arg);
4175 1085 (_, arg) := traverseBackendDAEVarsWithUpdate(dae.shared.aliasVars, inFunc, arg);
4176 end traverseBackendDAE;
4177
4178 public function traverseBackendDAEVarsWithUpdate<ArgT>
4179 "Traverse all vars of a BackendDAE.Variables array."
4180 input BackendDAE.Variables inVariables;
4181 input FuncType inFunc;
4182 input ArgT inArg;
4183 output BackendDAE.Variables outVariables;
4184 output ArgT outArg;
4185
4186 partial function FuncType
4187 input BackendDAE.Var inVar;
4188 input ArgT inArg;
4189 output BackendDAE.Var outVar;
4190 output ArgT outArg;
4191 end FuncType;
4192 protected
4193 Integer num_vars1, num_vars2;
4194 array<Option<BackendDAE.Var>> vars;
4195 algorithm
4196 38319 BackendDAE.VARIABLES(varArr = BackendDAE.VARIABLE_ARRAY(num_vars1, vars), numberOfVars = num_vars2) := inVariables;
4197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38319 times.
38319 if num_vars1 <> num_vars2 then
4198 ✗ Error.addInternalError("function traverseBackendDAEVarsWithUpdate failed", sourceInfo());
4199 ✗ fail();
4200 end if;
4201 38319 (vars, outArg) := BackendDAEUtil.traverseArrayNoCopyWithUpdate(vars, inFunc, traverseBackendDAEVarsWithUpdate2, inArg, num_vars1);
4202 outVariables := inVariables;
4203
2/2
✓ Branch 1 taken 11781 times.
✓ Branch 2 taken 26538 times.
76638 outVariables.varArr := BackendDAE.VARIABLE_ARRAY(num_vars1, vars);
4204 end traverseBackendDAEVarsWithUpdate;
4205
4206 protected function traverseBackendDAEVarsWithUpdate2<ArgT>
4207 input Option<BackendDAE.Var> inVar;
4208 input FuncType inFunc;
4209 input ArgT inArg;
4210 output Option<BackendDAE.Var> outVar;
4211 output ArgT outArg;
4212
4213 partial function FuncType
4214 input BackendDAE.Var inVar;
4215 input ArgT inArg;
4216 output BackendDAE.Var outVar;
4217 output ArgT outArg;
4218 end FuncType;
4219 algorithm
4220 (outVar, outArg) := match inVar
4221 local
4222 Option<BackendDAE.Var> ov;
4223 BackendDAE.Var v, new_v;
4224 ArgT arg;
4225
4226 case NONE() then (inVar, inArg);
4227
4228 case SOME(v)
4229 algorithm
4230
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2532026 times.
2532026 (new_v, arg) := inFunc(v, inArg);
4231
2/2
✓ Branch 0 taken 636172 times.
✓ Branch 1 taken 1895854 times.
2532026 ov := if referenceEq(v, new_v) then inVar else SOME(new_v);
4232 2532026 then
4233 (ov, arg);
4234
4235 end match;
4236 end traverseBackendDAEVarsWithUpdate2;
4237
4238 public function getAllCrefFromVariables
4239 input BackendDAE.Variables inVariables;
4240 output list<DAE.ComponentRef> cr_lst;
4241 algorithm
4242 1153 cr_lst := traverseBackendDAEVars(inVariables,traversingVarCrefFinder,{});
4243 end getAllCrefFromVariables;
4244
4245 protected function traversingVarCrefFinder
4246 input BackendDAE.Var inVar;
4247 input list<DAE.ComponentRef> inCrefs;
4248 output BackendDAE.Var outVar;
4249 output list<DAE.ComponentRef> outCrefs;
4250 algorithm
4251 outVar := inVar;
4252 240 outCrefs := varCref(inVar) :: inCrefs;
4253 end traversingVarCrefFinder;
4254
4255 public function collectVarKindVarinVariables
4256 input BackendDAE.Var inVar;
4257 input tuple<checkVarKindFunc, BackendDAE.Variables> inVarArrays;
4258 output BackendDAE.Var outVar = inVar;
4259 output tuple<checkVarKindFunc, BackendDAE.Variables> outVarArrays = inVarArrays;
4260 partial function checkVarKindFunc
4261 input BackendDAE.Var inVar;
4262 output Boolean outArg;
4263 end checkVarKindFunc;
4264 protected
4265 BackendDAE.Variables vararray;
4266 checkVarKindFunc checkVarKind;
4267 algorithm
4268 16098 (checkVarKind, vararray) := inVarArrays;
4269 outVarArrays := match inVar
4270 local
4271 case _ guard(checkVarKind(inVar)) algorithm
4272 7745 vararray := BackendVariable.addVar(inVar, vararray);
4273 7745 then (checkVarKind, vararray);
4274
4275 else outVarArrays;
4276 end match;
4277 end collectVarKindVarinVariables;
4278
4279 public function getAllDiscreteVarFromVariables
4280 input BackendDAE.Variables inVariables;
4281 output list<BackendDAE.Var> v_lst;
4282 algorithm
4283 ✗ v_lst := traverseBackendDAEVars(inVariables,traversingisisVarDiscreteFinder,{});
4284 end getAllDiscreteVarFromVariables;
4285
4286 protected function traversingisisVarDiscreteFinder
4287 input BackendDAE.Var inVar;
4288 input list<BackendDAE.Var> inVars;
4289 output BackendDAE.Var v;
4290 output list<BackendDAE.Var> v_lst;
4291 algorithm
4292 v := inVar;
4293 ✗ v_lst := List.consOnTrue(isVarDiscrete(v),v,inVars);
4294 end traversingisisVarDiscreteFinder;
4295
4296 public function getAllStateVarFromVariables
4297 input BackendDAE.Variables inVariables;
4298 output list<BackendDAE.Var> v_lst;
4299 algorithm
4300 1246 v_lst := traverseBackendDAEVars(inVariables,traversingisStateVarFinder,{});
4301 end getAllStateVarFromVariables;
4302
4303 public function getAllClockedStatesFromVariables
4304 input BackendDAE.Variables inVariables;
4305 output list<BackendDAE.Var> v_lst;
4306 algorithm
4307 92 v_lst := traverseBackendDAEVars(inVariables,traversingisClockedStateVarFinder,{});
4308 end getAllClockedStatesFromVariables;
4309
4310 public function getNumStateVarFromVariables
4311 input BackendDAE.Variables inVariables;
4312 output Integer count;
4313 algorithm
4314 61585 count := traverseBackendDAEVars(inVariables,traversingisStateCount,0);
4315 end getNumStateVarFromVariables;
4316
4317 protected function traversingisStateVarFinder
4318 input BackendDAE.Var inVar;
4319 input list<BackendDAE.Var> inVars;
4320 output BackendDAE.Var v;
4321 output list<BackendDAE.Var> v_lst;
4322 algorithm
4323 v := inVar;
4324 67145 v_lst := List.consOnTrue(isStateVar(v),v,inVars);
4325 end traversingisStateVarFinder;
4326
4327 protected function traversingisClockedStateVarFinder
4328 input BackendDAE.Var inVar;
4329 input list<BackendDAE.Var> inVars;
4330 output BackendDAE.Var v;
4331 output list<BackendDAE.Var> v_lst;
4332 algorithm
4333 v := inVar;
4334 1617 v_lst := List.consOnTrue(isClockedStateVar(v),v,inVars);
4335 end traversingisClockedStateVarFinder;
4336
4337 protected function traversingisStateCount
4338 input output BackendDAE.Var v;
4339 input output Integer count;
4340 algorithm
4341
2/2
✓ Branch 1 taken 442488 times.
✓ Branch 2 taken 20342 times.
462830 if isStateVar(v) then
4342 20342 count := count + 1;
4343 end if;
4344 end traversingisStateCount;
4345
4346 public function getAllVarIndicesFromVariables
4347 input BackendDAE.Variables inVariables;
4348 input FindFunc isFunc;
4349 output list<BackendDAE.Var> v_lst;
4350 output list<Integer> i_lst;
4351 partial function FindFunc
4352 input BackendDAE.Var inElement;
4353 output Boolean result;
4354 end FindFunc;
4355 protected
4356 array<list<BackendDAE.Var>> v_a;
4357 array<list<Integer>> i_a;
4358 algorithm
4359 53156 v_a := arrayCreate(1,{});
4360 53156 i_a := arrayCreate(1,{});
4361 53156 traverseBackendDAEVars(inVariables,function traversingisXXXFinder(v_lst=v_a,i_lst=i_a,isFunc=isFunc), arrayCreate(1,1));
4362 v_lst := v_a[1];
4363 i_lst := i_a[1];
4364 end getAllVarIndicesFromVariables;
4365
4366 protected function traversingisXXXFinder
4367 "author: hkiel 2016-04"
4368 input output BackendDAE.Var inVar;
4369 input array<list<BackendDAE.Var>> v_lst;
4370 input array<list<Integer>> i_lst;
4371 input FindFunc isFunc;
4372 input output array<Integer> i;
4373 partial function FindFunc
4374 input BackendDAE.Var inElement;
4375 output Boolean result;
4376 end FindFunc;
4377 algorithm
4378
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 212625 times.
✓ Branch 4 taken 5216 times.
✓ Branch 5 taken 207409 times.
212625 if isFunc(inVar) then
4379 5216 arrayUpdate(v_lst,1,inVar::v_lst[1]);
4380 5216 arrayUpdate(i_lst,1,i[1]::i_lst[1]);
4381 end if;
4382 212625 i[1] := i[1]+1;
4383 end traversingisXXXFinder;
4384
4385 public function mergeVariableOperations
4386 input BackendDAE.Var inVar;
4387 input list<DAE.SymbolicOperation> inOps;
4388 output BackendDAE.Var outVar = inVar;
4389 protected
4390 list<DAE.SymbolicOperation> ops;
4391 algorithm
4392 161786 ops := listReverse(inOps);
4393 161786 outVar.source := List.foldr(ops, ElementSource.addSymbolicTransformation, inVar.source);
4394 end mergeVariableOperations;
4395
4396 public function mergeAliasVars "author: Frenkel TUD 2011-04"
4397 input BackendDAE.Var inVar;
4398 input BackendDAE.Var inAVar "the alias var";
4399 input Boolean negate;
4400 input BackendDAE.Variables globalKnownVars "the globalKnownVars, need to report Warnings";
4401 output BackendDAE.Var outVar;
4402 protected
4403 BackendDAE.Var v1,v2;
4404 Boolean fixed,fixeda;
4405 Option<DAE.Exp> sv,sva;
4406 Option<DAE.StartOrigin> so,soa;
4407 algorithm
4408 // get attributes
4409 // fixed
4410 50 fixed := varFixed(inVar);
4411 50 fixeda := varFixed(inAVar);
4412 // start
4413 50 sv := varStartValueOption(inVar);
4414 50 sva := varStartValueOption(inAVar);
4415 50 so := varStartOrigin(inVar);
4416 50 soa := varStartOrigin(inAVar);
4417 50 v1 := mergeStartFixed(inVar,fixed,sv,so,inAVar,fixeda,sva,soa,negate,globalKnownVars);
4418 // nominal
4419 50 v2 := mergeNominalAttribute(inAVar,v1,negate);
4420 // minmax
4421 50 outVar := mergeMinMaxAttribute(inAVar,v2,negate);
4422 end mergeAliasVars;
4423
4424 protected function mergeStartFixed
4425 "author: Frenkel TUD 2011-04"
4426 input BackendDAE.Var inVar;
4427 input Boolean fixed;
4428 input Option<DAE.Exp> sv;
4429 input Option<DAE.StartOrigin> so;
4430 input BackendDAE.Var inAVar;
4431 input Boolean fixeda;
4432 input Option<DAE.Exp> sva;
4433 input Option<DAE.StartOrigin> soa;
4434 input Boolean negate;
4435 input BackendDAE.Variables globalKnownVars "the globalKnownVars, need to report Warnings";
4436 output BackendDAE.Var outVar;
4437 algorithm
4438 outVar :=
4439 matchcontinue (inVar, fixed, sv, inAVar, fixeda, sva)
4440 local
4441 BackendDAE.Var v,v1,v2;
4442 DAE.ComponentRef cr,cra;
4443 DAE.Exp sa,sb,e;
4444 Integer i,ia;
4445 Option<DAE.StartOrigin> origin;
4446 DAE.Type ty,tya;
4447 // legal cases one fixed the other one not fixed, use the fixed one
4448 case (v, true, _, _, false, _)
4449 then v;
4450 case (v, false, _, _, true, SOME(sb))
4451 algorithm
4452 ✗ e := if negate then Expression.negate(sb) else sb;
4453 ✗ v1 := setVarStartValue(v,e);
4454 ✗ v2 := setVarFixed(v1,true);
4455 then v2;
4456 case (v, false, NONE(), _, true, NONE())
4457 algorithm
4458 ✗ v1 := setVarFixed(v,true);
4459 then v1;
4460 case (v, false, SOME(_), _, true, NONE())
4461 algorithm
4462 ✗ setVarStartValueOption(v,NONE());
4463 ✗ v1 := setVarFixed(v,true);
4464 then v1;
4465 // legal case both fixed=false
4466 case (v, false, NONE(), _, false, NONE())
4467 then v;
4468 case (v, false, SOME(_), _, false, NONE())
4469 then v;
4470 case (v, false, NONE(), _, false, SOME(sb))
4471 algorithm
4472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 e := if negate then Expression.negate(sb) else sb;
4473 6 v1 := setVarStartValue(v,e);
4474 then v1;
4475 case (v as BackendDAE.VAR(varType=ty), false, _, BackendDAE.VAR(varType=tya), false, _)
4476 algorithm
4477 ✗ sa := startValueType(sv,ty);
4478 ✗ sb := startValueType(sva,tya);
4479 ✗ e := if negate then Expression.negate(sb) else sb;
4480 ✗ (e,origin) := getNonZeroStart(false,sa,so,e,soa,globalKnownVars);
4481 ✗ setVarStartValue(v,e);
4482 ✗ v1 := setVarStartOrigin(v,origin);
4483 then v1;
4484 case (v as BackendDAE.VAR(varName=cr,varType=ty), false, _, BackendDAE.VAR(varName=cra,varType=tya), false, _)
4485 algorithm
4486 ✗ sa := startValueType(sv,ty);
4487 ✗ sb := startValueType(sva,tya);
4488 ✗ e := if negate then Expression.negate(sb) else sb;
4489 // according to MSL
4490 // use the value from the variable that is closer to the top of the
4491 // hierarchy i.e. A.B value has priority over X.Y.Z value!
4492 ✗ i := ComponentReference.crefDepth(cr);
4493 ✗ ia := ComponentReference.crefDepth(cra);
4494 ✗ then
4495 mergeStartFixed1(intLt(ia,i),v,cr,sa,cra,e,soa,negate," have start values ");
4496 // legal case both fixed = true and start exp equal
4497 case (v, true, NONE(), _, true, NONE())
4498 then v;
4499 case (v as BackendDAE.VAR(varType=ty), true, _, BackendDAE.VAR(varType=tya), true, _)
4500 algorithm
4501 ✗ sa := startValueType(sv,ty);
4502 ✗ sb := startValueType(sva,tya);
4503 ✗ e := if negate then Expression.negate(sb) else sb;
4504 ✗ (e,origin) := getNonZeroStart(true,sa,so,e,soa,globalKnownVars);
4505 ✗ setVarStartValue(v,e);
4506 ✗ v1 := setVarStartOrigin(v,origin);
4507 then v1;
4508 // not legal case both fixed with unequal start values
4509 case (v as BackendDAE.VAR(varName=cr,varType=ty), true, _, BackendDAE.VAR(varName=cra,varType=tya), true, _)
4510 algorithm
4511 ✗ sa := startValueType(sv,ty);
4512 ✗ sb := startValueType(sva,tya);
4513 ✗ e := if negate then Expression.negate(sb) else sb;
4514 // overconstrained system report warning/error
4515 ✗ i := ComponentReference.crefDepth(cr);
4516 ✗ ia := ComponentReference.crefDepth(cra);
4517 ✗ then
4518 mergeStartFixed1(intLt(ia,i),v,cr,sa,cra,e,soa,negate," both fixed and have start values ");
4519 end matchcontinue;
4520 end mergeStartFixed;
4521
4522 protected function startValueType "author: Frenkel TUD 2012-10
4523 return the start value or the default value in case of NONE()"
4524 input Option<DAE.Exp> iExp;
4525 input DAE.Type iTy;
4526 output DAE.Exp oExp;
4527 algorithm
4528 oExp := match iExp
4529 local
4530 DAE.Exp e;
4531 case SOME(e) then e;
4532 case NONE() guard Types.isRealOrSubTypeReal(iTy)
4533 then
4534 DAE.RCONST(0.0);
4535 case NONE() guard Types.isIntegerOrSubTypeInteger(iTy)
4536 then
4537 DAE.ICONST(0);
4538 case NONE() guard Types.isBooleanOrSubTypeBoolean(iTy)
4539 then
4540 DAE.BCONST(false);
4541 case NONE() guard Types.isStringOrSubTypeString(iTy)
4542 then
4543 DAE.SCONST("");
4544 case NONE() guard Types.isEnumerationOrSubTypeEnumeration(iTy)
4545 ✗ then
4546 Types.getNthEnumLiteral(iTy, 1);
4547 else
4548 DAE.RCONST(0.0);
4549 end match;
4550 end startValueType;
4551
4552 protected function mergeStartFixed1 "author: Frenkel TUD 2011-04"
4553 input Boolean b "true if Alias Var have less dots in the name";
4554 input BackendDAE.Var inVar;
4555 input DAE.ComponentRef cr;
4556 input DAE.Exp sv;
4557 input DAE.ComponentRef cra;
4558 input DAE.Exp sva;
4559 input Option<DAE.StartOrigin> soa;
4560 input Boolean negate;
4561 input String s4;
4562 output BackendDAE.Var outVar;
4563 algorithm
4564 outVar :=
4565 match b
4566 local
4567 String s,s1,s2,s3,s5,s6;
4568 BackendDAE.Var v;
4569 // alias var has more dots in the name
4570 case false
4571 algorithm
4572 ✗ s1 := ComponentReferenceBasics.printComponentRefStr(cr);
4573 ✗ s2 := if negate then " = -" else " = ";
4574 ✗ s3 := ComponentReferenceBasics.printComponentRefStr(cra);
4575 ✗ s5 := ExpressionBasics.printExpStr(sv);
4576 ✗ s6 := ExpressionBasics.printExpStr(sva);
4577 ✗ s := stringAppendList({"Alias variables ",s1,s2,s3,s4,s5," != ",s6,". Use value from ",s1,"."});
4578 ✗ Error.addMessage(Error.COMPILER_WARNING,{s});
4579 then
4580 inVar;
4581 case true
4582 algorithm
4583 ✗ s1 := ComponentReferenceBasics.printComponentRefStr(cr);
4584 ✗ s2 := if negate then " = -" else " = ";
4585 ✗ s3 := ComponentReferenceBasics.printComponentRefStr(cra);
4586 ✗ s5 := ExpressionBasics.printExpStr(sv);
4587 ✗ s6 := ExpressionBasics.printExpStr(sva);
4588 ✗ s := stringAppendList({"Alias variables ",s1,s2,s3,s4,s5," != ",s6,". Use value from ",s3,"."});
4589 ✗ Error.addMessage(Error.COMPILER_WARNING,{s});
4590 ✗ v := setVarStartValue(inVar,sva);
4591 ✗ v := setVarStartOrigin(v,soa);
4592 then
4593 v;
4594 end match;
4595 end mergeStartFixed1;
4596
4597 protected function replaceCrefWithBindExp
4598 input DAE.Exp inExp;
4599 input tuple<BackendDAE.Variables,Boolean,HashSet.HashSet> inTuple;
4600 output DAE.Exp outExp;
4601 output tuple<BackendDAE.Variables,Boolean,HashSet.HashSet> outTuple;
4602 algorithm
4603 (outExp,outTuple) := matchcontinue (inExp,inTuple)
4604 local
4605 DAE.Exp e;
4606 BackendDAE.Variables vars;
4607 DAE.ComponentRef cr;
4608 HashSet.HashSet hs;
4609 // true if crefs replaced in expression
4610 case (DAE.CREF(componentRef=cr), (vars,_,hs))
4611 algorithm
4612 // check for cyclic bindings in start value
4613 ✗ false := BaseHashSet.has(cr, hs);
4614 ✗ (BackendDAE.VAR(bindExp = SOME(e)), _) := getVarSingle(cr, vars);
4615 ✗ hs := BaseHashSet.add(cr,hs);
4616 ✗ (e, (_,_,hs)) := Expression.traverseExpBottomUp(e, replaceCrefWithBindExp, (vars,false,hs));
4617 ✗ then (e, (vars,true,hs));
4618 // true if crefs in expression
4619 case (e as DAE.CREF(), (vars,_,hs))
4620 ✗ then (e, (vars,true,hs));
4621 else (inExp,inTuple);
4622 end matchcontinue;
4623 end replaceCrefWithBindExp;
4624
4625 protected function getNonZeroStart
4626 "author: Frenkel TUD 2011-04"
4627 input Boolean mustBeEqual;
4628 input DAE.Exp exp1;
4629 input Option<DAE.StartOrigin> so;
4630 input DAE.Exp exp2;
4631 input Option<DAE.StartOrigin> sao;
4632 input BackendDAE.Variables globalKnownVars "the globalKnownVars, need to report Warnings";
4633 output DAE.Exp outExp;
4634 output Option<DAE.StartOrigin> outStartOrigin;
4635 algorithm
4636 (outExp,outStartOrigin) :=
4637 matchcontinue mustBeEqual
4638 local
4639 DAE.Exp exp2_1,exp1_1;
4640 Boolean b1,b2;
4641 Option<DAE.StartOrigin> origin;
4642 case _
4643 algorithm
4644 ✗ true := ExpressionBasics.expEqual(exp1,exp2);
4645 ✗ origin := if startOriginCompare(sao,so) < 0 then sao else so;
4646 then (exp1,origin);
4647 case false
4648 algorithm
4649 // strongest origin wins
4650 ✗ false := startOriginCompare(so,sao) == 0;
4651 ✗ (exp1_1,origin) := if startOriginCompare(sao,so) < 0 then (exp2,sao) else (exp1,so);
4652 then
4653 (exp1_1,origin);
4654 case _
4655 algorithm
4656 // simple evaluation, by replace crefs with bind expressions recursivly
4657 ✗ (exp1_1, (_,b1,_)) := Expression.traverseExpBottomUp(exp1, replaceCrefWithBindExp, (globalKnownVars,false,HashSet.emptyHashSet()));
4658 ✗ (exp2_1, (_,b2,_)) := Expression.traverseExpBottomUp(exp2, replaceCrefWithBindExp, (globalKnownVars,false,HashSet.emptyHashSet()));
4659 ✗ (exp1_1,_) := ExpressionSimplify.condsimplify(b1,exp1_1);
4660 ✗ (exp2_1,_) := ExpressionSimplify.condsimplify(b2,exp2_1);
4661 ✗ true := ExpressionBasics.expEqual(exp1_1, exp2_1);
4662 ✗ exp1_1 := if b1 then exp1 else exp2;
4663 ✗ origin := if startOriginCompare(sao,so) < 0 then sao else so;
4664 then
4665 (exp1_1,origin);
4666 end matchcontinue;
4667 end getNonZeroStart;
4668
4669 public function startOriginCompare
4670 "Compares two start origins by MLS 8.6.2 priority:
4671 negative if so1 is stronger, 0 if equal, positive if so2 is stronger."
4672 input Option<DAE.StartOrigin> so1;
4673 input Option<DAE.StartOrigin> so2;
4674 output Integer cmp;
4675 protected
4676 Integer k1,a1,r1,k2,a2,r2;
4677 algorithm
4678 176634 (k1,a1,r1) := startOriginRank(so1);
4679 176634 (k2,a2,r2) := startOriginRank(so2);
4680
4/4
✓ Branch 0 taken 30483 times.
✓ Branch 1 taken 146151 times.
✓ Branch 2 taken 3157 times.
✓ Branch 3 taken 142994 times.
176634 cmp := if k1 <> k2 then k1 - k2 elseif a1 <> a2 then a1 - a2 else r1 - r2;
4681 end startOriginCompare;
4682
4683 protected function startOriginRank
4684 "Rank triple for a start origin, lexicographic, lower = stronger.
4685 A start set on a component (CONFIDENCE) beats one set by its type
4686 (TYPE_CONFIDENCE), which beats the legacy old-frontend origins in their old
4687 relative order."
4688 input Option<DAE.StartOrigin> so;
4689 output Integer kind;
4690 output Integer actual = 0;
4691 output Integer raw = 0;
4692 algorithm
4693 kind := match so
4694 local
4695 DAE.StartOrigin origin;
4696 case SOME(origin as DAE.StartOrigin.CONFIDENCE())
4697 algorithm
4698 24440 actual := origin.actual;
4699 24440 raw := origin.raw;
4700 then 0;
4701 case SOME(origin as DAE.StartOrigin.TYPE_CONFIDENCE())
4702 algorithm
4703 8240 actual := origin.level;
4704 then 1;
4705 case SOME(DAE.StartOrigin.BINDING_ORIGIN()) then 2;
4706 case SOME(DAE.StartOrigin.TYPE_ORIGIN()) then 3;
4707 case SOME(DAE.StartOrigin.UNDEFINED_ORIGIN()) then 4;
4708 case NONE() then 5;
4709 end match;
4710 end startOriginRank;
4711
4712 public function mergeNominalAttribute
4713 input BackendDAE.Var inAVar;
4714 input BackendDAE.Var inVar;
4715 input Boolean negate;
4716 output BackendDAE.Var outVar;
4717 algorithm
4718 outVar :=
4719 matchcontinue (inAVar, inVar)
4720 local
4721 BackendDAE.Var v,var,var1;
4722 DAE.Exp e,e_1,e1,esum,eaverage;
4723 case (v, var)
4724 algorithm
4725 // nominal
4726 60 e := varNominalValue(v);
4727 ✗ e1 := varNominalValue(var);
4728 ✗ e_1 := if negate then Expression.negate(e) else e;
4729 ✗ esum := Expression.makeSum({e_1,e1});
4730 ✗ eaverage := Expression.expDiv(esum,DAE.RCONST(2.0)); // Real is legal because only Reals have nominal attribute
4731 ✗ (eaverage,_) := ExpressionSimplify.simplify(eaverage);
4732 ✗ var1 := setVarNominalValue(var,eaverage);
4733 then var1;
4734 case (v, var)
4735 algorithm
4736 // nominal
4737 60 e := varNominalValue(v);
4738 ✗ e_1 := if negate then Expression.negate(e) else e;
4739 ✗ var1 := setVarNominalValue(var,e_1);
4740 then var1;
4741 case(_, _) then inVar;
4742 end matchcontinue;
4743 end mergeNominalAttribute;
4744
4745 public function mergeMinMaxAttribute
4746 input BackendDAE.Var inAVar;
4747 input BackendDAE.Var inVar;
4748 input Boolean negate;
4749 output BackendDAE.Var outVar;
4750 algorithm
4751 outVar := matchcontinue (inAVar, inVar)
4752 local
4753 BackendDAE.Var v,var,var1;
4754 Option<DAE.VariableAttributes> attr,attr1;
4755 Option<DAE.Exp> min1, min2, max1, max2;
4756 DAE.ComponentRef cr,cr1;
4757
4758 case (v as BackendDAE.VAR(values = attr), var as BackendDAE.VAR(values = attr1))
4759 algorithm
4760 // minmax
4761 56 (min1, max1) := DAEUtil.getMinMaxValues(attr);
4762 56 (min2, max2) := DAEUtil.getMinMaxValues(attr1);
4763 56 cr := varCref(v);
4764 56 cr1 := varCref(var);
4765 56 (min1, max1) := mergeMinMax(negate, min1, min2, max1, max2, cr, cr1);
4766 56 var1 := setVarMinMax(var, min1, max1);
4767 then var1;
4768
4769 else inVar;
4770 end matchcontinue;
4771 end mergeMinMaxAttribute;
4772
4773 public function getMinMaxAttribute
4774 input BackendDAE.Var inVar;
4775 output Option<DAE.Exp> outMin;
4776 output Option<DAE.Exp> outMax;
4777 algorithm
4778 574 (outMin, outMax) := DAEUtil.getMinMaxValues(inVar.values);
4779 end getMinMaxAttribute;
4780
4781 protected function mergeMinMax
4782 input Boolean negate;
4783 input Option<DAE.Exp> inMin1;
4784 input Option<DAE.Exp> inMin2;
4785 input Option<DAE.Exp> inMax1;
4786 input Option<DAE.Exp> inMax2;
4787 input DAE.ComponentRef cr;
4788 input DAE.ComponentRef cr1;
4789 output Option<DAE.Exp> outMin;
4790 output Option<DAE.Exp> outMax;
4791 algorithm
4792 // In case of a = -b, min and max have to be changed and negated.
4793
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 outMin := if negate then Util.applyOption(inMin1, Expression.negate) else inMin1;
4794
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 outMax := if negate then Util.applyOption(inMax1, Expression.negate) else inMax1;
4795 56 outMin := mergeMin(outMin, inMin2);
4796 56 outMax := mergeMax(outMax, inMax2);
4797 56 checkMinMax(outMin, outMax, cr, cr1, negate);
4798 end mergeMinMax;
4799
4800 protected function checkMinMax
4801 input Option<DAE.Exp> inMin;
4802 input Option<DAE.Exp> inMax;
4803 input DAE.ComponentRef cr1;
4804 input DAE.ComponentRef cr2;
4805 input Boolean negate;
4806 algorithm
4807 () := matchcontinue(inMin, inMax)
4808 local
4809 DAE.Exp min, max;
4810 String s, s1, s2, s3, s4, s5;
4811 Real rmin, rmax;
4812
4813 case (SOME(min), SOME(max)) algorithm
4814 40 rmin := Expression.toReal(min);
4815 36 rmax := Expression.toReal(max);
4816
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 true := realGt(rmin, rmax);
4817 ✗ s1 := ComponentReferenceBasics.printComponentRefStr(cr1);
4818 ✗ s2 := if negate then " = -" else " = ";
4819 ✗ s3 := ComponentReferenceBasics.printComponentRefStr(cr2);
4820 ✗ s4 := ExpressionBasics.printExpStr(min);
4821 ✗ s5 := ExpressionBasics.printExpStr(max);
4822 ✗ s := stringAppendList({"Alias variables ", s1, s2, s3, " with invalid limits min ", s4, " > max ", s5});
4823 ✗ Error.addMessage(Error.COMPILER_WARNING, {s});
4824 then ();
4825
4826 // no error
4827 else ();
4828 end matchcontinue;
4829 end checkMinMax;
4830
4831 protected function mergeMin
4832 input Option<DAE.Exp> inMin1;
4833 input Option<DAE.Exp> inMin2;
4834 output Option<DAE.Exp> outMin;
4835 algorithm
4836 outMin := match(inMin1, inMin2)
4837 local
4838 DAE.Exp min1, min2, min;
4839
4840 case (SOME(min1), SOME(min2)) algorithm
4841 6 min := Expression.expMaxScalar(min1, min2);
4842 6 min := ExpressionSimplify.simplify(min);
4843
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 then if referenceEq(min,min1) then inMin1 elseif referenceEq(min,min2) then inMin2 else SOME(min);
4844
4845 case (NONE(), _) then inMin2;
4846 case (_, NONE()) then inMin1;
4847 else inMin1;
4848 end match;
4849 end mergeMin;
4850
4851 protected function mergeMax
4852 input Option<DAE.Exp> inMax1;
4853 input Option<DAE.Exp> inMax2;
4854 output Option<DAE.Exp> outMax;
4855 algorithm
4856 outMax := match(inMax1, inMax2)
4857 local
4858 DAE.Exp max1, max2, max;
4859
4860 case (SOME(max1), SOME(max2)) algorithm
4861 6 max := Expression.expMinScalar(max1, max2);
4862 6 max := ExpressionSimplify.simplify(max);
4863
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 then if referenceEq(max,max1) then inMax1 elseif referenceEq(max,max2) then inMax2 else SOME(max);
4864
4865 case (NONE(), _) then inMax2;
4866 case (_, NONE()) then inMax1;
4867 else inMax1;
4868 end match;
4869 end mergeMax;
4870
4871 public function calcAliasKey "author Frenkel TUD 2011-04
4872 This function is mainly usable to chose the favourite name of the kept var."
4873 input BackendDAE.Var inVar;
4874 output Integer i;
4875 protected
4876 Boolean b;
4877 Integer d;
4878 algorithm
4879 // records
4880 131190 b := ComponentReference.isRecord(inVar.varName);
4881
1/2
✓ Branch 0 taken 131190 times.
✗ Branch 1 not taken.
131190 i := if b then -1 else 0;
4882 // array elements
4883 131190 b := ComponentReference.isArrayElement(inVar.varName);
4884
2/2
✓ Branch 0 taken 39771 times.
✓ Branch 1 taken 91419 times.
131190 i := intAdd(i,if b then -1 else 0);
4885 // protected
4886 131190 b := isProtectedVar(inVar);
4887
2/2
✓ Branch 0 taken 100966 times.
✓ Branch 1 taken 30224 times.
131190 i := intAdd(i,if b then 5 else 0);
4888 // connectors
4889 131190 b := isVarConnector(inVar);
4890
2/2
✓ Branch 0 taken 71944 times.
✓ Branch 1 taken 59246 times.
131190 i := intAdd(i,if b then 1 else 0);
4891 // self generated var
4892 131190 b := isDummyDerVar(inVar);
4893
2/2
✓ Branch 0 taken 128536 times.
✓ Branch 1 taken 2654 times.
131190 i := intAdd(i,if b then 10 else 0);
4894 131190 b := selfGeneratedVar(inVar.varName);
4895
2/2
✓ Branch 0 taken 126420 times.
✓ Branch 1 taken 4770 times.
131190 i := intAdd(i,if b then 100 else 0);
4896 // length of name (number of dots)
4897 131190 d := ComponentReference.crefDepth(inVar.varName);
4898 131190 i := i+d;
4899 end calcAliasKey;
4900
4901 public function selfGeneratedVar
4902 input DAE.ComponentRef inCref;
4903 output Boolean b = StringUtil.startsWith(ComponentReference.crefStr(inCref), "$");
4904 end selfGeneratedVar;
4905
4906 public function varStateSelectPrioAlias "Helper function to calculateVarPriorities.
4907 Calculates a priority contribution bases on the stateSelect attribute."
4908 input BackendDAE.Var v;
4909 output Integer prio;
4910 protected
4911 DAE.StateSelect ss;
4912 Boolean knownDer;
4913 algorithm
4914 6474 ss := varStateSelect(v);
4915 6474 prio := stateSelectToInteger(ss);
4916 6474 knownDer := varHasStateDerivative(v);
4917 6474 prio := prio*2;
4918
2/2
✓ Branch 0 taken 4087 times.
✓ Branch 1 taken 2387 times.
6474 prio := if knownDer then prio+1 else prio;
4919 end varStateSelectPrioAlias;
4920
4921 public function stateSelectToInteger "Never: -1
4922 Avoid: 0
4923 Default: 1
4924 Prefer: 2
4925 Always: 3"
4926 input DAE.StateSelect inStateSelect;
4927 output Integer prio;
4928 algorithm
4929 prio := match inStateSelect
4930 case DAE.NEVER() then -1;
4931 case DAE.AVOID() then 0;
4932 case DAE.DEFAULT() then 1;
4933 case DAE.PREFER() then 2;
4934 case DAE.ALWAYS() then 3;
4935 end match;
4936 end stateSelectToInteger;
4937
4938 public function transformXToXd "author: PA
4939 this function transforms x variables (in the state vector)
4940 to corresponding xd variable (in the derivatives vector)"
4941 input BackendDAE.Var inVar;
4942 output BackendDAE.Var outVar;
4943 algorithm
4944 outVar := match inVar
4945 case BackendDAE.VAR(varKind=BackendDAE.STATE()) algorithm
4946 outVar := inVar;
4947 1564 outVar.varName := ComponentReference.crefPrefixDer(inVar.varName);
4948 1564 outVar.varKind := BackendDAE.STATE_DER();
4949 then outVar;
4950
4951 else inVar;
4952 end match;
4953 end transformXToXd;
4954
4955 public function setStateIndex"updates the index of the state var.
4956 author:Waurich TUD"
4957 input BackendDAE.Var v1;
4958 input Integer idx;
4959 output BackendDAE.Var v2 = v1;
4960 protected
4961 Option<DAE.ComponentRef> derName;
4962 Boolean natural;
4963 algorithm
4964 ✗ if isStateVar(v1) then
4965 ✗ BackendDAE.STATE(_,derName,natural) := getVarKind(v1);
4966 ✗ v2 := setVarKind(v1,BackendDAE.STATE(idx,derName,natural));
4967 end if;
4968 end setStateIndex;
4969
4970 public function isRecordVar "outputs true if the variable belongs to a record.
4971 author: Waurich TUD 2014-09"
4972 input BackendDAE.Var inVar;
4973 output Boolean isRec = ComponentReference.traverseCref(inVar.varName, ComponentReference.crefIsRec, false);
4974 end isRecordVar;
4975
4976 public function varExp
4977 input BackendDAE.Var inVar;
4978 output DAE.Exp outExp;
4979 algorithm
4980 398985 outExp := Expression.crefToExp(inVar.varName);
4981 end varExp;
4982
4983 public function varExp2 "same as varExp but adds a der()-call for state derivatives"
4984 input BackendDAE.Var inVar;
4985 output DAE.Exp outExp;
4986 algorithm
4987 outExp := match inVar
4988 local
4989 DAE.Exp exp;
4990
4991 case BackendDAE.VAR(varKind=BackendDAE.STATE(index=1)) algorithm
4992 ✗ exp := Expression.crefExp(inVar.varName);
4993 ✗ then Expression.expDer(exp);
4994
4995 ✗ else Expression.crefExp(inVar.varName);
4996 end match;
4997 end varExp2;
4998
4999 public function scalarizeVariables
5000 input output BackendDAE.Variables vars;
5001 protected
5002 list<BackendDAE.Var> var_lst, new_var_lst = {};
5003 algorithm
5004 2164 var_lst := varList(vars);
5005
2/2
✓ Branch 0 taken 129970 times.
✓ Branch 1 taken 2164 times.
132134 for var in var_lst loop
5006 129970 new_var_lst := scalarizeVar(var, new_var_lst);
5007 end for;
5008 2164 vars := listVar(listReverse(new_var_lst));
5009 end scalarizeVariables;
5010
5011 public function scalarizeVar
5012 input BackendDAE.Var var;
5013 input output list<BackendDAE.Var> scalar_vars = {};
5014 protected
5015 list<DAE.ComponentRef> scalar_crefs;
5016 BackendDAE.Var scalar_var;
5017 algorithm
5018
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 129964 times.
129970 if Types.isArray(var.varType) then
5019 6 scalar_crefs := ComponentReference.expandCref(var.varName, false);
5020
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 6 times.
24 for cref in scalar_crefs loop
5021 18 scalar_var := BackendVariable.copyVarNewName(cref, var);
5022 18 scalar_var.varType := ComponentReference.crefTypeFull(cref);
5023 scalar_vars := scalar_var :: scalar_vars;
5024 end for;
5025 else
5026 scalar_vars := var :: scalar_vars;
5027 end if;
5028 end scalarizeVar;
5029
5030 annotation(__OpenModelica_Interface="backend");
5031 end BackendVariable;
5032