This repository was archived by the owner on May 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathNFUnitCheck.mo
More file actions
1413 lines (1230 loc) · 55.9 KB
/
NFUnitCheck.mo
File metadata and controls
1413 lines (1230 loc) · 55.9 KB
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 DAE;
import NFUnit;
import System;
protected
import BaseHashTable;
import ComponentReference;
import Error;
import Expression;
import ExpressionDump;
import Flags;
import NFHashTableCrToUnit;
import NFHashTableStringToUnit;
import NFHashTableUnitToString;
import List;
import DAEUtil;
import DAEDump;
import SCode;
import FCore;
import ExecStat.execStat;
public uniontype Functionargs
record FUNCTIONUNITS
String name;
list<String> invars;
list<String> outvars;
list<String> inunits;
list<String> outunits;
end FUNCTIONUNITS;
end Functionargs;
public function checkUnits
input DAE.DAElist inDAE;
input DAE.FunctionTree func;
output DAE.DAElist outDAE = inDAE;
protected
DAE.DAElist elts1, elts2;
list<DAE.Element> eqlist, varlist, newdaelist;
list<DAE.Function> functionlist;
list<Functionargs> args;
NFHashTableCrToUnit.HashTable HtCr2U1, HtCr2U2;
NFHashTableStringToUnit.HashTable HtS2U;
NFHashTableUnitToString.HashTable HtU2S;
algorithm
if not (Flags.isSet(Flags.NF_UNITCHECK) or Flags.isSet(Flags.OLD_FE_UNITCHECK) or (Flags.getConfigBool(Flags.CHECK_MODEL) and Flags.isSet(Flags.SCODE_INST))) then
return;
end if;
try
(elts1, elts2) := DAEUtil.splitDAEIntoVarsAndEquations(inDAE);
varlist := GetVarList(elts1);
eqlist := GetElementList(elts2);
functionlist := DAEUtil.getFunctionList(func);
HtCr2U1 := NFHashTableCrToUnit.emptyHashTableSized(Util.nextPrime(integer(10+1.4*listLength(varlist))));
HtS2U := NFUnit.getKnownUnits();
HtU2S := NFUnit.getKnownUnitsInverse();
args := {FUNCTIONUNITS("", {}, {}, {}, {})};
args := List.mapFlat(functionlist, parseFunctionList);
// new instantiation
//((HtCr2U1, HtS2U, HtU2S)) := List.fold(varlist, convertUnitString2unit, (HtCr2U1, HtS2U, HtU2S));
// old instantiation
((HtCr2U1, HtS2U, HtU2S)) := List.fold(varlist, convertUnitString2unit_old, (HtCr2U1, HtS2U, HtU2S));
HtCr2U2 := BaseHashTable.copy(HtCr2U1);
((HtCr2U2, HtS2U, HtU2S)) := algo(varlist, eqlist, args, HtCr2U2, HtS2U, HtU2S);
varlist := List.map2(varlist, returnVar, HtCr2U2, HtU2S);
newdaelist := listAppend(varlist, eqlist);
if Flags.isSet(Flags.DUMP_UNIT) then
BaseHashTable.dumpHashTable(HtCr2U2);
print("######## UnitCheck COMPLETED ########\n");
end if;
notification(HtCr2U1, HtCr2U2, HtU2S);
outDAE := updateDAElist(inDAE, newdaelist);
else
Error.addInternalError(getInstanceName() + ": unit check module failed", sourceInfo());
end try;
execStat(getInstanceName());
end checkUnits;
protected function parseFunctionList
input DAE.Function infunction;
output list<Functionargs> outTpl;
protected
list<DAE.Element> inelt, outelt;
list<String> inunits, outunits, inargs, outargs;
String unitString, s;
algorithm
s := getFunctionName(infunction);
inelt := DAEUtil.getFunctionInputVars(infunction);
outelt := DAEUtil.getFunctionOutputVars(infunction);
inunits := List.filterMap(inelt,getUnits);
outunits := List.filterMap(outelt,getUnits);
inargs := List.filterMap(inelt,getVars);
outargs := List.filterMap(outelt,getVars);
outTpl := {FUNCTIONUNITS(s,inargs,outargs,inunits,outunits)};
end parseFunctionList;
public function getFunctionName
input DAE.Function inFunction;
output String outString;
algorithm
outString := match inFunction
local
Absyn.Path path;
String s, s1;
case DAE.FUNCTION(path=path) algorithm
s := Absyn.pathStringDefault(path);
s1 := System.trim(s, ".");
then s1;
end match;
end getFunctionName;
function getVars
input DAE.Element inElement;
output String outString;
algorithm
outString := match inElement
local
DAE.ComponentRef cr;
case DAE.VAR(componentRef=cr)
then ComponentReference.crefStr(cr);
else "";
end match;
end getVars;
function getUnits
input DAE.Element inElement;
output String outString;
algorithm
outString := match inElement
local
String unitString;
case(DAE.VAR(ty=DAE.T_REAL(), variableAttributesOption=SOME(DAE.VAR_ATTR_REAL(unit=SOME(DAE.SCONST(unitString))))))
guard(unitString <> "")
then unitString;
else "NONE";
end match;
end getUnits;
protected function updateDAElist
input DAE.DAElist indaelist;
input list<DAE.Element> indaevarlist;
output DAE.DAElist outdaelist;
algorithm
outdaelist:= match(indaelist,indaevarlist)
local
DAE.Element v,e;
list<DAE.Element> varlist,varlist2,elts1,elts2;
DAE.DAElist outdae;
String ident;
DAE.ElementSource eltsrc;
Option<SCode.Comment> comment;
case(DAE.DAE(elementLst={DAE.COMP(ident=ident,source=eltsrc,comment=comment)}),varlist2)
equation
outdae=DAE.DAE({DAE.COMP(ident,varlist2,eltsrc,comment)});
then
(outdae);
end match;
end updateDAElist;
protected function returnVar "returns the new calculated units in DAE"
input DAE.Element inVar;
input NFHashTableCrToUnit.HashTable inHtCr2U;
input NFHashTableUnitToString.HashTable inHtU2S;
output DAE.Element outVar;
algorithm
outVar := match(inVar)
local
DAE.Element var;
DAE.ComponentRef cr;
NFUnit.Unit ut;
Option<DAE.VariableAttributes> attr;
String s;
case (DAE.VAR(variableAttributesOption=SOME(DAE.VAR_ATTR_REAL(unit=SOME(_))))) then inVar;
case (DAE.VAR(componentRef=cr,variableAttributesOption=attr)) equation
if BaseHashTable.hasKey(cr, inHtCr2U) then
ut = BaseHashTable.get(cr, inHtCr2U);
if NFUnit.isUnit(ut) then
s = NFUnit.unitString(ut, inHtU2S);
attr = DAEUtil.setUnitAttr(attr, DAE.SCONST(s));
inVar.variableAttributesOption = attr;
var = inVar;
else
var = inVar;
end if;
else
var = inVar;
end if;
then var;
end match;
end returnVar;
protected function notification "dumps the calculated units"
input NFHashTableCrToUnit.HashTable inHtCr2U1;
input NFHashTableCrToUnit.HashTable inHtCr2U2;
input NFHashTableUnitToString.HashTable inHtU2S;
protected
String str;
list<tuple<DAE.ComponentRef, NFUnit.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<DAE.ComponentRef, NFUnit.Unit>> inLt1;
input NFHashTableCrToUnit.HashTable inHtCr2U2;
input NFHashTableUnitToString.HashTable inHtU2S;
output String outS;
protected
DAE.ComponentRef cr1=DAE.emptyCref;
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
"\"" + ComponentReference.crefStr(cr1) + "\" has the Unit \"" + NFUnit.unitString(NFUnit.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,NFUnit.MASTER()) algorithm
b := false;
try
NFUnit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7) := BaseHashTable.get(cr1, inHtCr2U2);
b := true;
else
end try;
then b; else false; end match in inLt1
));
end notification2;
public function algo "algorithm to check the consistency"
input list<DAE.Element> invarlist;
input list<DAE.Element> ineqList;
input list<Functionargs> inargs;
input NFHashTableCrToUnit.HashTable inHtCr2U;
input NFHashTableStringToUnit.HashTable inHtS2U;
input NFHashTableUnitToString.HashTable inHtU2S;
output tuple<NFHashTableCrToUnit.HashTable /* outHtCr2U */, NFHashTableStringToUnit.HashTable /* outHtS2U */, NFHashTableUnitToString.HashTable /* outHtU2S */> outTpl;
protected
NFHashTableCrToUnit.HashTable HtCr2U;
NFHashTableStringToUnit.HashTable HtS2U;
NFHashTableUnitToString.HashTable HtU2S;
Boolean b1, b2, b3;
algorithm
((HtCr2U, b1, HtS2U, HtU2S)) := List.fold(invarlist, foldBindingExp, (inHtCr2U, true, inHtS2U, inHtU2S));
((HtCr2U, b2, HtS2U, HtU2S)) := List.fold1(ineqList, foldEquation ,inargs,(HtCr2U, true, HtS2U, HtU2S));
b3 := BaseHashTable.hasKey(NFUnit.UPDATECREF, HtCr2U);
//outTpl := algo2(b1, b2, b3, invarlist, ineqList, HtCr2U, HtS2U, HtU2S);
outTpl :=(HtCr2U, HtS2U, HtU2S);
end algo;
protected function foldBindingExp "folds the Binding expressions"
input DAE.Element inVar;
input tuple<NFHashTableCrToUnit.HashTable /* inHtCr2U */, Boolean /* success */, NFHashTableStringToUnit.HashTable /* inHtS2U */, NFHashTableUnitToString.HashTable /* inHtU2S */> inTpl;
output tuple<NFHashTableCrToUnit.HashTable /* outHtCr2U */, Boolean /* success */, NFHashTableStringToUnit.HashTable /* outHtS2U */, NFHashTableUnitToString.HashTable /* outHtU2S */> outTpl;
algorithm
outTpl := match(inVar, inTpl)
local
DAE.Exp exp, crefExp;
DAE.ComponentRef cref;
NFHashTableCrToUnit.HashTable HtCr2U;
NFHashTableStringToUnit.HashTable HtS2U;
NFHashTableUnitToString.HashTable HtU2S;
Boolean b;
DAE.Element eq;
DAE.ElementSource source;
case (DAE.VAR(componentRef=cref, ty=DAE.T_REAL(), binding=SOME(exp),source=source), (HtCr2U, b, HtS2U, HtU2S))
equation
crefExp = Expression.crefExp(cref);
eq = DAE.EQUATION(crefExp, exp, source);
((HtCr2U, b, HtS2U, HtU2S))=foldEquation(eq,{},(HtCr2U, b, HtS2U, HtU2S));
then ((HtCr2U, b, HtS2U, HtU2S));
case (DAE.VAR(ty=DAE.T_REAL(), binding=SOME(_)), (HtCr2U, _, HtS2U, HtU2S))
then ((HtCr2U, false, HtS2U, HtU2S));
else inTpl;
end match;
end foldBindingExp;
protected function foldEquation "folds the equations or return the error message of incosistent equations"
input DAE.Element inEq;
input list<Functionargs> inargs;
input tuple<NFHashTableCrToUnit.HashTable /* inHtCr2U */, Boolean /* success */, NFHashTableStringToUnit.HashTable /* inHtS2U */, NFHashTableUnitToString.HashTable /* inHtU2S */> inTpl;
output tuple<NFHashTableCrToUnit.HashTable /* outHtCr2U */, Boolean /* success */, NFHashTableStringToUnit.HashTable /* outHtS2U */, NFHashTableUnitToString.HashTable /* outHtU2S */> outTpl;
protected
NFHashTableCrToUnit.HashTable HtCr2U;
NFHashTableStringToUnit.HashTable HtS2U;
NFHashTableUnitToString.HashTable HtU2S;
list<list<tuple<DAE.Exp, NFUnit.Unit>>> expListList;
Boolean b;
algorithm
(HtCr2U, b, HtS2U, HtU2S):=inTpl;
(HtCr2U, HtS2U, HtU2S, expListList):=foldEquation2(inEq, HtCr2U, HtS2U, HtU2S,inargs);
List.map2_0(expListList, Errorfunction, inEq, HtU2S);
outTpl := (HtCr2U, b, HtS2U, HtU2S);
end foldEquation;
protected function foldEquation2 "help-function"
input DAE.Element eq;
input output NFHashTableCrToUnit.HashTable htCr2U;
input output NFHashTableStringToUnit.HashTable htS2U;
input output NFHashTableUnitToString.HashTable htU2S;
input list<Functionargs> args;
output list<list<tuple<DAE.Exp, NFUnit.Unit>>> inconsistentUnits;
algorithm
inconsistentUnits := match eq
local
DAE.Exp temp, lhs;
list<list<tuple<DAE.Exp, NFUnit.Unit>>> expList, expList2, expList3;
Absyn.Path path;
Boolean b;
NFUnit.Unit ut1, ut2;
String s1, formalargs, formalvar;
list<String> outvars, outunitlist;
list<DAE.Exp> expl;
// solved Equation
case DAE.DEFINE()
algorithm
lhs := DAE.CREF(eq.componentRef, DAE.T_REAL_DEFAULT);
temp := DAE.BINARY(eq.exp, DAE.SUB(DAE.T_REAL_DEFAULT), lhs);
if Flags.isSet(Flags.DUMP_EQ_UNIT_STRUCT) then
ExpressionDump.dumpExp(temp);
end if;
(_, (htCr2U, htS2U, htU2S), inconsistentUnits) :=
insertUnitInEquation(temp, (htCr2U, htS2U, htU2S), NFUnit.MASTER({}), args);
then
inconsistentUnits;
case DAE.INITIALDEFINE()
algorithm
lhs := DAE.CREF(eq.componentRef, DAE.T_REAL_DEFAULT);
temp := DAE.BINARY(eq.exp, DAE.SUB(DAE.T_REAL_DEFAULT), lhs);
if Flags.isSet(Flags.DUMP_EQ_UNIT_STRUCT) then
ExpressionDump.dumpExp(temp);
end if;
(_, (htCr2U, htS2U, htU2S), inconsistentUnits) :=
insertUnitInEquation(temp, (htCr2U, htS2U, htU2S), NFUnit.MASTER({}), args);
then
inconsistentUnits;
case DAE.EQUATION(exp = DAE.Exp.TUPLE(PR = expl),
scalar = DAE.CALL(path = Absyn.FULLYQUALIFIED(path)))
algorithm
s1 := Absyn.pathString(path);
s1 := System.trim(s1,".");
(_, outvars, _, outunitlist) := getNamedUnitlist(s1, args);
(htCr2U, htS2U, htU2S, expList2) :=
foldCallArg1(expl, htCr2U, htS2U, htU2S, NFUnit.MASTER({}), outunitlist, outvars, s1);
(_, (htCr2U, htS2U, htU2S), expList3) :=
insertUnitInEquation(eq.scalar, (htCr2U, htS2U, htU2S), NFUnit.MASTER({}), args);
then
List.append_reverse(expList2, expList3);
case DAE.EQUATION(exp = lhs,
scalar = DAE.CALL(path = Absyn.FULLYQUALIFIED(path)))
algorithm
s1 := Absyn.pathString(path);
s1 := System.trim(s1,".");
(_, outvars, _, outunitlist) := getNamedUnitlist(s1,args);
(ut1, (htCr2U, htS2U, htU2S), _) :=
insertUnitInEquation(lhs, (htCr2U, htS2U, htU2S), NFUnit.MASTER({}), args);
formalargs := listHead(outunitlist);
formalvar := listHead(outvars);
ut2 := if formalargs == "NONE" then NFUnit.MASTER({}) else NFUnit.parseUnitString(formalargs);
b := UnitTypesEqual(ut1, ut2, htCr2U);
if b then
expList2 := {};
else
temp := makenewcref(lhs, formalvar, s1);
expList2 := {(lhs, ut1), (temp, ut2)} :: {};
end if;
// rhs
(_, (htCr2U, htS2U, htU2S), expList3) :=
insertUnitInEquation(eq.scalar, (htCr2U, htS2U, htU2S), NFUnit.MASTER({}), args);
then
List.append_reverse(expList2, expList3);
case DAE.EQUATION()
algorithm
temp := DAE.BINARY(eq.scalar, DAE.SUB(DAE.T_REAL_DEFAULT), eq.exp);
if Flags.isSet(Flags.DUMP_EQ_UNIT_STRUCT) then
ExpressionDump.dumpExp(temp);
end if;
(_, (htCr2U, htS2U, htU2S), inconsistentUnits) :=
insertUnitInEquation(temp, (htCr2U, htS2U, htU2S), NFUnit.MASTER({}), args);
then
inconsistentUnits;
case DAE.EQUEQUATION() then {};
case DAE.INITIALEQUATION()
algorithm
temp := DAE.BINARY(eq.exp2, DAE.SUB(DAE.T_REAL_DEFAULT), eq.exp1);
if Flags.isSet(Flags.DUMP_EQ_UNIT_STRUCT) then
ExpressionDump.dumpExp(temp);
end if;
(_, (htCr2U, htS2U, htU2S), inconsistentUnits) :=
insertUnitInEquation(temp, (htCr2U, htS2U, htU2S), NFUnit.MASTER({}), args);
then
inconsistentUnits;
case DAE.ARRAY_EQUATION()
algorithm
temp := DAE.BINARY(eq.array, DAE.SUB(DAE.T_REAL_DEFAULT), eq.exp);
if Flags.isSet(Flags.DUMP_EQ_UNIT_STRUCT) then
ExpressionDump.dumpExp(temp);
end if;
(_, (htCr2U, htS2U, htU2S), inconsistentUnits) :=
insertUnitInEquation(temp, (htCr2U, htS2U, htU2S), NFUnit.MASTER({}), args);
then
inconsistentUnits;
case DAE.INITIAL_ARRAY_EQUATION()
algorithm
temp := DAE.BINARY(eq.array, DAE.SUB(DAE.T_REAL_DEFAULT), eq.exp);
if Flags.isSet(Flags.DUMP_EQ_UNIT_STRUCT) then
ExpressionDump.dumpExp(temp);
end if;
(_, (htCr2U, htS2U, htU2S), inconsistentUnits) :=
insertUnitInEquation(temp, (htCr2U, htS2U, htU2S), NFUnit.MASTER({}), args);
then
inconsistentUnits;
case DAE.COMPLEX_EQUATION()
algorithm
temp := DAE.BINARY(eq.rhs, DAE.SUB(DAE.T_REAL_DEFAULT), eq.lhs);
if Flags.isSet(Flags.DUMP_EQ_UNIT_STRUCT) then
ExpressionDump.dumpExp(temp);
end if;
(_, (htCr2U, htS2U, htU2S), inconsistentUnits) :=
insertUnitInEquation(temp, (htCr2U, htS2U, htU2S), NFUnit.MASTER({}), args);
then
inconsistentUnits;
case DAE.INITIAL_COMPLEX_EQUATION()
algorithm
temp := DAE.BINARY(eq.rhs, DAE.SUB(DAE.T_REAL_DEFAULT), eq.lhs);
if Flags.isSet(Flags.DUMP_EQ_UNIT_STRUCT) then
ExpressionDump.dumpExp(temp);
end if;
(_, (htCr2U, htS2U, htU2S), inconsistentUnits) :=
insertUnitInEquation(temp, (htCr2U, htS2U, htU2S), NFUnit.MASTER({}), args);
then
inconsistentUnits;
case DAE.WHEN_EQUATION()
algorithm
for e in eq.equations loop
(htCr2U, htS2U, htU2S, inconsistentUnits) := foldEquation2(e, htCr2U, htS2U, htU2S, args);
end for;
then
inconsistentUnits;
case DAE.IF_EQUATION() then {};
case DAE.INITIAL_IF_EQUATION() then {};
case DAE.NORETCALL()
algorithm
(_, (htCr2U, htS2U, htU2S), inconsistentUnits) :=
insertUnitInEquation(eq.exp, (htCr2U, htS2U, htU2S), NFUnit.MASTER({}), args);
then
inconsistentUnits;
case DAE.INITIAL_NORETCALL()
algorithm
(_, (htCr2U, htS2U, htU2S), inconsistentUnits) :=
insertUnitInEquation(eq.exp, (htCr2U, htS2U, htU2S), NFUnit.MASTER({}), args);
then
inconsistentUnits;
case DAE.INITIAL_ASSERT() then {};
case DAE.ASSERT() then {};
case DAE.TERMINATE() then {};
case DAE.INITIAL_TERMINATE() then {};
case DAE.REINIT() then {};
case DAE.ALGORITHM() then {};
case DAE.INITIALALGORITHM() then {};
else
algorithm
Error.addInternalError(getInstanceName() + " failed on: " +
DAEDump.dumpEquationStr(eq), sourceInfo());
then
fail();
end match;
end foldEquation2;
protected function makenewcref
input DAE.Exp inexp;
input String instring;
input String instring1;
output DAE.Exp outexp;
algorithm
outexp:=match(inexp,instring,instring1)
local
DAE.ComponentRef cr;
String name,name1,s1,s2;
case (DAE.CREF(componentRef=DAE.CREF_IDENT(ident=name )),s1,s2)
equation
name=s2+"()"+"."+s1;
cr=ComponentReference.makeUntypedCrefIdent(name);
inexp.componentRef=cr;
outexp=inexp;
then
outexp;
end match;
end makenewcref;
protected function insertUnitInEquation "inserts the units in Equation and check if the equation is consistent or not"
input DAE.Exp inEq;
input tuple<NFHashTableCrToUnit.HashTable /* inHtCr2U */, NFHashTableStringToUnit.HashTable /* inHtS2U */, NFHashTableUnitToString.HashTable /* inHtU2S */> inTpl;
input NFUnit.Unit inUt;
input list<Functionargs> inargs;
output NFUnit.Unit outUt;
output tuple<NFHashTableCrToUnit.HashTable /* outHtCr2U */, NFHashTableStringToUnit.HashTable /* outHtS2U */, NFHashTableUnitToString.HashTable /* outHtU2S */> outTpl;
output list<list<tuple<DAE.Exp, NFUnit.Unit>>> outexpList;
algorithm
(outUt, outTpl, outexpList) := matchcontinue(inEq, inTpl, inUt)
local
DAE.ComponentRef cr;
DAE.Exp exp1, exp2, exp3;
DAE.Type ty;
NFHashTableCrToUnit.HashTable HtCr2U;
NFHashTableStringToUnit.HashTable HtS2U;
NFHashTableUnitToString.HashTable HtU2S;
Integer i, i1, i2, i3, i4, i5, i6, i7;
list<DAE.ComponentRef> lcr, lcr2;
list<DAE.Exp> ExpList;
list<list<tuple<DAE.Exp, NFUnit.Unit>>> expListList, expListList2, expListList3;
Real factor1;
Real r;
String s1, s2;
Absyn.Path path;
NFUnit.Unit ut, ut2;
list<String> invars,outvars,inunitlist,outunitlist;
//SUB equal summands
case (DAE.BINARY(exp1, DAE.SUB(), exp2), (HtCr2U, HtS2U, HtU2S), _) equation
(ut as NFUnit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
(ut2, (HtCr2U, HtS2U, HtU2S), expListList2) = insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), ut, inargs);
(true, ut, HtCr2U) = UnitTypesEqual(ut, ut2, HtCr2U);
expListList=List.append_reverse(expListList, expListList2);
then
(ut, (HtCr2U, HtS2U, HtU2S), expListList);
//SUB equal summands
case (DAE.BINARY(exp1, DAE.SUB(), exp2), (HtCr2U, HtS2U, HtU2S), _) equation
(ut2, (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
(ut, (HtCr2U, HtS2U, HtU2S), expListList2) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), ut2, inargs);
(true, ut, HtCr2U) = UnitTypesEqual(ut, ut2, HtCr2U);
expListList=List.append_reverse(expListList, expListList2);
then
(ut, (HtCr2U, HtS2U, HtU2S), expListList);
//SUB unequal summands
case (DAE.BINARY(exp1, DAE.SUB(), exp2), (HtCr2U, HtS2U, HtU2S), _) equation
(ut as NFUnit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
(ut2, (HtCr2U, HtS2U, HtU2S), expListList2) = insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), ut, inargs);
(false, _, _) = UnitTypesEqual(ut, ut2, HtCr2U);
expListList = List.append_reverse(expListList, expListList2);
expListList = {(exp1, ut), (exp2, ut2)}::expListList;
then
(NFUnit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
//SUB unequal summands
case (DAE.BINARY(exp1, DAE.SUB(), exp2), (HtCr2U, HtS2U, HtU2S), _) equation
(ut2, (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
(ut, (HtCr2U, HtS2U, HtU2S), expListList2) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), ut2, inargs);
(false, _, _) = UnitTypesEqual(ut, ut2, HtCr2U);
expListList = List.append_reverse(expListList, expListList2);
expListList = {(exp1, ut), (exp2, ut2)}::expListList;
then
(NFUnit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
//ADD equal summands
case (DAE.BINARY(exp1, DAE.ADD(), exp2), (HtCr2U, HtS2U, HtU2S), _) equation
(ut as NFUnit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
(ut2, (HtCr2U, HtS2U, HtU2S), expListList2) = insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), ut, inargs);
(true, ut, HtCr2U) = UnitTypesEqual(ut, ut2, HtCr2U);
expListList = List.append_reverse(expListList, expListList2);
then
(ut, (HtCr2U, HtS2U, HtU2S), expListList);
//ADD equal summands
case (DAE.BINARY(exp1, DAE.ADD(), exp2), (HtCr2U, HtS2U, HtU2S), _) equation
(ut2, (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
(ut, (HtCr2U, HtS2U, HtU2S), expListList2) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), ut2, inargs);
(true, ut, HtCr2U) = UnitTypesEqual(ut, ut2, HtCr2U);
expListList = List.append_reverse(expListList, expListList2);
then
(ut, (HtCr2U, HtS2U, HtU2S), expListList);
//ADD unequal summands
case (DAE.BINARY(exp1, DAE.ADD(), exp2), (HtCr2U, HtS2U, HtU2S), _) equation
(ut as NFUnit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
(ut2, (HtCr2U, HtS2U, HtU2S), expListList2) = insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), ut, inargs);
(false, _, _) = UnitTypesEqual(ut, ut2, HtCr2U);
expListList = List.append_reverse(expListList, expListList2);
expListList = {(exp1, ut), (exp2, ut2)}::expListList;
then
(NFUnit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
//ADD unequal
case (DAE.BINARY(exp1, DAE.ADD(), exp2), (HtCr2U, HtS2U, HtU2S), _) equation
(ut2, (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
(ut, (HtCr2U, HtS2U, HtU2S), expListList2) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), ut2, inargs);
(false, _, _) = UnitTypesEqual(ut, ut2, HtCr2U);
expListList = List.append_reverse(expListList, expListList2);
expListList = {(exp1, ut), (exp2, ut2)}::expListList;
then
(NFUnit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
//MUL
case (DAE.BINARY(exp1, DAE.MUL(), exp2), (HtCr2U, HtS2U, HtU2S), _) equation
(ut as NFUnit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
(ut2 as NFUnit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList2) = insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
//s1="(" + Unit.unitString(ut, HtU2S) + ").(" + Unit.unitString(ut2, HtU2S) + ")";
ut = NFUnit.unitMul(ut, ut2);
s1 = NFUnit.unitString(ut, HtU2S);
expListList = List.append_reverse(expListList, expListList2);
HtS2U = addUnit2HtS2U((s1, ut), HtS2U);
HtU2S = addUnit2HtU2S((s1, ut), HtU2S);
then
(ut, (HtCr2U, HtS2U, HtU2S), expListList);
case (DAE.BINARY(exp1, DAE.MUL(), exp2), (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER()) equation
(NFUnit.MASTER(), (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
(NFUnit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList2) = insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
expListList = List.append_reverse(expListList, expListList2);
then
(NFUnit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
case (DAE.BINARY(exp1, DAE.MUL(), exp2), (HtCr2U, HtS2U, HtU2S), NFUnit.UNIT()) equation
(NFUnit.MASTER(varList=lcr), (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
(ut2 as NFUnit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList2) = insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
//s1="(" + Unit.unitString(inUt, HtU2S) + ")/(" + Unit.unitString(ut2, HtU2S) + ")";
ut = NFUnit.unitDiv(inUt, ut2);
s1 = NFUnit.unitString(ut, HtU2S);
HtCr2U = List.fold1(lcr, updateHtCr2U, ut, HtCr2U);
expListList = List.append_reverse(expListList, expListList2);
HtS2U = addUnit2HtS2U((s1, ut), HtS2U);
HtU2S = addUnit2HtU2S((s1, ut), HtU2S);
then
(inUt, (HtCr2U, HtS2U, HtU2S), expListList);
case (DAE.BINARY(exp1, DAE.MUL(), exp2), (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER()) equation
(NFUnit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
(NFUnit.MASTER(), (HtCr2U, HtS2U, HtU2S), expListList2) = insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
expListList = List.append_reverse(expListList, expListList2);
then
(NFUnit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
case (DAE.BINARY(exp1, DAE.MUL(), exp2), (HtCr2U, HtS2U, HtU2S), NFUnit.UNIT()) equation
(ut2 as NFUnit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
(NFUnit.MASTER(varList=lcr), (HtCr2U, HtS2U, HtU2S), expListList2) = insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
//s1="(" + Unit.unitString(inUt, HtU2S) + ")/(" + Unit.unitString(ut2, HtU2S) + ")";
ut = NFUnit.unitDiv(inUt, ut2);
s1 = NFUnit.unitString(ut, HtU2S);
HtCr2U = List.fold1(lcr, updateHtCr2U, ut, HtCr2U);
expListList = List.append_reverse(expListList, expListList2);
HtS2U = addUnit2HtS2U((s1, ut), HtS2U);
HtU2S = addUnit2HtU2S((s1, ut), HtU2S);
then
(inUt, (HtCr2U, HtS2U, HtU2S), expListList);
case (DAE.BINARY(exp1, DAE.MUL(), exp2), (HtCr2U, HtS2U, HtU2S), _) equation
(NFUnit.MASTER(), (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
(NFUnit.MASTER(), (HtCr2U, HtS2U, HtU2S), expListList2) = insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
expListList = List.append_reverse(expListList, expListList2);
then
(NFUnit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
//DIV
case (DAE.BINARY(exp1, DAE.DIV(), exp2), (HtCr2U, HtS2U, HtU2S), _) equation
(ut as NFUnit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
(ut2 as NFUnit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList2) = insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
//s1="(" + Unit.unitString(ut, HtU2S) + ")/(" + Unit.unitString(ut2, HtU2S) + ")";
ut = NFUnit.unitDiv(ut, ut2);
s1 = NFUnit.unitString(ut, HtU2S);
expListList = List.append_reverse(expListList, expListList2);
HtS2U = addUnit2HtS2U((s1, ut), HtS2U);
HtU2S = addUnit2HtU2S((s1, ut), HtU2S);
then
(ut, (HtCr2U, HtS2U, HtU2S), expListList);
case (DAE.BINARY(exp1, DAE.DIV(), exp2), (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER()) equation
(NFUnit.MASTER(), (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
(NFUnit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList2) = insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
expListList = List.append_reverse(expListList, expListList2);
then
(NFUnit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
case (DAE.BINARY(exp1, DAE.DIV(), exp2), (HtCr2U, HtS2U, HtU2S), NFUnit.UNIT()) equation
(NFUnit.MASTER(varList=lcr), (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
(ut2 as NFUnit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList2) = insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
//s1="(" + Unit.unitString(inUt, HtU2S) + ").(" + Unit.unitString(ut2, HtU2S) + ")";
ut = NFUnit.unitMul(inUt, ut2);
s1 = NFUnit.unitString(ut, HtU2S);
HtCr2U = List.fold1(lcr, updateHtCr2U, ut, HtCr2U);
expListList = List.append_reverse(expListList, expListList2);
HtS2U = addUnit2HtS2U((s1, ut), HtS2U);
HtU2S = addUnit2HtU2S((s1, ut), HtU2S);
then
(inUt, (HtCr2U, HtS2U, HtU2S), expListList);
case (DAE.BINARY(exp1, DAE.DIV(), exp2), (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER()) equation
(NFUnit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
(NFUnit.MASTER(), (HtCr2U, HtS2U, HtU2S), expListList2) = insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
expListList = List.append_reverse(expListList, expListList2);
then
(NFUnit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
case (DAE.BINARY(exp1, DAE.DIV(), exp2), (HtCr2U, HtS2U, HtU2S), NFUnit.UNIT()) equation
(ut2 as NFUnit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
(NFUnit.MASTER(varList=lcr), (HtCr2U, HtS2U, HtU2S), expListList2) = insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
//s1="(" + Unit.unitString(ut2, HtU2S) + ")/(" + Unit.unitString(inUt, HtU2S) + ")";
ut = NFUnit.unitDiv(ut2, inUt);
s1 = NFUnit.unitString(ut, HtU2S);
HtCr2U = List.fold1(lcr, updateHtCr2U, ut, HtCr2U);
expListList = List.append_reverse(expListList, expListList2);
HtS2U = addUnit2HtS2U((s1, ut), HtS2U);
HtU2S = addUnit2HtU2S((s1, ut), HtU2S);
then
(inUt, (HtCr2U, HtS2U, HtU2S), expListList);
case (DAE.BINARY(exp1, DAE.DIV(), exp2), (HtCr2U, HtS2U, HtU2S), _) equation
(NFUnit.MASTER(), (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
(NFUnit.MASTER(), (HtCr2U, HtS2U, HtU2S), expListList2) = insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
expListList = List.append_reverse(expListList, expListList2);
then
(NFUnit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
//POW
case (DAE.BINARY(exp1, DAE.POW(), DAE.RCONST(r)), (HtCr2U, HtS2U, HtU2S), _) equation
(ut as NFUnit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
i = realInt(r);
true = realEq(r, intReal(i));
ut = NFUnit.unitPow(ut, i);
s1 = NFUnit.unitString(ut, HtU2S);
HtS2U = addUnit2HtS2U((s1, ut), HtS2U);
HtU2S = addUnit2HtU2S((s1, ut), HtU2S);
then
(ut, (HtCr2U, HtS2U, HtU2S), expListList);
case (DAE.BINARY(exp1, DAE.POW(), DAE.RCONST(r)), (HtCr2U, HtS2U, HtU2S), ut as NFUnit.UNIT()) equation
(NFUnit.MASTER(lcr), (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
NFUnit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7) = NFUnit.unitRoot(ut, r);
HtCr2U = List.fold1(lcr, updateHtCr2U, NFUnit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7), HtCr2U);
s1 = NFUnit.unitString(NFUnit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7), HtU2S);
HtS2U = addUnit2HtS2U((s1, NFUnit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7)), HtS2U);
HtU2S = addUnit2HtU2S((s1, NFUnit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7)), HtU2S);
then
(inUt, (HtCr2U, HtS2U, HtU2S), expListList);
case (DAE.BINARY(exp1, DAE.POW(), DAE.RCONST(_)), (HtCr2U, HtS2U, HtU2S), _) equation
(_, (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
then
(NFUnit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
//PRE
case (DAE.CALL(path=Absyn.IDENT(name="pre"), expLst={exp1}), (HtCr2U, HtS2U, HtU2S), _) equation
(ut, (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
then (ut, (HtCr2U, HtS2U, HtU2S), expListList);
//DER
case (DAE.CALL(path=Absyn.IDENT(name="der"), expLst={exp1}), (HtCr2U, HtS2U, HtU2S), NFUnit.UNIT()) equation
(NFUnit.MASTER(lcr), (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
ut = NFUnit.unitMul(inUt, NFUnit.UNIT(1e0, 0, 0, 0, 1, 0, 0, 0));
HtCr2U = List.fold1(lcr, updateHtCr2U, ut, HtCr2U);
s1 = NFUnit.unitString(ut, HtU2S);
HtS2U = addUnit2HtS2U((s1, ut), HtS2U);
HtU2S = addUnit2HtU2S((s1, ut), HtU2S);
then
(inUt, (HtCr2U, HtS2U, HtU2S), expListList);
case (DAE.CALL(path = Absyn.IDENT(name="der"), expLst={exp1}), (HtCr2U, HtS2U, HtU2S), _) equation
(ut as NFUnit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList)=insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
ut=NFUnit.unitDiv(ut, NFUnit.UNIT(1e0, 0, 0, 0, 1, 0, 0, 0));
s1=NFUnit.unitString(ut, HtU2S);
HtS2U=addUnit2HtS2U((s1, ut), HtS2U);
HtU2S=addUnit2HtU2S((s1, ut), HtU2S);
then (ut, (HtCr2U, HtS2U, HtU2S), expListList);
case (DAE.CALL(path = Absyn.IDENT(name="der"), expLst={exp1}), (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER()) equation
(NFUnit.MASTER(), (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
then (NFUnit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
//SQRT
case (DAE.CALL(path=Absyn.IDENT(name="sqrt"), expLst={exp1}), (HtCr2U, HtS2U, HtU2S), _) equation
(ut as NFUnit.UNIT(), (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
NFUnit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7) = NFUnit.unitRoot(ut, 2.0);
s1 = NFUnit.unitString(NFUnit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7), HtU2S);
HtS2U = addUnit2HtS2U((s1, NFUnit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7)), HtS2U);
HtU2S = addUnit2HtU2S((s1, NFUnit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7)), HtU2S);
then (NFUnit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7), (HtCr2U, HtS2U, HtU2S), expListList);
case (DAE.CALL(path=Absyn.IDENT(name="sqrt"), expLst={exp1}), (HtCr2U, HtS2U, HtU2S), NFUnit.UNIT()) equation
(NFUnit.MASTER(lcr), (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
ut = NFUnit.unitPow(inUt, 2);
s1 = NFUnit.unitString(ut, HtU2S);
HtCr2U = List.fold1(lcr, updateHtCr2U, ut, HtCr2U);
HtS2U = addUnit2HtS2U((s1, ut), HtS2U);
HtU2S = addUnit2HtU2S((s1, ut), HtU2S);
then (inUt, (HtCr2U, HtS2U, HtU2S), expListList);
case (DAE.CALL(path=Absyn.IDENT(name="sqrt"), expLst={exp1}), (HtCr2U, HtS2U, HtU2S), _) equation
(_, (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
then (NFUnit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
//IFEXP
case (DAE.IFEXP(_, exp2, exp3), (HtCr2U, HtS2U, HtU2S), _) equation
//(_, (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
(ut, (HtCr2U, HtS2U, HtU2S), expListList2) = insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
(ut2, (HtCr2U, HtS2U, HtU2S), expListList3) = insertUnitInEquation(exp3, (HtCr2U, HtS2U, HtU2S), ut, inargs);
(true, ut, HtCr2U) = UnitTypesEqual(ut, ut2, HtCr2U);
//expListList = List.append_reverse(expListList, expListList2);
//expListList = List.append_reverse(expListList, expListList3);
expListList = List.append_reverse(expListList2, expListList3);
then (ut, (HtCr2U, HtS2U, HtU2S), expListList);
case (DAE.IFEXP(_, exp2, exp3), (HtCr2U, HtS2U, HtU2S), _) equation
//(_, (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), NFUnit.MASTER({}), inargs);
(ut, (HtCr2U, HtS2U, HtU2S), expListList2) = insertUnitInEquation(exp2, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
(ut2, (HtCr2U, HtS2U, HtU2S), expListList3) = insertUnitInEquation(exp3, (HtCr2U, HtS2U, HtU2S), ut, inargs);
(false, _, _) = UnitTypesEqual(ut, ut2, HtCr2U);
//expListList = List.append_reverse(expListList, expListList2);
//expListList = List.append_reverse(expListList, expListList3);
expListList = List.append_reverse(expListList2, expListList3);
expListList = {(exp2, ut), (exp3, ut2)}::expListList;
then (NFUnit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
//RELATIONS
case (DAE.RELATION(exp1=exp1), (HtCr2U, HtS2U, HtU2S), _) equation
(ut, (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
(ut2, (HtCr2U, HtS2U, HtU2S), expListList2) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
(true, ut, HtCr2U) = UnitTypesEqual(ut, ut2, HtCr2U);
expListList = List.append_reverse(expListList, expListList2);
then (ut, (HtCr2U, HtS2U, HtU2S), expListList);
case (DAE.RELATION(exp1=exp1, exp2=exp2), (HtCr2U, HtS2U, HtU2S), _) equation
(ut, (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
(ut2, (HtCr2U, HtS2U, HtU2S), expListList2) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
(false, _, _) = UnitTypesEqual(ut, ut2, HtCr2U);
expListList = List.append_reverse(expListList, expListList2);
expListList = {(exp1, ut), (exp2, ut2)}::expListList;
then (NFUnit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
// builtin function calls
case (DAE.CALL(path=Absyn.IDENT(), expLst=ExpList), (HtCr2U, HtS2U, HtU2S), _) equation
(HtCr2U, HtS2U, HtU2S, expListList) = foldCallArg(ExpList, HtCr2U, HtS2U, HtU2S);
then (NFUnit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
//user defined function CALL
case (DAE.CALL(path=Absyn.FULLYQUALIFIED(path), expLst=ExpList), (HtCr2U, HtS2U, HtU2S), _) equation
s1 = Absyn.pathString(path);
s1 = System.trim(s1,".");
(invars, _, inunitlist, _) = getNamedUnitlist(s1, inargs);
(HtCr2U, HtS2U, HtU2S, expListList) = foldCallArg1(ExpList, HtCr2U, HtS2U, HtU2S, inUt, inunitlist, invars, s1);
then (NFUnit.MASTER({}), (HtCr2U, HtS2U, HtU2S), expListList);
//UMINUS
case (DAE.UNARY(DAE.UMINUS(), exp1), (HtCr2U, HtS2U, HtU2S), _) equation
(ut, (HtCr2U, HtS2U, HtU2S), expListList) = insertUnitInEquation(exp1, (HtCr2U, HtS2U, HtU2S), inUt, inargs);
then (ut, (HtCr2U, HtS2U, HtU2S), expListList);
//"time"
case (DAE.CREF(componentRef=cr), (HtCr2U, HtS2U, HtU2S), _) equation
true = ComponentReference.crefEqual(cr, DAE.crefTime);
ut = NFUnit.UNIT(1e0, 0, 0, 0, 1, 0, 0, 0);
HtS2U = addUnit2HtS2U(("time", ut), HtS2U);
HtU2S = addUnit2HtU2S(("time", ut), HtU2S);
then (ut, (HtCr2U, HtS2U, HtU2S), {});
//CREF
case (DAE.CREF(componentRef=cr, ty=DAE.T_REAL()), (HtCr2U, _, _), _) equation
ut = BaseHashTable.get(cr, HtCr2U);
then (ut, inTpl, {});
//NO UNIT IN EQUATION
// all unhandled expressions, e.g. DAE.CAST, DAE.TUPLE, ...
else
//Error.addInternalError("./Compiler/NFFrontEnd/NFUnitCheck.mo: function insertUnitInEquation failed for " + ExpressionDump.printExpStr(inEq), sourceInfo());
then (NFUnit.MASTER({}), inTpl, {});
end matchcontinue;
end insertUnitInEquation;
protected function getNamedUnitlist
input String instring;
input list<Functionargs> inargs;
output list<String> outargs;
output list<String> outargs2;
output list<String> outargs3;
output list<String> outargs4;
algorithm
(outargs,outargs2,outargs3,outargs4):=match(instring, inargs)
local
list<Functionargs> rest;
String fnname,fnname1;
list<String> invars,inunitlist, outunitlist,outvars;
case(fnname,FUNCTIONUNITS(fnname1,invars,outvars,inunitlist,outunitlist)::_)
guard stringEq(fnname,fnname1)
equation
inunitlist=inunitlist;
outunitlist=outunitlist;
then
(invars,outvars,inunitlist,outunitlist);
case(fnname,_::rest)
equation
(invars,outvars,inunitlist,outunitlist)=getNamedUnitlist(fnname,rest);
then
(invars,outvars,inunitlist,outunitlist);
case(_,_) then ({},{},{},{});
end match;
end getNamedUnitlist;
protected function UnitTypesEqual "checks equality of two UnitExp's"
input NFUnit.Unit inut;
input NFUnit.Unit inut2;
input NFHashTableCrToUnit.HashTable inHtCr2U;
output Boolean b;
output NFUnit.Unit outUt;
output NFHashTableCrToUnit.HashTable outHtCr2U;
algorithm
(b, outUt, outHtCr2U) := matchcontinue(inut, inut2, inHtCr2U)
local
String s, s2;
Integer i1, i2, i3, i4, i5, i6, i7;
Integer j1, j2, j3, j4, j5, j6, j7;
list<DAE.ComponentRef> lcr, lcr2;
NFHashTableCrToUnit.HashTable HtCr2U;
Real factor1, factor2, r;
NFUnit.Unit ut;
case (NFUnit.UNIT(factor1, i1, i2, i3, i4, i5, i6, i7), NFUnit.UNIT(factor2, j1, j2, j3, j4, j5, j6, j7), _) equation