From 1bbe8f0a1c18334d88683b90f077a34af356f035 Mon Sep 17 00:00:00 2001 From: Vladimir Portyanikhin <86243191+portyanikhin@users.noreply.github.com> Date: Fri, 4 Aug 2023 16:39:22 +0300 Subject: [PATCH] Added the ability to specify the phase --- SharpProp.Tests/Fluids/FluidTests.cs | 15 +++++++++++++++ SharpProp/CoolProp/AbstractState.cs | 14 +++++++++++++- SharpProp/CoolProp/AbstractStatePInvoke.cs | 9 +++++++++ SharpProp/Fluids/AbstractFluid.cs | 4 ++++ SharpProp/Fluids/IAbstractFluid.cs | 12 ++++++++++++ 5 files changed, 53 insertions(+), 1 deletion(-) diff --git a/SharpProp.Tests/Fluids/FluidTests.cs b/SharpProp.Tests/Fluids/FluidTests.cs index dda1014..00b99cf 100644 --- a/SharpProp.Tests/Fluids/FluidTests.cs +++ b/SharpProp.Tests/Fluids/FluidTests.cs @@ -172,6 +172,21 @@ public void Reset_Always_ResetsAllNonTrivialProperties() .WithMessage("Invalid or not defined state!"); } + [Fact] + public void SpecifyPhase_Always_SpecifiesPhaseForAllFurtherCalculations() + { + IAbstractFluid fluid = _fluid; + fluid.SpecifyPhase(Phases.Gas); + var action = () => + fluid.Update( + Input.Pressure(1.Atmospheres()), + Input.Temperature(20.DegreesCelsius()) + ); + action.Should().Throw(); + fluid.UnspecifyPhase(); + action.Should().NotThrow(); + } + [Fact] public void WithState_WaterInStandardConditions_PhaseIsLiquid() => _fluid diff --git a/SharpProp/CoolProp/AbstractState.cs b/SharpProp/CoolProp/AbstractState.cs index 4e08a18..3d6aa4d 100644 --- a/SharpProp/CoolProp/AbstractState.cs +++ b/SharpProp/CoolProp/AbstractState.cs @@ -93,12 +93,24 @@ double secondInput SwigExceptions.ThrowPendingException(); } - public virtual void Clear() + public void Clear() { AbstractStatePInvoke.Clear(_handle); SwigExceptions.ThrowPendingException(); } + public void SpecifyPhase(Phases phase) + { + AbstractStatePInvoke.SpecifyPhase(_handle, (int)phase); + SwigExceptions.ThrowPendingException(); + } + + public void UnspecifyPhase() + { + AbstractStatePInvoke.UnspecifyPhase(_handle); + SwigExceptions.ThrowPendingException(); + } + public double KeyedOutput(Parameters key) { var result = AbstractStatePInvoke.KeyedOutput(_handle, (int)key); diff --git a/SharpProp/CoolProp/AbstractStatePInvoke.cs b/SharpProp/CoolProp/AbstractStatePInvoke.cs index 6491c9e..a67099a 100644 --- a/SharpProp/CoolProp/AbstractStatePInvoke.cs +++ b/SharpProp/CoolProp/AbstractStatePInvoke.cs @@ -45,6 +45,15 @@ double secondInput [DllImport(Library.Name, EntryPoint = "CSharp_AbstractState_clear")] public static extern bool Clear(HandleRef abstractState); + [DllImport(Library.Name, EntryPoint = "CSharp_AbstractState_specify_phase")] + public static extern void SpecifyPhase(HandleRef abstractState, int phase); + + [DllImport( + Library.Name, + EntryPoint = "CSharp_AbstractState_unspecify_phase" + )] + public static extern void UnspecifyPhase(HandleRef abstractState); + [DllImport(Library.Name, EntryPoint = "CSharp_AbstractState_keyed_output")] public static extern double KeyedOutput(HandleRef abstractState, int key); } diff --git a/SharpProp/Fluids/AbstractFluid.cs b/SharpProp/Fluids/AbstractFluid.cs index 4c81972..4c36fed 100644 --- a/SharpProp/Fluids/AbstractFluid.cs +++ b/SharpProp/Fluids/AbstractFluid.cs @@ -51,6 +51,10 @@ public virtual void Reset() _phase = null; } + public void SpecifyPhase(Phases phase) => Backend.SpecifyPhase(phase); + + public void UnspecifyPhase() => Backend.UnspecifyPhase(); + [SuppressMessage("ReSharper", "NonReadonlyMemberInGetHashCode")] public override int GetHashCode() => ( diff --git a/SharpProp/Fluids/IAbstractFluid.cs b/SharpProp/Fluids/IAbstractFluid.cs index 136eb25..94426f9 100644 --- a/SharpProp/Fluids/IAbstractFluid.cs +++ b/SharpProp/Fluids/IAbstractFluid.cs @@ -22,4 +22,16 @@ IKeyedInput secondInput /// Resets all non-trivial properties. /// public void Reset(); + + /// + /// Specify the phase state for all further calculations. + /// + /// Phase state. + public void SpecifyPhase(Phases phase); + + /// + /// Unspecify the phase state and + /// go back to calculating it based on the inputs. + /// + public void UnspecifyPhase(); }