Skip to content

Commit

Permalink
Add formula to compute electric power (#805)
Browse files Browse the repository at this point in the history
* Add formula to compute electric power

Electric power is defined as P = U * I (Power equals potential times current)
  • Loading branch information
pgrawehr authored Jun 10, 2020
1 parent bfde73f commit 783b1fc
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 0 deletions.
7 changes: 7 additions & 0 deletions UnitsNet.Tests/CustomCode/ElectricCurrentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,12 @@ public void ElectricCurrentTimesElectricResistanceEqualsElectricPotential(float
ElectricPotential potential = ElectricCurrent.FromAmperes(current) * ElectricResistance.FromOhms(resistance);
Assert.Equal(expected, potential.Volts);
}

[Fact]
public void ElectricCurrentMultipliedByElectricPotentialEqualsPower()
{
Power p = ElectricCurrent.FromAmperes(2) * ElectricPotential.FromVolts(10);
Assert.Equal(20, p.Watts);
}
}
}
7 changes: 7 additions & 0 deletions UnitsNet.Tests/CustomCode/ElectricPotentialTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,12 @@ public void ElectricPotentialDividedByElectricResistanceEqualsElectricCurrent(fl
ElectricCurrent current = ElectricPotential.FromVolts(potential) / ElectricResistance.FromOhms(resistance);
Assert.Equal(expected, current.Amperes);
}

[Fact]
public void ElectricPotentialMultipliedByElectricCurrentEqualsPower()
{
Power p = ElectricPotential.FromVolts(10) * ElectricCurrent.FromAmperes(2);
Assert.Equal(20, p.Watts);
}
}
}
14 changes: 14 additions & 0 deletions UnitsNet.Tests/CustomCode/PowerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,19 @@ public void PowerDividedBySpecificEnergyEqualsMassFlow()
MassFlow massFlow = Power.FromWatts(15.0) / SpecificEnergy.FromJoulesPerKilogram(3);
Assert.Equal(massFlow, MassFlow.FromKilogramsPerSecond(5));
}

[Fact]
public void PowerDividedByElectricCurrentEqualsElectricPotential()
{
ElectricPotential u = Power.FromWatts(10) / ElectricCurrent.FromAmperes(2);
Assert.Equal(5, u.Volts);
}

[Fact]
public void PowerDividedByElectricPotentialEqualsElectricCurrent()
{
ElectricCurrent i = Power.FromWatts(20) / ElectricPotential.FromVolts(5);
Assert.Equal(4, i.Amperes);
}
}
}
7 changes: 7 additions & 0 deletions UnitsNet/CustomCode/Quantities/ElectricCurrent.extra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,12 @@ public partial struct ElectricCurrent
{
return ElectricPotential.FromVolts(resistance.Ohms * current.Amperes);
}

/// <summary>Calculate <see cref="Power"/> from <see cref="ElectricPotential"/> multiplied by <see cref="ElectricCurrent"/>.</summary>
/// <remarks>Electric power is defined as P = U * I.</remarks>
public static Power operator *(ElectricCurrent current, ElectricPotential potential)
{
return Power.FromWatts(potential.Volts * current.Amperes);
}
}
}
7 changes: 7 additions & 0 deletions UnitsNet/CustomCode/Quantities/ElectricPotential.extra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,12 @@ public AmplitudeRatio ToAmplitudeRatio()
{
return ElectricCurrent.FromAmperes(potential.Volts / resistance.Ohms);
}

/// <summary>Calculate <see cref="Power"/> from <see cref="ElectricPotential"/> multiplied by <see cref="ElectricCurrent"/>.</summary>
/// <remarks>Electric power is defined as P = U * I.</remarks>
public static Power operator *(ElectricPotential potential, ElectricCurrent current)
{
return Power.FromWatts(potential.Volts * current.Amperes);
}
}
}
14 changes: 14 additions & 0 deletions UnitsNet/CustomCode/Quantities/Power.extra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,19 @@ public PowerRatio ToPowerRatio()
{
return Area.FromSquareMeters( power.Watts / heatFlux.WattsPerSquareMeter );
}

/// <summary>Calculate <see cref="ElectricCurrent"/> from <see cref="Power"/> divided by <see cref="ElectricPotential"/>.</summary>
/// <remarks>Electric power is defined as P = U * I, so I = P / U.</remarks>
public static ElectricCurrent operator /(Power power, ElectricPotential potential)
{
return ElectricCurrent.FromAmperes(power.Watts / potential.Volts);
}

/// <summary>Calculate <see cref="ElectricPotential"/> from <see cref="Power"/> divided by <see cref="ElectricCurrent"/>.</summary>
/// <remarks>Electric power is defined as P = U * I, so I = P / U.</remarks>
public static ElectricPotential operator /(Power power, ElectricCurrent current)
{
return ElectricPotential.FromVolts(power.Watts / current.Amperes);
}
}
}

0 comments on commit 783b1fc

Please sign in to comment.