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   * Energy is a physical quantity representing the capacity to do work, measured in joules (J).
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 Energy extends Quantity<Energy>
26  {
27      /** Constant with value zero. */
28      public static final Energy ZERO = ofSi(0.0);
29  
30      /** Constant with value one. */
31      public static final Energy ONE = ofSi(1.0);
32  
33      /** Constant with value NaN. */
34      @SuppressWarnings("checkstyle:constantname")
35      public static final Energy NaN = ofSi(Double.NaN);
36  
37      /** Constant with value POSITIVE_INFINITY. */
38      public static final Energy POSITIVE_INFINITY = ofSi(Double.POSITIVE_INFINITY);
39  
40      /** Constant with value NEGATIVE_INFINITY. */
41      public static final Energy NEGATIVE_INFINITY = ofSi(Double.NEGATIVE_INFINITY);
42  
43      /** Constant with value MAX_VALUE. */
44      public static final Energy POS_MAXVALUE = ofSi(Double.MAX_VALUE);
45  
46      /** Constant with value -MAX_VALUE. */
47      public static final Energy NEG_MAXVALUE = ofSi(-Double.MAX_VALUE);
48  
49      /** */
50      private static final long serialVersionUID = 600L;
51  
52      /**
53       * Instantiate a Energy 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 Energy(final double value, final Energy.Unit displayUnit, final boolean useSi)
59      {
60          super(value, displayUnit, useSi);
61      }
62  
63      /**
64       * Instantiate a Energy 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 Energy(final double valueInUnit, final Energy.Unit unit)
69      {
70          this(valueInUnit, unit, false);
71      }
72  
73      /**
74       * Return a Energy instance based on an SI value.
75       * @param si the si value
76       * @return the Energy instance based on an SI value
77       */
78      public static Energy ofSi(final double si)
79      {
80          return new Energy(si, Energy.Unit.SI, true);
81      }
82  
83      /**
84       * Instantiate a Energy 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 Energy instance based on an SI value with the given display unit
88       */
89      public static Energy ofSi(final double siValue, final Energy.Unit displayUnit)
90      {
91          return new Energy(siValue, displayUnit, true);
92      }
93  
94      @Override
95      public Energy instantiateSi(final double siValue, final UnitInterface<Energy> displayUnit)
96      {
97          return new Energy(siValue, (Energy.Unit) displayUnit, true);
98      }
99  
100     /**
101      * Returns a Energy 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 Energy
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 Energy valueOf(final String text)
110     {
111         return Quantity.valueOf(text, ZERO);
112     }
113 
114     /**
115      * Returns a Energy 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 Energy representation of the value in its unit
119      */
120     public static Energy of(final double valueInUnit, final Energy.Unit unit)
121     {
122         return new Energy(valueInUnit, unit);
123     }
124 
125     /**
126      * Returns a Energy 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 Energy of(final double valueInUnit, final String unitString)
134     {
135         return Quantity.of(valueInUnit, unitString, ZERO);
136     }
137 
138     @Override
139     public Energy.Unit getDisplayUnit()
140     {
141         return (Energy.Unit) super.getDisplayUnit();
142     }
143 
144     /**
145      * Calculate the division of Energy and Energy, which results in a Dimensionless quantity.
146      * @param v quantity
147      * @return quantity as a division of Energy and Energy
148      */
149     public Dimensionless divide(final Energy v)
150     {
151         return new Dimensionless(this.si() / v.si(), Unitless.BASE);
152     }
153 
154     /**
155      * Calculate the division of Energy and Force, which results in a Length scalar.
156      * @param v scalar
157      * @return scalar as a division of Energy and Force
158      */
159     public Length divide(final Force v)
160     {
161         return new Length(this.si() / v.si(), Length.Unit.SI);
162     }
163 
164     /**
165      * Calculate the division of Energy and Length, which results in a Force scalar.
166      * @param v scalar
167      * @return scalar as a division of Energy and Length
168      */
169     public Force divide(final Length v)
170     {
171         return new Force(this.si() / v.si(), Force.Unit.SI);
172     }
173 
174     /**
175      * Calculate the multiplication of Energy and LinearDensity, which results in a Force scalar.
176      * @param v scalar
177      * @return scalar as a multiplication of Energy and LinearDensity
178      */
179     public Force multiply(final LinearObjectDensity v)
180     {
181         return new Force(this.si() * v.si(), Force.Unit.SI);
182     }
183 
184     /**
185      * Calculate the division of Energy and Duration, which results in a Power scalar.
186      * @param v scalar
187      * @return scalar as a division of Energy and Duration
188      */
189     public Power divide(final Duration v)
190     {
191         return new Power(this.si() / v.si(), Power.Unit.SI);
192     }
193 
194     /**
195      * Calculate the division of Energy and Power, which results in a Duration scalar.
196      * @param v scalar
197      * @return scalar as a division of Energy and Power
198      */
199     public Duration divide(final Power v)
200     {
201         return new Duration(this.si() / v.si(), Duration.Unit.SI);
202     }
203 
204     /**
205      * Calculate the division of Energy and Volume, which results in a Pressure scalar.
206      * @param v scalar
207      * @return scalar as a division of Energy and Volume
208      */
209     public Pressure divide(final Volume v)
210     {
211         return new Pressure(this.si() / v.si(), Pressure.Unit.SI);
212     }
213 
214     /**
215      * Calculate the division of Energy and Pressure, which results in a Volume scalar.
216      * @param v scalar
217      * @return scalar as a division of Energy and Pressure
218      */
219     public Volume divide(final Pressure v)
220     {
221         return new Volume(this.si() / v.si(), Volume.Unit.SI);
222     }
223 
224     /**
225      * Calculate the multiplication of Energy and Frequency, which results in a Power scalar.
226      * @param v scalar
227      * @return scalar as a multiplication of Energy and Frequency
228      */
229     public Power multiply(final Frequency v)
230     {
231         return new Power(this.si() * v.si(), Power.Unit.SI);
232     }
233 
234     /**
235      * Calculate the division of Energy and Speed, which results in a Momentum scalar.
236      * @param v scalar
237      * @return scalar as a division of Energy and Speed
238      */
239     public Momentum divide(final Speed v)
240     {
241         return new Momentum(this.si() / v.si(), Momentum.Unit.SI);
242     }
243 
244     /**
245      * Calculate the division of Energy and Momentum, which results in a Speed scalar.
246      * @param v scalar
247      * @return scalar as a division of Energy and Momentum
248      */
249     public Speed divide(final Momentum v)
250     {
251         return new Speed(this.si() / v.si(), Speed.Unit.SI);
252     }
253 
254     /******************************************************************************************************/
255     /********************************************** UNIT CLASS ********************************************/
256     /******************************************************************************************************/
257 
258     /**
259      * Energy.Unit encodes the units of energy.
260      * <p>
261      * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
262      * See for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
263      * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
264      * @author Alexander Verbraeck
265      */
266     @SuppressWarnings("checkstyle:constantname")
267     public static class Unit extends AbstractUnit<Energy>
268     {
269         /** The dimensions of energy: kgm2/s2. */
270         public static final SIUnit SI_UNIT = SIUnit.of("kgm2/s2");
271 
272         /** Joule. */
273         public static final Energy.Unit J =
274                 new Energy.Unit("J", "J", "joule", IdentityScale.SCALE, UnitSystem.SI_DERIVED, SIPrefixes.getSiPrefix(""));
275 
276         /** The SI or BASE unit. */
277         public static final Energy.Unit SI = (Unit) J.generateSiPrefixes(false, false);
278 
279         /** microjoule. */
280         public static final Energy.Unit muJ = Units.resolve(Energy.Unit.class, "muJ");
281 
282         /** millijoule. */
283         public static final Energy.Unit mJ = Units.resolve(Energy.Unit.class, "mJ");
284 
285         /** kilojoule. */
286         public static final Energy.Unit kJ = Units.resolve(Energy.Unit.class, "kJ");
287 
288         /** megajoule. */
289         public static final Energy.Unit MJ = Units.resolve(Energy.Unit.class, "MJ");
290 
291         /** gigajoule. */
292         public static final Energy.Unit GJ = Units.resolve(Energy.Unit.class, "GJ");
293 
294         /** terajoule. */
295         public static final Energy.Unit TJ = Units.resolve(Energy.Unit.class, "TJ");
296 
297         /** petajoule. */
298         public static final Energy.Unit PJ = Units.resolve(Energy.Unit.class, "PJ");
299 
300         /** foot-pound force. */
301         public static final Energy.Unit ft_lbf = J.deriveUnit("ft.lbf", "foot pound-force",
302                 Length.Unit.CONST_FT * Mass.Unit.CONST_LB * Acceleration.Unit.CONST_GRAVITY, UnitSystem.IMPERIAL);
303 
304         /** inch-pound force. */
305         public static final Energy.Unit in_lbf = J.deriveUnit("in.lbf", "inch pound-force",
306                 Length.Unit.CONST_IN * Mass.Unit.CONST_LB * Acceleration.Unit.CONST_GRAVITY, UnitSystem.IMPERIAL);
307 
308         /** British thermal unit (ISO). */
309         public static final Energy.Unit BTU_ISO =
310                 J.deriveUnit("BTU(ISO)", "British thermal unit (ISO)", 1.0545E3, UnitSystem.IMPERIAL);
311 
312         /** British thermal unit (International Table). */
313         public static final Energy.Unit BTU_IT =
314                 J.deriveUnit("BTU(IT)", "British thermal unit (Int. Table)", 1.05505585262E3, UnitSystem.IMPERIAL);
315 
316         /** calorie (International Table). */
317         public static final Energy.Unit cal_IT = J.deriveUnit("cal(IT)", "calorie (Int. Table)", 4.1868, UnitSystem.IMPERIAL);
318 
319         /** calorie. */
320         public static final Energy.Unit cal = J.deriveUnit("cal", "calorie", 4.184, UnitSystem.OTHER);
321 
322         /** kilocalorie. */
323         public static final Energy.Unit kcal = cal.deriveUnit("kcal", "kilocalorie", 1000.0, UnitSystem.OTHER);
324 
325         /** watt hour. */
326         public static final Energy.Unit Wh = new Energy.Unit("Wh", "watt hour", 3600.0, UnitSystem.SI_DERIVED);
327 
328         /** microwatt hour. */
329         public static final Energy.Unit muWh =
330                 Wh.deriveUnit("muWh", "\u03BCWh", "microwatt hour", 1E-6, UnitSystem.SI_DERIVED, null);
331 
332         /** milliwatt hour. */
333         public static final Energy.Unit mWh = Wh.deriveUnit("mWh", "milliwatt hour", 1E-3, UnitSystem.SI_DERIVED);
334 
335         /** kilowatt hour. */
336         public static final Energy.Unit kWh = Wh.deriveUnit("kWh", "kilowatt hour", 1E3, UnitSystem.SI_DERIVED);
337 
338         /** megawatt hour. */
339         public static final Energy.Unit MWh = Wh.deriveUnit("MWh", "megawatt hour", 1E6, UnitSystem.SI_DERIVED);
340 
341         /** gigawatt hour. */
342         public static final Energy.Unit GWh = Wh.deriveUnit("GWh", "gigawatt hour", 1E9, UnitSystem.SI_DERIVED);
343 
344         /** terawatt hour. */
345         public static final Energy.Unit TWh = Wh.deriveUnit("TWh", "terawatt hour", 1E12, UnitSystem.SI_DERIVED);
346 
347         /** petawatt hour. */
348         public static final Energy.Unit PWh = Wh.deriveUnit("PWh", "petawatt hour", 1E15, UnitSystem.SI_DERIVED);
349 
350         /** electronvolt. */
351         public static final Energy.Unit eV = new Energy.Unit("eV", "electronvolt", 1.602176634E-19, UnitSystem.SI_ACCEPTED);
352 
353         /** kilo-electronvolt. */
354         public static final Energy.Unit keV = eV.deriveUnit("keV", "kiloelectronvolt", 1E3, UnitSystem.SI_ACCEPTED);
355 
356         /** mega-electronvolt. */
357         public static final Energy.Unit MeV = eV.deriveUnit("MeV", "megaelectronvolt", 1E6, UnitSystem.SI_ACCEPTED);
358 
359         /** giga-electronvolt. */
360         public static final Energy.Unit GeV = eV.deriveUnit("GeV", "gigaelectronvolt", 1E9, UnitSystem.SI_ACCEPTED);
361 
362         /** sthene-meter (mts). */
363         public static final Energy.Unit sn_m = J.deriveUnit("sn.m", "sthene meter", 1000.0, UnitSystem.MTS);
364 
365         /** erg (cgs). */
366         public static final Energy.Unit erg = J.deriveUnit("erg", "erg", 1.0E-7, UnitSystem.CGS);
367 
368         /**
369          * Create a new Energy unit.
370          * @param id the id or main abbreviation of the unit
371          * @param name the full name of the unit
372          * @param scaleFactorToBaseUnit the scale factor of the unit to convert it TO the base (SI) unit
373          * @param unitSystem the unit system such as SI or IMPERIAL
374          */
375         public Unit(final String id, final String name, final double scaleFactorToBaseUnit, final UnitSystem unitSystem)
376         {
377             super(id, name, scaleFactorToBaseUnit, unitSystem);
378         }
379 
380         /**
381          * Return a derived unit for this unit, with textual abbreviation(s) and a display abbreviation.
382          * @param textualAbbreviation the textual abbreviation of the unit, which doubles as the id
383          * @param displayAbbreviation the display abbreviation of the unit
384          * @param name the full name of the unit
385          * @param scale the scale to use to convert from this unit to the standard (e.g., SI, BASE) unit
386          * @param unitSystem unit system, e.g. SI or Imperial
387          * @param siPrefix the SI Prefix of this unit
388          */
389         public Unit(final String textualAbbreviation, final String displayAbbreviation, final String name, final Scale scale,
390                 final UnitSystem unitSystem, final SIPrefix siPrefix)
391         {
392             super(textualAbbreviation, displayAbbreviation, name, scale, unitSystem, siPrefix);
393         }
394 
395         @Override
396         public SIUnit siUnit()
397         {
398             return SI_UNIT;
399         }
400 
401         @Override
402         public Unit getBaseUnit()
403         {
404             return SI;
405         }
406 
407         @Override
408         public Energy ofSi(final double si, final UnitInterface<Energy> displayUnit)
409         {
410             return new Energy(si, (Unit) displayUnit, true);
411         }
412 
413         @Override
414         public Energy.Unit deriveUnit(final String textualAbbreviation, final String displayAbbreviation, final String name,
415                 final double scaleFactor, final UnitSystem unitSystem, final SIPrefix siPrefix)
416         {
417             if (getScale() instanceof LinearScale ls)
418             {
419                 return new Energy.Unit(textualAbbreviation, displayAbbreviation, name,
420                         new LinearScale(ls.getScaleFactorToBaseUnit() * scaleFactor), unitSystem, siPrefix);
421             }
422             throw new UnitRuntimeException("Only possible to derive a unit from a unit with a linear scale");
423         }
424 
425         @Override
426         public Energy.Unit deriveUnit(final String abbreviation, final String name, final double scaleFactor,
427                 final UnitSystem unitSystem)
428         {
429             return (Unit) super.deriveUnit(abbreviation, name, scaleFactor, unitSystem);
430         }
431 
432     }
433 
434 }