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