Skip to content

Commit

Permalink
Merge pull request #311 from portyanikhin/with-state-specified-phase
Browse files Browse the repository at this point in the history
The `WithState` method of the `Fluid` and `Mixture` no longer resets the specified phase
  • Loading branch information
portyanikhin authored Oct 2, 2024
2 parents 03ba619 + f924e8f commit 7aa8e96
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/SharpProp/Fluids/AbstractFluid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/// <inheritdoc cref="IAbstractFluid"/>
public abstract partial class AbstractFluid : IAbstractFluid
{
private Phases? _specifiedPhase;
protected AbstractState Backend = default!;

protected IList<IKeyedInput<Parameters>> Inputs { get; private set; } =
Expand Down Expand Up @@ -46,12 +47,14 @@ public virtual void Reset()
protected AbstractFluid SpecifyPhase(Phases phase)
{
Backend.SpecifyPhase(phase);
_specifiedPhase = phase;
return this;
}

protected AbstractFluid UnspecifyPhase()
{
Backend.UnspecifyPhase();
_specifiedPhase = null;
return this;
}

Expand All @@ -68,6 +71,11 @@ IKeyedInput<Parameters> secondInput
)
{
var fluid = CreateInstance();
if (_specifiedPhase is not null)
{
fluid.SpecifyPhase(_specifiedPhase.Value);
}

fluid.Update(firstInput, secondInput);
return fluid;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/SharpProp.Tests/Fluids/FluidExtended.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public override void Reset()
_ozoneDepletionPotential = null;
}

public new IFluidExtended SpecifyPhase(Phases phase) => (FluidExtended)base.SpecifyPhase(phase);

public new IFluidExtended UnspecifyPhase() => (FluidExtended)base.UnspecifyPhase();

public new IFluidExtended WithState(
IKeyedInput<Parameters> firstInput,
IKeyedInput<Parameters> secondInput
Expand Down
1 change: 1 addition & 0 deletions tests/SharpProp.Tests/Fluids/FluidExtendedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void Methods_New_ReturnsInstancesOfInheritedType()
{
_fluid.Clone().Should().BeOfType<FluidExtended>();
_fluid.Factory().Should().BeOfType<FluidExtended>();
_fluid.SpecifyPhase(Phases.Gas).UnspecifyPhase().Should().BeOfType<FluidExtended>();
_fluid
.WithState(Input.Pressure(HighPressure), Input.Temperature(_fluid.Temperature))
.Should()
Expand Down
45 changes: 42 additions & 3 deletions tests/SharpProp.Tests/Fluids/FluidTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,50 @@ public void Reset_Always_ResetsAllNonTrivialProperties()
public void SpecifyPhase_Always_SpecifiesPhaseForAllFurtherCalculations()
{
_fluid.SpecifyPhase(Phases.Gas);
var action = () =>
var update = () =>
_fluid.Update(Input.Pressure(1.Atmospheres()), Input.Temperature(20.DegreesCelsius()));
action.Should().Throw<ApplicationException>();
Action withState = () =>
_ = _fluid.WithState(
Input.Pressure(1.Atmospheres()),
Input.Temperature(20.DegreesCelsius())
);
update.Should().Throw<ApplicationException>();
withState.Should().Throw<ApplicationException>();
_fluid.UnspecifyPhase();
action.Should().NotThrow();
update.Should().NotThrow();
withState.Should().NotThrow();
}

[Fact]
public void SpecifyPhase_MethodsChaining_SpecifiesPhaseForAllFurtherCalculations()
{
_fluid.SpecifyPhase(Phases.Gas);
var invalidUpdate = () =>
_fluid
.SpecifyPhase(Phases.Gas)
.Update(Input.Pressure(1.Atmospheres()), Input.Temperature(20.DegreesCelsius()));
Action invalidWithState = () =>
_ = _fluid
.SpecifyPhase(Phases.Gas)
.WithState(Input.Pressure(1.Atmospheres()), Input.Temperature(20.DegreesCelsius()));
invalidUpdate.Should().Throw<ApplicationException>();
invalidWithState.Should().Throw<ApplicationException>();
var validUpdate = () =>
{
_fluid.SpecifyPhase(Phases.Gas);
_fluid
.UnspecifyPhase()
.Update(Input.Pressure(1.Atmospheres()), Input.Temperature(20.DegreesCelsius()));
};
var validWithState = () =>
{
_fluid.SpecifyPhase(Phases.Gas);
_ = _fluid
.UnspecifyPhase()
.WithState(Input.Pressure(1.Atmospheres()), Input.Temperature(20.DegreesCelsius()));
};
validUpdate.Should().NotThrow();
validWithState.Should().NotThrow();
}

[Fact]
Expand Down
6 changes: 6 additions & 0 deletions tests/SharpProp.Tests/Fluids/IFluidExtended.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public interface IFluidExtended : IFluid
/// </summary>
double? OzoneDepletionPotential { get; }

/// <inheritdoc cref="IFluid.SpecifyPhase"/>
new IFluidExtended SpecifyPhase(Phases phase);

/// <inheritdoc cref="IFluid.UnspecifyPhase"/>
new IFluidExtended UnspecifyPhase();

/// <inheritdoc cref="IFluid.WithState"/>
new IFluidExtended WithState(
IKeyedInput<Parameters> firstInput,
Expand Down
45 changes: 42 additions & 3 deletions tests/SharpProp.Tests/Fluids/MixtureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,53 @@ public void Update_Always_InputsAreCached()
public void SpecifyPhase_Always_SpecifiesPhaseForAllFurtherCalculations()
{
_mixture.SpecifyPhase(Phases.Gas);
var action = () =>
var update = () =>
_mixture.Update(
Input.Pressure(1.Atmospheres()),
Input.Temperature(20.DegreesCelsius())
);
action.Should().Throw<ApplicationException>();
Action withState = () =>
_ = _mixture.WithState(
Input.Pressure(1.Atmospheres()),
Input.Temperature(20.DegreesCelsius())
);
update.Should().Throw<ApplicationException>();
withState.Should().Throw<ApplicationException>();
_mixture.UnspecifyPhase();
action.Should().NotThrow();
update.Should().NotThrow();
withState.Should().NotThrow();
}

[Fact]
public void SpecifyPhase_MethodsChaining_SpecifiesPhaseForAllFurtherCalculations()
{
_mixture.SpecifyPhase(Phases.Gas);
var invalidUpdate = () =>
_mixture
.SpecifyPhase(Phases.Gas)
.Update(Input.Pressure(1.Atmospheres()), Input.Temperature(20.DegreesCelsius()));
Action invalidWithState = () =>
_ = _mixture
.SpecifyPhase(Phases.Gas)
.WithState(Input.Pressure(1.Atmospheres()), Input.Temperature(20.DegreesCelsius()));
invalidUpdate.Should().Throw<ApplicationException>();
invalidWithState.Should().Throw<ApplicationException>();
var validUpdate = () =>
{
_mixture.SpecifyPhase(Phases.Gas);
_mixture
.UnspecifyPhase()
.Update(Input.Pressure(1.Atmospheres()), Input.Temperature(20.DegreesCelsius()));
};
var validWithState = () =>
{
_mixture.SpecifyPhase(Phases.Gas);
_ = _mixture
.UnspecifyPhase()
.WithState(Input.Pressure(1.Atmospheres()), Input.Temperature(20.DegreesCelsius()));
};
validUpdate.Should().NotThrow();
validWithState.Should().NotThrow();
}

[Fact]
Expand Down

0 comments on commit 7aa8e96

Please sign in to comment.