-
Notifications
You must be signed in to change notification settings - Fork 297
/
NBVariable.mo
1966 lines (1790 loc) · 79.4 KB
/
NBVariable.mo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2021, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
encapsulated package NBVariable
" file: NBVariable.mo
description: This ONLY contains the backend variable functions!
===================================================================
kabdelhak: The Variable declarations for this file are defined in
NFVariable.mo to avoid that the FrontEnd depends on the BackEnd
due to the component references containing an InstNode with a
pointer to a variable.
===================================================================
"
public
//OF Imports
import SCodeUtil;
//NF Imports
import Attributes = NFAttributes;
import BackendExtension = NFBackendExtension;
import NFBackendExtension.{BackendInfo, VariableKind, VariableAttributes};
import NFBinding.Binding;
import ComponentRef = NFComponentRef;
import Dimension = NFDimension;
import Expression = NFExpression;
import NFInstNode.InstNode;
import Prefixes = NFPrefixes;
import Scalarize = NFScalarize;
import Subscript = NFSubscript;
import Type = NFType;
import Variable = NFVariable;
// Backend Imports
import NBAdjacency.Mapping;
import BackendDAE = NBackendDAE;
import BackendUtil = NBBackendUtil;
import NBEquation.Iterator;
import BVariable = NBVariable;
//Util Imports
import Array;
import BaseHashTable;
import ExpandableArray;
import Slice = NBSlice;
import StringUtil;
import UnorderedMap;
import Util;
public
type VariablePointer = Pointer<Variable> "mainly used for mapping purposes";
// ==========================================================================
// Single Variable constants and functions
// ==========================================================================
constant Variable DUMMY_VARIABLE = Variable.VARIABLE(ComponentRef.EMPTY(), Type.ANY(),
NFBinding.EMPTY_BINDING, NFPrefixes.Visibility.PUBLIC, NFAttributes.DEFAULT_ATTR,
{}, {}, NONE(), SCodeUtil.dummyInfo, NFBackendExtension.DUMMY_BACKEND_INFO);
constant Variable TIME_VARIABLE = Variable.VARIABLE(NFBuiltin.TIME_CREF, Type.REAL(),
NFBinding.EMPTY_BINDING, NFPrefixes.Visibility.PUBLIC, NFAttributes.DEFAULT_ATTR,
{}, {}, NONE(), SCodeUtil.dummyInfo, BackendExtension.BACKEND_INFO(
VariableKind.TIME(), NFBackendExtension.EMPTY_VAR_ATTR_REAL, NFBackendExtension.EMPTY_ANNOTATIONS, NONE()));
constant String DERIVATIVE_STR = "$DER";
constant String DUMMY_DERIVATIVE_STR = "$dDER";
constant String PARTIAL_DERIVATIVE_STR = "$pDER";
constant String FUNCTION_DERIVATIVE_STR = "$fDER";
constant String FUNCTION_STR = "$FUN";
constant String PREVIOUS_STR = "$PRE";
constant String AUXILIARY_STR = "$AUX";
constant String START_STR = "$START";
constant String RESIDUAL_STR = "$RES";
constant String TEMPORARY_STR = "$TMP";
constant String SEED_STR = "$SEED";
constant String TIME_EVENT_STR = "$TEV";
constant String STATE_EVENT_STR = "$SEV";
function toString
input Variable var;
input output String str = "";
protected
String attr;
algorithm
attr := VariableAttributes.toString(var.backendinfo.attributes);
str := str + VariableKind.toString(var.backendinfo.varKind) + " (" + intString(Variable.size(var)) + ") " + Variable.toString(var) + (if attr == "" then "" else " " + attr);
end toString;
function pointerToString
input Pointer<Variable> var_ptr;
output String str = toString(Pointer.access(var_ptr));
end pointerToString;
function nameString
input Pointer<Variable> var_ptr;
output String str = ComponentRef.toString(getVarName(var_ptr));
end nameString;
function hash
input Pointer<Variable> var_ptr;
output Integer i = Variable.hash(Pointer.access(var_ptr));
end hash;
function equalName
input Pointer<Variable> var_ptr1;
input Pointer<Variable> var_ptr2;
output Boolean b = Variable.equalName(Pointer.access(var_ptr1), Pointer.access(var_ptr2));
end equalName;
function size
input Pointer<Variable> var_ptr;
output Integer s = Variable.size(Pointer.access(var_ptr));
end size;
function fromCref
input ComponentRef cref;
input Attributes attr = NFAttributes.DEFAULT_ATTR;
input Binding binding = NFBinding.EMPTY_BINDING;
output Variable variable;
protected
InstNode node;
Type ty;
Prefixes.Visibility vis;
SourceInfo info;
algorithm
node := ComponentRef.node(cref);
ty := ComponentRef.getSubscriptedType(cref, true);
vis := InstNode.visibility(node);
info := InstNode.info(node);
variable := Variable.VARIABLE(cref, ty, binding, vis, attr, {}, {}, NONE(), info, NFBackendExtension.DUMMY_BACKEND_INFO);
end fromCref;
function makeVarPtrCyclic
"Needs a prepared variable and name cref and creates a cyclic dependency between
a pointer to the variable and its component reference."
input Variable var;
output Pointer<Variable> var_ptr;
input output ComponentRef name;
algorithm
var_ptr := Pointer.create(var);
name := BackendDAE.lowerComponentReferenceInstNode(name, var_ptr);
var.name := name;
Pointer.update(var_ptr, var);
end makeVarPtrCyclic;
function connectPrePostVar
"sets the pre() var for the variable and also sets the variable pointer at the pre() variable"
input Pointer<Variable> var_ptr;
input Pointer<Variable> pre_ptr;
protected
Variable var = Pointer.access(var_ptr);
Variable pre = Pointer.access(pre_ptr);
algorithm
var.backendinfo := BackendInfo.setPrePost(var.backendinfo, SOME(pre_ptr));
pre.backendinfo := BackendInfo.setPrePost(pre.backendinfo, SOME(var_ptr));
Pointer.update(var_ptr, var);
Pointer.update(pre_ptr, pre);
end connectPrePostVar;
function getVar
input ComponentRef cref;
output Variable var;
algorithm
var := Pointer.access(getVarPointer(cref));
end getVar;
// The following functions provide layers of protection. Whenever accessing names or pointers use these!
function getVarPointer
input ComponentRef cref;
output Pointer<Variable> var;
algorithm
var := match cref
local
Pointer<Variable> varPointer;
case ComponentRef.CREF(node = InstNode.VAR_NODE(varPointer = varPointer)) then varPointer;
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for " + ComponentRef.toString(cref) +
", because of wrong InstNode (not VAR_NODE). Show lowering errors with -d=failtrace."});
then fail();
end match;
end getVarPointer;
function getVarName
input Pointer<Variable> var_ptr;
output ComponentRef name;
protected
Variable var = Pointer.access(var_ptr);
algorithm
name := var.name;
end getVarName;
function setVarName
input output Pointer<Variable> var_ptr;
input ComponentRef name;
protected
Variable var = Pointer.access(var_ptr);
algorithm
var.name := name;
Pointer.update(var_ptr, var);
end setVarName;
function subIdxName
"creates new variable pointer to not change the old variable!"
input output Pointer<Variable> var_ptr;
input Pointer<Integer> index;
protected
Variable var = Pointer.access(var_ptr);
algorithm
var.name := ComponentRef.rename(ComponentRef.firstName(var.name) + "_" + intString(Pointer.access(index)), var.name);
var_ptr := Pointer.create(var);
end subIdxName;
function toExpression
input Pointer<Variable> var_ptr;
output Expression exp = Expression.fromCref(getVarName(var_ptr));
end toExpression;
partial function checkVar
input Pointer<Variable> var_ptr;
output Boolean b;
end checkVar;
function isArray extends checkVar;
protected
Variable var = Pointer.access(var_ptr);
algorithm
b := Type.isArray(var.ty);
end isArray;
function getDimensions
input Pointer<Variable> var_ptr;
output List<Dimension> dims;
protected
Variable var;
algorithm
var := Pointer.access(var_ptr);
dims := Type.arrayDims(var.ty);
end getDimensions;
function isEmpty extends checkVar;
algorithm
b := ComponentRef.isEmpty(getVarName(var_ptr));
end isEmpty;
function isState extends checkVar;
algorithm
b := match Pointer.access(var_ptr)
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.STATE())) then true;
else false;
end match;
end isState;
function isStateDerivative extends checkVar;
algorithm
b := match Pointer.access(var_ptr)
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.STATE_DER())) then true;
else false;
end match;
end isStateDerivative;
function isAlgebraic extends checkVar;
algorithm
b := match Pointer.access(var_ptr)
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.ALGEBRAIC())) then true;
else false;
end match;
end isAlgebraic;
function isStart extends checkVar;
algorithm
b := match Pointer.access(var_ptr)
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.START())) then true;
else false;
end match;
end isStart;
function isTime extends checkVar;
algorithm
b := match Pointer.access(var_ptr)
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.TIME())) then true;
else false;
end match;
end isTime;
function isContinuous extends checkVar;
algorithm
b := match Pointer.access(var_ptr)
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.DISCRETE_STATE())) then false;
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.DISCRETE())) then false;
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.PREVIOUS())) then false;
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.PARAMETER())) then false;
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.CONSTANT())) then false;
else true;
end match;
end isContinuous;
function isDiscreteState extends checkVar;
algorithm
b := match Pointer.access(var_ptr)
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.DISCRETE_STATE())) then true;
else false;
end match;
end isDiscreteState;
function isDiscrete extends checkVar;
algorithm
b := match Pointer.access(var_ptr)
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.DISCRETE())) then true;
else false;
end match;
end isDiscrete;
function isPrevious extends checkVar;
algorithm
b := match Pointer.access(var_ptr)
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.PREVIOUS())) then true;
else false;
end match;
end isPrevious;
function getPrePost
"gets the pre() / previous() var if its a variable / clocked variable or the other way around"
input Pointer<Variable> var_ptr;
output Option<Pointer<Variable>> pre_post;
protected
Variable var = Pointer.access(var_ptr);
algorithm
pre_post := var.backendinfo.pre_post;
end getPrePost;
function getPrePostCref
"only use if you are sure there is a pre-post variable"
input ComponentRef cref;
output ComponentRef pre_post;
protected
Option<Pointer<Variable>> pre_post_opt;
algorithm
pre_post_opt := getPrePost(getVarPointer(cref));
if Util.isSome(pre_post_opt) then
pre_post := getVarName(Util.getOption(pre_post_opt));
else
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for " + ComponentRef.toString(cref) + " because it had no pre or post variable."});
fail();
end if;
end getPrePostCref;
function hasPre
"only returns true if the variable itself is not a pre() or previous() and has a pre() pointer set"
extends checkVar;
algorithm
b := not isPrevious(var_ptr) and Util.isSome(getPrePost(var_ptr));
end hasPre;
function isDummyState extends checkVar;
algorithm
b := match Pointer.access(var_ptr)
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.DUMMY_STATE())) then true;
else false;
end match;
end isDummyState;
function isDummyDer extends checkVar;
algorithm
b := match Pointer.access(var_ptr)
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.DUMMY_DER())) then true;
else false;
end match;
end isDummyDer;
function isParamOrConst extends checkVar;
algorithm
b := match Pointer.access(var_ptr)
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.PARAMETER())) then true;
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.CONSTANT())) then true;
else false;
end match;
end isParamOrConst;
function isConst extends checkVar;
algorithm
b := match Pointer.access(var_ptr)
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.CONSTANT())) then true;
else false;
end match;
end isConst;
function isKnown extends checkVar;
algorithm
b := match Pointer.access(var_ptr)
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.PARAMETER())) then true;
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.CONSTANT())) then true;
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.STATE())) then true;
else false;
end match;
end isKnown;
function isDAEResidual extends checkVar;
algorithm
b := match Pointer.access(var_ptr)
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.DAE_RESIDUAL_VAR())) then true;
else false;
end match;
end isDAEResidual;
function isSeed extends checkVar;
algorithm
b := match Pointer.access(var_ptr)
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.SEED_VAR())) then true;
else false;
end match;
end isSeed;
function isInput extends checkVar;
algorithm
b := match Pointer.access(var_ptr)
case Variable.VARIABLE(attributes = Attributes.ATTRIBUTES(direction = NFPrefixes.Direction.INPUT)) then true;
else false;
end match;
end isInput;
function isOutput extends checkVar;
algorithm
b := match Pointer.access(var_ptr)
case Variable.VARIABLE(attributes = Attributes.ATTRIBUTES(direction = NFPrefixes.Direction.OUTPUT)) then true;
else false;
end match;
end isOutput;
function isFixed extends checkVar;
algorithm
b := match Pointer.access(var_ptr)
local
Expression fixed;
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(attributes = BackendExtension.VAR_ATTR_REAL(fixed = SOME(fixed)))) then Expression.isAllTrue(fixed);
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(attributes = BackendExtension.VAR_ATTR_INT(fixed = SOME(fixed)))) then Expression.isAllTrue(fixed);
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(attributes = BackendExtension.VAR_ATTR_BOOL(fixed = SOME(fixed)))) then Expression.isAllTrue(fixed);
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(attributes = BackendExtension.VAR_ATTR_STRING(fixed = SOME(fixed)))) then Expression.isAllTrue(fixed);
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(attributes = BackendExtension.VAR_ATTR_ENUMERATION(fixed = SOME(fixed)))) then Expression.isAllTrue(fixed);
else false;
end match;
end isFixed;
function isFixable
"states, discretes and parameters are fixable if they are not already fixed.
discrete states are always fixable. previous vars are only fixable if the discrete state for it wasn't fixed."
extends checkVar;
algorithm
b := match Pointer.access(var_ptr)
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.STATE())) then not isFixed(var_ptr);
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.ALGEBRAIC())) then not isFixed(var_ptr) or hasPre(var_ptr);
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.DISCRETE())) then not isFixed(var_ptr) or hasPre(var_ptr);
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.DISCRETE_STATE())) then not isFixed(var_ptr) or hasPre(var_ptr);
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.PARAMETER())) then not isFixed(var_ptr);
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.PREVIOUS())) then true;
else false;
end match;
end isFixable;
function isStateSelect
"checks if a variable has a certain StateSelect attribute"
extends checkVar;
input BackendExtension.StateSelect stateSelect;
algorithm
b := match Pointer.access(var_ptr)
local
VariableAttributes attributes;
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(attributes = attributes))
then VariableAttributes.getStateSelect(attributes) == stateSelect;
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for " + toString(Pointer.access(var_ptr))});
then fail();
end match;
end isStateSelect;
function getVariableAttributes
input Variable var;
output VariableAttributes variableAttributes = var.backendinfo.attributes;
end getVariableAttributes;
function setVariableAttributes
input output Variable var;
input VariableAttributes variableAttributes;
algorithm
var := match var
local
BackendExtension.BackendInfo backendinfo;
case NFVariable.VARIABLE(backendinfo = backendinfo) algorithm
backendinfo.attributes := variableAttributes;
var.backendinfo := backendinfo;
then var;
end match;
end setVariableAttributes;
function setVarKind
"use with caution: some variable kinds have extra information that needs to be correct"
input output Pointer<Variable> varPointer;
input VariableKind varKind;
protected
Variable var;
algorithm
var := Pointer.access(varPointer);
var.backendinfo := BackendExtension.BackendInfo.setVarKind(var.backendinfo, varKind);
Pointer.update(varPointer, var);
end setVarKind;
function isDummyVariable
"Returns true, if the variable is a dummy variable.
Note: !Only works in the backend, will return true for any variable if used
during frontend!"
extends checkVar;
algorithm
b := match Pointer.access(var_ptr)
case NFVariable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.FRONTEND_DUMMY())) then true;
else false;
end match;
end isDummyVariable;
function isFunctionAlias
input Pointer<Variable> var;
output Boolean b = false;
protected
String str = ComponentRef.firstName(getVarName(var));
algorithm
if stringLength(str) > 3 then
b := substring(str, 1, 4) == FUNCTION_STR;
end if;
end isFunctionAlias;
function createTimeVar
output Pointer<Variable> var_ptr;
protected
Variable var = TIME_VARIABLE;
algorithm
(var_ptr, _) := makeVarPtrCyclic(var, var.name);
end createTimeVar;
function makeStateVar
"Updates a variable pointer to be a state, requires the pointer to its derivative."
input Pointer<Variable> varPointer;
input Pointer<Variable> derivative;
protected
Variable var;
algorithm
var := Pointer.access(varPointer);
var.backendinfo := BackendExtension.BackendInfo.setVarKind(var.backendinfo, BackendExtension.STATE(1, SOME(derivative), true));
Pointer.update(varPointer, var);
end makeStateVar;
function makeAlgStateVar
"Updates a variable pointer to be an algebraic state.
Only if it currently is an algebraic variable, required for DAEMode."
input Pointer<Variable> varPointer;
protected
Variable var;
algorithm
if isAlgebraic(varPointer) then
var := Pointer.access(varPointer);
var.backendinfo := BackendExtension.BackendInfo.setVarKind(var.backendinfo, BackendExtension.ALG_STATE());
Pointer.update(varPointer, var);
end if;
end makeAlgStateVar;
function makeDerVar
"Creates a derivative variable pointer from the state cref.
e.g. height -> $DER.height"
input ComponentRef cref "old component reference";
output ComponentRef der_cref "new component reference";
output Pointer<Variable> var_ptr "pointer to new variable";
algorithm
() := match ComponentRef.node(cref)
local
InstNode derNode;
Pointer<Variable> state, dummy_ptr = Pointer.create(DUMMY_VARIABLE);
Variable var;
case InstNode.VAR_NODE()
algorithm
state := getVarPointer(cref);
derNode := InstNode.VAR_NODE(DERIVATIVE_STR, dummy_ptr);
der_cref := ComponentRef.append(cref, ComponentRef.fromNode(derNode, ComponentRef.scalarType(cref)));
var := fromCref(der_cref, Variable.attributes(Pointer.access(state)));
var.backendinfo := BackendExtension.BackendInfo.setVarKind(var.backendinfo, BackendExtension.STATE_DER(state, NONE()));
(var_ptr, der_cref) := makeVarPtrCyclic(var, der_cref);
then ();
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for " + ComponentRef.toString(cref)});
then fail();
end match;
end makeDerVar;
function getStateVar
input Pointer<Variable> der_var;
output Pointer<Variable> state_var;
algorithm
state_var := match Pointer.access(der_var)
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.STATE_DER(state = state_var)))
then state_var;
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for " + pointerToString(der_var) + " because of wrong variable kind."});
then fail();
end match;
end getStateVar;
function getStateCref
"Returns the state variable component reference from a state derivative component reference.
Only works after the state has been detected by the DetectStates module and fails for non-state derivative crefs!"
input output ComponentRef cref;
algorithm
cref := match cref
local
Pointer<Variable> state, derivative;
Variable stateVar;
case ComponentRef.CREF(node = InstNode.VAR_NODE(varPointer = derivative)) then match Pointer.access(derivative)
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.STATE_DER(state = state)))
algorithm
stateVar := Pointer.access(state);
then stateVar.name;
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for " + ComponentRef.toString(cref) + " because of wrong variable kind."});
then fail();
end match;
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for " + ComponentRef.toString(cref) + " because of wrong InstNode type."});
then fail();
end match;
end getStateCref;
function getDerVar
input Pointer<Variable> state_var;
output Pointer<Variable> der_var;
algorithm
der_var := match Pointer.access(state_var)
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.STATE(derivative = SOME(der_var))))
then der_var;
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for " + pointerToString(state_var) + " because of wrong variable kind."});
then fail();
end match;
end getDerVar;
function getDerCref
"Returns the derivative variable component reference from a state component reference.
Only works after the state has been detected by the DetectStates module and fails for non-state crefs!"
input output ComponentRef cref;
algorithm
cref := match cref
local
Pointer<Variable> state, derivative;
Variable derVar;
case ComponentRef.CREF(node = InstNode.VAR_NODE(varPointer = state)) then match Pointer.access(state)
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.STATE(derivative = SOME(derivative))))
algorithm
derVar := Pointer.access(derivative);
then derVar.name;
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for " + ComponentRef.toString(cref) + " because of wrong variable kind."});
then fail();
end match;
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for " + ComponentRef.toString(cref) + " because of wrong InstNode type."});
then fail();
end match;
end getDerCref;
function getRecordChildren
"returns all children of the variable if its a record, otherwise returns empty list"
input Pointer<Variable> var;
output list<Pointer<Variable>> children;
algorithm
children := match Pointer.access(var)
local
VariableKind varKind;
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = varKind as BackendExtension.RECORD()))
then varKind.children;
else {};
end match;
end getRecordChildren;
function makeDummyState
input Pointer<Variable> varPointer;
output Pointer<Variable> derivative;
protected
Variable var;
algorithm
var := Pointer.access(varPointer);
var.backendinfo := match BackendExtension.BackendInfo.getVarKind(var.backendinfo)
local
VariableKind varKind;
Variable der_var;
case varKind as BackendExtension.STATE(derivative = SOME(derivative)) algorithm
// also update the derivative to be a dummy derivative
der_var := Pointer.access(derivative);
der_var.backendinfo := BackendExtension.BackendInfo.setVarKind(der_var.backendinfo, BackendExtension.DUMMY_DER(varPointer));
Pointer.update(derivative, der_var);
then BackendExtension.BackendInfo.setVarKind(var.backendinfo, BackendExtension.DUMMY_STATE(derivative));
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for " + ComponentRef.toString(getVarName(varPointer)) + "."});
then fail();
end match;
Pointer.update(varPointer, var);
end makeDummyState;
function getDummyDerCref
"Returns the dummy derivative variable component reference from a dummy state component reference.
Only works after the dummy state has been created by the IndexReduction module and fails for non-dummy-state crefs!"
input output ComponentRef cref;
algorithm
cref := match cref
local
Pointer<Variable> dummy_state, dummy_derivative;
Variable dummy_derVar;
case ComponentRef.CREF(node = InstNode.VAR_NODE(varPointer = dummy_state)) then match Pointer.access(dummy_state)
case Variable.VARIABLE(backendinfo = BackendExtension.BACKEND_INFO(varKind = BackendExtension.DUMMY_STATE(dummy_der = dummy_derivative)))
algorithm
dummy_derVar := Pointer.access(dummy_derivative);
then dummy_derVar.name;
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for " + ComponentRef.toString(cref) + " because of wrong variable kind."});
then fail();
end match;
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for " + ComponentRef.toString(cref) + " because of wrong InstNode type."});
then fail();
end match;
end getDummyDerCref;
function makeDiscreteStateVar
"Updates a discrete variable pointer to be a discrete state, requires the pointer to its left limit (pre) variable."
input Pointer<Variable> varPointer;
protected
Variable var = Pointer.access(varPointer);
algorithm
var.backendinfo := BackendExtension.BackendInfo.setVarKind(var.backendinfo, BackendExtension.DISCRETE_STATE(false));
Pointer.update(varPointer, var);
end makeDiscreteStateVar;
function makePreVar
"Creates a previous variable pointer from the variable cref.
e.g. isOpen -> $PRE.isOpen"
input ComponentRef cref "old component reference";
output ComponentRef pre_cref "new component reference";
output Pointer<Variable> pre_ptr "pointer to new variable";
algorithm
() := match ComponentRef.node(cref)
local
InstNode qual;
Pointer<Variable> var_ptr;
Variable pre;
case qual as InstNode.VAR_NODE()
algorithm
var_ptr := BVariable.getVarPointer(cref);
qual.name := PREVIOUS_STR;
pre_cref := ComponentRef.append(cref, ComponentRef.fromNode(qual, ComponentRef.scalarType(cref)));
pre := fromCref(pre_cref, Variable.attributes(Pointer.access(var_ptr)));
pre.backendinfo := BackendExtension.BackendInfo.setVarKind(pre.backendinfo, BackendExtension.PREVIOUS());
(pre_ptr, pre_cref) := makeVarPtrCyclic(pre, pre_cref);
connectPrePostVar(var_ptr, pre_ptr);
then ();
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for " + ComponentRef.toString(cref)});
then fail();
end match;
end makePreVar;
function makeSeedVar
"Creates a seed variable pointer from a cref. Used in NBJacobian and NBHessian
to represent generic gradient equations.
e.g: (speed, 'Jac') -> $SEED_Jac.speed"
input output ComponentRef cref "old component reference to new component reference";
input String name "name of the matrix this seed belongs to";
output Pointer<Variable> var_ptr "pointer to new variable";
algorithm
() := match ComponentRef.node(cref)
local
InstNode qual;
Pointer<Variable> old_var_ptr;
Variable var;
case qual as InstNode.VAR_NODE()
algorithm
// get the variable pointer from the old cref to later on link back to it
old_var_ptr := BVariable.getVarPointer(cref);
// prepend the seed str and the matrix name and create the new cref
qual.name := SEED_STR + "_" + name;
cref := ComponentRef.append(cref, ComponentRef.fromNode(qual, ComponentRef.scalarType(cref)));
var := fromCref(cref, NFAttributes.IMPL_DISCRETE_ATTR);
// update the variable to be a seed and pass the pointer to the original variable
var.backendinfo := BackendExtension.BackendInfo.setVarKind(var.backendinfo, BackendExtension.SEED_VAR(old_var_ptr));
// create the new variable pointer and safe it to the component reference
(var_ptr, cref) := makeVarPtrCyclic(var, cref);
then ();
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for " + ComponentRef.toString(cref)});
then fail();
end match;
end makeSeedVar;
function makePDerVar
"Creates a partial derivative variable pointer from a cref. Used in NBJacobian and NBHessian
to represent generic gradient equations.
e.g: (speed, 'Jac') -> $pDer_Jac.speed"
input ComponentRef cref "old component reference";
input String name "name of the matrix this partial derivative belongs to";
input Boolean isTmp "sets variable kind for tmpVar or resultVar accordingly";
output ComponentRef pder_cref "new component reference";
output Pointer<Variable> var_ptr "pointer to new variable";
protected
VariableKind varKind = if isTmp then BackendExtension.JAC_TMP_VAR() else BackendExtension.JAC_VAR();
algorithm
() := match ComponentRef.node(cref)
local
InstNode qual;
Variable var;
// regular case for jacobians
case qual as InstNode.VAR_NODE() algorithm
// prepend the seed str and the matrix name and create the new cref_DIFF_DIFF
qual.name := PARTIAL_DERIVATIVE_STR + "_" + name;
pder_cref := ComponentRef.append(cref, ComponentRef.fromNode(qual, ComponentRef.scalarType(cref)));
var := fromCref(pder_cref, Variable.attributes(getVar(cref)));
// update the variable kind and pass the pointer to the original variable
var.backendinfo := BackendExtension.BackendInfo.setVarKind(var.backendinfo, varKind);
// create the new variable pointer and safe it to the component reference
(var_ptr, pder_cref) := makeVarPtrCyclic(var, pder_cref);
then ();
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for " + ComponentRef.toString(cref)});
then fail();
end match;
end makePDerVar;
function makeFDerVar
"Creates a function derivative cref. Used in NBDifferentiation
for differentiating body vars of a function."
input output ComponentRef cref "old component reference to new component reference";
algorithm
cref := match ComponentRef.node(cref)
local
InstNode qual;
// for function differentiation (crefs are not lowered and only known locally)
case qual as InstNode.COMPONENT_NODE() algorithm
// prepend the seed str, matrix name locally not needed
qual.name := FUNCTION_DERIVATIVE_STR + "_" + qual.name;
cref := ComponentRef.fromNode(qual, ComponentRef.nodeType(cref));
then cref;
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for " + ComponentRef.toString(cref)});
then fail();
end match;
end makeFDerVar;
function makeStartVar
"Creates a start variable pointer from a cref. Used in NBInitialization.
e.g: angle -> $START.angle"
input ComponentRef cref "old component reference";
output ComponentRef start_cref "new component reference";
output Pointer<Variable> var_ptr "pointer to new variable";
algorithm
() := match ComponentRef.node(cref)
local
InstNode qual;
Pointer<Variable> old_var_ptr;
Variable var;
case qual as InstNode.VAR_NODE()
algorithm
// get the variable pointer from the old cref to later on link back to it
old_var_ptr := BVariable.getVarPointer(cref);
// prepend the seed str and the matrix name and create the new cref
qual.name := START_STR;
start_cref := ComponentRef.append(cref, ComponentRef.fromNode(qual, ComponentRef.scalarType(cref)));
var := fromCref(start_cref, Variable.attributes(getVar(cref)));
// update the variable to be a seed and pass the pointer to the original variable
var.backendinfo := BackendExtension.BackendInfo.setVarKind(var.backendinfo, BackendExtension.START(old_var_ptr));
// create the new variable pointer and safe it to the component reference
var_ptr := Pointer.create(var);
start_cref := BackendDAE.lowerComponentReferenceInstNode(start_cref, var_ptr);
then ();
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for " + ComponentRef.toString(cref)});
then fail();
end match;
end makeStartVar;
function makeResidualVar
"Creates a residual variable pointer from a unique index and context name.
e.g. (\"DAE\", 4) --> $RES_DAE_4"
input String name "context name e.g. DAE";
input Integer uniqueIndex "unique identifier index";
input Type ty "equation type containing dims";
output Pointer<Variable> var_ptr "pointer to new variable";
output ComponentRef cref "new component reference";
protected
InstNode node;
Variable var;
list<Dimension> dims = Type.arrayDims(ty);
algorithm
// create inst node with dummy variable pointer and create cref from it
node := InstNode.VAR_NODE(RESIDUAL_STR + "_" + name + "_" + intString(uniqueIndex), Pointer.create(DUMMY_VARIABLE));
// Type for residuals is always REAL() !
cref := ComponentRef.CREF(node, {}, ty, NFComponentRef.Origin.CREF, ComponentRef.EMPTY());
// create variable and set its kind to dae_residual (change name?)
var := fromCref(cref);
// update the variable to be a seed and pass the pointer to the original variable
var.backendinfo := BackendExtension.BackendInfo.setVarKind(var.backendinfo, BackendExtension.DAE_RESIDUAL_VAR(uniqueIndex));
// create the new variable pointer and safe it to the component reference
(var_ptr, cref) := makeVarPtrCyclic(var, cref);
end makeResidualVar;
function makeEventVar
"Creates a generic boolean variable pointer from a unique index and context name.
e.g. (\"$SEV\", 4) --> $SEV_4"
input String name "context name e.g. §WHEN";
input Integer uniqueIndex "unique identifier index";
input Iterator iterator = Iterator.EMPTY() "optional for-loop iterator";
output Pointer<Variable> var_ptr "pointer to new variable";
output ComponentRef cref "new component reference";
protected
InstNode node;
ComponentRef var_cref;
Variable var;
list<ComponentRef> iter_crefs;
list<Subscript> iter_subs;
list<Integer> sub_sizes;
Type ty;
algorithm
// get subscripts from optional iterator
(iter_crefs, _) := Iterator.getFrames(iterator);
iter_subs := list(Subscript.fromTypedExp(Expression.fromCref(iter)) for iter in iter_crefs);
if listEmpty(iter_subs) then
ty := Type.BOOLEAN();
else
sub_sizes := Iterator.sizes(iterator);
ty := Type.ARRAY(Type.BOOLEAN(), list(Dimension.fromInteger(sub_size) for sub_size in sub_sizes));
end if;
// create inst node with dummy variable pointer and create cref from it
node := InstNode.VAR_NODE(name + "_" + intString(uniqueIndex), Pointer.create(DUMMY_VARIABLE));
cref := ComponentRef.CREF(node, iter_subs, ty, NFComponentRef.Origin.CREF, ComponentRef.EMPTY());
var_cref := ComponentRef.CREF(node, {}, ty, NFComponentRef.Origin.CREF, ComponentRef.EMPTY());
// create variable
var := fromCref(var_cref, NFAttributes.IMPL_DISCRETE_ATTR);
// update the variable to be discrete and pass the pointer to the original variable
var.backendinfo := BackendExtension.BackendInfo.setVarKind(var.backendinfo, BackendExtension.DISCRETE());
// create the new variable pointer and safe it to the component reference
(var_ptr, cref) := makeVarPtrCyclic(var, cref);
end makeEventVar;
function makeAuxVar
"Creates an auxillary variable pointer from a unique index and context name.
e.g. (\"FUN\", 4) --> $FUN_4"
input String name "context name e.g. FUN";
input Integer uniqueIndex "unique identifier index";
input Type ty "variable type containing dims";
input Boolean makeParam "true if it is a parameter";
output Pointer<Variable> var_ptr "pointer to new variable";
output ComponentRef cref "new component reference";
protected
InstNode node;
Variable var;
list<Dimension> dims = Type.arrayDims(ty);
algorithm
// create inst node with dummy variable pointer and create cref from it
node := InstNode.VAR_NODE(name + "_" + intString(uniqueIndex), Pointer.create(DUMMY_VARIABLE));
cref := ComponentRef.CREF(node, {}, ty, NFComponentRef.Origin.CREF, ComponentRef.EMPTY());
var := fromCref(cref);
// update the variable kind and set hideResult = true