-
Notifications
You must be signed in to change notification settings - Fork 298
/
NFArrayConnections.mo
1035 lines (899 loc) · 30.5 KB
/
NFArrayConnections.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-CurrentYear, Linköping University,
* 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
* AND THIS OSMC PUBLIC LICENSE (OSMC-PL).
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
* ACCEPTANCE OF THE OSMC PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköping University, 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 NFArrayConnections
import Connection = NFConnection;
import Connector = NFConnector;
import FlatModel = NFFlatModel;
import ComponentRef = NFComponentRef;
import Equation = NFEquation;
import Connections = NFConnections;
import Expression = NFExpression;
import SBSet;
import SBPWLinearMap;
protected
import AbsynUtil;
import AdjacencyList;
import Array;
import Call = NFCall;
import Ceval = NFCeval;
import Component = NFComponent;
import Dimension = NFDimension;
import ElementSource;
import MetaModelica.Dangerous.*;
import NFInstNode.InstNode;
import NFPrefixes.Variability;
import Operator = NFOperator;
import Op = NFOperator.Op;
import SBFunctions;
import SimplifyExp = NFSimplifyExp;
import Subscript = NFSubscript;
import System;
import Type = NFType;
import Variable = NFVariable;
uniontype SetVertex
record SET_VERTEX
ComponentRef name;
SBSet vs;
end SET_VERTEX;
function isEqual
input SetVertex v1;
input SetVertex v2;
output Boolean equal = ComponentRef.isEqual(v1.name, v2.name);
end isEqual;
function isNamed
input SetVertex v;
input ComponentRef name;
output Boolean equal = ComponentRef.isEqual(v.name, name);
end isNamed;
end SetVertex;
uniontype SetEdge
record SET_EDGE
String name;
SBPWLinearMap es1;
SBPWLinearMap es2;
end SET_EDGE;
function isEqual
input SetEdge e1;
input SetEdge e2;
output Boolean equal = e1.name == e2.name;
end isEqual;
end SetEdge;
// TODO: Implement better hash table and get rid of this.
encapsulated package NameVertexTable
import BaseHashTable;
import SBMultiInterval;
type Key = String;
type Value = SBMultiInterval;
type Table = 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 new
input Integer size = 257;
output Table table;
algorithm
table := BaseHashTable.emptyHashTableWork(size,
(stringHashDjb2Mod, stringEq, Util.id, SBMultiInterval.toString));
end new;
end NameVertexTable;
public
type SBGraph = AdjacencyList<SetVertex, SetEdge>;
function resolve
input output FlatModel flatModel;
protected
Integer max_dim = 1;
Vector<Integer> v_count, e_count;
list<Equation> conns, eql;
SBGraph graph;
SBSet vss;
SBPWLinearMap res, emap1, emap2;
NameVertexTable.Table nmv_table;
algorithm
for var in flatModel.variables loop
max_dim := max(max_dim, Type.dimensionCount(var.ty));
end for;
v_count := Vector.newFill(max_dim, 1);
e_count := Vector.newFill(max_dim, 1);
(flatModel, conns) := collect(flatModel);
if listEmpty(conns) then
return;
end if;
graph := AdjacencyList.new(SetVertex.isEqual, SetEdge.isEqual);
nmv_table := NameVertexTable.new();
nmv_table := createGraph(conns, graph, v_count, e_count, nmv_table);
(vss, emap1, emap2) := createMaps(graph);
res := SBFunctions.connectedComponents(vss, emap1, emap2);
conns := generateEquations(res, flatModel, graph, v_count, nmv_table);
eql := listAppend(flatModel.equations, conns);
flatModel.equations := eql;
end resolve;
protected
function collect
input output FlatModel flatModel;
output list<Equation> conns = {};
protected
list<Equation> eql = {};
algorithm
(conns, eql) := List.splitOnTrue(flatModel.equations, isConnection);
flatModel.equations := eql;
end collect;
function isConnection
input Equation eq;
output Boolean isConn;
algorithm
isConn := match eq
local
Equation e;
case Equation.CONNECT() then true;
case Equation.FOR(body = e :: _) then isConnection(e);
else false;
end match;
end isConnection;
function createGraph
input list<Equation> equations;
input SBGraph graph;
input Vector<Integer> vCount;
input Vector<Integer> eCount;
input output NameVertexTable.Table nmvTable;
protected
Expression range;
list<Equation> body;
algorithm
for eq in equations loop
() := match eq
case Equation.CONNECT()
algorithm
nmvTable := createConnection(eq.lhs, eq.rhs, eq.source, graph, vCount, eCount, nmvTable);
then
();
case Equation.FOR(range = SOME(range))
algorithm
range := Ceval.evalExp(range, Ceval.EvalTarget.RANGE(Equation.info(eq)));
body := applyIterator(eq.iterator, range, eq.body);
nmvTable := createGraph(body, graph, vCount, eCount, nmvTable);
then
();
else
algorithm
Error.assertion(false, getInstanceName() + " got unknown equation " +
Equation.toString(eq) + "\n", sourceInfo());
then
fail();
end match;
end for;
end createGraph;
function applyIterator
input InstNode iterator;
input Expression range;
input output list<Equation> body;
algorithm
body := Equation.mapExpList(body,
function Expression.replaceIterator(iterator = iterator, iteratorValue = range));
end applyIterator;
function createConnection
input Expression lhs;
input Expression rhs;
input DAE.ElementSource source;
input SBGraph graph;
input Vector<Integer> vCount;
input Vector<Integer> eCount;
input output NameVertexTable.Table nmvTable;
protected
ComponentRef lhs_cr, rhs_cr;
list<Subscript> lhs_subs, rhs_subs;
SBMultiInterval mi1, mi2;
AdjacencyList.VertexDescriptor d1, d2;
SourceInfo info = ElementSource.getInfo(source);
algorithm
(lhs_cr, lhs_subs) := separate(Expression.toCref(lhs));
(rhs_cr, rhs_subs) := separate(Expression.toCref(rhs));
(mi1, d1, nmvTable) := getConnectIntervals(lhs_cr, lhs_subs, graph, vCount, nmvTable, info);
(mi2, d2, nmvTable) := getConnectIntervals(rhs_cr, rhs_subs, graph, vCount, nmvTable, info);
updateGraph(d1, d2, mi1, mi2, graph, eCount);
end createConnection;
function separate
input output ComponentRef cref;
output list<Subscript> subs;
algorithm
subs := ComponentRef.subscriptsAllFlat(cref);
cref := ComponentRef.stripSubscriptsAll(cref);
// TODO: Replace any : in the subs with the correct dimension.
end separate;
function getConnectIntervals
input ComponentRef cr;
input list<Subscript> subs;
input SBGraph graph;
input Vector<Integer> vCount;
input NameVertexTable.Table nmvTable;
input SourceInfo info;
output SBMultiInterval outMI;
output AdjacencyList.VertexDescriptor d;
output NameVertexTable.Table outNmvTable = nmvTable;
protected
function make_lo_interval
input SBInterval i;
output SBInterval res;
protected
Integer lo = SBInterval.lowerBound(i);
algorithm
res := SBInterval.new(lo, 1, lo);
end make_lo_interval;
protected
array<SBInterval> mi, miv;
SBInterval int;
Integer index, aux_lo;
Expression sub_exp;
algorithm
(outMI, d, outNmvTable) := createVertex(cr, graph, vCount, nmvTable);
miv := SBMultiInterval.intervals(outMI);
if listEmpty(subs) then
mi := Array.map(miv, make_lo_interval);
else
index := 1;
mi := arrayCreate(listLength(subs), SBInterval.newEmpty());
for s in subs loop
sub_exp := evalCrefs(Subscript.toExp(s));
int := intervalFromExp(sub_exp);
aux_lo := SBInterval.lowerBound(miv[index]) - 1;
int := SBInterval.new(aux_lo + SBInterval.lowerBound(int),
SBInterval.stepValue(int),
aux_lo + SBInterval.upperBound(int));
if not SBInterval.isEmpty(int) then
mi[index] := int;
else
mi := listArray({});
break;
end if;
index := index + 1;
end for;
end if;
outMI := SBMultiInterval.fromArray(mi);
end getConnectIntervals;
function createVertex
input ComponentRef cr;
input SBGraph graph;
input Vector<Integer> vCount;
input NameVertexTable.Table nmvTable;
output SBMultiInterval mi;
output AdjacencyList.VertexDescriptor d;
output NameVertexTable.Table outNmvTable = nmvTable;
protected
Option<AdjacencyList.VertexDescriptor> od;
SetVertex v;
list<Dimension> dims;
Integer vc, dim_size, index;
SBSet s;
array<SBInterval> ints;
Vector<Integer> new_vc;
SBInterval int;
algorithm
od := AdjacencyList.findVertex(graph, function SetVertex.isNamed(name = cr));
if isSome(od) then
SOME(d) := od;
v := AdjacencyList.getVertex(graph, d);
mi := SBAtomicSet.aset(UnorderedSet.first(SBSet.asets(v.vs)));
return;
end if;
dims := crefDims(cr);
if listEmpty(dims) then
vc := Vector.get(vCount, 1);
Vector.update(vCount, 1, vc + 1);
mi := SBMultiInterval.fromArray(arrayCreate(Vector.size(vCount), SBInterval.new(vc, 1, vc)));
else
ints := arrayCreate(listLength(dims), SBInterval.newEmpty());
new_vc := Vector.copy(vCount);
index := 1;
for dim in dims loop
if not Dimension.isKnown(dim) then
Error.assertion(false, getInstanceName() + ": unknown dimension " + Dimension.toString(dim),
sourceInfo());
end if;
dim_size := Dimension.size(dim);
vc := Vector.get(vCount, index);
int := SBInterval.new(vc, 1, vc + dim_size - 1);
if SBInterval.isEmpty(int) then
ints := listArray({});
break;
else
ints[index] := int;
Vector.update(new_vc, index, vc + dim_size);
end if;
index := index + 1;
end for;
mi := SBMultiInterval.fromArray(ints);
if not SBMultiInterval.isEmpty(mi) then
Vector.swap(new_vc, vCount);
end if;
end if;
s := SBSet.newEmpty();
s := SBSet.addAtomicSet(SBAtomicSet.new(mi), s);
v := SET_VERTEX(cr, s);
d := AdjacencyList.addVertex(graph, v);
outNmvTable := BaseHashTable.addUnique((ComponentRef.toString(cr), mi), nmvTable);
end createVertex;
function crefDims
input ComponentRef cr;
output list<Dimension> dims = {};
protected
ComponentRef c = cr;
algorithm
while not ComponentRef.isEmpty(c) loop
dims := listAppend(Type.arrayDims(ComponentRef.nodeType(c)), dims);
c := ComponentRef.rest(c);
end while;
end crefDims;
function evalCrefs
input output Expression e;
protected
function evalCref
input Expression e;
output Expression outExp;
algorithm
if Expression.isCref(e) then
outExp := Ceval.evalExp(e, Ceval.EvalTarget.RANGE(AbsynUtil.dummyInfo));
else
outExp := e;
end if;
end evalCref;
algorithm
e := Expression.map(e, evalCref);
end evalCrefs;
function intervalFromExp
input Expression e;
output SBInterval i;
algorithm
i := match e
case Expression.INTEGER() then SBInterval.new(e.value, 1, e.value);
case Expression.BOOLEAN() then SBInterval.new(Util.boolInt(e.value), 1, Util.boolInt(e.value));
case Expression.REAL() then SBInterval.new(realInt(e.value), 1, realInt(e.value));
case Expression.BINARY() then intervalFromBinaryExp(e.exp1, e.operator, e.exp2);
case Expression.UNARY() then intervalFromUnaryExp(e.exp);
case Expression.RANGE() then intervalFromRange(e);
else
algorithm
Error.assertion(false, getInstanceName() + " got unknown expression " +
Expression.toString(e), sourceInfo());
then
fail();
end match;
end intervalFromExp;
function intervalFromBinaryExp
input Expression lhs;
input Operator op;
input Expression rhs;
output SBInterval i;
protected
SBInterval lhs_i, rhs_i;
Integer lhs_sz, rhs_sz, res;
Integer llo, rlo, lhi, rhi, step;
algorithm
lhs_i := intervalFromExp(lhs);
rhs_i := intervalFromExp(rhs);
lhs_sz := SBInterval.size(lhs_i);
rhs_sz := SBInterval.size(rhs_i);
llo := SBInterval.lowerBound(lhs_i);
rlo := SBInterval.lowerBound(rhs_i);
if lhs_sz == 1 and rhs_sz == 1 then
Expression.INTEGER(value = res) :=
Ceval.evalBinaryOp_dispatch(Expression.INTEGER(llo), op, Expression.INTEGER(rlo));
i := SBInterval.new(res, 1, res);
elseif lhs_sz == 1 or rhs_sz == 1 then
lhi := SBInterval.upperBound(lhs_i);
rhi := SBInterval.upperBound(rhs_i);
step := SBInterval.stepValue(if lhs_sz == 1 then rhs_i else lhs_i);
i := match op.op
case Op.ADD then SBInterval.new(llo + rlo, step, lhi + rhi);
case Op.SUB then SBInterval.new(llo - rlo, step, lhi - rhi);
case Op.MUL then SBInterval.new(llo * rlo, llo * step, lhi * rhi);
else
algorithm
Error.assertion(false, getInstanceName() +
" got unknown operator " + Operator.symbol(op), sourceInfo());
then
fail();
end match;
else
Error.assertion(false, getInstanceName() + " got unknown expression " +
Expression.toString(Expression.BINARY(lhs, op, rhs)) + "\n", sourceInfo());
end if;
end intervalFromBinaryExp;
function intervalFromUnaryExp
input Expression e;
output SBInterval i;
algorithm
i := intervalFromExp(e);
i := SBInterval.new(-SBInterval.lowerBound(i), 1, -SBInterval.upperBound(i));
end intervalFromUnaryExp;
function intervalFromRange
input Expression e;
output SBInterval i;
protected
Expression start, stop;
Option<Expression> ostep;
Integer lo, step, hi;
algorithm
Expression.RANGE(start = start, step = ostep, stop = stop) := SimplifyExp.simplify(e);
lo := Expression.toInteger(start);
hi := Expression.toInteger(stop);
if isSome(ostep) then
step := Expression.toInteger(Util.getOption(ostep));
else
step := 1;
end if;
i := SBInterval.new(lo, step, hi);
end intervalFromRange;
function updateGraph
input AdjacencyList.VertexDescriptor d1;
input AdjacencyList.VertexDescriptor d2;
input SBMultiInterval mi1;
input SBMultiInterval mi2;
input SBGraph graph;
input Vector<Integer> eCount;
protected
array<SBInterval> ints1, ints2, mi;
Integer mi1_sz, mi2_sz, sz, sz1, sz2;
Integer count, aux_ec;
array<Real> g1, g2, o1, o2;
Real g1i, g2i, o1i, o2i;
SBInterval i1, i2;
Vector<Integer> new_ec;
SBSet s;
SBLinearMap lm1, lm2;
SBPWLinearMap pw1, pw2;
String name;
SetEdge se;
algorithm
ints1 := SBMultiInterval.intervals(mi1);
mi1_sz := SBMultiInterval.size(mi1);
ints2 := SBMultiInterval.intervals(mi2);
mi2_sz := SBMultiInterval.size(mi2);
if SBMultiInterval.ndim(mi1) <> SBMultiInterval.ndim(mi2) and
mi1_sz <> 1 and mi2_sz <> 1 then
Error.assertion(false, getInstanceName() + " got incompatible connect", sourceInfo());
end if;
sz := arrayLength(ints1);
g1 := arrayCreateNoInit(sz, 0.0);
g2 := arrayCreateNoInit(sz, 0.0);
o1 := arrayCreateNoInit(sz, 0.0);
o2 := arrayCreateNoInit(sz, 0.0);
mi := arrayCreateNoInit(sz, ints1[1]);
new_ec := Vector.new(0);
for i in 1:sz loop
sz1 := SBInterval.size(ints1[i]);
sz2 := SBInterval.size(ints2[i]);
if sz1 <> sz2 and sz1 <> 1 and sz2 <> 1 then
Error.assertion(false, getInstanceName() + " got incompatible connect", sourceInfo());
end if;
count := max(sz1, sz2);
aux_ec := Vector.get(eCount, i);
mi[i] := SBInterval.new(aux_ec, 1, aux_ec + count - 1);
i1 := ints1[i];
i2 := ints2[i];
if sz1 == 1 then
g1[i] := 0.0;
o1[i] := SBInterval.lowerBound(i1);
else
g1i := SBInterval.stepValue(i1);
o1i := -g1i * aux_ec + SBInterval.lowerBound(i1);
g1[i] := g1i;
o1[i] := o1i;
end if;
if sz2 == 1 then
g2[i] := 0.0;
o2[i] := SBInterval.lowerBound(i2);
else
g2i := SBInterval.stepValue(i2);
o2i := -g2i * aux_ec + SBInterval.lowerBound(i2);
g2[i] := g2i;
o2[i] := o2i;
end if;
Vector.push(new_ec, aux_ec + count);
end for;
Vector.swap(eCount, new_ec);
s := SBSet.newEmpty();
s := SBSet.addAtomicSet(SBAtomicSet.new(SBMultiInterval.fromArray(mi)), s);
lm1 := SBLinearMap.new(g1, o1);
lm2 := SBLinearMap.new(g2, o2);
pw1 := SBPWLinearMap.newScalar(s, lm1);
pw2 := SBPWLinearMap.newScalar(s, lm2);
name := "E" + String(System.tmpTick());
se := SET_EDGE(name, pw1, pw2);
AdjacencyList.addEdge(graph, d1, d2, se);
end updateGraph;
function createMaps
input SBGraph graph;
output SBSet vss;
output SBPWLinearMap emap1;
output SBPWLinearMap emap2;
protected
list<SetVertex> vs;
list<SetEdge> es;
SetEdge e;
algorithm
vss := SBSet.newEmpty();
for v in AdjacencyList.vertices(graph) loop
vss := SBSet.union(vss, v.vs);
end for;
e :: es := AdjacencyList.edges(graph);
emap1 := e.es1;
emap2 := e.es2;
for e in es loop
emap1 := SBPWLinearMap.combine(e.es1, emap1);
emap2 := SBPWLinearMap.combine(e.es2, emap2);
end for;
end createMaps;
function generateEquations
input SBPWLinearMap pw;
input FlatModel flatModel;
input SBGraph graph;
input Vector<Integer> vCount;
input NameVertexTable.Table nmvTable;
output list<Equation> equations = {};
protected
SBSet vc_dom, vc_im, aux_s, vc_domi, vc_domi_aux;
array<InstNode> iterators;
list<Variable> pot_vars, flow_vars;
list<tuple<String, ComponentRef>> vars;
list<Expression> iter_expl;
algorithm
vc_dom := SBPWLinearMap.wholeDom(pw);
vc_im := SBPWLinearMap.image(pw, vc_dom);
iterators := arrayCreate(Vector.size(vCount), InstNode.EMPTY_NODE());
for i in 1:arrayLength(iterators) loop
iterators[i] := InstNode.fromComponent(
"$i" + String(i),
Component.newIterator(Type.INTEGER(), AbsynUtil.dummyInfo),
InstNode.EMPTY_NODE()
);
end for;
iter_expl := list(Expression.fromCref(ComponentRef.makeIterator(i, Type.INTEGER())) for i in iterators);
(pot_vars, flow_vars) := getConnectors(flatModel);
for aset in UnorderedSet.toArray(SBSet.asets(vc_im)) loop
aux_s := SBSet.newEmpty();
aux_s := SBSet.addAtomicSet(aset, aux_s);
vc_domi := SBPWLinearMap.preImage(pw, aux_s);
vc_domi_aux := SBSet.complement(vc_domi, aux_s);
vars := getVars(pot_vars, aux_s, graph);
equations := generatePotentialEquations(aset, vc_domi_aux, vars, iterators,
iter_expl, pot_vars, graph, nmvTable, equations);
equations := generateFlowEquation(aset, vc_domi, iterators, flow_vars, graph, nmvTable, equations);
end for;
equations := listReverseInPlace(equations);
end generateEquations;
function intervalToRange
input SBInterval interval;
output Expression range;
protected
Integer lo = SBInterval.lowerBound(interval);
Integer hi = SBInterval.upperBound(interval);
algorithm
if lo == hi then
range := Expression.INTEGER(lo);
else
range := Expression.makeIntegerRange(lo, SBInterval.stepValue(interval), hi);
end if;
end intervalToRange;
function generatePotentialEquations
input SBAtomicSet aset;
input SBSet dom;
input list<tuple<String, ComponentRef>> vars;
input array<InstNode> iterators;
input list<Expression> iterExps;
input list<Variable> potVars;
input SBGraph graph;
input NameVertexTable.Table nmvTable;
input output list<Equation> equations;
protected
SBSet aux_s, sauxi, vc_domi, vc_domi_aux;
SBMultiInterval mi, mi_range, aux_mi;
array<SBInterval> inters;
array<Expression> ranges;
list<tuple<String, ComponentRef>> vars1, vars2;
list<Equation> eql;
list<Expression> inds, iter_expl;
algorithm
for auxi in UnorderedSet.toArray(SBSet.asets(dom)) loop
mi := SBAtomicSet.aset(auxi);
mi_range := applyOffset(mi, getOffset(mi, nmvTable));
inters := SBMultiInterval.intervals(mi_range);
ranges := Array.map(inters, intervalToRange);
sauxi := SBSet.newEmpty();
sauxi := SBSet.addAtomicSet(auxi, sauxi);
vars1 := getVars(potVars, sauxi, graph);
mi := SBAtomicSet.aset(aset);
aux_mi := applyOffset(mi, getOffset(mi, nmvTable));
inds := transMulti(mi_range, aux_mi, iterators, false);
eql := generatePotentialEquations2(vars1, vars, iterExps, inds);
equations := generateForLoop(eql, iterators, ranges, equations);
end for;
end generatePotentialEquations;
function generatePotentialEquations2
input list<tuple<String, ComponentRef>> vars1;
input list<tuple<String, ComponentRef>> vars2;
input list<Expression> inds1;
input list<Expression> inds2;
output list<Equation> equations = {};
protected
ComponentRef cr1, cr2;
Expression l, r;
Type ty;
Equation eq;
DAE.ElementSource src;
algorithm
for var1 in vars1 loop
(_, cr1) := var1;
for var2 in vars2 loop
(_, cr2) := var2;
if ComponentRef.firstName(cr1) == ComponentRef.firstName(cr2) then
l := generateConnector(cr1, inds1);
r := generateConnector(cr2, inds2);
ty := Expression.typeOf(l);
if Type.isArray(ty) then
eq := Equation.ARRAY_EQUALITY(l, r, ty, DAE.emptyElementSource);
else
eq := Equation.EQUALITY(l, r, ty, DAE.emptyElementSource);
end if;
equations := eq :: equations;
end if;
end for;
end for;
equations := listReverseInPlace(equations);
end generatePotentialEquations2;
function generateFlowEquation
input SBAtomicSet aset;
input SBSet dom;
input array<InstNode> iterators;
input list<Variable> flowVars;
input SBGraph graph;
input NameVertexTable.Table nmvTable;
input output list<Equation> equations;
protected
SBMultiInterval mi, mi_range, mi_range2;
SBSet sauxi;
array<SBInterval> inters;
array<Expression> ranges;
list<Expression> expl, inds;
Boolean is_sum;
list<tuple<String, ComponentRef>> vars;
Expression e, sum_exp;
Type ty;
Equation eq;
algorithm
mi := SBAtomicSet.aset(aset);
mi_range := applyOffset(mi, getOffset(mi, nmvTable));
inters := SBMultiInterval.intervals(mi_range);
ranges := Array.map(inters, intervalToRange);
expl := {};
for auxi in UnorderedSet.toArray(SBSet.asets(dom)) loop
mi := SBAtomicSet.aset(auxi);
mi_range2 := applyOffset(mi, getOffset(mi, nmvTable));
(inds, is_sum) := transMulti(mi_range, mi_range2, iterators, true);
sauxi := SBSet.newEmpty();
sauxi := SBSet.addAtomicSet(auxi, sauxi);
vars := getVars(flowVars, sauxi, graph);
for var in vars loop
e := generateConnector(Util.tuple22(var), inds);
if is_sum then
e := Expression.CALL(Call.makeTypedCall(NFBuiltinFuncs.SUM,
{e}, Expression.variability(e), Type.arrayElementType(Expression.typeOf(e))));
end if;
expl := e :: expl;
end for;
end for;
if not listEmpty(expl) then
sum_exp :: expl := expl;
while not listEmpty(expl) loop
e :: expl := expl;
sum_exp := Expression.BINARY(e, Operator.makeAdd(Expression.typeOf(e)), sum_exp);
end while;
ty := Expression.typeOf(sum_exp);
eq := Equation.EQUALITY(sum_exp, Expression.makeZero(ty), ty, DAE.emptyElementSource);
equations := generateForLoop({eq}, iterators, ranges, equations);
end if;
end generateFlowEquation;
function generateConnector
input ComponentRef cr;
input list<Expression> indices;
output Expression outExp;
protected
list<Subscript> subs;
algorithm
outExp := Expression.fromCref(cr);
if Type.isArray(Expression.typeOf(outExp)) then
subs := list(Subscript.fromTypedExp(i) for i in indices);
outExp := Expression.applySubscripts(subs, outExp);
end if;
end generateConnector;
function generateForLoop
input list<Equation> connects;
input array<InstNode> iterators;
input array<Expression> ranges;
input output list<Equation> equations;
protected
list<Equation> body = connects;
algorithm
for i in arrayLength(iterators):-1:1 loop
if Expression.isInteger(ranges[i]) then
// Scalar range means the interval had the same lower and upper bound,
// in which case the iterator can be replaced with the scalar expression
// instead of creating an unnecessary for loop here.
body := Equation.mapExpList(body,
function Expression.replaceIterator(iterator = iterators[i], iteratorValue = ranges[i]));
else
body := {Equation.FOR(iterators[i], SOME(ranges[i]), body, DAE.emptyElementSource)};
end if;
end for;
equations := List.append_reverse(body, equations);
end generateForLoop;
function getConnectors
input FlatModel flatModel;
output list<Variable> effVars = {};
output list<Variable> flowVars = {};
algorithm
for v in flatModel.variables loop
if Variable.isPotential(v) then
effVars := v :: effVars;
elseif Variable.isFlow(v) then
flowVars := v :: flowVars;
end if;
end for;
effVars := listReverseInPlace(effVars);
flowVars := listReverseInPlace(flowVars);
end getConnectors;
function getOffset
input SBMultiInterval mi;
input NameVertexTable.Table nmvTable;
output array<Integer> res;
protected
SBMultiInterval i, aux;
algorithm
res := listArray({});
// TODO: Surely this isn't the best way to do this.
for i in BaseHashTable.hashTableValueList(nmvTable) loop
aux := SBMultiInterval.intersection(mi, i);
if not SBMultiInterval.isEmpty(aux) then
res := SBMultiInterval.minElem(i);
end if;
end for;
end getOffset;
function applyOffset
input SBMultiInterval mi;
input array<Integer> off;
output SBMultiInterval outMI;
protected
array<SBInterval> ints, res;
SBInterval i;
Integer o;
algorithm
if SBMultiInterval.ndim(mi) <> arrayLength(off) or arrayEmpty(off) then
outMI := SBMultiInterval.newEmpty();
else
ints := SBMultiInterval.intervals(mi);
res := arrayCreateNoInit(arrayLength(ints), ints[1]);
for j in 1:arrayLength(ints) loop
i := ints[j];
o := off[j];
res[j] := SBInterval.new(SBInterval.lowerBound(i) - o + 1,
SBInterval.stepValue(i),
SBInterval.upperBound(i) - o + 1);
end for;
outMI := SBMultiInterval.fromArray(res);
end if;
end applyOffset;
function getVars
input list<Variable> vars;
input SBSet sauxi;
input SBGraph graph;
output list<tuple<String, ComponentRef>> res = {};
protected
list<SetVertex> vl;
algorithm
vl := AdjacencyList.vertices(graph);
for v in vl loop
if not SBSet.isEmpty(SBSet.intersection(v.vs, sauxi)) then
for var in vars loop
if ComponentRef.isPrefix(v.name, var.name) then
res := (ComponentRef.toString(v.name), var.name) :: res;
end if;
end for;
end if;
end for;
res := listReverseInPlace(res);
end getVars;
function transMulti
input SBMultiInterval mi1;
input SBMultiInterval mi2;
input array<InstNode> iterators;
input Boolean forFlow;
output list<Expression> outExpl = {};
output Boolean flowRange = false;
protected
array<SBInterval> ints1, ints2;
SBInterval i1, i2;
Integer i1_sz, i2_sz, m_int, h_int;
Expression x, m, h, e;
algorithm
if SBMultiInterval.ndim(mi1) <> SBMultiInterval.ndim(mi2) then
return;
end if;
ints1 := SBMultiInterval.intervals(mi1);
ints2 := SBMultiInterval.intervals(mi2);
for i in 1:arrayLength(ints1) loop
i1 := ints1[i];
i2 := ints2[i];