Skip to content

Commit ca1580c

Browse files
authored
Add operators for energy quantities (#1105)
* added operators to work with entropy * Add convenience operator to multiply energy times frequency * added operators to work with Energy, Entropy, SpecificEnergy, SpecificEntropy, Mass, and TemperatureDelta * changed unit test factor of 1 because it does not cover the wrong operator being used, e.g. division instead of multiplication * changed multiplication order of SpecificEntropy times Mass equals Entropy to improve readability
1 parent 6216e37 commit ca1580c

File tree

10 files changed

+183
-2
lines changed

10 files changed

+183
-2
lines changed

UnitsNet.Tests/CustomCode/EnergyTests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,47 @@ public void EnergyDividedByDurationEqualsPower()
120120
Power p = Energy.FromWattHours(20) / Duration.FromHours(5);
121121
Assert.Equal(4, p.Watts);
122122
}
123+
124+
[Fact]
125+
public void EnergyTimesFrequencyEqualsPower()
126+
{
127+
Power p = Energy.FromJoules(25) * Frequency.FromPerSecond(5);
128+
Assert.Equal(125, p.Watts);
129+
}
130+
131+
[Fact]
132+
public void FrequencyTimesEnergyEqualsPower()
133+
{
134+
Power p = Frequency.FromCyclesPerHour(100) * Energy.FromWattHours(2);
135+
Assert.Equal(200, p.Watts);
136+
}
137+
138+
[Fact]
139+
public void EnergyDividedByTemperatureDeltaEqualsEntropy()
140+
{
141+
Entropy e = Energy.FromJoules(16) / TemperatureDelta.FromKelvins(8);
142+
Assert.Equal(Entropy.FromJoulesPerKelvin(2), e);
143+
}
144+
145+
[Fact]
146+
public void EnergyDividedByEntropyEqualsTemperatureDelta()
147+
{
148+
TemperatureDelta t = Energy.FromJoules(15) / Entropy.FromJoulesPerKelvin(3);
149+
Assert.Equal(TemperatureDelta.FromKelvins(5), t);
150+
}
151+
152+
[Fact]
153+
public void EnergyDividedByMassEqualsSpecificEnergy()
154+
{
155+
SpecificEnergy e = Energy.FromJoules(10) / Mass.FromKilograms(2);
156+
Assert.Equal(SpecificEnergy.FromJoulesPerKilogram(5), e);
157+
}
158+
159+
[Fact]
160+
public void EnergyDividedBySpecificEnergyEqualsMass()
161+
{
162+
Mass m = Energy.FromJoules(24) / SpecificEnergy.FromJoulesPerKilogram(8);
163+
Assert.Equal(Mass.FromKilograms(3), m);
164+
}
123165
}
124166
}

UnitsNet.Tests/CustomCode/EntropyTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323

2424
using System;
25+
using Xunit;
2526

2627
namespace UnitsNet.Tests.CustomCode
2728
{
@@ -35,5 +36,12 @@ public class EntropyTests : EntropyTestsBase
3536
protected override double KilojoulesPerDegreeCelsiusInOneJoulePerKelvin => 1e-3;
3637
protected override double KilojoulesPerKelvinInOneJoulePerKelvin => 1e-3;
3738
protected override double MegajoulesPerKelvinInOneJoulePerKelvin => 1e-6;
39+
40+
[Fact]
41+
public void EntropyDividedByMassEqualsSpecificEntropy()
42+
{
43+
SpecificEntropy specificEntropy = Entropy.FromJoulesPerKelvin(9) / Mass.FromKilograms(3);
44+
Assert.Equal(SpecificEntropy.FromJoulesPerKilogramKelvin(3), specificEntropy);
45+
}
3846
}
3947
}

UnitsNet.Tests/CustomCode/SpecificEnergyTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,12 @@ public void SpecificEnergyTimesBrakeSpecificFuelConsumptionEqualsEnergy()
8080
double value = SpecificEnergy.FromJoulesPerKilogram(10.0) * BrakeSpecificFuelConsumption.FromKilogramsPerJoule(20.0);
8181
Assert.Equal(200d, value);
8282
}
83+
84+
[Fact]
85+
public void SpecificEnergyDividedByTemperatureDeltaEqualsSpecificEntropy()
86+
{
87+
SpecificEntropy specificEntropy = SpecificEnergy.FromJoulesPerKilogram(4) / TemperatureDelta.FromKelvins(0.5);
88+
Assert.Equal(SpecificEntropy.FromJoulesPerKilogramKelvin(8), specificEntropy);
89+
}
8390
}
8491
}

