/[OhmsLawCalc]/trunk/libOhmsLawCalc/OhmsLawTypes.cs
ViewVC logotype

Contents of /trunk/libOhmsLawCalc/OhmsLawTypes.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 23 - (show annotations) (download)
Wed Jan 29 08:07:43 2014 UTC (5 years, 11 months ago) by william
File size: 13679 byte(s)

1 using Enterprise.Logging;
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6
7 namespace libOhmsLawCalc
8 {
9
10 /* Ohms's Law Types
11 // Current:
12 // * V/R
13 // * P/V
14 // * SQRT(P/R)
15 // Resistance:
16 // * P/I^2
17 // * V^2/P
18 // * V/I
19 // Voltage:
20 // * I*R
21 // * P/I
22 // * SQRT(P*R)
23 // Power:
24 // * V^2/R
25 // * I^2*R
26 // * V*I
27 */
28
29
30 public static class OhmsLaw
31 {
32 [Obsolete("Please use OhmsLawCalc.CreateInstance(type, OhmsLawValue[] values)")]
33 public static void CreateInstance(OhmsLawValueType type) { }
34 public static OhmsLawValue CreateInstance(OhmsLawValueType type, List<OhmsLawValue> values)
35 {
36 if (values == null)
37 {
38 throw new ArgumentNullException("values", "values cannot be null");
39 }
40 if (values.Count == 0)
41 {
42 throw new ArgumentOutOfRangeException("values", "length of values must be greated than 0 (zero)");
43 }
44 //List<OhmsLawValueType> v_types = new List<OhmsLawValueType>();
45 //foreach (OhmsLawValue v in values)
46 //{
47 // v_types.Add(v.ValueType);
48 //}
49 try
50 {
51 Calc calc = null;
52 switch (type)
53 {
54 case OhmsLawValueType.Current:
55 calc = new CurrentCalc(values.ToList());
56 break;
57 case OhmsLawValueType.Power:
58 calc = new PowerCalc(values.ToList());
59 break;
60 case OhmsLawValueType.Resistance:
61 calc = new ResistanceCalc(values.ToList());
62 break;
63 case OhmsLawValueType.Voltage:
64 calc = new VoltageCalc(values.ToList());
65 break;
66 case OhmsLawValueType.None: throw new InvalidCastException("A ValueType of None is invalid for calculation");
67 default:
68 throw new InvalidOperationException(string.Format("Value: 0x{0:x8} is not defined in OhmsLawValueType", (uint)type));
69 }
70 return calc.Result;
71 }
72 catch (UnableToPerformCalculationException ex)
73 {
74 gLog.Error.WriteLine(ex.ToString());
75 }
76 catch (Exception ex)
77 {
78 gLog.Error.WriteLine(ex.ToString());
79 }
80 return null;
81 }
82 public static OhmsLawValue CreateInstance(OhmsLawValueType type, params OhmsLawValue[] values)
83 {
84 return CreateInstance(type, values.ToList());
85 }
86
87 public static OhmsLawValue AutoConvertResult(OhmsLawValue result)
88 {
89 int v = (int)result.Value;
90 while (v == 0)
91 {
92 if (result.UnitType == OhmsLawUnitType.None)
93 {
94 break;
95 }
96 result = ConvertResult(result, (OhmsLawUnitType)((int)result.UnitType - 1));
97 v = (int)result.Value;
98 }
99
100 return result;
101 }
102
103 public static OhmsLawValue ConvertResult(OhmsLawValue result, OhmsLawUnitType unit)
104 {
105 OhmsLawValue olv = OhmsLawValue.Empty;
106 UnitConverter converter = new UnitConverter(result);
107 olv = converter.ConvertTo(unit);
108 return olv;
109 }
110 }
111
112
113 internal abstract class Calc
114 {
115 public Calc() {
116 VTypes = new List<OhmsLawValueType>();
117 Values = new List<OhmsLawValue>();
118 CalculationType = OhmsLawValueType.None;
119 Result = new OhmsLawValue(OhmsLawValueType.None, OhmsLawUnitType.None, 0f);
120 }
121 public Calc(List<OhmsLawValue> values, OhmsLawValueType calctype) : this()
122 {
123 CalculationType = calctype;
124 foreach (OhmsLawValue v in values)
125 {
126 VTypes.Add(v.ValueType);
127 }
128 Values = values;
129 var result = PerformCalc();
130 this.Result = result;
131 }
132
133 public OhmsLawValue Result { get; private set; }
134 protected List<OhmsLawValueType> VTypes { get; private set; }
135 protected List<OhmsLawValue> Values { get; private set; }
136 protected abstract OhmsLawValue PerformCalc();
137
138 protected OhmsLawValueType CalculationType { get; private set; }
139
140 protected OhmsLawValue GetValueByType(OhmsLawValueType type)
141 {
142 foreach (var value in Values)
143 {
144 if (value.ValueType == type)
145 {
146 return value;
147 }
148 }
149 throw new KeyNotFoundException(string.Format("Could not find value for type: {0}", type.ToString()));
150 }
151 }
152 internal class CurrentCalc : Calc
153 {
154 public CurrentCalc(List<OhmsLawValue> values)
155 : base(values, OhmsLawValueType.Current)
156 {
157 }
158 // Current:
159 // * V/R
160 // * P/V
161 // * SQRT(P/R)
162 protected override OhmsLawValue PerformCalc()
163 {
164 // if known v & r
165 if (VTypes.Contains(OhmsLawValueType.Voltage) && VTypes.Contains(OhmsLawValueType.Resistance))
166 {
167 // V/R
168 OhmsLawValue v = GetValueByType(OhmsLawValueType.Voltage);
169 OhmsLawValue r = GetValueByType(OhmsLawValueType.Resistance);
170 float value = v.DefaultValue.Value / r.DefaultValue.Value;
171 OhmsLawValue result = new OhmsLawValue(OhmsLawValueType.Current, OhmsLawUnitType.Default, value);
172 return result;
173 }
174 // if know p & v
175 else if (VTypes.Contains(OhmsLawValueType.Power) && VTypes.Contains(OhmsLawValueType.Voltage))
176 {
177 // P/V
178 OhmsLawValue p = GetValueByType(OhmsLawValueType.Power);
179 OhmsLawValue v = GetValueByType(OhmsLawValueType.Voltage);
180 float value = p.DefaultValue.Value / v.DefaultValue.Value;
181 OhmsLawValue result = new OhmsLawValue(OhmsLawValueType.Current, OhmsLawUnitType.Default, value);
182 return result;
183 }
184 // if known p & r
185 else if (VTypes.Contains(OhmsLawValueType.Power) && VTypes.Contains(OhmsLawValueType.Resistance))
186 {
187 // SQRT(P/R)
188 OhmsLawValue p = GetValueByType(OhmsLawValueType.Power);
189 OhmsLawValue r = GetValueByType(OhmsLawValueType.Resistance);
190 float value = Convert.ToSingle(Math.Sqrt(p.DefaultValue.Value / r.DefaultValue.Value));
191 OhmsLawValue result = new OhmsLawValue(OhmsLawValueType.Current, OhmsLawUnitType.Default, value);
192 return result;
193 }
194 throw new UnableToPerformCalculationException(this.CalculationType);
195 }
196 }
197 internal class ResistanceCalc : Calc
198 {
199 public ResistanceCalc(List<OhmsLawValue> values)
200 : base(values, OhmsLawValueType.Resistance)
201 {
202 }
203 // Resistance:
204 // * P/I^2
205 // * V^2/P
206 // * V/I
207 protected override OhmsLawValue PerformCalc()
208 {
209 if (VTypes.Contains(OhmsLawValueType.Power) && VTypes.Contains(OhmsLawValueType.Current))
210 {
211 // P/I^2
212 OhmsLawValue p = GetValueByType(OhmsLawValueType.Power);
213 OhmsLawValue i = GetValueByType(OhmsLawValueType.Current);
214 float value = p.DefaultValue.Value / Convert.ToSingle((Math.Pow(i.DefaultValue.Value,2)));
215 OhmsLawValue result = new OhmsLawValue(OhmsLawValueType.Resistance, OhmsLawUnitType.Default, value);
216 return result;
217 }
218 else if (VTypes.Contains(OhmsLawValueType.Voltage) && VTypes.Contains(OhmsLawValueType.Power))
219 {
220 // V^2/P
221 OhmsLawValue v = GetValueByType(OhmsLawValueType.Voltage);
222 OhmsLawValue p = GetValueByType(OhmsLawValueType.Power);
223 float value = Convert.ToSingle((Math.Pow(v.DefaultValue.Value, 2))) / p.DefaultValue.Value;
224 OhmsLawValue result = new OhmsLawValue(OhmsLawValueType.Resistance, OhmsLawUnitType.Default, value);
225 return result;
226 }
227 else if (VTypes.Contains(OhmsLawValueType.Voltage) && VTypes.Contains(OhmsLawValueType.Current))
228 {
229 // V/I
230 OhmsLawValue v = GetValueByType(OhmsLawValueType.Voltage);
231 OhmsLawValue i = GetValueByType(OhmsLawValueType.Current);
232 float value = v.DefaultValue.Value / i.DefaultValue.Value;
233 OhmsLawValue result = new OhmsLawValue(OhmsLawValueType.Resistance, OhmsLawUnitType.Default, value);
234 return result;
235 }
236 throw new UnableToPerformCalculationException(this.CalculationType);
237 }
238 }
239 internal class VoltageCalc : Calc
240 {
241 public VoltageCalc(List<OhmsLawValue> values)
242 : base(values, OhmsLawValueType.Voltage)
243 {
244 }
245 // Voltage:
246 // * I*R
247 // * P/I
248 // * SQRT(P*R)
249 protected override OhmsLawValue PerformCalc()
250 {
251 if (VTypes.Contains(OhmsLawValueType.Current) && VTypes.Contains(OhmsLawValueType.Resistance))
252 {
253 // * I*R
254 OhmsLawValue i = GetValueByType(OhmsLawValueType.Current);
255 OhmsLawValue r = GetValueByType(OhmsLawValueType.Resistance);
256 float value = i.DefaultValue.Value * r.DefaultValue.Value;
257 OhmsLawValue result = new OhmsLawValue(OhmsLawValueType.Voltage, OhmsLawUnitType.Default, value);
258 return result;
259 }
260 else if (VTypes.Contains(OhmsLawValueType.Power) && VTypes.Contains(OhmsLawValueType.Current))
261 {
262 // * P/I
263 OhmsLawValue p = GetValueByType(OhmsLawValueType.Power);
264 OhmsLawValue i = GetValueByType(OhmsLawValueType.Current);
265 float value = p.DefaultValue.Value / i.DefaultValue.Value;
266 OhmsLawValue result = new OhmsLawValue(OhmsLawValueType.Voltage, OhmsLawUnitType.Default, value);
267 return result;
268 }
269 else if (VTypes.Contains(OhmsLawValueType.Power) && VTypes.Contains(OhmsLawValueType.Resistance))
270 {
271 // * SQRT(P*R)
272 OhmsLawValue p = GetValueByType(OhmsLawValueType.Power);
273 OhmsLawValue r = GetValueByType(OhmsLawValueType.Resistance);
274 float value = Convert.ToSingle(Math.Sqrt(p.DefaultValue.Value * r.DefaultValue.Value));
275 OhmsLawValue result = new OhmsLawValue(OhmsLawValueType.Voltage, OhmsLawUnitType.Default, value);
276 return result;
277 }
278 throw new UnableToPerformCalculationException(this.CalculationType);
279 }
280 }
281 internal class PowerCalc : Calc
282 {
283 public PowerCalc(List<OhmsLawValue> values)
284 : base(values, OhmsLawValueType.Power)
285 {
286 }
287 // Power:
288 // * V^2/R
289 // * I^2*R
290 // * V*I
291 protected override OhmsLawValue PerformCalc()
292 {
293 if (VTypes.Contains(OhmsLawValueType.Voltage) && VTypes.Contains(OhmsLawValueType.Resistance))
294 {
295 // * V^2/R
296 OhmsLawValue v = GetValueByType(OhmsLawValueType.Voltage);
297 OhmsLawValue r = GetValueByType(OhmsLawValueType.Resistance);
298 float value = Convert.ToSingle((Math.Pow(v.DefaultValue.Value, 2))) / r.DefaultValue.Value;
299 OhmsLawValue result = new OhmsLawValue(OhmsLawValueType.Power, OhmsLawUnitType.Default, value);
300 return result;
301 }
302 else if (VTypes.Contains(OhmsLawValueType.Current) && VTypes.Contains(OhmsLawValueType.Resistance))
303 {
304 // * I^2*R
305 OhmsLawValue i = GetValueByType(OhmsLawValueType.Current);
306 OhmsLawValue r = GetValueByType(OhmsLawValueType.Resistance);
307 float value = Convert.ToSingle((Math.Pow(i.DefaultValue.Value, 2))) / r.DefaultValue.Value;
308 OhmsLawValue result = new OhmsLawValue(OhmsLawValueType.Power, OhmsLawUnitType.Default, value);
309 return result;
310 }
311 else if (VTypes.Contains(OhmsLawValueType.Voltage) && VTypes.Contains(OhmsLawValueType.Current))
312 {
313 // * V*I
314 OhmsLawValue v = GetValueByType(OhmsLawValueType.Voltage);
315 OhmsLawValue i = GetValueByType(OhmsLawValueType.Current);
316 float value = v.DefaultValue.Value * i.DefaultValue.Value;
317 OhmsLawValue result = new OhmsLawValue(OhmsLawValueType.Power, OhmsLawUnitType.Default, value);
318 return result;
319 }
320 throw new UnableToPerformCalculationException(this.CalculationType);
321 }
322 }
323 }

  ViewVC Help
Powered by ViewVC 1.1.22