OMCompiler/Compiler/NBackEnd/Modules/NBModule.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 NBModule | ||
| 37 | " file: NBModule.mo | ||
| 38 | package: NBModule | ||
| 39 | description: This file contains all functions and structures regarding | ||
| 40 | generic backend modules and interfaces. | ||
| 41 | |||
| 42 | This file contains following module wrappers: | ||
| 43 | |||
| 44 | *** PRE (Mandatory) | ||
| 45 | - eventsInterface | ||
| 46 | - detectStatesInterface | ||
| 47 | - detectContinuousStatesInterface | ||
| 48 | - detectDiscreteStatesInterface | ||
| 49 | |||
| 50 | *** PRE (Optional) | ||
| 51 | - aliasInterface | ||
| 52 | |||
| 53 | *** MAIN | ||
| 54 | - partitioningInterface | ||
| 55 | - causalizeInterface | ||
| 56 | - daeModeInterface | ||
| 57 | |||
| 58 | *** POST (Mandatory) | ||
| 59 | - jacobianInterface | ||
| 60 | |||
| 61 | *** POST (Optional) | ||
| 62 | - tearingInterface | ||
| 63 | " | ||
| 64 | public | ||
| 65 | import BackendDAE = NBackendDAE; | ||
| 66 | |||
| 67 | protected | ||
| 68 | // OF imports | ||
| 69 | import Absyn.Path; | ||
| 70 | import DAE; | ||
| 71 | |||
| 72 | // NF imports | ||
| 73 | import NFFunction.Function; | ||
| 74 | |||
| 75 | // Backend imports | ||
| 76 | import Adjacency = NBAdjacency; | ||
| 77 | import BEquation = NBEquation; | ||
| 78 | import NBEquation.{Equation, EquationPointers, EqData}; | ||
| 79 | import Jacobian = NBackendDAE; | ||
| 80 | import NBJacobian.JacobianType; | ||
| 81 | import StrongComponent = NBStrongComponent; | ||
| 82 | import Matching = NBMatching; | ||
| 83 | import Partition = NBPartition; | ||
| 84 | import NBPartitioning.ClockedInfo; | ||
| 85 | import BVariable = NBVariable; | ||
| 86 | import NBVariable.{VariablePointers, VarData}; | ||
| 87 | import NBEvents.EventInfo; | ||
| 88 | import Inline = NBInline; | ||
| 89 | |||
| 90 | // Util imports | ||
| 91 | import System; | ||
| 92 | import StringUtil; | ||
| 93 | |||
| 94 | public | ||
| 95 | partial function wrapper | ||
| 96 | input output BackendDAE bdae; | ||
| 97 | end wrapper; | ||
| 98 | |||
| 99 | function moduleClockString | ||
| 100 | input tuple<String, Real> name_clock; | ||
| 101 | output String str; | ||
| 102 | protected | ||
| 103 | String name; | ||
| 104 | Real clck; | ||
| 105 | algorithm | ||
| 106 | ✗ | (name, clck) := name_clock; | |
| 107 | ✗ | str := "\t" + name + StringUtil.repeat(".", 50 - stringLength(name)) + System.sprintff("%.4g", clck); | |
| 108 | end moduleClockString; | ||
| 109 | |||
| 110 | // ========================================================================= | ||
| 111 | // MAIN MODULES | ||
| 112 | // ========================================================================= | ||
| 113 | |||
| 114 | // PARTITIONING | ||
| 115 | // ************************************************************************* | ||
| 116 | partial function partitioningInterface | ||
| 117 | "Partitioning | ||
| 118 | This function is only allowed to create partitions of specialized Kind | ||
| 119 | by creating an adjacency matrix using provided variables and equations." | ||
| 120 | input Partition.Kind kind; | ||
| 121 | input VariablePointers variables; | ||
| 122 | input EquationPointers equations; | ||
| 123 | input VariablePointers clocks; | ||
| 124 | input EquationPointers clocked; | ||
| 125 | input ClockedInfo info; | ||
| 126 | output list<Partition.Partition> partitions; | ||
| 127 | end partitioningInterface; | ||
| 128 | |||
| 129 | // CAUSALIZE | ||
| 130 | // ************************************************************************* | ||
| 131 | partial function causalizeInterface | ||
| 132 | "Causalize | ||
| 133 | This function is allowed to add variables, equations and manipulate the | ||
| 134 | function tree (index reduction)." | ||
| 135 | input output Partition.Partition partition; | ||
| 136 | input output VarData varData; | ||
| 137 | input output EqData eqData; | ||
| 138 | input UnorderedMap<Path, Function> funcMap; | ||
| 139 | input output list<Partition.Partition> twins "partitions of nearly the same system, causalized from this one's matrices"; | ||
| 140 | end causalizeInterface; | ||
| 141 | |||
| 142 | // RESOLVING SINGULARITIES | ||
| 143 | // Index Reduction + Balance Initialization | ||
| 144 | // ************************************************************************* | ||
| 145 | partial function resolveSingularitiesInterface | ||
| 146 | input output Adjacency.Matrix adj; | ||
| 147 | input output Adjacency.Matrix full; | ||
| 148 | input output VariablePointers variables; | ||
| 149 | input output EquationPointers equations; | ||
| 150 | input output VarData varData; | ||
| 151 | input output EqData eqData; | ||
| 152 | input Partition.Kind kind; | ||
| 153 | input UnorderedMap<Path, Function> funcMap; | ||
| 154 | input Matching matching; | ||
| 155 | input Option<Adjacency.Mapping> mapping_opt; | ||
| 156 | output Boolean changed; | ||
| 157 | end resolveSingularitiesInterface; | ||
| 158 | |||
| 159 | // DAEMODE | ||
| 160 | // ************************************************************************* | ||
| 161 | partial function daeModeInterface | ||
| 162 | "DAEMode | ||
| 163 | This function is only allowed to create a list of new partitions for dae Mode." | ||
| 164 | input output list<Partition.Partition> partitions; | ||
| 165 | input VariablePointers variables; | ||
| 166 | input Pointer<Integer> uniqueIndex; | ||
| 167 | end daeModeInterface; | ||
| 168 | |||
| 169 | // ========================================================================= | ||
| 170 | // MANDATORY PRE-OPT MODULES | ||
| 171 | // ========================================================================= | ||
| 172 | |||
| 173 | // COLLECT EVENTS | ||
| 174 | // ************************************************************************* | ||
| 175 | partial function eventsInterface | ||
| 176 | "Events | ||
| 177 | This function is only allowed to read and change equations and create new | ||
| 178 | discrete zero crossing equations and variables. ($TEV, $SEV) | ||
| 179 | It also fills the EventInfo object." | ||
| 180 | input output VarData varData "Data containing variable pointers"; | ||
| 181 | input output EqData eqData "Data containing equation pointers"; | ||
| 182 | input output EventInfo eventInfo "object containing all zero crossings"; | ||
| 183 | input UnorderedMap<Path, Function> funcMap "function tree for differentiation (solve)"; | ||
| 184 | end eventsInterface; | ||
| 185 | |||
| 186 | // DETECT STATES | ||
| 187 | // ************************************************************************* | ||
| 188 | partial function detectStatesInterface | ||
| 189 | "DetectStates | ||
| 190 | This function is only allowed to read and change equations, change algebraic | ||
| 191 | variables to states and create state derivatives. It also detects der() and | ||
| 192 | pre() calls and replaces them with $DER and $PRE. | ||
| 193 | Sub-Modules: | ||
| 194 | - DetectContinuousStates | ||
| 195 | - DetectDiscreteStates" | ||
| 196 | input output VarData varData "Data containing variable pointers"; | ||
| 197 | input output EqData eqData "Data containing equation pointers"; | ||
| 198 | input detectContinuousStatesInterface continuousFunc "Subroutine for continuous states"; | ||
| 199 | input detectDiscreteStatesInterface discreteFunc "Subroutine for discrete states"; | ||
| 200 | end detectStatesInterface; | ||
| 201 | |||
| 202 | partial function detectContinuousStatesInterface | ||
| 203 | "DetectContinuousStates | ||
| 204 | This function is only allowed to read and change equations, change algebraic | ||
| 205 | variables to states and create state derivatives." | ||
| 206 | input output VariablePointers variables "All variables"; | ||
| 207 | input output VariablePointers unknowns "Unknowns"; | ||
| 208 | input output VariablePointers knowns "Knowns"; | ||
| 209 | input output VariablePointers initials "Initial unknowns"; | ||
| 210 | input output VariablePointers states "States"; | ||
| 211 | input output VariablePointers derivatives "State derivatives (der(x) -> $DER.x)"; | ||
| 212 | input output VariablePointers algebraics "Algebraic variables"; | ||
| 213 | input EquationPointers equations "Partition equations"; | ||
| 214 | output list<Pointer<Equation>> aux_eqns "New auxiliary equations"; | ||
| 215 | end detectContinuousStatesInterface; | ||
| 216 | |||
| 217 | partial function detectDiscreteStatesInterface | ||
| 218 | "DetectDiscreteStates | ||
| 219 | This function is only allowed to read and change equations, change algebraic | ||
| 220 | variables to discrete and create previous discrete variables." | ||
| 221 | input output VariablePointers variables "All variables"; | ||
| 222 | input output EquationPointers equations "ONLY discrete or initial equations!"; | ||
| 223 | input output VariablePointers knowns "Knowns"; | ||
| 224 | input output VariablePointers initials "Initial unknowns"; | ||
| 225 | input output VariablePointers discretes "Discrete variables"; | ||
| 226 | input output VariablePointers discrete_states "Discrete State variables"; | ||
| 227 | input output VariablePointers clocked_states "Clocked State variables"; | ||
| 228 | input output VariablePointers previous "Previous discrete variables (pre(d) -> $PRE.d)"; | ||
| 229 | input String context "only for debugging"; | ||
| 230 | end detectDiscreteStatesInterface; | ||
| 231 | |||
| 232 | // ========================================================================= | ||
| 233 | // Optional PRE-OPT MODULES | ||
| 234 | // ========================================================================= | ||
| 235 | |||
| 236 | // ALIAS | ||
| 237 | // ************************************************************************* | ||
| 238 | partial function functionAliasInterface | ||
| 239 | "Alias | ||
| 240 | This module is allowed to read and remove equations and move variables from | ||
| 241 | unknowns to knowns. Since this can also affect all other pointer arrays, the | ||
| 242 | full variable data is needed. All things that are allowed to be changed | ||
| 243 | are pointers, so no return value." | ||
| 244 | input output VarData varData "Data containing variable pointers"; | ||
| 245 | input output EqData eqData "Data containing equation pointers"; | ||
| 246 | input Partition.Kind kind; | ||
| 247 | end functionAliasInterface; | ||
| 248 | |||
| 249 | |||
| 250 | partial function aliasInterface | ||
| 251 | "Alias | ||
| 252 | This module is allowed to read and remove equations and move variables from | ||
| 253 | unknowns to knowns. Since this can also affect all other pointer arrays, the | ||
| 254 | full variable data is needed. All things that are allowed to be changed | ||
| 255 | are pointers, so no return value." | ||
| 256 | input output VarData varData "Data containing variable pointers"; | ||
| 257 | input output EqData eqData "Data containing equation pointers"; | ||
| 258 | input Partition.Kind kind; | ||
| 259 | end aliasInterface; | ||
| 260 | |||
| 261 | // INLINE | ||
| 262 | // ************************************************************************* | ||
| 263 | partial function inlineInterface | ||
| 264 | "Inline | ||
| 265 | This module is allowed to read, change and add equations. It uses the | ||
| 266 | function tree to evaluate and inline functions." | ||
| 267 | input output EqData eqData "Data containing equation pointers"; | ||
| 268 | input output VarData varData "Data containing variable pointers, for lowering purposes"; | ||
| 269 | input UnorderedMap<Path, Function> funcMap "function tree for differentiation (solve)"; | ||
| 270 | input list<DAE.InlineType> inline_types "Inline types for which to inline at the current state"; | ||
| 271 | input Boolean init "true if for initial partition"; | ||
| 272 | end inlineInterface; | ||
| 273 | |||
| 274 | // ========================================================================= | ||
| 275 | // MANDATORY POST-OPT MODULES | ||
| 276 | // ========================================================================= | ||
| 277 | |||
| 278 | // JACOBIAN | ||
| 279 | // ************************************************************************* | ||
| 280 | partial function jacobianInterface | ||
| 281 | "The jacobian is only allowed to read the variables and equations of current | ||
| 282 | partition and additionally the global known variables. It needs a unique name | ||
| 283 | and is allowed to manipulate the function tree. | ||
| 284 | [!] This function can not only be used as an optimization module but also for | ||
| 285 | nonlinear partitions, state sets, linearization and dynamic optimization." | ||
| 286 | input String name "Name of jacobian"; | ||
| 287 | input JacobianType jacType "Type of jacobian (ode/dae/lin/nonlin)"; | ||
| 288 | input VariablePointers seedCandidates "differentiate by these"; | ||
| 289 | input VariablePointers partialCandidates "solve the equations for these"; | ||
| 290 | input EquationPointers equations "Equations array"; | ||
| 291 | input Option<array<StrongComponent>> strongComponents "Strong Components"; | ||
| 292 | input Option<Adjacency.Matrix> full "full adjacency matrix to create sparsity pattern"; | ||
| 293 | output Option<Jacobian> jacobian "Resulting jacobian"; | ||
| 294 | input UnorderedMap<Path, Function> funcMap "Function call bodies"; | ||
| 295 | input Boolean staticAsContinuous "Treat static variables (constant over time, e.g. params) as continuous if these have a continuous type (Real)"; | ||
| 296 | end jacobianInterface; | ||
| 297 | |||
| 298 | // ========================================================================= | ||
| 299 | // Optional POST-OPT MODULES | ||
| 300 | // ========================================================================= | ||
| 301 | |||
| 302 | // TEARING | ||
| 303 | // ************************************************************************* | ||
| 304 | partial function tearingInterface | ||
| 305 | "Tearing | ||
| 306 | The tearing module analyzes each strong component and applies tearing if | ||
| 307 | necessary. Only has access to the strong component itself, everything else | ||
| 308 | accessable with pointers." | ||
| 309 | input output StrongComponent comp "the suspected algebraic loop."; | ||
| 310 | input output Adjacency.Matrix full "the full adjacency matrix containing solvability info"; | ||
| 311 | input UnorderedMap<Path, Function> funcMap "Function call bodies"; | ||
| 312 | input output Integer index "current unique loop index"; | ||
| 313 | input VariablePointers variables "all variables"; | ||
| 314 | input EquationPointers equations "all equations"; | ||
| 315 | input Pointer<Integer> eq_index "equation index"; | ||
| 316 | input Partition.Kind kind = NBPartition.Kind.ODE "partition type"; | ||
| 317 | end tearingInterface; | ||
| 318 | |||
| 319 | annotation(__OpenModelica_Interface="nbackend"); | ||
| 320 | end NBModule; | ||
| 321 |