UnitsNet.Tests/CustomCode/SpecificEntropyTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323

2424
using System;
25+
using Xunit;
2526

2627
namespace UnitsNet.Tests.CustomCode
2728
{
@@ -37,5 +38,19 @@ public class SpecificEntropyTests : SpecificEntropyTestsBase
3738
protected override double CaloriesPerGramKelvinInOneJoulePerKilogramKelvin => 2.390057e-4;
3839
protected override double KilocaloriesPerGramKelvinInOneJoulePerKilogramKelvin => 2.390057e-7;
3940
protected override double BtusPerPoundFahrenheitInOneJoulePerKilogramKelvin => 2.3884589662749594e-4;
41+
42+
[Fact]
43+
public void SpecificEntropyTimesMassEqualsEntropy()
44+
{
45+
Entropy e = SpecificEntropy.FromJoulesPerKilogramKelvin(24) * Mass.FromKilograms(2);
46+
Assert.Equal(Entropy.FromJoulesPerKelvin(48), e);
47+
}
48+
49+
[Fact]
50+
public void MassTimesSpecificEntropyEqualsEntropy()
51+
{
52+
Entropy e = Mass.FromKilograms(5) * SpecificEntropy.FromJoulesPerKilogramKelvin(7);
53+
Assert.Equal(Entropy.FromJoulesPerKelvin(35), e);
54+
}
4055
}
4156
}

UnitsNet.Tests/CustomCode/TemperatureDeltaTests.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,31 @@ public class TemperatureDeltaTests : TemperatureDeltaTestsBase
3838
protected override double MillidegreesCelsiusInOneKelvin => 1E3;
3939

4040
[Fact]
41-
public void TemperatureDeltaTimesSpecificEntropyEqualsSpecificEnergy()
41+
public void SpecificEntropyTimesTemperatureDeltaEqualsSpecificEnergy()
4242
{
4343
SpecificEnergy specificEnergy = SpecificEntropy.FromJoulesPerKilogramKelvin(10) * TemperatureDelta.FromKelvins(6);
44-
Assert.Equal(specificEnergy, SpecificEnergy.FromJoulesPerKilogram(60));
44+
Assert.Equal(SpecificEnergy.FromJoulesPerKilogram(60), specificEnergy);
45+
}
46+
47+
[Fact]
48+
public void TemperatureDeltaTimesSpecificEntropyEqualsSpecificEnergy()
49+
{
50+
SpecificEnergy specificEnergy = TemperatureDelta.FromKelvins(10) * SpecificEntropy.FromJoulesPerKilogramKelvin(14);
51+
Assert.Equal(SpecificEnergy.FromJoulesPerKilogram(140), specificEnergy);
52+
}
53+
54+
[Fact]
55+
public void EntropyTimesTemperatureDeltaEqualsEnergy()
56+
{
57+
Energy energy = Entropy.FromKilojoulesPerKelvin(3) * TemperatureDelta.FromKelvins(7);
58+
Assert.Equal(Energy.FromKilojoules(21), energy);
59+
}
60+
61+
[Fact]
62+
public void TemperatureDeltaTimesEntropyEqualsEnergy()
63+
{
64+
Energy energy = TemperatureDelta.FromKelvins(20) * Entropy.FromJoulesPerKelvin(4);
65+
Assert.Equal(Energy.FromJoules(80), energy);
4566
}
4667
}
4768
}

