OMCompiler/Compiler/NSimCode/NSimPartition.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 NSimPartition | ||
| 37 | "file: NSimPartition.mo | ||
| 38 | package: NSimPartition | ||
| 39 | description: This file contains the data types and functions for clocked partitions | ||
| 40 | in simulation code phase. | ||
| 41 | " | ||
| 42 | public | ||
| 43 | // self import | ||
| 44 | import SimPartition = NSimPartition; | ||
| 45 | |||
| 46 | // simcode imports | ||
| 47 | import NSimStrongComponent.Block; | ||
| 48 | import NSimCode.SimCodeIndices; | ||
| 49 | import NSimVar.SimVar; | ||
| 50 | |||
| 51 | // frontend import | ||
| 52 | import BuiltinFuncs = NFBuiltinFuncs; | ||
| 53 | import Call = NFCall; | ||
| 54 | import ClockKind = NFClockKind; | ||
| 55 | import ComponentRef = NFComponentRef; | ||
| 56 | import Expression = NFExpression; | ||
| 57 | import Prefixes = NFPrefixes; | ||
| 58 | |||
| 59 | // backend imports | ||
| 60 | import NBPartitioning.BClock; | ||
| 61 | import NBEquation.{EquationKind, EquationAttributes, WhenStatement}; | ||
| 62 | |||
| 63 | // import old simcode and frontend | ||
| 64 | import DAE; | ||
| 65 | import OldSimCode = SimCode; | ||
| 66 | |||
| 67 | record BASE_PARTITION | ||
| 68 | BClock baseClock; | ||
| 69 | list<SimPartition> subPartitions; | ||
| 70 | end BASE_PARTITION; | ||
| 71 | |||
| 72 | record SUB_PARTITION | ||
| 73 | list<tuple<SimVar, Boolean /*previous*/>> variables; | ||
| 74 | list<Block> equations; | ||
| 75 | list<Block> removedEquations; | ||
| 76 | BClock subClock; | ||
| 77 | Boolean holdEvents; | ||
| 78 | end SUB_PARTITION; | ||
| 79 | |||
| 80 | function createSubPartition | ||
| 81 | input BClock subClock; | ||
| 82 | input list<Block> equations; | ||
| 83 | input list<SimVar> variables; | ||
| 84 | input Boolean holdEvents; | ||
| 85 | output SimPartition part; | ||
| 86 | algorithm | ||
| 87 | // for now assume all variables need pre() | ||
| 88 |
5/6✓ Branch 0 taken 24 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 18 times.
✓ Branch 5 taken 18 times.
✗ Branch 6 not taken.
|
60 | part := SUB_PARTITION(list((v, true) for v in variables), equations, {}, subClock, holdEvents); |
| 89 | end createSubPartition; | ||
| 90 | |||
| 91 | function createBasePartitions | ||
| 92 | input UnorderedMap<BClock, list<SimPartition>> clock_collector; | ||
| 93 | output list<SimPartition> baseParts = {}; | ||
| 94 | output list<Block> eventClocks = {}; | ||
| 95 | input output SimCodeIndices simCodeIndices; | ||
| 96 | protected | ||
| 97 | BClock baseClock; | ||
| 98 | list<SimPartition> subClocks; | ||
| 99 | Integer clock_idx = 1; | ||
| 100 | algorithm | ||
| 101 | // create all base partitions, sort the sub partitions according to their clock dependencies | ||
| 102 |
2/2✓ Branch 1 taken 12 times.
✓ Branch 2 taken 188 times.
|
200 | for tpl in UnorderedMap.toList(clock_collector) loop |
| 103 | 12 | (baseClock, subClocks) := tpl; | |
| 104 |
2/2✓ Branch 1 taken 11 times.
✓ Branch 2 taken 1 time.
|
12 | if not BClock.isInferredClock(baseClock) then |
| 105 | 11 | baseParts := BASE_PARTITION(baseClock, subClocks) :: baseParts; | |
| 106 | end if; | ||
| 107 | end for; | ||
| 108 | |||
| 109 | // collect all event clocks | ||
| 110 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 188 times.
|
199 | for base in baseParts loop |
| 111 | () := match base | ||
| 112 | local | ||
| 113 | ComponentRef cond; | ||
| 114 | DAE.ElementSource source; | ||
| 115 | Expression fire; | ||
| 116 | WhenStatement stmt; | ||
| 117 | EquationAttributes attr; | ||
| 118 | Block blck; | ||
| 119 | |||
| 120 | case BASE_PARTITION(baseClock = BClock.BASE_CLOCK( | ||
| 121 | clock = ClockKind.EVENT_CLOCK(condition = Expression.CREF(cref = cond)))) | ||
| 122 | algorithm | ||
| 123 | // create a no-return when equation that triggers clock fire | ||
| 124 | // when (condition) then $_clkfire(i) | ||
| 125 | source := DAE.emptyElementSource; | ||
| 126 | 6 | fire := Expression.CALL(Call.makeTypedCall( | |
| 127 | fn = NFBuiltinFuncs.CLOCK_FIRE, | ||
| 128 | args = {Expression.INTEGER(clock_idx)}, | ||
| 129 | variability = NFPrefixes.Variability.CONSTANT, | ||
| 130 | purity = NFPrefixes.Purity.PURE)); | ||
| 131 | 3 | stmt := WhenStatement.NORETCALL(fire, source); | |
| 132 | 3 | attr := EquationAttributes.default(EquationKind.EMPTY, false); | |
| 133 | 3 | blck := Block.WHEN(simCodeIndices.equationIndex, false, {cond}, {stmt}, NONE(), source, attr); | |
| 134 | |||
| 135 | eventClocks := blck :: eventClocks; | ||
| 136 | 3 | simCodeIndices.equationIndex := simCodeIndices.equationIndex + 1; | |
| 137 | then (); | ||
| 138 | else (); | ||
| 139 | end match; | ||
| 140 | 11 | clock_idx := clock_idx + 1; | |
| 141 | end for; | ||
| 142 | end createBasePartitions; | ||
| 143 | |||
| 144 | function getClock | ||
| 145 | input SimPartition part; | ||
| 146 | output BClock clock; | ||
| 147 | algorithm | ||
| 148 | clock := match part | ||
| 149 | ✗ | case BASE_PARTITION() then part.baseClock; | |
| 150 | ✗ | case SUB_PARTITION() then part.subClock; | |
| 151 | else algorithm | ||
| 152 | ✗ | Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for unknown partition:\n" + toString(part)}); | |
| 153 | ✗ | then fail(); | |
| 154 | end match; | ||
| 155 | end getClock; | ||
| 156 | |||
| 157 | function listToString | ||
| 158 | input list<SimPartition> parts; | ||
| 159 | input output String str = ""; | ||
| 160 | input String header = ""; | ||
| 161 | protected | ||
| 162 | String indent = str; | ||
| 163 | algorithm | ||
| 164 | ✗ | str := if header <> "" then StringUtil.headline_3(header) else ""; | |
| 165 | ✗ | for part in parts loop | |
| 166 | ✗ | str := str + toString(part, indent); | |
| 167 | end for; | ||
| 168 | end listToString; | ||
| 169 | |||
| 170 | function toString | ||
| 171 | input SimPartition part; | ||
| 172 | input output String str = ""; | ||
| 173 | algorithm | ||
| 174 | str := match part | ||
| 175 | ✗ | case BASE_PARTITION() then "[BASE] Partition " + BClock.toString(part.baseClock) + List.toString(part.subPartitions, function toString(str = str), List.Style.NEWLINE) + "\n"; | |
| 176 | ✗ | case SUB_PARTITION() then str + "[SUB-] Partition " + BClock.toString(part.subClock) + List.toString(part.equations, function Block.toString(str = str), List.Style.NEWLINE); | |
| 177 | else "[ERR-]"; | ||
| 178 | end match; | ||
| 179 | end toString; | ||
| 180 | |||
| 181 | function toStringShort | ||
| 182 | input SimPartition part; | ||
| 183 | output String str; | ||
| 184 | algorithm | ||
| 185 | str := match part | ||
| 186 | ✗ | case BASE_PARTITION() then "[BASE] Partition " + BClock.toString(part.baseClock); | |
| 187 | ✗ | case SUB_PARTITION() then "[SUB-] Partition " + BClock.toString(part.subClock); | |
| 188 | else "[ERR-]"; | ||
| 189 | end match; | ||
| 190 | end toStringShort; | ||
| 191 | |||
| 192 | function convertBase | ||
| 193 | input SimPartition part; | ||
| 194 | output OldSimCode.ClockedPartition oldPart; | ||
| 195 | algorithm | ||
| 196 | oldPart := match part | ||
| 197 |
4/4✓ Branch 0 taken 18 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 11 times.
|
29 | case BASE_PARTITION() then OldSimCode.CLOCKED_PARTITION( |
| 198 | baseClock = BClock.convertBase(part.baseClock), | ||
| 199 | subPartitions = list(convertSub(sub) for sub in part.subPartitions)); | ||
| 200 | else algorithm | ||
| 201 | ✗ | Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for non-base partition:\n" + toString(part)}); | |
| 202 | ✗ | then fail(); | |
| 203 | end match; | ||
| 204 | end convertBase; | ||
| 205 | |||
| 206 | function convertSub | ||
| 207 | input SimPartition part; | ||
| 208 | output OldSimCode.SubPartition oldPart; | ||
| 209 | algorithm | ||
| 210 | oldPart := match part | ||
| 211 |
10/12✓ Branch 0 taken 24 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 18 times.
✓ Branch 5 taken 24 times.
✓ Branch 6 taken 18 times.
✓ Branch 7 taken 24 times.
✓ Branch 8 taken 18 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 18 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 18 times.
|
66 | case SUB_PARTITION() then OldSimCode.SUBPARTITION( |
| 212 | vars = list(SimVar.convertTpl(tpl) for tpl in part.variables), | ||
| 213 | equations = list(Block.convert(blck) for blck in part.equations), | ||
| 214 | removedEquations = list(Block.convert(blck) for blck in part.removedEquations), | ||
| 215 | subClock = BClock.convertSub(part.subClock), | ||
| 216 | holdEvents = part.holdEvents); | ||
| 217 | else algorithm | ||
| 218 | ✗ | Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for non-base partition:\n" + toString(part)}); | |
| 219 | ✗ | then fail(); | |
| 220 | end match; | ||
| 221 | end convertSub; | ||
| 222 | |||
| 223 | annotation(__OpenModelica_Interface="nbackend"); | ||
| 224 | end NSimPartition; | ||
| 225 |