Skip to content

Commit

Permalink
Merge pull request #310 from portyanikhin/specify-phase-methods-chaining
Browse files Browse the repository at this point in the history
The `SpecifyPhase` and `UnspecifyPhase` methods of the `Fluid` and `Mixture` now returns self (for methods chaining)
  • Loading branch information
portyanikhin authored Oct 2, 2024
2 parents 27d17f7 + 175bd57 commit 03ba619
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 18 deletions.
12 changes: 10 additions & 2 deletions src/SharpProp/Fluids/AbstractFluid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,17 @@ public virtual void Reset()
_phase = null;
}

public void SpecifyPhase(Phases phase) => Backend.SpecifyPhase(phase);
protected AbstractFluid SpecifyPhase(Phases phase)
{
Backend.SpecifyPhase(phase);
return this;
}

public void UnspecifyPhase() => Backend.UnspecifyPhase();
protected AbstractFluid UnspecifyPhase()
{
Backend.UnspecifyPhase();
return this;
}

[SuppressMessage("ReSharper", "NonReadonlyMemberInGetHashCode")]
public override int GetHashCode() =>
Expand Down
4 changes: 4 additions & 0 deletions src/SharpProp/Fluids/Fluid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ fraction is not null

public Ratio Fraction { get; }

public new IFluid SpecifyPhase(Phases phase) => (Fluid)base.SpecifyPhase(phase);

public new IFluid UnspecifyPhase() => (Fluid)base.UnspecifyPhase();

public new IFluid WithState(
IKeyedInput<Parameters> firstInput,
IKeyedInput<Parameters> secondInput
Expand Down
12 changes: 0 additions & 12 deletions src/SharpProp/Fluids/IAbstractFluid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,4 @@ public interface IAbstractFluid : IFluidState, IDisposable
/// Resets all non-trivial properties.
/// </summary>
void Reset();

/// <summary>
/// Specify the phase state for all further calculations.
/// </summary>
/// <param name="phase">Phase state.</param>
void SpecifyPhase(Phases phase);

/// <summary>
/// Unspecify the phase state and
/// go back to calculating it based on the inputs.
/// </summary>
void UnspecifyPhase();
}
13 changes: 13 additions & 0 deletions src/SharpProp/Fluids/IFluid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ public interface IFluid
/// </summary>
Ratio Fraction { get; }

/// <summary>
/// Specify the phase state for all further calculations.
/// </summary>
/// <param name="phase">Phase state.</param>
/// <returns>Current fluid instance.</returns>
IFluid SpecifyPhase(Phases phase);

/// <summary>
/// Unspecify the phase state and go back to calculating it based on the inputs.
/// </summary>
/// <returns>Current fluid instance.</returns>
IFluid UnspecifyPhase();

/// <summary>
/// Returns a new fluid instance with a defined state.
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions src/SharpProp/Fluids/IMixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ public interface IMixture
/// </summary>
IReadOnlyList<Ratio> Fractions { get; }

/// <summary>
/// Specify the phase state for all further calculations.
/// </summary>
/// <param name="phase">Phase state.</param>
/// <returns>Current mixture instance.</returns>
IMixture SpecifyPhase(Phases phase);

/// <summary>
/// Unspecify the phase state and go back to calculating it based on the inputs.
/// </summary>
/// <returns>Current mixture instance.</returns>
IMixture UnspecifyPhase();

/// <summary>
/// Returns a new mixture instance with a defined state.
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions src/SharpProp/Fluids/Mixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public Mixture(IEnumerable<FluidsList> fluids, IEnumerable<Ratio> fractions)

public IReadOnlyList<Ratio> Fractions { get; }

public new IMixture SpecifyPhase(Phases phase) => (Mixture)base.SpecifyPhase(phase);

public new IMixture UnspecifyPhase() => (Mixture)base.UnspecifyPhase();

public new IMixture WithState(
IKeyedInput<Parameters> firstInput,
IKeyedInput<Parameters> secondInput
Expand Down
7 changes: 3 additions & 4 deletions tests/SharpProp.Tests/Fluids/FluidTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,11 @@ public void Reset_Always_ResetsAllNonTrivialProperties()
[Fact]
public void SpecifyPhase_Always_SpecifiesPhaseForAllFurtherCalculations()
{
IAbstractFluid fluid = _fluid;
fluid.SpecifyPhase(Phases.Gas);
_fluid.SpecifyPhase(Phases.Gas);
var action = () =>
fluid.Update(Input.Pressure(1.Atmospheres()), Input.Temperature(20.DegreesCelsius()));
_fluid.Update(Input.Pressure(1.Atmospheres()), Input.Temperature(20.DegreesCelsius()));
action.Should().Throw<ApplicationException>();
fluid.UnspecifyPhase();
_fluid.UnspecifyPhase();
action.Should().NotThrow();
}

Expand Down
14 changes: 14 additions & 0 deletions tests/SharpProp.Tests/Fluids/MixtureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ public void Update_Always_InputsAreCached()
mixture.Temperature.Kelvins.Should().Be(293.15);
}

[Fact]
public void SpecifyPhase_Always_SpecifiesPhaseForAllFurtherCalculations()
{
_mixture.SpecifyPhase(Phases.Gas);
var action = () =>
_mixture.Update(
Input.Pressure(1.Atmospheres()),
Input.Temperature(20.DegreesCelsius())
);
action.Should().Throw<ApplicationException>();
_mixture.UnspecifyPhase();
action.Should().NotThrow();
}

[Fact]
public void WithState_VodkaInStandardConditions_PhaseIsLiquid() =>
_mixture
Expand Down

0 comments on commit 03ba619

Please sign in to comment.