/
ExpressionSolve.mo
1899 lines (1642 loc) · 65.5 KB
/
ExpressionSolve.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-2014, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
encapsulated package ExpressionSolve
" file: ExpressionSolve.mo
package: ExpressionSolve
description: ExpressionSolve
RCS: $Id$
This file contains the module ExpressionSolve, which contains functions
to solve a DAE.Exp for a DAE.Exp"
// public imports
public import Absyn;
public import DAE;
// protected imports
protected import ComponentReference;
protected import Debug;
protected import Differentiate;
protected import Expression;
protected import ExpressionDump;
protected import ExpressionSimplify;
protected import Flags;
protected import List;
protected import Inline;
public function solve
"Solves an equation consisting of a right hand side (rhs) and a
left hand side (lhs), with respect to the expression given as
third argument, usually a variable."
input DAE.Exp inExp1 "lhs";
input DAE.Exp inExp2 "rhs";
input DAE.Exp inExp3 "DAE.CREF or 'der(DAE.CREF())'";
output DAE.Exp outExp;
output list<DAE.Statement> outAsserts;
protected
list<BackendDAE.Equation> dummy1;
list<DAE.ComponentRef> dummy2;
Integer dummyI;
algorithm
/*
print("Try to solve: rhs: " +
ExpressionDump.dumpExpStr(inExp1,0) + " lhs: " +
ExpressionDump.dumpExpStr(inExp2,0) + " with respect to: " +
ExpressionDump.printExpStr(inExp3) + "\n");
*/
(outExp,outAsserts,dummy1, dummy2, dummyI) := matchcontinue(inExp1, inExp2, inExp3)
case(_,_,_) then solveSimple(inExp1, inExp2, inExp3,0);
case(_,_,_) then solveSimple(inExp2, inExp1, inExp3,0);
case(_,_,_) then solveWork(inExp1, inExp2, inExp3, NONE(), NONE(), 0);
else
equation
if Flags.isSet(Flags.FAILTRACE) then
print("\n-ExpressionSolve.solve failed:\n");
print(ExpressionDump.printExpStr(inExp1) + " = " + ExpressionDump.printExpStr(inExp2));
print(" with respect to: " + ExpressionDump.printExpStr(inExp3));
end if;
then fail();
end matchcontinue;
(outExp,_) := ExpressionSimplify.simplify1(outExp);
end solve;
public function solve2
"Solves an equation with modelica function consisting of a right hand side (rhs) and a
left hand side (lhs), with respect to the expression given as
third argument, usually a variable.
"
input DAE.Exp inExp1 "lhs";
input DAE.Exp inExp2 "rhs";
input DAE.Exp inExp3 "DAE.CREF or 'der(DAE.CREF())'";
input Option<DAE.FunctionTree> functions "need for solve modelica functions";
input Option<Integer> uniqueEqIndex "offset for tmp vars";
output DAE.Exp outExp;
output list<DAE.Statement> outAsserts;
output list<BackendDAE.Equation> eqnForNewVars "eqn for tmp vars";
output list<DAE.ComponentRef> newVarsCrefs;
protected
Integer dummyI;
algorithm
/*
print("Try to solve: rhs: " +
ExpressionDump.dumpExpStr(inExp1,0) + " lhs: " +
ExpressionDump.dumpExpStr(inExp2,0) + " with respect to: " +
ExpressionDump.printExpStr(inExp3) + "\n");
*/
(outExp,outAsserts,eqnForNewVars,newVarsCrefs,dummyI) := matchcontinue(inExp1, inExp2, inExp3, functions, uniqueEqIndex)
case(_,_,_,_,_) then solveSimple(inExp1, inExp2, inExp3,0);
case(_,_,_,_,_) then solveSimple(inExp2, inExp1, inExp3,0);
case(_,_,_,_,_) then solveWork(inExp1, inExp2, inExp3, functions, uniqueEqIndex, 0);
else
equation
if Flags.isSet(Flags.FAILTRACE) then
print("\n-ExpressionSolve.solve failed:\n");
print(ExpressionDump.printExpStr(inExp1) + " = " + ExpressionDump.printExpStr(inExp2));
print(" with respect to: " + ExpressionDump.printExpStr(inExp3));
end if;
then fail();
end matchcontinue;
(outExp,_) := ExpressionSimplify.simplify1(outExp);
end solve2;
protected function solveWork
input DAE.Exp inExp1 "lhs";
input DAE.Exp inExp2 "rhs";
input DAE.Exp inExp3 "DAE.CREF or 'der(DAE.CREF())'";
input Option<DAE.FunctionTree> functions;
input Option<Integer> uniqueEqIndex "offset for tmp vars";
input Integer idepth;
output DAE.Exp outExp;
output list<DAE.Statement> outAsserts;
output list<BackendDAE.Equation> eqnForNewVars "eqn for tmp vars";
output list<DAE.ComponentRef> newVarsCrefs;
output Integer depth;
protected
DAE.Exp e1, e2;
list<BackendDAE.Equation> eqnForNewVars1;
list<DAE.ComponentRef> newVarsCrefs1;
algorithm
(e1, e2, eqnForNewVars, newVarsCrefs, depth) := matchcontinue(inExp1, inExp2, inExp3, functions, uniqueEqIndex)
case(_,_,_,_,_) then preprocessingSolve(inExp1, inExp2, inExp3, functions, uniqueEqIndex, idepth);
else
equation
if Flags.isSet(Flags.FAILTRACE) then
Debug.trace("\n-ExpressionSolve.preprocessingSolve failed:\n");
Debug.trace(ExpressionDump.printExpStr(inExp1) + " = " + ExpressionDump.printExpStr(inExp2));
Debug.trace(" with respect to: " + ExpressionDump.printExpStr(inExp3));
end if;
then (inExp1,inExp2,{},{}, idepth);
end matchcontinue;
(outExp, outAsserts, eqnForNewVars1, newVarsCrefs1, depth) := matchcontinue(e1, e2, inExp3)
case(DAE.IFEXP(),_,_) then solveIfExp(e1, e2, inExp3, functions, uniqueEqIndex, depth);
case(_,_,_) then solveSimple(e1, e2, inExp3, depth);
case(_,_,_) then solveLinearSystem(e1, e2, inExp3, depth);
else fail();
end matchcontinue;
eqnForNewVars := List.appendNoCopy(eqnForNewVars, eqnForNewVars1);
newVarsCrefs := List.appendNoCopy(newVarsCrefs, newVarsCrefs1);
end solveWork;
public function solveLin
"function: solve linear equation
Solves an equation consisting of a right hand side (rhs) and a
left hand side (lhs), with respect to the expression given as
third argument, usually a variable."
input DAE.Exp inExp1;
input DAE.Exp inExp2;
input DAE.Exp inExp3;
output DAE.Exp outExp;
output list<DAE.Statement> outAsserts;
algorithm
(outExp,outAsserts) := matchcontinue(inExp1, inExp2, inExp3)
case(_,_,_) then solve(inExp1,inExp2,inExp3);
else
equation
if Flags.isSet(Flags.FAILTRACE) then
Debug.trace("\n-ExpressionSolve.solveLin failed:\n");
Debug.trace(ExpressionDump.printExpStr(inExp1) + " = " + ExpressionDump.printExpStr(inExp2));
Debug.trace(" with respect to: " + ExpressionDump.printExpStr(inExp3));
end if;
then fail();
end matchcontinue;
end solveLin;
protected function solveSimple
"Solves simple equations like
a = f(..)
der(a) = f(..)
-a = f(..)
-der(a) = f(..)"
input DAE.Exp inExp1 "lhs";
input DAE.Exp inExp2 "rhs";
input DAE.Exp inExp3 "DAE.CREF or 'der(DAE.CREF())'";
input Integer idepth;
output DAE.Exp outExp;
output list<DAE.Statement> outAsserts;
output list<BackendDAE.Equation> eqnForNewVars := {} "eqn for tmp vars";
output list<DAE.ComponentRef> newVarsCrefs := {};
output Integer odepth := idepth;
algorithm
/*
print("Try to solve: rhs: " +
ExpressionDump.dumpExpStr(inExp1,0) + " lhs: " +
ExpressionDump.dumpExpStr(inExp2,0) + " with respect to: " +
ExpressionDump.printExpStr(inExp3) + "\n");
*/
(outExp,outAsserts) := match (inExp1,inExp2,inExp3)
local
DAE.ComponentRef cr,cr1;
DAE.Type tp;
DAE.Exp e1,e2,res,e11;
Real r, r2;
list<DAE.Statement> asserts;
// special case for inital system when already solved, cr1 = $_start(...)
case (DAE.CREF(componentRef = cr1),DAE.CALL(path = Absyn.IDENT(name = "$_start")),DAE.CREF(componentRef = cr))
equation
true = ComponentReference.crefEqual(cr, cr1);
then
(inExp2,{});
case (DAE.CALL(path = Absyn.IDENT(name = "der"),expLst = {DAE.CREF(componentRef = cr1)}),DAE.CALL(path = Absyn.IDENT(name = "$_start")),DAE.CREF(componentRef = cr))
equation
true = ComponentReference.crefEqual(cr, cr1);
then
(inExp2,{});
// special case when already solved, cr1 = rhs, otherwise division by zero when dividing with derivative
case (DAE.CREF(componentRef = cr1),_,DAE.CREF(componentRef = cr))
equation
true = ComponentReference.crefEqual(cr, cr1);
false = Expression.expHasCrefNoPreOrStart(inExp2, cr);
then
(inExp2,{});
case (DAE.CALL(path = Absyn.IDENT(name = "der"),expLst = {DAE.CREF(componentRef = cr1)}),_,DAE.CALL(path = Absyn.IDENT(name = "der"),expLst = {DAE.CREF(componentRef = cr)}))
equation
true = ComponentReference.crefEqual(cr, cr1);
false = Expression.expHasDerCref(inExp2, cr);
then
(inExp2,{});
// -cr = exp
case (DAE.UNARY(operator = DAE.UMINUS(), exp = DAE.CREF(componentRef = cr1)),_,DAE.CREF(componentRef = cr))
equation
true = ComponentReference.crefEqual(cr1,cr);
// cr not in e2
false = Expression.expHasCrefNoPreOrStart(inExp2,cr);
then
(Expression.negate(inExp2),{});
case (DAE.UNARY(operator = DAE.UMINUS_ARR(), exp = DAE.CREF(componentRef = cr1)),_,DAE.CREF(componentRef = cr))
equation
true = ComponentReference.crefEqual(cr1,cr);
// cr not in e2
false = Expression.expHasCrefNoPreOrStart(inExp2,cr);
then
(Expression.negate(inExp2),{});
case (DAE.UNARY(operator = DAE.UMINUS(), exp = DAE.CALL(path = Absyn.IDENT(name = "der"),expLst = {DAE.CREF(componentRef = cr1)})),_,DAE.CALL(path = Absyn.IDENT(name = "der"),expLst = {DAE.CREF(componentRef = cr)}))
equation
true = ComponentReference.crefEqual(cr1,cr);
// cr not in e2
false = Expression.expHasDerCref(inExp2,cr);
then
(Expression.negate(inExp2),{});
case (DAE.UNARY(operator = DAE.UMINUS_ARR(), exp = DAE.CALL(path = Absyn.IDENT(name = "der"),expLst = {DAE.CREF(componentRef = cr1)})),_,DAE.CALL(path = Absyn.IDENT(name = "der"),expLst = {DAE.CREF(componentRef = cr)}))
equation
true = ComponentReference.crefEqual(cr1,cr);
// cr not in e2
false = Expression.expHasDerCref(inExp2,cr);
then
(Expression.negate(inExp2),{});
// !cr = exp
case (DAE.LUNARY(operator = DAE.NOT(ty=_), exp = DAE.CREF(componentRef = cr1)),_,DAE.CREF(componentRef = cr))
equation
true = ComponentReference.crefEqual(cr1,cr);
// cr not in e2
false = Expression.expHasCrefNoPreOrStart(inExp2,cr);
then
(Expression.negate(inExp2),{});
// Integer(enumcr) = ...
case (DAE.CALL(path = Absyn.IDENT(name = "Integer"),expLst={DAE.CREF(componentRef = cr1)}),_,DAE.CREF(componentRef = cr,ty=tp))
equation
true = ComponentReference.crefEqual(cr, cr1);
// cr not in e2
false = Expression.expHasCrefNoPreorDer(inExp2,cr);
asserts = generateAssertType(tp,cr,inExp3,{});
then (DAE.CAST(tp,inExp2),asserts);
else fail();
end match;
end solveSimple;
protected function generateAssertZero
input DAE.Exp inExp1;
input DAE.Exp inExp2;
input DAE.Exp inExp3;
input DAE.Exp a;
input list<DAE.Statement> inAsserts;
output list<DAE.Statement> outAsserts;
algorithm
outAsserts := matchcontinue (inExp1,inExp2,inExp3,a,inAsserts)
local
DAE.Exp z;
DAE.Type tp;
String estr,se1,se2,se3,sa;
case (_,_,_,_,_)
equation
// zero check already done
true = Expression.isConst(a);
then
inAsserts;
else
equation
tp = Expression.typeof(a);
(z,_) = Expression.makeZeroExpression(Expression.arrayDimension(tp));
se1 = ExpressionDump.printExpStr(inExp2);
se2 = ExpressionDump.printExpStr(inExp1);
se3 = ExpressionDump.printExpStr(inExp3);
sa = ExpressionDump.printExpStr(a);
estr = stringAppendList({"Singular expression ",se1," = ",se2," because ",sa," is Zero! When solving for exp: ", se3, "."});
then
DAE.STMT_ASSERT(DAE.RELATION(a,DAE.NEQUAL(tp),z,-1,NONE()),DAE.SCONST(estr),DAE.ASSERTIONLEVEL_WARNING,DAE.emptyElementSource)::inAsserts;
end matchcontinue;
end generateAssertZero;
protected function generateAssertType
input DAE.Type tp;
input DAE.ComponentRef cr;
input DAE.Exp iExp;
input list<DAE.Statement> inAsserts;
output list<DAE.Statement> outAsserts;
algorithm
outAsserts := match(tp,cr,iExp,inAsserts)
local
Absyn.Path path,p1,pn;
list<String> names;
Integer n;
DAE.Exp e1,en,e,es;
String s1,sn,se,estr,crstr;
case (DAE.T_ENUMERATION(path=path,names=names),_,_,_)
equation
p1 = Absyn.suffixPath(path,listGet(names,1));
e1 = DAE.ENUM_LITERAL(p1,1);
n = listLength(names);
pn = Absyn.suffixPath(path,listGet(names,n));
en = DAE.ENUM_LITERAL(p1,n);
s1 = Absyn.pathString(p1);
sn = Absyn.pathString(pn);
_ = ExpressionDump.printExpStr(iExp);
crstr = ComponentReference.printComponentRefStr(cr);
estr = "Expression for " + crstr + " out of min(" + s1 + ")/max(" + sn + ") = ";
// iExp >= e1 and iExp <= en
e = DAE.LBINARY(DAE.RELATION(iExp,DAE.GREATEREQ(DAE.T_INTEGER_DEFAULT),e1,-1,NONE()),DAE.AND(DAE.T_BOOL_DEFAULT),
DAE.RELATION(iExp,DAE.LESSEQ(DAE.T_INTEGER_DEFAULT),en,-1,NONE()));
es = Expression.makePureBuiltinCall("String", {iExp,DAE.SCONST("d")}, DAE.T_STRING_DEFAULT);
es = DAE.BINARY(DAE.SCONST(estr),DAE.ADD(DAE.T_STRING_DEFAULT),es);
then
DAE.STMT_ASSERT(e,es,DAE.ASSERTIONLEVEL_ERROR,DAE.emptyElementSource)::inAsserts;
else inAsserts;
end match;
end generateAssertType;
public function preprocessingSolve
"
preprocessing for solve1,
sorting and split terms , with respect to the expression given as
third argument.
{f(x,y), g(x,y),x} -> {h(x), k(y)}
author: Vitalij Ruge
"
input DAE.Exp inExp1 "lhs";
input DAE.Exp inExp2 "rhs";
input DAE.Exp inExp3 "DAE.CREF or 'der(DAE.CREF())'";
input Option<DAE.FunctionTree> functions;
input Option<Integer> uniqueEqIndex "offset for tmp vars";
input Integer idepth;
output DAE.Exp h;
output DAE.Exp k;
output list<BackendDAE.Equation> eqnForNewVars := {} "eqn for tmp vars";
output list<DAE.ComponentRef> newVarsCrefs := {};
output Integer depth := idepth;
protected
DAE.Exp res;
list<DAE.Exp> lhs, rhs, resTerms;
list<DAE.Exp> lhsWithX, rhsWithX, lhsWithoutX, rhsWithoutX, eWithX, factorWithX, factorWithoutX;
DAE.Exp lhsX, rhsX, lhsY, rhsY, x, y, N;
DAE.ComponentRef cr;
DAE.Boolean con, new_x, collect := true, inlineFun := true;
Integer iter;
algorithm
(x, _) := ExpressionSimplify.simplify(inExp1);
(y, _) := ExpressionSimplify.simplify(inExp2);
res := Expression.expSub(x, y);
resTerms := Expression.terms(res);
// split and sort
(lhsX, lhsY) := preprocessingSolve5(inExp1, inExp3,true);
(rhsX, rhsY) := preprocessingSolve5(inExp2, inExp3,true);
x := Expression.expSub(lhsX, rhsX);
y := Expression.expSub(rhsY, lhsY);
con := true;
iter := 0;
while con and iter < 1000 loop
(x, y, con) := preprocessingSolve2(x,y, inExp3);
(x, y, new_x) := preprocessingSolve3(x,y, inExp3);
con := con or new_x;
(x, y, new_x) := removeSimpleCalls(x,y, inExp3);
con := con or new_x;
(x, y, new_x) := preprocessingSolve4(x,y, inExp3);
con := new_x or con;
// TODO: use new defined function, which missing in the cpp runtime
if not stringEqual(Config.simCodeTarget(), "Cpp") then
(x, y, new_x, eqnForNewVars, newVarsCrefs, depth) := preprocessingSolveTmpVars(x, y, inExp3, uniqueEqIndex, eqnForNewVars, newVarsCrefs, depth);
con := new_x or con;
end if;
if not con then
(x, con) := ExpressionSimplify.simplify(x);
// Z/N = rhs -> Z = rhs*N
(x,N) := Expression.makeFraction(x);
if not Expression.isOne(N) then
//print("\nx ");print(ExpressionDump.printExpStr(x));print("\nN ");print(ExpressionDump.printExpStr(N));
new_x := true;
y := Expression.expMul(y,N);
end if;
con := new_x or con;
iter := iter + 50;
end if;
if con and collect then
(lhsX, lhsY) := preprocessingSolve5(x, inExp3, true);
(rhsX, rhsY) := preprocessingSolve5(y, inExp3, false);
x := Expression.expSub(lhsX, rhsX);
y := Expression.expSub(rhsY, lhsY);
collect := true;
inlineFun := true;
elseif collect then
collect := false;
con := true;
iter := iter + 50;
elseif inlineFun then
(x,con) := solveFunCalls(x, inExp3, functions);
collect := con;
inlineFun := false;
end if;
iter := iter + 1;
//print("\nx ");print(ExpressionDump.printExpStr(x));print("\ny ");print(ExpressionDump.printExpStr(y));
end while;
(k,_) := ExpressionSimplify.simplify1(y);
// h(x) = k(y)
(h,_) := ExpressionSimplify.simplify(x);
/*
if not Expression.expEqual(inExp1,h) then
print("\nIn: ");print(ExpressionDump.printExpStr(inExp1));print(" = ");print(ExpressionDump.printExpStr(inExp2));
print("\nOut: ");print(ExpressionDump.printExpStr(h));print(" = ");print(ExpressionDump.printExpStr(k));
print("\t w.r.t ");print(ExpressionDump.printExpStr(inExp3));
end if;
*/
end preprocessingSolve;
protected function preprocessingSolve2
"
helprer function for preprocessingSolve
e.g.
x/(x+c1) = -c2 --> x + (x+c1)*c2 = 0
author: Vitalij Ruge
"
input DAE.Exp inExp1 "lhs";
input DAE.Exp inExp2 "rhs";
input DAE.Exp inExp3 "DAE.CREF or 'der(DAE.CREF())'";
output DAE.Exp olhs;
output DAE.Exp orhs;
output Boolean con "continue";
algorithm
(olhs, orhs, con) := matchcontinue (inExp1,inExp2,inExp3)
local
DAE.Exp e,a, b, fb, fa, ga, lhs, rhs;
DAE.Type tp;
DAE.Operator op;
list<DAE.Exp> eWithX, factorWithX, factorWithoutX;
DAE.Exp pWithX, pWithoutX;
// -f(a) = b => f(a) = -b
case(DAE.UNARY(op as DAE.UMINUS(), fa),_,_)
equation
true = expHasCref(fa, inExp3);
false = expHasCref(inExp2, inExp3);
b = DAE.UNARY(op, inExp2);
then(fa, b, true);
case(DAE.UNARY(op as DAE.UMINUS_ARR(), fa),_,_)
equation
true = expHasCref(fa, inExp3);
false = expHasCref(inExp2, inExp3);
b = DAE.UNARY(op, inExp2);
then(fa, b, true);
// b/f(a) = rhs => f(a) = b/rhs solve for a
case(DAE.BINARY(b,DAE.DIV(_),fa), rhs, _)
equation
true = expHasCref(fa, inExp3);
false = expHasCref(b, inExp3);
false = expHasCref(rhs, inExp3);
e = Expression.makeDiv(b, rhs);
then(fa, e, true);
// b*f(a) = rhs => f(a) = rhs/b solve for a
case(DAE.BINARY(b, DAE.MUL(_), fa), rhs, _)
equation
false = expHasCref(b, inExp3);
true = expHasCref(fa, inExp3);
false = expHasCref(rhs, inExp3);
eWithX = Expression.expandFactors(inExp1);
(factorWithX, factorWithoutX) = List.split1OnTrue(eWithX, expHasCref, inExp3);
pWithX = makeProductLstSort(factorWithX);
pWithoutX = makeProductLstSort(factorWithoutX);
e = Expression.makeDiv(rhs, pWithoutX);
then(pWithX, e, true);
// b*a = rhs => a = rhs/b solve for a
case(DAE.BINARY(b, DAE.MUL(_), fa), rhs, _)
equation
false = expHasCref(b, inExp3);
true = expHasCref(fa, inExp3);
false = expHasCref(rhs, inExp3);
e = Expression.makeDiv(rhs, b);
then(fa, e, true);
// a*b = rhs => a = rhs/b solve for a
case(DAE.BINARY(fa, DAE.MUL(_), b), rhs, _)
equation
false = expHasCref(b, inExp3);
true = expHasCref(fa, inExp3);
false = expHasCref(rhs, inExp3);
e = Expression.makeDiv(rhs, b);
then(fa, e, true);
// f(a)/b = rhs => f(a) = rhs*b solve for a
case(DAE.BINARY(fa, DAE.DIV(_), b), rhs, _)
equation
true = expHasCref(fa, inExp3);
false = expHasCref(b, inExp3);
false = expHasCref(rhs, inExp3);
e = Expression.expMul(rhs, b);
then (fa, e, true);
// g(a)/f(a) = rhs => rhs*f(a) - g(a) = 0 solve for a
case(DAE.BINARY(ga, DAE.DIV(tp), fa), rhs, _)
equation
true = expHasCref(fa, inExp3);
true = expHasCref(ga, inExp3);
false = expHasCref(rhs, inExp3);
e = Expression.expMul(rhs, fa);
lhs = Expression.expSub(e, ga);
e = Expression.makeConstZero(tp);
then(lhs, e, true);
else (inExp1, inExp2, false);
end matchcontinue;
end preprocessingSolve2;
protected function preprocessingSolve3
"
helprer function for preprocessingSolve
(r1)^f(a) = r2 => f(a) = ln(r2)/ln(r1)
f(a)^b = 0 => f(a) = 0
f(a)^n = c => f(a) = c^(1/n)
abs(x) = 0
author: Vitalij Ruge
"
input DAE.Exp inExp1 "lhs";
input DAE.Exp inExp2 "rhs";
input DAE.Exp inExp3 "DAE.CREF or 'der(DAE.CREF())'";
output DAE.Exp olhs;
output DAE.Exp orhs;
output Boolean con "continue";
algorithm
(olhs, orhs, con) := matchcontinue(inExp1, inExp2, inExp3)
local
Real r, r1, r2;
DAE.Exp e1, e2, res;
// (r1)^f(a) = r2 => f(a) = ln(r2)/ln(r1)
case (DAE.BINARY(e1 as DAE.RCONST(r1),DAE.POW(_),e2), DAE.RCONST(r2), _)
equation
true = r2 > 0.0;
true = r1 > 0.0;
false = Expression.isConstOne(e1);
true = expHasCref(e2, inExp3);
r = realLn(r2) / realLn(r1);
res = DAE.RCONST(r);
then
(e2, res, true);
// f(a)^b = 0 => f(a) = 0
case (DAE.BINARY(e1,DAE.POW(_),e2), DAE.RCONST(real = 0.0), _)
equation
false = expHasCref(e2, inExp3);
true = expHasCref(e1, inExp3);
then
(e1, inExp2, true);
// f(a)^n = c => f(a) = c^(1/n)
// where n is odd
case (DAE.BINARY(e1,DAE.POW(_),e2 as DAE.RCONST(r)), _, _)
equation
false = expHasCref(inExp2, inExp3);
true = expHasCref(e1, inExp3);
1.0 = realMod(r,2.0);
res = Expression.makeDiv(DAE.RCONST(1.0),e2);
res = Expression.expPow(inExp2,res);
then
(e1, res, true);
// sqrt(f(a)) = f(a)^n = c => f(a) = c^(1/n)
case (DAE.BINARY(e1,DAE.POW(_),e2 as DAE.RCONST(0.5)), _, _)
equation
false = expHasCref(inExp2, inExp3);
true = expHasCref(e1, inExp3);
res = Expression.expPow(inExp2,DAE.RCONST(2.0));
then
(e1, res, true);
// abs(x) = 0
case (DAE.CALL(path = Absyn.IDENT(name = "abs"),expLst = {e1}), DAE.RCONST(0.0),_)
then (e1,inExp2,true);
// sign(x) = 0
case (DAE.CALL(path = Absyn.IDENT(name = "sign"),expLst = {e1}), DAE.RCONST(0.0),_)
then (e1,inExp2,true);
else (inExp1, inExp2, false);
end matchcontinue;
end preprocessingSolve3;
protected function preprocessingSolve4
"
helprer function for preprocessingSolve
e.g.
sqrt(f(x)) - sqrt(g(x))) = 0 = f(x) - g(x)
exp(f(x)) - exp(g(x))) = 0 = f(x) - g(x)
author: Vitalij Ruge
"
input DAE.Exp inExp1;
input DAE.Exp inExp2;
input DAE.Exp inExp3 "DAE.CREF or 'der(DAE.CREF())'";
output DAE.Exp oExp1;
output DAE.Exp oExp2;
output Boolean newX;
algorithm
(oExp1, oExp2, newX) := matchcontinue(inExp1, inExp2, inExp3)
local
String s1,s2;
DAE.Operator op;
DAE.Exp e1,e2,e3,e4, e, e_1, e_2;
DAE.Type tp;
// exp(f(x)) - exp(g(x)) = 0
case(DAE.BINARY(DAE.CALL(path = Absyn.IDENT("exp"), expLst={e1}), op as DAE.SUB(tp),
DAE.CALL(path = Absyn.IDENT("exp"), expLst={e2})),DAE.RCONST(0.0),_)
then (e1, e2, true);
// log(f(x)) - log(g(x)) = 0
case(DAE.BINARY(DAE.CALL(path = Absyn.IDENT("log"), expLst={e1}), op as DAE.SUB(tp),
DAE.CALL(path = Absyn.IDENT("log"), expLst={e2})),DAE.RCONST(0.0),_)
then (e1, e2, true);
// log10(f(x)) - log10(g(x)) = 0
case(DAE.BINARY(DAE.CALL(path = Absyn.IDENT("log10"), expLst={e1}), op as DAE.SUB(tp),
DAE.CALL(path = Absyn.IDENT("log10"), expLst={e2})),DAE.RCONST(0.0),_)
then (e1, e2, true);
// sinh(f(x)) - sinh(g(x)) = 0
case(DAE.BINARY(DAE.CALL(path = Absyn.IDENT("sinh"), expLst={e1}), op as DAE.SUB(tp),
DAE.CALL(path = Absyn.IDENT("sinh"), expLst={e2})),DAE.RCONST(0.0),_)
then (e1, e2, true);
// tanh(f(x)) - tanh(g(x)) = 0
case(DAE.BINARY(DAE.CALL(path = Absyn.IDENT("tanh"), expLst={e1}), op as DAE.SUB(tp),
DAE.CALL(path = Absyn.IDENT("tanh"), expLst={e2})),DAE.RCONST(0.0),_)
then (e1, e2, true);
// sqrt(f(x)) - sqrt(g(x)) = 0
case(DAE.BINARY(DAE.CALL(path = Absyn.IDENT("sqrt"), expLst={e1}), op as DAE.SUB(tp),
DAE.CALL(path = Absyn.IDENT("sqrt"), expLst={e2})),DAE.RCONST(0.0),_)
then (e1, e2, true);
// sinh(f(x)) - cosh(g(x)) = 0
case(DAE.BINARY(DAE.CALL(path = Absyn.IDENT("sinh"), expLst={e1}), op as DAE.SUB(tp),
DAE.CALL(path = Absyn.IDENT("cosh"), expLst={e2})),DAE.RCONST(0.0),_)
equation
true = Expression.expEqual(e1,e2);
then (e1, inExp2, true);
case(DAE.BINARY(DAE.CALL(path = Absyn.IDENT("cosh"), expLst={e1}), op as DAE.SUB(tp),
DAE.CALL(path = Absyn.IDENT("sinh"), expLst={e2})),DAE.RCONST(0.0),_)
equation
true = Expression.expEqual(e1,e2);
then (e1, inExp2, true);
// y*sinh(x) - z*cosh(x) = 0
case(DAE.BINARY(DAE.BINARY(e3,DAE.MUL(),DAE.CALL(path = Absyn.IDENT("sinh"), expLst={e1})), op as DAE.SUB(tp),
DAE.BINARY(e4,DAE.MUL(),DAE.CALL(path = Absyn.IDENT("cosh"), expLst={e2}))),DAE.RCONST(0.0),_)
equation
true = Expression.expEqual(e1,e2);
e = Expression.makePureBuiltinCall("tanh",{e1},tp);
then (Expression.expMul(e3,e), e4, true);
case(DAE.BINARY(DAE.BINARY(e4,DAE.MUL(),DAE.CALL(path = Absyn.IDENT("cosh"), expLst={e2})), op as DAE.SUB(tp),
DAE.BINARY(e3,DAE.MUL(),DAE.CALL(path = Absyn.IDENT("sinh"), expLst={e1}))),DAE.RCONST(0.0),_)
equation
true = Expression.expEqual(e1,e2);
e = Expression.makePureBuiltinCall("tanh",{e1},tp);
then (Expression.expMul(e3,e), e4, true);
// sqrt(x) - x = 0 -> x = x^2
case(DAE.BINARY(DAE.CALL(path = Absyn.IDENT("sqrt"), expLst={e1}), op as DAE.SUB(tp),e2), DAE.RCONST(0.0),_)
then (e1, Expression.expPow(e2, DAE.RCONST(2.0)), true);
case(DAE.BINARY(e2, op as DAE.SUB(tp),DAE.CALL(path = Absyn.IDENT("sqrt"), expLst={e1})), DAE.RCONST(0.0),_)
equation
then (e1, Expression.expPow(e2, DAE.RCONST(2.0)), true);
// f(x)^n - g(x)^n = 0 -> (f(x)/g(x))^n = 1
case(DAE.BINARY(DAE.BINARY(e1, DAE.POW(), e2), DAE.SUB(tp), DAE.BINARY(e3, DAE.POW(), e4)), DAE.RCONST(0.0),_)
equation
true = Expression.expEqual(e2,e4);
true = expHasCref(e1,inExp3);
true = expHasCref(e3,inExp3);
e = Expression.expPow(Expression.makeDiv(e1,e3),e2);
(e_1, e_2, _) = preprocessingSolve3(e, Expression.makeConstOne(tp), inExp3);
then (e_1, e_2, true);
else (inExp1, inExp2, false);
end matchcontinue;
end preprocessingSolve4;
protected function expAddX
"
helprer function for preprocessingSolve
if(y,g(x),h(x)) + x => if(y, g(x) + x, h(x) + x)
a*f(x) + b*f(x) = (a+b)*f(x)
author: Vitalij Ruge
"
input DAE.Exp inExp1 "lhs";
input DAE.Exp inExp2 "rhs";
input DAE.Exp inExp3 "DAE.CREF or 'der(DAE.CREF())'";
output DAE.Exp ores;
algorithm
ores := matchcontinue(inExp1, inExp2, inExp3)
local
DAE.Exp e, e1, e2, e3, e4, res;
case(DAE.IFEXP(e,e1,e2), _,_)
equation
false = expHasCref(e, inExp3);
true = expHasCref(e1, inExp3);
true = expHasCref(e2, inExp3);
e3 = expAddX(inExp2, e1, inExp3);
e4 = expAddX(inExp2, e2, inExp3);
res = DAE.IFEXP(e, e3, e4);
then res;
case(_, DAE.IFEXP(e,e1,e2), _)
equation
false = expHasCref(e, inExp3);
true = expHasCref(e1, inExp3);
true = expHasCref(e2, inExp3);
e3 = expAddX(inExp1, e1, inExp3);
e4 = expAddX(inExp1, e2, inExp3);
res = DAE.IFEXP(e, e3, e4);
then res;
else
equation
res = expAddX2(inExp1, inExp2, inExp3);
then res;
end matchcontinue;
end expAddX;
protected function expAddX2
"
helprer function for preprocessingSolve
a*f(x) + b*f(x) = (a+b)*f(x)
author: Vitalij Ruge
"
input DAE.Exp inExp1 "lhs";
input DAE.Exp inExp2 "rhs";
input DAE.Exp inExp3 "DAE.CREF or 'der(DAE.CREF())'";
output DAE.Exp ores;
protected
list<DAE.Exp> f1, f2;
DAE.Exp e0,e1,e2;
DAE.Boolean neg;
list<DAE.Exp> factorWithX1, factorWithoutX1, factorWithX2, factorWithoutX2;
DAE.Exp pWithX1, pWithoutX1, pWithX2, pWithoutX2;
algorithm
(e0, e1, neg) := match(inExp1)
local DAE.Exp ee1, ee2;
case(DAE.BINARY(ee1,DAE.ADD(),ee2))
then(ee1, ee2, false);
case(DAE.BINARY(ee1,DAE.SUB(),ee2))
then(ee1, ee2, true);
else
then(DAE.RCONST(0.0), inExp1, false);
end match;
f1 := Expression.expandFactors(e1);
(factorWithX1, factorWithoutX1) := List.split1OnTrue(f1, expHasCref, inExp3);
pWithX1 := makeProductLstSort(factorWithX1);
pWithoutX1 := makeProductLstSort(factorWithoutX1);
f2 := Expression.expandFactors(inExp2);
(factorWithX2, factorWithoutX2) := List.split1OnTrue(f2, expHasCref, inExp3);
(pWithX2,_) := ExpressionSimplify.simplify1(makeProductLstSort(factorWithX2));
pWithoutX2 := makeProductLstSort(factorWithoutX2);
//print("\nf1 =");print(ExpressionDump.printExpListStr(f1));
//print("\nf2 =");print(ExpressionDump.printExpListStr(f2));
if Expression.expEqual(pWithX2,pWithX1) then
// e0 + a*x + b*x -> e0 + (a+b)*x
if not neg then
ores := Expression.expAdd(pWithoutX1, pWithoutX2);
else
// e0 - a*x + b*x -> e0 + (b-a)*x
ores := Expression.expSub(pWithoutX2, pWithoutX1);
end if;
ores := Expression.expMul(ores, pWithX2);
elseif Expression.expEqual(pWithX2, Expression.negate(pWithX1)) then
// e0 + a*(-x) + b*x -> e0 + (b-a)*x
if not neg then
ores := Expression.expSub(pWithoutX2, pWithoutX1);
else
// e0 - a*(-x) + b*x -> e0 + (b-a)*x
ores := Expression.expAdd(pWithoutX1, pWithoutX2);
end if;
ores := Expression.expMul(ores, pWithX2);
else
e1 := Expression.expMul(pWithoutX1, pWithX1);
e2 := Expression.expMul(pWithoutX2, pWithX2);
ores := Expression.expAdd(e1,e2);
end if;
ores := Expression.expAdd(e0,ores);
end expAddX2;
protected function preprocessingSolve5
"
helprer function for preprocessingSolve
split and sort with respect to x
where x = cref
f(x,y) = {h(y)*g(x,y), k(y)}
author: Vitalij Ruge
"
input DAE.Exp inExp1 "lhs";
input DAE.Exp inExp3 "DAE.CREF or 'der(DAE.CREF())'";
input DAE.Boolean expand;
output DAE.Exp outLhs := DAE.RCONST(0.0);
output DAE.Exp outRhs;
protected
DAE.Exp res;
list<DAE.Exp> lhs, rhs, resTerms;
algorithm
//can be improve with Expression.getTermsContainingX ???
if expHasCref(inExp1, inExp3) then
resTerms := Expression.terms(inExp1);
// split
(lhs, rhs) := List.split1OnTrue(resTerms, expHasCref, inExp3);
//print("\nlhs =");print(ExpressionDump.printExpListStr(lhs));
//print("\nrhs =");print(ExpressionDump.printExpListStr(rhs));
// sort
// a*f(x)*b -> c*f(x)
for e in lhs loop
outLhs := expAddX(e, outLhs, inExp3); // special add
end for;
//rhs
outRhs := Expression.makeSum(rhs);
(outRhs,_) := ExpressionSimplify.simplify1(outRhs);
if expand then
resTerms := Expression.terms(Expression.expand(outLhs));
(lhs, rhs) := List.split1OnTrue(resTerms, expHasCref, inExp3);
outLhs := DAE.RCONST(0.0);
// sort
// a*f(x)*b -> c*f(x)
for e in lhs loop
outLhs := expAddX(e, outLhs, inExp3); // special add
end for;
//rhs
outRhs := Expression.expAdd(outRhs,Expression.makeSum(rhs));
(outRhs,_) := ExpressionSimplify.simplify1(outRhs);
resTerms := Expression.allTerms(outLhs);
(lhs, rhs) := List.split1OnTrue(resTerms, expHasCref, inExp3);
// sort
// a*f(x)*b -> c*f(x)
outLhs := DAE.RCONST(0.0);
for e in lhs loop
outLhs := expAddX(e, outLhs, inExp3); // special add
end for;
//rhs
outRhs := Expression.expAdd(outRhs,Expression.makeSum(rhs));
(outRhs,_) := ExpressionSimplify.simplify1(outRhs);
end if;
else
outRhs := inExp1;
end if;
end preprocessingSolve5;
protected function unifyFunCalls
"
e.g.
smooth() -> if
semiLinear() -> if
author: Vitalij Ruge
"
input DAE.Exp inExp1 "lhs";