/
FindZeroCrossings.mo
1442 lines (1327 loc) · 92.9 KB
/
FindZeroCrossings.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 FindZeroCrossings
" file: FindZeroCrossings.mo
package: FindZeroCrossings
description: This package contains all the functions to find zero crossings
inside BackendDAE.
RCS: $Id$
"
public import Absyn;
public import BackendDAE;
public import DAE;
public import Env;
protected import BackendDAECreate;
protected import BackendDAEUtil;
protected import BackendDump;
protected import BackendEquation;
protected import BackendVariable;
protected import ComponentReference;
protected import DAEDump;
protected import Debug;
protected import Error;
protected import Expression;
protected import ExpressionDump;
protected import Flags;
protected import List;
protected import Util;
// =============================================================================
// section for some public util functions
//
// =============================================================================
public function getZeroCrossings
input BackendDAE.BackendDAE inBackendDAE;
output list<BackendDAE.ZeroCrossing> outZeroCrossingList;
algorithm
BackendDAE.DAE(shared=BackendDAE.SHARED(eventInfo=BackendDAE.EVENT_INFO(zeroCrossingLst=outZeroCrossingList))) := inBackendDAE;
end getZeroCrossings;
public function getRelations
input BackendDAE.BackendDAE inBackendDAE;
output list<BackendDAE.ZeroCrossing> outZeroCrossingList;
algorithm
BackendDAE.DAE(shared=BackendDAE.SHARED(eventInfo=BackendDAE.EVENT_INFO(relationsLst=outZeroCrossingList))) := inBackendDAE;
end getRelations;
// =============================================================================
// section for zero crossings
//
// This section contains all the functions to find zero crossings inside
// BackendDAE.
// =============================================================================
public function findZeroCrossings "This function finds all zero crossings in the list of equations and
the list of when clauses."
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
protected
BackendDAE.BackendDAE dae;
list<BackendDAE.Var> vars;
algorithm
(dae, vars) := BackendDAEUtil.mapEqSystemAndFold(inDAE, findZeroCrossings1, {});
outDAE := findZeroCrossingsShared(dae, vars);
end findZeroCrossings;
protected function findZeroCrossingsShared "This function finds all zerocrossings in the shared part of the DAE."
input BackendDAE.BackendDAE inDAE;
input list<BackendDAE.Var> allvars;
output BackendDAE.BackendDAE outDAE;
protected
BackendDAE.Variables vars, knvars, exobj, av;
BackendDAE.EquationArray remeqns, inieqns;
list<DAE.Constraint> constrs;
list<DAE.ClassAttributes> clsAttrs;
BackendDAE.EventInfo einfo, einfo1;
BackendDAE.ExternalObjectClasses eoc;
list<BackendDAE.TimeEvent> timeEvents;
list<BackendDAE.WhenClause> whenclauses;
list<BackendDAE.Equation> eqs_lst, eqs_lst1;
list<BackendDAE.ZeroCrossing> zero_crossings;
list<BackendDAE.ZeroCrossing> relationsLst, sampleLst;
Integer countRelations, countMathFunctions;
BackendDAE.BackendDAEType btp;
DAE.FunctionTree funcs;
BackendDAE.SymbolicJacobians symjacs;
Env.Cache cache;
Env.Env env;
BackendDAE.EqSystems systs;
BackendDAE.ExtraInfo ei;
algorithm
BackendDAE.DAE(systs, BackendDAE.SHARED(knvars, exobj, av, inieqns, remeqns, constrs, clsAttrs,
cache, env, funcs, einfo as BackendDAE.EVENT_INFO(timeEvents=timeEvents, zeroCrossingLst=zero_crossings, relationsLst=relationsLst,
sampleLst=sampleLst, whenClauseLst=whenclauses, relationsNumber=countRelations,
numberMathEvents=countMathFunctions), eoc, btp, symjacs, ei)) := inDAE;
vars := BackendVariable.listVar1(allvars);
eqs_lst := BackendEquation.equationList(remeqns);
(zero_crossings, eqs_lst1, whenclauses, countRelations, countMathFunctions, relationsLst, sampleLst) := findZeroCrossings2(vars, knvars, eqs_lst, 0, whenclauses, 0, countRelations, countMathFunctions, zero_crossings, relationsLst, sampleLst, {}, {});
eqs_lst1 := listReverse(eqs_lst1);
remeqns := BackendEquation.listEquation(eqs_lst1);
eqs_lst := BackendEquation.equationList(inieqns);
(zero_crossings, eqs_lst1, _, countRelations, countMathFunctions, relationsLst, sampleLst) := findZeroCrossings2(vars, knvars, eqs_lst, 0, {}, 0, countRelations, countMathFunctions, zero_crossings, relationsLst, sampleLst, {}, {});
eqs_lst1 := listReverse(eqs_lst1);
inieqns := BackendEquation.listEquation(eqs_lst1);
Debug.fcall(Flags.RELIDX, print, "findZeroCrossings1 sample index: " +& intString(listLength(sampleLst)) +& "\n");
einfo1 := BackendDAE.EVENT_INFO(timeEvents, whenclauses, zero_crossings, sampleLst, relationsLst, countRelations, countMathFunctions);
outDAE := BackendDAE.DAE(systs, BackendDAE.SHARED(knvars, exobj, av, inieqns, remeqns, constrs, clsAttrs, cache, env, funcs, einfo1, eoc, btp, symjacs, ei));
end findZeroCrossingsShared;
protected function findZeroCrossings1 "This function finds all zerocrossings in the list of equations and
the list of when clauses."
input BackendDAE.EqSystem syst;
input tuple<BackendDAE.Shared, list<BackendDAE.Var>> shared;
output BackendDAE.EqSystem osyst;
output tuple<BackendDAE.Shared, list<BackendDAE.Var>> oshared;
protected
list<BackendDAE.Var> allvars;
BackendDAE.Variables vars, knvars, exobj, av;
BackendDAE.EquationArray eqns, remeqns, inieqns, eqns1;
list<DAE.Constraint> constrs;
list<DAE.ClassAttributes> clsAttrs;
BackendDAE.EventInfo einfo, einfo1;
BackendDAE.ExternalObjectClasses eoc;
list<BackendDAE.WhenClause> whenclauses;
list<BackendDAE.Equation> eqs_lst, eqs_lst1;
list<BackendDAE.TimeEvent> timeEvents;
list<BackendDAE.ZeroCrossing> zero_crossings;
list<BackendDAE.ZeroCrossing> relations, sampleLst;
Integer countRelations;
Integer countMathFunctions;
Option<BackendDAE.IncidenceMatrix> m, mT;
BackendDAE.BackendDAEType btp;
BackendDAE.Matching matching;
DAE.FunctionTree funcs;
BackendDAE.SymbolicJacobians symjacs;
Env.Cache cache;
Env.Env env;
BackendDAE.StateSets stateSets;
BackendDAE.ExtraInfo ei;
algorithm
BackendDAE.EQSYSTEM(vars, eqns, m, mT, matching, stateSets) := syst;
(BackendDAE.SHARED(knvars, exobj, av, inieqns, remeqns, constrs, clsAttrs,
cache, env, funcs, einfo as BackendDAE.EVENT_INFO(timeEvents=timeEvents, zeroCrossingLst=zero_crossings,
sampleLst=sampleLst, whenClauseLst=whenclauses, relationsLst=relations,
relationsNumber=countRelations, numberMathEvents=countMathFunctions),
eoc, btp, symjacs, ei), allvars) := shared;
eqs_lst := BackendEquation.equationList(eqns);
(zero_crossings, eqs_lst1, _, countRelations, countMathFunctions, relations, sampleLst) := findZeroCrossings2(vars, knvars, eqs_lst, 0, {}, 0, countRelations, countMathFunctions, zero_crossings, relations, sampleLst, {}, {});
eqs_lst1 := listReverse(eqs_lst1);
Debug.fcall(Flags.RELIDX, print, "findZeroCrossings1 number of relations : " +& intString(countRelations) +& "\n");
Debug.fcall(Flags.RELIDX, print, "findZeroCrossings1 sample index: " +& intString(listLength(sampleLst)) +& "\n");
eqns1 := BackendEquation.listEquation(eqs_lst1);
einfo1 := BackendDAE.EVENT_INFO(timeEvents, whenclauses, zero_crossings, sampleLst, relations, countRelations, countMathFunctions);
allvars := listAppend(allvars, BackendVariable.varList(vars));
osyst := BackendDAE.EQSYSTEM(vars, eqns1, m, mT, matching, stateSets);
oshared := (BackendDAE.SHARED(knvars, exobj, av, inieqns, remeqns, constrs, clsAttrs, cache, env, funcs, einfo1, eoc, btp, symjacs, ei), allvars);
end findZeroCrossings1;
protected function findZeroCrossings2 "
Helper function to find_zero_crossing.
modified: 2011-01 by wbraun
"
input BackendDAE.Variables inVariables1;
input BackendDAE.Variables knvars;
input list<BackendDAE.Equation> inEquationLst2;
input Integer inEqnCount;
input list<BackendDAE.WhenClause> inWhenClauseLst4;
input Integer inWhenClauseCount;
input Integer inNumberOfRelations;
input Integer inNumberOfMathFunctions;
input list<BackendDAE.ZeroCrossing> inZeroCrossingLst;
input list<BackendDAE.ZeroCrossing> inRelationsLst;
input list<BackendDAE.ZeroCrossing> inSamplesLst;
input list<BackendDAE.Equation> inEquationLstAccum;
input list<BackendDAE.WhenClause> inWhenClauseAccum;
output list<BackendDAE.ZeroCrossing> outZeroCrossingLst;
output list<BackendDAE.Equation> outEquationLst;
output list<BackendDAE.WhenClause> outWhenClauseLst;
output Integer outNumberOfRelations;
output Integer outNumberOfMathFunctions;
output list<BackendDAE.ZeroCrossing> outRelationsLst;
output list<BackendDAE.ZeroCrossing> outSamplesLst;
algorithm
(outZeroCrossingLst, outEquationLst, outWhenClauseLst, outNumberOfRelations, outNumberOfMathFunctions, outRelationsLst, outSamplesLst) :=
match (inVariables1, knvars, inEquationLst2, inEqnCount, inWhenClauseLst4, inWhenClauseCount, inNumberOfRelations, inNumberOfMathFunctions, inZeroCrossingLst, inRelationsLst, inSamplesLst, inEquationLstAccum, inWhenClauseAccum)
local
BackendDAE.Variables v;
list<BackendDAE.ZeroCrossing> zcs, zcs1, res, res1, relationsLst, sampleLst;
Integer size, countRelations, eq_count_1, eq_count, wc_count, countMathFunctions;
BackendDAE.Equation e;
list<BackendDAE.Equation> xs, el, eq_reslst, eqnsAccum;
DAE.Exp daeExp, e1, e2, eres1, eres2;
BackendDAE.WhenClause wc;
list<BackendDAE.WhenClause> xsWhen, wc_reslst;
DAE.ElementSource source, source_;
list<DAE.Statement> stmts, stmts_1;
DAE.ComponentRef cref;
list<BackendDAE.WhenOperator> whenOperations;
list<BackendDAE.WhenClause> whenClauseAccum;
Option<Integer> elseClause_;
list<Integer> dimsize;
BackendDAE.WhenEquation weqn;
Boolean diffed;
DAE.Expand expand;
case (_, _, {}, _, {}, _, countRelations, countMathFunctions, res, relationsLst, sampleLst, _, _) then (res, inEquationLstAccum, inWhenClauseAccum, countRelations, countMathFunctions, relationsLst, sampleLst);
// all algorithm stmts are processed firstly
case (v, _, ((e as BackendDAE.ALGORITHM(size=size, alg=DAE.ALGORITHM_STMTS(stmts), source= source_, expand=expand))::xs), eq_count, {}, _, countRelations, countMathFunctions, zcs, relationsLst, sampleLst, eqnsAccum, whenClauseAccum)
equation
eq_count = eq_count + 1;
((stmts_1, (_, _, _, (res, relationsLst, sampleLst, countRelations, countMathFunctions), (_, _, _)))) = traverseStmtsExps(stmts, collectZCAlgs, (DAE.RCONST(0.0), {}, DAE.RCONST(0.0), (zcs, relationsLst, sampleLst, countRelations, countMathFunctions), (eq_count, v, knvars)), knvars);
eqnsAccum = listAppend({BackendDAE.ALGORITHM(size, DAE.ALGORITHM_STMTS(stmts_1), source_, expand)}, eqnsAccum);
(res1, eq_reslst, wc_reslst, countRelations, countMathFunctions, relationsLst, sampleLst) = findZeroCrossings2(v, knvars, xs, eq_count, {}, 0, countRelations, countMathFunctions, res, relationsLst, sampleLst, eqnsAccum, whenClauseAccum);
then
(res1, eq_reslst, wc_reslst, countRelations, countMathFunctions, relationsLst, sampleLst);
// then all when clauses are processed
case (v, _, el, eq_count, ((wc as BackendDAE.WHEN_CLAUSE(condition = daeExp, reinitStmtLst=whenOperations , elseClause = elseClause_ ))::xsWhen), wc_count, countRelations, countMathFunctions, zcs, relationsLst, sampleLst, eqnsAccum, whenClauseAccum)
equation
Debug.fcall(Flags.RELIDX, BackendDump.debugStrExpStr, ("processed when clause: ", daeExp, "\n"));
wc_count = wc_count + 1;
(eres1, countRelations, countMathFunctions, res, relationsLst, sampleLst) = findZeroCrossings3(daeExp, zcs, relationsLst, sampleLst, countRelations, countMathFunctions, -1, wc_count, v, knvars);
whenClauseAccum = listAppend({BackendDAE.WHEN_CLAUSE(eres1, whenOperations, elseClause_)}, whenClauseAccum);
(res1, eq_reslst, wc_reslst, countRelations, countMathFunctions, relationsLst, sampleLst) = findZeroCrossings2(v, knvars, el, eq_count, xsWhen, wc_count, countRelations, countMathFunctions, res, relationsLst, sampleLst, eqnsAccum, whenClauseAccum);
then
(res1, eq_reslst, wc_reslst, countRelations, countMathFunctions, relationsLst, sampleLst);
// check when equation condition
case (v, _, ((e as BackendDAE.WHEN_EQUATION(size=size, whenEquation=weqn, source= source_))::xs), eq_count, {}, wc_count, countRelations, countMathFunctions, zcs, relationsLst, sampleLst, eqnsAccum, whenClauseAccum)
equation
eq_count = eq_count + 1;
(weqn, countRelations, countMathFunctions, res, relationsLst, sampleLst) = findZeroCrossingsWhenEqns(weqn, zcs, relationsLst, sampleLst, countRelations, countMathFunctions, eq_count, -1, v, knvars);
eqnsAccum = listAppend({BackendDAE.WHEN_EQUATION(size, weqn, source_)}, eqnsAccum);
(res1, eq_reslst, wc_reslst, countRelations, countMathFunctions, relationsLst, sampleLst) = findZeroCrossings2(v, knvars, xs, eq_count, {}, 0, countRelations, countMathFunctions, res, relationsLst, sampleLst, eqnsAccum, whenClauseAccum);
then
(res1, eq_reslst, wc_reslst, countRelations, countMathFunctions, relationsLst, sampleLst);
// after all algorithms and when clauses are processed, all equations are processed
case (v, _, ((e as BackendDAE.EQUATION(exp = e1, scalar = e2, source= source_, differentiated=diffed))::xs), eq_count, {}, _, countRelations, countMathFunctions, zcs, relationsLst, sampleLst, eqnsAccum, whenClauseAccum)
equation
eq_count = eq_count + 1;
(eres1, countRelations, countMathFunctions, zcs1, relationsLst, sampleLst) = findZeroCrossings3(e1, zcs, relationsLst, sampleLst, countRelations, countMathFunctions, eq_count, -1, v, knvars);
(eres2, countRelations, countMathFunctions, res, relationsLst, sampleLst) = findZeroCrossings3(e2, zcs1, relationsLst, sampleLst, countRelations, countMathFunctions, eq_count, -1, v, knvars);
eqnsAccum = listAppend({BackendDAE.EQUATION(eres1, eres2, source_, diffed)}, eqnsAccum);
(res1, eq_reslst, wc_reslst, countRelations, countMathFunctions, relationsLst, sampleLst) = findZeroCrossings2(v, knvars, xs, eq_count, {}, 0, countRelations, countMathFunctions, res, relationsLst, sampleLst, eqnsAccum, whenClauseAccum);
then
(res1, eq_reslst, wc_reslst, countRelations, countMathFunctions, relationsLst, sampleLst);
case (v, _, ((e as BackendDAE.COMPLEX_EQUATION(size=size, left=e1, right=e2, source=source, differentiated=diffed))::xs), eq_count, {}, _, countRelations, countMathFunctions, zcs, relationsLst, sampleLst, eqnsAccum, whenClauseAccum)
equation
eq_count = eq_count + 1;
(eres1, countRelations, countMathFunctions, zcs1, relationsLst, sampleLst) = findZeroCrossings3(e1, zcs, relationsLst, sampleLst, countRelations, countMathFunctions, eq_count, -1, v, knvars);
(eres2, countRelations, countMathFunctions, res, relationsLst, sampleLst) = findZeroCrossings3(e2, zcs1, relationsLst, sampleLst, countRelations, countMathFunctions, eq_count, -1, v, knvars);
eqnsAccum = listAppend({BackendDAE.COMPLEX_EQUATION(size, eres1, eres2, source, diffed)}, eqnsAccum);
(res1, eq_reslst, wc_reslst, countRelations, countMathFunctions, relationsLst, sampleLst) = findZeroCrossings2(v, knvars, xs, eq_count, {}, 0, countRelations, countMathFunctions, res, relationsLst, sampleLst, eqnsAccum, whenClauseAccum);
then
(res1, eq_reslst, wc_reslst, countRelations, countMathFunctions, relationsLst, sampleLst);
case (v, _, ((e as BackendDAE.ARRAY_EQUATION(dimSize=dimsize, left=e1, right=e2, source=source, differentiated=diffed))::xs), eq_count, {}, _, countRelations, countMathFunctions, zcs, relationsLst, sampleLst, eqnsAccum, whenClauseAccum)
equation
eq_count = eq_count + 1;
(eres1, countRelations, countMathFunctions, zcs1, relationsLst, sampleLst) = findZeroCrossings3(e1, zcs, relationsLst, sampleLst, countRelations, countMathFunctions, eq_count, -1, v, knvars);
(eres2, countRelations, countMathFunctions, res, relationsLst, sampleLst) = findZeroCrossings3(e2, zcs1, relationsLst, sampleLst, countRelations, countMathFunctions, eq_count, -1, v, knvars);
eqnsAccum = listAppend({BackendDAE.ARRAY_EQUATION(dimsize, eres1, eres2, source, diffed)}, eqnsAccum);
(res1, eq_reslst, wc_reslst, countRelations, countMathFunctions, relationsLst, sampleLst) = findZeroCrossings2(v, knvars, xs, eq_count, {}, 0, countRelations, countMathFunctions, res, relationsLst, sampleLst, eqnsAccum, whenClauseAccum);
then
(res1, eq_reslst, wc_reslst, countRelations, countMathFunctions, relationsLst, sampleLst);
case (v, _, ((e as BackendDAE.SOLVED_EQUATION(componentRef = cref, exp = e1, source= source_, differentiated=diffed))::xs), eq_count, {}, _, countRelations, countMathFunctions, zcs, relationsLst, sampleLst, eqnsAccum, whenClauseAccum)
equation
(eres1, countRelations, countMathFunctions, res, relationsLst, sampleLst) = findZeroCrossings3(e1, zcs, relationsLst, sampleLst, countRelations, countMathFunctions, eq_count, -1, v, knvars);
eqnsAccum = listAppend({BackendDAE.SOLVED_EQUATION(cref, eres1, source_, diffed)}, eqnsAccum);
(res1, eq_reslst, wc_reslst, countRelations, countMathFunctions, relationsLst, sampleLst) = findZeroCrossings2(v, knvars, xs, eq_count, {}, 0, countRelations, countMathFunctions, res, relationsLst, sampleLst, eqnsAccum, whenClauseAccum);
then
(res1, eq_reslst, wc_reslst, countRelations, countMathFunctions, relationsLst, sampleLst);
case (v, _, ((e as BackendDAE.RESIDUAL_EQUATION(exp = e1, source= source_, differentiated=diffed))::xs), eq_count, {}, _, countRelations, countMathFunctions, zcs, relationsLst, sampleLst, eqnsAccum, whenClauseAccum)
equation
eq_count = eq_count + 1;
(eres1, countRelations, countMathFunctions, relationsLst, res, sampleLst) = findZeroCrossings3(e1, zcs, relationsLst, sampleLst, countRelations, countMathFunctions, eq_count, -1, v, knvars);
eqnsAccum = listAppend({BackendDAE.RESIDUAL_EQUATION(eres1, source_, diffed)}, eqnsAccum);
(res1, eq_reslst, wc_reslst, countRelations, countMathFunctions, relationsLst, sampleLst) = findZeroCrossings2(v, knvars, xs, eq_count, {}, 0, countRelations, countMathFunctions, res, relationsLst, sampleLst, eqnsAccum, whenClauseAccum);
then
(res1, eq_reslst, wc_reslst, countRelations, countMathFunctions, relationsLst, sampleLst);
case (v, _, ((e as BackendDAE.IF_EQUATION(conditions=_))::xs), eq_count, {}, _, countRelations, countMathFunctions, zcs, relationsLst, sampleLst, eqnsAccum, whenClauseAccum)
equation
eq_count = eq_count + 1;
(e, countRelations, countMathFunctions, res, relationsLst, sampleLst) = findZeroCrossingsIfEqns(e, zcs, relationsLst, sampleLst, countRelations, countMathFunctions, eq_count, -1, v, knvars);
eqnsAccum = listAppend({e}, eqnsAccum);
(res1, eq_reslst, wc_reslst, countRelations, countMathFunctions, relationsLst, sampleLst) = findZeroCrossings2(v, knvars, xs, eq_count, {}, 0, countRelations, countMathFunctions, res, relationsLst, sampleLst, eqnsAccum, whenClauseAccum);
then
(res1, eq_reslst, wc_reslst, countRelations, countMathFunctions, relationsLst, sampleLst);
// let when equation pass they are discrete and can't contain ZeroCrossings
case (v, _, (e::xs), eq_count, {}, _, countRelations, countMathFunctions, res, relationsLst, sampleLst, eqnsAccum, whenClauseAccum)
equation
eq_count = eq_count + 1;
eqnsAccum = listAppend({e}, eqnsAccum);
(res1, eq_reslst, wc_reslst, countRelations, countMathFunctions, relationsLst, sampleLst) = findZeroCrossings2(v, knvars, xs, eq_count, {}, 0, countRelations, countMathFunctions, res, relationsLst, sampleLst, eqnsAccum, whenClauseAccum);
then
(res1, eq_reslst, wc_reslst, countRelations, countMathFunctions, relationsLst, sampleLst);
end match;
end findZeroCrossings2;
protected function findZeroCrossingsWhenEqns
"Helper function to findZeroCrossing."
input BackendDAE.WhenEquation inWhenEqn;
input list<BackendDAE.ZeroCrossing> inZeroCrossings;
input list<BackendDAE.ZeroCrossing> inrelationsinZC;
input list<BackendDAE.ZeroCrossing> inSamplesLst;
input Integer incountRelations;
input Integer incountMathFunctions;
input Integer counteq;
input Integer countwc;
input BackendDAE.Variables vars;
input BackendDAE.Variables knvars;
output BackendDAE.WhenEquation oWhenEqn;
output Integer outcountRelations;
output Integer outcountMathFunctions;
output list<BackendDAE.ZeroCrossing> outZeroCrossings;
output list<BackendDAE.ZeroCrossing> outrelationsinZC;
output list<BackendDAE.ZeroCrossing> outSamplesLst;
algorithm
(oWhenEqn, outcountRelations, outcountMathFunctions, outZeroCrossings, outrelationsinZC, outSamplesLst) :=
match(inWhenEqn, inZeroCrossings, inrelationsinZC, inSamplesLst, incountRelations, incountMathFunctions, counteq, countwc, vars, knvars)
local
DAE.Exp cond, e;
DAE.ComponentRef cr;
BackendDAE.WhenEquation we;
list<BackendDAE.ZeroCrossing> zc, relations, samples;
Integer countRelations, countMathFunctions;
case (BackendDAE.WHEN_EQ(condition=cond, left=cr, right=e, elsewhenPart=NONE()), _, _, _, _, _, _, _, _, _)
equation
Debug.fcall(Flags.RELIDX, BackendDump.debugStrExpStr, ("processed when condition: ", cond, "\n"));
(cond, countRelations, countMathFunctions, zc, relations, samples) = findZeroCrossings3(cond, inZeroCrossings, inrelationsinZC, inSamplesLst, incountRelations, incountMathFunctions, counteq, countwc, vars, knvars);
then
(BackendDAE.WHEN_EQ(cond, cr, e, NONE()), countRelations, countMathFunctions, zc, relations, samples);
case (BackendDAE.WHEN_EQ(condition=cond, left=cr, right=e, elsewhenPart=SOME(we)), _, _, _, _, _, _, _, _, _)
equation
(we, countRelations, countMathFunctions, zc, relations, samples) = findZeroCrossingsWhenEqns(we, inZeroCrossings, inrelationsinZC, inSamplesLst, incountRelations, incountMathFunctions, counteq, countwc, vars, knvars);
(cond, countRelations, countMathFunctions, zc, relations, samples) = findZeroCrossings3(cond, zc, relations, samples, countRelations, countMathFunctions, counteq, countwc, vars, knvars);
then
(BackendDAE.WHEN_EQ(cond, cr, e, SOME(we)), countRelations, countMathFunctions, zc, relations, samples);
end match;
end findZeroCrossingsWhenEqns;
protected function findZeroCrossingsIfEqns
"Helper function to findZeroCrossing."
input BackendDAE.Equation inIfEqn;
input list<BackendDAE.ZeroCrossing> inZeroCrossings;
input list<BackendDAE.ZeroCrossing> inrelationsinZC;
input list<BackendDAE.ZeroCrossing> inSamplesLst;
input Integer incountRelations;
input Integer incountMathFunctions;
input Integer counteq;
input Integer countwc;
input BackendDAE.Variables vars;
input BackendDAE.Variables knvars;
output BackendDAE.Equation oIfEqn;
output Integer outcountRelations;
output Integer outcountMathFunctions;
output list<BackendDAE.ZeroCrossing> outZeroCrossings;
output list<BackendDAE.ZeroCrossing> outrelationsinZC;
output list<BackendDAE.ZeroCrossing> outSamplesLst;
algorithm
(oIfEqn, outcountRelations, outcountMathFunctions, outZeroCrossings, outrelationsinZC, outSamplesLst) :=
match(inIfEqn, inZeroCrossings, inrelationsinZC, inSamplesLst, incountRelations, incountMathFunctions, counteq, countwc, vars, knvars)
local
DAE.Exp condition;
list<DAE.Exp> conditions, restconditions;
BackendDAE.Equation ifeqn;
list<BackendDAE.Equation> eqnstrue, elseeqns, eqnsAccum;
list<BackendDAE.WhenClause> whenClauseAccum;
list<list<BackendDAE.Equation>> eqnsTrueLst, resteqns;
list<BackendDAE.ZeroCrossing> zc, relations, samples;
Integer countRelations, countMathFunctions;
DAE.ElementSource source_;
case (BackendDAE.IF_EQUATION(conditions={}, eqnstrue={}, eqnsfalse=elseeqns, source=source_), _, _, _, _, _, _, _, _, _)
equation
(zc, elseeqns, _, countRelations, countMathFunctions, relations, samples) = findZeroCrossings2(vars, knvars, elseeqns, counteq, {}, countwc, incountRelations, incountMathFunctions, inZeroCrossings, inrelationsinZC, inSamplesLst, {}, {});
elseeqns = listReverse(elseeqns);
then
(BackendDAE.IF_EQUATION({}, {}, elseeqns, source_), countRelations, countMathFunctions, zc, relations, samples);
case (BackendDAE.IF_EQUATION(conditions=condition::restconditions, eqnstrue=eqnstrue::resteqns, eqnsfalse=elseeqns, source=source_), _, _, _, _, _, _, _, _, _)
equation
(condition, countRelations, countMathFunctions, zc, relations, samples) = findZeroCrossings3(condition, inZeroCrossings, inrelationsinZC, inSamplesLst, incountRelations, incountMathFunctions, counteq, countwc, vars, knvars);
(zc, eqnstrue, _, countRelations, countMathFunctions, relations, samples) = findZeroCrossings2(vars, knvars, eqnstrue, counteq, {}, countwc, countRelations, countMathFunctions, zc, relations, samples, {}, {});
eqnstrue = listReverse(eqnstrue);
ifeqn = BackendDAE.IF_EQUATION(restconditions, resteqns, elseeqns, source_);
(BackendDAE.IF_EQUATION(conditions=conditions, eqnstrue=eqnsTrueLst, eqnsfalse=elseeqns, source=source_), countRelations, countMathFunctions, zc, relations, samples) = findZeroCrossingsIfEqns(ifeqn, zc, relations, samples, countRelations, countMathFunctions, counteq, countwc, vars, knvars);
conditions = listAppend({condition}, conditions);
eqnsTrueLst = listAppend({eqnstrue}, eqnsTrueLst);
then
(BackendDAE.IF_EQUATION(conditions, eqnsTrueLst, elseeqns, source_), countRelations, countMathFunctions, zc, relations, samples);
end match;
end findZeroCrossingsIfEqns;
protected function findZeroCrossings3
"Helper function to findZeroCrossing."
input DAE.Exp e;
input list<BackendDAE.ZeroCrossing> inZeroCrossings;
input list<BackendDAE.ZeroCrossing> inrelationsinZC;
input list<BackendDAE.ZeroCrossing> inSamplesLst;
input Integer incountRelations;
input Integer incountMathFunctions;
input Integer counteq;
input Integer countwc;
input BackendDAE.Variables vars;
input BackendDAE.Variables knvars;
output DAE.Exp eres;
output Integer outcountRelations;
output Integer outcountMathFunctions;
output list<BackendDAE.ZeroCrossing> outZeroCrossings;
output list<BackendDAE.ZeroCrossing> outrelationsinZC;
output list<BackendDAE.ZeroCrossing> outSamplesLst;
algorithm
Debug.fcall(Flags.RELIDX, BackendDump.debugStrExpStr, ("start: ", e, "\n"));
((eres, ((outZeroCrossings, outrelationsinZC, outSamplesLst, outcountRelations, outcountMathFunctions), (_, _, _, _))))
:= Expression.traverseExpTopDown(e, collectZC, ((inZeroCrossings, inrelationsinZC, inSamplesLst, incountRelations, incountMathFunctions), (counteq, countwc, vars, knvars)));
end findZeroCrossings3;
protected function collectZC "author: unknown
modified: 2011-01 by wbraun
Collects zero crossings in equations"
input tuple<DAE.Exp, tuple<tuple<list<BackendDAE.ZeroCrossing>, list<BackendDAE.ZeroCrossing>, list<BackendDAE.ZeroCrossing>, Integer, Integer>, tuple<Integer, Integer, BackendDAE.Variables, BackendDAE.Variables>>> inTplExpExpTplExpExpLstVariables;
output tuple<DAE.Exp, Boolean, tuple<tuple<list<BackendDAE.ZeroCrossing>, list<BackendDAE.ZeroCrossing>, list<BackendDAE.ZeroCrossing>, Integer, Integer>, tuple<Integer, Integer, BackendDAE.Variables, BackendDAE.Variables>>> outTplExpExpTplExpExpLstVariables;
algorithm
outTplExpExpTplExpExpLstVariables:=
matchcontinue (inTplExpExpTplExpExpLstVariables)
local
DAE.Exp e, e1, e2, e_1, e_2, eres, eres1;
BackendDAE.Variables vars, knvars;
list<BackendDAE.ZeroCrossing> zeroCrossings, zc_lst, relations, samples;
DAE.Operator op;
Integer eq_count, wc_count, itmp, numRelations, numRelations1, numMathFunctions;
BackendDAE.ZeroCrossing zc;
DAE.CallAttributes attr;
DAE.Type ty;
case (((e as DAE.CALL(path = Absyn.IDENT(name = "noEvent"))), ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))))
then ((e, false, ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))));
case (((e as DAE.CALL(path = Absyn.IDENT(name = "smooth"))), ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))))
then ((e, false, ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))));
case (((e as DAE.CALL(path = Absyn.IDENT(name = "sample"))), ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))))
equation
zc_lst = makeZeroCrossings({e}, {eq_count}, {wc_count});
samples = listAppend(samples, zc_lst);
samples = mergeZeroCrossings(samples);
//itmp = (listLength(zc_lst)-listLength(zeroCrossings));
//indx = indx + (listLength(zc_lst) - listLength(zeroCrossings));
Debug.fcall(Flags.RELIDX, print, "sample index: " +& intString(listLength(samples)) +& "\n");
then ((e, true, ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))));
// function with discrete expressions generate no zerocrossing
case (((e as DAE.LUNARY(operator = op, exp = e1)), ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))))
equation
true = BackendDAEUtil.isDiscreteExp(e1, vars, knvars);
Debug.fcall(Flags.RELIDX, print, "discrete LUNARY: " +& intString(numRelations) +& "\n");
//Debug.fcall(Flags.RELIDX, BackendDump.debugExpStr, (e, "\n"));
then
((e, true, ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))));
case (((e as DAE.LBINARY(exp1 = e1, operator = op, exp2 = e2)), ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))))
equation
true = BackendDAEUtil.isDiscreteExp(e1, vars, knvars);
true = BackendDAEUtil.isDiscreteExp(e2, vars, knvars);
Debug.fcall(Flags.RELIDX, print, "discrete LBINARY: " +& intString(numRelations) +& "\n");
//Debug.fcall(Flags.RELIDX, BackendDump.debugExpStr, (e, "\n"));
then
((e, true, ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))));
// coditions that are zerocrossings.
case (((e as DAE.LUNARY(exp = e1, operator = op)), ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))))
equation
Debug.fcall(Flags.RELIDX, print, "continues LUNARY: " +& intString(numRelations) +& "\n");
((e1, ((_, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars)))) = Expression.traverseExpTopDown(e1, collectZC, ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars)));
e_1 = DAE.LUNARY(op, e1);
{zc} = makeZeroCrossings({e_1}, {eq_count}, {wc_count});
zc_lst = List.select1(zeroCrossings, sameZeroCrossing, zc);
zeroCrossings = Util.if_(List.isEmpty(zc_lst), listAppend(zeroCrossings, {zc}), zeroCrossings);
Debug.fcall(Flags.RELIDX, BackendDump.debugExpStr, (e_1, "\n"));
then
((e, false, ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))));
case (((e as DAE.LBINARY(exp1 = e1, operator = op, exp2 = e2)), ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))))
equation
Debug.fcall(Flags.RELIDX, print, "continues LBINARY: " +& intString(numRelations) +& "\n");
Debug.fcall(Flags.RELIDX, BackendDump.debugExpStr, (e, "\n"));
((e_1, ((_, relations, samples, numRelations1, numMathFunctions), (eq_count, wc_count, vars, knvars)))) = Expression.traverseExpTopDown(e1, collectZC, ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars)));
((e_2, ((_, relations, samples, numRelations1, numMathFunctions), (eq_count, wc_count, vars, knvars)))) = Expression.traverseExpTopDown(e2, collectZC, ((zeroCrossings, relations, samples, numRelations1, numMathFunctions), (eq_count, wc_count, vars, knvars)));
true = intGt(numRelations1, numRelations);
e_1 = DAE.LBINARY(e_1, op, e_2);
{zc} = makeZeroCrossings({e_1}, {eq_count}, {wc_count});
zc_lst = List.select1(zeroCrossings, sameZeroCrossing, zc);
zeroCrossings = Util.if_(List.isEmpty(zc_lst), listAppend(zeroCrossings, {zc}), zeroCrossings);
print(Debug.fcallret1(Flags.RELIDX, BackendDump.zeroCrossingListString, zeroCrossings, ""));
then
((e_1, false, ((zeroCrossings, relations, samples, numRelations1, numMathFunctions), (eq_count, wc_count, vars, knvars))));
// function with discrete expressions generate no zerocrossing
case (((e as DAE.RELATION(exp1 = e1, operator = op, exp2 = e2)), ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))))
equation
true = BackendDAEUtil.isDiscreteExp(e1, vars, knvars);
true = BackendDAEUtil.isDiscreteExp(e2, vars, knvars);
Debug.fcall(Flags.RELIDX, print, "discrete RELATION: " +& intString(numRelations) +& "\n");
then
((e, true, ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))));
// All other functions generate zerocrossing.
case (((e as DAE.RELATION(exp1 = e1, operator = op, exp2 = e2)), ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))))
equation
Debug.fcall(Flags.RELIDX, print, "start collectZC : " +& ExpressionDump.printExpStr(e) +& " numRelations: " +& intString(numRelations) +& "\n");
e_1 = DAE.RELATION(e1, op, e2, numRelations, NONE());
{zc} = makeZeroCrossings({e_1}, {eq_count}, {wc_count});
((eres, relations, numRelations)) = zerocrossingindex(e_1, numRelations, relations, zc);
{zc} = makeZeroCrossings({eres}, {eq_count}, {wc_count});
((eres1 as DAE.RELATION(index=itmp), zeroCrossings, _)) = zerocrossingindex(eres, numRelations, zeroCrossings, zc);
Debug.fcall(Flags.RELIDX, print, "collectZC result zc : " +& ExpressionDump.printExpStr(eres) +& " index: " +& intString(itmp) +& "\n");
then ((eres, true, ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))));
// math function that triggering events
case (((e as DAE.CALL(path=Absyn.IDENT("integer"), expLst={e1}, attr=attr)), ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))))
equation
Debug.fcall(Flags.RELIDX, print, "start collectZC : " +& ExpressionDump.printExpStr(e) +& " numMathFunctions: " +& intString(numMathFunctions) +& "\n");
e_1 = DAE.CALL(Absyn.IDENT("integer"), {e1, DAE.ICONST(numMathFunctions)}, attr);
{zc} = makeZeroCrossings({e_1}, {eq_count}, {wc_count});
((eres, zeroCrossings, numMathFunctions)) = zerocrossingindex(e_1, numMathFunctions, zeroCrossings, zc);
Debug.fcall(Flags.RELIDX, print, "collectZC result zc : " +& ExpressionDump.printExpStr(eres) +& "\n");
then ((eres, true, ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))));
case (((e as DAE.CALL(path=Absyn.IDENT("floor"), expLst={e1}, attr=attr)), ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))))
equation
Debug.fcall(Flags.RELIDX, print, "start collectZC : " +& ExpressionDump.printExpStr(e) +& " numMathFunctions: " +& intString(numMathFunctions) +& "\n");
e_1 = DAE.CALL(Absyn.IDENT("floor"), {e1, DAE.ICONST(numMathFunctions)}, attr);
{zc} = makeZeroCrossings({e_1}, {eq_count}, {wc_count});
((eres, zeroCrossings, numMathFunctions)) = zerocrossingindex(e_1, numMathFunctions, zeroCrossings, zc);
Debug.fcall(Flags.RELIDX, print, "collectZC result zc : " +& ExpressionDump.printExpStr(eres) +& "\n");
then ((eres, true, ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))));
case (((e as DAE.CALL(path=Absyn.IDENT("ceil"), expLst={e1}, attr=attr)), ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))))
equation
Debug.fcall(Flags.RELIDX, print, "start collectZC : " +& ExpressionDump.printExpStr(e) +& " numMathFunctions: " +& intString(numMathFunctions) +& "\n");
e_1 = DAE.CALL(Absyn.IDENT("ceil"), {e1, DAE.ICONST(numMathFunctions)}, attr);
{zc} = makeZeroCrossings({e_1}, {eq_count}, {wc_count});
((eres, zeroCrossings, numMathFunctions)) = zerocrossingindex(e_1, numMathFunctions, zeroCrossings, zc);
Debug.fcall(Flags.RELIDX, print, "collectZC result zc : " +& ExpressionDump.printExpStr(eres) +& "\n");
then ((eres, true, ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))));
case (((e as DAE.CALL(path=Absyn.IDENT("div"), expLst={e1, e2}, attr=attr)), ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))))
equation
Debug.fcall(Flags.RELIDX, print, "start collectZC : " +& ExpressionDump.printExpStr(e) +& " numMathFunctions: " +& intString(numMathFunctions) +& "\n");
e_1 = DAE.CALL(Absyn.IDENT("div"), {e1, e2, DAE.ICONST(numMathFunctions)}, attr);
{zc} = makeZeroCrossings({e_1}, {eq_count}, {wc_count});
((eres, zeroCrossings, numMathFunctions)) = zerocrossingindex(e_1, numMathFunctions, zeroCrossings, zc);
Debug.fcall(Flags.RELIDX, print, "collectZC result zc : " +& ExpressionDump.printExpStr(eres) +& "\n");
then ((eres, true, ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))));
/* mod is rewritten to x-floor(x/y)*y */
case ((e as DAE.CALL(path=Absyn.IDENT("mod"), expLst={e1, e2}, attr=attr as DAE.CALL_ATTR(ty = ty)), ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))))
equation
Debug.fcall(Flags.RELIDX, print, "start collectZC : " +& ExpressionDump.printExpStr(e) +& " numMathFunctions: " +& intString(numMathFunctions) +& "\n");
e_1 = DAE.CALL(Absyn.IDENT("floor"), {DAE.BINARY(e1, DAE.DIV(ty), e2), DAE.ICONST(numMathFunctions)}, attr);
{zc} = makeZeroCrossings({e_1}, {eq_count}, {wc_count});
((eres, zeroCrossings, numMathFunctions)) = zerocrossingindex(e_1, numMathFunctions, zeroCrossings, zc);
e_2 = DAE.BINARY(e1, DAE.SUB(ty), DAE.BINARY(eres, DAE.MUL(ty), e2));
Debug.fcall(Flags.RELIDX, print, "collectZC result zc : " +& ExpressionDump.printExpStr(eres) +& "\n");
then ((e_2, true, ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))));
/* rem is rewritten to div(x/y)*y - x */
case ((e as DAE.CALL(path=Absyn.IDENT("rem"), expLst={e1, e2}, attr=attr as DAE.CALL_ATTR(ty = ty)), ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))))
equation
Debug.fcall(Flags.RELIDX, print, "start collectZC : " +& ExpressionDump.printExpStr(e) +& " numMathFunctions: " +& intString(numMathFunctions) +& "\n");
e_1 = DAE.CALL(Absyn.IDENT("div"), {e1, e2, DAE.ICONST(numMathFunctions)}, attr);
{zc} = makeZeroCrossings({e_1}, {eq_count}, {wc_count});
((eres, zeroCrossings, numMathFunctions)) = zerocrossingindex(e_1, numMathFunctions, zeroCrossings, zc);
e_2 = DAE.BINARY(e1, DAE.SUB(ty), DAE.BINARY(eres, DAE.MUL(ty), e2));
Debug.fcall(Flags.RELIDX, print, "collectZC result zc : " +& ExpressionDump.printExpStr(eres) +& "\n");
then ((e_2, true, ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))));
case ((e, ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars)))) then ((e, true, ((zeroCrossings, relations, samples, numRelations, numMathFunctions), (eq_count, wc_count, vars, knvars))));
end matchcontinue;
end collectZC;
protected function collectZCAlgs "
Collects zero crossings in algorithms stamts, beside for loops those are
processed by collectZCAlgsFor
modified: 2011-01 by wbraun
"
input tuple<DAE.Exp, tuple<DAE.Exp, list<DAE.Exp>, DAE.Exp, tuple<list<BackendDAE.ZeroCrossing>, list<BackendDAE.ZeroCrossing>, list<BackendDAE.ZeroCrossing>, Integer, Integer>, tuple<Integer, BackendDAE.Variables, BackendDAE.Variables>>> inTplExpExpTplExpExpLstVariables;
output tuple<DAE.Exp, Boolean, tuple<DAE.Exp, list<DAE.Exp>, DAE.Exp, tuple<list<BackendDAE.ZeroCrossing>, list<BackendDAE.ZeroCrossing>, list<BackendDAE.ZeroCrossing>, Integer, Integer>, tuple<Integer, BackendDAE.Variables, BackendDAE.Variables>>> outTplExpExpTplExpExpLstVariables;
algorithm
outTplExpExpTplExpExpLstVariables:=
matchcontinue (inTplExpExpTplExpExpLstVariables)
local
DAE.Exp e, e1, e2, e_1, e_2, eres, eres1, iterator, range;
list<DAE.Exp> le;
BackendDAE.Variables vars, knvars;
list<BackendDAE.ZeroCrossing> zeroCrossings, zc_lst, relations, samples;
DAE.Operator op;
Integer numRelations, alg_indx, itmp, numRelations1, numMathFunctions, i;
BackendDAE.ZeroCrossing zc;
DAE.CallAttributes attr;
DAE.Type ty;
case (((e as DAE.CALL(path = Absyn.IDENT(name = "noEvent"))), (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
then ((e, false, (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))));
case (((e as DAE.CALL(path = Absyn.IDENT(name = "smooth"))), (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
then ((e, false, (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))));
case (((e as DAE.CALL(path = Absyn.IDENT(name = "sample"))), (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
equation
zc_lst = makeZeroCrossings({e}, {alg_indx}, {});
samples = listAppend(samples, zc_lst);
samples = mergeZeroCrossings(samples);
// Debug.fcall(Flags.RELIDX, print, "sample index algotihm: " +& intString(indx) +& "\n");
then ((e, true, (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))));
case (((e as DAE.LUNARY(operator = op, exp = e1)), (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
equation
true = BackendDAEUtil.isDiscreteExp(e1, vars, knvars);
//Debug.fcall(Flags.RELIDX, print, "discrete LUNARY: " +& intString(indx) +& "\n");
//Debug.fcall(Flags.RELIDX, BackendDump.debugExpStr, (e, "\n"));
then
((e, true, (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))));
case (((e as DAE.LBINARY(exp1 = e1, operator = op, exp2 = e2)), (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
equation
true = BackendDAEUtil.isDiscreteExp(e1, vars, knvars);
true = BackendDAEUtil.isDiscreteExp(e2, vars, knvars);
//Debug.fcall(Flags.RELIDX, print, "discrete LBINARY: " +& intString(numRelations) +& "\n");
//Debug.fcall(Flags.RELIDX, BackendDump.debugExpStr, (e, "\n"));
then
((e, true, (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))));
// coditions that are zerocrossings.
case (((e as DAE.LUNARY(exp = e1, operator = op)), (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
equation
Debug.fcall(Flags.RELIDX, print, "continues LUNARY: " +& intString(numRelations) +& "\n");
((e1, (iterator, le, range, (_, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars)))) = Expression.traverseExpTopDown(e1, collectZCAlgs, (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars)));
e_1 = DAE.LUNARY(op, e1);
{zc} = makeZeroCrossings({e_1}, {alg_indx}, {});
zc_lst = List.select1(zeroCrossings, sameZeroCrossing, zc);
zeroCrossings = Util.if_(List.isEmpty(zc_lst), listAppend(zeroCrossings, {zc}), zeroCrossings);
Debug.fcall(Flags.RELIDX, BackendDump.debugExpStr, (e_1, "\n"));
then
((e, false, (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))));
case (((e as DAE.LBINARY(exp1 = e1, operator = op, exp2 = e2)), (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
equation
Debug.fcall(Flags.RELIDX, print, "continues LBINARY: " +& intString(numRelations) +& "\n");
Debug.fcall(Flags.RELIDX, BackendDump.debugExpStr, (e, "\n"));
((e_1, (iterator, le, range, (_, relations, samples, numRelations1, numMathFunctions), (alg_indx, vars, knvars)))) = Expression.traverseExpTopDown(e1, collectZCAlgs, (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars)));
((e_2, (iterator, le, range, (_, relations, samples, numRelations1, numMathFunctions), (alg_indx, vars, knvars)))) = Expression.traverseExpTopDown(e2, collectZCAlgs, (iterator, le, range, (zeroCrossings, relations, samples, numRelations1, numMathFunctions), (alg_indx, vars, knvars)));
true = intGt(numRelations1, numRelations);
e_1 = DAE.LBINARY(e_1, op, e_2);
{zc} = makeZeroCrossings({e_1}, {alg_indx}, {});
zc_lst = List.select1(zeroCrossings, sameZeroCrossing, zc);
zeroCrossings = Util.if_(List.isEmpty(zc_lst), listAppend(zeroCrossings, {zc}), zeroCrossings);
print(Debug.fcallret1(Flags.RELIDX, BackendDump.zeroCrossingListString, zeroCrossings, ""));
then
((e_1, false, (iterator, le, range, (zeroCrossings, relations, samples, numRelations1, numMathFunctions), (alg_indx, vars, knvars))));
// function with discrete expressions generate no zerocrossing
case (((e as DAE.RELATION(exp1 = e1, operator = op, exp2 = e2)), (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
equation
true = BackendDAEUtil.isDiscreteExp(e1, vars, knvars);
true = BackendDAEUtil.isDiscreteExp(e2, vars, knvars);
then ((e, true, (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))));
// All other functions generate zerocrossing.
case (((e as DAE.RELATION(exp1 = e1, operator = op, exp2 = e2)), (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
equation
Debug.fcall(Flags.RELIDX, print, "start collectZC : " +& ExpressionDump.printExpStr(e) +& " numRelations: " +& intString(numRelations) +& "\n");
e_1 = DAE.RELATION(e1, op, e2, numRelations, NONE());
{zc} = makeZeroCrossings({e_1}, {alg_indx}, {});
((eres, relations, numRelations)) = zerocrossingindex(e_1, numRelations, relations, zc);
{zc} = makeZeroCrossings({eres}, {alg_indx}, {});
((eres1 as DAE.RELATION(index=itmp), zeroCrossings, _)) = zerocrossingindex(eres, numRelations, zeroCrossings, zc);
Debug.fcall(Flags.RELIDX, print, "collectZC result zc : " +& ExpressionDump.printExpStr(eres) +& " index: " +& intString(numRelations) +& "\n");
then ((eres, true, (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))));
// math function that triggering events
case (((e as DAE.CALL(path=Absyn.IDENT("integer"), expLst={e1}, attr=attr)), (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
equation
Debug.fcall(Flags.RELIDX, print, "start collectZC : " +& ExpressionDump.printExpStr(e) +& " numMathFunctions: " +& intString(numMathFunctions) +& "\n");
e_1 = DAE.CALL(Absyn.IDENT("integer"), {e1, DAE.ICONST(numMathFunctions)}, attr);
{zc} = makeZeroCrossings({e_1}, {alg_indx}, {});
((eres, zeroCrossings, numMathFunctions)) = zerocrossingindex(e_1, numMathFunctions, zeroCrossings, zc);
Debug.fcall(Flags.RELIDX, print, "collectZC result zc : " +& ExpressionDump.printExpStr(eres) +& "\n");
then ((eres, true, (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))));
case (((e as DAE.CALL(path=Absyn.IDENT("floor"), expLst={e1}, attr=attr)), (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
equation
Debug.fcall(Flags.RELIDX, print, "start collectZC : " +& ExpressionDump.printExpStr(e) +& " numMathFunctions: " +& intString(numMathFunctions) +& "\n");
e_1 = DAE.CALL(Absyn.IDENT("floor"), {e1, DAE.ICONST(numMathFunctions)}, attr);
{zc} = makeZeroCrossings({e_1}, {alg_indx}, {});
((eres, zeroCrossings, numMathFunctions)) = zerocrossingindex(e_1, numMathFunctions, zeroCrossings, zc);
Debug.fcall(Flags.RELIDX, print, "collectZC result zc : " +& ExpressionDump.printExpStr(eres) +& "\n");
then ((eres, true, (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))));
case (((e as DAE.CALL(path=Absyn.IDENT("ceil"), expLst={e1}, attr=attr)), (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
equation
Debug.fcall(Flags.RELIDX, print, "start collectZC : " +& ExpressionDump.printExpStr(e) +& " numMathFunctions: " +& intString(numMathFunctions) +& "\n");
e_1 = DAE.CALL(Absyn.IDENT("ceil"), {e1, DAE.ICONST(numMathFunctions)}, attr);
{zc} = makeZeroCrossings({e_1}, {alg_indx}, {});
((eres, zeroCrossings, numMathFunctions)) = zerocrossingindex(e_1, numMathFunctions, zeroCrossings, zc);
Debug.fcall(Flags.RELIDX, print, "collectZC result zc : " +& ExpressionDump.printExpStr(eres) +& "\n");
then ((eres, true, (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))));
case (((e as DAE.CALL(path=Absyn.IDENT("div"), expLst={e1, e2}, attr=attr)), (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
equation
Debug.fcall(Flags.RELIDX, print, "start collectZC : " +& ExpressionDump.printExpStr(e) +& " numMathFunctions: " +& intString(numMathFunctions) +& "\n");
e_1 = DAE.CALL(Absyn.IDENT("div"), {e1, e2, DAE.ICONST(numMathFunctions)}, attr);
{zc} = makeZeroCrossings({e_1}, {alg_indx}, {});
((eres, zeroCrossings, numMathFunctions)) = zerocrossingindex(e_1, numMathFunctions, zeroCrossings, zc);
Debug.fcall(Flags.RELIDX, print, "collectZC result zc : " +& ExpressionDump.printExpStr(eres) +& "\n");
then ((eres, true, (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))));
/* mod is rewritten to x-floor(x/y)*y */
case ((e as DAE.CALL(path=Absyn.IDENT("mod"), expLst={e1, e2}, attr=attr as DAE.CALL_ATTR(ty = ty)), (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
equation
Debug.fcall(Flags.RELIDX, print, "start collectZC : " +& ExpressionDump.printExpStr(e) +& " numMathFunctions: " +& intString(numMathFunctions) +& "\n");
e_1 = DAE.CALL(Absyn.IDENT("floor"), {DAE.BINARY(e1, DAE.DIV(ty), e2), DAE.ICONST(numMathFunctions)}, attr);
{zc} = makeZeroCrossings({e_1}, {alg_indx}, {});
((eres, zeroCrossings, numMathFunctions)) = zerocrossingindex(e_1, numMathFunctions, zeroCrossings, zc);
e_2 = DAE.BINARY(e1, DAE.SUB(ty), DAE.BINARY(eres, DAE.MUL(ty), e2));
Debug.fcall(Flags.RELIDX, print, "collectZC result zc : " +& ExpressionDump.printExpStr(eres) +& "\n");
then ((e_2, true, (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))));
/* rem is rewritten to div(x/y)*y - x */
case ((e as DAE.CALL(path=Absyn.IDENT("rem"), expLst={e1, e2}, attr=attr as DAE.CALL_ATTR(ty = ty)), (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
equation
Debug.fcall(Flags.RELIDX, print, "start collectZC : " +& ExpressionDump.printExpStr(e) +& " numMathFunctions: " +& intString(numMathFunctions) +& "\n");
e_1 = DAE.CALL(Absyn.IDENT("div"), {e1, e2, DAE.ICONST(numMathFunctions)}, attr);
{zc} = makeZeroCrossings({e_1}, {alg_indx}, {});
((eres, zeroCrossings, numMathFunctions)) = zerocrossingindex(e_1, numMathFunctions, zeroCrossings, zc);
e_2 = DAE.BINARY(e1, DAE.SUB(ty), DAE.BINARY(eres, DAE.MUL(ty), e2));
Debug.fcall(Flags.RELIDX, print, "collectZC result zc : " +& ExpressionDump.printExpStr(eres) +& "\n");
then ((e_2, true, (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))));
case ((e, (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
then ((e, true, (iterator, le, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))));
end matchcontinue;
end collectZCAlgs;
/* TODO: implement math functions support here */
protected function collectZCAlgsFor "Collects zero crossings in for loops
added: 2011-01 by wbraun"
input tuple<DAE.Exp, tuple<DAE.Exp, list<DAE.Exp>, DAE.Exp, tuple<list<BackendDAE.ZeroCrossing>, list<BackendDAE.ZeroCrossing>, list<BackendDAE.ZeroCrossing>, Integer, Integer>, tuple<Integer, BackendDAE.Variables, BackendDAE.Variables>>> inTplExpExpTplExpExpLstVariables;
output tuple<DAE.Exp, Boolean, tuple<DAE.Exp, list<DAE.Exp>, DAE.Exp, tuple<list<BackendDAE.ZeroCrossing>, list<BackendDAE.ZeroCrossing>, list<BackendDAE.ZeroCrossing>, Integer, Integer>, tuple<Integer, BackendDAE.Variables, BackendDAE.Variables>>> outTplExpExpTplExpExpLstVariables;
algorithm
outTplExpExpTplExpExpLstVariables:=
matchcontinue (inTplExpExpTplExpExpLstVariables)
local
DAE.Exp e, e1, e2, e_1, e_2, eres, iterator, range, range2;
list<DAE.Exp> inExpLst, explst;
BackendDAE.Variables vars, knvars;
list<BackendDAE.ZeroCrossing> zeroCrossings, zc_lst, zcLstNew, relations, samples;
DAE.Operator op;
Integer numRelations, alg_indx, itmp, numRelations1, numMathFunctions;
list<Integer> eqs;
Boolean b1, b2;
DAE.Exp startvalue, stepvalue;
Option<DAE.Exp> stepvalueopt;
Integer istart, istep;
BackendDAE.ZeroCrossing zc;
case (((e as DAE.CALL(path = Absyn.IDENT(name = "noEvent"))), (iterator, inExpLst, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
then ((e, false, (iterator, inExpLst, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))));
case (((e as DAE.CALL(path = Absyn.IDENT(name = "smooth"))), (iterator, inExpLst, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
then ((e, false, (iterator, inExpLst, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))));
case (((e as DAE.CALL(path = Absyn.IDENT(name = "sample"))), (iterator, inExpLst, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
equation
eqs = {alg_indx};
zc_lst = makeZeroCrossings({e}, eqs, {});
samples = listAppend(samples, zc_lst);
samples = mergeZeroCrossings(samples);
Debug.fcall(Flags.RELIDX, print, "collectZCAlgsFor sample" +& "\n");
then ((e, true, (iterator, inExpLst, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))));
case (((e as DAE.LUNARY(operator = op, exp = e1)), (iterator, inExpLst, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
equation
true = BackendDAEUtil.isDiscreteExp(e1, vars, knvars);
//Debug.fcall(Flags.RELIDX, print, "discrete LUNARY: " +& intString(indx) +& "\n");
//Debug.fcall(Flags.RELIDX, BackendDump.debugExpStr, (e, "\n"));
then
((e, true, (iterator, inExpLst, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))));
case (((e as DAE.LBINARY(exp1 = e1, operator = op, exp2 = e2)), (iterator, inExpLst, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
equation
true = BackendDAEUtil.isDiscreteExp(e1, vars, knvars);
true = BackendDAEUtil.isDiscreteExp(e2, vars, knvars);
//Debug.fcall(Flags.RELIDX, print, "discrete LBINARY: " +& intString(numRelations) +& "\n");
//Debug.fcall(Flags.RELIDX, BackendDump.debugExpStr, (e, "\n"));
then
((e, true, (iterator, inExpLst, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))));
// coditions that are zerocrossings.
case (((e as DAE.LUNARY(exp = e1, operator = op)), (iterator, inExpLst, range as DAE.RANGE(start=startvalue, step=stepvalueopt), (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
equation
Debug.fcall(Flags.RELIDX, print, "continues LUNARY: " +& intString(numRelations) +& "\n");
true = Expression.expContains(e, iterator);
((e1, (iterator, inExpLst, range2, (_, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars)))) = Expression.traverseExpTopDown(e1, collectZCAlgsFor, (iterator, inExpLst, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars)));
e_1 = DAE.LUNARY(op, e1);
(explst, itmp) = replaceIteratorwithStaticValues(e_1, iterator, inExpLst, numRelations);
zc_lst = makeZeroCrossings(explst, {alg_indx}, {});
zc_lst = listAppend(zeroCrossings, zc_lst);
zc_lst = mergeZeroCrossings(zc_lst);
itmp = (listLength(zc_lst)-listLength(zeroCrossings));
zeroCrossings = Util.if_(itmp>0, zc_lst, zeroCrossings);
Debug.fcall(Flags.RELIDX, BackendDump.debugExpStr, (e_1, "\n"));
then
((e_1, false, (iterator, inExpLst, range2, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))));
// coditions that are zerocrossings.
case (((e as DAE.LUNARY(exp = e1, operator = op)), (iterator, inExpLst, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
equation
Debug.fcall(Flags.RELIDX, print, "continues LUNARY: " +& intString(numRelations) +& "\n");
((e1, (iterator, inExpLst, range, (_, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars)))) = Expression.traverseExpTopDown(e1, collectZCAlgsFor, (iterator, inExpLst, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars)));
e_1 = DAE.LUNARY(op, e1);
{zc} = makeZeroCrossings({e_1}, {alg_indx}, {});
zc_lst = List.select1(zeroCrossings, sameZeroCrossing, zc);
zeroCrossings = Util.if_(List.isEmpty(zc_lst), listAppend(zeroCrossings, {zc}), zeroCrossings);
Debug.fcall(Flags.RELIDX, BackendDump.debugExpStr, (e_1, "\n"));
then
((e_1, false, (iterator, inExpLst, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))));
case (((e as DAE.LBINARY(exp1 = e1, operator = op, exp2 = e2)), (iterator, inExpLst, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
equation
Debug.fcall(Flags.RELIDX, print, "continues LBINARY: " +& intString(numRelations) +& "\n");
Debug.fcall(Flags.RELIDX, BackendDump.debugExpStr, (e, "\n"));
b1 = Expression.expContains(e1, iterator);
b2 = Expression.expContains(e2, iterator);
true = Util.boolOrList({b1, b2});
((e_1, (iterator, inExpLst, range, (_, relations, samples, numRelations1, numMathFunctions), (alg_indx, vars, knvars)))) = Expression.traverseExpTopDown(e1, collectZCAlgsFor, (iterator, inExpLst, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars)));
((e_2, (iterator, inExpLst, range, (_, relations, samples, numRelations1, numMathFunctions), (alg_indx, vars, knvars)))) = Expression.traverseExpTopDown(e2, collectZCAlgsFor, (iterator, inExpLst, range, (zeroCrossings, relations, samples, numRelations1, numMathFunctions), (alg_indx, vars, knvars)));
true = intGt(numRelations1, numRelations);
e_1 = DAE.LBINARY(e_1, op, e_2);
(explst, itmp) = replaceIteratorwithStaticValues(e_1, iterator, inExpLst, numRelations1);
zc_lst = makeZeroCrossings(explst, {alg_indx}, {});
zc_lst = listAppend(zeroCrossings, zc_lst);
zc_lst = mergeZeroCrossings(zc_lst);
itmp = (listLength(zc_lst)-listLength(zeroCrossings));
zeroCrossings = Util.if_(itmp>0, zc_lst, zeroCrossings);
print(Debug.fcallret1(Flags.RELIDX, BackendDump.zeroCrossingListString, zeroCrossings, ""));
then
((e_1, false, (iterator, inExpLst, range, (zeroCrossings, relations, samples, numRelations1, numMathFunctions), (alg_indx, vars, knvars))));
case (((e as DAE.LBINARY(exp1 = e1, operator = op, exp2 = e2)), (iterator, inExpLst, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
equation
Debug.fcall(Flags.RELIDX, print, "continues LBINARY: " +& intString(numRelations) +& "\n");
Debug.fcall(Flags.RELIDX, BackendDump.debugExpStr, (e, "\n"));
((e_1, (iterator, inExpLst, range, (_, relations, samples, numRelations1, numMathFunctions), (alg_indx, vars, knvars)))) = Expression.traverseExpTopDown(e1, collectZCAlgsFor, (iterator, inExpLst, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars)));
((e_2, (iterator, inExpLst, range, (_, relations, samples, numRelations1, numMathFunctions), (alg_indx, vars, knvars)))) = Expression.traverseExpTopDown(e2, collectZCAlgsFor, (iterator, inExpLst, range, (zeroCrossings, relations, samples, numRelations1, numMathFunctions), (alg_indx, vars, knvars)));
true = intGt(numRelations1, numRelations);
e_1 = DAE.LBINARY(e_1, op, e_2);
{zc} = makeZeroCrossings({e_1}, {alg_indx}, {});
zc_lst = List.select1(zeroCrossings, sameZeroCrossing, zc);
zeroCrossings = Util.if_(List.isEmpty(zc_lst), listAppend(zeroCrossings, {zc}), zeroCrossings);
print(Debug.fcallret1(Flags.RELIDX, BackendDump.zeroCrossingListString, zeroCrossings, ""));
then
((e_1, false, (iterator, inExpLst, range, (zeroCrossings, relations, samples, numRelations1, numMathFunctions), (alg_indx, vars, knvars))));
// function with discrete expressions generate no zerocrossing.
case (((e as DAE.RELATION(exp1 = e1, operator = op, exp2 = e2)), (iterator, inExpLst, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
equation
true = BackendDAEUtil.isDiscreteExp(e1, vars, knvars);
true = BackendDAEUtil.isDiscreteExp(e2, vars, knvars);
then ((e, true, (iterator, inExpLst, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))));
// All other functions generate zerocrossing.
case (((e as DAE.RELATION(exp1 = e1, operator = op, exp2 = e2)), (iterator, inExpLst, range as DAE.RANGE(start=startvalue, step=stepvalueopt), (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
equation
b1 = Expression.expContains(e1, iterator);
b2 = Expression.expContains(e2, iterator);
true = Util.boolOrList({b1, b2});
Debug.fcall(Flags.RELIDX, print, " number of relations : " +& intString(numRelations) +& "\n");
stepvalue = Util.getOptionOrDefault(stepvalueopt, DAE.ICONST(1));
istart = BackendDAECreate.expInt(startvalue, knvars);
istep = BackendDAECreate.expInt(stepvalue, knvars);
e_1 = DAE.RELATION(e1, op, e2, numRelations, SOME((iterator, istart, istep)));
(explst, itmp) = replaceIteratorwithStaticValues(e, iterator, inExpLst, numRelations);
Debug.fcall(Flags.RELIDX, print, " number of new zc : " +& intString(listLength(explst)) +& "\n");
zcLstNew = makeZeroCrossings(explst, {alg_indx}, {});
zc_lst = listAppend(relations, zcLstNew);
zc_lst = mergeZeroCrossings(zc_lst);
Debug.fcall(Flags.RELIDX, print, " number of new zc : " +& intString(listLength(zc_lst)) +& "\n");
itmp = (listLength(zc_lst)-listLength(relations));
Debug.fcall(Flags.RELIDX, print, " itmp : " +& intString(itmp) +& "\n");
numRelations = intAdd(itmp, numRelations);
eres = Util.if_((itmp>0), e_1, e);
zeroCrossings = listAppend(zeroCrossings, zcLstNew);
zeroCrossings = mergeZeroCrossings(zeroCrossings);
Debug.fcall(Flags.RELIDX, print, "collectZCAlgsFor result zc : " +& ExpressionDump.printExpStr(eres)+& " index:" +& intString(numRelations) +& "\n");
then ((eres, true, (iterator, inExpLst, range, (zeroCrossings, zc_lst, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))));
// All other functions generate zerocrossing.
case (((e as DAE.RELATION(exp1 = e1, operator = op, exp2 = e2)), (iterator, inExpLst, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
equation
b1 = Expression.expContains(e1, iterator);
b2 = Expression.expContains(e2, iterator);
false = Util.boolOrList({b1, b2});
e_1 = DAE.RELATION(e1, op, e2, numRelations, NONE());
zcLstNew = makeZeroCrossings({e_1}, {alg_indx}, {});
zc_lst = listAppend(relations, zcLstNew);
zc_lst = mergeZeroCrossings(zc_lst);
itmp = (listLength(zc_lst)-listLength(relations));
numRelations = numRelations + itmp;
eres = Util.if_((itmp>0), e_1, e);
zeroCrossings = listAppend(zeroCrossings, zcLstNew);
zeroCrossings = mergeZeroCrossings(zeroCrossings);
Debug.fcall(Flags.RELIDX, print, "collectZCAlgsFor result zc : " +& ExpressionDump.printExpStr(eres)+& " index:" +& intString(numRelations) +& "\n");
then ((eres, true, (iterator, inExpLst, range, (zeroCrossings, zc_lst, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))));
case ((e, (iterator, inExpLst, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))))
then ((e, true, (iterator, inExpLst, range, (zeroCrossings, relations, samples, numRelations, numMathFunctions), (alg_indx, vars, knvars))));
end matchcontinue;
end collectZCAlgsFor;
protected function replaceIteratorwithStaticValues
" Helper function for collectZCAlgFor "
input DAE.Exp inExp;
input DAE.Exp inIterator;
input list<DAE.Exp> inExpLst;
input Integer inIndex;
output list<DAE.Exp> outZeroCrossings;
output Integer outIndex;
algorithm
(outZeroCrossings, outIndex) := matchcontinue(inExp, inIterator, inExpLst, inIndex)
local
DAE.Exp e, e1, e2, i, exp, res1, e_1;
DAE.Operator op;
list<DAE.Exp> rest, res2;
Integer index;
case (_, _, {}, _) then ({}, inIndex);
case (exp as DAE.RELATION(exp1 = e1, operator = op, exp2 = e2), i, e::rest, index)
equation
e_1 = DAE.RELATION(e1, op, e2, index, NONE());
((res1, (_, _))) = Expression.replaceExpTpl((e_1, (i, e)));
(res2, index) = replaceIteratorwithStaticValues(exp, i, rest, index+1);
res2 = listAppend({res1}, res2);
then (res2, index);
case (exp as DAE.LUNARY(exp = e1, operator = op), i, e::rest, index)
equation
e_1 = DAE.LUNARY(op, e1);
((res1, (_, _))) = Expression.replaceExpTpl((e_1, (i, e)));
(res2, index) = replaceIteratorwithStaticValues(exp, i, rest, index+1);
res2 = listAppend({res1}, res2);
then (res2, index);
case (exp as DAE.LBINARY(exp1 = e1, operator = op, exp2 = e2), i, e::rest, index)
equation
e_1 = DAE.LBINARY(e1, op, e2);
((res1, (_, _))) = Expression.replaceExpTpl((e_1, (i, e)));
(res2, index) = replaceIteratorwithStaticValues(exp, i, rest, index+1);
res2 = listAppend({res1}, res2);
then (res2, index);
case (_, _, _, _)