OMCompiler/Compiler/NFFrontEnd/NFCheckModel.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 NFCheckModel | ||
| 37 | import FlatModel = NFFlatModel; | ||
| 38 | import Variable = NFVariable; | ||
| 39 | import Equation = NFEquation; | ||
| 40 | import Algorithm = NFAlgorithm; | ||
| 41 | import Statement = NFStatement; | ||
| 42 | |||
| 43 | protected | ||
| 44 | import Type = NFType; | ||
| 45 | import Binding = NFBinding; | ||
| 46 | import NFPrefixes.{Direction, Variability}; | ||
| 47 | import ComponentRef = NFComponentRef; | ||
| 48 | import UnorderedSet; | ||
| 49 | import Expression = NFExpression; | ||
| 50 | import Util; | ||
| 51 | import ExpandExp = NFExpandExp; | ||
| 52 | import Attributes = NFAttributes; | ||
| 53 | |||
| 54 | public | ||
| 55 | function checkModel | ||
| 56 | input FlatModel flatModel; | ||
| 57 | output Integer variables = 0; | ||
| 58 | output Integer equations = 0; | ||
| 59 | algorithm | ||
| 60 | ✗ | for v in flatModel.variables loop | |
| 61 | ✗ | (variables, equations) := countVariableSize(v, variables, equations); | |
| 62 | end for; | ||
| 63 | |||
| 64 | ✗ | equations := equations + Equation.sizeOfList(flatModel.equations); | |
| 65 | |||
| 66 | ✗ | for a in flatModel.algorithms loop | |
| 67 | ✗ | equations := equations + countAlgorithmSize(a); | |
| 68 | end for; | ||
| 69 | end checkModel; | ||
| 70 | |||
| 71 | function countVariableSize | ||
| 72 | input Variable var; | ||
| 73 | input output Integer variables; | ||
| 74 | input output Integer equations; | ||
| 75 | protected | ||
| 76 | Type ty; | ||
| 77 | Binding binding; | ||
| 78 | Attributes attr; | ||
| 79 | Integer var_size; | ||
| 80 | algorithm | ||
| 81 | ✗ | Variable.VARIABLE(ty = ty, binding = binding, attributes = attr) := var; | |
| 82 | |||
| 83 | ✗ | if attr.variability < Variability.DISCRETE then | |
| 84 | ✗ | return; | |
| 85 | end if; | ||
| 86 | |||
| 87 | ✗ | if Type.isExternalObject(ty) then | |
| 88 | ✗ | return; | |
| 89 | end if; | ||
| 90 | |||
| 91 | ✗ | var_size := Type.sizeOf(ty); | |
| 92 | ✗ | variables := variables + var_size; | |
| 93 | |||
| 94 | ✗ | if Variable.isTopLevelInput(var) then | |
| 95 | ✗ | equations := equations + var_size; | |
| 96 | else | ||
| 97 | ✗ | equations := equations + Type.sizeOf(Binding.getType(binding)); | |
| 98 | end if; | ||
| 99 | end countVariableSize; | ||
| 100 | |||
| 101 | function countAlgorithmSize | ||
| 102 | input Algorithm alg; | ||
| 103 | output Integer equations = 0; | ||
| 104 | protected | ||
| 105 | UnorderedSet<ComponentRef> crefs; | ||
| 106 | algorithm | ||
| 107 | ✗ | crefs := UnorderedSet.new(ComponentRef.hash, ComponentRef.isEqual); | |
| 108 | ✗ | crefs := List.fold(alg.statements, statementOutputs, crefs); | |
| 109 | ✗ | equations := equations + UnorderedSet.size(crefs); | |
| 110 | |||
| 111 | ✗ | print("Algorithm size: " + String(UnorderedSet.size(crefs)) + "\n"); | |
| 112 | ✗ | for cr in UnorderedSet.toList(crefs) loop | |
| 113 | ✗ | print(ComponentRef.toString(cr) + "\n"); | |
| 114 | end for; | ||
| 115 | end countAlgorithmSize; | ||
| 116 | |||
| 117 | protected | ||
| 118 | function statementOutputs | ||
| 119 | input Statement stmt; | ||
| 120 | input output UnorderedSet<ComponentRef> crefs; | ||
| 121 | algorithm | ||
| 122 | crefs := match stmt | ||
| 123 | case Statement.ASSIGNMENT() | ||
| 124 | ✗ | then Expression.fold(stmt.lhs, statementOutputCrefFinder, crefs); | |
| 125 | |||
| 126 | case Statement.FOR() | ||
| 127 | ✗ | then List.fold(stmt.body, statementOutputs, crefs); | |
| 128 | |||
| 129 | case Statement.IF() | ||
| 130 | algorithm | ||
| 131 | ✗ | for b in stmt.branches loop | |
| 132 | ✗ | crefs := List.fold(Util.tuple22(b), statementOutputs, crefs); | |
| 133 | end for; | ||
| 134 | then | ||
| 135 | crefs; | ||
| 136 | |||
| 137 | case Statement.WHEN() | ||
| 138 | algorithm | ||
| 139 | ✗ | for b in stmt.branches loop | |
| 140 | ✗ | crefs := List.fold(Util.tuple22(b), statementOutputs, crefs); | |
| 141 | end for; | ||
| 142 | then | ||
| 143 | crefs; | ||
| 144 | |||
| 145 | case Statement.WHILE() | ||
| 146 | ✗ | then List.fold(stmt.body, statementOutputs, crefs); | |
| 147 | |||
| 148 | else crefs; | ||
| 149 | end match; | ||
| 150 | end statementOutputs; | ||
| 151 | |||
| 152 | function statementOutputCrefFinder | ||
| 153 | input Expression exp; | ||
| 154 | input output UnorderedSet<ComponentRef> crefs; | ||
| 155 | protected | ||
| 156 | ComponentRef cref; | ||
| 157 | algorithm | ||
| 158 | crefs := match exp | ||
| 159 | case Expression.CREF() | ||
| 160 | algorithm | ||
| 161 | ✗ | cref := ComponentRef.stripSubscripts(exp.cref); | |
| 162 | ✗ | then Expression.fold(ExpandExp.expand(Expression.fromCref(cref)), statementOutputCrefFinder2, crefs); | |
| 163 | |||
| 164 | else crefs; | ||
| 165 | end match; | ||
| 166 | end statementOutputCrefFinder; | ||
| 167 | |||
| 168 | function statementOutputCrefFinder2 | ||
| 169 | input Expression exp; | ||
| 170 | input output UnorderedSet<ComponentRef> crefs; | ||
| 171 | algorithm | ||
| 172 | () := match exp | ||
| 173 | case Expression.CREF() | ||
| 174 | guard ComponentRef.isCref(exp.cref) and not ComponentRef.isIterator(exp.cref) | ||
| 175 | algorithm | ||
| 176 | ✗ | UnorderedSet.add(exp.cref, crefs); | |
| 177 | then | ||
| 178 | (); | ||
| 179 | |||
| 180 | else (); | ||
| 181 | end match; | ||
| 182 | |||
| 183 | end statementOutputCrefFinder2; | ||
| 184 | |||
| 185 | annotation(__OpenModelica_Interface="nf_frontend"); | ||
| 186 | end NFCheckModel; | ||
| 187 |