forked from angularsen/UnitsNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReferencePressure.cs
More file actions
194 lines (171 loc) · 8.84 KB
/
ReferencePressure.cs
File metadata and controls
194 lines (171 loc) · 8.84 KB
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
// Licensed under MIT No Attribution, see LICENSE file at the root.
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
using UnitsNet.CustomCode.Units;
using UnitsNet.InternalHelpers;
using UnitsNet.Units;
namespace UnitsNet.Wrappers
{
/// <summary>
/// Pressure tied to a real-world reference, allowing conversion between references.
/// <list type="bullet">
/// <item>
/// <description>Absolute is referenced to true vacuum.</description>
/// </item>
/// <item>
/// <description>Gauge references the local atmospheric pressure.</description>
/// </item>
/// <item>
/// <description>Vacuum is the negative of the gauge.</description>
/// </item>
/// </list>
/// </summary>
public struct ReferencePressure
{
/// <summary>
/// Represents the pressure at which _pressure is referenced (1 atm default)
/// </summary>
public Pressure AtmosphericPressure { get; set; }
private static readonly Pressure DefaultAtmosphericPressure = new Pressure(1, PressureUnit.Atmosphere);
/// <summary>
/// Gets a list of <see cref="PressureReference" /> options: <see cref="PressureReference.Gauge" />,
/// <see cref="PressureReference.Absolute" />, and <see cref="PressureReference.Vacuum" />
/// </summary>
public static PressureReference[] References { get; } = EnumHelper.GetValues<PressureReference>();
/// <summary>
/// Initializes a new instance of the <see cref="ReferencePressure" /> struct requiring measured
/// <see cref="UnitsNet.Pressure" />
/// parameter. Assumes the <see cref="PressureReference" /> to <see cref="PressureReference.Absolute" />, with 1 atm as
/// the atmospheric <see cref="UnitsNet.Pressure" />.
/// </summary>
/// <param name="pressure">The measured absolute <see cref="UnitsNet.Pressure" /></param>
public ReferencePressure(Pressure pressure) : this(pressure, BaseReference)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ReferencePressure" /> struct requiring
/// measured <see cref="UnitsNet.Pressure" /> and <see cref="PressureReference" /> parameters. Assumes 1 atm as the atmospheric
/// <see cref="UnitsNet.Pressure" />.
/// </summary>
/// <param name="pressure">The measured <see cref="UnitsNet.Pressure" /></param>
/// <param name="reference">
/// The referenced <see cref="PressureReference" /> for the measured <see cref="UnitsNet.Pressure" />
/// </param>
public ReferencePressure(Pressure pressure, PressureReference reference) : this(pressure, reference, DefaultAtmosphericPressure)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ReferencePressure" /> struct requiring
/// measured <see cref="UnitsNet.Pressure" />, <see cref="PressureReference" />, and atmospheric <see cref="UnitsNet.Pressure" />
/// parameters
/// </summary>
/// <param name="pressure">The measured <see cref="UnitsNet.Pressure" /></param>
/// <param name="reference">
/// The referenced <see cref="PressureReference" /> for the measured <see cref="UnitsNet.Pressure" />
/// </param>
/// <param name="atmosphericPressure">The atmospheric <see cref="UnitsNet.Pressure" /> where the measurement was taken.</param>
public ReferencePressure(Pressure pressure, PressureReference reference, Pressure atmosphericPressure)
{
Reference = reference;
Pressure = pressure;
AtmosphericPressure = atmosphericPressure;
}
/// <summary>
/// Gets the <see cref="PressureReference" /> of the <see cref="ReferencePressure" />
/// </summary>
public PressureReference Reference { get; }
/// <summary>
/// The base reference representation of <see cref="ReferencePressure" /> for the numeric value stored internally. All
/// conversions go via this value.
/// </summary>
public const PressureReference BaseReference = PressureReference.Absolute;
/// <summary>
/// The <see cref="Pressure"/> at the given <see cref="PressureReference"/>.
/// </summary>
public Pressure Pressure { get; }
/// <summary>
/// Get Gauge <see cref="UnitsNet.Pressure" />.
/// It references pressure level above Atmospheric pressure.
/// </summary>
public Pressure Gauge => As(PressureReference.Gauge);
/// <summary>
/// Get Absolute <see cref="UnitsNet.Pressure" />.
/// It is zero-referenced pressure to the perfect vacuum.
/// </summary>
public Pressure Absolute => As(PressureReference.Absolute);
/// <summary>
/// Get Vacuum <see cref="UnitsNet.Pressure" />.
/// It is a negative Gauge pressure when Absolute pressure is below Atmospheric pressure.
/// </summary>
public Pressure Vacuum => As(PressureReference.Vacuum);
/// <summary>
/// Converts <see cref="ReferencePressure" /> to <see cref="UnitsNet.Pressure" /> at <see cref="PressureReference" />
/// </summary>
/// <param name="reference">The <see cref="PressureReference" /> to convert <see cref="ReferencePressure" /> to.</param>
/// <returns>The <see cref="UnitsNet.Pressure" /> at the specified <see cref="PressureReference" /></returns>
private Pressure As(PressureReference reference)
{
var converted = AsBaseNumericType(reference);
return new Pressure(converted, Pressure.Unit);
}
/// <summary>
/// Converts <see cref="UnitsNet.Pressure.Value" /> to <see cref="double" /> at <see cref="PressureReference" />
/// </summary>
/// <param name="reference">The <see cref="PressureReference" /> to convert <see cref="ReferencePressure" /> to.</param>
/// <returns>The value of pressure at <see cref="PressureReference" /></returns>
private double AsBaseNumericType(PressureReference reference)
{
var baseReferenceValue = AsBaseReference();
if (Reference == reference)
{
return Pressure.Value;
}
var negatingValue = Reference == PressureReference.Vacuum ? -1 : 1;
switch (reference)
{
case PressureReference.Absolute: return baseReferenceValue;
case PressureReference.Gauge: return baseReferenceValue - AtmosphericPressure.ToUnit(Pressure.Unit).Value;
case PressureReference.Vacuum: return AtmosphericPressure.ToUnit(Pressure.Unit).Value - negatingValue * baseReferenceValue;
default:
throw new NotImplementedException($"Can not convert {Reference} to {reference}.");
}
}
/// <summary>
/// Converts <see cref="UnitsNet.Pressure.Value" /> at <see cref="Reference" /> to <see cref="double" /> at
/// <see cref="BaseReference" />
/// </summary>
/// <returns>The value of pressure at the <see cref="BaseReference" /></returns>
private double AsBaseReference()
{
switch (Reference)
{
case PressureReference.Absolute:
{
if (Pressure.Value < 0)
{
throw new ArgumentOutOfRangeException(nameof(Pressure), "Absolute pressure cannot be less than zero.");
}
return Pressure.Value;
}
case PressureReference.Gauge:
{
if (Pressure.Value * -1 > AtmosphericPressure.ToUnit(Pressure.Unit).Value)
{
throw new ArgumentOutOfRangeException(nameof(Pressure), "Absolute pressure cannot be less than zero.");
}
return AtmosphericPressure.ToUnit(Pressure.Unit).Value + Pressure.Value;
}
case PressureReference.Vacuum:
{
if (Pressure.Value > AtmosphericPressure.ToUnit(Pressure.Unit).Value)
{
throw new ArgumentOutOfRangeException(nameof(Pressure), "Absolute pressure cannot be less than zero.");
}
return AtmosphericPressure.ToUnit(Pressure.Unit).Value - Pressure.Value;
}
default:
throw new NotImplementedException($"Can not convert {Reference} to base reference.");
}
}
}
}