OMCompiler/Compiler/NFFrontEnd/NFScalarize.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 NFScalarize | ||
| 37 | |||
| 38 | import FlatModel = NFFlatModel; | ||
| 39 | import NFFlatten.FunctionTree; | ||
| 40 | |||
| 41 | protected | ||
| 42 | import Attributes = NFAttributes; | ||
| 43 | import NFBackendExtension.{BackendInfo, VariableAttributes}; | ||
| 44 | import ExecStat.execStat; | ||
| 45 | import ComponentRef = NFComponentRef; | ||
| 46 | import Type = NFType; | ||
| 47 | import Expression = NFExpression; | ||
| 48 | import Binding = NFBinding; | ||
| 49 | import Equation = NFEquation; | ||
| 50 | import ExpressionIterator = NFExpressionIterator; | ||
| 51 | import Dimension = NFDimension; | ||
| 52 | import MetaModelica.Dangerous.listReverseInPlace; | ||
| 53 | import MetaModelica.Dangerous.arrayCreateNoInit; | ||
| 54 | import Variable = NFVariable; | ||
| 55 | import Component = NFComponent; | ||
| 56 | import NFPrefixes.Visibility; | ||
| 57 | import NFPrefixes.Variability; | ||
| 58 | import List; | ||
| 59 | import ElementSource; | ||
| 60 | import DAE; | ||
| 61 | import Statement = NFStatement; | ||
| 62 | import Algorithm = NFAlgorithm; | ||
| 63 | import ExpandExp = NFExpandExp; | ||
| 64 | import NFInstNode.InstNode; | ||
| 65 | import NFInstNode; | ||
| 66 | import SCode; | ||
| 67 | |||
| 68 | uniontype AttributeIterator | ||
| 69 | record ATTRIBUTE_ITERATOR | ||
| 70 | String name; | ||
| 71 | NFBinding.Source source; | ||
| 72 | Integer confidence; | ||
| 73 | Mutable<ExpressionIterator> iterator; | ||
| 74 | end ATTRIBUTE_ITERATOR; | ||
| 75 | |||
| 76 | function create | ||
| 77 | input tuple<String, Binding> attribute; | ||
| 78 | output AttributeIterator iter; | ||
| 79 | protected | ||
| 80 | String name; | ||
| 81 | Binding binding; | ||
| 82 | algorithm | ||
| 83 | 78304 | (name, binding) := attribute; | |
| 84 | 78304 | iter := ATTRIBUTE_ITERATOR(name, Binding.source(binding), Binding.confidence(binding), | |
| 85 | Mutable.create(ExpressionIterator.fromBinding(binding))); | ||
| 86 | end create; | ||
| 87 | |||
| 88 | function nextBinding | ||
| 89 | input AttributeIterator iter; | ||
| 90 | output tuple<String, Binding> binding; | ||
| 91 | protected | ||
| 92 | ExpressionIterator it; | ||
| 93 | Expression exp; | ||
| 94 | algorithm | ||
| 95 | 252821 | (it, exp) := ExpressionIterator.next(Mutable.access(iter.iterator)); | |
| 96 | 252821 | Mutable.update(iter.iterator, it); | |
| 97 | 252821 | binding := (iter.name, Binding.makeFlat(exp, Variability.PARAMETER, iter.source, iter.confidence)); | |
| 98 | end nextBinding; | ||
| 99 | end AttributeIterator; | ||
| 100 | |||
| 101 | public | ||
| 102 | function scalarize | ||
| 103 | input output FlatModel flatModel; | ||
| 104 | algorithm | ||
| 105 | 1355 | flatModel.variables := scalarizeVariables(flatModel.variables); | |
| 106 | flatModel.equations := Equation.mapExpList(flatModel.equations, expandComplexCref); | ||
| 107 | 1355 | flatModel.equations := scalarizeEquations(flatModel.equations); | |
| 108 | flatModel.initialEquations := Equation.mapExpList(flatModel.initialEquations, expandComplexCref); | ||
| 109 |
8/8✓ Branch 1 taken 132 times.
✓ Branch 2 taken 1355 times.
✓ Branch 3 taken 132 times.
✓ Branch 4 taken 1355 times.
✓ Branch 6 taken 46 times.
✓ Branch 7 taken 1355 times.
✓ Branch 8 taken 46 times.
✓ Branch 9 taken 1355 times.
|
1533 | flatModel.initialEquations := scalarizeEquations(flatModel.initialEquations); |
| 110 | flatModel.algorithms := list(scalarizeAlgorithm(a) for a in flatModel.algorithms); | ||
| 111 | flatModel.initialAlgorithms := list(scalarizeAlgorithm(a) for a in flatModel.initialAlgorithms); | ||
| 112 | |||
| 113 | 1355 | execStat(getInstanceName()); | |
| 114 | end scalarize; | ||
| 115 | |||
| 116 | function scalarizeVariables | ||
| 117 | input list<Variable> vars; | ||
| 118 | input Boolean forceScalarize = false; | ||
| 119 | output list<Variable> outVars = {}; | ||
| 120 | algorithm | ||
| 121 |
2/2✓ Branch 0 taken 234194 times.
✓ Branch 1 taken 1360 times.
|
235554 | for v in vars loop |
| 122 | 234194 | outVars := scalarizeVariable(v, outVars, forceScalarize); | |
| 123 | end for; | ||
| 124 | |||
| 125 | 1360 | outVars := listReverseInPlace(outVars); | |
| 126 | end scalarizeVariables; | ||
| 127 | |||
| 128 | function scalarizeVariable | ||
| 129 | input Variable var; | ||
| 130 | input output list<Variable> vars = {}; | ||
| 131 | input Boolean forceScalarize = false; | ||
| 132 | protected | ||
| 133 | ComponentRef name; | ||
| 134 | Binding binding; | ||
| 135 | Type ty, elem_ty; | ||
| 136 | Visibility vis; | ||
| 137 | Attributes attr; | ||
| 138 | list<tuple<String, Binding>> ty_attr; | ||
| 139 | SCode.Comment cmt; | ||
| 140 | SourceInfo info; | ||
| 141 | ExpressionIterator binding_iter = ExpressionIterator.NONE_ITERATOR(); | ||
| 142 | list<ComponentRef> crefs; | ||
| 143 | Expression exp; | ||
| 144 | list<AttributeIterator> ty_attr_iters; | ||
| 145 | list<BackendInfo> backend_attributes; | ||
| 146 | Variability bind_var; | ||
| 147 | BackendInfo binfo; | ||
| 148 | Binding.Source bind_src; | ||
| 149 | Boolean has_binding; | ||
| 150 | Integer confidence; | ||
| 151 | algorithm | ||
| 152 | 234194 | var.binding := Binding.mapExp(var.binding, expandComplexCref_traverser); | |
| 153 | |||
| 154 |
4/4✓ Branch 1 taken 49432 times.
✓ Branch 2 taken 184762 times.
✓ Branch 4 taken 49429 times.
✓ Branch 5 taken 3 times.
|
234194 | if Type.isArray(var.ty) and Type.hasKnownSize(var.ty) then |
| 155 | try | ||
| 156 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 49429 times.
|
49429 | Variable.VARIABLE(name, ty, binding, vis, attr, ty_attr, _, cmt, info, binfo) := var; |
| 157 | 49429 | crefs := ComponentRef.scalarize(name, false); | |
| 158 | |||
| 159 |
2/2✓ Branch 0 taken 1647 times.
✓ Branch 1 taken 47782 times.
|
49429 | if listEmpty(crefs) then |
| 160 | 1961 | return; | |
| 161 | end if; | ||
| 162 | |||
| 163 | 47782 | has_binding := Binding.isBound(binding); | |
| 164 | 47782 | bind_src := Binding.source(binding); | |
| 165 | 47782 | confidence := Binding.confidence(binding); | |
| 166 | |||
| 167 |
2/2✓ Branch 0 taken 8611 times.
✓ Branch 1 taken 39171 times.
|
47782 | if has_binding then |
| 168 | 8611 | binding_iter := ExpressionIterator.fromExp(Binding.getTypedExp(binding)); | |
| 169 | 8611 | bind_var := Binding.variability(binding); | |
| 170 | |||
| 171 | // Avoid scalarizing the variable if it would result in indexing a | ||
| 172 | // function call (#6267), unless the variable has attributes that must | ||
| 173 | // be scalarized (#7485). | ||
| 174 |
6/6✓ Branch 0 taken 8610 times.
✓ Branch 1 taken 1 time.
✓ Branch 3 taken 322 times.
✓ Branch 4 taken 8288 times.
✓ Branch 6 taken 314 times.
✓ Branch 7 taken 8 times.
|
8611 | if not forceScalarize and |
| 175 | ExpressionIterator.isSubscriptedArrayCall(binding_iter) and | ||
| 176 | not variableHasForcedScalarAttribute(var) then | ||
| 177 | vars := var :: vars; | ||
| 178 | 314 | return; | |
| 179 | end if; | ||
| 180 | else | ||
| 181 | bind_var := Variability.CONSTANT; // Not used | ||
| 182 | end if; | ||
| 183 | |||
| 184 | 47468 | elem_ty := Type.arrayElementType(ty); | |
| 185 |
4/4✓ Branch 0 taken 78304 times.
✓ Branch 1 taken 47468 times.
✓ Branch 2 taken 78304 times.
✓ Branch 3 taken 47468 times.
|
125772 | ty_attr_iters := list(AttributeIterator.create(a) for a in ty_attr); |
| 186 | 47468 | backend_attributes := BackendInfo.scalarize(binfo, listLength(crefs)); | |
| 187 | |||
| 188 |
2/2✓ Branch 0 taken 188102 times.
✓ Branch 1 taken 47468 times.
|
235570 | for cr in crefs loop |
| 189 |
2/2✓ Branch 0 taken 33510 times.
✓ Branch 1 taken 154592 times.
|
188102 | if has_binding then |
| 190 | 33510 | (binding_iter, exp) := ExpressionIterator.next(binding_iter); | |
| 191 | 33510 | binding := Binding.makeFlat(exp, bind_var, bind_src, confidence); | |
| 192 | end if; | ||
| 193 | |||
| 194 |
4/4✓ Branch 0 taken 252821 times.
✓ Branch 1 taken 188102 times.
✓ Branch 2 taken 252821 times.
✓ Branch 3 taken 188102 times.
|
440923 | ty_attr := list(AttributeIterator.nextBinding(i) for i in ty_attr_iters); |
| 195 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 188102 times.
|
188102 | binfo :: backend_attributes := backend_attributes; |
| 196 | 188102 | vars := Variable.VARIABLE(cr, elem_ty, binding, vis, attr, ty_attr, {}, cmt, info, binfo) :: vars; | |
| 197 | end for; | ||
| 198 | else | ||
| 199 | ✗ | Error.terminate(getInstanceName() + " failed on " + | |
| 200 | Variable.toString(var, printBindingType = true), var.info); | ||
| 201 | end try; | ||
| 202 | else | ||
| 203 | vars := var :: vars; | ||
| 204 | end if; | ||
| 205 | end scalarizeVariable; | ||
| 206 | |||
| 207 | function scalarizeBackendVariable | ||
| 208 | input Variable var; | ||
| 209 | input List<Integer> indices = {}; | ||
| 210 | input Boolean resize = false "resizable dimensions with their resized size, the indices are positions in it"; | ||
| 211 | output list<Variable> vars = {}; | ||
| 212 | protected | ||
| 213 | list<ComponentRef> crefs; | ||
| 214 | ExpressionIterator binding_iter; | ||
| 215 | Binding binding; | ||
| 216 | Variability bind_var; | ||
| 217 | Binding.Source bind_src; | ||
| 218 | Expression exp; | ||
| 219 | Type elem_ty; | ||
| 220 | BackendInfo binfo; | ||
| 221 | list<BackendInfo> backend_attributes; | ||
| 222 | Integer confidence; | ||
| 223 | algorithm | ||
| 224 | try | ||
| 225 | 1483 | crefs := listReverse(ComponentRef.scalarizeAll(ComponentRef.stripSubscriptsAll(var.name), resize)); | |
| 226 | 1483 | elem_ty := Type.arrayElementType(var.ty); | |
| 227 | 1483 | backend_attributes := BackendInfo.scalarize(var.backendinfo, listLength(crefs)); | |
| 228 |
2/2✓ Branch 1 taken 449 times.
✓ Branch 2 taken 1034 times.
|
1483 | if Binding.isBound(var.binding) then |
| 229 | 449 | binding_iter := ExpressionIterator.fromExp(Binding.getTypedExp(var.binding), true, false); | |
| 230 | 449 | bind_var := Binding.variability(var.binding); | |
| 231 | 449 | bind_src := Binding.source(var.binding); | |
| 232 | 449 | confidence := Binding.confidence(var.binding); | |
| 233 |
4/4✓ Branch 0 taken 4126 times.
✓ Branch 1 taken 449 times.
✓ Branch 2 taken 4126 times.
✓ Branch 3 taken 449 times.
|
4575 | vars := list( |
| 234 | match cr | ||
| 235 | case _ algorithm | ||
| 236 | 4126 | (binding_iter, exp) := ExpressionIterator.next(binding_iter); | |
| 237 | 4126 | binding := Binding.makeFlat(exp, bind_var, bind_src, confidence); | |
| 238 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4126 times.
|
4126 | binfo :: backend_attributes := backend_attributes; |
| 239 | 4126 | then Variable.VARIABLE(cr, elem_ty, binding, var.visibility, var.attributes, {}, {}, var.comment, var.info, binfo); | |
| 240 | end match for cr in crefs | ||
| 241 | ); | ||
| 242 | else | ||
| 243 |
4/4✓ Branch 0 taken 8799 times.
✓ Branch 1 taken 1034 times.
✓ Branch 2 taken 8799 times.
✓ Branch 3 taken 1034 times.
|
9833 | vars := list( |
| 244 | match cr | ||
| 245 | case _ algorithm | ||
| 246 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8799 times.
|
8799 | binfo :: backend_attributes := backend_attributes; |
| 247 | 8799 | then Variable.VARIABLE(cr, elem_ty, var.binding, var.visibility, var.attributes, {}, {}, var.comment, var.info, binfo); | |
| 248 | end match for cr in crefs | ||
| 249 | ); | ||
| 250 | end if; | ||
| 251 | // filter sliced variables | ||
| 252 | // ToDo: do this more efficiently and not create them in the first place | ||
| 253 |
3/4✓ Branch 0 taken 68 times.
✓ Branch 1 taken 1415 times.
✓ Branch 4 taken 68 times.
✗ Branch 5 not taken.
|
1483 | if not (listEmpty(indices) or listLength(indices) == listLength(vars)) then |
| 254 | 68 | vars := List.keepPositions(vars, indices, zeroBased = true); | |
| 255 | end if; | ||
| 256 | else | ||
| 257 | ✗ | Error.terminate(getInstanceName() + " failed for: " + Variable.toString(var), sourceInfo()); | |
| 258 | end try; | ||
| 259 | end scalarizeBackendVariable; | ||
| 260 | |||
| 261 | function scalarizeComplexVariable | ||
| 262 | "Scalarizes a complex variable to its elements. Assumes potential arrays | ||
| 263 | have already been resolved with scalarizeVariable()." | ||
| 264 | input Variable var; | ||
| 265 | input output list<Variable> vars = {}; | ||
| 266 | algorithm | ||
| 267 | vars := match var.backendinfo.attributes | ||
| 268 | local | ||
| 269 | VariableAttributes attr; | ||
| 270 | String name; | ||
| 271 | Integer index; | ||
| 272 | Variable elem_var; | ||
| 273 | |||
| 274 | case attr as VariableAttributes.VAR_ATTR_RECORD() algorithm | ||
| 275 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
|
2 | for tpl in UnorderedMap.toList(attr.indexMap) loop |
| 276 | 1 | (name, index) := tpl; | |
| 277 | elem_var := var; | ||
| 278 | 1 | elem_var.name := ComponentRef.prepend(elem_var.name, ComponentRef.rename(name, elem_var.name)); | |
| 279 | 1 | elem_var.backendinfo := BackendInfo.setAttributes(elem_var.backendinfo, attr.childrenAttr[index], var.backendinfo.annotations); | |
| 280 | // update the types accordingly | ||
| 281 | 1 | elem_var.ty := VariableAttributes.elemType(attr.childrenAttr[index]); | |
| 282 | 1 | elem_var.name := ComponentRef.setNodeType(elem_var.ty, elem_var.name); | |
| 283 | vars := elem_var :: vars; | ||
| 284 | end for; | ||
| 285 | 1 | then listReverse(vars); | |
| 286 | |||
| 287 | else {var}; | ||
| 288 | end match; | ||
| 289 | end scalarizeComplexVariable; | ||
| 290 | |||
| 291 | function expandComplexCref | ||
| 292 | input output Expression exp; | ||
| 293 | algorithm | ||
| 294 | 270271 | exp := Expression.map(exp, expandComplexCref_traverser); | |
| 295 | end expandComplexCref; | ||
| 296 | |||
| 297 | function expandComplexCref_traverser | ||
| 298 | input output Expression exp; | ||
| 299 | algorithm | ||
| 300 | () := match exp | ||
| 301 | case Expression.CREF(ty = Type.ARRAY()) | ||
| 302 | algorithm | ||
| 303 | // Expand crefs where any of the prefix nodes are arrays. For example if | ||
| 304 | // b in a.b.c is SomeType[2] we expand it into {a.b[1].c, a.b[2].c}. | ||
| 305 | // TODO: This is only done due to backend issues and shouldn't be | ||
| 306 | // necessary. | ||
| 307 |
2/2✓ Branch 1 taken 688 times.
✓ Branch 2 taken 40953 times.
|
41641 | if ComponentRef.isComplexArray(exp.cref) then |
| 308 | 688 | exp := ExpandExp.expand(exp); | |
| 309 | end if; | ||
| 310 | then | ||
| 311 | (); | ||
| 312 | |||
| 313 | else (); | ||
| 314 | end match; | ||
| 315 | end expandComplexCref_traverser; | ||
| 316 | |||
| 317 | function scalarizeEquations | ||
| 318 | input list<Equation> eql; | ||
| 319 | input Boolean forceScalarize = false; | ||
| 320 | output list<Equation> equations = {}; | ||
| 321 | algorithm | ||
| 322 |
2/2✓ Branch 0 taken 134294 times.
✓ Branch 1 taken 3583 times.
|
137877 | for eq in eql loop |
| 323 | 134294 | equations := scalarizeEquation(eq, equations, forceScalarize); | |
| 324 | end for; | ||
| 325 | |||
| 326 | 3583 | equations := listReverseInPlace(equations); | |
| 327 | end scalarizeEquations; | ||
| 328 | |||
| 329 | function scalarizeEquation | ||
| 330 | input Equation eq; | ||
| 331 | input output list<Equation> equations; | ||
| 332 | input Boolean forceScalarize = false; | ||
| 333 | algorithm | ||
| 334 | equations := match eq | ||
| 335 | local | ||
| 336 | ExpressionIterator lhs_iter, rhs_iter; | ||
| 337 | Expression lhs, rhs; | ||
| 338 | Type ty; | ||
| 339 | DAE.ElementSource src; | ||
| 340 | Boolean scalarize; | ||
| 341 | |||
| 342 | case Equation.EQUALITY(lhs = lhs, rhs = rhs, ty = ty, source = src) | ||
| 343 | guard Type.isArray(ty) | ||
| 344 | algorithm | ||
| 345 |
3/4✓ Branch 0 taken 27345 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 27345 times.
✗ Branch 3 not taken.
|
27349 | if forceScalarize or eq.scalarizeMode == NFEquation.ScalarizeMode.SCALARIZE then |
| 346 | scalarize := true; | ||
| 347 | elseif eq.scalarizeMode == NFEquation.ScalarizeMode.DONT_SCALARIZE then | ||
| 348 | scalarize := false; | ||
| 349 | elseif Expression.hasArrayCall(lhs) or Expression.hasArrayCall(rhs) then | ||
| 350 | scalarize := false; | ||
| 351 | else | ||
| 352 | scalarize := true; | ||
| 353 | end if; | ||
| 354 | |||
| 355 | if scalarize then | ||
| 356 | 7495 | lhs_iter := ExpressionIterator.fromExp(lhs); | |
| 357 | 7495 | rhs_iter := ExpressionIterator.fromExp(rhs); | |
| 358 | 7495 | ty := Type.arrayElementType(ty); | |
| 359 | |||
| 360 |
2/2✓ Branch 1 taken 21506 times.
✓ Branch 2 taken 7495 times.
|
29001 | while ExpressionIterator.hasNext(lhs_iter) loop |
| 361 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 21506 times.
|
21506 | if not ExpressionIterator.hasNext(rhs_iter) then |
| 362 | ✗ | Error.addInternalError(getInstanceName() + " could not expand rhs " + | |
| 363 | Expression.toString(eq.rhs), ElementSource.getInfo(src)); | ||
| 364 | end if; | ||
| 365 | |||
| 366 | 21506 | (lhs_iter, lhs) := ExpressionIterator.next(lhs_iter); | |
| 367 | 21506 | (rhs_iter, rhs) := ExpressionIterator.next(rhs_iter); | |
| 368 | 21506 | equations := Equation.makeEquality(lhs, rhs, ty, src, InstNode.fromCell(eq.scope)) :: equations; | |
| 369 | end while; | ||
| 370 | else | ||
| 371 | equations := eq :: equations; | ||
| 372 | end if; | ||
| 373 | then | ||
| 374 | equations; | ||
| 375 | |||
| 376 | case Equation.CONNECT() then equations; | ||
| 377 | |||
| 378 | case Equation.IF() | ||
| 379 | 161 | then scalarizeIfEquation(eq.branches, eq.scope, eq.source, equations); | |
| 380 | |||
| 381 | case Equation.WHEN() | ||
| 382 | 262 | then scalarizeWhenEquation(eq.branches, eq.scope, eq.source, equations); | |
| 383 | |||
| 384 | else eq :: equations; | ||
| 385 | end match; | ||
| 386 | end scalarizeEquation; | ||
| 387 | |||
| 388 | function scalarizeIfEquation | ||
| 389 | input list<Equation.Branch> branches; | ||
| 390 | input NFInstNode.ScopeRef scope; | ||
| 391 | input DAE.ElementSource source; | ||
| 392 | input output list<Equation> equations; | ||
| 393 | protected | ||
| 394 | list<Equation.Branch> bl = {}; | ||
| 395 | Expression cond; | ||
| 396 | list<Equation> body; | ||
| 397 | Variability var; | ||
| 398 | algorithm | ||
| 399 |
2/2✓ Branch 0 taken 592 times.
✓ Branch 1 taken 161 times.
|
753 | for b in branches loop |
| 400 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 592 times.
|
592 | Equation.Branch.BRANCH(cond, var, body) := b; |
| 401 | 592 | body := scalarizeEquations(body); | |
| 402 | |||
| 403 | // Remove branches with no equations after scalarization. | ||
| 404 |
1/2✓ Branch 0 taken 592 times.
✗ Branch 1 not taken.
|
592 | if not listEmpty(body) then |
| 405 | 592 | bl := Equation.makeBranch(cond, body, var) :: bl; | |
| 406 | end if; | ||
| 407 | end for; | ||
| 408 | |||
| 409 | // Add the scalarized if equation to the list of equations unless we don't | ||
| 410 | // have any branches left. | ||
| 411 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 161 times.
|
161 | if not listEmpty(bl) then |
| 412 | 161 | equations := Equation.IF(listReverseInPlace(bl), scope, source) :: equations; | |
| 413 | end if; | ||
| 414 | end scalarizeIfEquation; | ||
| 415 | |||
| 416 | function scalarizeWhenEquation | ||
| 417 | input list<Equation.Branch> branches; | ||
| 418 | input NFInstNode.ScopeRef scope; | ||
| 419 | input DAE.ElementSource source; | ||
| 420 | input output list<Equation> equations; | ||
| 421 | protected | ||
| 422 | list<Equation.Branch> bl = {}; | ||
| 423 | Expression cond; | ||
| 424 | list<Equation> body; | ||
| 425 | Variability var; | ||
| 426 | algorithm | ||
| 427 |
2/2✓ Branch 0 taken 271 times.
✓ Branch 1 taken 262 times.
|
533 | for b in branches loop |
| 428 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 271 times.
|
271 | Equation.Branch.BRANCH(cond, var, body) := b; |
| 429 | 271 | body := scalarizeEquations(body); | |
| 430 | |||
| 431 |
2/2✓ Branch 2 taken 46 times.
✓ Branch 3 taken 225 times.
|
271 | if Type.isArray(Expression.typeOf(cond)) then |
| 432 | 46 | cond := ExpandExp.expand(cond); | |
| 433 | end if; | ||
| 434 | |||
| 435 | 271 | bl := Equation.makeBranch(cond, body, var) :: bl; | |
| 436 | end for; | ||
| 437 | |||
| 438 | 262 | equations := Equation.WHEN(listReverseInPlace(bl), scope, source) :: equations; | |
| 439 | end scalarizeWhenEquation; | ||
| 440 | |||
| 441 | function scalarizeAlgorithm | ||
| 442 | input output Algorithm alg; | ||
| 443 | algorithm | ||
| 444 | 178 | alg.statements := scalarizeStatements(alg.statements); | |
| 445 | end scalarizeAlgorithm; | ||
| 446 | |||
| 447 | function scalarizeStatements | ||
| 448 | input list<Statement> stmts; | ||
| 449 | output list<Statement> statements = {}; | ||
| 450 | algorithm | ||
| 451 |
2/2✓ Branch 0 taken 817 times.
✓ Branch 1 taken 538 times.
|
1355 | for s in stmts loop |
| 452 | 817 | statements := scalarizeStatement(s, statements); | |
| 453 | end for; | ||
| 454 | |||
| 455 | 538 | statements := listReverseInPlace(statements); | |
| 456 | end scalarizeStatements; | ||
| 457 | |||
| 458 | function scalarizeStatement | ||
| 459 | input Statement stmt; | ||
| 460 | input output list<Statement> statements; | ||
| 461 | algorithm | ||
| 462 | statements := match stmt | ||
| 463 | case Statement.FOR() | ||
| 464 | 72 | then Statement.FOR(stmt.iterator, stmt.range, scalarizeStatements(stmt.body), stmt.forType, stmt.source, stmt.sub_iters) :: statements; | |
| 465 | |||
| 466 | case Statement.IF() | ||
| 467 | 137 | then scalarizeIfStatement(stmt.branches, stmt.source, statements); | |
| 468 | |||
| 469 | case Statement.WHEN() | ||
| 470 | 74 | then scalarizeWhenStatement(stmt.branches, stmt.source, statements); | |
| 471 | |||
| 472 | case Statement.WHILE() | ||
| 473 | ✗ | then Statement.WHILE(stmt.condition, scalarizeStatements(stmt.body), stmt.source) :: statements; | |
| 474 | |||
| 475 | else stmt :: statements; | ||
| 476 | end match; | ||
| 477 | end scalarizeStatement; | ||
| 478 | |||
| 479 | function scalarizeIfStatement | ||
| 480 | input list<tuple<Expression, list<Statement>>> branches; | ||
| 481 | input DAE.ElementSource source; | ||
| 482 | input output list<Statement> statements; | ||
| 483 | protected | ||
| 484 | list<tuple<Expression, list<Statement>>> bl = {}; | ||
| 485 | Expression cond; | ||
| 486 | list<Statement> body; | ||
| 487 | algorithm | ||
| 488 |
2/2✓ Branch 0 taken 178 times.
✓ Branch 1 taken 137 times.
|
315 | for b in branches loop |
| 489 | 178 | (cond, body) := b; | |
| 490 | 178 | body := scalarizeStatements(body); | |
| 491 | |||
| 492 | // Remove branches with no statements after scalarization. | ||
| 493 |
1/2✓ Branch 0 taken 178 times.
✗ Branch 1 not taken.
|
178 | if not listEmpty(body) then |
| 494 | 178 | bl := (cond, body) :: bl; | |
| 495 | end if; | ||
| 496 | end for; | ||
| 497 | |||
| 498 | // Add the scalarized if statement to the list of statements unless we don't | ||
| 499 | // have any branches left. | ||
| 500 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 137 times.
|
137 | if not listEmpty(bl) then |
| 501 | 137 | statements := Statement.IF(listReverseInPlace(bl), source) :: statements; | |
| 502 | end if; | ||
| 503 | end scalarizeIfStatement; | ||
| 504 | |||
| 505 | function scalarizeWhenStatement | ||
| 506 | input list<tuple<Expression, list<Statement>>> branches; | ||
| 507 | input DAE.ElementSource source; | ||
| 508 | input output list<Statement> statements; | ||
| 509 | protected | ||
| 510 | list<tuple<Expression, list<Statement>>> bl = {}; | ||
| 511 | Expression cond; | ||
| 512 | list<Statement> body; | ||
| 513 | algorithm | ||
| 514 |
2/2✓ Branch 0 taken 110 times.
✓ Branch 1 taken 74 times.
|
184 | for b in branches loop |
| 515 | 110 | (cond, body) := b; | |
| 516 | 110 | body := scalarizeStatements(body); | |
| 517 | |||
| 518 |
2/2✓ Branch 2 taken 19 times.
✓ Branch 3 taken 91 times.
|
110 | if Type.isArray(Expression.typeOf(cond)) then |
| 519 | 19 | cond := ExpandExp.expand(cond); | |
| 520 | end if; | ||
| 521 | |||
| 522 | 110 | bl := (cond, body) :: bl; | |
| 523 | end for; | ||
| 524 | |||
| 525 | 74 | statements := Statement.WHEN(listReverseInPlace(bl), source) :: statements; | |
| 526 | end scalarizeWhenStatement; | ||
| 527 | |||
| 528 | function variableHasForcedScalarAttribute | ||
| 529 | input Variable var; | ||
| 530 | output Boolean res; | ||
| 531 | algorithm | ||
| 532 |
2/2✓ Branch 0 taken 950 times.
✓ Branch 1 taken 314 times.
|
1264 | for attribute in {"min", "max", "nominal"} loop |
| 533 |
2/2✓ Branch 2 taken 8 times.
✓ Branch 3 taken 942 times.
|
950 | if Binding.isBound(Variable.lookupTypeAttribute(attribute, var)) then |
| 534 | res := true; | ||
| 535 | 8 | return; | |
| 536 | end if; | ||
| 537 | end for; | ||
| 538 | |||
| 539 | res := false; | ||
| 540 | end variableHasForcedScalarAttribute; | ||
| 541 | |||
| 542 | annotation(__OpenModelica_Interface="nf_frontend"); | ||
| 543 | end NFScalarize; | ||
| 544 |