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