/
Unit.mo
291 lines (250 loc) · 9.42 KB
/
Unit.mo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2014, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
encapsulated package Unit
" file: Unit.mo
package: Unit
description:
authors: Jan Hagemann and Lennart Ochel (FH Bielefeld, Germany)
RCS: $Id$"
public import DAE;
protected import ComponentReference;
protected import Util;
public import System;
public uniontype Unit
record UNIT
Real factor "prefix";
Integer mol "exponent";
Integer cd "exponent";
Integer m "exponent";
Integer s "exponent";
Integer A "exponent";
Integer K "exponent";
Integer g "exponent";
//Real K_shift;
end UNIT;
record MASTER "unknown unit that belongs to all the variables from varList"
list<DAE.ComponentRef> varList;
end MASTER;
record UNKNOWN "unknown unit"
String unit;
end UNKNOWN;
end Unit;
public constant DAE.ComponentRef UPDATECREF = DAE.CREF_IDENT("jhagemann", DAE.T_REAL_DEFAULT, {});
public constant list<tuple<String, Unit>> LU_COMPLEXUNITS = {
/* fac,mol,cd, m, s, A, K, g*/
("mol", UNIT(1e0, 1, 0, 0, 0, 0, 0, 0)), //Mol
("cd", UNIT(1e0, 0, 1, 0, 0, 0, 0, 0)), //Candela
("m", UNIT(1e0, 0, 0, 1, 0, 0, 0, 0)), //Meter
("s", UNIT(1e0, 0, 0, 0, 1, 0, 0, 0)), //Sekunde
("A", UNIT(1e0, 0, 0, 0, 0, 1, 0, 0)), //Ampere
("K", UNIT(1e0, 0, 0, 0, 0, 0, 1, 0)), //Kelvin
("g", UNIT(1e0, 0, 0, 0, 0, 0, 0, 1)), //Gramm
("V", UNIT(1e3, 0, 0, 2,-3,-1, 0, 1)), //Volt
("W", UNIT(1e3, 0, 0, 2,-3, 0, 0, 1)), //Watt
//("VA", UNIT(1e3, 0, 0, 2,-3, 0, 0, 1)), //Voltampere=Watt
//("var", UNIT(1e3, 0, 0, 2,-3, 0, 0, 1)), //Var=Watt
("Hz", UNIT(1e0, 0, 0, 0,-1, 0, 0, 0)), //Hertz
("Ohm", UNIT(1e3, 0, 0, 2,-3,-2, 0, 1)), //Ohm
("F", UNIT(1e-3, 0, 0,-2, 4, 2, 0,-1)), //Farad
("H", UNIT(1e3, 0, 0, 2,-2,-2, 0, 1)), //Henry
("C", UNIT(1e0, 0, 0, 0, 1, 1, 0, 0)), //Coulomb
("T", UNIT(1e3, 0, 0, 0,-2,-1, 0, 1)), //Tesla
("S", UNIT(1e-3, 0, 0,-2, 3, 2, 0,-1)), //Siemens
("Wb", UNIT(1e3, 0, 0, 2,-2,-1, 0, 1)), //Weber
//("lm", UNIT(1e0, 0, 1, 0, 0, 0, 0, 0)), //Lumen=Candela
//("lx", UNIT(1e0, 0, 1,-2, 0, 0, 0, 0)), //Lux=lm/m^2
("N", UNIT(1e3, 0, 0, 1,-2, 0, 0, 1)), //Newton
("Pa", UNIT(1e3, 0, 0,-1,-2, 0, 0, 1)), //Pascal; displayUnit ="bar"
("J", UNIT(1e3, 0, 0, 2,-2, 0, 0, 1)), //Joule=N*m
("min", UNIT(6e1, 0, 0, 0, 1, 0, 0, 0)), //Minute
("h", UNIT(3.6e3, 0, 0, 0, 1, 0, 0, 0)), //Stunde
("d", UNIT(8.64e4, 0, 0, 0, 1, 0, 0, 0)), //Tag
("l", UNIT(1e-3, 0, 0, 3, 0, 0, 0, 0)), //Liter
("kg", UNIT(1e3, 0, 0, 0, 0, 0, 0, 1)), //Kilogramm
//("Bq", UNIT(1e0, 0, 0, 0,-1, 0, 0, 0)), //Becquerel = Hertz
//("Gy", UNIT(1e0, 0, 0, 2,-2, 0, 0, 1)), //Gray
//("Sv", UNIT(1e0, 0, 0, 2,-2, 0, 0, 1)), //Sievert=Gray
//("eV", UNIT(1.60218e-16, 0, 0, 2,-2, 0, 0, 1)), //Elektronenvolt 1, 602...*10^-19 kg*m^2/s^2
//("R", UNIT(2.58e-7, 0, 0, 0, 1, 1, 0,-1)), //Röntgen 2, 58*10^-4 C/kg
("kat", UNIT(1e0, 1, 0, 0,-1, 0, 0, 0)), //Katal
("1", UNIT(1e0, 0, 0, 0, 0, 0, 0, 0)), //1
("rad", UNIT(1e0, 0, 0, 0, 0, 0, 0, 0)), //rad; displayUnit ="deg"
//("B", UNIT(1e-2, 0, 0, 0, 0, 0, 0, 0)), //Bel (dezibel dB)
//("phon", UNIT(1e0, 0, 0, 0, 0, 0, 0, 0)), //Phon
//("sone", UNIT(1e0, 0, 0, 0, 0, 0, 0, 0)), //Sone
//("sr", UNIT(1e0, 0, 0, 0, 0, 0, 0, 0)), //Steradiant=m^2/m^2
("degC", UNIT(1e0, 0, 0, 0, 0, 0, 1, 0)), //°Celsius
("degF", UNIT(0.55555555555555555555555555555555555555, 0, 0, 0, 0, 0, 1, 0))};//°Fahrenheit
//("degF", UNIT(5.0 / 9.0, 0, 0, 0, 0, 0, 1, 0, 459.67)), //°Fahrenheit
//("degC", UNIT(1e0, 0, 0, 0, 0, 0, 1, 0, 273.15))};//°Celsius
/* fac, mol, cd, m, s, A, K, g*/
public function hashUnitMod
input Unit inKey;
input Integer inMod;
output Integer outHash;
protected
String str;
Integer i;
algorithm
str := unit2string(inKey);
outHash := System.stringHashDjb2Mod(str,inMod);
end hashUnitMod;
public function unitEqual
input Unit inKey;
input Unit inKey2;
output Boolean res;
algorithm
res := matchcontinue(inKey, inKey2)
local
Real factor1, factor2, r;
Integer i1, i2, i3, i4, i5, i6, i7;
Integer j1, j2, j3, j4, j5, j6, j7;
String s, s2;
list<DAE.ComponentRef> lcr, lcr2;
case (UNIT(factor1, i1, i2, i3, i4, i5, i6, i7), UNIT(factor2, j1, j2, j3, j4, j5, j6, j7)) equation
true = realEq(factor1, factor2);
true = intEq(i1, j1);
true = intEq(i2, j2);
true = intEq(i3, j3);
true = intEq(i4, j4);
true = intEq(i5, j5);
true = intEq(i6, j6);
true = intEq(i7, j7);
then true;
case (UNIT(factor1, i1, i2, i3, i4, i5, i6, i7), UNIT(factor2, j1, j2, j3, j4, j5, j6, j7)) equation
r = realMax(realAbs(factor1), realAbs(factor2));
true = realLe(realDiv(realAbs(realSub(factor1,factor2)),r),1e-3);
true = intEq(i1, j1);
true = intEq(i2, j2);
true = intEq(i3, j3);
true = intEq(i4, j4);
true = intEq(i5, j5);
true = intEq(i6, j6);
true = intEq(i7, j7);
then true;
case (MASTER(lcr), MASTER(lcr2)) //equation
// lcr comparison????
then true;
case (UNKNOWN(s), UNKNOWN(s2)) equation
true = stringEqual(s, s2);
then true;
else false;
end matchcontinue;
end unitEqual;
public function unit2string
input Unit inUnit;
output String outString;
algorithm
outString := match(inUnit)
local
String s, str;
Boolean b;
list<DAE.ComponentRef> crefList;
Real factor1;
Integer i1, i2, i3, i4, i5, i6, i7;
case UNIT(factor1, i1, i2, i3, i4, i5, i6, i7/* , shift1 */) equation
str = realString(factor1) +& " * ";
b = false;
s = "mol^(" +& intString(i1) +& ")";
s = Util.if_(intEq(i1, 0), "", s);
b = b or intNe(i1, 0);
str = str +& s;
s = Util.if_(b and intNe(i2, 0), " * ", "");
str = str +& s;
s = "cd^(" +& intString(i2) +& ")";
s = Util.if_(intEq(i2, 0), "", s);
b = b or intNe(i2, 0);
str = str +& s;
s = Util.if_(b and intNe(i3, 0), " * ", "");
str = str +& s;
s = "m^(" +& intString(i3) +& ")";
s = Util.if_(intEq(i3, 0), "", s);
b = b or intNe(i3, 0);
str = str +& s;
s = Util.if_(b and intNe(i4, 0), " * ", "");
str = str +& s;
s = "s^(" +& intString(i4) +& ")";
s = Util.if_(intEq(i4, 0), "", s);
b = b or intNe(i4, 0);
str = str +& s;
s = Util.if_(b and intNe(i5, 0), " * ", "");
str = str +& s;
s = "A^(" +& intString(i5) +& ")";
s = Util.if_(intEq(i5, 0), "", s);
b = b or intNe(i5, 0);
str = str +& s;
s = Util.if_(b and intNe(i6, 0), " * ", "");
str = str +& s;
//s = "(K-" +& realString(shift1) +& ")^(" +& intString(i6) +& ")";
s = "K^(" +& intString(i6) +& ")";
s = Util.if_(intEq(i6, 0), "", s);
b = b or intNe(i6, 0);
str = str +& s;
s = Util.if_(b and intNe(i7, 0), " * ", "");
str = str +& s;
s = "g^(" +& intString(i7) +& ")";
s = Util.if_(intEq(i7, 0), "", s);
b = b or intNe(i7, 0);
str = str +& s;
s = Util.if_(b , "", "1");
str = str +& s;
then str;
case MASTER(crefList) equation
str = "MASTER(";
str = str +& printListCr(crefList);
str = str +& ")";
then str;
case UNKNOWN(s) equation
str = "UNKOWN(" +& s +& ")";
then str;
end match;
end unit2string;
public function printListCr
input list<DAE.ComponentRef> inlCr;
output String outS;
algorithm
outS := match(inlCr)
local
list<DAE.ComponentRef> lCr;
DAE.ComponentRef cr;
String s;
case {} then "";
case cr::{} equation
s = ComponentReference.crefStr(cr);
then s;
case cr::lCr equation
s = ComponentReference.crefStr(cr);
s = s +& ", " +& printListCr(lCr);
then s;
end match;
end printListCr;
annotation(__OpenModelica_Interface="backend");
end Unit;