Skip to content

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

Merged
merged 25 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d805333
From YektaMirkan/Units - Added reference pressure property - by grate…
Dec 4, 2019
b8c6393
From YektaMirkan/Units - Added enum of pressure reference - by gratestas
Dec 4, 2019
b327ad5
From YektaMirkan/Units - Added pressure wrapper for pressure referenc…
Dec 4, 2019
ee072e9
Broke out tests into individual assertions. Corrected test values in …
Dec 4, 2019
27b06e3
Removed line in ReferencePressure.As that was causing sign errors and…
Dec 4, 2019
c7fcabc
Corrected issues with pressures less than absolute zero
Dec 5, 2019
e69383f
Updated nuget packages
Dec 6, 2019
77137f8
Improved code coverage and corrected exception handling
Dec 6, 2019
71708c8
Cleaned up unnecessary coverage file
Dec 6, 2019
b9d5045
Relocated the code created by gratestas from GeneratedCode to CustomCode
Dec 6, 2019
db74663
Old habits... rolling back NuGet packages to originals
Dec 9, 2019
82d65db
Renamed unit tests
Dec 9, 2019
ca68e3b
Sorted unit tests in PressureTests
Dec 9, 2019
a6a4b49
Refactored Pressure wrapper per review comments
Dec 9, 2019
b4fe2dd
Removed Pressure Reference property from Pressure.g generated code
Dec 9, 2019
aba8c9b
Added XML comments to Pressure.Wrapper
Dec 9, 2019
cf9e591
Added additional constructor overload for a user-specified atmospheri…
Dec 9, 2019
50a8c67
Merge branch 'master' of https://github.com/angularsen/unitsnet
Dec 11, 2019
28842bf
Updated target framework to latest minor in all projects
Dec 11, 2019
ffa1d05
Renamed files added to CustomCode, ran ReSharper rules on solution to…
Dec 11, 2019
ca7caf1
Revert "Renamed files added to CustomCode, ran ReSharper rules on sol…
Dec 11, 2019
2f7ef1e
Renamed .cs file and moved static methods.
Dec 11, 2019
afbc3b4
Changes based on change request.
Dec 11, 2019
9db829d
Changed IReadonlyList back to List, as it is not compatible with .NET…
Dec 11, 2019
04e9534
Changed References from list to array to minimize user modification.
Dec 11, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions CodeGen/CodeGen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<LangVersion>7.3</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="Serilog" Version="2.8.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="System.CommandLine.DragonFruit" Version="0.2.0-alpha.19174.3" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Serilog" Version="2.9.1-dev-01154" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.0-dev-00834" />
<PackageReference Include="System.CommandLine.DragonFruit" Version="0.3.0-alpha.19577.1" />
</ItemGroup>

</Project>
6 changes: 3 additions & 3 deletions UnitsNet.Benchmark/UnitsNet.Benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.11.5" />
<PackageReference Include="CommandLineParser" Version="2.5.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="2.9.3">
<PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
<PackageReference Include="CommandLineParser" Version="2.6.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="2.9.8">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<ItemGroup>
<!--Get the latest released version of UnitsNet.Serialization.JsonNet in Nuget-->
<PackageReference Include="UnitsNet.Serialization.JsonNet" Version="4.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
<!-- NuGet references that work for both signed and unsigned -->
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta-63127-02" PrivateAssets="All" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>

<!-- Project references, will also generate the corresponding nuget dependencies -->
Expand Down
100 changes: 100 additions & 0 deletions UnitsNet.Tests/CustomCode/PressureTests.cs
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
{
Expand Down Expand Up @@ -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()
{
ReferencePressure refPressure = new ReferencePressure(Pressure.FromAtmospheres(3));
AssertEx.Equals(PressureReference.Absolute, refPressure.Reference);
}
[Fact]
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()
{
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);
}

}
}
1 change: 1 addition & 0 deletions UnitsNet.Tests/GeneratedCode/PressureTestsBase.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion UnitsNet.Tests/UnitsNet.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
Expand Down
34 changes: 34 additions & 0 deletions UnitsNet/CustomCode/Units/PressureUnit.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

129 changes: 129 additions & 0 deletions UnitsNet/CustomCode/Wrappers/Pressure.Wrapper.cs
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
/// 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
/// 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
{
/// <summary>
///
/// </summary>
/// <param name="pressure"></param>
Copy link
Owner

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
/// </summary>
public PressureReference Reference => _reference.GetValueOrDefault(BaseReference);

/// <summary>
/// The measured reference this quantity was constructed with.
/// </summary>
private readonly PressureReference? _reference;

/// <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;

private Pressure Pressure { get; }

/// <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:
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);
}
}
5 changes: 5 additions & 0 deletions UnitsNet/GeneratedCode/Quantities/Pressure.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions UnitsNet/GeneratedCode/Units/PressureUnit.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions UnitsNet/UnitsNet.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<!-- NuGet properties -->
<PropertyGroup>
<PackageId>UnitsNet</PackageId>
Expand Down Expand Up @@ -39,11 +39,11 @@
<SignAssembly>true</SignAssembly>
<AssemblyName>UnitsNet</AssemblyName>
</PropertyGroup>

<!-- NuGet references that work for both signed and unsigned -->
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta-63127-02" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>

Expand Down