OMCompiler/Compiler/BackEnd/ZeroCrossings.mo
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of OpenModelica. | ||
| 3 | * | ||
| 4 | * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), | ||
| 5 | * c/o Linköpings universitet, Department of Computer and Information Science, | ||
| 6 | * SE-58183 Linköping, Sweden. | ||
| 7 | * | ||
| 8 | * All rights reserved. | ||
| 9 | * | ||
| 10 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF AGPL VERSION 3 LICENSE OR | ||
| 11 | * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. | ||
| 12 | * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES | ||
| 13 | * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GNU AGPL | ||
| 14 | * VERSION 3, ACCORDING TO RECIPIENTS CHOICE. | ||
| 15 | * | ||
| 16 | * The OpenModelica software and the OSMC (Open Source Modelica Consortium) | ||
| 17 | * Public License (OSMC-PL) are obtained from OSMC, either from the above | ||
| 18 | * address, from the URLs: | ||
| 19 | * http://www.openmodelica.org or | ||
| 20 | * https://github.com/OpenModelica/ or | ||
| 21 | * http://www.ida.liu.se/projects/OpenModelica, | ||
| 22 | * and in the OpenModelica distribution. | ||
| 23 | * | ||
| 24 | * GNU AGPL version 3 is obtained from: | ||
| 25 | * https://www.gnu.org/licenses/licenses.html#GPL | ||
| 26 | * | ||
| 27 | * This program is distributed WITHOUT ANY WARRANTY; without | ||
| 28 | * even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 29 | * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH | ||
| 30 | * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL. | ||
| 31 | * | ||
| 32 | * See the full OSMC Public License conditions for more details. | ||
| 33 | * | ||
| 34 | */ | ||
| 35 | |||
| 36 | encapsulated package ZeroCrossings | ||
| 37 | " file: ZeroCrossings.mo | ||
| 38 | package: ZeroCrossings | ||
| 39 | description: This package contains utility functions for zero crossings | ||
| 40 | inside BackendDAE. | ||
| 41 | |||
| 42 | " | ||
| 43 | |||
| 44 | import BackendDAE; | ||
| 45 | import BackendDAE.{ZeroCrossing,ZeroCrossingSet}; | ||
| 46 | import BackendDAE.ZeroCrossingSet.ZERO_CROSSING_SET; | ||
| 47 | |||
| 48 | protected | ||
| 49 | import DoubleEnded; | ||
| 50 | import ExpressionBasics; | ||
| 51 | |||
| 52 | public | ||
| 53 | type Tree = ZeroCrossingTree.Tree; | ||
| 54 | |||
| 55 | package ZeroCrossingTree "Lookup ZeroCrossing -> list<ZeroCrossing> (the cons-cell storing the ZC)" | ||
| 56 | extends BaseAvlTree; | ||
| 57 | redeclare type Key = ZeroCrossing; | ||
| 58 | redeclare type Value = list<ZeroCrossing>; | ||
| 59 | redeclare function extends keyStr | ||
| 60 | algorithm | ||
| 61 | ✗ | outString := ExpressionBasics.printExpStr(inKey.relation_); | |
| 62 | end keyStr; | ||
| 63 | redeclare function extends valueStr | ||
| 64 | protected | ||
| 65 | ZeroCrossing zc; | ||
| 66 | algorithm | ||
| 67 | ✗ | zc := listGet(inValue,1); | |
| 68 | ✗ | outString := ExpressionBasics.printExpStr(zc.relation_); | |
| 69 | end valueStr; | ||
| 70 | redeclare function extends keyCompare | ||
| 71 | algorithm | ||
| 72 | 27218 | outResult := ZeroCrossings.compare(inKey1, inKey2); | |
| 73 | end keyCompare; | ||
| 74 | end ZeroCrossingTree; | ||
| 75 | |||
| 76 | function new | ||
| 77 | output ZeroCrossingSet zc_set; | ||
| 78 | algorithm | ||
| 79 | 20936 | zc_set := ZERO_CROSSING_SET(DoubleEnded.fromList({}), arrayCreate(1, ZeroCrossingTree.new())); | |
| 80 | end new; | ||
| 81 | |||
| 82 | function length | ||
| 83 | input ZeroCrossingSet zc_set; | ||
| 84 | output Integer i; | ||
| 85 | protected | ||
| 86 | list<ZeroCrossing> zcs = DoubleEnded.toListNoCopyNoClear(zc_set.zc); | ||
| 87 | algorithm | ||
| 88 |
4/4✓ Branch 0 taken 1678 times.
✓ Branch 1 taken 1069 times.
✓ Branch 2 taken 1678 times.
✓ Branch 3 taken 1069 times.
|
2747 | i := sum(zeroCrossingSize(zc) for zc in zcs); |
| 89 | end length; | ||
| 90 | |||
| 91 | function zeroCrossingSize | ||
| 92 | input ZeroCrossing zc; | ||
| 93 | output Integer s; | ||
| 94 | algorithm | ||
| 95 | s := match zc.iter | ||
| 96 | local | ||
| 97 | list<BackendDAE.SimIterator> iter; | ||
| 98 | ✗ | case SOME(iter) then BackendDAE.getSimIteratorSize(iter); | |
| 99 | else 1; | ||
| 100 | end match; | ||
| 101 | end zeroCrossingSize; | ||
| 102 | |||
| 103 | function add | ||
| 104 | input ZeroCrossingSet zc_set; | ||
| 105 | input ZeroCrossing zc; | ||
| 106 | protected | ||
| 107 | list<ZeroCrossing> addedCell; | ||
| 108 | algorithm | ||
| 109 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 2 taken 4520 times.
|
4530 | if not contains(zc_set, zc) then |
| 110 | 4520 | DoubleEnded.push_back(zc_set.zc, zc); | |
| 111 | 4520 | addedCell := DoubleEnded.currentBackCell(zc_set.zc); | |
| 112 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4520 times.
|
9040 | arrayUpdate(zc_set.tree, 1, ZeroCrossingTree.add(arrayGet(zc_set.tree, 1), zc, addedCell)); |
| 113 | end if; | ||
| 114 | end add; | ||
| 115 | |||
| 116 | function add_list | ||
| 117 | input ZeroCrossingSet zc_set; | ||
| 118 | input list<ZeroCrossing> zc_lst; | ||
| 119 | algorithm | ||
| 120 |
2/2✓ Branch 0 taken 211 times.
✓ Branch 1 taken 144 times.
|
355 | for zc in zc_lst loop |
| 121 | 211 | add(zc_set, zc); | |
| 122 | end for; | ||
| 123 | end add_list; | ||
| 124 | |||
| 125 | function push "Appends without checking for duplicates; the lookup tree keeps the first occurrence." | ||
| 126 | input ZeroCrossingSet zc_set; | ||
| 127 | input ZeroCrossing zc; | ||
| 128 | protected | ||
| 129 | list<ZeroCrossing> addedCell; | ||
| 130 | algorithm | ||
| 131 | 291 | DoubleEnded.push_back(zc_set.zc, zc); | |
| 132 | 291 | addedCell := DoubleEnded.currentBackCell(zc_set.zc); | |
| 133 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 291 times.
|
582 | arrayUpdate(zc_set.tree, 1, ZeroCrossingTree.add(arrayGet(zc_set.tree, 1), zc, addedCell, ZeroCrossingTree.addConflictKeep)); |
| 134 | end push; | ||
| 135 | |||
| 136 | function push_list | ||
| 137 | input ZeroCrossingSet zc_set; | ||
| 138 | input list<ZeroCrossing> zc_lst; | ||
| 139 | algorithm | ||
| 140 |
2/2✓ Branch 0 taken 158 times.
✓ Branch 1 taken 93 times.
|
251 | for zc in zc_lst loop |
| 141 | 158 | push(zc_set, zc); | |
| 142 | end for; | ||
| 143 | end push_list; | ||
| 144 | |||
| 145 | function count "Number of stored zero crossings; unlike length() this does not expand iterators." | ||
| 146 | input ZeroCrossingSet zc_set; | ||
| 147 | output Integer i; | ||
| 148 | algorithm | ||
| 149 | 9636 | i := DoubleEnded.length(zc_set.zc); | |
| 150 | end count; | ||
| 151 | |||
| 152 | function toList | ||
| 153 | input ZeroCrossingSet zc; | ||
| 154 | output list<ZeroCrossing> lst = {}; | ||
| 155 | |||
| 156 | algorithm | ||
| 157 | 3378 | lst := DoubleEnded.toListNoCopyNoClear(zc.zc); | |
| 158 | end toList; | ||
| 159 | |||
| 160 | function updateIndices | ||
| 161 | input list<ZeroCrossing> ilst; | ||
| 162 | output list<ZeroCrossing> olst = {}; | ||
| 163 | protected | ||
| 164 | Integer i = 0; | ||
| 165 | algorithm | ||
| 166 |
2/2✓ Branch 1 taken 3431 times.
✓ Branch 2 taken 2138 times.
|
5569 | for zc in ilst loop |
| 167 | 3431 | zc.index := i; | |
| 168 | olst := zc :: olst; | ||
| 169 | 3431 | i := i + 1; | |
| 170 | end for; | ||
| 171 | 2138 | olst := listReverse(olst); | |
| 172 | end updateIndices; | ||
| 173 | |||
| 174 | function contains | ||
| 175 | input ZeroCrossingSet zc_set; | ||
| 176 | input ZeroCrossing zc; | ||
| 177 | output Boolean matches; | ||
| 178 | algorithm | ||
| 179 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9685 times.
|
19370 | matches := ZeroCrossingTree.hasKey(arrayGet(zc_set.tree,1), zc); |
| 180 | end contains; | ||
| 181 | |||
| 182 | function get | ||
| 183 | input ZeroCrossingSet zc_set; | ||
| 184 | input ZeroCrossing zc; | ||
| 185 | output ZeroCrossing outZc; | ||
| 186 | algorithm | ||
| 187 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 994 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 994 times.
|
1988 | outZc::_ := ZeroCrossingTree.get(arrayGet(zc_set.tree,1), zc); |
| 188 | end get; | ||
| 189 | |||
| 190 | function equals "Returns true if both zero crossings have the same function expression" | ||
| 191 | input ZeroCrossing zc1; | ||
| 192 | input ZeroCrossing zc2; | ||
| 193 | output Boolean outBoolean; | ||
| 194 | algorithm | ||
| 195 | ✗ | outBoolean := 0==compare(zc1, zc2); | |
| 196 | end equals; | ||
| 197 | |||
| 198 | function compare "Returns true if both zero crossings have the same function expression" | ||
| 199 | input ZeroCrossing zc1; | ||
| 200 | input ZeroCrossing zc2; | ||
| 201 | output Integer comp; | ||
| 202 | algorithm | ||
| 203 | comp := match (zc1, zc2) | ||
| 204 | local | ||
| 205 | DAE.Exp e1, e2, e3, e4; | ||
| 206 | |||
| 207 | case (BackendDAE.ZERO_CROSSING(relation_=DAE.CALL(path=Absyn.IDENT("sample"), expLst={e1, _, _})), BackendDAE.ZERO_CROSSING(relation_=DAE.CALL(path=Absyn.IDENT("sample"), expLst={e2, _, _}))) | ||
| 208 | 475 | then ExpressionBasics.compare(e1,e2); | |
| 209 | |||
| 210 | case (BackendDAE.ZERO_CROSSING(relation_=DAE.CALL(path=Absyn.IDENT("integer"), expLst={e1, _})), BackendDAE.ZERO_CROSSING(relation_=DAE.CALL(path=Absyn.IDENT("integer"), expLst={e2, _}))) | ||
| 211 | 148 | then ExpressionBasics.compare(e1,e2); | |
| 212 | |||
| 213 | case (BackendDAE.ZERO_CROSSING(relation_=DAE.CALL(path=Absyn.IDENT("floor"), expLst={e1, _})), BackendDAE.ZERO_CROSSING(relation_=DAE.CALL(path=Absyn.IDENT("floor"), expLst={e2, _}))) | ||
| 214 | ✗ | then ExpressionBasics.compare(e1,e2); | |
| 215 | |||
| 216 | case (BackendDAE.ZERO_CROSSING(relation_=DAE.CALL(path=Absyn.IDENT("ceil"), expLst={e1, _})), BackendDAE.ZERO_CROSSING(relation_=DAE.CALL(path=Absyn.IDENT("ceil"), expLst={e2, _}))) | ||
| 217 | ✗ | then ExpressionBasics.compare(e1,e2); | |
| 218 | |||
| 219 | case (BackendDAE.ZERO_CROSSING(relation_=DAE.CALL(path=Absyn.IDENT("mod"), expLst={e1, e2, _})), BackendDAE.ZERO_CROSSING(relation_=DAE.CALL(path=Absyn.IDENT("mod"), expLst={e3, e4, _}))) | ||
| 220 | algorithm | ||
| 221 | 3 | comp := ExpressionBasics.compare(e1,e3); | |
| 222 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | then if comp==0 then ExpressionBasics.compare(e2, e4) else comp; |
| 223 | |||
| 224 | case (BackendDAE.ZERO_CROSSING(relation_=DAE.CALL(path=Absyn.IDENT("div"), expLst={e1, e2, _})), BackendDAE.ZERO_CROSSING(relation_=DAE.CALL(path=Absyn.IDENT("div"), expLst={e3, e4, _}))) | ||
| 225 | algorithm | ||
| 226 | 6 | comp := ExpressionBasics.compare(e1,e3); | |
| 227 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | then if comp==0 then ExpressionBasics.compare(e2, e4) else comp; |
| 228 | |||
| 229 | case (BackendDAE.ZERO_CROSSING(relation_=e1), BackendDAE.ZERO_CROSSING(relation_=e2)) | ||
| 230 | 26586 | then ExpressionBasics.compare(e1, e2); | |
| 231 | end match; | ||
| 232 | end compare; | ||
| 233 | |||
| 234 | annotation(__OpenModelica_Interface="backend_types"); | ||
| 235 | end ZeroCrossings; | ||
| 236 |