-
Notifications
You must be signed in to change notification settings - Fork 298
/
NFUnitCheck.mo
1153 lines (1027 loc) · 42 KB
/
NFUnitCheck.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
encapsulated package NFUnitCheck
" file: NFUnitCheck.mo
package: UnitCheck
description: This package provides everything for advanced unit checking:
- for all variables unspecified units get calculated if possible
- inconsistent equations get reported in a user friendly way
authors: Jan Hagemann and Lennart Ochel (FH Bielefeld, Germany)"
public
import Absyn;
import AbsynUtil;
//import DAE;
import FlatModel = NFFlatModel;
import FunctionTree = NFFlatten.FunctionTree;
protected
import BaseHashTable;
import ComponentRef = NFComponentRef;
import ElementSource;
import Equation = NFEquation;
import ExecStat.execStat;
import ExpressionDump;
import Expression = NFExpression;
import HashTableCrToUnit = NFHashTableCrToUnit;
import HashTableStringToUnit = NFHashTableStringToUnit;
import HashTableUnitToString = NFHashTableUnitToString;
import Binding = NFBinding;
import Call = NFCall;
import Component = NFComponent;
import NFFunction.Function;
import NFInstNode.InstNode;
import Operator = NFOperator;
import Type = NFType;
import Unit = NFUnit;
import Variable = NFVariable;
import Variability = NFPrefixes.Variability;
uniontype Functionargs
record FUNCTIONUNITS
String name;
list<String> invars;
list<String> outvars;
list<String> inunits;
list<String> outunits;
end FUNCTIONUNITS;
end Functionargs;
package FunctionUnitCache
type Key = String;
type Value = Functionargs;
type Cache = tuple<
array<list<tuple<Key, Integer>>>,
tuple<Integer, Integer, array<Option<tuple<Key, Value>>>>,
Integer,
tuple<FuncHash, FuncEq, FuncKeyStr, FuncValueStr>>;
partial function FuncHash
input Key key;
input Integer mod;
output Integer res;
end FuncHash;
partial function FuncEq
input Key key1;
input Key key2;
output Boolean res;
end FuncEq;
partial function FuncKeyStr
input Key key;
output String res;
end FuncKeyStr;
partial function FuncValueStr
input Value value;
output String res;
end FuncValueStr;
function dummyPrint
input Functionargs args;
output String res = "";
end dummyPrint;
function emptyCache
input Integer size;
output Cache table;
algorithm
table := BaseHashTable.emptyHashTableWork(size, (stringHashDjb2Mod, stringEq, Util.id, dummyPrint));
end emptyCache;
end FunctionUnitCache;
public
function checkUnits
input output FlatModel flatModel;
protected
HashTableCrToUnit.HashTable htCr2U1, htCr2U2;
HashTableStringToUnit.HashTable htS2U;
HashTableUnitToString.HashTable htU2S;
FunctionUnitCache.Cache fn_cache;
algorithm
if not (Flags.getConfigBool(Flags.UNIT_CHECKING) or Flags.getConfigBool(Flags.CHECK_MODEL)) then
return;
end if;
try
htCr2U1 := HashTableCrToUnit.emptyHashTableSized(Util.nextPrime(integer(10+1.4*listLength(flatModel.variables))));
htS2U := Unit.getKnownUnits();
htU2S := Unit.getKnownUnitsInverse();
fn_cache := FunctionUnitCache.emptyCache(BaseHashTable.defaultBucketSize);
for v in flatModel.variables loop
(htCr2U1, htS2U, htU2S) := convertUnitString2unit(v, htCr2U1, htS2U, htU2S);
end for;
htCr2U2 := BaseHashTable.copy(htCr2U1);
(htCr2U2, htS2U, htU2S) := checkModelConsistency(flatModel.variables, flatModel.equations,
flatModel.initialEquations, htCr2U2, htS2U, htU2S, fn_cache);
if Flags.isSet(Flags.DUMP_UNIT) then
BaseHashTable.dumpHashTable(htCr2U2);
print("######## UnitCheck COMPLETED ########\n");
end if;
notification(htCr2U1, htCr2U2, htU2S);
flatModel := updateModel(flatModel, htCr2U2, htU2S);
else
Error.addInternalError(getInstanceName() + ": unit check module failed", sourceInfo());
end try;
execStat(getInstanceName());
end checkUnits;
protected
function updateModel
"Updates all variables without units with their calculated units."
input output FlatModel flatModel;
input HashTableCrToUnit.HashTable htCr2U;
input HashTableUnitToString.HashTable htU2S;
algorithm
flatModel.variables := list(updateVariable(v, htCr2U, htU2S) for v in flatModel.variables);
end updateModel;
function updateVariable
"Updates a variable without unit with its calculated unit."
input output Variable var;
input HashTableCrToUnit.HashTable htCr2U;
input HashTableUnitToString.HashTable htU2S;
protected
String name, unit_str;
Binding binding;
Integer unit_idx = 0;
Unit.Unit unit;
algorithm
if Type.isReal(var.ty) then
for attr in var.typeAttributes loop
(name, binding) := attr;
unit_idx := unit_idx + 1;
if name == "unit" then
if Binding.isBound(binding) then
// Variable already has a unit, keep it.
return;
else
// Variable has an empty unit, replace it.
var.typeAttributes := listDelete(var.typeAttributes, unit_idx);
break;
end if;
end if;
end for;
try
// Look up the variable's unit in the table.
unit := BaseHashTable.get(var.name, htCr2U);
if Unit.isUnit(unit) then
// Add the unit string to the variable's type attributes.
unit_str := Unit.unitString(unit, htU2S);
binding := Binding.FLAT_BINDING(Expression.STRING(unit_str), Variability.CONSTANT);
var.typeAttributes := ("unit", binding) :: var.typeAttributes;
end if;
else
end try;
end if;
end updateVariable;
function notification "dumps the calculated units"
input HashTableCrToUnit.HashTable inHtCr2U1;
input HashTableCrToUnit.HashTable inHtCr2U2;
input HashTableUnitToString.HashTable inHtU2S;
protected
String str;
list<tuple<ComponentRef, Unit.Unit>> lt1;
algorithm
lt1 := BaseHashTable.hashTableList(inHtCr2U1);
str := notification2(lt1, inHtCr2U2, inHtU2S);
if Flags.isSet(Flags.DUMP_UNIT) and str<>"" then
Error.addCompilerNotification(str);
end if;
end notification;
protected function notification2 "help-function"
input list<tuple<ComponentRef, Unit.Unit>> inLt1;
input HashTableCrToUnit.HashTable inHtCr2U2;
input HashTableUnitToString.HashTable inHtU2S;
output String outS;
protected
ComponentRef cr1 = ComponentRef.EMPTY();
Real factor1=0;
Integer i1=0, i2=0, i3=0, i4=0, i5=0, i6=0, i7=0;
algorithm
outS := stringAppendList(list(
// We already assigned the variables before
"\"" + ComponentRef.toString(cr1) + "\" has the Unit \"" + Unit.unitString(Unit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7), inHtU2S) + "\"\n"
// Do the filtering and unboxing stuff at the same time; then we only need one hashtable call
// And we only use a try-block for MASTER nodes
for t1 guard match t1 local Boolean b; case (cr1,Unit.MASTER()) algorithm
b := false;
try
Unit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7) :=
BaseHashTable.get(ComponentRef.stripSubscripts(cr1), inHtCr2U2);
b := true;
else
end try;
then b; else false; end match in inLt1
));
end notification2;
function checkModelConsistency
input list<Variable> variables;
input list<Equation> equations;
input list<Equation> initialEquations;
input output HashTableCrToUnit.HashTable htCr2U;
input output HashTableStringToUnit.HashTable htS2U;
input output HashTableUnitToString.HashTable htU2S;
input output FunctionUnitCache.Cache fnCache;
protected
Boolean dump_eq_unit = Flags.isSet(Flags.DUMP_EQ_UNIT_STRUCT);
algorithm
for v in variables loop
(htCr2U, htS2U, htU2S, fnCache) := foldBindingExp(v, htCr2U, htS2U, htU2S, fnCache, dump_eq_unit);
end for;
for eq in equations loop
(htCr2U, htS2U, htU2S, fnCache) := foldEquation(eq, htCr2U, htS2U, htU2S, fnCache, dump_eq_unit);
end for;
for ieq in initialEquations loop
(htCr2U, htS2U, htU2S, fnCache) := foldEquation(ieq, htCr2U, htS2U, htU2S, fnCache, dump_eq_unit);
end for;
end checkModelConsistency;
function foldBindingExp
input Variable var;
input output HashTableCrToUnit.HashTable htCr2U;
input output HashTableStringToUnit.HashTable htS2U;
input output HashTableUnitToString.HashTable htU2S;
input output FunctionUnitCache.Cache fnCache;
input Boolean dumpEqInitStruct;
protected
Expression binding_exp;
Equation eq;
algorithm
if Type.isReal(var.ty) and Binding.isBound(var.binding) then
binding_exp := Binding.getTypedExp(var.binding);
eq := Equation.makeEquality(Expression.fromCref(var.name), binding_exp, var.ty,
ElementSource.createElementSource(var.info));
(htCr2U, htS2U, htU2S, fnCache) := foldEquation(eq, htCr2U, htS2U, htU2S, fnCache, dumpEqInitStruct);
end if;
end foldBindingExp;
function foldEquation
"Folds the equation or returns the error message of inconsistent equations."
input Equation eq;
input output HashTableCrToUnit.HashTable htCr2U;
input output HashTableStringToUnit.HashTable htS2U;
input output HashTableUnitToString.HashTable htU2S;
input output FunctionUnitCache.Cache fnCache;
input Boolean dumpEqInitStruct;
protected
list<list<tuple<Expression, Unit.Unit>>> inconsistent_units;
algorithm
(htCr2U, htS2U, htU2S, fnCache, inconsistent_units) :=
foldEquation2(eq, dumpEqInitStruct, htCr2U, htS2U, htU2S, fnCache);
for u in inconsistent_units loop
Errorfunction(u, eq, htU2S);
end for;
end foldEquation;
function foldEquation2 "help function to foldEquation"
input Equation eq;
input Boolean dumpEqInitStruct;
input output HashTableCrToUnit.HashTable htCr2U;
input output HashTableStringToUnit.HashTable htS2U;
input output HashTableUnitToString.HashTable htU2S;
input output FunctionUnitCache.Cache fnCache;
output list<list<tuple<Expression, Unit.Unit>>> inconsistentUnits;
algorithm
inconsistentUnits := match eq
local
list<list<tuple<Expression, Unit.Unit>>> icu1, icu2;
Expression lhs, rhs, temp;
String fn_name, formal_args, formal_var;
list<String> out_vars, out_units;
Unit.Unit unit1, unit2;
list<Equation> eql;
Boolean b;
case Equation.EQUALITY(lhs = lhs as Expression.TUPLE(),
rhs = rhs as Expression.CALL())
guard not Function.isBuiltin(Call.typedFunction(rhs.call))
algorithm
fn_name := AbsynUtil.pathString(AbsynUtil.makeNotFullyQualified(Call.functionName(rhs.call)));
(_, out_vars, _, out_units) := getCallUnits(fn_name, rhs.call, fnCache);
(htCr2U, htS2U, htU2S, fnCache, icu1) :=
foldCallArg1(lhs.elements, htCr2U, htS2U, htU2S, fnCache, Unit.MASTER({}), out_units, out_vars, fn_name);
(_, htCr2U, htS2U, htU2S, fnCache, icu2) :=
insertUnitInEquation(rhs, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
then
List.append_reverse(icu1, icu2);
case Equation.EQUALITY(rhs = rhs as Expression.CALL())
guard not Function.isBuiltin(Call.typedFunction(rhs.call))
algorithm
fn_name := AbsynUtil.pathString(AbsynUtil.makeNotFullyQualified(Call.functionName(rhs.call)));
(_, out_vars, _, out_units, fnCache) := getCallUnits(fn_name, rhs.call, fnCache);
(unit1, htCr2U, htS2U, htU2S, fnCache, _) :=
insertUnitInEquation(eq.lhs, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
formal_args := listHead(out_units);
formal_var := listHead(out_vars);
unit2 := if formal_args == "NONE" then Unit.MASTER({}) else Unit.parseUnitString(formal_args, htS2U);
b := unitTypesEqual(unit1, unit2, htCr2U);
if b then
icu1 := {};
else
icu1 := {{(eq.lhs, unit1), (makeNewCref(formal_var, fn_name), unit2)}};
end if;
(_, htCr2U, htS2U, htU2S, fnCache, icu2) :=
insertUnitInEquation(rhs, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
then
List.append_reverse(icu1, icu2);
case Equation.EQUALITY()
algorithm
temp := Expression.BINARY(eq.rhs, Operator.makeSub(Type.REAL()), eq.lhs);
if dumpEqInitStruct then
ExpressionDump.dumpExp(Expression.toDAE(temp));
end if;
(_, htCr2U, htS2U, htU2S, fnCache, inconsistentUnits) :=
insertUnitInEquation(temp, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
then
inconsistentUnits;
case Equation.ARRAY_EQUALITY()
algorithm
temp := Expression.BINARY(eq.rhs, Operator.makeSub(Type.REAL()), eq.lhs);
if dumpEqInitStruct then
ExpressionDump.dumpExp(Expression.toDAE(temp));
end if;
(_, htCr2U, htS2U, htU2S, fnCache, inconsistentUnits) :=
insertUnitInEquation(temp, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
then
inconsistentUnits;
case Equation.WHEN(branches = Equation.Branch.BRANCH(body = eql) :: _)
algorithm
inconsistentUnits := {};
for e in eql loop
(htCr2U, htS2U, htU2S, fnCache, icu1) :=
foldEquation2(e, dumpEqInitStruct, htCr2U, htS2U, htU2S, fnCache);
inconsistentUnits := List.append_reverse(icu1, inconsistentUnits);
end for;
then
inconsistentUnits;
case Equation.NORETCALL()
algorithm
(_, htCr2U, htS2U, htU2S, fnCache, inconsistentUnits) :=
insertUnitInEquation(eq.exp, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
then
inconsistentUnits;
else {};
end match;
end foldEquation2;
function makeNewCref
input String paramName;
input String fnName;
output Expression outExp;
algorithm
outExp := Expression.CREF(Type.UNKNOWN(),
ComponentRef.STRING(paramName, ComponentRef.STRING(fnName + "()", ComponentRef.EMPTY())));
end makeNewCref;
function insertUnitInEquation
"Inserts the units in the equation and checks if the equation is consistent or not."
input Expression eq;
input output Unit.Unit unit;
input output HashTableCrToUnit.HashTable htCr2U;
input output HashTableStringToUnit.HashTable htS2U;
input output HashTableUnitToString.HashTable htU2S;
input output FunctionUnitCache.Cache fnCache;
output list<list<tuple<Expression, Unit.Unit>>> inconsistentUnits;
protected
import NFOperator.Op;
algorithm
(unit, inconsistentUnits) := matchcontinue eq
local
Expression exp1, exp2;
Unit.Unit unit1, unit2, op_unit;
list<list<tuple<Expression, Unit.Unit>>> icu1, icu2;
list<ComponentRef> vars;
Integer i;
Boolean b;
// SUB equal summands
case Expression.BINARY(exp1, Operator.OPERATOR(op = Op.SUB), exp2)
algorithm
(unit2 as Unit.UNIT(), htCr2U, htS2U, htU2S, fnCache, icu2) :=
insertUnitInEquation(exp2, unit, htCr2U, htS2U, htU2S, fnCache);
(unit1, htCr2U, htS2U, htU2S, fnCache, icu1) :=
insertUnitInEquation(exp1, unit2, htCr2U, htS2U, htU2S, fnCache);
(true, op_unit, htCr2U) := unitTypesEqual(unit1, unit2, htCr2U);
then
(op_unit, List.append_reverse(icu1, icu2));
// SUB equal summands
case Expression.BINARY(exp1, Operator.OPERATOR(op = Op.SUB), exp2)
algorithm
(unit1, htCr2U, htS2U, htU2S, fnCache, icu2) :=
insertUnitInEquation(exp1, unit, htCr2U, htS2U, htU2S, fnCache);
(unit2, htCr2U, htS2U, htU2S, fnCache, icu1) :=
insertUnitInEquation(exp2, unit1, htCr2U, htS2U, htU2S, fnCache);
(true, op_unit, htCr2U) := unitTypesEqual(unit1, unit2, htCr2U);
then
(op_unit, List.append_reverse(icu1, icu2));
// SUB unequal summands
case Expression.BINARY(exp1, Operator.OPERATOR(op = Op.SUB), exp2)
algorithm
(unit2 as Unit.UNIT(), htCr2U, htS2U, htU2S, fnCache, icu2) :=
insertUnitInEquation(exp2, unit, htCr2U, htS2U, htU2S, fnCache);
(unit1, htCr2U, htS2U, htU2S, fnCache, icu1) :=
insertUnitInEquation(exp1, unit2, htCr2U, htS2U, htU2S, fnCache);
(false, _, _) := unitTypesEqual(unit1, unit2, htCr2U);
then
(Unit.MASTER({}), {(exp1, unit1), (exp2, unit2)} :: List.append_reverse(icu1, icu2));
// SUB unequal summands
case Expression.BINARY(exp1, Operator.OPERATOR(op = Op.SUB), exp2)
algorithm
(unit1, htCr2U, htS2U, htU2S, fnCache, icu2) :=
insertUnitInEquation(exp1, unit, htCr2U, htS2U, htU2S, fnCache);
(unit2, htCr2U, htS2U, htU2S, fnCache, icu1) :=
insertUnitInEquation(exp2, unit1, htCr2U, htS2U, htU2S, fnCache);
(false, _, _) := unitTypesEqual(unit1, unit2, htCr2U);
then
(Unit.MASTER({}), {(exp1, unit1), (exp2, unit2)} :: List.append_reverse(icu1, icu2));
// ADD equal summands
case Expression.BINARY(exp1, Operator.OPERATOR(op = Op.ADD), exp2)
algorithm
(unit2 as Unit.UNIT(), htCr2U, htS2U, htU2S, fnCache, icu2) :=
insertUnitInEquation(exp2, unit, htCr2U, htS2U, htU2S, fnCache);
(unit1, htCr2U, htS2U, htU2S, fnCache, icu1) :=
insertUnitInEquation(exp1, unit2, htCr2U, htS2U, htU2S, fnCache);
(true, op_unit, htCr2U) := unitTypesEqual(unit1, unit2, htCr2U);
then
(op_unit, List.append_reverse(icu1, icu2));
// ADD equal summands
case Expression.BINARY(exp1, Operator.OPERATOR(op = Op.ADD), exp2)
algorithm
(unit1, htCr2U, htS2U, htU2S, fnCache, icu2) :=
insertUnitInEquation(exp1, unit, htCr2U, htS2U, htU2S, fnCache);
(unit2, htCr2U, htS2U, htU2S, fnCache, icu1) :=
insertUnitInEquation(exp2, unit1, htCr2U, htS2U, htU2S, fnCache);
(true, op_unit, htCr2U) := unitTypesEqual(unit1, unit2, htCr2U);
then
(op_unit, List.append_reverse(icu1, icu2));
// ADD unequal summands
case Expression.BINARY(exp1, Operator.OPERATOR(op = Op.ADD), exp2)
algorithm
(unit2 as Unit.UNIT(), htCr2U, htS2U, htU2S, fnCache, icu2) :=
insertUnitInEquation(exp2, unit, htCr2U, htS2U, htU2S, fnCache);
(unit1, htCr2U, htS2U, htU2S, fnCache, icu1) :=
insertUnitInEquation(exp1, unit2, htCr2U, htS2U, htU2S, fnCache);
(false, _, _) := unitTypesEqual(unit1, unit2, htCr2U);
then
(Unit.MASTER({}), {(exp1, unit1), (exp2, unit2)} :: List.append_reverse(icu1, icu2));
// ADD unequal summands
case Expression.BINARY(exp1, Operator.OPERATOR(op = Op.ADD), exp2)
algorithm
(unit1, htCr2U, htS2U, htU2S, fnCache, icu2) :=
insertUnitInEquation(exp1, unit, htCr2U, htS2U, htU2S, fnCache);
(unit2, htCr2U, htS2U, htU2S, fnCache, icu1) :=
insertUnitInEquation(exp2, unit1, htCr2U, htS2U, htU2S, fnCache);
(false, _, _) := unitTypesEqual(unit1, unit2, htCr2U);
then
(Unit.MASTER({}), {(exp1, unit1), (exp2, unit2)} :: List.append_reverse(icu1, icu2));
// MUL
case Expression.BINARY(exp1, Operator.OPERATOR(op = Op.MUL), exp2)
algorithm
(unit1 as Unit.UNIT(), htCr2U, htS2U, htU2S, fnCache, icu1) :=
insertUnitInEquation(exp1, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
(unit2 as Unit.UNIT(), htCr2U, htS2U, htU2S, fnCache, icu2) :=
insertUnitInEquation(exp2, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
op_unit := Unit.unitMul(unit1, unit2);
(htS2U, htU2S) := insertUnitString(op_unit, htS2U, htU2S);
then
(op_unit, List.append_reverse(icu1, icu2));
case Expression.BINARY(exp1, Operator.OPERATOR(op = Op.MUL), exp2)
guard Unit.isMaster(unit)
algorithm
(unit1 as Unit.MASTER(), htCr2U, htS2U, htU2S, fnCache, icu1) :=
insertUnitInEquation(exp1, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
(unit2 as Unit.UNIT(), htCr2U, htS2U, htU2S, fnCache, icu2) :=
insertUnitInEquation(exp2, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
then
(Unit.MASTER({}), List.append_reverse(icu1, icu2));
case Expression.BINARY(exp1, Operator.OPERATOR(op = Op.MUL), exp2)
guard Unit.isUnit(unit)
algorithm
(Unit.MASTER(varList = vars), htCr2U, htS2U, htU2S, fnCache, icu1) :=
insertUnitInEquation(exp1, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
(unit2 as Unit.UNIT(), htCr2U, htS2U, htU2S, fnCache, icu2) :=
insertUnitInEquation(exp2, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
op_unit := Unit.unitDiv(unit, unit2);
htCr2U := List.fold1(vars, updateHtCr2U, op_unit, htCr2U);
(htS2U, htU2S) := insertUnitString(op_unit, htS2U, htU2S);
then
(unit, List.append_reverse(icu1, icu2));
case Expression.BINARY(exp1, Operator.OPERATOR(op = Op.MUL), exp2)
guard Unit.isMaster(unit)
algorithm
(Unit.UNIT(), htCr2U, htS2U, htU2S, fnCache, icu1) :=
insertUnitInEquation(exp1, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
(Unit.MASTER(), htCr2U, htS2U, htU2S, fnCache, icu2) :=
insertUnitInEquation(exp2, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
then
(Unit.MASTER({}), List.append_reverse(icu1, icu2));
case Expression.BINARY(exp1, Operator.OPERATOR(op = Op.MUL), exp2)
guard Unit.isUnit(unit)
algorithm
(unit2 as Unit.UNIT(), htCr2U, htS2U, htU2S, fnCache, icu1) :=
insertUnitInEquation(exp1, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
(Unit.MASTER(varList = vars), htCr2U, htS2U, htU2S, fnCache, icu2) :=
insertUnitInEquation(exp2, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
op_unit := Unit.unitDiv(unit, unit2);
htCr2U := List.fold1(vars, updateHtCr2U, op_unit, htCr2U);
(htS2U, htU2S) := insertUnitString(op_unit, htS2U, htU2S);
then
(unit, List.append_reverse(icu1, icu2));
case Expression.BINARY(exp1, Operator.OPERATOR(op = Op.MUL), exp2)
algorithm
(Unit.MASTER(), htCr2U, htS2U, htU2S, fnCache, icu1) :=
insertUnitInEquation(exp1, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
(Unit.MASTER(), htCr2U, htS2U, htU2S, fnCache, icu2) :=
insertUnitInEquation(exp2, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
then
(Unit.MASTER({}), List.append_reverse(icu1, icu2));
// DIV
case Expression.BINARY(exp1, Operator.OPERATOR(op = Op.DIV), exp2)
algorithm
(unit1 as Unit.UNIT(), htCr2U, htS2U, htU2S, fnCache, icu1) :=
insertUnitInEquation(exp1, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
(unit2 as Unit.UNIT(), htCr2U, htS2U, htU2S, fnCache, icu2) :=
insertUnitInEquation(exp2, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
op_unit := Unit.unitDiv(unit1, unit2);
(htS2U, htU2S) := insertUnitString(op_unit, htS2U, htU2S);
then
(op_unit, List.append_reverse(icu1, icu2));
case Expression.BINARY(exp1, Operator.OPERATOR(op = Op.DIV), exp2)
guard Unit.isMaster(unit)
algorithm
(Unit.MASTER(), htCr2U, htS2U, htU2S, fnCache, icu1) :=
insertUnitInEquation(exp1, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
(Unit.UNIT(), htCr2U, htS2U, htU2S, fnCache, icu2) :=
insertUnitInEquation(exp2, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
inconsistentUnits := List.append_reverse(icu1, icu2);
then
(Unit.MASTER({}), List.append_reverse(icu1, icu2));
case Expression.BINARY(exp1, Operator.OPERATOR(op = Op.DIV), exp2)
guard Unit.isUnit(unit)
algorithm
(Unit.MASTER(varList = vars), htCr2U, htS2U, htU2S, fnCache, icu1) :=
insertUnitInEquation(exp1, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
(unit2 as Unit.UNIT(), htCr2U, htS2U, htU2S, fnCache, icu2) :=
insertUnitInEquation(exp2, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
op_unit := Unit.unitMul(unit, unit2);
htCr2U := List.fold1(vars, updateHtCr2U, op_unit, htCr2U);
(htS2U, htU2S) := insertUnitString(op_unit, htS2U, htU2S);
then
(unit, List.append_reverse(icu1, icu2));
case Expression.BINARY(exp1, Operator.OPERATOR(op = Op.DIV), exp2)
guard Unit.isMaster(unit)
algorithm
(Unit.UNIT(), htCr2U, htS2U, htU2S, fnCache, icu1) :=
insertUnitInEquation(exp1, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
(Unit.MASTER(), htCr2U, htS2U, htU2S, fnCache, icu2) :=
insertUnitInEquation(exp2, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
then
(Unit.MASTER({}), List.append_reverse(icu1, icu2));
case Expression.BINARY(exp1, Operator.OPERATOR(op = Op.DIV), exp2)
guard Unit.isUnit(unit)
algorithm
(unit2 as Unit.UNIT(), htCr2U, htS2U, htU2S, fnCache, icu1) :=
insertUnitInEquation(exp1, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
(Unit.MASTER(varList = vars), htCr2U, htS2U, htU2S, fnCache, icu2) :=
insertUnitInEquation(exp2, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
op_unit := Unit.unitDiv(unit2, unit);
htCr2U := List.fold1(vars, updateHtCr2U, op_unit, htCr2U);
(htS2U, htU2S) := insertUnitString(op_unit, htS2U, htU2S);
then
(unit, List.append_reverse(icu1, icu2));
case Expression.BINARY(exp1, Operator.OPERATOR(op = Op.DIV), exp2)
algorithm
(Unit.MASTER(), htCr2U, htS2U, htU2S, fnCache, icu1) :=
insertUnitInEquation(exp1, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
(Unit.MASTER(), htCr2U, htS2U, htU2S, fnCache, icu2) :=
insertUnitInEquation(exp2, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
then
(Unit.MASTER({}), List.append_reverse(icu1, icu2));
// POW
case Expression.BINARY(exp1, Operator.OPERATOR(op = Op.POW), exp2 as Expression.REAL())
algorithm
(unit1 as Unit.UNIT(), htCr2U, htS2U, htU2S, fnCache, icu1) :=
insertUnitInEquation(exp1, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
i := realInt(exp2.value);
true := realEq(exp2.value, i);
op_unit := Unit.unitPow(unit, i);
(htS2U, htU2S) := insertUnitString(op_unit, htS2U, htU2S);
then
(op_unit, icu1);
case Expression.BINARY(exp1, Operator.OPERATOR(op = Op.POW), exp2 as Expression.REAL())
guard Unit.isUnit(unit)
algorithm
(Unit.MASTER(varList = vars), htCr2U, htS2U, htU2S, fnCache, icu1) :=
insertUnitInEquation(exp1, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
op_unit := Unit.unitRoot(unit, exp2.value);
htCr2U := List.fold1(vars, updateHtCr2U, op_unit, htCr2U);
(htS2U, htU2S) := insertUnitString(op_unit, htS2U, htU2S);
then
(unit, icu1);
case Expression.BINARY(exp1, Operator.OPERATOR(op = Op.POW), Expression.REAL())
algorithm
(_, htCr2U, htS2U, htU2S, fnCache, icu1) :=
insertUnitInEquation(exp1, Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
then
(Unit.MASTER({}), icu1);
// Call
case Expression.CALL()
algorithm
(op_unit, htCr2U, htS2U, htU2S, fnCache, icu1) :=
insertUnitInEquationCall(eq.call, unit, htCr2U, htS2U, htU2S, fnCache);
then
(op_unit, icu1);
case Expression.IF()
algorithm
(unit1, htCr2U, htS2U, htU2S, fnCache, icu1) :=
insertUnitInEquation(eq.trueBranch, unit, htCr2U, htS2U, htU2S, fnCache);
(unit2, htCr2U, htS2U, htU2S, fnCache, icu2) :=
insertUnitInEquation(eq.falseBranch, unit1, htCr2U, htS2U, htU2S, fnCache);
(b, op_unit, htCr2U) := unitTypesEqual(unit1, unit2, htCr2U);
inconsistentUnits := List.append_reverse(icu1, icu2);
if not b then
inconsistentUnits := {(eq.trueBranch, unit1), (eq.falseBranch, unit2)} :: inconsistentUnits;
op_unit := Unit.MASTER({});
end if;
then
(op_unit, inconsistentUnits);
case Expression.RELATION()
algorithm
(unit1, htCr2U, htS2U, htU2S, fnCache, icu1) :=
insertUnitInEquation(eq.exp1, unit, htCr2U, htS2U, htU2S, fnCache);
(unit2, htCr2U, htS2U, htU2S, fnCache, icu2) :=
insertUnitInEquation(eq.exp2, unit, htCr2U, htS2U, htU2S, fnCache);
(b, op_unit, htCr2U) := unitTypesEqual(unit1, unit2, htCr2U);
inconsistentUnits := List.append_reverse(icu1, icu2);
if not b then
inconsistentUnits := {(eq.exp1, unit1), (eq.exp2, unit2)} :: inconsistentUnits;
op_unit := Unit.MASTER({});
end if;
then
(op_unit, inconsistentUnits);
case Expression.UNARY(operator = Operator.OPERATOR(op = Op.UMINUS))
algorithm
(op_unit, htCr2U, htS2U, htU2S, fnCache, icu1) :=
insertUnitInEquation(eq.exp, unit, htCr2U, htS2U, htU2S, fnCache);
then
(op_unit, icu1);
case Expression.CREF()
guard ComponentRef.isSimple(eq.cref) and ComponentRef.firstName(eq.cref) == "time"
algorithm
op_unit := Unit.UNIT(1e0, 0, 0, 0, 1, 0, 0, 0);
htS2U := addUnit2HtS2U("time", op_unit, htS2U);
htU2S := addUnit2HtU2S("time", op_unit, htU2S);
then
(op_unit, {});
case Expression.CREF(ty = Type.REAL())
then (BaseHashTable.get(ComponentRef.stripSubscripts(eq.cref), htCr2U), {});
else (Unit.MASTER({}), {});
end matchcontinue;
end insertUnitInEquation;
function insertUnitInEquationCall
"Inserts the units in the equation and checks if the equation is consistent or not."
input Call call;
input output Unit.Unit unit;
input output HashTableCrToUnit.HashTable htCr2U;
input output HashTableStringToUnit.HashTable htS2U;
input output HashTableUnitToString.HashTable htU2S;
input output FunctionUnitCache.Cache fnCache;
output list<list<tuple<Expression, Unit.Unit>>> inconsistentUnits;
protected
Absyn.Path fn_path;
String fn_name;
list<Expression> call_args;
Unit.Unit op_unit;
list<ComponentRef> vars;
list<String> var_names, unit_names;
algorithm
fn_path := Call.functionName(call);
call_args := Call.arguments(call);
(unit, inconsistentUnits) := matchcontinue fn_path
case Absyn.IDENT("pre")
algorithm
(op_unit, htCr2U, htS2U, htU2S, fnCache, inconsistentUnits) :=
insertUnitInEquation(listHead(call_args), unit, htCr2U, htS2U, htU2S, fnCache);
then
(Unit.MASTER({}), inconsistentUnits);
case Absyn.IDENT("der")
algorithm
(op_unit, htCr2U, htS2U, htU2S, fnCache, inconsistentUnits) :=
insertUnitInEquation(listHead(call_args), Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
if Unit.isUnit(op_unit) then
op_unit := Unit.unitDiv(op_unit, Unit.UNIT(1e0, 0, 0, 0, 1, 0, 0, 0));
(htS2U, htU2S) := insertUnitString(op_unit, htS2U, htU2S);
elseif Unit.isUnit(unit) then
Unit.MASTER(varList = vars) := op_unit;
op_unit := Unit.unitMul(unit, Unit.UNIT(1e0, 0, 0, 0, 1, 0, 0, 0));
htCr2U := List.fold1(vars, updateHtCr2U, op_unit, htCr2U);
(htS2U, htU2S) := insertUnitString(op_unit, htS2U, htU2S);
else
op_unit := Unit.MASTER({});
end if;
then
(op_unit, inconsistentUnits);
case Absyn.IDENT("sqrt")
algorithm
(op_unit, htCr2U, htS2U, htU2S, fnCache, inconsistentUnits) :=
insertUnitInEquation(listHead(call_args), Unit.MASTER({}), htCr2U, htS2U, htU2S, fnCache);
if Unit.isUnit(op_unit) then
op_unit := Unit.unitRoot(op_unit, 2.0);
(htS2U, htU2S) := insertUnitString(op_unit, htS2U, htU2S);
elseif Unit.isUnit(unit) then
Unit.MASTER(varList = vars) := op_unit;
op_unit := Unit.unitPow(unit, 2);
htCr2U := List.fold1(vars, updateHtCr2U, op_unit, htCr2U);
(htS2U, htU2S) := insertUnitString(op_unit, htS2U, htU2S);
op_unit := unit;
else
op_unit := Unit.MASTER({});
end if;
then
(op_unit, inconsistentUnits);
case Absyn.IDENT()
guard Function.isBuiltin(Call.typedFunction(call))
algorithm
(htCr2U, htS2U, htU2S, fnCache, inconsistentUnits) :=
foldCallArg(call_args, htCr2U, htS2U, htU2S, fnCache);
then
(Unit.MASTER({}), inconsistentUnits);
case _
algorithm
fn_name := AbsynUtil.pathString(AbsynUtil.makeNotFullyQualified(fn_path));
(var_names, _, unit_names, _, fnCache) := getCallUnits(fn_name, call, fnCache);
(htCr2U, htS2U, htU2S, fnCache, inconsistentUnits) :=
foldCallArg1(call_args, htCr2U, htS2U, htU2S, fnCache, unit, unit_names, var_names, fn_name);
then
(Unit.MASTER({}), inconsistentUnits);
else (Unit.MASTER({}), {});
end matchcontinue;
end insertUnitInEquationCall;
function insertUnitString
input Unit.Unit unit;
input output HashTableStringToUnit.HashTable htS2U;
input output HashTableUnitToString.HashTable htU2S;
protected
String unit_str;
algorithm
unit_str := Unit.unitString(unit, htU2S);
htS2U := addUnit2HtS2U(unit_str, unit, htS2U);
htU2S := addUnit2HtU2S(unit_str, unit, htU2S);
end insertUnitString;
function getCallUnits
input String fnName;
input Call call;
input FunctionUnitCache.Cache fnCache;
output list<String> inputVars;
output list<String> outputVars;
output list<String> inputUnits;
output list<String> outputUnits;
output FunctionUnitCache.Cache outFnCache = fnCache;
protected
Functionargs args;
algorithm
try
args := BaseHashTable.get(fnName, fnCache);
else
args := parseFunctionUnits(fnName, Call.typedFunction(call));
outFnCache := BaseHashTable.addUnique((fnName, args), outFnCache);
end try;
Functionargs.FUNCTIONUNITS(_, inputVars, outputVars, inputUnits, outputUnits) := args;
end getCallUnits;
function parseFunctionUnits
input String funcName;
input Function func;
output Functionargs outArgs;
protected
String fn_name;
list<String> in_units, out_units, in_args, out_args;
algorithm
in_units := list(Component.getUnitAttribute(InstNode.component(p), "NONE") for p in func.inputs);
out_units := list(Component.getUnitAttribute(InstNode.component(p), "NONE") for p in func.outputs);
in_args := list(InstNode.name(p) for p in func.inputs);
out_args := list(InstNode.name(p) for p in func.outputs);
outArgs := FUNCTIONUNITS(funcName, in_args, out_args, in_units, out_units);
end parseFunctionUnits;
function unitTypesEqual
"Checks equality of two units."
input Unit.Unit unit1;
input Unit.Unit unit2;
input HashTableCrToUnit.HashTable htCr2U;
output Boolean isEqual;
output Unit.Unit outUnit;
output HashTableCrToUnit.HashTable outHtCr2U;
algorithm
(isEqual, outUnit, outHtCr2U) := match (unit1, unit2)
local
Real r;
list<ComponentRef> vars1, vars2;
String s1, s2;
case (Unit.UNIT(), Unit.UNIT())
algorithm
isEqual := realEq(unit1.factor, unit2.factor);
if not isEqual then
r := realMax(realAbs(unit1.factor), realAbs(unit2.factor));
isEqual := realLe(realDiv(realAbs(realSub(unit1.factor, unit2.factor)), r), 1e-3);
end if;
isEqual := isEqual and
unit1.mol == unit2.mol and
unit1.cd == unit2.cd and
unit1.m == unit2.m and
unit1.s == unit2.s and
unit1.A == unit2.A and
unit1.K == unit2.K and
unit1.g == unit2.g;
then
(isEqual, unit1, htCr2U);
case (Unit.UNIT(), Unit.MASTER(varList = vars2))
algorithm
outHtCr2U := List.fold1(vars2, updateHtCr2U, unit1, htCr2U);
then
(true, unit1, outHtCr2U);
case (Unit.MASTER(varList = vars1), Unit.UNIT())
algorithm
outHtCr2U := List.fold1(vars1, updateHtCr2U, unit2, htCr2U);
then
(true, unit2, outHtCr2U);
case (Unit.MASTER(varList = vars1), Unit.MASTER(varList = vars2))
algorithm
vars2 := List.append_reverse(vars1, vars2);
then
(true, Unit.MASTER(vars2), htCr2U);
case (Unit.UNKNOWN(unit = s1), Unit.UNKNOWN(unit = s2))
then (s1 == s2, unit1, htCr2U);
case (Unit.UNKNOWN(), _) then (true, unit1, htCr2U);
case (_, Unit.UNKNOWN()) then (true, unit2, htCr2U);
else (false, unit1, htCr2U);
end match;
end unitTypesEqual;
function updateHtCr2U
input ComponentRef cref;
input Unit.Unit unit;
input output HashTableCrToUnit.HashTable htCr2U;
algorithm
if not BaseHashTable.hasKey(NFUnit.UPDATECREF, htCr2U) then
htCr2U := BaseHashTable.add((NFUnit.UPDATECREF, Unit.MASTER({})), htCr2U);
end if;
BaseHashTable.update((cref, unit), htCr2U);
end updateHtCr2U;
protected function Errorfunction "returns the inconsistent Equation with sub-expression"
input list<tuple<Expression, Unit.Unit>> inexpList;
input Equation inEq;
input HashTableUnitToString.HashTable inHtU2S;
algorithm
_ := match(inexpList, inEq, inHtU2S)
local
String s, s1, s2, s3, s4;
list<tuple<Expression, Unit.Unit>> expList;
Expression exp1, exp2;
Integer i;
SourceInfo info;
case (expList, _, _)
equation
info=Equation.info(inEq);
s = Equation.toString(inEq);
s1 = Errorfunction2(expList, inHtU2S);
s2="The following equation is INCONSISTENT due to specified unit information: " + s +"\n";
Error.addSourceMessage(Error.COMPILER_WARNING,{s2},info);
Error.addCompilerWarning("The units of following sub-expressions need to be equal:\n" + s1);
/*
Error.addCompilerWarning("The following NEWFRONTEND UNIT CHECK equation is INCONSISTENT due to specified unit information: " + s + "\n" +
"The units of following sub-expressions need to be equal:\n" + s1 );*/
then ();
end match;
end Errorfunction;
protected function Errorfunction2 "help-function"
input list<tuple<Expression, Unit.Unit>> inexpList;
input HashTableUnitToString.HashTable inHtU2S;
output String outS;
algorithm
outS := match(inexpList, inHtU2S)
local
list<tuple<Expression, Unit.Unit>> expList;
Expression exp;
Unit.Unit ut;
String s, s1, s2;
case ((exp, ut)::{}, _) equation
s = Expression.toString(exp);
s1 = Unit.unitString(ut, inHtU2S);
s = "- sub-expression \"" + s + "\" has unit \"" + s1 + "\"";
then s;
case ((exp, ut)::expList, _) equation
s = Expression.toString(exp);