OMCompiler/Compiler/SimCode/SerializeInitXML.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 SerializeInitXML | ||
| 37 | |||
| 38 | import Absyn; | ||
| 39 | import SimCode; | ||
| 40 | import SimCode.ModelInfo; | ||
| 41 | import SimCode.SimCode.SIMCODE; | ||
| 42 | import File; | ||
| 43 | import SimCodeVar; | ||
| 44 | |||
| 45 | protected | ||
| 46 | import BackendDAE.VarKind; | ||
| 47 | import ClassInf; | ||
| 48 | import CR=ComponentReference; | ||
| 49 | import ComponentReferenceBasics; | ||
| 50 | import DAE; | ||
| 51 | import DAE.{Exp,Type}; | ||
| 52 | import DAEUtil; | ||
| 53 | import Dump; | ||
| 54 | import Error; | ||
| 55 | import Expression; | ||
| 56 | import ExpressionBasics; | ||
| 57 | import ExpressionBasics.printExpStr; | ||
| 58 | import File.Escape.XML; | ||
| 59 | import Settings; | ||
| 60 | import SimCode.{SimulationSettings,VarInfo}; | ||
| 61 | import SimCodeVar.{AliasVariable,Causality,SimVar}; | ||
| 62 | import Types; | ||
| 63 | import TypesDump; | ||
| 64 | import UnorderedMap; | ||
| 65 | import Util; | ||
| 66 | import SimCodeCodegenUtil; | ||
| 67 | |||
| 68 | public | ||
| 69 | |||
| 70 | function simulationInitFile | ||
| 71 | "Generates the contents of the init.xml file for the simulation case." | ||
| 72 | input SimCode.SimCode simCode; | ||
| 73 | input String guid; | ||
| 74 | algorithm | ||
| 75 | ✗ | true := simulationInitFileReturnBool(simCode, guid); | |
| 76 | end simulationInitFile; | ||
| 77 | |||
| 78 | function simulationInitFileReturnBool | ||
| 79 | "Generates the contents of the init.xml file for the simulation case." | ||
| 80 | input SimCode.SimCode simCode; | ||
| 81 | input String guid; | ||
| 82 | output Boolean success = false; | ||
| 83 | protected | ||
| 84 | VarInfo vi; | ||
| 85 | SimulationSettings s; | ||
| 86 | File.File file = File.File(); | ||
| 87 | String fileName; | ||
| 88 | algorithm | ||
| 89 | try | ||
| 90 | 1122 | fileName := simCode.fileNamePrefix + "_init.xml"; | |
| 91 | 1122 | File.open(file, fileName, File.Mode.Write); | |
| 92 | |||
| 93 | 1122 | vi := simCode.modelInfo.varInfo; | |
| 94 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 1122 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1122 times.
|
1122 | SOME(s) := simCode.simulationSettingsOpt; |
| 95 | |||
| 96 | |||
| 97 | 1122 | File.write(file, "<?xml version = \"1.0\" encoding=\"UTF-8\"?>\n\n"); | |
| 98 | 1122 | File.write(file, "<!-- description of the model interface using an extention of the FMI standard -->\n"); | |
| 99 | 1122 | File.write(file, "<fmiModelDescription\n"); | |
| 100 | 1122 | File.write(file, " fmiVersion = \"1.0\"\n\n"); | |
| 101 | |||
| 102 | 1122 | File.write(file, " modelName = \""); | |
| 103 | 1122 | Dump.writePath(file, simCode.modelInfo.name, initialDot=false); | |
| 104 | 1122 | File.write(file, "\"\n"); | |
| 105 | |||
| 106 | 1122 | File.write(file, " modelIdentifier = \""); | |
| 107 | 1122 | Dump.writePath(file, simCode.modelInfo.name, delimiter="_", initialDot=false); | |
| 108 | 1122 | File.write(file, "\"\n\n"); | |
| 109 | |||
| 110 | 1122 | File.write(file, " OPENMODELICAHOME = \""); | |
| 111 | 1122 | File.write(file, simCode.makefileParams.omhome); | |
| 112 | 1122 | File.write(file, "\"\n\n"); | |
| 113 | |||
| 114 | |||
| 115 | 1122 | File.write(file, " guid = \"{"); | |
| 116 | 1122 | File.write(file, guid); | |
| 117 | 1122 | File.write(file, "}\"\n\n"); | |
| 118 | |||
| 119 | |||
| 120 | 1122 | File.write(file, " description = \""); | |
| 121 | 1122 | File.writeEscape(file, simCode.modelInfo.description, XML); | |
| 122 | 1122 | File.write(file, "\"\n"); | |
| 123 | |||
| 124 | 1122 | File.write(file, " generationTool = \"OpenModelica Compiler "); | |
| 125 | 1122 | File.write(file, Settings.getVersionNr()); | |
| 126 | 1122 | File.write(file, "\"\n"); | |
| 127 | |||
| 128 | 1122 | File.write(file, " generationDateAndTime = \""); | |
| 129 | 1122 | xsdateTime(file, Util.getCurrentDateTime()); | |
| 130 | 1122 | File.write(file, "\"\n\n"); | |
| 131 | |||
| 132 | |||
| 133 | 1122 | File.write(file, " variableNamingConvention = \"structured\"\n\n"); | |
| 134 | |||
| 135 | 1122 | File.write(file, " numberOfEventIndicators = \""); | |
| 136 | 1122 | File.writeInt(file, vi.numZeroCrossings); | |
| 137 | 1122 | File.write(file, "\" cmt_numberOfEventIndicators = \"NG: number of zero crossings, FMI\"\n"); | |
| 138 | |||
| 139 | 1122 | File.write(file, " numberOfTimeEvents = \""); | |
| 140 | 1122 | File.writeInt(file, vi.numTimeEvents); | |
| 141 | 1122 | File.write(file, "\" cmt_numberOfTimeEvents = \"NG_SAM: number of zero crossings that are samples, OMC\"\n\n"); | |
| 142 | |||
| 143 | |||
| 144 | 1122 | File.write(file, " numberOfInputVariables = \""); | |
| 145 | 1122 | File.writeInt(file, vi.numInVars); | |
| 146 | 1122 | File.write(file, "\" cmt_numberOfInputVariables = \"NI: number of inputvar on topmodel, OMC\"\n"); | |
| 147 | |||
| 148 | 1122 | File.write(file, " numberOfOutputVariables = \""); | |
| 149 | 1122 | File.writeInt(file, vi.numOutVars); | |
| 150 | 1122 | File.write(file, "\" cmt_numberOfOutputVariables = \"NO: number of outputvar on topmodel, OMC\"\n\n"); | |
| 151 | |||
| 152 | 1122 | File.write(file, " numberOfExternalObjects = \""); | |
| 153 | 1122 | File.writeInt(file, vi.numExternalObjects); | |
| 154 | |||
| 155 | 1122 | File.write(file, "\" cmt_numberOfExternalObjects = \"NEXT: number of external objects, OMC\"\n"); | |
| 156 | |||
| 157 | 1122 | File.write(file, " numberOfFunctions = \""); | |
| 158 | 1122 | File.writeInt(file, listLength(simCode.modelInfo.functions)); | |
| 159 | 1122 | File.write(file, "\" cmt_numberOfFunctions = \"NFUNC: number of functions used by the simulation, OMC\"\n\n"); | |
| 160 | |||
| 161 | |||
| 162 | 1122 | File.write(file, " numberOfContinuousStates = \""); | |
| 163 | 1122 | File.writeInt(file, vi.numStateVars); | |
| 164 | 1122 | File.write(file, "\" cmt_numberOfContinuousStates = \"NX: number of states, FMI\"\n"); | |
| 165 | |||
| 166 | 1122 | File.write(file, " numberOfRealAlgebraicVariables = \""); | |
| 167 | 1122 | File.writeInt(file, vi.numAlgVars+vi.numDiscreteReal+vi.numOptimizeConstraints+vi.numOptimizeFinalConstraints); | |
| 168 | 1122 | File.write(file, "\" cmt_numberOfRealAlgebraicVariables = \"NY: number of real variables, OMC\"\n"); | |
| 169 | |||
| 170 | 1122 | File.write(file, " numberOfRealAlgebraicAliasVariables = \""); | |
| 171 | 1122 | File.writeInt(file, vi.numAlgAliasVars); | |
| 172 | 1122 | File.write(file, "\" cmt_numberOfRealAlgebraicAliasVariables = \"NA: number of alias variables, OMC\"\n"); | |
| 173 | |||
| 174 | 1122 | File.write(file, " numberOfRealParameters = \""); | |
| 175 | 1122 | File.writeInt(file, vi.numParams); | |
| 176 | 1122 | File.write(file, "\" cmt_numberOfRealParameters = \"NP: number of parameters, OMC\"\n\n"); | |
| 177 | |||
| 178 | |||
| 179 | 1122 | File.write(file, " numberOfIntegerAlgebraicVariables = \""); | |
| 180 | 1122 | File.writeInt(file, vi.numIntAlgVars); | |
| 181 | 1122 | File.write(file, "\" cmt_numberOfIntegerAlgebraicVariables = \"NYINT: number of alg. int variables, OMC\"\n"); | |
| 182 | |||
| 183 | 1122 | File.write(file, " numberOfIntegerAliasVariables = \""); | |
| 184 | 1122 | File.writeInt(file, vi.numIntAliasVars); | |
| 185 | 1122 | File.write(file, "\" cmt_numberOfIntegerAliasVariables = \"NAINT: number of alias int variables, OMC\"\n"); | |
| 186 | |||
| 187 | 1122 | File.write(file, " numberOfIntegerParameters = \""); | |
| 188 | 1122 | File.writeInt(file, vi.numIntParams); | |
| 189 | 1122 | File.write(file, "\" cmt_numberOfIntegerParameters = \"NPINT: number of int parameters, OMC\"\n\n"); | |
| 190 | |||
| 191 | 1122 | File.write(file, " numberOfStringAlgebraicVariables = \""); | |
| 192 | 1122 | File.writeInt(file, vi.numStringAlgVars); | |
| 193 | 1122 | File.write(file, "\" cmt_numberOfStringAlgebraicVariables = \"NYSTR: number of alg. string variables, OMC\"\n"); | |
| 194 | |||
| 195 | 1122 | File.write(file, " numberOfStringAliasVariables = \""); | |
| 196 | 1122 | File.writeInt(file, vi.numStringAliasVars); | |
| 197 | 1122 | File.write(file, "\" cmt_numberOfStringAliasVariables = \"NASTR: number of alias string variables, OMC\"\n"); | |
| 198 | |||
| 199 | 1122 | File.write(file, " numberOfStringParameters = \""); | |
| 200 | 1122 | File.writeInt(file, vi.numStringParamVars); | |
| 201 | 1122 | File.write(file, "\" cmt_numberOfStringParameters = \"NPSTR: number of string parameters, OMC\"\n\n"); | |
| 202 | |||
| 203 | |||
| 204 | 1122 | File.write(file, " numberOfBooleanAlgebraicVariables = \""); | |
| 205 | 1122 | File.writeInt(file, vi.numBoolAlgVars); | |
| 206 | 1122 | File.write(file, "\" cmt_numberOfBooleanAlgebraicVariables = \"NYBOOL: number of alg. bool variables, OMC\"\n"); | |
| 207 | |||
| 208 | 1122 | File.write(file, " numberOfBooleanAliasVariables = \""); | |
| 209 | 1122 | File.writeInt(file, vi.numBoolAliasVars); | |
| 210 | 1122 | File.write(file, "\" cmt_numberOfBooleanAliasVariables = \"NABOOL: number of alias bool variables, OMC\"\n"); | |
| 211 | |||
| 212 | 1122 | File.write(file, " numberOfBooleanParameters = \""); | |
| 213 | 1122 | File.writeInt(file, vi.numBoolParams); | |
| 214 | 1122 | File.write(file, "\" cmt_numberOfBooleanParameters = \"NPBOOL: number of bool parameters, OMC\" >\n\n\n"); | |
| 215 | |||
| 216 | |||
| 217 | 1122 | File.write(file, " <!-- startTime, stopTime, tolerance are FMI specific, all others are OMC specific -->\n"); | |
| 218 | |||
| 219 | 1122 | File.write(file, " <DefaultExperiment\n"); | |
| 220 | |||
| 221 | 1122 | File.write(file, " startTime = \""); | |
| 222 | 1122 | File.writeReal(file, s.startTime); | |
| 223 | 1122 | File.write(file, "\"\n"); | |
| 224 | |||
| 225 | 1122 | File.write(file, " stopTime = \""); | |
| 226 | 1122 | File.writeReal(file, s.stopTime); | |
| 227 | 1122 | File.write(file, "\"\n"); | |
| 228 | |||
| 229 | 1122 | File.write(file, " stepSize = \""); | |
| 230 | 1122 | File.writeReal(file, s.stepSize); | |
| 231 | 1122 | File.write(file, "\"\n"); | |
| 232 | |||
| 233 | 1122 | File.write(file, " tolerance = \""); | |
| 234 | 1122 | File.writeReal(file, s.tolerance); | |
| 235 | 1122 | File.write(file, "\"\n"); | |
| 236 | |||
| 237 | 1122 | File.write(file, " solver = \""); | |
| 238 | 1122 | File.write(file, s.method); | |
| 239 | 1122 | File.write(file, "\"\n"); | |
| 240 | |||
| 241 | 1122 | File.write(file, " outputFormat = \""); | |
| 242 | 1122 | File.write(file, s.outputFormat); | |
| 243 | 1122 | File.write(file, "\"\n"); | |
| 244 | |||
| 245 | 1122 | File.write(file, " variableFilter = \""); | |
| 246 | 1122 | File.write(file, s.variableFilter); | |
| 247 | 1122 | File.write(file, "\" />\n\n"); | |
| 248 | |||
| 249 | 1122 | File.write(file, " <!-- variables in the model -->\n"); | |
| 250 | 1122 | File.write(file, " <ModelVariables>\n\n"); | |
| 251 | 1122 | modelVariables(file, simCode.modelInfo.vars); | |
| 252 | 1122 | File.write(file, "\n\n\n </ModelVariables>\n\n"); | |
| 253 | |||
| 254 | 1122 | File.write(file, "\n</fmiModelDescription>\n\n"); | |
| 255 | success := true; | ||
| 256 | else | ||
| 257 | end try; | ||
| 258 | end simulationInitFileReturnBool; | ||
| 259 | |||
| 260 | protected | ||
| 261 | |||
| 262 | function modelVariables "Generates code for ModelVariables file for FMU target." | ||
| 263 | input File.File file; | ||
| 264 | input SimCodeVar.SimVars vars; | ||
| 265 | protected | ||
| 266 | Integer vr, ix=0; | ||
| 267 | UnorderedMap<DAE.Exp, Integer> dims; | ||
| 268 | algorithm | ||
| 269 | // set starting index | ||
| 270 | vr := 1000; | ||
| 271 | |||
| 272 | 1122 | dims := dimensionValueReferences(vars, vr); | |
| 273 | |||
| 274 | 1122 | vr := scalarVariables(file, vars.stateVars, "rSta", vr, dims = dims); | |
| 275 | 1122 | vr := scalarVariables(file, vars.derivativeVars, "rDer", vr, dims = dims); | |
| 276 | 1122 | (vr,ix) := scalarVariables(file, vars.algVars, "rAlg", vr, ix, dims); | |
| 277 | 1122 | (vr,ix) := scalarVariables(file, vars.discreteAlgVars, "rAlg", vr, ix, dims); | |
| 278 | 1122 | (vr,ix) := scalarVariables(file, vars.realOptimizeConstraintsVars, "rAlg", vr, ix, dims); | |
| 279 | 1122 | (vr,ix) := scalarVariables(file, vars.realOptimizeFinalConstraintsVars, "rAlg", vr, ix, dims); | |
| 280 | 1122 | vr := scalarVariables(file, vars.paramVars, "rPar", vr, dims = dims); | |
| 281 | 1122 | vr := scalarVariables(file, vars.aliasVars, "rAli", vr, dims = dims); | |
| 282 | |||
| 283 | 1122 | vr := scalarVariables(file, vars.intAlgVars, "iAlg", vr, dims = dims); | |
| 284 | 1122 | vr := scalarVariables(file, vars.intParamVars, "iPar", vr, dims = dims); | |
| 285 | 1122 | vr := scalarVariables(file, vars.intAliasVars, "iAli", vr, dims = dims); | |
| 286 | |||
| 287 | 1122 | vr := scalarVariables(file, vars.boolAlgVars, "bAlg", vr, dims = dims); | |
| 288 | 1122 | vr := scalarVariables(file, vars.boolParamVars, "bPar", vr, dims = dims); | |
| 289 | 1122 | vr := scalarVariables(file, vars.boolAliasVars, "bAli", vr, dims = dims); | |
| 290 | |||
| 291 | 1122 | vr := scalarVariables(file, vars.stringAlgVars, "sAlg", vr, dims = dims); | |
| 292 | 1122 | vr := scalarVariables(file, vars.stringParamVars, "sPar", vr, dims = dims); | |
| 293 | 1122 | vr := scalarVariables(file, vars.stringAliasVars, "sAli", vr, dims = dims); | |
| 294 | |||
| 295 | // sensitivity variables | ||
| 296 | 1122 | vr := scalarVariables(file, vars.sensitivityVars, "rSen", vr, dims = dims); | |
| 297 | end modelVariables; | ||
| 298 | |||
| 299 | function dimensionValueReferences | ||
| 300 | "The value references the Integer parameters get in modelVariables, keyed by | ||
| 301 | the dimension they stand for: the parameter itself, and for a size parameter | ||
| 302 | $DIM_k of a derived dimension (N-1) its start expression. A dimension given by | ||
| 303 | one of them (resizable arrays) refers to it by value reference, so it can be | ||
| 304 | changed with -override before the simulation without compiling the model | ||
| 305 | again; a $DIM_k is computed by updateStructuralParameters." | ||
| 306 | input SimCodeVar.SimVars vars; | ||
| 307 | input Integer firstValueReference; | ||
| 308 | output UnorderedMap<DAE.Exp, Integer> dims = UnorderedMap.new<Integer>(ExpressionBasics.hashExp, ExpressionBasics.expEqual); | ||
| 309 | protected | ||
| 310 | Integer vr; | ||
| 311 | algorithm | ||
| 312 | // the value reference of the first Integer parameter, see modelVariables | ||
| 313 | 1122 | vr := firstValueReference + listLength(vars.stateVars) + listLength(vars.derivativeVars) | |
| 314 | + listLength(vars.algVars) + listLength(vars.discreteAlgVars) + listLength(vars.realOptimizeConstraintsVars) | ||
| 315 | + listLength(vars.realOptimizeFinalConstraintsVars) + listLength(vars.paramVars) + listLength(vars.aliasVars) | ||
| 316 | + listLength(vars.intAlgVars); | ||
| 317 |
2/2✓ Branch 0 taken 8138 times.
✓ Branch 1 taken 1122 times.
|
9260 | for var in vars.intParamVars loop |
| 318 | 8138 | UnorderedMap.add(DAE.CREF(var.name, var.type_), vr, dims); | |
| 319 |
2/2✓ Branch 1 taken 9 times.
✓ Branch 2 taken 8129 times.
|
8138 | if SimCodeCodegenUtil.isDimensionParameter(var) then |
| 320 | 9 | UnorderedMap.add(Util.getOption(var.initialValue), vr, dims); | |
| 321 | end if; | ||
| 322 | 8138 | vr := vr + 1; | |
| 323 | end for; | ||
| 324 | end dimensionValueReferences; | ||
| 325 | |||
| 326 | function scalarVariables | ||
| 327 | input File.File file; | ||
| 328 | input list<SimVar> vars; | ||
| 329 | input String classType; | ||
| 330 | input output Integer valueReference; | ||
| 331 | input output Integer index=0; | ||
| 332 | input UnorderedMap<DAE.Exp, Integer> dims "value references of the parameters that are dimensions"; | ||
| 333 | algorithm | ||
| 334 |
2/2✓ Branch 0 taken 358368 times.
✓ Branch 1 taken 20196 times.
|
378564 | for var in vars loop |
| 335 | 358368 | scalarVariable(file, var, classType, valueReference, index, dims); | |
| 336 | 358368 | index := index + 1; | |
| 337 | 358368 | valueReference := valueReference + 1; | |
| 338 | end for; | ||
| 339 | end scalarVariables; | ||
| 340 | |||
| 341 | function scalarVariable | ||
| 342 | input File.File file; | ||
| 343 | input SimVar var; | ||
| 344 | input String classType; | ||
| 345 | input Integer valueReference; | ||
| 346 | input Integer classIndex; | ||
| 347 | input UnorderedMap<DAE.Exp, Integer> dims; | ||
| 348 | protected | ||
| 349 | String type_name = if DAEUtil.expTypeArray(var.type_) then "ArrayVariable" else "ScalarVariable"; | ||
| 350 | algorithm | ||
| 351 | 358368 | File.write(file, " <" + type_name + "\n"); | |
| 352 | 358368 | scalarVariableAttribute(file, var, classType, valueReference, classIndex, dims); | |
| 353 | 358368 | File.write(file, " "); | |
| 354 | 358368 | scalarVariableType(file, var); | |
| 355 | 358368 | File.write(file, "\n </" + type_name + ">\n"); | |
| 356 | end scalarVariable; | ||
| 357 | |||
| 358 | function scalarVariableAttribute "Generates code for ScalarVariable Attribute file for FMU target." | ||
| 359 | input File.File file; | ||
| 360 | input SimVar simVar; | ||
| 361 | input String classType; | ||
| 362 | input Integer valueReference; | ||
| 363 | input Integer classIndex; | ||
| 364 | input UnorderedMap<DAE.Exp, Integer> dims; | ||
| 365 | protected | ||
| 366 | Integer inputIndex = SimCodeCodegenUtil.getInputIndex(simVar); | ||
| 367 | SourceInfo info = simVar.source.info; | ||
| 368 | algorithm | ||
| 369 | |||
| 370 | 358368 | File.write(file, " name = \""); | |
| 371 | 358368 | CR.writeCref(file, simVar.name, XML); | |
| 372 | 358368 | File.write(file, "\"\n"); | |
| 373 | |||
| 374 | 358368 | File.write(file, " valueReference = \""); | |
| 375 | 358368 | File.writeInt(file, valueReference); | |
| 376 | 358368 | File.write(file, "\"\n"); | |
| 377 | |||
| 378 |
3/4✓ Branch 0 taken 67147 times.
✓ Branch 1 taken 291221 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 67147 times.
|
358368 | if simVar.comment <> "" then |
| 379 | 291221 | File.write(file, " description = \""); | |
| 380 | 291221 | File.writeEscape(file, simVar.comment, XML); | |
| 381 | 291221 | File.write(file, "\"\n"); | |
| 382 | end if; | ||
| 383 | |||
| 384 | 358368 | File.write(file, " variability = \""); | |
| 385 | 358368 | File.write(file, getVariablity(simVar.varKind)); | |
| 386 | 358368 | File.write(file, "\" isDiscrete = \""); | |
| 387 | 358368 | File.write(file, String(simVar.isDiscrete)); | |
| 388 | 358368 | File.write(file, "\"\n"); | |
| 389 | |||
| 390 | 358368 | File.write(file, " causality = \""); | |
| 391 | 358368 | File.write(file, getCausality(simVar.causality)); | |
| 392 | 358368 | File.write(file, "\" isValueChangeable = \""); | |
| 393 | 358368 | File.write(file, String(simVar.isValueChangeable)); | |
| 394 | 358368 | File.write(file, "\"\n"); | |
| 395 | |||
| 396 |
2/2✓ Branch 0 taken 248 times.
✓ Branch 1 taken 358120 times.
|
358368 | if inputIndex <> -1 then |
| 397 | 248 | File.write(file, " inputIndex = \""); | |
| 398 | 248 | File.writeInt(file, inputIndex); | |
| 399 | 248 | File.write(file, "\"\n"); | |
| 400 | end if; | ||
| 401 | |||
| 402 | 358368 | File.write(file, " alias = "); | |
| 403 | 358368 | getAliasVar(file, simVar); | |
| 404 | 358368 | File.write(file, "\n"); | |
| 405 | |||
| 406 | 358368 | File.write(file, " classIndex = \""); | |
| 407 | 358368 | File.writeInt(file, classIndex); | |
| 408 | 358368 | File.write(file, "\" classType = \""); | |
| 409 | 358368 | File.write(file, classType); | |
| 410 | 358368 | File.write(file, "\"\n"); | |
| 411 | |||
| 412 | 358368 | File.write(file, " isProtected = \""); | |
| 413 | 358368 | File.write(file, String(simVar.isProtected)); | |
| 414 | 358368 | File.write(file, "\" hideResult = \""); | |
| 415 | 358368 | File.write(file, Util.applyOptionOrDefault(simVar.hideResult, boolString, "")); | |
| 416 | 358368 | File.write(file, "\" isEncrypted = \""); | |
| 417 |
1/2✓ Branch 0 taken 358368 times.
✗ Branch 1 not taken.
|
716736 | File.write(file, boolString(simVar.isEncrypted)); |
| 418 | 358368 | File.write(file, "\" initNonlinear = \""); | |
| 419 |
2/2✓ Branch 0 taken 357992 times.
✓ Branch 1 taken 376 times.
|
716360 | File.write(file, boolString(simVar.initNonlinear)); |
| 420 | 358368 | File.write(file, "\"\n"); | |
| 421 | |||
| 422 | 358368 | File.write(file, " fileName = \""); | |
| 423 | 358368 | File.writeEscape(file, info.fileName, XML); | |
| 424 | 358368 | File.write(file, "\" startLine = \""); | |
| 425 | 358368 | File.writeInt(file, info.lineNumberStart); | |
| 426 | 358368 | File.write(file, "\" startColumn = \""); | |
| 427 | 358368 | File.writeInt(file, info.columnNumberStart); | |
| 428 | 358368 | File.write(file, "\" endLine = \""); | |
| 429 | 358368 | File.writeInt(file, info.lineNumberEnd); | |
| 430 | 358368 | File.write(file, "\" endColumn = \""); | |
| 431 | 358368 | File.writeInt(file, info.columnNumberEnd); | |
| 432 | 358368 | File.write(file, "\" fileWritable = \""); | |
| 433 | 358368 | File.write(file, String(not info.isReadOnly)); | |
| 434 | 358368 | File.write(file, "\">\n"); | |
| 435 | |||
| 436 |
2/2✓ Branch 1 taken 173 times.
✓ Branch 2 taken 358368 times.
|
358541 | for dim in Expression.arrayDimension(simVar.type_) loop |
| 437 | _ := match dim | ||
| 438 | local | ||
| 439 | Integer vr; | ||
| 440 | // a dimension given by a parameter, or a derived one by its size parameter $DIM_k, refers to it | ||
| 441 | case DAE.DIM_EXP() guard not Expression.isConst(dim.exp) | ||
| 442 | algorithm | ||
| 443 | try | ||
| 444 |
2/4✗ Branch 1 not taken.
✓ Branch 2 taken 87 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 87 times.
|
87 | SOME(vr) := UnorderedMap.get(dim.exp, dims); |
| 445 | else | ||
| 446 | ✗ | Error.addCompilerError("The dimension " + printExpStr(dim.exp) + " of " + ComponentReferenceBasics.printComponentRefStr(simVar.name) | |
| 447 | + " is neither an Integer parameter nor has a size parameter. It is not supported for resizable arrays (--resizableArrays)."); | ||
| 448 | ✗ | fail(); | |
| 449 | end try; | ||
| 450 | 87 | File.write(file, " <Dimension valueReference=\"" + intString(vr) + "\"/>\n"); | |
| 451 | then (); | ||
| 452 | else algorithm | ||
| 453 | 86 | File.write(file, " <Dimension start=\"" + intString(Expression.dimensionSize(dim)) + "\"/>\n"); | |
| 454 | then (); | ||
| 455 | end match; | ||
| 456 | end for; | ||
| 457 | end scalarVariableAttribute; | ||
| 458 | |||
| 459 | function scalarVariableType "Generates code for ScalarVariable Type file for FMU target." | ||
| 460 | input File.File file; | ||
| 461 | input SimVar v; | ||
| 462 | protected | ||
| 463 | Absyn.Path path; | ||
| 464 | algorithm | ||
| 465 | () := match Types.arrayElementType(v.type_) | ||
| 466 | case Type.T_INTEGER() | ||
| 467 | algorithm | ||
| 468 | 12825 | File.write(file, "<Integer"); | |
| 469 | 12825 | scalarVariableTypeAttribute(file, v.initialValue, "start"); | |
| 470 | 12825 | scalarVariableTypeFixedAttribute(file, v.isFixed); | |
| 471 | 12825 | scalarVariableTypeAttribute(file, v.minValue, "min"); | |
| 472 | 12825 | scalarVariableTypeAttribute(file, v.maxValue, "max"); | |
| 473 | 12825 | scalarVariableTypeStringAttribute(file, v.unit, "unit"); | |
| 474 | 12825 | scalarVariableTypeStringAttribute(file, v.displayUnit, "displayUnit"); | |
| 475 | 12825 | File.write(file, " />"); | |
| 476 | then (); | ||
| 477 | case Type.T_REAL() | ||
| 478 | algorithm | ||
| 479 | 326210 | File.write(file, "<Real"); | |
| 480 | 326210 | scalarVariableTypeAttribute(file, v.initialValue, "start"); | |
| 481 | 326210 | scalarVariableTypeFixedAttribute(file, v.isFixed); | |
| 482 | 326210 | scalarVariableTypeUseAttribute(file, v.nominalValue, "useNominal", "nominal"); | |
| 483 | 326210 | scalarVariableTypeAttribute(file, v.minValue, "min"); | |
| 484 | 326210 | scalarVariableTypeAttribute(file, v.maxValue, "max"); | |
| 485 | 326210 | scalarVariableTypeStringAttribute(file, v.unit, "unit"); | |
| 486 | 326210 | scalarVariableTypeStringAttribute(file, v.displayUnit, "displayUnit"); | |
| 487 |
2/2✓ Branch 0 taken 55 times.
✓ Branch 1 taken 326155 times.
|
326210 | if v.relativeQuantity then |
| 488 | 55 | File.write(file, " relativeQuantity=\"true\""); | |
| 489 | end if; | ||
| 490 | 326210 | File.write(file, " />"); | |
| 491 | then (); | ||
| 492 | case Type.T_BOOL() | ||
| 493 | algorithm | ||
| 494 | 15110 | File.write(file, "<Boolean"); | |
| 495 | 15110 | scalarVariableTypeAttribute(file, v.initialValue, "start"); | |
| 496 | 15110 | scalarVariableTypeFixedAttribute(file, v.isFixed); | |
| 497 | 15110 | scalarVariableTypeStringAttribute(file, v.unit, "unit"); | |
| 498 | 15110 | scalarVariableTypeStringAttribute(file, v.displayUnit, "displayUnit"); | |
| 499 | 15110 | File.write(file, " />"); | |
| 500 | then (); | ||
| 501 | case Type.T_STRING() | ||
| 502 | algorithm | ||
| 503 | 1613 | File.write(file, "<String"); | |
| 504 | 1613 | scalarVariableTypeAttribute(file, v.initialValue, "start"); | |
| 505 | 1613 | scalarVariableTypeFixedAttribute(file, v.isFixed); | |
| 506 | 1613 | scalarVariableTypeStringAttribute(file, v.unit, "unit"); | |
| 507 | 1613 | scalarVariableTypeStringAttribute(file, v.displayUnit, "displayUnit"); | |
| 508 | 1613 | File.write(file, " />"); | |
| 509 | then (); | ||
| 510 | case Type.T_ENUMERATION() | ||
| 511 | algorithm | ||
| 512 | 2610 | File.write(file, "<Integer"); | |
| 513 | 2610 | scalarVariableTypeAttribute(file, v.initialValue, "start"); | |
| 514 | 2610 | scalarVariableTypeFixedAttribute(file, v.isFixed); | |
| 515 | 2610 | scalarVariableTypeStringAttribute(file, v.unit, "unit"); | |
| 516 | 2610 | scalarVariableTypeStringAttribute(file, v.displayUnit, "displayUnit"); | |
| 517 | 2610 | File.write(file, " />"); | |
| 518 | then (); | ||
| 519 | case Type.T_COMPLEX(complexClassType = ClassInf.EXTERNAL_OBJ(path=path)) | ||
| 520 | algorithm | ||
| 521 | ✗ | File.write(file, "<ExternalObject path=\""); | |
| 522 | ✗ | Dump.writePath(file, path, XML); | |
| 523 | ✗ | File.write(file, "\" />"); | |
| 524 | then (); | ||
| 525 | else | ||
| 526 | algorithm | ||
| 527 | ✗ | Error.addInternalError(getInstanceName() + ": " + TypesDump.unparseType(v.type_), sourceInfo()); | |
| 528 | ✗ | then fail(); | |
| 529 | end match; | ||
| 530 | end scalarVariableType; | ||
| 531 | |||
| 532 | function scalarVariableTypeUseAttribute | ||
| 533 | input File.File file; | ||
| 534 | input Option<Exp> attr; | ||
| 535 | input String use, name; | ||
| 536 | algorithm | ||
| 537 | 326210 | File.write(file, " "); | |
| 538 | 326210 | File.write(file, use); | |
| 539 | 326210 | File.write(file, "=\""); | |
| 540 |
3/4✗ Branch 0 not taken.
✓ Branch 1 taken 326210 times.
✓ Branch 2 taken 24308 times.
✓ Branch 3 taken 301902 times.
|
350518 | File.write(file, String(isSome(attr))); |
| 541 | 326210 | File.write(file, "\""); | |
| 542 | 326210 | scalarVariableTypeAttribute(file, attr, name); | |
| 543 | end scalarVariableTypeUseAttribute; | ||
| 544 | |||
| 545 | function scalarVariableTypeFixedAttribute | ||
| 546 | input File.File file; | ||
| 547 | input Boolean isFixed; | ||
| 548 | algorithm | ||
| 549 | 358368 | File.write(file, " fixed=\""); | |
| 550 | 358368 | File.write(file, String(isFixed)); | |
| 551 | 358368 | File.write(file, "\""); | |
| 552 | end scalarVariableTypeFixedAttribute; | ||
| 553 | |||
| 554 | function scalarVariableTypeAttribute | ||
| 555 | input File.File file; | ||
| 556 | input Option<Exp> attr; | ||
| 557 | input String name; | ||
| 558 | protected | ||
| 559 | String expStr; | ||
| 560 | algorithm | ||
| 561 | try | ||
| 562 | 1362648 | expStr := expString(Util.getOption(attr)); | |
| 563 | 248252 | File.write(file, " "); | |
| 564 | 248252 | File.write(file, name); | |
| 565 | 248252 | File.write(file, "=\""); | |
| 566 | 248252 | File.write(file, expStr); | |
| 567 | 248252 | File.write(file, "\""); | |
| 568 | else | ||
| 569 | end try; | ||
| 570 | end scalarVariableTypeAttribute; | ||
| 571 | |||
| 572 | function scalarVariableTypeStringAttribute | ||
| 573 | input File.File file; | ||
| 574 | input String attr; | ||
| 575 | input String name; | ||
| 576 | algorithm | ||
| 577 |
3/4✓ Branch 0 taken 496058 times.
✓ Branch 1 taken 220678 times.
✓ Branch 3 taken 496058 times.
✗ Branch 4 not taken.
|
716736 | if attr == "" then |
| 578 | 496058 | return; | |
| 579 | end if; | ||
| 580 | 220678 | File.write(file, " "); | |
| 581 | 220678 | File.write(file, name); | |
| 582 | 220678 | File.write(file, "=\""); | |
| 583 | 220678 | File.writeEscape(file, attr, XML); | |
| 584 | 220678 | File.write(file, "\""); | |
| 585 | end scalarVariableTypeStringAttribute; | ||
| 586 | |||
| 587 | function getCausality "Returns the Causality Attribute of ScalarVariable." | ||
| 588 | input Option<Causality> c; | ||
| 589 | output String str; | ||
| 590 | algorithm | ||
| 591 | str := match c | ||
| 592 | case SOME(Causality.NONECAUS()) then "none"; | ||
| 593 | case SOME(Causality.OUTPUT()) then "output"; | ||
| 594 | case SOME(Causality.INPUT()) then "input"; | ||
| 595 | case SOME(Causality.LOCAL()) then "local"; // replacement for INTERNAL(), From now Use LOCAL according to FMI-2.0 specification | ||
| 596 | case SOME(Causality.PARAMETER()) then "parameter"; | ||
| 597 | case SOME(Causality.CALCULATED_PARAMETER()) then "calculatedParameter"; | ||
| 598 | else "local"; | ||
| 599 | end match; | ||
| 600 | end getCausality; | ||
| 601 | |||
| 602 | function getVariablity "Returns the variablity Attribute of ScalarVariable." | ||
| 603 | input VarKind varKind; | ||
| 604 | output String str; | ||
| 605 | algorithm | ||
| 606 | str := match varKind | ||
| 607 | case VarKind.DISCRETE() then "discrete"; | ||
| 608 | case VarKind.PARAM() then "parameter"; | ||
| 609 | case VarKind.CONST() then "constant"; | ||
| 610 | else "continuous"; | ||
| 611 | end match; | ||
| 612 | end getVariablity; | ||
| 613 | |||
| 614 | function getAliasVar "Returns the alias Attribute of ScalarVariable." | ||
| 615 | input File.File file; | ||
| 616 | input SimCodeVar.SimVar simVar; | ||
| 617 | algorithm | ||
| 618 | () := match simVar | ||
| 619 | local SimCodeVar.AliasVariable aliasvar; | ||
| 620 | case SimCodeVar.SIMVAR(aliasvar = aliasvar as AliasVariable.ALIAS()) | ||
| 621 | algorithm | ||
| 622 | 88466 | File.write(file, "\"alias\" aliasVariable=\""); | |
| 623 | 88466 | CR.writeCref(file, aliasvar.varName, XML); | |
| 624 | 88466 | File.write(file, "\" aliasVariableId=\""); | |
| 625 | 88466 | File.write(file, SimCodeCodegenUtil.getValueReference(simVar, SimCodeCodegenUtil.getSimCode(), true)); | |
| 626 | 88466 | File.write(file, "\""); | |
| 627 | then (); | ||
| 628 | case SimCodeVar.SIMVAR(aliasvar = aliasvar as AliasVariable.NEGATEDALIAS()) | ||
| 629 | algorithm | ||
| 630 | 10283 | File.write(file, "\"negatedAlias\" aliasVariable=\""); | |
| 631 | 10283 | CR.writeCref(file, aliasvar.varName, XML); | |
| 632 | 10283 | File.write(file, "\" aliasVariableId=\""); | |
| 633 | 10283 | File.write(file, SimCodeCodegenUtil.getValueReference(simVar, SimCodeCodegenUtil.getSimCode(), true)); | |
| 634 | 10283 | File.write(file, "\""); | |
| 635 | then (); | ||
| 636 | else | ||
| 637 | 259619 | algorithm File.write(file, "\"noAlias\""); then (); | |
| 638 | end match; | ||
| 639 | end getAliasVar; | ||
| 640 | |||
| 641 | function xsdateTime "YYYY-MM-DDThh:mm:ssZ" | ||
| 642 | input File.File file; | ||
| 643 | input Util.DateTime dt; | ||
| 644 | algorithm | ||
| 645 | 1122 | File.writeInt(file, dt.year); | |
| 646 | 1122 | File.writeInt(file, dt.mon, "-%02d"); | |
| 647 | 1122 | File.writeInt(file, dt.mday, "-%02d"); | |
| 648 | 1122 | File.writeInt(file, dt.hour, "T%02d"); | |
| 649 | 1122 | File.writeInt(file, dt.min, ":%02d"); | |
| 650 | 1122 | File.writeInt(file, dt.sec, ":%02dZ"); | |
| 651 | end xsdateTime; | ||
| 652 | |||
| 653 | function expString | ||
| 654 | input Exp exp; | ||
| 655 | output String str; | ||
| 656 | algorithm | ||
| 657 | str := match exp | ||
| 658 | 23520 | case Exp.ICONST() then intString(exp.integer); | |
| 659 | 210109 | case Exp.RCONST() then realString(exp.real); | |
| 660 | 1330 | case Exp.SCONST() then Util.escapeModelicaStringToXmlString(exp.string); | |
| 661 |
2/2✓ Branch 0 taken 8253 times.
✓ Branch 1 taken 3306 times.
|
11559 | case Exp.BCONST() then boolString(exp.bool); |
| 662 | 1956 | case Exp.ENUM_LITERAL() then intString(exp.index); | |
| 663 |
4/4✓ Branch 0 taken 251 times.
✓ Branch 1 taken 49 times.
✓ Branch 2 taken 251 times.
✓ Branch 3 taken 49 times.
|
300 | case Exp.ARRAY() guard Expression.isSimpleLiteralValue(exp, true) then stringDelimitList(list(arrayElementString(e) for e in exp.array), " "); |
| 664 | 26 | case Exp.REDUCTION() then expString(exp.expr); | |
| 665 | else fail(); | ||
| 666 | //else algorithm Error.addInternalError("initial value of unknown type: " + printExpStr(exp), sourceInfo()); then fail(); | ||
| 667 | end match; | ||
| 668 | end expString; | ||
| 669 | |||
| 670 | function arrayElementString | ||
| 671 | "Like expString, but String elements are enclosed in quotes, so the values | ||
| 672 | of a String array can be told apart." | ||
| 673 | input Exp exp; | ||
| 674 | output String str; | ||
| 675 | algorithm | ||
| 676 | str := match exp | ||
| 677 | 2 | case Exp.SCONST() then """ + Util.escapeModelicaStringToXmlString(exp.string) + """; | |
| 678 |
4/4✓ Branch 0 taken 30 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 8 times.
|
38 | case Exp.ARRAY() guard Expression.isSimpleLiteralValue(exp, true) then stringDelimitList(list(arrayElementString(e) for e in exp.array), " "); |
| 679 | 271 | else expString(exp); | |
| 680 | end match; | ||
| 681 | end arrayElementString; | ||
| 682 | |||
| 683 | annotation(__OpenModelica_Interface="backend_tools"); | ||
| 684 | end SerializeInitXML; | ||
| 685 |