Linux GNU 11.4.0 Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 72.3% 509 / 0 / 704
Functions: -% 0 / 1 / 1
Branches: 55.7% 117 / 0 / 210

OMCompiler/Compiler/SimCode/SerializeModelInfo.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 SerializeModelInfo
37
38 function serialize
39 input SimCode.SimCode code;
40 input Boolean withOperations;
41 output String fileName;
42 algorithm
43
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1204 times.
1204 (true,fileName) := serializeWork(code,withOperations);
44 end serialize;
45
46 import Absyn;
47 import BackendDAE;
48 import DAE;
49 import SimCode;
50
51 protected
52 import Algorithm;
53 import DAEDump;
54 import Error;
55 import Expression;
56 import File;
57 import File.Escape.JSON;
58 import writeCref = ComponentReference.writeCref;
59 import ComponentReferenceBasics;
60 import expStr = ExpressionBasics.printExpStr;
61 import List;
62 import PrefixUtil;
63 import SimCodeFunction;
64 import SimCodeUtil;
65 import SimCodeVar;
66 import SCodeDump;
67 import Util;
68 import UnorderedSet;
69 import SimCodeCodegenUtil;
70
71 function serializeWork "Always succeeds in order to clean-up external objects"
72 input SimCode.SimCode code;
73 input Boolean withOperations;
74 output Boolean success; // We always need to return in order to clean up external objects
75 output String fileName;
76 protected
77 File.File file = File.File();
78 algorithm
79 (success,fileName) := matchcontinue code
80 local
81 SimCode.ModelInfo mi;
82 String eqsName;
83 list<SimCode.SimEqSystem> eqsLst;
84
85 case SimCode.SIMCODE(modelInfo = mi as SimCode.MODELINFO())
86 algorithm
87 1204 fileName := code.fileNamePrefix + "_info.json";
88 1204 File.open(file,fileName,File.Mode.Write);
89 1204 File.write(file, "{\"format\":\"Transformational debugger info\",\"version\":1,\n\"info\":{\"name\":");
90 1204 serializePath(file, mi.name);
91 1204 File.write(file, ",\"description\":\"");
92 1204 File.writeEscape(file, mi.description, escape=JSON);
93 1204 File.write(file, "\"},\n\"variables\":{\n");
94 1204 serializeVars(file, mi.vars, withOperations);
95 1204 File.write(file, "\n},\n\"equations\":[");
96 // Handle no comma for the first equation
97 1204 File.write(file,"{\"eqIndex\":0,\"tag\":\"dummy\"}");
98
99
2/2
✓ Branch 18 taken 16856 times.
✓ Branch 19 taken 1204 times.
19264 for tpl in {
100 ("initial", code.initialEquations),
101 ("initial-lambda0", code.initialEquations_lambda0),
102 ("removed-initial", code.removedInitialEquations),
103 ("regular", code.allEquations),
104 ("synchronous", SimCodeCodegenUtil.getClockedEquations(SimCodeCodegenUtil.getSubPartitions(code.clockedPartitions))),
105 ("start", code.startValueEquations),
106 ("nominal", code.nominalValueEquations),
107 ("min", code.minValueEquations),
108 ("max", code.maxValueEquations),
109 ("parameter", code.parameterEquations),
110 ("assertions", code.algorithmAndEquationAsserts),
111 ("inline", code.inlineEquations),
112 ("residuals", List.flatten(SimCodeUtil.getSimCodeDAEModeDataEqns(code.daeModeData))),
113 ("jacobian", code.jacobianEquations)
114 } loop
115 16856 (eqsName, eqsLst) := tpl;
116
2/2
✓ Branch 1 taken 329207 times.
✓ Branch 2 taken 16856 times.
346063 for eq in SimCodeCodegenUtil.sortEqSystems(eqsLst) loop
117 try
118 329207 serializeEquation(file, eq, eqsName, withOperations);
119 else
120 ✗ Error.addMessage(Error.INTERNAL_ERROR, {"SerializeModelInfo.serializeWork failed for section=" + eqsName + " eqIndex=" + intString(SimCodeCodegenUtil.simEqSystemIndex(eq))});
121 ✗ fail();
122 end try;
123 end for;
124 end for;
125
126 1204 File.write(file, "\n],\n\"functions\":[");
127 1204 serializeList(file,mi.functions,serializeFunction);
128 1204 File.write(file, "\n]\n}");
129 1204 then (true,fileName);
130 else
131 algorithm
132 ✗ Error.addInternalError("SerializeModelInfo.serialize failed", sourceInfo());
133 then (false,"");
134 end matchcontinue;
135 end serializeWork;
136
137 function serializeVars
138 input File.File file;
139 input SimCodeVar.SimVars vars;
140 input Boolean withOperations;
141 protected
142 Boolean b;
143 algorithm
144 1204 b := serializeVarsHelp(file, vars.stateVars, withOperations, true);
145 1204 b := serializeVarsHelp(file, vars.derivativeVars, withOperations, b);
146 1204 b := serializeVarsHelp(file, vars.algVars, withOperations, b);
147 1204 b := serializeVarsHelp(file, vars.aliasVars, withOperations, b);
148 1204 b := serializeVarsHelp(file, vars.intAlgVars, withOperations, b);
149 1204 b := serializeVarsHelp(file, vars.boolAlgVars, withOperations, b);
150 1204 b := serializeVarsHelp(file, vars.inputVars, withOperations, b);
151 1204 b := serializeVarsHelp(file, vars.intAliasVars, withOperations, b);
152 1204 b := serializeVarsHelp(file, vars.boolAliasVars, withOperations, b);
153 1204 b := serializeVarsHelp(file, vars.paramVars, withOperations, b);
154 1204 b := serializeVarsHelp(file, vars.intParamVars, withOperations, b);
155 1204 b := serializeVarsHelp(file, vars.boolParamVars, withOperations, b);
156 1204 b := serializeVarsHelp(file, vars.stringAlgVars, withOperations, b);
157 1204 b := serializeVarsHelp(file, vars.stringAliasVars, withOperations, b);
158 1204 b := serializeVarsHelp(file, vars.extObjVars, withOperations, b);
159 1204 b := serializeVarsHelp(file, vars.constVars, withOperations, b);
160 1204 b := serializeVarsHelp(file, vars.intConstVars, withOperations, b);
161 1204 b := serializeVarsHelp(file, vars.boolConstVars, withOperations, b);
162 1204 b := serializeVarsHelp(file, vars.stringConstVars, withOperations, b);
163 1204 b := serializeVarsHelp(file, vars.jacobianVars, withOperations, b);
164 1204 serializeVarsHelp(file, vars.sensitivityVars, withOperations, b);
165 end serializeVars;
166
167 function serializeVarsHelp
168 input File.File file;
169 input list<SimCodeVar.SimVar> vars;
170 input Boolean withOperations;
171 input Boolean inFirst;
172 output Boolean outFirst = inFirst and listEmpty(vars);
173 algorithm
174
2/2
✓ Branch 0 taken 25263 times.
✓ Branch 1 taken 21 times.
50547 serializeList(file, vars, function serializeVar(withOperations = withOperations), not inFirst, ",\n");
175 end serializeVarsHelp;
176
177 function serializeVar
178 input File.File file;
179 input SimCodeVar.SimVar var;
180 input Boolean withOperations;
181 algorithm
182 395988 File.write(file, "\"");
183 395988 writeCref(file, var.name, escape=JSON);
184 395988 File.write(file,"\":{\"comment\":\"");
185 395988 File.writeEscape(file,var.comment,escape=JSON);
186 395988 File.write(file,"\",\"kind\":\"");
187 395988 File.write(file, varKindString(var.varKind, var));
188 395988 File.write(file,"\"");
189 395988 serializeTypeName(file,var.type_);
190 395988 File.write(file,",\"unit\":\"");
191 395988 File.writeEscape(file,var.unit,escape=JSON);
192 395988 File.write(file,"\",\"displayUnit\":\"");
193 395988 File.writeEscape(file,var.displayUnit,escape=JSON);
194 395988 File.write(file,"\",\"source\":");
195 395988 serializeSource(file,var.source,withOperations);
196 395988 File.write(file, ",\"index\":");
197 395988 File.writeInt(file, var.index);
198 () := match var.aliasvar
199 local
200 DAE.ComponentRef cr;
201 case SimCodeVar.ALIAS(varName = cr)
202 algorithm
203 90901 File.write(file, ",\"alias\":\"");
204 90901 writeCref(file, cr, escape=JSON);
205 90901 File.write(file, "\"");
206 then ();
207 case SimCodeVar.NEGATEDALIAS(varName = cr)
208 algorithm
209 10521 File.write(file, ",\"alias\":\"-");
210 10521 writeCref(file, cr, escape=JSON);
211 10521 File.write(file, "\"");
212 then ();
213 else ();
214 end match;
215 395988 File.write(file,"}");
216 end serializeVar;
217
218 function serializeTypeName
219 input File.File file;
220 input DAE.Type ty;
221 algorithm
222 () := match ty
223 361511 case DAE.T_REAL() algorithm File.write(file,",\"type\":\"Real\""); then ();
224 15202 case DAE.T_INTEGER() algorithm File.write(file,",\"type\":\"Integer\""); then ();
225 16044 case DAE.T_BOOL() algorithm File.write(file,",\"type\":\"Boolean\""); then ();
226 78 case DAE.T_STRING() algorithm File.write(file,",\"type\":\"String\""); then ();
227 2777 case DAE.T_ENUMERATION() algorithm File.write(file,",\"type\":\"Enumeration\""); then ();
228 else ();
229 end match;
230 end serializeTypeName;
231
232 function serializeSource
233 input File.File file;
234 input DAE.ElementSource source;
235 input Boolean withOperations;
236 protected
237 SourceInfo info;
238 list<Absyn.Path> paths,typeLst;
239 list<Absyn.Within> partOfLst;
240 DAE.ComponentPrefix instance;
241 list<DAE.SymbolicOperation> operations;
242 algorithm
243 712716 DAE.SOURCE(typeLst=typeLst,info=info,instance=instance,partOfLst=partOfLst,operations=operations) := source;
244 712716 File.write(file,"{");
245 712716 serializeInfo(file,info);
246
247
2/2
✓ Branch 0 taken 198767 times.
✓ Branch 1 taken 513949 times.
712716 if not listEmpty(partOfLst) then
248
2/2
✓ Branch 0 taken 202322 times.
✓ Branch 1 taken 198767 times.
603411 paths := list(match w case Absyn.WITHIN() then w.path; end match
249 for w guard (match w case Absyn.TOP() then false; else true; end match)
250 in partOfLst);
251 198767 File.write(file,",\"within\":[");
252 198767 serializeList(file,paths,serializePath);
253 198767 File.write(file,"]");
254 end if;
255
256 () := match instance
257 case DAE.NOCOMPPRE() then ();
258 case DAE.PRE() algorithm
259 172220 File.write(file,",\"instance\":\"");
260 172220 PrefixUtil.writeComponentPrefix(file,instance,escape=JSON);
261 172220 File.write(file,"\"");
262 then ();
263 end match;
264
265
2/2
✓ Branch 0 taken 3660 times.
✓ Branch 1 taken 709056 times.
712716 if not listEmpty(typeLst) then
266 3660 File.write(file,",\"typeLst\":[");
267 3660 serializeList(file,typeLst,serializePath);
268 3660 File.write(file,"]");
269 end if;
270
271
4/4
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 712620 times.
✓ Branch 2 taken 46 times.
✓ Branch 3 taken 50 times.
712716 if withOperations and not listEmpty(operations) then
272 46 File.write(file,",\"operations\":[");
273 46 serializeList(file, operations, serializeOperation);
274 46 File.write(file,"]");
275 end if;
276 712716 File.write(file,"}");
277 end serializeSource;
278
279 function serializeInfo
280 input File.File file;
281 input SourceInfo info;
282 algorithm
283 712716 File.write(file,"\"info\":{\"file\":\"");
284 712716 File.writeEscape(file, info.fileName, escape=JSON);
285 712716 File.write(file, "\",\"lineStart\":");
286 712716 File.writeInt(file, info.lineNumberStart);
287 712716 File.write(file, ",\"lineEnd\":");
288 712716 File.writeInt(file, info.lineNumberEnd);
289 712716 File.write(file, ",\"colStart\":");
290 712716 File.writeInt(file, info.columnNumberStart);
291 712716 File.write(file, ",\"colEnd\":");
292 712716 File.writeInt(file, info.columnNumberEnd);
293 712716 File.write(file, "}");
294 end serializeInfo;
295
296 function serializeOperation
297 input File.File file;
298 input DAE.SymbolicOperation op;
299 algorithm
300 () := match op
301 local
302 DAE.Element elt;
303 case DAE.FLATTEN(dae=SOME(elt))
304 algorithm
305 35 File.write(file,"{\"op\":\"before-after\",\"display\":\"flattening\",\"data\":[\"");
306 35 File.writeEscape(file,System.trim(SCodeDump.equationStr(op.scode,SCodeDump.defaultOptions)),escape=JSON);
307 35 File.write(file,"\",\"");
308 35 File.writeEscape(file,System.trim(DAEDump.dumpEquationStr(elt)),escape=JSON);
309 35 File.write(file,"\"]}");
310 then ();
311 case DAE.FLATTEN()
312 algorithm
313 ✗ File.write(file,"{\"op\":\"info\",\"display\":\"scode\",\"data\":[\"");
314 ✗ File.writeEscape(file,System.trim(SCodeDump.equationStr(op.scode,SCodeDump.defaultOptions)),escape=JSON);
315 ✗ File.write(file,"\"]}");
316 then ();
317 case DAE.SIMPLIFY()
318 algorithm
319 1 File.write(file,"{\"op\":\"before-after\",\"display\":\"simplify\",\"data\":[\"");
320 1 writeEqExpStr(file,op.before);
321 1 File.write(file,"\",\"");
322 1 writeEqExpStr(file,op.after);
323 1 File.write(file,"\"]}");
324 then ();
325 case DAE.OP_INLINE()
326 algorithm
327 ✗ File.write(file,"{\"op\":\"before-after\",\"display\":\"inline\",\"data\":[\"");
328 ✗ writeEqExpStr(file,op.before);
329 ✗ File.write(file,"\",\"");
330 ✗ writeEqExpStr(file,op.after);
331 ✗ File.write(file,"\"]}");
332 then ();
333 case DAE.SOLVE(assertConds={})
334 algorithm
335 6 File.write(file,"{\"op\":\"before-after\",\"display\":\"solved\",\"data\":[\"");
336 6 File.writeEscape(file,expStr(op.exp1),escape=JSON);
337 6 File.write(file," = ");
338 6 File.writeEscape(file,expStr(op.exp2),escape=JSON);
339 6 File.write(file,"\",\"");
340 6 writeCref(file,op.cr,escape=JSON);
341 6 File.write(file," = ");
342 6 File.writeEscape(file,expStr(op.res),escape=JSON);
343 6 File.write(file,"\"]}");
344 then ();
345 case DAE.SOLVE()
346 algorithm
347 ✗ File.write(file,"{\"op\":\"before-after-assert\",\"display\":\"solved\",\"data\":[\"");
348 ✗ File.writeEscape(file,expStr(op.exp1),escape=JSON);
349 ✗ File.write(file," = ");
350 ✗ File.writeEscape(file,expStr(op.exp2),escape=JSON);
351 ✗ File.write(file,"\",\"");
352 ✗ writeCref(file,op.cr,escape=JSON);
353 ✗ File.write(file," = ");
354 ✗ File.writeEscape(file,expStr(op.res),escape=JSON);
355 ✗ File.write(file,"\"");
356 ✗ serializeList(file, op.assertConds, serializeExp, true);
357 ✗ File.write(file,"]}");
358 then ();
359 case DAE.OP_RESIDUAL()
360 algorithm
361 ✗ File.write(file,"{\"op\":\"before-after\",\"display\":\"residual\",\"data\":[");
362 ✗ File.writeEscape(file,expStr(op.e1),escape=JSON);
363 ✗ File.write(file," = ");
364 ✗ File.writeEscape(file,expStr(op.e2),escape=JSON);
365 ✗ File.write(file,",\"0 = ");
366 ✗ File.writeEscape(file,expStr(op.e),escape=JSON);
367 ✗ File.write(file,"\"]}");
368 then ();
369 case DAE.SUBSTITUTION()
370 algorithm
371 1 File.write(file,"{\"op\":\"chain\",\"display\":\"substitution\",\"data\":[\"");
372 1 File.writeEscape(file,expStr(op.source),escape=JSON);
373 1 File.write(file,"\"");
374 1 serializeList(file, op.substitutions, serializeExp, true);
375 1 File.write(file,"]}");
376 then ();
377 case DAE.SOLVED()
378 algorithm
379 59 File.write(file,"{\"op\":\"info\",\"display\":\"solved\",\"data\":[\"");
380 59 writeCref(file,op.cr,escape=JSON);
381 59 File.write(file," = ");
382 59 File.writeEscape(file,expStr(op.exp),escape=JSON);
383 59 File.write(file,"\"]}");
384 then ();
385 case DAE.OP_DIFFERENTIATE()
386 algorithm
387 64 File.write(file,"{\"op\":\"before-after\",\"display\":\"differentiate d/d");
388 64 writeCref(file,op.cr,escape=JSON);
389 64 File.write(file,"\",\"data\":[\"");
390 64 File.writeEscape(file,expStr(op.before),escape=JSON);
391 64 File.write(file,"\",\"");
392 64 File.writeEscape(file,expStr(op.after),escape=JSON);
393 64 File.write(file,"\"]}");
394 then ();
395
396 case DAE.OP_SCALARIZE()
397 algorithm
398 ✗ File.write(file,"{\"op\":\"before-after\",\"display\":\"scalarize [");
399 ✗ File.write(file,intString(op.index));
400 ✗ File.write(file,"]\",\"data\":[\"");
401 ✗ writeEqExpStr(file,op.before);
402 ✗ File.write(file,"\",\"");
403 ✗ writeEqExpStr(file,op.after);
404 ✗ File.write(file,"\"]}");
405 then ();
406
407 // Custom operations - operations that can not be described in a general way because they are specialized
408 case DAE.NEW_DUMMY_DER()
409 algorithm
410 ✗ File.write(file,"{\"op\":\"dummy-der\",\"display\":\"dummy derivative");
411 ✗ File.write(file,"\",\"data\":[\"");
412 ✗ writeCref(file,op.chosen);
413 ✗ File.write(file,"\"");
414 ✗ serializeList(file, op.candidates, serializeCref, true);
415 ✗ File.write(file,"]}");
416 then ();
417
418 else
419 algorithm
420 ✗ Error.addInternalError("serializeOperation failed: " + anyString(op), sourceInfo());
421 ✗ then fail();
422 end match;
423 end serializeOperation;
424
425 type AssignType = enumeration(NORMAL, TORN, JACOBIAN);
426
427 function tagFromAssignType
428 input AssignType assignType;
429 output String tag;
430 algorithm
431 tag := match assignType
432 case AssignType.NORMAL then "assign";
433 case AssignType.TORN then "torn";
434 case AssignType.JACOBIAN then "jacobian";
435 end match;
436 end tagFromAssignType;
437
438 function serializeEquation
439 input File.File file;
440 input SimCode.SimEqSystem eq;
441 input String section;
442 input Boolean withOperations;
443 input Integer parent = 0 "No parent";
444 input Boolean first = false;
445 input AssignType assign_type = AssignType.NORMAL;
446 algorithm
447
2/2
✓ Branch 0 taken 373883 times.
✓ Branch 1 taken 3328 times.
377211 if not first then
448 373883 File.write(file, ",");
449 end if;
450 () := match eq
451 local
452 Integer i,j;
453 DAE.Statement stmt;
454 list<SimCode.SimEqSystem> eqs,jeqs,constantEqns;
455 SimCode.LinearSystem lSystem, atL;
456 SimCode.NonlinearSystem nlSystem, atNL;
457 BackendDAE.WhenOperator whenOp;
458 list<DAE.ComponentRef> crefs;
459
460 case SimCode.SES_RESIDUAL() algorithm
461 7660 File.write(file, "\n{\"eqIndex\":");
462 7660 File.writeInt(file, eq.index);
463
2/2
✓ Branch 0 taken 7632 times.
✓ Branch 1 taken 28 times.
7660 if parent <> 0 then
464 7632 File.write(file, ",\"parent\":");
465 7632 File.writeInt(file, parent);
466 end if;
467 7660 File.write(file, ",\"section\":\"");
468 7660 File.write(file, section);
469 7660 File.write(file, "\",\"tag\":\"residual\",\"uses\":[");
470 7660 serializeList(file,Expression.extractUniqueCrefsFromExpDerPreStart(eq.exp), serializeCref);
471 7660 File.write(file, "],\"equation\":[\"");
472 7660 File.writeEscape(file,expStr(eq.exp),escape=JSON);
473 7660 File.write(file, "\"],\"source\":");
474 7660 serializeSource(file,eq.source,withOperations);
475 7660 File.write(file, "}");
476 then ();
477
478 case SimCode.SES_FOR_RESIDUAL() algorithm
479 14 File.write(file, "\n{\"eqIndex\":");
480 14 File.writeInt(file, eq.index);
481
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 if parent <> 0 then
482 14 File.write(file, ",\"parent\":");
483 14 File.writeInt(file, parent);
484 end if;
485 14 File.write(file, ",\"section\":\"");
486 14 File.write(file, section);
487 14 File.write(file, "\",\"tag\":\"residual\",\"uses\":[");
488 14 serializeList(file,Expression.extractUniqueCrefsFromExpDerPreStart(eq.exp), serializeCref);
489 14 File.write(file, "],\"equation\":[\"");
490 14 File.writeEscape(file,expStr(eq.exp),escape=JSON);
491 14 File.write(file, "\"],\"source\":");
492 14 serializeSource(file,eq.source,withOperations);
493 14 File.write(file, "}");
494 then ();
495
496 case SimCode.SES_GENERIC_RESIDUAL() algorithm
497 ✗ File.write(file, "\n{\"eqIndex\":");
498 ✗ File.writeInt(file, eq.index);
499 ✗ if parent <> 0 then
500 ✗ File.write(file, ",\"parent\":");
501 ✗ File.writeInt(file, parent);
502 end if;
503 ✗ File.write(file, ",\"section\":\"");
504 ✗ File.write(file, section);
505 ✗ File.write(file, "\",\"tag\":\"residual\",\"uses\":[");
506 ✗ serializeList(file,Expression.extractUniqueCrefsFromExpDerPreStart(eq.exp), serializeCref);
507 ✗ File.write(file, "],\"equation\":[\"");
508 ✗ File.writeEscape(file,expStr(eq.exp),escape=JSON);
509 ✗ File.write(file, "\"],\"source\":");
510 ✗ serializeSource(file,eq.source,withOperations);
511 ✗ File.write(file, "}");
512 then ();
513
514 case SimCode.SES_SIMPLE_ASSIGN() algorithm
515 261744 File.write(file, "\n{\"eqIndex\":");
516 261744 File.writeInt(file, eq.index);
517
2/2
✓ Branch 0 taken 39675 times.
✓ Branch 1 taken 222069 times.
261744 if parent <> 0 then
518 39675 File.write(file, ",\"parent\":");
519 39675 File.writeInt(file, parent);
520 end if;
521 261744 File.write(file, ",\"section\":\"");
522 261744 File.write(file, section);
523 261744 File.write(file, "\",\"tag\":\"" + tagFromAssignType(assign_type) + "\",\"defines\":[\"");
524 261744 writeCref(file,eq.cref,escape=JSON);
525 261744 File.write(file, "\"],\"uses\":[");
526 261744 serializeList(file,Expression.extractUniqueCrefsFromExpDerPreStart(eq.exp), serializeCref);
527 261744 File.write(file, "],\"equation\":[\"");
528 261744 File.writeEscape(file,expStr(eq.exp),escape=JSON);
529 261744 File.write(file, "\"],\"source\":");
530 261744 serializeSource(file,eq.source,withOperations);
531 261744 File.write(file, "}");
532 then ();
533
534 case SimCode.SES_RESIZABLE_ASSIGN() algorithm
535 385 File.write(file, "\n{\"eqIndex\":");
536 385 File.writeInt(file, eq.index);
537
2/2
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 275 times.
385 if parent <> 0 then
538 110 File.write(file, ",\"parent\":");
539 110 File.writeInt(file, parent);
540 end if;
541 385 File.write(file, ",\"section\":\"");
542 385 File.write(file, section);
543 385 File.write(file, "\",\"tag\":\"" + tagFromAssignType(assign_type) + "\",\"defines\":[\"");
544 385 File.write(file, "\"],\"source\":");
545 385 serializeSource(file,eq.source,withOperations);
546 385 File.write(file, "}");
547 then ();
548
549 case SimCode.SES_GENERIC_ASSIGN() algorithm
550 29 File.write(file, "\n{\"eqIndex\":");
551 29 File.writeInt(file, eq.index);
552
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 15 times.
29 if parent <> 0 then
553 14 File.write(file, ",\"parent\":");
554 14 File.writeInt(file, parent);
555 end if;
556 29 File.write(file, ",\"section\":\"");
557 29 File.write(file, section);
558 29 File.write(file, "\",\"tag\":\"" + tagFromAssignType(assign_type) + "\",\"defines\":[\"");
559 29 File.write(file, "\"],\"source\":");
560 29 serializeSource(file,eq.source,withOperations);
561 29 File.write(file, "}");
562 then ();
563
564 case SimCode.SES_ENTWINED_ASSIGN() algorithm
565 2 File.write(file, "\n{\"eqIndex\":");
566 2 File.writeInt(file, eq.index);
567
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if parent <> 0 then
568 ✗ File.write(file, ",\"parent\":");
569 ✗ File.writeInt(file, parent);
570 end if;
571 2 File.write(file, ",\"section\":\"");
572 2 File.write(file, section);
573 2 File.write(file, "\",\"tag\":\"" + tagFromAssignType(assign_type) + "\",\"defines\":[\"");
574 2 File.write(file, "\"],\"source\":");
575 2 serializeSource(file,eq.source,withOperations);
576 2 File.write(file, "}");
577 then ();
578
579 case SimCode.SES_SIMPLE_ASSIGN_CONSTRAINTS() algorithm
580 4 File.write(file, "\n{\"eqIndex\":");
581 4 File.writeInt(file, eq.index);
582
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if parent <> 0 then
583 4 File.write(file, ",\"parent\":");
584 4 File.writeInt(file, parent);
585 end if;
586 4 File.write(file, ",\"section\":\"");
587 4 File.write(file, section);
588 4 File.write(file, "\",\"tag\":\"" + tagFromAssignType(assign_type) + "\",\"defines\":[\"");
589 4 writeCref(file,eq.cref,escape=JSON);
590 4 File.write(file, "\"],\"uses\":[");
591 4 serializeList(file,Expression.extractUniqueCrefsFromExpDerPreStart(eq.exp), serializeCref);
592 4 File.write(file, "],\"equation\":[\"");
593 4 File.writeEscape(file,expStr(eq.exp),escape=JSON);
594 4 File.write(file, "\"],\"source\":");
595 4 serializeSource(file,eq.source,withOperations);
596 4 File.write(file, "}");
597 then ();
598
599 case SimCode.SES_ARRAY_CALL_ASSIGN() algorithm
600 982 File.write(file, "\n{\"eqIndex\":");
601 982 File.writeInt(file, eq.index);
602
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 920 times.
982 if parent <> 0 then
603 62 File.write(file, ",\"parent\":");
604 62 File.writeInt(file, parent);
605 end if;
606 982 File.write(file, ",\"section\":\"");
607 982 File.write(file, section);
608 982 File.write(file, "\",\"tag\":\"" + tagFromAssignType(assign_type) + "\",\"defines\":[\"");
609 982 writeCref(file,Expression.expCref(eq.lhs),escape=JSON);
610 982 File.write(file, "\"],\"uses\":[");
611 982 serializeList(file,Expression.extractUniqueCrefsFromExpDerPreStart(eq.exp), serializeCref);
612 982 File.write(file, "],\"equation\":[\"");
613 982 File.writeEscape(file,expStr(eq.exp),escape=JSON);
614 982 File.write(file, "\"],\"source\":");
615 982 serializeSource(file,eq.source,withOperations);
616 982 File.write(file, "}");
617 then ();
618
619 // no dynamic tearing
620 case SimCode.SES_LINEAR(lSystem = lSystem as SimCode.LINEARSYSTEM(), alternativeTearing = NONE()) algorithm
621 1056 i := listLength(lSystem.beqs);
622 1056 j := listLength(lSystem.simJac);
623
624 1056 eqs := SimCodeCodegenUtil.sortEqSystems(lSystem.residual);
625
2/2
✓ Branch 0 taken 916 times.
✓ Branch 1 taken 140 times.
1056 if not listEmpty(eqs) then
626
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 916 times.
916 serializeEquation(file,listHead(eqs),section,withOperations,parent=lSystem.index,first=true,assign_type=if lSystem.tornSystem then AssignType.TORN else AssignType.NORMAL);
627
3/4
✗ Branch 1 not taken.
✓ Branch 2 taken 11810 times.
✓ Branch 4 taken 11810 times.
✓ Branch 5 taken 916 times.
13642 for e in listRest(eqs) loop serializeEquation(file,e,section,withOperations,parent=lSystem.index,assign_type=if lSystem.tornSystem then AssignType.TORN else AssignType.NORMAL); end for;
628 end if;
629
630 jeqs := match lSystem.jacobianMatrix
631 916 case SOME(SimCode.JAC_MATRIX(columns={SimCode.JAC_COLUMN(columnEqns=jeqs,constantEqns=constantEqns)})) then SimCodeCodegenUtil.sortEqSystems(listAppend(jeqs,constantEqns));
632 else {};
633 end match;
634
2/2
✓ Branch 0 taken 914 times.
✓ Branch 1 taken 142 times.
1056 if not listEmpty(jeqs) then
635 914 File.write(file, ",");
636 914 serializeEquation(file,listHead(jeqs),section,withOperations,parent=lSystem.index,first=true,assign_type=AssignType.JACOBIAN);
637
2/2
✓ Branch 2 taken 10326 times.
✓ Branch 3 taken 914 times.
12154 for e in listRest(jeqs) loop serializeEquation(file,e,section,withOperations,parent=lSystem.index,assign_type=AssignType.JACOBIAN); end for;
638 end if;
639
640
3/4
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 916 times.
✓ Branch 2 taken 140 times.
✗ Branch 3 not taken.
1056 if listEmpty(eqs) and listEmpty(jeqs) then
641 140 File.write(file, "\n{\"eqIndex\":");
642 else
643 916 File.write(file, ",\n{\"eqIndex\":");
644 end if;
645 1056 File.writeInt(file, lSystem.index);
646
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1056 times.
1056 if parent <> 0 then
647 ✗ File.write(file, ",\"parent\":");
648 ✗ File.writeInt(file, parent);
649 end if;
650 1056 File.write(file, ",\"section\":\"");
651 1056 File.write(file, section);
652
653 // Ax=b
654
2/2
✓ Branch 0 taken 916 times.
✓ Branch 1 taken 140 times.
1056 if lSystem.tornSystem then
655 916 File.write(file, "\",\"tag\":\"tornsystem\"");
656 else
657 140 File.write(file, "\",\"tag\":\"system\"");
658 end if;
659
660 1056 File.write(file, ",\"display\":\"linear\",\"unknowns\":" + intString(lSystem.nUnknowns) + ",\"defines\":[");
661
4/4
✓ Branch 0 taken 7338 times.
✓ Branch 1 taken 1056 times.
✓ Branch 2 taken 7338 times.
✓ Branch 3 taken 1056 times.
8394 serializeList(file, list(v.name for v in lSystem.vars), serializeCref);
662 1056 File.write(file, "],\"uses\":[");
663 crefs := {};
664 1056 serializeList(file, crefs, serializeCref);
665 1056 File.write(file, "],\"equation\":[{\"size\":");
666 1056 File.write(file,intString(i));
667
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 916 times.
1056 if i <> 0 then
668 140 File.write(file,",\"density\":");
669
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 140 times.
140 File.writeReal(file,j / (i*i),format="%.2f");
670 end if;
671 1056 File.write(file,",\"A\":[");
672
2/2
✓ Branch 0 taken 1052 times.
✓ Branch 1 taken 4 times.
2108 serializeList(file, lSystem.simJac, function serializeLinearCell(withOperations = withOperations));
673 1056 File.write(file,"],\"b\":[");
674 1056 serializeList(file,lSystem.beqs,serializeExp);
675 1056 File.write(file,"]}]}");
676 then ();
677
678 // dynamic tearing
679 case SimCode.SES_LINEAR(lSystem = lSystem as SimCode.LINEARSYSTEM(), alternativeTearing = SOME(atL as SimCode.LINEARSYSTEM())) algorithm
680 // for strict tearing set
681 ✗ i := listLength(lSystem.beqs);
682 ✗ j := listLength(lSystem.simJac);
683
684 ✗ eqs := SimCodeCodegenUtil.sortEqSystems(lSystem.residual);
685 ✗ if not listEmpty(eqs) then
686 ✗ serializeEquation(file,listHead(eqs),section,withOperations,parent=lSystem.index,first=true,assign_type=if lSystem.tornSystem then AssignType.TORN else AssignType.NORMAL);
687 ✗ for e in listRest(eqs) loop serializeEquation(file,e,section,withOperations,parent=lSystem.index,assign_type=if lSystem.tornSystem then AssignType.TORN else AssignType.NORMAL); end for;
688 end if;
689
690 jeqs := match lSystem.jacobianMatrix
691 ✗ case SOME(SimCode.JAC_MATRIX(columns={SimCode.JAC_COLUMN(columnEqns=jeqs,constantEqns=constantEqns)})) then SimCodeCodegenUtil.sortEqSystems(listAppend(jeqs,constantEqns));
692 else {};
693 end match;
694 ✗ if not listEmpty(jeqs) then
695 ✗ File.write(file, ",");
696 ✗ serializeEquation(file,listHead(jeqs),section,withOperations,parent=lSystem.index,first=true,assign_type=AssignType.JACOBIAN);
697 ✗ for e in listRest(jeqs) loop serializeEquation(file,e,section,withOperations,parent=lSystem.index,assign_type=AssignType.JACOBIAN); end for;
698 end if;
699
700 ✗ if listEmpty(eqs) and listEmpty(jeqs) then
701 ✗ File.write(file, "\n{\"eqIndex\":");
702 else
703 ✗ File.write(file, ",\n{\"eqIndex\":");
704 end if;
705 ✗ File.writeInt(file, lSystem.index);
706 ✗ if parent <> 0 then
707 ✗ File.write(file, ",\"parent\":");
708 ✗ File.writeInt(file, parent);
709 end if;
710 ✗ File.write(file, ",\"section\":\"");
711 ✗ File.write(file, section);
712
713 // Ax=b
714 ✗ if lSystem.tornSystem then
715 ✗ File.write(file, "\",\"tag\":\"tornsystem\"");
716 else
717 ✗ File.write(file, "\",\"tag\":\"system\"");
718 end if;
719
720 ✗ File.write(file, ",\"display\":\"linear\",\"unknowns\":" + intString(lSystem.nUnknowns) + ",\"defines\":[");
721 ✗ serializeList(file, list(v.name for v in lSystem.vars), serializeCref);
722 ✗ File.write(file, "],\"uses\":[");
723 crefs := {};
724 ✗ serializeList(file, crefs, serializeCref);
725 ✗ File.write(file, "],\"equation\":[{\"size\":");
726 ✗ File.write(file,intString(i));
727 ✗ if i <> 0 then
728 ✗ File.write(file,",\"density\":");
729 ✗ File.writeReal(file,j / (i*i),format="%.2f");
730 end if;
731 ✗ File.write(file,",\"A\":[");
732 ✗ serializeList(file, lSystem.simJac, function serializeLinearCell(withOperations = withOperations));
733 ✗ File.write(file,"],\"b\":[");
734 ✗ serializeList(file,lSystem.beqs,serializeExp);
735 ✗ File.write(file,"]}]},");
736
737 // for casual tearing set
738 ✗ i := listLength(atL.beqs);
739 ✗ j := listLength(atL.simJac);
740
741 ✗ eqs := SimCodeCodegenUtil.sortEqSystems(atL.residual);
742 ✗ if not listEmpty(eqs) then
743 ✗ serializeEquation(file,listHead(eqs),section,withOperations,parent=atL.index,first=true,assign_type=if atL.tornSystem then AssignType.TORN else AssignType.NORMAL);
744 ✗ for e in listRest(eqs) loop serializeEquation(file,e,section,withOperations,parent=atL.index,assign_type=if atL.tornSystem then AssignType.TORN else AssignType.NORMAL); end for;
745 end if;
746
747 jeqs := match atL.jacobianMatrix
748 ✗ case SOME(SimCode.JAC_MATRIX(columns={SimCode.JAC_COLUMN(columnEqns=jeqs,constantEqns=constantEqns)})) then SimCodeCodegenUtil.sortEqSystems(listAppend(jeqs,constantEqns));
749 else {};
750 end match;
751 ✗ if not listEmpty(jeqs) then
752 ✗ File.write(file, ",");
753 ✗ serializeEquation(file,listHead(jeqs),section,withOperations,parent=atL.index,first=true,assign_type=AssignType.JACOBIAN);
754 ✗ for e in listRest(jeqs) loop serializeEquation(file,e,section,withOperations,parent=atL.index,assign_type=AssignType.JACOBIAN); end for;
755 end if;
756
757 ✗ if listEmpty(eqs) and listEmpty(jeqs) then
758 ✗ File.write(file, "\n{\"eqIndex\":");
759 else
760 ✗ File.write(file, ",\n{\"eqIndex\":");
761 end if;
762 ✗ File.writeInt(file, atL.index);
763 ✗ if parent <> 0 then
764 ✗ File.write(file, ",\"parent\":");
765 ✗ File.writeInt(file, parent);
766 end if;
767 ✗ File.write(file, ",\"section\":\"");
768 ✗ File.write(file, section);
769
770 // Ax=b
771 ✗ if atL.tornSystem then
772 ✗ File.write(file, "\",\"tag\":\"tornsystem\"");
773 else
774 ✗ File.write(file, "\",\"tag\":\"system\"");
775 end if;
776
777 ✗ File.write(file, ",\"display\":\"linear\",\"unknowns\":" + intString(atL.nUnknowns) + ",\"defines\":[");
778 ✗ serializeList(file, list(v.name for v in atL.vars), serializeCref);
779 ✗ File.write(file, "],\"uses\":[");
780 crefs := {};
781 ✗ serializeList(file, crefs, serializeCref);
782 ✗ File.write(file, "],\"equation\":[{\"size\":");
783 ✗ File.write(file,intString(i));
784 ✗ if i <> 0 then
785 ✗ File.write(file,",\"density\":");
786 ✗ File.writeReal(file,j / (i*i),format="%.2f");
787 end if;
788 ✗ File.write(file,",\"A\":[");
789 ✗ serializeList(file, atL.simJac, function serializeLinearCell(withOperations = withOperations));
790 ✗ File.write(file,"],\"b\":[");
791 ✗ serializeList(file,atL.beqs,serializeExp);
792 ✗ File.write(file,"]}]}");
793 then ();
794
795 case SimCode.SES_ALGORITHM(statements={stmt as DAE.STMT_ASSIGN()}) algorithm
796 785 File.write(file, "\n{\"eqIndex\":");
797 785 File.writeInt(file, eq.index);
798
2/2
✓ Branch 0 taken 199 times.
✓ Branch 1 taken 586 times.
785 if parent <> 0 then
799 199 File.write(file, ",\"parent\":");
800 199 File.writeInt(file, parent);
801 end if;
802 785 File.write(file, ",\"section\":\"");
803 785 File.write(file, section + "\",\"tag\":\"algorithm\",\"defines\":[\"");
804 785 writeCref(file, Expression.expCref(stmt.exp1),escape=JSON);
805 785 File.write(file, "\"],\"uses\":[");
806 785 serializeList(file, Expression.extractUniqueCrefsFromExpDerPreStart(stmt.exp), serializeCref);
807 785 File.write(file, "],\"equation\":[");
808 785 serializeList(file,eq.statements,serializeStatement);
809 785 File.write(file, "],\"source\":");
810 785 serializeSource(file,Algorithm.getStatementSource(stmt),withOperations);
811 785 File.write(file, "}");
812 then ();
813
814 case SimCode.SES_ALGORITHM(statements=stmt::_) algorithm
815 33506 File.write(file, "\n{\"eqIndex\":");
816 33506 File.writeInt(file, eq.index);
817
2/2
✓ Branch 0 taken 223 times.
✓ Branch 1 taken 33283 times.
33506 if parent <> 0 then
818 223 File.write(file, ",\"parent\":");
819 223 File.writeInt(file, parent);
820 end if;
821 33506 File.write(file, ",\"section\":\"");
822 33506 File.write(file, section + "\",\"tag\":\"algorithm\",\"equation\":[");
823 33506 serializeList(file,eq.statements,serializeStatement);
824 33506 File.write(file, "],\"source\":");
825 33506 serializeSource(file,Algorithm.getStatementSource(stmt),withOperations);
826 33506 File.write(file, "}");
827 then ();
828
829 case SimCode.SES_INVERSE_ALGORITHM(statements=stmt::_) algorithm
830 3 File.write(file, "\n{\"eqIndex\":");
831 3 File.writeInt(file, eq.index);
832
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if parent <> 0 then
833 3 File.write(file, ",\"parent\":");
834 3 File.writeInt(file, parent);
835 end if;
836 3 File.write(file, ",\"section\":\"");
837 3 File.write(file, section + "\",\"tag\":\"algorithm\",\"equation\":[");
838 3 serializeList(file,eq.statements,serializeStatement);
839 3 File.write(file, "],\"source\":");
840 3 serializeSource(file,Algorithm.getStatementSource(stmt),withOperations);
841 3 File.write(file, "}");
842 then ();
843
844 // no dynamic tearing
845 case SimCode.SES_NONLINEAR(nlSystem = nlSystem as SimCode.NONLINEARSYSTEM(), alternativeTearing = NONE()) algorithm
846 1097 eqs := SimCodeCodegenUtil.sortEqSystems(nlSystem.eqs);
847
2/2
✓ Branch 0 taken 20795 times.
✓ Branch 1 taken 1097 times.
21892 for e in eqs loop
848 try
849
2/2
✓ Branch 0 taken 524 times.
✓ Branch 1 taken 20271 times.
21319 serializeEquation(file,e,section,withOperations,parent=nlSystem.index,first=(SimCodeCodegenUtil.simEqSystemIndex(e)==SimCodeCodegenUtil.simEqSystemIndex(listHead(eqs))),assign_type=if nlSystem.tornSystem then AssignType.TORN else AssignType.NORMAL);
850 else
851 ✗ Error.addMessage(Error.INTERNAL_ERROR, {"SerializeModelInfo inner eq failed in NLS " + intString(nlSystem.index) + " for inner eqIndex=" + intString(SimCodeCodegenUtil.simEqSystemIndex(e))});
852 ✗ fail();
853 end try;
854 end for;
855
856 jeqs := match nlSystem.jacobianMatrix
857 384 case SOME(SimCode.JAC_MATRIX(columns={SimCode.JAC_COLUMN(columnEqns=jeqs,constantEqns=constantEqns)})) then SimCodeCodegenUtil.sortEqSystems(listAppend(jeqs,constantEqns));
858 else {};
859 end match;
860
2/2
✓ Branch 0 taken 383 times.
✓ Branch 1 taken 714 times.
1097 if not listEmpty(jeqs) then
861 383 File.write(file, ",");
862 383 serializeEquation(file,listHead(jeqs),section,withOperations,parent=nlSystem.index,first=true,assign_type=AssignType.JACOBIAN);
863
2/2
✓ Branch 2 taken 2770 times.
✓ Branch 3 taken 383 times.
3536 for e in listRest(jeqs) loop serializeEquation(file,e,section,withOperations,parent=nlSystem.index,assign_type=AssignType.JACOBIAN); end for;
864 end if;
865
866 1097 File.write(file, ",\n{\"eqIndex\":");
867 1097 File.writeInt(file, nlSystem.index);
868
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1097 times.
1097 if parent <> 0 then
869 ✗ File.write(file, ",\"parent\":");
870 ✗ File.writeInt(file, parent);
871 end if;
872 1097 File.write(file, ",\"section\":\"");
873 1097 File.write(file, section);
874
875
2/2
✓ Branch 0 taken 878 times.
✓ Branch 1 taken 219 times.
1097 if nlSystem.tornSystem then
876 878 File.write(file, "\",\"tag\":\"tornsystem\"");
877 else
878 219 File.write(file, "\",\"tag\":\"system\"");
879 end if;
880
881 1097 File.write(file, ",\"display\":\"non-linear\",\"unknowns\":" + intString(nlSystem.nUnknowns) + ",\"defines\":[");
882 1097 serializeList(file, nlSystem.crefs, serializeCref);
883 1097 File.write(file, "],\"uses\":[");
884 crefs := {};
885 1097 serializeList(file, crefs, serializeCref);
886 1097 File.write(file, "],\"equation\":[[");
887 1097 serializeList(file,eqs,serializeEquationIndex);
888 1097 File.write(file, "],[");
889 1097 serializeList(file,jeqs,serializeEquationIndex);
890 1097 File.write(file, "]]}");
891 then ();
892
893 // dynamic tearing
894 case SimCode.SES_NONLINEAR(nlSystem = nlSystem as SimCode.NONLINEARSYSTEM(), alternativeTearing = SOME(atNL as SimCode.NONLINEARSYSTEM())) algorithm
895 // for strict tearing set
896 3 eqs := SimCodeCodegenUtil.sortEqSystems(nlSystem.eqs);
897
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 serializeEquation(file,listHead(eqs),section,withOperations,parent=nlSystem.index,first=true,assign_type=if nlSystem.tornSystem then AssignType.TORN else AssignType.NORMAL);
898
3/4
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 3 times.
11 for e in listRest(eqs) loop serializeEquation(file,e,section,withOperations,parent=nlSystem.index,assign_type=if nlSystem.tornSystem then AssignType.TORN else AssignType.NORMAL); end for;
899
900 jeqs := match nlSystem.jacobianMatrix
901 2 case SOME(SimCode.JAC_MATRIX(columns={SimCode.JAC_COLUMN(columnEqns=jeqs,constantEqns=constantEqns)})) then SimCodeCodegenUtil.sortEqSystems(listAppend(jeqs,constantEqns));
902 else {};
903 end match;
904
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
3 if not listEmpty(jeqs) then
905 2 File.write(file, ",");
906 2 serializeEquation(file,listHead(jeqs),section,withOperations,parent=nlSystem.index,first=true,assign_type=AssignType.JACOBIAN);
907
2/2
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
6 for e in listRest(jeqs) loop serializeEquation(file,e,section,withOperations,parent=nlSystem.index,assign_type=AssignType.JACOBIAN); end for;
908 end if;
909
910 3 File.write(file, ",\n{\"eqIndex\":");
911 3 File.writeInt(file, nlSystem.index);
912
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if parent <> 0 then
913 ✗ File.write(file, ",\"parent\":");
914 ✗ File.writeInt(file, parent);
915 end if;
916 3 File.write(file, ",\"section\":\"");
917 3 File.write(file, section);
918
919
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if nlSystem.tornSystem then
920 3 File.write(file, "\",\"tag\":\"tornsystem\"");
921 else
922 ✗ File.write(file, "\",\"tag\":\"system\"");
923 end if;
924
925 3 File.write(file, ",\"display\":\"non-linear\",\"unknowns\":" + intString(nlSystem.nUnknowns) + ",\"defines\":[");
926 3 serializeList(file, nlSystem.crefs, serializeCref);
927 3 File.write(file, "],\"uses\":[");
928 crefs := {};
929 3 serializeList(file, crefs, serializeCref);
930 3 File.write(file, "],\"equation\":[[");
931 3 serializeList(file,eqs,serializeEquationIndex);
932 3 File.write(file, "],[");
933 3 serializeList(file,jeqs,serializeEquationIndex);
934 3 File.write(file, "]]},");
935
936 // for casual tearing set
937 3 eqs := SimCodeCodegenUtil.sortEqSystems(atNL.eqs);
938
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 serializeEquation(file,listHead(eqs),section,withOperations,parent=atNL.index,first=true,assign_type=if atNL.tornSystem then AssignType.TORN else AssignType.NORMAL);
939
3/4
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 3 times.
12 for e in listRest(eqs) loop serializeEquation(file,e,section,withOperations,parent=atNL.index,assign_type=if atNL.tornSystem then AssignType.TORN else AssignType.NORMAL); end for;
940
941 jeqs := match atNL.jacobianMatrix
942 2 case SOME(SimCode.JAC_MATRIX(columns={SimCode.JAC_COLUMN(columnEqns=jeqs,constantEqns=constantEqns)})) then SimCodeCodegenUtil.sortEqSystems(listAppend(jeqs,constantEqns));
943 else {};
944 end match;
945
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
3 if not listEmpty(jeqs) then
946 2 File.write(file, ",");
947 2 serializeEquation(file,listHead(jeqs),section,withOperations,parent=atNL.index,first=true,assign_type=AssignType.JACOBIAN);
948
2/2
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
6 for e in listRest(jeqs) loop serializeEquation(file,e,section,withOperations,parent=atNL.index,assign_type=AssignType.JACOBIAN); end for;
949 end if;
950
951 3 File.write(file, ",\n{\"eqIndex\":");
952 3 File.writeInt(file, atNL.index);
953
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if parent <> 0 then
954 ✗ File.write(file, ",\"parent\":");
955 ✗ File.writeInt(file, parent);
956 end if;
957 3 File.write(file, ",\"section\":\"");
958 3 File.write(file, section);
959
960
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if atNL.tornSystem then
961 3 File.write(file, "\",\"tag\":\"tornsystem\"");
962 else
963 ✗ File.write(file, "\",\"tag\":\"system\"");
964 end if;
965
966 3 File.write(file, ",\"display\":\"non-linear\",\"unknowns\":" + intString(atNL.nUnknowns) + ",\"defines\":[");
967 3 serializeList(file, atNL.crefs, serializeCref);
968 3 File.write(file, "],\"uses\":[");
969 crefs := {};
970 3 serializeList(file, crefs, serializeCref);
971 3 File.write(file, "],\"equation\":[[");
972 3 serializeList(file,eqs,serializeEquationIndex);
973 3 File.write(file, "],[");
974 3 serializeList(file,jeqs,serializeEquationIndex);
975 3 File.write(file, "]]}");
976 then ();
977
978 case SimCode.SES_IFEQUATION() algorithm
979
4/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 8 times.
16 eqs := listAppend(List.flatten(list(Util.tuple22(e) for e in eq.ifbranches)), eq.elsebranch);
980 8 serializeEquation(file,listHead(eqs),section,withOperations,first=true);
981
2/2
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 8 times.
24 for e in listRest(eqs) loop serializeEquation(file,e,section,withOperations); end for;
982 8 File.write(file, ",\n{\"eqIndex\":");
983 8 File.writeInt(file, eq.index);
984
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if parent <> 0 then
985 ✗ File.write(file, ",\"parent\":");
986 ✗ File.writeInt(file, parent);
987 end if;
988 8 File.write(file, ",\"section\":\"");
989 8 File.write(file, section);
990 8 File.write(file, "\",\"tag\":\"if-equation\",\"display\":\"if-equation\",\"equation\":[");
991 8 serializeList(file,eq.ifbranches,serializeIfBranch);
992 8 File.write(file, ",");
993 8 serializeIfBranch(file,(DAE.BCONST(true),eq.elsebranch));
994 8 File.write(file, "]}");
995 then ();
996
997 case SimCode.SES_MIXED() algorithm
998 ✗ serializeEquation(file,eq.cont,section,withOperations,first=true);
999 ✗ for e in eq.discEqs loop serializeEquation(file,e,section,withOperations); end for;
1000 ✗ File.write(file, ",\n{\"eqIndex\":");
1001 ✗ File.writeInt(file, eq.index);
1002 ✗ if parent <> 0 then
1003 ✗ File.write(file, ",\"parent\":");
1004 ✗ File.writeInt(file, parent);
1005 end if;
1006 ✗ File.write(file, ",\"section\":\"");
1007 ✗ File.write(file, section);
1008 ✗ File.write(file, "\",\"tag\":\"container\",\"display\":\"mixed\",\"defines\":[");
1009 ✗ serializeList(file, list(v.name for v in eq.discVars), serializeCref);
1010 ✗ File.write(file, "],\"uses\":[");
1011 crefs := {};
1012 ✗ serializeList(file, crefs, serializeCref);
1013 ✗ File.write(file, "],\"equation\":[");
1014 ✗ serializeEquationIndex(file,eq.cont);
1015 ✗ for e1 in eq.discEqs loop
1016 ✗ File.write(file,",");
1017 ✗ serializeEquationIndex(file,e1);
1018 end for;
1019 ✗ File.write(file, "]}");
1020 then ();
1021
1022 case SimCode.SES_WHEN() algorithm
1023 564 File.write(file, "\n{\"eqIndex\":");
1024 564 File.writeInt(file, eq.index);
1025
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 561 times.
564 if parent <> 0 then
1026 3 File.write(file, ",\"parent\":");
1027 3 File.writeInt(file, parent);
1028 end if;
1029 564 File.write(file, ",\"section\":\"");
1030 564 File.write(file, section);
1031
2/2
✓ Branch 0 taken 564 times.
✓ Branch 1 taken 564 times.
1128 for whenOps in eq.whenStmtLst loop
1032 () := match whenOps
1033 case whenOp as BackendDAE.ASSIGN() algorithm
1034 486 File.write(file, "\",\"tag\":\"when\",\"defines\":[");
1035 486 serializeExp(file,whenOp.left);
1036 486 File.write(file, "],\"uses\":[");
1037 486 serializeList(file, getWhenUses(eq.conditions, whenOp.right), serializeCref);
1038 486 File.write(file, "],\"equation\":[");
1039 486 serializeExp(file,whenOp.right);
1040 486 File.write(file, "],\"source\":");
1041 486 serializeSource(file,eq.source,withOperations);
1042 486 File.write(file, "}");
1043 then ();
1044 case whenOp as BackendDAE.REINIT() algorithm
1045 34 File.write(file, "\",\"tag\":\"when\",\"defines\":[");
1046 34 serializeCref(file,whenOp.stateVar);
1047 34 File.write(file, "],\"uses\":[");
1048 34 serializeList(file, getWhenUses(eq.conditions, whenOp.value), serializeCref);
1049 34 File.write(file, "],\"equation\":[");
1050 34 serializeExp(file,whenOp.value);
1051 34 File.write(file, "],\"source\":");
1052 34 serializeSource(file,eq.source,withOperations);
1053 34 File.write(file, "}");
1054 then ();
1055 case whenOp as BackendDAE.ASSERT() algorithm
1056 13 File.write(file, "\",\"tag\":\"when\"");
1057 13 File.write(file, ",\"uses\":[");
1058 13 crefs := Expression.extractCrefsFromExpDerPreStart(whenOp.condition);
1059 13 serializeList(file, getWhenUses(crefs, whenOp.message), serializeCref);
1060 13 File.write(file, "],\"equation\":[");
1061 13 serializeExp(file,whenOp.message);
1062 13 File.write(file, "],\"source\":");
1063 13 serializeSource(file,eq.source,withOperations);
1064 13 File.write(file, "}");
1065 then ();
1066 case whenOp as BackendDAE.TERMINATE() algorithm
1067 8 File.write(file, "\",\"tag\":\"when\"");
1068 8 File.write(file, ",\"uses\":[");
1069 8 serializeList(file, getWhenUses(eq.conditions, whenOp.message), serializeCref);
1070 8 File.write(file, "],\"equation\":[");
1071 8 serializeExp(file,whenOp.message);
1072 8 File.write(file, "],\"source\":");
1073 8 serializeSource(file,eq.source,withOperations);
1074 8 File.write(file, "}");
1075 then ();
1076 case whenOp as BackendDAE.NORETCALL() algorithm
1077 23 File.write(file, "\",\"tag\":\"when\"");
1078 23 File.write(file, ",\"uses\":[");
1079 23 serializeList(file, getWhenUses(eq.conditions, whenOp.exp), serializeCref);
1080 23 File.write(file, "],\"equation\":[");
1081 23 serializeExp(file,whenOp.exp);
1082 23 File.write(file, "],\"source\":");
1083 23 serializeSource(file,eq.source,withOperations);
1084 23 File.write(file, "}");
1085 then ();
1086 end match;
1087 end for;
1088 () := match eq.elseWhen
1089 local
1090 SimCode.SimEqSystem e;
1091 case SOME(e) algorithm
1092
1/2
✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
49 if SimCodeCodegenUtil.simEqSystemIndex(e) <>0 then
1093 49 serializeEquation(file,e,section,withOperations);
1094 end if;
1095 then ();
1096 else ();
1097 end match;
1098 then ();
1099
1100 case SimCode.SES_FOR_LOOP() algorithm
1101 ✗ File.write(file, "\n{\"eqIndex\":");
1102 ✗ File.writeInt(file, eq.index);
1103 ✗ if parent <> 0 then
1104 ✗ File.write(file, ",\"parent\":");
1105 ✗ File.writeInt(file, parent);
1106 end if;
1107 ✗ File.write(file, ",\"section\":\"");
1108 ✗ File.write(file, section);
1109 ✗ File.write(file, "\",\"tag\":\"" + tagFromAssignType(assign_type) + "\",\"defines\":[\"");
1110 ✗ writeCref(file,eq.cref,escape=JSON);
1111 ✗ File.write(file, "\"],\"uses\":[");
1112 ✗ serializeList(file, Expression.extractUniqueCrefsFromExpDerPreStart(eq.exp), serializeCref);
1113 ✗ File.write(file, "],\"equation\":[\"");
1114 ✗ File.writeEscape(file,expStr(eq.exp),escape=JSON);
1115 ✗ File.write(file, "\"],\"source\":");
1116 ✗ serializeSource(file,eq.source,withOperations);
1117 ✗ File.write(file, "}");
1118 then ();
1119
1120 case SimCode.SES_ALIAS() algorithm
1121 69369 File.write(file, "\n{\"eqIndex\":");
1122 69369 File.writeInt(file, eq.index);
1123 69369 File.write(file, ",\"tag\":\"alias\",\"equation\":[");
1124 69369 File.writeInt(file, eq.aliasOf);
1125 69369 File.write(file, "],\"section\":\"");
1126 69369 File.write(file, section);
1127 69369 File.write(file, "\"}");
1128 then ();
1129
1130 else algorithm
1131 ✗ Error.addInternalError("serializeEquation failed: " + anyString(eq), sourceInfo());
1132 ✗ then fail();
1133 end match;
1134 end serializeEquation;
1135
1136 function serializeLinearCell
1137 input File.File file;
1138 input tuple<Integer, Integer, SimCode.SimEqSystem> cell;
1139 input Boolean withOperations;
1140 algorithm
1141 () := match cell
1142 local
1143 Integer i,j;
1144 SimCode.SimEqSystem eq;
1145 case (i,j,eq as SimCode.SES_RESIDUAL())
1146 algorithm
1147 11050 File.write(file,"{\"row\":");
1148 11050 File.write(file,intString(i));
1149 11050 File.write(file,",\"column\":");
1150 11050 File.write(file,intString(j));
1151 11050 File.write(file,",\"exp\":\"");
1152 11050 File.writeEscape(file,expStr(eq.exp),escape=JSON);
1153 11050 File.write(file,"\",\"source\":");
1154 11050 serializeSource(file,eq.source,withOperations);
1155 11050 File.write(file,"}");
1156 then ();
1157 else
1158 algorithm
1159 ✗ Error.addMessage(Error.INTERNAL_ERROR,{"SerializeModelInfo.serializeLinearCell failed. Expected only SES_RESIDUAL as input."});
1160 ✗ then fail();
1161 end match;
1162 end serializeLinearCell;
1163
1164 function varKindString
1165 input BackendDAE.VarKind varKind;
1166 input SimCodeVar.SimVar var;
1167 output String str;
1168 algorithm
1169 str := match varKind
1170 case BackendDAE.VARIABLE() then "variable";
1171 case BackendDAE.STATE() then "state"; // Output number of times it was differentiated?
1172 case BackendDAE.STATE_DER() then "derivative";
1173 case BackendDAE.DUMMY_DER() then "dummy derivative";
1174 case BackendDAE.DUMMY_STATE() then "dummy state";
1175 case BackendDAE.CLOCKED_STATE() then "clocked state";
1176 case BackendDAE.DISCRETE() then "discrete";
1177 case BackendDAE.PARAM() then "parameter";
1178 case BackendDAE.CONST() then "constant";
1179 case BackendDAE.EXTOBJ() then "external object";
1180 case BackendDAE.JAC_VAR() then "jacobian variable";
1181 case BackendDAE.JAC_TMP_VAR() then "jacobian differentiated variable";
1182 case BackendDAE.OPT_CONSTR() then "constraint";
1183 case BackendDAE.OPT_FCONSTR() then "final constraint";
1184 case BackendDAE.OPT_INPUT_WITH_DER() then "use derivation of input";
1185 case BackendDAE.OPT_INPUT_DER() then "derivation of input";
1186 case BackendDAE.OPT_TGRID() then "time grid for optimization";
1187 case BackendDAE.OPT_LOOP_INPUT() then "variable for transform loop in constraint";
1188 case BackendDAE.ALG_STATE() then "helper variable transform ode for symSolver";
1189 case BackendDAE.ALG_STATE_OLD() then "helper variable transform ode for symSolver";
1190 case BackendDAE.LOOP_ITERATION() then "iteration variable for solving an algebraic loop";
1191 case BackendDAE.DAE_RESIDUAL_VAR() then "residual variable for dae mode";
1192 else
1193 algorithm
1194 ✗ Error.addMessage(Error.INTERNAL_ERROR, {getInstanceName() + " failed for " + SimCodeCodegenUtil.simVarString(var)});
1195 ✗ then fail();
1196 end match;
1197 end varKindString;
1198
1199 function getWhenUses
1200 input list<DAE.ComponentRef> conditions;
1201 input DAE.Exp value;
1202 output list<DAE.ComponentRef> uses;
1203 algorithm
1204 564 uses := listAppend(conditions, Expression.extractCrefsFromExpDerPreStart(value));
1205 564 uses := UnorderedSet.unique_list(uses, ComponentReferenceBasics.hashComponentRef, ComponentReferenceBasics.crefEqual);
1206 end getWhenUses;
1207
1208 function serializeStatement
1209 input File.File file;
1210 input DAE.Statement stmt;
1211 algorithm
1212 37528 File.write(file,"\"");
1213 37528 File.writeEscape(file, System.trim(DAEDump.ppStatementStr(stmt)), escape=JSON);
1214 37528 File.write(file,"\"");
1215 end serializeStatement;
1216
1217 function serializeList<ArgType>
1218 input File.File file;
1219 input list<ArgType> lst;
1220 input FuncType func;
1221 input Boolean append = false "start with sep";
1222 input String sep = "," "separator between elements";
1223
1224 partial function FuncType
1225 input File.File file;
1226 input ArgType a;
1227 end FuncType;
1228 algorithm
1229
2/2
✓ Branch 0 taken 150272 times.
✓ Branch 1 taken 393397 times.
543669 if not listEmpty(lst) then
1230
2/2
✓ Branch 0 taken 4540 times.
✓ Branch 1 taken 388857 times.
393397 if append then
1231 4540 File.write(file, sep);
1232 end if;
1233
2/2
✓ Branch 0 taken 5874 times.
✓ Branch 1 taken 387523 times.
393397 func(file, listHead(lst));
1234
2/2
✓ Branch 1 taken 734858 times.
✓ Branch 2 taken 393397 times.
1128255 for a in listRest(lst) loop
1235 734858 File.write(file, sep);
1236
2/2
✓ Branch 0 taken 401164 times.
✓ Branch 1 taken 333694 times.
734858 func(file, a);
1237 end for;
1238 end if;
1239 end serializeList;
1240
1241 function serializeExp
1242 input File.File file;
1243 input DAE.Exp exp;
1244 algorithm
1245 4170 File.write(file, "\"");
1246 4170 File.writeEscape(file, expStr(exp), escape=JSON);
1247 4170 File.write(file, "\"");
1248 end serializeExp;
1249
1250 function serializeCref
1251 input File.File file;
1252 input DAE.ComponentRef cr;
1253 algorithm
1254 431652 File.write(file, "\"");
1255 431652 writeCref(file, cr, escape=JSON);
1256 431652 File.write(file, "\"");
1257 end serializeCref;
1258
1259 function serializeString
1260 input File.File file;
1261 input String string;
1262 algorithm
1263 ✗ File.write(file, "\"");
1264 ✗ File.writeEscape(file, string, escape=JSON);
1265 ✗ File.write(file, "\"");
1266 end serializeString;
1267
1268 function serializePath
1269 input File.File file;
1270 input Absyn.Path path;
1271 protected
1272 Absyn.Path p=path;
1273 Boolean b=true;
1274 algorithm
1275 226008 File.write(file, "\"");
1276
2/2
✓ Branch 0 taken 561121 times.
✓ Branch 1 taken 226008 times.
787129 while b loop
1277 (p,b) := match p
1278 case Absyn.IDENT()
1279 algorithm
1280 226008 File.writeEscape(file, p.name, escape=JSON);
1281 then (p,false);
1282 case Absyn.QUALIFIED()
1283 algorithm
1284 330974 File.writeEscape(file, p.name, escape=JSON);
1285 330974 File.write(file, ".");
1286 330974 then (p.path,true);
1287 case Absyn.FULLYQUALIFIED()
1288 4139 then (p.path,true);
1289 end match;
1290 end while;
1291 226008 File.write(file, "\"");
1292 end serializePath;
1293
1294 function serializeEquationIndex
1295 input File.File file;
1296 input SimCode.SimEqSystem eq;
1297 algorithm
1298 23989 File.writeInt(file, SimCodeCodegenUtil.simEqSystemIndex(eq));
1299 end serializeEquationIndex;
1300
1301 function serializeIfBranch
1302 input File.File file;
1303 input tuple<DAE.Exp,list<SimCode.SimEqSystem>> branch;
1304 protected
1305 DAE.Exp exp;
1306 list<SimCode.SimEqSystem> eqs;
1307 algorithm
1308 16 (exp,eqs) := branch;
1309 16 File.write(file,"[");
1310 16 serializeExp(file,exp);
1311 16 serializeList(file, eqs, serializeEquationIndex, true);
1312 16 File.write(file,"]");
1313 end serializeIfBranch;
1314
1315 function writeEqExpStr
1316 input File.File file;
1317 input DAE.EquationExp eqExp;
1318 algorithm
1319 () := match eqExp
1320 case DAE.PARTIAL_EQUATION()
1321 algorithm
1322 ✗ File.writeEscape(file,expStr(eqExp.exp),escape=JSON);
1323 then ();
1324 case DAE.RESIDUAL_EXP()
1325 algorithm
1326 ✗ File.write(file,"0 = ");
1327 ✗ File.writeEscape(file,expStr(eqExp.exp),escape=JSON);
1328 then ();
1329 case DAE.EQUALITY_EXPS()
1330 algorithm
1331 2 File.writeEscape(file,expStr(eqExp.lhs),escape=JSON);
1332 2 File.write(file," = ");
1333 2 File.writeEscape(file,expStr(eqExp.rhs),escape=JSON);
1334 then ();
1335 end match;
1336 end writeEqExpStr;
1337
1338 function serializeFunction
1339 input File.File file;
1340 input SimCodeFunction.Function func;
1341 algorithm
1342 10601 File.write(file, "\n");
1343 10601 serializePath(file, SimCodeUtil.functionPath(func));
1344 end serializeFunction;
1345
1346 annotation(__OpenModelica_Interface="backend_tools");
1347 end SerializeModelInfo;
1348