Linux GNU 11.4.0 Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 26.2% 124 / 0 / 473
Functions: -% 0 / 1 / 1
Branches: 21.1% 40 / 0 / 190

OMCompiler/Compiler/SimCode/SerializeTaskSystemInfo.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 SerializeTaskSystemInfo
37
38 import Absyn;
39 import BackendDAE;
40 import DAE;
41 import SimCode;
42
43 protected
44 import Algorithm;
45 import DAEDump;
46 import Error;
47 import Expression;
48 import File;
49 import File.Escape.JSON;
50 import writeCref = ComponentReference.writeCref;
51 import expStr = ExpressionBasics.printExpStr;
52 import List;
53 import SimCodeVar;
54 import System;
55 import Util;
56 import SimCodeCodegenUtil;
57
58 public
59 function serializeParMod
60 input SimCode.SimCode code;
61 input Boolean withOperations;
62 output String fileName;
63 algorithm
64
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1 (true,fileName) := serializeParModWork(code,withOperations);
65 end serializeParMod;
66
67 protected
68 function serializeParModWork "Always succeeds in order to clean-up external objects"
69 input SimCode.SimCode code;
70 input Boolean withOperations;
71 output Boolean success; // We always need to return in order to clean up external objects
72 output String fileName;
73 protected
74 File.File file = File.File();
75 SimCode.ModelInfo mi;
76 SimCodeVar.SimVars vars;
77 algorithm
78 try
79 1 SimCode.SIMCODE(modelInfo=mi as SimCode.MODELINFO(vars=vars)) := code;
80 1 fileName := code.fileNamePrefix + "_ode.json";
81 1 File.open(file,fileName,File.Mode.Write);
82 1 File.write(file, "{\"format\":\"ParModelica task system info\",\"version\":1,\n\"info\":{\"name\":");
83 1 serializePath(file, mi.name);
84 1 File.write(file, ",\"description\":\"");
85 1 File.writeEscape(file, mi.description, escape=JSON);
86 1 File.write(file, "\"},\n\"ode-equations\":[");
87 // Handle no comma for the first equation
88 1 File.write(file,"{\"eqIndex\":0,\"tag\":\"dummy\"}");
89
2/2
✓ Branch 2 taken 833 times.
✓ Branch 3 taken 1 time.
835 for eq in SimCodeCodegenUtil.sortEqSystems(List.flatten(code.odeEquations)) loop
90 833 serializeEquation(file,eq,"regular",withOperations);
91 end for;
92 1 File.write(file, "\n]\n}");
93 // file.close();
94 success := true;
95 else
96 fileName := "SerializeTaskSystemInfo.serializeParModWork failed";
97 ✗ Error.addInternalError(fileName, sourceInfo());
98 success := false;
99 end try;
100 end serializeParModWork;
101
102 function serializeEquation
103 input File.File file;
104 input SimCode.SimEqSystem eq;
105 input String section;
106 input Boolean withOperations;
107 input Integer parent = 0 "No parent";
108 input Boolean first = false;
109 input Integer assign_type = 0 "0: normal equation, 1: torn equation, 2: jacobian equation";
110 output Boolean success;
111 algorithm
112
2/2
✓ Branch 0 taken 983 times.
✓ Branch 1 taken 52 times.
1035 if not first then
113 983 File.write(file, ",");
114 end if;
115 success := match eq
116 local
117 Integer i,j;
118 DAE.Statement stmt;
119 list<SimCode.SimEqSystem> eqs,jeqs,constantEqns;
120 SimCode.LinearSystem lSystem, atL;
121 SimCode.NonlinearSystem nlSystem, atNL;
122 BackendDAE.WhenOperator whenOp;
123 list<DAE.ComponentRef> crefs, crefs2;
124
125 case SimCode.SES_RESIDUAL() algorithm
126 67 File.write(file, "\n{\"eqIndex\":");
127 67 File.writeInt(file, eq.index);
128
1/2
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
67 if parent <> 0 then
129 67 File.write(file, ",\"parent\":");
130 67 File.writeInt(file, parent);
131 end if;
132 67 File.write(file, ",\"section\":\"");
133 67 File.write(file, section);
134 67 File.write(file, "\",\"tag\":\"residual\",\"uses\":[");
135 67 serializeUses(file,Expression.extractUniqueCrefsFromExpDerPreStart(eq.exp));
136 67 File.write(file, "],\"equation\":[\"");
137 67 File.writeEscape(file,expStr(eq.exp),escape=JSON);
138 67 File.write(file, "\"],\"source\":");
139 serializeSource(file,eq.source,withOperations);
140 67 File.write(file, "}");
141 then true;
142
143 case SimCode.SES_SIMPLE_ASSIGN() algorithm
144 921 File.write(file, "\n{\"eqIndex\":");
145 921 File.writeInt(file, eq.index);
146
2/2
✓ Branch 0 taken 135 times.
✓ Branch 1 taken 786 times.
921 if parent <> 0 then
147 135 File.write(file, ",\"parent\":");
148 135 File.writeInt(file, parent);
149 end if;
150 921 File.write(file, ",\"section\":\"");
151 921 File.write(file, section);
152
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 796 times.
921 if (assign_type==1) then
153 125 File.write(file, "\",\"tag\":\"torn\",\"defines\":[\"");
154 elseif (assign_type==2) then
155 10 File.write(file, "\",\"tag\":\"jacobian\",\"defines\":[\"");
156 else
157 786 File.write(file, "\",\"tag\":\"assign\",\"defines\":[\"");
158 end if;
159 921 writeCref(file,eq.cref,escape=JSON);
160 921 File.write(file, "\"],\"uses\":[");
161 921 serializeUses(file,Expression.extractUniqueCrefsFromExpDerPreStart(eq.exp));
162 921 File.write(file, "],\"equation\":[\"");
163 921 File.writeEscape(file,expStr(eq.exp),escape=JSON);
164 921 File.write(file, "\"],\"source\":");
165 serializeSource(file,eq.source,withOperations);
166 921 File.write(file, "}");
167 then true;
168
169 case SimCode.SES_SIMPLE_ASSIGN_CONSTRAINTS() algorithm
170 ✗ File.write(file, "\n{\"eqIndex\":");
171 ✗ File.writeInt(file, eq.index);
172 ✗ if parent <> 0 then
173 ✗ File.write(file, ",\"parent\":");
174 ✗ File.writeInt(file, parent);
175 end if;
176 ✗ File.write(file, ",\"section\":\"");
177 ✗ File.write(file, section);
178 ✗ if (assign_type==1) then
179 ✗ File.write(file, "\",\"tag\":\"torn\",\"defines\":[\"");
180 elseif (assign_type==2) then
181 ✗ File.write(file, "\",\"tag\":\"jacobian\",\"defines\":[\"");
182 else
183 ✗ File.write(file, "\",\"tag\":\"assign\",\"defines\":[\"");
184 end if;
185 ✗ writeCref(file,eq.cref,escape=JSON);
186 ✗ File.write(file, "\"],\"uses\":[");
187 ✗ serializeUses(file,Expression.extractUniqueCrefsFromExpDerPreStart(eq.exp));
188 ✗ File.write(file, "],\"equation\":[\"");
189 ✗ File.writeEscape(file,expStr(eq.exp),escape=JSON);
190 ✗ File.write(file, "\"],\"source\":");
191 serializeSource(file,eq.source,withOperations);
192 ✗ File.write(file, "}");
193 then true;
194
195 case SimCode.SES_ARRAY_CALL_ASSIGN() algorithm
196 ✗ File.write(file, "\n{\"eqIndex\":");
197 ✗ File.writeInt(file, eq.index);
198 ✗ if parent <> 0 then
199 ✗ File.write(file, ",\"parent\":");
200 ✗ File.writeInt(file, parent);
201 end if;
202 ✗ File.write(file, ",\"section\":\"");
203 ✗ File.write(file, section);
204 ✗ if (assign_type==1) then
205 ✗ File.write(file, "\",\"tag\":\"torn\",\"defines\":[\"");
206 elseif (assign_type==2) then
207 ✗ File.write(file, "\",\"tag\":\"jacobian\",\"defines\":[\"");
208 else
209 ✗ File.write(file, "\",\"tag\":\"assign\",\"defines\":[\"");
210 end if;
211 ✗ writeCref(file,Expression.expCref(eq.lhs),escape=JSON);
212 ✗ File.write(file, "\"],\"uses\":[");
213 ✗ serializeUses(file,Expression.extractUniqueCrefsFromExpDerPreStart(eq.exp));
214 ✗ File.write(file, "],\"equation\":[\"");
215 ✗ File.writeEscape(file,expStr(eq.exp),escape=JSON);
216 ✗ File.write(file, "\"],\"source\":");
217 serializeSource(file,eq.source,withOperations);
218 ✗ File.write(file, "}");
219 then true;
220
221 // no dynamic tearing
222 case SimCode.SES_LINEAR(lSystem = lSystem as SimCode.LINEARSYSTEM(), alternativeTearing = NONE()) algorithm
223 5 i := listLength(lSystem.beqs);
224 5 j := listLength(lSystem.simJac);
225 5 eqs := SimCodeCodegenUtil.sortEqSystems(lSystem.residual);
226 jeqs := match lSystem.jacobianMatrix
227 5 case SOME(SimCode.JAC_MATRIX(columns={SimCode.JAC_COLUMN(columnEqns=jeqs,constantEqns=constantEqns)})) then SimCodeCodegenUtil.sortEqSystems(listAppend(jeqs,constantEqns));
228 else {};
229 end match;
230
231 5 File.write(file, "\n{\"eqIndex\":");
232 5 File.writeInt(file, lSystem.index);
233
234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if parent <> 0 then
235 ✗ File.write(file, ",\"parent\":");
236 ✗ File.writeInt(file, parent);
237 end if;
238 5 File.write(file, ",\"section\":\"");
239 5 File.write(file, section);
240
241 // Ax=b
242
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if lSystem.tornSystem then
243 5 File.write(file, "\",\"tag\":\"tornsystem\"");
244 else
245 ✗ File.write(file, "\",\"tag\":\"system\"");
246 end if;
247
248 5 File.write(file, ",\"display\":\"linear\",\"unknowns\":" + intString(lSystem.nUnknowns) + ",\"defines\":[");
249
4/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 5 times.
15 serializeUses(file,list(match v case SimCodeVar.SIMVAR() then v.name; end match
250 for v in lSystem.vars));
251 5 File.write(file, "],\"equation\":[{\"size\":");
252 5 File.write(file,intString(i));
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if i <> 0 then
254 ✗ File.write(file,",\"density\":");
255 ✗ File.writeReal(file,j / (i*i),format="%.2f");
256 end if;
257 5 File.write(file,",\"A\":[");
258
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
10 serializeList1(file,lSystem.simJac,withOperations,serializeLinearCell);
259 5 File.write(file,"],\"b\":[");
260 5 serializeList(file,lSystem.beqs,serializeExp);
261 5 File.write(file,"]}]");
262
263 5 File.write(file, ",\n\"internal-equations\":[");
264
265
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if not listEmpty(eqs) then
266
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 serializeEquation(file,listHead(eqs),section,withOperations,parent=lSystem.index,first=true,assign_type=if lSystem.tornSystem then 1 else 0);
267
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5 times.
10 for e in listRest(eqs) loop
268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 serializeEquation(file,e,section,withOperations,parent=lSystem.index,assign_type=if lSystem.tornSystem then 1 else 0);
269 end for;
270 end if;
271 5 File.write(file, "\n]");
272
273 5 File.write(file, ",\n\"jacobian-equations\":[");
274
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if not listEmpty(jeqs) then
275 5 serializeEquation(file,listHead(jeqs),section,withOperations,parent=lSystem.index,first=true,assign_type=2);
276
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5 times.
10 for e in listRest(jeqs) loop
277 5 serializeEquation(file,e,section,withOperations,parent=lSystem.index,assign_type=2);
278 end for;
279 end if;
280
281 5 File.write(file, "\n]}");
282 then true;
283
284 // dynamic tearing
285 case SimCode.SES_LINEAR(lSystem = lSystem as SimCode.LINEARSYSTEM(), alternativeTearing = SOME(atL as SimCode.LINEARSYSTEM())) algorithm
286 // for strict tearing set
287 ✗ i := listLength(lSystem.beqs);
288 ✗ j := listLength(lSystem.simJac);
289
290 ✗ eqs := SimCodeCodegenUtil.sortEqSystems(lSystem.residual);
291 ✗ if not listEmpty(eqs) then
292 ✗ serializeEquation(file,listHead(eqs),section,withOperations,parent=lSystem.index,first=true,assign_type=if lSystem.tornSystem then 1 else 0);
293 ✗ for e in listRest(eqs) loop
294 ✗ serializeEquation(file,e,section,withOperations,parent=lSystem.index,assign_type=if lSystem.tornSystem then 1 else 0);
295 end for;
296 end if;
297
298 jeqs := match lSystem.jacobianMatrix
299 ✗ case SOME(SimCode.JAC_MATRIX(columns={SimCode.JAC_COLUMN(columnEqns=jeqs,constantEqns=constantEqns)})) then SimCodeCodegenUtil.sortEqSystems(listAppend(jeqs,constantEqns));
300 else {};
301 end match;
302 ✗ if not listEmpty(jeqs) then
303 ✗ File.write(file, ",");
304 ✗ serializeEquation(file,listHead(jeqs),section,withOperations,parent=lSystem.index,first=true,assign_type=2);
305 ✗ for e in listRest(jeqs) loop
306 ✗ serializeEquation(file,e,section,withOperations,parent=lSystem.index,assign_type=2);
307 end for;
308 end if;
309
310 ✗ if listEmpty(eqs) and listEmpty(jeqs) then
311 ✗ File.write(file, "\n{\"eqIndex\":");
312 else
313 ✗ File.write(file, ",\n{\"eqIndex\":");
314 end if;
315 ✗ File.writeInt(file, lSystem.index);
316 ✗ if parent <> 0 then
317 ✗ File.write(file, ",\"parent\":");
318 ✗ File.writeInt(file, parent);
319 end if;
320 ✗ File.write(file, ",\"section\":\"");
321 ✗ File.write(file, section);
322
323 // Ax=b
324 ✗ if lSystem.tornSystem then
325 ✗ File.write(file, "\",\"tag\":\"tornsystem-dynamic\"");
326 else
327 ✗ File.write(file, "\",\"tag\":\"system-dynamic\"");
328 end if;
329
330 ✗ File.write(file, ",\"display\":\"linear\",\"unknowns\":" + intString(lSystem.nUnknowns) + ",\"defines\":[");
331 ✗ serializeUses(file,list(match v case SimCodeVar.SIMVAR() then v.name; end match
332 for v in lSystem.vars));
333 ✗ File.write(file, "],\"equation\":[{\"size\":");
334 ✗ File.write(file,intString(i));
335 ✗ if i <> 0 then
336 ✗ File.write(file,",\"density\":");
337 ✗ File.writeReal(file,j / (i*i),format="%.2f");
338 end if;
339 ✗ File.write(file,",\"A\":[");
340 ✗ serializeList1(file,lSystem.simJac,withOperations,serializeLinearCell);
341 ✗ File.write(file,"],\"b\":[");
342 ✗ serializeList(file,lSystem.beqs,serializeExp);
343 ✗ File.write(file,"]}]},");
344
345 // for casual tearing set
346 ✗ i := listLength(atL.beqs);
347 ✗ j := listLength(atL.simJac);
348
349 ✗ eqs := SimCodeCodegenUtil.sortEqSystems(atL.residual);
350 ✗ if not listEmpty(eqs) then
351 ✗ serializeEquation(file,listHead(eqs),section,withOperations,parent=atL.index,first=true,assign_type=if atL.tornSystem then 1 else 0);
352 ✗ for e in listRest(eqs) loop
353 ✗ serializeEquation(file,e,section,withOperations,parent=atL.index,assign_type=if atL.tornSystem then 1 else 0);
354 end for;
355 end if;
356
357 jeqs := match atL.jacobianMatrix
358 ✗ case SOME(SimCode.JAC_MATRIX(columns={SimCode.JAC_COLUMN(columnEqns=jeqs,constantEqns=constantEqns)})) then SimCodeCodegenUtil.sortEqSystems(listAppend(jeqs,constantEqns));
359 else {};
360 end match;
361 ✗ if not listEmpty(jeqs) then
362 ✗ File.write(file, ",");
363 ✗ serializeEquation(file,listHead(jeqs),section,withOperations,parent=atL.index,first=true,assign_type=2);
364 ✗ for e in listRest(jeqs) loop
365 ✗ serializeEquation(file,e,section,withOperations,parent=atL.index,assign_type=2);
366 end for;
367 end if;
368
369 ✗ if listEmpty(eqs) and listEmpty(jeqs) then
370 ✗ File.write(file, "\n{\"eqIndex\":");
371 else
372 ✗ File.write(file, ",\n{\"eqIndex\":");
373 end if;
374 ✗ File.writeInt(file, atL.index);
375 ✗ if parent <> 0 then
376 ✗ File.write(file, ",\"parent\":");
377 ✗ File.writeInt(file, parent);
378 end if;
379 ✗ File.write(file, ",\"section\":\"");
380 ✗ File.write(file, section);
381
382 // Ax=b
383 ✗ if atL.tornSystem then
384 ✗ File.write(file, "\",\"tag\":\"tornsystem\"");
385 else
386 ✗ File.write(file, "\",\"tag\":\"system\"");
387 end if;
388
389 ✗ File.write(file, ",\"display\":\"linear\",\"unknowns\":" + intString(atL.nUnknowns) + ",\"defines\":[");
390 ✗ serializeUses(file,list(match v case SimCodeVar.SIMVAR() then v.name; end match
391 for v in atL.vars));
392 ✗ File.write(file, "],\"equation\":[{\"size\":");
393 ✗ File.write(file,intString(i));
394 ✗ if i <> 0 then
395 ✗ File.write(file,",\"density\":");
396 ✗ File.writeReal(file,j / (i*i),format="%.2f");
397 end if;
398 ✗ File.write(file,",\"A\":[");
399 ✗ serializeList1(file,atL.simJac,withOperations,serializeLinearCell);
400 ✗ File.write(file,"],\"b\":[");
401 ✗ serializeList(file,atL.beqs,serializeExp);
402 ✗ File.write(file,"]}]}");
403 then true;
404
405 case SimCode.SES_ALGORITHM(statements=stmt::_) algorithm
406 ✗ File.write(file, "\n{\"eqIndex\":");
407 ✗ File.writeInt(file, eq.index);
408 ✗ if parent <> 0 then
409 ✗ File.write(file, ",\"parent\":");
410 ✗ File.writeInt(file, parent);
411 end if;
412 ✗ File.write(file, ",\"section\":\"");
413 ✗ File.write(file, section + "\",\"tag\":\"algorithm\",\"defines\":[");
414 ✗ (crefs, crefs2) := Expression.extractUniqueCrefsFromStatmentS(eq.statements);
415 ✗ serializeUses(file,crefs);
416 ✗ File.write(file, "],\"uses\":[");
417 ✗ serializeUses(file,crefs2);
418 ✗ File.write(file, "],\"equation\":[");
419 ✗ serializeList(file,eq.statements,serializeStatement);
420 ✗ File.write(file, "],\"source\":");
421 ✗ serializeSource(file,Algorithm.getStatementSource(stmt),withOperations);
422 ✗ File.write(file, "}");
423 then true;
424
425 case SimCode.SES_INVERSE_ALGORITHM(statements=stmt::_) algorithm
426 ✗ File.write(file, "\n{\"eqIndex\":");
427 ✗ File.writeInt(file, eq.index);
428 ✗ if parent <> 0 then
429 ✗ File.write(file, ",\"parent\":");
430 ✗ File.writeInt(file, parent);
431 end if;
432 ✗ File.write(file, ",\"section\":\"");
433 ✗ File.write(file, section + "\",\"tag\":\"algorithm\",\"defines\":[");
434 ✗ (crefs, crefs2) := Expression.extractUniqueCrefsFromStatmentS(eq.statements);
435 ✗ serializeUses(file,crefs);
436 ✗ File.write(file, "],\"uses\":[");
437 ✗ serializeUses(file,crefs2);
438 ✗ File.write(file, "],\"equation\":[");
439 ✗ serializeList(file,eq.statements,serializeStatement);
440 ✗ File.write(file, "],\"source\":");
441 ✗ serializeSource(file,Algorithm.getStatementSource(stmt),withOperations);
442 ✗ File.write(file, "}");
443 then true;
444
445 // no dynamic tearing
446 case SimCode.SES_NONLINEAR(nlSystem = nlSystem as SimCode.NONLINEARSYSTEM(), alternativeTearing = NONE()) algorithm
447 42 eqs := SimCodeCodegenUtil.sortEqSystems(nlSystem.eqs);
448 jeqs := match nlSystem.jacobianMatrix
449 ✗ case SOME(SimCode.JAC_MATRIX(columns={SimCode.JAC_COLUMN(columnEqns=jeqs,constantEqns=constantEqns)})) then SimCodeCodegenUtil.sortEqSystems(listAppend(jeqs,constantEqns));
450 else {};
451 end match;
452
453 42 File.write(file, "\n{\"eqIndex\":");
454 42 File.writeInt(file, nlSystem.index);
455
456
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if parent <> 0 then
457 ✗ File.write(file, ",\"parent\":");
458 ✗ File.writeInt(file, parent);
459 end if;
460 42 File.write(file, ",\"section\":\"");
461 42 File.write(file, section);
462
463
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 22 times.
42 if nlSystem.tornSystem then
464 20 File.write(file, "\",\"tag\":\"tornsystem\"");
465 else
466 22 File.write(file, "\",\"tag\":\"system\"");
467 end if;
468
469 42 File.write(file, ",\"display\":\"non-linear\",\"unknowns\":" + intString(nlSystem.nUnknowns) + ",\"defines\":[");
470 42 serializeUses(file,nlSystem.crefs);
471 42 File.write(file, "],\"equation\":[[");
472 42 serializeList(file,eqs,serializeEquationIndex);
473 42 File.write(file, "],[");
474 42 serializeList(file,jeqs,serializeEquationIndex);
475 42 File.write(file, "]]");
476
477 42 File.write(file, ",\n\"internal-equations\":[");
478
479
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 if not listEmpty(eqs) then
480
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 20 times.
64 serializeEquation(file,listHead(eqs),section,withOperations,parent=nlSystem.index,first=true,assign_type=if nlSystem.tornSystem then 1 else 0);
481
2/2
✓ Branch 1 taken 140 times.
✓ Branch 2 taken 42 times.
182 for e in listRest(eqs) loop
482
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 140 times.
140 serializeEquation(file,e,section,withOperations,parent=nlSystem.index,assign_type=if nlSystem.tornSystem then 1 else 0);
483 end for;
484 end if;
485 42 File.write(file, "\n]");
486
487 42 File.write(file, ",\n\"jacobian-equations\":[");
488
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if not listEmpty(jeqs) then
489 ✗ serializeEquation(file,listHead(jeqs),section,withOperations,parent=nlSystem.index,first=true,assign_type=2);
490 ✗ for e in listRest(jeqs) loop
491 ✗ serializeEquation(file,e,section,withOperations,parent=nlSystem.index,assign_type=2);
492 end for;
493 end if;
494
495 42 File.write(file, "\n]}");
496 then true;
497
498 // dynamic tearing
499 case SimCode.SES_NONLINEAR(nlSystem = nlSystem as SimCode.NONLINEARSYSTEM(), alternativeTearing = SOME(atNL as SimCode.NONLINEARSYSTEM())) algorithm
500 // for strict tearing set
501 ✗ eqs := SimCodeCodegenUtil.sortEqSystems(nlSystem.eqs);
502 ✗ serializeEquation(file,listHead(eqs),section,withOperations,parent=nlSystem.index,first=true,assign_type=if nlSystem.tornSystem then 1 else 0);
503 ✗ for e in listRest(eqs) loop
504 ✗ serializeEquation(file,e,section,withOperations,parent=nlSystem.index,assign_type=if nlSystem.tornSystem then 1 else 0);
505 end for;
506
507 jeqs := match nlSystem.jacobianMatrix
508 ✗ case SOME(SimCode.JAC_MATRIX(columns={SimCode.JAC_COLUMN(columnEqns=jeqs,constantEqns=constantEqns)})) then SimCodeCodegenUtil.sortEqSystems(listAppend(jeqs,constantEqns));
509 else {};
510 end match;
511 ✗ if not listEmpty(jeqs) then
512 ✗ File.write(file, ",");
513 ✗ serializeEquation(file,listHead(jeqs),section,withOperations,parent=nlSystem.index,first=true,assign_type=2);
514 ✗ for e in listRest(jeqs) loop
515 ✗ serializeEquation(file,e,section,withOperations,parent=nlSystem.index,assign_type=2);
516 end for;
517 end if;
518
519 ✗ File.write(file, ",\n{\"eqIndex\":");
520 ✗ File.writeInt(file, nlSystem.index);
521 ✗ if parent <> 0 then
522 ✗ File.write(file, ",\"parent\":");
523 ✗ File.writeInt(file, parent);
524 end if;
525 ✗ File.write(file, ",\"section\":\"");
526 ✗ File.write(file, section);
527
528 ✗ if nlSystem.tornSystem then
529 ✗ File.write(file, "\",\"tag\":\"tornsystem\"");
530 else
531 ✗ File.write(file, "\",\"tag\":\"system\"");
532 end if;
533
534 ✗ File.write(file, ",\"display\":\"non-linear\",\"unknowns\":" + intString(nlSystem.nUnknowns) + ",\"defines\":[");
535 ✗ serializeUses(file,nlSystem.crefs);
536 ✗ File.write(file, "],\"equation\":[[");
537 ✗ serializeList(file,eqs,serializeEquationIndex);
538 ✗ File.write(file, "],[");
539 ✗ serializeList(file,jeqs,serializeEquationIndex);
540 ✗ File.write(file, "]]},");
541
542 // for casual tearing set
543 ✗ eqs := SimCodeCodegenUtil.sortEqSystems(atNL.eqs);
544 ✗ serializeEquation(file,listHead(eqs),section,withOperations,parent=atNL.index,first=true,assign_type=if atNL.tornSystem then 1 else 0);
545 ✗ for e in listRest(eqs) loop
546 ✗ serializeEquation(file,e,section,withOperations,parent=atNL.index,assign_type=if atNL.tornSystem then 1 else 0);
547 end for;
548
549 jeqs := match atNL.jacobianMatrix
550 ✗ case SOME(SimCode.JAC_MATRIX(columns={SimCode.JAC_COLUMN(columnEqns=jeqs,constantEqns=constantEqns)})) then SimCodeCodegenUtil.sortEqSystems(listAppend(jeqs,constantEqns));
551 else {};
552 end match;
553 ✗ if not listEmpty(jeqs) then
554 ✗ File.write(file, ",");
555 ✗ serializeEquation(file,listHead(jeqs),section,withOperations,parent=atNL.index,first=true,assign_type=2);
556 ✗ for e in listRest(jeqs) loop
557 ✗ serializeEquation(file,e,section,withOperations,parent=atNL.index,assign_type=2);
558 end for;
559 end if;
560
561 ✗ File.write(file, ",\n{\"eqIndex\":");
562 ✗ File.writeInt(file, atNL.index);
563 ✗ if parent <> 0 then
564 ✗ File.write(file, ",\"parent\":");
565 ✗ File.writeInt(file, parent);
566 end if;
567 ✗ File.write(file, ",\"section\":\"");
568 ✗ File.write(file, section);
569
570 ✗ if atNL.tornSystem then
571 ✗ File.write(file, "\",\"tag\":\"tornsystem\"");
572 else
573 ✗ File.write(file, "\",\"tag\":\"system\"");
574 end if;
575
576 ✗ File.write(file, ",\"display\":\"non-linear\",\"unknowns\":" + intString(atNL.nUnknowns) + ",\"defines\":[");
577 ✗ serializeUses(file,atNL.crefs);
578 ✗ File.write(file, "],\"equation\":[[");
579 ✗ serializeList(file,eqs,serializeEquationIndex);
580 ✗ File.write(file, "],[");
581 ✗ serializeList(file,jeqs,serializeEquationIndex);
582 ✗ File.write(file, "]]}");
583 then true;
584
585 case SimCode.SES_IFEQUATION() algorithm
586 ✗ eqs := listAppend(List.flatten(list(Util.tuple22(e) for e in eq.ifbranches)), eq.elsebranch);
587 ✗ serializeEquation(file,listHead(eqs),section,withOperations,first=true);
588 ✗ for e in listRest(eqs) loop
589 ✗ serializeEquation(file,e,section,withOperations);
590 end for;
591 ✗ File.write(file, ",\n{\"eqIndex\":");
592 ✗ File.writeInt(file, eq.index);
593 ✗ if parent <> 0 then
594 ✗ File.write(file, ",\"parent\":");
595 ✗ File.writeInt(file, parent);
596 end if;
597 ✗ File.write(file, ",\"section\":\"");
598 ✗ File.write(file, section);
599 ✗ File.write(file, "\",\"tag\":\"if-equation\",\"display\":\"if-equation\",\"equation\":[");
600 ✗ serializeList(file,eq.ifbranches,serializeIfBranch);
601 ✗ File.write(file, ",");
602 ✗ serializeIfBranch(file,(DAE.BCONST(true),eq.elsebranch));
603 ✗ File.write(file, "]}");
604 then true;
605
606 case SimCode.SES_MIXED()
607 algorithm
608 ✗ serializeEquation(file,eq.cont,section,withOperations,first=true);
609 ✗ for e in eq.discEqs loop
610 ✗ serializeEquation(file,e,section,withOperations);
611 end for;
612 ✗ File.write(file, ",\n{\"eqIndex\":");
613 ✗ File.writeInt(file, eq.index);
614 ✗ if parent <> 0 then
615 ✗ File.write(file, ",\"parent\":");
616 ✗ File.writeInt(file, parent);
617 end if;
618 ✗ File.write(file, ",\"section\":\"");
619 ✗ File.write(file, section);
620 ✗ File.write(file, "\",\"tag\":\"container\",\"display\":\"mixed\",\"defines\":[");
621 ✗ serializeUses(file,list(v.name for v in eq.discVars));
622 ✗ File.write(file, "],\"equation\":[");
623 ✗ serializeEquationIndex(file,eq.cont);
624 ✗ for e1 in eq.discEqs loop
625 ✗ File.write(file,",");
626 ✗ serializeEquationIndex(file,e1);
627 end for;
628 ✗ File.write(file, "]}");
629 then true;
630
631 case SimCode.SES_WHEN() algorithm
632 ✗ File.write(file, "\n{\"eqIndex\":");
633 ✗ File.writeInt(file, eq.index);
634 ✗ if parent <> 0 then
635 ✗ File.write(file, ",\"parent\":");
636 ✗ File.writeInt(file, parent);
637 end if;
638 ✗ File.write(file, ",\"section\":\"");
639 ✗ File.write(file, section);
640 ✗ for whenOps in eq.whenStmtLst loop
641 () := match whenOps
642 case whenOp as BackendDAE.ASSIGN() algorithm
643 ✗ File.write(file, "\",\"tag\":\"when\",\"defines\":[");
644 ✗ serializeExp(file,whenOp.left);
645 ✗ File.write(file, "],\"uses\":[");
646 ✗ serializeUses(file,List.union(eq.conditions,Expression.extractUniqueCrefsFromExpDerPreStart(whenOp.right)));
647 ✗ File.write(file, "],\"equation\":[");
648 ✗ serializeExp(file,whenOp.right);
649 ✗ File.write(file, "],\"source\":");
650 serializeSource(file,eq.source,withOperations);
651 ✗ File.write(file, "}");
652 then ();
653 case whenOp as BackendDAE.REINIT() algorithm
654 ✗ File.write(file, "\",\"tag\":\"when\",\"defines\":[");
655 ✗ serializeCref(file,whenOp.stateVar);
656 ✗ File.write(file, "],\"uses\":[");
657 ✗ serializeUses(file,List.union(eq.conditions,Expression.extractUniqueCrefsFromExpDerPreStart(whenOp.value)));
658 ✗ File.write(file, "],\"equation\":[");
659 ✗ serializeExp(file,whenOp.value);
660 ✗ File.write(file, "],\"source\":");
661 serializeSource(file,eq.source,withOperations);
662 ✗ File.write(file, "}");
663 then ();
664 case whenOp as BackendDAE.ASSERT() algorithm
665 ✗ File.write(file, "\",\"tag\":\"when\"");
666 ✗ File.write(file, ",\"uses\":[");
667 ✗ crefs := listAppend(Expression.extractUniqueCrefsFromExpDerPreStart(whenOp.condition), Expression.extractUniqueCrefsFromExpDerPreStart(whenOp.message));
668 ✗ serializeUses(file,List.union(eq.conditions,crefs));
669 ✗ File.write(file, "],\"equation\":[");
670 ✗ serializeExp(file,whenOp.message);
671 ✗ File.write(file, "],\"source\":");
672 serializeSource(file,eq.source,withOperations);
673 ✗ File.write(file, "}");
674 then ();
675 case whenOp as BackendDAE.TERMINATE() algorithm
676 ✗ File.write(file, "\",\"tag\":\"when\"");
677 ✗ File.write(file, ",\"uses\":[");
678 ✗ serializeUses(file,List.union(eq.conditions,Expression.extractUniqueCrefsFromExpDerPreStart(whenOp.message)));
679 ✗ File.write(file, "],\"equation\":[");
680 ✗ serializeExp(file,whenOp.message);
681 ✗ File.write(file, "],\"source\":");
682 serializeSource(file,eq.source,withOperations);
683 ✗ File.write(file, "}");
684 then ();
685 case whenOp as BackendDAE.NORETCALL() algorithm
686 ✗ File.write(file, "\",\"tag\":\"when\"");
687 ✗ File.write(file, ",\"uses\":[");
688 ✗ serializeUses(file,List.union(eq.conditions,Expression.extractUniqueCrefsFromExpDerPreStart(whenOp.exp)));
689 ✗ File.write(file, "],\"equation\":[");
690 ✗ serializeExp(file,whenOp.exp);
691 ✗ File.write(file, "],\"source\":");
692 serializeSource(file,eq.source,withOperations);
693 ✗ File.write(file, "}");
694 then ();
695 end match;
696 end for;
697 () := match eq.elseWhen
698 local
699 SimCode.SimEqSystem e;
700 ✗ case SOME(e) algorithm if SimCodeCodegenUtil.simEqSystemIndex(e) <>0 then serializeEquation(file,e,section,withOperations); end if; then ();
701 else ();
702 end match;
703 then true;
704
705 case SimCode.SES_FOR_LOOP() algorithm
706 ✗ File.write(file, "\n{\"eqIndex\":");
707 ✗ File.writeInt(file, eq.index);
708 ✗ if parent <> 0 then
709 ✗ File.write(file, ",\"parent\":");
710 ✗ File.writeInt(file, parent);
711 end if;
712 ✗ File.write(file, ",\"section\":\"");
713 ✗ File.write(file, section);
714 ✗ if (assign_type==1) then
715 ✗ File.write(file, "\",\"tag\":\"torn\",\"defines\":[\"");
716 elseif (assign_type==2) then
717 ✗ File.write(file, "\",\"tag\":\"jacobian\",\"defines\":[\"");
718 else
719 ✗ File.write(file, "\",\"tag\":\"assign\",\"defines\":[\"");
720 end if;
721 ✗ writeCref(file,eq.cref,escape=JSON);
722 ✗ File.write(file, "\"],\"uses\":[");
723 ✗ serializeUses(file,Expression.extractUniqueCrefsFromExpDerPreStart(eq.exp));
724 ✗ File.write(file, "],\"equation\":[\"");
725 ✗ File.writeEscape(file,expStr(eq.exp),escape=JSON);
726 ✗ File.write(file, "\"],\"source\":");
727 serializeSource(file,eq.source,withOperations);
728 ✗ File.write(file, "}");
729 then true;
730
731 case SimCode.SES_ALIAS() algorithm
732 ✗ File.write(file, "\n{\"eqIndex\":");
733 ✗ File.writeInt(file, eq.index);
734 ✗ File.write(file, ",\"tag\":\"alias\",\"equation\":[");
735 ✗ File.writeInt(file, eq.aliasOf);
736 ✗ File.write(file, "],\"section\":\"");
737 ✗ File.write(file, section);
738 ✗ File.write(file, "\"}");
739 then true;
740
741 else algorithm
742 ✗ Error.addInternalError("serializeEquation failed: " + anyString(eq), sourceInfo());
743 ✗ then fail();
744 end match;
745 end serializeEquation;
746
747 function serializeLinearCell
748 input File.File file;
749 input tuple<Integer, Integer, SimCode.SimEqSystem> cell;
750 input Boolean withOperations;
751 algorithm
752 () := match cell
753 local
754 Integer i,j;
755 SimCode.SimEqSystem eq;
756 case (i,j,eq as SimCode.SES_RESIDUAL())
757 algorithm
758 ✗ File.write(file,"{\"row\":");
759 ✗ File.write(file,intString(i));
760 ✗ File.write(file,",\"column\":");
761 ✗ File.write(file,intString(j));
762 ✗ File.write(file,",\"exp\":\"");
763 ✗ File.writeEscape(file,expStr(eq.exp),escape=JSON);
764 ✗ File.write(file,"\",\"source\":");
765 serializeSource(file,eq.source,withOperations);
766 ✗ File.write(file,"}");
767 then ();
768 else
769 algorithm
770 ✗ Error.addMessage(Error.INTERNAL_ERROR,{"SerializeTaskSystemInfo.serializeLinearCell failed. Expected only SES_RESIDUAL as input."});
771 ✗ then fail();
772 end match;
773 end serializeLinearCell;
774
775 function serializeUses
776 input File.File file;
777 input list<DAE.ComponentRef> crefs;
778 algorithm
779 () := match crefs
780 local
781 DAE.ComponentRef cr;
782 list<DAE.ComponentRef> rest;
783 case {} then ();
784 case {cr}
785 algorithm
786 1035 File.write(file, "\"");
787 1035 writeCref(file, cr, escape=JSON);
788 1035 File.write(file, "\"");
789 then ();
790 case cr::rest
791 algorithm
792 2666 File.write(file, "\"");
793 2666 writeCref(file, cr, escape=JSON);
794 2666 File.write(file, "\",");
795 2666 serializeUses(file,rest);
796 then ();
797 end match;
798 end serializeUses;
799
800 function serializeStatement
801 input File.File file;
802 input DAE.Statement stmt;
803 algorithm
804 ✗ File.write(file,"\"");
805 ✗ File.writeEscape(file, System.trim(DAEDump.ppStatementStr(stmt)), escape=JSON);
806 ✗ File.write(file,"\"");
807 end serializeStatement;
808
809 function serializeList<ArgType>
810 input File.File file;
811 input list<ArgType> lst;
812 input FuncType func;
813
814 partial function FuncType
815 input File.File file;
816 input ArgType a;
817 end FuncType;
818 algorithm
819 () := match lst
820 local
821 ArgType a;
822 list<ArgType> rest;
823 case {} then ();
824 case {a}
825 algorithm
826
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 func(file,a);
827 then ();
828 case a::rest
829 algorithm
830
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 140 times.
140 func(file,a);
831 140 File.write(file, ",");
832 140 serializeList(file,rest,func);
833 then ();
834 end match;
835 end serializeList;
836
837 function serializeList1<ArgType,Extra>
838 input File.File file;
839 input list<ArgType> lst;
840 input Extra extra;
841 input FuncType func;
842
843 partial function FuncType
844 input File.File file;
845 input ArgType a;
846 input Extra extra;
847 end FuncType;
848 algorithm
849 () := match lst
850 local
851 ArgType a;
852 list<ArgType> rest;
853 case {} then ();
854 case {a}
855 algorithm
856 ✗ func(file,a,extra);
857 then ();
858 case a::rest
859 algorithm
860 ✗ func(file,a,extra);
861 ✗ File.write(file, ",");
862 ✗ serializeList1(file,rest,extra,func);
863 then ();
864 end match;
865 end serializeList1;
866
867 function serializeExp
868 input File.File file;
869 input DAE.Exp exp;
870 algorithm
871 ✗ File.write(file, "\"");
872 ✗ File.writeEscape(file, expStr(exp), escape=JSON);
873 ✗ File.write(file, "\"");
874 end serializeExp;
875
876 function serializeCref
877 input File.File file;
878 input DAE.ComponentRef cr;
879 algorithm
880 ✗ File.write(file, "\"");
881 ✗ writeCref(file, cr, escape=JSON);
882 ✗ File.write(file, "\"");
883 end serializeCref;
884
885 function serializeString
886 input File.File file;
887 input String string;
888 algorithm
889 ✗ File.write(file, "\"");
890 ✗ File.writeEscape(file, string, escape=JSON);
891 ✗ File.write(file, "\"");
892 end serializeString;
893
894 function serializePath
895 input File.File file;
896 input Absyn.Path path;
897 protected
898 Absyn.Path p=path;
899 Boolean b=true;
900 algorithm
901 1 File.write(file, "\"");
902
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 time.
5 while b loop
903 (p,b) := match p
904 case Absyn.IDENT()
905 algorithm
906 1 File.writeEscape(file, p.name, escape=JSON);
907 then (p,false);
908 case Absyn.QUALIFIED()
909 algorithm
910 3 File.writeEscape(file, p.name, escape=JSON);
911 3 File.write(file, ".");
912 3 then (p.path,true);
913 case Absyn.FULLYQUALIFIED()
914 ✗ then (p.path,true);
915 end match;
916 end while;
917 1 File.write(file, "\"");
918 end serializePath;
919
920 function serializeEquationIndex
921 input File.File file;
922 input SimCode.SimEqSystem eq;
923 algorithm
924 182 File.writeInt(file, SimCodeCodegenUtil.simEqSystemIndex(eq));
925 end serializeEquationIndex;
926
927 function serializeIfBranch
928 input File.File file;
929 input tuple<DAE.Exp,list<SimCode.SimEqSystem>> branch;
930 protected
931 DAE.Exp exp;
932 list<SimCode.SimEqSystem> eqs;
933 algorithm
934 ✗ (exp,eqs) := branch;
935 ✗ File.write(file,"[");
936 ✗ serializeExp(file,exp);
937 ✗ File.write(file,",");
938 ✗ serializeList(file,eqs,serializeEquationIndex);
939 ✗ File.write(file,"]");
940 end serializeIfBranch;
941
942 function serializeSource
943 input File.File file;
944 input DAE.ElementSource source;
945 input Boolean withOperations;
946 algorithm
947 988 File.write(file,"{}");
948 end serializeSource;
949
950 annotation(__OpenModelica_Interface="backend_tools");
951 end SerializeTaskSystemInfo;
952