/
Differentiate.mo
3180 lines (2778 loc) · 132 KB
/
Differentiate.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 Differentiate
" file: Differentiate.mo
package: Differentiate
description: Differentiation of equations from BackendDAE.BackendDAE
This module is responsible for symbolic differentiation of equations and
expressions.
The symbolic differentiation is used in the Newton-Raphson method and in
index reduction."
// public imports
public import Absyn;
public import BackendDAE;
public import DAE;
public import DAEUtil;
public import Types;
// protected imports
protected import Algorithm;
protected import Array;
protected import BackendDump;
protected import BackendDAECreate;
protected import BackendDAEUtil;
protected import BackendEquation;
protected import BackendVariable;
protected import ClassInf;
protected import ComponentReference;
protected import DAEDump;
protected import DAEDumpTpl;
protected import Debug;
protected import ElementSource;
protected import Error;
protected import Expression;
protected import ExpressionSimplify;
protected import ExpressionDump;
protected import Flags;
protected import Inline;
protected import List;
protected import SCode;
protected import Util;
constant Integer defaultMaxIter = 20;
// =============================================================================
// differentiation interfaces:
// - createDifferentiatedCrefName
// - createSeedCrefName
// - differentiateEquation
// - differentiateEquationTime
// - differentiateExpCrefFullJacobian
// - differentiateExpSolve
// - differentiateExpTime
// =============================================================================
public function differentiateEquationTime
"Differentiates an equation with respect to time.
Returns NONE() if it was not possible to calculate a derivative."
input BackendDAE.Equation inEquation;
input BackendDAE.Variables inVariables;
input BackendDAE.Shared inShared;
output Option<BackendDAE.Equation> outEquation;
output BackendDAE.Shared outShared = inShared;
protected
BackendDAE.DifferentiateInputData diffData;
BackendDAE.Equation eqn;
BackendDAE.Variables knvars;
DAE.ElementSource source;
DAE.FunctionTree funcs;
algorithm
try
if Flags.isSet(Flags.DEBUG_DIFFERENTIATION) then
BackendDump.debugStrEqnStr("### differentiateEquationTime\n", inEquation, " w.r.t. time\n");
end if;
funcs := BackendDAEUtil.getFunctions(inShared);
knvars := BackendDAEUtil.getGlobalKnownVarsFromShared(inShared);
diffData := BackendDAE.emptyInputData;
diffData.dependenentVars := SOME(inVariables);
diffData.knownVars := SOME(knvars);
diffData.allVars := SOME(inVariables);
(eqn, funcs) := differentiateEquation(inEquation, DAE.crefTime, diffData, BackendDAE.DIFFERENTIATION_TIME(), funcs);
outEquation := SOME(eqn);
outShared := BackendDAEUtil.setSharedFunctionTree(inShared, funcs);
if Flags.isSet(Flags.DEBUG_DIFFERENTIATION) then
BackendDump.debugStrEqnStr("### Result of differentiateEquationTime\n --> ", eqn, "\n");
end if;
else
source := BackendEquation.equationSource(inEquation);
Error.addSourceMessage(Error.INTERNAL_ERROR, {"\nDifferentiate.differentiateEquationTime failed for " + BackendDump.equationString(inEquation) + "\n\n"}, ElementSource.getElementSourceFileInfo(source));
outEquation := NONE();
end try;
end differentiateEquationTime;
public function differentiateExpTime
"Differentiates an expression with respect to time."
input DAE.Exp inExp;
input BackendDAE.Variables inVariables;
input BackendDAE.Shared inShared;
output DAE.Exp outExp;
output BackendDAE.Shared outShared;
protected
DAE.Exp dexp;
DAE.FunctionTree funcs;
BackendDAE.DifferentiateInputData diffData;
BackendDAE.Variables knvars;
algorithm
try
if Flags.isSet(Flags.DEBUG_DIFFERENTIATION) then
BackendDump.debugStrExpStr("### differentiateExpTime\n ", inExp, " w.r.t. time\n");
end if;
funcs := BackendDAEUtil.getFunctions(inShared);
knvars := BackendDAEUtil.getGlobalKnownVarsFromShared(inShared);
diffData := BackendDAE.emptyInputData;
diffData.dependenentVars := SOME(inVariables);
diffData.knownVars := SOME(knvars);
(dexp, funcs) := differentiateExp(inExp, DAE.crefTime, diffData, BackendDAE.DIFFERENTIATION_TIME(), funcs, defaultMaxIter);
(outExp, _) := ExpressionSimplify.simplify(dexp);
outShared := BackendDAEUtil.setSharedFunctionTree(inShared, funcs);
if Flags.isSet(Flags.DEBUG_DIFFERENTIATION) then
BackendDump.debugStrExpStr("### Result of differentiateExpTime\n --> ", outExp, "n");
end if;
else
// expandDerOperator expects sometime that differentiate fails,
// so the calling function need to take care of the error messages.
// TODO: change that in expandDerOperator
//Error.addSourceMessage(Error.INTERNAL_ERROR, {msg}, ElementSource.getElementSourceFileInfo(DAE.emptyElementSource));
if Flags.isSet(Flags.FAILTRACE) then
Error.addSourceMessage(Error.NON_EXISTING_DERIVATIVE, {ExpressionDump.printExpStr(inExp), "time"}, sourceInfo());
end if;
fail();
end try;
end differentiateExpTime;
public function differentiateExpSolve
"Differentiates an expression with respect to inCref."
input DAE.Exp inExp;
input DAE.ComponentRef inCref;
input Option<DAE.FunctionTree> functions;
output DAE.Exp outExp;
protected
list<DAE.Exp> fac = Expression.factors(inExp);
DAE.Exp dexp;
DAE.FunctionTree fun;
algorithm
({}, _) := List.split1OnTrue(fac, Expression.expHasCrefInIf, inCref); // check if differentiateExpSolve is allowed
try
fun := match(functions)
local
DAE.FunctionTree fun_;
case SOME(fun_) then fun_;
else DAE.AvlTreePathFunction.Tree.EMPTY();
end match;
if Flags.isSet(Flags.DEBUG_DIFFERENTIATION) then
BackendDump.debugStrExpStrCrefStr("### differentiateExpSolve\n ", inExp, " w.r.t. ", inCref, "\n");
end if;
(dexp, _) := differentiateExp(inExp, inCref, BackendDAE.emptyInputData, BackendDAE.SIMPLE_DIFFERENTIATION(), fun, defaultMaxIter);
(outExp, _) := ExpressionSimplify.simplify(dexp);
if Flags.isSet(Flags.DEBUG_DIFFERENTIATION) then
BackendDump.debugStrExpStr("### Result of differentiateExpSolve\n --> ", outExp, "\n");
end if;
else
if Flags.isSet(Flags.FAILTRACE) then
Error.addSourceMessage(Error.NON_EXISTING_DERIVATIVE, {ExpressionDump.printExpStr(inExp), ComponentReference.crefStr(inCref)}, sourceInfo());
end if;
fail();
end try;
end differentiateExpSolve;
public function differentiateExpCrefFullJacobian
"Differentiates an expression inExp with respect to inCref."
input DAE.Exp inExp;
input DAE.ComponentRef inCref;
input BackendDAE.Variables inVariables;
input BackendDAE.Shared inShared;
output DAE.Exp outExp;
output BackendDAE.Shared outShared;
protected
DAE.Exp dexp;
DAE.FunctionTree funcs;
BackendDAE.DifferentiateInputData diffData;
BackendDAE.Variables knvars;
algorithm
try
funcs := BackendDAEUtil.getFunctions(inShared);
knvars := BackendDAEUtil.getGlobalKnownVarsFromShared(inShared);
diffData := BackendDAE.emptyInputData;
diffData.dependenentVars := SOME(inVariables);
diffData.knownVars := SOME(knvars);
(dexp, funcs) := differentiateExp(inExp, inCref, diffData, BackendDAE.DIFF_FULL_JACOBIAN(), funcs, defaultMaxIter);
(outExp,_) := ExpressionSimplify.simplify(dexp);
outShared := BackendDAEUtil.setSharedFunctionTree(inShared, funcs);
else
// expandDerOperator expects sometimes that differentiate fails,
// so the calling function need to take care of the error messages.
// TODO: change that in expandDerOperator
//Error.addSourceMessage(Error.INTERNAL_ERROR, {msg}, ElementSource.getElementSourceFileInfo(DAE.emptyElementSource));
if Flags.isSet(Flags.FAILTRACE) then
Error.addSourceMessage(Error.NON_EXISTING_DERIVATIVE, {ExpressionDump.printExpStr(inExp), ComponentReference.crefStr(inCref)}, sourceInfo());
end if;
fail();
end try;
end differentiateExpCrefFullJacobian;
// =============================================================================
// further interface functions to differentiation
// - differentiateEquation
// - differentiateBackendDAE
//
// =============================================================================
public function differentiateEquation
"Differentiates an equation with respect to a cref."
input BackendDAE.Equation inEquation;
input DAE.ComponentRef inDiffwrtCref;
input BackendDAE.DifferentiateInputData inInputData;
input BackendDAE.DifferentiationType inDiffType;
input DAE.FunctionTree inFunctionTree;
output BackendDAE.Equation outEquation;
output DAE.FunctionTree outFunctionTree;
algorithm
try
// Debug dump
if Flags.isSet(Flags.DEBUG_DIFFERENTIATION) then
BackendDump.debugStrEqnStr("### differentiateEquation\n ", inEquation, " w.r.t. " + ComponentReference.crefStr(inDiffwrtCref) + "\n");
end if;
(outEquation, outFunctionTree) := match inEquation
local
DAE.Exp e1_1, e2_1, e1_2, e2_2, e1, e2;
DAE.ComponentRef cref;
DAE.ElementSource source;
Integer size;
list<DAE.Exp> out1, expExpLst, expExpLst1;
DAE.Type exptyp;
list<Integer> dimSize;
String se1, dse1, se2, dse2;
DAE.SymbolicOperation op1, op2;
DAE.FunctionTree funcs;
DAE.Algorithm alg;
list<BackendDAE.Equation> eqns;
list<list<BackendDAE.Equation>> eqnslst;
list<DAE.Statement> statementLst;
DAE.Expand expand;
BackendDAE.WhenEquation whenEqn;
BackendDAE.EquationAttributes eqAttr;
// equations
case BackendDAE.EQUATION(exp=e1, scalar=e2, source=source, attr=eqAttr)
equation
(e1_1, funcs) = differentiateExp(e1, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, defaultMaxIter);
(e1_1, _) = ExpressionSimplify.simplify(e1_1);
(e2_1, funcs) = differentiateExp(e2, inDiffwrtCref, inInputData, inDiffType, funcs, defaultMaxIter);
(e2_1, _) = ExpressionSimplify.simplify(e2_1);
op1 = DAE.OP_DIFFERENTIATE(inDiffwrtCref, e1, e1_1);
op2 = DAE.OP_DIFFERENTIATE(inDiffwrtCref, e2, e2_1);
source = List.foldr({op1, op2}, ElementSource.addSymbolicTransformation, source);
then
(BackendDAE.EQUATION(e1_1, e2_1, source, eqAttr), funcs);
// solved equations
case BackendDAE.SOLVED_EQUATION(componentRef=cref, exp=e2, source=source, attr=eqAttr)
equation
e1 = Expression.crefExp(cref);
(e1_1, funcs) = differentiateExp(e1, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, defaultMaxIter);
(e1_1, _) = ExpressionSimplify.simplify(e1_1);
(e2_1, funcs) = differentiateExp(e2, inDiffwrtCref, inInputData, inDiffType, funcs, defaultMaxIter);
(e2_1, _) = ExpressionSimplify.simplify(e2_1);
op1 = DAE.OP_DIFFERENTIATE(inDiffwrtCref, e1, e1_1);
op2 = DAE.OP_DIFFERENTIATE(inDiffwrtCref, e2, e2_1);
source = List.foldr({op1, op2}, ElementSource.addSymbolicTransformation, source);
then
(BackendDAE.EQUATION(e1_1, e2_1, source, eqAttr), funcs);
// RESIDUAL_EQUATION
case BackendDAE.RESIDUAL_EQUATION(exp=e1, source=source, attr=eqAttr)
equation
(e1_1, funcs) = differentiateExp(e1, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, defaultMaxIter);
(e1_1, _) = ExpressionSimplify.simplify(e1_1);
op1 = DAE.OP_DIFFERENTIATE(inDiffwrtCref, e1, e1_1);
source = List.foldr({op1}, ElementSource.addSymbolicTransformation, source);
then
(BackendDAE.RESIDUAL_EQUATION(e1_1, source, eqAttr), funcs);
// complex equations
case BackendDAE.COMPLEX_EQUATION(size=size, left=e1, right=e2, source=source, attr=eqAttr)
equation
(e1_1, funcs) = differentiateExp(e1, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, defaultMaxIter);
(e1_1, _) = ExpressionSimplify.simplify(e1_1);
(e2_1, funcs) = differentiateExp(e2, inDiffwrtCref, inInputData, inDiffType, funcs, defaultMaxIter);
(e2_1, _) = ExpressionSimplify.simplify(e2_1);
op1 = DAE.OP_DIFFERENTIATE(inDiffwrtCref, e1, e1_1);
op2 = DAE.OP_DIFFERENTIATE(inDiffwrtCref, e2, e2_1);
source = List.foldr({op1, op2}, ElementSource.addSymbolicTransformation, source);
then
(BackendDAE.COMPLEX_EQUATION(size, e1_1, e2_1, source, eqAttr), funcs);
// Array Equations
case BackendDAE.ARRAY_EQUATION(dimSize=dimSize, left=e1, right=e2, source=source, attr=eqAttr)
equation
(e1_1, funcs) = differentiateExp(e1, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, defaultMaxIter);
(e1_1, _) = ExpressionSimplify.simplify(e1_1);
(e2_1, funcs) = differentiateExp(e2, inDiffwrtCref, inInputData, inDiffType, funcs, defaultMaxIter);
(e2_1, _) = ExpressionSimplify.simplify(e2_1);
op1 = DAE.OP_DIFFERENTIATE(inDiffwrtCref, e1, e1_1);
op2 = DAE.OP_DIFFERENTIATE(inDiffwrtCref, e2, e2_1);
source = List.foldr({op1, op2}, ElementSource.addSymbolicTransformation, source);
then
(BackendDAE.ARRAY_EQUATION(dimSize, e1_1, e2_1, source, eqAttr), funcs);
// differentiate algorithm
case BackendDAE.ALGORITHM(size=size, alg=DAE.ALGORITHM_STMTS(statementLst=statementLst), source=source, expand=expand, attr=eqAttr)
equation
// get Allgorithm
(statementLst, funcs) = differentiateStatements(statementLst, inDiffwrtCref, inInputData, inDiffType, {}, inFunctionTree, defaultMaxIter);
alg = DAE.ALGORITHM_STMTS(statementLst);
//op1 = DAE.OP_DIFFERENTIATE(cr, before, after)
//op1 = DAE.OP_DIFFERENTIATE(inDiffwrtCref, e1, e2);
//source = ElementSource.addSymbolicTransformation(source, op1);
then
(BackendDAE.ALGORITHM(size, alg, source, expand, eqAttr), funcs);
// if-equations
case BackendDAE.IF_EQUATION(conditions=expExpLst, eqnstrue=eqnslst, eqnsfalse=eqns, source=source, attr=eqAttr)
equation
(eqnslst, funcs) = differentiateEquationsLst(eqnslst, inDiffwrtCref, inInputData, inDiffType, {}, inFunctionTree);
(eqns, funcs) = differentiateEquations(eqns, inDiffwrtCref, inInputData, inDiffType, {}, funcs);
then
(BackendDAE.IF_EQUATION(expExpLst, eqnslst, eqns, source, eqAttr), funcs);
case BackendDAE.WHEN_EQUATION(size=size, whenEquation=whenEqn, source=source, attr=eqAttr)
equation
(whenEqn, funcs) = differentiateWhenEquations(whenEqn, inDiffwrtCref, inInputData, inDiffType, inFunctionTree);
then
(BackendDAE.WHEN_EQUATION(size, whenEqn, source, eqAttr), funcs);
else equation
Error.addSourceMessage(Error.NON_EXISTING_DERIVATIVE, {BackendDump.equationString(inEquation), ComponentReference.crefStr(inDiffwrtCref)}, sourceInfo());
then fail();
end match;
// Debug dump
if Flags.isSet(Flags.DEBUG_DIFFERENTIATION) then
BackendDump.debugStrEqnStr("### Result of differentiateEquation\n --> ", outEquation,"\n");
end if;
else
Error.addSourceMessage(Error.NON_EXISTING_DERIVATIVE, {BackendDump.equationString(inEquation), ComponentReference.crefStr(inDiffwrtCref)}, sourceInfo());
fail();
end try;
end differentiateEquation;
protected function differentiateEquations
"Differentiates an equation with respect to a cref."
input list<BackendDAE.Equation> inEquations;
input DAE.ComponentRef inDiffwrtCref;
input BackendDAE.DifferentiateInputData inInputData;
input BackendDAE.DifferentiationType inDiffType;
input list<BackendDAE.Equation> inEquationsAccum;
input DAE.FunctionTree inFunctionTree;
output list<BackendDAE.Equation> outEquations;
output DAE.FunctionTree outFunctionTree;
algorithm
(outEquations,outFunctionTree) := matchcontinue (inEquations)
local
DAE.FunctionTree funcs;
list<BackendDAE.Equation> rest, eqns;
BackendDAE.Equation eqn;
case {} then (listReverse(inEquationsAccum), inFunctionTree);
// equations
case eqn::rest
equation
(eqn, funcs) = differentiateEquation(eqn, inDiffwrtCref, inInputData, inDiffType, inFunctionTree);
eqns = eqn::inEquationsAccum;
(eqns, funcs) = differentiateEquations(rest, inDiffwrtCref, inInputData, inDiffType, eqns, funcs);
then (eqns, funcs);
case eqn::_
equation
Error.addSourceMessage(Error.NON_EXISTING_DERIVATIVE, {BackendDump.equationString(eqn), ComponentReference.crefStr(inDiffwrtCref)}, sourceInfo());
then
fail();
end matchcontinue;
end differentiateEquations;
protected function differentiateEquationsLst
"Differentiates a list of an equation list with respect to a cref.
Helper function to differentiate a IF-EQUATION."
input list<list<BackendDAE.Equation>> inEquationsLst;
input DAE.ComponentRef inDiffwrtCref;
input BackendDAE.DifferentiateInputData inInputData;
input BackendDAE.DifferentiationType inDiffType;
input list<list<BackendDAE.Equation>> inEquationsLstAccum;
input DAE.FunctionTree inFunctionTree;
output list<list<BackendDAE.Equation>> outEquationsLst;
output DAE.FunctionTree outFunctionTree;
algorithm
(outEquationsLst, outFunctionTree) := matchcontinue inEquationsLst
local
DAE.FunctionTree funcs;
list<list<BackendDAE.Equation>> rest, eqnsLst;
list<BackendDAE.Equation> eqns;
String msg;
DAE.ElementSource source;
case {} then (listReverse(inEquationsLstAccum), inFunctionTree);
// equations
case eqns::rest
equation
(eqns, funcs) = differentiateEquations(eqns, inDiffwrtCref, inInputData, inDiffType, {}, inFunctionTree);
eqnsLst = eqns::inEquationsLstAccum;
(eqnsLst, funcs) = differentiateEquationsLst(rest, inDiffwrtCref, inInputData, inDiffType, eqnsLst, funcs);
then (eqnsLst, funcs);
case eqns::_
equation
Error.addSourceMessage(Error.NON_EXISTING_DERIVATIVE, {BackendDump.equationListString(eqns, "equation list"), ComponentReference.crefStr(inDiffwrtCref)}, sourceInfo());
then
fail();
end matchcontinue;
end differentiateEquationsLst;
protected function differentiateWhenEquations
"Differentiates a when equation with respect to a cref."
input BackendDAE.WhenEquation inWhenEquations;
input DAE.ComponentRef inDiffwrtCref;
input BackendDAE.DifferentiateInputData inInputData;
input BackendDAE.DifferentiationType inDiffType;
input DAE.FunctionTree inFunctionTree;
output BackendDAE.WhenEquation outWhenEquations;
output DAE.FunctionTree outFunctionTree;
protected
BackendDAE.WhenEquation elsewhenPart, delsewhenPart;
Option<BackendDAE.WhenEquation> oelsepart;
list<BackendDAE.WhenOperator> whenStmtLst, stmtLst;
DAE.FunctionTree funcs;
DAE.Exp condition;
algorithm
BackendDAE.WHEN_STMTS(condition = condition, whenStmtLst = whenStmtLst, elsewhenPart = oelsepart) := inWhenEquations;
funcs := inFunctionTree;
stmtLst := {};
for rs in whenStmtLst loop
rs := match(rs)
local
DAE.ElementSource src;
DAE.Exp right, dright, eleft, dleft;
case BackendDAE.ASSIGN(eleft, right, src) equation
(dleft, funcs) = differentiateExp(eleft, inDiffwrtCref, inInputData, inDiffType, funcs, defaultMaxIter);
(dright, funcs) = differentiateExp(right, inDiffwrtCref, inInputData, inDiffType, funcs, defaultMaxIter);
then BackendDAE.ASSIGN(dleft, dright, src);
else rs;
end match;
stmtLst := rs::stmtLst;
end for;
if isSome(oelsepart) then
SOME(elsewhenPart) := oelsepart;
(delsewhenPart, funcs) := differentiateWhenEquations(elsewhenPart, inDiffwrtCref, inInputData, inDiffType, funcs);
oelsepart := SOME(delsewhenPart);
else
oelsepart := NONE();
end if;
outWhenEquations := BackendDAE.WHEN_STMTS(condition, stmtLst, oelsepart);
outFunctionTree := funcs;
end differentiateWhenEquations;
// =============================================================================
// main differentiation functions
// - differentiateExp
// - differentiateStatements
//
// =============================================================================
protected function differentiateExp
input DAE.Exp inExp;
input DAE.ComponentRef inDiffwrtCref;
input BackendDAE.DifferentiateInputData inInputData;
input BackendDAE.DifferentiationType inDiffType;
input DAE.FunctionTree inFunctionTree;
input Integer maxIter;
output DAE.Exp outDiffedExp;
output DAE.FunctionTree outFunctionTree;
protected
constant Boolean debug = false;
algorithm
if debug then print("\nDifferentiate Exp: "+ExpressionDump.printExpStr(inExp)+
" w.r.t. "+ComponentReference.printComponentRefStr(inDiffwrtCref)+"\n"); end if;
/*
// This check does not seem to be necessary since looking through the stack of expression seems to stop iteration in most cases, and you get a spam of messages from this check.
if maxIter < 1 then
Error.addInternalError("Differentiation reached maximum number of iterations ("+String(defaultMaxIter)+"). Current expression is: " + ExpressionDump.printExpStr(inExp) + " w.r.t. " + ComponentReference.printComponentRefStr(inDiffwrtCref), sourceInfo());
fail();
end if;
*/
(outDiffedExp, outFunctionTree) := match inExp
local
Absyn.Path p, p1, p2;
Boolean b;
DAE.CallAttributes attr;
DAE.Exp e1, e2, e3, actual, simplified;
DAE.Exp res, res1, res2;
DAE.FunctionTree functionTree;
DAE.Operator op;
DAE.Type tp;
Integer i;
String s1, s2, stp;
list<String> strLst;
list<DAE.Var> varLst;
//String se1;
list<DAE.Exp> sub, expl;
list<list<DAE.Exp>> matrix, dmatrix;
DAE.ComponentRef cref;
// constants => results in zero
case DAE.BCONST(bool=b) then (DAE.BCONST(b), inFunctionTree);
case DAE.ICONST() then (DAE.ICONST(0), inFunctionTree);
case DAE.RCONST() then (DAE.RCONST(0.0), inFunctionTree);
case DAE.SCONST() then (inExp, inFunctionTree);
case DAE.RECORD(path = p, exps = expl, comp = strLst, ty=tp)
algorithm
sub := {};
functionTree := inFunctionTree;
for e in expl loop
(e1, functionTree) := differentiateExp(e,inDiffwrtCref, inInputData, inDiffType, functionTree, maxIter);
sub := e1 :: sub;
end for;
then (DAE.RECORD(p, listReverse(sub), strLst, tp), functionTree);
// differentiate cref
case DAE.CREF(componentRef=cref, ty=tp) equation
if ComponentReference.isStartCref(cref) then
// differentiate start value
res = Expression.makeConstZero(tp);
functionTree = inFunctionTree;
else
(res, functionTree) = differentiateCrefs(inExp, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter-1);
end if;
then (res, functionTree);
// differentiate homotopy
case DAE.CALL(path=p as Absyn.IDENT(name="homotopy"), expLst={actual, simplified}, attr=attr) equation
(e1, functionTree) = differentiateExp(actual, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter);
(e2, functionTree) = differentiateExp(simplified, inDiffwrtCref, inInputData, inDiffType, functionTree, maxIter);
res = DAE.CALL(p, {e1, e2}, attr);
then (res, functionTree);
// differentiate call
case DAE.CALL() equation
(res, functionTree) = differentiateCalls(inExp, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter-1);
(res,_) = ExpressionSimplify.simplify1(res);
then (res, functionTree);
// differentiate binary
case DAE.BINARY() equation
(res, functionTree) = differentiateBinary(inExp, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter-1);
(res) = ExpressionSimplify.simplifyBinaryExp(res);
then (res, functionTree);
// differentiate operator
case DAE.UNARY(operator=op, exp=e1) equation
(res, functionTree) = differentiateExp(e1, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter-1);
res = DAE.UNARY(op,res);
(res) = ExpressionSimplify.simplifyUnaryExp(res);
then (res, functionTree);
// differentiate cast
case DAE.CAST(ty=tp, exp=e1) equation
(res, functionTree) = differentiateExp(e1, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter-1);
(res,_) = ExpressionSimplify.simplify1(res);
then (DAE.CAST(tp, res), functionTree);
// differentiate asub
case DAE.ASUB(exp=e1, sub=sub) equation
(res1, functionTree) = differentiateExp(e1, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter-1);
res = Expression.makeASUB(res1,sub);
(res,_) = ExpressionSimplify.simplify1(res);
then (res, functionTree);
case DAE.ARRAY(ty=tp, scalar=b, array=expl) equation
(expl, functionTree) = List.map3Fold(expl, function differentiateExp(maxIter=maxIter-1), inDiffwrtCref, inInputData, inDiffType, inFunctionTree);
res = DAE.ARRAY(tp, b, expl);
(res,_) = ExpressionSimplify.simplify1(res);
then (res, functionTree);
case DAE.MATRIX(ty=tp, integer=i, matrix=matrix) equation
(dmatrix, functionTree) = List.map3FoldList(matrix, function differentiateExp(maxIter=maxIter-1), inDiffwrtCref, inInputData, inDiffType, inFunctionTree);
res = DAE.MATRIX(tp, i, dmatrix);
(res,_) = ExpressionSimplify.simplify1(res);
then (res, functionTree);
// differentiate tsub
case DAE.TSUB(exp=e1, ix=i, ty=tp)
algorithm
(res1, functionTree) := differentiateExp(e1, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter-1);
if not referenceEq(e1, res1) then
res := DAE.TSUB(res1, i, tp);
(res,_) := ExpressionSimplify.simplify1(res);
else
res := inExp;
end if;
then (res, functionTree);
// differentiate rsub
case e1 as DAE.RSUB()
algorithm
(res1, functionTree) := differentiateExp(e1.exp, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter-1);
if not referenceEq(e1.exp, res1) then
try
(expl, strLst) := match res1
case DAE.RECORD(exps=expl,comp=strLst) then (expl, strLst);
case DAE.CALL(path=p1,expLst=expl,attr=DAE.CALL_ATTR(ty=DAE.T_COMPLEX(complexClassType=ClassInf.RECORD(path=p2), varLst=varLst)))
guard Absyn.pathEqual(p1,p2)
then (expl, list(v.name for v in varLst));
end match;
res := listGet(expl, List.position1OnTrue(strLst, stringEq, e1.fieldName));
else
e1.exp := res1;
(res,_) := ExpressionSimplify.simplify1(e1);
end try;
end if;
then (res, functionTree);
// differentiate tuple
case DAE.TUPLE(PR=expl) equation
(expl, functionTree) = List.map3Fold(expl, function differentiateExp(maxIter=maxIter-1), inDiffwrtCref, inInputData, inDiffType, inFunctionTree);
res = DAE.TUPLE(expl);
(res,_) = ExpressionSimplify.simplify1(res);
then (res, functionTree);
case DAE.IFEXP(expCond=e1, expThen=e2, expElse=e3) equation
(res1, functionTree) = differentiateExp(e2, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter-1);
(res2, functionTree) = differentiateExp(e3, inDiffwrtCref, inInputData, inDiffType, functionTree, maxIter-1);
res = DAE.IFEXP(e1, res1, res2);
(res,_) = ExpressionSimplify.simplify1(res);
then (res, functionTree);
// boolean expression, e.g. relation, are left as they are
case DAE.RELATION()
then (inExp, inFunctionTree);
case DAE.LBINARY()
then (inExp, inFunctionTree);
case DAE.LUNARY()
then (inExp, inFunctionTree);
case DAE.SIZE()
then (inExp, inFunctionTree);
case DAE.RANGE()
then (inExp, inFunctionTree);
else equation
true = Flags.isSet(Flags.FAILTRACE);
s1 = ExpressionDump.printExpStr(inExp);
s2 = ComponentReference.printComponentRefStr(inDiffwrtCref);
stp = Types.printTypeStr(Expression.typeof(inExp));
Debug.trace("- differentiateExp " + s1 + " type: " + stp + " w.r.t " + s2 + " failed\n");
then fail();
end match;
if debug then print("Differentiate-Exp-result: " + ExpressionDump.printExpStr(outDiffedExp) + "\n"); end if;
end differentiateExp;
protected function differentiateStatements
input list<DAE.Statement> inStmts;
input DAE.ComponentRef inDiffwrtCref;
input BackendDAE.DifferentiateInputData inInputData;
input BackendDAE.DifferentiationType inDiffType;
input list<DAE.Statement> inStmtsAccum;
input DAE.FunctionTree inFunctionTree;
input Integer maxIter;
output list<DAE.Statement> outDiffedStmts;
output DAE.FunctionTree outFunctionTree;
algorithm
(outDiffedStmts, outFunctionTree) := matchcontinue inStmts
local
DAE.ComponentRef cref;
DAE.ElementSource source;
DAE.Exp lhs, rhs, derivedLHS, derivedRHS;
DAE.FunctionTree functions;
DAE.Statement currStatement, stmt, dstmt;
DAE.Type type_;
BackendDAE.DifferentiateInputData inputData;
BackendDAE.Var controlVar;
Boolean iterIsArray,initialCall;
DAE.Ident ident;
DAE.Exp exp;
DAE.Exp elseif_exp;
DAE.Else elseif_else_;
list<DAE.Exp> expLst, dexpLst, expLstRHS;
Integer index;
list<tuple<DAE.Exp, DAE.Exp>> exptl;
list<DAE.Statement> statementLst, restStatements, derivedStatements1, derivedStatements2, else_statementLst, elseif_statementLst;
String s1,s2;
list<Option<DAE.Statement>> optDerivedStatements1;
case {} then (listReverse(inStmtsAccum), inFunctionTree);
case (currStatement as DAE.STMT_ASSIGN(type_=type_, exp1=lhs, exp=rhs, source=source))::restStatements
equation
(derivedLHS, functions) = differentiateExp(lhs, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter);
(derivedRHS, functions) = differentiateExp(rhs, inDiffwrtCref, inInputData, inDiffType, functions, maxIter);
(derivedRHS,_) = ExpressionSimplify.simplify(derivedRHS);
/* if assigment is empty remove derived assignment */
if (Expression.isZero(derivedLHS)) then
derivedStatements1 = {currStatement};
else
derivedStatements1 = {DAE.STMT_ASSIGN(type_, derivedLHS, derivedRHS, source), currStatement};
end if;
derivedStatements1 = listAppend(derivedStatements1, inStmtsAccum);
(derivedStatements2, functions) = differentiateStatements(restStatements, inDiffwrtCref, inInputData, inDiffType, derivedStatements1, functions, maxIter);
then (derivedStatements2, functions);
case (currStatement as DAE.STMT_TUPLE_ASSIGN(expExpLst= expLst, exp=rhs, source=source))::restStatements
equation
(dexpLst,functions) = List.map3Fold(expLst, function differentiateExp(maxIter=maxIter), inDiffwrtCref, inInputData, inDiffType, inFunctionTree);
(derivedRHS as DAE.TUPLE(expLstRHS), functions) = differentiateExp(rhs, inDiffwrtCref, inInputData, inDiffType, functions, maxIter);
(DAE.TUPLE(expLstRHS),_) = ExpressionSimplify.simplify(derivedRHS);
exptl = List.threadTuple(dexpLst, expLstRHS);
optDerivedStatements1 = List.map2(exptl, makeAssignmentfromTuple, source, inFunctionTree);
derivedStatements1 = List.flatten(List.map(optDerivedStatements1, List.fromOption));
derivedStatements1 = listAppend(derivedStatements1, {currStatement});
derivedStatements1 = listAppend(derivedStatements1, inStmtsAccum);
(derivedStatements2, functions) = differentiateStatements(restStatements, inDiffwrtCref, inInputData, inDiffType, derivedStatements1, functions, maxIter);
then (derivedStatements2, functions);
case (currStatement as DAE.STMT_TUPLE_ASSIGN(expExpLst=expLst, exp=rhs as DAE.CALL(), type_= type_, source=source))::restStatements
equation
(dexpLst,functions) = List.map3Fold(expLst, function differentiateExp(maxIter=maxIter), inDiffwrtCref, inInputData, inDiffType, inFunctionTree);
(derivedRHS as DAE.CALL(attr=DAE.CALL_ATTR(ty=type_)), functions) = differentiateExp(rhs, inDiffwrtCref, inInputData, inDiffType, functions, maxIter);
optDerivedStatements1 = {SOME(DAE.STMT_TUPLE_ASSIGN(type_, dexpLst, derivedRHS, source))};
derivedStatements1 = List.flatten(List.map(optDerivedStatements1, List.fromOption));
derivedStatements1 = listAppend(derivedStatements1, {currStatement});
derivedStatements1 = listAppend(derivedStatements1, inStmtsAccum);
(derivedStatements2, functions) = differentiateStatements(restStatements, inDiffwrtCref, inInputData, inDiffType, derivedStatements1, functions, maxIter);
then (derivedStatements2, functions);
case (currStatement as DAE.STMT_ASSIGN_ARR(lhs=lhs, exp=rhs, type_=type_, source=source))::restStatements
equation
(derivedLHS, functions) = differentiateExp(lhs, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter);
(derivedRHS, functions) = differentiateExp(rhs, inDiffwrtCref, inInputData, inDiffType, functions, maxIter);
(derivedRHS,_) = ExpressionSimplify.simplify(derivedRHS);
derivedStatements1 = {DAE.STMT_ASSIGN_ARR(type_, derivedLHS, derivedRHS, source), currStatement};
derivedStatements1 = listAppend(derivedStatements1, inStmtsAccum);
(derivedStatements2, functions) = differentiateStatements(restStatements, inDiffwrtCref, inInputData, inDiffType, derivedStatements1, functions, maxIter);
then (derivedStatements2, functions);
case DAE.STMT_FOR(type_=type_, iterIsArray=iterIsArray, iter=ident, index=index, range=exp, statementLst=statementLst, source=source)::restStatements
equation
cref = ComponentReference.makeCrefIdent(ident, DAE.T_INTEGER_DEFAULT, {});
controlVar = BackendDAE.VAR(cref, BackendDAE.DISCRETE(), DAE.BIDIR(), DAE.NON_PARALLEL(), DAE.T_REAL_DEFAULT, NONE(), NONE(), {}, DAE.emptyElementSource, NONE(), NONE(), DAE.BCONST(false), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), false);
inputData = addGlobalVars({controlVar}, inInputData);
(derivedStatements1, functions) = differentiateStatements(statementLst, inDiffwrtCref, inputData, inDiffType, {}, inFunctionTree, maxIter);
derivedStatements1 = {DAE.STMT_FOR(type_, iterIsArray, ident, index, exp, derivedStatements1, source)};
derivedStatements1 = listAppend(derivedStatements1, inStmtsAccum);
(derivedStatements2, functions) = differentiateStatements(restStatements, inDiffwrtCref, inInputData, inDiffType, derivedStatements1, functions, maxIter);
then (derivedStatements2, functions);
case DAE.STMT_IF(exp=exp, statementLst=statementLst, else_=DAE.NOELSE(), source=source)::restStatements
equation
(derivedStatements1, functions) = differentiateStatements(statementLst, inDiffwrtCref, inInputData, inDiffType, {}, inFunctionTree, maxIter);
derivedStatements1 = {DAE.STMT_IF(exp, derivedStatements1, DAE.NOELSE(), source)};
derivedStatements1 = listAppend(derivedStatements1, inStmtsAccum);
(derivedStatements2, functions) = differentiateStatements(restStatements, inDiffwrtCref, inInputData, inDiffType, derivedStatements1, functions, maxIter);
then (derivedStatements2, functions);
case DAE.STMT_IF(exp=exp, statementLst=statementLst, else_=DAE.ELSEIF(exp=elseif_exp, statementLst=elseif_statementLst, else_=elseif_else_), source=source)::restStatements
equation
(derivedStatements1, functions) = differentiateStatements(statementLst, inDiffwrtCref, inInputData, inDiffType, {}, inFunctionTree, maxIter);
(derivedStatements2, functions) = differentiateStatements({DAE.STMT_IF(elseif_exp, elseif_statementLst, elseif_else_, source)}, inDiffwrtCref, inInputData, inDiffType, {}, functions, maxIter);
derivedStatements1 = {DAE.STMT_IF(exp, derivedStatements1, DAE.ELSE(derivedStatements2), source)};
derivedStatements1 = listAppend(derivedStatements1, inStmtsAccum);
(derivedStatements2, functions) = differentiateStatements(restStatements, inDiffwrtCref, inInputData, inDiffType, derivedStatements1, functions, maxIter);
then (derivedStatements2, functions);
case DAE.STMT_IF(exp=exp, statementLst=statementLst, else_=DAE.ELSE(statementLst=else_statementLst), source=source)::restStatements
equation
(derivedStatements1, functions) = differentiateStatements(statementLst, inDiffwrtCref, inInputData, inDiffType, {}, inFunctionTree, maxIter);
(derivedStatements2, functions) = differentiateStatements(else_statementLst, inDiffwrtCref, inInputData, inDiffType, {}, functions, maxIter);
derivedStatements1 = {DAE.STMT_IF(exp, derivedStatements1, DAE.ELSE(derivedStatements2), source)};
derivedStatements1 = listAppend(derivedStatements1, inStmtsAccum);
(derivedStatements2, functions) = differentiateStatements(restStatements, inDiffwrtCref, inInputData, inDiffType, derivedStatements1, functions, maxIter);
then (derivedStatements2, functions);
case DAE.STMT_WHILE(exp=exp, statementLst=statementLst, source=source)::restStatements
equation
(derivedStatements1, functions) = differentiateStatements(statementLst, inDiffwrtCref, inInputData, inDiffType, {}, inFunctionTree, maxIter);
derivedStatements1 = {DAE.STMT_WHILE(exp, derivedStatements1, source)};
derivedStatements1 = listAppend(derivedStatements1, inStmtsAccum);
(derivedStatements2, functions) = differentiateStatements(restStatements, inDiffwrtCref, inInputData, inDiffType, derivedStatements1, functions, maxIter);
then (derivedStatements2, functions);
case DAE.STMT_WHEN(exp=exp, initialCall=initialCall, statementLst=statementLst, elseWhen= NONE(), source=source)::restStatements
equation
(derivedStatements1, functions) = differentiateStatements(statementLst, inDiffwrtCref, inInputData, inDiffType, {}, inFunctionTree, maxIter);
derivedStatements1 = {DAE.STMT_WHEN(exp, {}, initialCall, derivedStatements1, NONE(), source)};
derivedStatements1 = listAppend(derivedStatements1, inStmtsAccum);
(derivedStatements2, functions) = differentiateStatements(restStatements, inDiffwrtCref, inInputData, inDiffType, derivedStatements1, functions, maxIter);
then (derivedStatements2, functions);
case DAE.STMT_WHEN(exp=exp, initialCall=initialCall, statementLst=statementLst, elseWhen= SOME(stmt), source=source)::restStatements
equation
(derivedStatements1, functions) = differentiateStatements(statementLst, inDiffwrtCref, inInputData, inDiffType, {}, inFunctionTree, maxIter);
({dstmt}, functions) = differentiateStatements({stmt}, inDiffwrtCref, inInputData, inDiffType, {}, functions, maxIter);
derivedStatements1 = {DAE.STMT_WHEN(exp, {}, initialCall, derivedStatements1, SOME(dstmt), source)};
derivedStatements1 = listAppend(derivedStatements1, inStmtsAccum);
(derivedStatements2, functions) = differentiateStatements(restStatements, inDiffwrtCref, inInputData, inDiffType, derivedStatements1, functions, maxIter);
then (derivedStatements2, functions);
case (DAE.STMT_ASSERT())::restStatements
equation
(derivedStatements1, functions) = differentiateStatements(restStatements, inDiffwrtCref, inInputData, inDiffType, inStmtsAccum, inFunctionTree, maxIter);
then (derivedStatements1, functions);
case (currStatement as DAE.STMT_TERMINATE())::restStatements
equation
derivedStatements1 = currStatement::inStmtsAccum;
(derivedStatements2, functions) = differentiateStatements(restStatements, inDiffwrtCref, inInputData, inDiffType, derivedStatements1, inFunctionTree, maxIter);
then (derivedStatements2, functions);
case (currStatement as DAE.STMT_REINIT())::restStatements
equation
derivedStatements1 = currStatement::inStmtsAccum;
(derivedStatements1, functions) = differentiateStatements(restStatements, inDiffwrtCref, inInputData, inDiffType, derivedStatements1, inFunctionTree, maxIter);
then (derivedStatements1, functions);
case (currStatement as DAE.STMT_NORETCALL())::restStatements
equation
derivedStatements1 = currStatement::inStmtsAccum;
(derivedStatements1, functions) = differentiateStatements(restStatements, inDiffwrtCref, inInputData, inDiffType, derivedStatements1, inFunctionTree, maxIter);
then (derivedStatements1, functions);
case (currStatement as DAE.STMT_RETURN())::restStatements
equation
derivedStatements1 = currStatement::inStmtsAccum;
(derivedStatements1, functions) = differentiateStatements(restStatements, inDiffwrtCref, inInputData, inDiffType, derivedStatements1, inFunctionTree, maxIter);
then (derivedStatements1, functions);
case (currStatement as DAE.STMT_BREAK())::restStatements
equation
derivedStatements1 = currStatement::inStmtsAccum;
(derivedStatements1, functions) = differentiateStatements(restStatements, inDiffwrtCref, inInputData, inDiffType, derivedStatements1, inFunctionTree, maxIter);
then (derivedStatements1, functions);
else
equation
if Flags.isSet(Flags.FAILTRACE) then
(currStatement::_) = inStmts;
s1 = DAEDump.ppStatementStr(currStatement);
s2 = ComponentReference.printComponentRefStr(inDiffwrtCref);
Debug.trace("- differentiateStatements " + s1 + " w.r.t: " + s2 + " failed\n");
end if;
then fail();
end matchcontinue;
end differentiateStatements;
protected function isDiscreteAssignStatment
input DAE.Statement inStmt;
output Boolean out;
algorithm
out := match (inStmt)
local
DAE.Type tp;
case DAE.STMT_ASSIGN(type_= tp)
then Types.isDiscreteType(tp);
case DAE.STMT_ASSIGN_ARR(type_= tp)
then Types.isDiscreteType(tp);
case DAE.STMT_TUPLE_ASSIGN(type_= tp)
then Types.isDiscreteType(tp);
else false;
end match;
end isDiscreteAssignStatment;
protected function makeAssignmentfromTuple
"Help function for differentiateStatements"
input tuple<DAE.Exp, DAE.Exp> inTpl;
input DAE.ElementSource source;
input DAE.FunctionTree inFunctionTree;
output Option<DAE.Statement> outStmt;
algorithm
outStmt := match(inTpl)
local
DAE.Exp e1, e2;
DAE.Type tp;
case ((e1 as DAE.CREF(ty=tp), e2))
then SOME(DAE.STMT_ASSIGN(tp, e1, e2, source));
case ((e1 as DAE.CALL(), e2)) guard( Expression.isRecordCall(e1, inFunctionTree))
equation
tp = Expression.typeof(e1);
then SOME(DAE.STMT_ASSIGN(tp, e1, e2, source));
case ((e1, e2)) guard(Expression.isZero(e1))
equation
_ = Expression.typeof(e2);
then NONE();
end match;
end makeAssignmentfromTuple;
// =============================================================================
// help functions for differentiation
// - differentiateCrefs
// - differentiateCalls
// - differentiateBinary (e.g.: ADD, SUB, MUL, DIV, POW, ...