Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PhysicalUnits.cs
1using System;
2using System.Collections.Generic;
4
6{
11 {
20 PUI_SI_UNITS = 0,
21
27 PUI_RATIO_SI_UNITS = 1,
28
34 PUI_LOG10_SI_UNITS = 2,
35
41 PUI_LOG10_RATIO_SI_UNITS = 3,
42
49 PUI_DIGITAL_DATA = 4,
50
56 PUI_ARBITRARY = 5
57 }
58
62 public struct PhysicalUnits
63 {
67 public byte[] Binary;
68
73
77 public byte Radians;
78
82 public byte Steradians;
83
87 public byte Meters;
88
92 public byte Kilograms;
93
97 public byte Seconds;
98
102 public byte Amperes;
103
107 public byte Kelvins;
108
112 public byte Moles;
113
117 public byte Candelas;
118
124 {
125 LinkedList<KeyValuePair<AtomicUnit, int>> Parts = new LinkedList<KeyValuePair<AtomicUnit, int>>();
126 Prefix Prefix = Prefix.None;
127
128 if (unitPerBase64.TryGetValue(Convert.ToBase64String(this.Binary), out string s))
129 return new Unit(new AtomicUnit(s));
130
131 if (this.AddStep(Parts, "rad", this.Radians, ref Prefix, 0) &&
132 this.AddStep(Parts, "sr", this.Steradians, ref Prefix, 0) &&
133 this.AddStep(Parts, "m", this.Meters, ref Prefix, 0) &&
134 this.AddStep(Parts, "g", this.Kilograms, ref Prefix, 3) &&
135 this.AddStep(Parts, "s", this.Seconds, ref Prefix, 0) &&
136 this.AddStep(Parts, "A", this.Amperes, ref Prefix, 0) &&
137 this.AddStep(Parts, "K", this.Kelvins, ref Prefix, 0) &&
138 this.AddStep(Parts, "mol", this.Moles, ref Prefix, 0) &&
139 this.AddStep(Parts, "cd", this.Candelas, ref Prefix, 0))
140 {
141 return new Unit(Prefix, Parts);
142 }
143 else
144 return null;
145 }
146
147 private const byte _ = 128;
148 private const byte p1 = 130;
149 private const byte p2 = 132;
150 private const byte p3 = 134;
151 private const byte p4 = 136;
152 private const byte m1 = 126;
153 private const byte m2 = 124;
154 private const byte m3 = 122;
155 private static readonly KeyValuePair<string, byte[]>[] specialUnits = new KeyValuePair<string, byte[]>[]
156 { // rad sr m kg s A K mol cd
157 new KeyValuePair<string, byte[]>("Pa", new byte[] { 0, _, _, m1, p1, m2, _, _, _, _ }),
158 new KeyValuePair<string, byte[]>("Ω", new byte[] { 0, _, _, p2, p1, m3, m2, _, _, _ }),
159 new KeyValuePair<string, byte[]>("C", new byte[] { 0, _, _, _, _, p1, p1, _, _, _ }),
160 new KeyValuePair<string, byte[]>("J", new byte[] { 0, _, _, p2, p1, m2, _, _, _, _ }),
161 new KeyValuePair<string, byte[]>("Hz", new byte[] { 0, _, _, _, _, m1, _, _, _, _ }),
162 new KeyValuePair<string, byte[]>("N", new byte[] { 0, _, _, p1, p1, m2, _, _, _, _ }),
163 new KeyValuePair<string, byte[]>("W", new byte[] { 0, _, _, p2, p1, m3, _, _, _, _ }),
164 new KeyValuePair<string, byte[]>("V", new byte[] { 0, _, _, p2, p1, m3, m1, _, _, _ }),
165 new KeyValuePair<string, byte[]>("F", new byte[] { 0, _, _, m2, m1, p4, p2, _, _, _ }),
166 new KeyValuePair<string, byte[]>("S", new byte[] { 0, _, _, m2, m1, p3, p2, _, _, _ }),
167 new KeyValuePair<string, byte[]>("Wb", new byte[] { 0, _, _, p2, p1, m2, m1, _, _, _ }),
168 new KeyValuePair<string, byte[]>("T", new byte[] { 0, _, _, _, p1, m2, m1, _, _, _ }),
169 new KeyValuePair<string, byte[]>("H", new byte[] { 0, _, _, p2, p1, m2, m2, _, _, _ }),
170 new KeyValuePair<string, byte[]>("lm", new byte[] { 0, _, p1, _, _, _, _, _, _, p1 }),
171 new KeyValuePair<string, byte[]>("lx", new byte[] { 0, _, p1, m2, _, _, _, _, _, p1 })
172 };
173 private static readonly Dictionary<string, string> unitPerBase64 = OrderUnits();
174
175 private static Dictionary<string, string> OrderUnits()
176 {
177 Dictionary<string, string> Result = new Dictionary<string, string>();
178
179 foreach (KeyValuePair<string, byte[]> P in specialUnits)
180 Result[Convert.ToBase64String(P.Value)] = P.Key;
181
182 return Result;
183 }
184
185 private bool AddStep(LinkedList<KeyValuePair<AtomicUnit, int>> Parts, string Unit, byte Exponent,
186 ref Prefix Prefix, int ExponentModifier)
187 {
188 int i = Exponent;
189 if ((i & 1) != 0)
190 return false;
191
192 i -= 128;
193 i /= 2;
194
195 if (i != 0)
196 {
197 Parts.AddLast(new KeyValuePair<AtomicUnit, int>(new AtomicUnit(Unit), i));
198 Prefix += ExponentModifier;
199 }
200
201 return true;
202 }
203
212 public static bool TryCreate(Unit Unit, ref double Value, ref double NrDecimals, out PhysicalUnits Units)
213 {
214 Units = new PhysicalUnits()
215 {
217 Radians = 128,
218 Steradians = 128,
219 Meters = 128,
220 Kilograms = 128,
221 Seconds = 128,
222 Amperes = 128,
223 Kelvins = 128,
224 Moles = 128,
225 Candelas = 128
226 };
227
228 if (Unit.HasFactors)
229 {
230 Unit = Unit.ToReferenceUnits(ref Value, ref NrDecimals);
231
232 foreach (KeyValuePair<AtomicUnit, int> P in Unit.Factors)
233 {
234 int Exponent = 128 + (P.Value << 1);
235 if (Exponent < 0 || Exponent > 255)
236 return false;
237
238 byte Exp = (byte)Exponent;
239
240 switch (P.Key.Name)
241 {
242 case "rad":
243 Units.Radians = Exp;
244 break;
245
246 case "sr":
247 Units.Steradians = Exp;
248 break;
249
250 case "m":
251 Units.Meters = Exp;
252 break;
253
254 case "g":
255 Units.Kilograms = Exp;
256 Value *= Math.Pow(1000, -P.Value);
257 NrDecimals += P.Value;
258 break;
259
260 case "s":
261 Units.Seconds = Exp;
262 break;
263
264 case "A":
265 Units.Amperes = Exp;
266 break;
267
268 case "K":
269 Units.Kelvins = Exp;
270 break;
271
272 case "mol":
273 Units.Moles = Exp;
274 break;
275
276 case "cd":
277 Units.Candelas = Exp;
278 break;
279
280 default:
281 return false;
282 }
283 }
284 }
285
286 if (Unit.Prefix != Prefix.None)
287 {
288 int i = (int)Unit.Prefix;
289 Value *= Math.Pow(10, i);
290 NrDecimals -= i;
291 }
292
293 return true;
294 }
295 }
296}
Represents an atomic unit.
Definition: AtomicUnit.cs:7
Represents a unit.
Definition: Unit.cs:15
bool HasFactors
If the unit has any factors.
Definition: Unit.cs:426
Unit ToReferenceUnits(ref double Magnitude)
Converts the unit to a series of reference unit factors. (Unrecognized units will be assumed to be re...
Definition: Unit.cs:807
ICollection< KeyValuePair< AtomicUnit, int > > Factors
Sequence of atomic unit factors, and their corresponding exponents.
Definition: Unit.cs:409
Prefix Prefix
Associated prefix.
Definition: Unit.cs:404
Prefix
SI prefixes. http://physics.nist.gov/cuu/Units/prefixes.html
Definition: Prefixes.cs:11
Ieee1451_0PhysicalUnitsInterpretation
How to interpret physical units.
byte Kelvins
(2 * <exponent of kelvins>) + 128
Unit TryCreateUnit()
Tries to create a unit from the description available in the object.
byte Kilograms
(2 * <exponent of kilograms>) + 128
byte[] Binary
Binary byte array representation of the physical unit.
byte Candelas
(2 * <exponent of candelas>) + 128
byte Radians
(2 * <exponent of radians>) + 128
Ieee1451_0PhysicalUnitsInterpretation Interpretation
Physical Units interpretation
byte Meters
(2 * <exponent of meters>) + 128
byte Amperes
(2 * <exponent of amperes>) + 128
static bool TryCreate(Unit Unit, ref double Value, ref double NrDecimals, out PhysicalUnits Units)
Tries to create an IEEE 1451 units object from a script unit.
byte Steradians
(2 * <exponent of steradians>) + 128
byte Seconds
(2 * <exponent of seconds>) + 128