Linux GNU 11.4.0 Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 74.1% 203 / 0 / 274
Functions: -% 0 / 1 / 1
Branches: 42.0% 131 / 0 / 312

OMCompiler/Compiler/FrontEnd/FUnit.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 FUnit
37 " file: FUnit.mo
38 package: Unit
39 description: This package defines the type Unit, which represents a unit based
40 on SI base units, and some auxiliary functions therefore.
41
42 authors: Jan Hagemann and Lennart Ochel (FH Bielefeld, Germany)"
43
44 public
45 import DAE;
46
47 protected
48 import ComponentReference;
49 import Error;
50 import HashTableStringToUnit = FHashTableStringToUnit;
51 import HashTableUnitToString = FHashTableUnitToString;
52 import Util;
53
54
55 public uniontype Unit
56 record UNIT "based on SI base units"
57 Real factor "prefix";
58 Integer mol "exponent";
59 Integer cd "exponent";
60 Integer m "exponent";
61 Integer s "exponent";
62 Integer A "exponent";
63 Integer K "exponent";
64 Integer g "exponent";
65 //Real K_shift;
66 end UNIT;
67
68 record MASTER "unknown unit that belongs to all the variables from varList"
69 list<DAE.ComponentRef> varList;
70 end MASTER;
71
72 record UNKNOWN "unknown SI base unit decomposition"
73 String unit;
74 end UNKNOWN;
75 end Unit;
76
77 protected uniontype Token
78 record T_NUMBER
79 Integer number;
80 end T_NUMBER;
81
82 record T_UNIT
83 String unit;
84 end T_UNIT;
85
86 record T_MUL end T_MUL;
87 record T_DIV end T_DIV;
88 record T_LPAREN end T_LPAREN;
89 record T_RPAREN end T_RPAREN;
90 end Token;
91
92 public constant DAE.ComponentRef UPDATECREF = DAE.CREF_IDENT("jhagemann", DAE.T_REAL_DEFAULT, {});
93
94 public constant list<tuple<String, Unit>> LU_COMPLEXUNITS = {
95 /* fac,mol,cd, m, s, A, K, g*/
96 ("mol", UNIT(1e0, 1, 0, 0, 0, 0, 0, 0)), //Mol
97 ("cd", UNIT(1e0, 0, 1, 0, 0, 0, 0, 0)), //Candela
98 ("m", UNIT(1e0, 0, 0, 1, 0, 0, 0, 0)), //Meter
99 ("s", UNIT(1e0, 0, 0, 0, 1, 0, 0, 0)), //Sekunde
100 ("A", UNIT(1e0, 0, 0, 0, 0, 1, 0, 0)), //Ampere
101 ("K", UNIT(1e0, 0, 0, 0, 0, 0, 1, 0)), //Kelvin
102 ("g", UNIT(1e0, 0, 0, 0, 0, 0, 0, 1)), //Gramm
103 ("V", UNIT(1e3, 0, 0, 2,-3,-1, 0, 1)), //Volt
104 ("W", UNIT(1e3, 0, 0, 2,-3, 0, 0, 1)), //Watt
105 //("VA", UNIT(1e3, 0, 0, 2,-3, 0, 0, 1)), //Voltampere=Watt
106 //("var", UNIT(1e3, 0, 0, 2,-3, 0, 0, 1)), //Var=Watt
107 ("Hz", UNIT(1e0, 0, 0, 0,-1, 0, 0, 0)), //Hertz
108 ("Ohm", UNIT(1e3, 0, 0, 2,-3,-2, 0, 1)), //Ohm
109 ("F", UNIT(1e-3, 0, 0,-2, 4, 2, 0,-1)), //Farad
110 ("H", UNIT(1e3, 0, 0, 2,-2,-2, 0, 1)), //Henry
111 ("C", UNIT(1e0, 0, 0, 0, 1, 1, 0, 0)), //Coulomb
112 ("T", UNIT(1e3, 0, 0, 0,-2,-1, 0, 1)), //Tesla
113 ("S", UNIT(1e-3, 0, 0,-2, 3, 2, 0,-1)), //Siemens
114 ("Wb", UNIT(1e3, 0, 0, 2,-2,-1, 0, 1)), //Weber
115 //("lm", UNIT(1e0, 0, 1, 0, 0, 0, 0, 0)), //Lumen=Candela
116 //("lx", UNIT(1e0, 0, 1,-2, 0, 0, 0, 0)), //Lux=lm/m^2
117 ("N", UNIT(1e3, 0, 0, 1,-2, 0, 0, 1)), //Newton
118 ("Pa", UNIT(1e3, 0, 0,-1,-2, 0, 0, 1)), //Pascal; displayUnit ="bar"
119 ("J", UNIT(1e3, 0, 0, 2,-2, 0, 0, 1)), //Joule=N*m
120 ("min", UNIT(6e1, 0, 0, 0, 1, 0, 0, 0)), //Minute
121 ("h", UNIT(3.6e3, 0, 0, 0, 1, 0, 0, 0)), //Stunde
122 ("d", UNIT(8.64e4, 0, 0, 0, 1, 0, 0, 0)), //Tag
123 ("l", UNIT(1e-3, 0, 0, 3, 0, 0, 0, 0)), //Liter
124 ("kg", UNIT(1e3, 0, 0, 0, 0, 0, 0, 1)), //Kilogramm
125 //("Bq", UNIT(1e0, 0, 0, 0,-1, 0, 0, 0)), //Becquerel = Hertz
126 //("Gy", UNIT(1e0, 0, 0, 2,-2, 0, 0, 1)), //Gray
127 //("Sv", UNIT(1e0, 0, 0, 2,-2, 0, 0, 1)), //Sievert=Gray
128 //("eV", UNIT(1.60218e-16, 0, 0, 2,-2, 0, 0, 1)), //Elektronenvolt 1, 602...*10^-19 kg*m^2/s^2
129 //("R", UNIT(2.58e-7, 0, 0, 0, 1, 1, 0,-1)), //Röntgen 2, 58*10^-4 C/kg
130 ("kat", UNIT(1e0, 1, 0, 0,-1, 0, 0, 0)), //Katal
131 ("1", UNIT(1e0, 0, 0, 0, 0, 0, 0, 0)), //1
132 ("rad", UNIT(1e0, 0, 0, 0, 0, 0, 0, 0)), //rad; displayUnit ="deg"
133 //("B", UNIT(1e-2, 0, 0, 0, 0, 0, 0, 0)), //Bel (dezibel dB)
134 //("phon", UNIT(1e0, 0, 0, 0, 0, 0, 0, 0)), //Phon
135 //("sone", UNIT(1e0, 0, 0, 0, 0, 0, 0, 0)), //Sone
136 //("sr", UNIT(1e0, 0, 0, 0, 0, 0, 0, 0)), //Steradiant=m^2/m^2
137 ("degC", UNIT(1e0, 0, 0, 0, 0, 0, 1, 0)), //°Celsius
138 ("degF", UNIT(0.55555555555555555555555555555555555555, 0, 0, 0, 0, 0, 1, 0))};//°Fahrenheit
139 //("degF", UNIT(5.0 / 9.0, 0, 0, 0, 0, 0, 1, 0, 459.67)), //°Fahrenheit
140 //("degC", UNIT(1e0, 0, 0, 0, 0, 0, 1, 0, 273.15))};//°Celsius
141 /* fac, mol, cd, m, s, A, K, g*/
142
143 public function getKnownUnits
144 output HashTableStringToUnit.HashTable outKnownUnits;
145 algorithm
146 12 outKnownUnits := HashTableStringToUnit.emptyHashTableSized(Util.nextPrime(4 * listLength(LU_COMPLEXUNITS)));
147
148
2/2
✓ Branch 0 taken 360 times.
✓ Branch 1 taken 12 times.
372 for unit in LU_COMPLEXUNITS loop
149 360 outKnownUnits := BaseHashTable.add(unit, outKnownUnits);
150 end for;
151 end getKnownUnits;
152
153 public function getKnownUnitsInverse
154 output HashTableUnitToString.HashTable outKnownUnitsInverse;
155 protected
156 String s;
157 Unit ut;
158 algorithm
159 10 outKnownUnitsInverse := HashTableUnitToString.emptyHashTableSized(Util.nextPrime(4 * listLength(LU_COMPLEXUNITS)));
160
161
2/2
✓ Branch 0 taken 300 times.
✓ Branch 1 taken 10 times.
310 for unit in LU_COMPLEXUNITS loop
162 300 (s, ut) := unit;
163
164
2/2
✓ Branch 1 taken 280 times.
✓ Branch 2 taken 20 times.
300 if not BaseHashTable.hasKey(ut, outKnownUnitsInverse) then
165 280 outKnownUnitsInverse := BaseHashTable.add((ut, s), outKnownUnitsInverse);
166 end if;
167 end for;
168 end getKnownUnitsInverse;
169
170 public function isUnit
171 input Unit inUnit;
172 output Boolean b;
173 algorithm
174 b := match inUnit
175 case UNIT() then true;
176 else false;
177 end match;
178 end isUnit;
179
180 public function hashUnit
181 input Unit inKey;
182 output Integer outHash;
183 protected
184 String str;
185 algorithm
186 749 str := unit2string(inKey);
187 749 outHash := stringHashDjb2(str);
188 end hashUnit;
189
190 public function unitEqual
191 input Unit inKey;
192 input Unit inKey2;
193 output Boolean res;
194 algorithm
195 res := matchcontinue(inKey, inKey2)
196 local
197 Real factor1, factor2, r;
198 Integer i1, i2, i3, i4, i5, i6, i7;
199 Integer j1, j2, j3, j4, j5, j6, j7;
200 String s, s2;
201
202 case (UNIT(factor1, i1, i2, i3, i4, i5, i6, i7), UNIT(factor2, j1, j2, j3, j4, j5, j6, j7)) algorithm
203
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 190 times.
246 true := realEq(factor1, factor2);
204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 190 times.
190 true := intEq(i1, j1);
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 190 times.
190 true := intEq(i2, j2);
206
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 150 times.
190 true := intEq(i3, j3);
207
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 121 times.
150 true := intEq(i4, j4);
208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 121 times.
121 true := intEq(i5, j5);
209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 121 times.
121 true := intEq(i6, j6);
210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 121 times.
121 true := intEq(i7, j7);
211 then true;
212
213 case (UNIT(factor1, i1, i2, i3, i4, i5, i6, i7), UNIT(factor2, j1, j2, j3, j4, j5, j6, j7)) algorithm
214 125 r := realMax(realAbs(factor1), realAbs(factor2));
215
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
✓ Branch 2 taken 56 times.
✓ Branch 3 taken 69 times.
125 true := realLe(realDiv(realAbs(realSub(factor1,factor2)),r),1e-3);
216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 69 times.
69 true := intEq(i1, j1);
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 69 times.
69 true := intEq(i2, j2);
218
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 29 times.
69 true := intEq(i3, j3);
219
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
29 true := intEq(i4, j4);
220 ✗ true := intEq(i5, j5);
221 ✗ true := intEq(i6, j6);
222 ✗ true := intEq(i7, j7);
223 then true;
224
225 case (MASTER(), MASTER()) //equation
226 // lcr comparison????
227 then true;
228
229 case (UNKNOWN(s), UNKNOWN(s2)) algorithm
230 ✗ true := stringEqual(s, s2);
231 then true;
232
233 else false;
234 end matchcontinue;
235 end unitEqual;
236
237 public function unit2string
238 input Unit inUnit;
239 output String outString;
240 algorithm
241 outString := match inUnit
242 local
243 String s, str;
244 Boolean b;
245 list<DAE.ComponentRef> crefList;
246 Real factor1;
247 Integer i1, i2, i3, i4, i5, i6, i7;
248
249 case UNIT(factor1, i1, i2, i3, i4, i5, i6, i7/* , shift1 */) algorithm
250 766 str := realString(factor1) + " * ";
251
252 b := false;
253 766 s := "mol^(" + intString(i1) + ")";
254
2/2
✓ Branch 0 taken 698 times.
✓ Branch 1 taken 68 times.
766 s := if intEq(i1, 0) then "" else s;
255 766 b := b or intNe(i1, 0);
256 766 str := str + s;
257
258
1/2
✓ Branch 0 taken 766 times.
✗ Branch 1 not taken.
766 s := if b and intNe(i2, 0) then " * " else "";
259 766 str := str + s;
260 766 s := "cd^(" + intString(i2) + ")";
261
2/2
✓ Branch 0 taken 746 times.
✓ Branch 1 taken 20 times.
766 s := if intEq(i2, 0) then "" else s;
262 766 b := b or intNe(i2, 0);
263 766 str := str + s;
264
265
1/2
✓ Branch 0 taken 766 times.
✗ Branch 1 not taken.
766 s := if b and intNe(i3, 0) then " * " else "";
266 766 str := str + s;
267 766 s := "m^(" + intString(i3) + ")";
268
2/2
✓ Branch 0 taken 384 times.
✓ Branch 1 taken 382 times.
766 s := if intEq(i3, 0) then "" else s;
269 766 b := b or intNe(i3, 0);
270 766 str := str + s;
271
272
2/2
✓ Branch 0 taken 431 times.
✓ Branch 1 taken 335 times.
766 s := if b and intNe(i4, 0) then " * " else "";
273 766 str := str + s;
274 766 s := "s^(" + intString(i4) + ")";
275
2/2
✓ Branch 0 taken 279 times.
✓ Branch 1 taken 487 times.
766 s := if intEq(i4, 0) then "" else s;
276 766 b := b or intNe(i4, 0);
277 766 str := str + s;
278
279
2/2
✓ Branch 0 taken 606 times.
✓ Branch 1 taken 160 times.
766 s := if b and intNe(i5, 0) then " * " else "";
280 766 str := str + s;
281 766 s := "A^(" + intString(i5) + ")";
282
2/2
✓ Branch 0 taken 586 times.
✓ Branch 1 taken 180 times.
766 s := if intEq(i5, 0) then "" else s;
283 766 b := b or intNe(i5, 0);
284 766 str := str + s;
285
286
1/2
✓ Branch 0 taken 766 times.
✗ Branch 1 not taken.
766 s := if b and intNe(i6, 0) then " * " else "";
287 766 str := str + s;
288 //s = "(K-" + realString(shift1) + ")^(" + intString(i6) + ")";
289 766 s := "K^(" + intString(i6) + ")";
290
2/2
✓ Branch 0 taken 716 times.
✓ Branch 1 taken 50 times.
766 s := if intEq(i6, 0) then "" else s;
291 766 b := b or intNe(i6, 0);
292 766 str := str + s;
293
294
2/2
✓ Branch 0 taken 546 times.
✓ Branch 1 taken 220 times.
766 s := if b and intNe(i7, 0) then " * " else "";
295 766 str := str + s;
296 766 s := "g^(" + intString(i7) + ")";
297
2/2
✓ Branch 0 taken 506 times.
✓ Branch 1 taken 260 times.
766 s := if intEq(i7, 0) then "" else s;
298 b := b or intNe(i7, 0);
299 766 str := str + s;
300
301
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 732 times.
766 s := if b then "" else "1";
302 766 str := str + s;
303 then str;
304
305 case MASTER(crefList) algorithm
306 str := "MASTER(";
307 32 str := str + printListCr(crefList);
308 32 str := str + ")";
309 then str;
310
311 case UNKNOWN(s) algorithm
312 ✗ str := "UNKOWN(" + s + ")";
313 then str;
314 end match;
315 end unit2string;
316
317 public function printListCr
318 input list<DAE.ComponentRef> inlCr;
319 output String outS;
320 algorithm
321 outS := match inlCr
322
323 local
324 list<DAE.ComponentRef> lCr;
325 DAE.ComponentRef cr;
326 String s;
327
328 case {} then "";
329
330 case cr::{} algorithm
331 26 s := ComponentReference.crefStr(cr);
332 then s;
333
334 case cr::lCr algorithm
335 ✗ s := ComponentReference.crefStr(cr);
336 ✗ s := s + ", " + printListCr(lCr);
337 then s;
338
339 end match;
340 end printListCr;
341
342 public function unitMul
343 input Unit inUnit1;
344 input Unit inUnit2;
345 output Unit outUnit;
346 protected
347 Real factor1, factor2;
348 Integer i1, i2, i3, i4, i5, i6, i7;
349 Integer j1, j2, j3, j4, j5, j6, j7;
350 algorithm
351
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 20 times.
20 UNIT(factor1, i1, i2, i3, i4, i5, i6, i7) := inUnit1;
352
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 UNIT(factor2, j1, j2, j3, j4, j5, j6, j7) := inUnit2;
353 20 factor1 := factor1 * factor2;
354 20 i1 := i1+j1;
355 20 i2 := i2+j2;
356 20 i3 := i3+j3;
357 20 i4 := i4+j4;
358 20 i5 := i5+j5;
359 20 i6 := i6+j6;
360 20 i7 := i7+j7;
361 20 outUnit := UNIT(factor1, i1, i2, i3, i4, i5, i6, i7);
362 end unitMul;
363
364 public function unitDiv
365 input Unit inUnit1;
366 input Unit inUnit2;
367 output Unit outUnit;
368 protected
369 Real factor1, factor2;
370 Integer i1, i2, i3, i4, i5, i6, i7;
371 Integer j1, j2, j3, j4, j5, j6, j7;
372 algorithm
373
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 23 times.
23 UNIT(factor1, i1, i2, i3, i4, i5, i6, i7) := inUnit1;
374
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 23 times.
23 UNIT(factor2, j1, j2, j3, j4, j5, j6, j7) := inUnit2;
375
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 factor1 := factor1 / factor2;
376 23 i1 := i1-j1;
377 23 i2 := i2-j2;
378 23 i3 := i3-j3;
379 23 i4 := i4-j4;
380 23 i5 := i5-j5;
381 23 i6 := i6-j6;
382 23 i7 := i7-j7;
383 23 outUnit := UNIT(factor1, i1, i2, i3, i4, i5, i6, i7);
384 end unitDiv;
385
386 public function unitPow
387 input Unit inUnit;
388 input Integer inExp "exponent";
389 output Unit outUnit;
390 protected
391 Real factor;
392 Integer i1, i2, i3, i4, i5, i6, i7;
393 algorithm
394
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
6 UNIT(factor, i1, i2, i3, i4, i5, i6, i7) := inUnit;
395
3/22
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✓ Branch 19 taken 6 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✓ Branch 22 taken 6 times.
6 factor := realPow(factor, intReal(inExp));
396 6 i1 := i1*inExp;
397 6 i2 := i2*inExp;
398 6 i3 := i3*inExp;
399 6 i4 := i4*inExp;
400 6 i5 := i5*inExp;
401 6 i6 := i6*inExp;
402 6 i7 := i7*inExp;
403 6 outUnit := UNIT(factor, i1, i2, i3, i4, i5, i6, i7);
404 end unitPow;
405
406 public function unitMulReal
407 input Unit inUnit;
408 input Real inFactor;
409 output Unit outUnit;
410 algorithm
411 outUnit := match inUnit
412 local
413 Unit unit;
414
415 case unit as UNIT() algorithm
416 2 unit.factor := unit.factor * inFactor;
417 then unit;
418
419 else fail();
420 end match;
421 end unitMulReal;
422
423 public function unitRoot
424 input Unit inUnit;
425 input Real inExponent;
426 output Unit outUnit;
427 protected
428 Real r, factor;
429 Integer i, i1, i2, i3, i4, i5, i6, i7;
430 algorithm
431 1 i := realInt(inExponent);
432
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 r := realDiv(1.0, inExponent);
433
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
1 UNIT(factor, i1, i2, i3, i4, i5, i6, i7) := inUnit;
434
3/22
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✓ Branch 19 taken 1 time.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✓ Branch 22 taken 1 time.
1 factor := realPow(factor, r);
435
436 1 r := realDiv(intReal(i1),inExponent);
437
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 i1 := intDiv(i1, i);
438
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 true := realEq(r, intReal(i1));
439
440 1 r := realDiv(intReal(i2),inExponent);
441 1 i2 := intDiv(i2, i);
442
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 true := realEq(r, intReal(i2));
443
444 1 r := realDiv(intReal(i3),inExponent);
445 1 i3 := intDiv(i3, i);
446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 true := realEq(r, intReal(i3));
447
448 1 r := realDiv(intReal(i4),inExponent);
449 1 i4 := intDiv(i4, i);
450
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 true := realEq(r, intReal(i4));
451
452 1 r := realDiv(intReal(i5),inExponent);
453 1 i5 := intDiv(i5, i);
454
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 true := realEq(r, intReal(i5));
455
456 1 r := realDiv(intReal(i6),inExponent);
457 1 i6 := intDiv(i6, i);
458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 true := realEq(r, intReal(i6));
459
460 1 r := realDiv(intReal(i7),inExponent);
461 1 i7 := intDiv(i7, i);
462
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 true := realEq(r, intReal(i7));
463
464 1 outUnit := UNIT(factor, i1, i2, i3, i4, i5, i6, i7);
465 end unitRoot;
466
467 public function unitString "Unit to Modelica unit string"
468 input Unit inUnit;
469 input HashTableUnitToString.HashTable inHtU2S = getKnownUnitsInverse();
470 output String outString;
471 algorithm
472 outString := match inUnit
473 local
474 String s, s1, s2, s3, s4, s5, s6, s7, sExponent;
475 Boolean b;
476 Unit unit;
477
478 case _ guard BaseHashTable.hasKey(inUnit, inHtU2S) algorithm
479 43 s := BaseHashTable.get(inUnit, inHtU2S);
480 then s;
481
482 case unit as UNIT() algorithm
483 6 s := prefix2String(unit.factor);
484
485
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 s := if realEq(unit.factor, 1.0) then "" else s;
486 b := false;
487
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 sExponent := if intEq(unit.mol, 1) then "" else intString(unit.mol);
488 6 s1 := "mol" + sExponent;
489
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 s1 := if intEq(unit.mol, 0) then "" else s1;
490
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 b := b or intNe(unit.mol, 0);
491
492
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 s2 := if b and intNe(unit.cd, 0) then "." else "";
493
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 sExponent := if intEq(unit.cd, 1) then "" else intString(unit.cd);
494 6 s2 := s2 + "cd" + sExponent;
495
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 s2 := if intEq(unit.cd, 0) then "" else s2;
496
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
6 b := b or intNe(unit.cd, 0);
497
498
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 s3 := if b and intNe(unit.m, 0) then "." else "";
499
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 sExponent := if intEq(unit.m, 1) then "" else intString(unit.m);
500 6 s3 := s3 + "m" + sExponent;
501
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 s3 := if intEq(unit.m, 0) then "" else s3;
502
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
6 b := b or intNe(unit.m, 0);
503
504
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 5 times.
6 s4 := if b and intNe(unit.s, 0) then "." else "";
505
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 time.
6 sExponent := if intEq(unit.s, 1) then "" else intString(unit.s);
506 6 s4 := s4 + "s" + sExponent;
507
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 5 times.
6 s4 := if intEq(unit.s, 0) then "" else s4;
508
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6 b := b or intNe(unit.s, 0);
509
510
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 s5 := if b and intNe(unit.A, 0) then "." else "";
511
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 sExponent := if intEq(unit.A, 1) then "" else intString(unit.A);
512 6 s5 := s5 + "A" + sExponent;
513
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 s5 := if intEq(unit.A, 0) then "" else s5;
514
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6 b := b or intNe(unit.A, 0);
515
516
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 s6 := if b and intNe(unit.K, 0) then "." else "";
517
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 sExponent := if intEq(unit.K, 1) then "" else intString(unit.K);
518 6 s6 := s6 + "K" + sExponent;
519
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 s6 := if intEq(unit.K, 0) then "" else s6;
520
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6 b := b or intNe(unit.K, 0);
521
522
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 s7 := if b and intNe(unit.g, 0) then "." else "";
523
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 sExponent := if intEq(unit.g, 1) then "" else intString(unit.g);
524 6 s7 := s7 + "g" + sExponent;
525
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 s7 := if intEq(unit.g, 0) then "" else s7;
526
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6 b := b or intNe(unit.g, 0);
527
528 6 s := if b then s + s1 + s2 + s3 + s4 + s5 + s6 + s7 else "1";
529 then s;
530
531 else algorithm
532 ✗ Error.addCompilerWarning("function Unit.unitString failed for \"" + unit2string(inUnit) +"\".");
533 ✗ then fail();
534 end match;
535 end unitString;
536
537 protected function prefix2String
538 input Real inReal;
539 output String outPrefix;
540 algorithm
541 outPrefix := match inReal
542 case 1e-24 then "y";
543 case 1e-21 then "z";
544 case 1e-18 then "a";
545 case 1e-15 then "f";
546 case 1e-12 then "p";
547 case 1e-6 then "u";
548 case 1e-3 then "m";
549 case 1e-2 then "c";
550 case 1e-1 then "d";
551 case 1e1 then "da";
552 case 1e2 then "h";
553 case 1e3 then "k";
554 case 1e6 then "M";
555 case 1e9 then "G";
556 case 1e12 then "T";
557 case 1e15 then "P";
558 case 1e18 then "E";
559 case 1e21 then "Z";
560 case 1e24 then "Y";
561 6 else realString(inReal);
562 end match;
563 end prefix2String;
564
565 public function parseUnitString "author: lochel
566 The second argument is optional."
567 input String inUnitString;
568 input HashTableStringToUnit.HashTable inKnownUnits = getKnownUnits();
569 output Unit outUnit;
570 protected
571 list<String> charList;
572 list<Token> tokenList;
573 algorithm
574 15 charList := stringListStringChar(inUnitString);
575
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if listEmpty(charList) then
576 ✗ fail();
577 end if;
578 15 tokenList := lexer(charList);
579 15 outUnit := parser3({true, true}, tokenList, UNIT(1e0, 0, 0, 0, 0, 0, 0, 0), inKnownUnits);
580
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if not isUnit(outUnit) then
581 ✗ fail();
582 end if;
583 end parseUnitString;
584
585 protected function parser3
586 input list<Boolean> inMul "true=Mul, false=Div, initial call with true";
587 input list<Token> inTokenList "Tokenliste";
588 input Unit inUnit "initial call with UNIT(1e0, 0, 0, 0, 0, 0, 0, 0)";
589 input HashTableStringToUnit.HashTable inHtS2U;
590 output Unit outUnit;
591 algorithm
592 outUnit := matchcontinue(inMul, inTokenList)
593 local
594 String s, unit;
595 list<Token> tokens;
596 Unit ut;
597 Integer exponent;
598 Boolean bMul, b;
599 list<Boolean> bRest;
600
601 // ""
602 case ({true}, {}) then inUnit;
603
604 // "1"
605 case (bMul::bRest, T_NUMBER(number=1)::tokens) algorithm
606 ut := UNIT(1e0, 0, 0, 0, 0, 0, 0, 0/* , 0e0 */);
607 ✗ ut := if bMul then unitMul(inUnit,ut) else unitDiv(inUnit, ut);
608 ✗ ut := parser3(bRest, tokens, ut, inHtS2U);
609 then ut;
610
611 // "unit^i"
612 case (bMul::bRest, T_UNIT(unit=s)::T_NUMBER(exponent)::tokens) algorithm
613 5 ut := unitToken2unit(s, inHtS2U);
614 5 ut := unitPow(ut, exponent);
615
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
5 ut := if bMul then unitMul(inUnit,ut) else unitDiv(inUnit, ut);
616 5 ut := parser3(bRest, tokens, ut, inHtS2U);
617 then ut;
618
619 // "unit"
620 case (bMul::bRest, T_UNIT(unit=s)::tokens) algorithm
621 22 ut := unitToken2unit(s, inHtS2U);
622
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 8 times.
22 ut := if bMul then unitMul(inUnit,ut) else unitDiv(inUnit, ut);
623 22 ut := parser3(bRest, tokens, ut, inHtS2U);
624 then ut;
625
626 // "*("
627 case (bMul::_, T_MUL()::T_LPAREN()::tokens) algorithm
628 ✗ ut := parser3(bMul::bMul::inMul, tokens, inUnit, inHtS2U);
629 then ut;
630
631 // "/("
632 case (bMul::_, T_DIV()::T_LPAREN()::tokens) algorithm
633 b := not bMul;
634 ✗ ut := parser3(b::b::inMul, tokens, inUnit, inHtS2U);
635 then ut;
636
637 // ")"
638 case (_::bRest, T_RPAREN()::tokens) algorithm
639 ✗ ut := parser3(bRest, tokens, inUnit, inHtS2U);
640 then ut;
641
642 // "*"
643 case (bMul::_, T_MUL()::tokens) algorithm
644 ✗ ut := parser3(bMul::inMul, tokens, inUnit, inHtS2U);
645 then ut;
646
647 // "/"
648 case (bMul::_, T_DIV()::tokens) algorithm
649 b := not bMul;
650
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
24 ut := parser3(b::inMul, tokens, inUnit, inHtS2U);
651 then ut;
652
653 else fail();
654 end matchcontinue;
655 end parser3;
656
657 protected function unitToken2unit
658 input String inS;
659 input HashTableStringToUnit.HashTable inHtS2U;
660 output Unit outUnit;
661 algorithm
662 outUnit := matchcontinue inHtS2U
663 local
664 String s;
665 Real r;
666 Unit ut;
667
668 case _ algorithm
669 29 ut:=BaseHashTable.get(inS, inHtS2U);
670 then ut;
671
672 else algorithm
673 2 s := stringGetStringChar(inS, 1);
674 2 (r, s) := getPrefix(s, inS);
675 2 ut := unitToken2unit(s, inHtS2U);
676 2 ut := unitMulReal(ut, r);
677 then ut;
678 end matchcontinue;
679 end unitToken2unit;
680
681 protected function getPrefix
682 input String inS;
683 input String inS2;
684 output Real outR;
685 output String outUnit;
686 algorithm
687 (outR, outUnit) := matchcontinue inS
688 local
689 list<String> strRest;
690 String s;
691
692 case "y" //-24
693 algorithm
694 ✗ _::strRest := stringListStringChar(inS2);
695 ✗ s := stringCharListString(strRest);
696 then (1e-24, s);
697
698 case "z" //-21
699 algorithm
700 ✗ _::strRest := stringListStringChar(inS2);
701 ✗ s := stringCharListString(strRest);
702 then (1e-21, s);
703
704 case "a" //-18
705 algorithm
706 ✗ _::strRest := stringListStringChar(inS2);
707 ✗ s := stringCharListString(strRest);
708 then (1e-18, s);
709
710 case "f" //-15
711 algorithm
712 ✗ _::strRest := stringListStringChar(inS2);
713 ✗ s := stringCharListString(strRest);
714 then (1e-15, s);
715
716 case "p" //-12
717 algorithm
718 ✗ _::strRest := stringListStringChar(inS2);
719 ✗ s := stringCharListString(strRest);
720 then (1e-12, s);
721
722 case "u" //-6
723 algorithm
724 ✗ _::strRest := stringListStringChar(inS2);
725 ✗ s := stringCharListString(strRest);
726 then (1e-6, s);
727
728 case "m" //-3
729 algorithm
730
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 _::strRest := stringListStringChar(inS2);
731 2 s := stringCharListString(strRest);
732 then (1e-3, s);
733
734 case "c" //-2
735 algorithm
736 ✗ _::strRest := stringListStringChar(inS2);
737 ✗ s := stringCharListString(strRest);
738 then (1e-2, s);
739
740 case "d" //+1
741 algorithm
742 ✗ strRest := stringListStringChar(inS2);
743 ✗ "d"::"a"::strRest := strRest;
744 ✗ s := stringCharListString(strRest);
745 then (1e1, s);
746
747 case "d" //-1
748 algorithm
749 ✗ _::strRest := stringListStringChar(inS2);
750 ✗ s := stringCharListString(strRest);
751 then (1e-1, s);
752
753 case "h" //+2
754 algorithm
755 ✗ _::strRest := stringListStringChar(inS2);
756 ✗ s := stringCharListString(strRest);
757 then (1e2, s);
758
759 case "k" //+3
760 algorithm
761 ✗ _::strRest := stringListStringChar(inS2);
762 ✗ s := stringCharListString(strRest);
763 then (1e3, s);
764
765 case "M" //+6
766 algorithm
767 ✗ _::strRest := stringListStringChar(inS2);
768 ✗ s := stringCharListString(strRest);
769 then (1e6, s);
770
771 case "G" //+9
772 algorithm
773 ✗ _::strRest := stringListStringChar(inS2);
774 ✗ s := stringCharListString(strRest);
775 then (1e9, s);
776
777 case "T" //+12
778 algorithm
779 ✗ _::strRest := stringListStringChar(inS2);
780 ✗ s := stringCharListString(strRest);
781 then (1e12, s);
782
783 case "P" //+15
784 algorithm
785 ✗ _::strRest := stringListStringChar(inS2);
786 ✗ s := stringCharListString(strRest);
787 then (1e15, s);
788
789 case "E" //+18
790 algorithm
791 ✗ _::strRest := stringListStringChar(inS2);
792 ✗ s := stringCharListString(strRest);
793 then (1e18, s);
794
795 case "Z" //+21
796 algorithm
797 ✗ _::strRest := stringListStringChar(inS2);
798 ✗ s := stringCharListString(strRest);
799 then (1e21, s);
800
801 case "Y" //+24
802 algorithm
803 ✗ _::strRest := stringListStringChar(inS2);
804 ✗ s := stringCharListString(strRest);
805 then (1e24, s);
806
807 else fail();
808 end matchcontinue;
809 end getPrefix;
810
811 protected function lexer "author: lochel
812 Tokenizer: charList to tokenList"
813 input list<String> inCharList;
814 output list<Token> outTokenList;
815 algorithm
816 outTokenList := matchcontinue inCharList
817 local
818 list<String> charList;
819 String number;
820 String unit;
821 list<Token> tokenList;
822 Integer i;
823
824 case {} then {};
825
826 case "."::charList algorithm
827 ✗ tokenList := lexer(charList);
828 then T_MUL()::tokenList;
829
830 case "("::charList algorithm
831 ✗ tokenList := lexer(charList);
832 then T_LPAREN()::tokenList;
833
834 case ")"::charList algorithm
835 ✗ tokenList := lexer(charList);
836 then T_RPAREN()::tokenList;
837
838 case "/"::charList algorithm
839 12 tokenList := lexer(charList);
840 then T_DIV()::tokenList;
841
842 case "+"::charList algorithm
843 ✗ (charList, number) := popNumber(charList);
844 ✗ false := (number == "");
845 ✗ tokenList := lexer(charList);
846 ✗ i := stringInt(number);
847 ✗ then T_NUMBER(i)::tokenList;
848
849 case "-"::charList algorithm
850 ✗ (charList, number) := popNumber(charList);
851 ✗ false := (number == "");
852 ✗ tokenList := lexer(charList);
853 ✗ i := -stringInt(number);
854 ✗ then T_NUMBER(i)::tokenList;
855
856 case charList algorithm
857 32 (charList, number) := popNumber(charList);
858
3/4
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 27 times.
32 false := (number == "");
859 5 tokenList := lexer(charList);
860 5 i := stringInt(number);
861 5 then T_NUMBER(i)::tokenList;
862
863 case charList algorithm
864 27 (charList, unit) := popUnit(charList);
865
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
27 false := (unit == "");
866 27 tokenList := lexer(charList);
867 27 then T_UNIT(unit)::tokenList;
868
869 else algorithm
870 ✗ Error.addInternalError("function lexer failed", sourceInfo());
871 ✗ then fail();
872 end matchcontinue;
873 end lexer;
874
875 protected function popUnit
876 input list<String> inCharList;
877 output list<String> outCharList;
878 output String outUnit;
879 algorithm
880 (outCharList, outUnit) := match inCharList
881 local
882 String s1, s2;
883 list<String> strRest;
884
885 case {}
886 then ({}, "");
887
888 case s1::strRest guard (stringCompare(s1, "a") >= 0) and (stringCompare(s1, "z") <= 0) algorithm
889 35 (strRest, s2) := popUnit(strRest);
890 35 then (strRest, s1 + s2);
891
892 case s1::strRest guard (stringCompare(s1, "A") >= 0) and (stringCompare(s1, "Z") <= 0) algorithm
893 ✗ (strRest, s2) := popUnit(strRest);
894 ✗ then (strRest, s1 + s2);
895
896 else (inCharList, "");
897 end match;
898 end popUnit;
899
900 protected function popNumber
901 input list<String> inCharList;
902 output list<String> outCharList;
903 output String outNumber;
904 algorithm
905 (outCharList, outNumber) := matchcontinue inCharList
906 local
907 String s1, s2;
908 list<String> strRest;
909 Integer i;
910
911 case {}
912 then ({}, "");
913
914 case s1::strRest algorithm
915 32 i := stringInt(s1);
916
2/4
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 5 times.
✗ Branch 6 not taken.
5 true := (intString(i) == s1);
917 5 (strRest, s2) := popNumber(strRest);
918 5 then (strRest, s1 + s2);
919
920 else (inCharList, "");
921 end matchcontinue;
922 end popNumber;
923
924 annotation(__OpenModelica_Interface="frontend");
925 end FUnit;
926