-
Notifications
You must be signed in to change notification settings - Fork 398
Conversion between pressure measurement references #726
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
d805333
b8c6393
b327ad5
ee072e9
27b06e3
c7fcabc
e69383f
77137f8
71708c8
b9d5045
db74663
82d65db
ca68e3b
a6a4b49
b4fe2dd
aba8c9b
cf9e591
50a8c67
28842bf
ffa1d05
ca7caf1
2f7ef1e
afbc3b4
9db829d
04e9534
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
// Licensed under MIT No Attribution, see LICENSE file at the root. | ||
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet. | ||
|
||
using UnitsNet.CustomCode.Wrappers; | ||
using UnitsNet.CustomCode.Units; | ||
using Xunit; | ||
using System; | ||
|
||
namespace UnitsNet.Tests.CustomCode | ||
{ | ||
|
@@ -117,5 +120,102 @@ public void PressureDividedByLengthEqualsSpecificWeight() | |
SpecificWeight specificWeight = Pressure.FromPascals(20) / Length.FromMeters(2); | ||
Assert.Equal(SpecificWeight.FromNewtonsPerCubicMeter(10), specificWeight); | ||
} | ||
// Pressure Measurement References | ||
[Fact] | ||
public void ReferenceConversion_WithDefaultPressureReference_PressureReferenceIsAbsolute() | ||
sequc82 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
ReferencePressure refPressure = new ReferencePressure(Pressure.FromAtmospheres(3)); | ||
AssertEx.Equals(PressureReference.Absolute, refPressure.Reference); | ||
} | ||
[Fact] | ||
sequc82 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public void ReferenceConversion_WithDefaultPressureReference_GaugeIsOneLess() | ||
{ | ||
ReferencePressure refPressure = new ReferencePressure(Pressure.FromAtmospheres(3)); | ||
AssertEx.EqualTolerance(2, refPressure.Gauge.Atmospheres, AtmospheresTolerance); | ||
} | ||
[Fact] | ||
public void ReferenceConversion_WithDefaultPressureReference_VacuumIsNegativeGaugeAsOneLess() | ||
{ | ||
ReferencePressure refPressure = new ReferencePressure(Pressure.FromAtmospheres(3)); | ||
AssertEx.EqualTolerance(-2, refPressure.Vacuum.Atmospheres, AtmospheresTolerance); | ||
} | ||
[Fact] | ||
public void ReferenceConversion_WithDefaultPressureReference_AbsoluteIsEqual() | ||
{ | ||
ReferencePressure refPressure = new ReferencePressure(Pressure.FromAtmospheres(3)); | ||
AssertEx.EqualTolerance(3, refPressure.Absolute.Atmospheres, AtmospheresTolerance); | ||
} | ||
[Fact] | ||
public void ReferenceConversion_WithGaugePressureReference_AbsoluteIsOneMore() | ||
{ | ||
ReferencePressure refPressure = new ReferencePressure(Pressure.FromAtmospheres(3), PressureReference.Gauge); | ||
AssertEx.EqualTolerance(4, refPressure.Absolute.Atmospheres, AtmospheresTolerance); | ||
} | ||
[Fact] | ||
public void ReferenceConversion_WithGaugePressureReference_VacuumIsNegativeGauge() | ||
{ | ||
ReferencePressure refPressure = new ReferencePressure(Pressure.FromAtmospheres(3), PressureReference.Gauge); | ||
AssertEx.EqualTolerance(-3, refPressure.Vacuum.Atmospheres, AtmospheresTolerance); | ||
} | ||
[Fact] | ||
public void ReferenceConversion_WithGaugePressureReference_GaugeIsEqual() | ||
{ | ||
ReferencePressure refPressure = new ReferencePressure(Pressure.FromAtmospheres(3), PressureReference.Gauge); | ||
AssertEx.EqualTolerance(3, refPressure.Gauge.Atmospheres, AtmospheresTolerance); | ||
} | ||
[Fact] | ||
public void ReferenceConversion_WithVacuumPressureReference_VacuumIsEqual() | ||
{ | ||
ReferencePressure refPressure = new ReferencePressure(Pressure.FromAtmospheres(1), PressureReference.Vacuum); | ||
AssertEx.EqualTolerance(1, refPressure.Vacuum.Atmospheres, AtmospheresTolerance); | ||
} | ||
[Fact] | ||
public void ReferenceConversion_WithVacuumPressureReference_GaugeIsNegativeVacuum() | ||
{ | ||
ReferencePressure refPressure = new ReferencePressure(Pressure.FromAtmospheres(1), PressureReference.Vacuum); | ||
AssertEx.EqualTolerance(-1, refPressure.Gauge.Atmospheres, AtmospheresTolerance); | ||
} | ||
[Fact] | ||
public void ReferenceConversion_WithVacuumPressureReferenceToGauge_AbsoluteCannotBeLessThanZero() | ||
{ | ||
ReferencePressure refPressure = new ReferencePressure(Pressure.FromAtmospheres(3), PressureReference.Vacuum); | ||
Assert.Throws<ArgumentOutOfRangeException>(() => refPressure.Gauge.Atmospheres); | ||
} | ||
[Fact] | ||
public void ReferenceConversion_WithVacuumPressureReference_AbsoluteIsOneLessNegative() | ||
{ | ||
ReferencePressure refPressure = new ReferencePressure(Pressure.FromAtmospheres(1), PressureReference.Vacuum); | ||
AssertEx.EqualTolerance(0, refPressure.Absolute.Atmospheres, AtmospheresTolerance); | ||
} | ||
[Fact] | ||
public void ReferenceConversion_WithVacuumPressureReferenceToAbsolute_AbsoluteCannotBeLessThanZero() | ||
{ | ||
ReferencePressure refPressure = new ReferencePressure(Pressure.FromAtmospheres(3), PressureReference.Vacuum); | ||
Assert.Throws<ArgumentOutOfRangeException>(() => refPressure.Absolute.Atmospheres); | ||
} | ||
[Fact] | ||
public void ReferenceConversion_WithAbsolutePressureReferenceToAbsolute_AbsoluteIsEqual() | ||
{ | ||
ReferencePressure refPressure = new ReferencePressure(Pressure.FromAtmospheres(3), PressureReference.Absolute); | ||
AssertEx.EqualTolerance(3, refPressure.Absolute.Atmospheres, AtmospheresTolerance); | ||
} | ||
[Fact] | ||
public void ReferenceConversion_WithNegativeAbsolutePressureReferenceToAbsolute_AbsoluteCannotBeLessThanZero() | ||
{ | ||
ReferencePressure refPressure = new ReferencePressure(Pressure.FromAtmospheres(-3), PressureReference.Absolute); | ||
Assert.Throws<ArgumentOutOfRangeException>(() => refPressure.Absolute.Atmospheres); | ||
} | ||
[Fact] | ||
public void ReferenceConversion_WithNegativeGaugePressureReferenceToAbsolute_AbsoluteCannotBeLessThanZero() | ||
sequc82 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
ReferencePressure refPressure = new ReferencePressure(Pressure.FromAtmospheres(-3), PressureReference.Gauge); | ||
Assert.Throws<ArgumentOutOfRangeException>(() => refPressure.Absolute.Atmospheres); | ||
} | ||
[Fact] | ||
public void ReferencesDoesNotContainUndefined() | ||
{ | ||
Assert.DoesNotContain(PressureReference.Undefined, ReferencePressure.References); | ||
} | ||
|
||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using UnitsNet.Units; | ||
using UnitsNet.CustomCode.Units; | ||
|
||
namespace UnitsNet.CustomCode.Wrappers | ||
{ | ||
/// <summary> | ||
/// From gratestas in #422 | ||
sequc82 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// Pressure is a state function, for its measurement depends on environmental factors such as ambient pressure, elevation above sea level and local weather conditions. For two persons located in the different environment to speak of the same pressure, one must relate it to a reference. There are three basis reference: absolute, gauge, vacuum | ||
sequc82 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// Absolute is zero-referenced to the total vacuum. | ||
/// Gauge refers to a level of the local atmospheric pressure. | ||
/// Vacuum is the negative of the gauge. | ||
///Therefore, to obtain consistent and qualitative data, the reference measurement is crucial. | ||
/// </summary> | ||
public struct ReferencePressure | ||
sequc82 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="pressure"></param> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add a copy of the summaries for each property to the parameters here for convenience. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To confirm, you are suggesting a copy of the summary of pressure and reference? |
||
/// <param name="reference"></param> | ||
public ReferencePressure(Pressure pressure, PressureReference reference) | ||
{ | ||
_reference = reference; | ||
Pressure = pressure; | ||
} | ||
|
||
/// <summary> | ||
/// ctor using BaseReference of absolute to assign default _reference | ||
/// </summary> | ||
/// <param name="pressure"></param> | ||
public ReferencePressure(Pressure pressure) | ||
{ | ||
_reference = BaseReference; | ||
Pressure = pressure; | ||
} | ||
|
||
/// <summary> | ||
/// The public repersentation of the measured reference this quantity was constructed with. | ||
sequc82 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// </summary> | ||
public PressureReference Reference => _reference.GetValueOrDefault(BaseReference); | ||
|
||
/// <summary> | ||
/// The measured reference this quantity was constructed with. | ||
/// </summary> | ||
private readonly PressureReference? _reference; | ||
angularsen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// <summary> | ||
/// List property of reference options: Gauge, Absolute, and Vacuum | ||
/// </summary> | ||
public static List<PressureReference> References { get; } = Enum.GetValues(typeof(PressureReference)).Cast<PressureReference>().Except(new[] { PressureReference.Undefined }).ToList(); | ||
|
||
/// <summary> | ||
/// The base reference representation of this quantity for the numeric value stored internally. All conversions go via this value. | ||
/// </summary> | ||
public static PressureReference BaseReference { get; } = PressureReference.Absolute; | ||
sequc82 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private Pressure Pressure { get; } | ||
sequc82 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// <summary> | ||
/// Get Gauge Pressure. | ||
/// It refers pressure level above Reference Pressure. | ||
/// </summary> | ||
public Pressure Gauge => As(PressureReference.Gauge); | ||
|
||
/// <summary> | ||
/// Get Absolute Pressure. | ||
/// It is zero-referenced pressure to the perfect vacuum. | ||
/// </summary> | ||
public Pressure Absolute => As(PressureReference.Absolute); | ||
|
||
/// <summary> | ||
/// Get Vacuum Pressure. | ||
/// It is a negative Gauge Pressure when Absolute Pressure is below Reference Pressure. | ||
/// </summary> | ||
public Pressure Vacuum => As(PressureReference.Vacuum); | ||
|
||
|
||
private Pressure As(PressureReference reference) | ||
{ | ||
var converted = AsBaseNumericType(reference); | ||
|
||
return new Pressure(converted, Pressure.Unit); | ||
} | ||
|
||
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 - ReferencedPressure.ToUnit(Pressure.Unit).Value; | ||
case PressureReference.Vacuum: return ReferencedPressure.ToUnit(Pressure.Unit).Value - negatingValue * baseReferenceValue; | ||
default: | ||
throw new NotImplementedException($"Can not convert {Reference} to {reference}."); | ||
} | ||
} | ||
|
||
private double AsBaseReference() | ||
{ | ||
switch (Reference) | ||
{ | ||
case PressureReference.Absolute: | ||
sequc82 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (Pressure.Value < 0) throw new ArgumentOutOfRangeException("Absolute pressure cannot be less than zero."); | ||
else return Pressure.Value; | ||
case PressureReference.Gauge: | ||
if (Pressure.Value * -1 > ReferencedPressure.ToUnit(Pressure.Unit).Value) throw new ArgumentOutOfRangeException("Absolute pressure cannot be less than zero."); | ||
else return ReferencedPressure.ToUnit(Pressure.Unit).Value + Pressure.Value; | ||
case PressureReference.Vacuum: | ||
if (Pressure.Value > ReferencedPressure.ToUnit(Pressure.Unit).Value) throw new ArgumentOutOfRangeException("Absolute pressure cannot be less than zero."); | ||
else return ReferencedPressure.ToUnit(Pressure.Unit).Value - Pressure.Value; | ||
default: | ||
throw new NotImplementedException($"Can not convert {Reference} to base reference."); | ||
} | ||
} | ||
/// <summary> | ||
/// Represents the pressure at which Pressure is referenced (1 atm default) | ||
/// </summary> | ||
public static Pressure ReferencedPressure { get; } = new Pressure(1, PressureUnit.Atmosphere); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.