View Javadoc
1   package org.djunits.quantity;
2   
3   import org.djunits.quantity.def.Quantity;
4   import org.djunits.unit.AbstractUnit;
5   import org.djunits.unit.UnitInterface;
6   import org.djunits.unit.UnitRuntimeException;
7   import org.djunits.unit.Unitless;
8   import org.djunits.unit.Units;
9   import org.djunits.unit.scale.IdentityScale;
10  import org.djunits.unit.scale.LinearScale;
11  import org.djunits.unit.scale.Scale;
12  import org.djunits.unit.si.SIPrefix;
13  import org.djunits.unit.si.SIPrefixes;
14  import org.djunits.unit.si.SIUnit;
15  import org.djunits.unit.system.UnitSystem;
16  
17  /**
18   * Power is the rate of energy transfer or work done per unit time, measured in watts (W).
19   * <p>
20   * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
21   * for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
22   * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
23   * @author Alexander Verbraeck
24   */
25  public class Power extends Quantity<Power>
26  {
27      /** Constant with value zero. */
28      public static final Power ZERO = ofSi(0.0);
29  
30      /** Constant with value one. */
31      public static final Power ONE = ofSi(1.0);
32  
33      /** Constant with value NaN. */
34      @SuppressWarnings("checkstyle:constantname")
35      public static final Power NaN = ofSi(Double.NaN);
36  
37      /** Constant with value POSITIVE_INFINITY. */
38      public static final Power POSITIVE_INFINITY = ofSi(Double.POSITIVE_INFINITY);
39  
40      /** Constant with value NEGATIVE_INFINITY. */
41      public static final Power NEGATIVE_INFINITY = ofSi(Double.NEGATIVE_INFINITY);
42  
43      /** Constant with value MAX_VALUE. */
44      public static final Power POS_MAXVALUE = ofSi(Double.MAX_VALUE);
45  
46      /** Constant with value -MAX_VALUE. */
47      public static final Power NEG_MAXVALUE = ofSi(-Double.MAX_VALUE);
48  
49      /** */
50      private static final long serialVersionUID = 600L;
51  
52      /**
53       * Instantiate a Power quantity with an SI or base value and a display unit.
54       * @param value the quantity value expressed in the SI or base unit
55       * @param displayUnit the display unit to use
56       * @param useSi use SI value when true, use value in unit when false
57       */
58      public Power(final double value, final Power.Unit displayUnit, final boolean useSi)
59      {
60          super(value, displayUnit, useSi);
61      }
62  
63      /**
64       * Instantiate a Power quantity expressed in the given unit.
65       * @param valueInUnit the quantity value expressed in the given unit
66       * @param unit the unit of the value, also acts as the display unit
67       */
68      public Power(final double valueInUnit, final Power.Unit unit)
69      {
70          this(valueInUnit, unit, false);
71      }
72  
73      /**
74       * Return a Power instance based on an SI value.
75       * @param si the si value
76       * @return the Power instance based on an SI value
77       */
78      public static Power ofSi(final double si)
79      {
80          return new Power(si, Power.Unit.SI, true);
81      }
82  
83      /**
84       * Instantiate a Power quantity with an SI or base value and a display unit.
85       * @param siValue the quantity value expressed in the SI or base unit
86       * @param displayUnit the display unit to use
87       * @return the Power instance based on an SI value with the given display unit
88       */
89      public static Power ofSi(final double siValue, final Power.Unit displayUnit)
90      {
91          return new Power(siValue, displayUnit, true);
92      }
93  
94      @Override
95      public Power instantiateSi(final double siValue, final UnitInterface<Power> displayUnit)
96      {
97          return new Power(siValue, (Power.Unit) displayUnit, true);
98      }
99  
100     /**
101      * Returns a Power representation of a textual representation of a value with a unit. The String representation that can be
102      * parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are allowed,
103      * but not required, between the value and the unit.
104      * @param text the textual representation to parse into a Power
105      * @return the Scalar representation of the value in its unit
106      * @throws IllegalArgumentException when the text cannot be parsed
107      * @throws NullPointerException when the text argument is null
108      */
109     public static Power valueOf(final String text)
110     {
111         return Quantity.valueOf(text, ZERO);
112     }
113 
114     /**
115      * Returns a Power based on a value expressed in the unit.
116      * @param valueInUnit the value, expressed in the given unit
117      * @param unit the unit of the value, also acts as the display unit
118      * @return ab Power representation of the value in its unit
119      */
120     public static Power of(final double valueInUnit, final Power.Unit unit)
121     {
122         return new Power(valueInUnit, unit);
123     }
124 
125     /**
126      * Returns a Power based on a value and the textual representation of the unit, which can be localized.
127      * @param valueInUnit the value, expressed in the unit as given by unitString
128      * @param unitString the textual representation of the unit
129      * @return the Scalar representation of the value in its unit
130      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
131      * @throws NullPointerException when the unitString argument is null
132      */
133     public static Power of(final double valueInUnit, final String unitString)
134     {
135         return Quantity.of(valueInUnit, unitString, ZERO);
136     }
137 
138     @Override
139     public Power.Unit getDisplayUnit()
140     {
141         return (Power.Unit) super.getDisplayUnit();
142     }
143 
144     /**
145      * Calculate the division of Power and Power, which results in a Dimensionless quantity.
146      * @param v quantity
147      * @return quantity as a division of Power and Power
148      */
149     public Dimensionless divide(final Power v)
150     {
151         return new Dimensionless(this.si() / v.si(), Unitless.BASE);
152     }
153 
154     /**
155      * Calculate the multiplication of Power and Duration, which results in a Energy scalar.
156      * @param v scalar
157      * @return scalar as a multiplication of Power and Duration
158      */
159     public Energy multiply(final Duration v)
160     {
161         return new Energy(this.si() * v.si(), Energy.Unit.SI);
162     }
163 
164     /**
165      * Calculate the division of Power and Frequency, which results in a Energy scalar.
166      * @param v scalar
167      * @return scalar as a division of Power and Frequency
168      */
169     public Energy divide(final Frequency v)
170     {
171         return new Energy(this.si() / v.si(), Energy.Unit.SI);
172     }
173 
174     /**
175      * Calculate the division of Power and Energy, which results in a Frequency scalar.
176      * @param v scalar
177      * @return scalar as a division of Power and Energy
178      */
179     public Frequency divide(final Energy v)
180     {
181         return new Frequency(this.si() / v.si(), Frequency.Unit.SI);
182     }
183 
184     /**
185      * Calculate the division of Power and Speed, which results in a Force scalar.
186      * @param v scalar
187      * @return scalar as a division of Power and Speed
188      */
189     public Force divide(final Speed v)
190     {
191         return new Force(this.si() / v.si(), Force.Unit.SI);
192     }
193 
194     /**
195      * Calculate the division of Power and Force, which results in a Speed scalar.
196      * @param v scalar
197      * @return scalar as a division of Power and Force
198      */
199     public Speed divide(final Force v)
200     {
201         return new Speed(this.si() / v.si(), Speed.Unit.SI);
202     }
203 
204     /**
205      * Calculate the division of Power and ElectricPotential, which results in a ElectricCurrent scalar.
206      * @param v scalar
207      * @return scalar as a division of Power and ElectricPotential
208      */
209     public ElectricCurrent divide(final ElectricPotential v)
210     {
211         return new ElectricCurrent(this.si() / v.si(), ElectricCurrent.Unit.SI);
212     }
213 
214     /**
215      * Calculate the division of Power and ElectricCurrent, which results in a ElectricPotential scalar.
216      * @param v scalar
217      * @return scalar as a division of Power and ElectricCurrent
218      */
219     public ElectricPotential divide(final ElectricCurrent v)
220     {
221         return new ElectricPotential(this.si() / v.si(), ElectricPotential.Unit.SI);
222     }
223 
224     /**
225      * Calculate the division of Power and Acceleration, which results in a Momentum scalar.
226      * @param v scalar
227      * @return scalar as a division of Power and Acceleration
228      */
229     public Momentum divide(final Acceleration v)
230     {
231         return new Momentum(this.si() / v.si(), Momentum.Unit.SI);
232     }
233 
234     /**
235      * Calculate the division of Power and Momentum, which results in a Acceleration scalar.
236      * @param v scalar
237      * @return scalar as a division of Power and Momentum
238      */
239     public Acceleration divide(final Momentum v)
240     {
241         return new Acceleration(this.si() / v.si(), Acceleration.Unit.SI);
242     }
243 
244     /******************************************************************************************************/
245     /********************************************** UNIT CLASS ********************************************/
246     /******************************************************************************************************/
247 
248     /**
249      * Power.Unit encodes the units for the rate of energy transfer or work done per unit time.
250      * <p>
251      * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
252      * See for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
253      * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
254      * @author Alexander Verbraeck
255      */
256     @SuppressWarnings("checkstyle:constantname")
257     public static class Unit extends AbstractUnit<Power>
258     {
259         /** The dimensions of power: kgm2/s3. */
260         public static final SIUnit SI_UNIT = SIUnit.of("kgm2/s3");
261 
262         /** Watt. */
263         public static final Power.Unit W =
264                 new Power.Unit("W", "W", "watt", IdentityScale.SCALE, UnitSystem.SI_DERIVED, SIPrefixes.getSiPrefix(""));
265 
266         /** The SI or BASE unit. */
267         public static final Power.Unit SI = (Unit) W.generateSiPrefixes(false, false);
268 
269         /** microwatt. */
270         public static final Power.Unit muW = Units.resolve(Power.Unit.class, "muW");
271 
272         /** milliwatt. */
273         public static final Power.Unit mW = Units.resolve(Power.Unit.class, "mW");
274 
275         /** kilowatt. */
276         public static final Power.Unit kW = Units.resolve(Power.Unit.class, "kW");
277 
278         /** megawatt. */
279         public static final Power.Unit MW = Units.resolve(Power.Unit.class, "MW");
280 
281         /** gigawatt. */
282         public static final Power.Unit GW = Units.resolve(Power.Unit.class, "GW");
283 
284         /** terawatt. */
285         public static final Power.Unit TW = Units.resolve(Power.Unit.class, "TW");
286 
287         /** petawatt. */
288         public static final Power.Unit PW = Units.resolve(Power.Unit.class, "PW");
289 
290         /** foot-pound-force per hour. */
291         public static final Power.Unit ft_lbf_h = SI.deriveUnit("ft.lbf/h", "foot pound-force per hour",
292                 Length.Unit.CONST_FT * Mass.Unit.CONST_LB * Acceleration.Unit.CONST_GRAVITY / 3600.0, UnitSystem.IMPERIAL);
293 
294         /** foot-pound-force per minute. */
295         public static final Power.Unit ft_lbf_min = SI.deriveUnit("ft.lbf/min", "foot pound-force per minute",
296                 Length.Unit.CONST_FT * Mass.Unit.CONST_LB * Acceleration.Unit.CONST_GRAVITY / 60.0, UnitSystem.IMPERIAL);
297 
298         /** foot-pound-force per second. */
299         public static final Power.Unit ft_lbf_s = SI.deriveUnit("ft.lbf/s", "foot pound-force per second",
300                 Length.Unit.CONST_FT * Mass.Unit.CONST_LB * Acceleration.Unit.CONST_GRAVITY, UnitSystem.IMPERIAL);
301 
302         /** horsepower (metric). */
303         public static final Power.Unit hp_M = W.deriveUnit("hp(M)", "horsepower (metric)", 735.49875, UnitSystem.OTHER);
304 
305         /** sthene-meter per second. */
306         public static final Power.Unit sn_m_s = SI.deriveUnit("sn.m/s", "sthene meter per second", 1000.0, UnitSystem.MTS);
307 
308         /** erg per second. */
309         public static final Power.Unit erg_s = SI.deriveUnit("erg/s", "erg per second", 1.0E-7, UnitSystem.CGS);
310 
311         /**
312          * Create a new Power unit.
313          * @param id the id or main abbreviation of the unit
314          * @param name the full name of the unit
315          * @param scaleFactorToBaseUnit the scale factor of the unit to convert it TO the base (SI) unit
316          * @param unitSystem the unit system such as SI or IMPERIAL
317          */
318         public Unit(final String id, final String name, final double scaleFactorToBaseUnit, final UnitSystem unitSystem)
319         {
320             super(id, name, scaleFactorToBaseUnit, unitSystem);
321         }
322 
323         /**
324          * Return a derived unit for this unit, with textual abbreviation(s) and a display abbreviation.
325          * @param textualAbbreviation the textual abbreviation of the unit, which doubles as the id
326          * @param displayAbbreviation the display abbreviation of the unit
327          * @param name the full name of the unit
328          * @param scale the scale to use to convert from this unit to the standard (e.g., SI, BASE) unit
329          * @param unitSystem unit system, e.g. SI or Imperial
330          * @param siPrefix the SI Prefix of this unit
331          */
332         public Unit(final String textualAbbreviation, final String displayAbbreviation, final String name, final Scale scale,
333                 final UnitSystem unitSystem, final SIPrefix siPrefix)
334         {
335             super(textualAbbreviation, displayAbbreviation, name, scale, unitSystem, siPrefix);
336         }
337 
338         @Override
339         public SIUnit siUnit()
340         {
341             return SI_UNIT;
342         }
343 
344         @Override
345         public Unit getBaseUnit()
346         {
347             return SI;
348         }
349 
350         @Override
351         public Power ofSi(final double si, final UnitInterface<Power> displayUnit)
352         {
353             return new Power(si, (Unit) displayUnit, true);
354         }
355 
356         @Override
357         public Power.Unit deriveUnit(final String textualAbbreviation, final String displayAbbreviation, final String name,
358                 final double scaleFactor, final UnitSystem unitSystem, final SIPrefix siPrefix)
359         {
360             if (getScale() instanceof LinearScale ls)
361             {
362                 return new Power.Unit(textualAbbreviation, displayAbbreviation, name,
363                         new LinearScale(ls.getScaleFactorToBaseUnit() * scaleFactor), unitSystem, siPrefix);
364             }
365             throw new UnitRuntimeException("Only possible to derive a unit from a unit with a linear scale");
366         }
367 
368         @Override
369         public Power.Unit deriveUnit(final String abbreviation, final String name, final double scaleFactor,
370                 final UnitSystem unitSystem)
371         {
372             return (Unit) super.deriveUnit(abbreviation, name, scaleFactor, unitSystem);
373         }
374 
375     }
376 
377 }