Skip to content

Commit

Permalink
Add formula to compute electric power
Browse files Browse the repository at this point in the history
Electric power is defined as P = U * I (Power
equals potential times current)
  • Loading branch information
pgrawehr committed Jun 2, 2020
1 parent 5719cb1 commit 089f858
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 CurrentMultipliedByPotentialEqualsPower()
{
ElectricCurrent i = ElectricCurrent.FromAmperes(5);
ElectricPotential u = ElectricPotential.FromVolts(10);
Power p = Power.FromWatts(50);
Assert.Equal(p, i * u);
Assert.Equal(p, u * i);

// Validate the inverse is true as well
Assert.Equal(i, p / u);
Assert.Equal(u, p / i);
}
}
}
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 089f858

Please sign in to comment.