OMCompiler/Compiler/Util/SBPWAtomicLinearMap.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 uniontype SBPWAtomicLinearMap | ||
| 37 | import SBAtomicSet; | ||
| 38 | import SBInterval; | ||
| 39 | import SBLinearMap; | ||
| 40 | |||
| 41 | protected | ||
| 42 | import Array; | ||
| 43 | import List; | ||
| 44 | import System; | ||
| 45 | import MetaModelica.Dangerous.*; | ||
| 46 | |||
| 47 | public | ||
| 48 | record PW_ATOMIC_LINEAR_MAP | ||
| 49 | SBAtomicSet dom; | ||
| 50 | SBLinearMap lmap; | ||
| 51 | end PW_ATOMIC_LINEAR_MAP; | ||
| 52 | |||
| 53 | function new | ||
| 54 | input SBAtomicSet dom; | ||
| 55 | input SBLinearMap lmap; | ||
| 56 | output SBPWAtomicLinearMap map; | ||
| 57 | protected | ||
| 58 | Boolean compatible = true; | ||
| 59 | array<SBInterval> ints; | ||
| 60 | array<Real> g, o; | ||
| 61 | SBInterval i; | ||
| 62 | Real gain, offset, lo, step, hi; | ||
| 63 | algorithm | ||
| 64 |
2/2✓ Branch 2 taken 293 times.
✓ Branch 3 taken 5002 times.
|
5295 | if SBAtomicSet.ndim(dom) <> SBLinearMap.ndim(lmap) then |
| 65 | // Warning: Atomic set and map should be of the same dimension. | ||
| 66 | 293 | map := newEmpty(); | |
| 67 | 293 | return; | |
| 68 | end if; | ||
| 69 | |||
| 70 | 5002 | ints := SBMultiInterval.intervals(SBAtomicSet.aset(dom)); | |
| 71 | 5002 | g := SBLinearMap.gain(lmap); | |
| 72 | 5002 | o := SBLinearMap.offset(lmap); | |
| 73 | |||
| 74 |
1/2✓ Branch 0 taken 5002 times.
✗ Branch 1 not taken.
|
10470 | for j in 1:arrayLength(ints) loop |
| 75 | 5468 | i := arrayGetNoBoundsChecking(ints, j); | |
| 76 | 5468 | gain := arrayGetNoBoundsChecking(g, j); | |
| 77 | offset := arrayGetNoBoundsChecking(g, j); | ||
| 78 | |||
| 79 |
2/2✓ Branch 1 taken 5120 times.
✓ Branch 2 taken 348 times.
|
5468 | if gain < intReal(System.intMaxLit()) then |
| 80 | 5120 | lo := SBInterval.lowerBound(i) * gain + offset; | |
| 81 | 5120 | step := SBInterval.stepValue(i) * gain; | |
| 82 | 5120 | hi := SBInterval.upperBound(i) * gain + offset; | |
| 83 | |||
| 84 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 5120 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
5120 | if lo <> realInt(lo) and SBInterval.lowerBound(i) > 0 then |
| 85 | // Warning: Incompatible map. | ||
| 86 | compatible := false; | ||
| 87 | break; | ||
| 88 | end if; | ||
| 89 | |||
| 90 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 5120 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
5120 | if step <> realInt(step) and SBInterval.stepValue(i) > 0 then |
| 91 | // Warning: Incompatible map. | ||
| 92 | compatible := false; | ||
| 93 | break; | ||
| 94 | end if; | ||
| 95 | |||
| 96 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 5120 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
5120 | if hi <> realInt(hi) and SBInterval.upperBound(i) > 0 then |
| 97 | // Warning: Incompatible map. | ||
| 98 | compatible := false; | ||
| 99 | break; | ||
| 100 | end if; | ||
| 101 | end if; | ||
| 102 | end for; | ||
| 103 | |||
| 104 |
1/2✓ Branch 0 taken 5002 times.
✗ Branch 1 not taken.
|
5002 | if compatible then |
| 105 | 5002 | map := PW_ATOMIC_LINEAR_MAP(SBAtomicSet.copy(dom), SBLinearMap.copy(lmap)); | |
| 106 | else | ||
| 107 | // Warning: Incompatible map. | ||
| 108 | ✗ | map := newEmpty(); | |
| 109 | end if; | ||
| 110 | end new; | ||
| 111 | |||
| 112 | function newEmpty | ||
| 113 | output SBPWAtomicLinearMap map; | ||
| 114 | algorithm | ||
| 115 | 293 | map := PW_ATOMIC_LINEAR_MAP(SBAtomicSet.newEmpty(), SBLinearMap.newEmpty()); | |
| 116 | end newEmpty; | ||
| 117 | |||
| 118 | function dom | ||
| 119 | input SBPWAtomicLinearMap map; | ||
| 120 | output SBAtomicSet dom = map.dom; | ||
| 121 | end dom; | ||
| 122 | |||
| 123 | function lmap | ||
| 124 | input SBPWAtomicLinearMap map; | ||
| 125 | output SBLinearMap lmap = map.lmap; | ||
| 126 | end lmap; | ||
| 127 | |||
| 128 | function isEmpty | ||
| 129 | input SBPWAtomicLinearMap map; | ||
| 130 | output Boolean empty; | ||
| 131 | algorithm | ||
| 132 | ✗ | empty := SBAtomicSet.isEmpty(map.dom) and SBLinearMap.isEmpty(map.lmap); | |
| 133 | end isEmpty; | ||
| 134 | |||
| 135 | function image | ||
| 136 | input SBPWAtomicLinearMap map; | ||
| 137 | input SBAtomicSet set; | ||
| 138 | output SBAtomicSet outSet; | ||
| 139 | protected | ||
| 140 | function crop_inf | ||
| 141 | input Real v; | ||
| 142 | output Integer i; | ||
| 143 | algorithm | ||
| 144 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6678 times.
|
6678 | i := if v >= intReal(System.intMaxLit()) then System.intMaxLit() else realInt(v); |
| 145 | end crop_inf; | ||
| 146 | |||
| 147 | array<SBInterval> inters, res; | ||
| 148 | array<Real> gains, offsets; | ||
| 149 | SBAtomicSet set_int; | ||
| 150 | SBInterval int; | ||
| 151 | Real gain, offset; | ||
| 152 | Integer new_lo, new_step, new_hi; | ||
| 153 | Real tmp_lo, tmp_step, tmp_hi; | ||
| 154 | algorithm | ||
| 155 |
2/2✓ Branch 1 taken 293 times.
✓ Branch 2 taken 2288 times.
|
2581 | if SBAtomicSet.isEmpty(map.dom) then |
| 156 | 293 | outSet := SBAtomicSet.newEmpty(); | |
| 157 | 293 | return; | |
| 158 | end if; | ||
| 159 | |||
| 160 | 2288 | set_int := SBAtomicSet.intersection(set, map.dom); | |
| 161 | 2288 | inters := SBMultiInterval.intervals(SBAtomicSet.aset(set_int)); | |
| 162 | |||
| 163 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2288 times.
|
2288 | if arrayEmpty(inters) then |
| 164 | ✗ | outSet := SBAtomicSet.newEmpty(); | |
| 165 | ✗ | return; | |
| 166 | end if; | ||
| 167 | |||
| 168 | 2288 | gains := SBLinearMap.gain(map.lmap); | |
| 169 | 2288 | offsets := SBLinearMap.offset(map.lmap); | |
| 170 | |||
| 171 | 2288 | res := arrayCreateNoInit(arrayLength(inters), inters[1]); | |
| 172 | |||
| 173 |
1/2✓ Branch 0 taken 2288 times.
✗ Branch 1 not taken.
|
4862 | for i in 1:arrayLength(inters) loop |
| 174 | 2574 | int := arrayGetNoBoundsChecking(inters, i); | |
| 175 | 2574 | gain := gains[i]; | |
| 176 | 2574 | offset := offsets[i]; | |
| 177 | |||
| 178 | 2574 | tmp_lo := SBInterval.lowerBound(int) * gain + offset; | |
| 179 | 2574 | tmp_step := SBInterval.stepValue(int) * gain; | |
| 180 | 2574 | tmp_hi := SBInterval.upperBound(int) * gain + offset; | |
| 181 | |||
| 182 |
2/2✓ Branch 1 taken 2226 times.
✓ Branch 2 taken 348 times.
|
2574 | if gain < intReal(System.intMaxLit()) then |
| 183 | 2226 | new_lo := crop_inf(tmp_lo); | |
| 184 | 2226 | new_step := crop_inf(tmp_step); | |
| 185 | 2226 | new_hi := crop_inf(tmp_hi); | |
| 186 | else | ||
| 187 | new_lo := 1; | ||
| 188 | new_step := 1; | ||
| 189 | 348 | new_hi := System.intMaxLit(); | |
| 190 | end if; | ||
| 191 | |||
| 192 | 2574 | arrayUpdateNoBoundsChecking(res, i, SBInterval.new(new_lo, new_step, new_hi)); | |
| 193 | end for; | ||
| 194 | |||
| 195 | 2288 | outSet := SBAtomicSet.new(SBMultiInterval.fromArray(res)); | |
| 196 | end image; | ||
| 197 | |||
| 198 | function preImage | ||
| 199 | input SBPWAtomicLinearMap map; | ||
| 200 | input SBAtomicSet set; | ||
| 201 | output SBAtomicSet outSet; | ||
| 202 | protected | ||
| 203 | SBAtomicSet full_im, actual_im, aux; | ||
| 204 | SBPWAtomicLinearMap inv; | ||
| 205 | algorithm | ||
| 206 | 778 | full_im := image(map, map.dom); | |
| 207 | 778 | actual_im := SBAtomicSet.intersection(full_im, set); | |
| 208 | 778 | inv := new(actual_im, SBLinearMap.inverse(map.lmap)); | |
| 209 | 778 | aux := image(inv, actual_im); | |
| 210 | 778 | outSet := SBAtomicSet.intersection(map.dom, aux); | |
| 211 | end preImage; | ||
| 212 | |||
| 213 | function isEqual | ||
| 214 | input SBPWAtomicLinearMap map1; | ||
| 215 | input SBPWAtomicLinearMap map2; | ||
| 216 | output Boolean equal; | ||
| 217 | algorithm | ||
| 218 | ✗ | equal := SBAtomicSet.isEqual(map1.dom, map2.dom) and | |
| 219 | SBLinearMap.isEqual(map1.lmap, map2.lmap); | ||
| 220 | end isEqual; | ||
| 221 | |||
| 222 | function toString | ||
| 223 | input SBPWAtomicLinearMap map; | ||
| 224 | output String str; | ||
| 225 | protected | ||
| 226 | list<String> strl = {}; | ||
| 227 | array<Real> g, o; | ||
| 228 | array<SBInterval> ints; | ||
| 229 | algorithm | ||
| 230 | ✗ | g := SBLinearMap.gain(map.lmap); | |
| 231 | ✗ | o := SBLinearMap.offset(map.lmap); | |
| 232 | ✗ | ints := SBMultiInterval.intervals(SBAtomicSet.aset(map.dom)); | |
| 233 | |||
| 234 | ✗ | for i in arrayLength(ints):-1:1 loop | |
| 235 | ✗ | str := "(" + SBInterval.toString(ints[i]) + ", " + String(g[i]) + " * x + " + String(o[i]) + ")"; | |
| 236 | strl := str :: strl; | ||
| 237 | end for; | ||
| 238 | |||
| 239 | ✗ | str := stringDelimitList(strl, "x"); | |
| 240 | end toString; | ||
| 241 | |||
| 242 | annotation(__OpenModelica_Interface="util"); | ||
| 243 | end SBPWAtomicLinearMap; | ||
| 244 |