Skip to content

Commit 783b1fc

Browse files
authored
Add formula to compute electric power (#805)
* Add formula to compute electric power Electric power is defined as P = U * I (Power equals potential times current)
1 parent bfde73f commit 783b1fc

File tree

6 files changed

+56
-0
lines changed

6 files changed

+56
-0
lines changed

UnitsNet.Tests/CustomCode/ElectricCurrentTests.cs

+7
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,12 @@ public void ElectricCurrentTimesElectricResistanceEqualsElectricPotential(float
3434
ElectricPotential potential = ElectricCurrent.FromAmperes(current) * ElectricResistance.FromOhms(resistance);
3535
Assert.Equal(expected, potential.Volts);
3636
}
37+
38+
[Fact]
39+
public void ElectricCurrentMultipliedByElectricPotentialEqualsPower()
40+
{
41+
Power p = ElectricCurrent.FromAmperes(2) * ElectricPotential.FromVolts(10);
42+
Assert.Equal(20, p.Watts);
43+
}
3744
}
3845
}

UnitsNet.Tests/CustomCode/ElectricPotentialTests.cs

+7
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,12 @@ public void ElectricPotentialDividedByElectricResistanceEqualsElectricCurrent(fl
4040
ElectricCurrent current = ElectricPotential.FromVolts(potential) / ElectricResistance.FromOhms(resistance);
4141
Assert.Equal(expected, current.Amperes);
4242
}
43+
44+
[Fact]
45+
public void ElectricPotentialMultipliedByElectricCurrentEqualsPower()
46+
{
47+
Power p = ElectricPotential.FromVolts(10) * ElectricCurrent.FromAmperes(2);
48+
Assert.Equal(20, p.Watts);
49+
}
4350
}
4451
}

UnitsNet.Tests/CustomCode/PowerTests.cs

+14
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,19 @@ public void PowerDividedBySpecificEnergyEqualsMassFlow()
127127
MassFlow massFlow = Power.FromWatts(15.0) / SpecificEnergy.FromJoulesPerKilogram(3);
128128
Assert.Equal(massFlow, MassFlow.FromKilogramsPerSecond(5));
129129
}
130+
131+
[Fact]
132+
public void PowerDividedByElectricCurrentEqualsElectricPotential()
133+
{
134+
ElectricPotential u = Power.FromWatts(10) / ElectricCurrent.FromAmperes(2);
135+
Assert.Equal(5, u.Volts);
136+
}
137+
138+
[Fact]
139+
public void PowerDividedByElectricPotentialEqualsElectricCurrent()
140+
{
141+
ElectricCurrent i = Power.FromWatts(20) / ElectricPotential.FromVolts(5);
142+
Assert.Equal(4, i.Amperes);
143+
}
130144
}
131145
}

UnitsNet/CustomCode/Quantities/ElectricCurrent.extra.cs

+7
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,12 @@ public partial struct ElectricCurrent
1111
{
1212
return ElectricPotential.FromVolts(resistance.Ohms * current.Amperes);
1313
}
14+
15+
/// <summary>Calculate <see cref="Power"/> from <see cref="ElectricPotential"/> multiplied by <see cref="ElectricCurrent"/>.</summary>
16+
/// <remarks>Electric power is defined as P = U * I.</remarks>
17+
public static Power operator *(ElectricCurrent current, ElectricPotential potential)
18+
{
19+
return Power.FromWatts(potential.Volts * current.Amperes);
20+
}
1421
}
1522
}

UnitsNet/CustomCode/Quantities/ElectricPotential.extra.cs

+7
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,12 @@ public AmplitudeRatio ToAmplitudeRatio()
3232
{
3333
return ElectricCurrent.FromAmperes(potential.Volts / resistance.Ohms);
3434
}
35+
36+
/// <summary>Calculate <see cref="Power"/> from <see cref="ElectricPotential"/> multiplied by <see cref="ElectricCurrent"/>.</summary>
37+
/// <remarks>Electric power is defined as P = U * I.</remarks>
38+
public static Power operator *(ElectricPotential potential, ElectricCurrent current)
39+
{
40+
return Power.FromWatts(potential.Volts * current.Amperes);
41+
}
3542
}
3643
}

UnitsNet/CustomCode/Quantities/Power.extra.cs

+14
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,19 @@ public PowerRatio ToPowerRatio()
9292
{
9393
return Area.FromSquareMeters( power.Watts / heatFlux.WattsPerSquareMeter );
9494
}
95+
96+
/// <summary>Calculate <see cref="ElectricCurrent"/> from <see cref="Power"/> divided by <see cref="ElectricPotential"/>.</summary>
97+
/// <remarks>Electric power is defined as P = U * I, so I = P / U.</remarks>
98+
public static ElectricCurrent operator /(Power power, ElectricPotential potential)
99+
{
100+
return ElectricCurrent.FromAmperes(power.Watts / potential.Volts);
101+
}
102+
103+
/// <summary>Calculate <see cref="ElectricPotential"/> from <see cref="Power"/> divided by <see cref="ElectricCurrent"/>.</summary>
104+
/// <remarks>Electric power is defined as P = U * I, so I = P / U.</remarks>
105+
public static ElectricPotential operator /(Power power, ElectricCurrent current)
106+
{
107+
return ElectricPotential.FromVolts(power.Watts / current.Amperes);
108+
}
95109
}
96110
}

0 commit comments

Comments
 (0)