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.UnitRuntimeException;
6   import org.djunits.unit.Unitless;
7   import org.djunits.unit.Units;
8   import org.djunits.unit.scale.LinearScale;
9   import org.djunits.unit.scale.Scale;
10  import org.djunits.unit.si.SIUnit;
11  import org.djunits.unit.system.UnitSystem;
12  
13  /**
14   * Acceleration is the rate of change of velocity over time, measured in meters per second squared (m/s2).
15   * <p>
16   * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
17   * for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
18   * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
19   * @author Alexander Verbraeck
20   */
21  public class Acceleration extends Quantity<Acceleration, Acceleration.Unit>
22  {
23      /** Constant with value zero. */
24      public static final Acceleration ZERO = Acceleration.ofSi(0.0);
25  
26      /** Constant with value one. */
27      public static final Acceleration ONE = Acceleration.ofSi(1.0);
28  
29      /** Constant with value NaN. */
30      @SuppressWarnings("checkstyle:constantname")
31      public static final Acceleration NaN = Acceleration.ofSi(Double.NaN);
32  
33      /** Constant with value POSITIVE_INFINITY. */
34      public static final Acceleration POSITIVE_INFINITY = Acceleration.ofSi(Double.POSITIVE_INFINITY);
35  
36      /** Constant with value NEGATIVE_INFINITY. */
37      public static final Acceleration NEGATIVE_INFINITY = Acceleration.ofSi(Double.NEGATIVE_INFINITY);
38  
39      /** Constant with value MAX_VALUE. */
40      public static final Acceleration POS_MAXVALUE = Acceleration.ofSi(Double.MAX_VALUE);
41  
42      /** Constant with value -MAX_VALUE. */
43      public static final Acceleration NEG_MAXVALUE = Acceleration.ofSi(-Double.MAX_VALUE);
44  
45      /** */
46      private static final long serialVersionUID = 600L;
47  
48      /**
49       * Instantiate a Acceleration quantity with a unit.
50       * @param value the value, expressed in the unit
51       * @param unit the unit in which the value is expressed
52       */
53      public Acceleration(final double value, final Acceleration.Unit unit)
54      {
55          super(value, unit);
56      }
57  
58      /**
59       * Instantiate a Acceleration quantity with a unit, expressed as a String.
60       * @param value the value, expressed in the unit
61       * @param abbreviation the String abbreviation of the unit in which the value is expressed
62       */
63      public Acceleration(final double value, final String abbreviation)
64      {
65          this(value, Units.resolve(Acceleration.Unit.class, abbreviation));
66      }
67  
68      /**
69       * Construct Acceleration quantity.
70       * @param value Scalar from which to construct this instance
71       */
72      public Acceleration(final Acceleration value)
73      {
74          super(value.si(), Acceleration.Unit.SI);
75          setDisplayUnit(value.getDisplayUnit());
76      }
77  
78      /**
79       * Return a Acceleration instance based on an SI value.
80       * @param si the si value
81       * @return the Acceleration instance based on an SI value
82       */
83      public static Acceleration ofSi(final double si)
84      {
85          return new Acceleration(si, Acceleration.Unit.SI);
86      }
87  
88      @Override
89      public Acceleration instantiate(final double si)
90      {
91          return ofSi(si);
92      }
93  
94      @Override
95      public SIUnit siUnit()
96      {
97          return Acceleration.Unit.SI_UNIT;
98      }
99  
100     /**
101      * Returns a Acceleration representation of a textual representation of a value with a unit. The String representation that
102      * can be parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are
103      * allowed, but not required, between the value and the unit.
104      * @param text the textual representation to parse into a Acceleration
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 Acceleration valueOf(final String text)
110     {
111         return Quantity.valueOf(text, ZERO);
112     }
113 
114     /**
115      * Returns a Acceleration based on a value and the textual representation of the unit, which can be localized.
116      * @param value the value to use
117      * @param unitString the textual representation of the unit
118      * @return the Scalar representation of the value in its unit
119      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
120      * @throws NullPointerException when the unitString argument is null
121      */
122     public static Acceleration of(final double value, final String unitString)
123     {
124         return Quantity.of(value, unitString, ZERO);
125     }
126 
127     /**
128      * Calculate the division of Acceleration and Acceleration, which results in a Dimensionless scalar.
129      * @param v scalar
130      * @return scalar as a division of Acceleration and Acceleration
131      */
132     public final Dimensionless divide(final Acceleration v)
133     {
134         return new Dimensionless(this.si() / v.si(), Unitless.BASE);
135     }
136 
137     /**
138      * Calculate the multiplication of Acceleration and Mass, which results in a Force scalar.
139      * @param v scalar
140      * @return scalar as a multiplication of Acceleration and Mass
141      */
142     public final Force multiply(final Mass v)
143     {
144         return new Force(this.si() * v.si(), Force.Unit.SI);
145     }
146 
147     /**
148      * Calculate the multiplication of Acceleration and Duration, which results in a Speed scalar.
149      * @param v scalar
150      * @return scalar as a multiplication of Acceleration and Duration
151      */
152     public final Speed multiply(final Duration v)
153     {
154         return new Speed(this.si() * v.si(), Speed.Unit.SI);
155     }
156 
157     /**
158      * Calculate the division of Acceleration and Frequency, which results in a Speed scalar.
159      * @param v scalar
160      * @return scalar as a division of Acceleration and Frequency
161      */
162     public final Speed divide(final Frequency v)
163     {
164         return new Speed(this.si() / v.si(), Speed.Unit.SI);
165     }
166 
167     /**
168      * Calculate the division of Acceleration and Speed, which results in a Frequency scalar.
169      * @param v scalar
170      * @return scalar as a division of Acceleration and Speed
171      */
172     public final Frequency divide(final Speed v)
173     {
174         return new Frequency(this.si() / v.si(), Frequency.Unit.SI);
175     }
176 
177     /**
178      * Calculate the multiplication of Acceleration and Momentum, which results in a Power scalar.
179      * @param v scalar
180      * @return scalar as a multiplication of Acceleration and Momentum
181      */
182     public final Power multiply(final Momentum v)
183     {
184         return new Power(this.si() * v.si(), Power.Unit.SI);
185     }
186 
187     /******************************************************************************************************/
188     /********************************************** UNIT CLASS ********************************************/
189     /******************************************************************************************************/
190 
191     /**
192      * Acceleration.Unit encodes the units of acceleration.
193      * <p>
194      * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
195      * See for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
196      * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
197      * @author Alexander Verbraeck
198      */
199     @SuppressWarnings("checkstyle:constantname")
200     public static class Unit extends AbstractUnit<Acceleration.Unit, Acceleration>
201     {
202         /** Constant for standard gravity. */
203         public static final double CONST_GRAVITY = 9.80665;
204 
205         /** The dimensions of Acceleration: m/s2. */
206         public static final SIUnit SI_UNIT = SIUnit.of("m/s2");
207 
208         /** m/s2. */
209         public static final Acceleration.Unit m_s2 =
210                 new Acceleration.Unit("m/s2", "meter per second squared", 1.0, UnitSystem.SI_DERIVED);
211 
212         /** The SI or BASE unit. */
213         public static final Acceleration.Unit SI = m_s2;
214 
215         /** km/h2. */
216         public static final Acceleration.Unit km_h2 = new Acceleration.Unit("km/h2", "kilometer per hour squared",
217                 1000.0 / (3600.0 * 3600.0), UnitSystem.SI_ACCEPTED);
218 
219         /** ft/s2. */
220         public static final Acceleration.Unit ft_s2 =
221                 new Acceleration.Unit("ft/s2", "foot per second squared", Length.Unit.CONST_FT, UnitSystem.IMPERIAL);
222 
223         /** in/s2. */
224         public static final Acceleration.Unit in_s2 =
225                 new Acceleration.Unit("in/s2", "inch per second squared", Length.Unit.CONST_IN, UnitSystem.IMPERIAL);
226 
227         /** mi/h2. */
228         public static final Acceleration.Unit mi_h2 = new Acceleration.Unit("mi/h2", "mile per hour squared",
229                 Length.Unit.CONST_MI / (3600.0 * 3600.0), UnitSystem.IMPERIAL);
230 
231         /** mi/s2. */
232         public static final Acceleration.Unit mi_s2 =
233                 new Acceleration.Unit("mi/s2", "mile per second squared", Length.Unit.CONST_MI, UnitSystem.IMPERIAL);
234 
235         /** kt/s = Nautical Mile / h / s. */
236         public static final Acceleration.Unit kt_s =
237                 new Acceleration.Unit("kt/s", "knot per second", Length.Unit.CONST_NM / 3600.0, UnitSystem.OTHER);
238 
239         /** mi/h/s. */
240         public static final Acceleration.Unit mi_h_s =
241                 new Acceleration.Unit("mi/h/s", "mile per hour per second", Length.Unit.CONST_MI / 3600.0, UnitSystem.IMPERIAL);
242 
243         /** The standard gravity. */
244         public static final Acceleration.Unit g =
245                 new Acceleration.Unit("g", "standard gravity", CONST_GRAVITY, UnitSystem.OTHER);
246 
247         /** Gal or cm/s. */
248         public static final Acceleration.Unit Gal = new Acceleration.Unit("Gal", "gal", 0.01, UnitSystem.CGS);
249 
250         /**
251          * Create a new Acceleration unit.
252          * @param id the id or main abbreviation of the unit
253          * @param name the full name of the unit
254          * @param scaleFactorToBaseUnit the scale factor of the unit to convert it TO the base (SI) unit
255          * @param unitSystem the unit system such as SI or IMPERIAL
256          */
257         public Unit(final String id, final String name, final double scaleFactorToBaseUnit, final UnitSystem unitSystem)
258         {
259             super(id, name, new LinearScale(scaleFactorToBaseUnit), unitSystem);
260         }
261 
262         /**
263          * Return a derived unit for this unit, with textual abbreviation(s) and a display abbreviation.
264          * @param textualAbbreviation the textual abbreviation of the unit, which doubles as the id
265          * @param displayAbbreviation the display abbreviation of the unit
266          * @param name the full name of the unit
267          * @param scale the scale to use to convert between this unit and the standard (e.g., SI, BASE) unit
268          * @param unitSystem unit system, e.g. SI or Imperial
269          */
270         public Unit(final String textualAbbreviation, final String displayAbbreviation, final String name, final Scale scale,
271                 final UnitSystem unitSystem)
272         {
273             super(textualAbbreviation, displayAbbreviation, name, scale, unitSystem);
274         }
275 
276         @Override
277         public SIUnit siUnit()
278         {
279             return SI_UNIT;
280         }
281 
282         @Override
283         public Unit getBaseUnit()
284         {
285             return SI;
286         }
287 
288         @Override
289         public Acceleration ofSi(final double si)
290         {
291             return Acceleration.ofSi(si);
292         }
293 
294         @Override
295         public Unit deriveUnit(final String textualAbbreviation, final String displayAbbreviation, final String name,
296                 final double scaleFactor, final UnitSystem unitSystem)
297         {
298             if (getScale() instanceof LinearScale ls)
299             {
300                 return new Acceleration.Unit(textualAbbreviation, displayAbbreviation, name,
301                         new LinearScale(ls.getScaleFactorToBaseUnit() * scaleFactor), unitSystem);
302             }
303             throw new UnitRuntimeException("Only possible to derive a unit from a unit with a linear scale");
304         }
305 
306     }
307 }