UnitsNet/CustomCode/Quantities/Energy.extra.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,41 @@ public partial struct Energy
1818
{
1919
return Power.FromWatts(energy.Joules / duration.Seconds);
2020
}
21+
22+
/// <summary>Get <see cref="Power"/> from <see cref="Energy"/> times <see cref="Frequency"/>.</summary>
23+
public static Power operator *(Energy energy, Frequency frequency)
24+
{
25+
return Power.FromWatts(energy.Joules * frequency.PerSecond);
26+
}
27+
28+
/// <summary>Get <see cref="Power"/> from <see cref="Frequency"/> times <see cref="Power"/>.</summary>
29+
public static Power operator *(Frequency frequency, Energy energy)
30+
{
31+
return Power.FromWatts(energy.Joules * frequency.PerSecond);
32+
}
33+
34+
/// <summary>Get <see cref="Entropy"/> from <see cref="Energy"/> divided by <see cref="TemperatureDelta"/> </summary>
35+
public static Entropy operator /(Energy energy, TemperatureDelta temperatureDelta)
36+
{
37+
return Entropy.FromJoulesPerKelvin(energy.Joules / temperatureDelta.Kelvins);
38+
}
39+
40+
/// <summary>Get <see cref="TemperatureDelta"/> from <see cref="Energy"/> divided by <see cref="Entropy"/>.</summary>
41+
public static TemperatureDelta operator /(Energy energy, Entropy entropy)
42+
{
43+
return TemperatureDelta.FromKelvins(energy.Joules / entropy.JoulesPerKelvin);
44+
}
45+
46+
/// <summary>Get <see cref="SpecificEnergy"/> from <see cref="Energy"/> divided by <see cref="Mass"/> </summary>
47+
public static SpecificEnergy operator /(Energy energy, Mass mass)
48+
{
49+
return SpecificEnergy.FromJoulesPerKilogram(energy.Joules / mass.Kilograms);
50+
}
51+
52+
/// <summary>Get <see cref="Mass"/> from <see cref="Energy"/> divided by <see cref="SpecificEnergy"/>.</summary>
53+
public static Mass operator /(Energy energy, SpecificEnergy specificEnergy)
54+
{
55+
return Mass.FromKilograms(energy.Joules / specificEnergy.JoulesPerKilogram);
56+
}
2157
}
2258
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Licensed under MIT No Attribution, see LICENSE file at the root.
2+
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
3+
4+
namespace UnitsNet
5+
{
6+
public partial struct Entropy
7+
{
8+
/// <summary>Get <see cref="SpecificEntropy"/> from <see cref="Entropy"/> divided by <see cref="Mass"/>.</summary>
9+
public static SpecificEntropy operator /(Entropy entropy, Mass mass)
10+
{
11+
return SpecificEntropy.FromJoulesPerKilogramKelvin(entropy.JoulesPerKelvin / mass.Kilograms);
12+
}
13+
}
14+
}

UnitsNet/CustomCode/Quantities/SpecificEnergy.extra.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,11 @@ public partial struct SpecificEnergy
3434
{
3535
return Power.FromWatts(massFlow.KilogramsPerSecond * specificEnergy.JoulesPerKilogram);
3636
}
37+
38+
/// <summary>Get <see cref="SpecificEntropy"/> from <see cref="SpecificEnergy"/> divided by <see cref="TemperatureDelta"/>.</summary>
39+
public static SpecificEntropy operator /(SpecificEnergy specificEnergy, TemperatureDelta temperatureDelta)
40+
{
41+
return SpecificEntropy.FromJoulesPerKilogramKelvin(specificEnergy.JoulesPerKilogram / temperatureDelta.Kelvins);
42+
}
3743
}
3844
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Licensed under MIT No Attribution, see LICENSE file at the root.
2+
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
3+
4+
namespace UnitsNet
5+
{
6+
public partial struct SpecificEntropy
7+
{
8+
/// <summary>Get <see cref="Entropy"/> from <see cref="SpecificEntropy"/> times <see cref="Mass"/>.</summary>
9+
public static Entropy operator *(SpecificEntropy specificEntropy, Mass mass)
10+
{
11+
return Entropy.FromJoulesPerKelvin(specificEntropy.JoulesPerKilogramKelvin * mass.Kilograms);
12+
}
13+
14+
/// <summary>Get <see cref="Entropy"/> from <see cref="Mass"/> times <see cref="SpecificEntropy"/>.</summary>
15+
public static Entropy operator *(Mass mass, SpecificEntropy specificEntropy)
16+
{
17+
return Entropy.FromJoulesPerKelvin(specificEntropy.JoulesPerKilogramKelvin * mass.Kilograms);
18+
}
19+
}
20+
}

UnitsNet/CustomCode/Quantities/TemperatureDelta.extra.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,17 @@ public partial struct TemperatureDelta
2222
{
2323
return specificEntropy * temperatureDelta;
2424
}
25+
26+
/// <summary>Get <see cref="Energy"/> from <see cref="Entropy"/> times <see cref="TemperatureDelta"/>.</summary>
27+
public static Energy operator *(Entropy entropy, TemperatureDelta temperatureDelta)
28+
{
29+
return Energy.FromJoules(entropy.JoulesPerKelvin * temperatureDelta.Kelvins);
30+
}
31+
32+
/// <summary>Get <see cref="Energy"/> from <see cref="TemperatureDelta"/> times <see cref="Entropy"/>.</summary>
33+
public static Energy operator *(TemperatureDelta temperatureDelta, Entropy entropy)
34+
{
35+
return Energy.FromJoules(entropy.JoulesPerKelvin * temperatureDelta.Kelvins);
36+
}
2537
}
2638
}

0 commit comments

Comments
 (0)